luat_lib_uart.c 41 KB

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