hmc5883l.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Driver for Honeywell HMC5883L compass IC
  3. *
  4. * Code based on BMP085 driver.
  5. */
  6. #include "module.h"
  7. #include "lauxlib.h"
  8. #include "platform.h"
  9. #include "c_stdlib.h"
  10. #include "c_string.h"
  11. static const uint32_t hmc5883_i2c_id = 0;
  12. static const uint8_t hmc5883_i2c_addr = 0x1E;
  13. static uint8_t ICACHE_FLASH_ATTR r8u(uint32_t id, uint8_t reg) {
  14. uint8_t ret;
  15. platform_i2c_send_start(id);
  16. platform_i2c_send_address(id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  17. platform_i2c_send_byte(id, reg);
  18. platform_i2c_send_stop(id);
  19. platform_i2c_send_start(id);
  20. platform_i2c_send_address(id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  21. ret = platform_i2c_recv_byte(id, 0);
  22. platform_i2c_send_stop(id);
  23. return ret;
  24. }
  25. static void ICACHE_FLASH_ATTR w8u(uint32_t id, uint8_t reg, uint8_t val) {
  26. platform_i2c_send_start(hmc5883_i2c_id);
  27. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  28. platform_i2c_send_byte(hmc5883_i2c_id, reg);
  29. platform_i2c_send_byte(hmc5883_i2c_id, val);
  30. platform_i2c_send_stop(hmc5883_i2c_id);
  31. }
  32. static int ICACHE_FLASH_ATTR hmc5883_init(lua_State* L) {
  33. uint32_t sda;
  34. uint32_t scl;
  35. uint8_t devid_a, devid_b, devid_c;
  36. sda = luaL_checkinteger(L, 1);
  37. scl = luaL_checkinteger(L, 2);
  38. luaL_argcheck(L, sda > 0 && scl > 0, 1, "no i2c for D0");
  39. platform_i2c_setup(hmc5883_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  40. devid_a = r8u(hmc5883_i2c_id, 10);
  41. devid_b = r8u(hmc5883_i2c_id, 11);
  42. devid_c = r8u(hmc5883_i2c_id, 12);
  43. if ((devid_a != 0x48) || (devid_b != 0x34) || (devid_c != 0x33)) {
  44. return luaL_error(L, "device not found");
  45. }
  46. // 8 sample average, 15 Hz update rate, normal measurement
  47. w8u(hmc5883_i2c_id, 0x00, 0x70);
  48. // Gain = 5
  49. w8u(hmc5883_i2c_id, 0x01, 0xA0);
  50. // Continuous measurement
  51. w8u(hmc5883_i2c_id, 0x02, 0x00);
  52. return 0;
  53. }
  54. static int ICACHE_FLASH_ATTR hmc5883_read(lua_State* L) {
  55. uint8_t data[6];
  56. int x,y,z;
  57. int i;
  58. platform_i2c_send_start(hmc5883_i2c_id);
  59. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  60. platform_i2c_send_byte(hmc5883_i2c_id, 0x03);
  61. platform_i2c_send_stop(hmc5883_i2c_id);
  62. platform_i2c_send_start(hmc5883_i2c_id);
  63. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  64. for (i=0; i<5; i++) {
  65. data[i] = platform_i2c_recv_byte(hmc5883_i2c_id, 1);
  66. }
  67. data[5] = platform_i2c_recv_byte(hmc5883_i2c_id, 0);
  68. platform_i2c_send_stop(hmc5883_i2c_id);
  69. x = (int16_t) ((data[0] << 8) | data[1]);
  70. y = (int16_t) ((data[2] << 8) | data[3]);
  71. z = (int16_t) ((data[4] << 8) | data[5]);
  72. lua_pushinteger(L, x);
  73. lua_pushinteger(L, y);
  74. lua_pushinteger(L, z);
  75. return 3;
  76. }
  77. static const LUA_REG_TYPE hmc5883_map[] = {
  78. { LSTRKEY( "read" ), LFUNCVAL( hmc5883_read )},
  79. { LSTRKEY( "init" ), LFUNCVAL( hmc5883_init )},
  80. { LNILKEY, LNILVAL}
  81. };
  82. NODEMCU_MODULE(HMC5883L, "hmc5883l", hmc5883_map, NULL);