luat_lib_spi.c 27 KB

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