MCU.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : MCU.c
  3. * Author : WCH
  4. * Version : V1.2
  5. * Date : 2022/01/18
  6. * Description : HAL task processing function and BLE and hardware initialization
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. /******************************************************************************/
  13. /* Header file contains */
  14. #include "HAL.h"
  15. #include "string.h"
  16. tmosTaskID halTaskID;
  17. uint32_t g_LLE_IRQLibHandlerLocation;
  18. /*******************************************************************************
  19. * @fn Lib_Calibration_LSI
  20. *
  21. * @brief Internal 32K calibration
  22. *
  23. * @param None.
  24. *
  25. * @return None.
  26. */
  27. void Lib_Calibration_LSI(void)
  28. {
  29. Calibration_LSI(Level_64);
  30. }
  31. #if(defined(BLE_SNV)) && (BLE_SNV == TRUE)
  32. /*******************************************************************************
  33. * @fn Lib_Read_Flash
  34. *
  35. * @brief Callback function used for BLE lib.
  36. *
  37. * @param addr.
  38. * @param num.
  39. * @param pBuf.
  40. *
  41. * @return None.
  42. */
  43. uint32_t Lib_Read_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
  44. {
  45. tmos_memcpy(pBuf, (uint32_t*)addr, num*4);
  46. return 0;
  47. }
  48. /*******************************************************************************
  49. * @fn Lib_Write_Flash
  50. *
  51. * @brief Callback function used for BLE lib.
  52. *
  53. * @param addr.
  54. * @param num.
  55. * @param pBuf.
  56. *
  57. * @return None.
  58. */
  59. uint32_t Lib_Write_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
  60. {
  61. FLASH_Unlock_Fast();
  62. FLASH_ErasePage_Fast( addr );
  63. FLASH_ProgramPage_Fast( addr, pBuf);
  64. FLASH_Lock_Fast();
  65. Delay_Us(1);
  66. return 0;
  67. }
  68. #endif
  69. /*******************************************************************************
  70. * @fn WCHBLE_Init
  71. *
  72. * @brief BLE library initialization
  73. *
  74. * @param None.
  75. *
  76. * @return None.
  77. */
  78. void WCHBLE_Init(void)
  79. {
  80. uint8_t i;
  81. bleConfig_t cfg;
  82. g_LLE_IRQLibHandlerLocation = (uint32_t)LLE_IRQLibHandler;
  83. if(!tmos_memcmp(VER_LIB, VER_FILE, strlen(VER_FILE)))
  84. {
  85. PRINT("head file error...\n");
  86. while(1);
  87. }
  88. // 32M crystal capacitance and current
  89. OSC->HSE_CAL_CTRL &= ~(0x07<<28);
  90. OSC->HSE_CAL_CTRL |= 0x03<<28;
  91. OSC->HSE_CAL_CTRL |= 3<<24;
  92. tmos_memset(&cfg, 0, sizeof(bleConfig_t));
  93. cfg.MEMAddr = (uint32_t)MEM_BUF;
  94. cfg.MEMLen = (uint32_t)BLE_MEMHEAP_SIZE;
  95. cfg.BufMaxLen = (uint32_t)BLE_BUFF_MAX_LEN;
  96. cfg.BufNumber = (uint32_t)BLE_BUFF_NUM;
  97. cfg.TxNumEvent = (uint32_t)BLE_TX_NUM_EVENT;
  98. cfg.TxPower = (uint32_t)BLE_TX_POWER;
  99. #if(defined(BLE_SNV)) && (BLE_SNV == TRUE)
  100. cfg.SNVAddr = (uint32_t)BLE_SNV_ADDR;
  101. cfg.SNVNum = (uint32_t)BLE_SNV_NUM;
  102. cfg.readFlashCB = Lib_Read_Flash;
  103. cfg.writeFlashCB = Lib_Write_Flash;
  104. #endif
  105. cfg.ClockFrequency = CAB_LSIFQ/2;
  106. #if(CLK_OSC32K==0)
  107. cfg.ClockAccuracy = 50;
  108. #else
  109. cfg.ClockAccuracy = 1000;
  110. #endif
  111. cfg.ConnectNumber = (PERIPHERAL_MAX_CONNECTION & 3) | (CENTRAL_MAX_CONNECTION << 2);
  112. #if(defined TEM_SAMPLE) && (TEM_SAMPLE == TRUE)
  113. // Calibrate RF and internal RC according to temperature changes (greater than 7 degrees Celsius)
  114. cfg.tsCB = HAL_GetInterTempValue;
  115. #if(CLK_OSC32K)
  116. cfg.rcCB = Lib_Calibration_LSI; // Internal 32K clock calibration
  117. #endif
  118. #endif
  119. #if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
  120. cfg.idleCB = BLE_LowPower; // Enable sleep
  121. #endif
  122. #if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
  123. for(i = 0; i < 6; i++)
  124. {
  125. cfg.MacAddr[i] = MacAddr[5 - i];
  126. }
  127. #else
  128. {
  129. uint8_t MacAddr[6];
  130. FLASH_GetMACAddress(MacAddr);
  131. for(i = 0; i < 6; i++)
  132. {
  133. cfg.MacAddr[i] = MacAddr[i]; // Use chip mac address
  134. }
  135. }
  136. #endif
  137. if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024)
  138. {
  139. while(1);
  140. }
  141. i = BLE_LibInit(&cfg);
  142. if(i)
  143. {
  144. PRINT("LIB init error code: %x ...\n", i);
  145. while(1);
  146. }
  147. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE );
  148. NVIC_EnableIRQ( BB_IRQn );
  149. NVIC_EnableIRQ( LLE_IRQn );
  150. }
  151. /*******************************************************************************
  152. * @fn HAL_ProcessEvent
  153. *
  154. * @brief HAL processing
  155. *
  156. * @param task_id - The TMOS assigned task ID.
  157. * @param events - events to process. This is a bit map and can
  158. * contain more than one event.
  159. *
  160. * @return events.
  161. */
  162. tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events)
  163. {
  164. uint8_t *msgPtr;
  165. if(events & SYS_EVENT_MSG)
  166. {
  167. /**
  168. * Process the HAL layer message, call tmos_msg_receive to read the message,
  169. * and delete the message after processing.
  170. */
  171. msgPtr = tmos_msg_receive(task_id);
  172. if(msgPtr)
  173. {
  174. /* De-allocate */
  175. tmos_msg_deallocate(msgPtr);
  176. }
  177. return events ^ SYS_EVENT_MSG;
  178. }
  179. if(events & LED_BLINK_EVENT)
  180. {
  181. #if(defined HAL_LED) && (HAL_LED == TRUE)
  182. HalLedUpdate();
  183. #endif // HAL_LED
  184. return events ^ LED_BLINK_EVENT;
  185. }
  186. if(events & HAL_KEY_EVENT)
  187. {
  188. #if(defined HAL_KEY) && (HAL_KEY == TRUE)
  189. HAL_KeyPoll(); /* Check for keys */
  190. tmos_start_task(halTaskID, HAL_KEY_EVENT, MS1_TO_SYSTEM_TIME(100));
  191. return events ^ HAL_KEY_EVENT;
  192. #endif
  193. }
  194. if(events & HAL_REG_INIT_EVENT)
  195. {
  196. #if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE) // Calibration tasks, a single time is less than 10ms
  197. BLE_RegInit(); // Calibrate RF
  198. #if(CLK_OSC32K)
  199. Lib_Calibration_LSI(); // Calibrate internal RC
  200. #endif
  201. tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD));
  202. return events ^ HAL_REG_INIT_EVENT;
  203. #endif
  204. }
  205. if(events & HAL_TEST_EVENT)
  206. {
  207. PRINT("* \n");
  208. tmos_start_task(halTaskID, HAL_TEST_EVENT, MS1_TO_SYSTEM_TIME(1000));
  209. return events ^ HAL_TEST_EVENT;
  210. }
  211. return 0;
  212. }
  213. /*******************************************************************************
  214. * @fn HAL_Init
  215. *
  216. * @brief Ó²¼þ³õʼ»¯
  217. *
  218. * @param None.
  219. *
  220. * @return None.
  221. */
  222. void HAL_Init()
  223. {
  224. halTaskID = TMOS_ProcessEventRegister(HAL_ProcessEvent);
  225. HAL_TimeInit();
  226. #if(defined HAL_SLEEP) && (HAL_SLEEP == TRUE)
  227. HAL_SleepInit();
  228. #endif
  229. #if(defined HAL_LED) && (HAL_LED == TRUE)
  230. HAL_LedInit();
  231. #endif
  232. #if(defined HAL_KEY) && (HAL_KEY == TRUE)
  233. HAL_KeyInit();
  234. #endif
  235. #if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE)
  236. // Add a calibration task, and a single calibration takes less than 10ms
  237. tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD));
  238. #endif
  239. // tmos_start_task(halTaskID, HAL_TEST_EVENT, MS1_TO_SYSTEM_TIME(1000)); // Add a test task
  240. }
  241. /*******************************************************************************
  242. * @fn HAL_GetInterTempValue
  243. *
  244. * @brief Get the internal temperature sampling value, if the ADC interrupt sampling is used,
  245. * it is necessary to temporarily shield the interrupt in this function.
  246. *
  247. * @return Internal temperature sampling value.
  248. */
  249. uint16_t HAL_GetInterTempValue(void)
  250. {
  251. uint32_t rcc_apb2pcenr, rcc_cfgr0, adc1_ctrl1, adc1_ctrl2, adc1_rsqr1, adc1_rsqr2, adc1_rsqr3, adc1_samptr1, adc1_samptr2;
  252. uint32_t adc1_iofr1, adc1_iofr2, adc1_iofr3, adc1_iofr4, adc1_wdhtr, adc1_wdltr, adc1_isqr;
  253. ADC_InitTypeDef ADC_InitStructure = {0};
  254. uint16_t adc_data;
  255. rcc_apb2pcenr = RCC->APB2PCENR;
  256. rcc_cfgr0 = RCC->CFGR0;
  257. adc1_ctrl1 = ADC1->CTLR1;
  258. adc1_ctrl2 = ADC1->CTLR2;
  259. adc1_rsqr1 = ADC1->RSQR1;
  260. adc1_rsqr2 = ADC1->RSQR2;
  261. adc1_rsqr3 = ADC1->RSQR3;
  262. adc1_samptr1 = ADC1->SAMPTR1;
  263. adc1_samptr2 = ADC1->SAMPTR2;
  264. adc1_iofr1 = ADC1->IOFR1;
  265. adc1_iofr2 = ADC1->IOFR2;
  266. adc1_iofr3 = ADC1->IOFR3;
  267. adc1_iofr4 = ADC1->IOFR4;
  268. adc1_wdhtr = ADC1->WDHTR;
  269. adc1_wdltr = ADC1->WDLTR;
  270. adc1_isqr = ADC1->ISQR;
  271. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  272. RCC_ADCCLKConfig(RCC_PCLK2_Div8);
  273. ADC_DeInit(ADC1);
  274. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  275. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  276. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  277. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  278. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  279. ADC_InitStructure.ADC_NbrOfChannel = 1;
  280. ADC_Init(ADC1, &ADC_InitStructure);
  281. ADC_Cmd(ADC1, ENABLE);
  282. ADC_BufferCmd(ADC1, ENABLE); //enable buffer
  283. ADC_TempSensorVrefintCmd(ENABLE);
  284. ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_239Cycles5);
  285. ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  286. while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
  287. adc_data = ADC_GetConversionValue(ADC1);
  288. ADC_DeInit(ADC1);
  289. RCC->APB2PCENR = rcc_apb2pcenr;
  290. RCC->CFGR0 = rcc_cfgr0;
  291. ADC1->CTLR1 = adc1_ctrl1;
  292. ADC1->CTLR2 = adc1_ctrl2;
  293. ADC1->RSQR1 = adc1_rsqr1;
  294. ADC1->RSQR2 = adc1_rsqr2;
  295. ADC1->RSQR3 = adc1_rsqr3;
  296. ADC1->SAMPTR1 = adc1_samptr1;
  297. ADC1->SAMPTR2 = adc1_samptr2;
  298. ADC1->IOFR1 = adc1_iofr1;
  299. ADC1->IOFR2 = adc1_iofr2;
  300. ADC1->IOFR3 = adc1_iofr3;
  301. ADC1->IOFR4 = adc1_iofr4;
  302. ADC1->WDHTR = adc1_wdhtr;
  303. ADC1->WDLTR = adc1_wdltr;
  304. ADC1->ISQR = adc1_isqr;
  305. return (adc_data);
  306. }
  307. /******************************** endfile @ mcu ******************************/