luat_lib_uart.c 41 KB

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