main.c 23 KB

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