From 6f0ace91575a3b2ae1b4d44c337ef8fdbf5bdbc8 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 18 Nov 2018 20:24:04 +0100 Subject: [PATCH] mag3110: add driver --- mag3110/mag3110.go | 55 ++++++++++++++++++++++++++++++++++++++++++++ mag3110/registers.go | 28 ++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 mag3110/mag3110.go create mode 100644 mag3110/registers.go diff --git a/mag3110/mag3110.go b/mag3110/mag3110.go new file mode 100644 index 0000000..47133a9 --- /dev/null +++ b/mag3110/mag3110.go @@ -0,0 +1,55 @@ +// Package mag3110 implements a driver for the MAG3110 3-axis magnetometer by +// Freescale/NXP. +// +// Datasheet: https://www.nxp.com/docs/en/data-sheet/MAG3110.pdf +package mag3110 + +import ( + "machine" +) + +// Device wraps an I2C connection to a MAG3110 device. +type Device struct { + bus machine.I2C +} + +// NewI2C creates a new MAG3110 connection. The I2C bus must already be +// configured. +// +// This function only creates the Device object, it does not touch the device. +func NewI2C(bus machine.I2C) Device { + return Device{bus} +} + +// Connected returns whether a MAG3110 has been found. +// It does a "who am I" request and checks the response. +func (d Device) Connected() bool { + data := []byte{0} + d.bus.ReadRegister(Address, WHO_AM_I, data) + return data[0] == 0xC4 +} + +// Configure sets up the device for communication. +func (d Device) Configure() { + d.bus.WriteRegister(Address, CTRL_REG2, []uint8{0x80}) // Power down when not used +} + +// ReadMagnetic reads the vectors of the magnetic field of the device and +// returns it. +func (d Device) ReadMagnetic() (x int16, y int16, z int16) { + d.bus.WriteRegister(Address, CTRL_REG1, []uint8{0x1a}) // Request a measurement + + data := make([]byte, 6) + d.bus.ReadRegister(Address, OUT_X_MSB, data) + x = int16((uint16(data[0]) << 8) | uint16(data[1])) + y = int16((uint16(data[2]) << 8) | uint16(data[3])) + z = int16((uint16(data[4]) << 8) | uint16(data[5])) + return +} + +// ReadTemperature reads the current die temperature in degrees Celsius. +func (d Device) ReadTemperature() (temp int8) { + data := make([]byte, 1) + d.bus.ReadRegister(Address, DIE_TEMP, data) + return int8(data[0]) +} diff --git a/mag3110/registers.go b/mag3110/registers.go new file mode 100644 index 0000000..a2f0347 --- /dev/null +++ b/mag3110/registers.go @@ -0,0 +1,28 @@ +package mag3110 + +// Constants/addresses used for I2C. + +// The I2C address which this device listens to. +const Address = 0x0E + +// Registers. Names, addresses and comments are copied from the datasheet. +const ( + DR_STATUS = 0x00 // Data ready status per axis + OUT_X_MSB = 0x01 // Bits [15:8] of X measurement + OUT_X_LSB = 0x02 // Bits [7:0] of X measurement + OUT_Y_MSB = 0x03 // Bits [15:8] of Y measurement + OUT_Y_LSB = 0x04 // Bits [7:0] of Y measurement + OUT_Z_MSB = 0x05 // Bits [15:8] of Z measurement + OUT_Z_LSB = 0x06 // Bits [7:0] of Z measurement + WHO_AM_I = 0x07 // Device ID Number + SYSMOD = 0x08 // Current System Mode + OFF_X_MSB = 0x09 // Bits [14:7] of user X offset + OFF_X_LSB = 0x0A // Bits [6:0] of user X offset + OFF_Y_MSB = 0x0B // Bits [14:7] of user Y offset + OFF_Y_LSB = 0x0C // Bits [6:0] of user Y offset + OFF_Z_MSB = 0x0D // Bits [14:7] of user Z offset + OFF_Z_LSB = 0x0E // Bits [6:0] of user Z offset + DIE_TEMP = 0x0F // Temperature, signed 8 bits in °C + CTRL_REG1 = 0x10 // Operation modes + CTRL_REG2 = 0x11 // Operation modes +)