spi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Module for interfacing with the SPI interface
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #define SPI_HALFDUPLEX 0
  6. #define SPI_FULLDUPLEX 1
  7. static u8 spi_databits[NUM_SPI] = {0, 0};
  8. static u8 spi_duplex[NUM_SPI] = {SPI_HALFDUPLEX, SPI_HALFDUPLEX};
  9. // Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div, [duplex_mode] )
  10. static int spi_setup( lua_State *L )
  11. {
  12. int id = luaL_checkinteger( L, 1 );
  13. int mode = luaL_checkinteger( L, 2 );
  14. int cpol = luaL_checkinteger( L, 3 );
  15. int cpha = luaL_checkinteger( L, 4 );
  16. int databits = luaL_checkinteger( L, 5 );
  17. u32 clock_div = luaL_checkinteger( L, 6 );
  18. int duplex_mode = luaL_optinteger( L, 7, SPI_HALFDUPLEX );
  19. MOD_CHECK_ID( spi, id );
  20. if (mode != PLATFORM_SPI_SLAVE && mode != PLATFORM_SPI_MASTER) {
  21. return luaL_error( L, "wrong arg type" );
  22. }
  23. if (cpol != PLATFORM_SPI_CPOL_LOW && cpol != PLATFORM_SPI_CPOL_HIGH) {
  24. return luaL_error( L, "wrong arg type" );
  25. }
  26. if (cpha != PLATFORM_SPI_CPHA_LOW && cpha != PLATFORM_SPI_CPHA_HIGH) {
  27. return luaL_error( L, "wrong arg type" );
  28. }
  29. if (databits < 0 || databits > 32) {
  30. return luaL_error( L, "out of range" );
  31. }
  32. if (clock_div < 4) {
  33. // defaulting to 8
  34. clock_div = 8;
  35. }
  36. if (duplex_mode == SPI_HALFDUPLEX || duplex_mode == SPI_FULLDUPLEX)
  37. {
  38. spi_duplex[id] = duplex_mode;
  39. }
  40. else
  41. {
  42. return luaL_error( L, "out of range" );
  43. }
  44. spi_databits[id] = databits;
  45. u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div);
  46. lua_pushinteger( L, res );
  47. return 1;
  48. }
  49. // Half-duplex mode:
  50. // Lua: wrote = spi.send( id, data1, [data2], ..., [datan] )
  51. // Full-duplex mode:
  52. // Lua: wrote, [data1], ..., [datan] = spi.send_recv( id, data1, [data2], ..., [datan] )
  53. // data can be either a string, a table or an 8-bit number
  54. static int spi_send_recv( lua_State *L )
  55. {
  56. unsigned id = luaL_checkinteger( L, 1 );
  57. const char *pdata;
  58. size_t datalen, i;
  59. u32 numdata;
  60. u32 wrote = 0;
  61. int pushed = 1;
  62. unsigned argn, tos;
  63. u8 recv = spi_duplex[id] == SPI_FULLDUPLEX ? 1 : 0;
  64. MOD_CHECK_ID( spi, id );
  65. if( (tos = lua_gettop( L )) < 2 )
  66. return luaL_error( L, "wrong arg type" );
  67. // prepare first returned item 'wrote' - value is yet unknown
  68. // position on stack is tos+1
  69. lua_pushinteger( L, 0 );
  70. for( argn = 2; argn <= tos; argn ++ )
  71. {
  72. // *** Send integer value and return received data as integer ***
  73. // lua_isnumber() would silently convert a string of digits to an integer
  74. // whereas here strings are handled separately.
  75. if( lua_type( L, argn ) == LUA_TNUMBER )
  76. {
  77. numdata = luaL_checkinteger( L, argn );
  78. if (recv > 0)
  79. {
  80. lua_pushinteger( L, platform_spi_send_recv( id, spi_databits[id], numdata ) );
  81. pushed ++;
  82. }
  83. else
  84. {
  85. platform_spi_send( id, spi_databits[id], numdata );
  86. }
  87. wrote ++;
  88. }
  89. // *** Send table elements and return received data items as a table ***
  90. else if( lua_istable( L, argn ) )
  91. {
  92. datalen = lua_objlen( L, argn );
  93. if (recv > 0 && datalen > 0) {
  94. // create a table for the received data
  95. lua_createtable( L, datalen, 0 );
  96. pushed ++;
  97. }
  98. for( i = 0; i < datalen; i ++ )
  99. {
  100. lua_rawgeti( L, argn, i + 1 );
  101. numdata = luaL_checkinteger( L, -1 );
  102. lua_pop( L, 1 );
  103. if (recv > 0) {
  104. lua_pushinteger( L, platform_spi_send_recv( id, spi_databits[id], numdata ) );
  105. lua_rawseti( L, -2, i + 1 );
  106. }
  107. else
  108. {
  109. platform_spi_send( id, spi_databits[id], numdata );
  110. }
  111. }
  112. wrote += i;
  113. if( i < datalen )
  114. break;
  115. }
  116. // *** Send characters of a string and return received data items as string ***
  117. else
  118. {
  119. luaL_Buffer b;
  120. pdata = luaL_checklstring( L, argn, &datalen );
  121. if (recv > 0) {
  122. luaL_buffinit( L, &b );
  123. }
  124. for( i = 0; i < datalen; i ++ )
  125. {
  126. if (recv > 0)
  127. {
  128. luaL_addchar( &b, (char)platform_spi_send_recv( id, spi_databits[id], pdata[ i ] ) );
  129. }
  130. else
  131. {
  132. platform_spi_send( id, spi_databits[id], pdata[ i ] );
  133. }
  134. }
  135. if (recv > 0 && datalen > 0) {
  136. luaL_pushresult( &b );
  137. pushed ++;
  138. }
  139. wrote += i;
  140. if( i < datalen )
  141. break;
  142. }
  143. }
  144. // update item 'wrote' on stack
  145. lua_pushinteger( L, wrote );
  146. lua_replace( L, tos+1 );
  147. return pushed;
  148. }
  149. // Lua: read = spi.recv( id, size, [default data] )
  150. static int spi_recv( lua_State *L )
  151. {
  152. int id = luaL_checkinteger( L, 1 );
  153. int size = luaL_checkinteger( L, 2 ), i;
  154. int def = luaL_optinteger( L, 3, 0xffffffff );
  155. luaL_Buffer b;
  156. MOD_CHECK_ID( spi, id );
  157. if (size == 0) {
  158. return 0;
  159. }
  160. luaL_buffinit( L, &b );
  161. for (i=0; i<size; i++)
  162. {
  163. luaL_addchar( &b, ( char )platform_spi_send_recv( id, spi_databits[id], def ) );
  164. }
  165. luaL_pushresult( &b );
  166. return 1;
  167. }
  168. // Lua: spi.set_mosi( id, offset, bitlen, data1, [data2], ..., [datan] )
  169. static int spi_set_mosi( lua_State *L )
  170. {
  171. int id = luaL_checkinteger( L, 1 );
  172. int offset = luaL_checkinteger( L, 2 );
  173. int bitlen = luaL_checkinteger( L, 3 );
  174. int argn;
  175. MOD_CHECK_ID( spi, id );
  176. if (offset < 0 || offset > 511) {
  177. return luaL_error( L, "offset out of range" );
  178. }
  179. if (bitlen < 1 || bitlen > 32) {
  180. return luaL_error( L, "bitlen out of range" );
  181. }
  182. if (lua_gettop( L ) < 4) {
  183. return luaL_error( L, "too few args" );
  184. }
  185. for (argn = 4; argn <= lua_gettop( L ); argn++, offset += bitlen )
  186. {
  187. u32 data = ( u32 )luaL_checkinteger(L, argn );
  188. if (offset + bitlen > 512) {
  189. return luaL_error( L, "data range exceeded > 512 bits" );
  190. }
  191. if (PLATFORM_OK != platform_spi_set_mosi( id, offset, bitlen, data )) {
  192. return luaL_error( L, "failed" );
  193. }
  194. }
  195. return 0;
  196. }
  197. // Lua: data = spi.get_miso( id, offset, bitlen, num )
  198. static int spi_get_miso( lua_State *L )
  199. {
  200. int id = luaL_checkinteger( L, 1 );
  201. int offset = luaL_checkinteger( L, 2 );
  202. int bitlen = luaL_checkinteger( L, 3 );
  203. int num = luaL_checkinteger( L, 4 ), i;
  204. MOD_CHECK_ID( spi, id );
  205. if (offset < 0 || offset > 511) {
  206. return luaL_error( L, "out of range" );
  207. }
  208. if (bitlen < 1 || bitlen > 32) {
  209. return luaL_error( L, "bitlen out of range" );
  210. }
  211. if (offset + bitlen * num > 512) {
  212. return luaL_error( L, "out of range" );
  213. }
  214. for (i = 0; i < num; i++)
  215. {
  216. lua_pushinteger( L, platform_spi_get_miso( id, offset + (bitlen * i), bitlen ) );
  217. }
  218. return num;
  219. }
  220. // Lua: spi.transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data, mosi_bitlen, dummy_bitlen, miso_bitlen )
  221. static int spi_transaction( lua_State *L )
  222. {
  223. int id = luaL_checkinteger( L, 1 );
  224. int cmd_bitlen = luaL_checkinteger( L, 2 );
  225. u16 cmd_data = ( u16 )luaL_checkinteger( L, 3 );
  226. int addr_bitlen = luaL_checkinteger( L, 4 );
  227. u32 addr_data = ( u32 )luaL_checkinteger( L, 5 );
  228. int mosi_bitlen = luaL_checkinteger( L, 6 );
  229. int dummy_bitlen = luaL_checkinteger( L, 7 );
  230. int miso_bitlen = luaL_checkinteger( L, 8 );
  231. MOD_CHECK_ID( spi, id );
  232. if (cmd_bitlen < 0 || cmd_bitlen > 16) {
  233. return luaL_error( L, "cmd_bitlen out of range" );
  234. }
  235. if (addr_bitlen < 0 || addr_bitlen > 32) {
  236. return luaL_error( L, "addr_bitlen out of range" );
  237. }
  238. if (mosi_bitlen < 0 || mosi_bitlen > 512) {
  239. return luaL_error( L, "mosi_bitlen out of range" );
  240. }
  241. if (dummy_bitlen < 0 || dummy_bitlen > 256) {
  242. return luaL_error( L, "dummy_bitlen out of range" );
  243. }
  244. if (miso_bitlen < -512 || miso_bitlen > 512) {
  245. return luaL_error( L, "miso_bitlen out of range" );
  246. }
  247. if (PLATFORM_OK != platform_spi_transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data,
  248. mosi_bitlen, dummy_bitlen, miso_bitlen) ) {
  249. return luaL_error( L, "failed" );
  250. }
  251. return 0;
  252. }
  253. // Module function map
  254. static const LUA_REG_TYPE spi_map[] = {
  255. { LSTRKEY( "setup" ), LFUNCVAL( spi_setup ) },
  256. { LSTRKEY( "send" ), LFUNCVAL( spi_send_recv ) },
  257. { LSTRKEY( "recv" ), LFUNCVAL( spi_recv ) },
  258. { LSTRKEY( "set_mosi" ), LFUNCVAL( spi_set_mosi ) },
  259. { LSTRKEY( "get_miso" ), LFUNCVAL( spi_get_miso ) },
  260. { LSTRKEY( "transaction" ), LFUNCVAL( spi_transaction ) },
  261. { LSTRKEY( "MASTER" ), LNUMVAL( PLATFORM_SPI_MASTER ) },
  262. { LSTRKEY( "SLAVE" ), LNUMVAL( PLATFORM_SPI_SLAVE) },
  263. { LSTRKEY( "CPHA_LOW" ), LNUMVAL( PLATFORM_SPI_CPHA_LOW) },
  264. { LSTRKEY( "CPHA_HIGH" ), LNUMVAL( PLATFORM_SPI_CPHA_HIGH) },
  265. { LSTRKEY( "CPOL_LOW" ), LNUMVAL( PLATFORM_SPI_CPOL_LOW) },
  266. { LSTRKEY( "CPOL_HIGH" ), LNUMVAL( PLATFORM_SPI_CPOL_HIGH) },
  267. { LSTRKEY( "DATABITS_8" ), LNUMVAL( 8 ) },
  268. { LSTRKEY( "HALFDUPLEX" ), LNUMVAL( SPI_HALFDUPLEX ) },
  269. { LSTRKEY( "FULLDUPLEX" ), LNUMVAL( SPI_FULLDUPLEX ) },
  270. { LNILKEY, LNILVAL }
  271. };
  272. NODEMCU_MODULE(SPI, "spi", spi_map, NULL);