bmp085.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "platform.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "esp_misc.h"
  7. static const uint32_t bmp085_i2c_id = 0;
  8. static const uint8_t bmp085_i2c_addr = 0x77;
  9. static struct {
  10. int16_t AC1;
  11. int16_t AC2;
  12. int16_t AC3;
  13. uint16_t AC4;
  14. uint16_t AC5;
  15. uint16_t AC6;
  16. int16_t B1;
  17. int16_t B2;
  18. int16_t MB;
  19. int16_t MC;
  20. int16_t MD;
  21. } bmp085_data;
  22. static uint8_t ICACHE_FLASH_ATTR r8u(uint32_t id, uint8_t reg) {
  23. uint8_t ret;
  24. platform_i2c_send_start(id);
  25. platform_i2c_send_address(id, bmp085_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  26. platform_i2c_send_byte(id, reg);
  27. platform_i2c_send_stop(id);
  28. platform_i2c_send_start(id);
  29. platform_i2c_send_address(id, bmp085_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  30. ret = platform_i2c_recv_byte(id, 0);
  31. platform_i2c_send_stop(id);
  32. return ret;
  33. }
  34. static uint16_t ICACHE_FLASH_ATTR r16u(uint32_t id, uint8_t reg) {
  35. uint8_t high = r8u(id, reg);
  36. uint8_t low = r8u(id, reg + 1);
  37. return (high << 8) | low;
  38. }
  39. static int16_t ICACHE_FLASH_ATTR r16(uint32_t id, uint8_t reg) {
  40. return (int16_t) r16u(id, reg);
  41. }
  42. static int ICACHE_FLASH_ATTR bmp085_init(lua_State* L) {
  43. uint32_t sda;
  44. uint32_t scl;
  45. if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
  46. return luaL_error(L, "wrong arg range");
  47. }
  48. sda = luaL_checkinteger(L, 1);
  49. scl = luaL_checkinteger(L, 2);
  50. if (scl == 0 || sda == 0) {
  51. return luaL_error(L, "no i2c for D0");
  52. }
  53. platform_i2c_setup(bmp085_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  54. bmp085_data.AC1 = r16(bmp085_i2c_id, 0xAA);
  55. bmp085_data.AC2 = r16(bmp085_i2c_id, 0xAC);
  56. bmp085_data.AC3 = r16(bmp085_i2c_id, 0xAE);
  57. bmp085_data.AC4 = r16u(bmp085_i2c_id, 0xB0);
  58. bmp085_data.AC5 = r16u(bmp085_i2c_id, 0xB2);
  59. bmp085_data.AC6 = r16u(bmp085_i2c_id, 0xB4);
  60. bmp085_data.B1 = r16(bmp085_i2c_id, 0xB6);
  61. bmp085_data.B2 = r16(bmp085_i2c_id, 0xB8);
  62. bmp085_data.MB = r16(bmp085_i2c_id, 0xBA);
  63. bmp085_data.MC = r16(bmp085_i2c_id, 0xBC);
  64. bmp085_data.MD = r16(bmp085_i2c_id, 0xBE);
  65. return 0;
  66. }
  67. static uint32_t bmp085_temperature_raw_b5(void) {
  68. int16_t t, X1, X2;
  69. platform_i2c_send_start(bmp085_i2c_id);
  70. platform_i2c_send_address(bmp085_i2c_id, bmp085_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  71. platform_i2c_send_byte(bmp085_i2c_id, 0xF4);
  72. platform_i2c_send_byte(bmp085_i2c_id, 0x2E);
  73. platform_i2c_send_stop(bmp085_i2c_id);
  74. // Wait for device to complete sampling
  75. os_delay_us(4500);
  76. t = r16(bmp085_i2c_id, 0xF6);
  77. X1 = ((t - bmp085_data.AC6) * bmp085_data.AC5) >> 15;
  78. X2 = (bmp085_data.MC << 11)/ (X1 + bmp085_data.MD);
  79. return X1 + X2;
  80. }
  81. static int16_t ICACHE_FLASH_ATTR bmp085_temperature(void) {
  82. return (bmp085_temperature_raw_b5() + 8) >> 4;
  83. }
  84. static int ICACHE_FLASH_ATTR bmp085_lua_temperature(lua_State* L) {
  85. lua_pushinteger(L, bmp085_temperature());
  86. return 1;
  87. }
  88. static int32_t ICACHE_FLASH_ATTR bmp085_pressure_raw(int oss) {
  89. int32_t p;
  90. int32_t p1, p2, p3;
  91. platform_i2c_send_start(bmp085_i2c_id);
  92. platform_i2c_send_address(bmp085_i2c_id, bmp085_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  93. platform_i2c_send_byte(bmp085_i2c_id, 0xF4);
  94. platform_i2c_send_byte(bmp085_i2c_id, 0x34 + 64 * oss);
  95. platform_i2c_send_stop(bmp085_i2c_id);
  96. // Wait for device to complete sampling
  97. switch (oss) {
  98. case 0: os_delay_us( 4500); break;
  99. case 1: os_delay_us( 7500); break;
  100. case 2: os_delay_us(13500); break;
  101. case 3: os_delay_us(25500); break;
  102. }
  103. p1 = r8u(bmp085_i2c_id, 0xF6);
  104. p2 = r8u(bmp085_i2c_id, 0xF7);
  105. p3 = r8u(bmp085_i2c_id, 0xF8);
  106. p = (p1 << 16) | (p2 << 8) | p3;
  107. p = p >> (8 - oss);
  108. return p;
  109. }
  110. static int ICACHE_FLASH_ATTR bmp085_lua_pressure_raw(lua_State* L) {
  111. uint8_t oss = 0;
  112. int32_t p;
  113. if (lua_isnumber(L, 1)) {
  114. oss = luaL_checkinteger(L, 1);
  115. if (oss > 3) {
  116. oss = 3;
  117. }
  118. }
  119. p = bmp085_pressure_raw(oss);
  120. lua_pushinteger(L, p);
  121. return 1;
  122. }
  123. static int ICACHE_FLASH_ATTR bmp085_lua_pressure(lua_State* L) {
  124. uint8_t oss = 0;
  125. int32_t p;
  126. int32_t X1, X2, X3, B3, B4, B5, B6, B7;
  127. if (lua_isnumber(L, 1)) {
  128. oss = luaL_checkinteger(L, 1);
  129. if (oss > 3) {
  130. oss = 3;
  131. }
  132. }
  133. p = bmp085_pressure_raw(oss);
  134. B5 = bmp085_temperature_raw_b5();
  135. B6 = B5 - 4000;
  136. X1 = ((int32_t)bmp085_data.B2 * ((B6 * B6) >> 12)) >> 11;
  137. X2 = ((int32_t)bmp085_data.AC2 * B6) >> 11;
  138. X3 = X1 + X2;
  139. B3 = ((((int32_t)bmp085_data.AC1 << 2) + X3) * (1 << oss) + 2) >> 2;
  140. X1 = ((int32_t)bmp085_data.AC3 * B6) >> 13;
  141. X2 = ((int32_t)bmp085_data.B1 * ((B6 * B6) >> 12)) >> 16;
  142. X3 = (X1 + X2 + 2) >> 2;
  143. B4 = ((int32_t)bmp085_data.AC4 * (X3 + 32768)) >> 15;
  144. B7 = (p - B3) * (50000 / (1 << oss));
  145. p = (B7 / B4) << 1;
  146. X1 = (p >> 8) * (p >> 8);
  147. X1 = (X1 * 3038) >> 16;
  148. X2 = (-7357 * p) >> 16;
  149. p = p + ((X1 + X2 + 3791) >> 4);
  150. lua_pushinteger(L, p);
  151. return 1;
  152. }
  153. static const LUA_REG_TYPE bmp085_map[] = {
  154. { LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )},
  155. { LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )},
  156. { LSTRKEY( "pressure_raw" ), LFUNCVAL( bmp085_lua_pressure_raw )},
  157. { LSTRKEY( "init" ), LFUNCVAL( bmp085_init )},
  158. { LNILKEY, LNILVAL}
  159. };
  160. NODEMCU_MODULE(BMP085, "bmp085", bmp085_map, NULL);