Monday, 20 June 2016

How to use STM32 GPIO

Firstly, add the header files. Configure the clocks if needed.

This header is for STM32F1 change it to your own.

Secondly, configure the GPIOs. This is for PA3 set to output.

To set pin low.

To set pin high.

To toggle pin.

To read pin.

How to use STM32 ADC

Firstly, add the header files. Configure the clocks if needed.

This header is for STM32F1 change it to your own.

Configure the ADC.
Set hadc.Init.ContinuousConvMode to ENABLE and hadc.Init.EOCSelection to DISABLE for continuous sampling.

Or equivalently on STM32CubeMX.

Simple continuous sampling blinking. hadc is the structure of ADC. Starts ADC with HAL_ADC_Start. Then polls all the conversions with HAL_ADC_PollForConversion. The data retrieved with HAL_ADC_GetValue. digital value =((Analog input voltage)/(reference voltage high- reference voltage low)) * (2n - 1) where n = number of bits of ADC digital output

Sunday, 12 June 2016


I had a lot of trouble using USB CDC. So I decided to create a small library. The library uses STM32 HAL and the USB middle-ware. The code is for STM32L0. However it can be easily modified for other STM32 lines.

First add VCP.h

Then add VCP.c

Modify usbd_cdc_if.c. I also changed APP_RX_DATA_SIZE and APP_RX_DATA_SIZE to 256

Finally change main.c . Include the VCP.h .

The VCP_read checks if the FIFO has data. If it has data then it sends the data back to the computer using VCP_write.

To see interaction use

picocom -b 9600 /dev/ttyACM0

Then enter some data into the VCP.

 echo "hello world" > /dev/ttyACM0

The response

 Terminal ready
stack=0,i=12,data=hello world

Saturday, 11 June 2016

How to use OpenSTM32

See How to use STM32CubeMX to generate code.

Open the IDE.

Import the project. File > Import

Select Existing Projects into Workspace .

Select the folder.

Now the project is imported. Add some code.

To debug/flash follow this. Project > Target > Program chip

Select the executable.

Configure the debugger. Select ST-LinkV2-1 for built in debugger. Select ST-LinkV2 for external debugger.

Debug > Debug Configurations

Select your executable under Ac6 STM32 Debugging. Then select Debugger. Click on Manual spec in Script. Then configure the debugger. 
Hit Apply and then Debug.

 OpenSTM32 should flash the chip and enter debug mode.

Friday, 10 June 2016

How to use Unity for testing code

Unit tests are important for testing code. Unity is a library for testing C code.

To use unity add this header.

The main function starts Unity. Runs the test_stuff(). Then stops Unity.

The function for testing code. This tests if the condition is true.

This tests if the 32 bit int is equal to 10.

Function to test null pointers. If the pointer is null then it will fail and give a message.

How to use STM32CubeMX


Open STM32CubeMX and click New project.

Select the MCU.

Select configuration and enable the middle wares (e.g. FreeRTOS).

Set pin configurations and clocks in pinouts.

Set clock configurations.

Project > Generate code. Name project and select directory. Select SW4STM32 for OpenSTM32 IDE.

How to use GCC and make

GCC is the main compiler for C.

To compile a C file.

gcc -o hello hello.c

Running the executable


Makefiles is used for building the whole project. This is an example file.

all: hello

hello: hello.o
     gcc -o hello hello.o

hello.o: hello.c
     gcc -c hello.c
     rm hello.o hello

Use this to build everything into a executable.


Use this to clean the project.

make clean

Monday, 6 June 2016

Plans for Project Popsicle Stick v2.0

Well I failed to program the STM32 with STLinkV2. Not matter what I do, the STLinkV2 doesn't detect the STM32.
List of things checked:
  • All VDD pins are 3.3V
  • All ground pins are grounded
  • There are no shorts to ground
  • Ripple in VDD is minimal
  • BOOT0 is grounded
  • NSRT is connect to VDD
  • STLinkV2 is connected using SWD
  • All PCB traces are continuous
  • Power for STM32 is 3.63 mW (1.1 mA at 3.3V). Seems reasonable.
  • STM32 is in the correct orientation
  • STLinkV2 works on other STM32
  • All STM32 pins were checked
  • Supplier didn't give a faulty STM32 as I un-soldered it and replaced it with a new one

I am thinking about designing version 2.0 of Project Popsicle Stick. However, I have trouble deciding how to pick a new ICE40 chip. The ICE40LP is pretty cost effective for the amount of LUTs. The downside is that I need a 4 layer board. That adds complexity and cost. iCE40-HX4K-TQ144 doesn't require a 4 layer board. The downside is that I need a bigger board and iCE40-HX4K-TQ144 has less LUTs. Anyone have advice?
I also created a poll if anyone want to help me decide.