n32_msp.c 15 KB

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