Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

Committer:
masahikofukasawa
Date:
Tue Jul 18 23:20:26 2017 +0000
Revision:
39:3821886c902e
Parent:
29:b488d2c89fba
Child:
40:42e48427e4b7
For AK09970 Small demo board. Takezawa-san's request.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masahikofukasawa 10:5c69b067d88a 1 #include "ak09970ctrl.h"
masahikofukasawa 10:5c69b067d88a 2 #include "debug.h"
masahikofukasawa 10:5c69b067d88a 3
masahikofukasawa 29:b488d2c89fba 4 #ifndef PI
masahikofukasawa 29:b488d2c89fba 5 #define PI 3.14159265358979323846
masahikofukasawa 29:b488d2c89fba 6 #endif
masahikofukasawa 29:b488d2c89fba 7
masahikofukasawa 10:5c69b067d88a 8 #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low)))
masahikofukasawa 10:5c69b067d88a 9 /**
masahikofukasawa 10:5c69b067d88a 10 * Constructor.
masahikofukasawa 10:5c69b067d88a 11 *
masahikofukasawa 10:5c69b067d88a 12 */
masahikofukasawa 27:41aa9fb23a2f 13 Ak09970Ctrl::Ak09970Ctrl() : AkmSensor(){
masahikofukasawa 10:5c69b067d88a 14 ak09970 = NULL;
masahikofukasawa 29:b488d2c89fba 15 // interrupt = NULL;
masahikofukasawa 10:5c69b067d88a 16 }
masahikofukasawa 10:5c69b067d88a 17
masahikofukasawa 10:5c69b067d88a 18 /**
masahikofukasawa 10:5c69b067d88a 19 * Destructor.
masahikofukasawa 10:5c69b067d88a 20 *
masahikofukasawa 10:5c69b067d88a 21 */
masahikofukasawa 10:5c69b067d88a 22 Ak09970Ctrl::~Ak09970Ctrl(){
masahikofukasawa 10:5c69b067d88a 23 if (ak09970) delete ak09970;
masahikofukasawa 10:5c69b067d88a 24 }
masahikofukasawa 10:5c69b067d88a 25
masahikofukasawa 10:5c69b067d88a 26 AkmSensor::Status Ak09970Ctrl::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 // initialize readConfig
masahikofukasawa 10:5c69b067d88a 31 readConfig.enabledReadX = false;
masahikofukasawa 10:5c69b067d88a 32 readConfig.enabledReadY = false;
masahikofukasawa 10:5c69b067d88a 33 readConfig.enabledReadZ = false;
masahikofukasawa 10:5c69b067d88a 34 readConfig.enabledUpperOnly = false;
masahikofukasawa 10:5c69b067d88a 35
masahikofukasawa 10:5c69b067d88a 36 if(primaryId == AKM_PRIMARY_ID_AKD_I2C){
masahikofukasawa 10:5c69b067d88a 37
masahikofukasawa 10:5c69b067d88a 38 I2C* i2c = new I2C(I2C_SDA,I2C_SCL);
masahikofukasawa 10:5c69b067d88a 39 i2c->frequency(I2C_SPEED_100KHZ);
masahikofukasawa 10:5c69b067d88a 40
masahikofukasawa 10:5c69b067d88a 41 switch(subid){
masahikofukasawa 10:5c69b067d88a 42 case Ak09970Ctrl::SUB_ID_AK09970:
masahikofukasawa 10:5c69b067d88a 43 ak09970 = new AK09970();
masahikofukasawa 10:5c69b067d88a 44 Ak09970Ctrl::sensorName = "AK09970";
masahikofukasawa 10:5c69b067d88a 45 break;
masahikofukasawa 10:5c69b067d88a 46 default:
masahikofukasawa 10:5c69b067d88a 47 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 48 }
masahikofukasawa 10:5c69b067d88a 49
masahikofukasawa 10:5c69b067d88a 50 bool foundSensor = false;
masahikofukasawa 10:5c69b067d88a 51
masahikofukasawa 10:5c69b067d88a 52 AK09970::SlaveAddress slaveAddr[]
masahikofukasawa 10:5c69b067d88a 53 = { AK09970::SLAVE_ADDR_1,
masahikofukasawa 10:5c69b067d88a 54 AK09970::SLAVE_ADDR_2};
masahikofukasawa 10:5c69b067d88a 55
masahikofukasawa 10:5c69b067d88a 56 for(int i=0; i<sizeof(slaveAddr); i++)
masahikofukasawa 10:5c69b067d88a 57 {
masahikofukasawa 10:5c69b067d88a 58 ak09970->init(i2c, slaveAddr[i]);
masahikofukasawa 10:5c69b067d88a 59 // Checks connectivity
masahikofukasawa 10:5c69b067d88a 60 if(ak09970->checkConnection() == AK09970::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 61 // found
masahikofukasawa 10:5c69b067d88a 62 foundSensor = true;
masahikofukasawa 10:5c69b067d88a 63 break;
masahikofukasawa 10:5c69b067d88a 64 }
masahikofukasawa 10:5c69b067d88a 65 }
masahikofukasawa 10:5c69b067d88a 66 if(foundSensor != true){
masahikofukasawa 10:5c69b067d88a 67 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 68 }
masahikofukasawa 10:5c69b067d88a 69
masahikofukasawa 10:5c69b067d88a 70 // software reset
masahikofukasawa 10:5c69b067d88a 71 ak09970->reset();
masahikofukasawa 10:5c69b067d88a 72
masahikofukasawa 10:5c69b067d88a 73 }else if(primaryId == AKM_PRIMARY_ID_AKD_SPI){
masahikofukasawa 10:5c69b067d88a 74
masahikofukasawa 10:5c69b067d88a 75 SPI* spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK);
masahikofukasawa 10:5c69b067d88a 76 spi->format(8,3); // 8bit, Mode=3
masahikofukasawa 10:5c69b067d88a 77 spi->frequency(1000000);
masahikofukasawa 10:5c69b067d88a 78
masahikofukasawa 10:5c69b067d88a 79 DigitalOut* cs = new DigitalOut(SPI_CS);
masahikofukasawa 10:5c69b067d88a 80
masahikofukasawa 10:5c69b067d88a 81 switch(subid){
masahikofukasawa 10:5c69b067d88a 82 case Ak09970Ctrl::SUB_ID_AK09970:
masahikofukasawa 10:5c69b067d88a 83 ak09970 = new AK09970();
masahikofukasawa 10:5c69b067d88a 84 Ak09970Ctrl::sensorName = "AK09970";
masahikofukasawa 10:5c69b067d88a 85 break;
masahikofukasawa 10:5c69b067d88a 86 default:
masahikofukasawa 10:5c69b067d88a 87 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 88 }
masahikofukasawa 10:5c69b067d88a 89
masahikofukasawa 10:5c69b067d88a 90 bool foundSensor = false;
masahikofukasawa 10:5c69b067d88a 91 ak09970->init(spi, cs);
masahikofukasawa 10:5c69b067d88a 92 if(ak09970->checkConnection() == AK09970::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 93 foundSensor = true;
masahikofukasawa 10:5c69b067d88a 94 }
masahikofukasawa 10:5c69b067d88a 95 if(foundSensor != true){
masahikofukasawa 39:3821886c902e 96 MSG("#Error: Failed checkConnetion(SPI). %s\r\n",Ak09970Ctrl::sensorName);
masahikofukasawa 10:5c69b067d88a 97 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 98 }
masahikofukasawa 10:5c69b067d88a 99
masahikofukasawa 10:5c69b067d88a 100 // software reset
masahikofukasawa 10:5c69b067d88a 101 ak09970->reset();
masahikofukasawa 10:5c69b067d88a 102 }
masahikofukasawa 10:5c69b067d88a 103 else{
masahikofukasawa 10:5c69b067d88a 104 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 105 }
masahikofukasawa 10:5c69b067d88a 106
masahikofukasawa 11:cef8dc1cf010 107 MSG("#%s detected.\r\n", Ak09970Ctrl::sensorName);
masahikofukasawa 10:5c69b067d88a 108 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 109 }
masahikofukasawa 10:5c69b067d88a 110
masahikofukasawa 29:b488d2c89fba 111 void Ak09970Ctrl::setEvent(){
masahikofukasawa 29:b488d2c89fba 112 // MSG("#setEvent() in %s.\r\n",sensorName);
masahikofukasawa 29:b488d2c89fba 113 AK09970::Status status = ak09970->isDataReady();
masahikofukasawa 29:b488d2c89fba 114 if( status == AK09970::DATA_READY ) base::setEvent();
masahikofukasawa 10:5c69b067d88a 115 }
masahikofukasawa 10:5c69b067d88a 116
masahikofukasawa 10:5c69b067d88a 117 AkmSensor::Status Ak09970Ctrl::startSensor(){
masahikofukasawa 11:cef8dc1cf010 118 // read one data to clear switch
masahikofukasawa 11:cef8dc1cf010 119 AK09970::SwitchStatus sw_status;
masahikofukasawa 11:cef8dc1cf010 120 ak09970->getSwitchStatus(&sw_status, readConfig);
masahikofukasawa 11:cef8dc1cf010 121
masahikofukasawa 11:cef8dc1cf010 122 // set operation mode
masahikofukasawa 10:5c69b067d88a 123 if(ak09970->setOperationMode(mode,sensorDriveMode,sensorMeasurementRange) != AK09970::SUCCESS) {
masahikofukasawa 39:3821886c902e 124 MSG("#Error: Start sensor %s.\r\n", sensorName);
masahikofukasawa 10:5c69b067d88a 125 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 126 }
masahikofukasawa 10:5c69b067d88a 127
masahikofukasawa 10:5c69b067d88a 128 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 129 }
masahikofukasawa 10:5c69b067d88a 130
masahikofukasawa 10:5c69b067d88a 131 AkmSensor::Status Ak09970Ctrl::startSensor(const float sec){
masahikofukasawa 39:3821886c902e 132 MSG("#Error: Start sensor %s.\r\n", sensorName);
masahikofukasawa 11:cef8dc1cf010 133 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 134 }
masahikofukasawa 10:5c69b067d88a 135
masahikofukasawa 10:5c69b067d88a 136 AkmSensor::Status Ak09970Ctrl::stopSensor(){
masahikofukasawa 29:b488d2c89fba 137 AkmSensor::clearEvent();
masahikofukasawa 11:cef8dc1cf010 138
masahikofukasawa 10:5c69b067d88a 139 if(ak09970->setOperationMode(AK09970::MODE_POWER_DOWN, sensorDriveMode, sensorMeasurementRange) != AK09970::SUCCESS) {
masahikofukasawa 39:3821886c902e 140 MSG("#Error: Stop sensor %s.\r\n", sensorName);
masahikofukasawa 10:5c69b067d88a 141 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 142 }
masahikofukasawa 11:cef8dc1cf010 143
masahikofukasawa 11:cef8dc1cf010 144 // read one data to clear switch
masahikofukasawa 11:cef8dc1cf010 145 AK09970::SwitchStatus sw_status;
masahikofukasawa 11:cef8dc1cf010 146 ak09970->getSwitchStatus(&sw_status, readConfig);
masahikofukasawa 11:cef8dc1cf010 147
masahikofukasawa 10:5c69b067d88a 148 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 149 }
masahikofukasawa 10:5c69b067d88a 150
masahikofukasawa 10:5c69b067d88a 151 AkmSensor::Status Ak09970Ctrl::readSensorData(Message* msg){
masahikofukasawa 29:b488d2c89fba 152 AkmSensor::clearEvent();
masahikofukasawa 10:5c69b067d88a 153
masahikofukasawa 10:5c69b067d88a 154 AK09970::SwitchStatus sw_status;
masahikofukasawa 10:5c69b067d88a 155 if( ak09970->getSwitchStatus(&sw_status, readConfig) != AkmSensor::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 156 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 157 }
masahikofukasawa 10:5c69b067d88a 158
masahikofukasawa 10:5c69b067d88a 159 msg->setCommand(Message::CMD_START_MEASUREMENT);
masahikofukasawa 10:5c69b067d88a 160 msg->setArgument(0, sensorMeasurementRange);
masahikofukasawa 10:5c69b067d88a 161 msg->setArgument(1, sw_status.st_hi);
masahikofukasawa 10:5c69b067d88a 162 msg->setArgument(2, sw_status.st_lo);
masahikofukasawa 10:5c69b067d88a 163 msg->setArgument(3, (char)(sw_status.mag.mx>>8));
masahikofukasawa 10:5c69b067d88a 164 msg->setArgument(4, (char)(sw_status.mag.mx & 0x00FF));
masahikofukasawa 10:5c69b067d88a 165 msg->setArgument(5, (char)(sw_status.mag.my>>8));
masahikofukasawa 10:5c69b067d88a 166 msg->setArgument(6, (char)(sw_status.mag.my & 0x00FF));
masahikofukasawa 10:5c69b067d88a 167 msg->setArgument(7, (char)(sw_status.mag.mz>>8));
masahikofukasawa 10:5c69b067d88a 168 msg->setArgument(8, (char)(sw_status.mag.mz & 0x00FF));
masahikofukasawa 10:5c69b067d88a 169
masahikofukasawa 10:5c69b067d88a 170 return AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 171 }
masahikofukasawa 10:5c69b067d88a 172
masahikofukasawa 10:5c69b067d88a 173 AkmSensor::Status Ak09970Ctrl::requestCommand(Message* in, Message* out){
masahikofukasawa 10:5c69b067d88a 174 AkmSensor::Status status = AkmSensor::SUCCESS;
masahikofukasawa 10:5c69b067d88a 175 Message::Command cmd = in->getCommand();
masahikofukasawa 10:5c69b067d88a 176 out->setCommand(cmd);
masahikofukasawa 10:5c69b067d88a 177
masahikofukasawa 10:5c69b067d88a 178 switch(cmd){
masahikofukasawa 10:5c69b067d88a 179 case Message::CMD_PROGSW_GET_THRESHOLD:
masahikofukasawa 10:5c69b067d88a 180 {
masahikofukasawa 10:5c69b067d88a 181 if(in->getArgNum() != 1){
masahikofukasawa 39:3821886c902e 182 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 183 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 184 }
masahikofukasawa 10:5c69b067d88a 185
masahikofukasawa 10:5c69b067d88a 186 AK09970::SensorAxis axis = (AK09970::SensorAxis)in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 187 AK09970::Threshold th;
masahikofukasawa 10:5c69b067d88a 188 if( ak09970->getThreshold(axis, &th) != AkmSensor::SUCCESS ){
masahikofukasawa 10:5c69b067d88a 189 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 190 }
masahikofukasawa 10:5c69b067d88a 191 out->setArgument(0, axis);
masahikofukasawa 10:5c69b067d88a 192 out->setArgument(1,(char)((int32_t)(th.BOP1) >> 8));
masahikofukasawa 10:5c69b067d88a 193 out->setArgument(2,(char)((int32_t)(th.BOP1) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 194 out->setArgument(3,(char)((int32_t)(th.BRP1) >> 8));
masahikofukasawa 10:5c69b067d88a 195 out->setArgument(4,(char)((int32_t)(th.BRP1) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 196 out->setArgument(5,(char)((int32_t)(th.BOP2) >> 8));
masahikofukasawa 10:5c69b067d88a 197 out->setArgument(6,(char)((int32_t)(th.BOP2) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 198 out->setArgument(7,(char)((int32_t)(th.BRP2) >> 8));
masahikofukasawa 10:5c69b067d88a 199 out->setArgument(8,(char)((int32_t)(th.BRP2) & 0x00FF));
masahikofukasawa 10:5c69b067d88a 200 break;
masahikofukasawa 10:5c69b067d88a 201 }
masahikofukasawa 10:5c69b067d88a 202 case Message::CMD_PROGSW_SET_THRESHOLD:
masahikofukasawa 10:5c69b067d88a 203 {
masahikofukasawa 10:5c69b067d88a 204 if(in->getArgNum() != 9){
masahikofukasawa 39:3821886c902e 205 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 206 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 207 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 208 return status;
masahikofukasawa 10:5c69b067d88a 209 }
masahikofukasawa 10:5c69b067d88a 210
masahikofukasawa 10:5c69b067d88a 211 AK09970::SensorAxis axis = (AK09970::SensorAxis)in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 212 AK09970::Threshold th;
masahikofukasawa 10:5c69b067d88a 213
masahikofukasawa 10:5c69b067d88a 214 th.BOP1 = CONV16I(in->getArgument(1),in->getArgument(2));
masahikofukasawa 10:5c69b067d88a 215 th.BRP1 = CONV16I(in->getArgument(3),in->getArgument(4));
masahikofukasawa 10:5c69b067d88a 216 th.BOP2 = CONV16I(in->getArgument(5),in->getArgument(6));
masahikofukasawa 10:5c69b067d88a 217 th.BRP2 = CONV16I(in->getArgument(7),in->getArgument(8));
masahikofukasawa 10:5c69b067d88a 218 if( ak09970->setThreshold(axis, th) != AkmSensor::SUCCESS ){
masahikofukasawa 10:5c69b067d88a 219 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 220 }
masahikofukasawa 10:5c69b067d88a 221 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 222 break;
masahikofukasawa 10:5c69b067d88a 223 }
masahikofukasawa 10:5c69b067d88a 224 case Message::CMD_PROGSW_GET_READ_COFIGURATION:
masahikofukasawa 10:5c69b067d88a 225 {
masahikofukasawa 10:5c69b067d88a 226 if(in->getArgNum() != 0){
masahikofukasawa 39:3821886c902e 227 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 228 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 229 }
masahikofukasawa 10:5c69b067d88a 230
masahikofukasawa 10:5c69b067d88a 231 out->setArgument(0, readConfig.enabledReadX);
masahikofukasawa 10:5c69b067d88a 232 out->setArgument(1, readConfig.enabledReadY);
masahikofukasawa 10:5c69b067d88a 233 out->setArgument(2, readConfig.enabledReadZ);
masahikofukasawa 10:5c69b067d88a 234 out->setArgument(3, readConfig.enabledUpperOnly);
masahikofukasawa 10:5c69b067d88a 235 break;
masahikofukasawa 10:5c69b067d88a 236 }
masahikofukasawa 10:5c69b067d88a 237 case Message::CMD_PROGSW_SET_READ_COFIGURATION:
masahikofukasawa 10:5c69b067d88a 238 {
masahikofukasawa 10:5c69b067d88a 239 if(in->getArgNum() != 4){
masahikofukasawa 39:3821886c902e 240 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 241 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 242 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 243 return status;
masahikofukasawa 10:5c69b067d88a 244 }
masahikofukasawa 10:5c69b067d88a 245
masahikofukasawa 10:5c69b067d88a 246 readConfig.enabledReadX = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 247 readConfig.enabledReadY = in->getArgument(1);
masahikofukasawa 10:5c69b067d88a 248 readConfig.enabledReadZ = in->getArgument(2);
masahikofukasawa 10:5c69b067d88a 249 readConfig.enabledUpperOnly = in->getArgument(3);
masahikofukasawa 10:5c69b067d88a 250 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 251 break;
masahikofukasawa 10:5c69b067d88a 252 }
masahikofukasawa 10:5c69b067d88a 253 case Message::CMD_PROGSW_GET_SWITCH_COFIGURATION:
masahikofukasawa 10:5c69b067d88a 254 {
masahikofukasawa 10:5c69b067d88a 255 if(in->getArgNum() != 0){
masahikofukasawa 39:3821886c902e 256 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 257 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 258 }
masahikofukasawa 10:5c69b067d88a 259
masahikofukasawa 10:5c69b067d88a 260 if(ak09970->getSwitchConfig(&switchConfig) != AK09970::SUCCESS) {
masahikofukasawa 39:3821886c902e 261 MSG("#Error: getSwitchConfig. AK09970.\r\n");
masahikofukasawa 15:1238993fd75f 262 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 263 }
masahikofukasawa 10:5c69b067d88a 264 out->setArgument(0,switchConfig.enabledODINTEN);
masahikofukasawa 10:5c69b067d88a 265 out->setArgument(1,switchConfig.enabledINTEN);
masahikofukasawa 10:5c69b067d88a 266 out->setArgument(2,switchConfig.enabledERRADCEN);
masahikofukasawa 10:5c69b067d88a 267 out->setArgument(3,switchConfig.enabledERRXYEN);
masahikofukasawa 10:5c69b067d88a 268 out->setArgument(4,switchConfig.enabledSWZ2EN);
masahikofukasawa 10:5c69b067d88a 269 out->setArgument(5,switchConfig.enabledSWZ1EN);
masahikofukasawa 10:5c69b067d88a 270 out->setArgument(6,switchConfig.enabledSWY2EN);
masahikofukasawa 10:5c69b067d88a 271 out->setArgument(7,switchConfig.enabledSWY1EN);
masahikofukasawa 10:5c69b067d88a 272 out->setArgument(8,switchConfig.enabledSWX2EN);
masahikofukasawa 10:5c69b067d88a 273 out->setArgument(9,switchConfig.enabledSWX1EN);
masahikofukasawa 10:5c69b067d88a 274 out->setArgument(10,switchConfig.enabledDRDYEN);
masahikofukasawa 10:5c69b067d88a 275 break;
masahikofukasawa 10:5c69b067d88a 276 }
masahikofukasawa 10:5c69b067d88a 277 case Message::CMD_PROGSW_SET_SWITCH_COFIGURATION:
masahikofukasawa 10:5c69b067d88a 278 {
masahikofukasawa 10:5c69b067d88a 279 if(in->getArgNum() != 11){
masahikofukasawa 39:3821886c902e 280 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 281 for(int i=0; i<in->getArgNum(); i++){
masahikofukasawa 11:cef8dc1cf010 282 MSG("#%d = %02X\r\n",i,in->getArgument(i));
masahikofukasawa 10:5c69b067d88a 283 }
masahikofukasawa 10:5c69b067d88a 284 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 285 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 286 return status;
masahikofukasawa 10:5c69b067d88a 287 }
masahikofukasawa 10:5c69b067d88a 288
masahikofukasawa 10:5c69b067d88a 289 switchConfig.enabledODINTEN = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 290 switchConfig.enabledINTEN = in->getArgument(1);
masahikofukasawa 10:5c69b067d88a 291 switchConfig.enabledERRADCEN = in->getArgument(2);
masahikofukasawa 10:5c69b067d88a 292 switchConfig.enabledERRXYEN = in->getArgument(3);
masahikofukasawa 10:5c69b067d88a 293 switchConfig.enabledSWZ2EN = in->getArgument(4);
masahikofukasawa 10:5c69b067d88a 294 switchConfig.enabledSWZ1EN = in->getArgument(5);
masahikofukasawa 10:5c69b067d88a 295 switchConfig.enabledSWY2EN = in->getArgument(6);
masahikofukasawa 10:5c69b067d88a 296 switchConfig.enabledSWY1EN = in->getArgument(7);
masahikofukasawa 10:5c69b067d88a 297 switchConfig.enabledSWX2EN = in->getArgument(8);
masahikofukasawa 10:5c69b067d88a 298 switchConfig.enabledSWX1EN = in->getArgument(9);
masahikofukasawa 10:5c69b067d88a 299 switchConfig.enabledDRDYEN = in->getArgument(10);
masahikofukasawa 10:5c69b067d88a 300
masahikofukasawa 10:5c69b067d88a 301 if(ak09970->setSwitchConfig(switchConfig) != AK09970::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 302 status = AkmSensor::ERROR;
masahikofukasawa 39:3821886c902e 303 MSG("#Error: setSwitchConfig. AK09970.\r\n");
masahikofukasawa 10:5c69b067d88a 304 }
masahikofukasawa 10:5c69b067d88a 305 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 306 break;
masahikofukasawa 10:5c69b067d88a 307 }
masahikofukasawa 10:5c69b067d88a 308 case Message::CMD_PROGSW_GET_OPERATION_MODE:
masahikofukasawa 10:5c69b067d88a 309 {
masahikofukasawa 10:5c69b067d88a 310 if(in->getArgNum() != 0){
masahikofukasawa 39:3821886c902e 311 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 312 return AkmSensor::ERROR;
masahikofukasawa 15:1238993fd75f 313 }
masahikofukasawa 10:5c69b067d88a 314
masahikofukasawa 10:5c69b067d88a 315 if(ak09970->getOperationMode(&mode, &sensorDriveMode, &sensorMeasurementRange) != AK09970::SUCCESS) {
masahikofukasawa 39:3821886c902e 316 MSG("#Error: getOperationMode. AK09970.\r\n");
masahikofukasawa 15:1238993fd75f 317 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 318 }
masahikofukasawa 10:5c69b067d88a 319 out->setArgument(0,(char)sensorMeasurementRange);
masahikofukasawa 10:5c69b067d88a 320 out->setArgument(1,(char)sensorDriveMode);
masahikofukasawa 10:5c69b067d88a 321 out->setArgument(2,(char)mode);
masahikofukasawa 10:5c69b067d88a 322 break;
masahikofukasawa 10:5c69b067d88a 323 }
masahikofukasawa 10:5c69b067d88a 324 case Message::CMD_PROGSW_SET_OPERATION_MODE:
masahikofukasawa 10:5c69b067d88a 325 {
masahikofukasawa 10:5c69b067d88a 326 if(in->getArgNum() != 3){
masahikofukasawa 39:3821886c902e 327 MSG("#Error: Argument num. AK09970. Args=%d\r\n",in->getArgNum());
masahikofukasawa 10:5c69b067d88a 328 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 329 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 330 return status;
masahikofukasawa 10:5c69b067d88a 331 }
masahikofukasawa 10:5c69b067d88a 332
masahikofukasawa 10:5c69b067d88a 333 sensorMeasurementRange = (AK09970::SensorMeasurementRange)(in->getArgument(0));
masahikofukasawa 10:5c69b067d88a 334 sensorDriveMode = (AK09970::SensorDriveMode)(in->getArgument(1));
masahikofukasawa 10:5c69b067d88a 335 mode = (AK09970::OperationMode)(in->getArgument(2));
masahikofukasawa 10:5c69b067d88a 336 if(ak09970->setOperationMode(mode, sensorDriveMode, sensorMeasurementRange) != AK09970::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 337 status = AkmSensor::ERROR;
masahikofukasawa 39:3821886c902e 338 MSG("#Error: setOperationMode. AK09970. %d,%d,%d\r\n",mode,sensorDriveMode,sensorMeasurementRange);
masahikofukasawa 10:5c69b067d88a 339 }
masahikofukasawa 10:5c69b067d88a 340 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 341 break;
masahikofukasawa 10:5c69b067d88a 342 }
masahikofukasawa 10:5c69b067d88a 343 case Message::CMD_REG_WRITE:
masahikofukasawa 10:5c69b067d88a 344 case Message::CMD_REG_WRITEN:
masahikofukasawa 10:5c69b067d88a 345 {
masahikofukasawa 10:5c69b067d88a 346 char address = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 347 int len = (int)in->getArgument(1);
masahikofukasawa 15:1238993fd75f 348 if(in->getArgNum() != len+2){
masahikofukasawa 39:3821886c902e 349 MSG("#Error: Argument num. Args=%d\r\n",in->getArgNum());
masahikofukasawa 15:1238993fd75f 350 status = AkmSensor::ERROR;
masahikofukasawa 15:1238993fd75f 351 out->setArgument(0,(char)status);
masahikofukasawa 15:1238993fd75f 352 return status;
masahikofukasawa 15:1238993fd75f 353 }
masahikofukasawa 15:1238993fd75f 354
masahikofukasawa 10:5c69b067d88a 355 char data[len];
masahikofukasawa 10:5c69b067d88a 356 for(int i=0; i<len; i++){
masahikofukasawa 15:1238993fd75f 357 data[i] = in->getArgument(i+2);
masahikofukasawa 10:5c69b067d88a 358 }
masahikofukasawa 10:5c69b067d88a 359 if( ak09970->write(address, data, len) != AK09970::SUCCESS) {
masahikofukasawa 10:5c69b067d88a 360 status = AkmSensor::ERROR;
masahikofukasawa 39:3821886c902e 361 MSG("#Error: Register write.\r\n");
masahikofukasawa 10:5c69b067d88a 362 }
masahikofukasawa 10:5c69b067d88a 363 out->setArgument(0,(char)status);
masahikofukasawa 10:5c69b067d88a 364 break;
masahikofukasawa 10:5c69b067d88a 365 }
masahikofukasawa 10:5c69b067d88a 366 case Message::CMD_REG_READ:
masahikofukasawa 10:5c69b067d88a 367 case Message::CMD_REG_READN:
masahikofukasawa 10:5c69b067d88a 368 {
masahikofukasawa 10:5c69b067d88a 369 if(in->getArgNum() != 2){
masahikofukasawa 39:3821886c902e 370 MSG("#Error: Argument num. Args=%d\r\n",in->getArgNum());
masahikofukasawa 15:1238993fd75f 371 return AkmSensor::ERROR;
masahikofukasawa 15:1238993fd75f 372 }
masahikofukasawa 10:5c69b067d88a 373
masahikofukasawa 10:5c69b067d88a 374 char address = in->getArgument(0);
masahikofukasawa 10:5c69b067d88a 375 int len = (int)in->getArgument(1);
masahikofukasawa 10:5c69b067d88a 376 char data[len];
masahikofukasawa 10:5c69b067d88a 377 if( ak09970->read(address, data, len) != AK09970::SUCCESS) {
masahikofukasawa 39:3821886c902e 378 MSG("#Error: Register read.\r\n");
masahikofukasawa 15:1238993fd75f 379 return AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 380 }
masahikofukasawa 10:5c69b067d88a 381 for(int i=0; i<len; i++){
masahikofukasawa 10:5c69b067d88a 382 out->setArgument(i, data[i]);
masahikofukasawa 10:5c69b067d88a 383 }
masahikofukasawa 10:5c69b067d88a 384 break;
masahikofukasawa 10:5c69b067d88a 385 }
masahikofukasawa 10:5c69b067d88a 386 default:
masahikofukasawa 10:5c69b067d88a 387 {
masahikofukasawa 39:3821886c902e 388 MSG("#Error: No command.\r\n");
masahikofukasawa 10:5c69b067d88a 389 status = AkmSensor::ERROR;
masahikofukasawa 10:5c69b067d88a 390 break;
masahikofukasawa 10:5c69b067d88a 391 }
masahikofukasawa 10:5c69b067d88a 392 }
masahikofukasawa 10:5c69b067d88a 393 return status;
masahikofukasawa 10:5c69b067d88a 394 }