Basic functions is OK. Lack interrupts function.

Dependents:   Mt05_MtSense07

Committer:
bcc6
Date:
Thu Mar 23 08:54:15 2017 +0000
Revision:
4:413b8ef9aed5
Parent:
1:4eefcf1d7351
Merge from branch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcc6 1:4eefcf1d7351 1 /* Copyright (c) 2016 MtM Technology Corporation, MIT License
bcc6 1:4eefcf1d7351 2 *
bcc6 1:4eefcf1d7351 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
bcc6 1:4eefcf1d7351 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
bcc6 1:4eefcf1d7351 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
bcc6 1:4eefcf1d7351 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
bcc6 1:4eefcf1d7351 7 * furnished to do so, subject to the following conditions:
bcc6 1:4eefcf1d7351 8 *
bcc6 1:4eefcf1d7351 9 * The above copyright notice and this permission notice shall be included in all copies or
bcc6 1:4eefcf1d7351 10 * substantial portions of the Software.
bcc6 1:4eefcf1d7351 11 *
bcc6 1:4eefcf1d7351 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
bcc6 1:4eefcf1d7351 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
bcc6 1:4eefcf1d7351 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
bcc6 1:4eefcf1d7351 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bcc6 1:4eefcf1d7351 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
bcc6 1:4eefcf1d7351 17 */
bcc6 0:4f87d5af61b1 18 #include "AK09912.h"
bcc6 0:4f87d5af61b1 19
bcc6 0:4f87d5af61b1 20
bcc6 0:4f87d5af61b1 21 AK09912::AK09912(I2C &i2c, PinName int1) : _i2c(i2c), _int1(int1) {
bcc6 0:4f87d5af61b1 22
bcc6 0:4f87d5af61b1 23 }
bcc6 0:4f87d5af61b1 24
bcc6 0:4f87d5af61b1 25 void AK09912::ConfigDevice() {
bcc6 0:4f87d5af61b1 26 /* Soft reset */
bcc6 0:4f87d5af61b1 27 RegWrite(0x32, 0x01);
bcc6 0:4f87d5af61b1 28
bcc6 0:4f87d5af61b1 29 /* Get sensitivity adjustment values */
bcc6 0:4f87d5af61b1 30 RegWrite(0x31, 0x1F); // MODE(Fuse ROM ACCESS)
bcc6 0:4f87d5af61b1 31 RegRead(0x60, (char *)_asa, 3); // ASAX, ASAY, ASAZ
bcc6 0:4f87d5af61b1 32 RegWrite(0x31, 0x00); // MODE(Power down)
bcc6 0:4f87d5af61b1 33
bcc6 0:4f87d5af61b1 34 /* Enable both temperature measurement and noise suppression filter */
bcc6 0:4f87d5af61b1 35 RegWrite(0x30, 0xE0); // TEM(Enable), NSF(High)
bcc6 0:4f87d5af61b1 36
bcc6 0:4f87d5af61b1 37 /* Mode */
bcc6 0:4f87d5af61b1 38 RegWrite(0x31, 0x02); // MODE(ContMode1)
bcc6 0:4f87d5af61b1 39 }
bcc6 0:4f87d5af61b1 40
bcc6 0:4f87d5af61b1 41 void AK09912::GetDeviceID(uint8_t *id) {
bcc6 0:4f87d5af61b1 42 RegRead(0x01, (char *)id, 1);
bcc6 0:4f87d5af61b1 43 }
bcc6 0:4f87d5af61b1 44
bcc6 0:4f87d5af61b1 45 void AK09912::GetData(Data *data){
bcc6 0:4f87d5af61b1 46 char status;
bcc6 0:4f87d5af61b1 47 char buf[10];
bcc6 0:4f87d5af61b1 48
bcc6 0:4f87d5af61b1 49 /* Read data */
bcc6 0:4f87d5af61b1 50 RegRead(0x10, &status, 1);
bcc6 0:4f87d5af61b1 51 RegRead(0x11, buf, 7);
bcc6 0:4f87d5af61b1 52 RegRead(0x18, &status, 1); // Dummy read
bcc6 0:4f87d5af61b1 53
bcc6 0:4f87d5af61b1 54 /* Format data */
bcc6 0:4f87d5af61b1 55 int16_t x_adc = (((int16_t)buf[1]) << 8) | buf[0];
bcc6 0:4f87d5af61b1 56 int16_t y_adc = (((int16_t)buf[3]) << 8) | buf[2];
bcc6 0:4f87d5af61b1 57 int16_t z_adc = (((int16_t)buf[5]) << 8) | buf[4];
bcc6 0:4f87d5af61b1 58 uint8_t t_adc = buf[6];
bcc6 0:4f87d5af61b1 59
bcc6 0:4f87d5af61b1 60 /* Convert data */
bcc6 0:4f87d5af61b1 61 data->x = ConvertAdcToMagnetic(x_adc, _asa[0]);
bcc6 0:4f87d5af61b1 62 data->y = ConvertAdcToMagnetic(y_adc, _asa[1]);
bcc6 0:4f87d5af61b1 63 data->z = ConvertAdcToMagnetic(z_adc, _asa[2]);
bcc6 0:4f87d5af61b1 64 data->t = ConvertAdcToTemperature(t_adc);
bcc6 0:4f87d5af61b1 65 }
bcc6 0:4f87d5af61b1 66
bcc6 0:4f87d5af61b1 67 float AK09912::ConvertAdcToMagnetic(int16_t adc, uint8_t asa) {
bcc6 0:4f87d5af61b1 68 float magnetic_flux_density = (float)adc * 0.15f; // Original
bcc6 0:4f87d5af61b1 69 return magnetic_flux_density * (((float)asa - 128) * 0.5f / 128 + 1); // Adjustment
bcc6 0:4f87d5af61b1 70 }
bcc6 0:4f87d5af61b1 71
bcc6 0:4f87d5af61b1 72 float AK09912::ConvertAdcToTemperature(uint8_t adc) {
bcc6 0:4f87d5af61b1 73 return (120.0f - (float)adc) / 1.6f + 35.0f;
bcc6 0:4f87d5af61b1 74 }
bcc6 0:4f87d5af61b1 75
bcc6 0:4f87d5af61b1 76 void AK09912::RegWrite(char reg, char val) {
bcc6 0:4f87d5af61b1 77 char data[2];
bcc6 0:4f87d5af61b1 78 data[0] = reg;
bcc6 0:4f87d5af61b1 79 data[1] = val;
bcc6 0:4f87d5af61b1 80 _i2c.write(AK09912_SLAVE_ADDR, data, 2, 0);
bcc6 0:4f87d5af61b1 81 }
bcc6 0:4f87d5af61b1 82
bcc6 0:4f87d5af61b1 83 void AK09912::RegRead(char reg, char *val, int len) {
bcc6 0:4f87d5af61b1 84 _i2c.write(AK09912_SLAVE_ADDR, &reg, 1, 0);
bcc6 0:4f87d5af61b1 85 _i2c.read (AK09912_SLAVE_ADDR, val, len);
bcc6 0:4f87d5af61b1 86 }
bcc6 0:4f87d5af61b1 87
bcc6 0:4f87d5af61b1 88 void AK09912::RegReadModifyWrite(char reg, char clr_mask, char set_mask) {
bcc6 0:4f87d5af61b1 89 char val;
bcc6 0:4f87d5af61b1 90 RegRead (reg, &val, 1); // Read
bcc6 0:4f87d5af61b1 91 val = (val & ~clr_mask) | set_mask; // Modify
bcc6 0:4f87d5af61b1 92 RegWrite(reg, val); // Write
bcc6 0:4f87d5af61b1 93 }