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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 - 3: DIGI:OUTP command
  • 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: 18 Oct 2022 8:05 PM Date Created
  • Views 1715 views
  • Likes 7 likes
  • Comments 8 comments
  • raspberry
  • pico_freertos_smp
  • pico_freertos_scpi_switch
  • pico
  • freertos
  • scpi
Related
Recommended

mini project: PICO-PI programmable Lab Switch - 3: DIGI:OUTP command

Jan Cumps
Jan Cumps
18 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 this post: add the SCPI command to switch an output (the LED in this demo): DIGItal:OUTPut# TRUE|FALSE.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Implement the SCPI command

In this post, we'll finally have a working design. Because all of the preparation work is done in the previous posts (set up FreeRTOS, UART, SCPI parser), the last step is very simple.
This is the command we'll support:

DIGItal:OUTPut# TRUE|FALSE

Where:

  • DIGItal:OUTPut (or DIGI:OUTP) is the command
  • # is the output you want to control.
    In this version there's one output with index 0: the on-board LED. See the Homework section later
  • TRUE|FALSE is the value. valid options are either 1|0 or ON|OFF

example:

DIGI:OUTP0 1

The command register:

   /* custom commands for the switch */
    {.pattern = "DIGItal:OUTPut#", .callback = SCPI_DigitalOutput,},

Array (size 1 Slight smile) of pins that can be controlled. The values are Pico GPIO pin numbers (the led is pin 25, but I use the constant that the Pico SDK defined):

#include "hardware/gpio.h"

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

The handler of the DIGI:OUTP command:

static scpi_result_t SCPI_DigitalOutput(scpi_t * context) {


  scpi_bool_t param1;
  int32_t numbers[1];

  // retrieve the output index
  SCPI_CommandNumbers(context, numbers, 1, 0);
  if (! ((numbers[0] > -1) && (numbers[0] < sizeof(pins)/sizeof(pins[0])))) {
    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;
  }

  gpio_put(pins[numbers[0]], param1 ? 1 : 0);

  return SCPI_RES_OK;
}

This one has three sections:

  • parse parameter 1: the index, 0 based. With validation  if it's in the array range
    note: in SCPI lingo this is actually a command number, not a parameter. This is the way to indicate what channel to operate on.
  • parse parameter 2: the state, ON or OFF
  • Set the GPIO pin that sits on that index to the state, using the Pico GPIO API

That is it. The parser lib makes it really easy to register and implement SCPI commands.
You can test this by sending the command DIGI:OUTP0 1 or DIGI:OUTP0 0 to the USB port. The led will turn on or off.

If you typed an error, you can always find that back: SYST:ERR:COUN? shows how many errors are stacked. SYST:ERR? pops the last error of the error stack.

Homework

These are easy-to-do improvements, that you can do yourself:

  • 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.
  • still doable: write a command that returns the state of an output
    hint: {.pattern = "DIGItal:OUTPut#?", .callback = SCPI_DigitalOutputQ,},

project sources (VSCode compatible):

freertos_scpi_switch_20221019.zip

binary (uf2, you can use this for drag-and-drop programming). There's one if you are using UART (pins 0 for TX, 1 for RX - the usual setup when a picoprobe is in use) and a version that uses the Pico's USB.

scpi_switch_uart_and_usb_uf2.zip

Show all blog posts

  • Sign in to reply
Parents
  • shabaz
    shabaz over 3 years ago

    Awesome, that was a 30-second job to install it and test it! 

    image

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

    Awesome, that was a 30-second job to install it and test it! 

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
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