luat_lib_spi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. @module spi
  3. @summary spi操作库
  4. @version 1.0
  5. @date 2020.04.23
  6. @demo spi
  7. @video https://www.bilibili.com/video/BV1VY411M7YH
  8. @tag LUAT_USE_SPI
  9. @usage
  10. -- 本库支持2套API风格
  11. -- 1. 老的API,spi.xxx 方式,需要自己控制软件cs引脚,不同设备要手动重新配置spi参数
  12. -- 2. 新的API(推荐使用), spidevice对象方式,不需要手动控制cs引脚,不同设备也无需重复配置参数,设备内部自动管理
  13. -- 老API
  14. spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
  15. local result = spi.send(0, "123")--发送123
  16. local recv = spi.recv(0, 4)--接收4字节数据
  17. spi.close(0)
  18. -- 新API, 注意spi_device是一个对象,选一个全局名称, 避免被回收
  19. spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,1)
  20. local result = spi_device:send("123")--发送123
  21. local recv = spi_device:recv(4)--接收4字节数据
  22. spi_device:close()
  23. */
  24. #include "luat_base.h"
  25. #include "luat_log.h"
  26. #include "luat_sys.h"
  27. #include "luat_msgbus.h"
  28. #include "luat_timer.h"
  29. #include "luat_mem.h"
  30. #include "luat_spi.h"
  31. #include "luat_zbuff.h"
  32. #include "luat_gpio.h"
  33. #include "luat_irq.h"
  34. #include "luat_zbuff.h"
  35. #define LUAT_LOG_TAG "spi"
  36. #define META_SPI "SPI*"
  37. #define LUAT_ESPI_TYPE "ESPI*"
  38. #define toespi(L) ((luat_espi_t *)luaL_checkudata(L, 1, LUAT_ESPI_TYPE))
  39. // 软件spi
  40. typedef struct luat_espi {
  41. uint8_t cs;
  42. uint8_t mosi;
  43. uint8_t miso;
  44. uint32_t clk;
  45. uint8_t CPHA;
  46. uint8_t CPOL;
  47. uint8_t dataw;
  48. uint8_t bit_dict;
  49. uint8_t master;
  50. uint8_t mode;
  51. } luat_espi_t;
  52. // 软SPI 发送一个字节
  53. static void spi_soft_send_byte(luat_espi_t *espi, uint8_t data)
  54. {
  55. uint8_t i;
  56. for (i = 0; i < 8; i++)
  57. {
  58. if (data&0x80)
  59. {
  60. luat_gpio_set(espi->mosi, Luat_GPIO_HIGH);
  61. }
  62. else
  63. {
  64. luat_gpio_set(espi->mosi, Luat_GPIO_LOW);
  65. }
  66. data<<=1;
  67. if (espi->CPOL == 0)
  68. {
  69. luat_gpio_set(espi->clk, Luat_GPIO_HIGH);
  70. luat_gpio_set(espi->clk, Luat_GPIO_LOW);
  71. }
  72. else
  73. {
  74. luat_gpio_set(espi->clk, Luat_GPIO_LOW);
  75. luat_gpio_set(espi->clk, Luat_GPIO_HIGH);
  76. }
  77. }
  78. }
  79. // 软SPI 接收一个字节
  80. static char spi_soft_recv_byte(luat_espi_t *espi)
  81. {
  82. unsigned char i = 8;
  83. unsigned char data = 0;
  84. while (i--)
  85. {
  86. data <<= 1;
  87. if (luat_gpio_get(espi->miso))
  88. {
  89. data |= 0x01;
  90. }
  91. if (espi->CPOL == 0)
  92. {
  93. luat_gpio_set(espi->clk, Luat_GPIO_HIGH);
  94. luat_gpio_set(espi->clk, Luat_GPIO_LOW);
  95. }else{
  96. luat_gpio_set(espi->clk, Luat_GPIO_LOW);
  97. luat_gpio_set(espi->clk, Luat_GPIO_HIGH);
  98. }
  99. }
  100. return data;
  101. }
  102. // 软SPI 收发一个字节
  103. static char spi_soft_xfer_byte(luat_espi_t *espi, uint8_t send_data)
  104. {
  105. unsigned char i = 8;
  106. unsigned char data = 0;
  107. while (i--)
  108. {
  109. // 发送
  110. if (send_data&0x80)
  111. {
  112. luat_gpio_set(espi->mosi, Luat_GPIO_HIGH);
  113. }
  114. else
  115. {
  116. luat_gpio_set(espi->mosi, Luat_GPIO_LOW);
  117. }
  118. send_data<<=1;
  119. // 接收
  120. data <<= 1;
  121. if (luat_gpio_get(espi->miso))
  122. {
  123. data |= 0x01;
  124. }
  125. if (espi->CPOL == 0)
  126. {
  127. luat_gpio_set(espi->clk, Luat_GPIO_HIGH);
  128. luat_gpio_set(espi->clk, Luat_GPIO_LOW);
  129. }else{
  130. luat_gpio_set(espi->clk, Luat_GPIO_LOW);
  131. luat_gpio_set(espi->clk, Luat_GPIO_HIGH);
  132. }
  133. }
  134. return data;
  135. }
  136. int luat_spi_soft_send(luat_espi_t *espi, const char*data, size_t len)
  137. {
  138. size_t i = 0;
  139. if (espi->cs != Luat_GPIO_MAX_ID)
  140. {
  141. luat_gpio_set(espi->cs, Luat_GPIO_LOW);
  142. }
  143. for (i = 0; i < len; i++)
  144. {
  145. spi_soft_send_byte(espi, data[i]);
  146. }
  147. if (espi->cs != Luat_GPIO_MAX_ID)
  148. {
  149. luat_gpio_set(espi->cs, Luat_GPIO_HIGH);
  150. }
  151. return 0;
  152. }
  153. int luat_spi_soft_recv(luat_espi_t *espi, char *buff, size_t len)
  154. {
  155. size_t i = 0;
  156. if (espi->cs != Luat_GPIO_MAX_ID)
  157. {
  158. luat_gpio_set(espi->cs, Luat_GPIO_LOW);
  159. }
  160. luat_gpio_set(espi->mosi, Luat_GPIO_LOW);
  161. for (i = 0; i < len; i++)
  162. {
  163. *buff++ = spi_soft_recv_byte(espi);
  164. }
  165. if (espi->cs != Luat_GPIO_MAX_ID)
  166. {
  167. luat_gpio_set(espi->cs, Luat_GPIO_HIGH);
  168. }
  169. luat_gpio_set(espi->mosi, Luat_GPIO_HIGH);
  170. return 0;
  171. }
  172. int luat_spi_soft_xfer(luat_espi_t *espi, const char *send_buff, char* recv_buff, size_t len)
  173. {
  174. size_t i = 0;
  175. if (espi->cs != Luat_GPIO_MAX_ID)
  176. {
  177. luat_gpio_set(espi->cs, Luat_GPIO_LOW);
  178. }
  179. luat_gpio_set(espi->mosi, Luat_GPIO_LOW);
  180. for (i = 0; i < len; i++)
  181. {
  182. *recv_buff++ = spi_soft_xfer_byte(espi, (uint8_t)send_buff[i]);
  183. }
  184. if (espi->cs != Luat_GPIO_MAX_ID)
  185. {
  186. luat_gpio_set(espi->cs, Luat_GPIO_HIGH);
  187. }
  188. luat_gpio_set(espi->mosi, Luat_GPIO_HIGH);
  189. return 0;
  190. }
  191. /**
  192. 设置并启用SPI
  193. @api spi.setup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)
  194. @int SPI号,例如0
  195. @int CS 片选脚,在w600不可用请填nil
  196. @int CPHA 默认0,可选0/1
  197. @int CPOL 默认0,可选0/1
  198. @int 数据宽度,默认8bit
  199. @int 波特率,默认2M=2000000
  200. @int 大小端, 默认spi.MSB, 可选spi.LSB
  201. @int 主从设置, 默认主1, 可选从机0. 通常只支持主机模式
  202. @int 工作模式, 全双工1, 半双工0, 默认全双工
  203. @return int 成功返回0,否则返回其他值
  204. @usage
  205. -- 初始化spi
  206. spi.setup(0,20,0,0,8,2000000,spi.MSB,1,1)
  207. */
  208. static int l_spi_setup(lua_State *L) {
  209. luat_spi_t spi_config = {0};
  210. spi_config.id = luaL_checkinteger(L, 1);
  211. spi_config.cs = luaL_optinteger(L, 2, Luat_GPIO_MAX_ID); // 默认无
  212. spi_config.CPHA = luaL_optinteger(L, 3, 0); // CPHA0
  213. spi_config.CPOL = luaL_optinteger(L, 4, 0); // CPOL0
  214. spi_config.dataw = luaL_optinteger(L, 5, 8); // 8bit
  215. spi_config.bandrate = luaL_optinteger(L, 6, 2000000U); // 2000000U
  216. spi_config.bit_dict = luaL_optinteger(L, 7, 1); // MSB=1, LSB=0
  217. spi_config.master = luaL_optinteger(L, 8, 1); // master=1,slave=0
  218. spi_config.mode = luaL_optinteger(L, 9, 1); // FULL=1, half=0
  219. lua_pushinteger(L, luat_spi_setup(&spi_config));
  220. return 1;
  221. }
  222. /**
  223. 设置并启用软件SPI
  224. @api spi.createSoft(cs, mosi, miso, clk, CPHA, CPOL, dataw, bitdict, ms, mode)
  225. @int cs引脚编号,传入nil意为Lua控制cs脚
  226. @int mosi引脚编号
  227. @int miso引脚编号
  228. @int clk引脚编号
  229. @int 默认0,可选0/1
  230. @int 默认0,可选0/1
  231. @int 数据宽度,默认8bit
  232. @int 大小端,默认spi.MSB, 可选spi.LSB
  233. @int 主从设置,默认主1, 可选从机0. 通常只支持主机模式
  234. @int 工作模式,全双工1,半双工0,默认半双工
  235. @return 软件SPI对象 可当作SPI的id使用
  236. @usage
  237. -- 初始化软件spi
  238. local softSpiDevice = spi.createSoft(0, 1, 2, 3, 0, 0, 8, spi.MSB, 1, 1)
  239. local result = spi.send(softSpiDevice, string.char(0x9f))
  240. */
  241. static int l_spi_soft(lua_State *L) {
  242. luat_espi_t *espi = (luat_espi_t *)lua_newuserdata(L, sizeof(luat_espi_t));
  243. espi->cs = luaL_optinteger(L, 1, Luat_GPIO_MAX_ID);
  244. espi->mosi = luaL_checkinteger(L, 2);
  245. espi->miso = luaL_checkinteger(L, 3);
  246. espi->clk = luaL_checkinteger(L, 4);
  247. espi->CPHA = luaL_optinteger(L, 5, 0);
  248. espi->CPOL = luaL_optinteger(L, 6, 0);
  249. espi->dataw = luaL_optinteger(L, 7, 8);
  250. espi->bit_dict = luaL_optinteger(L, 8, 1);
  251. espi->master = luaL_optinteger(L, 9, 1);
  252. espi->mode = luaL_optinteger(L, 10, 0);
  253. luat_gpio_mode(espi->cs, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  254. luat_gpio_mode(espi->mosi, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  255. luat_gpio_mode(espi->miso, Luat_GPIO_INPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW);
  256. if (espi->CPOL == 0)
  257. {
  258. luat_gpio_mode(espi->clk, Luat_GPIO_OUTPUT, Luat_GPIO_PULLDOWN, Luat_GPIO_LOW);
  259. }
  260. else if (espi->CPOL == 1)
  261. {
  262. luat_gpio_mode(espi->clk, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  263. }
  264. luaL_setmetatable(L, LUAT_ESPI_TYPE);
  265. return 1;
  266. }
  267. /**
  268. 关闭指定的SPI
  269. @api spi.close(id)
  270. @int SPI号,例如0
  271. @return int 成功返回0,否则返回其他值
  272. @usage
  273. -- 关闭spi0
  274. spi.close(0)
  275. */
  276. static int l_spi_close(lua_State *L) {
  277. if (lua_isinteger(L, 1))
  278. {
  279. int id = luaL_checkinteger(L, 1);
  280. lua_pushinteger(L, luat_spi_close(id));
  281. }
  282. else if (lua_isuserdata(L, 1))
  283. {
  284. luat_spi_device_t *spi_device = (luat_spi_device_t *)luaL_testudata(L, 1, META_SPI);
  285. if (spi_device){
  286. int ret = luat_spi_device_close(spi_device);
  287. lua_pushinteger(L, ret);
  288. }
  289. else {
  290. lua_pushinteger(L, 0);
  291. }
  292. }
  293. else {
  294. lua_pushinteger(L, 0);
  295. }
  296. return 1;
  297. }
  298. /**
  299. 传输SPI数据
  300. @api spi.transfer(id, send_data, send_len, recv_len)
  301. @int SPI号(例如0)或软件SPI对象
  302. @string/zbuff 待发送的数据,如果为zbuff数据,则会从对象所处的指针处开始读
  303. @int 可选。待发送数据的长度,默认为data长度
  304. @int 可选。读取数据的长度,默认为1. 注意, 如果是全双工模式,收发长度必须相等
  305. @return string 读取成功返回字符串,否则返回nil
  306. @usage
  307. -- 初始化spi
  308. spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
  309. local recv = spi.transfer(0, "123")--发送123,并读取数据
  310. local buff = zbuff.create(1024, 0x33) --创建一个初值全为0x33的内存区域
  311. local recv = spi.transfer(0, buff)--把zbuff数据从指针开始,全发出去,并读取数据
  312. */
  313. static int l_spi_transfer(lua_State *L) {
  314. size_t send_length = 0;
  315. const char* send_buff = NULL;
  316. char* recv_buff = NULL;
  317. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  318. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  319. send_buff = (const char*)(buff->addr+buff->cursor);
  320. send_length = buff->len - buff->cursor;
  321. }else{
  322. send_buff = luaL_checklstring(L, 2, &send_length);
  323. }
  324. size_t length = luaL_optinteger(L,3,send_length);
  325. if(length <= send_length)
  326. send_length = length;
  327. size_t recv_length = luaL_optinteger(L,4,1);
  328. //长度为0时,直接返回空字符串
  329. if(send_length == 0){
  330. lua_pushlstring(L, "",0);
  331. return 1;
  332. }
  333. if (recv_length > 0) {
  334. recv_buff = luat_heap_malloc(recv_length);
  335. if(recv_buff == NULL)
  336. return 0;
  337. }
  338. if (lua_isinteger(L, 1))
  339. {
  340. int id = luaL_checkinteger(L, 1);
  341. luat_spi_lock(id);
  342. int ret = luat_spi_transfer(id, send_buff, send_length, recv_buff, recv_length);
  343. luat_spi_unlock(id);
  344. if (ret > 0) {
  345. lua_pushlstring(L, recv_buff, ret);
  346. luat_heap_free(recv_buff);
  347. return 1;
  348. }
  349. }
  350. else
  351. {
  352. // 软SPI
  353. luat_espi_t *espi = toespi(L);
  354. uint8_t csPin = Luat_GPIO_MAX_ID;
  355. if (espi->cs != Luat_GPIO_MAX_ID)
  356. {
  357. csPin = espi->cs;
  358. espi->cs = Luat_GPIO_MAX_ID;
  359. luat_gpio_set(csPin, Luat_GPIO_LOW);
  360. }
  361. // 半双工, 先发后读
  362. if (espi->mode == 0 || (send_length != recv_length)) {
  363. luat_spi_soft_send(espi, send_buff, send_length);
  364. luat_spi_soft_recv(espi, recv_buff, recv_length);
  365. }
  366. // 全双工, 边发边读, 当且仅当收发都是一样长度时,才是全双工
  367. else {
  368. luat_spi_soft_xfer(espi, send_buff, recv_buff, send_length);
  369. }
  370. if (csPin!= Luat_GPIO_MAX_ID)
  371. {
  372. luat_gpio_set(csPin, Luat_GPIO_HIGH);
  373. espi->cs = csPin;
  374. }
  375. lua_pushlstring(L, recv_buff, recv_length);
  376. luat_heap_free(recv_buff);
  377. return 1;
  378. }
  379. luat_heap_free(recv_buff);
  380. return 0;
  381. }
  382. /**
  383. 接收指定长度的SPI数据
  384. @api spi.recv(id, size, buff)
  385. @int SPI号,例如0
  386. @int 数据长度
  387. @userdata zbuff对象,可选,2024.3.29新增
  388. @return string/int 读取成功返回字符串,若传入的是zbuff就返回读取大小,出错返回nil
  389. @usage
  390. -- 初始化spi
  391. spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
  392. -- 接收数据
  393. local recv = spi.recv(0, 4)--接收4字节数据
  394. -- 当传入zbuff参数时,返回值有所不同. 2024.3.29新增
  395. -- 读取成功后, 指针会往后移动len个字节
  396. -- 写入位置以当前used()位置开始, 请务必确保有足够空间写入len长度的数据
  397. local len = spi.recv(0, 4, buff)
  398. */
  399. static int l_spi_recv(lua_State *L) {
  400. luat_zbuff_t* buff = NULL;
  401. char* recv_buff = NULL;
  402. luaL_Buffer b = {0};
  403. int ret = 0;
  404. int len = luaL_optinteger(L, 2, 1);
  405. if (len <= 0) {
  406. return 0;
  407. }
  408. if (lua_isuserdata(L, 3)) {
  409. buff = (luat_zbuff_t*)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE);
  410. recv_buff = (char*)buff->addr + buff->used;
  411. if (buff->len - buff->used < len) {
  412. LLOGW("zbuff放不下 %d %d %d", buff->len - buff->used, len);
  413. return 0;
  414. }
  415. }
  416. else {
  417. luaL_buffinitsize(L, &b, len);
  418. recv_buff = b.b;
  419. }
  420. if(recv_buff == NULL) {
  421. LLOGW("out of memory when malloc spi buff %d", len);
  422. return 0;
  423. }
  424. if (lua_isinteger(L, 1))
  425. {
  426. int id = luaL_checkinteger(L, 1);
  427. luat_spi_lock(id);
  428. ret = luat_spi_recv(id, recv_buff, len);
  429. luat_spi_unlock(id);
  430. b.n = ret;
  431. }
  432. else if (lua_isuserdata(L, 1))
  433. {
  434. luat_spi_device_t *spi_device = (luat_spi_device_t *)luaL_testudata(L, 1, META_SPI);
  435. if (spi_device){
  436. luat_spi_device_recv(spi_device, recv_buff, len);
  437. ret = len;
  438. }
  439. else {
  440. luat_espi_t *espi = (luat_espi_t *)luaL_testudata(L, 1, LUAT_ESPI_TYPE);
  441. if (espi){
  442. luat_spi_soft_recv(espi, recv_buff, len);
  443. ret = len;
  444. }
  445. }
  446. }
  447. if (ret <= 0) {
  448. return 0;
  449. }
  450. if (buff == NULL) {
  451. luaL_pushresult(&b);
  452. }
  453. else {
  454. buff->used += len;
  455. lua_pushinteger(L, len);
  456. }
  457. return 1;
  458. }
  459. /**
  460. 发送SPI数据
  461. @api spi.send(id, data, len)
  462. @int SPI号,例如0
  463. @string/zbuff 待发送的数据,如果为zbuff数据,则会从对象所处的指针处开始读
  464. @int 可选。待发送数据的长度,默认为data长度
  465. @return int 发送结果
  466. @usage
  467. -- 初始化spi
  468. spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
  469. local result = spi.send(0, "123")--发送123
  470. local buff = zbuff.create(1024, 0x33) --创建一个初值全为0x33的内存区域
  471. local result = spi.send(0, buff)--把zbuff数据从指针开始,全发出去
  472. */
  473. static int l_spi_send(lua_State *L) {
  474. size_t len = 0;
  475. const char* send_buff = NULL;
  476. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  477. luat_zbuff_t *buff = (luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  478. send_buff = (const char*)(buff->addr+buff->cursor);
  479. len = buff->len - buff->cursor;
  480. }
  481. else{
  482. send_buff = luaL_checklstring(L, 2, &len);
  483. }
  484. if(lua_isinteger(L,3)){//长度参数
  485. size_t len_temp = luaL_checkinteger(L,3);
  486. if(len_temp < len)
  487. len = len_temp;
  488. }
  489. //长度为0时,直接返回
  490. if(len <= 0){
  491. lua_pushinteger(L,0);
  492. return 1;
  493. }
  494. if (lua_isinteger(L, 1))
  495. {
  496. int id = luaL_checkinteger(L, 1);
  497. luat_spi_lock(id);
  498. int ret = luat_spi_send(id, send_buff, len);
  499. luat_spi_unlock(id);
  500. lua_pushinteger(L, ret);
  501. }
  502. else
  503. {
  504. luat_espi_t *espi = toespi(L);
  505. lua_pushinteger(L, luat_spi_soft_send(espi, send_buff, len));
  506. }
  507. return 1;
  508. }
  509. /**
  510. 设置并启用SPI(对象方式)
  511. @api spi.deviceSetup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)
  512. @int SPI号,例如0
  513. @int CS 片选脚,在w600不可用请填nil
  514. @int CPHA 默认0,可选0/1
  515. @int CPOL 默认0,可选0/1
  516. @int 数据宽度,默认8bit
  517. @int 波特率,默认20M=20000000
  518. @int 大小端, 默认spi.MSB, 可选spi.LSB
  519. @int 主从设置, 默认主1, 可选从机0. 通常只支持主机模式
  520. @int 工作模式, 全双工1, 半双工0, 默认全双工
  521. @return userdata spi_device
  522. @usage
  523. -- 初始化spi
  524. spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,1)
  525. */
  526. static int l_spi_device_setup(lua_State *L) {
  527. int bus_id = luaL_checkinteger(L, 1);
  528. int cs = luaL_optinteger(L, 2, 255); // 默认无
  529. int CPHA = luaL_optinteger(L, 3, 0); // CPHA0
  530. int CPOL = luaL_optinteger(L, 4, 0); // CPOL0
  531. int dataw = luaL_optinteger(L, 5, 8); // 8bit
  532. int bandrate = luaL_optinteger(L, 6, 20000000U); // 20000000U
  533. int bit_dict = luaL_optinteger(L, 7, 1); // MSB=1, LSB=0
  534. int master = luaL_optinteger(L, 8, 1); // master=1,slave=0
  535. int mode = luaL_optinteger(L, 9, 1); // FULL=1, half=0
  536. luat_spi_device_t* spi_device = lua_newuserdata(L, sizeof(luat_spi_device_t));
  537. if (spi_device == NULL)return 0;
  538. memset(spi_device, 0, sizeof(luat_spi_device_t));
  539. spi_device->bus_id = bus_id;
  540. spi_device->spi_config.cs = cs;
  541. spi_device->spi_config.CPHA = CPHA;
  542. spi_device->spi_config.CPOL = CPOL;
  543. spi_device->spi_config.dataw = dataw;
  544. spi_device->spi_config.bandrate = bandrate;
  545. spi_device->spi_config.bit_dict = bit_dict;
  546. spi_device->spi_config.master = master;
  547. spi_device->spi_config.mode = mode;
  548. luat_spi_device_setup(spi_device);
  549. luaL_setmetatable(L, META_SPI);
  550. return 1;
  551. }
  552. /**
  553. 关闭指定的SPI(对象方式)
  554. @api spi_device:close()
  555. @userdata spi_device
  556. @return int 成功返回0,否则返回其他值
  557. @usage
  558. -- 初始化spi
  559. spi_device:close()
  560. */
  561. static int l_spi_device_close(lua_State *L) {
  562. luat_spi_device_t* spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  563. int ret = luat_spi_device_close(spi_device);
  564. lua_pushboolean(L, ret == 0 ? 1 : 0);
  565. return 1;
  566. }
  567. /**
  568. 传输SPI数据(对象方式)
  569. @api spi_device:transfer(send_data, send_len, recv_len)
  570. @userdata spi_device
  571. @string/zbuff 待发送的数据,如果为zbuff数据,则会从对象所处的指针处开始读
  572. @int 可选。待发送数据的长度,默认为data长度
  573. @int 可选。读取数据的长度,默认为1. 注意, 如果是全双工模式,收发长度必须相等
  574. @return string 读取成功返回字符串,否则返回nil
  575. @usage
  576. -- 初始化spi
  577. spi_device = spi.device_setup(0,17,0,0,8,2000000,spi.MSB,1,1)
  578. local recv = spi_device:transfer("123")--发送123,并读取数据
  579. local result = spi_device:transfer({0x00,0x01})--发送0x00,0x01,并读取数据
  580. local buff = zbuff.create(1024, 0x33) --创建一个初值全为0x33的内存区域
  581. local recv = spi_device:transfer(buff)--把zbuff数据从指针开始,全发出去,并读取数据
  582. */
  583. static int l_spi_device_transfer(lua_State *L) {
  584. luat_spi_device_t* spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  585. size_t send_length = 0;
  586. char* send_buff = NULL;
  587. char* recv_buff = NULL;
  588. uint8_t send_mode = 0;
  589. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  590. luat_zbuff_t *buff = (luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  591. send_buff = (char*)(buff->addr+buff->cursor);
  592. send_length = buff->len - buff->cursor;
  593. }else if (lua_istable(L, 2)){
  594. send_mode = LUA_TTABLE;
  595. send_length = lua_rawlen(L, 2); //返回数组的长度
  596. send_buff = (char*)luat_heap_malloc(send_length);
  597. for (size_t i = 0; i < send_length; i++){
  598. lua_rawgeti(L, 2, 1 + i);
  599. send_buff[i] = (char)lua_tointeger(L, -1);
  600. lua_pop(L, 1); //将刚刚获取的元素值从栈中弹出
  601. }
  602. }else if(lua_isstring(L, 2)){
  603. send_buff = (char*)luaL_checklstring(L, 2, &send_length);
  604. }else{
  605. LLOGE("spi_device transfer first arg error");
  606. return 0;
  607. }
  608. if (lua_isinteger(L, 3)){
  609. send_length = (size_t)lua_tointeger(L, 3);
  610. }
  611. size_t recv_length = luaL_optinteger(L,4,1);
  612. //长度为0时,直接返回空字符串
  613. if(recv_length == 0){
  614. lua_pushlstring(L,NULL,0);
  615. return 1;
  616. }
  617. if (recv_length > 0) {
  618. recv_buff = luat_heap_malloc(recv_length);
  619. if(recv_buff == NULL)
  620. return 0;
  621. }
  622. int ret = luat_spi_device_transfer(spi_device, send_buff, send_length, recv_buff, recv_length);
  623. if (send_mode == LUA_TTABLE){
  624. luat_heap_free(send_buff);
  625. }
  626. if (ret > 0) {
  627. lua_pushlstring(L, recv_buff, ret);
  628. luat_heap_free(recv_buff);
  629. return 1;
  630. }
  631. luat_heap_free(recv_buff);
  632. return 0;
  633. }
  634. /**
  635. 发送SPI数据(对象方式)
  636. @api spi_device:send(data[, len])
  637. @userdata spi_device
  638. @string/zbuff 待发送的数据,如果为zbuff数据,则会从对象所处的指针处开始读
  639. @return int 发送结果
  640. @usage
  641. -- 初始化spi
  642. spi_device = spi.device_setup(0,17,0,0,8,2000000,spi.MSB,1,1)
  643. local result = spi_device:send("123")--发送123
  644. local result = spi_device:send({0x00,0x01})--发送0x00,0x01
  645. local buff = zbuff.create(1024, 0x33) --创建一个初值全为0x33的内存区域
  646. local result = spi_device:send(buff)--把zbuff数据从指针开始,全发出去
  647. */
  648. static int l_spi_device_send(lua_State *L) {
  649. luat_spi_device_t* spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  650. size_t len = 0;
  651. char* send_buff = NULL;
  652. int ret = 0;
  653. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  654. luat_zbuff_t *buff = (luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  655. send_buff = (char*)(buff->addr+buff->cursor);
  656. len = buff->len - buff->cursor;
  657. ret = luat_spi_device_send(spi_device, send_buff, len);
  658. lua_pushinteger(L, ret);
  659. }else if (lua_istable(L, 2)){
  660. len = lua_rawlen(L, 2); //返回数组的长度
  661. send_buff = (char*)luat_heap_malloc(len);
  662. for (size_t i = 0; i < len; i++){
  663. lua_rawgeti(L, 2, 1 + i);
  664. send_buff[i] = (char)lua_tointeger(L, -1);
  665. lua_pop(L, 1); //将刚刚获取的元素值从栈中弹出
  666. }
  667. ret = luat_spi_device_send(spi_device, send_buff, len);
  668. lua_pushinteger(L, ret);
  669. luat_heap_free(send_buff);
  670. }else if(lua_isstring(L, 2)){
  671. send_buff = (char*)luaL_checklstring(L, 2, &len);
  672. lua_pushinteger(L, luat_spi_device_send(spi_device, send_buff, len));
  673. }else{
  674. LLOGE("spi_device transfer first arg error");
  675. return 0;
  676. }
  677. return 1;
  678. }
  679. /**
  680. 接收指定长度的SPI数据(对象方式)
  681. @api spi_device:recv(size)
  682. @userdata spi_device
  683. @int 数据长度
  684. @return string 读取成功返回字符串,否则返回nil
  685. @usage
  686. -- 初始化spi
  687. spi_device = spi.device_setup(0,17,0,0,8,2000000,spi.MSB,1,1)
  688. local recv = spi_device:recv(4)--接收4字节数据
  689. */
  690. static int l_spi_device_recv(lua_State *L) {
  691. luat_spi_device_t* spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  692. int len = luaL_optinteger(L, 2,1);
  693. char* recv_buff = luat_heap_malloc(len);
  694. if(recv_buff == NULL) return 0;
  695. int ret = luat_spi_device_recv(spi_device, recv_buff, len);
  696. if (ret > 0) {
  697. lua_pushlstring(L, recv_buff, ret);
  698. luat_heap_free(recv_buff);
  699. return 1;
  700. }
  701. luat_heap_free(recv_buff);
  702. return 0;
  703. }
  704. static int _spi_struct_gc(lua_State *L) {
  705. luat_spi_device_t* spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  706. LLOGW("spi_device对象正在被回收,相关内存将释放 %p , spi id=%d", spi_device, spi_device->bus_id);
  707. return 0;
  708. }
  709. int _spi_struct_newindex(lua_State *L) {
  710. const char* key = luaL_checkstring(L, 2);
  711. if (!strcmp("close", key)) {
  712. lua_pushcfunction(L, l_spi_device_close);
  713. return 1;
  714. }
  715. else if (!strcmp("transfer", key)) {
  716. lua_pushcfunction(L, l_spi_device_transfer);
  717. return 1;
  718. }
  719. else if (!strcmp("send", key)) {
  720. lua_pushcfunction(L, l_spi_device_send);
  721. return 1;
  722. }
  723. else if (!strcmp("recv", key)) {
  724. lua_pushcfunction(L, l_spi_device_recv);
  725. return 1;
  726. }
  727. return 0;
  728. }
  729. void luat_spi_struct_init(lua_State *L) {
  730. luaL_newmetatable(L, META_SPI);
  731. lua_pushcfunction(L, _spi_struct_gc);
  732. lua_setfield( L, -2, "__gc" );
  733. lua_pushcfunction(L, _spi_struct_newindex);
  734. lua_setfield( L, -2, "__index" );
  735. lua_pop(L, 1);
  736. }
  737. void luat_soft_spi_struct_init(lua_State *L) {
  738. luaL_newmetatable(L, LUAT_ESPI_TYPE);
  739. lua_pop(L, 1);
  740. }
  741. int LUAT_WEAK luat_spi_no_block_transfer(int id, uint8_t *tx_buff, uint8_t *rx_buff, size_t len, void *CB, void *pParam)
  742. {
  743. return -1;
  744. }
  745. /**
  746. 非阻塞方式硬件SPI传输SPI数据,目的为了提高核心利用率。API直接返回是否启动传输,传输完成后通过topic回调,本API适合硬件SPI传输大量数据传输,外设功能(LCD SPI,W5500 SPI之类的)占据的SPI和软件SPI不能用,少量数据传输建议使用传统阻塞型API
  747. @api spi.xfer(id, txbuff, rxbuff, rx_len, transfer_done_topic)
  748. @userdata or int spi_device或者spi_id,注意,如果是spi_device,需要手动在传输完成后拉高cs!!!!!!
  749. @zbuff 待发送的数据,如果为nil,则只接收数据,由于用的非阻塞模型,为保证动态数据的有效性,只能使用zbuff,发送的数据从zbuff.addr
  750. @zbuff 接收数据,如果为nil,则只发送数据,由于用的非阻塞模型,为保证动态数据的有效性,只能使用zbuff,接收的数据从zbuff.addr开始存储
  751. @int 传输数据长度,特别说明 如果为半双工,先发后收,比如spi flash操作这种,则长度=发送字节+接收字节,注意上面发送和接收buff都要留足够的数据,后续接收数据处理需要跳过发送数据长度字节
  752. @string 传输完成后回调的topic
  753. @return boolean true/false 本次传输是否正确启动,true,启动,false,有错误无法启动。传输完成会发布消息transfer_done_topic和boolean型结果
  754. @usage
  755. local result = spi.xfer(spi.SPI_0, txbuff, rxbuff, 1024, "SPIDONE") if result then result, spi_id, succ, error_code = sys.waitUntil("SPIDONE") end if not result or not succ then log.info("spi fail, error code", error_code) else log.info("spi ok") end
  756. */
  757. static int l_spi_no_block_transfer(lua_State *L)
  758. {
  759. size_t topic_len = 0;
  760. const char *topic = luaL_checklstring(L, 5, &topic_len);
  761. size_t len = luaL_optinteger(L, 4, 0);
  762. int result = -1;
  763. uint8_t *tx_buff = NULL;
  764. uint8_t *rx_buff = NULL;
  765. luat_zbuff_t *txbuff = ((luat_zbuff_t *)luaL_testudata(L, 2, LUAT_ZBUFF_TYPE));
  766. luat_zbuff_t *rxbuff = ((luat_zbuff_t *)luaL_testudata(L, 3, LUAT_ZBUFF_TYPE));
  767. if ((!txbuff && !rxbuff) || !len) {
  768. lua_pushboolean(L, 0);
  769. return 1;
  770. }
  771. if (txbuff) tx_buff = txbuff->addr;
  772. if (rxbuff) rx_buff = rxbuff->addr;
  773. char *cb_topic = luat_heap_malloc(topic_len + 1);
  774. memcpy(cb_topic, topic, topic_len);
  775. cb_topic[topic_len] = 0;
  776. if (lua_isinteger(L, 1)) {
  777. int id = luaL_checkinteger(L, 1);
  778. result = luat_spi_no_block_transfer(id, tx_buff, rx_buff, len, luat_irq_hardware_cb_handler, cb_topic);
  779. }
  780. else {
  781. luat_spi_device_t* spi_dev = (luat_spi_device_t*)lua_touserdata(L, 1);
  782. luat_spi_device_config(spi_dev);
  783. luat_gpio_set(spi_dev->spi_config.cs, 0);
  784. result = luat_spi_no_block_transfer(spi_dev->bus_id, tx_buff, rx_buff, len, luat_irq_hardware_cb_handler, cb_topic);
  785. }
  786. if (result) {
  787. luat_heap_free(cb_topic);
  788. }
  789. lua_pushboolean(L, !result);
  790. return 1;
  791. }
  792. //------------------------------------------------------------------
  793. #include "rotable2.h"
  794. static const rotable_Reg_t reg_spi[] =
  795. {
  796. { "setup" , ROREG_FUNC(l_spi_setup)},
  797. { "createSoft", ROREG_FUNC(l_spi_soft) },
  798. { "close", ROREG_FUNC(l_spi_close)},
  799. { "transfer", ROREG_FUNC(l_spi_transfer)},
  800. { "recv", ROREG_FUNC(l_spi_recv)},
  801. { "send", ROREG_FUNC(l_spi_send)},
  802. { "deviceSetup", ROREG_FUNC(l_spi_device_setup)},
  803. { "deviceTransfer", ROREG_FUNC(l_spi_device_transfer)},
  804. { "deviceSend", ROREG_FUNC(l_spi_device_send)},
  805. { "xfer", ROREG_FUNC(l_spi_no_block_transfer)},
  806. //@const MSB number 大端模式
  807. { "MSB", ROREG_INT(1)},
  808. //@const LSB number 小端模式
  809. { "LSB", ROREG_INT(0)},
  810. //@const master number 主机模式
  811. { "master", ROREG_INT(1)},
  812. //@const slave number 从机模式
  813. { "slave", ROREG_INT(0)},
  814. //@const full number 全双工
  815. { "full", ROREG_INT(1)},
  816. //@const half number 半双工
  817. { "half", ROREG_INT(0)},
  818. //@const SPI_0 number SPI0
  819. { "SPI_0", ROREG_INT(0)},
  820. //@const SPI_1 number SPI1
  821. { "SPI_1", ROREG_INT(1)},
  822. //@const SPI_2 number SPI2
  823. { "SPI_2", ROREG_INT(2)},
  824. //@const SPI_3 number SPI3
  825. { "SPI_3", ROREG_INT(3)},
  826. //@const SPI_4 number SPI4
  827. { "SPI_4", ROREG_INT(4)},
  828. //@const HSPI_0 number 高速SPI0,目前105专用
  829. { "HSPI_0", ROREG_INT(5)},
  830. { NULL, ROREG_INT(0) }
  831. };
  832. LUAMOD_API int luaopen_spi( lua_State *L ) {
  833. luat_newlib2(L, reg_spi);
  834. luat_spi_struct_init(L);
  835. luat_soft_spi_struct_init(L);
  836. return 1;
  837. }