bsp.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /**
  2. ******************************************************************************
  3. * @file bsp.c
  4. * @author
  5. * @version V0.1.0
  6. * @date 27-January-2017
  7. * @brief This file provides set of firmware functions to ANCHOR board.
  8. ******************************************************************************
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "bsp.h"
  21. #include "utils.h"
  22. /** Macro definations
  23. *
  24. */
  25. /**
  26. * @brief BSP Driver version number V0.1.0
  27. */
  28. /** Global variables
  29. *
  30. */
  31. extern uint32_t SystemCoreClock;
  32. /** Private Variables
  33. *
  34. */
  35. #if defined(HAL_GPIO_MODULE_ENABLED)
  36. GPIO_TypeDef* GPIO_PORT[LED_NUM] = {LED_UWB_GPIO_PORT, LED_COM_GPIO_PORT, LED_BAT_GPIO_PORT, LED_RUN_GPIO_PORT};
  37. const uint16_t GPIO_PIN[LED_NUM] = {LED_UWB_PIN, LED_COM_PIN, LED_BAT_PIN, LED_RUN_PIN};
  38. /*
  39. GPIO_TypeDef* BUTTON_PORT[BUTTON_NUM] = {KEYUP_BUTTON_GPIO_PORT, KEY0_BUTTON_GPIO_PORT, KEY1_BUTTON_GPIO_PORT};
  40. const uint16_t BUTTON_PIN[BUTTON_NUM] = {KEYUP_BUTTON_PIN, KEY0_BUTTON_PIN, KEY1_BUTTON_PIN};
  41. const uint8_t BUTTON_IRQn[BUTTON_NUM] = {KEYUP_BUTTON_EXTI_IRQn, KEY0_BUTTON_EXTI_IRQn, KEY1_BUTTON_EXTI_IRQn};*/
  42. GPIO_TypeDef* BUTTON_PORT[BUTTON_NUM] = { KEY1_BUTTON_GPIO_PORT, KEY2_BUTTON_GPIO_PORT};
  43. const uint16_t BUTTON_PIN[BUTTON_NUM] = { KEY1_BUTTON_PIN, KEY2_BUTTON_PIN};
  44. const uint8_t BUTTON_IRQn[BUTTON_NUM] = { KEY1_BUTTON_EXTI_IRQn, KEY2_BUTTON_EXTI_IRQn};
  45. const uint8_t BUTTON_IRQn_Priority[BUTTON_NUM] = {KEY1_BUTTON_EXTI_IRQn_PRIORITY, KEY2_BUTTON_EXTI_IRQn_PRIORITY};
  46. #endif
  47. #if defined(HAL_SPI_MODULE_ENABLED)
  48. const uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
  49. /* SPI handler declaration */
  50. static SPI_HandleTypeDef W5500_SpiHandle = {0};
  51. static SPI_HandleTypeDef Rak_SpiHandle = {0};
  52. extern SPI_HandleTypeDef DWM_SpiHandle = {0};
  53. #endif
  54. #if defined(HAL_RTC_MODULE_ENABLED)
  55. RTC_HandleTypeDef hRTC_Handle;
  56. RTC_TimeTypeDef currentTime;
  57. RTC_DateTypeDef currentDate;
  58. #endif
  59. #if defined(HAL_ADC_MODULE_ENABLED)
  60. ADC_HandleTypeDef BSP_AdcHandle;
  61. ADC_ChannelConfTypeDef BSP_AdcConfig;
  62. stAdcConvertStatus BSP_AdcStatus;
  63. #endif
  64. #if defined(HAL_IWDG_MODULE_ENABLED)
  65. static IWDG_HandleTypeDef BSP_IwdgHandle;
  66. #endif
  67. #if defined(HAL_UART_MODULE_ENABLED)
  68. /* UART handler declaration */
  69. UART_HandleTypeDef debug_UartHandle = {0};
  70. __IO uint32_t UartReady = RESET;
  71. /* Private define ------------------------------------------------------------*/
  72. #define UARTBUFFERSIZE 256
  73. /* Private typedef -----------------------------------------------------------*/
  74. /* Buffer used for transmission */
  75. uint8_t aTxBuffer[UARTBUFFERSIZE] = {0};
  76. #endif
  77. #if defined(HAL_I2C_MODULE_ENABLED)
  78. I2C_HandleTypeDef I2cHandle;
  79. #endif
  80. /** Private Functions
  81. */
  82. static void W5500_SPIx_Init(void);
  83. static void W5500_SPIx_MspInit(void);
  84. static void W5500_SPIx_Error(void);
  85. //static uint8_t W5500_SPIx_WriteRead(uint8_t Byte);
  86. static void DWM_SPI_MspInit(SPI_HandleTypeDef *hspi);
  87. void BSP_ResetModules(void);
  88. void BSP_W5500_HW_Reset(void);
  89. void BSP_DWM_Reset(void);
  90. void BSP_RAK_Reset(void);
  91. void BSP_RTC_Reset(void);
  92. /****************************** Utils Functions ********************************/
  93. #if 1
  94. /*
  95. *reboot
  96. *
  97. */
  98. void BSP_Reboot(void)
  99. {
  100. /*ACTION_ENTRY Jump_To_Application;
  101. uint32_t JumpAddress;
  102. JumpAddress = *(volatile uint32_t*) (0x00000004);
  103. Jump_To_Application = (ACTION_ENTRY) JumpAddress;
  104. Jump_To_Application();*/
  105. NVIC_SystemReset();
  106. }
  107. void NVIC_DeInit(void)
  108. {
  109. uint32_t index = 0;
  110. for(index = 0; index < 8; index++)
  111. {
  112. NVIC->ICER[index] = 0xFFFFFFFF;
  113. NVIC->ICPR[index] = 0xFFFFFFFF;
  114. }
  115. for(index = 0; index < 240; index++)
  116. {
  117. NVIC->IP[index] = 0x00000000;
  118. }
  119. }
  120. void BSP_IAP_APP_Jump(void)
  121. {
  122. uint32_t SpInitVal;
  123. uint32_t JumpAddr;
  124. ACTION_ENTRY pFun;
  125. SpInitVal = *(uint32_t *)IAP_ADDRESS;
  126. JumpAddr = *(uint32_t *)( IAP_ADDRESS + 4);
  127. if((SpInitVal & STACK_MASK) == MEM_RAM_BASE)//Check top of stack
  128. {
  129. //__disable_irq();
  130. //HAL_RCC_DeInit();
  131. //NVIC_DeInit();
  132. BSP_ResetModules();
  133. pFun = (ACTION_ENTRY)JumpAddr;
  134. //System_SetVector(IAP_ADDRESS);
  135. __set_MSP(SpInitVal);
  136. pFun();
  137. }
  138. }
  139. void BSP_Jumpto_APP(void)
  140. {
  141. uint32_t IapSpInitVal;
  142. uint32_t IapJumpAddr;
  143. ACTION_ENTRY pIapFun;
  144. IapSpInitVal = *(uint32_t *)APP_ADDRESS;
  145. IapJumpAddr = *(uint32_t *)(APP_ADDRESS + 4);
  146. if((IapSpInitVal & STACK_MASK) == MEM_RAM_BASE)//Check top of stack
  147. {
  148. //__disable_irq();
  149. //HAL_RCC_DeInit();
  150. //NVIC_DeInit();
  151. BSP_ResetModules();
  152. pIapFun = (ACTION_ENTRY)IapJumpAddr;
  153. //System_SetVector(APP_ADDRESS);
  154. __set_MSP(IapSpInitVal);
  155. pIapFun();
  156. }
  157. }
  158. void BSP_ResetModules(void)
  159. {
  160. BSP_W5500_HW_Reset();
  161. BSP_DWM_Reset();
  162. BSP_RAK_Reset();
  163. #if defined(HAL_I2C_MODULE_ENABLED)
  164. BSP_RTC_Reset();
  165. #endif
  166. }
  167. /**
  168. * @brief This function is executed in case of error occurrence.
  169. * @param None
  170. * @retval None
  171. */
  172. /*void Error_Handler(void)
  173. {
  174. while(1)
  175. {
  176. }
  177. }*/
  178. #endif
  179. /********************************* clock ***************************************/
  180. #if defined(HAL_RCC_MODULE_ENABLED)
  181. /**
  182. * @brief System Clock Configuration
  183. * The system Clock is configured as follow :
  184. * System Clock source = PLL (HSE)
  185. * SYSCLK(Hz) = 160000000
  186. * HCLK(Hz) = 160000000
  187. * AHB Prescaler = 1; 160MHZ
  188. * APB1 Prescaler = 4 40MHZ
  189. * APB2 Prescaler = 2 80MHZ
  190. * HSE Frequency(Hz) = 8000000
  191. * PLL_M = 8
  192. * PLL_N = 320
  193. * PLL_P = 2
  194. * PLL_Q = 7
  195. * VDD(V) = 3.3
  196. * Main regulator output voltage = Scale1 mode
  197. * Flash Latency(WS) = 5
  198. * @param None
  199. * @retval None
  200. */
  201. void BSP_SystemClock_Config(void)
  202. {
  203. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  204. RCC_OscInitTypeDef RCC_OscInitStruct;
  205. /* Enable Power Control clock */
  206. __HAL_RCC_PWR_CLK_ENABLE();
  207. /* The voltage scaling allows optimizing the power consumption when the device is
  208. clocked below the maximum system frequency, to update the voltage scaling value
  209. regarding system frequency refer to product datasheet. */
  210. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  211. /* Enable HSE Oscillator and activate PLL with HSE as source */
  212. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  213. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  214. #if RTC_ENABLE==1
  215. #ifdef RTC_CLOCK_SOURCE_LSE
  216. RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSE;
  217. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  218. RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  219. #else
  220. RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI;
  221. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  222. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  223. #endif
  224. #endif
  225. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  226. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  227. /*
  228. PLL_VCO = (HSE_VALUE / PLL_M) * PLL_N; (HSE_VALUE / PLL_M):1~2MHZ; PLL_VCO: 192~432MHZ;
  229. SYSCLK = PLL_VCO / PLL_P;
  230. HIDEVCLK = PLL_VCO / PLL_Q; For OTG FS, SDIO and RNG clocks;
  231. */
  232. RCC_OscInitStruct.PLL.PLLM = 8;
  233. //RCC_OscInitStruct.PLL.PLLN = 336;//168MHZ
  234. RCC_OscInitStruct.PLL.PLLN = 320;//160MHZ
  235. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  236. RCC_OscInitStruct.PLL.PLLQ = 7;//24MHZ when PLL out is 168MHZ!
  237. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  238. {
  239. //Error_Handler();
  240. }
  241. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 to be enabled
  242. SysClk=HCLK=PLLCLK<=168MHZ;
  243. clocks dividers: PCLK1<=42MHZ; PCLK2<=84MHZ;
  244. */
  245. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  246. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  247. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  248. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  249. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  250. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  251. {
  252. //Error_Handler();
  253. }
  254. /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
  255. if (HAL_GetREVID() == 0x1001)
  256. {
  257. /* Enable the Flash prefetch */
  258. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  259. }
  260. }
  261. #endif
  262. /********************************* IDWG ***************************************/
  263. #if defined(HAL_IWDG_MODULE_ENABLED)
  264. /*
  265. * @brief IWDG Initialize and start
  266. * @param none
  267. * @retval None
  268. */
  269. void BSP_IWDG_Init(void)
  270. {
  271. /*IWDG counter clock Frequency = LsiFreq/PreScaler (8bits)
  272. Counter Reload Value(12bits) = Periond / IWDG counter clock period
  273. ReloadValue = 2s / (256/LsiFreq) = LsiFreq / 128
  274. ReloadValue = 4s / (256/LsiFreq) = LsiFreq / 64
  275. */
  276. BSP_IwdgHandle.Instance = IWDG;
  277. BSP_IwdgHandle.Init.Prescaler = IWDG_PRESCALER_256;
  278. BSP_IwdgHandle.Init.Reload = (LSI_VALUE / 64);
  279. if(HAL_IWDG_Init(&BSP_IwdgHandle) != HAL_OK)
  280. {
  281. /* Initialization Error */
  282. //Error_Handler();
  283. }
  284. }
  285. /*
  286. * @brief IWDG Reload
  287. * @param none
  288. * @retval None
  289. *
  290. */
  291. void BSP_IWDG_Reload(void)
  292. {
  293. if(HAL_IWDG_Refresh(&BSP_IwdgHandle) != HAL_OK)
  294. {
  295. /* Refresh Error */
  296. //Error_Handler();
  297. }
  298. }
  299. /**
  300. * @brief IWDG MSP Initialization
  301. * This function configures the hardware resources used in this example:
  302. * - Peripheral's clock enable
  303. * - LSI enable
  304. * @param hwwdg: IWDG handle pointer
  305. * @retval None
  306. */
  307. void HAL_IWDG_MspInit(IWDG_HandleTypeDef* hiwdg)
  308. {
  309. __HAL_RCC_PWR_CLK_ENABLE();
  310. HAL_PWR_EnableBkUpAccess();
  311. /*## Enable peripherals and GPIO Clocks ####################################*/
  312. /* RCC LSI clock enable */
  313. __HAL_RCC_LSI_ENABLE();
  314. /* Wait till LSI is ready */
  315. while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  316. {
  317. }
  318. }
  319. #endif
  320. /******************************* LEDs & Keys ***********************************/
  321. #if defined(HAL_GPIO_MODULE_ENABLED)
  322. /**
  323. * @brief Configures LED GPIO.
  324. * @param Led: Specifies the Led to be configured.
  325. * This parameter can be one of following parameters:
  326. * @arg LED2
  327. * @arg LED3
  328. */
  329. void BSP_LED_Init(Led_TypeDef Led)
  330. {
  331. GPIO_InitTypeDef GPIO_InitStruct = {0};
  332. /* Enable the GPIO_LED Clock */
  333. LEDx_GPIO_CLK_ENABLE(Led);
  334. /* Configure the GPIO_LED pin */
  335. GPIO_InitStruct.Pin = GPIO_PIN[Led];
  336. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  337. //GPIO_InitStruct.Pull = GPIO_PULLUP;
  338. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  339. HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
  340. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
  341. }
  342. /**
  343. * @brief Turns selected LED On.
  344. * @param Led: Specifies the Led to be set on.
  345. * This parameter can be one of following parameters:
  346. * @arg LED2
  347. * @arg LED3
  348. */
  349. void BSP_LED_On(Led_TypeDef Led)
  350. {
  351. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
  352. }
  353. /**
  354. * @brief Turns selected LED Off.
  355. * @param Led: Specifies the Led to be set off.
  356. * This parameter can be one of following parameters:
  357. * @arg LED2
  358. * @arg LED3
  359. */
  360. void BSP_LED_Off(Led_TypeDef Led)
  361. {
  362. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
  363. }
  364. /**
  365. * @brief Toggles the selected LED.
  366. * @param Led: Specifies the Led to be toggled.
  367. * This parameter can be one of following parameters:
  368. * @arg LED2
  369. * @arg LED3
  370. */
  371. void BSP_LED_Toggle(Led_TypeDef Led)
  372. {
  373. HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
  374. }
  375. /**
  376. * @}
  377. */
  378. /** @defgroup STM32F4_LOW_LEVEL_BUTTON_Functions STM32F4 DISCOVERY LOW LEVEL BUTTON Functions
  379. * @{
  380. */
  381. /**
  382. * @brief Configures Button GPIO and EXTI Line.
  383. * @param Button: Specifies the Button to be configured.
  384. * This parameter should be: BUTTON_KEY
  385. * @param Mode: Specifies Button mode.
  386. * This parameter can be one of following parameters:
  387. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  388. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  389. * generation capability
  390. */
  391. void BSP_Button_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode)
  392. {
  393. GPIO_InitTypeDef GPIO_InitStruct;
  394. /* Enable the BUTTON Clock */
  395. BUTTONx_GPIO_CLK_ENABLE(Button);
  396. GPIO_InitStruct.Pin = BUTTON_PIN[Button];
  397. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  398. /*if(Button == BUTTON_KEYUP)
  399. {
  400. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  401. }
  402. else*/
  403. {
  404. GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLUP;
  405. }
  406. if (Mode == BUTTON_MODE_GPIO)
  407. {
  408. /* Configure Button pin as input */
  409. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  410. HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
  411. }
  412. else if (Mode == BUTTON_MODE_EXTI)
  413. {
  414. /* Configure Button pin as input with External interrupt */
  415. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  416. HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
  417. /* Enable and set Button EXTI Interrupt to the lowest priority */
  418. HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), BUTTON_IRQn_Priority[Button], 0);
  419. HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  420. }
  421. }
  422. /**
  423. * @brief Returns the selected Button state.
  424. * @param Button: Specifies the Button to be checked.
  425. * This parameter should be: BUTTON_KEY
  426. * @retval The Button GPIO pin value.
  427. */
  428. uint32_t BSP_Button_GetState(Button_TypeDef Button)
  429. {
  430. return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  431. }
  432. void BSP_DDLED_Init(void)
  433. {
  434. GPIO_InitTypeDef GPIO_InitStruct;
  435. __HAL_RCC_GPIOB_CLK_ENABLE();
  436. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
  437. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  438. GPIO_InitStruct.Pull = GPIO_NOPULL;
  439. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  440. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  441. }
  442. void BSP_DDLED_Power_0n(void)
  443. {
  444. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  445. }
  446. void BSP_DDLED_Power_Off(void)
  447. {
  448. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  449. }
  450. void BSP_DDLED_UWB_On(void)
  451. {
  452. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
  453. }
  454. void BSP_DDLED_UWB_Off(void)
  455. {
  456. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
  457. }
  458. /*
  459. *
  460. */
  461. void BSP_BAT_Init(void)
  462. {
  463. GPIO_InitTypeDef GPIO_InitStruct = {0};
  464. BSP_BAT_STATUS_CLK_ENABLE();
  465. //status
  466. GPIO_InitStruct.Pin = BSP_BAT_STATUS_STANDBY_PIN | BSP_BAT_STATUS_CHARGING_PIN;
  467. GPIO_InitStruct.Pull = GPIO_PULLUP; ////HW request
  468. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  469. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  470. HAL_GPIO_Init(BSP_BAT_STATUS_PORT, &GPIO_InitStruct);
  471. /*BSP_BAT_STOP_CLK_ENABLE();
  472. //stop
  473. GPIO_InitStruct.Pin = BSP_BAT_STOP_PIN;
  474. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  475. GPIO_InitStruct.Pull = GPIO_NOPULL;
  476. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  477. HAL_GPIO_Init(BSP_BAT_STOP_PORT, &GPIO_InitStruct);
  478. HAL_GPIO_WritePin(BSP_BAT_STOP_PORT, BSP_BAT_STOP_PIN, GPIO_PIN_RESET);*/
  479. }
  480. /*
  481. CD<-->CHRG# CM<-->STDBY# status
  482. CD=0 CM=1 In charging
  483. CD=1 CM=0 Battery Fully Charged
  484. CD=1 CM=1 DC input in not connected
  485. CD=Toggle CM=0 No battary connected
  486. */
  487. uint16_t BSP_BAT_UpdateStatus(void)
  488. {
  489. uint16_t status = 0;
  490. if(!HAL_GPIO_ReadPin(BSP_BAT_STATUS_PORT, BSP_BAT_STATUS_CHARGING_PIN))
  491. {
  492. status |= 1;
  493. }
  494. if(!HAL_GPIO_ReadPin(BSP_BAT_STATUS_PORT, BSP_BAT_STATUS_STANDBY_PIN))
  495. {
  496. status |= 2;
  497. }
  498. return status;
  499. }
  500. /*
  501. void BSP_BAT_StartCharge(void)
  502. {
  503. HAL_GPIO_WritePin(BSP_BAT_STOP_PORT, BSP_BAT_STOP_PIN, GPIO_PIN_SET);
  504. }
  505. void BSP_BAT_StopCharge(void)
  506. {
  507. HAL_GPIO_WritePin(BSP_BAT_STOP_PORT, BSP_BAT_STOP_PIN, GPIO_PIN_RESET);
  508. }
  509. */
  510. void BSP_WDG_Init(void)
  511. {
  512. GPIO_InitTypeDef GPIO_InitStruct = {0};
  513. BSP_WATCHDOG_CLK_ENABLE();
  514. //stop
  515. GPIO_InitStruct.Pin = BSP_WATCHDOG_PIN;
  516. //GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  517. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  518. GPIO_InitStruct.Pull = GPIO_PULLUP;
  519. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  520. HAL_GPIO_Init(BSP_WATCHDOG_PORT, &GPIO_InitStruct);
  521. HAL_GPIO_WritePin(BSP_WATCHDOG_PORT, BSP_WATCHDOG_PIN, GPIO_PIN_RESET);
  522. }
  523. void BSP_WDG_Clear(void)
  524. {
  525. //HAL_GPIO_WritePin(BSP_WATCHDOG_PORT, BSP_WATCHDOG_PIN, GPIO_PIN_SET);
  526. HAL_GPIO_TogglePin(BSP_WATCHDOG_PORT, BSP_WATCHDOG_PIN);
  527. }
  528. #endif
  529. /******************************** RTC ******************************************/
  530. #if defined(HAL_RTC_MODULE_ENABLED)
  531. /**
  532. * @}
  533. */
  534. HAL_StatusTypeDef BSP_RTC_Init(void)
  535. {
  536. uint32_t counter = 0;
  537. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  538. /* Enable PWR and BKP clocks */
  539. //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  540. HAL_PWR_EnableBkUpAccess();
  541. #ifdef RTC_CLOCK_SOURCE_LSE
  542. /* Configue LSE as RTC clock soucre */
  543. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  544. #elif defined (RTC_CLOCK_SOURCE_LSI)
  545. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  546. #else
  547. #error Please select the RTC Clock source
  548. #endif /* RTC_CLOCK_SOURCE_LSE */
  549. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  550. hRTC_Handle.Instance = RTC;
  551. if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK)
  552. {
  553. /* Enable RTC Clock */
  554. __HAL_RCC_RTC_ENABLE();
  555. /* The time base should be 1ms
  556. Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK \
  557. LSE as RTC clock
  558. Time base = ((31 + 1) * (0 + 1)) / 32.768Khz
  559. = ~1ms
  560. LSI as RTC clock
  561. Time base = ((31 + 1) * (0 + 1)) / 32Khz
  562. = 1ms
  563. */
  564. hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
  565. hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
  566. hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
  567. hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
  568. hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  569. hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  570. HAL_RTC_Init(&hRTC_Handle);
  571. /* Disable the write protection for RTC registers */
  572. __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
  573. /* Disable the Wake-up Timer */
  574. __HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle);
  575. /* In case of interrupt mode is used, the interrupt source must disabled */
  576. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle,RTC_IT_WUT);
  577. /* Wait till RTC WUTWF flag is set */
  578. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET)
  579. {
  580. if(counter++ == SystemCoreClock /48U)
  581. {
  582. return HAL_ERROR;
  583. }
  584. }
  585. /* Clear PWR wake up Flag */
  586. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  587. /* Clear RTC Wake Up timer Flag */
  588. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF);
  589. /* Configure the Wake-up Timer counter */
  590. hRTC_Handle.Instance->WUTR = (uint32_t)RTC_WAKEUP_COUNTER;
  591. /* Clear the Wake-up Timer clock source bits in CR register */
  592. hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  593. /* Configure the clock source */
  594. hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUP_CLOCK;
  595. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  596. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  597. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  598. /* Configure the Interrupt in the RTC_CR register */
  599. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle,RTC_IT_WUT);
  600. /* Enable the Wake-up Timer */
  601. __HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle);
  602. /* Enable the write protection for RTC registers */
  603. __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
  604. /* Set Interrupt Group Priority */
  605. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  606. HAL_NVIC_SetPriority(RTC_WKUP_IRQn, RTC_WAKEUP_PRIORITY, 0U);
  607. HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
  608. return HAL_OK;
  609. }
  610. return HAL_ERROR;
  611. }
  612. void BSP_RTC_Update(void)
  613. {
  614. HAL_RTC_GetTime(&hRTC_Handle, &currentTime, RTC_FORMAT_BIN);
  615. HAL_RTC_GetDate(&hRTC_Handle, &currentDate, RTC_FORMAT_BIN);
  616. }
  617. #endif
  618. /******************************* SPI Routines **********************************/
  619. #if defined(HAL_SPI_MODULE_ENABLED)
  620. void DWM_SPI_Init(uint8_t high)
  621. {
  622. //HAL_SPI_DeInit(&DWM_SpiHandle);
  623. /*##-1- Configure the SPI peripheral #######################################*/
  624. /* Set the SPI parameters */
  625. DWM_SpiHandle.Instance = DWM_SPIx;
  626. DWM_SpiHandle.Init.Mode = SPI_MODE_MASTER;
  627. DWM_SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
  628. DWM_SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
  629. DWM_SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
  630. DWM_SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
  631. DWM_SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
  632. DWM_SpiHandle.Init.NSS = SPI_NSS_SOFT;
  633. DWM_SpiHandle.Init.BaudRatePrescaler = high ? DWM_SPIx_HIGHSPEED_PRESCALER : DWM_SPIx_LOWSPEED_PRESCALER;
  634. DWM_SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  635. DWM_SpiHandle.Init.CRCPolynomial = 7;
  636. DWM_SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
  637. if(HAL_SPI_Init(&DWM_SpiHandle) != HAL_OK)
  638. {
  639. /* Initialization Error */
  640. //Error_Handler();
  641. }
  642. DWM_SPI_MspInit(&DWM_SpiHandle);
  643. }
  644. void BSP_DWM_Init(void)
  645. {
  646. GPIO_InitTypeDef GPIO_InitStruct;
  647. DWM_SPI_Init(0);
  648. //cs
  649. DWM_SPIx_CS_GPIO_CLK_ENABLE();
  650. GPIO_InitStruct.Pin = DWM_SPIx_CS_PIN;
  651. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  652. GPIO_InitStruct.Pull = GPIO_PULLUP;
  653. //GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  654. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  655. GPIO_InitStruct.Alternate = 0;
  656. HAL_GPIO_Init(DWM_SPIx_CS_GPIO_PORT, &GPIO_InitStruct);
  657. HAL_GPIO_WritePin(DWM_SPIx_CS_GPIO_PORT, DWM_SPIx_CS_PIN, GPIO_PIN_SET);
  658. //wake
  659. DWM_WAKEUP_CLK_ENABLE();
  660. GPIO_InitStruct.Pin = DWM_WAKEUP_PIN;
  661. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  662. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  663. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  664. HAL_GPIO_Init(DWM_WAKEUP_PORT, &GPIO_InitStruct);
  665. HAL_GPIO_WritePin(DWM_WAKEUP_PORT, DWM_WAKEUP_PIN, GPIO_PIN_RESET);
  666. //reset
  667. DWM_RST_CLK_ENABLE();
  668. GPIO_InitStruct.Pin = DWM_RST_PIN;
  669. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//GPIO_MODE_INPUT;
  670. GPIO_InitStruct.Pull = GPIO_PULLUP;
  671. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  672. HAL_GPIO_Init(DWM_RST_PORT, &GPIO_InitStruct);
  673. HAL_GPIO_WritePin(DWM_RST_PORT, DWM_RST_PIN, GPIO_PIN_RESET);
  674. //irq
  675. DWM_IRQ_CLK_ENABLE();
  676. GPIO_InitStruct.Pin = DWM_IRQ_PIN;
  677. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;//MPW3 IRQ polarity is high by default
  678. //GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLDOWN;//should be Pull Down
  679. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  680. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  681. GPIO_InitStruct.Alternate = 0;
  682. HAL_GPIO_Init(DWM_IRQ_PORT, &GPIO_InitStruct);
  683. HAL_NVIC_SetPriority(DWM_IRQ_EXTI, DWM_IRQ_EXTI_PRIORITY, 0);
  684. HAL_NVIC_EnableIRQ(DWM_IRQ_EXTI);
  685. }
  686. void BSP_DWM_SPI_FastRate(uint16_t isHigh)
  687. {
  688. #if 1
  689. DWM_SPI_Init(isHigh);
  690. #else
  691. DWM_SpiHandle.Init.BaudRatePrescaler = scalingfactor;
  692. if(HAL_SPI_Init(&DWM_SpiHandle) != HAL_OK)
  693. {
  694. //Error_Handler();
  695. }
  696. #endif
  697. }
  698. /**
  699. * @brief SPI MSP Initialization
  700. * This function configures the hardware resources used in this example:
  701. * - Peripheral's clock enable
  702. * - Peripheral's GPIO Configuration
  703. * - NVIC configuration for SPI interrupt request enable
  704. * @param hspi: SPI handle pointer
  705. * @retval None
  706. */
  707. void DWM_SPI_MspInit(SPI_HandleTypeDef *hspi)
  708. {
  709. GPIO_InitTypeDef GPIO_InitStruct;
  710. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  711. /* Enable GPIO clock */
  712. DWM_SPIx_GPIO_CLK_ENABLE();
  713. /* Enable SPI clock */
  714. DWM_SPIx_CLK_ENABLE();
  715. /*##-2- Configure peripheral GPIO ##########################################*/
  716. /* SPI SCK GPIO pin configuration */
  717. GPIO_InitStruct.Pin = DWM_SPIx_SCK_PIN | DWM_SPIx_MOSI_PIN;
  718. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  719. GPIO_InitStruct.Pull = GPIO_PULLDOWN;//GPIO_PULLUP;
  720. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  721. GPIO_InitStruct.Alternate = DWM_SPIx_GPIO_AF;
  722. HAL_GPIO_Init(DWM_SPIx_GPIO_PORT, &GPIO_InitStruct);
  723. /* SPI MISO/MOSI GPIO pin configuration */
  724. GPIO_InitStruct.Pin = DWM_SPIx_MISO_PIN;
  725. //GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  726. GPIO_InitStruct.Pull = GPIO_PULLUP;
  727. GPIO_InitStruct.Alternate = DWM_SPIx_GPIO_AF;
  728. HAL_GPIO_Init(DWM_SPIx_GPIO_PORT, &GPIO_InitStruct);
  729. /*##-3- Configure the NVIC for SPI #########################################*/
  730. /* NVIC for SPI */
  731. HAL_NVIC_SetPriority(DWM_SPIx_IRQn, DWM_SPIx_IRQ_PRIORITY, 0);
  732. HAL_NVIC_EnableIRQ(DWM_SPIx_IRQn);
  733. }
  734. /**
  735. * @brief SPI MSP De-Initialization
  736. * This function frees the hardware resources used in this example:
  737. * - Disable the Peripheral's clock
  738. * - Revert GPIO and NVIC configuration to their default state
  739. * @param hspi: SPI handle pointer
  740. * @retval None
  741. */
  742. void DWM_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
  743. {
  744. /*##-1- Reset peripherals ##################################################*/
  745. DWM_SPIx_FORCE_RESET();
  746. DWM_SPIx_RELEASE_RESET();
  747. /*##-2- Disable peripherals and GPIO Clocks ################################*/
  748. /* Configure SPI SCK as alternate function */
  749. HAL_GPIO_DeInit(DWM_SPIx_GPIO_PORT, DWM_SPIx_SCK_PIN);
  750. /* Configure SPI MISO/MOSI as alternate function */
  751. HAL_GPIO_DeInit(DWM_SPIx_GPIO_PORT, DWM_SPIx_MISO_PIN | DWM_SPIx_MOSI_PIN);
  752. /*##-3- Disable the NVIC for SPI ###########################################*/
  753. HAL_NVIC_DisableIRQ(DWM_SPIx_IRQn);
  754. }
  755. void BSP_DWM_Reset(void)
  756. {
  757. //
  758. HAL_GPIO_WritePin(DWM_SPIx_CS_GPIO_PORT, DWM_SPIx_CS_PIN, GPIO_PIN_RESET);
  759. HAL_GPIO_WritePin(DWM_RST_PORT, DWM_RST_PIN, GPIO_PIN_RESET);
  760. DWM_SPI_MspDeInit(&DWM_SpiHandle);
  761. }
  762. /**
  763. * @brief SPIx Bus initialization
  764. */
  765. static void W5500_SPIx_Init(void)
  766. {
  767. HAL_SPI_StateTypeDef status = HAL_SPI_STATE_RESET;
  768. /* Enable the SPI peripheral */
  769. W5500_SPIx_CLK_ENABLE();
  770. status = HAL_SPI_GetState(&W5500_SpiHandle);
  771. if(status == HAL_SPI_STATE_RESET)
  772. {
  773. /* SPI configuration -----------------------------------------------------*/
  774. W5500_SpiHandle.Instance = W5500_SPIx;
  775. W5500_SpiHandle.Init.BaudRatePrescaler = W5500_SPIx_PRESCALER;
  776. W5500_SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
  777. W5500_SpiHandle.Init.Mode = SPI_MODE_MASTER;
  778. W5500_SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
  779. W5500_SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
  780. W5500_SpiHandle.Init.NSS = SPI_NSS_SOFT;
  781. W5500_SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
  782. W5500_SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
  783. //W5500_SpiHandle.Init.CLKPhase = SPI_PHASE_2EDGE;
  784. //W5500_SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
  785. W5500_SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  786. W5500_SpiHandle.Init.CRCPolynomial = 7;
  787. W5500_SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
  788. W5500_SPIx_MspInit();
  789. HAL_SPI_Init(&W5500_SpiHandle);
  790. __HAL_SPI_ENABLE(&W5500_SpiHandle);
  791. status = HAL_SPI_GetState(&W5500_SpiHandle);
  792. status++;
  793. }
  794. }
  795. /**
  796. * @brief SPI MSP Init.
  797. */
  798. static void W5500_SPIx_MspInit(void)
  799. {
  800. GPIO_InitTypeDef GPIO_InitStructure;
  801. /* SPI SCK, MOSI, MISO pin configuration */
  802. W5500_SPIx_GPIO_CLK_ENABLE();
  803. GPIO_InitStructure.Pin = (W5500_SPIx_SCLK_PIN | W5500_SPIx_MISO_PIN | W5500_SPIx_MOSI_PIN);
  804. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  805. GPIO_InitStructure.Pull = GPIO_PULLDOWN;
  806. GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  807. GPIO_InitStructure.Alternate = W5500_SPIx_AF;
  808. HAL_GPIO_Init(W5500_SPIx_GPIO_PORT, &GPIO_InitStructure);
  809. /* SPI SCS, RST pin configuration */
  810. W5500_SPIx_SCS_CLK_ENABLE();
  811. GPIO_InitStructure.Pin = W5500_SPIx_SCS_PIN;
  812. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  813. GPIO_InitStructure.Pull = GPIO_PULLUP;
  814. GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  815. GPIO_InitStructure.Alternate = 0;
  816. HAL_GPIO_Init(W5500_SPIx_SCS_PORT, &GPIO_InitStructure);
  817. HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_SET);
  818. W5500_CHIP_RESET_CLK_ENABLE();
  819. GPIO_InitStructure.Pin = W5500_CHIP_RESET_PIN;
  820. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  821. GPIO_InitStructure.Pull = GPIO_PULLUP;
  822. GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  823. GPIO_InitStructure.Alternate = 0;
  824. HAL_GPIO_Init(W5500_CHIP_RESET_PORT, &GPIO_InitStructure);
  825. HAL_GPIO_WritePin(W5500_CHIP_RESET_PORT, W5500_CHIP_RESET_PIN, GPIO_PIN_SET);
  826. /* SPI IRQ pin configuration */
  827. W5500_EXTI_IRQ_CLK_ENABLE();
  828. GPIO_InitStructure.Pin = W5500_EXTI_IRQ_PIN;
  829. GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
  830. GPIO_InitStructure.Pull = GPIO_PULLUP;
  831. GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  832. HAL_GPIO_Init(W5500_EXTI_IRQ_PORT, &GPIO_InitStructure);
  833. /* Enable and set Button EXTI Interrupt to the lowest priority */
  834. HAL_NVIC_SetPriority(W5500_EXTI_IRQ_IRQn, W5500_EXIT_IRQ_PRIORITY, 0);
  835. HAL_NVIC_EnableIRQ(W5500_EXTI_IRQ_IRQn);
  836. }
  837. /**
  838. * @brief SPIx error treatment function.
  839. */
  840. static void W5500_SPIx_Error(void)
  841. {
  842. /* De-initialize the SPI communication bus */
  843. HAL_SPI_DeInit(&W5500_SpiHandle);
  844. /* Re-Initialize the SPI communication bus */
  845. W5500_SPIx_Init();
  846. }
  847. void BSP_W5500_HW_Init(void)
  848. {
  849. W5500_SPIx_Init();
  850. BSP_W5500_HW_Reset();
  851. }
  852. void BSP_W5500_HW_Reset(void)
  853. {
  854. HAL_GPIO_WritePin(W5500_CHIP_RESET_PORT, W5500_CHIP_RESET_PIN, GPIO_PIN_RESET);
  855. Delay_ms(2);//500us at least
  856. HAL_GPIO_WritePin(W5500_CHIP_RESET_PORT, W5500_CHIP_RESET_PIN, GPIO_PIN_SET);
  857. Delay_ms(1000);
  858. }
  859. void BSP_W5500_Enable(void)
  860. {
  861. HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_RESET);
  862. HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_RESET);
  863. }
  864. void BSP_W5500_Disable(void)
  865. {
  866. HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_RESET);
  867. HAL_GPIO_WritePin(W5500_SPIx_SCS_PORT, W5500_SPIx_SCS_PIN, GPIO_PIN_SET);
  868. }
  869. /**
  870. * @brief Sends a Byte through the SPI interface and return the Byte received
  871. * from the SPI bus.
  872. * @param Byte: Byte send.
  873. * @retval The received byte value
  874. */
  875. int BSP_W5500_Write(uint8_t *pData, uint16_t Size)
  876. {
  877. HAL_StatusTypeDef status = HAL_SPI_Transmit(&W5500_SpiHandle, pData, Size, SpixTimeout);
  878. return status;
  879. }
  880. int BSP_W5500_Read(uint8_t *pData, uint16_t Size)
  881. {
  882. HAL_StatusTypeDef status = HAL_SPI_Receive(&W5500_SpiHandle, pData, Size, SpixTimeout);
  883. return status;
  884. }
  885. uint8_t BSP_W5500_WriteRead(uint8_t Byte)
  886. {
  887. uint8_t receivedbyte = 0;
  888. /* Send a Byte through the SPI peripheral */
  889. /* Read byte from the SPI bus */
  890. if(HAL_SPI_TransmitReceive(&W5500_SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
  891. {
  892. W5500_SPIx_Error();
  893. }
  894. return receivedbyte;
  895. }
  896. /*
  897. *
  898. *
  899. *
  900. *
  901. *
  902. */
  903. static void RAK_SPIx_MspInit(void)
  904. {
  905. GPIO_InitTypeDef GPIO_InitStruct;
  906. /*##-1- Enable PORT Clock*/
  907. RAK_SPIx_CS_GPIO_CLK_ENABLE();
  908. RAK_SPIx_INT_GPIO_CLK_ENABLE();
  909. RAK_Reset_GPIO_CLK_ENABLE();
  910. /*Configure GPIO pin Output Level */
  911. /*Configure GPIO pin Output Level */
  912. HAL_GPIO_WritePin(RAK_SPIx_CS_GPIO_PORT, RAK_SPIx_CS_PIN, GPIO_PIN_RESET);
  913. HAL_GPIO_WritePin(RAK_Reset_GPIO_PORT, RAK_Reset_PIN, GPIO_PIN_RESET);
  914. /*##-2- Configure PA05 IO in output push-pull mode to drive external LED ###*/
  915. GPIO_InitStruct.Pin = RAK_SPIx_CS_PIN;
  916. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  917. GPIO_InitStruct.Pull = GPIO_PULLUP;
  918. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  919. HAL_GPIO_Init(RAK_SPIx_CS_GPIO_PORT, &GPIO_InitStruct);
  920. GPIO_InitStruct.Pin = RAK_Reset_PIN;
  921. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  922. GPIO_InitStruct.Pull = GPIO_NOPULL;
  923. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  924. HAL_GPIO_Init(RAK_Reset_GPIO_PORT, &GPIO_InitStruct);
  925. GPIO_InitStruct.Pin = RAK_SPIx_INT_PIN;
  926. //GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  927. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  928. GPIO_InitStruct.Pull = GPIO_NOPULL;
  929. // GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  930. HAL_GPIO_Init(RAK_SPIx_INT_GPIO_PORT, &GPIO_InitStruct);
  931. HAL_NVIC_SetPriority(RAK_SPI_INT_IRQ, RAK_SPI_INT_IRQ_PRIORITY, 0);
  932. HAL_NVIC_EnableIRQ(RAK_SPI_INT_IRQ);
  933. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  934. /* Enable GPIO TX/RX clock */
  935. RAK_SPIx_SCK_GPIO_CLK_ENABLE();
  936. RAK_SPIx_MISO_GPIO_CLK_ENABLE();
  937. RAK_SPIx_MOSI_GPIO_CLK_ENABLE();
  938. /* Enable SPI clock */
  939. RAK_SPIx_CLK_ENABLE();
  940. /*##-2- Configure peripheral GPIO ##########################################*/
  941. /* SPI SCK GPIO pin configuration */
  942. GPIO_InitStruct.Pin = RAK_SPIx_SCK_PIN;
  943. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  944. GPIO_InitStruct.Pull = GPIO_PULLUP;
  945. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  946. GPIO_InitStruct.Alternate = RAK_SPIx_SCK_AF;
  947. HAL_GPIO_Init(RAK_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
  948. /* SPI MISO GPIO pin configuration */
  949. GPIO_InitStruct.Pin = RAK_SPIx_MISO_PIN;
  950. GPIO_InitStruct.Alternate = RAK_SPIx_MISO_AF;
  951. HAL_GPIO_Init(RAK_SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);
  952. /* SPI MOSI GPIO pin configuration */
  953. GPIO_InitStruct.Pin = RAK_SPIx_MOSI_PIN;
  954. GPIO_InitStruct.Alternate = RAK_SPIx_MOSI_AF;
  955. HAL_GPIO_Init(RAK_SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);
  956. }
  957. /*
  958. *****************/
  959. void BSP_RAK_SPI_Init(void)
  960. {
  961. HAL_SPI_StateTypeDef status = HAL_SPI_STATE_RESET;
  962. RAK_SPIx_CLK_ENABLE();
  963. status = HAL_SPI_GetState(&Rak_SpiHandle);
  964. if(status == HAL_SPI_STATE_RESET)
  965. {
  966. Rak_SpiHandle.Instance = RAK_SPIx;
  967. Rak_SpiHandle.Init.BaudRatePrescaler = RAK_SPIx_PRESCALER;
  968. Rak_SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
  969. Rak_SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;//SPI_PHASE_2EDGE;
  970. Rak_SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;//SPI_POLARITY_HIGH;
  971. Rak_SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  972. Rak_SpiHandle.Init.CRCPolynomial = 7;
  973. Rak_SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
  974. Rak_SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
  975. Rak_SpiHandle.Init.NSS = SPI_NSS_SOFT;
  976. Rak_SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
  977. Rak_SpiHandle.Init.Mode = RAK_SPIx_MODE;
  978. /*if(HAL_SPI_Init(&Rak_SpiHandle) != HAL_OK)
  979. {
  980. // Initialization Error
  981. Error_Handler();
  982. }*/
  983. RAK_SPIx_MspInit();
  984. HAL_SPI_Init(&Rak_SpiHandle);
  985. __HAL_SPI_ENABLE(&Rak_SpiHandle);
  986. status = HAL_SPI_GetState(&Rak_SpiHandle);
  987. status++;
  988. }
  989. }
  990. int BSP_Rak_Write(uint8_t *pData, uint16_t Size)
  991. {
  992. HAL_StatusTypeDef status = HAL_SPI_Transmit(&Rak_SpiHandle, pData, Size, SpixTimeout);
  993. return status;
  994. }
  995. int BSP_Rak_Read(uint8_t *pData, uint16_t Size)
  996. {
  997. HAL_StatusTypeDef status = HAL_SPI_Receive(&Rak_SpiHandle, pData, Size, SpixTimeout);
  998. return status;
  999. }
  1000. int BSP_RAK_WriteRead(uint8_t byte, uint8_t* rdvalue)
  1001. {
  1002. HAL_StatusTypeDef temp = HAL_SPI_TransmitReceive(&Rak_SpiHandle, (uint8_t*)&byte, rdvalue, 1, 1000);//5000
  1003. if(temp != HAL_OK)
  1004. {
  1005. /* Transfer error in transmission process */
  1006. //Error_Handler();
  1007. }
  1008. return temp;
  1009. }
  1010. void BSP_RAK_Reset(void)
  1011. {
  1012. RAK_Reset_Clear();
  1013. RAK_SPIx_CS_Clear();
  1014. RAK_SPIx_CLK_DISABLE();
  1015. __HAL_SPI_DISABLE(&Rak_SpiHandle);
  1016. }
  1017. #endif
  1018. /******************************* UART Routines *********************************/
  1019. #if defined(HAL_UART_MODULE_ENABLED)
  1020. /**
  1021. * @brief UART MSP Initialization
  1022. * This function configures the hardware resources used in this example:
  1023. * - Peripheral's clock enable
  1024. * - Peripheral's GPIO Configuration
  1025. * - DMA configuration for transmission request by peripheral
  1026. * - NVIC configuration for DMA interrupt request enable
  1027. * @param huart: UART handle pointer
  1028. * @retval None
  1029. */
  1030. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  1031. {
  1032. #if UART_DMA_ENABLE==1
  1033. static DMA_HandleTypeDef hdma_tx;
  1034. static DMA_HandleTypeDef hdma_rx;
  1035. #endif
  1036. GPIO_InitTypeDef GPIO_InitStruct;
  1037. if(huart->Instance == MODULE_USART1)
  1038. {
  1039. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  1040. /* Enable GPIO TX/RX clock */
  1041. /* Enable USARTx clock */
  1042. USART1_TX_GPIO_CLK_ENABLE();
  1043. USART1_RX_GPIO_CLK_ENABLE();
  1044. USART1_CLOCK_ENABLE();
  1045. /*##-2- Configure peripheral GPIO ##########################################*/
  1046. /* UART TX GPIO pin configuration */
  1047. GPIO_InitStruct.Pin = USART1_TX_GPIO_PIN;
  1048. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1049. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1050. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1051. GPIO_InitStruct.Alternate = USART1_TX_GPIO_AF;
  1052. HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct);
  1053. /* UART RX GPIO pin configuration */
  1054. GPIO_InitStruct.Pin = USART1_RX_GPIO_PIN;
  1055. GPIO_InitStruct.Alternate = USART1_RX_GPIO_AF;
  1056. HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct);
  1057. #if UART_DMA_ENABLE==1
  1058. /*##-3- Configure the DMA streams ##########################################*/
  1059. /* Enable DMA1 clock */
  1060. USART1_DMA_CLK_ENABLE();
  1061. /* Configure the DMA handler for Transmission process */
  1062. hdma_tx.Instance = USART1_TX_DMA_STREAM;
  1063. hdma_tx.Init.Channel = USART1_TX_DMA_CHANNEL;
  1064. hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  1065. hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  1066. hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
  1067. hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1068. hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1069. hdma_tx.Init.Mode = DMA_NORMAL;
  1070. hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
  1071. hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1072. hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1073. hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
  1074. hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  1075. HAL_DMA_Init(&hdma_tx);
  1076. /* Associate the initialized DMA handle to the the UART handle */
  1077. __HAL_LINKDMA(huart, hdmatx, hdma_tx);
  1078. /* Configure the DMA handler for Transmission process */
  1079. hdma_rx.Instance = USART1_RX_DMA_STREAM;
  1080. hdma_rx.Init.Channel = USART1_RX_DMA_CHANNEL;
  1081. hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  1082. hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  1083. hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
  1084. hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1085. hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1086. hdma_rx.Init.Mode = DMA_NORMAL;
  1087. hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
  1088. hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1089. hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1090. hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
  1091. hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  1092. HAL_DMA_Init(&hdma_rx);
  1093. /* Associate the initialized DMA handle to the the UART handle */
  1094. __HAL_LINKDMA(huart, hdmarx, hdma_rx);
  1095. /*##-4- Configure the NVIC for DMA #########################################*/
  1096. /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
  1097. HAL_NVIC_SetPriority(USART1_DMA_TX_IRQn, USART1_DMA_TX_IRQ_PRIORITY, 0);
  1098. HAL_NVIC_EnableIRQ(USART1_DMA_TX_IRQn);
  1099. /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
  1100. HAL_NVIC_SetPriority(USART1_DMA_RX_IRQn, USART1_DMA_RX_IRQ_PRIORITY, 0);
  1101. HAL_NVIC_EnableIRQ(USART1_DMA_RX_IRQn);
  1102. #endif
  1103. /* NVIC configuration for USART TC interrupt */
  1104. HAL_NVIC_SetPriority(USART1_IRQn, USART1_IRQ_PRIORITY, 0);
  1105. HAL_NVIC_EnableIRQ(USART1_IRQn);
  1106. }
  1107. else if(huart->Instance == MODULE_USART2)
  1108. {
  1109. USART2_TX_GPIO_CLK_ENABLE();
  1110. USART2_RX_GPIO_CLK_ENABLE();
  1111. USART2_CLOCK_ENABLE();
  1112. /*##-2- Configure peripheral GPIO ##########################################*/
  1113. /* UART TX GPIO pin configuration */
  1114. GPIO_InitStruct.Pin = USART2_TX_GPIO_PIN;
  1115. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1116. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1117. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1118. GPIO_InitStruct.Alternate = USART2_TX_GPIO_AF;
  1119. HAL_GPIO_Init(USART2_TX_GPIO_PORT, &GPIO_InitStruct);
  1120. /* UART RX GPIO pin configuration */
  1121. GPIO_InitStruct.Pin = USART2_RX_GPIO_PIN;
  1122. GPIO_InitStruct.Alternate = USART2_RX_GPIO_AF;
  1123. HAL_GPIO_Init(USART2_RX_GPIO_PORT, &GPIO_InitStruct);
  1124. #if UART_DMA_ENABLE==1
  1125. /*##-3- Configure the DMA streams ##########################################*/
  1126. /* Enable DMA1 clock */
  1127. USART2_DMA_CLK_ENABLE();
  1128. /* Configure the DMA handler for Transmission process */
  1129. hdma_tx.Instance = USART2_TX_DMA_STREAM;
  1130. hdma_tx.Init.Channel = USART2_TX_DMA_CHANNEL;
  1131. hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  1132. hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  1133. hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
  1134. hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1135. hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1136. hdma_tx.Init.Mode = DMA_NORMAL;
  1137. hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
  1138. hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1139. hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1140. hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
  1141. hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  1142. HAL_DMA_Init(&hdma_tx);
  1143. /* Associate the initialized DMA handle to the the UART handle */
  1144. __HAL_LINKDMA(huart, hdmatx, hdma_tx);
  1145. /* Configure the DMA handler for Transmission process */
  1146. hdma_rx.Instance = USART2_RX_DMA_STREAM;
  1147. hdma_rx.Init.Channel = USART2_RX_DMA_CHANNEL;
  1148. hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  1149. hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  1150. hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
  1151. hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1152. hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1153. hdma_rx.Init.Mode = DMA_NORMAL;
  1154. hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
  1155. hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1156. hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1157. hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
  1158. hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  1159. HAL_DMA_Init(&hdma_rx);
  1160. /* Associate the initialized DMA handle to the the UART handle */
  1161. __HAL_LINKDMA(huart, hdmarx, hdma_rx);
  1162. /*##-4- Configure the NVIC for DMA #########################################*/
  1163. /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
  1164. HAL_NVIC_SetPriority(USART2_DMA_TX_IRQn, USART2_DMA_TX_IRQ_PRIORITY, 0);
  1165. HAL_NVIC_EnableIRQ(USART2_DMA_TX_IRQn);
  1166. /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
  1167. HAL_NVIC_SetPriority(USART2_DMA_RX_IRQn, USART2_DMA_RX_IRQ_PRIORITY, 0);
  1168. HAL_NVIC_EnableIRQ(USART2_DMA_RX_IRQn);
  1169. #endif
  1170. /* NVIC configuration for USART TC interrupt */
  1171. HAL_NVIC_SetPriority(USART2_IRQn, USART2_IRQ_PRIORITY, 0);
  1172. HAL_NVIC_EnableIRQ(USART2_IRQn);
  1173. }
  1174. else if(huart->Instance==USART3)
  1175. {
  1176. /* USER CODE BEGIN USART3_MspInit 0 */
  1177. /* USER CODE END USART3_MspInit 0 */
  1178. /* Peripheral clock enable */
  1179. __HAL_RCC_USART3_CLK_ENABLE();
  1180. __HAL_RCC_GPIOC_CLK_ENABLE();
  1181. /**USART3 GPIO Configuration
  1182. PC10 ------> USART3_TX
  1183. PC11 ------> USART3_RX
  1184. */
  1185. GPIO_InitStruct.Pin = GPIO_PIN_11;
  1186. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1187. GPIO_InitStruct.Pull = GPIO_PULLUP;
  1188. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1189. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  1190. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  1191. GPIO_InitStruct.Pin = GPIO_PIN_10;
  1192. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1193. GPIO_InitStruct.Pull = GPIO_PULLUP;
  1194. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1195. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  1196. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  1197. /* USART3 interrupt Init */
  1198. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  1199. HAL_NVIC_EnableIRQ(USART3_IRQn);
  1200. /* USER CODE BEGIN USART3_MspInit 1 */
  1201. /* USER CODE END USART3_MspInit 1 */
  1202. }
  1203. else if(huart->Instance == MODULE_UART4)
  1204. {
  1205. UART4_TX_GPIO_CLK_ENABLE();
  1206. UART4_RX_GPIO_CLK_ENABLE();
  1207. UART4_CLOCK_ENABLE();
  1208. /*##-2- Configure peripheral GPIO ##########################################*/
  1209. /* UART TX GPIO pin configuration */
  1210. GPIO_InitStruct.Pin = UART4_TX_GPIO_PIN;
  1211. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1212. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1213. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1214. GPIO_InitStruct.Alternate = UART4_TX_GPIO_AF;
  1215. HAL_GPIO_Init(UART4_TX_GPIO_PORT, &GPIO_InitStruct);
  1216. /* UART RX GPIO pin configuration */
  1217. GPIO_InitStruct.Pin = UART4_RX_GPIO_PIN;
  1218. GPIO_InitStruct.Alternate = UART4_RX_GPIO_AF;
  1219. HAL_GPIO_Init(UART4_RX_GPIO_PORT, &GPIO_InitStruct);
  1220. #if UART_DMA_ENABLE==1
  1221. /*##-3- Configure the DMA streams ##########################################*/
  1222. /* Enable DMA1 clock */
  1223. UART4_DMA_CLK_ENABLE();
  1224. /* Configure the DMA handler for Transmission process */
  1225. hdma_tx.Instance = UART4_TX_DMA_STREAM;
  1226. hdma_tx.Init.Channel = UART4_TX_DMA_CHANNEL;
  1227. hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  1228. hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  1229. hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
  1230. hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1231. hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1232. hdma_tx.Init.Mode = DMA_NORMAL;
  1233. hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
  1234. hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1235. hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1236. hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
  1237. hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  1238. HAL_DMA_Init(&hdma_tx);
  1239. /* Associate the initialized DMA handle to the the UART handle */
  1240. __HAL_LINKDMA(huart, hdmatx, hdma_tx);
  1241. /* Configure the DMA handler for Transmission process */
  1242. hdma_rx.Instance = UART4_RX_DMA_STREAM;
  1243. hdma_rx.Init.Channel = UART4_RX_DMA_CHANNEL;
  1244. hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  1245. hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  1246. hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
  1247. hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1248. hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1249. hdma_rx.Init.Mode = DMA_NORMAL;
  1250. hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
  1251. hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1252. hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1253. hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
  1254. hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  1255. HAL_DMA_Init(&hdma_rx);
  1256. /* Associate the initialized DMA handle to the the UART handle */
  1257. __HAL_LINKDMA(huart, hdmarx, hdma_rx);
  1258. /*##-4- Configure the NVIC for DMA #########################################*/
  1259. /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
  1260. HAL_NVIC_SetPriority(UART4_DMA_TX_IRQn, UART4_DMA_TX_IRQ_PRIORITY, 0);
  1261. HAL_NVIC_EnableIRQ(UART4_DMA_TX_IRQn);
  1262. /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
  1263. HAL_NVIC_SetPriority(UART4_DMA_RX_IRQn, UART4_DMA_RX_IRQ_PRIORITY, 0);
  1264. HAL_NVIC_EnableIRQ(UART4_DMA_RX_IRQn);
  1265. #endif
  1266. /* NVIC configuration for USART TC interrupt */
  1267. HAL_NVIC_SetPriority(UART4_IRQn, UART4_IRQ_PRIORITY, 0);
  1268. HAL_NVIC_EnableIRQ(UART4_IRQn);
  1269. }
  1270. else if(huart->Instance == MODULE_UART5)
  1271. {
  1272. UART5_TX_GPIO_CLK_ENABLE();
  1273. UART5_RX_GPIO_CLK_ENABLE();
  1274. UART5_CLOCK_ENABLE();
  1275. /*##-2- Configure peripheral GPIO ##########################################*/
  1276. /* UART TX GPIO pin configuration */
  1277. GPIO_InitStruct.Pin = UART5_TX_GPIO_PIN;
  1278. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1279. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1280. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1281. GPIO_InitStruct.Alternate = UART5_TX_GPIO_AF;
  1282. HAL_GPIO_Init(UART5_TX_GPIO_PORT, &GPIO_InitStruct);
  1283. /* UART RX GPIO pin configuration */
  1284. GPIO_InitStruct.Pin = UART5_RX_GPIO_PIN;
  1285. GPIO_InitStruct.Alternate = UART5_RX_GPIO_AF;
  1286. HAL_GPIO_Init(UART5_RX_GPIO_PORT, &GPIO_InitStruct);
  1287. #if UART_DMA_ENABLE==1
  1288. /*##-3- Configure the DMA streams ##########################################*/
  1289. /* Enable DMA1 clock */
  1290. UART4_DMA_CLK_ENABLE();
  1291. /* Configure the DMA handler for Transmission process */
  1292. hdma_tx.Instance = UART5_TX_DMA_STREAM;
  1293. hdma_tx.Init.Channel = UART5_TX_DMA_CHANNEL;
  1294. hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  1295. hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  1296. hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
  1297. hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1298. hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1299. hdma_tx.Init.Mode = DMA_NORMAL;
  1300. hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
  1301. hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1302. hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1303. hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
  1304. hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  1305. HAL_DMA_Init(&hdma_tx);
  1306. /* Associate the initialized DMA handle to the the UART handle */
  1307. __HAL_LINKDMA(huart, hdmatx, hdma_tx);
  1308. /* Configure the DMA handler for Transmission process */
  1309. hdma_rx.Instance = UART5_RX_DMA_STREAM;
  1310. hdma_rx.Init.Channel = UART5_RX_DMA_CHANNEL;
  1311. hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  1312. hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  1313. hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
  1314. hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  1315. hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  1316. hdma_rx.Init.Mode = DMA_NORMAL;
  1317. hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
  1318. hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  1319. hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  1320. hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
  1321. hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  1322. HAL_DMA_Init(&hdma_rx);
  1323. /* Associate the initialized DMA handle to the the UART handle */
  1324. __HAL_LINKDMA(huart, hdmarx, hdma_rx);
  1325. /*##-4- Configure the NVIC for DMA #########################################*/
  1326. /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
  1327. HAL_NVIC_SetPriority(UART4_DMA_TX_IRQn, UART4_DMA_TX_IRQ_PRIORITY, 0);
  1328. HAL_NVIC_EnableIRQ(UART4_DMA_TX_IRQn);
  1329. /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
  1330. HAL_NVIC_SetPriority(UART4_DMA_RX_IRQn, UART4_DMA_RX_IRQ_PRIORITY, 0);
  1331. HAL_NVIC_EnableIRQ(UART4_DMA_RX_IRQn);
  1332. #endif
  1333. /* NVIC configuration for USART TC interrupt */
  1334. HAL_NVIC_SetPriority(UART5_IRQn, UART5_IRQ_PRIORITY, 0);
  1335. HAL_NVIC_EnableIRQ(UART5_IRQn);
  1336. }
  1337. }
  1338. /**
  1339. * @brief UART MSP De-Initialization
  1340. * This function frees the hardware resources used in this example:
  1341. * - Disable the Peripheral's clock
  1342. * - Revert GPIO, DMA and NVIC configuration to their default state
  1343. * @param huart: UART handle pointer
  1344. * @retval None
  1345. */
  1346. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  1347. {
  1348. static DMA_HandleTypeDef hdma_tx;
  1349. static DMA_HandleTypeDef hdma_rx;
  1350. if(huart->Instance == MODULE_USART1)
  1351. {
  1352. /*##-1- Reset peripherals ##################################################*/
  1353. USART1_FORCE_RESET();
  1354. USART1_RELEASE_RESET();
  1355. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  1356. /* Configure UART Tx as alternate function */
  1357. HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_GPIO_PIN);
  1358. /* Configure UART Rx as alternate function */
  1359. HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_GPIO_PIN);
  1360. /*##-3- Disable the DMA Streams ############################################*/
  1361. /* De-Initialize the DMA Stream associate to transmission process */
  1362. HAL_DMA_DeInit(&hdma_tx);
  1363. /* De-Initialize the DMA Stream associate to reception process */
  1364. HAL_DMA_DeInit(&hdma_rx);
  1365. /*##-4- Disable the NVIC for DMA ###########################################*/
  1366. HAL_NVIC_DisableIRQ(USART1_DMA_TX_IRQn);
  1367. HAL_NVIC_DisableIRQ(USART1_DMA_RX_IRQn);
  1368. }
  1369. else if(huart->Instance == MODULE_USART2)
  1370. {
  1371. /*##-1- Reset peripherals ##################################################*/
  1372. USART2_FORCE_RESET();
  1373. USART2_RELEASE_RESET();
  1374. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  1375. /* Configure UART Tx as alternate function */
  1376. HAL_GPIO_DeInit(USART2_TX_GPIO_PORT, USART2_TX_GPIO_PIN);
  1377. /* Configure UART Rx as alternate function */
  1378. HAL_GPIO_DeInit(USART2_RX_GPIO_PORT, USART2_RX_GPIO_PIN);
  1379. #if UART_DMA_ENABLE==1
  1380. /*##-3- Disable the DMA Streams ############################################*/
  1381. /* De-Initialize the DMA Stream associate to transmission process */
  1382. HAL_DMA_DeInit(&hdma_tx);
  1383. /* De-Initialize the DMA Stream associate to reception process */
  1384. HAL_DMA_DeInit(&hdma_rx);
  1385. /*##-4- Disable the NVIC for DMA ###########################################*/
  1386. HAL_NVIC_DisableIRQ(USART2_DMA_TX_IRQn);
  1387. HAL_NVIC_DisableIRQ(USART2_DMA_RX_IRQn);
  1388. #endif
  1389. }
  1390. /*else if(huart->Instance == MODULE_USART6)
  1391. {
  1392. USART6_FORCE_RESET();
  1393. USART6_RELEASE_RESET();
  1394. HAL_GPIO_DeInit(USART6_TX_GPIO_PORT, USART6_TX_GPIO_PIN);
  1395. HAL_GPIO_DeInit(USART6_RX_GPIO_PORT, USART6_RX_GPIO_PIN);
  1396. #if UART_DMA_ENABLE==1
  1397. HAL_DMA_DeInit(&hdma_tx);
  1398. HAL_DMA_DeInit(&hdma_rx);
  1399. HAL_NVIC_DisableIRQ(USART6_DMA_TX_IRQn);
  1400. HAL_NVIC_DisableIRQ(USART6_DMA_RX_IRQn);
  1401. #endif
  1402. }*/
  1403. }
  1404. /**
  1405. * @brief Tx Transfer completed callback
  1406. * @param UartHandle: UART handle.
  1407. * @note This example shows a simple way to report end of DMA Tx transfer, and
  1408. * you can add your own implementation.
  1409. * @retval None
  1410. */
  1411. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
  1412. {
  1413. /* Set transmission flag: transfer complete */
  1414. UartReady = SET;
  1415. }
  1416. /**
  1417. * @brief Rx Transfer completed callback
  1418. * @param UartHandle: UART handle
  1419. * @note This example shows a simple way to report end of DMA Rx transfer, and
  1420. * you can add your own implementation.
  1421. * @retval None
  1422. */
  1423. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
  1424. {
  1425. static int i=0;
  1426. /* Set transmission flag: transfer complete */
  1427. UartReady = SET;
  1428. if(UartHandle->Instance==MODULE_USART6)
  1429. {
  1430. i++;
  1431. }
  1432. }
  1433. /**
  1434. * @brief UART error callbacks
  1435. * @param UartHandle: UART handle
  1436. * @note This example shows a simple way to report transfer error, and you can
  1437. * add your own implementation.
  1438. * @retval None
  1439. */
  1440. void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
  1441. {
  1442. if(UartHandle->ErrorCode==HAL_UART_ERROR_PE)
  1443. {
  1444. }
  1445. else if(UartHandle->ErrorCode== HAL_UART_ERROR_NE)
  1446. {
  1447. }
  1448. else if(UartHandle->ErrorCode== HAL_UART_ERROR_FE)
  1449. {
  1450. }
  1451. else if(UartHandle->ErrorCode== HAL_UART_ERROR_ORE)
  1452. {
  1453. }
  1454. else if(UartHandle->ErrorCode== HAL_UART_ERROR_DMA)
  1455. {
  1456. ;
  1457. }
  1458. }
  1459. /*
  1460. *
  1461. *
  1462. */
  1463. void DebugUsart_Init(void)
  1464. {
  1465. /* Put the USART peripheral in the Asynchronous mode (UART Mode)
  1466. UARTx configured as follow:
  1467. - Word Length = 8 Bits
  1468. - Stop Bit = One Stop bit
  1469. - Parity = None
  1470. - BaudRate = 4000000 bps
  1471. - Hardware flow control disabled (RTS and CTS signals) */
  1472. debug_UartHandle.Instance = DEBUG_PORT;
  1473. debug_UartHandle.Init.BaudRate = DEBUG_BAUDRATE;
  1474. debug_UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  1475. debug_UartHandle.Init.StopBits = UART_STOPBITS_1;
  1476. debug_UartHandle.Init.Parity = UART_PARITY_NONE;
  1477. debug_UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  1478. debug_UartHandle.Init.Mode = UART_MODE_TX_RX;
  1479. debug_UartHandle.Init.OverSampling = UART_OVERSAMPLING_8;
  1480. if(HAL_UART_Init(&debug_UartHandle) != HAL_OK)
  1481. {
  1482. //Error_Handler();
  1483. }
  1484. }
  1485. #if 0
  1486. void DebugMessage(uint8_t* msg)
  1487. {
  1488. int len=0;
  1489. strcpy(aTxBuffer,msg);
  1490. /*##-2- Start the transmission process #####################################*/
  1491. /* While the UART in reception process, user can transmit data through
  1492. "aTxBuffer" buffer */
  1493. len = strlen(aTxBuffer);
  1494. #if UART_DMA_ENABLE==1
  1495. if(HAL_UART_Transmit_DMA(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK)
  1496. {
  1497. //Error_Handler();
  1498. }
  1499. #else
  1500. if(HAL_UART_Transmit_IT(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK)
  1501. {
  1502. //Error_Handler();
  1503. }
  1504. #endif
  1505. /*##-3- Wait for the end of the transfer ###################################*/
  1506. while (UartReady != SET)
  1507. {
  1508. }
  1509. /* Reset transmission flag */
  1510. UartReady = RESET;
  1511. }
  1512. void ReportMessage(uint8_t* msg, uint32_t len)
  1513. {
  1514. memcpy(aTxBuffer,msg,len);
  1515. aTxBuffer[len] = 0;
  1516. #if UART_DMA_ENABLE==1
  1517. if(HAL_UART_Transmit_DMA(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK)
  1518. {
  1519. //Error_Handler();
  1520. }
  1521. #else
  1522. if(HAL_UART_Transmit_IT(&debug_UartHandle, (uint8_t*)aTxBuffer, len)!= HAL_OK)
  1523. {
  1524. //Error_Handler();
  1525. }
  1526. #endif
  1527. /*##-3- Wait for the end of the transfer ###################################*/
  1528. while (UartReady != SET)
  1529. {
  1530. }
  1531. /* Reset transmission flag */
  1532. UartReady = RESET;
  1533. }
  1534. #endif
  1535. #endif
  1536. /******************************* ADC Routines **********************************/
  1537. #if defined(HAL_ADC_MODULE_ENABLED)
  1538. void BSP_ADC_Init(void)
  1539. {
  1540. /*##-1- Configure the ADC peripheral #######################################*/
  1541. BSP_AdcHandle.Instance = BSP_ADCx;
  1542. BSP_AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8;
  1543. //BSP_AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
  1544. BSP_AdcHandle.Init.Resolution = ADC_RESOLUTION_10B;
  1545. BSP_AdcHandle.Init.ScanConvMode = DISABLE;
  1546. BSP_AdcHandle.Init.ContinuousConvMode = DISABLE;
  1547. BSP_AdcHandle.Init.DiscontinuousConvMode = DISABLE;
  1548. BSP_AdcHandle.Init.NbrOfDiscConversion = 0;
  1549. BSP_AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  1550. BSP_AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  1551. BSP_AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  1552. BSP_AdcHandle.Init.NbrOfConversion = 1;
  1553. BSP_AdcHandle.Init.DMAContinuousRequests = DISABLE;
  1554. BSP_AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  1555. if(HAL_ADC_Init(&BSP_AdcHandle) != HAL_OK)
  1556. {
  1557. /* Initialization Error */
  1558. //Error_Handler();
  1559. }
  1560. }
  1561. void BSP_ADC_START_Convert(eAdcChannels channel)
  1562. {
  1563. /* ### - 3 - Channel configuration ######################################## */
  1564. /* ### - 4 - Start conversion in Interrupt mode ########################### */
  1565. BSP_AdcConfig.Channel = channel;
  1566. BSP_AdcConfig.Rank = 1;
  1567. BSP_AdcConfig.Offset = 0;
  1568. BSP_AdcConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;
  1569. if (HAL_ADC_ConfigChannel(&BSP_AdcHandle, &BSP_AdcConfig) == HAL_OK &&
  1570. HAL_ADC_Start_IT(&BSP_AdcHandle) == HAL_OK)
  1571. {
  1572. BSP_AdcStatus.lastChannel = BSP_AdcStatus.currentChannel;
  1573. BSP_AdcStatus.lastValue = BSP_AdcStatus.currentValue;
  1574. BSP_AdcStatus.currentChannel = channel;
  1575. BSP_AdcStatus.currentValue = 0xffff;
  1576. BSP_AdcStatus.status = ADC_CONVERT;
  1577. }
  1578. else
  1579. {
  1580. BSP_AdcStatus.status = ADC_ERROR;
  1581. }
  1582. }
  1583. /**
  1584. * @brief ADC MSP Initialization
  1585. * This function configures the hardware resources used in this example:
  1586. * - Peripheral's clock enable
  1587. * - Peripheral's GPIO Configuration
  1588. * @param huart: UART handle pointer
  1589. * @retval None
  1590. */
  1591. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  1592. {
  1593. GPIO_InitTypeDef GPIO_InitStruct;
  1594. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  1595. /* ADC3 Periph clock enable */
  1596. BSP_ADCx_CLK_ENABLE();
  1597. /* Enable GPIO clock ****************************************/
  1598. BSP_ADCx_CHAN_CLK_ENABLE();
  1599. /*##-2- Configure peripheral GPIO ##########################################*/
  1600. /* ADC3 Channel8 GPIO pin configuration */
  1601. GPIO_InitStruct.Pin = BSP_ADCx_VBAT_PIN | BSP_ADCx_V5IN_PIN;
  1602. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  1603. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1604. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1605. HAL_GPIO_Init(BSP_ADCx_CHAN_PORT, &GPIO_InitStruct);
  1606. /*##-3- Configure the NVIC #################################################*/
  1607. /* NVIC configuration for DMA transfer complete interrupt (USART1_TX) */
  1608. HAL_NVIC_SetPriority(BSP_ADCx_IRQn, BSP_ADCx_IRQ_PRIORITY, 0);
  1609. HAL_NVIC_EnableIRQ(BSP_ADCx_IRQn);
  1610. }
  1611. /**
  1612. * @brief ADC MSP De-Initialization
  1613. * This function frees the hardware resources used in this example:
  1614. * - Disable the Peripheral's clock
  1615. * - Revert GPIO to their default state
  1616. * @param hadc: ADC handle pointer
  1617. * @retval None
  1618. */
  1619. void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
  1620. {
  1621. /*##-1- Reset peripherals ##################################################*/
  1622. BSP_ADCx_FORCE_RESET();
  1623. BSP_ADCx_RELEASE_RESET();
  1624. /*##-2- Disable peripherals and GPIO Clocks ################################*/
  1625. HAL_GPIO_DeInit(BSP_ADCx_CHAN_PORT, BSP_ADCx_VBAT_PIN | BSP_ADCx_V5IN_PIN);
  1626. }
  1627. /**
  1628. * @brief Conversion complete callback in non blocking mode
  1629. * @param AdcHandle : AdcHandle handle
  1630. * @note This example shows a simple way to report end of conversion, and
  1631. * you can add your own implementation.
  1632. * @retval None
  1633. */
  1634. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
  1635. {
  1636. /* Get the converted value of regular channel */
  1637. BSP_AdcStatus.currentValue = HAL_ADC_GetValue(AdcHandle);
  1638. //BSP_AdcStatus.currentValue >>= 2;
  1639. BSP_AdcStatus.status = ADC_COMPLETE;
  1640. }
  1641. #endif
  1642. /******************************* I2C Routines **********************************/
  1643. #if defined(HAL_I2C_MODULE_ENABLED)
  1644. HAL_StatusTypeDef BSP_I2C_Init(void)
  1645. {
  1646. I2cHandle.Instance = BSP_I2Cx;
  1647. I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  1648. I2cHandle.Init.ClockSpeed = BSP_I2Cx_SPEED;
  1649. I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  1650. I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
  1651. I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  1652. I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  1653. I2cHandle.Init.OwnAddress1 = BSP_I2Cx_SLAVE_ADDRESS;
  1654. I2cHandle.Init.OwnAddress2 = 0xFE;
  1655. if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
  1656. {
  1657. /* Initialization Error */
  1658. //Error_Handler();
  1659. return HAL_ERROR;
  1660. }
  1661. return HAL_OK;
  1662. }
  1663. /**
  1664. * @brief I2C MSP Initialization
  1665. * This function configures the hardware resources used in this example:
  1666. * - Peripheral's clock enable
  1667. * - Peripheral's GPIO Configuration
  1668. * - DMA configuration for transmission request by peripheral
  1669. * - NVIC configuration for DMA interrupt request enable
  1670. * @param hi2c: I2C handle pointer
  1671. * @retval None
  1672. */
  1673. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  1674. {
  1675. GPIO_InitTypeDef GPIO_InitStruct;
  1676. /*##-1- Enable GPIO Clocks #################################################*/
  1677. /* Enable GPIO TX/RX clock */
  1678. BSP_I2Cx_SCL_GPIO_CLK_ENABLE();
  1679. BSP_I2Cx_SDA_GPIO_CLK_ENABLE();
  1680. /*##-2- Configure peripheral GPIO ##########################################*/
  1681. /* I2C TX GPIO pin configuration */
  1682. GPIO_InitStruct.Pin = BSP_I2Cx_SCL_PIN;
  1683. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  1684. GPIO_InitStruct.Pull = GPIO_PULLUP;
  1685. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1686. GPIO_InitStruct.Alternate = BSP_I2Cx_SCL_AF;
  1687. HAL_GPIO_Init(BSP_I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
  1688. /* I2C RX GPIO pin configuration */
  1689. GPIO_InitStruct.Pin = BSP_I2Cx_SDA_PIN;
  1690. GPIO_InitStruct.Alternate = BSP_I2Cx_SDA_AF;
  1691. HAL_GPIO_Init(BSP_I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
  1692. /*##-3- Enable I2C peripheral Clock ########################################*/
  1693. /* Enable I2C1 clock */
  1694. BSP_I2Cx_CLK_ENABLE();
  1695. }
  1696. /**
  1697. * @brief I2C MSP De-Initialization
  1698. * This function frees the hardware resources used in this example:
  1699. * - Disable the Peripheral's clock
  1700. * - Revert GPIO, DMA and NVIC configuration to their default state
  1701. * @param hi2c: I2C handle pointer
  1702. * @retval None
  1703. */
  1704. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  1705. {
  1706. /*##-1- Reset peripherals ##################################################*/
  1707. BSP_I2Cx_FORCE_RESET();
  1708. BSP_I2Cx_RELEASE_RESET();
  1709. /*##-2- Disable peripherals and GPIO Clocks ################################*/
  1710. /* Configure I2C Tx as alternate function */
  1711. HAL_GPIO_DeInit(BSP_I2Cx_SCL_GPIO_PORT, BSP_I2Cx_SCL_PIN);
  1712. /* Configure I2C Rx as alternate function */
  1713. HAL_GPIO_DeInit(BSP_I2Cx_SDA_GPIO_PORT, BSP_I2Cx_SDA_PIN);
  1714. }
  1715. void BSP_RTC_EXT_Init(void)
  1716. {
  1717. GPIO_InitTypeDef GPIO_InitStruct;
  1718. /*##-1- Enable GPIO Clocks #################################################*/
  1719. BSP_RTC_INTERRUPT_CLK_ENABLE();
  1720. /*##-2- Configure peripheral GPIO ##########################################*/
  1721. GPIO_InitStruct.Pin = BSP_RTC_INTERRUPT_PIN;
  1722. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  1723. GPIO_InitStruct.Pull = BSP_RTC_INTERRUPT_PULL;
  1724. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1725. HAL_GPIO_Init(BSP_RTC_INTERRUPT_PORT, &GPIO_InitStruct);
  1726. HAL_NVIC_SetPriority(BSP_RTC_INTERRUPT_IRQn, BSP_RTC_INTERRUPT_PRIORITY, 0);
  1727. HAL_NVIC_EnableIRQ(BSP_RTC_INTERRUPT_IRQn);
  1728. }
  1729. void BSP_RTC_Reset(void)
  1730. {
  1731. GPIO_InitTypeDef GPIO_InitStruct;
  1732. HAL_NVIC_DisableIRQ(BSP_RTC_INTERRUPT_IRQn);
  1733. /*##-2- Configure peripheral GPIO ##########################################*/
  1734. GPIO_InitStruct.Pin = BSP_RTC_INTERRUPT_PIN;
  1735. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  1736. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1737. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1738. HAL_GPIO_Init(BSP_RTC_INTERRUPT_PORT, &GPIO_InitStruct);
  1739. HAL_GPIO_WritePin(BSP_EEROM_WRITE_PROTECT_PORT, BSP_EEROM_WRITE_PROTECT_PIN, GPIO_PIN_SET);
  1740. }
  1741. void BSP_EEROM_EXT_Init(void)
  1742. {
  1743. GPIO_InitTypeDef GPIO_InitStruct;
  1744. /*##-1- Enable GPIO Clocks #################################################*/
  1745. BSP_EEROM_WRITE_PROTECT_CLK_ENABLE();
  1746. /*##-2- Configure peripheral GPIO ##########################################*/
  1747. GPIO_InitStruct.Pin = BSP_EEROM_WRITE_PROTECT_PIN;
  1748. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1749. GPIO_InitStruct.Pull = BSP_EEROM_WRITE_PROTECT_PULL;
  1750. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  1751. HAL_GPIO_Init(BSP_EEROM_WRITE_PROTECT_PORT, &GPIO_InitStruct);
  1752. BSP_EEROM_WRITE_FORBIDEN();
  1753. }
  1754. int BSP_EEROM_Read(uint16_t MemAddress, uint16_t Size, uint8_t *pData)
  1755. {
  1756. HAL_StatusTypeDef status = HAL_ERROR;
  1757. status = HAL_I2C_Mem_Read(&I2cHandle, BSP_EEROM_RD_ADDRESS, MemAddress, I2C_MEMADD_SIZE_16BIT, pData, Size, 1000);
  1758. return status;
  1759. }
  1760. int BSP_EEROM_write(uint16_t MemAddress, uint16_t Size, uint8_t *pData)
  1761. {
  1762. HAL_StatusTypeDef status = HAL_ERROR;
  1763. BSP_EEROM_WRITE_ENABLE();
  1764. status = HAL_I2C_Mem_Write(&I2cHandle, BSP_EEROM_WR_ADDRESS, MemAddress, I2C_MEMADD_SIZE_16BIT, pData, Size, 1000);
  1765. BSP_EEROM_WRITE_FORBIDEN();
  1766. return status;
  1767. }
  1768. #endif
  1769. /*******************************END OF FILE*************************************/