/** ****************************************************************************** * @file bsp.c * @author * @version V0.1.0 * @date 27-January-2017 * @brief This file provides set of firmware functions to ANCHOR board. ****************************************************************************** * * * * * * * * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "bsp.h" #include "utils.h" /** Macro definations * */ /** * @brief BSP Driver version number V0.1.0 */ /** Global variables * */ extern uint32_t SystemCoreClock; /** Private Variables * */ #if defined(HAL_GPIO_MODULE_ENABLED) GPIO_TypeDef* GPIO_PORT[LED_NUM] = {LED_UWB_GPIO_PORT, LED_COM_GPIO_PORT, LED_BAT_GPIO_PORT, LED_RUN_GPIO_PORT}; const uint16_t GPIO_PIN[LED_NUM] = {LED_UWB_PIN, LED_COM_PIN, LED_BAT_PIN, LED_RUN_PIN}; /* GPIO_TypeDef* BUTTON_PORT[BUTTON_NUM] = {KEYUP_BUTTON_GPIO_PORT, KEY0_BUTTON_GPIO_PORT, KEY1_BUTTON_GPIO_PORT}; const uint16_t BUTTON_PIN[BUTTON_NUM] = {KEYUP_BUTTON_PIN, KEY0_BUTTON_PIN, KEY1_BUTTON_PIN}; const uint8_t BUTTON_IRQn[BUTTON_NUM] = {KEYUP_BUTTON_EXTI_IRQn, KEY0_BUTTON_EXTI_IRQn, KEY1_BUTTON_EXTI_IRQn};*/ GPIO_TypeDef* BUTTON_PORT[BUTTON_NUM] = { KEY1_BUTTON_GPIO_PORT, KEY2_BUTTON_GPIO_PORT}; const uint16_t BUTTON_PIN[BUTTON_NUM] = { KEY1_BUTTON_PIN, KEY2_BUTTON_PIN}; const uint8_t BUTTON_IRQn[BUTTON_NUM] = { KEY1_BUTTON_EXTI_IRQn, KEY2_BUTTON_EXTI_IRQn}; const uint8_t BUTTON_IRQn_Priority[BUTTON_NUM] = {KEY1_BUTTON_EXTI_IRQn_PRIORITY, KEY2_BUTTON_EXTI_IRQn_PRIORITY}; #endif #if defined(HAL_SPI_MODULE_ENABLED) const uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*ICER[index] = 0xFFFFFFFF; NVIC->ICPR[index] = 0xFFFFFFFF; } for(index = 0; index < 240; index++) { NVIC->IP[index] = 0x00000000; } } void BSP_IAP_APP_Jump(void) { uint32_t SpInitVal; uint32_t JumpAddr; ACTION_ENTRY pFun; SpInitVal = *(uint32_t *)IAP_ADDRESS; JumpAddr = *(uint32_t *)( IAP_ADDRESS + 4); if((SpInitVal & STACK_MASK) == MEM_RAM_BASE)//Check top of stack { //__disable_irq(); //HAL_RCC_DeInit(); //NVIC_DeInit(); BSP_ResetModules(); pFun = (ACTION_ENTRY)JumpAddr; //System_SetVector(IAP_ADDRESS); __set_MSP(SpInitVal); pFun(); } } void BSP_Jumpto_APP(void) { uint32_t IapSpInitVal; uint32_t IapJumpAddr; ACTION_ENTRY pIapFun; IapSpInitVal = *(uint32_t *)APP_ADDRESS; IapJumpAddr = *(uint32_t *)(APP_ADDRESS + 4); if((IapSpInitVal & STACK_MASK) == MEM_RAM_BASE)//Check top of stack { //__disable_irq(); //HAL_RCC_DeInit(); //NVIC_DeInit(); BSP_ResetModules(); pIapFun = (ACTION_ENTRY)IapJumpAddr; //System_SetVector(APP_ADDRESS); __set_MSP(IapSpInitVal); pIapFun(); } } void BSP_ResetModules(void) { BSP_W5500_HW_Reset(); BSP_DWM_Reset(); BSP_RAK_Reset(); #if defined(HAL_I2C_MODULE_ENABLED) BSP_RTC_Reset(); #endif } /** * @brief This function is executed in case of error occurrence. * @param None * @retval None */ /*void Error_Handler(void) { while(1) { } }*/ #endif /********************************* clock ***************************************/ #if defined(HAL_RCC_MODULE_ENABLED) /** * @brief System Clock Configuration * The system Clock is configured as follow : * System Clock source = PLL (HSE) * SYSCLK(Hz) = 160000000 * HCLK(Hz) = 160000000 * AHB Prescaler = 1; 160MHZ * APB1 Prescaler = 4 40MHZ * APB2 Prescaler = 2 80MHZ * HSE Frequency(Hz) = 8000000 * PLL_M = 8 * PLL_N = 320 * PLL_P = 2 * PLL_Q = 7 * VDD(V) = 3.3 * Main regulator output voltage = Scale1 mode * Flash Latency(WS) = 5 * @param None * @retval None */ void BSP_SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; #if RTC_ENABLE==1 #ifdef RTC_CLOCK_SOURCE_LSE RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.LSIState = RCC_LSI_OFF; #else RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; #endif #endif RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; /* PLL_VCO = (HSE_VALUE / PLL_M) * PLL_N; (HSE_VALUE / PLL_M):1~2MHZ; PLL_VCO: 192~432MHZ; SYSCLK = PLL_VCO / PLL_P; HIDEVCLK = PLL_VCO / PLL_Q; For OTG FS, SDIO and RNG clocks; */ RCC_OscInitStruct.PLL.PLLM = 8; //RCC_OscInitStruct.PLL.PLLN = 336;//168MHZ RCC_OscInitStruct.PLL.PLLN = 320;//160MHZ RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7;//24MHZ when PLL out is 168MHZ! if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { //Error_Handler(); } /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 to be enabled SysClk=HCLK=PLLCLK<=168MHZ; clocks dividers: PCLK1<=42MHZ; PCLK2<=84MHZ; */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK) { //Error_Handler(); } /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */ if (HAL_GetREVID() == 0x1001) { /* Enable the Flash prefetch */ __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); } } #endif /********************************* IDWG ***************************************/ #if defined(HAL_IWDG_MODULE_ENABLED) /* * @brief IWDG Initialize and start * @param none * @retval None */ void BSP_IWDG_Init(void) { /*IWDG counter clock Frequency = LsiFreq/PreScaler (8bits) Counter Reload Value(12bits) = Periond / IWDG counter clock period ReloadValue = 2s / (256/LsiFreq) = LsiFreq / 128 ReloadValue = 4s / (256/LsiFreq) = LsiFreq / 64 */ BSP_IwdgHandle.Instance = IWDG; BSP_IwdgHandle.Init.Prescaler = IWDG_PRESCALER_256; BSP_IwdgHandle.Init.Reload = (LSI_VALUE / 64); if(HAL_IWDG_Init(&BSP_IwdgHandle) != HAL_OK) { /* Initialization Error */ //Error_Handler(); } } /* * @brief IWDG Reload * @param none * @retval None * */ void BSP_IWDG_Reload(void) { if(HAL_IWDG_Refresh(&BSP_IwdgHandle) != HAL_OK) { /* Refresh Error */ //Error_Handler(); } } /** * @brief IWDG MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - LSI enable * @param hwwdg: IWDG handle pointer * @retval None */ void HAL_IWDG_MspInit(IWDG_HandleTypeDef* hiwdg) { __HAL_RCC_PWR_CLK_ENABLE(); HAL_PWR_EnableBkUpAccess(); /*## Enable peripherals and GPIO Clocks ####################################*/ /* RCC LSI clock enable */ __HAL_RCC_LSI_ENABLE(); /* Wait till LSI is ready */ while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) { } } #endif /******************************* LEDs & Keys ***********************************/ #if defined(HAL_GPIO_MODULE_ENABLED) /** * @brief Configures LED GPIO. * @param Led: Specifies the Led to be configured. * This parameter can be one of following parameters: * @arg LED2 * @arg LED3 */ void BSP_LED_Init(Led_TypeDef Led) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* Enable the GPIO_LED Clock */ LEDx_GPIO_CLK_ENABLE(Led); /* Configure the GPIO_LED pin */ GPIO_InitStruct.Pin = GPIO_PIN[Led]; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct); HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); } /** * @brief Turns selected LED On. * @param Led: Specifies the Led to be set on. * This parameter can be one of following parameters: * @arg LED2 * @arg LED3 */ void BSP_LED_On(Led_TypeDef Led) { HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); } /** * @brief Turns selected LED Off. * @param Led: Specifies the Led to be set off. * This parameter can be one of following parameters: * @arg LED2 * @arg LED3 */ void BSP_LED_Off(Led_TypeDef Led) { HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); } /** * @brief Toggles the selected LED. * @param Led: Specifies the Led to be toggled. * This parameter can be one of following parameters: * @arg LED2 * @arg LED3 */ void BSP_LED_Toggle(Led_TypeDef Led) { HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]); } /** * @} */ /** @defgroup STM32F4_LOW_LEVEL_BUTTON_Functions STM32F4 DISCOVERY LOW LEVEL BUTTON Functions * @{ */ /** * @brief Configures Button GPIO and EXTI Line. * @param Button: Specifies the Button to be configured. * This parameter should be: BUTTON_KEY * @param Mode: Specifies Button mode. * This parameter can be one of following parameters: * @arg BUTTON_MODE_GPIO: Button will be used as simple IO * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt * generation capability */ void BSP_Button_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable the BUTTON Clock */ BUTTONx_GPIO_CLK_ENABLE(Button); GPIO_InitStruct.Pin = BUTTON_PIN[Button]; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; /*if(Button == BUTTON_KEYUP) { GPIO_InitStruct.Pull = GPIO_PULLDOWN; } else*/ { GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLUP; } if (Mode == BUTTON_MODE_GPIO) { /* Configure Button pin as input */ GPIO_InitStruct.Mode = GPIO_MODE_INPUT; HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct); } else if (Mode == BUTTON_MODE_EXTI) { /* Configure Button pin as input with External interrupt */ GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct); /* Enable and set Button EXTI Interrupt to the lowest priority */ HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), BUTTON_IRQn_Priority[Button], 0); HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button])); } } /** * @brief Returns the selected Button state. * @param Button: Specifies the Button to be checked. * This parameter should be: BUTTON_KEY * @retval The Button GPIO pin value. */ uint32_t BSP_Button_GetState(Button_TypeDef Button) { return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]); } void BSP_DDLED_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOB_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } void BSP_DDLED_Power_0n(void) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET); } void BSP_DDLED_Power_Off(void) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET); } void BSP_DDLED_UWB_On(void) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET); } void BSP_DDLED_UWB_Off(void) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET); } /* * */ void BSP_BAT_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; BSP_BAT_STATUS_CLK_ENABLE(); //status GPIO_InitStruct.Pin = BSP_BAT_STATUS_STANDBY_PIN | BSP_BAT_STATUS_CHARGING_PIN; GPIO_InitStruct.Pull = GPIO_PULLUP; ////HW request GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(BSP_BAT_STATUS_PORT, &GPIO_InitStruct); /*BSP_BAT_STOP_CLK_ENABLE(); //stop GPIO_InitStruct.Pin = BSP_BAT_STOP_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(BSP_BAT_STOP_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(BSP_BAT_STOP_PORT, BSP_BAT_STOP_PIN, GPIO_PIN_RESET);*/ } /* CD<-->CHRG# CM<-->STDBY# status CD=0 CM=1 In charging CD=1 CM=0 Battery Fully Charged CD=1 CM=1 DC input in not connected CD=Toggle CM=0 No battary connected */ uint16_t BSP_BAT_UpdateStatus(void) { uint16_t status = 0; if(!HAL_GPIO_ReadPin(BSP_BAT_STATUS_PORT, BSP_BAT_STATUS_CHARGING_PIN)) { status |= 1; } if(!HAL_GPIO_ReadPin(BSP_BAT_STATUS_PORT, BSP_BAT_STATUS_STANDBY_PIN)) { status |= 2; } return status; } /* void BSP_BAT_StartCharge(void) { HAL_GPIO_WritePin(BSP_BAT_STOP_PORT, BSP_BAT_STOP_PIN, GPIO_PIN_SET); } void BSP_BAT_StopCharge(void) { HAL_GPIO_WritePin(BSP_BAT_STOP_PORT, BSP_BAT_STOP_PIN, GPIO_PIN_RESET); } */ void BSP_WDG_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; BSP_WATCHDOG_CLK_ENABLE(); //stop GPIO_InitStruct.Pin = BSP_WATCHDOG_PIN; //GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(BSP_WATCHDOG_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(BSP_WATCHDOG_PORT, BSP_WATCHDOG_PIN, GPIO_PIN_RESET); } void BSP_WDG_Clear(void) { //HAL_GPIO_WritePin(BSP_WATCHDOG_PORT, BSP_WATCHDOG_PIN, GPIO_PIN_SET); HAL_GPIO_TogglePin(BSP_WATCHDOG_PORT, BSP_WATCHDOG_PIN); } #endif /******************************** RTC ******************************************/ #if defined(HAL_RTC_MODULE_ENABLED) /** * @} */ HAL_StatusTypeDef BSP_RTC_Init(void) { uint32_t counter = 0; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; /* Enable PWR and BKP clocks */ //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); HAL_PWR_EnableBkUpAccess(); #ifdef RTC_CLOCK_SOURCE_LSE /* Configue LSE as RTC clock soucre */ PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; #elif defined (RTC_CLOCK_SOURCE_LSI) PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; #else #error Please select the RTC Clock source #endif /* RTC_CLOCK_SOURCE_LSE */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; hRTC_Handle.Instance = RTC; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK) { /* Enable RTC Clock */ __HAL_RCC_RTC_ENABLE(); /* The time base should be 1ms Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK \ LSE as RTC clock Time base = ((31 + 1) * (0 + 1)) / 32.768Khz = ~1ms LSI as RTC clock Time base = ((31 + 1) * (0 + 1)) / 32Khz = 1ms */ hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24; hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV; hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE; hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&hRTC_Handle); /* Disable the write protection for RTC registers */ __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle); /* Disable the Wake-up Timer */ __HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle); /* In case of interrupt mode is used, the interrupt source must disabled */ __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle,RTC_IT_WUT); /* Wait till RTC WUTWF flag is set */ while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET) { if(counter++ == SystemCoreClock /48U) { return HAL_ERROR; } } /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear RTC Wake Up timer Flag */ __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF); /* Configure the Wake-up Timer counter */ hRTC_Handle.Instance->WUTR = (uint32_t)RTC_WAKEUP_COUNTER; /* Clear the Wake-up Timer clock source bits in CR register */ hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL; /* Configure the clock source */ hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUP_CLOCK; /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */ __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT(); __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE(); /* Configure the Interrupt in the RTC_CR register */ __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle,RTC_IT_WUT); /* Enable the Wake-up Timer */ __HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle); /* Enable the write protection for RTC registers */ __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle); /* Set Interrupt Group Priority */ HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); HAL_NVIC_SetPriority(RTC_WKUP_IRQn, RTC_WAKEUP_PRIORITY, 0U); HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn); return HAL_OK; } return HAL_ERROR; } void BSP_RTC_Update(void) { HAL_RTC_GetTime(&hRTC_Handle, ¤tTime, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hRTC_Handle, ¤tDate, RTC_FORMAT_BIN); } #endif /******************************* SPI Routines **********************************/ #if defined(HAL_SPI_MODULE_ENABLED) void DWM_SPI_Init(uint8_t high) { //HAL_SPI_DeInit(&DWM_SpiHandle); /*##-1- Configure the SPI peripheral #######################################*/ /* Set the SPI parameters */ DWM_SpiHandle.Instance = DWM_SPIx; DWM_SpiHandle.Init.Mode = SPI_MODE_MASTER; DWM_SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; DWM_SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; DWM_SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; DWM_SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; DWM_SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; DWM_SpiHandle.Init.NSS = SPI_NSS_SOFT; DWM_SpiHandle.Init.BaudRatePrescaler = high ? DWM_SPIx_HIGHSPEED_PRESCALER : DWM_SPIx_LOWSPEED_PRESCALER; DWM_SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; DWM_SpiHandle.Init.CRCPolynomial = 7; DWM_SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; if(HAL_SPI_Init(&DWM_SpiHandle) != HAL_OK) { /* Initialization Error */ //Error_Handler(); } DWM_SPI_MspInit(&DWM_SpiHandle); } void BSP_DWM_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; DWM_SPI_Init(0); //cs DWM_SPIx_CS_GPIO_CLK_ENABLE(); GPIO_InitStruct.Pin = DWM_SPIx_CS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; //GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = 0; HAL_GPIO_Init(DWM_SPIx_CS_GPIO_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(DWM_SPIx_CS_GPIO_PORT, DWM_SPIx_CS_PIN, GPIO_PIN_SET); //wake DWM_WAKEUP_CLK_ENABLE(); GPIO_InitStruct.Pin = DWM_WAKEUP_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(DWM_WAKEUP_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(DWM_WAKEUP_PORT, DWM_WAKEUP_PIN, GPIO_PIN_RESET); //reset DWM_RST_CLK_ENABLE(); GPIO_InitStruct.Pin = DWM_RST_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(DWM_RST_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(DWM_RST_PORT, DWM_RST_PIN, GPIO_PIN_RESET); //irq DWM_IRQ_CLK_ENABLE(); GPIO_InitStruct.Pin = DWM_IRQ_PIN; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;//MPW3 IRQ polarity is high by default //GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLDOWN;//should be Pull Down GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = 0; HAL_GPIO_Init(DWM_IRQ_PORT, &GPIO_InitStruct); HAL_NVIC_SetPriority(DWM_IRQ_EXTI, DWM_IRQ_EXTI_PRIORITY, 0); HAL_NVIC_EnableIRQ(DWM_IRQ_EXTI); } void BSP_DWM_SPI_FastRate(uint16_t isHigh) { #if 1 DWM_SPI_Init(isHigh); #else DWM_SpiHandle.Init.BaudRatePrescaler = scalingfactor; if(HAL_SPI_Init(&DWM_SpiHandle) != HAL_OK) { //Error_Handler(); } #endif } /** * @brief SPI MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - NVIC configuration for SPI interrupt request enable * @param hspi: SPI handle pointer * @retval None */ void DWM_SPI_MspInit(SPI_HandleTypeDef *hspi) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock */ DWM_SPIx_GPIO_CLK_ENABLE(); /* Enable SPI clock */ DWM_SPIx_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* SPI SCK GPIO pin configuration */ GPIO_InitStruct.Pin = DWM_SPIx_SCK_PIN | DWM_SPIx_MOSI_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN;//GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = DWM_SPIx_GPIO_AF; HAL_GPIO_Init(DWM_SPIx_GPIO_PORT, &GPIO_InitStruct); /* SPI MISO/MOSI GPIO pin configuration */ GPIO_InitStruct.Pin = DWM_SPIx_MISO_PIN; //GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = DWM_SPIx_GPIO_AF; HAL_GPIO_Init(DWM_SPIx_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the NVIC for SPI #########################################*/ /* NVIC for SPI */ HAL_NVIC_SetPriority(DWM_SPIx_IRQn, DWM_SPIx_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(DWM_SPIx_IRQn); } /** * @brief SPI MSP De-Initialization * This function frees the hardware resources used in this example: * - Disable the Peripheral's clock * - Revert GPIO and NVIC configuration to their default state * @param hspi: SPI handle pointer * @retval None */ void DWM_SPI_MspDeInit(SPI_HandleTypeDef *hspi) { /*##-1- Reset peripherals ##################################################*/ DWM_SPIx_FORCE_RESET(); DWM_SPIx_RELEASE_RESET(); /*##-2- Disable peripherals and GPIO Clocks ################################*/ /* Configure SPI SCK as alternate function */ HAL_GPIO_DeInit(DWM_SPIx_GPIO_PORT, DWM_SPIx_SCK_PIN); /* Configure SPI MISO/MOSI as alternate function */ HAL_GPIO_DeInit(DWM_SPIx_GPIO_PORT, DWM_SPIx_MISO_PIN | DWM_SPIx_MOSI_PIN); /*##-3- Disable the NVIC for SPI ###########################################*/ HAL_NVIC_DisableIRQ(DWM_SPIx_IRQn); } void BSP_DWM_Reset(void) { // HAL_GPIO_WritePin(DWM_SPIx_CS_GPIO_PORT, DWM_SPIx_CS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(DWM_RST_PORT, DWM_RST_PIN, GPIO_PIN_RESET); DWM_SPI_MspDeInit(&DWM_SpiHandle); } /** * @brief SPIx Bus initialization */ static void W5500_SPIx_Init(void) { HAL_SPI_StateTypeDef status = HAL_SPI_STATE_RESET; /* Enable the SPI peripheral */ W5500_SPIx_CLK_ENABLE(); status = HAL_SPI_GetState(&W5500_SpiHandle); if(status == HAL_SPI_STATE_RESET) { /* SPI configuration -----------------------------------------------------*/ W5500_SpiHandle.Instance = W5500_SPIx; W5500_SpiHandle.Init.BaudRatePrescaler = W5500_SPIx_PRESCALER; W5500_SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; W5500_SpiHandle.Init.Mode = SPI_MODE_MASTER; W5500_SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; W5500_SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; W5500_SpiHandle.Init.NSS = SPI_NSS_SOFT; W5500_SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; W5500_SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; //W5500_SpiHandle.Init.CLKPhase = SPI_PHASE_2EDGE; //W5500_SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; W5500_SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; W5500_SpiHandle.Init.CRCPolynomial = 7; W5500_SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; W5500_SPIx_MspInit(); HAL_SPI_Init(&W5500_SpiHandle); __HAL_SPI_ENABLE(&W5500_SpiHandle); status = HAL_SPI_GetState(&W5500_SpiHandle); status++; } } /** * @brief SPI MSP Init. */ static void W5500_SPIx_MspInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /* SPI SCK, MOSI, MISO pin configuration */ W5500_SPIx_GPIO_CLK_ENABLE(); GPIO_InitStructure.Pin = (W5500_SPIx_SCLK_PIN | W5500_SPIx_MISO_PIN | W5500_SPIx_MOSI_PIN); GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLDOWN; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = W5500_SPIx_AF; HAL_GPIO_Init(W5500_SPIx_GPIO_PORT, &GPIO_InitStructure); /* SPI SCS, RST pin configuration */ W5500_SPIx_SCS_CLK_ENABLE(); GPIO_InitStructure.Pin = W5500_SPIx_SCS_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = 0; HAL_GPIO_Init(W5500_SPIx_SCS_PORT, &GPIO_InitStructure); HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_SET); W5500_CHIP_RESET_CLK_ENABLE(); GPIO_InitStructure.Pin = W5500_CHIP_RESET_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = 0; HAL_GPIO_Init(W5500_CHIP_RESET_PORT, &GPIO_InitStructure); HAL_GPIO_WritePin(W5500_CHIP_RESET_PORT, W5500_CHIP_RESET_PIN, GPIO_PIN_SET); /* SPI IRQ pin configuration */ W5500_EXTI_IRQ_CLK_ENABLE(); GPIO_InitStructure.Pin = W5500_EXTI_IRQ_PIN; GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(W5500_EXTI_IRQ_PORT, &GPIO_InitStructure); /* Enable and set Button EXTI Interrupt to the lowest priority */ HAL_NVIC_SetPriority(W5500_EXTI_IRQ_IRQn, W5500_EXIT_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(W5500_EXTI_IRQ_IRQn); } /** * @brief SPIx error treatment function. */ static void W5500_SPIx_Error(void) { /* De-initialize the SPI communication bus */ HAL_SPI_DeInit(&W5500_SpiHandle); /* Re-Initialize the SPI communication bus */ W5500_SPIx_Init(); } void BSP_W5500_HW_Init(void) { W5500_SPIx_Init(); BSP_W5500_HW_Reset(); } void BSP_W5500_HW_Reset(void) { HAL_GPIO_WritePin(W5500_CHIP_RESET_PORT, W5500_CHIP_RESET_PIN, GPIO_PIN_RESET); Delay_ms(2);//500us at least HAL_GPIO_WritePin(W5500_CHIP_RESET_PORT, W5500_CHIP_RESET_PIN, GPIO_PIN_SET); Delay_ms(1000); } void BSP_W5500_Enable(void) { HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_RESET); } void BSP_W5500_Disable(void) { HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_SET); } /** * @brief Sends a Byte through the SPI interface and return the Byte received * from the SPI bus. * @param Byte: Byte send. * @retval The received byte value */ int BSP_W5500_Write(uint8_t *pData, uint16_t Size) { HAL_StatusTypeDef status = HAL_SPI_Transmit(&W5500_SpiHandle, pData, Size, SpixTimeout); return status; } int BSP_W5500_Read(uint8_t *pData, uint16_t Size) { HAL_StatusTypeDef status = HAL_SPI_Receive(&W5500_SpiHandle, pData, Size, SpixTimeout); return status; } uint8_t BSP_W5500_WriteRead(uint8_t Byte) { uint8_t receivedbyte = 0; /* Send a Byte through the SPI peripheral */ /* Read byte from the SPI bus */ if(HAL_SPI_TransmitReceive(&W5500_SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK) { W5500_SPIx_Error(); } return receivedbyte; } /* * * * * * */ static void RAK_SPIx_MspInit(void) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable PORT Clock*/ RAK_SPIx_CS_GPIO_CLK_ENABLE(); RAK_SPIx_INT_GPIO_CLK_ENABLE(); RAK_Reset_GPIO_CLK_ENABLE(); /*Configure GPIO pin Output Level */ /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(RAK_SPIx_CS_GPIO_PORT, RAK_SPIx_CS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(RAK_Reset_GPIO_PORT, RAK_Reset_PIN, GPIO_PIN_RESET); /*##-2- Configure PA05 IO in output push-pull mode to drive external LED ###*/ GPIO_InitStruct.Pin = RAK_SPIx_CS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(RAK_SPIx_CS_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = RAK_Reset_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(RAK_Reset_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = RAK_SPIx_INT_PIN; //GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; // GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(RAK_SPIx_INT_GPIO_PORT, &GPIO_InitStruct); HAL_NVIC_SetPriority(RAK_SPI_INT_IRQ, RAK_SPI_INT_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(RAK_SPI_INT_IRQ); /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ RAK_SPIx_SCK_GPIO_CLK_ENABLE(); RAK_SPIx_MISO_GPIO_CLK_ENABLE(); RAK_SPIx_MOSI_GPIO_CLK_ENABLE(); /* Enable SPI clock */ RAK_SPIx_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* SPI SCK GPIO pin configuration */ GPIO_InitStruct.Pin = RAK_SPIx_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = RAK_SPIx_SCK_AF; HAL_GPIO_Init(RAK_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); /* SPI MISO GPIO pin configuration */ GPIO_InitStruct.Pin = RAK_SPIx_MISO_PIN; GPIO_InitStruct.Alternate = RAK_SPIx_MISO_AF; HAL_GPIO_Init(RAK_SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); /* SPI MOSI GPIO pin configuration */ GPIO_InitStruct.Pin = RAK_SPIx_MOSI_PIN; GPIO_InitStruct.Alternate = RAK_SPIx_MOSI_AF; HAL_GPIO_Init(RAK_SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); } /* *****************/ void BSP_RAK_SPI_Init(void) { HAL_SPI_StateTypeDef status = HAL_SPI_STATE_RESET; RAK_SPIx_CLK_ENABLE(); status = HAL_SPI_GetState(&Rak_SpiHandle); if(status == HAL_SPI_STATE_RESET) { Rak_SpiHandle.Instance = RAK_SPIx; Rak_SpiHandle.Init.BaudRatePrescaler = RAK_SPIx_PRESCALER; Rak_SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; Rak_SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;//SPI_PHASE_2EDGE; Rak_SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;//SPI_POLARITY_HIGH; Rak_SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; Rak_SpiHandle.Init.CRCPolynomial = 7; Rak_SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; Rak_SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; Rak_SpiHandle.Init.NSS = SPI_NSS_SOFT; Rak_SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; Rak_SpiHandle.Init.Mode = RAK_SPIx_MODE; /*if(HAL_SPI_Init(&Rak_SpiHandle) != HAL_OK) { // Initialization Error Error_Handler(); }*/ RAK_SPIx_MspInit(); HAL_SPI_Init(&Rak_SpiHandle); __HAL_SPI_ENABLE(&Rak_SpiHandle); status = HAL_SPI_GetState(&Rak_SpiHandle); status++; } } int BSP_Rak_Write(uint8_t *pData, uint16_t Size) { HAL_StatusTypeDef status = HAL_SPI_Transmit(&Rak_SpiHandle, pData, Size, SpixTimeout); return status; } int BSP_Rak_Read(uint8_t *pData, uint16_t Size) { HAL_StatusTypeDef status = HAL_SPI_Receive(&Rak_SpiHandle, pData, Size, SpixTimeout); return status; } int BSP_RAK_WriteRead(uint8_t byte, uint8_t* rdvalue) { HAL_StatusTypeDef temp = HAL_SPI_TransmitReceive(&Rak_SpiHandle, (uint8_t*)&byte, rdvalue, 1, 1000);//5000 if(temp != HAL_OK) { /* Transfer error in transmission process */ //Error_Handler(); } return temp; } void BSP_RAK_Reset(void) { RAK_Reset_Clear(); RAK_SPIx_CS_Clear(); RAK_SPIx_CLK_DISABLE(); __HAL_SPI_DISABLE(&Rak_SpiHandle); } #endif /******************************* UART Routines *********************************/ #if defined(HAL_UART_MODULE_ENABLED) /** * @brief UART MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - DMA configuration for transmission request by peripheral * - NVIC configuration for DMA interrupt request enable * @param huart: UART handle pointer * @retval None */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { #if UART_DMA_ENABLE==1 static DMA_HandleTypeDef hdma_tx; static DMA_HandleTypeDef hdma_rx; #endif GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance == MODULE_USART1) { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ /* Enable USARTx clock */ USART1_TX_GPIO_CLK_ENABLE(); USART1_RX_GPIO_CLK_ENABLE(); USART1_CLOCK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = USART1_TX_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = USART1_TX_GPIO_AF; HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = USART1_RX_GPIO_PIN; GPIO_InitStruct.Alternate = USART1_RX_GPIO_AF; HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct); #if UART_DMA_ENABLE==1 /*##-3- Configure the DMA streams ##########################################*/ /* Enable DMA1 clock */ USART1_DMA_CLK_ENABLE(); /* Configure the DMA handler for Transmission process */ hdma_tx.Instance = USART1_TX_DMA_STREAM; hdma_tx.Init.Channel = USART1_TX_DMA_CHANNEL; hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_tx.Init.Mode = DMA_NORMAL; hdma_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_tx.Init.MemBurst = DMA_MBURST_INC4; hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_tx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_tx); /* Configure the DMA handler for Transmission process */ hdma_rx.Instance = USART1_RX_DMA_STREAM; hdma_rx.Init.Channel = USART1_RX_DMA_CHANNEL; hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_rx.Init.Mode = DMA_NORMAL; hdma_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_rx.Init.MemBurst = DMA_MBURST_INC4; hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_rx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmarx, hdma_rx); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */ HAL_NVIC_SetPriority(USART1_DMA_TX_IRQn, USART1_DMA_TX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(USART1_DMA_TX_IRQn); /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */ HAL_NVIC_SetPriority(USART1_DMA_RX_IRQn, USART1_DMA_RX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(USART1_DMA_RX_IRQn); #endif /* NVIC configuration for USART TC interrupt */ HAL_NVIC_SetPriority(USART1_IRQn, USART1_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); } else if(huart->Instance == MODULE_USART2) { USART2_TX_GPIO_CLK_ENABLE(); USART2_RX_GPIO_CLK_ENABLE(); USART2_CLOCK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = USART2_TX_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = USART2_TX_GPIO_AF; HAL_GPIO_Init(USART2_TX_GPIO_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = USART2_RX_GPIO_PIN; GPIO_InitStruct.Alternate = USART2_RX_GPIO_AF; HAL_GPIO_Init(USART2_RX_GPIO_PORT, &GPIO_InitStruct); #if UART_DMA_ENABLE==1 /*##-3- Configure the DMA streams ##########################################*/ /* Enable DMA1 clock */ USART2_DMA_CLK_ENABLE(); /* Configure the DMA handler for Transmission process */ hdma_tx.Instance = USART2_TX_DMA_STREAM; hdma_tx.Init.Channel = USART2_TX_DMA_CHANNEL; hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_tx.Init.Mode = DMA_NORMAL; hdma_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_tx.Init.MemBurst = DMA_MBURST_INC4; hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_tx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_tx); /* Configure the DMA handler for Transmission process */ hdma_rx.Instance = USART2_RX_DMA_STREAM; hdma_rx.Init.Channel = USART2_RX_DMA_CHANNEL; hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_rx.Init.Mode = DMA_NORMAL; hdma_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_rx.Init.MemBurst = DMA_MBURST_INC4; hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_rx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmarx, hdma_rx); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */ HAL_NVIC_SetPriority(USART2_DMA_TX_IRQn, USART2_DMA_TX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(USART2_DMA_TX_IRQn); /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */ HAL_NVIC_SetPriority(USART2_DMA_RX_IRQn, USART2_DMA_RX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(USART2_DMA_RX_IRQn); #endif /* NVIC configuration for USART TC interrupt */ HAL_NVIC_SetPriority(USART2_IRQn, USART2_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); } else if(huart->Instance==USART3) { /* USER CODE BEGIN USART3_MspInit 0 */ /* USER CODE END USART3_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_USART3_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); /**USART3 GPIO Configuration PC10 ------> USART3_TX PC11 ------> USART3_RX */ GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* USART3 interrupt Init */ HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspInit 1 */ /* USER CODE END USART3_MspInit 1 */ } else if(huart->Instance == MODULE_UART4) { UART4_TX_GPIO_CLK_ENABLE(); UART4_RX_GPIO_CLK_ENABLE(); UART4_CLOCK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = UART4_TX_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = UART4_TX_GPIO_AF; HAL_GPIO_Init(UART4_TX_GPIO_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = UART4_RX_GPIO_PIN; GPIO_InitStruct.Alternate = UART4_RX_GPIO_AF; HAL_GPIO_Init(UART4_RX_GPIO_PORT, &GPIO_InitStruct); #if UART_DMA_ENABLE==1 /*##-3- Configure the DMA streams ##########################################*/ /* Enable DMA1 clock */ UART4_DMA_CLK_ENABLE(); /* Configure the DMA handler for Transmission process */ hdma_tx.Instance = UART4_TX_DMA_STREAM; hdma_tx.Init.Channel = UART4_TX_DMA_CHANNEL; hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_tx.Init.Mode = DMA_NORMAL; hdma_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_tx.Init.MemBurst = DMA_MBURST_INC4; hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_tx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_tx); /* Configure the DMA handler for Transmission process */ hdma_rx.Instance = UART4_RX_DMA_STREAM; hdma_rx.Init.Channel = UART4_RX_DMA_CHANNEL; hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_rx.Init.Mode = DMA_NORMAL; hdma_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_rx.Init.MemBurst = DMA_MBURST_INC4; hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_rx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmarx, hdma_rx); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */ HAL_NVIC_SetPriority(UART4_DMA_TX_IRQn, UART4_DMA_TX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART4_DMA_TX_IRQn); /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */ HAL_NVIC_SetPriority(UART4_DMA_RX_IRQn, UART4_DMA_RX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART4_DMA_RX_IRQn); #endif /* NVIC configuration for USART TC interrupt */ HAL_NVIC_SetPriority(UART4_IRQn, UART4_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART4_IRQn); } else if(huart->Instance == MODULE_UART5) { UART5_TX_GPIO_CLK_ENABLE(); UART5_RX_GPIO_CLK_ENABLE(); UART5_CLOCK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = UART5_TX_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = UART5_TX_GPIO_AF; HAL_GPIO_Init(UART5_TX_GPIO_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = UART5_RX_GPIO_PIN; GPIO_InitStruct.Alternate = UART5_RX_GPIO_AF; HAL_GPIO_Init(UART5_RX_GPIO_PORT, &GPIO_InitStruct); #if UART_DMA_ENABLE==1 /*##-3- Configure the DMA streams ##########################################*/ /* Enable DMA1 clock */ UART4_DMA_CLK_ENABLE(); /* Configure the DMA handler for Transmission process */ hdma_tx.Instance = UART5_TX_DMA_STREAM; hdma_tx.Init.Channel = UART5_TX_DMA_CHANNEL; hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_tx.Init.Mode = DMA_NORMAL; hdma_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_tx.Init.MemBurst = DMA_MBURST_INC4; hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_tx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_tx); /* Configure the DMA handler for Transmission process */ hdma_rx.Instance = UART5_RX_DMA_STREAM; hdma_rx.Init.Channel = UART5_RX_DMA_CHANNEL; hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_rx.Init.Mode = DMA_NORMAL; hdma_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_rx.Init.MemBurst = DMA_MBURST_INC4; hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_rx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmarx, hdma_rx); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */ HAL_NVIC_SetPriority(UART4_DMA_TX_IRQn, UART4_DMA_TX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART4_DMA_TX_IRQn); /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */ HAL_NVIC_SetPriority(UART4_DMA_RX_IRQn, UART4_DMA_RX_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART4_DMA_RX_IRQn); #endif /* NVIC configuration for USART TC interrupt */ HAL_NVIC_SetPriority(UART5_IRQn, UART5_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART5_IRQn); } } /** * @brief UART MSP De-Initialization * This function frees the hardware resources used in this example: * - Disable the Peripheral's clock * - Revert GPIO, DMA and NVIC configuration to their default state * @param huart: UART handle pointer * @retval None */ void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) { static DMA_HandleTypeDef hdma_tx; static DMA_HandleTypeDef hdma_rx; if(huart->Instance == MODULE_USART1) { /*##-1- Reset peripherals ##################################################*/ USART1_FORCE_RESET(); USART1_RELEASE_RESET(); /*##-2- Disable peripherals and GPIO Clocks #################################*/ /* Configure UART Tx as alternate function */ HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_GPIO_PIN); /* Configure UART Rx as alternate function */ HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_GPIO_PIN); /*##-3- Disable the DMA Streams ############################################*/ /* De-Initialize the DMA Stream associate to transmission process */ HAL_DMA_DeInit(&hdma_tx); /* De-Initialize the DMA Stream associate to reception process */ HAL_DMA_DeInit(&hdma_rx); /*##-4- Disable the NVIC for DMA ###########################################*/ HAL_NVIC_DisableIRQ(USART1_DMA_TX_IRQn); HAL_NVIC_DisableIRQ(USART1_DMA_RX_IRQn); } else if(huart->Instance == MODULE_USART2) { /*##-1- Reset peripherals ##################################################*/ USART2_FORCE_RESET(); USART2_RELEASE_RESET(); /*##-2- Disable peripherals and GPIO Clocks #################################*/ /* Configure UART Tx as alternate function */ HAL_GPIO_DeInit(USART2_TX_GPIO_PORT, USART2_TX_GPIO_PIN); /* Configure UART Rx as alternate function */ HAL_GPIO_DeInit(USART2_RX_GPIO_PORT, USART2_RX_GPIO_PIN); #if UART_DMA_ENABLE==1 /*##-3- Disable the DMA Streams ############################################*/ /* De-Initialize the DMA Stream associate to transmission process */ HAL_DMA_DeInit(&hdma_tx); /* De-Initialize the DMA Stream associate to reception process */ HAL_DMA_DeInit(&hdma_rx); /*##-4- Disable the NVIC for DMA ###########################################*/ HAL_NVIC_DisableIRQ(USART2_DMA_TX_IRQn); HAL_NVIC_DisableIRQ(USART2_DMA_RX_IRQn); #endif } /*else if(huart->Instance == MODULE_USART6) { USART6_FORCE_RESET(); USART6_RELEASE_RESET(); HAL_GPIO_DeInit(USART6_TX_GPIO_PORT, USART6_TX_GPIO_PIN); HAL_GPIO_DeInit(USART6_RX_GPIO_PORT, USART6_RX_GPIO_PIN); #if UART_DMA_ENABLE==1 HAL_DMA_DeInit(&hdma_tx); HAL_DMA_DeInit(&hdma_rx); HAL_NVIC_DisableIRQ(USART6_DMA_TX_IRQn); HAL_NVIC_DisableIRQ(USART6_DMA_RX_IRQn); #endif }*/ } /** * @brief Tx Transfer completed callback * @param UartHandle: UART handle. * @note This example shows a simple way to report end of DMA Tx transfer, and * you can add your own implementation. * @retval None */ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle) { /* Set transmission flag: transfer complete */ UartReady = SET; } /** * @brief Rx Transfer completed callback * @param UartHandle: UART handle * @note This example shows a simple way to report end of DMA Rx transfer, and * you can add your own implementation. * @retval None */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) { static int i=0; /* Set transmission flag: transfer complete */ UartReady = SET; if(UartHandle->Instance==MODULE_USART6) { i++; } } /** * @brief UART error callbacks * @param UartHandle: UART handle * @note This example shows a simple way to report transfer error, and you can * add your own implementation. * @retval None */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) { if(UartHandle->ErrorCode==HAL_UART_ERROR_PE) { } else if(UartHandle->ErrorCode== HAL_UART_ERROR_NE) { } else if(UartHandle->ErrorCode== HAL_UART_ERROR_FE) { } else if(UartHandle->ErrorCode== HAL_UART_ERROR_ORE) { } else if(UartHandle->ErrorCode== HAL_UART_ERROR_DMA) { ; } } /* * * */ void DebugUsart_Init(void) { /* Put the USART peripheral in the Asynchronous mode (UART Mode) UARTx configured as follow: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = None - BaudRate = 4000000 bps - Hardware flow control disabled (RTS and CTS signals) */ debug_UartHandle.Instance = DEBUG_PORT; debug_UartHandle.Init.BaudRate = DEBUG_BAUDRATE; debug_UartHandle.Init.WordLength = UART_WORDLENGTH_8B; debug_UartHandle.Init.StopBits = UART_STOPBITS_1; debug_UartHandle.Init.Parity = UART_PARITY_NONE; debug_UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; debug_UartHandle.Init.Mode = UART_MODE_TX_RX; debug_UartHandle.Init.OverSampling = UART_OVERSAMPLING_8; if(HAL_UART_Init(&debug_UartHandle) != HAL_OK) { //Error_Handler(); } } #if 0 void DebugMessage(uint8_t* msg) { int len=0; strcpy(aTxBuffer,msg); /*##-2- Start the transmission process #####################################*/ /* While the UART in reception process, user can transmit data through "aTxBuffer" buffer */ len = strlen(aTxBuffer); #if UART_DMA_ENABLE==1 if(HAL_UART_Transmit_DMA(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK) { //Error_Handler(); } #else if(HAL_UART_Transmit_IT(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK) { //Error_Handler(); } #endif /*##-3- Wait for the end of the transfer ###################################*/ while (UartReady != SET) { } /* Reset transmission flag */ UartReady = RESET; } void ReportMessage(uint8_t* msg, uint32_t len) { memcpy(aTxBuffer,msg,len); aTxBuffer[len] = 0; #if UART_DMA_ENABLE==1 if(HAL_UART_Transmit_DMA(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK) { //Error_Handler(); } #else if(HAL_UART_Transmit_IT(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK) { //Error_Handler(); } #endif /*##-3- Wait for the end of the transfer ###################################*/ while (UartReady != SET) { } /* Reset transmission flag */ UartReady = RESET; } #endif #endif /******************************* ADC Routines **********************************/ #if defined(HAL_ADC_MODULE_ENABLED) void BSP_ADC_Init(void) { /*##-1- Configure the ADC peripheral #######################################*/ BSP_AdcHandle.Instance = BSP_ADCx; BSP_AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8; //BSP_AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; BSP_AdcHandle.Init.Resolution = ADC_RESOLUTION_10B; BSP_AdcHandle.Init.ScanConvMode = DISABLE; BSP_AdcHandle.Init.ContinuousConvMode = DISABLE; BSP_AdcHandle.Init.DiscontinuousConvMode = DISABLE; BSP_AdcHandle.Init.NbrOfDiscConversion = 0; BSP_AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; BSP_AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; BSP_AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; BSP_AdcHandle.Init.NbrOfConversion = 1; BSP_AdcHandle.Init.DMAContinuousRequests = DISABLE; BSP_AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; if(HAL_ADC_Init(&BSP_AdcHandle) != HAL_OK) { /* Initialization Error */ //Error_Handler(); } } void BSP_ADC_START_Convert(eAdcChannels channel) { /* ### - 3 - Channel configuration ######################################## */ /* ### - 4 - Start conversion in Interrupt mode ########################### */ BSP_AdcConfig.Channel = channel; BSP_AdcConfig.Rank = 1; BSP_AdcConfig.Offset = 0; BSP_AdcConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES; if (HAL_ADC_ConfigChannel(&BSP_AdcHandle, &BSP_AdcConfig) == HAL_OK && HAL_ADC_Start_IT(&BSP_AdcHandle) == HAL_OK) { BSP_AdcStatus.lastChannel = BSP_AdcStatus.currentChannel; BSP_AdcStatus.lastValue = BSP_AdcStatus.currentValue; BSP_AdcStatus.currentChannel = channel; BSP_AdcStatus.currentValue = 0xffff; BSP_AdcStatus.status = ADC_CONVERT; } else { BSP_AdcStatus.status = ADC_ERROR; } } /** * @brief ADC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param huart: UART handle pointer * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC3 Periph clock enable */ BSP_ADCx_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ BSP_ADCx_CHAN_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC3 Channel8 GPIO pin configuration */ GPIO_InitStruct.Pin = BSP_ADCx_VBAT_PIN | BSP_ADCx_V5IN_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(BSP_ADCx_CHAN_PORT, &GPIO_InitStruct); /*##-3- Configure the NVIC #################################################*/ /* NVIC configuration for DMA transfer complete interrupt (USART1_TX) */ HAL_NVIC_SetPriority(BSP_ADCx_IRQn, BSP_ADCx_IRQ_PRIORITY, 0); HAL_NVIC_EnableIRQ(BSP_ADCx_IRQn); } /** * @brief ADC MSP De-Initialization * This function frees the hardware resources used in this example: * - Disable the Peripheral's clock * - Revert GPIO to their default state * @param hadc: ADC handle pointer * @retval None */ void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc) { /*##-1- Reset peripherals ##################################################*/ BSP_ADCx_FORCE_RESET(); BSP_ADCx_RELEASE_RESET(); /*##-2- Disable peripherals and GPIO Clocks ################################*/ HAL_GPIO_DeInit(BSP_ADCx_CHAN_PORT, BSP_ADCx_VBAT_PIN | BSP_ADCx_V5IN_PIN); } /** * @brief Conversion complete callback in non blocking mode * @param AdcHandle : AdcHandle handle * @note This example shows a simple way to report end of conversion, and * you can add your own implementation. * @retval None */ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle) { /* Get the converted value of regular channel */ BSP_AdcStatus.currentValue = HAL_ADC_GetValue(AdcHandle); //BSP_AdcStatus.currentValue >>= 2; BSP_AdcStatus.status = ADC_COMPLETE; } #endif /******************************* I2C Routines **********************************/ #if defined(HAL_I2C_MODULE_ENABLED) HAL_StatusTypeDef BSP_I2C_Init(void) { I2cHandle.Instance = BSP_I2Cx; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.ClockSpeed = BSP_I2Cx_SPEED; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; I2cHandle.Init.OwnAddress1 = BSP_I2Cx_SLAVE_ADDRESS; I2cHandle.Init.OwnAddress2 = 0xFE; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ //Error_Handler(); return HAL_ERROR; } return HAL_OK; } /** * @brief I2C MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - DMA configuration for transmission request by peripheral * - NVIC configuration for DMA interrupt request enable * @param hi2c: I2C handle pointer * @retval None */ void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable GPIO Clocks #################################################*/ /* Enable GPIO TX/RX clock */ BSP_I2Cx_SCL_GPIO_CLK_ENABLE(); BSP_I2Cx_SDA_GPIO_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* I2C TX GPIO pin configuration */ GPIO_InitStruct.Pin = BSP_I2Cx_SCL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = BSP_I2Cx_SCL_AF; HAL_GPIO_Init(BSP_I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct); /* I2C RX GPIO pin configuration */ GPIO_InitStruct.Pin = BSP_I2Cx_SDA_PIN; GPIO_InitStruct.Alternate = BSP_I2Cx_SDA_AF; HAL_GPIO_Init(BSP_I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct); /*##-3- Enable I2C peripheral Clock ########################################*/ /* Enable I2C1 clock */ BSP_I2Cx_CLK_ENABLE(); } /** * @brief I2C MSP De-Initialization * This function frees the hardware resources used in this example: * - Disable the Peripheral's clock * - Revert GPIO, DMA and NVIC configuration to their default state * @param hi2c: I2C handle pointer * @retval None */ void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c) { /*##-1- Reset peripherals ##################################################*/ BSP_I2Cx_FORCE_RESET(); BSP_I2Cx_RELEASE_RESET(); /*##-2- Disable peripherals and GPIO Clocks ################################*/ /* Configure I2C Tx as alternate function */ HAL_GPIO_DeInit(BSP_I2Cx_SCL_GPIO_PORT, BSP_I2Cx_SCL_PIN); /* Configure I2C Rx as alternate function */ HAL_GPIO_DeInit(BSP_I2Cx_SDA_GPIO_PORT, BSP_I2Cx_SDA_PIN); } void BSP_RTC_EXT_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable GPIO Clocks #################################################*/ BSP_RTC_INTERRUPT_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ GPIO_InitStruct.Pin = BSP_RTC_INTERRUPT_PIN; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = BSP_RTC_INTERRUPT_PULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(BSP_RTC_INTERRUPT_PORT, &GPIO_InitStruct); HAL_NVIC_SetPriority(BSP_RTC_INTERRUPT_IRQn, BSP_RTC_INTERRUPT_PRIORITY, 0); HAL_NVIC_EnableIRQ(BSP_RTC_INTERRUPT_IRQn); } void BSP_RTC_Reset(void) { GPIO_InitTypeDef GPIO_InitStruct; HAL_NVIC_DisableIRQ(BSP_RTC_INTERRUPT_IRQn); /*##-2- Configure peripheral GPIO ##########################################*/ GPIO_InitStruct.Pin = BSP_RTC_INTERRUPT_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(BSP_RTC_INTERRUPT_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(BSP_EEROM_WRITE_PROTECT_PORT, BSP_EEROM_WRITE_PROTECT_PIN, GPIO_PIN_SET); } void BSP_EEROM_EXT_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable GPIO Clocks #################################################*/ BSP_EEROM_WRITE_PROTECT_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ GPIO_InitStruct.Pin = BSP_EEROM_WRITE_PROTECT_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = BSP_EEROM_WRITE_PROTECT_PULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(BSP_EEROM_WRITE_PROTECT_PORT, &GPIO_InitStruct); BSP_EEROM_WRITE_FORBIDEN(); } int BSP_EEROM_Read(uint16_t MemAddress, uint16_t Size, uint8_t *pData) { HAL_StatusTypeDef status = HAL_ERROR; status = HAL_I2C_Mem_Read(&I2cHandle, BSP_EEROM_RD_ADDRESS, MemAddress, I2C_MEMADD_SIZE_16BIT, pData, Size, 1000); return status; } int BSP_EEROM_write(uint16_t MemAddress, uint16_t Size, uint8_t *pData) { HAL_StatusTypeDef status = HAL_ERROR; BSP_EEROM_WRITE_ENABLE(); status = HAL_I2C_Mem_Write(&I2cHandle, BSP_EEROM_WR_ADDRESS, MemAddress, I2C_MEMADD_SIZE_16BIT, pData, Size, 1000); BSP_EEROM_WRITE_FORBIDEN(); return status; } #endif /*******************************END OF FILE*************************************/