hmc5883l.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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 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 hmc5883_setup(lua_State* L) {
  33. uint8_t devid_a, devid_b, devid_c;
  34. devid_a = r8u(hmc5883_i2c_id, 10);
  35. devid_b = r8u(hmc5883_i2c_id, 11);
  36. devid_c = r8u(hmc5883_i2c_id, 12);
  37. if ((devid_a != 0x48) || (devid_b != 0x34) || (devid_c != 0x33)) {
  38. return luaL_error(L, "device not found");
  39. }
  40. // 8 sample average, 15 Hz update rate, normal measurement
  41. w8u(hmc5883_i2c_id, 0x00, 0x70);
  42. // Gain = 5
  43. w8u(hmc5883_i2c_id, 0x01, 0xA0);
  44. // Continuous measurement
  45. w8u(hmc5883_i2c_id, 0x02, 0x00);
  46. return 0;
  47. }
  48. static int hmc5883_init(lua_State* L) {
  49. uint32_t sda;
  50. uint32_t scl;
  51. platform_print_deprecation_note("hmc5883l.init() is replaced by hmc5883l.setup()", "in the next version");
  52. sda = luaL_checkinteger(L, 1);
  53. scl = luaL_checkinteger(L, 2);
  54. luaL_argcheck(L, sda > 0 && scl > 0, 1, "no i2c for D0");
  55. platform_i2c_setup(hmc5883_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  56. return hmc5883_setup(L);
  57. }
  58. static int hmc5883_read(lua_State* L) {
  59. uint8_t data[6];
  60. int x,y,z;
  61. int i;
  62. platform_i2c_send_start(hmc5883_i2c_id);
  63. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  64. platform_i2c_send_byte(hmc5883_i2c_id, 0x03);
  65. platform_i2c_send_stop(hmc5883_i2c_id);
  66. platform_i2c_send_start(hmc5883_i2c_id);
  67. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  68. for (i=0; i<5; i++) {
  69. data[i] = platform_i2c_recv_byte(hmc5883_i2c_id, 1);
  70. }
  71. data[5] = platform_i2c_recv_byte(hmc5883_i2c_id, 0);
  72. platform_i2c_send_stop(hmc5883_i2c_id);
  73. x = (int16_t) ((data[0] << 8) | data[1]);
  74. z = (int16_t) ((data[2] << 8) | data[3]);
  75. y = (int16_t) ((data[4] << 8) | data[5]);
  76. lua_pushinteger(L, x);
  77. lua_pushinteger(L, y);
  78. lua_pushinteger(L, z);
  79. return 3;
  80. }
  81. static const LUA_REG_TYPE hmc5883_map[] = {
  82. { LSTRKEY( "read" ), LFUNCVAL( hmc5883_read )},
  83. { LSTRKEY( "setup" ), LFUNCVAL( hmc5883_setup )},
  84. // init() is deprecated
  85. { LSTRKEY( "init" ), LFUNCVAL( hmc5883_init )},
  86. { LNILKEY, LNILVAL}
  87. };
  88. NODEMCU_MODULE(HMC5883L, "hmc5883l", hmc5883_map, NULL);