luat_lib_uart.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. 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. LUAT_WEAK void luat_uart_clear_rx_cache(int uart_id)
  747. {
  748. }
  749. /*
  750. 清除串口Rx缓存中剩余数据量,目前air105,air780e支持这个操作
  751. @api uart.rxClear(id)
  752. @int 串口id, uart0写0, uart1写1
  753. @usage
  754. uart.rxClear(1)
  755. */
  756. static int l_uart_rx_clear(lua_State *L)
  757. {
  758. uint8_t id = luaL_checkinteger(L, 1);
  759. #ifdef LUAT_USE_SOFT_UART
  760. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  761. {
  762. prv_uart_soft->rx_buffer.Pos = 0;
  763. }
  764. else
  765. {
  766. luat_uart_clear_rx_cache(id);
  767. }
  768. #else
  769. luat_uart_clear_rx_cache(id);
  770. #endif
  771. return 0;
  772. }
  773. /*
  774. buff形式写串口,等同于c语言uart_tx(uart_id, &buff[start], len);
  775. @api uart.tx(id, buff, start, len)
  776. @int 串口id, uart0写0, uart1写1
  777. @zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  778. @int 可选,要发送的数据起始位置,默认为0
  779. @int 可选,要发送的数据长度,默认为zbuff内有效数据,最大值不超过zbuff的最大空间
  780. @return int 成功的数据长度
  781. @usage
  782. uart.tx(1, buf)
  783. */
  784. static int l_uart_tx(lua_State *L)
  785. {
  786. size_t start, len;
  787. // const char *buf;
  788. luat_zbuff_t *buff;
  789. uint8_t id = luaL_checkinteger(L, 1);
  790. if(lua_isuserdata(L, 2))
  791. {
  792. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  793. }
  794. else
  795. {
  796. lua_pushinteger(L, 0);
  797. return 1;
  798. }
  799. start = luaL_optinteger(L, 3, 0);
  800. len = luaL_optinteger(L, 4, buff->used);
  801. if (start >= buff->len)
  802. {
  803. lua_pushinteger(L, 0);
  804. return 1;
  805. }
  806. if ((start + len)>= buff->len)
  807. {
  808. len = buff->len - start;
  809. }
  810. #ifdef LUAT_USE_SOFT_UART
  811. int result;
  812. if (prv_uart_soft && (prv_uart_soft->uart_id == id))
  813. {
  814. result = luat_uart_soft_write((const uint8_t*)(buff->addr + start), len);
  815. }
  816. else
  817. {
  818. result = luat_uart_write(id, buff->addr + start, len);
  819. }
  820. lua_pushinteger(L, result);
  821. #else
  822. int result = luat_uart_write(id, buff->addr + start, len);
  823. lua_pushinteger(L, result);
  824. #endif
  825. return 1;
  826. }
  827. #ifdef LUAT_USE_SOFT_UART
  828. static int l_uart_soft_handler_tx_done(lua_State *L, void* ptr)
  829. {
  830. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  831. lua_pop(L, 1);
  832. if (prv_uart_soft->is_inited)
  833. {
  834. luat_uart_soft_tx_node_t *node = (luat_uart_soft_tx_node_t *)(prv_uart_soft->tx_queue_head.next);
  835. llist_del(&node->tx_node);
  836. luat_heap_alloc(NULL, node->data, 0, 0);
  837. luat_heap_alloc(NULL, node, 0, 0);
  838. if (llist_empty(&prv_uart_soft->tx_queue_head))
  839. {
  840. Buffer_StaticInit(&prv_uart_soft->tx_buffer, NULL, 0);
  841. prv_uart_soft->is_tx_busy = 0;
  842. if (!prv_uart_soft->is_rx_busy)
  843. {
  844. luat_uart_soft_sleep_enable(1);
  845. }
  846. if (prv_uart_soft->pin485 != 0xff)
  847. {
  848. luat_gpio_set(prv_uart_soft->pin485, prv_uart_soft->rs485_rx_level);
  849. }
  850. if (uart_cbs[prv_uart_soft->uart_id].sent) {
  851. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[prv_uart_soft->uart_id].sent);
  852. if (lua_isfunction(L, -1)) {
  853. lua_pushinteger(L, prv_uart_soft->uart_id);
  854. lua_call(L, 1, 0);
  855. }
  856. }
  857. }
  858. else
  859. {
  860. node = (luat_uart_soft_tx_node_t *)prv_uart_soft->tx_queue_head.next;
  861. Buffer_StaticInit(&prv_uart_soft->tx_buffer, node->data, node->len);
  862. prv_uart_soft->tx_shift_bits = 0;
  863. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  864. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  865. if (prv_uart_soft->parity)
  866. {
  867. 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];
  868. }
  869. else
  870. {
  871. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[0];
  872. }
  873. }
  874. }
  875. lua_pushinteger(L, 0);
  876. return 1;
  877. }
  878. static int l_uart_soft_handler_rx_done(lua_State *L, void* ptr)
  879. {
  880. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  881. lua_pop(L, 1);
  882. if (prv_uart_soft->is_inited)
  883. {
  884. if (msg->ptr && msg->arg1)
  885. {
  886. if (((uint32_t)msg->arg1 + prv_uart_soft->rx_buffer.Pos) > prv_uart_soft->rx_buffer.MaxLen)
  887. {
  888. uint8_t *new = luat_heap_alloc(NULL, NULL, 0, (prv_uart_soft->rx_buffer.MaxLen + (uint32_t)msg->arg1) * 2);
  889. if (new)
  890. {
  891. prv_uart_soft->rx_buffer.MaxLen = (prv_uart_soft->rx_buffer.MaxLen + (uint32_t)msg->arg1) * 2;
  892. memcpy(new, prv_uart_soft->rx_buffer.Data, prv_uart_soft->rx_buffer.Pos);
  893. luat_heap_alloc(NULL, prv_uart_soft->rx_buffer.Data, 0, 0);
  894. prv_uart_soft->rx_buffer.Data = new;
  895. memcpy(prv_uart_soft->rx_buffer.Data + prv_uart_soft->rx_buffer.Pos, msg->ptr, (uint32_t)msg->arg1);
  896. prv_uart_soft->rx_buffer.Pos += (uint32_t)msg->arg1;
  897. }
  898. else
  899. {
  900. LLOGE("soft uart resize no mem!");
  901. }
  902. }
  903. else
  904. {
  905. memcpy(prv_uart_soft->rx_buffer.Data + prv_uart_soft->rx_buffer.Pos, msg->ptr, (uint32_t)msg->arg1);
  906. prv_uart_soft->rx_buffer.Pos += (uint32_t)msg->arg1;
  907. }
  908. }
  909. if ((prv_uart_soft->rx_buffer.Pos > prv_uart_soft->rx_buffer_size) || msg->arg2)
  910. {
  911. if (uart_app_recvs[prv_uart_soft->uart_id]) {
  912. uart_app_recvs[prv_uart_soft->uart_id](prv_uart_soft->uart_id, msg->arg2);
  913. }
  914. if (uart_cbs[prv_uart_soft->uart_id].received) {
  915. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[prv_uart_soft->uart_id].received);
  916. if (lua_isfunction(L, -1)) {
  917. lua_pushinteger(L, prv_uart_soft->uart_id);
  918. lua_pushinteger(L, prv_uart_soft->rx_buffer.Pos);
  919. lua_call(L, 2, 0);
  920. }
  921. }
  922. }
  923. if (msg->arg2)
  924. {
  925. prv_uart_soft->is_rx_busy = 0;
  926. if (!prv_uart_soft->is_tx_busy)
  927. {
  928. luat_uart_soft_sleep_enable(1);
  929. }
  930. }
  931. }
  932. if (msg->ptr)
  933. {
  934. luat_heap_alloc(NULL, msg->ptr, 0, 0);
  935. }
  936. lua_pushinteger(L, 0);
  937. return 1;
  938. }
  939. static void __LUAT_C_CODE_IN_RAM__ luat_uart_soft_send_hwtimer_irq(void)
  940. {
  941. if (prv_uart_soft->tx_shift_bits >= prv_uart_soft->total_bits)
  942. {
  943. //发送完了
  944. if (prv_uart_soft->tx_buffer.Pos >= prv_uart_soft->tx_buffer.MaxLen)
  945. {
  946. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, 0);
  947. rtos_msg_t msg;
  948. msg.handler = l_uart_soft_handler_tx_done;
  949. msg.ptr = NULL;
  950. msg.arg1 = NULL;
  951. msg.arg2 = NULL;
  952. luat_msgbus_put(&msg, 0);
  953. }
  954. else
  955. {
  956. //发送新的字节的起始位
  957. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, 0);
  958. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->tx_period);
  959. prv_uart_soft->tx_shift_bits = 0;
  960. }
  961. return;
  962. }
  963. luat_uart_soft_gpio_fast_output(prv_uart_soft->tx_pin, (prv_uart_soft->tx_bit >> prv_uart_soft->tx_shift_bits) & 0x01);
  964. prv_uart_soft->tx_shift_bits++;
  965. if (prv_uart_soft->tx_shift_bits > prv_uart_soft->data_bits)
  966. {
  967. luat_uart_soft_hwtimer_onoff(prv_uart_soft->tx_hwtimer_id, prv_uart_soft->stop_period);
  968. prv_uart_soft->tx_shift_bits = prv_uart_soft->total_bits;
  969. prv_uart_soft->tx_buffer.Pos++;
  970. if (prv_uart_soft->tx_buffer.Pos < prv_uart_soft->tx_buffer.MaxLen)
  971. {
  972. if (prv_uart_soft->parity)
  973. {
  974. 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];
  975. }
  976. else
  977. {
  978. prv_uart_soft->tx_bit = (0xffff << prv_uart_soft->data_bits) | prv_uart_soft->tx_buffer.Data[prv_uart_soft->tx_buffer.Pos];
  979. }
  980. }
  981. }
  982. }
  983. static void __LUAT_C_CODE_IN_RAM__ luat_uart_soft_recv_hwtimer_irq(void)
  984. {
  985. uint8_t bit = luat_uart_soft_gpio_fast_input(prv_uart_soft->rx_pin);
  986. uint8_t is_end = 0;
  987. if (!prv_uart_soft->rx_shift_bits) //检测起始位
  988. {
  989. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->rx_period);
  990. prv_uart_soft->rx_bit = bit;
  991. prv_uart_soft->rx_shift_bits++;
  992. prv_uart_soft->rx_parity_bit += bit;
  993. return ;
  994. }
  995. else if (0xef == prv_uart_soft->rx_shift_bits) //RX检测超时了,没有新的起始位
  996. {
  997. is_end = 1;
  998. goto UART_SOFT_RX_DONE;
  999. }
  1000. if (prv_uart_soft->rx_shift_bits < prv_uart_soft->data_bits)
  1001. {
  1002. prv_uart_soft->rx_bit |= (bit << prv_uart_soft->rx_shift_bits);
  1003. prv_uart_soft->rx_shift_bits++;
  1004. prv_uart_soft->rx_parity_bit += bit;
  1005. if (prv_uart_soft->rx_shift_bits >= prv_uart_soft->data_bits)
  1006. {
  1007. if (!prv_uart_soft->parity) //如果不做奇偶校验,就直接开始下一个字节
  1008. {
  1009. goto UART_SOFT_RX_BYTE_DONE;
  1010. }
  1011. }
  1012. return;
  1013. }
  1014. if ((prv_uart_soft->rx_parity_bit & 0x01) != bit) //奇偶校验错误
  1015. {
  1016. is_end = 1;
  1017. goto UART_SOFT_RX_DONE;
  1018. }
  1019. UART_SOFT_RX_BYTE_DONE:
  1020. prv_uart_soft->rx_fifo[prv_uart_soft->rx_fifo_cnt] = prv_uart_soft->rx_bit;
  1021. prv_uart_soft->rx_fifo_cnt++;
  1022. luat_uart_soft_gpio_fast_irq_set(prv_uart_soft->rx_pin, 1);
  1023. prv_uart_soft->rx_shift_bits = 0xef;
  1024. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, prv_uart_soft->stop_period * 20); //这里做接收超时检测
  1025. if (prv_uart_soft->rx_fifo_cnt < LUAT_UART_SOFT_FIFO_CNT) //接收fifo没有满,继续接收
  1026. {
  1027. return;
  1028. }
  1029. UART_SOFT_RX_DONE:
  1030. if (prv_uart_soft->rx_fifo_cnt || is_end)
  1031. {
  1032. rtos_msg_t msg;
  1033. msg.handler = l_uart_soft_handler_rx_done;
  1034. msg.ptr = luat_heap_alloc(0, 0, 0, prv_uart_soft->rx_fifo_cnt);
  1035. msg.arg1 = prv_uart_soft->rx_fifo_cnt;
  1036. msg.arg2 = is_end;
  1037. if (msg.ptr)
  1038. {
  1039. memcpy(msg.ptr, prv_uart_soft->rx_fifo, prv_uart_soft->rx_fifo_cnt);
  1040. }
  1041. prv_uart_soft->rx_fifo_cnt = 0;
  1042. luat_msgbus_put(&msg, 0);
  1043. }
  1044. if (is_end)
  1045. {
  1046. luat_uart_soft_gpio_fast_irq_set(prv_uart_soft->rx_pin, 1);
  1047. luat_uart_soft_hwtimer_onoff(prv_uart_soft->rx_hwtimer_id, 0);
  1048. }
  1049. return;
  1050. }
  1051. #endif
  1052. /**
  1053. 设置软件uart的硬件配置,只有支持硬件定时器的SOC才能使用,目前只能设置一个,波特率根据平台的软硬件配置有不同的极限,建议9600,接收缓存不超过65535,不支持MSB,支持485自动控制。后续仍要setup操作
  1054. @api uart.createSoft(tx_pin, tx_hwtimer_id, rx_pin, rx_hwtimer_id, adjust_period)
  1055. @int 发送引脚编号
  1056. @int 发送用的硬件定时器ID
  1057. @int 接收引脚编号
  1058. @int 接收用的硬件定时器ID
  1059. @int 发送时序调整,单位是定时器时钟周期,默认是0,需要根据示波器或者逻辑分析仪进行微调
  1060. @int 接收时序调整,单位是定时器时钟周期,默认是0,需要根据示波器或者逻辑分析仪进行微调
  1061. @return int 软件uart的id,如果失败则返回nil
  1062. @usage
  1063. -- 初始化软件uart
  1064. local uart_id = uart.createSoft(21, 0, 1, 2) --air780e建议用定时器0和2,tx_pin最好用AGPIO,防止休眠时误触发对端RX
  1065. */
  1066. static int l_uart_soft(lua_State *L) {
  1067. #ifdef LUAT_USE_SOFT_UART
  1068. if (!prv_uart_soft)
  1069. {
  1070. prv_uart_soft = luat_heap_alloc(NULL, NULL, 0, sizeof(luat_uart_soft_t));
  1071. if (prv_uart_soft)
  1072. {
  1073. memset(prv_uart_soft, 0, sizeof(luat_uart_soft_t));
  1074. INIT_LLIST_HEAD(&prv_uart_soft->tx_queue_head);
  1075. prv_uart_soft->uart_id = 0xff;
  1076. }
  1077. else
  1078. {
  1079. lua_pushnil(L);
  1080. goto CREATE_DONE;
  1081. }
  1082. }
  1083. if (prv_uart_soft->is_inited)
  1084. {
  1085. lua_pushnil(L);
  1086. goto CREATE_DONE;
  1087. }
  1088. for(int uart_id = 1; uart_id < MAX_DEVICE_COUNT; uart_id++)
  1089. {
  1090. if (!luat_uart_exist(uart_id))
  1091. {
  1092. LLOGD("find free uart id, %d", uart_id);
  1093. prv_uart_soft->is_inited = 1;
  1094. prv_uart_soft->uart_id = uart_id;
  1095. break;
  1096. }
  1097. }
  1098. if (!prv_uart_soft->is_inited)
  1099. {
  1100. lua_pushnil(L);
  1101. goto CREATE_DONE;
  1102. }
  1103. prv_uart_soft->tx_pin = luaL_optinteger(L, 1, 0xff);
  1104. prv_uart_soft->tx_hwtimer_id = luaL_optinteger(L, 2, 0xff);
  1105. prv_uart_soft->rx_pin = luaL_optinteger(L, 3, 0xff);
  1106. prv_uart_soft->rx_hwtimer_id = luaL_optinteger(L, 4, 0xff);
  1107. prv_uart_soft->tx_adjust_period = luaL_optinteger(L, 5, 0);
  1108. prv_uart_soft->rx_adjust_period = luaL_optinteger(L, 6, 0);
  1109. if (luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, luat_uart_soft_send_hwtimer_irq))
  1110. {
  1111. prv_uart_soft->is_inited = 0;
  1112. }
  1113. if (luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->rx_hwtimer_id, luat_uart_soft_recv_hwtimer_irq))
  1114. {
  1115. luat_uart_soft_setup_hwtimer_callback(prv_uart_soft->tx_hwtimer_id, NULL);
  1116. prv_uart_soft->is_inited = 0;
  1117. }
  1118. if (!prv_uart_soft->is_inited)
  1119. {
  1120. lua_pushnil(L);
  1121. goto CREATE_DONE;
  1122. }
  1123. lua_pushinteger(L, prv_uart_soft->uart_id);
  1124. #else
  1125. LLOGE("not support soft uart");
  1126. lua_pushnil(L);
  1127. #endif
  1128. #ifdef LUAT_USE_SOFT_UART
  1129. CREATE_DONE:
  1130. #endif
  1131. return 1;
  1132. }
  1133. /*
  1134. 获取可用串口号列表,当前仅限win32
  1135. @api uart.list(max)
  1136. @int 可选,默认256,最多获取多少个串口
  1137. @return table 获取到的可用串口号列表
  1138. */
  1139. #ifdef LUAT_FORCE_WIN32
  1140. static int l_uart_list(lua_State *L)
  1141. {
  1142. size_t len = luaL_optinteger(L,1,256);
  1143. lua_newtable(L);//返回用的table
  1144. uint8_t* buff = (uint8_t*)luat_heap_malloc(len);
  1145. if (!buff)
  1146. return 1;
  1147. int rlen = luat_uart_list(buff, len);
  1148. for(int i = 0;i<rlen;i++)
  1149. {
  1150. lua_pushinteger(L,i+1);
  1151. lua_pushinteger(L,buff[i]);
  1152. lua_settable(L,-3);
  1153. }
  1154. luat_heap_free(buff);
  1155. return 1;
  1156. }
  1157. #endif
  1158. #include "rotable2.h"
  1159. static const rotable_Reg_t reg_uart[] =
  1160. {
  1161. { "write", ROREG_FUNC(l_uart_write)},
  1162. { "read", ROREG_FUNC(l_uart_read)},
  1163. { "wait485", ROREG_FUNC(l_uart_wait485_tx_done)},
  1164. { "tx", ROREG_FUNC(l_uart_tx)},
  1165. { "rx", ROREG_FUNC(l_uart_rx)},
  1166. { "rxClear", ROREG_FUNC(l_uart_rx_clear)},
  1167. { "rxSize", ROREG_FUNC(l_uart_rx_size)},
  1168. { "rx_size", ROREG_FUNC(l_uart_rx_size)},
  1169. { "createSoft", ROREG_FUNC(l_uart_soft)},
  1170. { "close", ROREG_FUNC(l_uart_close)},
  1171. { "on", ROREG_FUNC(l_uart_on)},
  1172. { "setup", ROREG_FUNC(l_uart_setup)},
  1173. { "exist", ROREG_FUNC(l_uart_exist)},
  1174. #ifdef LUAT_FORCE_WIN32
  1175. { "list", ROREG_FUNC(l_uart_list)},
  1176. #endif
  1177. //@const Odd number 奇校验,大小写兼容性
  1178. { "Odd", ROREG_INT(LUAT_PARITY_ODD)},
  1179. //@const Even number 偶校验,大小写兼容性
  1180. { "Even", ROREG_INT(LUAT_PARITY_EVEN)},
  1181. //@const None number 无校验,大小写兼容性
  1182. { "None", ROREG_INT(LUAT_PARITY_NONE)},
  1183. //@const ODD number 奇校验
  1184. { "ODD", ROREG_INT(LUAT_PARITY_ODD)},
  1185. //@const EVEN number 偶校验
  1186. { "EVEN", ROREG_INT(LUAT_PARITY_EVEN)},
  1187. //@const NONE number 无校验
  1188. { "NONE", ROREG_INT(LUAT_PARITY_NONE)},
  1189. //高低位顺序
  1190. //@const LSB number 小端模式
  1191. { "LSB", ROREG_INT(LUAT_BIT_ORDER_LSB)},
  1192. //@const MSB number 大端模式
  1193. { "MSB", ROREG_INT(LUAT_BIT_ORDER_MSB)},
  1194. //@const VUART_0 number 虚拟串口0
  1195. { "VUART_0", ROREG_INT(LUAT_VUART_ID_0)},
  1196. { NULL, ROREG_INT(0) }
  1197. };
  1198. LUAMOD_API int luaopen_uart(lua_State *L)
  1199. {
  1200. luat_newlib2(L, reg_uart);
  1201. return 1;
  1202. }