|
|
@ -2,10 +2,10 @@ |
|
|
|
#include "usb_device.h" |
|
|
|
|
|
|
|
DAC_HandleTypeDef hdac1; |
|
|
|
TIM_HandleTypeDef htim_dac1; |
|
|
|
DMA_HandleTypeDef hdma_dac1; |
|
|
|
TIM_HandleTypeDef htim2; |
|
|
|
TIM_HandleTypeDef htim6; |
|
|
|
TIM_HandleTypeDef htim7; |
|
|
|
ADC_HandleTypeDef hadc1; |
|
|
|
TIM_HandleTypeDef htim_blinky_led; |
|
|
|
RNG_HandleTypeDef hrng; |
|
|
|
|
|
|
|
|
|
|
@ -15,6 +15,10 @@ void sysclk_init(void) |
|
|
|
__HAL_RCC_SYSCFG_CLK_ENABLE(); |
|
|
|
__HAL_RCC_PWR_CLK_ENABLE(); |
|
|
|
|
|
|
|
__HAL_RCC_ADC1_CLK_ENABLE(); |
|
|
|
__HAL_RCC_ADC2_CLK_ENABLE(); |
|
|
|
__HAL_RCC_ADC3_CLK_ENABLE(); |
|
|
|
|
|
|
|
// ALL GPIO Ports
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE(); |
|
|
|
__HAL_RCC_GPIOB_CLK_ENABLE(); |
|
|
@ -109,133 +113,96 @@ void debug_init() |
|
|
|
MX_USB_DEVICE_Init(); // USB first, for logging over serial port
|
|
|
|
} |
|
|
|
|
|
|
|
void gpio_init(void) |
|
|
|
void blinky_led_init() |
|
|
|
{ |
|
|
|
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); |
|
|
|
|
|
|
|
{ |
|
|
|
// LED Green
|
|
|
|
GPIO_InitTypeDef conf = { |
|
|
|
.Pin = GPIO_PIN_12, |
|
|
|
.Mode = GPIO_MODE_OUTPUT_PP, |
|
|
|
.Pull = GPIO_NOPULL, |
|
|
|
.Speed = GPIO_SPEED_FREQ_LOW, |
|
|
|
}; |
|
|
|
|
|
|
|
HAL_GPIO_Init(GPIOD, &conf); |
|
|
|
// GPIO
|
|
|
|
GPIO_InitTypeDef git = { |
|
|
|
.Pin = GPIO_PIN_12, |
|
|
|
.Mode = GPIO_MODE_OUTPUT_PP, |
|
|
|
.Pull = GPIO_NOPULL, |
|
|
|
.Speed = GPIO_SPEED_FREQ_LOW, |
|
|
|
}; |
|
|
|
HAL_GPIO_Init(GPIOD, &git); |
|
|
|
|
|
|
|
// Timer
|
|
|
|
htim_blinky_led.Instance = TIM3; |
|
|
|
htim_blinky_led.Init.Prescaler = 0; |
|
|
|
htim_blinky_led.Init.CounterMode = TIM_COUNTERMODE_UP; |
|
|
|
htim_blinky_led.Init.Period = 1LL << 32; |
|
|
|
htim_blinky_led.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
|
|
|
htim_blinky_led.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
|
|
|
if (HAL_TIM_Base_Init(&htim_blinky_led) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
// DAC1
|
|
|
|
GPIO_InitTypeDef conf = { |
|
|
|
.Pin = GPIO_PIN_4, |
|
|
|
.Mode = GPIO_MODE_ANALOG, |
|
|
|
.Pull = GPIO_NOPULL, |
|
|
|
.Speed = GPIO_SPEED_FREQ_HIGH, |
|
|
|
}; |
|
|
|
TIM_ClockConfigTypeDef tcc = { |
|
|
|
.ClockSource = TIM_CLOCKSOURCE_INTERNAL, |
|
|
|
}; |
|
|
|
if (HAL_TIM_ConfigClockSource(&htim_blinky_led, &tcc) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
HAL_GPIO_Init(GPIOA, &conf); |
|
|
|
TIM_MasterConfigTypeDef tmc = { |
|
|
|
.MasterOutputTrigger = TIM_TRGO_RESET, |
|
|
|
.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE, |
|
|
|
}; |
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim_blinky_led, &tmc) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(TIM3_IRQn, 5, 0); |
|
|
|
HAL_NVIC_EnableIRQ(TIM3_IRQn); |
|
|
|
} |
|
|
|
|
|
|
|
void timer_init(void) |
|
|
|
void dac_init(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
htim2.Instance = TIM2; |
|
|
|
htim2.Init.Prescaler = 0; |
|
|
|
htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
|
|
|
htim2.Init.Period = 42949000; |
|
|
|
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
|
|
|
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
|
|
|
if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
TIM_ClockConfigTypeDef conf = { |
|
|
|
.ClockSource = TIM_CLOCKSOURCE_INTERNAL, |
|
|
|
}; |
|
|
|
if (HAL_TIM_ConfigClockSource(&htim2, &conf) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
TIM_MasterConfigTypeDef conf = { |
|
|
|
.MasterOutputTrigger = TIM_TRGO_RESET, |
|
|
|
.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE, |
|
|
|
}; |
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &conf) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0); |
|
|
|
HAL_NVIC_EnableIRQ(TIM2_IRQn); |
|
|
|
// DAC1
|
|
|
|
GPIO_InitTypeDef conf = { |
|
|
|
.Pin = GPIO_PIN_4, |
|
|
|
.Mode = GPIO_MODE_ANALOG, |
|
|
|
.Pull = GPIO_NOPULL, |
|
|
|
.Speed = GPIO_SPEED_FREQ_HIGH, |
|
|
|
}; |
|
|
|
HAL_GPIO_Init(GPIOA, &conf); |
|
|
|
|
|
|
|
// timer
|
|
|
|
htim_dac1.Instance = TIM2; |
|
|
|
htim_dac1.Init.Prescaler = 0; |
|
|
|
htim_dac1.Init.CounterMode = TIM_COUNTERMODE_UP; |
|
|
|
htim_dac1.Init.Period = 2000;// * 10000;
|
|
|
|
htim_dac1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; |
|
|
|
if (HAL_TIM_Base_Init(&htim_dac1) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 1, 0); |
|
|
|
HAL_NVIC_EnableIRQ(TIM2_IRQn); |
|
|
|
|
|
|
|
{ |
|
|
|
htim6.Instance = TIM6; |
|
|
|
htim6.Init.Prescaler = 0; |
|
|
|
htim6.Init.CounterMode = TIM_COUNTERMODE_UP; |
|
|
|
htim6.Init.Period = 1904; |
|
|
|
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
|
|
|
if (HAL_TIM_Base_Init(&htim6) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
TIM_MasterConfigTypeDef conf = { |
|
|
|
.MasterOutputTrigger = TIM_TRGO_UPDATE, |
|
|
|
.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE, |
|
|
|
}; |
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &conf) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
} |
|
|
|
TIM_MasterConfigTypeDef tmc = { |
|
|
|
.MasterOutputTrigger = TIM_TRGO_UPDATE, |
|
|
|
.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE, |
|
|
|
}; |
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim_dac1, &tmc) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
htim7.Instance = TIM7; |
|
|
|
htim7.Init.Prescaler = 10000; |
|
|
|
htim7.Init.CounterMode = TIM_COUNTERMODE_UP; |
|
|
|
htim7.Init.Period = 429490000; |
|
|
|
htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; |
|
|
|
if (HAL_TIM_Base_Init(&htim7) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
TIM_MasterConfigTypeDef conf = { |
|
|
|
.MasterOutputTrigger = TIM_TRGO_RESET, |
|
|
|
.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE, |
|
|
|
}; |
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &conf) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(TIM7_IRQn, 5, 0); |
|
|
|
HAL_NVIC_EnableIRQ(TIM7_IRQn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void dac_dma_init(void) |
|
|
|
{ |
|
|
|
// DAC
|
|
|
|
hdac1.Instance = DAC1; |
|
|
|
if (HAL_DAC_Init(&hdac1) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
DAC_ChannelConfTypeDef conf = { |
|
|
|
.DAC_Trigger = DAC_TRIGGER_T6_TRGO, |
|
|
|
DAC_ChannelConfTypeDef cct = { |
|
|
|
.DAC_Trigger = DAC_TRIGGER_T2_TRGO, |
|
|
|
.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE, |
|
|
|
}; |
|
|
|
if (HAL_DAC_ConfigChannel(&hdac1, &conf, DAC_CHANNEL_1) != HAL_OK) { |
|
|
|
if (HAL_DAC_ConfigChannel(&hdac1, &cct, DAC_CHANNEL_1) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DMA
|
|
|
|
hdma_dac1.Instance = DMA1_Stream5; |
|
|
|
hdma_dac1.Init.Channel = DMA_CHANNEL_7; |
|
|
|
hdma_dac1.Init.Direction = DMA_MEMORY_TO_PERIPH; |
|
|
@ -243,25 +210,105 @@ void dac_dma_init(void) |
|
|
|
hdma_dac1.Init.MemInc = DMA_MINC_ENABLE; |
|
|
|
hdma_dac1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; |
|
|
|
hdma_dac1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; |
|
|
|
hdma_dac1.Init.Mode = DMA_NORMAL; |
|
|
|
hdma_dac1.Init.Priority = DMA_PRIORITY_LOW; |
|
|
|
hdma_dac1.Init.Mode = DMA_CIRCULAR; |
|
|
|
hdma_dac1.Init.Priority = DMA_PRIORITY_HIGH; |
|
|
|
hdma_dac1.Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
|
|
|
if (HAL_DMA_Init(&hdma_dac1) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
// Originally that was:
|
|
|
|
// __HAL_LINKDMA(&hdac1, DMA_Handle1, hdma_dac1);
|
|
|
|
__HAL_LINKDMA(&hdac1, DMA_Handle1, hdma_dac1); |
|
|
|
// Exapanded to:
|
|
|
|
// hdma_dac1.Parent = &hdac1;
|
|
|
|
// hdac1.DMA_Handle1 = &hdma_dac1;
|
|
|
|
// But this seems to be enough:
|
|
|
|
hdac1.DMA_Handle1 = &hdma_dac1; |
|
|
|
// hdac1.DMA_Handle1 = &hdma_dac1;
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 5, 0); |
|
|
|
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0); |
|
|
|
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void adc_init(void) |
|
|
|
{ |
|
|
|
// GPIO
|
|
|
|
GPIO_InitTypeDef conf = { |
|
|
|
.Pin = GPIO_PIN_4, |
|
|
|
.Mode = GPIO_MODE_ANALOG, |
|
|
|
.Pull = GPIO_NOPULL, |
|
|
|
.Speed = GPIO_SPEED_FREQ_MEDIUM, |
|
|
|
}; |
|
|
|
|
|
|
|
HAL_GPIO_Init(GPIOC, &conf); |
|
|
|
|
|
|
|
ADC_Common_TypeDef *adc_common = ADC123_COMMON; |
|
|
|
ADC_TypeDef *adc1 = ADC1; |
|
|
|
// SET_BIT(adc_common->CCR, ADC_CCR_ADCPRE_0);
|
|
|
|
// CLEAR_BIT(adc_common->CCR, ADC_CCR_ADCPRE_1);
|
|
|
|
//
|
|
|
|
// CLEAR_BIT(adc1->CR1, ~ADC_CR1_RES_0);
|
|
|
|
// CLEAR_BIT(adc1->CR1, ADC_CR1_RES_1);
|
|
|
|
// SET_BIT(adc1->CR2, ADC_CR2_CONT);
|
|
|
|
// CLEAR_BIT(adc1->CR2, ADC_CR2_ALIGN);
|
|
|
|
// SET_BIT(adc1->CR2, ADC_CR2_ADON);
|
|
|
|
// SET_BIT(adc1->CR2, ADC_CR2_SWSTART);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// SET_BIT(adc1->SQR1,ADC_SQR1_L_0);
|
|
|
|
//// CLEAR_BIT(adc1->SQR1,ADC_SQR1_L_1);
|
|
|
|
//// CLEAR_BIT(adc1->SQR1,ADC_SQR1_L_2);
|
|
|
|
//// CLEAR_BIT(adc1->SQR1,ADC_SQR1_L_3);
|
|
|
|
////
|
|
|
|
//// ADC_SQR3_RK(1,1);
|
|
|
|
// SET_BIT(adc1->SQR3,ADC_SQR3_SQ1_0);
|
|
|
|
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_1);
|
|
|
|
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_2);
|
|
|
|
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_3);
|
|
|
|
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_4);
|
|
|
|
|
|
|
|
hadc1.Instance = ADC1; |
|
|
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; |
|
|
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B; |
|
|
|
hadc1.Init.ScanConvMode = DISABLE; |
|
|
|
hadc1.Init.ContinuousConvMode = ENABLE; |
|
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE; |
|
|
|
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
|
|
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
|
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
|
|
|
hadc1.Init.NbrOfConversion = 1; |
|
|
|
hadc1.Init.DMAContinuousRequests = DISABLE; |
|
|
|
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; |
|
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {}; |
|
|
|
sConfig.Channel = ADC_CHANNEL_14; |
|
|
|
sConfig.Rank = 1; |
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; |
|
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { |
|
|
|
Error_Handler(); |
|
|
|
} |
|
|
|
|
|
|
|
// adc1->CR2
|
|
|
|
while (1) { |
|
|
|
// SET_BIT(adc1->CR2, ADC_CR2_ADON);
|
|
|
|
// SET_BIT(adc1->CR2, ADC_CR2_SWSTART);
|
|
|
|
HAL_ADC_Start(&hadc1); |
|
|
|
|
|
|
|
// heck_log("ADC123_COMMON->CCR:" + reg_to_string(adc_common->CCR));
|
|
|
|
// heck_log("ADC1->CR1:" + reg_to_string(adc1->CR1));
|
|
|
|
// heck_log("ADC1->CR2:" + reg_to_string(adc1->CR2));
|
|
|
|
// heck_log("ADC1->SQR1:" + reg_to_string(adc1->SQR1));
|
|
|
|
// heck_log("ADC1->SQR2:" + reg_to_string(adc1->SQR2));
|
|
|
|
// heck_log("ADC1->SQR3:" + reg_to_string(adc1->SQR3));
|
|
|
|
// heck_log("ADC1->DR:" + reg_to_string(adc1->DR));
|
|
|
|
heck_log("\n"); |
|
|
|
HAL_Delay(100); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void rng_init(void) |
|
|
|
{ |
|
|
|
hrng.Instance = RNG; |
|
|
@ -270,21 +317,14 @@ void rng_init(void) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//void irq_init()
|
|
|
|
//{
|
|
|
|
//// This is for RTOS i guess
|
|
|
|
// HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
|
|
|
|
//}
|
|
|
|
|
|
|
|
void heck_init() |
|
|
|
{ |
|
|
|
HAL_Init(); |
|
|
|
sysclk_init(); |
|
|
|
debug_init(); |
|
|
|
heck_debug_suspend(); |
|
|
|
gpio_init(); |
|
|
|
timer_init(); |
|
|
|
dac_dma_init(); |
|
|
|
// heck_debug_suspend();
|
|
|
|
blinky_led_init(); |
|
|
|
dac_init(); |
|
|
|
adc_init(); |
|
|
|
rng_init(); |
|
|
|
|
|
|
|
} |
|
|
|