Browse Source

Slim it down, slim it down

makefile_for_cmsis
heck 2 years ago
parent
commit
8c3f2b4899
  1. 181
      src/main.c
  2. 15
      src/main.h

181
src/main.c

@ -2,6 +2,7 @@
#include "cmsis_os.h" #include "cmsis_os.h"
#include "cppmain.h" #include "cppmain.h"
#include "usb_device.h" #include "usb_device.h"
#include "stm32f4xx_hal_cortex.h"
DAC_HandleTypeDef hdac1; DAC_HandleTypeDef hdac1;
DMA_HandleTypeDef hdma_dac1; DMA_HandleTypeDef hdma_dac1;
@ -13,7 +14,7 @@ RNG_HandleTypeDef hrng;
extern PCD_HandleTypeDef hpcd_USB_OTG_FS; extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
void SystemClock_Config(void) void sysclk_init(void)
{ {
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 }; RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 }; RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
@ -165,60 +166,56 @@ void OTG_FS_IRQHandler(void)
// Peripheral init // Peripheral init
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
void MX_GPIO_Init(void) void gpio_init(void)
{ {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
{ {
GPIO_InitTypeDef GPIO_InitStruct = { 0 }; GPIO_InitTypeDef conf = { 0 };
GPIO_InitStruct.Pin = GPIO_PIN_0; conf.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; conf.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL; conf.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &conf);
} }
{ {
GPIO_InitTypeDef GPIO_InitStruct = { 0 }; GPIO_InitTypeDef conf = { 0 };
GPIO_InitStruct.Pin = GPIO_PIN_12; conf.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; conf.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL; conf.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; conf.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &conf);
} }
{ {
GPIO_InitTypeDef GPIO_InitStruct = { 0 }; GPIO_InitTypeDef conf = { 0 };
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0); conf.Pin = GPIO_PIN_4;
HAL_NVIC_EnableIRQ(EXTI0_IRQn); conf.Mode = GPIO_MODE_ANALOG;
conf.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &conf);
}
} }
void dac_init(void)
// --------------------------------------------------------------------------------------------------
// Peripheral init
// --------------------------------------------------------------------------------------------------
void MX_DAC_Init(void)
{ {
DAC_ChannelConfTypeDef conf = { 0 };
hdac1.Instance = DAC1; hdac1.Instance = DAC1;
if (HAL_DAC_Init(&hdac1) != HAL_OK) { if (HAL_DAC_Init(&hdac1) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
DAC_ChannelConfTypeDef conf = { 0 };
conf.DAC_Trigger = DAC_TRIGGER_T6_TRGO; conf.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
conf.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; conf.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
if (HAL_DAC_ConfigChannel(&hdac1, &conf, DAC_CHANNEL_1) != HAL_OK) { if (HAL_DAC_ConfigChannel(&hdac1, &conf, DAC_CHANNEL_1) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
}
void dma_init(void)
{
hdma_dac1.Instance = DMA1_Stream5; hdma_dac1.Instance = DMA1_Stream5;
hdma_dac1.Init.Channel = DMA_CHANNEL_7; hdma_dac1.Init.Channel = DMA_CHANNEL_7;
hdma_dac1.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_dac1.Init.Direction = DMA_MEMORY_TO_PERIPH;
@ -233,41 +230,20 @@ void MX_DAC_Init(void)
Error_Handler(); Error_Handler();
} }
__HAL_LINKDMA(&hdac1, DMA_Handle1, hdma_dac1); // Originally that was:
} // __HAL_LINKDMA(&hdac1, DMA_Handle1, hdma_dac1);
// Exapanded to:
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *dacHandle) // hdma_dac1.Parent = &hdac1;
{ hdac1.DMA_Handle1 = &hdma_dac1;
if (dacHandle->Instance == DAC1) {
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4 | GPIO_PIN_5);
HAL_DMA_DeInit(dacHandle->DMA_Handle1);
}
}
// --------------------------------------------------------------------------------------------------
// Peripheral init
// --------------------------------------------------------------------------------------------------
void MX_DMA_Init(void)
{
/* DMA interrupt init */
/* DMA1_Stream5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// Timers // Timers
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
void MX_TIM2_Init(void) void tim2_init(void)
{ {
TIM_ClockConfigTypeDef sClockSourceConfig = { 0 };
TIM_MasterConfigTypeDef sMasterConfig = { 0 };
htim2.Instance = TIM2; htim2.Instance = TIM2;
htim2.Init.Prescaler = 0; htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
@ -277,10 +253,14 @@ void MX_TIM2_Init(void)
if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { if (HAL_TIM_Base_Init(&htim2) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
TIM_ClockConfigTypeDef sClockSourceConfig = { 0 };
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) { if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
TIM_MasterConfigTypeDef sMasterConfig = { 0 };
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) { if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) {
@ -288,10 +268,8 @@ void MX_TIM2_Init(void)
} }
} }
void MX_TIM6_Init(void) void tim6_init(void)
{ {
TIM_MasterConfigTypeDef sMasterConfig = { 0 };
htim6.Instance = TIM6; htim6.Instance = TIM6;
htim6.Init.Prescaler = 0; htim6.Init.Prescaler = 0;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP; htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
@ -300,6 +278,8 @@ void MX_TIM6_Init(void)
if (HAL_TIM_Base_Init(&htim6) != HAL_OK) { if (HAL_TIM_Base_Init(&htim6) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
TIM_MasterConfigTypeDef sMasterConfig = { 0 };
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) { if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) {
@ -307,10 +287,8 @@ void MX_TIM6_Init(void)
} }
} }
void MX_TIM7_Init(void) void tim7_init(void)
{ {
TIM_MasterConfigTypeDef sMasterConfig = { 0 };
htim7.Instance = TIM7; htim7.Instance = TIM7;
htim7.Init.Prescaler = 10000; htim7.Init.Prescaler = 10000;
htim7.Init.CounterMode = TIM_COUNTERMODE_UP; htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
@ -319,6 +297,8 @@ void MX_TIM7_Init(void)
if (HAL_TIM_Base_Init(&htim7) != HAL_OK) { if (HAL_TIM_Base_Init(&htim7) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
TIM_MasterConfigTypeDef sMasterConfig = { 0 };
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK) { if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK) {
@ -326,37 +306,29 @@ void MX_TIM7_Init(void)
} }
} }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *tim_baseHandle) void irq_init()
{ {
if (tim_baseHandle->Instance == TIM2) { // This is for RTOS i guess
/* TIM2 interrupt Init */ // HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0); HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn); HAL_NVIC_EnableIRQ(TIM2_IRQn);
} else if (tim_baseHandle->Instance == TIM6) {
} else if (tim_baseHandle->Instance == TIM7) {
/* TIM7 interrupt Init */
HAL_NVIC_SetPriority(TIM7_IRQn, 5, 0); HAL_NVIC_SetPriority(TIM7_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(TIM7_IRQn); HAL_NVIC_EnableIRQ(TIM7_IRQn);
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *tim_baseHandle) HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0);
{ HAL_NVIC_EnableIRQ(EXTI0_IRQn);
if (tim_baseHandle->Instance == TIM2) {
HAL_NVIC_DisableIRQ(TIM2_IRQn);
} else if (tim_baseHandle->Instance == TIM6) {
} else if (tim_baseHandle->Instance == TIM7) {
HAL_NVIC_DisableIRQ(TIM7_IRQn);
}
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// Random Number Generator (RNG) // Random Number Generator (RNG)
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
void MX_RNG_Init(void) void rng_init(void)
{ {
hrng.Instance = RNG; hrng.Instance = RNG;
if (HAL_RNG_Init(&hrng) != HAL_OK) { if (HAL_RNG_Init(&hrng) != HAL_OK) {
@ -371,7 +343,6 @@ void Error_Handler(void)
while (1) {} while (1) {}
} }
#ifdef USE_FULL_ASSERT
/** /**
* @brief Reports the name of the source file and the source line number * @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred. * where the assert_param error has occurred.
@ -384,47 +355,41 @@ void assert_failed(uint8_t *file, uint32_t line)
/* 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) */
} }
#endif /* USE_FULL_ASSERT */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityNormal,
};
void StartDefaultTask(void *argument) void rtos_default_task(void *argument)
{ {
MX_USB_DEVICE_Init();
heck_cppmain(); heck_cppmain();
} }
void MX_FREERTOS_Init(void) void rtos_init(void)
{ {
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityNormal,
};
osThreadId_t defaultTaskHandle;
defaultTaskHandle = osThreadNew(rtos_default_task, NULL, &defaultTask_attributes);
} }
int main(void) int main(void)
{ {
HAL_Init(); HAL_Init();
SystemClock_Config(); sysclk_init();
MX_GPIO_Init(); MX_USB_DEVICE_Init(); // USB first, for logging over serial port
MX_DMA_Init(); gpio_init();
MX_DAC_Init(); dac_init();
MX_TIM2_Init(); tim2_init();
MX_TIM6_Init(); tim6_init();
MX_TIM7_Init(); tim7_init();
MX_RNG_Init(); rng_init();
irq_init();
dma_init();
osKernelInitialize(); osKernelInitialize();
MX_FREERTOS_Init();
osKernelStart();
}
void HAL_MspInit(void)
{
HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0); rtos_init();
osKernelStart();
} }

15
src/main.h

@ -9,18 +9,17 @@ extern "C" {
#endif #endif
extern DAC_HandleTypeDef hdac1; extern DAC_HandleTypeDef hdac1;
extern RNG_HandleTypeDef hrng;
extern TIM_HandleTypeDef htim2; extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim6; extern TIM_HandleTypeDef htim6;
extern TIM_HandleTypeDef htim7; extern TIM_HandleTypeDef htim7;
extern RNG_HandleTypeDef hrng;
void MX_DAC_Init(void); void dac_init(void);
void MX_DMA_Init(void); void gpio_init(void);
void MX_GPIO_Init(void); void rng_init(void);
void MX_RNG_Init(void); void tim2_init(void);
void MX_TIM2_Init(void); void tim6_init(void);
void MX_TIM6_Init(void); void tim7_init(void);
void MX_TIM7_Init(void);
void Error_Handler(void); void Error_Handler(void);

Loading…
Cancel
Save