luat_lib_i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. @module i2c
  3. @summary I2C操作
  4. @version 1.0
  5. @date 2020.03.30
  6. */
  7. #include "luat_base.h"
  8. #include "luat_log.h"
  9. #include "luat_timer.h"
  10. #include "luat_malloc.h"
  11. #include "luat_i2c.h"
  12. #include "luat_gpio.h"
  13. #include "luat_zbuff.h"
  14. #define LUAT_LOG_TAG "i2c"
  15. #include "luat_log.h"
  16. static void i2c_soft_start(luat_ei2c *ei2c)
  17. {
  18. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  19. luat_timer_us_delay(5);
  20. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  21. luat_timer_us_delay(5);
  22. luat_gpio_set(ei2c->sda, Luat_GPIO_LOW);
  23. luat_timer_us_delay(5);
  24. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  25. luat_timer_us_delay(5);
  26. }
  27. static void i2c_soft_stop(luat_ei2c *ei2c)
  28. {
  29. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  30. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  31. luat_timer_us_delay(5);
  32. luat_gpio_set(ei2c->sda, Luat_GPIO_LOW);
  33. luat_timer_us_delay(5);
  34. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  35. luat_timer_us_delay(5);
  36. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  37. luat_timer_us_delay(5);
  38. }
  39. static unsigned char i2c_soft_wait_ack(luat_ei2c *ei2c)
  40. {
  41. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  42. luat_gpio_mode(ei2c->sda, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, 1);
  43. luat_timer_us_delay(5);
  44. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  45. luat_timer_us_delay(15);
  46. int max_wait_time = 3000;
  47. while (max_wait_time--)
  48. {
  49. if (luat_gpio_get(ei2c->sda) == Luat_GPIO_LOW)
  50. {
  51. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  52. return 1;
  53. }
  54. luat_timer_us_delay(1);
  55. }
  56. i2c_soft_stop(ei2c);
  57. return 0;
  58. }
  59. static void i2c_soft_send_ack(luat_ei2c *ei2c)
  60. {
  61. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0);
  62. luat_timer_us_delay(5);
  63. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  64. luat_timer_us_delay(5);
  65. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  66. }
  67. static void i2c_soft_send_noack(luat_ei2c *ei2c)
  68. {
  69. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  70. luat_timer_us_delay(5);
  71. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  72. luat_timer_us_delay(5);
  73. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  74. }
  75. static void i2c_soft_send_byte(luat_ei2c *ei2c, unsigned char data)
  76. {
  77. unsigned char i = 8;
  78. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0);
  79. while (i--)
  80. {
  81. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  82. luat_timer_us_delay(10);
  83. if (data & 0x80)
  84. {
  85. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  86. }
  87. else
  88. {
  89. luat_gpio_set(ei2c->sda, Luat_GPIO_LOW);
  90. }
  91. luat_timer_us_delay(5);
  92. data <<= 1;
  93. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  94. luat_timer_us_delay(5);
  95. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  96. luat_timer_us_delay(5);
  97. }
  98. }
  99. static char i2c_soft_recv_byte(luat_ei2c *ei2c)
  100. {
  101. unsigned char i = 8;
  102. unsigned char data = 0;
  103. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  104. luat_gpio_mode(ei2c->sda, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, 1);
  105. while (i--)
  106. {
  107. data <<= 1;
  108. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  109. luat_timer_us_delay(5);
  110. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  111. luat_timer_us_delay(5);
  112. if (luat_gpio_get(ei2c->sda))
  113. data |= 0x01;
  114. }
  115. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  116. return (data);
  117. }
  118. static char i2c_soft_recv(luat_ei2c *ei2c, unsigned char addr, char *buff, size_t len)
  119. {
  120. size_t i;
  121. i2c_soft_start(ei2c);
  122. i2c_soft_send_byte(ei2c, (addr << 1) + 1);
  123. if (!i2c_soft_wait_ack(ei2c))
  124. {
  125. i2c_soft_stop(ei2c);
  126. return -1;
  127. }
  128. luat_timer_us_delay(50);
  129. for (i = 0; i < len; i++)
  130. {
  131. *buff++ = i2c_soft_recv_byte(ei2c);
  132. if (i < (len - 1))
  133. i2c_soft_send_ack(ei2c);
  134. }
  135. i2c_soft_send_noack(ei2c);
  136. i2c_soft_stop(ei2c);
  137. return 0;
  138. }
  139. static char i2c_soft_send(luat_ei2c *ei2c, unsigned char addr, char *data, size_t len, uint8_t stop)
  140. {
  141. size_t i;
  142. i2c_soft_start(ei2c);
  143. i2c_soft_send_byte(ei2c, addr << 1);
  144. if (!i2c_soft_wait_ack(ei2c))
  145. {
  146. i2c_soft_stop(ei2c);
  147. return -1;
  148. }
  149. for (i = 0; i < len; i++)
  150. {
  151. i2c_soft_send_byte(ei2c, data[i]);
  152. if (!i2c_soft_wait_ack(ei2c))
  153. {
  154. i2c_soft_stop(ei2c);
  155. return -1;
  156. }
  157. }
  158. if (stop){
  159. i2c_soft_stop(ei2c);
  160. }
  161. return 0;
  162. }
  163. /*
  164. i2c编号是否存在
  165. @api i2c.exist(id)
  166. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  167. @return int 存在就返回1,否则返回0
  168. @usage
  169. -- 检查i2c1是否存在
  170. if i2c.exist(1) then
  171. log.info("存在 i2c1")
  172. end
  173. */
  174. static int l_i2c_exist(lua_State *L)
  175. {
  176. int re = luat_i2c_exist(luaL_checkinteger(L, 1));
  177. lua_pushinteger(L, re);
  178. return 1;
  179. }
  180. /*
  181. i2c初始化
  182. @api i2c.setup(id, speed, slaveAddr)
  183. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  184. @int I2C速度, 例如i2c.FAST
  185. @int 从设备地址(7位), 例如0x38
  186. @return int 成功就返回1,否则返回0
  187. @usage
  188. -- 初始化i2c1
  189. if i2c.setup(1, i2c.FAST, 0x38) == 1 then
  190. log.info("存在 i2c1")
  191. else
  192. i2c.close(1) -- 关掉
  193. end
  194. */
  195. static int l_i2c_setup(lua_State *L)
  196. {
  197. int re = luat_i2c_setup(luaL_checkinteger(L, 1), luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, 0));
  198. lua_pushinteger(L, re == 0 ? luaL_optinteger(L, 2, 0) : -1);
  199. return 1;
  200. }
  201. /*
  202. 新建一个软件i2c对象
  203. @api i2c.createSoft(scl,sda,slaveAddr)
  204. @int i2c SCL引脚编号
  205. @int i2c SDA引脚编号
  206. @int 从设备地址(7位), 例如0x38
  207. @return 软件I2C对象 可当作i2c的id使用
  208. @usage
  209. -- 注意!这个接口是软件模拟i2c,速度可能会比硬件的慢
  210. -- 不需要调用i2c.close接口
  211. -- 初始化软件i2c
  212. local softI2C = i2c.createSoft(1,2,0x38)
  213. i2c.send(softI2C, 0x5C, string.char(0x0F, 0x2F))
  214. */
  215. static int l_i2c_soft(lua_State *L)
  216. {
  217. luat_ei2c *ei2c = (luat_ei2c *)lua_newuserdata(L, sizeof(luat_ei2c));
  218. ei2c->scl = luaL_checkinteger(L, 1);
  219. ei2c->sda = luaL_checkinteger(L, 2);
  220. ei2c->addr = luaL_checkinteger(L, 3);
  221. luat_gpio_mode(ei2c->scl, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  222. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  223. i2c_soft_stop(ei2c);
  224. luaL_setmetatable(L, LUAT_EI2C_TYPE);
  225. return 1;
  226. }
  227. /*
  228. i2c发送数据
  229. @api i2c.send(id, addr, data,stop)
  230. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  231. @int I2C子设备的地址, 7位地址
  232. @integer/string/table 待发送的数据,自适应参数类型
  233. @integer 可选参数 是否发送停止位 1发送 0不发送 默认发送(105不支持)
  234. @return true/false 发送是否成功
  235. @usage
  236. -- 往i2c0发送1个字节的数据
  237. i2c.send(0, 0x68, 0x75)
  238. -- 往i2c1发送2个字节的数据
  239. i2c.send(1, 0x5C, string.char(0x0F, 0x2F))
  240. i2c.send(1, 0x5C, {0x0F, 0x2F})
  241. */
  242. static int l_i2c_send(lua_State *L)
  243. {
  244. int id = 0;
  245. if (!lua_isuserdata(L, 1))
  246. {
  247. id = luaL_checkinteger(L, 1);
  248. }
  249. int addr = luaL_checkinteger(L, 2);
  250. size_t len = 0;
  251. int result = 0;
  252. int stop = luaL_optnumber(L, 4 , 1);
  253. if (lua_isinteger(L, 3))
  254. {
  255. char buff = (char)luaL_checkinteger(L, 3);
  256. if (lua_isuserdata(L, 1))
  257. {
  258. luat_ei2c *ei2c = toei2c(L);
  259. result = i2c_soft_send(ei2c, addr, &buff, 1,stop);
  260. }
  261. else
  262. {
  263. result = luat_i2c_send(id, addr, &buff, 1,stop);
  264. }
  265. // luat_heap_free(buff);
  266. }
  267. else if (lua_isstring(L, 3))
  268. {
  269. const char *buff = luaL_checklstring(L, 3, &len);
  270. if (lua_isuserdata(L, 1))
  271. {
  272. luat_ei2c *ei2c = toei2c(L);
  273. result = i2c_soft_send(ei2c, addr, (char *)buff, len,stop);
  274. }
  275. else
  276. {
  277. result = luat_i2c_send(id, addr, (char *)buff, len,stop);
  278. }
  279. }
  280. else if (lua_istable(L, 3))
  281. {
  282. const int len = lua_rawlen(L, 3); //返回数组的长度
  283. char *buff = (char *)luat_heap_malloc(len);
  284. for (size_t i = 0; i < len; i++)
  285. {
  286. lua_rawgeti(L, 3, 1 + i);
  287. buff[i] = (char)lua_tointeger(L, -1);
  288. lua_pop(L, 1); //将刚刚获取的元素值从栈中弹出
  289. }
  290. if (lua_isuserdata(L, 1))
  291. {
  292. luat_ei2c *ei2c = toei2c(L);
  293. result = i2c_soft_send(ei2c, addr, buff, len,stop);
  294. }
  295. else
  296. {
  297. result = luat_i2c_send(id, addr, buff, len,stop);
  298. }
  299. luat_heap_free(buff);
  300. }
  301. else
  302. {
  303. if (lua_isuserdata(L, 1))
  304. {
  305. luat_ei2c *ei2c = toei2c(L);
  306. result = i2c_soft_send(ei2c, addr, NULL, 0,stop);
  307. }
  308. else
  309. {
  310. result = luat_i2c_send(id, addr, NULL, 0,stop);
  311. }
  312. }
  313. lua_pushboolean(L, result == 0);
  314. return 1;
  315. }
  316. /*
  317. i2c接收数据
  318. @api i2c.recv(id, addr, len)
  319. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  320. @int I2C子设备的地址, 7位地址
  321. @int 接收数据的长度
  322. @return string 收到的数据
  323. @usage
  324. -- 从i2c1读取2个字节的数据
  325. local data = i2c.recv(1, 0x5C, 2)
  326. */
  327. static int l_i2c_recv(lua_State *L)
  328. {
  329. int id = 0;
  330. if (!lua_isuserdata(L, 1))
  331. {
  332. id = luaL_checkinteger(L, 1);
  333. }
  334. int addr = luaL_checkinteger(L, 2);
  335. int len = luaL_checkinteger(L, 3);
  336. char *buff = (char *)luat_heap_malloc(len);
  337. int result;
  338. if (lua_isuserdata(L, 1))
  339. {
  340. luat_ei2c *ei2c = toei2c(L);
  341. result = i2c_soft_recv(ei2c, addr, buff, len);
  342. }
  343. else
  344. {
  345. result = luat_i2c_recv(id, addr, buff, len);
  346. }
  347. if (result != 0)
  348. { //如果返回值不为0,说明收失败了
  349. len = 0;
  350. LLOGD("i2c receive result %d", result);
  351. }
  352. lua_pushlstring(L, buff, len);
  353. luat_heap_free(buff);
  354. return 1;
  355. }
  356. /*
  357. i2c写寄存器数据
  358. @api i2c.writeReg(id, addr, reg, data,stop)
  359. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  360. @int I2C子设备的地址, 7位地址
  361. @int 寄存器地址
  362. @string 待发送的数据
  363. @integer 可选参数 是否发送停止位 1发送 0不发送 默认发送(105不支持)
  364. @return true/false 发送是否成功
  365. @usage
  366. -- 从i2c1的地址为0x5C的设备的寄存器0x01写入2个字节的数据
  367. i2c.writeReg(1, 0x5C, 0x01, string.char(0x00, 0xF2))
  368. */
  369. static int l_i2c_write_reg(lua_State *L)
  370. {
  371. int id = 0;
  372. if (!lua_isuserdata(L, 1))
  373. {
  374. id = luaL_checkinteger(L, 1);
  375. }
  376. int addr = luaL_checkinteger(L, 2);
  377. int reg = luaL_checkinteger(L, 3);
  378. size_t len;
  379. const char *lb = luaL_checklstring(L, 4, &len);
  380. int stop = luaL_optnumber(L, 5 , 1);
  381. char *buff = (char *)luat_heap_malloc(sizeof(char) * len + 1);
  382. *buff = (char)reg;
  383. memcpy(buff + 1, lb, sizeof(char) + len + 1);
  384. int result;
  385. if (lua_isuserdata(L, 1))
  386. {
  387. luat_ei2c *ei2c = toei2c(L);
  388. result = i2c_soft_send(ei2c, addr, buff, len + 1,stop);
  389. }
  390. else
  391. {
  392. result = luat_i2c_send(id, addr, buff, len + 1,stop);
  393. }
  394. luat_heap_free(buff);
  395. lua_pushboolean(L, result == 0);
  396. return 1;
  397. }
  398. /*
  399. i2c读寄存器数据
  400. @api i2c.readReg(id, addr, reg, len)
  401. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  402. @int I2C子设备的地址, 7位地址
  403. @int 寄存器地址
  404. @int 待接收的数据长度
  405. @integer 可选参数 是否发送停止位 1发送 0不发送 默认发送(105不支持)
  406. @return string 收到的数据
  407. @usage
  408. -- 从i2c1的地址为0x5C的设备的寄存器0x01读出2个字节的数据
  409. i2c.readReg(1, 0x5C, 0x01, 2)
  410. */
  411. static int l_i2c_read_reg(lua_State *L)
  412. {
  413. int id = 0;
  414. if (!lua_isuserdata(L, 1))
  415. {
  416. id = luaL_checkinteger(L, 1);
  417. }
  418. int addr = luaL_checkinteger(L, 2);
  419. int reg = luaL_checkinteger(L, 3);
  420. int len = luaL_checkinteger(L, 4);
  421. int stop = luaL_optnumber(L, 5 , 0);
  422. char temp = (char)reg;
  423. int result;
  424. if (lua_isuserdata(L, 1))
  425. {
  426. luat_ei2c *ei2c = toei2c(L);
  427. result = i2c_soft_send(ei2c, addr, &temp, 1,stop);
  428. }
  429. else
  430. {
  431. result = luat_i2c_send(id, addr, &temp, 1,stop);
  432. }
  433. if (result != 0)
  434. { //如果返回值不为0,说明收失败了
  435. LLOGD("i2c send result %d", result);
  436. lua_pushlstring(L, NULL, 0);
  437. return 1;
  438. }
  439. char *buff = (char *)luat_heap_malloc(sizeof(char) * len);
  440. if (lua_isuserdata(L, 1))
  441. {
  442. luat_ei2c *ei2c = toei2c(L);
  443. result = i2c_soft_recv(ei2c, addr, buff, len);
  444. }
  445. else
  446. {
  447. result = luat_i2c_recv(id, addr, buff, len);
  448. }
  449. if (result != 0)
  450. { //如果返回值不为0,说明收失败了
  451. len = 0;
  452. LLOGD("i2c receive result %d", result);
  453. }
  454. lua_pushlstring(L, buff, len);
  455. luat_heap_free(buff);
  456. return 1;
  457. }
  458. /*
  459. 关闭i2c设备
  460. @api i2c.close(id)
  461. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  462. @return nil 无返回值
  463. @usage
  464. -- 关闭i2c1
  465. i2c.close(1)
  466. */
  467. static int l_i2c_close(lua_State *L)
  468. {
  469. int id = luaL_checkinteger(L, 1);
  470. luat_i2c_close(id);
  471. return 0;
  472. }
  473. /*
  474. 从i2c总线读取DHT12的温湿度数据
  475. @api i2c.readDHT12(id)
  476. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  477. @int DHT12的设备地址,默认0x5C
  478. @return boolean 读取成功返回true,否则返回false
  479. @return int 湿度值,单位0.1%, 例如 591 代表 59.1%
  480. @return int 温度值,单位0.1摄氏度, 例如 292 代表 29.2摄氏度
  481. @usage
  482. -- 从i2c0读取DHT12
  483. i2c.setup(0)
  484. local re, H, T = i2c.readDHT12(0)
  485. if re then
  486. log.info("dht12", H, T)
  487. end
  488. */
  489. static int l_i2c_readDHT12(lua_State *L)
  490. {
  491. int id = 0;
  492. if (!lua_isuserdata(L, 1))
  493. {
  494. id = luaL_checkinteger(L, 1);
  495. }
  496. int addr = luaL_optinteger(L, 2, 0x5C);
  497. char buff[5] = {0};
  498. char temp = 0x00;
  499. int result = -1;
  500. if (lua_isuserdata(L, 1))
  501. {
  502. luat_ei2c *ei2c = toei2c(L);
  503. result = i2c_soft_send(ei2c, addr, &temp, 1,1);
  504. }
  505. else
  506. {
  507. result = luat_i2c_send(id, addr, &temp, 1,1);
  508. }
  509. if (result != 0)
  510. {
  511. LLOGD("DHT12 i2c bus write fail");
  512. lua_pushboolean(L, 0);
  513. return 1;
  514. }
  515. if (lua_isuserdata(L, 1))
  516. {
  517. luat_ei2c *ei2c = toei2c(L);
  518. result = i2c_soft_recv(ei2c, addr, buff, 5);
  519. }
  520. else
  521. {
  522. result = luat_i2c_recv(id, addr, buff, 5);
  523. }
  524. if (result != 0)
  525. {
  526. lua_pushboolean(L, 0);
  527. return 1;
  528. }
  529. else
  530. {
  531. if (buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 0 && buff[4] == 0)
  532. {
  533. LLOGD("DHT12 DATA emtry");
  534. lua_pushboolean(L, 0);
  535. return 1;
  536. }
  537. // 检查crc值
  538. LLOGD("DHT12 DATA %02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4]);
  539. uint8_t crc_act = (uint8_t)buff[0] + (uint8_t)buff[1] + (uint8_t)buff[2] + (uint8_t)buff[3];
  540. uint8_t crc_exp = (uint8_t)buff[4];
  541. if (crc_act != crc_exp)
  542. {
  543. LLOGD("DATA12 DATA crc not ok");
  544. lua_pushboolean(L, 0);
  545. return 1;
  546. }
  547. lua_pushboolean(L, 1);
  548. lua_pushinteger(L, (uint8_t)buff[0] * 10 + (uint8_t)buff[1]);
  549. if (((uint8_t)buff[2]) > 127)
  550. lua_pushinteger(L, ((uint8_t)buff[2] - 128) * -10 + (uint8_t)buff[3]);
  551. else
  552. lua_pushinteger(L, (uint8_t)buff[2] * 10 + (uint8_t)buff[3]);
  553. return 3;
  554. }
  555. }
  556. /*
  557. 从i2c总线读取DHT30的温湿度数据(由"好奇星"贡献)
  558. @api i2c.readSHT30(id,addr)
  559. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  560. @int 设备addr,SHT30的设备地址,默认0x44 bit7
  561. @return boolean 读取成功返回true,否则返回false
  562. @return int 湿度值,单位0.1%, 例如 591 代表 59.1%
  563. @return int 温度值,单位0.1摄氏度, 例如 292 代表 29.2摄氏度
  564. @usage
  565. -- 从i2c0读取SHT30
  566. i2c.setup(0)
  567. local re, H, T = i2c.readSHT30(0)
  568. if re then
  569. log.info("sht30", H, T)
  570. end
  571. */
  572. static int l_i2c_readSHT30(lua_State *L)
  573. {
  574. char buff[7] = {0x2c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00};
  575. float temp = 0x00;
  576. float hum = 0x00;
  577. int result = -1;
  578. if (lua_isuserdata(L, 1))
  579. {
  580. luat_ei2c *ei2c = toei2c(L);
  581. i2c_soft_send(ei2c, ei2c->addr, buff, 2,1);
  582. luat_timer_mdelay(13);
  583. result = i2c_soft_recv(ei2c, ei2c->addr, buff, 6);
  584. }
  585. else
  586. {
  587. int id = luaL_optinteger(L, 1, 0);
  588. int addr = luaL_optinteger(L, 2, 0x44);
  589. luat_i2c_send(id, addr, &buff, 2,1);
  590. luat_timer_mdelay(1);
  591. result = luat_i2c_recv(id, addr, buff, 6);
  592. }
  593. if (result != 0)
  594. {
  595. lua_pushboolean(L, 0);
  596. return 1;
  597. }
  598. else
  599. {
  600. if (buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 0 && buff[4] == 0)
  601. {
  602. LLOGD("SHT30 DATA emtry");
  603. lua_pushboolean(L, 0);
  604. return 1;
  605. }
  606. // 检查crc值
  607. // LLOGD("SHT30 DATA %02X %02X %02X %02X %02X %02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  608. temp = ((((buff[0] * 256) + buff[1]) * 175) / 6553.5) - 450;
  609. hum = ((((buff[3] * 256) + buff[4]) * 100) / 6553.5);
  610. // LLOGD("\r\n SHT30 %d deg %d Hum ", temp, hum);
  611. // 跳过CRC
  612. // uint8_t crc_act = (uint8_t)buff[0] + (uint8_t)buff[1] + (uint8_t)buff[2] + (uint8_t)buff [3];
  613. // uint8_t crc_exp = (uint8_t)buff[4];
  614. // if (crc_act != crc_exp) {
  615. // LLOGD("DATA12 DATA crc not ok");
  616. // lua_pushboolean(L, 0);
  617. // return 1;
  618. // }
  619. // Convert the data
  620. lua_pushboolean(L, 1);
  621. lua_pushinteger(L, (int)hum);
  622. lua_pushinteger(L, (int)temp);
  623. return 3;
  624. // 华氏度
  625. // fTemp = (cTemp * 1.8) + 32;
  626. }
  627. }
  628. int LUAT_WEAK luat_i2c_transfer(int id, int addr, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len)
  629. {
  630. int result;
  631. result = luat_i2c_send(id, addr, reg, reg_len, 0);
  632. if (result != 0) return-1;
  633. return luat_i2c_recv(id, addr, buff, len);
  634. }
  635. /**
  636. i2c通用传输,包括发送N字节,发送N字节+接收N字节,接收N字节三种功能,在发送转接收过程中发送reStart信号,解决类似mlx90614必须带restart信号,但是又不能用i2c.send来控制的,比如air105
  637. @api i2c.transfer(id, addr, txBuff, rxBuff, rxLen)
  638. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  639. @int I2C子设备的地址, 7位地址
  640. @integer/string/zbuff 待发送的数据,自适应参数类型,如果为nil,则不发送数据
  641. @zbuff 待接收数据的zbuff 如果不用zbuff,则接收数据将在return返回
  642. @int 需要接收的数据长度,如果为0或nil,则不接收数据
  643. @return
  644. boolean true/false 发送是否成功
  645. string or nil 如果参数4是interger,则返回接收到的数据
  646. @usage
  647. local result, _ = i2c.transfer(0, 0x11, txbuff, rxbuff)
  648. local result, rxdata = i2c.transfer(0, 0x11, "\x01\x02", 1) --发送0x01, 0x02,然后接收1个字节,典型应用就是eeprom
  649. local result, rxdata = i2c.transfer(0, 0x11, 0x00, 1) --发送0x00,然后接收1个字节,典型应用各种传感器
  650. */
  651. static int l_i2c_transfer(lua_State *L)
  652. {
  653. int addr = luaL_checkinteger(L, 2);
  654. size_t tx_len = 0;
  655. size_t rx_len = 0;
  656. int result = 0;
  657. uint8_t temp[1];
  658. uint8_t *tx_buff = NULL;
  659. uint8_t *rx_buff = NULL;
  660. uint8_t tx_heap_flag = 0;
  661. if (lua_isnil(L, 3)) {
  662. tx_len = 0;
  663. }
  664. else if (lua_isinteger(L, 3)) {
  665. temp[0] = luaL_checkinteger(L, 3);
  666. tx_buff = temp;
  667. tx_len = 1;
  668. }
  669. else if (lua_isstring(L, 3)) {
  670. tx_buff = luaL_checklstring(L, 3, &tx_len);
  671. }
  672. else if (lua_istable(L, 3)) {
  673. const int tx_len = lua_rawlen(L, 3); //返回数组的长度
  674. tx_buff = (uint8_t *)luat_heap_malloc(tx_len);
  675. tx_heap_flag = 1;
  676. for (size_t i = 0; i < tx_len; i++)
  677. {
  678. lua_rawgeti(L, 3, 1 + i);
  679. tx_buff[i] = (char)lua_tointeger(L, -1);
  680. lua_pop(L, 1); //将刚刚获取的元素值从栈中弹出
  681. }
  682. }
  683. else {
  684. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE));
  685. tx_buff = buff->addr;
  686. tx_len = buff->used;
  687. }
  688. luat_zbuff_t *rbuff = ((luat_zbuff_t *)luaL_testudata(L, 4, LUAT_ZBUFF_TYPE));
  689. if (lua_isnil(L, 5)) {
  690. rx_len = 0;
  691. }
  692. else if (lua_isinteger(L, 5)) {
  693. rx_len = luaL_checkinteger(L, 5);
  694. if (rx_len) {
  695. if (!rbuff) {
  696. rx_buff = luat_heap_malloc(rx_len);
  697. }
  698. else {
  699. if ((rbuff->used + rx_len) > rbuff->len) {
  700. __zbuff_resize(rbuff, rbuff->len + rx_len);
  701. }
  702. rx_buff = rbuff->addr + rbuff->used;
  703. }
  704. }
  705. }
  706. int id = 0;
  707. if (!lua_isuserdata(L, 1)) {
  708. id = luaL_checkinteger(L, 1);
  709. if (rx_buff && rx_len) {
  710. result = luat_i2c_transfer(id, addr, tx_buff, tx_len, rx_buff, rx_len);
  711. } else {
  712. result = luat_i2c_transfer(id, addr, NULL, 0, tx_buff, tx_len);
  713. }
  714. }
  715. if (tx_heap_flag) {
  716. luat_heap_free(tx_buff);
  717. }
  718. // else if (lua_isuserdata(L, 1))
  719. // {
  720. // luat_ei2c *ei2c = toei2c(L);
  721. // }
  722. lua_pushboolean(L, !result);
  723. if (rx_buff && rx_len) {
  724. if (rbuff) {
  725. rbuff->used += rx_len;
  726. lua_pushnil(L);
  727. } else {
  728. lua_pushlstring(L, (const char *)rx_buff, rx_len);
  729. luat_heap_free(rx_buff);
  730. }
  731. } else {
  732. lua_pushnil(L);
  733. }
  734. return 2;
  735. }
  736. #include "rotable2.h"
  737. static const rotable_Reg_t reg_i2c[] =
  738. {
  739. { "exist", ROREG_FUNC(l_i2c_exist)},
  740. { "setup", ROREG_FUNC(l_i2c_setup)},
  741. { "createSoft", ROREG_FUNC(l_i2c_soft)},
  742. #ifdef __F1C100S__
  743. #else
  744. { "send", ROREG_FUNC(l_i2c_send)},
  745. { "recv", ROREG_FUNC(l_i2c_recv)},
  746. #endif
  747. { "transfer", ROREG_FUNC(l_i2c_transfer)},
  748. { "writeReg", ROREG_FUNC(l_i2c_write_reg)},
  749. { "readReg", ROREG_FUNC(l_i2c_read_reg)},
  750. { "close", ROREG_FUNC(l_i2c_close)},
  751. { "readDHT12", ROREG_FUNC(l_i2c_readDHT12)},
  752. { "readSHT30", ROREG_FUNC(l_i2c_readSHT30)},
  753. { "FAST", ROREG_INT(1)},
  754. { "SLOW", ROREG_INT(0)},
  755. { NULL, ROREG_INT(0) }
  756. };
  757. LUAMOD_API int luaopen_i2c(lua_State *L)
  758. {
  759. luat_newlib2(L, reg_i2c);
  760. luaL_newmetatable(L, LUAT_EI2C_TYPE);
  761. lua_pop(L, 1);
  762. return 1;
  763. }