Most of the embedded projects share certain initial steps. You need to confirm the clock settings before doing anything, then you want to have debug connection via a UART channel since it is cheap but still it can provide useful information for debugging.
Let us start with CubeMX. You select a device/board of your choice, set up the pinouts for one GPIO output and one UART port. Configure the clock if necessary then create a project.
Once the SysTick is initialized, it generates 1 msec interrupt and this interrupt is handled by SysTick_Handler()according to the Cube framework.Source file
Source file
This SysTick output is useful throughout the development. You can monitor the state of the MCU for example whether the watchdog kicked in or brown-out reset occurred.
Source file
Source file
Source file
Let us start with CubeMX. You select a device/board of your choice, set up the pinouts for one GPIO output and one UART port. Configure the clock if necessary then create a project.
Clock Checking using SysTick
The sanity of the clock setting can be done by checking the SysTick interval. All Cortex-M series core have SysTick timer by default, which should fire at 1msec interval while the MCU is active. In the STM32Cube, the SysTick is initialized by HAL_Init() call, which in turn calls SysTick_Config() in CMSIS.Once the SysTick is initialized, it generates 1 msec interrupt and this interrupt is handled by SysTick_Handler()according to the Cube framework.
1 /** 2 * @brief This function handles System tick timer. 3 */ 4 void SysTick_Handler(void) 5 { 6 /* USER CODE BEGIN SysTick_IRQn 0 */ 7 8 /* USER CODE END SysTick_IRQn 0 */ 9 HAL_IncTick(); 10 HAL_SYSTICK_IRQHandler(); 11 /* USER CODE BEGIN SysTick_IRQn 1 */ 12 13 /* USER CODE END SysTick_IRQn 1 */ 14 }The SysTick_Handler() then calls HAL_SYSTICK_IRQHandler() as shown above and finally it calls HAL_SYSTICK_Callback(), which is implemented as a weak function. So you can overwrite it in your main.c. In this function, you toggle the GPIO output.
312 /* USER CODE BEGIN 4 */ 313 void HAL_SYSTICK_Callback(void) 314 { 315 HAL_GPIO_TogglePin(TEST_LED_GPIO_Port, TEST_LED_Pin); 316 } 317 /* USER CODE END 4 */By measuring the frequency of the waveform, you can check the frequency of the SysTick.
This SysTick output is useful throughout the development. You can monitor the state of the MCU for example whether the watchdog kicked in or brown-out reset occurred.
UART Debug Output
Another useful feature that you want to have is the UART debug output. Basically you can implement the same function as printf() whose output is redirected to UART port.312 /* USER CODE BEGIN 4 */ 313 void UartPrintf(const char *format, ...) 314 { 315 char buffer[128]; 316 uint16_t size; 317 va_list args; 318 319 va_start(args, format); 320 size = vsprintf(buffer, format, args); 321 va_end(args); 322 323 HAL_UART_Transmit(&huart1, (uint8_t*)buffer, size, 1000); 324 } 325 /* USER CODE END 4 */To compile this code, you may need to include following header files.
54 /* USER CODE BEGIN Includes */ 55 #include <stdio.h> 56 #include <stdarg.h> 57 /* USER CODE END Includes */Depending on the c library the toolchain was built on, actual feature of the format conversion may vary, for example floating point conversion may not supported in Cortex-M0. However you can use the same syntax as printf().
117 /* USER CODE BEGIN 2 */ 118 UartPrintf("System Reset: %d\n\r", some_int); 119 /* USER CODE END 2 */
Comments
Post a Comment