test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-15 Emuzit first version
  9. * 2022-07-20 Emuzit add watchdog test
  10. * 2022-07-26 Emuzit add hwtimer test
  11. * 2022-07-30 Emuzit add spi master test
  12. * 2022-08-04 Emuzit add pwm test
  13. */
  14. #include <rtthread.h>
  15. #include <drivers/pin.h>
  16. #include <drivers/watchdog.h>
  17. #include <drivers/hwtimer.h>
  18. #include <drivers/spi.h>
  19. #include <drivers/rt_drv_pwm.h>
  20. #include "board.h"
  21. #define PWM_CYCLE_MAX 255
  22. static const rt_base_t gpio_int_pins[8] = GPIO_INT_PINS;
  23. /* note : PIN_IRQ_MODE_RISING_FALLING not supported */
  24. static const uint32_t gpint_mode[] =
  25. {
  26. PIN_IRQ_MODE_RISING,
  27. PIN_IRQ_MODE_RISING,
  28. PIN_IRQ_MODE_RISING,
  29. PIN_IRQ_MODE_RISING,
  30. PIN_IRQ_MODE_FALLING,
  31. PIN_IRQ_MODE_FALLING,
  32. PIN_IRQ_MODE_FALLING,
  33. PIN_IRQ_MODE_FALLING,
  34. };
  35. static struct rt_mailbox *gpint_mb = RT_NULL;
  36. static struct rt_thread *gpint_thread = RT_NULL;
  37. static rt_device_t wdg_dev;
  38. static rt_base_t led0, led1;
  39. static void gpio_int_callback(void *pin)
  40. {
  41. led1 = (led1 == PIN_LOW) ? PIN_HIGH : PIN_LOW;
  42. rt_pin_write(LED1_PIN, led1);
  43. if (gpint_mb != RT_NULL)
  44. {
  45. /* non-block, silently ignore RT_EFULL */
  46. rt_mb_send(gpint_mb, (uint32_t)pin);
  47. }
  48. }
  49. static void gpio_int_thread(void *param)
  50. {
  51. while (1)
  52. {
  53. rt_err_t res;
  54. uint32_t pin;
  55. res = rt_mb_recv(gpint_mb, &pin, RT_WAITING_FOREVER);
  56. if (res == RT_EOK)
  57. {
  58. rt_kprintf("gpio_int #%d (%d)\n", pin, rt_pin_read(pin));
  59. }
  60. rt_thread_mdelay(100);
  61. #ifdef RT_USING_WDT
  62. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL);
  63. #endif
  64. }
  65. }
  66. static void test_gpio_int(void)
  67. {
  68. rt_err_t res;
  69. int i;
  70. rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
  71. rt_pin_write(LED1_PIN, led1 = PIN_HIGH);
  72. /* Enable all gpio interrupt with various modes.
  73. * LED0 or GND touching can be used to trigger pin interrupt.
  74. */
  75. gpint_mb = rt_mb_create("pximb", 8, RT_IPC_FLAG_FIFO);
  76. if (gpint_mb == RT_NULL)
  77. {
  78. rt_kprintf("gpint mailbox create failed !\n");
  79. }
  80. else
  81. {
  82. gpint_thread = rt_thread_create("pxith", gpio_int_thread, RT_NULL,
  83. 512, RT_MAIN_THREAD_PRIORITY, 50);
  84. if (gpint_thread == RT_NULL)
  85. {
  86. rt_kprintf("gpint thread create failed !\n");
  87. }
  88. else
  89. {
  90. rt_thread_startup(gpint_thread);
  91. for (i = 0; i < 8; i++)
  92. {
  93. rt_base_t pin = gpio_int_pins[i];
  94. #ifdef RT_USING_PWM
  95. if (pin == PWM0_PIN || pin == PWM1_PIN)
  96. continue;
  97. #endif
  98. rt_pin_mode(pin, PIN_MODE_INPUT_PULLUP);
  99. res = rt_pin_attach_irq(
  100. pin, gpint_mode[i], gpio_int_callback, (void *)pin);
  101. if (res != RT_EOK)
  102. {
  103. rt_kprintf("rt_pin_attach_irq failed (%d:%d)\n", i, res);
  104. }
  105. else
  106. {
  107. rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. #ifdef RT_USING_WDT
  114. static void test_watchdog(uint32_t seconds)
  115. {
  116. /* Test watchdog with 30s timeout, keepalive with gpio interrupt.
  117. *
  118. * CAVEAT: With only 8-bit WDOG_COUNT and fixed clocking at Fsys/524288,
  119. * watchdog of ch56x may be quite limited with very short timeout.
  120. */
  121. seconds = 30;
  122. wdg_dev = rt_device_find("wdt");
  123. if (!wdg_dev)
  124. {
  125. rt_kprintf("watchdog device not found !\n");
  126. }
  127. else if (rt_device_init(wdg_dev) != RT_EOK ||
  128. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &seconds) != RT_EOK)
  129. {
  130. rt_kprintf("watchdog setup failed !\n");
  131. }
  132. else
  133. {
  134. rt_kprintf("WDT_TIMEOUT in %d seconds, trigger gpio interrupt to keep alive.\n\n", seconds);
  135. }
  136. }
  137. #else
  138. #define test_watchdog(tov) do {} while(0)
  139. #endif
  140. #ifdef RT_USING_HWTIMER
  141. static struct rt_device *tmr_dev_0;
  142. static struct rt_device *tmr_dev_1;
  143. static rt_err_t tmr_timeout_cb(rt_device_t dev, rt_size_t size)
  144. {
  145. rt_tick_t tick = rt_tick_get();
  146. int tmr = (dev == tmr_dev_1) ? 1 : 0;
  147. rt_kprintf("hwtimer %d timeout callback fucntion @tick %d\n", tmr, tick);
  148. return RT_EOK;
  149. }
  150. static void test_hwtimer(void)
  151. {
  152. rt_hwtimerval_t timerval;
  153. rt_hwtimer_mode_t mode;
  154. rt_size_t tsize;
  155. /* setup two timers, ONESHOT & PERIOD each
  156. */
  157. tmr_dev_0 = rt_device_find("timer0");
  158. tmr_dev_1 = rt_device_find("timer1");
  159. if (tmr_dev_0 == RT_NULL || tmr_dev_1 == RT_NULL)
  160. {
  161. rt_kprintf("hwtimer device(s) not found !\n");
  162. }
  163. else if (rt_device_open(tmr_dev_0, RT_DEVICE_OFLAG_RDWR) != RT_EOK ||
  164. rt_device_open(tmr_dev_1, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  165. {
  166. rt_kprintf("hwtimer device(s) open failed !\n");
  167. }
  168. else
  169. {
  170. rt_device_set_rx_indicate(tmr_dev_0, tmr_timeout_cb);
  171. rt_device_set_rx_indicate(tmr_dev_1, tmr_timeout_cb);
  172. timerval.sec = 3;
  173. timerval.usec = 500000;
  174. tsize = sizeof(timerval);
  175. mode = HWTIMER_MODE_ONESHOT;
  176. if (rt_device_control(tmr_dev_0, HWTIMER_CTRL_MODE_SET, &mode) != RT_EOK)
  177. {
  178. rt_kprintf("timer0 set mode failed !\n");
  179. }
  180. else if (rt_device_write(tmr_dev_0, 0, &timerval, tsize) != tsize)
  181. {
  182. rt_kprintf("timer0 start failed !\n");
  183. }
  184. else
  185. {
  186. rt_kprintf("timer0 started !\n");
  187. }
  188. timerval.sec = 5;
  189. timerval.usec = 0;
  190. tsize = sizeof(timerval);
  191. mode = HWTIMER_MODE_PERIOD;
  192. if (rt_device_control(tmr_dev_1, HWTIMER_CTRL_MODE_SET, &mode) != RT_EOK)
  193. {
  194. rt_kprintf("timer1 set mode failed !\n");
  195. }
  196. else if (rt_device_write(tmr_dev_1, 0, &timerval, tsize) != tsize)
  197. {
  198. rt_kprintf("timer1 start failed !\n");
  199. }
  200. else
  201. {
  202. rt_kprintf("timer1 started !\n\n");
  203. }
  204. }
  205. }
  206. #else
  207. #define test_hwtimer() do {} while(0)
  208. #endif
  209. #ifdef RT_USING_SPI
  210. static struct rt_spi_device spi_dev_w25q;
  211. static void test_spi_master(void)
  212. {
  213. struct rt_spi_configuration cfg;
  214. struct rt_spi_message msg1, msg2;
  215. rt_err_t res;
  216. uint8_t buf[16];
  217. int i;
  218. cfg.max_hz = 25 * 1000000;
  219. cfg.data_width = 8;
  220. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
  221. res = rt_spi_bus_attach_device(
  222. &spi_dev_w25q, W25Q32_SPI_NAME, SPI0_BUS_NAME, (void *)W25Q32_CS_PIN);
  223. if (res == RT_EOK && rt_spi_configure(&spi_dev_w25q, &cfg) == RT_EOK)
  224. {
  225. /* cmd : Read Manufacturer / Device ID (90h) */
  226. buf[0] = 0x90;
  227. /* address : 0 */
  228. buf[1] = buf[2] = buf[3] = 0;
  229. msg1.send_buf = buf;
  230. msg1.recv_buf = RT_NULL;
  231. msg1.length = 4;
  232. msg1.cs_take = 1;
  233. msg1.cs_release = 0;
  234. msg1.next = &msg2;
  235. msg2.send_buf = RT_NULL;
  236. msg2.recv_buf = buf;
  237. msg2.length = 2;
  238. msg2.cs_take = 0;
  239. msg2.cs_release = 1;
  240. msg2.next = RT_NULL;
  241. rt_spi_transfer_message(&spi_dev_w25q, &msg1);
  242. rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", buf[0], buf[1]);
  243. /* cmd : Read Data (03h) */
  244. buf[0] = 0x03;
  245. /* address : 0 */
  246. buf[1] = buf[2] = buf[3] = 0;
  247. msg2.length = 16;
  248. if (rt_spi_transfer_message(&spi_dev_w25q, &msg1) == RT_NULL)
  249. {
  250. rt_kprintf("SPI0 16-byte DMA read :");
  251. for (i = 0; i < 16; i++)
  252. rt_kprintf(" %02x", buf[i]);
  253. rt_kprintf("\n\n");
  254. }
  255. }
  256. else
  257. {
  258. rt_kprintf("w25q32 attach/configure failed (%d) !\n", res);
  259. }
  260. }
  261. #else
  262. #define test_spi_master() do {} while(0)
  263. #endif
  264. #ifdef RT_USING_PWM
  265. static struct rt_device_pwm *pwm_dev;
  266. static uint32_t pwm_period;
  267. rt_err_t rt_pwm_get(struct rt_device_pwm *device,
  268. struct rt_pwm_configuration *cfg);
  269. static void pwm_tick_hook(void)
  270. {
  271. uint32_t pulse;
  272. if (pwm_dev)
  273. {
  274. /* PWM.CH3 duty cycle : 0%->100% for every ~2.5 seconds */
  275. pulse = (rt_tick_get() >> 1) % (PWM_CYCLE_MAX + 1);
  276. pulse = (pwm_period * pulse + PWM_CYCLE_MAX/2) / PWM_CYCLE_MAX;
  277. rt_pwm_set_pulse(pwm_dev, 3, pulse);
  278. }
  279. }
  280. static void test_pwm(void)
  281. {
  282. struct rt_pwm_configuration cfg;
  283. uint32_t pulse[4];
  284. int ch;
  285. pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEVICE_NAME);
  286. if (pwm_dev == RT_NULL)
  287. {
  288. rt_kprintf("can't find %s device !\n", PWM_DEVICE_NAME);
  289. }
  290. else
  291. {
  292. /* for HCLK@80MHz, allowed period is 3187 ~ 812812 */
  293. pwm_period = 800*1000;
  294. pulse[0] = 100*1000;
  295. pulse[1] = 400*1000;
  296. pulse[2] = 600*1000;
  297. pulse[3] = 0;
  298. for (ch = 0; ch < PWM_CHANNELS; ch++)
  299. {
  300. rt_pwm_set(pwm_dev, ch, pwm_period, pulse[ch]);
  301. rt_pwm_enable(pwm_dev, ch);
  302. cfg.channel = ch;
  303. rt_pwm_get(pwm_dev, &cfg);
  304. rt_kprintf("pwm%d period set/get : %d/%d\n", ch, pwm_period, cfg.period);
  305. rt_kprintf("pwm%d pulse set/get : %d/%d\n\n", ch, pulse[ch], cfg.pulse);
  306. }
  307. /* disable PWM.CH0 after 1 second, also start changing CH3 */
  308. rt_thread_mdelay(1000);
  309. rt_pwm_disable(pwm_dev, 0);
  310. /* connect PWM3 (PB.2) to LED2 for a visualized PWM effect */
  311. rt_pin_mode(LED2_PIN, PIN_MODE_INPUT);
  312. rt_tick_sethook(pwm_tick_hook);
  313. }
  314. }
  315. #else
  316. #define test_pwm() do {} while(0)
  317. #endif
  318. #ifdef RT_USING_USB_DEVICE
  319. #if !defined(RT_USING_EVENT) || !defined(RT_USING_MESSAGEQUEUE)
  320. #error "event flag or message queue IPC not enabled"
  321. #endif
  322. static struct rt_thread *udvcom_thread;
  323. static rt_device_t vcom;
  324. static void usbd_vcom_thread(void *param)
  325. {
  326. char ch;
  327. while (1)
  328. {
  329. while (rt_device_read(vcom, 0, &ch, 1) != 1)
  330. rt_thread_delay(1);
  331. rt_kprintf("(%2d) %02x:%c\n", rt_device_write(vcom, 0, &ch, 1), ch, ch);
  332. rt_pin_write(LED1_PIN, (ch & 1) ? PIN_LOW : PIN_HIGH);
  333. }
  334. }
  335. static void test_usbd()
  336. {
  337. char name[] = "vcom";
  338. vcom = rt_device_find(name);
  339. if (vcom && rt_device_open(vcom, RT_DEVICE_FLAG_INT_RX) == RT_EOK)
  340. {
  341. rt_kprintf("%s opened\n", name);
  342. rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
  343. rt_pin_write(LED1_PIN, PIN_LOW);
  344. udvcom_thread = rt_thread_create("udvcom", usbd_vcom_thread, vcom,
  345. 512, 20, 50);
  346. if (udvcom_thread != RT_NULL)
  347. rt_thread_startup(udvcom_thread);
  348. else
  349. rt_kprintf("usvcom thread create failed !\n");
  350. rt_device_write(vcom, 0, name, rt_strlen(name));
  351. }
  352. }
  353. #else
  354. #define test_usbd() do {} while(0)
  355. #endif
  356. void main(void)
  357. {
  358. uint32_t wdog_timeout = 32;
  359. rt_kprintf("\nCH569W-R0-1v0, HCLK: %dMHz\n\n", sys_hclk_get() / 1000000);
  360. test_gpio_int();
  361. test_watchdog(wdog_timeout);
  362. test_hwtimer();
  363. test_spi_master();
  364. test_pwm();
  365. test_usbd();
  366. rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
  367. rt_pin_write(LED0_PIN, led0 = PIN_LOW);
  368. while (1)
  369. {
  370. /* flashing LED0 every 1 second */
  371. rt_thread_mdelay(500);
  372. led0 = (led0 == PIN_LOW) ? PIN_HIGH : PIN_LOW;
  373. rt_pin_write(LED0_PIN, led0);
  374. }
  375. }