n32_msp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-20 breo.com first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "n32g45x.h"
  13. #include "n32_msp.h"
  14. #ifdef BSP_USING_UART
  15. void n32_msp_usart_init(void *Instance)
  16. {
  17. GPIO_InitType GPIO_InitCtlStruct;
  18. USART_Module *USARTx = (USART_Module *)Instance;
  19. GPIO_InitStruct(&GPIO_InitCtlStruct);
  20. GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_50MHz;
  21. #ifdef BSP_USING_UART1
  22. if (USART1 == USARTx)
  23. {
  24. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);
  25. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  26. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  27. GPIO_InitCtlStruct.Pin = GPIO_PIN_9;
  28. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  29. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  30. GPIO_InitCtlStruct.Pin = GPIO_PIN_10;
  31. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  32. }
  33. #endif
  34. #ifdef BSP_USING_UART2
  35. if (USART2 == USARTx)
  36. {
  37. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
  38. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  39. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  40. GPIO_InitCtlStruct.Pin = GPIO_PIN_2;
  41. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  42. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  43. GPIO_InitCtlStruct.Pin = GPIO_PIN_3;
  44. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  45. }
  46. #endif
  47. #ifdef BSP_USING_UART3
  48. if (USART3 == USARTx)
  49. {
  50. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART3, ENABLE);
  51. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  52. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  53. GPIO_InitCtlStruct.Pin = GPIO_PIN_10;
  54. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  55. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  56. GPIO_InitCtlStruct.Pin = GPIO_PIN_11;
  57. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  58. }
  59. #endif
  60. #ifdef BSP_USING_UART4
  61. if (UART4 == USARTx)
  62. {
  63. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_UART4, ENABLE);
  64. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  65. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  66. GPIO_InitCtlStruct.Pin = GPIO_PIN_10;
  67. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  68. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  69. GPIO_InitCtlStruct.Pin = GPIO_PIN_11;
  70. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  71. }
  72. #endif
  73. /* Add others */
  74. }
  75. #endif /* BSP_USING_SERIAL */
  76. #ifdef BSP_USING_SPI
  77. void n32_msp_spi_init(void *Instance)
  78. {
  79. GPIO_InitType GPIO_InitCtlStruct;
  80. SPI_Module *SPIx = (SPI_Module *)Instance;
  81. GPIO_InitStruct(&GPIO_InitCtlStruct);
  82. GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_50MHz;
  83. #ifdef BSP_USING_SPI1
  84. if (SPI1 == SPIx)
  85. {
  86. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_SPI1, ENABLE);
  87. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  88. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  89. GPIO_InitCtlStruct.Pin = GPIO_PIN_4;
  90. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  91. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  92. GPIO_InitCtlStruct.Pin = GPIO_PIN_5 | GPIO_PIN_7;
  93. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  94. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  95. GPIO_InitCtlStruct.Pin = GPIO_PIN_6;
  96. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  97. }
  98. #endif
  99. #ifdef BSP_USING_SPI2
  100. if (SPI2 == SPIx)
  101. {
  102. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_SPI2, ENABLE);
  103. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  104. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  105. GPIO_InitCtlStruct.Pin = GPIO_PIN_12;
  106. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  107. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  108. GPIO_InitCtlStruct.Pin = GPIO_PIN_13 | GPIO_PIN_15;
  109. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  110. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  111. GPIO_InitCtlStruct.Pin = GPIO_PIN_14;
  112. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  113. }
  114. #endif
  115. /* Add others */
  116. }
  117. #endif /* BSP_USING_SPI */
  118. #ifdef BSP_USING_SDIO
  119. void n32_msp_sdio_init(void *Instance)
  120. {
  121. GPIO_InitType GPIO_InitCtlStructure;
  122. SDIO_Module *SDIOx = (SDIO_Module *)Instance;
  123. GPIO_InitStruct(&GPIO_InitCtlStructure);
  124. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  125. if (SDIO == SDIOx)
  126. {
  127. /* if used dma ... */
  128. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA2, ENABLE);
  129. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_SDIO, ENABLE);
  130. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_GPIOD, ENABLE);
  131. GPIO_InitCtlStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
  132. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  133. GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStructure);
  134. GPIO_InitCtlStructure.Pin = GPIO_PIN_2;
  135. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  136. GPIO_InitPeripheral(GPIOD, &GPIO_InitCtlStructure);
  137. }
  138. }
  139. #endif /* BSP_USING_SDIO */
  140. #ifdef BSP_USING_PWM
  141. void n32_msp_tim_init(void *Instance)
  142. {
  143. GPIO_InitType GPIO_InitCtlStructure;
  144. GPIO_InitStruct(&GPIO_InitCtlStructure);
  145. TIM_Module *TIMx = (TIM_Module *)Instance;
  146. if (TIMx == TIM1)
  147. {
  148. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
  149. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  150. GPIO_InitCtlStructure.Pin = GPIO_PIN_8 | GPIO_PIN_11;
  151. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  152. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  153. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
  154. }
  155. if (TIMx == TIM2)
  156. {
  157. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
  158. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  159. GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
  160. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  161. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  162. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
  163. }
  164. if (TIMx == TIM3)
  165. {
  166. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
  167. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB, ENABLE);
  168. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  169. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  170. GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  171. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
  172. GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  173. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStructure);
  174. }
  175. if (TIMx == TIM4)
  176. {
  177. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4, ENABLE);
  178. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  179. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  180. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  181. GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  182. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStructure);
  183. }
  184. if (TIMx == TIM5)
  185. {
  186. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM5, ENABLE);
  187. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  188. GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
  189. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  190. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  191. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
  192. }
  193. if (TIMx == TIM8)
  194. {
  195. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);
  196. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  197. GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  198. GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  199. GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
  200. GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStructure);
  201. }
  202. }
  203. #endif /* BSP_USING_PWM */
  204. #ifdef BSP_USING_ADC
  205. void n32_msp_adc_init(void *Instance)
  206. {
  207. GPIO_InitType GPIO_InitCtlStruct;
  208. GPIO_InitStruct(&GPIO_InitCtlStruct);
  209. ADC_Module *ADCx = (ADC_Module *)Instance;
  210. #ifdef BSP_USING_ADC1
  211. if (ADCx == ADC1)
  212. {
  213. /* ADC1 & GPIO clock enable */
  214. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC1, ENABLE);
  215. ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV8);
  216. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  217. /* Configure ADC Channel as analog input */
  218. GPIO_InitCtlStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
  219. GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_2MHz;
  220. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AIN;
  221. GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStruct);
  222. }
  223. #endif
  224. #ifdef BSP_USING_ADC2
  225. if (ADCx == ADC2)
  226. {
  227. /* ADC2 & GPIO clock enable */
  228. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC2, ENABLE);
  229. ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV8);
  230. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  231. /* Configure ADC Channel as analog input */
  232. GPIO_InitCtlStruct.Pin = GPIO_PIN_1;
  233. GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_2MHz;
  234. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AIN;
  235. GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStruct);
  236. }
  237. #endif
  238. }
  239. #endif /* BSP_USING_ADC */
  240. #ifdef BSP_USING_HWTIMER
  241. void n32_msp_hwtim_init(void *Instance)
  242. {
  243. TIM_Module *TIMx = (TIM_Module *)Instance;
  244. #ifdef BSP_USING_HWTIM3
  245. if (TIMx == TIM3)
  246. {
  247. /* TIM3 clock enable */
  248. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
  249. }
  250. #endif
  251. #ifdef BSP_USING_HWTIM4
  252. if (TIMx == TIM4)
  253. {
  254. /* TIM4 clock enable */
  255. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4, ENABLE);
  256. }
  257. #endif
  258. #ifdef BSP_USING_HWTIM5
  259. if (TIMx == TIM5)
  260. {
  261. /* TIM5 clock enable */
  262. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM5, ENABLE);
  263. }
  264. #endif
  265. #ifdef BSP_USING_HWTIM6
  266. if (TIMx == TIM6)
  267. {
  268. /* TIM6 clock enable */
  269. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM6, ENABLE);
  270. }
  271. #endif
  272. #ifdef BSP_USING_HWTIM7
  273. if (TIMx == TIM7)
  274. {
  275. /* TIM7 clock enable */
  276. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM7, ENABLE);
  277. }
  278. #endif
  279. }
  280. #endif
  281. #ifdef BSP_USING_CAN
  282. void n32_msp_can_init(void *Instance)
  283. {
  284. GPIO_InitType GPIO_InitCtlStruct;
  285. CAN_Module *CANx = (CAN_Module *)Instance;
  286. GPIO_InitStruct(&GPIO_InitCtlStruct);
  287. GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_50MHz;
  288. #ifdef BSP_USING_CAN1
  289. if (CAN1 == CANx)
  290. {
  291. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_CAN1, ENABLE);
  292. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  293. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  294. GPIO_InitCtlStruct.Pin = GPIO_PIN_12;
  295. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  296. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  297. GPIO_InitCtlStruct.Pin = GPIO_PIN_11;
  298. GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
  299. }
  300. #endif
  301. #ifdef BSP_USING_CAN2
  302. if (CAN2 == CANx)
  303. {
  304. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_CAN2, ENABLE);
  305. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
  306. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  307. // GPIO_PinsRemapConfig(AFIO_MAP6_CAN2_0001, ENABLE);
  308. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  309. GPIO_InitCtlStruct.Pin = GPIO_PIN_6;
  310. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  311. GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  312. GPIO_InitCtlStruct.Pin = GPIO_PIN_5;
  313. GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
  314. }
  315. #endif
  316. }
  317. #endif /* BSP_USING_CAN */
  318. #ifdef RT_USING_FINSH
  319. #include <finsh.h>
  320. #if defined(BSP_USING_UART2) || defined(BSP_USING_UART3)
  321. static void uart_test_rw(rt_device_t uartx, const char *name)
  322. {
  323. if (uartx == NULL)
  324. {
  325. uartx = rt_device_find(name);
  326. rt_err_t err = rt_device_open(uartx, RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX);
  327. RT_ASSERT(err == RT_EOK);
  328. }
  329. rt_device_write(uartx, 0, name, strlen(name));
  330. rt_device_write(uartx, 0, "\r\n", 2);
  331. uint8_t recv_buf[64] = {0x0};
  332. int ret = rt_device_read(uartx, 0, recv_buf, sizeof(recv_buf));
  333. if (ret != 0)
  334. {
  335. for (int i = 0; i < ret; ++i)
  336. rt_kprintf("[%02x]", recv_buf[i]);
  337. }
  338. rt_device_write(uartx, 0, "\r\n", 2);
  339. }
  340. static void uart_test(void)
  341. {
  342. #ifdef BSP_USING_UART2
  343. static rt_device_t u2 = NULL;
  344. uart_test_rw(u2, "uart2");
  345. #endif
  346. #ifdef BSP_USING_UART2
  347. static rt_device_t u3 = NULL;
  348. uart_test_rw(u3, "uart3");
  349. #endif
  350. }
  351. MSH_CMD_EXPORT(uart_test, uart_test)
  352. #endif
  353. #ifdef BSP_USING_ADC
  354. #ifdef BSP_USING_ADC1
  355. #define ADC_DEV_NAME "adc1"
  356. #else
  357. #define ADC_DEV_NAME "adc2"
  358. #endif
  359. #define REFER_VOLTAGE 3300
  360. #define CONVERT_BITS (1 << 12)
  361. static int adc_vol_sample(int argc, char *argv[])
  362. {
  363. rt_adc_device_t adc_dev;
  364. rt_uint32_t value, vol;
  365. rt_err_t ret = RT_EOK;
  366. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  367. if (adc_dev == RT_NULL)
  368. {
  369. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  370. return RT_ERROR;
  371. }
  372. for (int i = 6; i <= 9; ++i)
  373. {
  374. ret = rt_adc_enable(adc_dev, i);
  375. value = rt_adc_read(adc_dev, i);
  376. rt_kprintf("ch=[%d] the value is :[%d] \n", i, value);
  377. vol = value * REFER_VOLTAGE / CONVERT_BITS;
  378. rt_kprintf("ch=[%d] the voltage is :[%d] \n", i, vol);
  379. }
  380. return ret;
  381. }
  382. MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);
  383. #endif
  384. #ifdef BSP_USING_HWTIMER
  385. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  386. {
  387. rt_kprintf("this is hwtimer timeout callback fucntion!\n");
  388. rt_kprintf("timer name is :%s.\n", dev->parent.name);
  389. rt_kprintf("tick is :%d !\n", rt_tick_get());
  390. return 0;
  391. }
  392. static int hwtimer_init(const char *name)
  393. {
  394. rt_err_t ret = RT_EOK;
  395. rt_hwtimerval_t timeout_s;
  396. rt_device_t hw_dev = RT_NULL;
  397. rt_hwtimer_mode_t mode;
  398. hw_dev = rt_device_find(name);
  399. if (hw_dev == RT_NULL)
  400. {
  401. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", name);
  402. return RT_ERROR;
  403. }
  404. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  405. if (ret != RT_EOK)
  406. {
  407. rt_kprintf("open %s device failed!\n", name);
  408. return ret;
  409. }
  410. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  411. mode = HWTIMER_MODE_PERIOD;
  412. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  413. if (ret != RT_EOK)
  414. {
  415. rt_kprintf("set mode failed! ret is :%d\n", ret);
  416. return ret;
  417. }
  418. timeout_s.sec = 5;
  419. timeout_s.usec = 0;
  420. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  421. {
  422. rt_kprintf("set timeout value failed\n");
  423. return RT_ERROR;
  424. }
  425. rt_thread_mdelay(3500);
  426. rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  427. rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  428. return ret;
  429. }
  430. static int hwtimer_sample(int argc, char *argv[])
  431. {
  432. #ifdef BSP_USING_HWTIM6
  433. hwtimer_init("timer6");
  434. #endif
  435. #ifdef BSP_USING_HWTIM7
  436. hwtimer_init("timer7");
  437. #endif
  438. return RT_EOK;
  439. }
  440. MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
  441. #endif
  442. #endif