main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. * 2023-01-31 shelton first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_common.h"
  13. #include "drv_gpio.h"
  14. /* defined the led2 pin: pc2 */
  15. #define LED2_PIN GET_PIN(C, 2)
  16. /* defined the led3 pin: pc3 */
  17. #define LED3_PIN GET_PIN(C, 3)
  18. /* defined the led4 pin: pc5 */
  19. #define LED4_PIN GET_PIN(C, 5)
  20. int main(void)
  21. {
  22. rt_uint32_t speed = 200;
  23. /* set led2 pin mode to output */
  24. rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
  25. /* set led3 pin mode to output */
  26. rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
  27. /* set led4 pin mode to output */
  28. rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
  29. while (1)
  30. {
  31. rt_pin_write(LED2_PIN, PIN_LOW);
  32. rt_thread_mdelay(speed);
  33. rt_pin_write(LED3_PIN, PIN_LOW);
  34. rt_thread_mdelay(speed);
  35. rt_pin_write(LED4_PIN, PIN_LOW);
  36. rt_thread_mdelay(speed);
  37. rt_pin_write(LED2_PIN, PIN_HIGH);
  38. rt_thread_mdelay(speed);
  39. rt_pin_write(LED3_PIN, PIN_HIGH);
  40. rt_thread_mdelay(speed);
  41. rt_pin_write(LED4_PIN, PIN_HIGH);
  42. rt_thread_mdelay(speed);
  43. }
  44. }
  45. #define SAMPLE_UART_NAME "uart2"
  46. static struct rt_semaphore rx_sem;
  47. static rt_device_t serial;
  48. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  49. {
  50. rt_sem_release(&rx_sem);
  51. return RT_EOK;
  52. }
  53. static void serial_thread_entry(void *parameter)
  54. {
  55. rt_uint32_t len = 0;
  56. char ch[20];
  57. while (1)
  58. {
  59. while ((len = rt_device_read(serial, 0, ch, 50)) <= 0)
  60. {
  61. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  62. }
  63. rt_device_write(serial, 0, ch, len);
  64. }
  65. }
  66. static int uart_sample(int argc, char *argv[])
  67. {
  68. rt_err_t ret = RT_EOK;
  69. char uart_name[RT_NAME_MAX];
  70. if (argc == 2)
  71. {
  72. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  73. }
  74. else
  75. {
  76. rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  77. }
  78. serial = rt_device_find(uart_name);
  79. if (!serial)
  80. {
  81. rt_kprintf("find %s failed!\n", uart_name);
  82. return RT_ERROR;
  83. }
  84. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  85. rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
  86. rt_device_set_rx_indicate(serial, uart_input);
  87. rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
  88. if (thread != RT_NULL)
  89. {
  90. rt_thread_startup(thread);
  91. }
  92. else
  93. {
  94. ret = RT_ERROR;
  95. }
  96. return ret;
  97. }
  98. MSH_CMD_EXPORT(uart_sample, uart device sample);
  99. #ifdef BSP_USING_SPI
  100. #include "drv_spi.h"
  101. #include "spi_flash.h"
  102. #include "spi_flash_sfud.h"
  103. static int rt_hw_spi_flash_with_sfud_init(void)
  104. {
  105. rt_hw_spi_device_attach("spi2", "spi2_0", GPIOB, GPIO_PINS_12);
  106. if (RT_NULL == rt_sfud_flash_probe("w25q", "spi2_0"))
  107. {
  108. return -RT_ERROR;
  109. }
  110. return RT_EOK;
  111. }
  112. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
  113. #endif
  114. #ifdef BSP_USING_QSPI
  115. #include "drv_qspi.h"
  116. #include "spi_flash.h"
  117. #include "spi_flash_sfud.h"
  118. static int rt_hw_qspi_flash_with_sfud_init(void)
  119. {
  120. at32_qspi_bus_attach_device("qspi2", "qspi2_0", RT_NULL, 4, RT_NULL, RT_NULL);
  121. /* init n25qxx */
  122. if (RT_NULL == rt_sfud_flash_probe("w25q", "qspi2_0"))
  123. {
  124. return -RT_ERROR;
  125. }
  126. return RT_EOK;
  127. }
  128. INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_with_sfud_init);
  129. #endif
  130. #ifdef BSP_USING_ON_CHIP_FLASH
  131. #include "drv_flash.h"
  132. void dump(rt_uint8_t *buff, rt_uint32_t size)
  133. {
  134. rt_uint32_t index = 0;
  135. rt_kprintf("dump data:");
  136. for(index = 0; index < size; index++)
  137. {
  138. rt_kprintf(" %x", *(buff + index));
  139. }
  140. rt_kprintf("\n");
  141. }
  142. void fill(rt_uint8_t *buff, rt_uint32_t size)
  143. {
  144. rt_uint32_t index = 0;
  145. for(index = 0; index < size; index++)
  146. {
  147. *(buff + index) = ~(*(buff + index) + index);
  148. }
  149. }
  150. static int flash_sample(int argc, char *argv[])
  151. {
  152. #define TEST_ADDR 0x0800F000
  153. #define BUFFER_SIZE 0x1000
  154. rt_uint8_t *buff = rt_malloc(BUFFER_SIZE);
  155. rt_memset(buff, 0, BUFFER_SIZE);
  156. if(at32_flash_read(TEST_ADDR, buff, BUFFER_SIZE) != BUFFER_SIZE)
  157. {
  158. rt_kprintf("flash read error\n");
  159. }
  160. dump(buff, BUFFER_SIZE);
  161. if(at32_flash_erase(TEST_ADDR, BUFFER_SIZE) != BUFFER_SIZE)
  162. {
  163. rt_kprintf("flash erase error\n");
  164. }
  165. fill(buff, BUFFER_SIZE);
  166. if(at32_flash_write(TEST_ADDR, buff, BUFFER_SIZE) != BUFFER_SIZE)
  167. {
  168. rt_kprintf("flash write error\n");
  169. }
  170. if(at32_flash_read(TEST_ADDR, buff, BUFFER_SIZE) != BUFFER_SIZE)
  171. {
  172. rt_kprintf("flash read error\n");
  173. }
  174. dump(buff, BUFFER_SIZE);
  175. rt_free(buff);
  176. return 0;
  177. }
  178. MSH_CMD_EXPORT(flash_sample, flash sample);
  179. #endif
  180. #ifdef RT_USING_WDT
  181. #define WDT_DEVICE_NAME "wdt"
  182. static rt_device_t wdt_dev;
  183. static void idle_hook(void)
  184. {
  185. rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
  186. rt_kprintf("feed the dog!\n ");
  187. }
  188. static int wdt_sample(int argc, char *argv[])
  189. {
  190. rt_err_t ret = RT_EOK;
  191. rt_uint32_t timeout = 1;
  192. char device_name[RT_NAME_MAX];
  193. if (argc == 2)
  194. {
  195. rt_strncpy(device_name, argv[1], RT_NAME_MAX);
  196. }
  197. else
  198. {
  199. rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
  200. }
  201. wdt_dev = rt_device_find(device_name);
  202. if (!wdt_dev)
  203. {
  204. rt_kprintf("find %s failed!\n", device_name);
  205. return RT_ERROR;
  206. }
  207. ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  208. if (ret != RT_EOK)
  209. {
  210. rt_kprintf("set %s timeout failed!\n", device_name);
  211. return RT_ERROR;
  212. }
  213. ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
  214. if (ret != RT_EOK)
  215. {
  216. rt_kprintf("start %s failed!\n", device_name);
  217. return -RT_ERROR;
  218. }
  219. rt_thread_idle_sethook(idle_hook);
  220. return ret;
  221. }
  222. MSH_CMD_EXPORT(wdt_sample, wdt sample);
  223. #endif
  224. #ifdef BSP_USING_CAN
  225. #define CAN_DEV_NAME "can1"
  226. static struct rt_semaphore rx_sem;
  227. static rt_device_t can_dev;
  228. static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
  229. {
  230. rt_sem_release(&rx_sem);
  231. return RT_EOK;
  232. }
  233. static void can_rx_thread(void *parameter)
  234. {
  235. int i;
  236. #ifdef RT_CAN_USING_HDR
  237. rt_err_t res;
  238. #endif
  239. struct rt_can_msg rxmsg = {0};
  240. rt_device_set_rx_indicate(can_dev, can_rx_call);
  241. #ifdef RT_CAN_USING_HDR
  242. struct rt_can_filter_item items[5] =
  243. {
  244. RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,hdr ? - 1,??????? */
  245. RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x300~0x3ff,hdr ? - 1 */
  246. RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL), /* std,match ID:0x211,hdr ? - 1 */
  247. RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), /* std,match ID:0x486,hdr ? - 1 */
  248. {0x555, 0, 0, 0, 0x7ff, 7,} /* std,match ID:0x555,hdr ? 7,???? 7 ???? */
  249. };
  250. struct rt_can_filter_config cfg = {5, 1, items};
  251. res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
  252. RT_ASSERT(res == RT_EOK);
  253. #endif
  254. while (1)
  255. {
  256. rxmsg.hdr_index = -1;
  257. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  258. rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
  259. rt_kprintf("ID:%x", rxmsg.id);
  260. for (i = 0; i < 8; i++)
  261. {
  262. rt_kprintf("%2x", rxmsg.data[i]);
  263. }
  264. rt_kprintf("\n");
  265. }
  266. }
  267. int can_sample(int argc, char *argv[])
  268. {
  269. struct rt_can_msg msg = {0};
  270. rt_err_t res;
  271. rt_size_t size;
  272. rt_thread_t thread;
  273. char can_name[RT_NAME_MAX];
  274. if (argc == 2)
  275. {
  276. rt_strncpy(can_name, argv[1], RT_NAME_MAX);
  277. }
  278. else
  279. {
  280. rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
  281. }
  282. can_dev = rt_device_find(can_name);
  283. if (!can_dev)
  284. {
  285. rt_kprintf("find %s failed!\n", can_name);
  286. return RT_ERROR;
  287. }
  288. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  289. res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
  290. RT_ASSERT(res == RT_EOK);
  291. res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN1MBaud);
  292. thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
  293. if (thread != RT_NULL)
  294. {
  295. rt_thread_startup(thread);
  296. }
  297. else
  298. {
  299. rt_kprintf("create can_rx thread failed!\n");
  300. }
  301. msg.id = 0x78;
  302. msg.ide = RT_CAN_STDID;
  303. msg.rtr = RT_CAN_DTR;
  304. msg.len = 8;
  305. msg.data[0] = 0x00;
  306. msg.data[1] = 0x11;
  307. msg.data[2] = 0x22;
  308. msg.data[3] = 0x33;
  309. msg.data[4] = 0x44;
  310. msg.data[5] = 0x55;
  311. msg.data[6] = 0x66;
  312. msg.data[7] = 0x77;
  313. size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
  314. if (size == 0)
  315. {
  316. rt_kprintf("can dev write data failed!\n");
  317. }
  318. return res;
  319. }
  320. MSH_CMD_EXPORT(can_sample, can_sample);
  321. #endif
  322. #ifdef BSP_USING_I2C
  323. #define EEPROM_I2C_BUS_NAME "i2c1"
  324. #define EEPROM_ADDR 0x50
  325. static rt_err_t read_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
  326. {
  327. rt_uint8_t buf[3];
  328. struct rt_i2c_msg msgs;
  329. buf[0] = reg;
  330. msgs.addr = EEPROM_ADDR;
  331. msgs.flags = RT_I2C_WR;
  332. msgs.buf = buf;
  333. msgs.len = 1;
  334. if (rt_i2c_transfer(bus, &msgs, 1) != 1)
  335. {
  336. return -RT_ERROR;
  337. }
  338. msgs.addr = EEPROM_ADDR;
  339. msgs.flags = RT_I2C_RD;
  340. msgs.buf = data;
  341. msgs.len = 2;
  342. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  343. {
  344. return RT_EOK;
  345. }
  346. else
  347. {
  348. return -RT_ERROR;
  349. }
  350. }
  351. static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
  352. {
  353. rt_uint8_t buf[3];
  354. struct rt_i2c_msg msgs;
  355. buf[0] = reg;
  356. buf[1] = data[0];
  357. buf[2] = data[1];
  358. msgs.addr = EEPROM_ADDR;
  359. msgs.flags = RT_I2C_WR;
  360. msgs.buf = buf;
  361. msgs.len = 3;
  362. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  363. {
  364. return RT_EOK;
  365. }
  366. else
  367. {
  368. return -RT_ERROR;
  369. }
  370. }
  371. int i2c_sample(int argc, char *argv[])
  372. {
  373. rt_err_t ret = RT_EOK;
  374. static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
  375. rt_uint8_t rx_buff[2] = {0}, tx_buff[2] = {0x12, 0x34};
  376. i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(EEPROM_I2C_BUS_NAME);
  377. if (i2c_bus == RT_NULL)
  378. {
  379. rt_kprintf("can't find %s device!\n", EEPROM_I2C_BUS_NAME);
  380. }
  381. else
  382. {
  383. write_reg(i2c_bus, 0x10, tx_buff);
  384. rt_thread_mdelay(10);
  385. read_reg(i2c_bus, 0x10, rx_buff);
  386. rt_kprintf("rx_buff: %x %x\r\n", rx_buff[0], rx_buff[1]);
  387. }
  388. return ret;
  389. }
  390. MSH_CMD_EXPORT(i2c_sample, i2c sample);
  391. #endif
  392. #ifdef BSP_USING_HWTIMER
  393. #define HWTIMER_DEV_NAME "timer3"
  394. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  395. {
  396. rt_kprintf("this is hwtimer timeout callback fucntion!\n");
  397. rt_kprintf("tick is :%d !\n", rt_tick_get());
  398. return 0;
  399. }
  400. static int hwtimer_sample(int argc, char *argv[])
  401. {
  402. rt_err_t ret = RT_EOK;
  403. rt_hwtimerval_t timeout_s;
  404. rt_device_t hw_dev = RT_NULL;
  405. rt_hwtimer_mode_t mode;
  406. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  407. if (hw_dev == RT_NULL)
  408. {
  409. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  410. return RT_ERROR;
  411. }
  412. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  413. if (ret != RT_EOK)
  414. {
  415. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  416. return ret;
  417. }
  418. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  419. mode = HWTIMER_MODE_PERIOD;
  420. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  421. if (ret != RT_EOK)
  422. {
  423. rt_kprintf("set mode failed! ret is :%d\n", ret);
  424. return ret;
  425. }
  426. timeout_s.sec = 5;
  427. timeout_s.usec = 0;
  428. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  429. {
  430. rt_kprintf("set timeout value failed\n");
  431. return RT_ERROR;
  432. }
  433. rt_thread_mdelay(1000);
  434. rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  435. rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  436. return ret;
  437. }
  438. MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
  439. #endif
  440. #ifdef BSP_USING_SPI
  441. #include <rtthread.h>
  442. #include <rtdevice.h>
  443. #include "drv_spi.h"
  444. #define W25Q_SPI_DEVICE "spi1_0"
  445. #define MAX_DATA_LEN 3
  446. static void spi_sample(int argc, char * argv[])
  447. {
  448. struct rt_spi_device *spi_device;
  449. char name[RT_NAME_MAX];
  450. rt_uint8_t tx_data[MAX_DATA_LEN] = {0x0};
  451. rt_uint8_t rx_data[MAX_DATA_LEN] = {0x0};
  452. rt_uint32_t index = 0;
  453. rt_hw_spi_device_attach("spi1", W25Q_SPI_DEVICE, GPIOA, GPIO_PINS_4);
  454. for(index = 0; index < MAX_DATA_LEN; index ++)
  455. {
  456. tx_data[index] = index;
  457. }
  458. if(argc == 2)
  459. {
  460. rt_strncpy(name, argv[1], RT_NAME_MAX);
  461. }
  462. else
  463. {
  464. rt_strncpy(name, W25Q_SPI_DEVICE, RT_NAME_MAX);
  465. }
  466. spi_device = (struct rt_spi_device *)rt_device_find(name);
  467. if(!spi_device)
  468. {
  469. rt_kprintf("can't find %s device!\n", name);
  470. }
  471. else
  472. {
  473. rt_spi_send_then_recv(spi_device, tx_data, MAX_DATA_LEN, rx_data, MAX_DATA_LEN);
  474. rt_kprintf("recv data:\n");
  475. for(index = 0; index < MAX_DATA_LEN; index ++)
  476. {
  477. rt_kprintf(" 0x%x", rx_data[index]);
  478. }
  479. rt_kprintf("\n");
  480. struct rt_spi_message msg1, msg2;
  481. msg1.send_buf = tx_data;
  482. msg1.recv_buf = RT_NULL;
  483. msg1.length = MAX_DATA_LEN;
  484. msg1.cs_take = 1;
  485. msg1.cs_release = 0;
  486. msg1.next = RT_NULL;
  487. rt_spi_transfer_message(spi_device, &msg1);
  488. msg2.send_buf = RT_NULL;
  489. msg2.recv_buf = rx_data;
  490. msg2.length = MAX_DATA_LEN;
  491. msg2.cs_take = 0;
  492. msg2.cs_release = 1;
  493. msg2.next = RT_NULL;
  494. rt_spi_transfer_message(spi_device, &msg2);
  495. }
  496. }
  497. MSH_CMD_EXPORT(spi_sample, spi device sample);
  498. #endif