printf_e907.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /******************************************************************************
  2. 版权所有: 文件名称: printf_e907.c
  3. 文件版本: 01.01
  4. 创建作者: sunxi
  5. 创建日期: 2022-5-11
  6. 功能说明: 打印裸核e907的信息
  7. 其它说明:
  8. 修改记录:
  9. */
  10. #include "bspconfig.h"
  11. #include "bsp.h"
  12. #include "shm_comm_packet.h"
  13. #include <linux/sched.h>
  14. #include <sys/prctl.h>
  15. #define P_BUF_LEN_MAX 4096 // buf总长度
  16. // 打印数据结构体
  17. typedef struct SHM_PRINTF_T
  18. {
  19. uint16_t A;
  20. uint16_t B;
  21. uint16_t w;
  22. uint16_t r;
  23. uint16_t crc;
  24. } SHM_PRINTF_T;
  25. static uint8_t p_buf[P_BUF_LEN_MAX];
  26. static uint16_t w_p = 0; // 写指针
  27. static uint16_t r_p = 0; // 读指针
  28. #if 0
  29. static char test_1[] = "printf_e907_func----------------------\r\n";
  30. static char test_2[] = "printf_e907_func----------------------123\r\n";
  31. static char test_3[] = "printf_e907_func----------------------456\r\n";
  32. #endif
  33. static pthread_t printf_e907_tid = 0;
  34. static int printf_e907_func(void *arg);
  35. static int exit_flag = 0;
  36. extern int main_mod_is_exit(void);
  37. static int get_printf_wr(uint8_t *out_data)
  38. {
  39. int ret = 0;
  40. int cnt = 0;
  41. if (out_data == NULL)
  42. return -1;
  43. while (1)
  44. {
  45. ret = shm_comm_packet_read(SHM_ADDR_U_WR, sizeof(SHM_PRINTF_T), out_data, sizeof(SHM_PRINTF_T));
  46. if (ret > 0)
  47. {
  48. break;
  49. }
  50. if (++cnt > 3)
  51. break;
  52. usleep(30);
  53. }
  54. return ret;
  55. }
  56. /******************************************************************************
  57. 函数名称: printf_e907_func
  58. 函数版本: 01.01
  59. 创建作者: sunxi
  60. 创建日期: 2022-5-11
  61. 函数说明: 打印工作函数.
  62. 参数说明:
  63. 返回值: 返回0
  64. 修改记录:
  65. */
  66. static int printf_e907_func(void *arg)
  67. {
  68. SHM_PRINTF_T wr;
  69. int ret = 0;
  70. int len = 0;
  71. int len_1 = 0;
  72. memset(p_buf, 0, sizeof(p_buf));
  73. w_p = 0;
  74. r_p = 0;
  75. #if 0
  76. strcpy(&p_buf[w_p],test_1);
  77. w_p += strlen(test_1);
  78. strcpy(&p_buf[w_p],test_2);
  79. w_p += strlen(test_2);
  80. strcpy(&p_buf[w_p],test_3);
  81. w_p += strlen(test_3);
  82. rt_printf(p_buf);
  83. #endif
  84. uint32_t wdt_id;
  85. int rc = watchdog_add_item("printf_e907_func", &wdt_id, 120);
  86. if (rc != 0)
  87. {
  88. printf("can not add printf_e907_func task to wdt\r\n");
  89. return 0;
  90. }
  91. prctl(PR_SET_NAME, "printf_e907");
  92. while (!exit_flag)
  93. {
  94. msleep(10);
  95. watchdog_feed(wdt_id);
  96. if (main_mod_is_exit())
  97. {
  98. break;
  99. }
  100. w_p = 0;
  101. // 获取w,r
  102. memset(&wr, 0, sizeof(wr));
  103. ret = get_printf_wr((uint8_t *)&wr);
  104. if (ret >= 0)
  105. {
  106. w_p = wr.w;
  107. }
  108. // rt_printf("w_p = %d, r_p = %d\r\n",w_p,r_p);
  109. if (w_p != r_p)
  110. {
  111. memset(p_buf, 0, sizeof(p_buf));
  112. // 获取打印信息
  113. if (w_p > r_p)
  114. {
  115. len = w_p - r_p;
  116. ret = shm_read(SHM_ADDR_U_DEBUG_MSG + r_p, len, p_buf, sizeof(p_buf));
  117. if (ret > 0)
  118. r_p += ret;
  119. }
  120. else
  121. {
  122. // w_p指针翻滚,过0
  123. // 分两段读取,再合并
  124. len = P_BUF_LEN_MAX - r_p;
  125. ret = shm_read(SHM_ADDR_U_DEBUG_MSG + r_p, len, p_buf, sizeof(p_buf));
  126. if (ret > 0)
  127. r_p += ret;
  128. len_1 = w_p;
  129. ret = shm_read(SHM_ADDR_U_DEBUG_MSG, len_1, &p_buf[len], sizeof(p_buf) - len);
  130. if (ret > 0)
  131. r_p = ret;
  132. }
  133. if (r_p >= P_BUF_LEN_MAX)
  134. r_p = 0;
  135. p_buf[sizeof(p_buf) - 1] = 0; // 保证结束
  136. if (ret > 0)
  137. {
  138. rt_printf(p_buf);
  139. rt_printf("\r\n");
  140. }
  141. }
  142. }
  143. watchdog_remove_item(wdt_id);
  144. return 0;
  145. }
  146. /******************************************************************************
  147. 函数名称: printf_e907_init
  148. 函数版本: 01.01
  149. 创建作者: sunxi
  150. 创建日期: 2022-5-11
  151. 函数说明: 初始化.
  152. 参数说明:
  153. 无
  154. 返回值: 成功返回0, 失败返回-1
  155. 修改记录:
  156. */
  157. int printf_e907_init(void)
  158. {
  159. int ret;
  160. ret = pthread_create(&printf_e907_tid, NULL, (void *)printf_e907_func, NULL);
  161. if (ret)
  162. {
  163. rt_printf("ret = %d, err = %s\r\n", ret, strerror(ret));
  164. printf_e907_tid = 0;
  165. return ret;
  166. }
  167. return 0;
  168. }
  169. /******************************************************************************
  170. 函数名称: printf_e907_exit
  171. 函数版本: 01.01
  172. 创建作者: sunxi
  173. 创建日期: 2022-5-11
  174. 函数说明: 反初始化.
  175. 参数说明:
  176. 无
  177. 返回值: 总是返回0.
  178. 修改记录:
  179. */
  180. int printf_e907_exit(void)
  181. {
  182. exit_flag = 1;
  183. pthread_join(printf_e907_tid, NULL);
  184. return 0;
  185. }