Reputation: 2723
I'm new to STM32 and I followed the instructions here in order to program my first stm32f103c8t6 board in Ubuntu.
Here is the code which I added to the source code:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
HAL_Delay(500);
}
/* USER CODE END 3 */
}
And of course, I have set PA0
port as GPIO_output
in STM32CubeMX
.
Here is the output of function MX_GPIO_Init
:
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
The code builds and uploads successfully to the board. Here is the output for rebuilding the code which builds and uploads the code:
-------------- Clean: Release in sample1 (compiler: GNU GCC Compiler for ARM)---------------
Executing clean command: make -f Makefile cleanRelease
rm -fR .dep build
Cleaned "sample1 - Release"
-------------- Build: Release in sample1 (compiler: GNU GCC Compiler for ARM)---------------
Checking if target is up-to-date: make -q -f Makefile Release
Running command: make -f Makefile Release
mkdir -p build
C. Compiling build/system_stm32f1xx.o...
C. Compiling build/stm32f1xx_hal.o...
C. Compiling build/stm32f1xx_hal_cortex.o...
C. Compiling build/stm32f1xx_hal_dma.o...
C. Compiling build/stm32f1xx_hal_flash.o...
C. Compiling build/stm32f1xx_hal_flash_ex.o...
C. Compiling build/stm32f1xx_hal_gpio.o...
C. Compiling build/stm32f1xx_hal_gpio_ex.o...
C. Compiling build/stm32f1xx_hal_pwr.o...
C. Compiling build/stm32f1xx_hal_rcc.o...
C. Compiling build/stm32f1xx_hal_rcc_ex.o...
C. Compiling build/stm32f1xx_hal_tim.o...
C. Compiling build/stm32f1xx_hal_tim_ex.o...
C. Compiling build/main.o...
C. Compiling build/stm32f1xx_hal_msp.o...
C. Compiling build/stm32f1xx_it.o...
S. Compiling build/startup_stm32f103xb.o...
2018-06-21T10:32:46 INFO usb.c: -- exit_dfu_mode
C. Linking build/sample1.elf...
/usr/bin/arm-none-eabi-size build/sample1.elf
text data bss dec hex filename
3560 20 1572 5152 1420 build/sample1.elf
H. Linking build/sample1.hex...
B. Building build/sample1.bin...
Used gcc: 6.3.1
/usr/local/bin/st-flash erase
2018-06-21T10:32:46 INFO common.c: Loading device parameters....
2018-06-21T10:32:46 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
2018-06-21T10:32:46 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
2018-06-21T10:32:46 INFO common.c: Loading device parameters....
2018-06-21T10:32:46 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
2018-06-21T10:32:46 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
st-flash 1.4.0-39-g6db0fc2
Mass erasing
/usr/local/bin/st-flash --reset write build/sample1.bin 0x8000000
2018-06-21T10:32:46 INFO common.c: Attempting to write 3580 (0xdfc) bytes to stm32 address: 134217728 (0x8000000)
st-flash 1.4.0-39-g6db0fc2
Flash page at addr: 0x08000000 erased
Flash page at addr: 0x08000400 erased
Flash page at addr: 0x08000800 erased
2018-06-21T10:32:46 INFO common.c: Finished erasing 4 pages of 1024 (0x400) bytes
2018-06-21T10:32:46 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id
2018-06-21T10:32:46 INFO flash_loader.c: Successfully loaded flash loader in sram
Flash page at addr: 0x08000c00 erased
1/4 pages written
2/4 pages written
2018-06-21T10:32:46 INFO common.c: Starting verification of write complete
2018-06-21T10:32:46 INFO common.c: Flash written and verified! jolly good!
3/4 pages written
4/4 pages written
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
However, the LED does not start to blink as expected. LED works fine when I connect it to 5volt. I have checked the board's pins by AVO meter and they are all connected to the micro-controller.
After a few research, I thought that it has to do something with Boot0
and Boot1
pins so I tried different options from here but none of them worked.
Here is the image of my board:
How can I fix it?
Upvotes: 2
Views: 7920
Reputation: 1
I had the same problem and I solved that by connecting BOOT0 pin to the ground...
Upvotes: 0
Reputation: 31
I was dealing with the same problem for a week. a simple program for blinking the LED diode in the arduino environment with stm32f103c8t6 was working, the combination of stm32f103c8t6, stm32CubeMX, ubuntu's gcc-arm (gcc-arm-none-eabi version:15:6.3.1+svn253039-1build1) and ubuntu 18.04 does not. After I installed GNU Embedded Toolchain for Arm the things started to work. GNU Arm Embedded Toolchain Version 8-2018-q4-major Linux 64 solved my problem.
Upvotes: 1
Reputation: 97
I believe the HAL_Delay function is using systick to count time in ms, systick must be set to 1ms to get the right delay, did you configure that ? I am not sure how you do this.
If the time between the output toggles is very low, the output will not toggle because you set GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;, try GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; then you can use an oscilloscope to check if it toggles.
If you do not have an oscilloscope at hand, you could try to insert a "while loop" counting towards a high number, instead of the HAL_Delay function.
Counter=0;
while (Counter<500000000)
{
Counter++;
}
Assuming your clock frequency is set at 72MHz, this will give a time in the range of 0.5 to 1 sec. Remember to declare the variable as integer 32 bit or higher, e.g. uint32_t counter = 0; If it does not work, set a breakpoint at the HAL_GPIO_Togglepin line, it will then pause at the breakpoint line and every time you click "RUN", it should then run to the breakpoint again and the output should toggle.
Upvotes: 2
Reputation: 1102
Your code should work, but you are only toggling an unconnected µC pin.
If you look at the schematics from the "black pill" homepage the LED is connected to PB12
The BOOT0 pin should be connected to GND if you want to run your firmware (from flash) the BOOT1 pin doesn't matter.
One thing in your image: you don't have a resistor for your LED to limit the current, depending on the LED type this could damage the LED and/or the µC pin.
Maybe you should first try to use D2 from the board and if that works you could switch over to your on LED.
I don't know if you've done a correct initialization of the pin:
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
/*Configure GPIO pin : PB12 */
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
The Clock enable is very important for each peripheral.
Upvotes: 0