element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Test & Tools
  • Technologies
  • More
Test & Tools
Blog mini project: PICO-PI programmable Lab Switch - 4: Your Homework - extra pins
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Test & Tools to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 20 Oct 2022 7:02 PM Date Created
  • Views 1312 views
  • Likes 7 likes
  • Comments 8 comments
  • pico_freertos_scpi_switch
Related
Recommended

mini project: PICO-PI programmable Lab Switch - 4: Your Homework - extra pins

Jan Cumps
Jan Cumps
20 Oct 2022

In this series, I design a programmable lab switch based on RP2040, that can turn a set of signals or supplies on or off. Over USB. SCPI compatible.
In the previous post I gave you homework, but you didn't do it Slight smile. So I'll pick up task 1 and 2:

  • very easy: extend the array of pins. Currently it's an array of 1 element (the LED), but you can define many. All GPIOs if you want.
    The input validation will automatically adapt to the array size and only stack an error if you pass an index out of array range in the command
    Don't forget to initialise these pins as outputs in the main() function
  • very easy: write code that initialises the pins automatically: loop over all array items and set them as outputs.

Task 1: extend the array of pins

Currently it's an array of 1 element (the LED), but you can define many. All GPIOs if you want.
The input validation will automatically adapt to the array size and only stack an error if you pass an index out of array range in the command
Don't forget to initialise these pins as outputs in the main() function

This is easy. I had prepared the code to deal with more than one pin. It originally only had a single definition: the on-board LED. But I had already (visionary :) ) placed it in an array:

// supported pins
uint pins[] = {PICO_DEFAULT_LED_PIN};

Let's add another pin. GP22 on pin 29 of the Pico. Why: because it's a pure GPIO pin, no other function. You can add any number of pins more if you want (homework).

uint pins[] = {PICO_DEFAULT_LED_PIN, 22};

Let's also move the definition out of the scpi sources, and place it in its own source file gpio/gpio_utils.c, where we also will put the code of task 2.

#include "hardware/gpio.h"

#include "gpio_utils.h"

// supported pins
uint pins[] = {PICO_DEFAULT_LED_PIN, 22};


uint32_t pinCount() {
    return sizeof(pins)/sizeof(pins[0]);
}

void setPinAt(uint32_t index, bool on) {
    gpio_put(pins[index], on);
}

You can see that I've taken the opportunity to add 2 helper functions, so that the scpi code doesn't have to know how many pins we have and how we store them. Here's the corresponding header, gpio/gpio_utils.h I created a corresponding header file:

#ifndef _GPIO_UTILS_H
#define _GPIO_UTILS_H


uint32_t pinCount();
void setPinAt(uint32_t index, bool on);

#endif // _GPIO_UTILS_H

The code in the scpi parser now becomes very easy:

#include "gpio_utils.h"


// ...

  // retrieve the output index
  SCPI_CommandNumbers(context, numbers, 1, 0);
  if (! ((numbers[0] > -1) && (numbers[0] < pinCount()))) {
    SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX);
    return SCPI_RES_ERR;
  }

  /* read first parameter if present */
  if (!SCPI_ParamBool(context, &param1, TRUE)) {
    return SCPI_RES_ERR;
  }

  setPinAt(numbers[0], param1 ? true : false);
  
// ...  

You see the use of pinCount() and setPinAt() helpers here. Cleaner, simpler to maintain, and change the set of supported pins.

Task 2: write code that initialises the pins automatically

loop over all array items and set them as outputs.

In the original code, I configured the pin that I used in the main file. Now that we have the utility files, let's create an initialisation function that will work for 1 pin and many pins:

void initPins() {
    for (uint32_t i = 0; i < pinCount(); i++) {
        gpio_init(pins[i]);
        gpio_set_dir(pins[i], 1);
        gpio_put(pins[i], 0);
    }
}

I've put it in the header file too, so that it can be called from main. 

In the initialisation code, you get this:

    // ...
    stdio_init_all();
    initPins();
    initUART();
    // ...

Show all blog posts

  • Sign in to reply
  • shabaz
    shabaz over 3 years ago in reply to Jan Cumps

    Just saw these messages. (Comment notificarions were off : (

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to shabaz

    If you download FreeRTOS with this URL: https://github.com/FreeRTOS/FreeRTOS-Kernel/archive/refs/heads/smp.zip, you hget the SMP version. It can run tasks on both RP2040 cores.

    (Not relevant for this exercise that runs 1 task. Very interesting though)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to shabaz

    ... in the last post, I attached latest sources, including the USB poll task and the extendible # of output pins.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to shabaz

    Nice!

    The reason why I don't set the actual paths in the CMakeLists.txt files, but use (env) variables: It allows to check the code into version control, without tying other users to my directory structure.
    At the cost of convenience. The developer now has to set those variables somewhere. In the OS, or in the User Settings of the IDE.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 3 years ago in reply to shabaz

    (Can't edit): Once you are happy with the code, I'll write up these steps with screenshots in a blog, so that it doesn't get lost in comments. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube