Table of Contents
STM32 Notes
Embedded Wednesdays: Getting Started in Embedded Systems — Embedded
Code
CubeMX may duplicate c files in C_SOURCES variable they must be removed of you will get multiple declaration errors in linking
HAL – Hardware abstraction layer, abstracts hardware commands
LL – TBD
Delay – HAL_Delay( 500);
* Stop for 1/2 second *
GPIO
Port initialization
/* Struct options are defined in stm32f1xx_hal_gpio.h */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
The following functions are in format Port, Pin
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
– Toggle Pin state
GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOA, BUTTINPIN)
– Read Pin status
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState )
– Write Specific state to Pin
PinState = GPIO_PIN_RESET, GPIO_PIN_SET
UART
both functions should be added un “USER CODE BEGIN 4”
Add following code to allow printf() to print out UART
int _write(int file, char *outgoing, int len) {
HAL_UART_Transmit(&huart3, outgoing, len, 100);
return len;
}
This code allows ch = getchar();
to work,
int _read(int file, char *result, size_t len) {
HAL_StatusTypeDef status;
int retcode = 0;
if (len != 0) {
status = HAL_UART_Receive( &huart3, (uint8_t *) result, len, HAL_MAX_DELAY);
if (status == HAL_OK) {
retcode = len;
} else {
retcode = -1;
}
}
return( retcode);
}
also add setvbuf(stdin, NULL, _IONBF, 0);
before while loop to tune off 1k buffer
i2c
I2C_HandleTypeDef *hi2c
uint8_t data[6];
Shift Address by 1 leaving room for read / write bit
status = HAL_I2C_Master_Receive(hi2c, HMC5883LADDRESS<<1, &data, 6, HMC5883LTIMEOUT);
status = HAL_I2C_Master_Transmit(hi2c, HMC5883LADDRESS<<1, &returntofirstvalue, 1, HMC5883LTIMEOUT);
Programer and Debug
Memory locations
0x2000000 – RAM
0x8000000 – Flash
Bluepill programing
- Set the ‘boot 0′ pin/jumper high (1), and keep ‘boot 1’ low (0)
- Upload code
- For normal use, set both boot pins low (0)
- Reset board
st-link utility
GitHub – texane/stlink: stm32 discovery line linux programmer
Programming STM32 Microcontroller Flash in Linux
st-flash write myflash.bin 0x8000000
– write code to flash
st-util
– start debugging
./st-util - usage:
-h, --help Print this help
-vXX, --verbose=XX Specify a specific verbosity level (0..99)
-v, --verbose Specify generally verbose logging
-s X, --stlink_version=X
Choose what version of stlink to use, (defaults to 2)
-1, --stlinkv1 Force stlink version 1
-p 4242, --listen_port=1234
Set the gdb server listen port. (default port: 4242)
-m, --multi
Set gdb server to extended mode.
st-util will continue listening for connections after disconnect.
-n, --no-reset
Do not reset board on connection.
OpenOCD
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg
telnet localhost 4444
GoJimmyPi: Using OpenOCD and GDB
Simulation
IDEs
STM32CubeMX – STM32Cube initialization code generator – STMicroelectronics
SW4STM32 – System Workbench for STM32: free IDE on Windows, Linux and OS X – STMicroelectronics
The System Workbench toolchain, called SW4STM32, is a free multi-OS software development environment based on Eclipse, which supports the full range of STM32 microcontrollers and associated boards.
Eclipse
Build STM32 applications with Eclipse, GCC and STM32Cube
6+ IDEs to debug the $2 STM32 BluePill | Wassim | Hackaday.io
Tutorial: STM32 – Integrated Debugging in Eclipse using GNU toolchain – ErikaWiki
#microcontroller/stm32 #7-notes