main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. */
  9. #include <rtthread.h>
  10. #include <rtdevice.h>
  11. #include "board.h"
  12. #define LED_PIN GET_PIN(B,0)
  13. int main(void)
  14. {
  15. int count = 1;
  16. rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
  17. while (count++)
  18. {
  19. rt_pin_write(LED_PIN, PIN_HIGH);
  20. rt_thread_mdelay(500);
  21. rt_pin_write(LED_PIN, PIN_LOW);
  22. rt_thread_mdelay(500);
  23. }
  24. return RT_EOK;
  25. }
  26. #ifdef BSP_USING_GPIO
  27. #define KEY1_PIN GET_PIN(A,8)
  28. void key1_cb(void *args)
  29. {
  30. rt_kprintf("key1 irq!\n");
  31. }
  32. static int pin_sample(int argc, char *argv[])
  33. {
  34. rt_pin_mode(KEY1_PIN, PIN_IRQ_MODE_FALLING);
  35. rt_pin_attach_irq(KEY1_PIN, PIN_IRQ_MODE_FALLING, key1_cb, RT_NULL);
  36. rt_pin_irq_enable(KEY1_PIN, PIN_IRQ_ENABLE);
  37. return RT_EOK;
  38. }
  39. MSH_CMD_EXPORT(pin_sample, pin sample);
  40. #endif
  41. #ifdef BSP_USING_ADC
  42. #define ADC_DEV_NAME "adc0"
  43. #define ADC_DEV_CHANNEL 0
  44. #define REFER_VOLTAGE 330
  45. #define CONVERT_BITS (1 << 12)
  46. static int adc_vol_sample(int argc, char *argv[])
  47. {
  48. rt_adc_device_t adc_dev;
  49. rt_uint32_t value, vol;
  50. rt_err_t ret = RT_EOK;
  51. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  52. if (adc_dev == RT_NULL)
  53. {
  54. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  55. return -RT_ERROR;
  56. }
  57. ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
  58. value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
  59. rt_kprintf("the value is :%d,", value);
  60. vol = value * REFER_VOLTAGE / CONVERT_BITS;
  61. rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
  62. ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
  63. return ret;
  64. }
  65. MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);
  66. #endif
  67. #ifdef BSP_USING_DAC
  68. #include <stdlib.h>
  69. #define DAC_DEV_NAME "dac" /* DAC 设备名称 */
  70. #define DAC_DEV_CHANNEL 0 /* DAC 通道 */
  71. #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
  72. #define CONVERT_BITS (1 << 12) /* 转换位数为12位 */
  73. static int dac_vol_sample(int argc, char *argv[])
  74. {
  75. rt_dac_device_t dac_dev;
  76. rt_uint32_t value, vol;
  77. rt_err_t ret = RT_EOK;
  78. /* 查找设备 */
  79. dac_dev = (rt_dac_device_t)rt_device_find(DAC_DEV_NAME);
  80. if (dac_dev == RT_NULL)
  81. {
  82. rt_kprintf("dac sample run failed! can't find %s device!\n", DAC_DEV_NAME);
  83. return -RT_ERROR;
  84. }
  85. /* 打开通道 */
  86. ret = rt_dac_enable(dac_dev, DAC_DEV_CHANNEL);
  87. /* 设置输出值 */
  88. value = atoi(argv[1]);
  89. rt_dac_write(dac_dev, DAC_DEV_CHANNEL, value);
  90. rt_kprintf("the value is :%d \n", value);
  91. /* 转换为对应电压值 */
  92. vol = value * REFER_VOLTAGE / CONVERT_BITS;
  93. rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
  94. rt_thread_mdelay(500);
  95. /* 关闭通道 */
  96. ret = rt_dac_disable(dac_dev, DAC_DEV_CHANNEL);
  97. return ret;
  98. }
  99. /* 导出到 msh 命令列表中 */
  100. MSH_CMD_EXPORT(dac_vol_sample, dac voltage convert sample);
  101. #endif
  102. #ifdef BSP_USING_CAN
  103. #define CAN_DEV_NAME "can0" /* CAN 设备名称 */
  104. static struct rt_semaphore rx_sem; /* 用于接收消息的信号量 */
  105. static rt_device_t can_dev; /* CAN 设备句柄 */
  106. /* 接收数据回调函数 */
  107. static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
  108. {
  109. /* CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
  110. rt_sem_release(&rx_sem);
  111. return RT_EOK;
  112. }
  113. static void can_rx_thread(void *parameter)
  114. {
  115. int i;
  116. rt_err_t res;
  117. struct rt_can_msg rxmsg = {0};
  118. /* 设置接收回调函数 */
  119. rt_device_set_rx_indicate(can_dev, can_rx_call);
  120. #ifdef RT_CAN_USING_HDR
  121. struct rt_can_filter_item items[5] =
  122. {
  123. RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,hdr 为 - 1,设置默认过滤表 */
  124. RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x300~0x3ff,hdr 为 - 1 */
  125. RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL), /* std,match ID:0x211,hdr 为 - 1 */
  126. RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), /* std,match ID:0x486,hdr 为 - 1 */
  127. {0x555, 0, 0, 0, 0x7ff, 7,} /* std,match ID:0x555,hdr 为 7,指定设置 7 号过滤表 */
  128. };
  129. struct rt_can_filter_config cfg = {5, 1, items}; /* 一共有 5 个过滤表 */
  130. /* 设置硬件过滤表 */
  131. res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
  132. RT_ASSERT(res == RT_EOK);
  133. #endif
  134. while (1)
  135. {
  136. /* hdr 值为 - 1,表示直接从 uselist 链表读取数据 */
  137. v .hdr = -1;
  138. /* 阻塞等待接收信号量 */
  139. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  140. /* 从 CAN 读取一帧数据 */
  141. rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
  142. /* 打印数据 ID 及内容 */
  143. rt_kprintf("ID:%x", rxmsg.id);
  144. for (i = 0; i < 8; i++)
  145. {
  146. rt_kprintf("%2x", rxmsg.data[i]);
  147. }
  148. rt_kprintf("\n");
  149. }
  150. }
  151. int can_sample(int argc, char *argv[])
  152. {
  153. struct rt_can_msg msg = {0};
  154. rt_err_t res;
  155. rt_size_t size;
  156. rt_thread_t thread;
  157. char can_name[RT_NAME_MAX];
  158. if (argc == 2)
  159. {
  160. rt_strncpy(can_name, argv[1], RT_NAME_MAX);
  161. }
  162. else
  163. {
  164. rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
  165. }
  166. /* 查找 CAN 设备 */
  167. can_dev = rt_device_find(can_name);
  168. if (!can_dev)
  169. {
  170. rt_kprintf("find %s failed!\n", can_name);
  171. return -RT_ERROR;
  172. }
  173. /* 初始化 CAN 接收信号量 */
  174. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  175. /* 以中断接收及发送方式打开 CAN 设备 */
  176. res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
  177. RT_ASSERT(res == RT_EOK);
  178. /* 创建数据接收线程 */
  179. thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
  180. if (thread != RT_NULL)
  181. {
  182. rt_thread_startup(thread);
  183. }
  184. else
  185. {
  186. rt_kprintf("create can_rx thread failed!\n");
  187. }
  188. msg.id = 0x78; /* ID 为 0x78 */
  189. msg.ide = RT_CAN_STDID; /* 标准格式 */
  190. msg.rtr = RT_CAN_DTR; /* 数据帧 */
  191. msg.len = 8; /* 数据长度为 8 */
  192. /* 待发送的 8 字节数据 */
  193. msg.data[0] = 0x00;
  194. msg.data[1] = 0x11;
  195. msg.data[2] = 0x22;
  196. msg.data[3] = 0x33;
  197. msg.data[4] = 0x44;
  198. msg.data[5] = 0x55;
  199. msg.data[6] = 0x66;
  200. msg.data[7] = 0x77;
  201. /* 发送一帧 CAN 数据 */
  202. size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
  203. if (size == 0)
  204. {
  205. rt_kprintf("can dev write data failed!\n");
  206. }
  207. return res;
  208. }
  209. /* 导出到 msh 命令列表中 */
  210. MSH_CMD_EXPORT(can_sample, can device sample);
  211. #endif
  212. #ifdef BSP_USING_TIM
  213. #define HWTIMER_DEV_NAME "timer0"
  214. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  215. {
  216. rt_kprintf("this is hwtimer timeout callback fucntion!\n");
  217. rt_kprintf("tick is :%d !\n", rt_tick_get());
  218. return 0;
  219. }
  220. static int hwtimer_sample(int argc, char *argv[])
  221. {
  222. rt_err_t ret = RT_EOK;
  223. rt_hwtimerval_t timeout_s;
  224. rt_device_t hw_dev = RT_NULL;
  225. rt_hwtimer_mode_t mode;
  226. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  227. if (hw_dev == RT_NULL)
  228. {
  229. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  230. return -RT_ERROR;
  231. }
  232. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  233. if (ret != RT_EOK)
  234. {
  235. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  236. return ret;
  237. }
  238. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  239. mode = HWTIMER_MODE_PERIOD;
  240. //mode = HWTIMER_MODE_ONESHOT;
  241. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  242. if (ret != RT_EOK)
  243. {
  244. rt_kprintf("set mode failed! ret is :%d\n", ret);
  245. return ret;
  246. }
  247. timeout_s.sec = 2;
  248. timeout_s.usec = 0;
  249. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  250. {
  251. rt_kprintf("set timeout value failed\n");
  252. return -RT_ERROR;
  253. }
  254. rt_thread_mdelay(3500);
  255. rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  256. rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  257. return ret;
  258. }
  259. MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
  260. #endif
  261. #ifdef BSP_USING_PWM
  262. #define PWM_DEV_NAME "pwm0" /* PWM设备名称 */
  263. #define PWM_DEV_CHANNEL 0 /* PWM通道 */
  264. struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
  265. static int pwm_sample(int argc, char *argv[])
  266. {
  267. rt_uint32_t period, pulse;
  268. period = 500000; /* 周期为0.5ms,单位为纳秒ns */
  269. pulse = 100000; /* PWM脉冲宽度值,单位为纳秒ns */
  270. pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
  271. if (pwm_dev == RT_NULL)
  272. {
  273. rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
  274. return -RT_ERROR;
  275. }
  276. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
  277. rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
  278. return RT_EOK;
  279. }
  280. MSH_CMD_EXPORT(pwm_sample, pwm sample);
  281. #endif
  282. #ifdef BSP_USING_RTC
  283. #include "sys/time.h"
  284. static int rtc_sample(int argc, char *argv[])
  285. {
  286. rt_err_t ret = RT_EOK;
  287. time_t now;
  288. ret = set_date(2000, 2, 28);
  289. if (ret != RT_EOK)
  290. {
  291. rt_kprintf("set RTC date failed\n");
  292. return ret;
  293. }
  294. ret = set_time(23, 59, 55);
  295. if (ret != RT_EOK)
  296. {
  297. rt_kprintf("set RTC time failed\n");
  298. return ret;
  299. }
  300. rt_thread_mdelay(3000);
  301. now = time(RT_NULL);
  302. rt_kprintf("%s\n", ctime(&now));
  303. return ret;
  304. }
  305. MSH_CMD_EXPORT(rtc_sample, rtc sample);
  306. #endif
  307. #ifdef RT_USING_WDT
  308. #define WDT_DEVICE_NAME "wdt"
  309. static rt_device_t wdg_dev;
  310. static void idle_hook(void)
  311. {
  312. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL);
  313. rt_kprintf("feed the dog!\n ");
  314. }
  315. static int wdt_sample(int argc, char *argv[])
  316. {
  317. rt_err_t ret = RT_EOK;
  318. rt_uint32_t timeout = 5;
  319. char device_name[RT_NAME_MAX];
  320. if (argc == 2)
  321. {
  322. rt_strncpy(device_name, argv[1], RT_NAME_MAX);
  323. }
  324. else
  325. {
  326. rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
  327. }
  328. wdg_dev = rt_device_find(device_name);
  329. if (!wdg_dev)
  330. {
  331. rt_kprintf("find %s failed!\n", device_name);
  332. return -RT_ERROR;
  333. }
  334. ret = rt_device_init(wdg_dev);
  335. if (ret != RT_EOK)
  336. {
  337. rt_kprintf("initialize %s failed!\n", device_name);
  338. return -RT_ERROR;
  339. }
  340. ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  341. if (ret != RT_EOK)
  342. {
  343. rt_kprintf("set %s timeout failed!\n", device_name);
  344. return -RT_ERROR;
  345. }
  346. ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
  347. if (ret != RT_EOK)
  348. {
  349. rt_kprintf("start %s failed!\n", device_name);
  350. return -RT_ERROR;
  351. }
  352. // rt_thread_idle_sethook(idle_hook);
  353. return ret;
  354. }
  355. MSH_CMD_EXPORT(wdt_sample, wdt sample);
  356. #endif
  357. #ifdef BSP_USING_SPI
  358. #define W25Q_SPI_DEVICE_NAME "spi00"
  359. #define W25Q_FLASH_NAME "norflash0"
  360. #include "drv_spi.h"
  361. #ifdef RT_USING_SFUD
  362. #include "spi_flash_sfud.h"
  363. static int rt_hw_spi_flash_init(void)
  364. {
  365. rt_hw_spi_device_attach("spi0", "spi00", GPIOM, PIN3);
  366. if (RT_NULL == rt_sfud_flash_probe(W25Q_FLASH_NAME, W25Q_SPI_DEVICE_NAME))
  367. {
  368. return -RT_ERROR;
  369. };
  370. return RT_EOK;
  371. }
  372. /* 导出到自动初始化 */
  373. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
  374. static void spi_w25q_sample(int argc, char *argv[])
  375. {
  376. struct rt_spi_device *spi_dev_w25q;
  377. char name[RT_NAME_MAX];
  378. rt_uint8_t w25x_read_id = 0x90;
  379. rt_uint8_t id[5] = {0};
  380. if (argc == 2)
  381. {
  382. rt_strncpy(name, argv[1], RT_NAME_MAX);
  383. }
  384. else
  385. {
  386. rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
  387. }
  388. /* 查找 spi 设备获取设备句柄 */
  389. spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
  390. if (!spi_dev_w25q)
  391. {
  392. rt_kprintf("spi sample run failed! can't find %s device!\n", name);
  393. }
  394. else
  395. {
  396. /* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */
  397. rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
  398. rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
  399. /* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */
  400. struct rt_spi_message msg1, msg2;
  401. msg1.send_buf = &w25x_read_id;
  402. msg1.recv_buf = RT_NULL;
  403. msg1.length = 1;
  404. msg1.cs_take = 1;
  405. msg1.cs_release = 0;
  406. msg1.next = &msg2;
  407. msg2.send_buf = RT_NULL;
  408. msg2.recv_buf = id;
  409. msg2.length = 5;
  410. msg2.cs_take = 0;
  411. msg2.cs_release = 1;
  412. msg2.next = RT_NULL;
  413. rt_spi_transfer_message(spi_dev_w25q, &msg1);
  414. rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
  415. }
  416. }
  417. /* 导出到 msh 命令列表中 */
  418. MSH_CMD_EXPORT(spi_w25q_sample, spi w25q sample);
  419. #ifdef RT_USING_DFS_ELMFAT
  420. #include <dfs_file.h>
  421. #include <unistd.h>
  422. static void elmfat_sample(void)
  423. {
  424. int fd, size;
  425. struct statfs elm_stat;
  426. char str[] = "elmfat mount to W25Q flash.\r\n", buf[80];
  427. if (dfs_mkfs("elm", W25Q_FLASH_NAME) == 0)
  428. rt_kprintf("make elmfat filesystem success.\n");
  429. if (dfs_mount(W25Q_FLASH_NAME, "/", "elm", 0, 0) == 0)
  430. rt_kprintf("elmfat filesystem mount success.\n");
  431. if (statfs("/", &elm_stat) == 0)
  432. rt_kprintf("elmfat filesystem block size: %d, total blocks: %d, free blocks: %d.\n",
  433. elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
  434. if (mkdir("/user", 0x777) == 0)
  435. rt_kprintf("make a directory: '/user'.\n");
  436. rt_kprintf("Write string '%s' to /user/test.txt.\n", str);
  437. fd = open("/user/test.txt", O_WRONLY | O_CREAT);
  438. if (fd >= 0)
  439. {
  440. if (write(fd, str, sizeof(str)) == sizeof(str))
  441. rt_kprintf("Write data done.\n");
  442. close(fd);
  443. }
  444. fd = open("/user/test.txt", O_RDONLY);
  445. if (fd >= 0)
  446. {
  447. size = read(fd, buf, sizeof(buf));
  448. close(fd);
  449. if (size == sizeof(str))
  450. rt_kprintf("Read data from file test.txt(size: %d): %s \n", size, buf);
  451. }
  452. }
  453. MSH_CMD_EXPORT(elmfat_sample, elmfat sample);
  454. #endif
  455. #endif
  456. #endif
  457. #ifdef BSP_USING_SPI
  458. #ifdef RT_USING_SPI_MSD
  459. #define SD_SPI_DEVICE_NAME "spi00"
  460. #define SDCARD_NAME "sd0"
  461. #include "drv_spi.h"
  462. #include "spi_msd.h"
  463. #include <dfs_file.h>
  464. #include <unistd.h>
  465. static int rt_hw_spi0_tfcard(void)
  466. {
  467. rt_hw_spi_device_attach("spi0", SD_SPI_DEVICE_NAME, GPION, PIN1);
  468. return msd_init(SDCARD_NAME, SD_SPI_DEVICE_NAME);
  469. }
  470. INIT_DEVICE_EXPORT(rt_hw_spi0_tfcard);
  471. static void elmfat_sample(void)
  472. {
  473. int fd, size;
  474. struct statfs elm_stat;
  475. char str[] = "elmfat mount to sdcard.\r\n", buf[80];
  476. if (dfs_mkfs("elm", SDCARD_NAME) == 0)
  477. rt_kprintf("make elmfat filesystem success.\n");
  478. if (dfs_mount(SDCARD_NAME, "/", "elm", 0, 0) == 0)
  479. rt_kprintf("elmfat filesystem mount success.\n");
  480. if (statfs("/", &elm_stat) == 0)
  481. rt_kprintf("elmfat filesystem block size: %d, total blocks: %d, free blocks: %d.\n",
  482. elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
  483. if (mkdir("/user", 0x777) == 0)
  484. rt_kprintf("make a directory: '/user'.\n");
  485. rt_kprintf("Write string '%s' to /user/test.txt.\n", str);
  486. fd = open("/user/test.txt", O_WRONLY | O_CREAT);
  487. if (fd >= 0)
  488. {
  489. if (write(fd, str, sizeof(str)) == sizeof(str))
  490. rt_kprintf("Write data done.\n");
  491. close(fd);
  492. }
  493. fd = open("/user/test.txt", O_RDONLY);
  494. if (fd >= 0)
  495. {
  496. size = read(fd, buf, sizeof(buf));
  497. close(fd);
  498. if (size == sizeof(str))
  499. rt_kprintf("Read data from file test.txt(size: %d): %s \n", size, buf);
  500. }
  501. }
  502. MSH_CMD_EXPORT(elmfat_sample, elmfat sample);
  503. #endif
  504. #endif
  505. #ifdef BSP_USING_SDIO
  506. #define SDCARD_NAME "sd0"
  507. #include <dfs_file.h>
  508. #include <unistd.h>
  509. static void elmfat_sample(void)
  510. {
  511. int fd, size;
  512. struct statfs elm_stat;
  513. char str[] = "elmfat mount to sdcard.\n", buf[80];
  514. if (dfs_mkfs("elm", SDCARD_NAME) == 0)
  515. rt_kprintf("make elmfat filesystem success.\n");
  516. if (dfs_mount(SDCARD_NAME, "/", "elm", 0, 0) == 0)
  517. rt_kprintf("elmfat filesystem mount success.\n");
  518. if (statfs("/", &elm_stat) == 0)
  519. rt_kprintf("elmfat filesystem block size: %d, total blocks: %d, free blocks: %d.\n",
  520. elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
  521. if (mkdir("/user", 0x777) == 0)
  522. rt_kprintf("make a directory: '/user'.\n");
  523. rt_kprintf("Write string '%s' to /user/test.txt.\n", str);
  524. fd = open("/user/test.txt", O_WRONLY | O_CREAT);
  525. if (fd >= 0)
  526. {
  527. if (write(fd, str, sizeof(str)) == sizeof(str))
  528. rt_kprintf("Write data done.\n");
  529. close(fd);
  530. }
  531. fd = open("/user/test.txt", O_RDONLY);
  532. if (fd >= 0)
  533. {
  534. size = read(fd, buf, sizeof(buf));
  535. close(fd);
  536. if (size == sizeof(str))
  537. rt_kprintf("Read data from file test.txt(size: %d): %s \n", size, buf);
  538. }
  539. }
  540. MSH_CMD_EXPORT(elmfat_sample, elmfat sample);
  541. #endif
  542. #ifdef RT_USING_HWCRYPTO
  543. static void crypto_sample(void)
  544. {
  545. #ifdef BSP_USING_CRC
  546. rt_uint8_t temp[] = {0, 1, 2, 3, 4, 5, 6, 7};
  547. struct rt_hwcrypto_ctx *ctx;
  548. rt_uint32_t result = 0;
  549. struct hwcrypto_crc_cfg cfg =
  550. {
  551. .last_val = 0x00000000,
  552. .poly = 0x04C11DB7,
  553. .width = 8,
  554. .xorout = 0x00000000, //不支持XOR
  555. .flags = 0,
  556. };
  557. ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
  558. rt_hwcrypto_crc_cfg(ctx, &cfg);
  559. result = rt_hwcrypto_crc_update(ctx, temp, sizeof(temp));
  560. rt_kprintf("result: 0x%08x \n", result);
  561. rt_hwcrypto_crc_destroy(ctx);
  562. #endif /* BSP_USING_CRC */
  563. #ifdef BSP_USING_RNG
  564. rt_uint32_t rng_result = 0;
  565. int i;
  566. for (i = 0; i < 20; i++)
  567. {
  568. rng_result = rt_hwcrypto_rng_update();
  569. rt_kprintf("rng:0x%08x.\n", rng_result);
  570. }
  571. #endif /* BSP_USING_RNG */
  572. }
  573. MSH_CMD_EXPORT(crypto_sample, crypto sample);
  574. #endif
  575. #ifdef BSP_USING_SDRAM
  576. #include <rtthread.h>
  577. #define THREAD_PRIORITY 25
  578. #define THREAD_STACK_SIZE 512
  579. #define THREAD_TIMESLICE 5
  580. /* 线程入口 */
  581. void thread1_entry(void *parameter)
  582. {
  583. int i;
  584. char *ptr = RT_NULL; /* 内存块的指针 */
  585. for (i = 0;; i++)
  586. {
  587. /* 每次分配 (1 << i) 大小字节数的内存空间 */
  588. ptr = rt_malloc(1 << i);
  589. /* 如果分配成功 */
  590. if (ptr != RT_NULL)
  591. {
  592. rt_kprintf("get memory :%d byte\n", (1 << i));
  593. /* 释放内存块 */
  594. rt_free(ptr);
  595. rt_kprintf("free memory :%d byte\n", (1 << i));
  596. ptr = RT_NULL;
  597. }
  598. else
  599. {
  600. rt_kprintf("try to get %d byte memory failed!\n", (1 << i));
  601. return;
  602. }
  603. }
  604. }
  605. int dynmem_sample(void)
  606. {
  607. rt_thread_t tid = RT_NULL;
  608. /* 创建线程 1 */
  609. tid = rt_thread_create("thread1",
  610. thread1_entry, RT_NULL,
  611. THREAD_STACK_SIZE,
  612. THREAD_PRIORITY,
  613. THREAD_TIMESLICE);
  614. if (tid != RT_NULL)
  615. rt_thread_startup(tid);
  616. return 0;
  617. }
  618. /* 导出到 msh 命令列表中 */
  619. MSH_CMD_EXPORT(dynmem_sample, dynmem sample);
  620. #endif
  621. #ifdef RT_USING_TOUCH
  622. #include "gt9147.h"
  623. #define THREAD_PRIORITY 25
  624. #define THREAD_STACK_SIZE 512
  625. #define THREAD_TIMESLICE 5
  626. int rt_hw_gt9147_port(void)
  627. {
  628. struct rt_touch_config config;
  629. rt_uint8_t rst;
  630. rst = GT9147_RST_PIN;
  631. config.dev_name = "i2c0";
  632. config.irq_pin.pin = GT9147_IRQ_PIN;
  633. config.irq_pin.mode = PIN_MODE_INPUT_PULLDOWN;
  634. config.user_data = &rst;
  635. rt_hw_gt9147_init("gt9147", &config);
  636. return 0;
  637. }
  638. INIT_ENV_EXPORT(rt_hw_gt9147_port);
  639. static rt_thread_t gt9147_thread = RT_NULL;
  640. static rt_sem_t gt9147_sem = RT_NULL;
  641. static rt_device_t dev = RT_NULL;
  642. static struct rt_touch_data *read_data;
  643. /* 读取数据线程入口函数 */
  644. static void gt9147_entry(void *parameter)
  645. {
  646. struct rt_touch_data *read_data;
  647. read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * 5);
  648. while (1)
  649. {
  650. /* 请求信号量 */
  651. rt_sem_take(gt9147_sem, RT_WAITING_FOREVER);
  652. /* 读取五个点的触摸信息 */
  653. if (rt_device_read(dev, 0, read_data, 5) == 5)
  654. {
  655. for (rt_uint8_t i = 0; i < 5; i++)
  656. {
  657. if (read_data[i].event == RT_TOUCH_EVENT_DOWN || read_data[i].event == RT_TOUCH_EVENT_MOVE)
  658. {
  659. rt_kprintf("%d %d %d %d %d\n",
  660. read_data[i].track_id,
  661. read_data[i].x_coordinate,
  662. read_data[i].y_coordinate,
  663. read_data[i].timestamp,
  664. read_data[i].width);
  665. }
  666. }
  667. }
  668. /* 打开中断 */
  669. rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  670. }
  671. }
  672. /* 接收回调函数 */
  673. static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
  674. {
  675. /* 关闭中断 */
  676. rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
  677. /* 释放信号量 */
  678. rt_sem_release(gt9147_sem);
  679. return 0;
  680. }
  681. static int gt9147_sample(void)
  682. {
  683. /* 查找 Touch 设备 */
  684. dev = rt_device_find("gt9147");
  685. if (dev == RT_NULL)
  686. {
  687. rt_kprintf("can't find device:%s\n", "touch");
  688. return -1;
  689. }
  690. /* 以中断的方式打开设备 */
  691. if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
  692. {
  693. rt_kprintf("open device failed!");
  694. return -1;
  695. }
  696. /* 设置接收回调 */
  697. rt_device_set_rx_indicate(dev, rx_callback);
  698. /* 创建信号量 */
  699. gt9147_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO);
  700. if (gt9147_sem == RT_NULL)
  701. {
  702. rt_kprintf("create dynamic semaphore failed.\n");
  703. return -1;
  704. }
  705. /* 创建读取数据线程 */
  706. gt9147_thread = rt_thread_create("thread1",
  707. gt9147_entry,
  708. RT_NULL,
  709. THREAD_STACK_SIZE,
  710. THREAD_PRIORITY,
  711. THREAD_TIMESLICE);
  712. /* 启动线程 */
  713. if (gt9147_thread != RT_NULL)
  714. rt_thread_startup(gt9147_thread);
  715. return 0;
  716. }
  717. MSH_CMD_EXPORT(gt9147_sample, gt9147 sample);
  718. #endif