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

Revision:
18:d75838bceaaf
Parent:
17:a22b52136eb8
Child:
19:7a6913400380
--- a/main.cpp	Fri Aug 05 23:17:17 2016 +0000
+++ b/main.cpp	Wed Aug 10 23:51:01 2016 +0000
@@ -19,7 +19,7 @@
 #define CR                              '\r'
 #define LF                              '\n'
 
-#define DEVICE_NAME                     "AKDP Rev.D7.006"
+#define DEVICE_NAME                     "AKDP Rev.D7.007"
 
 BLE                 ble;
 UARTService*        uartService;
@@ -33,6 +33,18 @@
 uint8_t id;
 uint8_t subId;
 
+enum {
+    UNIT_0_625_MS = 625,
+    UNIT_1_25_MS  = 1250,
+    UNIT_10_MS    = 10000
+};
+
+#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
+#define MIN_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)              /**< Minimum connection interval (7.5 ms) */
+#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(30, UNIT_1_25_MS)               /**< Maximum connection interval (30 ms). */
+#define SLAVE_LATENCY                   0                                             /**< Slave latency. */
+#define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(3000, UNIT_10_MS)               /**< Connection supervisory timeout (3 seconds). */
+
 void WrittenHandler(const GattWriteCallbackParams *Handler)
 {   
     static char command[TXRX_LEN]="";
@@ -66,7 +78,7 @@
     }
 }
 
-void usbUartCallback(void)
+static void usbUartCallback(void)
 {   
     static char command[TXRX_LEN] = "";
     static uint16_t len=0;
@@ -90,13 +102,22 @@
     }
 }
 
-void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
+static void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
+  
+    Gap::Handle_t gap_handle = params->handle;
+    Gap::ConnectionParams_t gap_conn_params;
+    gap_conn_params.minConnectionInterval = MIN_CONN_INTERVAL;
+    gap_conn_params.maxConnectionInterval = MAX_CONN_INTERVAL;
+    gap_conn_params.slaveLatency = SLAVE_LATENCY;
+    gap_conn_params.connectionSupervisionTimeout = CONN_SUP_TIMEOUT;
+    ble.updateConnectionParams(gap_handle, &gap_conn_params);
+
     manager->setEventConnected();
     MSG("#Connected\r\n");
 }
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+static void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     manager->setEventDisconnected();
     MSG("#Disconnected\r\n");
@@ -105,6 +126,7 @@
 
 void bleSetup(){
     ble.init();
+
     // setup advertising 
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -112,12 +134,21 @@
                                     (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+    
+    // Set desired connection parameters
+    Gap::ConnectionParams_t gap_conn_params;
+    gap_conn_params.minConnectionInterval = MIN_CONN_INTERVAL;
+    gap_conn_params.maxConnectionInterval = MAX_CONN_INTERVAL;
+    gap_conn_params.slaveLatency = SLAVE_LATENCY;
+    gap_conn_params.connectionSupervisionTimeout = CONN_SUP_TIMEOUT;
+    ble.setPreferredConnectionParams(&gap_conn_params);
+    
+    ble.gap().onDisconnection(disconnectionCallback);
     ble.gap().onConnection(connectionCallback);
-    ble.gap().onDisconnection(disconnectionCallback);
     ble.gattServer().onDataWritten(WrittenHandler);  
     
     // 100ms; in multiples of 0.625ms. 
-    ble.gap().setAdvertisingInterval(100);
+    ble.gap().setAdvertisingInterval(160);
     ble.gap().startAdvertising(); 
 }
 
@@ -192,8 +223,8 @@
     // CSN High to activate I2C_GATE
     DigitalOut _cs = DigitalOut(SPI_CS);
     _cs.write(1);
-    
-    // I2C communication ports to HIGH for preventing mulfunction.
+
+    // I2C communication ports to HIGH(just in case).
     DigitalOut _scl = DigitalOut(I2C_SCL);
     _scl.write(1);
     DigitalOut _sda = DigitalOut(I2C_SDA);
@@ -222,24 +253,27 @@
     tca9554a.configurePort(PORT_SPIN, TCA9554A::DIR_OUTPUT);
     tca9554a.configurePort(PORT_RSV_RSTN, TCA9554A::DIR_OUTPUT);
     
-    //  enable 5V level shifter
+    //  enable LVS1 and LVS2 level shifter
     tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
     tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
     tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::HIGH);
     tca9554a.setPortLevel(PORT_SPIN, TCA9554A::HIGH);
     wait_ms(TIME_FOR_OE_MS);
 
-    //  disable 1.8V level shifter to read ID
-    tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
-    MSG("#LVS1 Low.\r\n");
+    //  reset LVS2
+    tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::LOW);
+    wait_ms(TIME_FOR_OE_MS);
+    tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
     wait_ms(TIME_FOR_OE_MS);
 
+    //  reset LVS1
+    tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
+    wait_ms(TIME_FOR_OE_MS);
     tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
-    MSG("#LVS1 High.\r\n");
     wait_ms(TIME_FOR_OE_MS);
 
+    //  disable LVS1 level shifter to read ID
     tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
-    MSG("#LVS1 Low.\r\n");
     wait_ms(TIME_FOR_OE_MS);
     
     //  read ID and subId from ADC