Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

Committer:
masahikofukasawa
Date:
Wed Jan 04 00:52:30 2017 +0000
Revision:
16:d85be9bafb80
Parent:
15:1238993fd75f
Child:
27:41aa9fb23a2f
Rev012. Modified for using the latest BLE_API, nRF51822 and mbed libs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masahikofukasawa 10:5c69b067d88a 1 #include "ak9752ctrl.h"
masahikofukasawa 10:5c69b067d88a 2 #include "debug.h"
masahikofukasawa 10:5c69b067d88a 3
masahikofukasawa 10:5c69b067d88a 4 #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low)))
masahikofukasawa 10:5c69b067d88a 5
masahikofukasawa 10:5c69b067d88a 6 /**
masahikofukasawa 10:5c69b067d88a 7 * Constructor.
masahikofukasawa 10:5c69b067d88a 8 *
masahikofukasawa 10:5c69b067d88a 9 */
masahikofukasawa 10:5c69b067d88a 10 Ak9752Ctrl::Ak9752Ctrl(){
masahikofukasawa 10:5c69b067d88a 11 ak9752 = NULL;
masahikofukasawa 10:5c69b067d88a 12 event = false;
masahikofukasawa 10:5c69b067d88a 13 interrupt = NULL;
masahikofukasawa 13:d008249f0359 14 sensorName = "";
masahikofukasawa 10:5c69b067d88a 15 }
masahikofukasawa 10:5c69b067d88a 16
masahikofukasawa 10:5c69b067d88a 17 /**
masahikofukasawa 10:5c69b067d88a 18 * Destructor.
masahikofukasawa 10:5c69b067d88a 19 *
masahikofukasawa 10:5c69b067d88a 20 */
masahikofukasawa 10:5c69b067d88a 21 Ak9752Ctrl::~Ak9752Ctrl(){
masahikofukasawa 10:5c69b067d88a 22 if (ak9752) delete ak9752;
masahikofukasawa 10:5c69b067d88a 23 if (interrupt) delete interrupt;
masahikofukasawa 10:5c69b067d88a 24 }
masahikofukasawa 10:5c69b067d88a 25
masahikofukasawa 10:5c69b067d88a 26 AkmSensor::Status Ak9752Ctrl::init(const uint8_t id, const uint8_t subid){
masahikofukasawa 10:5c69b067d88a 27 primaryId = id;
masahikofukasawa 10:5c69b067d88a 28 subId = subid;
masahikofukasawa 10:5c69b067d88a 29
masahikofukasawa 10:5c69b067d88a 30 interrupt = new InterruptIn(I2C_DRDY);
masahikofukasawa 10:5c69b067d88a 31 I2C* i2c = new I2C(I2C_SDA,I2C_SCL);
masahikofukasawa 10:5c69b067d88a 32 i2c->frequency(I2C_SPEED_100KHZ);
masahikofukasawa 10:5c69b067d88a 33
masahikofukasawa 10:5c69b067d88a 34 if(subId == SUB_ID_AK9752){
masahikofukasawa 10:5c69b067d88a 35 ak9752 = new AK9752();
masahikofukasawa 13:d008249f0359 36 sensorName = "AK9752";
masahikofukasawa 10:5c69b067d88a 37 }
masahikofukasawa 10:5c69b067d88a 38 else{
masahikofukasawa 10:5c69b067d88a 39 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 40 }
masahikofukasawa 10:5c69b067d88a 41
masahikofukasawa 10:5c69b067d88a 42 bool foundSensor = false;
masahikofukasawa 10:5c69b067d88a 43
masahikofukasawa 10:5c69b067d88a 44 AK9752::SlaveAddress slaveAddr[]
masahikofukasawa 10:5c69b067d88a 45 = { AK9752::SLAVE_ADDR_1 };
masahikofukasawa 10:5c69b067d88a 46
masahikofukasawa 10:5c69b067d88a 47 for(int i=0; i<sizeof(slaveAddr); i++)
masahikofukasawa 10:5c69b067d88a 48 {
masahikofukasawa 10:5c69b067d88a 49 ak9752->init(i2c, slaveAddr[i]);
masahikofukasawa 10:5c69b067d88a 50 // Checks connectivity
masahikofukasawa 10:5c69b067d88a 51 if(ak9752->checkConnection() == Ak9752Ctrl::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 52 // found
masahikofukasawa 10:5c69b067d88a 53 foundSensor = true;
masahikofukasawa 10:5c69b067d88a 54 break;
masahikofukasawa 10:5c69b067d88a 55 }
masahikofukasawa 10:5c69b067d88a 56 }
masahikofukasawa 10:5c69b067d88a 57 if(foundSensor != true) return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 58
masahikofukasawa 10:5c69b067d88a 59 // reset
masahikofukasawa 10:5c69b067d88a 60 if (ak9752->reset() != AK9752::SUCCESS) {
masahikofukasawa 11:cef8dc1cf010 61 MSG("#Failed to reset AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 62 }
masahikofukasawa 10:5c69b067d88a 63
masahikofukasawa 11:cef8dc1cf010 64 MSG("#Init success AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 65 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 66 }
masahikofukasawa 10:5c69b067d88a 67
masahikofukasawa 10:5c69b067d88a 68 void Ak9752Ctrl::detectINT(){
masahikofukasawa 10:5c69b067d88a 69 event = true;
masahikofukasawa 10:5c69b067d88a 70 }
masahikofukasawa 10:5c69b067d88a 71
masahikofukasawa 10:5c69b067d88a 72 bool Ak9752Ctrl::isEvent(){
masahikofukasawa 10:5c69b067d88a 73 return event;
masahikofukasawa 10:5c69b067d88a 74 }
masahikofukasawa 10:5c69b067d88a 75
masahikofukasawa 10:5c69b067d88a 76 AkmSensor::Status Ak9752Ctrl::startSensor(){
masahikofukasawa 11:cef8dc1cf010 77 // read one data to clear INT pin
masahikofukasawa 11:cef8dc1cf010 78 AK9752::SensorData data;
masahikofukasawa 11:cef8dc1cf010 79 ak9752->getSensorData(&data);
masahikofukasawa 11:cef8dc1cf010 80
masahikofukasawa 11:cef8dc1cf010 81 // enable interrupt
masahikofukasawa 16:d85be9bafb80 82 interrupt->fall(callback(this, &Ak9752Ctrl::detectINT));
masahikofukasawa 11:cef8dc1cf010 83
masahikofukasawa 11:cef8dc1cf010 84 // set operation mode
masahikofukasawa 10:5c69b067d88a 85 if(ak9752->setOperationMode(mode,fc_tmp, fc_ir) != AK9752::SUCCESS) {
masahikofukasawa 11:cef8dc1cf010 86 MSG("#Start sensor failed.\r\n");
masahikofukasawa 10:5c69b067d88a 87 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 88 }
masahikofukasawa 10:5c69b067d88a 89
masahikofukasawa 11:cef8dc1cf010 90 MSG("#Start sensor succceeded.\r\n");
masahikofukasawa 10:5c69b067d88a 91 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 92 }
masahikofukasawa 10:5c69b067d88a 93
masahikofukasawa 10:5c69b067d88a 94 AkmSensor::Status Ak9752Ctrl::startSensor(const float sec){
masahikofukasawa 16:d85be9bafb80 95 interrupt->fall(callback(this, &Ak9752Ctrl::detectINT));
masahikofukasawa 10:5c69b067d88a 96 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 97 }
masahikofukasawa 10:5c69b067d88a 98
masahikofukasawa 10:5c69b067d88a 99 AkmSensor::Status Ak9752Ctrl::stopSensor(){
masahikofukasawa 10:5c69b067d88a 100 event = false;
masahikofukasawa 11:cef8dc1cf010 101 interrupt->fall(NULL);
masahikofukasawa 10:5c69b067d88a 102 if(ak9752->setOperationMode(AK9752::MODE_STANDBY, fc_tmp, fc_ir) != AK9752::SUCCESS) {
masahikofukasawa 11:cef8dc1cf010 103 MSG("#Error setOperationMode. AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 104 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 105 }
masahikofukasawa 10:5c69b067d88a 106
masahikofukasawa 10:5c69b067d88a 107 // read one data to clear INT pin
masahikofukasawa 11:cef8dc1cf010 108 AK9752::SensorData data;
masahikofukasawa 11:cef8dc1cf010 109 ak9752->getSensorData(&data);
masahikofukasawa 10:5c69b067d88a 110
masahikofukasawa 10:5c69b067d88a 111 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 112 }
masahikofukasawa 10:5c69b067d88a 113
masahikofukasawa 10:5c69b067d88a 114 AkmSensor::Status Ak9752Ctrl::readSensorData(Message* msg){
masahikofukasawa 10:5c69b067d88a 115 event = false;
masahikofukasawa 10:5c69b067d88a 116
masahikofukasawa 10:5c69b067d88a 117 AK9752::SensorData data;
masahikofukasawa 10:5c69b067d88a 118 AK9752::Status status = ak9752->getSensorData(&data);
masahikofukasawa 10:5c69b067d88a 119 if( status != AK9752::SUCCESS){
masahikofukasawa 11:cef8dc1cf010 120 MSG("#Error getSensorData. AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 121 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 122 }
masahikofukasawa 10:5c69b067d88a 123 msg->setCommand(Message::CMD_START_MEASUREMENT);
masahikofukasawa 10:5c69b067d88a 124 msg->setArgument( 0, data.intStatus.irh);
masahikofukasawa 10:5c69b067d88a 125 msg->setArgument( 1, data.intStatus.irl);
masahikofukasawa 10:5c69b067d88a 126 msg->setArgument( 2, data.intStatus.tmph);
masahikofukasawa 10:5c69b067d88a 127 msg->setArgument( 3, data.intStatus.tmpl);
masahikofukasawa 10:5c69b067d88a 128 msg->setArgument( 4, data.intStatus.dr);
masahikofukasawa 10:5c69b067d88a 129 msg->setArgument( 5,(char)((int32_t)(data.ir) >> 8));
masahikofukasawa 10:5c69b067d88a 130 msg->setArgument( 6, (char)((int32_t)(data.ir) & 0x00FF) );
masahikofukasawa 10:5c69b067d88a 131 msg->setArgument( 7,(char)((int32_t)(data.temperature) >> 8));
masahikofukasawa 10:5c69b067d88a 132 msg->setArgument( 8, (char)((int32_t)(data.temperature) & 0x00FF) );
masahikofukasawa 10:5c69b067d88a 133 msg->setArgument( 9, data.dor);
masahikofukasawa 10:5c69b067d88a 134
masahikofukasawa 10:5c69b067d88a 135 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 136 }
masahikofukasawa 10:5c69b067d88a 137
masahikofukasawa 10:5c69b067d88a 138 AkmSensor::Status Ak9752Ctrl::requestCommand(Message* in, Message* out){
masahikofukasawa 10:5c69b067d88a 139 AkmSensor::Status status = AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 140 Message::Command cmd = in->getCommand();
masahikofukasawa 10:5c69b067d88a 141 AK9752::Threshold th;
masahikofukasawa 10:5c69b067d88a 142 AK9752::InterruptStatus interrupt;
masahikofukasawa 10:5c69b067d88a 143
masahikofukasawa 10:5c69b067d88a 144 out->setCommand(cmd);
masahikofukasawa 10:5c69b067d88a 145
masahikofukasawa 10:5c69b067d88a 146 switch(cmd){
masahikofukasawa 10:5c69b067d88a 147 case Message::CMD_IR_GET_THRESHOLD:
masahikofukasawa 10:5c69b067d88a 148 {
masahikofukasawa 10:5c69b067d88a 149 if (ak9752->getThreshold(&th) != AK9752::SUCCESS) {
masahikofukasawa 11:cef8dc1cf010 150 MSG("#Failed to set threshold to AK9752.\r\n");
masahikofukasawa 15:1238993fd75f 151 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 152 }
masahikofukasawa 10:5c69b067d88a 153 out->setArgument(0,(char)((int32_t)(th.thirh) >> 8));
masahikofukasawa 10:5c69b067d88a 154 out->setArgument(1,(char)((int32_t)(th.thirh) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 155 out->setArgument(2,(char)((int32_t)(th.thirl) >> 8));
masahikofukasawa 10:5c69b067d88a 156 out->setArgument(3,(char)((int32_t)(th.thirl) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 157 out->setArgument(4,(char)((int32_t)(th.thtmph) >> 8));
masahikofukasawa 10:5c69b067d88a 158 out->setArgument(5,(char)((int32_t)(th.thtmph) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 159 out->setArgument(6,(char)((int32_t)(th.thtmpl) >> 8));
masahikofukasawa 10:5c69b067d88a 160 out->setArgument(7,(char)((int32_t)(th.thtmpl) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 161 break;
masahikofukasawa 10:5c69b067d88a 162 }
masahikofukasawa 10:5c69b067d88a 163 case Message::CMD_IR_SET_THRESHOLD:
masahikofukasawa 10:5c69b067d88a 164 {
masahikofukasawa 10:5c69b067d88a 165 th.thirh = CONV16I(in->getArgument(0), in->getArgument(1));
masahikofukasawa 10:5c69b067d88a 166 th.thirl = CONV16I(in->getArgument(2), in->getArgument(3));
masahikofukasawa 10:5c69b067d88a 167 th.thtmph = CONV16I(in->getArgument(4), in->getArgument(5));
masahikofukasawa 10:5c69b067d88a 168 th.thtmpl = CONV16I(in->getArgument(6), in->getArgument(7));
masahikofukasawa 10:5c69b067d88a 169 if (ak9752->setThreshold(&th) != AK9752::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 170 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 171 MSG("#Failed to set threshold to AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 172 }
masahikofukasawa 10:5c69b067d88a 173 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 174 break;
masahikofukasawa 10:5c69b067d88a 175 }
masahikofukasawa 10:5c69b067d88a 176 case Message::CMD_IR_GET_INTERRUPT:
masahikofukasawa 10:5c69b067d88a 177 {
masahikofukasawa 10:5c69b067d88a 178 if (ak9752->getInterruptEnable(&interrupt) != AK9752::SUCCESS) {
masahikofukasawa 11:cef8dc1cf010 179 MSG("#Failed to set hysteresis to AK9752.\r\n");
masahikofukasawa 15:1238993fd75f 180 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 181 }
masahikofukasawa 10:5c69b067d88a 182 out->setArgument(0, interrupt.irh);
masahikofukasawa 10:5c69b067d88a 183 out->setArgument(1, interrupt.irl);
masahikofukasawa 10:5c69b067d88a 184 out->setArgument(2, interrupt.tmph);
masahikofukasawa 10:5c69b067d88a 185 out->setArgument(3, interrupt.tmpl);
masahikofukasawa 10:5c69b067d88a 186 out->setArgument(4, interrupt.dr);
masahikofukasawa 10:5c69b067d88a 187 break;
masahikofukasawa 10:5c69b067d88a 188 }
masahikofukasawa 10:5c69b067d88a 189 case Message::CMD_IR_SET_INTERRUPT:
masahikofukasawa 10:5c69b067d88a 190 {
masahikofukasawa 10:5c69b067d88a 191 interrupt.irh = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 192 interrupt.irl = in->getArgument(1);
masahikofukasawa 10:5c69b067d88a 193 interrupt.tmph = in->getArgument(2);
masahikofukasawa 10:5c69b067d88a 194 interrupt.tmpl = in->getArgument(3);
masahikofukasawa 10:5c69b067d88a 195 interrupt.dr = in->getArgument(4);
masahikofukasawa 10:5c69b067d88a 196 if (ak9752->setInterruptEnable(&interrupt) != AK9752::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 197 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 198 MSG("#Failed to set hysteresis to AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 199 }
masahikofukasawa 10:5c69b067d88a 200 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 201 break;
masahikofukasawa 10:5c69b067d88a 202 }
masahikofukasawa 10:5c69b067d88a 203 case Message::CMD_IR_GET_OPERATION_MODE:
masahikofukasawa 10:5c69b067d88a 204 {
masahikofukasawa 10:5c69b067d88a 205 if(ak9752->getOperationMode(&mode, &fc_tmp, &fc_ir) != AK9752::SUCCESS) {
masahikofukasawa 15:1238993fd75f 206 MSG("#Error getOperationMode. AK9752.\r\n");
masahikofukasawa 15:1238993fd75f 207 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 208 }
masahikofukasawa 10:5c69b067d88a 209 out->setArgument(0,(char)mode);
masahikofukasawa 10:5c69b067d88a 210 out->setArgument(1,(char)fc_tmp);
masahikofukasawa 10:5c69b067d88a 211 out->setArgument(2,(char)fc_ir);
masahikofukasawa 10:5c69b067d88a 212 break;
masahikofukasawa 10:5c69b067d88a 213 }
masahikofukasawa 10:5c69b067d88a 214 case Message::CMD_IR_SET_OPERATION_MODE:
masahikofukasawa 10:5c69b067d88a 215 {
masahikofukasawa 10:5c69b067d88a 216 mode = (AK9752::OperationMode)in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 217 fc_tmp = (AK9752::FcTmp)in->getArgument(1);
masahikofukasawa 10:5c69b067d88a 218 fc_ir = (AK9752::FcIr)in->getArgument(2);
masahikofukasawa 10:5c69b067d88a 219 if(ak9752->setOperationMode(mode, fc_tmp, fc_ir) != AK9752::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 220 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 221 MSG("#Error setOperationMode. AK9752.\r\n");
masahikofukasawa 10:5c69b067d88a 222 }
masahikofukasawa 10:5c69b067d88a 223 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 224 break;
masahikofukasawa 10:5c69b067d88a 225 }
masahikofukasawa 10:5c69b067d88a 226 case Message::CMD_REG_WRITE:
masahikofukasawa 10:5c69b067d88a 227 case Message::CMD_REG_WRITEN:
masahikofukasawa 10:5c69b067d88a 228 {
masahikofukasawa 10:5c69b067d88a 229 char address = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 230 int len = (int)in->getArgument(1);
masahikofukasawa 15:1238993fd75f 231 if(in->getArgNum() != len+2){
masahikofukasawa 15:1238993fd75f 232 MSG("#Error argument num. Args=%d\r\n",in->getArgNum());
masahikofukasawa 15:1238993fd75f 233 status = AkmSensor::ERROR;
masahikofukasawa 15:1238993fd75f 234 out->setArgument(0,(char)status);
masahikofukasawa 15:1238993fd75f 235 return status;
masahikofukasawa 15:1238993fd75f 236 }
masahikofukasawa 15:1238993fd75f 237
masahikofukasawa 10:5c69b067d88a 238 char data[len];
masahikofukasawa 10:5c69b067d88a 239 for(int i=0; i<len; i++){
masahikofukasawa 15:1238993fd75f 240 data[i] = in->getArgument(i+2);
masahikofukasawa 10:5c69b067d88a 241 }
masahikofukasawa 10:5c69b067d88a 242 if( ak9752->write(address, data, len) != AK9752::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 243 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 244 MSG("#Error register write.\r\n");
masahikofukasawa 10:5c69b067d88a 245 }
masahikofukasawa 10:5c69b067d88a 246 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 247 break;
masahikofukasawa 10:5c69b067d88a 248 }
masahikofukasawa 10:5c69b067d88a 249 case Message::CMD_REG_READ:
masahikofukasawa 10:5c69b067d88a 250 case Message::CMD_REG_READN:
masahikofukasawa 10:5c69b067d88a 251 {
masahikofukasawa 15:1238993fd75f 252 if(in->getArgNum() != 2){
masahikofukasawa 15:1238993fd75f 253 MSG("#Error argument num. Args=%d\r\n",in->getArgNum());
masahikofukasawa 15:1238993fd75f 254 return AkmSensor::ERROR;
masahikofukasawa 15:1238993fd75f 255 }
masahikofukasawa 15:1238993fd75f 256
masahikofukasawa 10:5c69b067d88a 257 char address = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 258 int len = (int)in->getArgument(1);
masahikofukasawa 10:5c69b067d88a 259 char data[len];
masahikofukasawa 10:5c69b067d88a 260 if( ak9752->read(address, data, len) != AK9752::SUCCESS) {
masahikofukasawa 11:cef8dc1cf010 261 MSG("#Error register read.\r\n");
masahikofukasawa 15:1238993fd75f 262 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 263 }
masahikofukasawa 10:5c69b067d88a 264 for(int i=0; i<len; i++){
masahikofukasawa 10:5c69b067d88a 265 out->setArgument(i, data[i]);
masahikofukasawa 10:5c69b067d88a 266 }
masahikofukasawa 10:5c69b067d88a 267 break;
masahikofukasawa 10:5c69b067d88a 268 }
masahikofukasawa 10:5c69b067d88a 269 default:
masahikofukasawa 10:5c69b067d88a 270 {
masahikofukasawa 11:cef8dc1cf010 271 MSG("#Error no command.\r\n");
masahikofukasawa 10:5c69b067d88a 272 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 273 break;
masahikofukasawa 10:5c69b067d88a 274 }
masahikofukasawa 10:5c69b067d88a 275 }
masahikofukasawa 10:5c69b067d88a 276 return status;
masahikofukasawa 10:5c69b067d88a 277 }
masahikofukasawa 13:d008249f0359 278 char* Ak9752Ctrl::getSensorName(){
masahikofukasawa 13:d008249f0359 279 return sensorName;
masahikofukasawa 13:d008249f0359 280 }