decoupled user code from hal main mess

master
Ondřej Hruška 8 years ago
parent 6ec9fe86be
commit fc7f31f033
  1. 1
      CMakeLists.txt
  2. 14
      Inc/user_main.h
  3. 54
      Src/main.c
  4. 79
      Src/user_main.c

@ -15,6 +15,7 @@ include_directories(Drivers/CMSIS/Include)
include_directories(Drivers/CMSIS/Device/ST/STM32F1xx/Include) include_directories(Drivers/CMSIS/Device/ST/STM32F1xx/Include)
add_definitions(-DSTM32F107xC) add_definitions(-DSTM32F107xC)
add_definitions(-DUSE_FULL_ASSERT)
add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${LINKER_SCRIPT}) add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${LINKER_SCRIPT})
target_link_libraries(${PROJECT_NAME}.elf HAL CMSIS) target_link_libraries(${PROJECT_NAME}.elf HAL CMSIS)

@ -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

@ -31,7 +31,6 @@
****************************************************************************** ******************************************************************************
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <inttypes.h>
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "adc.h" #include "adc.h"
#include "dma.h" #include "dma.h"
@ -41,8 +40,7 @@
#include "gpio.h" #include "gpio.h"
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "utils.h" #include "user_main.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
@ -64,32 +62,12 @@ void Error_Handler(void);
/* USER CODE BEGIN 0 */ /* 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 */ /* USER CODE END 0 */
int main(void) int main(void)
{ {
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
/* USER CODE END 1 */ /* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/ /* MCU Configuration----------------------------------------------------------*/
@ -109,36 +87,14 @@ int main(void)
MX_USART1_UART_Init(); MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */ /* 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 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (1) {
/* USER CODE END WHILE */ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
user_main();
// Blink
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(500);
uart_print("Main loop\n");
start_DMA();
}
#pragma clang diagnostic pop
/* USER CODE END 3 */ /* USER CODE END 3 */
} }
@ -208,8 +164,7 @@ void Error_Handler(void)
{ {
/* USER CODE BEGIN Error_Handler */ /* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */ /* User can add his own implementation to report the HAL error return state */
while (1) { user_Error_Handler();
}
/* USER CODE END Error_Handler */ /* USER CODE END Error_Handler */
} }
@ -227,6 +182,9 @@ void assert_failed(uint8_t* file, uint32_t line)
/* USER CODE BEGIN 6 */ /* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number, /* 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 */ /* USER CODE END 6 */
} }

@ -0,0 +1,79 @@
//
// Created by MightyPork on 2.9.16.
//
#include <inttypes.h>
#include <stm32f1xx_hal_gpio.h>
#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
Loading…
Cancel
Save