AKM Development Platform. This is the D7.014 version.

Dependencies:   AK09970 AK099XX AK7401 AK7451 AK8963X AK9750 AK9752 AkmSensor BLE_API I2CNano MCP342x SerialNano SpiNano TCA9554A mbed nRF51822

Fork of AKDP by Masahiko Fukasawa

Committer:
masahikofukasawa
Date:
Wed May 03 18:31:33 2017 +0000
Branch:
multi_sensor_test
Revision:
40:24065d634473
Parent:
32:7c4beec92be9
re-publish for Rev D7.014.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masahikofukasawa 0:c240899240e7 1 #include <stdio.h>
masahikofukasawa 0:c240899240e7 2 #include <stdlib.h>
masahikofukasawa 0:c240899240e7 3 #include "mbed.h"
masahikofukasawa 0:c240899240e7 4 #include "ble/BLE.h"
masahikofukasawa 0:c240899240e7 5 #include "ble/services/UARTService.h"
masahikofukasawa 0:c240899240e7 6 #include "SerialNano.h"
masahikofukasawa 25:12ed05de91f9 7 #include "akdphwinfo.h"
masahikofukasawa 12:522a22a23f8a 8 #include "akmsensor.h"
masahikofukasawa 0:c240899240e7 9 #include "akmsensormanager.h"
masahikofukasawa 0:c240899240e7 10 #include "debug.h"
coisme 2:11a5873f8ec0 11 #include "tca9554a.h"
masahikofukasawa 25:12ed05de91f9 12 #include "mcp342x.h"
masahikofukasawa 19:7a6913400380 13 #include "akmakd.h"
masahikofukasawa 0:c240899240e7 14
masahikofukasawa 0:c240899240e7 15 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
masahikofukasawa 0:c240899240e7 16 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
masahikofukasawa 0:c240899240e7 17 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
masahikofukasawa 10:a710e8c3311c 18 #define BLE_BUF_LEN UARTService::BLE_UART_SERVICE_MAX_DATA_LEN+1
masahikofukasawa 10:a710e8c3311c 19 #define TXRX_LEN 50
masahikofukasawa 0:c240899240e7 20
masahikofukasawa 0:c240899240e7 21 BLE ble;
masahikofukasawa 0:c240899240e7 22 UARTService* uartService;
masahikofukasawa 7:f1f544deaaa3 23 SerialNano serial(P0_4, P0_5); // Rev.D pin configuration
masahikofukasawa 25:12ed05de91f9 24 AkmSensorManager* manager;
masahikofukasawa 4:749a21fd9c2d 25
masahikofukasawa 12:522a22a23f8a 26 uint8_t id;
masahikofukasawa 12:522a22a23f8a 27 uint8_t subId;
masahikofukasawa 0:c240899240e7 28
masahikofukasawa 32:7c4beec92be9 29
masahikofukasawa 18:d75838bceaaf 30 enum {
masahikofukasawa 18:d75838bceaaf 31 UNIT_0_625_MS = 625,
masahikofukasawa 18:d75838bceaaf 32 UNIT_1_25_MS = 1250,
masahikofukasawa 18:d75838bceaaf 33 UNIT_10_MS = 10000
masahikofukasawa 18:d75838bceaaf 34 };
masahikofukasawa 32:7c4beec92be9 35
masahikofukasawa 18:d75838bceaaf 36
masahikofukasawa 19:7a6913400380 37 // default setting of Nexus 5X, Motorola Droid Turbo:
masahikofukasawa 19:7a6913400380 38 //#define MIN_CONN_INTERVAL MSEC_TO_UNITS(45, UNIT_1_25_MS) /**< Minimum connection interval (45 ms) */
masahikofukasawa 19:7a6913400380 39 //#define MAX_CONN_INTERVAL MSEC_TO_UNITS(45, UNIT_1_25_MS) /**< Maximum connection interval (45 ms). */
masahikofukasawa 19:7a6913400380 40 //#define SLAVE_LATENCY 0 /**< Slave latency. */
masahikofukasawa 19:7a6913400380 41 //#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(20000, UNIT_10_MS) /**< Connection supervisory timeout (20 seconds). */
masahikofukasawa 19:7a6913400380 42
masahikofukasawa 19:7a6913400380 43 /*
masahikofukasawa 19:7a6913400380 44 iOS requirement:
masahikofukasawa 19:7a6913400380 45 Interval Max * (Slave Latency + 1) ≤ 2 seconds
masahikofukasawa 19:7a6913400380 46 Interval Min ≥ 20 ms
masahikofukasawa 19:7a6913400380 47 Interval Min + 20 ms ≤ Interval Max Slave Latency ≤ 4
masahikofukasawa 19:7a6913400380 48 connSupervisionTimeout ≤ 6 seconds
masahikofukasawa 19:7a6913400380 49 Interval Max * (Slave Latency + 1) * 3 < connSupervisionTimeout
masahikofukasawa 19:7a6913400380 50 */
masahikofukasawa 19:7a6913400380 51
masahikofukasawa 18:d75838bceaaf 52 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
masahikofukasawa 18:d75838bceaaf 53 #define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum connection interval (7.5 ms) */
masahikofukasawa 18:d75838bceaaf 54 #define MAX_CONN_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS) /**< Maximum connection interval (30 ms). */
masahikofukasawa 18:d75838bceaaf 55 #define SLAVE_LATENCY 0 /**< Slave latency. */
masahikofukasawa 19:7a6913400380 56 #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */
masahikofukasawa 18:d75838bceaaf 57
masahikofukasawa 21:f8f44ad15d13 58 // Command received from BLE
masahikofukasawa 0:c240899240e7 59 void WrittenHandler(const GattWriteCallbackParams *Handler)
masahikofukasawa 0:c240899240e7 60 {
masahikofukasawa 10:a710e8c3311c 61 static char command[TXRX_LEN]="";
masahikofukasawa 10:a710e8c3311c 62 static uint16_t len=0;
masahikofukasawa 10:a710e8c3311c 63
masahikofukasawa 10:a710e8c3311c 64 uint8_t buf[BLE_BUF_LEN];
masahikofukasawa 0:c240899240e7 65 uint16_t bytesRead;
masahikofukasawa 0:c240899240e7 66 if (Handler->handle == uartService->getTXCharacteristicHandle())
masahikofukasawa 0:c240899240e7 67 {
masahikofukasawa 0:c240899240e7 68 ble.gattServer().read(uartService->getTXCharacteristicHandle(), buf, &bytesRead);
masahikofukasawa 10:a710e8c3311c 69
masahikofukasawa 10:a710e8c3311c 70 for(uint16_t i=0; i<bytesRead; i++){
masahikofukasawa 10:a710e8c3311c 71 if(buf[i] == CR)
masahikofukasawa 10:a710e8c3311c 72 {
masahikofukasawa 10:a710e8c3311c 73 ; // ignore CR
masahikofukasawa 10:a710e8c3311c 74 }
masahikofukasawa 10:a710e8c3311c 75 else if(buf[i] == LF || len > TXRX_LEN)
masahikofukasawa 0:c240899240e7 76 {
masahikofukasawa 10:a710e8c3311c 77 manager->commandReceived(command);
masahikofukasawa 10:a710e8c3311c 78 for(int j=0; j<TXRX_LEN; j++){
masahikofukasawa 10:a710e8c3311c 79 command[j] = 0;
masahikofukasawa 10:a710e8c3311c 80 }
masahikofukasawa 10:a710e8c3311c 81 len = 0;
masahikofukasawa 10:a710e8c3311c 82 }
masahikofukasawa 10:a710e8c3311c 83 else
masahikofukasawa 10:a710e8c3311c 84 {
masahikofukasawa 10:a710e8c3311c 85 command[len++] = (char)buf[i];
masahikofukasawa 0:c240899240e7 86 }
masahikofukasawa 0:c240899240e7 87 }
masahikofukasawa 0:c240899240e7 88 }
masahikofukasawa 0:c240899240e7 89 }
masahikofukasawa 0:c240899240e7 90
masahikofukasawa 21:f8f44ad15d13 91
masahikofukasawa 21:f8f44ad15d13 92 // Command received from USB
masahikofukasawa 18:d75838bceaaf 93 static void usbUartCallback(void)
masahikofukasawa 0:c240899240e7 94 {
masahikofukasawa 10:a710e8c3311c 95 static char command[TXRX_LEN] = "";
masahikofukasawa 10:a710e8c3311c 96 static uint16_t len=0;
masahikofukasawa 10:a710e8c3311c 97
masahikofukasawa 23:9a404e36feaf 98 while(serial.readable())
masahikofukasawa 0:c240899240e7 99 {
masahikofukasawa 0:c240899240e7 100 uint8_t c = serial.getc();
masahikofukasawa 21:f8f44ad15d13 101
masahikofukasawa 0:c240899240e7 102 // ignore CR
masahikofukasawa 21:f8f44ad15d13 103 if(c != CR)
masahikofukasawa 0:c240899240e7 104 {
masahikofukasawa 21:f8f44ad15d13 105 command[len++] = c;
masahikofukasawa 21:f8f44ad15d13 106 if(len>=TXRX_LEN || c == LF)
masahikofukasawa 21:f8f44ad15d13 107 {
masahikofukasawa 21:f8f44ad15d13 108 manager->commandReceived(command);
masahikofukasawa 21:f8f44ad15d13 109 for(int j=0; j<TXRX_LEN; j++){
masahikofukasawa 21:f8f44ad15d13 110 command[j] = 0;
masahikofukasawa 21:f8f44ad15d13 111 }
masahikofukasawa 21:f8f44ad15d13 112 len = 0;
masahikofukasawa 10:a710e8c3311c 113 }
masahikofukasawa 21:f8f44ad15d13 114 }
masahikofukasawa 0:c240899240e7 115 }
masahikofukasawa 0:c240899240e7 116 }
masahikofukasawa 0:c240899240e7 117
masahikofukasawa 18:d75838bceaaf 118 static void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
masahikofukasawa 0:c240899240e7 119 {
masahikofukasawa 19:7a6913400380 120 MSG("#From central: minConnectionInterval = %d\r\n", params->connectionParams->minConnectionInterval);
masahikofukasawa 19:7a6913400380 121 MSG("#From central: maxConnectionInterval = %d\r\n", params->connectionParams->maxConnectionInterval);
masahikofukasawa 19:7a6913400380 122 MSG("#From central: slaveLatency = %d\r\n", params->connectionParams->slaveLatency);
masahikofukasawa 19:7a6913400380 123 MSG("#From central: connectionSupervisionTimeout = %d\r\n", params->connectionParams->connectionSupervisionTimeout);
masahikofukasawa 19:7a6913400380 124
masahikofukasawa 18:d75838bceaaf 125 Gap::Handle_t gap_handle = params->handle;
masahikofukasawa 18:d75838bceaaf 126 Gap::ConnectionParams_t gap_conn_params;
masahikofukasawa 19:7a6913400380 127 gap_conn_params.minConnectionInterval = params->connectionParams->minConnectionInterval;
masahikofukasawa 19:7a6913400380 128 gap_conn_params.maxConnectionInterval = params->connectionParams->maxConnectionInterval;
masahikofukasawa 19:7a6913400380 129 gap_conn_params.slaveLatency = params->connectionParams->slaveLatency;
masahikofukasawa 18:d75838bceaaf 130 gap_conn_params.connectionSupervisionTimeout = CONN_SUP_TIMEOUT;
masahikofukasawa 18:d75838bceaaf 131 ble.updateConnectionParams(gap_handle, &gap_conn_params);
masahikofukasawa 18:d75838bceaaf 132
masahikofukasawa 19:7a6913400380 133 MSG("#From peripheral: minConnectionInterval = %d\r\n", gap_conn_params.minConnectionInterval);
masahikofukasawa 19:7a6913400380 134 MSG("#From peripheral: maxConnectionInterval = %d\r\n", gap_conn_params.maxConnectionInterval);
masahikofukasawa 19:7a6913400380 135 MSG("#From peripheral: slaveLatency = %d\r\n", gap_conn_params.slaveLatency);
masahikofukasawa 19:7a6913400380 136 MSG("#From peripheral: connectionSupervisionTimeout = %d\r\n", gap_conn_params.connectionSupervisionTimeout);
masahikofukasawa 19:7a6913400380 137
masahikofukasawa 0:c240899240e7 138 manager->setEventConnected();
masahikofukasawa 14:76205d28fea2 139 MSG("#Connected\r\n");
masahikofukasawa 0:c240899240e7 140 }
masahikofukasawa 0:c240899240e7 141
masahikofukasawa 18:d75838bceaaf 142 static void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
masahikofukasawa 0:c240899240e7 143 {
masahikofukasawa 0:c240899240e7 144 manager->setEventDisconnected();
masahikofukasawa 14:76205d28fea2 145 MSG("#Disconnected\r\n");
masahikofukasawa 0:c240899240e7 146 ble.gap().startAdvertising();
masahikofukasawa 0:c240899240e7 147 }
masahikofukasawa 0:c240899240e7 148
masahikofukasawa 19:7a6913400380 149 void bleSetup(char* device_name){
masahikofukasawa 0:c240899240e7 150 ble.init();
masahikofukasawa 18:d75838bceaaf 151
masahikofukasawa 0:c240899240e7 152 // setup advertising
masahikofukasawa 0:c240899240e7 153 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
masahikofukasawa 0:c240899240e7 154 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
masahikofukasawa 0:c240899240e7 155 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
masahikofukasawa 19:7a6913400380 156 (const uint8_t *)device_name, strlen(device_name));
masahikofukasawa 19:7a6913400380 157 // (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
masahikofukasawa 0:c240899240e7 158 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
masahikofukasawa 0:c240899240e7 159 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
masahikofukasawa 19:7a6913400380 160
masahikofukasawa 18:d75838bceaaf 161
masahikofukasawa 18:d75838bceaaf 162 // Set desired connection parameters
masahikofukasawa 18:d75838bceaaf 163 Gap::ConnectionParams_t gap_conn_params;
masahikofukasawa 18:d75838bceaaf 164 gap_conn_params.minConnectionInterval = MIN_CONN_INTERVAL;
masahikofukasawa 18:d75838bceaaf 165 gap_conn_params.maxConnectionInterval = MAX_CONN_INTERVAL;
masahikofukasawa 18:d75838bceaaf 166 gap_conn_params.slaveLatency = SLAVE_LATENCY;
masahikofukasawa 18:d75838bceaaf 167 gap_conn_params.connectionSupervisionTimeout = CONN_SUP_TIMEOUT;
masahikofukasawa 18:d75838bceaaf 168 ble.setPreferredConnectionParams(&gap_conn_params);
masahikofukasawa 18:d75838bceaaf 169
masahikofukasawa 18:d75838bceaaf 170 ble.gap().onDisconnection(disconnectionCallback);
masahikofukasawa 0:c240899240e7 171 ble.gap().onConnection(connectionCallback);
masahikofukasawa 0:c240899240e7 172 ble.gattServer().onDataWritten(WrittenHandler);
masahikofukasawa 0:c240899240e7 173
masahikofukasawa 12:522a22a23f8a 174 // 100ms; in multiples of 0.625ms.
masahikofukasawa 18:d75838bceaaf 175 ble.gap().setAdvertisingInterval(160);
masahikofukasawa 0:c240899240e7 176 ble.gap().startAdvertising();
masahikofukasawa 12:522a22a23f8a 177 }
masahikofukasawa 0:c240899240e7 178
masahikofukasawa 12:522a22a23f8a 179
masahikofukasawa 12:522a22a23f8a 180 int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) {
masahikofukasawa 12:522a22a23f8a 181 const int WAIT_ADC_MS = 1;
masahikofukasawa 12:522a22a23f8a 182
masahikofukasawa 12:522a22a23f8a 183 // Configure channel and trigger.
masahikofukasawa 12:522a22a23f8a 184 mcp3428->setChannel(ch);
masahikofukasawa 12:522a22a23f8a 185 mcp3428->setSampleSetting(s);
masahikofukasawa 12:522a22a23f8a 186 mcp3428->trigger();
masahikofukasawa 12:522a22a23f8a 187
masahikofukasawa 12:522a22a23f8a 188 // polling data (!blocking)
masahikofukasawa 12:522a22a23f8a 189 MCP342X::Data data;
masahikofukasawa 12:522a22a23f8a 190 do {
masahikofukasawa 12:522a22a23f8a 191 wait_ms(WAIT_ADC_MS);
masahikofukasawa 12:522a22a23f8a 192 mcp3428->getData(&data);
masahikofukasawa 12:522a22a23f8a 193 } while(data.st == MCP342X::DATA_NOT_UPDATED);
masahikofukasawa 12:522a22a23f8a 194
masahikofukasawa 12:522a22a23f8a 195 return data.value;
masahikofukasawa 12:522a22a23f8a 196 }
masahikofukasawa 12:522a22a23f8a 197
masahikofukasawa 12:522a22a23f8a 198 uint8_t getId(PinName pin, uint8_t bits)
masahikofukasawa 12:522a22a23f8a 199 {
masahikofukasawa 12:522a22a23f8a 200 I2C i2c(I2C_SDA, I2C_SCL);
masahikofukasawa 12:522a22a23f8a 201 // ADC
masahikofukasawa 12:522a22a23f8a 202 MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH);
masahikofukasawa 12:522a22a23f8a 203 mcp342x.setConversionMode(MCP342X::ONE_SHOT);
masahikofukasawa 12:522a22a23f8a 204 MCP342X::AdcChannel ch;
masahikofukasawa 12:522a22a23f8a 205 if (pin == ANALOG_SENSOR_ID) {
masahikofukasawa 12:522a22a23f8a 206 ch = MCP342X::ADC_CH1;
masahikofukasawa 12:522a22a23f8a 207 } else { // pin == ANALOG_SENSOR_ID_SUB
masahikofukasawa 12:522a22a23f8a 208 ch = MCP342X::ADC_CH2;
masahikofukasawa 12:522a22a23f8a 209 }
masahikofukasawa 12:522a22a23f8a 210 int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT);
masahikofukasawa 14:76205d28fea2 211 // MSG("#12bit ADC Val = %d.\r\n", val);
masahikofukasawa 12:522a22a23f8a 212
masahikofukasawa 12:522a22a23f8a 213 const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V
masahikofukasawa 12:522a22a23f8a 214 const int16_t VAL_MIN = -2048; // Corresponds to 0V
masahikofukasawa 12:522a22a23f8a 215
masahikofukasawa 12:522a22a23f8a 216 uint8_t value = (uint8_t)((val - VAL_MIN)/(float)(VAL_MAX - VAL_MIN) * (1 << bits) + 0.5);
masahikofukasawa 14:76205d28fea2 217 // MSG("#ID = %d.\r\n", value);
masahikofukasawa 12:522a22a23f8a 218
masahikofukasawa 12:522a22a23f8a 219 return value;
masahikofukasawa 12:522a22a23f8a 220 }
masahikofukasawa 12:522a22a23f8a 221
masahikofukasawa 12:522a22a23f8a 222
masahikofukasawa 12:522a22a23f8a 223 void releaseTWI(){
masahikofukasawa 12:522a22a23f8a 224 NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
masahikofukasawa 12:522a22a23f8a 225 NRF_TWI0->POWER = 0;
masahikofukasawa 12:522a22a23f8a 226 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
masahikofukasawa 12:522a22a23f8a 227 NRF_TWI1->POWER = 0;
masahikofukasawa 12:522a22a23f8a 228 }
masahikofukasawa 12:522a22a23f8a 229
masahikofukasawa 14:76205d28fea2 230 bool initAkdpBoard(){
masahikofukasawa 17:a22b52136eb8 231 MSG("#Init AKDP board.\r\n");
masahikofukasawa 17:a22b52136eb8 232 const int TIME_FOR_OE_MS = 100;
masahikofukasawa 17:a22b52136eb8 233
masahikofukasawa 14:76205d28fea2 234 // CSN High to activate I2C_GATE
masahikofukasawa 12:522a22a23f8a 235 DigitalOut _cs = DigitalOut(SPI_CS);
masahikofukasawa 12:522a22a23f8a 236 _cs.write(1);
masahikofukasawa 18:d75838bceaaf 237
masahikofukasawa 18:d75838bceaaf 238 // I2C communication ports to HIGH(just in case).
masahikofukasawa 17:a22b52136eb8 239 DigitalOut _scl = DigitalOut(I2C_SCL);
masahikofukasawa 17:a22b52136eb8 240 _scl.write(1);
masahikofukasawa 17:a22b52136eb8 241 DigitalOut _sda = DigitalOut(I2C_SDA);
masahikofukasawa 17:a22b52136eb8 242 _sda.write(1);
masahikofukasawa 17:a22b52136eb8 243 MSG("#SCL,SDA port high.\r\n");
masahikofukasawa 17:a22b52136eb8 244 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 17:a22b52136eb8 245
masahikofukasawa 12:522a22a23f8a 246 const TCA9554A::Port PORT_OE_LVS1 = TCA9554A::PORT_7;
masahikofukasawa 12:522a22a23f8a 247 const TCA9554A::Port PORT_OE_LVS2 = TCA9554A::PORT_6;
masahikofukasawa 12:522a22a23f8a 248 const TCA9554A::Port PORT_SPIN = TCA9554A::PORT_5;
masahikofukasawa 12:522a22a23f8a 249 const TCA9554A::Port PORT_RSV_RSTN = TCA9554A::PORT_0;
masahikofukasawa 12:522a22a23f8a 250
masahikofukasawa 12:522a22a23f8a 251 I2C i2c(I2C_SDA, I2C_SCL);
masahikofukasawa 17:a22b52136eb8 252
masahikofukasawa 17:a22b52136eb8 253 // call I2C general reset only once
masahikofukasawa 17:a22b52136eb8 254 char cmd[] = {0x06}; // general reset code
masahikofukasawa 17:a22b52136eb8 255 i2c.write(0x00, cmd, 1);
masahikofukasawa 17:a22b52136eb8 256 MSG("#General Reset.\r\n");
masahikofukasawa 17:a22b52136eb8 257 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 17:a22b52136eb8 258
masahikofukasawa 12:522a22a23f8a 259 TCA9554A tca9554a(&i2c, TCA9554A::SLAVE_ADDRESS_38H);
masahikofukasawa 12:522a22a23f8a 260
masahikofukasawa 12:522a22a23f8a 261 // Initializes TCA9554A (I2C GPIO Expander)
masahikofukasawa 12:522a22a23f8a 262 tca9554a.configurePort(PORT_OE_LVS1, TCA9554A::DIR_OUTPUT);
masahikofukasawa 12:522a22a23f8a 263 tca9554a.configurePort(PORT_OE_LVS2, TCA9554A::DIR_OUTPUT);
masahikofukasawa 12:522a22a23f8a 264 tca9554a.configurePort(PORT_SPIN, TCA9554A::DIR_OUTPUT);
masahikofukasawa 12:522a22a23f8a 265 tca9554a.configurePort(PORT_RSV_RSTN, TCA9554A::DIR_OUTPUT);
masahikofukasawa 12:522a22a23f8a 266
masahikofukasawa 18:d75838bceaaf 267 // enable LVS1 and LVS2 level shifter
masahikofukasawa 12:522a22a23f8a 268 tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
masahikofukasawa 12:522a22a23f8a 269 tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
masahikofukasawa 12:522a22a23f8a 270 tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::HIGH);
masahikofukasawa 12:522a22a23f8a 271 tca9554a.setPortLevel(PORT_SPIN, TCA9554A::HIGH);
masahikofukasawa 14:76205d28fea2 272 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 12:522a22a23f8a 273
masahikofukasawa 18:d75838bceaaf 274 // reset LVS2
masahikofukasawa 18:d75838bceaaf 275 tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::LOW);
masahikofukasawa 18:d75838bceaaf 276 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 18:d75838bceaaf 277 tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
masahikofukasawa 12:522a22a23f8a 278 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 16:5343a852fea4 279
masahikofukasawa 18:d75838bceaaf 280 // reset LVS1
masahikofukasawa 18:d75838bceaaf 281 tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
masahikofukasawa 18:d75838bceaaf 282 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 16:5343a852fea4 283 tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
masahikofukasawa 16:5343a852fea4 284 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 16:5343a852fea4 285
masahikofukasawa 18:d75838bceaaf 286 // disable LVS1 level shifter to read ID
masahikofukasawa 16:5343a852fea4 287 tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
masahikofukasawa 16:5343a852fea4 288 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 12:522a22a23f8a 289
masahikofukasawa 12:522a22a23f8a 290 // read ID and subId from ADC
masahikofukasawa 12:522a22a23f8a 291 id = getId(ANALOG_SENSOR_ID,4);
masahikofukasawa 12:522a22a23f8a 292 uint8_t subid_bitlen = 4;
masahikofukasawa 12:522a22a23f8a 293 if(id == AkmSensor::AKM_PRIMARY_ID_AKD_SPI || id == AkmSensor::AKM_PRIMARY_ID_AKD_I2C){
masahikofukasawa 14:76205d28fea2 294 // MSG("#5 bit sub ID.\r\n");
masahikofukasawa 12:522a22a23f8a 295 subid_bitlen = 5;
masahikofukasawa 12:522a22a23f8a 296 }
masahikofukasawa 12:522a22a23f8a 297 subId = getId(ANALOG_SENSOR_ID_SUB,subid_bitlen);
masahikofukasawa 12:522a22a23f8a 298
masahikofukasawa 14:76205d28fea2 299 if( (id == 11 && subId == 11) || (id == 55 && subId == 55) ){
masahikofukasawa 14:76205d28fea2 300 return true;
masahikofukasawa 14:76205d28fea2 301 }
masahikofukasawa 14:76205d28fea2 302
masahikofukasawa 12:522a22a23f8a 303 // enable 1.8V level shifter
masahikofukasawa 12:522a22a23f8a 304 tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
masahikofukasawa 17:a22b52136eb8 305 MSG("#LVS1 High.\r\n");
masahikofukasawa 12:522a22a23f8a 306 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 12:522a22a23f8a 307
masahikofukasawa 12:522a22a23f8a 308 // RSTN control
masahikofukasawa 12:522a22a23f8a 309 if(id == AkmSensor::AKM_PRIMARY_ID_AKD_SPI || id == AkmSensor::AKM_PRIMARY_ID_AKD_I2C){
masahikofukasawa 12:522a22a23f8a 310 tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::LOW);
masahikofukasawa 12:522a22a23f8a 311 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 12:522a22a23f8a 312 tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::HIGH);
masahikofukasawa 14:76205d28fea2 313 // MSG("#Detect AKD, RSTN control.\r\n");
masahikofukasawa 12:522a22a23f8a 314 }
masahikofukasawa 12:522a22a23f8a 315
masahikofukasawa 12:522a22a23f8a 316 // SPI disable/enable
masahikofukasawa 12:522a22a23f8a 317 if( id == AkmSensor::AKM_PRIMARY_ID_AKD_SPI || id == AkmSensor::AKM_PRIMARY_ID_ANGLE_SENSOR ){
masahikofukasawa 12:522a22a23f8a 318 tca9554a.setPortLevel(PORT_SPIN, TCA9554A::LOW);
masahikofukasawa 12:522a22a23f8a 319 // Disable 5.0V level shifter in order to ADC doesn't respond.
masahikofukasawa 12:522a22a23f8a 320 tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::LOW);
masahikofukasawa 14:76205d28fea2 321 // MSG("#Detect SPI, set SPIN low.\r\n");
masahikofukasawa 12:522a22a23f8a 322 }
masahikofukasawa 12:522a22a23f8a 323 else{
masahikofukasawa 12:522a22a23f8a 324 tca9554a.setPortLevel(PORT_SPIN, TCA9554A::HIGH);
masahikofukasawa 12:522a22a23f8a 325 tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
masahikofukasawa 12:522a22a23f8a 326 }
masahikofukasawa 12:522a22a23f8a 327
masahikofukasawa 12:522a22a23f8a 328 wait_ms(TIME_FOR_OE_MS);
masahikofukasawa 12:522a22a23f8a 329
masahikofukasawa 12:522a22a23f8a 330 releaseTWI();
masahikofukasawa 14:76205d28fea2 331
masahikofukasawa 14:76205d28fea2 332 return false;
masahikofukasawa 12:522a22a23f8a 333 }
masahikofukasawa 12:522a22a23f8a 334
masahikofukasawa 19:7a6913400380 335 char* my_strcat(char* str1, char* str2)
masahikofukasawa 19:7a6913400380 336 {
masahikofukasawa 19:7a6913400380 337 int num1;
masahikofukasawa 19:7a6913400380 338 char* str;
masahikofukasawa 19:7a6913400380 339
masahikofukasawa 19:7a6913400380 340 num1=strlen(str1) + strlen(str2);
masahikofukasawa 19:7a6913400380 341 str = (char *)malloc(num1 + 1);
masahikofukasawa 19:7a6913400380 342 sprintf(str,"%s%s",str1,str2);
masahikofukasawa 19:7a6913400380 343 return str;
masahikofukasawa 19:7a6913400380 344 }
masahikofukasawa 31:9f9246822fea 345
masahikofukasawa 30:ea67020c9e05 346 void error(const char* format, ...) {
masahikofukasawa 30:ea67020c9e05 347 }
masahikofukasawa 31:9f9246822fea 348
masahikofukasawa 12:522a22a23f8a 349 int main(void)
masahikofukasawa 14:76205d28fea2 350 {
masahikofukasawa 12:522a22a23f8a 351 // USB serial
masahikofukasawa 12:522a22a23f8a 352 serial.baud(115200);
masahikofukasawa 12:522a22a23f8a 353
masahikofukasawa 12:522a22a23f8a 354 // serial port RX event
masahikofukasawa 12:522a22a23f8a 355 serial.attach(&usbUartCallback);
masahikofukasawa 12:522a22a23f8a 356
masahikofukasawa 12:522a22a23f8a 357 #ifdef DEBUG
masahikofukasawa 12:522a22a23f8a 358 Debug::setSerial(&serial);
masahikofukasawa 14:76205d28fea2 359 MSG("#Debug Mode.\r\n");
masahikofukasawa 12:522a22a23f8a 360 #endif
masahikofukasawa 12:522a22a23f8a 361
masahikofukasawa 12:522a22a23f8a 362 // initialize AKDP board
masahikofukasawa 14:76205d28fea2 363 if( initAkdpBoard() ){
masahikofukasawa 14:76205d28fea2 364 MSG("#Error: AKDP boot failed.\r\n");
masahikofukasawa 14:76205d28fea2 365 }
masahikofukasawa 19:7a6913400380 366
masahikofukasawa 19:7a6913400380 367 // create sensor manager
masahikofukasawa 19:7a6913400380 368 manager = new AkmSensorManager(&serial);
masahikofukasawa 19:7a6913400380 369
masahikofukasawa 19:7a6913400380 370 if( manager->init(id, subId) == AkmSensorManager::ERROR){
masahikofukasawa 19:7a6913400380 371 MSG("#Error: sensor is NULL\r\n");
masahikofukasawa 19:7a6913400380 372 }
masahikofukasawa 19:7a6913400380 373
masahikofukasawa 25:12ed05de91f9 374 // create device name
masahikofukasawa 25:12ed05de91f9 375 // "AKDP D7.0XX SENSOR_NAME"
masahikofukasawa 25:12ed05de91f9 376 char fw_version[5];
masahikofukasawa 25:12ed05de91f9 377 sprintf(fw_version, "%03d", FIRMWARE_VERSION);
masahikofukasawa 25:12ed05de91f9 378 char* name = my_strcat(MOTHER_BOARD_NAME, " ");
masahikofukasawa 25:12ed05de91f9 379 name = my_strcat(name, MOTHER_BOARD_VERSION);
masahikofukasawa 25:12ed05de91f9 380 name = my_strcat(name, ".");
masahikofukasawa 25:12ed05de91f9 381 name = my_strcat(name, fw_version);
masahikofukasawa 25:12ed05de91f9 382 name = my_strcat(name, " ");
masahikofukasawa 25:12ed05de91f9 383 name = my_strcat(name, manager->getSensorName());
masahikofukasawa 19:7a6913400380 384
masahikofukasawa 19:7a6913400380 385 // BLE initialize
masahikofukasawa 19:7a6913400380 386 bleSetup(name);
masahikofukasawa 32:7c4beec92be9 387
masahikofukasawa 0:c240899240e7 388 // BLE UART service
masahikofukasawa 0:c240899240e7 389 uartService = new UARTService(ble);
masahikofukasawa 12:522a22a23f8a 390
masahikofukasawa 19:7a6913400380 391 // set BLE UART service
masahikofukasawa 19:7a6913400380 392 manager->setBleUartService(uartService);
masahikofukasawa 11:53e52f5f1051 393
masahikofukasawa 14:76205d28fea2 394 MSG("#Connecting...\r\n");
masahikofukasawa 19:7a6913400380 395
masahikofukasawa 0:c240899240e7 396 // main loop
masahikofukasawa 0:c240899240e7 397 while(1)
masahikofukasawa 0:c240899240e7 398 {
masahikofukasawa 0:c240899240e7 399 if(manager->isEvent()){
masahikofukasawa 0:c240899240e7 400 manager->processEvent();
masahikofukasawa 0:c240899240e7 401 }else{
masahikofukasawa 0:c240899240e7 402 ble.waitForEvent();
masahikofukasawa 0:c240899240e7 403 }
masahikofukasawa 0:c240899240e7 404 }
masahikofukasawa 0:c240899240e7 405 }