/** ********************************************************************************* * @file Peripheral.c * @author FMD AE * @brief Peripheral program body * @version V1.0.0 * @data 2021-09-27 ********************************************************************************* * @attention * COPYRIGHT (C) 2021 Fremont Micro Devices (SZ) Corporation All rights reserved. * This software is provided by the copyright holders and contributors,and the *software is believed to be accurate and reliable. However, Fremont Micro Devices *(SZ) Corporation assumes no responsibility for the consequences of use of such *software or for any infringement of patents of other rights of third parties, *which may result from its use. No license is granted by implication or otherwise *under any patent rights of Fremont Micro Devices (SZ) Corporation. * ****************************************************************************** */ /* Includes ----------------------------------------------------------------------*/ #include "Peripheral.h" /* Private Constant --------------------------------------------------------------*/ /* Public Constant ---------------------------------------------------------------*/ const uint16_t TICK_RATE_HZ = 100; /* Private typedef ---------------------------------------------------------------*/ /* Private variables -------------------------------------------------------------*/ uint16_t Channel1Pulse; uint16_t TimerPeriod; /* Public variables --------------------------------------------------------------*/ __IO uint32_t CaptureNumber, PeriodValue; __IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0; __IO uint32_t Timecount; __IO uint8_t usart_rx_buff[RX_SIZE]; /* Private function prototypes ---------------------------------------------------*/ static void DMA_USART_RX_Config(void); /* Public function ------ --------------------------------------------------------*/ /********************************************************************************** * @brief Usart_Init program * @param None * @note * @retval None ********************************************************************************* */ void Usart_Init(void) { USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); /*GPIO INIT*/ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1); USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1,&USART_InitStruct); USART_Cmd(USART1,ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); USART_ClearFlag(USART1,USART_FLAG_IDLE); DMA_USART_RX_Config(); USART_DMAReceptionErrorConfig(USART1,USART_DMAOnError_Enable); USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); DMA_Cmd(DMA1_Channel3,ENABLE); // printf("USART1 Init was complete!\r\n"); } /********************************************************************************** * @brief ADC_Config program * @param None * @note * @retval None ********************************************************************************* */ void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); ADC_VrefselConfig(ADC_Vrefsel_2_5V); ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); } /********************************************************************************** * @brief ADC_Measure program * @param None * @note * @retval None ********************************************************************************* */ void ADC_Measure(void) { uint32_t ADC_Max,ADC_Min,ADC_SUM = 0; uint32_t ADC_Buff[10]; for(uint8_t i = 0;i<10;i++) { /* Test EOC flag */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); /* Get ADC1 converted data */ ADC1ConvertedValue =ADC_GetConversionValue(ADC1); /* Compute the voltage */ ADC_Buff[i] = (ADC1ConvertedValue *2500)/0xFFF; } ADC_Max = ADC_Min = ADC_Buff[0]; for(uint8_t i = 0;i<10;i++) { ADC_SUM += ADC_Buff[i]; if(ADC_Buff[i]>ADC_Max)ADC_Max = ADC_Buff[i]; else if(ADC_Buff[i]CSR & RCC_CSR_LSIRDY) == 0); //wait LSI ready for(uint32_t i=1000;i>0;i--) { __ASM("NOP"); } IWDG_Enable(); IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); IWDG_SetPrescaler(IWDG_Prescaler_16); while(IWDG_GetFlagStatus(IWDG_FLAG_PVU) == SET); IWDG_SetReload(500); while(IWDG_GetFlagStatus(IWDG_FLAG_RVU) == SET); IWDG_ReloadCounter(); } /********************************************************************************** * @brief LED_Flash program * @param None * @note * @retval None ********************************************************************************* */ void LED_Flash(void) { LED_Toggle(LED_PORT,LED3_PIN); } /********************************************************************************** * @brief Key_Pro program * @param Key_Value * @note * @retval None ********************************************************************************* */ void Key_Pro(uint8_t Key_Value) { if(Key_Value) { switch(Key_Value) { case KEY1_Data: LED_Toggle(LED_PORT,LED4_PIN); if(Channel1Pulse100)Channel1Pulse -= 100; TIM_SetCompare1(TIM16,Channel1Pulse); printf("KEY2 PRESS, PWMPeriod = %02x, PWMPulse = %02x\r\n",TimerPeriod,Channel1Pulse); break; } } } /********************************************************************************** * @brief fputc program * @param int ch, FILE *f * @note * @retval None ********************************************************************************* */ int fputc(int ch, FILE *f) { while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET) { } USART_SendData(USART1,(uint8_t) ch); return ch; } /* Private function ------ -------------------------------------------------------*/ /********************************************************************************** * @brief DMA_USART_RX_Config program. * @param None * @note * @retval None ********************************************************************************* */ static void DMA_USART_RX_Config(void) { DMA_InitTypeDef DMA_InitStruct; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); DMA_InitStruct.DMA_BufferSize = RX_SIZE; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&usart_rx_buff[0]; DMA_InitStruct.DMA_MemoryDataSize = 8; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_Mode = DMA_Mode_Normal; DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART1->RDR; DMA_InitStruct.DMA_PeripheralDataSize = 8; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_Priority = DMA_Priority_Low; DMA_Init(DMA1_Channel3,&DMA_InitStruct); } /************************* (C) COPYRIGHT FMD *****END OF FILE*********************/