spi_master.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Module for interfacing with the SPI master hardware
  2. #include <string.h>
  3. #include "module.h"
  4. #include "lauxlib.h"
  5. #include "lextra.h"
  6. #include "driver/spi_master.h"
  7. #include "esp_heap_alloc_caps.h"
  8. #include "esp_log.h"
  9. #include "spi_common.h"
  10. #define SPI_MASTER_TAG "spi.master"
  11. #define UD_HOST_STR "spi.master"
  12. #define UD_DEVICE_STR "spi.device"
  13. static int no_err( esp_err_t err )
  14. {
  15. switch (err)
  16. {
  17. default:
  18. ESP_LOGI(SPI_MASTER_TAG, "unknown error");
  19. return 0;
  20. case ESP_ERR_INVALID_ARG:
  21. ESP_LOGI(SPI_MASTER_TAG, "invalid argument");
  22. return 0;
  23. case ESP_ERR_INVALID_STATE:
  24. ESP_LOGI(SPI_MASTER_TAG, "internal logic error");
  25. return 0;
  26. case ESP_ERR_NO_MEM:
  27. ESP_LOGI(SPI_MASTER_TAG, "no memory");
  28. return 0;
  29. case ESP_OK:
  30. return 1;
  31. }
  32. }
  33. // ****************************************************************************
  34. // Device related functions
  35. //
  36. typedef struct {
  37. spi_device_handle_t device;
  38. int host_ref, host;
  39. } lspi_device_t;
  40. #define GET_UD_DEVICE \
  41. lspi_device_t *ud = (lspi_device_t *)luaL_checkudata( L, 1, UD_DEVICE_STR );
  42. #define CONFIG_TRANS_FROM_FIELD(field) \
  43. lua_getfield( L, stack, #field ); \
  44. trans.field = luaL_optint( L, -1, 0 );
  45. static int lspi_device_free( lua_State *L )
  46. {
  47. GET_UD_DEVICE;
  48. if (ud->device) {
  49. spi_bus_remove_device( ud->device );
  50. ud->device = NULL;
  51. // unref host to unblock automatic gc from this object
  52. luaL_unref( L, LUA_REGISTRYINDEX, ud->host_ref );
  53. ud->host_ref = LUA_NOREF;
  54. }
  55. return 0;
  56. }
  57. // Lua:
  58. // recv = dev:transfer(trans_desc)
  59. // recv = dev:transfer(data)
  60. static int lspi_device_transfer( lua_State *L )
  61. {
  62. GET_UD_DEVICE;
  63. int stack = 1;
  64. luaL_argcheck( L, ud->device, stack, "no device" );
  65. spi_transaction_t trans;
  66. memset( &trans, 0, sizeof( trans ) );
  67. size_t data_len, rx_len;
  68. const char *data;
  69. int type = lua_type( L, ++stack );
  70. luaL_argcheck( L, type == LUA_TSTRING || type == LUA_TTABLE, stack, "string or table expected" );
  71. if (type == LUA_TSTRING) {
  72. data = luaL_checklstring( L, stack, &data_len );
  73. rx_len = data_len;
  74. } else {
  75. const char * const options[] = {"std", "dio", "qio"};
  76. const uint32_t options_flags[] = {0, SPI_TRANS_MODE_DIO, SPI_TRANS_MODE_QIO};
  77. CONFIG_TRANS_FROM_FIELD(command);
  78. CONFIG_TRANS_FROM_FIELD(address);
  79. //
  80. lua_getfield( L, stack, "rxlen" );
  81. rx_len = luaL_optint( L, -1, 0 );
  82. //
  83. lua_getfield( L, stack, "addr_mode" );
  84. trans.flags |= luaL_optbool( L, -1, false ) ? SPI_TRANS_MODE_DIOQIO_ADDR : 0;
  85. //
  86. lua_getfield( L, stack, "mode" );
  87. trans.flags |= options_flags[ luaL_checkoption( L, -1, options[0], options ) ];
  88. //
  89. lua_getfield( L, stack, "txdata" );
  90. data = luaL_optlstring( L, -1, "", &data_len );
  91. lua_settop( L, stack );
  92. }
  93. const char *msg = NULL;
  94. trans.length = data_len * 8;
  95. if (data_len == 0) {
  96. //no MOSI phase requested
  97. trans.tx_buffer = NULL;
  98. } else if (data_len <=4 ) {
  99. // use local tx data buffer
  100. trans.flags |= SPI_TRANS_USE_TXDATA;
  101. memcpy( trans.tx_data, data, data_len );
  102. } else {
  103. // use DMA'able buffer
  104. if ((trans.tx_buffer = pvPortMallocCaps( data_len, MALLOC_CAP_DMA ))) {
  105. memcpy( (void *)trans.tx_buffer, data, data_len );
  106. } else {
  107. msg = "no memory";
  108. goto free_mem;
  109. }
  110. }
  111. trans.rxlength = rx_len * 8;
  112. if (rx_len == 0) {
  113. // no MISO phase requested
  114. trans.rx_buffer = NULL;
  115. } else if (rx_len <= 4) {
  116. // use local rx data buffer
  117. trans.flags |= SPI_TRANS_USE_RXDATA;
  118. } else {
  119. // use DMA'able buffer
  120. if (!(trans.rx_buffer = pvPortMallocCaps( rx_len, MALLOC_CAP_DMA ))) {
  121. msg = "no mem";
  122. goto free_mem;
  123. }
  124. }
  125. // finally perform the transaction
  126. if (no_err( spi_device_transmit( ud->device, &trans ) )) {
  127. // evaluate receive data
  128. if (trans.flags & SPI_TRANS_USE_RXDATA) {
  129. lua_pushlstring( L, (const char *)&(trans.rx_data[0]), rx_len );
  130. } else {
  131. lua_pushlstring( L, trans.rx_buffer, rx_len );
  132. }
  133. } else
  134. msg = "transfer failed";
  135. free_mem:
  136. if (!(trans.flags & SPI_TRANS_USE_TXDATA) && trans.tx_buffer)
  137. free( (void *)trans.tx_buffer );
  138. if (!(trans.flags & SPI_TRANS_USE_RXDATA) && trans.rx_buffer)
  139. free( (void *)trans.rx_buffer );
  140. if (msg)
  141. return luaL_error( L, msg );
  142. return 1;
  143. }
  144. static const LUA_REG_TYPE lspi_device_map[] = {
  145. { LSTRKEY( "transfer" ), LFUNCVAL( lspi_device_transfer ) },
  146. { LSTRKEY( "remove" ), LFUNCVAL( lspi_device_free ) },
  147. { LSTRKEY( "__gc" ), LFUNCVAL( lspi_device_free ) },
  148. { LSTRKEY( "__index" ), LROVAL( lspi_device_map ) },
  149. {LNILKEY, LNILVAL}
  150. };
  151. // ****************************************************************************
  152. // Host related functions
  153. //
  154. #define GET_UD_HOST \
  155. lspi_host_t *ud = (lspi_host_t *)luaL_checkudata( L, 1, UD_HOST_STR );
  156. //
  157. #define CONFIG_BUS_PIN_FROM_FIELD(pin) \
  158. lua_getfield( L, stack, #pin ); \
  159. config.pin ## _io_num = luaL_optint( L, -1, -1 );
  160. //
  161. #define CONFIG_DEVICE_FROM_INT_FIELD(field) \
  162. lua_getfield( L, stack, #field ); \
  163. config.field = luaL_optint( L, -1, 0 );
  164. #define CONFIG_DEVICE_FROM_BOOL_FIELD(field, mask) \
  165. lua_getfield( L, stack, #field ); \
  166. config.flags |= luaL_optbool( L, -1, false ) ? mask : 0;
  167. static int lspi_host_free( lua_State *L )
  168. {
  169. GET_UD_HOST;
  170. if (ud->host >= 0) {
  171. spi_bus_free( ud->host );
  172. ud->host = -1;
  173. }
  174. return 0;
  175. }
  176. // Lua: master = spi.master(host, config)
  177. int lspi_master( lua_State *L )
  178. {
  179. int stack = 0;
  180. int host = luaL_checkint( L, ++stack );
  181. luaL_argcheck( L,
  182. host == SPI_HOST || host == HSPI_HOST || host == VSPI_HOST,
  183. stack,
  184. "invalid host" );
  185. luaL_checktype( L, ++stack, LUA_TTABLE );
  186. spi_bus_config_t config;
  187. memset( &config, 0, sizeof( config ) );
  188. //
  189. CONFIG_BUS_PIN_FROM_FIELD(sclk);
  190. CONFIG_BUS_PIN_FROM_FIELD(mosi);
  191. CONFIG_BUS_PIN_FROM_FIELD(miso);
  192. CONFIG_BUS_PIN_FROM_FIELD(quadwp);
  193. CONFIG_BUS_PIN_FROM_FIELD(quadhd);
  194. lua_settop( L, stack );
  195. //
  196. if (no_err( spi_bus_initialize( host, &config, 1 ) )) {
  197. lspi_host_t *ud = (lspi_host_t *)lua_newuserdata( L, sizeof( lspi_host_t ) );
  198. luaL_getmetatable( L, UD_HOST_STR );
  199. lua_setmetatable( L, -2 );
  200. ud->host = host;
  201. return 1;
  202. }
  203. return luaL_error( L, "bus init failed" );
  204. }
  205. // Lua: dev = master:device(config)
  206. static int lspi_host_device( lua_State *L )
  207. {
  208. GET_UD_HOST;
  209. int stack = 1;
  210. luaL_argcheck( L, ud->host >= 0, stack, "no active bus host" );
  211. luaL_checktype( L, ++stack, LUA_TTABLE );
  212. spi_device_interface_config_t config;
  213. memset( &config, 0, sizeof( config ) );
  214. // mandatory fields
  215. lua_getfield( L, stack, "cs" );
  216. config.spics_io_num = luaL_optint( L, -1, -1 );
  217. //
  218. lua_getfield( L, stack, "mode" );
  219. int mode = luaL_optint( L, -1, -1 );
  220. luaL_argcheck( L, mode >= 0, stack, "mode setting missing" );
  221. config.mode = (uint8_t)mode;
  222. //
  223. lua_getfield( L, stack, "freq" );
  224. int freq = luaL_optint( L, -1, -1 );
  225. luaL_argcheck( L, freq >= 0, stack, "freq setting missing" );
  226. config.clock_speed_hz = freq;
  227. //
  228. // optional fields
  229. CONFIG_DEVICE_FROM_INT_FIELD(command_bits);
  230. CONFIG_DEVICE_FROM_INT_FIELD(address_bits);
  231. CONFIG_DEVICE_FROM_INT_FIELD(dummy_bits);
  232. CONFIG_DEVICE_FROM_INT_FIELD(cs_ena_pretrans);
  233. CONFIG_DEVICE_FROM_INT_FIELD(cs_ena_posttrans);
  234. CONFIG_DEVICE_FROM_INT_FIELD(duty_cycle_pos);
  235. CONFIG_DEVICE_FROM_BOOL_FIELD(tx_lsb_first, SPI_DEVICE_TXBIT_LSBFIRST);
  236. CONFIG_DEVICE_FROM_BOOL_FIELD(rx_lsb_first, SPI_DEVICE_RXBIT_LSBFIRST);
  237. CONFIG_DEVICE_FROM_BOOL_FIELD(wire3, SPI_DEVICE_3WIRE);
  238. CONFIG_DEVICE_FROM_BOOL_FIELD(positive_cs, SPI_DEVICE_POSITIVE_CS);
  239. CONFIG_DEVICE_FROM_BOOL_FIELD(halfduplex, SPI_DEVICE_HALFDUPLEX);
  240. CONFIG_DEVICE_FROM_BOOL_FIELD(clk_as_cs, SPI_DEVICE_CLK_AS_CS);
  241. lua_settop( L, stack );
  242. //
  243. // fill remaining config entries
  244. config.queue_size = 1;
  245. lspi_device_t *dev = (lspi_device_t *)lua_newuserdata( L, sizeof( lspi_device_t ) );
  246. dev->device = NULL;
  247. if (no_err( spi_bus_add_device( ud->host, &config, &(dev->device) ) )) {
  248. luaL_getmetatable( L, UD_DEVICE_STR );
  249. lua_setmetatable( L, -2 );
  250. // reference host object to avoid automatic gc
  251. lua_pushvalue( L, 1 );
  252. dev->host_ref = luaL_ref( L, LUA_REGISTRYINDEX );
  253. dev->host = ud->host;
  254. return 1;
  255. }
  256. lua_pop( L, 1 );
  257. return luaL_error( L, "failed to add device" );
  258. }
  259. static const LUA_REG_TYPE lspi_master_map[] = {
  260. { LSTRKEY( "device" ), LFUNCVAL( lspi_host_device ) },
  261. { LSTRKEY( "close" ), LFUNCVAL( lspi_host_free ) },
  262. { LSTRKEY( "__gc" ), LFUNCVAL( lspi_host_free ) },
  263. { LSTRKEY( "__index" ), LROVAL( lspi_master_map ) },
  264. {LNILKEY, LNILVAL}
  265. };
  266. // ****************************************************************************
  267. // Generic
  268. //
  269. int luaopen_spi_master( lua_State *L ) {
  270. luaL_rometatable(L, UD_HOST_STR, (void *)lspi_master_map);
  271. luaL_rometatable(L, UD_DEVICE_STR, (void *)lspi_device_map);
  272. return 0;
  273. }