shm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. 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. 创建作者: sunxi
  65. 创建日期: 2022-04-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. 创建作者: sunxi
  98. 创建日期: 2022-04-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 int co_open(struct dev_co *co, char *name, int blk)
  125. {
  126. //int fd_co;
  127. int fd_shm0;
  128. int fd_shm1;
  129. uint8_t *shm0 = NULL;
  130. uint8_t *shm1 = NULL;
  131. printf("open co dev %s blk=%d\n", name, blk);
  132. if(0 == blk) {
  133. fd_shm0 = open("/dev/arv-shmem", O_RDWR);
  134. if (!fd_shm0) {
  135. printf("open arv-shmem error\n");
  136. return -1;
  137. }
  138. fd_shm1 = open("/dev/arv-shmem", O_RDWR);
  139. if (!fd_shm1) {
  140. printf("open arv-shmem error\n");
  141. return -1;
  142. }
  143. shm0 = mmap(NULL, SHM_MAX_SIZE,
  144. PROT_READ | PROT_WRITE,
  145. MAP_FILE | MAP_SHARED,
  146. fd_shm0, 0);
  147. if (shm0 == MAP_FAILED) {
  148. printf("shm mmap error\n");
  149. return -1;
  150. }
  151. shm1 = mmap(NULL, SHM_MAX_SIZE,
  152. PROT_READ | PROT_WRITE,
  153. MAP_FILE | MAP_SHARED,
  154. fd_shm1, 0);
  155. if (shm1 == MAP_FAILED) {
  156. printf("shm mmap error\n");
  157. return -1;
  158. }
  159. } else if(1 == blk) {
  160. fd_shm0 = open("/dev/sc-shm2", O_RDWR);
  161. if (!fd_shm0) {
  162. printf("open sc-shm2 error\n");
  163. return -1;
  164. }
  165. fd_shm1 = open("/dev/sc-shm3", O_RDWR);
  166. if (!fd_shm1) {
  167. printf("open sc-shm3 error\n");
  168. return -1;
  169. }
  170. shm0 = mmap(NULL, SHM_MAX_SIZE,
  171. PROT_READ | PROT_WRITE,
  172. MAP_FILE | MAP_SHARED,
  173. fd_shm0, 0);
  174. if (shm0 == MAP_FAILED) {
  175. printf("shm mmap error\n");
  176. return -1;
  177. }
  178. shm1 = mmap(NULL, SHM_MAX_SIZE,
  179. PROT_READ | PROT_WRITE,
  180. MAP_FILE | MAP_SHARED,
  181. fd_shm1, 0);
  182. if (shm1 == MAP_FAILED) {
  183. printf("shm mmap error\n");
  184. return -1;
  185. }
  186. } else {
  187. printf("donot support this shm blk\n");
  188. return -1;
  189. }
  190. /*
  191. fd_co = open(name, O_RDWR);
  192. if (!fd_co) {
  193. printf("open co dev failed\n");
  194. return -1;
  195. }
  196. */
  197. co->name = name;
  198. //co->fd_co = fd_co;
  199. co->fd_shm_w = fd_shm0;
  200. co->fd_shm_r = fd_shm1;
  201. co->shm_w = shm0;
  202. co->shm_r = shm1;
  203. return 0;
  204. }
  205. static void *thread_co_test(void *arg)
  206. {
  207. // struct dev_co *co = (struct dev_co *)arg;
  208. // uint8_t *shm0 = co->shm_w;
  209. // uint8_t *shm1 = co->shm_r;
  210. int i;
  211. int state = 0;
  212. // int ret;
  213. // uint16_t crc = 0;
  214. // int crc_err_cnt = 0;
  215. // int cnt_alive = 0;
  216. // int cnt_busy = 0;
  217. // int cnt_crcerr = 0;
  218. // int cnt_good = 0;
  219. uint8_t test_buf_r[2000];
  220. // uint8_t test_buf_r_record[2000];
  221. uint8_t test_buf_w[2000];
  222. int nr = 0;
  223. // uint8_t lastcode = 0;
  224. // int cnt = 0;
  225. // int last_A = 0;
  226. // int last_B = 0;
  227. // struct test_data_s *p_rec_buf = (struct test_data_s*)test_buf_r;
  228. // uint16_t temval = 0;
  229. memset(test_buf_r, 0, 2000);
  230. memset(test_buf_w, 0, 2000);
  231. state = 1;
  232. while (1) {
  233. sleep(5);
  234. nr++;
  235. //读 分两种接口
  236. dmsg_u8(SHM_BASE_R, 10, "read:");
  237. shm_read(10, 10, test_buf_r, 2000);
  238. dmsg_u8(test_buf_r, 10, "shm_read:");
  239. //写 分两种接口
  240. for(i = 0; i< 10; i++)
  241. SHM_BASE_W[i] = nr+i;
  242. for(i = 0; i< 10; i++)
  243. test_buf_w[i] = nr+i+10;
  244. shm_write(10, test_buf_w, 10);
  245. #if 0
  246. if (state == 1) {
  247. last_A = shm1[0]+((uint16_t)shm1[1]<<8);
  248. last_B = shm1[sizeof(struct test_data_s)-2]+((uint16_t)shm1[sizeof(struct test_data_s)-1]<<8);
  249. if(last_A == last_B)
  250. {
  251. //idle
  252. memcpy(&test_buf_r,shm1,sizeof(struct test_data_s));
  253. if((p_rec_buf->A == p_rec_buf->B) && (p_rec_buf->A == last_A) && (last_A == shm1[0]+((uint16_t)shm1[1]<<8))
  254. && (last_B == shm1[sizeof(struct test_data_s)-2]+((uint16_t)shm1[sizeof(struct test_data_s)-1]<<8)))
  255. {
  256. //在拷贝过程中,数据没有被修改
  257. crc = CrcStr(p_rec_buf->buf,sizeof(p_rec_buf->buf));
  258. if(crc != p_rec_buf->crc)
  259. {
  260. #if 0
  261. printf("crc err!!! cal crc=%04X, read crc=%04X\n", crc,p_rec_buf->crc);
  262. dmsg_u8(test_buf_r, sizeof(struct test_data_s), "test_buf_r");
  263. dmsg_u8(shm1, sizeof(struct test_data_s), "shm1");
  264. printf("p_rec_buf->A=%d, last_A=%d\n", p_rec_buf->A,last_A);
  265. #endif
  266. cnt_crcerr++;
  267. }
  268. else
  269. {
  270. cnt_good++;
  271. }
  272. }
  273. else
  274. {
  275. //busy 裸核正在写数据
  276. cnt_busy++;
  277. }
  278. }
  279. else
  280. {
  281. //busy 裸核正在写数据
  282. cnt_busy++;
  283. }
  284. if(cnt_alive++ > 100000)
  285. {
  286. cnt_alive = 0;
  287. printf("cnt_busy=%d, cnt_crcerr=%d, cnt_good=%d\n", cnt_busy,cnt_crcerr,cnt_good);
  288. printf("cal crc=%04X, read crc=%04X\n", crc,p_rec_buf->crc);
  289. }
  290. usleep(10);
  291. }
  292. #endif
  293. }
  294. printf("thread co exit\r\n");
  295. pthread_exit(NULL);
  296. }
  297. static int co_close(struct dev_co *co)
  298. {
  299. //if(co->fd_co)
  300. // close(co->fd_co);
  301. if(co->fd_shm_w)
  302. close(co->fd_shm_w);
  303. if(co->fd_shm_r)
  304. close(co->fd_shm_r);
  305. return 0;
  306. }
  307. //测试代码
  308. void shm_test(void)
  309. {
  310. int ret;
  311. if((ret = pthread_create(&pco0->thread_id, NULL, thread_co_test, pco0)) == 0)
  312. printf("thread co0 create success\r\n");
  313. else
  314. printf("thread co0 create fail ret=%d\r\n", ret);
  315. }
  316. /******************************************************************************
  317. 函数名称: shm_init
  318. 函数版本: 01.01
  319. 创建作者: sunxi
  320. 创建日期: 2022-04-21
  321. 函数说明: shm初始化.
  322. 参数说明:
  323. 返回值: 成功返回0, 失败返回-1
  324. 修改记录:
  325. */
  326. int shm_init(void)
  327. {
  328. // int ret;
  329. int i;
  330. SHM_BASE_R = NULL;
  331. SHM_BASE_W = NULL;
  332. pco0 = malloc(sizeof(struct dev_co));
  333. memset(pco0, 0, sizeof(struct dev_co));
  334. if(co_open(pco0, "/dev/arv-shmem", 0)) {
  335. printf("open shm blk 0 error\r\n");
  336. return -1;
  337. }
  338. SHM_BASE_R = pco0->shm_r; //共享内存---读(从裸核读取数据)---开始地址
  339. SHM_BASE_W = pco0->shm_w; //共享内存---写(向裸核写入数据)---开始地址
  340. //初始化shm区
  341. //暂时初始化参数区
  342. for(i=0;i<SHM_ADDR_U_ADC_1;i++)
  343. {
  344. SHM_BASE_R[i] = 0;
  345. SHM_BASE_W[i] = i;
  346. }
  347. // shm_test();//sunxi for test
  348. return 0;
  349. }
  350. /******************************************************************************
  351. 函数名称: shm_exit
  352. 函数版本: 01.01
  353. 创建作者: sunxi
  354. 创建日期: 2022-04-21
  355. 函数说明: shm反初始化.
  356. 参数说明:
  357. 返回值: 总是返回0.
  358. 修改记录:
  359. */
  360. int shm_exit(void)
  361. {
  362. //pthread_join(pco0->thread_id, NULL);
  363. co_close(pco0);
  364. return 0;
  365. }