From fc7f31f0330730c8563af8f3b3732669c9c84c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 2 Sep 2016 18:01:13 +0200 Subject: [PATCH] decoupled user code from hal main mess --- CMakeLists.txt | 1 + Inc/user_main.h | 14 +++++++++ Src/main.c | 58 +++++------------------------------- Src/user_main.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 50 deletions(-) create mode 100644 Inc/user_main.h create mode 100644 Src/user_main.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 01b9aa7..3299ead 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ include_directories(Drivers/CMSIS/Include) include_directories(Drivers/CMSIS/Device/ST/STM32F1xx/Include) add_definitions(-DSTM32F107xC) +add_definitions(-DUSE_FULL_ASSERT) add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${LINKER_SCRIPT}) target_link_libraries(${PROJECT_NAME}.elf HAL CMSIS) diff --git a/Inc/user_main.h b/Inc/user_main.h new file mode 100644 index 0000000..8eb6e0f --- /dev/null +++ b/Inc/user_main.h @@ -0,0 +1,14 @@ +// +// Created by MightyPork on 2.9.16. +// + +#ifndef F107_FFT_USER_MAIN_H +#define F107_FFT_USER_MAIN_H + +void user_main(); + +void user_Error_Handler(); + +void user_assert_failed(uint8_t* file, uint32_t line); + +#endif //F107_FFT_USER_MAIN_H diff --git a/Src/main.c b/Src/main.c index 9186187..5ac0401 100644 --- a/Src/main.c +++ b/Src/main.c @@ -31,7 +31,6 @@ ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ -#include #include "stm32f1xx_hal.h" #include "adc.h" #include "dma.h" @@ -41,8 +40,7 @@ #include "gpio.h" /* USER CODE BEGIN Includes */ -#include "utils.h" - +#include "user_main.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ @@ -64,32 +62,12 @@ void Error_Handler(void); /* USER CODE BEGIN 0 */ -static uint32_t audio_samples[256]; - -void start_DMA() { - uart_print("- Starting ADC DMA\n"); - - HAL_ADC_Start_DMA(&hadc1, audio_samples, 256); - HAL_TIM_Base_Start(&htim3); -} - -/** This callback is called by HAL after the transfer is complete */ -void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) -{ - uart_print("- DMA complete.\n"); - - char x[100]; - sprintf(x, "%"PRIu32"\n", audio_samples[0]); - uart_print(x); -} - /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ - /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ @@ -109,36 +87,14 @@ int main(void) MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ - - // Leds OFF - HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, 1); - HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, 1); - HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, 1); - HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, 1); - - // Enable audio input - HAL_GPIO_WritePin(AUDIO_NSTBY_GPIO_Port, AUDIO_NSTBY_Pin, 1); - - /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wmissing-noreturn" - while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - - // Blink - HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); - HAL_Delay(500); - - uart_print("Main loop\n"); - start_DMA(); - } -#pragma clang diagnostic pop + user_main(); /* USER CODE END 3 */ } @@ -207,9 +163,8 @@ void SystemClock_Config(void) void Error_Handler(void) { /* USER CODE BEGIN Error_Handler */ - /* User can add his own implementation to report the HAL error return state */ - while (1) { - } + /* User can add his own implementation to report the HAL error return state */ + user_Error_Handler(); /* USER CODE END Error_Handler */ } @@ -226,7 +181,10 @@ void assert_failed(uint8_t* file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + + user_assert_failed(file, line); + /* USER CODE END 6 */ } diff --git a/Src/user_main.c b/Src/user_main.c new file mode 100644 index 0000000..b5600c0 --- /dev/null +++ b/Src/user_main.c @@ -0,0 +1,79 @@ +// +// Created by MightyPork on 2.9.16. +// + +#include +#include +#include "mxconstants.h" +#include "stm32f1xx_hal.h" +#include "utils.h" +#include "adc.h" +#include "tim.h" +#include "user_main.h" + +static uint32_t audio_samples[256]; + +void start_DMA() { + uart_print("- Starting ADC DMA\n"); + + HAL_ADC_Start_DMA(&hadc1, audio_samples, 256); + HAL_TIM_Base_Start(&htim3); +} + +/** This callback is called by HAL after the transfer is complete */ +void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) { + uart_print("- DMA complete.\n"); + + char x[100]; + sprintf(x, "%"PRIu32"\n", audio_samples[0]); + uart_print(x); +} + +void user_main() { + // Leds OFF + HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, 1); + HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, 1); + HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, 1); + HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, 1); + + // Enable audio input + HAL_GPIO_WritePin(AUDIO_NSTBY_GPIO_Port, AUDIO_NSTBY_Pin, 1); + + while (1) { + // Blink + HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); + HAL_Delay(500); + + uart_print("Main loop\n"); + start_DMA(); + } +} + +//region Error handlers + +void user_Error_Handler() { + uart_print("HAL error occurred.\n"); + while (1); +} + +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void user_assert_failed(uint8_t *file, uint32_t line) { + uart_print("Assert failed in file "); + uart_print((char *) file); + uart_print(" on line "); + + char x[10]; + sprintf(x, "%"PRIu32, line); + uart_print(x); + uart_print("\n"); + + while (1); +} + +// endregion \ No newline at end of file