luat_lib_uart.c 37 KB

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