shm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /******************************************************************************
  2. 版权所有: 文件名称: shm.c
  3. 文件名称: shm.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2022-04-21
  7. 功能说明: 共享内存驱动
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "bspconfig.h"
  12. #include "bsp.h"
  13. #include "shm.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/mman.h>
  22. #include <pthread.h>
  23. #include <stdbool.h>
  24. uint8_t *SHM_BASE_R = NULL; // 共享内存---读(从裸核读取数据)---开始地址
  25. uint8_t *SHM_BASE_W = NULL; // 共享内存---写(向裸核写入数据)---开始地址
  26. struct test_data_s
  27. {
  28. uint16_t A; // 标志A,写buf和crc前,A++
  29. uint8_t buf[1000];
  30. uint16_t crc; // buf的crc
  31. uint16_t B; // 标志B,写buf和crc后,B++,且A=B
  32. };
  33. struct dev_co
  34. {
  35. char *name;
  36. pthread_t thread_id;
  37. // int fd_co; //mailbox 暂时去掉
  38. int fd_shm_w;
  39. int fd_shm_r;
  40. uint8_t *shm_w;
  41. uint8_t *shm_r;
  42. // struct co_io coio; //mailbox 暂时去掉
  43. };
  44. struct dev_co *pco0;
  45. static inline void dmsg_u8(uint8_t *data, int length, char *str)
  46. {
  47. int i, j;
  48. int tmp;
  49. printf("%s:\n", str);
  50. for (i = 0; i < (length / 16); i++)
  51. {
  52. for (j = 0; j < 16; j++)
  53. printf("%02x ", data[i * 16 + j]);
  54. printf("\n");
  55. }
  56. tmp = (length / 16) * 16;
  57. for (i = 0; i < (length % 16); i++)
  58. printf("%02x ", data[tmp + i]);
  59. if (length % 16)
  60. printf("\n");
  61. }
  62. /******************************************************************************
  63. 函数名称: shm_read
  64. 函数版本: 01.01
  65. 创建作者: sunxi
  66. 创建日期: 2022-04-21
  67. 函数说明: 读取共享内存的数据.
  68. 参数说明:
  69. addr:所以读取共享内存的地址
  70. len:需要读取数据的长度
  71. out_data:输出buf
  72. out_data_size:输出buf的长度
  73. 返回值: 成功返回读取数据的长度, 失败返回-1
  74. 修改记录:
  75. */
  76. int shm_read(uint32_t addr, uint32_t len, uint8_t *out_data, uint32_t out_data_size)
  77. {
  78. uint32_t i = 0;
  79. if (SHM_BASE_R == NULL)
  80. return -1;
  81. if (out_data == NULL)
  82. return -1;
  83. if ((len == 0) || (out_data_size == 0))
  84. return -1;
  85. for (i = 0; i < len; i++)
  86. {
  87. if ((addr + i) >= SHM_MAX_SIZE)
  88. return -1;
  89. if (i >= out_data_size)
  90. return -1;
  91. out_data[i] = SHM_BASE_R[addr + i];
  92. }
  93. return i;
  94. }
  95. /******************************************************************************
  96. 函数名称: shm_write
  97. 函数版本: 01.01
  98. 创建作者: sunxi
  99. 创建日期: 2022-04-21
  100. 函数说明: 向共享内存写入数据.
  101. 参数说明:
  102. addr:共享内存的地址
  103. data:写入数据
  104. len:写入数据长度
  105. 返回值: 成功返回写入数据的长度, 失败返回-1
  106. 修改记录:
  107. */
  108. int shm_write(uint32_t addr, uint8_t *data, uint32_t len)
  109. {
  110. uint32_t i = 0;
  111. if (SHM_BASE_W == NULL)
  112. return -1;
  113. if (data == NULL)
  114. return -1;
  115. if (len == 0)
  116. return -1;
  117. for (i = 0; i < len; i++)
  118. {
  119. if ((addr + i) >= SHM_MAX_SIZE)
  120. return -1;
  121. SHM_BASE_W[addr + i] = data[i];
  122. }
  123. return i;
  124. }
  125. static int co_open(struct dev_co *co, char *name, int blk)
  126. {
  127. // int fd_co;
  128. int fd_shm0;
  129. int fd_shm1;
  130. uint8_t *shm0 = NULL;
  131. uint8_t *shm1 = NULL;
  132. #ifdef CO_DEBUG
  133. printf("open co dev %s blk=%d\n", name, blk);
  134. #endif
  135. if (0 == blk)
  136. {
  137. fd_shm0 = open("/dev/arv-shmem", O_RDWR);
  138. if (!fd_shm0)
  139. {
  140. printf("open arv-shmem error\n");
  141. return -1;
  142. }
  143. fd_shm1 = open("/dev/arv-shmem", O_RDWR);
  144. if (!fd_shm1)
  145. {
  146. printf("open arv-shmem error\n");
  147. return -1;
  148. }
  149. shm0 = mmap(NULL, SHM_MAX_SIZE,
  150. PROT_READ | PROT_WRITE,
  151. MAP_FILE | MAP_SHARED,
  152. fd_shm0, 0);
  153. if (shm0 == MAP_FAILED)
  154. {
  155. printf("shm mmap error\n");
  156. return -1;
  157. }
  158. shm1 = mmap(NULL, SHM_MAX_SIZE,
  159. PROT_READ | PROT_WRITE,
  160. MAP_FILE | MAP_SHARED,
  161. fd_shm1, 0);
  162. if (shm1 == MAP_FAILED)
  163. {
  164. printf("shm mmap error\n");
  165. return -1;
  166. }
  167. }
  168. else if (1 == blk)
  169. {
  170. fd_shm0 = open("/dev/sc-shm2", O_RDWR);
  171. if (!fd_shm0)
  172. {
  173. printf("open sc-shm2 error\n");
  174. return -1;
  175. }
  176. fd_shm1 = open("/dev/sc-shm3", O_RDWR);
  177. if (!fd_shm1)
  178. {
  179. printf("open sc-shm3 error\n");
  180. return -1;
  181. }
  182. shm0 = mmap(NULL, SHM_MAX_SIZE,
  183. PROT_READ | PROT_WRITE,
  184. MAP_FILE | MAP_SHARED,
  185. fd_shm0, 0);
  186. if (shm0 == MAP_FAILED)
  187. {
  188. printf("shm mmap error\n");
  189. return -1;
  190. }
  191. shm1 = mmap(NULL, SHM_MAX_SIZE,
  192. PROT_READ | PROT_WRITE,
  193. MAP_FILE | MAP_SHARED,
  194. fd_shm1, 0);
  195. if (shm1 == MAP_FAILED)
  196. {
  197. printf("shm mmap error\n");
  198. return -1;
  199. }
  200. }
  201. else
  202. {
  203. printf("donot support this shm blk\n");
  204. return -1;
  205. }
  206. /*
  207. fd_co = open(name, O_RDWR);
  208. if (!fd_co) {
  209. printf("open co dev failed\n");
  210. return -1;
  211. }
  212. */
  213. co->name = name;
  214. // co->fd_co = fd_co;
  215. co->fd_shm_w = fd_shm0;
  216. co->fd_shm_r = fd_shm1;
  217. co->shm_w = shm0;
  218. co->shm_r = shm1;
  219. return 0;
  220. }
  221. static void *thread_co_test(void *arg)
  222. {
  223. // struct dev_co *co = (struct dev_co *)arg;
  224. // uint8_t *shm0 = co->shm_w;
  225. // uint8_t *shm1 = co->shm_r;
  226. int i;
  227. int state = 0;
  228. // int ret;
  229. // uint16_t crc = 0;
  230. // int crc_err_cnt = 0;
  231. // int cnt_alive = 0;
  232. // int cnt_busy = 0;
  233. // int cnt_crcerr = 0;
  234. // int cnt_good = 0;
  235. uint8_t test_buf_r[2000];
  236. // uint8_t test_buf_r_record[2000];
  237. uint8_t test_buf_w[2000];
  238. int nr = 0;
  239. // uint8_t lastcode = 0;
  240. // int cnt = 0;
  241. // int last_A = 0;
  242. // int last_B = 0;
  243. // struct test_data_s *p_rec_buf = (struct test_data_s*)test_buf_r;
  244. // uint16_t temval = 0;
  245. memset(test_buf_r, 0, 2000);
  246. memset(test_buf_w, 0, 2000);
  247. state = 1;
  248. while (1)
  249. {
  250. sleep(5);
  251. nr++;
  252. // 读 分两种接口
  253. dmsg_u8(SHM_BASE_R, 10, "read:");
  254. shm_read(10, 10, test_buf_r, 2000);
  255. dmsg_u8(test_buf_r, 10, "shm_read:");
  256. // 写 分两种接口
  257. for (i = 0; i < 10; i++)
  258. SHM_BASE_W[i] = nr + i;
  259. for (i = 0; i < 10; i++)
  260. test_buf_w[i] = nr + i + 10;
  261. shm_write(10, test_buf_w, 10);
  262. #if 0
  263. if (state == 1) {
  264. last_A = shm1[0]+((uint16_t)shm1[1]<<8);
  265. last_B = shm1[sizeof(struct test_data_s)-2]+((uint16_t)shm1[sizeof(struct test_data_s)-1]<<8);
  266. if(last_A == last_B)
  267. {
  268. //idle
  269. memcpy(&test_buf_r,shm1,sizeof(struct test_data_s));
  270. if((p_rec_buf->A == p_rec_buf->B) && (p_rec_buf->A == last_A) && (last_A == shm1[0]+((uint16_t)shm1[1]<<8))
  271. && (last_B == shm1[sizeof(struct test_data_s)-2]+((uint16_t)shm1[sizeof(struct test_data_s)-1]<<8)))
  272. {
  273. //在拷贝过程中,数据没有被修改
  274. crc = CrcStr(p_rec_buf->buf,sizeof(p_rec_buf->buf));
  275. if(crc != p_rec_buf->crc)
  276. {
  277. #if 0
  278. printf("crc err!!! cal crc=%04X, read crc=%04X\n", crc,p_rec_buf->crc);
  279. dmsg_u8(test_buf_r, sizeof(struct test_data_s), "test_buf_r");
  280. dmsg_u8(shm1, sizeof(struct test_data_s), "shm1");
  281. printf("p_rec_buf->A=%d, last_A=%d\n", p_rec_buf->A,last_A);
  282. #endif
  283. cnt_crcerr++;
  284. }
  285. else
  286. {
  287. cnt_good++;
  288. }
  289. }
  290. else
  291. {
  292. //busy 裸核正在写数据
  293. cnt_busy++;
  294. }
  295. }
  296. else
  297. {
  298. //busy 裸核正在写数据
  299. cnt_busy++;
  300. }
  301. if(cnt_alive++ > 100000)
  302. {
  303. cnt_alive = 0;
  304. printf("cnt_busy=%d, cnt_crcerr=%d, cnt_good=%d\n", cnt_busy,cnt_crcerr,cnt_good);
  305. printf("cal crc=%04X, read crc=%04X\n", crc,p_rec_buf->crc);
  306. }
  307. usleep(10);
  308. }
  309. #endif
  310. }
  311. printf("thread co exit\r\n");
  312. pthread_exit(NULL);
  313. }
  314. static int co_close(struct dev_co *co)
  315. {
  316. // if(co->fd_co)
  317. // close(co->fd_co);
  318. if (co->fd_shm_w)
  319. close(co->fd_shm_w);
  320. if (co->fd_shm_r)
  321. close(co->fd_shm_r);
  322. return 0;
  323. }
  324. // 测试代码
  325. void shm_test(void)
  326. {
  327. int ret;
  328. if ((ret = pthread_create(&pco0->thread_id, NULL, thread_co_test, pco0)) == 0)
  329. printf("thread co0 create success\r\n");
  330. else
  331. printf("thread co0 create fail ret=%d\r\n", ret);
  332. }
  333. /******************************************************************************
  334. 函数名称: shm_init
  335. 函数版本: 01.01
  336. 创建作者: sunxi
  337. 创建日期: 2022-04-21
  338. 函数说明: shm初始化.
  339. 参数说明:
  340. 返回值: 成功返回0, 失败返回-1
  341. 修改记录:
  342. */
  343. int shm_init(void)
  344. {
  345. // int ret;
  346. int i;
  347. SHM_BASE_R = NULL;
  348. SHM_BASE_W = NULL;
  349. pco0 = malloc(sizeof(struct dev_co));
  350. memset(pco0, 0, sizeof(struct dev_co));
  351. if (co_open(pco0, "/dev/arv-shmem", 0))
  352. {
  353. printf("open shm blk 0 error\r\n");
  354. return -1;
  355. }
  356. SHM_BASE_R = pco0->shm_r; // 共享内存---读(从裸核读取数据)---开始地址
  357. SHM_BASE_W = pco0->shm_w; // 共享内存---写(向裸核写入数据)---开始地址
  358. // 初始化shm区
  359. // 暂时初始化参数区
  360. for (i = 0; i < SHM_ADDR_U_ADC_1; i++)
  361. {
  362. SHM_BASE_R[i] = 0;
  363. SHM_BASE_W[i] = i;
  364. }
  365. // shm_test();//sunxi for test
  366. return 0;
  367. }
  368. /******************************************************************************
  369. 函数名称: shm_exit
  370. 函数版本: 01.01
  371. 创建作者: sunxi
  372. 创建日期: 2022-04-21
  373. 函数说明: shm反初始化.
  374. 参数说明:
  375. 返回值: 总是返回0.
  376. 修改记录:
  377. */
  378. int shm_exit(void)
  379. {
  380. // pthread_join(pco0->thread_id, NULL);
  381. co_close(pco0);
  382. return 0;
  383. }