hmc5883l.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <stdlib.h>
  10. #include <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_read(lua_State* L) {
  49. uint8_t data[6];
  50. int x,y,z;
  51. int i;
  52. platform_i2c_send_start(hmc5883_i2c_id);
  53. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  54. platform_i2c_send_byte(hmc5883_i2c_id, 0x03);
  55. platform_i2c_send_stop(hmc5883_i2c_id);
  56. platform_i2c_send_start(hmc5883_i2c_id);
  57. platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  58. for (i=0; i<5; i++) {
  59. data[i] = platform_i2c_recv_byte(hmc5883_i2c_id, 1);
  60. }
  61. data[5] = platform_i2c_recv_byte(hmc5883_i2c_id, 0);
  62. platform_i2c_send_stop(hmc5883_i2c_id);
  63. x = (int16_t) ((data[0] << 8) | data[1]);
  64. z = (int16_t) ((data[2] << 8) | data[3]);
  65. y = (int16_t) ((data[4] << 8) | data[5]);
  66. lua_pushinteger(L, x);
  67. lua_pushinteger(L, y);
  68. lua_pushinteger(L, z);
  69. return 3;
  70. }
  71. LROT_BEGIN(hmc5883)
  72. LROT_FUNCENTRY( read, hmc5883_read )
  73. LROT_FUNCENTRY( setup, hmc5883_setup )
  74. LROT_END( hmc5883, NULL, 0 )
  75. NODEMCU_MODULE(HMC5883L, "hmc5883l", hmc5883, NULL);