luat_lib_uart.c 39 KB

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