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