uart.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Module for interfacing with serial
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include "rom.h"
  8. #include "driver/input.h"
  9. static int uart_receive_rf = LUA_NOREF;
  10. void uart_on_data_cb(const char *buf, size_t len){
  11. lua_State *L = lua_getstate();
  12. lua_rawgeti(L, LUA_REGISTRYINDEX, uart_receive_rf);
  13. lua_pushlstring(L, buf, len);
  14. luaN_call(L, 1, 0, 0);
  15. }
  16. // Lua: uart.on("method", [number/char], function, [run_input])
  17. static int l_uart_on( lua_State* L )
  18. {
  19. size_t el;
  20. int stack = 2, data_len = -1;
  21. char end_char = 0;
  22. const char *method = lua_tostring( L, 1);
  23. bool run_input = true;
  24. luaL_argcheck(L, method && !strcmp(method, "data"), 1, "method not supported");
  25. if (lua_type( L, stack ) == LUA_TNUMBER)
  26. {
  27. data_len = luaL_checkinteger( L, stack );
  28. luaL_argcheck(L, data_len >= 0 && data_len <= LUA_MAXINPUT, stack, "wrong arg range");
  29. stack++;
  30. }
  31. else if (lua_isstring(L, stack))
  32. {
  33. const char *end = luaL_checklstring( L, stack, &el );
  34. data_len = 0;
  35. end_char = (int16_t) end[0];
  36. stack++;
  37. if(el!=1) {
  38. return luaL_error( L, "wrong arg range" );
  39. }
  40. }
  41. if (lua_anyfunction(L, stack)) {
  42. if (lua_isnumber(L, stack+1) && lua_tointeger(L, stack+1) == 0) {
  43. run_input = false;
  44. }
  45. lua_pushvalue(L, stack);
  46. luaL_unref(L, LUA_REGISTRYINDEX, uart_receive_rf);
  47. uart_receive_rf = luaL_ref(L, LUA_REGISTRYINDEX);
  48. } else {
  49. luaL_unref(L, LUA_REGISTRYINDEX, uart_receive_rf);
  50. uart_receive_rf = LUA_NOREF;
  51. }
  52. input_setup_receive(uart_on_data_cb, data_len, end_char, run_input);
  53. return 0;
  54. }
  55. bool uart0_echo = true;
  56. // Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo )
  57. static int l_uart_setup( lua_State* L )
  58. {
  59. uint32_t id, databits, parity, stopbits;
  60. uint32_t baud, res;
  61. id = luaL_checkinteger( L, 1 );
  62. MOD_CHECK_ID( uart, id );
  63. baud = luaL_checkinteger( L, 2 );
  64. databits = luaL_checkinteger( L, 3 );
  65. parity = luaL_checkinteger( L, 4 );
  66. stopbits = luaL_checkinteger( L, 5 );
  67. if (lua_isnumber(L,6)) {
  68. input_setecho(lua_tointeger(L,6) ? true : false);
  69. }
  70. res = platform_uart_setup( id, baud, databits, parity, stopbits );
  71. lua_pushinteger( L, res );
  72. return 1;
  73. }
  74. // uart.getconfig(id)
  75. static int l_uart_getconfig( lua_State* L )
  76. {
  77. uint32_t id, databits, parity, stopbits;
  78. uint32_t baud;
  79. id = luaL_checkinteger( L, 1 );
  80. MOD_CHECK_ID( uart, id );
  81. platform_uart_get_config(id, &baud, &databits, &parity, &stopbits);
  82. lua_pushinteger(L, baud);
  83. lua_pushinteger(L, databits);
  84. lua_pushinteger(L, parity);
  85. lua_pushinteger(L, stopbits);
  86. return 4;
  87. }
  88. // Lua: alt( set )
  89. static int l_uart_alt( lua_State* L )
  90. {
  91. unsigned set;
  92. set = luaL_checkinteger( L, 1 );
  93. platform_uart_alt( set );
  94. return 0;
  95. }
  96. // Lua: write( id, string1, [string2], ..., [stringn] )
  97. static int l_uart_write( lua_State* L )
  98. {
  99. int id;
  100. const char* buf;
  101. size_t len, i;
  102. int total = lua_gettop( L ), s;
  103. id = luaL_checkinteger( L, 1 );
  104. MOD_CHECK_ID( uart, id );
  105. for( s = 2; s <= total; s ++ )
  106. {
  107. if( lua_type( L, s ) == LUA_TNUMBER )
  108. {
  109. len = lua_tointeger( L, s );
  110. if( len > 255 )
  111. return luaL_error( L, "invalid number" );
  112. platform_uart_send( id, ( u8 )len );
  113. }
  114. else
  115. {
  116. luaL_checktype( L, s, LUA_TSTRING );
  117. buf = lua_tolstring( L, s, &len );
  118. for( i = 0; i < len; i ++ )
  119. platform_uart_send( id, buf[ i ] );
  120. }
  121. }
  122. return 0;
  123. }
  124. // Module function map
  125. LROT_BEGIN(uart)
  126. LROT_FUNCENTRY( setup, l_uart_setup )
  127. LROT_FUNCENTRY( getconfig, l_uart_getconfig )
  128. LROT_FUNCENTRY( write, l_uart_write )
  129. LROT_FUNCENTRY( on, l_uart_on )
  130. LROT_FUNCENTRY( alt, l_uart_alt )
  131. LROT_NUMENTRY( STOPBITS_1, PLATFORM_UART_STOPBITS_1 )
  132. LROT_NUMENTRY( STOPBITS_1_5, PLATFORM_UART_STOPBITS_1_5 )
  133. LROT_NUMENTRY( STOPBITS_2, PLATFORM_UART_STOPBITS_2 )
  134. LROT_NUMENTRY( PARITY_NONE, PLATFORM_UART_PARITY_NONE )
  135. LROT_NUMENTRY( PARITY_EVEN, PLATFORM_UART_PARITY_EVEN )
  136. LROT_NUMENTRY( PARITY_ODD, PLATFORM_UART_PARITY_ODD )
  137. LROT_END( uart, NULL, 0 )
  138. NODEMCU_MODULE(UART, "uart", uart, NULL);