luat_lib_uart.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*
  2. @module uart
  3. @summary 串口操作库
  4. @version 1.0
  5. @date 2020.03.30
  6. @demo uart
  7. @video https://www.bilibili.com/video/BV1er4y1p75y
  8. @tag LUAT_USE_UART
  9. */
  10. #include "luat_base.h"
  11. #include "luat_uart.h"
  12. #include "luat_mem.h"
  13. #include "luat_msgbus.h"
  14. #include "luat_fs.h"
  15. #include "string.h"
  16. #include "luat_zbuff.h"
  17. #include "luat_gpio.h"
  18. #ifdef LUAT_USE_DRV_UART
  19. #include "luat/drv_uart.h"
  20. #endif
  21. #define LUAT_LOG_TAG "uart"
  22. #include "luat_log.h"
  23. #ifndef LUAT_UART_RX_MAX_LEN
  24. #define LUAT_UART_RX_MAX_LEN 0x10000
  25. #endif
  26. #ifdef LUAT_USE_DRV_UART
  27. #define MAX_DEVICE_COUNT 19
  28. #else
  29. #define MAX_DEVICE_COUNT 9
  30. #endif
  31. #define MAX_USB_DEVICE_COUNT 3
  32. typedef struct luat_uart_cb {
  33. int received;//回调函数
  34. int sent;//回调函数
  35. } luat_uart_cb_t;
  36. static luat_uart_cb_t uart_cbs[MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT];
  37. static luat_uart_recv_callback_t uart_app_recvs[MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT];
  38. static luat_uart_sent_callback_t uart_app_sents[MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT];
  39. #ifdef LUAT_USE_SOFT_UART
  40. #ifndef __LUAT_C_CODE_IN_RAM__
  41. #define __LUAT_C_CODE_IN_RAM__
  42. #endif
  43. #ifndef __BSP_COMMON_H__
  44. #include "c_common.h"
  45. #endif
  46. #define LUAT_UART_SOFT_FIFO_CNT (128)
  47. typedef struct
  48. {
  49. llist_head tx_node;
  50. uint8_t *data;
  51. uint32_t len;
  52. }luat_uart_soft_tx_node_t;
  53. typedef struct
  54. {
  55. uint32_t tx_period;
  56. uint32_t rx_period;
  57. uint32_t stop_period;
  58. int tx_adjust_period;
  59. int rx_adjust_period;
  60. Buffer_Struct rx_buffer;
  61. Buffer_Struct tx_buffer;
  62. llist_head tx_queue_head;
  63. uint16_t rx_bit;
  64. uint16_t rx_buffer_size;
  65. uint16_t tx_bit;
  66. uint8_t rx_fifo[LUAT_UART_SOFT_FIFO_CNT];
  67. uint8_t rx_fifo_cnt;
  68. uint8_t tx_shift_bits;
  69. uint8_t rx_shift_bits;
  70. uint8_t data_bits;
  71. uint8_t total_bits;
  72. uint8_t rx_parity_bit;
  73. uint8_t parity; /**< 奇偶校验位 */
  74. uint8_t parity_odd; /**< 奇偶校验位 */
  75. uint8_t tx_pin;
  76. uint8_t rx_pin;
  77. uint8_t tx_hwtimer_id;
  78. uint8_t rx_hwtimer_id;
  79. uint8_t pin485;
  80. uint8_t rs485_rx_level; /**< 接收方向的电平 */
  81. uint8_t uart_id;
  82. uint8_t is_inited;
  83. uint8_t is_tx_busy;
  84. uint8_t is_rx_busy;
  85. }luat_uart_soft_t;
  86. static luat_uart_soft_t *prv_uart_soft;
  87. static int32_t luat_uart_soft_del_tx_queue(void *pdata, void *param)
  88. {
  89. luat_uart_soft_tx_node_t *node = (luat_uart_soft_tx_node_t *)pdata;
  90. luat_heap_alloc(NULL, node->data, 0, 0);
  91. return LIST_DEL;
  92. }
  93. static uint16_t __LUAT_C_CODE_IN_RAM__ luat_uart_soft_check_party(uint8_t data, uint8_t data_bits, uint8_t is_odd)
  94. {
  95. uint16_t data_bits2 = data_bits;
  96. uint8_t party_bits = is_odd;
  97. while(party_bits)
  98. {
  99. party_bits += (data & 0x01);
  100. data >>= 1;
  101. party_bits--;
  102. };
  103. if (party_bits & 0x01)
  104. {
  105. return 1 << data_bits2;
  106. }
  107. return 0;
  108. }
  109. static int __LUAT_C_CODE_IN_RAM__ luat_uart_soft_recv_start_irq(int pin, void *param)
  110. {
  111. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->rx_period + (prv_uart_soft->rx_period >> 4));
  112. prv_uart_soft->rx_shift_bits = 0;
  113. prv_uart_soft->rx_parity_bit = prv_uart_soft->parity_odd;
  114. luat_uart_soft_gpio_fast_irq_set(pin, 0);
  115. luat_uart_soft_sleep_enable(0);
  116. return 0;
  117. }
  118. static int luat_uart_soft_setup(luat_uart_t *uart)
  119. {
  120. prv_uart_soft->rx_buffer_size = uart->bufsz * 2;
  121. prv_uart_soft->rx_buffer.MaxLen = uart->bufsz * 2;
  122. luat_heap_alloc(NULL, prv_uart_soft->rx_buffer.Data, 0, 0);
  123. prv_uart_soft->rx_buffer.Data = luat_heap_alloc(NULL, NULL, 0, prv_uart_soft->rx_buffer_size);
  124. if (!prv_uart_soft->rx_buffer.Data)
  125. {
  126. LLOGE("soft uart no mem!");
  127. prv_uart_soft->is_inited = 0;
  128. return -1;
  129. }
  130. prv_uart_soft->is_inited = 1;
  131. prv_uart_soft->data_bits = uart->data_bits;
  132. switch(uart->parity)
  133. {
  134. case LUAT_PARITY_NONE:
  135. prv_uart_soft->parity = 0;
  136. break;
  137. case LUAT_PARITY_ODD:
  138. prv_uart_soft->parity = 1;
  139. prv_uart_soft->parity_odd = 1;
  140. break;
  141. case LUAT_PARITY_EVEN:
  142. prv_uart_soft->parity = 1;
  143. prv_uart_soft->parity_odd = 0;
  144. break;
  145. }
  146. prv_uart_soft->parity = uart->parity;
  147. if (prv_uart_soft->tx_adjust_period < 0)
  148. {
  149. prv_uart_soft->tx_period = luat_uart_soft_cal_baudrate(uart->baud_rate) - (-prv_uart_soft->tx_adjust_period);
  150. }
  151. else
  152. {
  153. prv_uart_soft->tx_period = luat_uart_soft_cal_baudrate(uart->baud_rate) + prv_uart_soft->tx_adjust_period;
  154. }
  155. if (prv_uart_soft->rx_adjust_period < 0)
  156. {
  157. prv_uart_soft->rx_period = luat_uart_soft_cal_baudrate(uart->baud_rate) - (-prv_uart_soft->rx_adjust_period);
  158. }
  159. else
  160. {
  161. prv_uart_soft->rx_period = luat_uart_soft_cal_baudrate(uart->baud_rate) + prv_uart_soft->rx_adjust_period;
  162. }
  163. // LLOGD("soft uart period %u,%u!", prv_uart_soft->tx_period, prv_uart_soft->rx_period);
  164. switch(uart->stop_bits)
  165. {
  166. case 2:
  167. prv_uart_soft->total_bits = prv_uart_soft->data_bits + prv_uart_soft->parity + 4;
  168. prv_uart_soft->stop_period = luat_uart_soft_cal_baudrate(uart->baud_rate) * 3;
  169. break;
  170. default:
  171. prv_uart_soft->total_bits = prv_uart_soft->data_bits + prv_uart_soft->parity + 2;
  172. prv_uart_soft->stop_period = luat_uart_soft_cal_baudrate(uart->baud_rate) * 2;
  173. break;
  174. }
  175. if (uart->pin485 != 0xffffffff)
  176. {
  177. prv_uart_soft->pin485 = uart->pin485;
  178. prv_uart_soft->rs485_rx_level = uart->rx_level;
  179. }
  180. else
  181. {
  182. prv_uart_soft->pin485 = 0xff;
  183. }
  184. luat_gpio_t conf = {0};
  185. conf.pin = prv_uart_soft->rx_pin;
  186. conf.mode = Luat_GPIO_IRQ;
  187. conf.irq_cb = luat_uart_soft_recv_start_irq;
  188. conf.pull = LUAT_GPIO_PULLUP;
  189. conf.irq = LUAT_GPIO_FALLING_IRQ;
  190. conf.alt_func = -1;
  191. luat_gpio_setup(&conf);
  192. conf.pin = prv_uart_soft->tx_pin;
  193. conf.mode = Luat_GPIO_OUTPUT;
  194. luat_gpio_setup(&conf);
  195. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 1);
  196. if (prv_uart_soft->pin485 != 0xff)
  197. {
  198. conf.pin = prv_uart_soft->pin485;
  199. conf.mode = Luat_GPIO_OUTPUT;
  200. luat_gpio_set(prv_uart_soft->pin485, prv_uart_soft->rs485_rx_level);
  201. luat_gpio_setup(&conf);
  202. }
  203. prv_uart_soft->rx_shift_bits = 0;
  204. prv_uart_soft->tx_shift_bits = 0;
  205. prv_uart_soft->rx_fifo_cnt = 0;
  206. prv_uart_soft->is_tx_busy = 0;
  207. prv_uart_soft->is_rx_busy= 0;
  208. luat_uart_soft_sleep_enable(1);
  209. return 0;
  210. }
  211. static void luat_uart_soft_close(void)
  212. {
  213. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, 0);
  214. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, 0);
  215. luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, NULL);
  216. luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->rx_hwtimer_id, NULL);
  217. if (prv_uart_soft->is_inited)
  218. {
  219. luat_uart_soft_tx_node_t *node;
  220. while (!llist_empty(&prv_uart_soft->tx_queue_head))
  221. {
  222. node = (luat_uart_soft_tx_node_t *)(prv_uart_soft->tx_queue_head.next);
  223. llist_del(&node->tx_node);
  224. luat_heap_alloc(NULL, node->data, 0, 0);
  225. luat_heap_alloc(NULL, node, 0, 0);
  226. }
  227. }
  228. prv_uart_soft->is_inited = 0;
  229. luat_gpio_close(prv_uart_soft->rx_pin);
  230. luat_gpio_close(prv_uart_soft->tx_pin);
  231. if (prv_uart_soft->pin485 != 0xff)
  232. {
  233. luat_gpio_close(prv_uart_soft->pin485);
  234. }
  235. luat_heap_alloc(NULL, prv_uart_soft->rx_buffer.Data, 0, 0);
  236. memset(&prv_uart_soft->rx_buffer, 0, sizeof(Buffer_Struct));
  237. memset(&prv_uart_soft->tx_buffer, 0, sizeof(Buffer_Struct));
  238. prv_uart_soft->is_tx_busy = 0;
  239. prv_uart_soft->is_rx_busy= 0;
  240. luat_uart_soft_sleep_enable(1);
  241. }
  242. static uint32_t luat_uart_soft_read(uint8_t *data, uint32_t len)
  243. {
  244. // if (!data) return prv_uart_soft->rx_buffer.Pos;
  245. uint32_t read_len = (len > prv_uart_soft->rx_buffer.Pos)?prv_uart_soft->rx_buffer.Pos:len;
  246. memcpy(data, prv_uart_soft->rx_buffer.Data, read_len);
  247. if (read_len >= prv_uart_soft->rx_buffer.Pos)
  248. {
  249. prv_uart_soft->rx_buffer.Pos = 0;
  250. // if (prv_uart_soft->rx_buffer.MaxLen > prv_uart_soft->rx_buffer_size)
  251. // {
  252. // luat_heap_alloc(NULL, prv_uart_soft->rx_buffer.Data, 0, 0);
  253. // prv_uart_soft->rx_buffer.Data = luat_heap_alloc(NULL, NULL, 0, prv_uart_soft->rx_buffer_size);
  254. // }
  255. }
  256. else
  257. {
  258. uint32_t rest = prv_uart_soft->rx_buffer.Pos - read_len;
  259. memmove(prv_uart_soft->rx_buffer.Data, prv_uart_soft->rx_buffer.Data + read_len, rest);
  260. prv_uart_soft->rx_buffer.Pos = rest;
  261. }
  262. return read_len;
  263. }
  264. static int luat_uart_soft_write(const uint8_t *data, uint32_t len)
  265. {
  266. luat_uart_soft_tx_node_t *node = luat_heap_alloc(NULL, NULL, 0, sizeof(luat_uart_soft_tx_node_t));
  267. if (!node)
  268. {
  269. return -1;
  270. }
  271. node->data = luat_heap_alloc(NULL, NULL, 0, len);
  272. if (!data)
  273. {
  274. luat_heap_alloc(NULL, node, 0, 0);
  275. return -1;
  276. }
  277. memcpy(node->data, data, len);
  278. node->len = len;
  279. llist_add_tail(&node->tx_node, &prv_uart_soft->tx_queue_head);
  280. if (!prv_uart_soft->tx_buffer.Data)
  281. {
  282. if (prv_uart_soft->pin485 != 0xff)
  283. {
  284. luat_gpio_set(prv_uart_soft->pin485, !prv_uart_soft->rs485_rx_level);
  285. }
  286. node = (luat_uart_soft_tx_node_t *)prv_uart_soft->tx_queue_head.next;
  287. Buffer_StaticInit(&prv_uart_soft->tx_buffer, node->data, node->len);
  288. prv_uart_soft->tx_shift_bits = 0;
  289. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  290. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  291. if (prv_uart_soft->parity)
  292. {
  293. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | luat_uart_soft_check_party(prv_uart_soft->tx_buffer.Data[0], prv_uart_soft->data_bits, prv_uart_soft->parity_odd) | prv_uart_soft->tx_buffer.Data[0];
  294. }
  295. else
  296. {
  297. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[0];
  298. }
  299. }
  300. prv_uart_soft->is_tx_busy = 1;
  301. luat_uart_soft_sleep_enable(0);
  302. return 0;
  303. }
  304. #endif
  305. #ifdef LUAT_USE_DRV_UART
  306. int luat_drv_uart_exist(int id) {
  307. if (id >= LUAT_VUART_ID_0)
  308. return 1;
  309. return id >= 0 && id <= MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT;
  310. }
  311. #endif
  312. void luat_uart_set_app_recv(int id, luat_uart_recv_callback_t cb) {
  313. #ifdef LUAT_USE_DRV_UART
  314. if (luat_drv_uart_exist(id))
  315. #else
  316. if (luat_uart_exist(id))
  317. #endif
  318. {
  319. uart_app_recvs[id] = cb;
  320. luat_setup_cb(id, 1, 0); // 暂时覆盖
  321. }
  322. else {
  323. LLOGW("not exist uart id=%d", id);
  324. }
  325. }
  326. void luat_uart_set_app_sent(int id, luat_uart_sent_callback_t cb) {
  327. #ifdef LUAT_USE_DRV_UART
  328. if (luat_drv_uart_exist(id))
  329. #else
  330. if (luat_uart_exist(id))
  331. #endif
  332. {
  333. uart_app_sents[id] = cb;
  334. luat_setup_cb(id, 1, 1); // 暂时覆盖
  335. }
  336. else {
  337. LLOGW("not exist uart id=%d", id);
  338. }
  339. }
  340. int l_vuart_state_handler(lua_State *L, void* ptr) {
  341. (void)ptr;
  342. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  343. lua_getglobal(L, "sys_pub");
  344. if (lua_isfunction(L, -1)) {
  345. lua_pushstring(L, "VUART_STATE");
  346. lua_pushinteger(L, msg->arg1);
  347. lua_pushboolean(L, !msg->arg2);
  348. lua_call(L, 3, 0);
  349. }
  350. lua_pushinteger(L, 0);
  351. return 1;
  352. }
  353. int l_uart_handler(lua_State *L, void* ptr) {
  354. (void)ptr;
  355. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  356. lua_pop(L, 1);
  357. int uart_id = msg->arg1;
  358. // LLOGD("l_uart_handler %d", uart_id);
  359. #ifdef LUAT_USE_DRV_UART
  360. if (!luat_drv_uart_exist(uart_id))
  361. #else
  362. if (!luat_uart_exist(uart_id))
  363. #endif
  364. {
  365. //LLOGW("not exist uart id=%ld but event fired?!", uart_id);
  366. return 0;
  367. }
  368. int org_uart_id = uart_id;
  369. #if !defined(LUAT_USE_WINDOWS) && !defined(LUAT_USE_LINUX) && !defined(LUAT_USE_MACOS)
  370. if (uart_id >= LUAT_VUART_ID_0)
  371. {
  372. uart_id = MAX_DEVICE_COUNT + uart_id - LUAT_VUART_ID_0;
  373. }
  374. #endif
  375. // sent event
  376. if (msg->arg2 == 0) {
  377. // LLOGD("uart%ld sent callback", uart_id);
  378. if (uart_app_sents[uart_id]) {
  379. uart_app_sents[uart_id](uart_id, (void *)msg->arg2);
  380. }
  381. if (uart_cbs[uart_id].sent) {
  382. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  383. if (lua_isfunction(L, -1)) {
  384. lua_pushinteger(L, org_uart_id);
  385. lua_call(L, 1, 0);
  386. }
  387. }
  388. }
  389. else {
  390. if (uart_app_recvs[uart_id]) {
  391. uart_app_recvs[uart_id](uart_id, msg->arg2);
  392. }
  393. if (uart_cbs[uart_id].received) {
  394. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  395. if (lua_isfunction(L, -1)) {
  396. lua_pushinteger(L, org_uart_id);
  397. lua_pushinteger(L, msg->arg2);
  398. lua_call(L, 2, 0);
  399. }
  400. else {
  401. LLOGD("uart%ld received callback not function", uart_id);
  402. }
  403. }
  404. else if (uart_app_recvs[uart_id] == NULL) {
  405. //LLOGD("uart%ld no received callback", uart_id);
  406. }
  407. }
  408. // 给rtos.recv方法返回个空数据
  409. lua_pushinteger(L, 0);
  410. return 1;
  411. }
  412. /*
  413. 配置串口参数
  414. @api uart.setup(id, baud_rate, data_bits, stop_bits, partiy, bit_order, buff_size, rs485_gpio, rs485_level, rs485_delay, debug_enable, error_drop)
  415. @int 串口id, uart0写0, uart1写1, 如此类推, 最大值取决于设备
  416. @int 波特率, 默认115200,可选择波特率表:{2000000,921600,460800,230400,115200,57600,38400,19200,9600,4800,2400}
  417. @int 数据位,默认为8, 可选 7/8
  418. @int 停止位,默认为1, 根据实际情况,可以有0.5/1/1.5/2等
  419. @int 校验位,可选 uart.None/uart.Even/uart.Odd
  420. @int 大小端,默认小端 uart.LSB, 可选 uart.MSB
  421. @int 缓冲区大小,默认值1024
  422. @int 485模式下的转换GPIO, 默认值0xffffffff
  423. @int 485模式下的rx方向GPIO的电平, 默认值0
  424. @int 485模式下tx向rx转换的延迟时间,默认值12bit的时间,单位us, 9600波特率填20000
  425. @int 开启调试功能,默认使能,填写uart.DEBUG或者非数字使能,其他值都是关闭,目前只有移芯平台支持
  426. @int 遇到接收错误是否放弃缓存数据,默认使能,填写uart.ERROR_DROP或者非数字使能,其他值都是关闭,目前只有移芯平台支持
  427. @return int 成功返回0,失败返回其他值
  428. @usage
  429. -- 最常用115200 8N1
  430. uart.setup(1, 115200, 8, 1, uart.NONE)
  431. -- 可以简写为 uart.setup(1)
  432. -- 485自动切换, 选取GPIO10作为收发转换脚
  433. uart.setup(1, 115200, 8, 1, uart.NONE, uart.LSB, 1024, 10, 0, 2000)
  434. -- 遇到接收错误不抛弃缓存数据
  435. uart.setup(1, 115200, 8, 1, uart.NONE, nil, 1024, nil, nil, nil, nil, 0)
  436. */
  437. static int l_uart_setup(lua_State *L)
  438. {
  439. lua_Number stop_bits = luaL_optnumber(L, 4, 1);
  440. luat_uart_t uart_config = {
  441. .id = luaL_checkinteger(L, 1),
  442. .baud_rate = luaL_optinteger(L, 2, 115200),
  443. .data_bits = luaL_optinteger(L, 3, 8),
  444. .parity = luaL_optinteger(L, 5, LUAT_PARITY_NONE),
  445. .bit_order = luaL_optinteger(L, 6, LUAT_BIT_ORDER_LSB),
  446. .bufsz = luaL_optinteger(L, 7, 1024),
  447. .pin485 = luaL_optinteger(L, 8, 0xffffffff),
  448. .rx_level = luaL_optinteger(L, 9, 0),
  449. };
  450. if(stop_bits == 0.5)
  451. uart_config.stop_bits = LUAT_0_5_STOP_BITS;
  452. else if(stop_bits == 1.5)
  453. uart_config.stop_bits = LUAT_1_5_STOP_BITS;
  454. else
  455. uart_config.stop_bits = (uint8_t)stop_bits;
  456. uart_config.delay = luaL_optinteger(L, 10, 12000000/uart_config.baud_rate);
  457. uart_config.debug_enable = luaL_optinteger(L, 11, LUAT_UART_DEBUG_ENABLE);
  458. uart_config.error_drop = luaL_optinteger(L, 12, LUAT_UART_RX_ERROR_DROP_DATA);
  459. int result = 0;
  460. #ifdef LUAT_USE_SOFT_UART
  461. if (prv_uart_soft && (prv_uart_soft->uart_id == uart_config.id))
  462. {
  463. result = luat_uart_soft_setup(&uart_config);
  464. }
  465. else
  466. {
  467. #ifdef LUAT_USE_DRV_UART
  468. result = luat_drv_uart_setup(&uart_config);
  469. #else
  470. result = luat_uart_setup(&uart_config);
  471. #endif
  472. }
  473. lua_pushinteger(L, result);
  474. #else
  475. #ifdef LUAT_USE_DRV_UART
  476. result = luat_drv_uart_setup(&uart_config);
  477. #else
  478. result = luat_uart_setup(&uart_config);
  479. #endif
  480. lua_pushinteger(L, result);
  481. #endif
  482. return 1;
  483. }
  484. /*
  485. 写串口
  486. @api uart.write(id, data)
  487. @int 串口id, uart0写0, uart1写1
  488. @string/zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  489. @int 可选,要发送的数据长度,默认全发
  490. @return int 成功的数据长度
  491. @usage
  492. -- 写入可见字符串
  493. uart.write(1, "rdy\r\n")
  494. -- 写入十六进制的数据串
  495. uart.write(1, string.char(0x55,0xAA,0x4B,0x03,0x86))
  496. */
  497. static int l_uart_write(lua_State *L)
  498. {
  499. size_t len;
  500. const char *buf;
  501. uint8_t id = luaL_checkinteger(L, 1);
  502. if(lua_isuserdata(L, 2))
  503. {
  504. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  505. len = buff->len - buff->cursor;
  506. buf = (const char *)(buff->addr + buff->cursor);
  507. }
  508. else
  509. {
  510. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  511. }
  512. if(lua_isinteger(L, 3))
  513. {
  514. size_t l = luaL_checkinteger(L, 3);
  515. if(len > l)
  516. len = l;
  517. }
  518. int result;
  519. #ifdef LUAT_USE_SOFT_UART
  520. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  521. {
  522. result = luat_uart_soft_write((const uint8_t*)buf, len);
  523. }
  524. else
  525. {
  526. result = luat_uart_write(id, (char*)buf, len);
  527. }
  528. lua_pushinteger(L, result);
  529. #else
  530. #ifdef LUAT_USE_DRV_UART
  531. result = luat_drv_uart_write(id, (char*)buf, len);
  532. #else
  533. result = luat_uart_write(id, (char*)buf, len);
  534. #endif
  535. lua_pushinteger(L, result);
  536. #endif
  537. return 1;
  538. }
  539. /*
  540. FIFO方式取出串口缓存的数据
  541. @api uart.read(id, len)
  542. @int 串口id, uart0写0, uart1写1
  543. @int 期望读取长度,可选,不填时读取全部,无论写多少,最多读取当前缓存区所有数据
  544. @file/zbuff 可选:文件句柄或zbuff对象
  545. @return string 读取到的数据 / 传入zbuff时,返回读到的长度,并把zbuff指针后移
  546. @usage
  547. uart.read(1, 16)
  548. */
  549. static int l_uart_read(lua_State *L)
  550. {
  551. uint8_t id = luaL_checkinteger(L, 1);
  552. uint32_t length = luaL_optinteger(L, 2, 1024);
  553. int result = 0;
  554. if(lua_isuserdata(L, 3)){//zbuff对象特殊处理
  555. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE));
  556. uint8_t* recv = buff->addr+buff->cursor;
  557. if(length > buff->len - buff->cursor) {
  558. length = buff->len - buff->cursor;
  559. }
  560. #ifdef LUAT_USE_SOFT_UART
  561. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  562. {
  563. result = luat_uart_soft_read(recv, length);
  564. }
  565. else
  566. {
  567. result = luat_uart_read(id, recv, length);
  568. }
  569. #else
  570. #ifdef LUAT_USE_DRV_UART
  571. result = luat_drv_uart_read(id, recv, length);
  572. #else
  573. result = luat_uart_read(id, recv, length);
  574. #endif
  575. #endif
  576. if(result < 0) {
  577. result = 0;
  578. }
  579. buff->cursor += result;
  580. lua_pushinteger(L, result);
  581. return 1;
  582. }
  583. // 不再限制最小读取数量
  584. // if (length < 512)
  585. // length = 512;
  586. // 若读取0字节, 直接返回空串
  587. if (length < 1) {
  588. lua_pushliteral(L, "");
  589. return 1;
  590. }
  591. // 限制最大读取量,否则容易死机
  592. if (length > LUAT_UART_RX_MAX_LEN) {
  593. LLOGE("uart read length too much %u, change to %u", length, LUAT_UART_RX_MAX_LEN);
  594. length = LUAT_UART_RX_MAX_LEN;
  595. }
  596. uint8_t tmpbuff[128];
  597. uint8_t* recv = tmpbuff;
  598. uint8_t* rr = NULL;
  599. if (length > 128) { // 如果读取量比较大,就malloc
  600. rr = luat_heap_malloc(length);
  601. recv = rr;
  602. }
  603. if (recv == NULL) {
  604. LLOGE("system is out of memory!!!");
  605. lua_pushstring(L, "");
  606. return 1;
  607. }
  608. uint32_t read_length = 0;
  609. while(read_length < length)//循环读完
  610. {
  611. #ifdef LUAT_USE_SOFT_UART
  612. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  613. {
  614. result = luat_uart_soft_read((void*)(recv + read_length), length - read_length);
  615. }
  616. else
  617. {
  618. result = luat_uart_read(id, (void*)(recv + read_length), length - read_length);
  619. }
  620. #else
  621. #ifdef LUAT_USE_DRV_UART
  622. result = luat_drv_uart_read(id, (void*)(recv + read_length), length - read_length);
  623. #else
  624. result = luat_uart_read(id, (void*)(recv + read_length), length - read_length);
  625. #endif
  626. #endif
  627. if (result > 0) {
  628. read_length += result;
  629. }
  630. else
  631. {
  632. break;
  633. }
  634. }
  635. if(read_length > 0)
  636. {
  637. if (lua_isinteger(L, 3)) {
  638. uint32_t fd = luaL_checkinteger(L, 3);
  639. luat_fs_fwrite(recv, 1, read_length, (FILE*)fd);
  640. }
  641. else {
  642. lua_pushlstring(L, (const char*)recv, read_length);
  643. }
  644. }
  645. else
  646. {
  647. lua_pushstring(L, "");
  648. }
  649. if (rr != NULL)
  650. luat_heap_free(rr);
  651. return 1;
  652. }
  653. /*
  654. 关闭串口
  655. @api uart.close(id)
  656. @int 串口id, uart0写0, uart1写1
  657. @return nil 无返回值
  658. @usage
  659. uart.close(1)
  660. */
  661. static int l_uart_close(lua_State *L)
  662. {
  663. uint8_t id = luaL_checkinteger(L,1);
  664. #ifdef LUAT_USE_SOFT_UART
  665. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  666. {
  667. luat_uart_soft_close();
  668. }
  669. else
  670. {
  671. luat_uart_close(id);
  672. }
  673. return 0;
  674. #else
  675. #ifdef LUAT_USE_DRV_UART
  676. luat_drv_uart_close(id);
  677. #else
  678. luat_uart_close(id);
  679. #endif
  680. return 0;
  681. #endif
  682. }
  683. /*
  684. 注册串口事件回调
  685. @api uart.on(id, event, func)
  686. @int 串口id, uart0写0, uart1写1
  687. @string 事件名称,目前只有"receive"/"recv":新数据接收完成或者接收缓存满了,"sent":发送结束
  688. @function 回调方法
  689. @return nil 无返回值
  690. @usage
  691. uart.on(1, "receive", function(id, len)
  692. local data = uart.read(id, len)
  693. log.info("uart", id, len, data)
  694. end)
  695. */
  696. static int l_uart_on(lua_State *L) {
  697. int uart_id = luaL_checkinteger(L, 1);
  698. int org_uart_id = uart_id;
  699. #ifdef LUAT_USE_SOFT_UART
  700. if (prv_uart_soft && (prv_uart_soft->uart_id == (uint8_t)uart_id))
  701. {
  702. ;
  703. }
  704. else
  705. {
  706. #ifndef LUAT_USE_DRV_UART
  707. if (!luat_uart_exist(uart_id)) {
  708. lua_pushliteral(L, "no such uart id");
  709. return 1;
  710. }
  711. #endif
  712. }
  713. #else
  714. #ifndef LUAT_USE_DRV_UART
  715. if (!luat_uart_exist(uart_id)) {
  716. lua_pushliteral(L, "no such uart id");
  717. return 1;
  718. }
  719. #endif
  720. #endif
  721. if (uart_id >= LUAT_VUART_ID_0)
  722. {
  723. uart_id = MAX_DEVICE_COUNT + uart_id - LUAT_VUART_ID_0;
  724. }
  725. const char* event = luaL_checkstring(L, 2);
  726. if (!strcmp("receive", event) || !strcmp("recv", event)) {
  727. if (uart_cbs[uart_id].received != 0) {
  728. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  729. uart_cbs[uart_id].received = 0;
  730. }
  731. if (lua_isfunction(L, 3)) {
  732. lua_pushvalue(L, 3);
  733. uart_cbs[uart_id].received = luaL_ref(L, LUA_REGISTRYINDEX);
  734. }
  735. }
  736. else if (!strcmp("sent", event)) {
  737. if (uart_cbs[uart_id].sent != 0) {
  738. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  739. uart_cbs[uart_id].sent = 0;
  740. }
  741. if (lua_isfunction(L, 3)) {
  742. lua_pushvalue(L, 3);
  743. uart_cbs[uart_id].sent = luaL_ref(L, LUA_REGISTRYINDEX);
  744. }
  745. }
  746. luat_setup_cb(org_uart_id, uart_cbs[uart_id].received, uart_cbs[uart_id].sent);
  747. return 0;
  748. }
  749. /*
  750. 等待485模式下TX完成,mcu不支持串口发送移位寄存器空或者类似中断时才需要,在sent事件回调后使用
  751. @api uart.wait485(id)
  752. @int 串口id, uart0写0, uart1写1
  753. @return int 等待了多少次循环才等到tx完成,用于粗劣的观察delay时间是否足够,返回不为0说明还需要放大delay
  754. */
  755. static int l_uart_wait485_tx_done(lua_State *L) {
  756. int uart_id = luaL_checkinteger(L, 1);
  757. if (!luat_uart_exist(uart_id)) {
  758. lua_pushinteger(L, 0);
  759. return 1;
  760. }
  761. #ifdef LUAT__UART_TX_NEED_WAIT_DONE
  762. lua_pushinteger(L, luat_uart_wait_485_tx_done(uart_id));
  763. #else
  764. lua_pushinteger(L, 0);
  765. #endif
  766. return 1;
  767. }
  768. /*
  769. 检查串口号是否存在
  770. @api uart.exist(id)
  771. @int 串口id, uart0写0, uart1写1, 如此类推
  772. @return bool 存在返回true
  773. */
  774. static int l_uart_exist(lua_State *L)
  775. {
  776. #ifdef LUAT_USE_SOFT_UART
  777. uint8_t id = luaL_checkinteger(L,1);
  778. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  779. {
  780. lua_pushboolean(L, 1);
  781. }
  782. else
  783. {
  784. lua_pushboolean(L, luat_uart_exist(id));
  785. }
  786. return 1;
  787. #else
  788. #ifdef LUAT_USE_DRV_UART
  789. lua_pushboolean(L, luaL_checkinteger(L,1) <= LUAT_VUART_ID_0);
  790. #else
  791. lua_pushboolean(L, luat_uart_exist(luaL_checkinteger(L,1)));
  792. #endif
  793. return 1;
  794. #endif
  795. }
  796. /*
  797. buff形式读串口,一次读出全部数据存入buff中,如果buff空间不够会自动扩展,目前air105,Air780EXXX支持这个操作
  798. @api uart.rx(id, buff)
  799. @int 串口id, uart0写0, uart1写1
  800. @zbuff zbuff对象
  801. @return int 返回读到的长度,并把zbuff指针后移
  802. @usage
  803. uart.rx(1, buff)
  804. */
  805. static int l_uart_rx(lua_State *L)
  806. {
  807. uint8_t id = luaL_checkinteger(L, 1);
  808. int result;
  809. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  810. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  811. #ifdef LUAT_USE_SOFT_UART
  812. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  813. {
  814. result = prv_uart_soft->rx_buffer.Pos;
  815. }
  816. else
  817. {
  818. result = luat_uart_read(id, NULL, 0);
  819. }
  820. #else
  821. #ifdef LUAT_USE_DRV_UART
  822. result = luat_drv_uart_read(id, NULL, 0);
  823. #else
  824. result = luat_uart_read(id, NULL, 0);
  825. #endif
  826. #endif
  827. if (result > (buff->len - buff->used))
  828. {
  829. __zbuff_resize(buff, buff->len + result);
  830. }
  831. #ifdef LUAT_USE_SOFT_UART
  832. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  833. {
  834. luat_uart_soft_read(buff->addr + buff->used, result);
  835. }
  836. else
  837. {
  838. luat_uart_read(id, buff->addr + buff->used, result);
  839. }
  840. #else
  841. #ifdef LUAT_USE_DRV_UART
  842. luat_drv_uart_read(id, buff->addr + buff->used, result);
  843. #else
  844. luat_uart_read(id, buff->addr + buff->used, result);
  845. #endif
  846. #endif
  847. lua_pushinteger(L, result);
  848. buff->used += result;
  849. return 1;
  850. }
  851. else
  852. {
  853. lua_pushinteger(L, 0);
  854. return 1;
  855. }
  856. return 1;
  857. }
  858. /*
  859. 读串口Rx缓存中剩余数据量,目前air105,Air780EXXX支持这个操作
  860. @api uart.rxSize(id)
  861. @int 串口id, uart0写0, uart1写1
  862. @return int 返回读到的长度
  863. @usage
  864. local size = uart.rxSize(1)
  865. */
  866. static int l_uart_rx_size(lua_State *L)
  867. {
  868. uint8_t id = luaL_checkinteger(L, 1);
  869. int result;
  870. #ifdef LUAT_USE_SOFT_UART
  871. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  872. {
  873. result = prv_uart_soft->rx_buffer.Pos;
  874. }
  875. else
  876. {
  877. result = luat_uart_read(id, NULL, 0);
  878. }
  879. lua_pushinteger(L, result);
  880. #else
  881. #ifdef LUAT_USE_DRV_UART
  882. result = luat_drv_uart_read(id, NULL, 0);
  883. #else
  884. result = luat_uart_read(id, NULL, 0);
  885. #endif
  886. lua_pushinteger(L, result);
  887. #endif
  888. return 1;
  889. }
  890. LUAT_WEAK void luat_uart_clear_rx_cache(int uart_id)
  891. {
  892. }
  893. /*
  894. 清除串口Rx缓存中剩余数据量,目前air105,Air780EXXX支持这个操作
  895. @api uart.rxClear(id)
  896. @int 串口id, uart0写0, uart1写1
  897. @usage
  898. uart.rxClear(1)
  899. */
  900. static int l_uart_rx_clear(lua_State *L)
  901. {
  902. uint8_t id = luaL_checkinteger(L, 1);
  903. #ifdef LUAT_USE_SOFT_UART
  904. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  905. {
  906. prv_uart_soft->rx_buffer.Pos = 0;
  907. }
  908. else
  909. {
  910. luat_uart_clear_rx_cache(id);
  911. }
  912. #else
  913. luat_uart_clear_rx_cache(id);
  914. #endif
  915. return 0;
  916. }
  917. /*
  918. buff形式写串口,等同于c语言uart_tx(uart_id, &buff[start], len);
  919. @api uart.tx(id, buff, start, len)
  920. @int 串口id, uart0写0, uart1写1
  921. @zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  922. @int 可选,要发送的数据起始位置,默认为0
  923. @int 可选,要发送的数据长度,默认为zbuff内有效数据,最大值不超过zbuff的最大空间
  924. @return int 成功的数据长度
  925. @usage
  926. uart.tx(1, buf)
  927. */
  928. static int l_uart_tx(lua_State *L)
  929. {
  930. size_t start, len;
  931. // const char *buf;
  932. luat_zbuff_t *buff;
  933. int result;
  934. uint8_t id = luaL_checkinteger(L, 1);
  935. if(lua_isuserdata(L, 2))
  936. {
  937. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  938. }
  939. else
  940. {
  941. lua_pushinteger(L, 0);
  942. return 1;
  943. }
  944. start = luaL_optinteger(L, 3, 0);
  945. len = luaL_optinteger(L, 4, buff->used);
  946. if (start >= buff->len)
  947. {
  948. lua_pushinteger(L, 0);
  949. return 1;
  950. }
  951. if ((start + len)>= buff->len)
  952. {
  953. len = buff->len - start;
  954. }
  955. #ifdef LUAT_USE_SOFT_UART
  956. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  957. {
  958. result = luat_uart_soft_write((const uint8_t*)(buff->addr + start), len);
  959. }
  960. else
  961. {
  962. result = luat_uart_write(id, buff->addr + start, len);
  963. }
  964. lua_pushinteger(L, result);
  965. #else
  966. #ifdef LUAT_USE_DRV_UART
  967. result = luat_drv_uart_write(id, buff->addr + start, len);
  968. #else
  969. result = luat_uart_write(id, buff->addr + start, len);
  970. #endif
  971. lua_pushinteger(L, result);
  972. #endif
  973. return 1;
  974. }
  975. #ifdef LUAT_USE_SOFT_UART
  976. static int l_uart_soft_handler_tx_done(lua_State *L, void* ptr)
  977. {
  978. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  979. lua_pop(L, 1);
  980. if (prv_uart_soft->is_inited)
  981. {
  982. luat_uart_soft_tx_node_t *node = (luat_uart_soft_tx_node_t *)(prv_uart_soft->tx_queue_head.next);
  983. llist_del(&node->tx_node);
  984. luat_heap_alloc(NULL, node->data, 0, 0);
  985. luat_heap_alloc(NULL, node, 0, 0);
  986. if (llist_empty(&prv_uart_soft->tx_queue_head))
  987. {
  988. Buffer_StaticInit(&prv_uart_soft->tx_buffer, NULL, 0);
  989. prv_uart_soft->is_tx_busy = 0;
  990. if (!prv_uart_soft->is_rx_busy)
  991. {
  992. luat_uart_soft_sleep_enable(1);
  993. }
  994. if (prv_uart_soft->pin485 != 0xff)
  995. {
  996. luat_gpio_set(prv_uart_soft->pin485, prv_uart_soft->rs485_rx_level);
  997. }
  998. if (uart_cbs[prv_uart_soft->uart_id].sent) {
  999. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[prv_uart_soft->uart_id].sent);
  1000. if (lua_isfunction(L, -1)) {
  1001. lua_pushinteger(L, prv_uart_soft->uart_id);
  1002. lua_call(L, 1, 0);
  1003. }
  1004. }
  1005. }
  1006. else
  1007. {
  1008. node = (luat_uart_soft_tx_node_t *)prv_uart_soft->tx_queue_head.next;
  1009. Buffer_StaticInit(&prv_uart_soft->tx_buffer, node->data, node->len);
  1010. prv_uart_soft->tx_shift_bits = 0;
  1011. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  1012. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  1013. if (prv_uart_soft->parity)
  1014. {
  1015. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | luat_uart_soft_check_party(prv_uart_soft->tx_buffer.Data[0], prv_uart_soft->data_bits, prv_uart_soft->parity_odd) | prv_uart_soft->tx_buffer.Data[0];
  1016. }
  1017. else
  1018. {
  1019. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[0];
  1020. }
  1021. }
  1022. }
  1023. lua_pushinteger(L, 0);
  1024. return 1;
  1025. }
  1026. static int l_uart_soft_handler_rx_done(lua_State *L, void* ptr)
  1027. {
  1028. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  1029. lua_pop(L, 1);
  1030. if (prv_uart_soft->is_inited)
  1031. {
  1032. // if (msg->ptr && msg->arg1)
  1033. // {
  1034. // if (((uint32_t)msg->arg1 + prv_uart_soft->rx_buffer.Pos) > prv_uart_soft->rx_buffer.MaxLen)
  1035. // {
  1036. // uint8_t *new = luat_heap_alloc(NULL, NULL, 0, (prv_uart_soft->rx_buffer.MaxLen + (uint32_t)msg->arg1) * 2);
  1037. // if (new)
  1038. // {
  1039. // prv_uart_soft->rx_buffer.MaxLen = (prv_uart_soft->rx_buffer.MaxLen + (uint32_t)msg->arg1) * 2;
  1040. // memcpy(new, prv_uart_soft->rx_buffer.Data, prv_uart_soft->rx_buffer.Pos);
  1041. // luat_heap_alloc(NULL, prv_uart_soft->rx_buffer.Data, 0, 0);
  1042. // prv_uart_soft->rx_buffer.Data = new;
  1043. // memcpy(prv_uart_soft->rx_buffer.Data + prv_uart_soft->rx_buffer.Pos, msg->ptr, (uint32_t)msg->arg1);
  1044. // prv_uart_soft->rx_buffer.Pos += (uint32_t)msg->arg1;
  1045. // }
  1046. // else
  1047. // {
  1048. // LLOGE("soft uart resize no mem!");
  1049. // }
  1050. // }
  1051. // else
  1052. // {
  1053. // memcpy(prv_uart_soft->rx_buffer.Data + prv_uart_soft->rx_buffer.Pos, msg->ptr, (uint32_t)msg->arg1);
  1054. // prv_uart_soft->rx_buffer.Pos += (uint32_t)msg->arg1;
  1055. // }
  1056. // }
  1057. // LLOGD("%d,%d", prv_uart_soft->rx_buffer.Pos, msg->arg2);
  1058. if (prv_uart_soft->rx_buffer.Pos || msg->arg2)
  1059. {
  1060. if (uart_app_recvs[prv_uart_soft->uart_id]) {
  1061. uart_app_recvs[prv_uart_soft->uart_id](prv_uart_soft->uart_id, msg->arg2);
  1062. }
  1063. if (uart_cbs[prv_uart_soft->uart_id].received) {
  1064. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[prv_uart_soft->uart_id].received);
  1065. if (lua_isfunction(L, -1)) {
  1066. lua_pushinteger(L, prv_uart_soft->uart_id);
  1067. lua_pushinteger(L, prv_uart_soft->rx_buffer.Pos);
  1068. lua_call(L, 2, 0);
  1069. }
  1070. }
  1071. }
  1072. if (msg->arg2)
  1073. {
  1074. prv_uart_soft->is_rx_busy = 0;
  1075. if (!prv_uart_soft->is_tx_busy)
  1076. {
  1077. luat_uart_soft_sleep_enable(1);
  1078. }
  1079. }
  1080. }
  1081. // if (msg->ptr)
  1082. // {
  1083. // luat_heap_alloc(NULL, msg->ptr, 0, 0);
  1084. // }
  1085. lua_pushinteger(L, 0);
  1086. return 1;
  1087. }
  1088. static void __LUAT_C_CODE_IN_RAM__ luat_uart_soft_send_hwtimer_irq(void)
  1089. {
  1090. if (prv_uart_soft->tx_shift_bits >= prv_uart_soft->total_bits)
  1091. {
  1092. //发送完了
  1093. if (prv_uart_soft->tx_buffer.Pos >= prv_uart_soft->tx_buffer.MaxLen)
  1094. {
  1095. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, 0);
  1096. rtos_msg_t msg;
  1097. msg.handler = l_uart_soft_handler_tx_done;
  1098. msg.ptr = NULL;
  1099. msg.arg1 = NULL;
  1100. msg.arg2 = NULL;
  1101. luat_msgbus_put(&msg, 0);
  1102. }
  1103. else
  1104. {
  1105. //发送新的字节的起始位
  1106. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  1107. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  1108. prv_uart_soft->tx_shift_bits = 0;
  1109. }
  1110. return;
  1111. }
  1112. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, (prv_uart_soft->tx_bit >> prv_uart_soft->tx_shift_bits) & 0x01);
  1113. prv_uart_soft->tx_shift_bits++;
  1114. if (prv_uart_soft->tx_shift_bits > prv_uart_soft->data_bits)
  1115. {
  1116. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->stop_period);
  1117. prv_uart_soft->tx_shift_bits = prv_uart_soft->total_bits;
  1118. prv_uart_soft->tx_buffer.Pos++;
  1119. if (prv_uart_soft->tx_buffer.Pos < prv_uart_soft->tx_buffer.MaxLen)
  1120. {
  1121. if (prv_uart_soft->parity)
  1122. {
  1123. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | luat_uart_soft_check_party(prv_uart_soft->tx_buffer.Data[prv_uart_soft->tx_buffer.Pos], prv_uart_soft->data_bits, prv_uart_soft->parity_odd) | prv_uart_soft->tx_buffer.Data[prv_uart_soft->tx_buffer.Pos];
  1124. }
  1125. else
  1126. {
  1127. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[prv_uart_soft->tx_buffer.Pos];
  1128. }
  1129. }
  1130. }
  1131. }
  1132. static void __LUAT_C_CODE_IN_RAM__ luat_uart_soft_recv_hwtimer_irq(void)
  1133. {
  1134. uint8_t bit = luat_uart_soft_gpio_fast_input(prv_uart_soft->rx_pin);
  1135. uint8_t is_end = 0;
  1136. if (!prv_uart_soft->rx_shift_bits) //检测起始位
  1137. {
  1138. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->rx_period);
  1139. prv_uart_soft->rx_bit = bit;
  1140. prv_uart_soft->rx_shift_bits++;
  1141. prv_uart_soft->rx_parity_bit += bit;
  1142. return ;
  1143. }
  1144. else if (0xef == prv_uart_soft->rx_shift_bits) //RX检测超时了,没有新的起始位
  1145. {
  1146. is_end = 1;
  1147. goto UART_SOFT_RX_DONE;
  1148. }
  1149. if (prv_uart_soft->rx_shift_bits < prv_uart_soft->data_bits)
  1150. {
  1151. prv_uart_soft->rx_bit |= (bit << prv_uart_soft->rx_shift_bits);
  1152. prv_uart_soft->rx_shift_bits++;
  1153. prv_uart_soft->rx_parity_bit += bit;
  1154. if (prv_uart_soft->rx_shift_bits >= prv_uart_soft->data_bits)
  1155. {
  1156. if (!prv_uart_soft->parity) //如果不做奇偶校验,就直接开始下一个字节
  1157. {
  1158. goto UART_SOFT_RX_BYTE_DONE;
  1159. }
  1160. }
  1161. return;
  1162. }
  1163. if ((prv_uart_soft->rx_parity_bit & 0x01) != bit) //奇偶校验错误
  1164. {
  1165. is_end = 1;
  1166. goto UART_SOFT_RX_DONE;
  1167. }
  1168. UART_SOFT_RX_BYTE_DONE:
  1169. prv_uart_soft->rx_fifo[prv_uart_soft->rx_fifo_cnt] = prv_uart_soft->rx_bit;
  1170. prv_uart_soft->rx_fifo_cnt++;
  1171. luat_uart_soft_gpio_fast_irq_set(prv_uart_soft->rx_pin, 1);
  1172. prv_uart_soft->rx_shift_bits = 0xef;
  1173. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->stop_period * 20); //这里做接收超时检测
  1174. if (prv_uart_soft->rx_fifo_cnt < LUAT_UART_SOFT_FIFO_CNT) //接收fifo没有满,继续接收
  1175. {
  1176. return;
  1177. }
  1178. UART_SOFT_RX_DONE:
  1179. if (prv_uart_soft->rx_fifo_cnt || is_end)
  1180. {
  1181. rtos_msg_t msg = {0};
  1182. msg.handler = l_uart_soft_handler_rx_done;
  1183. // msg.ptr = luat_heap_alloc(0, 0, 0, prv_uart_soft->rx_fifo_cnt);
  1184. msg.arg1 = prv_uart_soft->rx_fifo_cnt;
  1185. msg.arg2 = is_end;
  1186. // if (msg.ptr)
  1187. // {
  1188. // memcpy(msg.ptr, prv_uart_soft->rx_fifo, prv_uart_soft->rx_fifo_cnt);
  1189. // }
  1190. OS_BufferWriteLimit(&prv_uart_soft->rx_buffer, prv_uart_soft->rx_fifo, prv_uart_soft->rx_fifo_cnt);
  1191. prv_uart_soft->rx_fifo_cnt = 0;
  1192. luat_msgbus_put(&msg, 0);
  1193. }
  1194. if (is_end)
  1195. {
  1196. luat_uart_soft_gpio_fast_irq_set(prv_uart_soft->rx_pin, 1);
  1197. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, 0);
  1198. }
  1199. return;
  1200. }
  1201. #endif
  1202. /**
  1203. 设置软件uart的硬件配置,只有支持硬件定时器的SOC才能使用,目前只能设置一个,波特率根据平台的软硬件配置有不同的极限,建议9600,接收缓存不超过65535,不支持MSB,支持485自动控制。后续仍要setup操作
  1204. @api uart.createSoft(tx_pin, tx_hwtimer_id, rx_pin, rx_hwtimer_id, adjust_period)
  1205. @int 发送引脚编号
  1206. @int 发送用的硬件定时器ID
  1207. @int 接收引脚编号
  1208. @int 接收用的硬件定时器ID
  1209. @int 发送时序调整,单位是定时器时钟周期,默认是0,需要根据示波器或者逻辑分析仪进行微调
  1210. @int 接收时序调整,单位是定时器时钟周期,默认是0,需要根据示波器或者逻辑分析仪进行微调
  1211. @return int 软件uart的id,如果失败则返回nil
  1212. @usage
  1213. -- 初始化软件uart
  1214. local uart_id = uart.createSoft(21, 0, 1, 2) --air780e建议用定时器0和2,tx_pin最好用AGPIO,防止休眠时误触发对端RX
  1215. */
  1216. static int l_uart_soft(lua_State *L) {
  1217. #ifdef LUAT_USE_SOFT_UART
  1218. if (!prv_uart_soft)
  1219. {
  1220. prv_uart_soft = luat_heap_alloc(NULL, NULL, 0, sizeof(luat_uart_soft_t));
  1221. if (prv_uart_soft)
  1222. {
  1223. memset(prv_uart_soft, 0, sizeof(luat_uart_soft_t));
  1224. INIT_LLIST_HEAD(&prv_uart_soft->tx_queue_head);
  1225. prv_uart_soft->uart_id = 0xff;
  1226. }
  1227. else
  1228. {
  1229. lua_pushnil(L);
  1230. goto CREATE_DONE;
  1231. }
  1232. }
  1233. if (prv_uart_soft->is_inited)
  1234. {
  1235. lua_pushnil(L);
  1236. goto CREATE_DONE;
  1237. }
  1238. for(int uart_id = 1; uart_id < MAX_DEVICE_COUNT; uart_id++)
  1239. {
  1240. if (!luat_uart_exist(uart_id))
  1241. {
  1242. LLOGD("find free uart id, %d", uart_id);
  1243. prv_uart_soft->is_inited = 1;
  1244. prv_uart_soft->uart_id = uart_id;
  1245. break;
  1246. }
  1247. }
  1248. if (!prv_uart_soft->is_inited)
  1249. {
  1250. lua_pushnil(L);
  1251. goto CREATE_DONE;
  1252. }
  1253. prv_uart_soft->tx_pin = luaL_optinteger(L, 1, 0xff);
  1254. prv_uart_soft->tx_hwtimer_id = luaL_optinteger(L, 2, 0xff);
  1255. prv_uart_soft->rx_pin = luaL_optinteger(L, 3, 0xff);
  1256. prv_uart_soft->rx_hwtimer_id = luaL_optinteger(L, 4, 0xff);
  1257. prv_uart_soft->tx_adjust_period = luaL_optinteger(L, 5, 0);
  1258. prv_uart_soft->rx_adjust_period = luaL_optinteger(L, 6, 0);
  1259. if (luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, luat_uart_soft_send_hwtimer_irq))
  1260. {
  1261. prv_uart_soft->is_inited = 0;
  1262. }
  1263. if (luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->rx_hwtimer_id, luat_uart_soft_recv_hwtimer_irq))
  1264. {
  1265. luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, NULL);
  1266. prv_uart_soft->is_inited = 0;
  1267. }
  1268. if (!prv_uart_soft->is_inited)
  1269. {
  1270. lua_pushnil(L);
  1271. goto CREATE_DONE;
  1272. }
  1273. lua_pushinteger(L, prv_uart_soft->uart_id);
  1274. #else
  1275. LLOGE("not support soft uart");
  1276. lua_pushnil(L);
  1277. #endif
  1278. #ifdef LUAT_USE_SOFT_UART
  1279. CREATE_DONE:
  1280. #endif
  1281. return 1;
  1282. }
  1283. /*
  1284. 获取可用串口号列表,当前仅限win32
  1285. @api uart.list(max)
  1286. @int 可选,默认256,最多获取多少个串口
  1287. @return table 获取到的可用串口号列表
  1288. */
  1289. #ifdef LUAT_FORCE_WIN32
  1290. static int l_uart_list(lua_State *L)
  1291. {
  1292. size_t len = luaL_optinteger(L,1,256);
  1293. lua_newtable(L);//返回用的table
  1294. uint8_t* buff = (uint8_t*)luat_heap_malloc(len);
  1295. if (!buff)
  1296. return 1;
  1297. int rlen = luat_uart_list(buff, len);
  1298. for(int i = 0;i<rlen;i++)
  1299. {
  1300. lua_pushinteger(L,i+1);
  1301. lua_pushinteger(L,buff[i]);
  1302. lua_settable(L,-3);
  1303. }
  1304. luat_heap_free(buff);
  1305. return 1;
  1306. }
  1307. #endif
  1308. LUAT_WEAK void luat_uart_patch(int *param) {;}
  1309. static int l_uart_patch(lua_State *L)
  1310. {
  1311. int param[6];
  1312. for(int i = 1; i <= 6; i++)
  1313. {
  1314. param[i - 1] = luaL_optinteger(L, i, 0);
  1315. }
  1316. luat_uart_patch(param);
  1317. return 0;
  1318. }
  1319. #include "rotable2.h"
  1320. static const rotable_Reg_t reg_uart[] =
  1321. {
  1322. { "write", ROREG_FUNC(l_uart_write)},
  1323. { "read", ROREG_FUNC(l_uart_read)},
  1324. { "wait485", ROREG_FUNC(l_uart_wait485_tx_done)},
  1325. { "tx", ROREG_FUNC(l_uart_tx)},
  1326. { "rx", ROREG_FUNC(l_uart_rx)},
  1327. { "rxClear", ROREG_FUNC(l_uart_rx_clear)},
  1328. { "rxSize", ROREG_FUNC(l_uart_rx_size)},
  1329. { "rx_size", ROREG_FUNC(l_uart_rx_size)},
  1330. { "createSoft", ROREG_FUNC(l_uart_soft)},
  1331. { "close", ROREG_FUNC(l_uart_close)},
  1332. { "on", ROREG_FUNC(l_uart_on)},
  1333. { "setup", ROREG_FUNC(l_uart_setup)},
  1334. { "exist", ROREG_FUNC(l_uart_exist)},
  1335. { "patch", ROREG_FUNC(l_uart_patch)},
  1336. #ifdef LUAT_FORCE_WIN32
  1337. { "list", ROREG_FUNC(l_uart_list)},
  1338. #endif
  1339. //@const Odd number 奇校验,大小写兼容性
  1340. { "Odd", ROREG_INT(LUAT_PARITY_ODD)},
  1341. //@const Even number 偶校验,大小写兼容性
  1342. { "Even", ROREG_INT(LUAT_PARITY_EVEN)},
  1343. //@const None number 无校验,大小写兼容性
  1344. { "None", ROREG_INT(LUAT_PARITY_NONE)},
  1345. //@const ODD number 奇校验
  1346. { "ODD", ROREG_INT(LUAT_PARITY_ODD)},
  1347. //@const EVEN number 偶校验
  1348. { "EVEN", ROREG_INT(LUAT_PARITY_EVEN)},
  1349. //@const NONE number 无校验
  1350. { "NONE", ROREG_INT(LUAT_PARITY_NONE)},
  1351. //高低位顺序
  1352. //@const LSB number 小端模式
  1353. { "LSB", ROREG_INT(LUAT_BIT_ORDER_LSB)},
  1354. //@const MSB number 大端模式
  1355. { "MSB", ROREG_INT(LUAT_BIT_ORDER_MSB)},
  1356. //@const VUART_0 number 虚拟串口0
  1357. { "VUART_0", ROREG_INT(LUAT_VUART_ID_0)},
  1358. //@const VUART_1 number 虚拟串口1
  1359. { "VUART_1", ROREG_INT(LUAT_VUART_ID_1)},
  1360. //@const VUART_2 number 虚拟串口2
  1361. { "VUART_2", ROREG_INT(LUAT_VUART_ID_2)},
  1362. //@const ERROR_DROP number 遇到错误时抛弃缓存的数据
  1363. { "ERROR_DROP", ROREG_INT(LUAT_UART_RX_ERROR_DROP_DATA)},
  1364. //@const DEBUG number 开启调试功能
  1365. { "DEBUG", ROREG_INT(LUAT_UART_DEBUG_ENABLE)},
  1366. { NULL, ROREG_INT(0) }
  1367. };
  1368. LUAMOD_API int luaopen_uart(lua_State *L)
  1369. {
  1370. luat_newlib2(L, reg_uart);
  1371. return 1;
  1372. }