[September 1, 2024] I am currently migrating my blog away from WordPress. I am trying to keep all url's and content the same. This is a work in progress. The old WordPress website is archived and available at https://archive.bartslinger.com. This is all in markdown now, which is awesome. Maybe I blog about that later.
Getting to Blinky with CX-10
As with all embedded systems, getting to blinky is the first thing you want. Please refer to the previous post on preparing the hardware to have a programmable setup with the flash memory of the processor cleared.
Libopencm3
Most of the examples found on the internet rely on the default STM32 libraries. For this project, I am going to use libopencm3, which is an open-source version. To download libopencm3 and examples, download the following git repository:
git clone https://github.com/libopencm3/libopencm3-examples.git
The example we want to use (and modify a little) is in this directory:
libopencm3-examples/examples/stm32/f0/stm32f0-discovery/miniblink/
Modification of miniblink code
The default blinky code will not work on the CX-10, since the LED is at a different pin. But even more important, the CX-10 [blue pcb] has an onboard voltage regulator which needs to be enabled by the processor itself, otherwise the processor will shutdown! This feature is useful later because thereby you can switch off power if the battery voltage level drops below a certain value. Change the miniblink.c file to the following:
/* License removed for clarity */
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
/* Blue LEDs connected at PB2 */
#define PORT_LED GPIOB
#define PIN_LED GPIO2
/* Linear Dropout Regulator connected at PA5 */
#define PORT_LDO GPIOA
#define PIN_LDO GPIO5
static void gpio_setup(void)
{
/* Enable GPIOA and GPIOB clock. */
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
/* Configure pins as output. */
gpio_mode_setup(PORT_LDO, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LDO);
gpio_mode_setup(PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED);
/* Set LDO pin HIGH to enable the voltage regulator which supplies power to the processor */
gpio_set(GPIOA, GPIO5);
}
int main(void)
{
int i;
gpio_setup();
/* Blink the LED (PC8) on the board. */
while (1) {
/* Using API function gpio_toggle(): */
gpio_toggle(PORT_LED, PIN_LED); /* LED on/off */
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
__asm__("nop");
}
}
return 0;
}
Now, open the terminal and navigate to this directory. Connect the Discovery Board with the CX-10 connected to the SWD header. Make sure to apply external power as explained in the previous post. You also need to download and install stlink first. Upload with the following command:
make miniblink.stlink-flash
Now, the blue LEDs should be blinking. If you remove the external power and use battery power, it should also be blinking.