2016-06-05 22:59:39 +02:00
|
|
|
/*
|
|
|
|
* Driver for Honeywell HMC5883L compass IC
|
|
|
|
*
|
|
|
|
* Code based on BMP085 driver.
|
|
|
|
*/
|
|
|
|
#include "module.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-06-05 22:59:39 +02:00
|
|
|
|
|
|
|
static const uint32_t hmc5883_i2c_id = 0;
|
|
|
|
static const uint8_t hmc5883_i2c_addr = 0x1E;
|
|
|
|
|
2017-05-21 16:17:29 +02:00
|
|
|
static uint8_t r8u(uint32_t id, uint8_t reg) {
|
2016-06-05 22:59:39 +02:00
|
|
|
uint8_t ret;
|
|
|
|
|
|
|
|
platform_i2c_send_start(id);
|
|
|
|
platform_i2c_send_address(id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
|
|
|
|
platform_i2c_send_byte(id, reg);
|
|
|
|
platform_i2c_send_stop(id);
|
|
|
|
platform_i2c_send_start(id);
|
|
|
|
platform_i2c_send_address(id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
|
|
|
|
ret = platform_i2c_recv_byte(id, 0);
|
|
|
|
platform_i2c_send_stop(id);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:17:29 +02:00
|
|
|
static void w8u(uint32_t id, uint8_t reg, uint8_t val) {
|
2016-06-05 22:59:39 +02:00
|
|
|
platform_i2c_send_start(hmc5883_i2c_id);
|
|
|
|
platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
|
|
|
|
platform_i2c_send_byte(hmc5883_i2c_id, reg);
|
|
|
|
platform_i2c_send_byte(hmc5883_i2c_id, val);
|
|
|
|
platform_i2c_send_stop(hmc5883_i2c_id);
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:17:29 +02:00
|
|
|
static int hmc5883_setup(lua_State* L) {
|
2016-06-05 22:59:39 +02:00
|
|
|
uint8_t devid_a, devid_b, devid_c;
|
|
|
|
|
|
|
|
devid_a = r8u(hmc5883_i2c_id, 10);
|
|
|
|
devid_b = r8u(hmc5883_i2c_id, 11);
|
|
|
|
devid_c = r8u(hmc5883_i2c_id, 12);
|
|
|
|
|
|
|
|
if ((devid_a != 0x48) || (devid_b != 0x34) || (devid_c != 0x33)) {
|
|
|
|
return luaL_error(L, "device not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 8 sample average, 15 Hz update rate, normal measurement
|
|
|
|
w8u(hmc5883_i2c_id, 0x00, 0x70);
|
|
|
|
|
|
|
|
// Gain = 5
|
|
|
|
w8u(hmc5883_i2c_id, 0x01, 0xA0);
|
|
|
|
|
|
|
|
// Continuous measurement
|
|
|
|
w8u(hmc5883_i2c_id, 0x02, 0x00);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:17:29 +02:00
|
|
|
static int hmc5883_read(lua_State* L) {
|
2016-06-05 22:59:39 +02:00
|
|
|
|
|
|
|
uint8_t data[6];
|
|
|
|
int x,y,z;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
platform_i2c_send_start(hmc5883_i2c_id);
|
|
|
|
platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
|
|
|
|
platform_i2c_send_byte(hmc5883_i2c_id, 0x03);
|
|
|
|
platform_i2c_send_stop(hmc5883_i2c_id);
|
|
|
|
platform_i2c_send_start(hmc5883_i2c_id);
|
|
|
|
platform_i2c_send_address(hmc5883_i2c_id, hmc5883_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
|
|
|
|
|
|
|
|
for (i=0; i<5; i++) {
|
|
|
|
data[i] = platform_i2c_recv_byte(hmc5883_i2c_id, 1);
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-06-05 22:59:39 +02:00
|
|
|
data[5] = platform_i2c_recv_byte(hmc5883_i2c_id, 0);
|
|
|
|
|
|
|
|
platform_i2c_send_stop(hmc5883_i2c_id);
|
|
|
|
|
|
|
|
x = (int16_t) ((data[0] << 8) | data[1]);
|
2017-10-16 14:51:24 +02:00
|
|
|
z = (int16_t) ((data[2] << 8) | data[3]);
|
|
|
|
y = (int16_t) ((data[4] << 8) | data[5]);
|
2016-06-05 22:59:39 +02:00
|
|
|
|
|
|
|
lua_pushinteger(L, x);
|
|
|
|
lua_pushinteger(L, y);
|
|
|
|
lua_pushinteger(L, z);
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(hmc5883, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( read, hmc5883_read )
|
|
|
|
LROT_FUNCENTRY( setup, hmc5883_setup )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(hmc5883, NULL, 0)
|
2016-06-05 22:59:39 +02:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
NODEMCU_MODULE(HMC5883L, "hmc5883l", hmc5883, NULL);
|