Introduction:
This blog is not part of the actual project. This is something I came across while learning labview with pi pico. So, I wanted to share it.
In this blog, I will share the steps I followed to create a custom pi pico firmware image with a custom SCPI command to enable led control on the pico.
The Prerequisites are:
- pico sdk must be installed
- pico_scpi_usbtmc_labtool must be cloned
- pico_scpi_labtool_labview_driver should be cloned
- pi pico must be setup
You can follow this blog to setup your pi pico with labview.
Building custom Firmware:
I went through the blink example in pico-sdk & the gpio_utils.c & gpio_utils.h files in the pico_scpi_usbtmc_labtool repo to understand how the led can be accessed. In the blink example, there is a function gpio_put which is being used to control the led.
The same function I have to use in pico_scpi_usbtmc_labtool and create a firmware image from it.
So, inside the pico_scpi_usbtmc_labtool/source folder, I created a new folder led and created two files led_utils.c & led_utils.h inside that.
led_utils.c:
#include "hardware/gpio.h"
#include "led_utils.h"
void initLED() {
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, true);
}
void setLED(uint32_t led_pin, bool on) {
gpio_put(led_pin, on);
}
scpi_result_t SCPI_LEDOutput(scpi_t * context) {
const uint32_t LED_PIN = PICO_DEFAULT_LED_PIN;
scpi_bool_t param1;
/* read first parameter if present */
if (!SCPI_ParamBool(context, ¶m1, TRUE)) {
return SCPI_RES_ERR;
}
setLED(LED_PIN, param1 ? true : false);
return SCPI_RES_OK;
}
initLED() - To initialize the LED
setLED() - To turn the LED on/off
SCPI_LEDOutput() - Gets called whenever a particular SCPI command is called. Based on the value of the parameter with the SCPI command, it will turn on/off the LED.
led_utils.h:
#ifndef _LED_UTILS_H
#define _LED_UTILS_H
#include "scpi/scpi.h"
#define INSTRUMENT_LED_OUTP_COMMANDS \
{.pattern = "LED:OUTPut", .callback = SCPI_LEDOutput,},
void initLED();
void setLED(uint32_t index, bool on);
scpi_result_t SCPI_LEDOutput(scpi_t * context);
#endif // _LED_UTILS_H
Contains function declarations and a macro that combines a SCPI command pattern with a function callback.
Now we add this macro to the SCPI command list in the file: pico_scpi_usbtmc_labtool/source/scpi/scpi-def.c
And add the initLED() function in the initInstrument() function in the same file.
Include the led_utils.h file in the same file.
Now in the pico_scpi_usbtmc_labtool/CMakeLists.txt file, add the led_utils.c file as an executable and the led folder as the target directory.
Now build the project (follow this blog for build process) and load the .uf2 file generated onto the pico.
Making a VI:
We can create the VI similar to the digital out VI. The only difference will be the SCPI command string (Keep it in mind to connect this command string to the "format string" port of the format into string block and not to the initial string port. See this issue). Now, we can add this SubVI to other VIs and control the pico LED.
Controlling LED:
Now we can control the pico LED with the below VI.
The pico led is used by the USB code also. So, its not advised to use it for general purposes, but for demonstration, I have used it here.