rpmsg_echo.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * RT-Thread RuiChing
  3. *
  4. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * The license and distribution terms for this file may be
  8. * found in the file LICENSE in this distribution.
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. struct rt_rpmsg_ep_addr
  13. {
  14. const char *name;
  15. rt_uint32_t src;
  16. rt_uint32_t dst;
  17. };
  18. #define THREAD_PRIORITY 18
  19. #define THREAD_STACK_SIZE 5120
  20. #define THREAD_TIMESLICE 5
  21. #define BUFF_SIZE 256
  22. static struct rt_device *rpmsg = RT_NULL;
  23. struct rt_rpmsg_ep_addr rpmsg_remote_echo = {
  24. "rpmsg_chrdev", 0x3000U, 0x1000
  25. };
  26. static void rpmsg_echo_thread(void *parameter)
  27. {
  28. rt_uint32_t len = 0;
  29. rt_uint8_t buff[BUFF_SIZE];
  30. while ((len = rt_device_read(rpmsg, rpmsg_remote_echo.src, buff, BUFF_SIZE - 1)) >= 0)
  31. {
  32. buff[len] = 0;
  33. rt_kprintf("message:%s len:%d\r\n", buff, len);
  34. rt_device_write(rpmsg, rpmsg_remote_echo.dst, buff, len);
  35. }
  36. }
  37. int rpmsg_echo()
  38. {
  39. uint32_t ret = RT_EOK;
  40. rpmsg = rt_device_find("rpmsg");
  41. if (rpmsg == RT_NULL)
  42. {
  43. rt_kprintf("Unable to find rpmsg device.\r\n");
  44. return RT_ERROR;
  45. }
  46. rt_device_open(rpmsg, RT_DEVICE_OFLAG_OPEN);
  47. rt_device_control(rpmsg, RT_DEVICE_CTRL_CONFIG, &rpmsg_remote_echo);
  48. static rt_thread_t thread = RT_NULL;
  49. thread = rt_thread_create("rpmsg_echo",
  50. rpmsg_echo_thread, RT_NULL,
  51. THREAD_STACK_SIZE,
  52. THREAD_PRIORITY, THREAD_TIMESLICE);
  53. if (thread != RT_NULL)
  54. rt_thread_startup(thread);
  55. if(ret != RT_EOK)
  56. {
  57. return RT_ERROR;
  58. }
  59. rt_kprintf("rpmsg echo init end.\r\n");
  60. return RT_EOK;
  61. }
  62. #ifdef RT_USING_FINSH
  63. #include <finsh.h>
  64. FINSH_FUNCTION_EXPORT(rpmsg_echo, startup rpmsg_echo server);
  65. #ifdef FINSH_USING_MSH
  66. MSH_CMD_EXPORT(rpmsg_echo, startup rpmsg_echo server)
  67. #endif /* FINSH_USING_MSH */
  68. #endif /* RT_USING_FINSH */