test.c 23 KB

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