bmp085.c 5.4 KB

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