43 #include "stm32l4xx_hal.h" 44 #include "stm32l476g_discovery.h" 45 #include "stm32l476g_discovery_glass_lcd.h" 64 static void config_clkout(
void);
67 DAC_HandleTypeDef* hdac,
149 BSP_LCD_GLASS_Init();
228 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
229 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
232 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
233 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
234 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_8;
235 RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
237 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
238 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
239 RCC_OscInitStruct.PLL.PLLM = 2;
240 RCC_OscInitStruct.PLL.PLLN = 20;
241 RCC_OscInitStruct.PLL.PLLR = 2;
242 RCC_OscInitStruct.PLL.PLLP = 7;
243 RCC_OscInitStruct.PLL.PLLQ = 5;
244 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
252 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
253 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
254 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
255 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
256 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
257 if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
295 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
296 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
300 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
301 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
302 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
303 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
304 RCC_OscInitStruct.PLL.PLLM = 1;
305 RCC_OscInitStruct.PLL.PLLN = 20;
306 RCC_OscInitStruct.PLL.PLLR = 2;
307 RCC_OscInitStruct.PLL.PLLP = 7;
308 RCC_OscInitStruct.PLL.PLLQ = 5;
309 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
317 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
318 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
319 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
320 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
321 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
322 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
331 static DMA_HandleTypeDef hdma;
332 static TIM_HandleTypeDef htim;
337 htim.Instance = TIM4;
338 HAL_TIM_Base_DeInit(&htim);
345 hdma.Instance = DMA2_Channel5;
346 HAL_DMA_DeInit(&hdma);
347 hdma.Instance = DMA1_Channel1;
348 HAL_DMA_DeInit(&hdma);
360 GPIO_InitTypeDef my_io_pin;
362 DIGITAL_IO_CLK_ENABLE();
363 my_io_pin.Pin = DIGITAL_IO_PIN;
364 my_io_pin.Mode = GPIO_MODE_OUTPUT_PP;
365 my_io_pin.Pull = GPIO_NOPULL;
366 my_io_pin.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
367 HAL_GPIO_Init(DIGITAL_IO_PORT, &my_io_pin);
376 static void config_clkout(
void)
378 GPIO_InitTypeDef my_io_pin;
380 __HAL_RCC_GPIOB_CLK_ENABLE();
381 my_io_pin.Pin = GPIO_PIN_6;
382 my_io_pin.Mode = GPIO_MODE_AF_PP;
383 my_io_pin.Pull = GPIO_NOPULL;
384 my_io_pin.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
385 my_io_pin.Alternate = GPIO_AF2_TIM4;
386 HAL_GPIO_Init(DIGITAL_IO_PORT, &my_io_pin);
398 GPIO_InitTypeDef joy_center;
400 __HAL_RCC_GPIOA_CLK_ENABLE();
401 joy_center.Pin = GPIO_PIN_0;
402 joy_center.Mode = GPIO_MODE_IT_FALLING;
403 joy_center.Pull = GPIO_PULLDOWN;
404 joy_center.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
405 HAL_GPIO_Init(GPIOA, &joy_center);
406 HAL_NVIC_SetPriority( (IRQn_Type) EXTI0_IRQn, 0x0F, 0x00);
407 HAL_NVIC_EnableIRQ((IRQn_Type) EXTI0_IRQn);
422 BSP_LCD_GLASS_DisplayString((uint8_t*)
"ECE486");
434 BSP_LCD_GLASS_DisplayString((uint8_t*)
"RUN ");
459 static DAC_ChannelConfTypeDef DacConfig;
472 DacConfig.DAC_Trigger = DAC_TRIGGER_T4_TRGO;
473 DacConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
474 DacConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
475 DacConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
476 DacConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
479 if(HAL_DAC_ConfigChannel(&
DacHandle, &DacConfig, DAC_CHANNEL_2) != HAL_OK)
492 if(HAL_DAC_ConfigChannel(&
DacHandle, &DacConfig, DAC_CHANNEL_2) != HAL_OK)
499 DacConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
500 if(HAL_DAC_ConfigChannel(&
DacHandle, &DacConfig, DAC_CHANNEL_1) != HAL_OK)
526 DAC_HandleTypeDef* hdac,
536 hdac->State = HAL_DAC_STATE_BUSY;
539 hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
542 hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
545 hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
548 hdac->Instance->CR |= DAC_CR_DMAEN2;
550 tmpreg = (uint32_t)&hdac->Instance->DHR12LD;
553 HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
556 __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_2);
557 __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_1);
572 GPIO_InitTypeDef gpio_dacpins;
573 static DMA_HandleTypeDef hdma;
576 __HAL_RCC_DAC1_CLK_ENABLE();
577 __HAL_RCC_GPIOA_CLK_ENABLE();
578 __HAL_RCC_DMA2_CLK_ENABLE();
581 gpio_dacpins.Pin = GPIO_PIN_5;
582 gpio_dacpins.Mode = GPIO_MODE_ANALOG;
583 gpio_dacpins.Pull = GPIO_NOPULL;
584 gpio_dacpins.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
585 HAL_GPIO_Init(GPIOA, &gpio_dacpins);
588 hdma.Instance = DMA2_Channel5;
589 hdma.Init.Request = DMA_REQUEST_3;
590 hdma.Init.Direction = DMA_MEMORY_TO_PERIPH;
591 hdma.Init.PeriphInc = DMA_PINC_DISABLE;
592 hdma.Init.MemInc = DMA_MINC_ENABLE;
593 hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
594 hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
595 hdma.Init.Mode = DMA_CIRCULAR;
596 hdma.Init.Priority = DMA_PRIORITY_HIGH;
601 __HAL_LINKDMA(hdac, DMA_Handle2, hdma);
604 HAL_NVIC_SetPriority(DMA2_Channel5_IRQn, 1, 0);
610 HAL_NVIC_SetPriority(TIM6_DAC_IRQn,3,0);
611 HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
619 __HAL_RCC_DAC1_FORCE_RESET();
620 __HAL_RCC_DAC1_RELEASE_RESET();
624 HAL_DMA_DeInit(hdac->DMA_Handle2);
627 HAL_NVIC_DisableIRQ(DMA2_Channel5_IRQn);
628 HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn);
636 if(htim->Instance == TIM4) {
637 __HAL_RCC_TIM4_CLK_ENABLE();
645 if(htim->Instance == TIM4) {
646 __HAL_RCC_TIM4_FORCE_RESET();
647 __HAL_RCC_TIM4_RELEASE_RESET();
676 ADC_ChannelConfTypeDef sConfig;
677 ADC_MultiModeTypeDef mode;
686 AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
687 AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
689 AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
690 AdcHandle.Init.ContinuousConvMode = DISABLE;
692 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
694 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
699 AdcHandle.Init.ExternalTrigConv = LL_ADC_REG_TRIG_EXT_TIM4_TRGO;
700 AdcHandle.Init.DataAlign = ADC_DATAALIGN_LEFT;
701 AdcHandle.Init.DMAContinuousRequests = ENABLE;
702 AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
703 AdcHandle.Init.OversamplingMode = DISABLE;
708 sConfig.Channel = ADC1_CHANNEL;
709 sConfig.Rank = ADC_REGULAR_RANK_1;
710 sConfig.SamplingTime = ADC_SAMPLETIME_92CYCLES_5;
711 sConfig.SingleDiff = ADC_SINGLE_ENDED;
712 sConfig.OffsetNumber = ADC_OFFSET_NONE;
729 AdcHandle2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
733 sConfig.Channel = ADC2_CHANNEL;
739 mode.Mode = ADC_DUALMODE_REGSIMULT;
740 mode.DMAAccessMode = ADC_DMAACCESSMODE_12_10_BITS;
741 mode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_5CYCLES;
757 static TIM_HandleTypeDef htim;
758 TIM_MasterConfigTypeDef sMasterConfig;
760 htim.Instance = TIM4;
766 htim.Init.Period = fs;
767 htim.Init.Prescaler = 0;
768 htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
769 htim.Init.CounterMode = TIM_COUNTERMODE_UP;
770 HAL_TIM_Base_Init(&htim);
772 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
773 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
775 HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);
778 HAL_TIM_Base_Start(&htim);
791 GPIO_InitTypeDef gpio_adcpins;
792 static DMA_HandleTypeDef hdma;
794 __HAL_RCC_ADC_CLK_ENABLE();
795 __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
797 if (hadc->Instance == ADC2) {
802 ADC2_GPIO_CLK_ENABLE();
804 gpio_adcpins.Pin = ADC2_GPIO_PIN;
805 gpio_adcpins.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
806 gpio_adcpins.Pull = GPIO_NOPULL;
807 HAL_GPIO_Init(ADC2_GPIO_PORT, &gpio_adcpins);
814 ADC1_GPIO_CLK_ENABLE();
815 __HAL_RCC_DMA1_CLK_ENABLE();
817 gpio_adcpins.Pin = ADC1_GPIO_PIN;
818 gpio_adcpins.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
819 gpio_adcpins.Pull = GPIO_NOPULL;
820 HAL_GPIO_Init(ADC1_GPIO_PORT, &gpio_adcpins);
823 hdma.Instance = DMA1_Channel1;
824 hdma.Init.Request = DMA_REQUEST_0;
825 hdma.Init.Direction = DMA_PERIPH_TO_MEMORY;
826 hdma.Init.PeriphInc = DMA_PINC_DISABLE;
827 hdma.Init.MemInc = DMA_MINC_ENABLE;
828 hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
829 hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
830 hdma.Init.Mode = DMA_CIRCULAR;
831 hdma.Init.Priority = DMA_PRIORITY_HIGH;
836 __HAL_LINKDMA(hadc, DMA_Handle, hdma);
840 HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
841 HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
848 HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
849 HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
856 __ADC_RELEASE_RESET();
858 if(hadc->DMA_Handle != NULL) HAL_DMA_DeInit(hadc->DMA_Handle);
860 HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn);
861 HAL_NVIC_DisableIRQ(ADC1_2_IRQn);
874 HAL_DMA_IRQHandler(
DacHandle.DMA_Handle2);
900 HAL_DMA_IRQHandler(
AdcHandle.DMA_Handle);
915 HAL_GPIO_EXTI_IRQHandler(SEL_JOY_PIN);
920 HAL_GPIO_EXTI_IRQHandler(LEFT_JOY_PIN);
925 HAL_GPIO_EXTI_IRQHandler(RIGHT_JOY_PIN);
930 HAL_GPIO_EXTI_IRQHandler(UP_JOY_PIN);
935 HAL_GPIO_EXTI_IRQHandler(DOWN_JOY_PIN);
941 if ( GPIO_Pin & (DOWN_JOY_PIN | UP_JOY_PIN | RIGHT_JOY_PIN | LEFT_JOY_PIN | SEL_JOY_PIN) ){
#define NORMAL_LED
Green LED, STM32L476G-Discovery.
JOYState_TypeDef JoyState
static void TIM4_Config(uint16_t fs)
volatile uint32_t * ADC_Input_Buffer
Buffer to store samples transfered from the ADC by a DMA.
Stereo Output: Configure DAC1 and DAC2, dual DMA Transfer.
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
static void init_adc(uint16_t fs, enum Num_Channels_In chanin)
static void SystemClock_Config_HSE(void)
Hummels Config to give 80 MHz SYSCLK, with PLL48M1CLK=48 MHz The system Clock is configured as follow...
void EXTI0_IRQHandler(void)
void ADC1_2_IRQHandler(void)
void initerror()
Initialization routine to set up error buffers.
Mono Input: Only configure ADC1, single DMA Transfer.
#define CLOCK_CONFIG_ERROR
enum Num_Channels_Out Output_Configuration
static void ece_486_deinit(void)
void EXTI9_5_IRQHandler(void)
void usart_init(void)
USART Configuration to talk to ST-LINK.
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
static void config_pushbutton(void)
Configure PA6 (Joystic Center) generate an interrupt.
static void SystemClock_Config_MSI(void)
System Clock Configuration.
static DAC_HandleTypeDef DacHandle
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
Internal MSI RC Oscillator.
volatile uint32_t * DAC_Output_Buffer
Buffer to hold samples to be transfered to the DAC(s) by a DMA.
Num_Channels_In
Number of input audio channels.
Clock_Reference
Clock Reference Source.
__IO FlagStatus KeyPressed
enum Num_Channels_In Input_Configuration
uint32_t ADC_Buffer_Size
Total buffer size being filled by DMA for ADC/DAC.
float getsamplingfrequency(void)
Simple function to return the best guess at the actual sampling frequency.
void SysTick_Handler(void)
void flagerror(int errorcode)
Records and indicates an error condition.
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
void DMA1_Channel1_IRQHandler(void)
void opamp_init(void)
Set up OPAMP1 to buffer DAC Channel 1 to drive PA3.
static void config_digital_io(void)
Configure a digital output pin.
static ADC_HandleTypeDef AdcHandle
void DMA2_Channel5_IRQHandler(void)
USART Initialization to stream printf() output back to the ST-LINK.
void initialize(uint16_t fs, enum Num_Channels_In chanin, enum Num_Channels_Out chanout)
Wrapper function to perform all processor initialization for ECE 486.
void TIM6_DAC_IRQHandler(void)
void initialize_ece486(uint16_t fs, enum Num_Channels_In chanin, enum Num_Channels_Out chanout, enum Clock_Reference clkref)
Wrapper function to perform all processor initialization for ECE 486.
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
void EXTI3_IRQHandler(void)
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
static void blinkandwait(void)
Wait for a button-press while blinking the LEDs.
ECE 486 Interface fuctions to manipulate blocks of sampled data.
#define MEMORY_ALLOCATION_ERROR
malloc() or calloc() returned NULL
Error Handling for ECE 486 STM32L476G-Discovery Interface.
static HAL_StatusTypeDef Hacked_HAL_DAC_DualStart_DMA(DAC_HandleTypeDef *hdac, uint32_t *pData, uint32_t Length)
STM32L476-Discovery configuration routines to support ECE 486.
#define ERROR_LED
Red LED, STM32L476G-Discovery.
Mono Output: Only configure DAC1, single DMA Transfer.
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
static float Actual_Sampling_Frequency
Num_Channels_Out
Number of output audio channels.
void EXTI2_IRQHandler(void)
static void init_dac(uint16_t fs, enum Num_Channels_Out chanout)
void EXTI1_IRQHandler(void)
Initialization of OPAMP1 to buffer DAC Ch 1 to an output pin.
Stereo Input: Configure ADC1 and ADC2, dual DMA Transfer.
static ADC_HandleTypeDef AdcHandle2