Firefly开源社区

打印 上一主题 下一主题

分享一个在linux app层调用/dev/mem来控制WORK_LED的方法

39

积分

0

威望

0

贡献

游客

积分
39

分享一个在linux app层调用/dev/mem来控制WORK_LED的方法

发表于 2015-5-12 21:11:30      浏览:8167 | 回复:5        打印     [复制链接] 楼主
如下代码完全由本人编写,亲自验证通过的,大家欢迎交流~通过这种方法不光可以控制指示灯哦~
APP代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<unistd.h>
  4. #include<sys/mman.h>
  5. #include<sys/types.h>
  6. #include<sys/stat.h>
  7. #include<fcntl.h>

  8. /* 数据类型*/
  9. #define INVALID_UCHAR 0xff
  10. #define FALSE 0
  11. #define TRUE 1
  12. #define uchar unsigned char
  13. #define ushort unsigned short
  14. #define uint unsigned int
  15. #define ulong unsigned long

  16. /* 命令行相关宏定义*/
  17. #define CMD_BUF_LEN 32
  18. #define CMD_LOGO "<WSF>"
  19. #define OUTPUT_BUF_LEN 64

  20. #define CMD_PRIMY_NUM 3
  21. #define CMD_EXTEND_NUM 2
  22. #define CMD_WORD_MAX 10
  23. #define CMD_LEN 20

  24. char g_acCMD_PrimyWord[CMD_PRIMY_NUM][CMD_LEN] = {"test", "wr", "rd"};
  25. char g_acCMD_ExtendWord[CMD_EXTEND_NUM][CMD_LEN] = {"openmem", "closemem"};
  26. char g_acCMD_Word[CMD_WORD_MAX][CMD_LEN];

  27. /* 内存设备相关*/
  28. typedef struct MEM_DeviceInfo
  29. {
  30. int fd;
  31. int *map_VirAddr;
  32. int *map_PhyAddr;
  33. int memlen;
  34. int flag;
  35. }MEM_INFO_S;

  36. MEM_INFO_S g_stMemInfo = {0};

  37. uchar MEM_InitDevice(int phyaddr, int len)
  38. {
  39. int fd;//文件句柄
  40. uchar *map_base = NULL;

  41. /* 未打开MEM设备则打开*/
  42. if (0 == g_stMemInfo.fd)
  43. {
  44. /* 打开mem设备*/
  45. fd = open("/dev/mem", O_RDWR|O_SYNC);
  46. if (fd == -1)
  47. {
  48. printf("ERROR: Open mem device failed!\r\n");
  49. return FALSE;
  50. }
  51. g_stMemInfo.fd = fd;
  52. }

  53. /* 映射虚拟地址*/
  54. map_base = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, g_stMemInfo.fd, phyaddr);
  55. if (MAP_FAILED == map_base)
  56. {
  57. perror("mmap");
  58. printf("ERROR: mmap mem device failed!\r\n");
  59. return FALSE;
  60. }
  61. //        printf("mmap virtual address 0x%x\r\n", map_base);
  62. g_stMemInfo.map_VirAddr = (int *)map_base;
  63. // g_stMemInfo.map_PhyAddr = (int *)phyaddr;
  64. g_stMemInfo.memlen = len;
  65. g_stMemInfo.flag = TRUE;

  66. return TRUE;
  67. }

  68. void MEM_ReleaseDevice(void)
  69. {
  70. if (TRUE == g_stMemInfo.flag)
  71. {
  72. /* 释放虚拟地址*/
  73. munmap( g_stMemInfo.map_VirAddr, g_stMemInfo.memlen);

  74. g_stMemInfo.flag = FALSE;
  75. }

  76. /* 关闭文件*/
  77. close(g_stMemInfo.fd);

  78. memset(&g_stMemInfo, 0, sizeof(g_stMemInfo));
  79. }

  80. uchar CMD_GetWord(char *cCmd)
  81. {
  82. char *cString, *cTmp;
  83. char cDelims[] = " ";
  84. uchar ucWordCnt = 0;
  85. int mapAddr = 0;

  86. // cString = ltrim(cCmd);
  87. cString = cCmd;

  88. // printf("\r\nget:");
  89. // printf(cString);
  90. memset(g_acCMD_Word, 0, sizeof(g_acCMD_Word)) ;


  91. while (ucWordCnt < CMD_WORD_MAX)
  92. {
  93. if (0 == ucWordCnt)
  94. {
  95. cTmp = strtok(cString, cDelims);
  96. }
  97. else
  98. {
  99. cTmp = strtok(NULL, cDelims);
  100. }

  101. if (NULL == cTmp)
  102. {
  103. break;
  104. }

  105. strcpy(g_acCMD_Word[ucWordCnt], cTmp);

  106. // printf("\r\naa:");
  107. // printf(g_acCMD_Word[ucWordCnt]);

  108. ucWordCnt++;
  109. }

  110. return ucWordCnt;
  111. }


  112. uchar CMD_MatchWord(char *cCmdSrc, char cCmdDes[][CMD_LEN])
  113. {
  114. char *cString;
  115. uchar ucIndex = INVALID_UCHAR;

  116. // cString = ltrim(cCmdSrc);
  117. // cString = rtrim(cCmdSrc);
  118. cString = cCmdSrc;

  119. for (ucIndex = 0; ucIndex < CMD_PRIMY_NUM; ucIndex++)
  120. {
  121. if (0 == strcmp (cString, cCmdDes[ucIndex]))
  122. {
  123. break;
  124. }
  125. // printf("\r\nbb:");
  126. // printf(cCmdDes[ucIndex]);
  127. }

  128. if (CMD_PRIMY_NUM == ucIndex)
  129. {
  130. ucIndex = INVALID_UCHAR;
  131. }

  132. return ucIndex;
  133. }

  134. int main (void)
  135. {
  136. int i;
  137. char cCmd[CMD_BUF_LEN] = {0};
  138. char cOutBuf[OUTPUT_BUF_LEN] = {0};
  139. uchar ucCmdWordCnt = 0;
  140. uchar ucCmdPrimyIndex = 0, ucCmdExtendIndex = 0;
  141. uint uiAddr, uiData, uiLen;
  142. uint *pTmp = NULL;

  143. printf ("hello my test\r\n");
  144. printf(CMD_LOGO);//打印命令logo

  145. while(1)
  146. {
  147. gets (cCmd);//获取输入命令

  148. ucCmdWordCnt = CMD_GetWord(cCmd);
  149. if (ucCmdWordCnt > 0)
  150. {
  151. ucCmdPrimyIndex = CMD_MatchWord (g_acCMD_Word[0], g_acCMD_PrimyWord) ;
  152. switch(ucCmdPrimyIndex)
  153. {
  154. case 0:
  155. {
  156. if (ucCmdWordCnt == 4)
  157. {
  158. ucCmdExtendIndex = CMD_MatchWord (g_acCMD_Word[1], g_acCMD_ExtendWord) ;
  159. if (0 == ucCmdExtendIndex)
  160. {
  161. sscanf(g_acCMD_Word[2], "%x", &uiAddr);
  162. sscanf(g_acCMD_Word[3], "%x", &uiLen);
  163. if (FALSE == MEM_InitDevice (uiAddr, uiLen))
  164. {
  165. break;
  166. }
  167. printf("Command : test initmem addr:0x%x,len:0x%x\r\n", uiAddr, uiLen);
  168. }
  169. }
  170. else if (ucCmdWordCnt == 2)
  171. {

  172. ucCmdExtendIndex = CMD_MatchWord (g_acCMD_Word[1], g_acCMD_ExtendWord) ;
  173. if (1 == ucCmdExtendIndex)
  174. {
  175. MEM_ReleaseDevice ();
  176. printf("Command : test close mem\r\n");
  177. }
  178. }
  179. else
  180. {
  181. printf("ERROR: Cmd invaild!\r\n");
  182. }
  183. break;
  184. }
  185. case 1:
  186. {
  187. if (ucCmdWordCnt == 3)
  188. {
  189. sscanf(g_acCMD_Word[1], "%x", &uiAddr);
  190. sscanf(g_acCMD_Word[2], "%x", &uiData);       
  191. if (TRUE == g_stMemInfo.flag)
  192. {
  193. if (uiAddr <= g_stMemInfo.memlen)
  194. {
  195. uiAddr = (long)g_stMemInfo.map_VirAddr + uiAddr;

  196. pTmp = (uint *)uiAddr;
  197. *pTmp = uiData;
  198. }       
  199. printf("Command : write addr:0x%x,data:0x%x\r\n", uiAddr, *pTmp);
  200. break;
  201. }
  202. printf("ERROR: Device not be opened!\r\n");
  203. }
  204. else
  205. {
  206. printf("ERROR: Cmd invaild!\r\n");
  207. }
  208. break;
  209. }
  210. case 2:
  211. {
  212. if (ucCmdWordCnt == 2)
  213. {
  214. sscanf(g_acCMD_Word[1], "%x", &uiAddr);
  215. if (TRUE == g_stMemInfo.flag)
  216. {
  217. if (uiAddr <= g_stMemInfo.memlen)
  218. {
  219. uiAddr = (long)g_stMemInfo.map_VirAddr + uiAddr;
  220. printf("read address 0x%x\r\n", uiAddr);

  221. pTmp = (uint *)uiAddr;
  222. uiData = *pTmp;
  223. }       
  224. printf("Command : read addr:0x%x,data:0x%x\r\n", uiAddr, uiData);       
  225. break;
  226. }
  227. printf("ERROR: Device not be opened!\r\n");
  228. }
  229. else
  230. {
  231. printf("ERROR: Cmd invaild!\r\n");
  232. }
  233. break;
  234. }
  235. default:
  236. {
  237. printf("ERROR: Cmd invaild!\r\n");
  238. break;
  239. }
  240. }

  241. }

  242. printf(CMD_LOGO);//打印命令logo
  243. // sprintf(cOutBuf, "%s\r\n", cCmd);//打印输入字符串
  244. // printf(cCmd);

  245. /* 清缓存*/
  246. memset (cCmd, 0, CMD_BUF_LEN);
  247. // memset (cOutBuf, 0, OUTPUT_BUF_LEN);
  248. }

  249. return 0;
  250. }
复制代码
Makefile代码:
  1. all = test
  2. objects = testmem.o
  3. CC = arm-linux-gnueabihf-gcc

  4. $(all):$(objects)
  5. $(CC) -static -o $(all) $(objects)

  6. $(objects):%.o:%.c
  7. $(CC) -c [        DISCUZ_CODE_13        ]lt; -o $@

  8. clean:
  9. rm -f *.o $(all) *~
复制代码

实际界面操作:
  1. <blockquote>shell@rk3288:/data/local/test #
复制代码




回复

使用道具 举报

39

积分

0

威望

0

贡献

游客

积分
39
发表于 2015-5-12 21:14:04      沙发
这代码段存放直接把我的空格搞没了:o,大家将就着看吧
回复

使用道具 举报

39

积分

0

威望

0

贡献

游客

积分
39
发表于 2015-5-12 21:19:19      板凳
实际界面操作部分信息也没有,我晕~还不能重新编辑吗?
实际界面操作:
  1. shell@rk3288:/data/local/test #
  2. shell@rk3288:/data/local/test #
  3. shell@rk3288:/data/local/test # ./test                                         
  4. hello my test
  5. <WSF>
  6. <WSF>
  7. <WSF>
  8. <WSF>test openmem ff7f0000 100
  9. Command : test initmem addr:0xff7f0000,len:0x100
  10. <WSF>
  11. <WSF>
  12. <WSF>
  13. <WSF>
  14. <WSF>rd 4
  15. read address 0xb6fcb004
  16. Command : read addr:0xb6fcb004,data:0xf
  17. <WSF>wr 4 1
  18. Command : write addr:0xb6fcb004,data:0x1
  19. <WSF>
  20. <WSF>wr 4 f
  21. Command : write addr:0xb6fcb004,data:0xf
  22. <WSF>
  23. <WSF>
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表