test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-06-17 QT-one first version
  9. */
  10. #include "board.h"
  11. #ifdef BSP_USING_TEST
  12. /* Task stack */
  13. #define THREAD_PRIORITY 25
  14. #define THREAD_STACK_SIZE 512
  15. #define THREAD_TIMESLICE 5
  16. /* Test pins */
  17. #define TEST_LED0_PIN GET_PIN(C, 14)
  18. #define TEST_LED1_PIN GET_PIN(C, 15)
  19. #define TEST_LED2_PIN GET_PIN(C, 1)
  20. #define TEST_WAKEUP_PIN GET_PIN(B, 12)
  21. #define TEST_KEY1_PIN GET_PIN(D, 1)
  22. #define TEST_KEY2_PIN GET_PIN(D, 2)
  23. #define TEST_OTHER_PIN GET_PIN(B, 12)
  24. #define TEST_OUTPUT_PIN GET_PIN(C, 1)
  25. #define TEST_INPUT_PIN GET_PIN(D, 1)
  26. #define TEST_INT_PIN GET_PIN(D, 2)
  27. #define TEST_RES_PIN GET_PIN(C, 1)
  28. /* Event flags */
  29. #define TEST_GPIO_INT_ENV (1 << 10)
  30. #define TEST_GPIO_KEY_ENV (1 << 15)
  31. static struct rt_event led_event; /* LED event */
  32. #define TASK_KILL_FLAG (1 << 10)
  33. static struct rt_event task_event; /* Task event */
  34. /* EEPROM Read/Write Data Structure */
  35. typedef union
  36. {
  37. rt_uint8_t data[30];
  38. struct
  39. {
  40. rt_uint8_t write_addr;
  41. char write_date[29];
  42. }in_data;
  43. }eeprom_write_type;
  44. /* Semaphore variables */
  45. static struct rt_semaphore rx_sem;
  46. /* Mutually exclusive variables */
  47. static rt_mutex_t task_mutex = RT_NULL; /* task mutex */
  48. /* device handle */
  49. static rt_device_t serial;
  50. static rt_device_t wdt_dev;
  51. struct rt_i2c_bus_device *i2c_dev;
  52. static struct rt_spi_device *spi_dev;
  53. /* In-file function declarations */
  54. static void sys_run_dir(void *parameter);
  55. static void gpio_output_test(void *parameter);
  56. static void gpio_input_test(void *parameter);
  57. static void key_iqr_handle(void *args);
  58. /* Task registration */
  59. int task_registration(void)
  60. {
  61. /* Create a dynamic mutex */
  62. task_mutex = rt_mutex_create("task_mutex", RT_IPC_FLAG_FIFO);
  63. if (task_mutex == RT_NULL)
  64. {
  65. rt_kprintf("rt_mutex_create error.\n");
  66. return -1;
  67. }
  68. /* Create a task event */
  69. if(rt_event_init(&task_event,"task_event",RT_IPC_FLAG_FIFO) != RT_EOK)
  70. {
  71. rt_kprintf("rt_mutex_create error.\n");
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. INIT_BOARD_EXPORT(task_registration);
  77. /* System operation indicator */
  78. static void sys_run_dir(void *parameter)
  79. {
  80. rt_uint32_t e;
  81. rt_pin_mode(TEST_LED2_PIN, PIN_MODE_OUTPUT);
  82. while(1)
  83. {
  84. if(rt_event_recv(&task_event,TASK_KILL_FLAG,
  85. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  86. RT_WAITING_NO, &e) == RT_EOK)
  87. {
  88. rt_thread_t tid = rt_thread_self();
  89. rt_thread_delete(tid);
  90. }
  91. rt_pin_write(TEST_LED2_PIN, PIN_LOW);
  92. rt_thread_mdelay(500);
  93. rt_pin_write(TEST_LED2_PIN, PIN_HIGH);
  94. rt_thread_mdelay(500);
  95. }
  96. }
  97. static int sys_run_task(int argc, char *argv[])
  98. {
  99. if(argc == 2)
  100. {
  101. if(rt_strcmp(argv[1],"start") == 0)
  102. {
  103. if(rt_mutex_take(task_mutex, RT_WAITING_NO) != RT_EOK)
  104. {
  105. rt_kprintf("The test thread is occupied.\n");
  106. return -RT_ERROR;
  107. }
  108. else
  109. {
  110. /* Register the system indicator task */
  111. rt_thread_t sys_led_task = rt_thread_create("sys_led_task",
  112. sys_run_dir, RT_NULL,
  113. THREAD_STACK_SIZE,
  114. THREAD_PRIORITY, THREAD_TIMESLICE);
  115. if (sys_led_task != RT_NULL)
  116. rt_thread_startup(sys_led_task);
  117. rt_kprintf("The sys run task is registered.\n");
  118. }
  119. }
  120. else if(rt_strcmp(argv[1],"end") == 0)
  121. {
  122. rt_event_send(&task_event,TASK_KILL_FLAG);
  123. rt_mutex_release(task_mutex);
  124. rt_kprintf("The sys run task has been deleted.\n");
  125. }
  126. }
  127. else
  128. {
  129. rt_kprintf("Necessary parameters are missing.\n");
  130. rt_kprintf("You can use the following commands.\n");
  131. rt_kprintf("%s start\n",__func__);
  132. rt_kprintf("%s end\n",__func__);
  133. return -1;
  134. }
  135. return -1;
  136. }
  137. MSH_CMD_EXPORT(sys_run_task, sys run task operation);
  138. /* Gpio output test */
  139. static void gpio_output_test(void *parameter)
  140. {
  141. rt_uint32_t e;
  142. rt_pin_mode(TEST_OUTPUT_PIN, PIN_MODE_OUTPUT);
  143. while(1)
  144. {
  145. if(rt_event_recv(&task_event,TASK_KILL_FLAG,
  146. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  147. RT_WAITING_NO, &e) == RT_EOK)
  148. {
  149. rt_thread_t tid = rt_thread_self();
  150. rt_thread_delete(tid);
  151. }
  152. rt_pin_write(TEST_OUTPUT_PIN, PIN_LOW);
  153. rt_thread_mdelay(500);
  154. rt_pin_write(TEST_OUTPUT_PIN, PIN_HIGH);
  155. rt_thread_mdelay(500);
  156. }
  157. }
  158. static int gpio_output_task(int argc, char *argv[])
  159. {
  160. if(argc == 2)
  161. {
  162. if(rt_strcmp(argv[1],"start") == 0)
  163. {
  164. if(rt_mutex_take(task_mutex, RT_WAITING_NO) != RT_EOK)
  165. {
  166. rt_kprintf("The test thread is occupied.\n");
  167. return -RT_ERROR;
  168. }
  169. else
  170. {
  171. /* Gpio output test tasks */
  172. rt_thread_t gpio_output_task = rt_thread_create("gpio_output_task",
  173. gpio_output_test, RT_NULL,
  174. THREAD_STACK_SIZE,
  175. THREAD_PRIORITY, THREAD_TIMESLICE);
  176. if (gpio_output_task != RT_NULL)
  177. rt_thread_startup(gpio_output_task);
  178. rt_kprintf("The gpio output task is registered.\n");
  179. }
  180. }
  181. else if(rt_strcmp(argv[1],"end") == 0)
  182. {
  183. rt_event_send(&task_event,TASK_KILL_FLAG);
  184. rt_mutex_release(task_mutex);
  185. rt_kprintf("The gpio output task has been deleted.\n");
  186. }
  187. }
  188. else
  189. {
  190. rt_kprintf("Necessary parameters are missing.\n");
  191. rt_kprintf("You can use the following commands.\n");
  192. rt_kprintf("%s start\n",__func__);
  193. rt_kprintf("%s end\n",__func__);
  194. return -1;
  195. }
  196. return -1;
  197. }
  198. MSH_CMD_EXPORT(gpio_output_task, gpio output task operation);
  199. /* Gpio input test */
  200. static void key_iqr_handle(void *args)
  201. {
  202. /* gpio iqr fun */
  203. rt_event_send(&led_event,TEST_GPIO_INT_ENV);
  204. }
  205. static void gpio_input_test(void *parameter)
  206. {
  207. uint8_t led_flag = PIN_LOW;
  208. rt_uint32_t e;
  209. rt_pin_mode(TEST_RES_PIN, PIN_MODE_OUTPUT);
  210. rt_pin_write(TEST_RES_PIN, PIN_LOW);
  211. rt_pin_mode(TEST_WAKEUP_PIN,PIN_MODE_INPUT_PULLDOWN);
  212. rt_pin_mode(TEST_INPUT_PIN,PIN_MODE_INPUT_PULLUP);
  213. rt_pin_attach_irq(TEST_INT_PIN,PIN_IRQ_MODE_FALLING,key_iqr_handle,RT_NULL);
  214. rt_pin_irq_enable(TEST_INT_PIN,PIN_IRQ_ENABLE);
  215. if(rt_event_init(&led_event,"led_event",RT_IPC_FLAG_FIFO) != RT_EOK)
  216. {
  217. rt_kprintf("rt_mutex_create error.\n");
  218. }
  219. while(1)
  220. {
  221. if(PIN_LOW == rt_pin_read(TEST_INPUT_PIN))
  222. {
  223. while(PIN_LOW == rt_pin_read(TEST_INPUT_PIN));
  224. rt_event_send(&led_event,TEST_GPIO_KEY_ENV);
  225. }
  226. if(rt_event_recv(&led_event,(TEST_GPIO_INT_ENV|TEST_GPIO_KEY_ENV),
  227. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  228. RT_WAITING_NO, &e) == RT_EOK)
  229. {
  230. led_flag = (led_flag == PIN_LOW)?PIN_HIGH:PIN_LOW;
  231. rt_pin_write(TEST_RES_PIN, led_flag);
  232. }
  233. if(rt_event_recv(&task_event,TASK_KILL_FLAG,
  234. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  235. RT_WAITING_NO, &e) == RT_EOK)
  236. {
  237. rt_thread_t tid = rt_thread_self();
  238. rt_thread_delete(tid);
  239. }
  240. }
  241. }
  242. static int gpio_input_task(int argc, char *argv[])
  243. {
  244. if(argc == 2)
  245. {
  246. if(rt_strcmp(argv[1],"start") == 0)
  247. {
  248. if(rt_mutex_take(task_mutex, RT_WAITING_NO) != RT_EOK)
  249. {
  250. rt_kprintf("The test thread is occupied.\n");
  251. return -RT_ERROR;
  252. }
  253. /* Gpio input test tasks */
  254. rt_thread_t gpio_input_task = rt_thread_create("gpio_input_task",
  255. gpio_input_test, RT_NULL,
  256. THREAD_STACK_SIZE,
  257. THREAD_PRIORITY, THREAD_TIMESLICE);
  258. if (gpio_input_task != RT_NULL)
  259. rt_thread_startup(gpio_input_task);
  260. rt_kprintf("The gpio input task is registered.\n");
  261. }
  262. else if(rt_strcmp(argv[1],"end") == 0)
  263. {
  264. rt_event_send(&task_event,TASK_KILL_FLAG);
  265. rt_mutex_release(task_mutex);
  266. rt_kprintf("The gpio input task has been deleted.\n");
  267. }
  268. }
  269. else
  270. {
  271. rt_kprintf("Necessary parameters are missing.\n");
  272. rt_kprintf("You can use the following commands.\n");
  273. rt_kprintf("%s start\n",__func__);
  274. rt_kprintf("%s end\n",__func__);
  275. return -1;
  276. }
  277. return -1;
  278. }
  279. MSH_CMD_EXPORT(gpio_input_task, gpio input task operation);
  280. /* uart test */
  281. static rt_err_t uart_iqr_handle(rt_device_t dev, rt_size_t size)
  282. {
  283. /* Serial port callback function */
  284. rt_sem_release(&rx_sem);
  285. return RT_EOK;
  286. }
  287. static void uart_thread(void *parameter)
  288. {
  289. char ch;
  290. while (1)
  291. {
  292. /* Serial port readout */
  293. while (rt_device_read(serial, -1, &ch, 1) != 1)
  294. {
  295. /* semaphore blocking */
  296. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  297. }
  298. /* Output the data obtained from the serial port */
  299. rt_device_write(serial, 0, &ch, 1);
  300. rt_device_write(serial,0,"\n",1);
  301. }
  302. }
  303. static int uart_task(int argc, char *argv[])
  304. {
  305. rt_err_t ret = RT_EOK;
  306. char uart_name[RT_NAME_MAX] = "uart1";
  307. char str[] = "hello RT-Thread!\r\n";
  308. if (argc == 3)
  309. {
  310. if(rt_strcmp(argv[2],"start") == 0)
  311. {
  312. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  313. }
  314. else
  315. {
  316. rt_kprintf("Necessary parameters are missing.\n");
  317. rt_kprintf("You can use the following commands.\n");
  318. rt_kprintf("%s <uart name> start\n",__func__);
  319. rt_kprintf("%s <uart name> end\n",__func__);
  320. return -1;
  321. }
  322. }
  323. else if(argc == 2)
  324. {
  325. if(rt_strcmp(argv[1],"start") == 0)
  326. {
  327. }
  328. else
  329. {
  330. rt_kprintf("Necessary parameters are missing.\n");
  331. rt_kprintf("You can use the following commands.\n");
  332. rt_kprintf("%s start\n",__func__);
  333. rt_kprintf("%s end\n",__func__);
  334. return -1;
  335. }
  336. }
  337. else
  338. {
  339. rt_kprintf("Incomplete instruction.\n");
  340. rt_kprintf("You can use the following commands.\n");
  341. rt_kprintf("%s <uart name> start/end\n",__func__);
  342. rt_kprintf("or\n");
  343. rt_kprintf("%s start/end\n",__func__);
  344. return -1;
  345. }
  346. /* Find Serial Devices */
  347. serial = rt_device_find(uart_name);
  348. if (!serial)
  349. {
  350. rt_kprintf("find %s failed!\n", uart_name);
  351. return -RT_ERROR;
  352. }
  353. /* Initializing a Signal */
  354. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  355. /* Open the serial device with read/write and interrupt reception. */
  356. rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  357. /* Setting the receive callback function */
  358. rt_device_set_rx_indicate(serial, uart_iqr_handle);
  359. /* Send String */
  360. rt_device_write(serial, 0, str, (sizeof(str) - 1));
  361. /* Creating a serial thread */
  362. rt_thread_t thread = rt_thread_create("serial",
  363. uart_thread, RT_NULL,
  364. THREAD_STACK_SIZE,
  365. THREAD_PRIORITY, THREAD_TIMESLICE);
  366. if (thread != RT_NULL)
  367. rt_thread_startup(thread);
  368. return ret;
  369. }
  370. MSH_CMD_EXPORT(uart_task, uart device sample);
  371. /* hw/sw iic test */
  372. static void i2c_thread(void *parameter)
  373. {
  374. uint8_t write_addr = 0x00;
  375. eeprom_write_type eeprom_date;
  376. char send_dat[] = "i2c write eeprom";
  377. char read_dat[20] = {0};
  378. struct rt_i2c_msg msg1[2];
  379. eeprom_date.in_data.write_addr = write_addr;
  380. rt_strncpy(eeprom_date.in_data.write_date, send_dat, rt_strlen(send_dat));
  381. msg1[0].addr = 0x51;
  382. msg1[0].flags = RT_I2C_WR;
  383. msg1[0].buf = eeprom_date.data;
  384. msg1[0].len = (rt_strlen(send_dat) + 1);
  385. if (rt_i2c_transfer(i2c_dev, msg1, 1) == 1)
  386. {
  387. rt_kprintf("eeprom write succeed!\n");
  388. rt_kprintf("write_dat = %s\r\n",send_dat);
  389. }
  390. else
  391. {
  392. rt_kprintf("eeprom write error!\n");
  393. }
  394. msg1[0].addr = 0x51;
  395. msg1[0].flags = RT_I2C_WR;
  396. msg1[0].buf = &write_addr;
  397. msg1[0].len = 1;
  398. msg1[1].addr = 0x51;
  399. msg1[1].flags = RT_I2C_RD;
  400. msg1[1].buf = (uint8_t *)read_dat;
  401. msg1[1].len = rt_strlen(send_dat);
  402. if (rt_i2c_transfer(i2c_dev, msg1, 2) == 2)
  403. {
  404. rt_kprintf("eeprom read succeed!\n");
  405. rt_kprintf("read_dat = %s\r\n",read_dat);
  406. }
  407. else
  408. {
  409. rt_kprintf("eeprom read error!\n");
  410. }
  411. }
  412. static int i2c_task(int argc, char *argv[])
  413. {
  414. rt_err_t ret = RT_EOK;
  415. char i2c_name[RT_NAME_MAX] = "hw_i2c1";
  416. if (argc == 3)
  417. {
  418. if(rt_strcmp(argv[2],"start") == 0)
  419. {
  420. rt_strncpy(i2c_name, argv[1], RT_NAME_MAX);
  421. }
  422. else
  423. {
  424. rt_kprintf("Necessary parameters are missing.\n");
  425. rt_kprintf("You can use the following commands.\n");
  426. rt_kprintf("%s <i2c name> start\n",__func__);
  427. rt_kprintf("%s <i2c name> end\n",__func__);
  428. return -1;
  429. }
  430. }
  431. else if(argc == 2)
  432. {
  433. if(rt_strcmp(argv[1],"start") == 0)
  434. {
  435. }
  436. else
  437. {
  438. rt_kprintf("Necessary parameters are missing.\n");
  439. rt_kprintf("You can use the following commands.\n");
  440. rt_kprintf("%s start\n",__func__);
  441. rt_kprintf("%s end\n",__func__);
  442. return -1;
  443. }
  444. }
  445. else
  446. {
  447. rt_kprintf("Incomplete instruction.\n");
  448. rt_kprintf("You can use the following commands.\n");
  449. rt_kprintf("%s <i2c name> start/end\n",__func__);
  450. rt_kprintf("or\n");
  451. rt_kprintf("%s start/end\n",__func__);
  452. return -1;
  453. }
  454. /* Find I2C Devices */
  455. i2c_dev = (struct rt_i2c_bus_device *)rt_device_find(i2c_name);
  456. if (!i2c_dev)
  457. {
  458. rt_kprintf("find %s failed!\n", i2c_name);
  459. return -RT_ERROR;
  460. }
  461. /* Execute I2C read/write eeprom function */
  462. i2c_thread(RT_NULL);
  463. return ret;
  464. }
  465. MSH_CMD_EXPORT(i2c_task, i2c device sample);
  466. /* spi test */
  467. static void spi_thread(void *parameter)
  468. {
  469. rt_uint8_t w25x_read_id = 0x9F;
  470. rt_uint8_t id[5] = {0};
  471. /* Use rt_spi_send_then_recv() to send commands to read IDs */
  472. rt_spi_take_bus(spi_dev);
  473. rt_spi_take(spi_dev);
  474. rt_spi_send_then_recv(spi_dev, &w25x_read_id, 1, id, 3);
  475. rt_spi_release(spi_dev);
  476. rt_spi_release_bus(spi_dev);
  477. rt_kprintf("use rt_spi_send_then_recv() read MX25L6406 ID is:0x%X%X%X\n", id[0], id[1], id[2]);
  478. }
  479. static int spi_task(int argc, char *argv[])
  480. {
  481. rt_err_t ret = RT_EOK;
  482. struct rt_spi_configuration cfg;
  483. char spi_name[RT_NAME_MAX] = "spi1";
  484. char flash_name[RT_NAME_MAX] = "flash";
  485. if (argc == 3)
  486. {
  487. if(rt_strcmp(argv[2],"start") == 0)
  488. {
  489. rt_strncpy(spi_name, argv[1], RT_NAME_MAX);
  490. }
  491. else
  492. {
  493. rt_kprintf("Necessary parameters are missing.\n");
  494. rt_kprintf("You can use the following commands.\n");
  495. rt_kprintf("%s <spi name> start\n",__func__);
  496. rt_kprintf("%s <spi name> end\n",__func__);
  497. return -1;
  498. }
  499. }
  500. else if(argc == 2)
  501. {
  502. if(rt_strcmp(argv[1],"start") == 0)
  503. {
  504. }
  505. else
  506. {
  507. rt_kprintf("Necessary parameters are missing.\n");
  508. rt_kprintf("You can use the following commands.\n");
  509. rt_kprintf("%s start\n",__func__);
  510. rt_kprintf("%s end\n",__func__);
  511. return -1;
  512. }
  513. }
  514. else
  515. {
  516. rt_kprintf("Incomplete instruction.\n");
  517. rt_kprintf("You can use the following commands.\n");
  518. rt_kprintf("%s <spi name> start/end\n",__func__);
  519. rt_kprintf("or\n");
  520. rt_kprintf("%s start/end\n",__func__);
  521. return -1;
  522. }
  523. /* Binding CS pin */
  524. ret = rt_hw_spi_device_attach(spi_name,flash_name,HT_GPIOD,GPIO_PIN_0);
  525. if(ret != RT_EOK)
  526. {
  527. rt_kprintf("Failed CS pin binding for %s!\n", spi_name);
  528. return -RT_ERROR;
  529. }
  530. /* Find flash devices */
  531. spi_dev = (struct rt_spi_device*)rt_device_find(flash_name);
  532. if (!spi_dev)
  533. {
  534. rt_kprintf("find %s failed!\n", spi_name);
  535. return -RT_ERROR;
  536. }
  537. /* Configuring the SPI Bus */
  538. cfg.data_width = 8;
  539. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB;
  540. cfg.max_hz = 8;
  541. rt_spi_configure(spi_dev,&cfg);
  542. rt_kprintf("SPI0 initialization succeeded!\n");
  543. /* Execute flash read and write functions */
  544. spi_thread(RT_NULL);
  545. rt_device_unregister((rt_device_t)spi_dev);
  546. return ret;
  547. }
  548. MSH_CMD_EXPORT(spi_task, spi device sample);
  549. /* adc test */
  550. static void adc_test(void *parameter)
  551. {
  552. rt_uint32_t adc0_ch6_val,adc0_ch7_val;
  553. rt_adc_device_t adc_dev = (rt_adc_device_t)rt_device_find("adc0");
  554. if (!adc_dev)
  555. {
  556. rt_kprintf("No ADC0 device found!\n");
  557. }
  558. else
  559. {
  560. rt_adc_enable(adc_dev,ADC_CH_6);
  561. rt_adc_enable(adc_dev,ADC_CH_7);
  562. }
  563. while(1)
  564. {
  565. adc0_ch6_val = rt_adc_read(adc_dev,6);
  566. adc0_ch7_val = rt_adc_read(adc_dev,7);
  567. rt_kprintf("adc0_ch6_val = %d\n",adc0_ch6_val);
  568. rt_kprintf("adc0_ch7_val = %d\n",adc0_ch7_val);
  569. rt_thread_mdelay(50);
  570. }
  571. }
  572. static int adc_task(int argc, char *argv[])
  573. {
  574. if(argc == 2)
  575. {
  576. if(rt_strcmp(argv[1],"start") == 0)
  577. {
  578. /* Adc test tasks */
  579. rt_thread_t adc_task = rt_thread_create("adc_task",
  580. adc_test, RT_NULL,
  581. THREAD_STACK_SIZE,
  582. THREAD_PRIORITY, THREAD_TIMESLICE);
  583. if (adc_task != RT_NULL)
  584. rt_thread_startup(adc_task);
  585. rt_kprintf("The adc task is registered.\n");
  586. }
  587. else if(rt_strcmp(argv[1],"end") == 0)
  588. {
  589. rt_event_send(&task_event,TASK_KILL_FLAG);
  590. rt_kprintf("The adc task has been deleted.\n");
  591. }
  592. }
  593. else
  594. {
  595. rt_kprintf("Necessary parameters are missing.\n");
  596. rt_kprintf("You can use the following commands.\n");
  597. rt_kprintf("%s start\n",__func__);
  598. rt_kprintf("%s end\n",__func__);
  599. return -1;
  600. }
  601. return -1;
  602. }
  603. MSH_CMD_EXPORT(adc_task, adc task operation);
  604. /* wdt test */
  605. static void wdt_test(void)
  606. {
  607. rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL);
  608. }
  609. static int wdt_task(int argc, char *argv[])
  610. {
  611. rt_err_t ret = -RT_ERROR;
  612. rt_uint16_t wdt_time = 5;
  613. char dev_name[] = "wdt";
  614. if(argc == 2)
  615. {
  616. if(rt_strcmp(argv[1],"start") == 0)
  617. {
  618. /* Find wdt devices */
  619. wdt_dev = rt_device_find(dev_name);
  620. if(wdt_dev == RT_NULL)
  621. {
  622. rt_kprintf("No corresponding equipment found.\n");
  623. return -1;
  624. }
  625. /* Configuring the Watchdog */
  626. ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &wdt_time);
  627. if(ret != RT_EOK)
  628. {
  629. rt_kprintf("wdt configuration failed.\n");
  630. return -1;
  631. }
  632. /* Start the Watchdog */
  633. ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
  634. if(ret != RT_EOK)
  635. {
  636. rt_kprintf("wdt start failed.\n");
  637. return -1;
  638. }
  639. /* Setting up idle threads */
  640. rt_thread_idle_sethook(wdt_test);
  641. rt_kprintf("Watchdog started successfully.\n");
  642. }
  643. else if(rt_strcmp(argv[1],"stop") == 0)
  644. {
  645. /* Verify device handle */
  646. if(wdt_dev == RT_NULL)
  647. {
  648. rt_kprintf("Device handle does not exist.\n");
  649. return -1;
  650. }
  651. /* Stop the Watchdog */
  652. ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_STOP, RT_NULL);
  653. if(ret != RT_EOK)
  654. {
  655. rt_kprintf("wdt start failed.\n");
  656. return -1;
  657. }
  658. /* Hook function to delete idle threads */
  659. rt_thread_idle_delhook(wdt_test);
  660. rt_kprintf("Watchdog has stopped.\n");
  661. }
  662. }
  663. else
  664. {
  665. rt_kprintf("Necessary parameters are missing.\n");
  666. rt_kprintf("You can use the following commands.\n");
  667. rt_kprintf("%s start\n",__func__);
  668. rt_kprintf("%s stop\n",__func__);
  669. return -1;
  670. }
  671. return -1;
  672. }
  673. MSH_CMD_EXPORT(wdt_task, wdt task operation);
  674. #endif /* BSP_USING_TEST */