adxl345.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Driver for Analog Devices ADXL345 accelerometer.
  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 adxl345_i2c_id = 0;
  12. static const uint8_t adxl345_i2c_addr = 0x53;
  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, adxl345_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, adxl345_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 int ICACHE_FLASH_ATTR adxl345_init(lua_State* L) {
  26. uint32_t sda;
  27. uint32_t scl;
  28. uint8_t devid;
  29. sda = luaL_checkinteger(L, 1);
  30. scl = luaL_checkinteger(L, 2);
  31. luaL_argcheck(L, sda > 0 && scl > 0, 1, "no i2c for D0");
  32. platform_i2c_setup(adxl345_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  33. devid = r8u(adxl345_i2c_id, 0x00);
  34. if (devid != 229) {
  35. return luaL_error(L, "device not found");
  36. }
  37. // Enable sensor
  38. platform_i2c_send_start(adxl345_i2c_id);
  39. platform_i2c_send_address(adxl345_i2c_id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  40. platform_i2c_send_byte(adxl345_i2c_id, 0x2D);
  41. platform_i2c_send_byte(adxl345_i2c_id, 0x08);
  42. platform_i2c_send_stop(adxl345_i2c_id);
  43. return 0;
  44. }
  45. static int ICACHE_FLASH_ATTR adxl345_read(lua_State* L) {
  46. uint8_t data[6];
  47. int x,y,z;
  48. int i;
  49. platform_i2c_send_start(adxl345_i2c_id);
  50. platform_i2c_send_address(adxl345_i2c_id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  51. platform_i2c_send_byte(adxl345_i2c_id, 0x32);
  52. platform_i2c_send_start(adxl345_i2c_id);
  53. platform_i2c_send_address(adxl345_i2c_id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  54. for (i=0; i<5; i++) {
  55. data[i] = platform_i2c_recv_byte(adxl345_i2c_id, 1);
  56. }
  57. data[5] = platform_i2c_recv_byte(adxl345_i2c_id, 0);
  58. platform_i2c_send_stop(adxl345_i2c_id);
  59. x = (int16_t) ((data[1] << 8) | data[0]);
  60. y = (int16_t) ((data[3] << 8) | data[2]);
  61. z = (int16_t) ((data[5] << 8) | data[4]);
  62. lua_pushinteger(L, x);
  63. lua_pushinteger(L, y);
  64. lua_pushinteger(L, z);
  65. return 3;
  66. }
  67. static const LUA_REG_TYPE adxl345_map[] = {
  68. { LSTRKEY( "read" ), LFUNCVAL( adxl345_read )},
  69. { LSTRKEY( "init" ), LFUNCVAL( adxl345_init )},
  70. { LNILKEY, LNILVAL}
  71. };
  72. NODEMCU_MODULE(ADXL345, "adxl345", adxl345_map, NULL);