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 Jan 04 00:59:22 2017 +0000
Revision:
31:9f9246822fea
Parent:
30:ea67020c9e05
Child:
32:7c4beec92be9
Rev012_pre. Modified for using the latest BLE_API, nRF51822 and mbed libs.

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