bsp_shm.c 7.0 KB

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