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 1316 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
Parents
  • shabaz
    shabaz over 3 years ago

    I built your code. (the version that uses UART from the previous blog), it works well : ) 

    For anyone interested in doing it with CLion (I didn't use VS Code), the steps that worked for me are (some of these steps can be replaced with environmental variable changes if desired):

    0. Install CLion and the ARM toolchain and the Pico SDK. The steps are here: /products/raspberry-pi/b/blog/posts/using-clion-for-easier-coding-with-pi-pico-and-c-c 

    1.Create a Pico development folder, e.g. C:\development\pico

    2. In that folder place the pico programmable lab switch source code, i.e. unzip the freertos_scpi_switch.zip so the path to its contents is C:\development\pico\freertos_scpi_switch

    3. Download FreeRTOS, I downloaded the full FreeRTOSv202112.00.zip from https://www.freertos.org/a00104.html

    4. Unzip it. I did it in th pico folder too, although you could put it outside that folder if you wish to use it with other microcontrollers too. My path was C:\development\pico\FreeRTOSv202112.00 

    5. Download the scpi-parser source zip file from https://github.com/j123b567/scpi-parser/releases

    6. I extracted the scpi-parser zip file to the pico library (you could extract it elsewhere if you wish to use it with other microcontrollers too). My path was C:\development\pico\scpi-parser-2.2

    7. In the freertos_scpi_switch\CMakeLists.txt file, I did search-replace to change the text $ENV{SCPI_LIB_PATH} with ../scpi-parser-2.2/libscpi

    8. In the C:\development\freertos_scpi_switch\FreeRTOS_Kernel_import.cmake file I added a line near the top of the file:

    set(FREERTOS_KERNEL_PATH "C:/development/pico/FreeRTOSv202112.00/FreeRTOS/Source")

    9. I opened the freertos_scpi_switch folder project using CLion. As part of its Open Project Wizard, it brings up a window where one line needs to be typed in this box:

    image

    The line to type is PICO_SDK_PATH=c:\development\pico\pico-sdk  (this assumed you have previously installed the SDK there as discussed in the blog in step 0).

    10. Click the hammer icon to build the code. CLion created a folder and the .uf2 suffix executable was available in C:\development\pico\freertos_scpi_switch\cmake-build-debug

    11. The executable can be uploaded onto the Pico by first holding down the BOOTSEL button and plugging in the USB connection into the PC, and then dropping the .uf2 suffix file into the USB drive letter that appears. There are a couple of steps with CLion to enable PicoProbe if that's preferred. I can write up those steps if anyone wants help with that.

    • 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
Comment
  • 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
Children
  • 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
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