You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
17 KiB

3 years ago
  1. /**
  2. ****************************************************************************************************
  3. * @file usmart.c
  4. * @author (ALIENTEK)
  5. * @version V3.5
  6. * @date 2020-12-20
  7. * @brief USMART
  8. *
  9. * USMART是由ALIENTEK开发的一个灵巧的串口调试互交组件, ,
  10. * ,.,((10/16,)
  11. * ),10, .
  12. * V2.1hex和dec两个指令..
  13. * ,:
  14. * "hex 100" HEX 0X64.
  15. * "dec 0X64" DEC 100.
  16. * @note
  17. * USMART资源占用情况@MDK 3.80A@2.0
  18. * FLASH:4K~K字节(USMART_USE_HELP和USMART_USE_WRFUNS设置)
  19. * SRAM:72()
  20. * SRAM计算公式: SRAM=PARM_LEN+72-4 PARM_LEN必须大于等于4.
  21. * 100.
  22. * @license Copyright (c) 2020-2032, 广
  23. ****************************************************************************************************
  24. * @attention
  25. *
  26. * 线:www.yuanzige.com
  27. * :www.openedv.com
  28. * :www.alientek.com
  29. * :openedv.taobao.com
  30. *
  31. *
  32. *
  33. * V3.4USMART文件夹下的:readme.txt
  34. *
  35. * V3.4 20200324
  36. * 1, usmart_port.c和usmart_port.h,USMART的移植,便
  37. * 2, : uint8_t, uint16_t, uint32_t
  38. * 3, usmart_reset_runtime为usmart_timx_reset_time
  39. * 4, usmart_get_runtime为usmart_timx_get_time
  40. * 5, usmart_scan函数实现方式,usmart_get_input_string获取数据流
  41. * 6, printf函数为USMART_PRINTF宏定义
  42. * 7, ,,便
  43. *
  44. * V3.5 20201220
  45. * 1AC6编译器
  46. *
  47. ****************************************************************************************************
  48. */
  49. #include "./USMART/usmart.h"
  50. #include "./USMART/usmart_str.h"
  51. #include "./USMART/usmart_port.h"
  52. /* 系统命令 */
  53. char *sys_cmd_tab[] =
  54. {
  55. "?",
  56. "help",
  57. "list",
  58. "id",
  59. "hex",
  60. "dec",
  61. "runtime",
  62. };
  63. /**
  64. * @brief
  65. * @param str :
  66. * @retval 0,;,;
  67. */
  68. uint8_t usmart_sys_cmd_exe(char *str)
  69. {
  70. uint8_t i;
  71. char sfname[MAX_FNAME_LEN]; /* 存放本地函数名 */
  72. uint8_t pnum;
  73. uint8_t rval;
  74. uint32_t res;
  75. res = usmart_get_cmdname(str, sfname, &i, MAX_FNAME_LEN); /* 得到指令及指令长度 */
  76. if (res)return USMART_FUNCERR; /* 错误的指令 */
  77. str += i;
  78. for (i = 0; i < sizeof(sys_cmd_tab) / 4; i++) /* 支持的系统指令 */
  79. {
  80. if (usmart_strcmp(sfname, sys_cmd_tab[i]) == 0)break;
  81. }
  82. switch (i)
  83. {
  84. case 0:
  85. case 1: /* 帮助指令 */
  86. USMART_PRINTF("\r\n");
  87. #if USMART_USE_HELP
  88. USMART_PRINTF("------------------------USMART V3.5------------------------ \r\n");
  89. USMART_PRINTF(" USMART是由ALIENTEK开发的一个灵巧的串口调试互交组件,通过 \r\n");
  90. USMART_PRINTF("它,你可以通过串口助手调用程序里面的任何函数,并执行.因此,你可\r\n");
  91. USMART_PRINTF("以随意更改函数的输入参数(支持数字(10/16进制,支持负数)、字符串\r\n"),
  92. USMART_PRINTF("、函数入口地址等作为参数),单个函数最多支持10个输入参数,并支持\r\n"),
  93. USMART_PRINTF("函数返回值显示.支持参数显示进制设置功能,支持进制转换功能.\r\n");
  94. USMART_PRINTF("技术支持:www.openedv.com\r\n");
  95. USMART_PRINTF("USMART有7个系统命令(必须小写):\r\n");
  96. USMART_PRINTF("?: 获取帮助信息\r\n");
  97. USMART_PRINTF("help: 获取帮助信息\r\n");
  98. USMART_PRINTF("list: 可用的函数列表\r\n\n");
  99. USMART_PRINTF("id: 可用函数的ID列表\r\n\n");
  100. USMART_PRINTF("hex: 参数16进制显示,后跟空格+数字即执行进制转换\r\n\n");
  101. USMART_PRINTF("dec: 参数10进制显示,后跟空格+数字即执行进制转换\r\n\n");
  102. USMART_PRINTF("runtime:1,开启函数运行计时;0,关闭函数运行计时;\r\n\n");
  103. USMART_PRINTF("请按照程序编写格式输入函数名及参数并以回车键结束.\r\n");
  104. USMART_PRINTF("--------------------------正点原子------------------------- \r\n");
  105. #else
  106. USMART_PRINTF("指令失效\r\n");
  107. #endif
  108. break;
  109. case 2: /* 查询指令 */
  110. USMART_PRINTF("\r\n");
  111. USMART_PRINTF("-------------------------函数清单--------------------------- \r\n");
  112. for (i = 0; i < usmart_dev.fnum; i++)USMART_PRINTF("%s\r\n", usmart_dev.funs[i].name);
  113. USMART_PRINTF("\r\n");
  114. break;
  115. case 3: /* 查询ID */
  116. USMART_PRINTF("\r\n");
  117. USMART_PRINTF("-------------------------函数 ID --------------------------- \r\n");
  118. for (i = 0; i < usmart_dev.fnum; i++)
  119. {
  120. usmart_get_fname((char *)usmart_dev.funs[i].name, sfname, &pnum, &rval); /* 得到本地函数名 */
  121. USMART_PRINTF("%s id is:\r\n0X%08X\r\n", sfname, (unsigned int)usmart_dev.funs[i].func); /* 显示ID */
  122. }
  123. USMART_PRINTF("\r\n");
  124. break;
  125. case 4: /* hex指令 */
  126. USMART_PRINTF("\r\n");
  127. usmart_get_aparm(str, sfname, &i);
  128. if (i == 0) /* 参数正常 */
  129. {
  130. i = usmart_str2num(sfname, &res); /* 记录该参数 */
  131. if (i == 0) /* 进制转换功能 */
  132. {
  133. USMART_PRINTF("HEX:0X%X\r\n", res); /* 转为16进制 */
  134. }
  135. else if (i != 4)return USMART_PARMERR; /* 参数错误. */
  136. else /* 参数显示设定功能 */
  137. {
  138. USMART_PRINTF("16进制参数显示!\r\n");
  139. usmart_dev.sptype = SP_TYPE_HEX;
  140. }
  141. }
  142. else return USMART_PARMERR; /* 参数错误. */
  143. USMART_PRINTF("\r\n");
  144. break;
  145. case 5: /* dec指令 */
  146. USMART_PRINTF("\r\n");
  147. usmart_get_aparm(str, sfname, &i);
  148. if (i == 0) /* 参数正常 */
  149. {
  150. i = usmart_str2num(sfname, &res); /* 记录该参数 */
  151. if (i == 0) /* 进制转换功能 */
  152. {
  153. USMART_PRINTF("DEC:%lu\r\n", (unsigned long)res); /* 转为10进制 */
  154. }
  155. else if (i != 4)
  156. {
  157. return USMART_PARMERR; /* 参数错误. */
  158. }
  159. else /* 参数显示设定功能 */
  160. {
  161. USMART_PRINTF("10进制参数显示!\r\n");
  162. usmart_dev.sptype = SP_TYPE_DEC;
  163. }
  164. }
  165. else
  166. {
  167. return USMART_PARMERR; /* 参数错误. */
  168. }
  169. USMART_PRINTF("\r\n");
  170. break;
  171. case 6: /* runtime指令,设置是否显示函数执行时间 */
  172. USMART_PRINTF("\r\n");
  173. usmart_get_aparm(str, sfname, &i);
  174. if (i == 0) /* 参数正常 */
  175. {
  176. i = usmart_str2num(sfname, &res); /* 记录该参数 */
  177. if (i == 0) /* 读取指定地址数据功能 */
  178. {
  179. if (USMART_ENTIMX_SCAN == 0)
  180. {
  181. USMART_PRINTF("\r\nError! \r\nTo EN RunTime function,Please set USMART_ENTIMX_SCAN = 1 first!\r\n");/* 报错 */
  182. }
  183. else
  184. {
  185. usmart_dev.runtimeflag = res;
  186. if (usmart_dev.runtimeflag)
  187. {
  188. USMART_PRINTF("Run Time Calculation ON\r\n");
  189. }
  190. else
  191. {
  192. USMART_PRINTF("Run Time Calculation OFF\r\n");
  193. }
  194. }
  195. }
  196. else
  197. {
  198. return USMART_PARMERR; /* 未带参数,或者参数错误 */
  199. }
  200. }
  201. else
  202. {
  203. return USMART_PARMERR; /* 参数错误. */
  204. }
  205. USMART_PRINTF("\r\n");
  206. break;
  207. default:/* 非法指令 */
  208. return USMART_FUNCERR;
  209. }
  210. return 0;
  211. }
  212. /**
  213. * @brief USMART
  214. * @param tclk: (:Mhz)
  215. * @retval
  216. */
  217. void usmart_init(uint16_t tclk)
  218. {
  219. #if USMART_ENTIMX_SCAN == 1
  220. usmart_timx_init(1000, tclk * 100 - 1);
  221. #endif
  222. usmart_dev.sptype = 1; /* 十六进制显示参数 */
  223. }
  224. /**
  225. * @brief str中获取函数名,id,
  226. * @param str: .
  227. * @retval 0,;,.
  228. */
  229. uint8_t usmart_cmd_rec(char *str)
  230. {
  231. uint8_t sta, i, rval; /* 状态 */
  232. uint8_t rpnum, spnum;
  233. char rfname[MAX_FNAME_LEN]; /* 暂存空间,用于存放接收到的函数名 */
  234. char sfname[MAX_FNAME_LEN]; /* 存放本地函数名 */
  235. sta = usmart_get_fname(str, rfname, &rpnum, &rval); /* 得到接收到的数据的函数名及参数个数 */
  236. if (sta)return sta; /* 错误 */
  237. for (i = 0; i < usmart_dev.fnum; i++)
  238. {
  239. sta = usmart_get_fname((char *)usmart_dev.funs[i].name, sfname, &spnum, &rval); /* 得到本地函数名及参数个数 */
  240. if (sta)return sta; /* 本地解析有误 */
  241. if (usmart_strcmp(sfname, rfname) == 0) /* 相等 */
  242. {
  243. if (spnum > rpnum)return USMART_PARMERR;/* 参数错误(输入参数比源函数参数少) */
  244. usmart_dev.id = i; /* 记录函数ID. */
  245. break; /* 跳出. */
  246. }
  247. }
  248. if (i == usmart_dev.fnum)return USMART_NOFUNCFIND; /* 未找到匹配的函数 */
  249. sta = usmart_get_fparam(str, &i); /* 得到函数参数个数 */
  250. if (sta)return sta; /* 返回错误 */
  251. usmart_dev.pnum = i; /* 参数个数记录 */
  252. return USMART_OK;
  253. }
  254. /**
  255. * @brief USMART执行函数
  256. * @note
  257. * .
  258. * 10,..5.
  259. * .:"函数名(参数1,参数2...参数N)=返回值"..
  260. * ,.
  261. *
  262. * @param
  263. * @retval
  264. */
  265. void usmart_exe(void)
  266. {
  267. uint8_t id, i;
  268. uint32_t res;
  269. uint32_t temp[MAX_PARM]; /* 参数转换,使之支持了字符串 */
  270. char sfname[MAX_FNAME_LEN]; /* 存放本地函数名 */
  271. uint8_t pnum, rval;
  272. id = usmart_dev.id;
  273. if (id >= usmart_dev.fnum)return; /* 不执行. */
  274. usmart_get_fname((char *)usmart_dev.funs[id].name, sfname, &pnum, &rval); /* 得到本地函数名,及参数个数 */
  275. USMART_PRINTF("\r\n%s(", sfname); /* 输出正要执行的函数名 */
  276. for (i = 0; i < pnum; i++) /* 输出参数 */
  277. {
  278. if (usmart_dev.parmtype & (1 << i)) /* 参数是字符串 */
  279. {
  280. USMART_PRINTF("%c", '"');
  281. USMART_PRINTF("%s", usmart_dev.parm + usmart_get_parmpos(i));
  282. USMART_PRINTF("%c", '"');
  283. temp[i] = (uint32_t) & (usmart_dev.parm[usmart_get_parmpos(i)]);
  284. }
  285. else /* 参数是数字 */
  286. {
  287. temp[i] = *(uint32_t *)(usmart_dev.parm + usmart_get_parmpos(i));
  288. if (usmart_dev.sptype == SP_TYPE_DEC)
  289. {
  290. USMART_PRINTF("%ld", (long)temp[i]); /* 10进制参数显示 */
  291. }
  292. else
  293. {
  294. USMART_PRINTF("0X%X", temp[i]); /* 16进制参数显示 */
  295. }
  296. }
  297. if (i != pnum - 1)USMART_PRINTF(",");
  298. }
  299. USMART_PRINTF(")");
  300. #if USMART_ENTIMX_SCAN==1
  301. usmart_timx_reset_time(); /* 计时器清零,开始计时 */
  302. #endif
  303. switch (usmart_dev.pnum)
  304. {
  305. case 0: /* 无参数(void类型) */
  306. res = (*(uint32_t(*)())usmart_dev.funs[id].func)();
  307. break;
  308. case 1: /* 有1个参数 */
  309. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0]);
  310. break;
  311. case 2: /* 有2个参数 */
  312. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1]);
  313. break;
  314. case 3: /* 有3个参数 */
  315. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2]);
  316. break;
  317. case 4: /* 有4个参数 */
  318. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3]);
  319. break;
  320. case 5: /* 有5个参数 */
  321. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3], temp[4]);
  322. break;
  323. case 6: /* 有6个参数 */
  324. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3], temp[4], \
  325. temp[5]);
  326. break;
  327. case 7: /* 有7个参数 */
  328. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3], temp[4], \
  329. temp[5], temp[6]);
  330. break;
  331. case 8: /* 有8个参数 */
  332. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3], temp[4], \
  333. temp[5], temp[6], temp[7]);
  334. break;
  335. case 9: /* 有9个参数 */
  336. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3], temp[4], \
  337. temp[5], temp[6], temp[7], temp[8]);
  338. break;
  339. case 10:/* 有10个参数 */
  340. res = (*(uint32_t(*)())usmart_dev.funs[id].func)(temp[0], temp[1], temp[2], temp[3], temp[4], \
  341. temp[5], temp[6], temp[7], temp[8], temp[9]);
  342. break;
  343. }
  344. #if USMART_ENTIMX_SCAN==1
  345. usmart_timx_get_time(); /* 获取函数执行时间 */
  346. #endif
  347. if (rval == 1) /* 需要返回值. */
  348. {
  349. if (usmart_dev.sptype == SP_TYPE_DEC)USMART_PRINTF("=%lu;\r\n", (unsigned long)res); /* 输出执行结果(10进制参数显示) */
  350. else USMART_PRINTF("=0X%X;\r\n", res); /* 输出执行结果(16进制参数显示) */
  351. }
  352. else USMART_PRINTF(";\r\n"); /* 不需要返回值,直接输出结束 */
  353. if (usmart_dev.runtimeflag) /* 需要显示函数执行时间 */
  354. {
  355. USMART_PRINTF("Function Run Time:%d.%1dms\r\n", usmart_dev.runtime / 10, usmart_dev.runtime % 10); /* 打印函数执行时间 */
  356. }
  357. }
  358. /**
  359. * @brief USMART扫描函数
  360. * @note
  361. * ,USMART的各个控制.
  362. * .
  363. * ,.
  364. * ,USART_RX_STA和USART_RX_BUF[]
  365. *
  366. * @param
  367. * @retval
  368. */
  369. void usmart_scan(void)
  370. {
  371. uint8_t sta, len;
  372. char *pbuf = 0;
  373. pbuf = usmart_get_input_string(); /* 获取数据数据流 */
  374. if (pbuf == 0) return ; /* 数据流空, 直接返回 */
  375. sta = usmart_dev.cmd_rec(pbuf); /* 得到函数各个信息 */
  376. if (sta == 0)
  377. {
  378. usmart_dev.exe(); /* 执行函数 */
  379. }
  380. else
  381. {
  382. len = usmart_sys_cmd_exe(pbuf);
  383. if (len != USMART_FUNCERR)sta = len;
  384. if (sta)
  385. {
  386. switch (sta)
  387. {
  388. case USMART_FUNCERR:
  389. USMART_PRINTF("函数错误!\r\n");
  390. break;
  391. case USMART_PARMERR:
  392. USMART_PRINTF("参数错误!\r\n");
  393. break;
  394. case USMART_PARMOVER:
  395. USMART_PRINTF("参数太多!\r\n");
  396. break;
  397. case USMART_NOFUNCFIND:
  398. USMART_PRINTF("未找到匹配的函数!\r\n");
  399. break;
  400. }
  401. }
  402. }
  403. }
  404. #if USMART_USE_WRFUNS == 1 /* 如果使能了读写操作 */
  405. /**
  406. * @brief
  407. * @param
  408. * @retval
  409. */
  410. uint32_t read_addr(uint32_t addr)
  411. {
  412. return *(uint32_t *)addr;
  413. }
  414. /**
  415. * @brief
  416. * @param
  417. * @retval
  418. */
  419. void write_addr(uint32_t addr, uint32_t val)
  420. {
  421. *(uint32_t *)addr = val;
  422. }
  423. #endif