luat_lib_uart.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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(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. if (length < 512)
  484. length = 512;
  485. uint8_t* recv = luat_heap_malloc(length);
  486. if (recv == NULL) {
  487. LLOGE("system is out of memory!!!");
  488. lua_pushstring(L, "");
  489. return 1;
  490. }
  491. uint32_t read_length = 0;
  492. while(read_length < length)//循环读完
  493. {
  494. #ifdef LUAT_USE_SOFT_UART
  495. int result;
  496. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  497. {
  498. result = luat_uart_soft_read((void*)(recv + read_length), length - read_length);
  499. }
  500. else
  501. {
  502. result = luat_uart_read(id, (void*)(recv + read_length), length - read_length);
  503. }
  504. #else
  505. int result = luat_uart_read(id, (void*)(recv + read_length), length - read_length);
  506. #endif
  507. if (result > 0) {
  508. read_length += result;
  509. }
  510. else
  511. {
  512. break;
  513. }
  514. }
  515. if(read_length > 0)
  516. {
  517. if (lua_isinteger(L, 3)) {
  518. uint32_t fd = luaL_checkinteger(L, 3);
  519. luat_fs_fwrite(recv, 1, read_length, (FILE*)fd);
  520. }
  521. else {
  522. lua_pushlstring(L, (const char*)recv, read_length);
  523. }
  524. }
  525. else
  526. {
  527. lua_pushstring(L, "");
  528. }
  529. luat_heap_free(recv);
  530. return 1;
  531. }
  532. /*
  533. 关闭串口
  534. @api uart.close(id)
  535. @int 串口id, uart0写0, uart1写1
  536. @return nil 无返回值
  537. @usage
  538. uart.close(1)
  539. */
  540. static int l_uart_close(lua_State *L)
  541. {
  542. #ifdef LUAT_USE_SOFT_UART
  543. uint8_t id = luaL_checkinteger(L,1);
  544. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  545. {
  546. luat_uart_soft_close();
  547. }
  548. else
  549. {
  550. luat_uart_close(id);
  551. }
  552. return 0;
  553. #else
  554. // uint8_t result = luat_uart_close(luaL_checkinteger(L, 1));
  555. // lua_pushinteger(L, result);
  556. luat_uart_close(luaL_checkinteger(L, 1));
  557. return 0;
  558. #endif
  559. }
  560. /*
  561. 注册串口事件回调
  562. @api uart.on(id, event, func)
  563. @int 串口id, uart0写0, uart1写1
  564. @string 事件名称
  565. @function 回调方法
  566. @return nil 无返回值
  567. @usage
  568. uart.on(1, "receive", function(id, len)
  569. local data = uart.read(id, len)
  570. log.info("uart", id, len, data)
  571. end)
  572. */
  573. static int l_uart_on(lua_State *L) {
  574. int uart_id = luaL_checkinteger(L, 1);
  575. int org_uart_id = uart_id;
  576. #ifdef LUAT_USE_SOFT_UART
  577. if (prv_uart_soft && (prv_uart_soft->uart_id == (uint8_t)uart_id))
  578. {
  579. ;
  580. }
  581. else
  582. {
  583. if (!luat_uart_exist(uart_id)) {
  584. lua_pushliteral(L, "no such uart id");
  585. return 1;
  586. }
  587. }
  588. #else
  589. if (!luat_uart_exist(uart_id)) {
  590. lua_pushliteral(L, "no such uart id");
  591. return 1;
  592. }
  593. #endif
  594. if (uart_id >= LUAT_VUART_ID_0)
  595. {
  596. uart_id = MAX_DEVICE_COUNT + uart_id - LUAT_VUART_ID_0;
  597. }
  598. const char* event = luaL_checkstring(L, 2);
  599. if (!strcmp("receive", event) || !strcmp("recv", event)) {
  600. if (uart_cbs[uart_id].received != 0) {
  601. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  602. uart_cbs[uart_id].received = 0;
  603. }
  604. if (lua_isfunction(L, 3)) {
  605. lua_pushvalue(L, 3);
  606. uart_cbs[uart_id].received = luaL_ref(L, LUA_REGISTRYINDEX);
  607. }
  608. }
  609. else if (!strcmp("sent", event)) {
  610. if (uart_cbs[uart_id].sent != 0) {
  611. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  612. uart_cbs[uart_id].sent = 0;
  613. }
  614. if (lua_isfunction(L, 3)) {
  615. lua_pushvalue(L, 3);
  616. uart_cbs[uart_id].sent = luaL_ref(L, LUA_REGISTRYINDEX);
  617. }
  618. }
  619. luat_setup_cb(org_uart_id, uart_cbs[uart_id].received, uart_cbs[uart_id].sent);
  620. return 0;
  621. }
  622. /*
  623. 等待485模式下TX完成,mcu不支持串口发送移位寄存器空或者类似中断时才需要,在sent事件回调后使用
  624. @api uart.wait485(id)
  625. @int 串口id, uart0写0, uart1写1
  626. @return int 等待了多少次循环才等到tx完成,用于粗劣的观察delay时间是否足够,返回不为0说明还需要放大delay
  627. */
  628. static int l_uart_wait485_tx_done(lua_State *L) {
  629. int uart_id = luaL_checkinteger(L, 1);
  630. if (!luat_uart_exist(uart_id)) {
  631. lua_pushinteger(L, 0);
  632. return 1;
  633. }
  634. #ifdef LUAT__UART_TX_NEED_WAIT_DONE
  635. lua_pushinteger(L, luat_uart_wait_485_tx_done(uart_id));
  636. #else
  637. lua_pushinteger(L, 0);
  638. #endif
  639. return 1;
  640. }
  641. /*
  642. 检查串口号是否存在
  643. @api uart.exist(id)
  644. @int 串口id, uart0写0, uart1写1, 如此类推
  645. @return bool 存在返回true
  646. */
  647. static int l_uart_exist(lua_State *L)
  648. {
  649. #ifdef LUAT_USE_SOFT_UART
  650. uint8_t id = luaL_checkinteger(L,1);
  651. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  652. {
  653. lua_pushboolean(L, 1);
  654. }
  655. else
  656. {
  657. lua_pushboolean(L, luat_uart_exist(id));
  658. }
  659. return 1;
  660. #else
  661. lua_pushboolean(L, luat_uart_exist(luaL_checkinteger(L,1)));
  662. return 1;
  663. #endif
  664. }
  665. /*
  666. buff形式读串口,一次读出全部数据存入buff中,如果buff空间不够会自动扩展,目前air105,air780e支持这个操作
  667. @api uart.rx(id, buff)
  668. @int 串口id, uart0写0, uart1写1
  669. @zbuff zbuff对象
  670. @return int 返回读到的长度,并把zbuff指针后移
  671. @usage
  672. uart.rx(1, buff)
  673. */
  674. static int l_uart_rx(lua_State *L)
  675. {
  676. uint8_t id = luaL_checkinteger(L, 1);
  677. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  678. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  679. #ifdef LUAT_USE_SOFT_UART
  680. int result;
  681. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  682. {
  683. result = prv_uart_soft->rx_buffer.Pos;
  684. }
  685. else
  686. {
  687. result = luat_uart_read(id, NULL, 0);
  688. }
  689. #else
  690. int result = luat_uart_read(id, NULL, 0);
  691. #endif
  692. if (result > (buff->len - buff->used))
  693. {
  694. __zbuff_resize(buff, buff->len + result);
  695. }
  696. #ifdef LUAT_USE_SOFT_UART
  697. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  698. {
  699. luat_uart_soft_read(buff->addr + buff->used, result);
  700. }
  701. else
  702. {
  703. luat_uart_read(id, buff->addr + buff->used, result);
  704. }
  705. #else
  706. luat_uart_read(id, buff->addr + buff->used, result);
  707. #endif
  708. lua_pushinteger(L, result);
  709. buff->used += result;
  710. return 1;
  711. }
  712. else
  713. {
  714. lua_pushinteger(L, 0);
  715. return 1;
  716. }
  717. return 1;
  718. }
  719. /*
  720. 读串口Rx缓存中剩余数据量,目前air105,air780e支持这个操作
  721. @api uart.rxSize(id)
  722. @int 串口id, uart0写0, uart1写1
  723. @return int 返回读到的长度
  724. @usage
  725. local size = uart.rxSize(1)
  726. */
  727. static int l_uart_rx_size(lua_State *L)
  728. {
  729. uint8_t id = luaL_checkinteger(L, 1);
  730. #ifdef LUAT_USE_SOFT_UART
  731. int result;
  732. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  733. {
  734. result = prv_uart_soft->rx_buffer.Pos;
  735. }
  736. else
  737. {
  738. result = luat_uart_read(id, NULL, 0);
  739. }
  740. lua_pushinteger(L, result);
  741. #else
  742. lua_pushinteger(L, luat_uart_read(id, NULL, 0));
  743. #endif
  744. return 1;
  745. }
  746. /*
  747. buff形式写串口,等同于c语言uart_tx(uart_id, &buff[start], len);
  748. @api uart.tx(id, buff, start, len)
  749. @int 串口id, uart0写0, uart1写1
  750. @zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  751. @int 可选,要发送的数据起始位置,默认为0
  752. @int 可选,要发送的数据长度,默认为zbuff内有效数据,最大值不超过zbuff的最大空间
  753. @return int 成功的数据长度
  754. @usage
  755. uart.tx(1, buf)
  756. */
  757. static int l_uart_tx(lua_State *L)
  758. {
  759. size_t start, len;
  760. // const char *buf;
  761. luat_zbuff_t *buff;
  762. uint8_t id = luaL_checkinteger(L, 1);
  763. if(lua_isuserdata(L, 2))
  764. {
  765. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  766. }
  767. else
  768. {
  769. lua_pushinteger(L, 0);
  770. return 1;
  771. }
  772. start = luaL_optinteger(L, 3, 0);
  773. len = luaL_optinteger(L, 4, buff->used);
  774. if (start >= buff->len)
  775. {
  776. lua_pushinteger(L, 0);
  777. return 1;
  778. }
  779. if ((start + len)>= buff->len)
  780. {
  781. len = buff->len - start;
  782. }
  783. #ifdef LUAT_USE_SOFT_UART
  784. int result;
  785. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  786. {
  787. result = luat_uart_soft_write(buff->addr + start, len);
  788. }
  789. else
  790. {
  791. result = luat_uart_write(id, buff->addr + start, len);
  792. }
  793. lua_pushinteger(L, result);
  794. #else
  795. int result = luat_uart_write(id, buff->addr + start, len);
  796. lua_pushinteger(L, result);
  797. #endif
  798. return 1;
  799. }
  800. #ifdef LUAT_USE_SOFT_UART
  801. static int l_uart_soft_handler_tx_done(lua_State *L, void* ptr)
  802. {
  803. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  804. lua_pop(L, 1);
  805. if (prv_uart_soft->is_inited)
  806. {
  807. luat_uart_soft_tx_node_t *node = (luat_uart_soft_tx_node_t *)(prv_uart_soft->tx_queue_head.next);
  808. llist_del(&node->tx_node);
  809. luat_heap_alloc(NULL, node->data, 0, 0);
  810. luat_heap_alloc(NULL, node, 0, 0);
  811. if (llist_empty(&prv_uart_soft->tx_queue_head))
  812. {
  813. Buffer_StaticInit(&prv_uart_soft->tx_buffer, NULL, 0);
  814. prv_uart_soft->is_tx_busy = 0;
  815. if (!prv_uart_soft->is_rx_busy)
  816. {
  817. luat_uart_soft_sleep_enable(1);
  818. }
  819. if (prv_uart_soft->pin485 != 0xff)
  820. {
  821. luat_gpio_set(prv_uart_soft->pin485, prv_uart_soft->rs485_rx_level);
  822. }
  823. if (uart_cbs[prv_uart_soft->uart_id].sent) {
  824. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[prv_uart_soft->uart_id].sent);
  825. if (lua_isfunction(L, -1)) {
  826. lua_pushinteger(L, prv_uart_soft->uart_id);
  827. lua_call(L, 1, 0);
  828. }
  829. }
  830. }
  831. else
  832. {
  833. node = (luat_uart_soft_tx_node_t *)prv_uart_soft->tx_queue_head.next;
  834. Buffer_StaticInit(&prv_uart_soft->tx_buffer, node->data, node->len);
  835. prv_uart_soft->tx_shift_bits = 0;
  836. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  837. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  838. if (prv_uart_soft->parity)
  839. {
  840. 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];
  841. }
  842. else
  843. {
  844. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[0];
  845. }
  846. }
  847. }
  848. lua_pushinteger(L, 0);
  849. return 1;
  850. }
  851. static int l_uart_soft_handler_rx_done(lua_State *L, void* ptr)
  852. {
  853. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  854. lua_pop(L, 1);
  855. if (prv_uart_soft->is_inited)
  856. {
  857. if (msg->ptr && msg->arg1)
  858. {
  859. if (((uint32_t)msg->arg1 + prv_uart_soft->rx_buffer.Pos) > prv_uart_soft->rx_buffer.MaxLen)
  860. {
  861. uint8_t *new = luat_heap_alloc(NULL, NULL, 0, (prv_uart_soft->rx_buffer.MaxLen + (uint32_t)msg->arg1) * 2);
  862. if (new)
  863. {
  864. prv_uart_soft->rx_buffer.MaxLen = (prv_uart_soft->rx_buffer.MaxLen + (uint32_t)msg->arg1) * 2;
  865. memcpy(new, prv_uart_soft->rx_buffer.Data, prv_uart_soft->rx_buffer.Pos);
  866. luat_heap_alloc(NULL, prv_uart_soft->rx_buffer.Data, 0, 0);
  867. prv_uart_soft->rx_buffer.Data = new;
  868. memcpy(prv_uart_soft->rx_buffer.Data + prv_uart_soft->rx_buffer.Pos, msg->ptr, (uint32_t)msg->arg1);
  869. prv_uart_soft->rx_buffer.Pos += (uint32_t)msg->arg1;
  870. }
  871. else
  872. {
  873. LLOGE("soft uart resize no mem!");
  874. }
  875. }
  876. else
  877. {
  878. memcpy(prv_uart_soft->rx_buffer.Data + prv_uart_soft->rx_buffer.Pos, msg->ptr, (uint32_t)msg->arg1);
  879. prv_uart_soft->rx_buffer.Pos += (uint32_t)msg->arg1;
  880. }
  881. }
  882. if ((prv_uart_soft->rx_buffer.Pos > prv_uart_soft->rx_buffer_size) || msg->arg2)
  883. {
  884. if (uart_app_recvs[prv_uart_soft->uart_id]) {
  885. uart_app_recvs[prv_uart_soft->uart_id](prv_uart_soft->uart_id, msg->arg2);
  886. }
  887. if (uart_cbs[prv_uart_soft->uart_id].received) {
  888. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[prv_uart_soft->uart_id].received);
  889. if (lua_isfunction(L, -1)) {
  890. lua_pushinteger(L, prv_uart_soft->uart_id);
  891. lua_pushinteger(L, prv_uart_soft->rx_buffer.Pos);
  892. lua_call(L, 2, 0);
  893. }
  894. }
  895. }
  896. if (msg->arg2)
  897. {
  898. prv_uart_soft->is_rx_busy = 0;
  899. if (!prv_uart_soft->is_tx_busy)
  900. {
  901. luat_uart_soft_sleep_enable(1);
  902. }
  903. }
  904. }
  905. if (msg->ptr)
  906. {
  907. luat_heap_alloc(NULL, msg->ptr, 0, 0);
  908. }
  909. lua_pushinteger(L, 0);
  910. return 1;
  911. }
  912. static void __LUAT_C_CODE_IN_RAM__ luat_uart_soft_send_hwtimer_irq(void)
  913. {
  914. if (prv_uart_soft->tx_shift_bits >= prv_uart_soft->total_bits)
  915. {
  916. //发送完了
  917. if (prv_uart_soft->tx_buffer.Pos >= prv_uart_soft->tx_buffer.MaxLen)
  918. {
  919. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, 0);
  920. rtos_msg_t msg;
  921. msg.handler = l_uart_soft_handler_tx_done;
  922. msg.ptr = NULL;
  923. msg.arg1 = NULL;
  924. msg.arg2 = NULL;
  925. luat_msgbus_put(&msg, 0);
  926. }
  927. else
  928. {
  929. //发送新的字节的起始位
  930. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  931. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  932. prv_uart_soft->tx_shift_bits = 0;
  933. }
  934. return;
  935. }
  936. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, (prv_uart_soft->tx_bit >> prv_uart_soft->tx_shift_bits) & 0x01);
  937. prv_uart_soft->tx_shift_bits++;
  938. if (prv_uart_soft->tx_shift_bits > prv_uart_soft->data_bits)
  939. {
  940. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->stop_period);
  941. prv_uart_soft->tx_shift_bits = prv_uart_soft->total_bits;
  942. prv_uart_soft->tx_buffer.Pos++;
  943. if (prv_uart_soft->tx_buffer.Pos < prv_uart_soft->tx_buffer.MaxLen)
  944. {
  945. if (prv_uart_soft->parity)
  946. {
  947. 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];
  948. }
  949. else
  950. {
  951. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[prv_uart_soft->tx_buffer.Pos];
  952. }
  953. }
  954. }
  955. }
  956. static void __LUAT_C_CODE_IN_RAM__ luat_uart_soft_recv_hwtimer_irq(void)
  957. {
  958. uint8_t bit = luat_uart_soft_gpio_fast_input(prv_uart_soft->rx_pin);
  959. uint8_t is_end = 0;
  960. if (!prv_uart_soft->rx_shift_bits) //检测起始位
  961. {
  962. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->rx_period);
  963. prv_uart_soft->rx_bit = bit;
  964. prv_uart_soft->rx_shift_bits++;
  965. prv_uart_soft->rx_parity_bit += bit;
  966. return ;
  967. }
  968. else if (0xef == prv_uart_soft->rx_shift_bits) //RX检测超时了,没有新的起始位
  969. {
  970. is_end = 1;
  971. goto UART_SOFT_RX_DONE;
  972. }
  973. if (prv_uart_soft->rx_shift_bits < prv_uart_soft->data_bits)
  974. {
  975. prv_uart_soft->rx_bit |= (bit << prv_uart_soft->rx_shift_bits);
  976. prv_uart_soft->rx_shift_bits++;
  977. prv_uart_soft->rx_parity_bit += bit;
  978. if (prv_uart_soft->rx_shift_bits >= prv_uart_soft->data_bits)
  979. {
  980. if (!prv_uart_soft->parity) //如果不做奇偶校验,就直接开始下一个字节
  981. {
  982. goto UART_SOFT_RX_BYTE_DONE;
  983. }
  984. }
  985. return;
  986. }
  987. if ((prv_uart_soft->rx_parity_bit & 0x01) != bit) //奇偶校验错误
  988. {
  989. is_end = 1;
  990. goto UART_SOFT_RX_DONE;
  991. }
  992. UART_SOFT_RX_BYTE_DONE:
  993. prv_uart_soft->rx_fifo[prv_uart_soft->rx_fifo_cnt] = prv_uart_soft->rx_bit;
  994. prv_uart_soft->rx_fifo_cnt++;
  995. luat_uart_soft_gpio_fast_irq_set(prv_uart_soft->rx_pin, 1);
  996. prv_uart_soft->rx_shift_bits = 0xef;
  997. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->stop_period * 20); //这里做接收超时检测
  998. if (prv_uart_soft->rx_fifo_cnt < LUAT_UART_SOFT_FIFO_CNT) //接收fifo没有满,继续接收
  999. {
  1000. return;
  1001. }
  1002. UART_SOFT_RX_DONE:
  1003. if (prv_uart_soft->rx_fifo_cnt || is_end)
  1004. {
  1005. rtos_msg_t msg;
  1006. msg.handler = l_uart_soft_handler_rx_done;
  1007. msg.ptr = luat_heap_alloc(0, 0, 0, prv_uart_soft->rx_fifo_cnt);
  1008. msg.arg1 = prv_uart_soft->rx_fifo_cnt;
  1009. msg.arg2 = is_end;
  1010. if (msg.ptr)
  1011. {
  1012. memcpy(msg.ptr, prv_uart_soft->rx_fifo, prv_uart_soft->rx_fifo_cnt);
  1013. }
  1014. prv_uart_soft->rx_fifo_cnt = 0;
  1015. luat_msgbus_put(&msg, 0);
  1016. }
  1017. if (is_end)
  1018. {
  1019. luat_uart_soft_gpio_fast_irq_set(prv_uart_soft->rx_pin, 1);
  1020. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, 0);
  1021. }
  1022. return;
  1023. }
  1024. #endif
  1025. /**
  1026. 设置软件uart的硬件配置,只有支持硬件定时器的SOC才能使用,目前只能设置一个,波特率根据平台的软硬件配置有不同的极限,建议9600,接收缓存不超过65535,不支持MSB,支持485自动控制。后续仍要setup操作
  1027. @api uart.createSoft(tx_pin, tx_hwtimer_id, rx_pin, rx_hwtimer_id, adjust_period)
  1028. @int 发送引脚编号
  1029. @int 发送用的硬件定时器ID
  1030. @int 接收引脚编号
  1031. @int 接收用的硬件定时器ID
  1032. @int 发送时序调整,单位是定时器时钟周期,默认是0,需要根据示波器或者逻辑分析仪进行微调
  1033. @int 接收时序调整,单位是定时器时钟周期,默认是0,需要根据示波器或者逻辑分析仪进行微调
  1034. @return int 软件uart的id,如果失败则返回nil
  1035. @usage
  1036. -- 初始化软件uart
  1037. local uart_id = uart.createSoft(21, 0, 1, 2) --air780e建议用定时器0和2,tx_pin最好用AGPIO,防止休眠时误触发对端RX
  1038. */
  1039. static int l_uart_soft(lua_State *L) {
  1040. #ifdef LUAT_USE_SOFT_UART
  1041. if (!prv_uart_soft)
  1042. {
  1043. prv_uart_soft = luat_heap_alloc(NULL, NULL, 0, sizeof(luat_uart_soft_t));
  1044. if (prv_uart_soft)
  1045. {
  1046. memset(prv_uart_soft, 0, sizeof(luat_uart_soft_t));
  1047. INIT_LLIST_HEAD(&prv_uart_soft->tx_queue_head);
  1048. prv_uart_soft->uart_id = 0xff;
  1049. }
  1050. else
  1051. {
  1052. lua_pushnil(L);
  1053. goto CREATE_DONE;
  1054. }
  1055. }
  1056. if (prv_uart_soft->is_inited)
  1057. {
  1058. lua_pushnil(L);
  1059. goto CREATE_DONE;
  1060. }
  1061. for(int uart_id = 1; uart_id < MAX_DEVICE_COUNT; uart_id++)
  1062. {
  1063. if (!luat_uart_exist(uart_id))
  1064. {
  1065. LLOGD("find free uart id, %d", uart_id);
  1066. prv_uart_soft->is_inited = 1;
  1067. prv_uart_soft->uart_id = uart_id;
  1068. break;
  1069. }
  1070. }
  1071. if (!prv_uart_soft->is_inited)
  1072. {
  1073. lua_pushnil(L);
  1074. goto CREATE_DONE;
  1075. }
  1076. prv_uart_soft->tx_pin = luaL_optinteger(L, 1, 0xff);
  1077. prv_uart_soft->tx_hwtimer_id = luaL_optinteger(L, 2, 0xff);
  1078. prv_uart_soft->rx_pin = luaL_optinteger(L, 3, 0xff);
  1079. prv_uart_soft->rx_hwtimer_id = luaL_optinteger(L, 4, 0xff);
  1080. prv_uart_soft->tx_adjust_period = luaL_optinteger(L, 5, 0);
  1081. prv_uart_soft->rx_adjust_period = luaL_optinteger(L, 6, 0);
  1082. if (luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, luat_uart_soft_send_hwtimer_irq))
  1083. {
  1084. prv_uart_soft->is_inited = 0;
  1085. }
  1086. if (luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->rx_hwtimer_id, luat_uart_soft_recv_hwtimer_irq))
  1087. {
  1088. luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, NULL);
  1089. prv_uart_soft->is_inited = 0;
  1090. }
  1091. if (!prv_uart_soft->is_inited)
  1092. {
  1093. lua_pushnil(L);
  1094. goto CREATE_DONE;
  1095. }
  1096. lua_pushinteger(L, prv_uart_soft->uart_id);
  1097. #else
  1098. lua_pushnil(L);
  1099. #endif
  1100. #ifdef LUAT_USE_SOFT_UART
  1101. CREATE_DONE:
  1102. #endif
  1103. return 1;
  1104. }
  1105. /*
  1106. 获取可用串口号列表,当前仅限win32
  1107. @api uart.list(max)
  1108. @int 可选,默认256,最多获取多少个串口
  1109. @return table 获取到的可用串口号列表
  1110. */
  1111. #ifdef LUAT_FORCE_WIN32
  1112. static int l_uart_list(lua_State *L)
  1113. {
  1114. size_t len = luaL_optinteger(L,1,256);
  1115. lua_newtable(L);//返回用的table
  1116. uint8_t* buff = (uint8_t*)luat_heap_malloc(len);
  1117. if (!buff)
  1118. return 1;
  1119. int rlen = luat_uart_list(buff, len);
  1120. for(int i = 0;i<rlen;i++)
  1121. {
  1122. lua_pushinteger(L,i+1);
  1123. lua_pushinteger(L,buff[i]);
  1124. lua_settable(L,-3);
  1125. }
  1126. luat_heap_free(buff);
  1127. return 1;
  1128. }
  1129. #endif
  1130. #include "rotable2.h"
  1131. static const rotable_Reg_t reg_uart[] =
  1132. {
  1133. { "write", ROREG_FUNC(l_uart_write)},
  1134. { "read", ROREG_FUNC(l_uart_read)},
  1135. { "wait485", ROREG_FUNC(l_uart_wait485_tx_done)},
  1136. { "tx", ROREG_FUNC(l_uart_tx)},
  1137. { "rx", ROREG_FUNC(l_uart_rx)},
  1138. { "rxSize", ROREG_FUNC(l_uart_rx_size)},
  1139. { "rx_size", ROREG_FUNC(l_uart_rx_size)},
  1140. { "createSoft", ROREG_FUNC(l_uart_soft)},
  1141. { "close", ROREG_FUNC(l_uart_close)},
  1142. { "on", ROREG_FUNC(l_uart_on)},
  1143. { "setup", ROREG_FUNC(l_uart_setup)},
  1144. { "exist", ROREG_FUNC(l_uart_exist)},
  1145. #ifdef LUAT_FORCE_WIN32
  1146. { "list", ROREG_FUNC(l_uart_list)},
  1147. #endif
  1148. //@const Odd number 奇校验,大小写兼容性
  1149. { "Odd", ROREG_INT(LUAT_PARITY_ODD)},
  1150. //@const Even number 偶校验,大小写兼容性
  1151. { "Even", ROREG_INT(LUAT_PARITY_EVEN)},
  1152. //@const None number 无校验,大小写兼容性
  1153. { "None", ROREG_INT(LUAT_PARITY_NONE)},
  1154. //@const ODD number 奇校验
  1155. { "ODD", ROREG_INT(LUAT_PARITY_ODD)},
  1156. //@const EVEN number 偶校验
  1157. { "EVEN", ROREG_INT(LUAT_PARITY_EVEN)},
  1158. //@const NONE number 无校验
  1159. { "NONE", ROREG_INT(LUAT_PARITY_NONE)},
  1160. //高低位顺序
  1161. //@const LSB number 小端模式
  1162. { "LSB", ROREG_INT(LUAT_BIT_ORDER_LSB)},
  1163. //@const MSB number 大端模式
  1164. { "MSB", ROREG_INT(LUAT_BIT_ORDER_MSB)},
  1165. //@const VUART_0 number 虚拟串口0
  1166. { "VUART_0", ROREG_INT(LUAT_VUART_ID_0)},
  1167. { NULL, ROREG_INT(0) }
  1168. };
  1169. LUAMOD_API int luaopen_uart(lua_State *L)
  1170. {
  1171. luat_newlib2(L, reg_uart);
  1172. return 1;
  1173. }