decoupled user code from hal main mess

This commit is contained in:
2016-09-02 18:01:13 +02:00
parent 6ec9fe86be
commit fc7f31f033
4 changed files with 102 additions and 50 deletions
+1
View File
@@ -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)
+14
View File
@@ -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
+8 -50
View File
@@ -31,7 +31,6 @@
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <inttypes.h>
#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 */
}
+79
View File
@@ -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