bsp_shm.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /******************************************************************************
  2. 版权所有: @copyright (C) 2024-2034 HaiYang Technology Corp. All rights reserved.
  3. 文件名称: shm.c
  4. 文件版本: 01.01
  5. 创建作者: zhaoyang
  6. 创建日期: 2025-08-21
  7. 功能说明: 共享内存驱动
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "bsp_shm.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/mman.h>
  20. #include <pthread.h>
  21. #include <stdbool.h>
  22. #include "bsp_share.h"
  23. uint8_t *SHM_BASE_R = NULL; // 共享内存---读(从裸核读取数据)---开始地址
  24. uint8_t *SHM_BASE_W = NULL; // 共享内存---写(向裸核写入数据)---开始地址
  25. struct test_data_s
  26. {
  27. uint16_t A; // 标志A,写buf和crc前,A++
  28. uint8_t buf[1000];
  29. uint16_t crc; // buf的crc
  30. uint16_t B; // 标志B,写buf和crc后,B++,且A=B
  31. };
  32. struct dev_co
  33. {
  34. char *name;
  35. pthread_t thread_id;
  36. // int fd_co; //mailbox 暂时去掉
  37. int fd_shm_w;
  38. int fd_shm_r;
  39. uint8_t *shm_w;
  40. uint8_t *shm_r;
  41. // struct co_io coio; //mailbox 暂时去掉
  42. };
  43. struct dev_co *pco0;
  44. static inline void dmsg_u8(uint8_t *data, int length, char *str)
  45. {
  46. int i, j;
  47. int tmp;
  48. printf("%s:\n", str);
  49. for (i = 0; i < (length / 16); i++)
  50. {
  51. for (j = 0; j < 16; j++)
  52. printf("%02x ", data[i * 16 + j]);
  53. printf("\n");
  54. }
  55. tmp = (length / 16) * 16;
  56. for (i = 0; i < (length % 16); i++)
  57. printf("%02x ", data[tmp + i]);
  58. if (length % 16)
  59. printf("\n");
  60. }
  61. /******************************************************************************
  62. 函数名称: shm_read
  63. 函数版本: 01.01
  64. 创建作者: zhaoyang
  65. 创建日期: 2025-08-21
  66. 函数说明: 读取共享内存的数据.
  67. 参数说明:
  68. addr:所以读取共享内存的地址
  69. len:需要读取数据的长度
  70. out_data:输出buf
  71. out_data_size:输出buf的长度
  72. 返回值: 成功返回读取数据的长度, 失败返回-1
  73. 修改记录:
  74. */
  75. int shm_read(uint32_t addr, uint32_t len, uint8_t *out_data, uint32_t out_data_size)
  76. {
  77. uint32_t i = 0;
  78. if (SHM_BASE_R == NULL)
  79. return -1;
  80. if (out_data == NULL)
  81. return -1;
  82. if ((len == 0) || (out_data_size == 0))
  83. return -1;
  84. for (i = 0; i < len; i++)
  85. {
  86. if ((addr + i) >= SHM_MAX_SIZE)
  87. return -1;
  88. if (i >= out_data_size)
  89. return -1;
  90. out_data[i] = SHM_BASE_R[addr + i];
  91. }
  92. return i;
  93. }
  94. /******************************************************************************
  95. 函数名称: shm_write
  96. 函数版本: 01.01
  97. 创建作者: zhaoyang
  98. 创建日期: 2025-08-21
  99. 函数说明: 向共享内存写入数据.
  100. 参数说明:
  101. addr:共享内存的地址
  102. data:写入数据
  103. len:写入数据长度
  104. 返回值: 成功返回写入数据的长度, 失败返回-1
  105. 修改记录:
  106. */
  107. int shm_write(uint32_t addr, uint8_t *data, uint32_t len)
  108. {
  109. uint32_t i = 0;
  110. if (SHM_BASE_W == NULL)
  111. return -1;
  112. if (data == NULL)
  113. return -1;
  114. if (len == 0)
  115. return -1;
  116. for (i = 0; i < len; i++)
  117. {
  118. if ((addr + i) >= SHM_MAX_SIZE)
  119. return -1;
  120. SHM_BASE_W[addr + i] = data[i];
  121. }
  122. return i;
  123. }
  124. static void *thread_co_test(void *arg)
  125. {
  126. // struct dev_co *co = (struct dev_co *)arg;
  127. // uint8_t *shm0 = co->shm_w;
  128. // uint8_t *shm1 = co->shm_r;
  129. int i;
  130. int state = 0;
  131. // int ret;
  132. // uint16_t crc = 0;
  133. // int crc_err_cnt = 0;
  134. // int cnt_alive = 0;
  135. // int cnt_busy = 0;
  136. // int cnt_crcerr = 0;
  137. // int cnt_good = 0;
  138. uint8_t test_buf_r[2000];
  139. // uint8_t test_buf_r_record[2000];
  140. uint8_t test_buf_w[2000];
  141. int nr = 0;
  142. // uint8_t lastcode = 0;
  143. // int cnt = 0;
  144. // int last_A = 0;
  145. // int last_B = 0;
  146. // struct test_data_s *p_rec_buf = (struct test_data_s*)test_buf_r;
  147. // uint16_t temval = 0;
  148. memset(test_buf_r, 0, 2000);
  149. memset(test_buf_w, 0, 2000);
  150. state = 1;
  151. while (1)
  152. {
  153. sleep(5);
  154. nr++;
  155. // 读 分两种接口
  156. dmsg_u8(SHM_BASE_R, 10, "read:");
  157. shm_read(10, 10, test_buf_r, 2000);
  158. dmsg_u8(test_buf_r, 10, "shm_read:");
  159. // 写 分两种接口
  160. for (i = 0; i < 10; i++)
  161. SHM_BASE_W[i] = nr + i;
  162. for (i = 0; i < 10; i++)
  163. test_buf_w[i] = nr + i + 10;
  164. shm_write(10, test_buf_w, 10);
  165. #if 0
  166. if (state == 1) {
  167. last_A = shm1[0]+((uint16_t)shm1[1]<<8);
  168. last_B = shm1[sizeof(struct test_data_s)-2]+((uint16_t)shm1[sizeof(struct test_data_s)-1]<<8);
  169. if(last_A == last_B)
  170. {
  171. //idle
  172. memcpy(&test_buf_r,shm1,sizeof(struct test_data_s));
  173. if((p_rec_buf->A == p_rec_buf->B) && (p_rec_buf->A == last_A) && (last_A == shm1[0]+((uint16_t)shm1[1]<<8))
  174. && (last_B == shm1[sizeof(struct test_data_s)-2]+((uint16_t)shm1[sizeof(struct test_data_s)-1]<<8)))
  175. {
  176. //在拷贝过程中,数据没有被修改
  177. crc = CrcStr(p_rec_buf->buf,sizeof(p_rec_buf->buf));
  178. if(crc != p_rec_buf->crc)
  179. {
  180. #if 0
  181. printf("crc err!!! cal crc=%04X, read crc=%04X\n", crc,p_rec_buf->crc);
  182. dmsg_u8(test_buf_r, sizeof(struct test_data_s), "test_buf_r");
  183. dmsg_u8(shm1, sizeof(struct test_data_s), "shm1");
  184. printf("p_rec_buf->A=%d, last_A=%d\n", p_rec_buf->A,last_A);
  185. #endif
  186. cnt_crcerr++;
  187. }
  188. else
  189. {
  190. cnt_good++;
  191. }
  192. }
  193. else
  194. {
  195. //busy 裸核正在写数据
  196. cnt_busy++;
  197. }
  198. }
  199. else
  200. {
  201. //busy 裸核正在写数据
  202. cnt_busy++;
  203. }
  204. if(cnt_alive++ > 100000)
  205. {
  206. cnt_alive = 0;
  207. printf("cnt_busy=%d, cnt_crcerr=%d, cnt_good=%d\n", cnt_busy,cnt_crcerr,cnt_good);
  208. printf("cal crc=%04X, read crc=%04X\n", crc,p_rec_buf->crc);
  209. }
  210. usleep(10);
  211. }
  212. #endif
  213. }
  214. printf("thread co exit\r\n");
  215. pthread_exit(NULL);
  216. }
  217. // 测试代码
  218. void shm_test(void)
  219. {
  220. int ret;
  221. if ((ret = pthread_create(&pco0->thread_id, NULL, thread_co_test, pco0)) == 0)
  222. printf("thread co0 create success\r\n");
  223. else
  224. printf("thread co0 create fail ret=%d\r\n", ret);
  225. }
  226. /******************************************************************************
  227. 函数名称: shm_init
  228. 函数版本: 01.01
  229. 创建作者: zhaoyang
  230. 创建日期: 2025-08-21
  231. 函数说明: shm初始化.
  232. 参数说明:
  233. 返回值: 成功返回0, 失败返回-1
  234. 修改记录:
  235. */
  236. int shm_init(void)
  237. {
  238. // int ret;
  239. int i;
  240. unsigned int *tmp_buf;
  241. SHM_BASE_R = NULL;
  242. SHM_BASE_W = NULL;
  243. pco0 = malloc(sizeof(struct dev_co));
  244. memset(pco0, 0, sizeof(struct dev_co));
  245. if (shmem_init(&shmem_fd))
  246. {
  247. printf("shmem_init failed!\n");
  248. return -1;
  249. }
  250. tmp_buf = mmap(NULL, shmem_fd.linux_read_len, PROT_READ | PROT_WRITE,
  251. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  252. if (tmp_buf <= 0)
  253. {
  254. printf("mmap failed!\n");
  255. return -1;
  256. }
  257. printf("tmp_buf:%lx\n", (unsigned long)tmp_buf);
  258. SHM_BASE_R = shmem_fd.linux_read->data; // 共享内存---读(从裸核读取数据)---开始地址
  259. SHM_BASE_W = shmem_fd.linux_write->data; // 共享内存---写(向裸核写入数据)---开始地址
  260. // 初始化shm区
  261. // 暂时初始化参数区
  262. for (i = 0; i < SHM_ADDR_U_ADC_1; i++)
  263. {
  264. SHM_BASE_R[i] = 0;
  265. SHM_BASE_W[i] = 0;
  266. }
  267. // shm_test();//zhaoyang for test
  268. return 0;
  269. }
  270. /******************************************************************************
  271. 函数名称: shm_exit
  272. 函数版本: 01.01
  273. 创建作者: zhaoyang
  274. 创建日期: 2025-08-21
  275. 函数说明: shm反初始化.
  276. 参数说明: 无
  277. 返回值: 总是返回0.
  278. 修改记录:
  279. */
  280. int shm_exit(void)
  281. {
  282. // pthread_join(pco0->thread_id, NULL);
  283. return 0;
  284. }