nRF51822 serves as the bridge between BLE central and MCU, which makes cental able to fetch photos from serial camera.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_blinky by RedBearLab

Committer:
stormysun513
Date:
Sat May 21 09:13:27 2016 +0000
Revision:
20:f6e313950373
Parent:
19:03d39b923919
Child:
21:4753996b0bcb
Change low power threshold from 100 to 103

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stormysun513 12:7060e287bdae 1 #ifndef DEVICE_SERIAL_FC
stormysun513 12:7060e287bdae 2 #define DEVICE_SERIAL_FC 1
stormysun513 12:7060e287bdae 3 #endif
stormysun513 14:f62d55b7dead 4
stormysun513 13:65c9b7d667f6 5 #define APPLICATION_TIMEOUT
stormysun513 16:5552833a3401 6 #define LOW_POWER_CONSTRAINT
stormysun513 12:7060e287bdae 7
dan 0:7dec7e9ac085 8 #include "mbed.h"
stormysun513 12:7060e287bdae 9 #include "ble/BLE.h"
stormysun513 12:7060e287bdae 10 #include "ble/Gap.h"
stormysun513 12:7060e287bdae 11 #include "ringBuffer.h"
stormysun513 11:b3929de96933 12 #include "application.h"
stormysun513 11:b3929de96933 13
stormysun513 20:f6e313950373 14 #define DEVICE_NAME "ket_048"
stormysun513 20:f6e313950373 15 #define CODE_VERSION 7
stormysun513 14:f62d55b7dead 16
stormysun513 12:7060e287bdae 17 #define SDA_PIN P0_8
stormysun513 12:7060e287bdae 18 #define SCL_PIN P0_9
stormysun513 12:7060e287bdae 19 #define UARTTX_PIN P0_18
stormysun513 12:7060e287bdae 20 #define UARTRX_PIN P0_17
stormysun513 12:7060e287bdae 21 #define POWER_LED_PIN P0_11
stormysun513 12:7060e287bdae 22 #define INSERTION_LED_PIN P0_10
stormysun513 12:7060e287bdae 23 #define ANI_BATTERY_VOLTAGE_PIN P0_2
stormysun513 12:7060e287bdae 24 #define ANI_USB_CHARGE_PIN P0_3
stormysun513 12:7060e287bdae 25 #define BTN_POWER_ON_PIN P0_4
stormysun513 12:7060e287bdae 26 #define ANI_SALIVA_VOLTAGE_PIN P0_5
stormysun513 14:f62d55b7dead 27 #define STM32_ENABLE_PIN P0_12
stormysun513 12:7060e287bdae 28 #define CHECK_BUTTON_TIMES 2
stormysun513 12:7060e287bdae 29 #define CHECK_BUTTON_VOLTAGE_LEVEL 0.05f
stormysun513 12:7060e287bdae 30
stormysun513 14:f62d55b7dead 31 #ifdef LOW_POWER_CONSTRAINT
stormysun513 20:f6e313950373 32 #define LOW_POWER_THERS 103 // changed 5/21
stormysun513 14:f62d55b7dead 33 #define LOW_POWER_THERS_MARGIN 2
stormysun513 14:f62d55b7dead 34 #define MAXIMUM_LOW_POWER_COUNTER 30
stormysun513 14:f62d55b7dead 35 #endif
stormysun513 14:f62d55b7dead 36
stormysun513 11:b3929de96933 37 #define UART_BAUDRATE 14400
stormysun513 11:b3929de96933 38 #define MINIMUM_TICKER_PERIOD 2500
stormysun513 12:7060e287bdae 39
stormysun513 12:7060e287bdae 40 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
stormysun513 12:7060e287bdae 41 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
stormysun513 12:7060e287bdae 42 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
stormysun513 12:7060e287bdae 43
stormysun513 12:7060e287bdae 44 #define BLE_GAP_CP_MIN_CONN_INTVL_MIN 0x0006 //Lowest mimimum connection interval permitted, in units of 1.25 ms, i.e. 7.5 ms.
stormysun513 12:7060e287bdae 45 #define BLE_GAP_CP_MAX_CONN_INTVL_MAX 0x0C80 //Highest maximum connection interval permitted, in units of 1.25 ms, i.e. 4 s.
stormysun513 12:7060e287bdae 46 #define BLE_GAP_CP_SLAVE_LATENCY_MAX 0x01F3 //Highest slave latency permitted, in connection events.
stormysun513 12:7060e287bdae 47 #define BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN 0x000A //Lowest supervision timeout permitted, in units of 10 ms, i.e. 100 ms.
stormysun513 12:7060e287bdae 48 #define BLE_GAP_CP_CONN_SUP_TIMEOUT_MAX 0x0C80 //Highest supervision timeout permitted, in units of 10 ms, i.e. 32 s.
stormysun513 12:7060e287bdae 49
stormysun513 11:b3929de96933 50 #define BLE_BUF_LEN 20
stormysun513 12:7060e287bdae 51 #define BLE_RETRANSMISSION_BUF 18
stormysun513 16:5552833a3401 52 #define MONITORING_PAYLOAD 5
stormysun513 12:7060e287bdae 53 #define SINGLE_UINT16_PAYLOAD 3
stormysun513 12:7060e287bdae 54 #define UART_DATA_PACKET_PAYLOAD 1
stormysun513 12:7060e287bdae 55 #define UART_PACKET_SIZE 111
stormysun513 12:7060e287bdae 56
stormysun513 12:7060e287bdae 57 typedef enum {
stormysun513 12:7060e287bdae 58 SYS_POWERSAVING = 0,
stormysun513 12:7060e287bdae 59 SYS_RELAXING_BUTTON,
stormysun513 12:7060e287bdae 60 SYS_LOCKING_BUTTON
stormysun513 12:7060e287bdae 61 }SystemStateTypeDef;
stormysun513 12:7060e287bdae 62
stormysun513 12:7060e287bdae 63 typedef enum {
stormysun513 12:7060e287bdae 64 BTN_IDLE = 0,
stormysun513 12:7060e287bdae 65 BTN_CHANGE_ANALOG,
stormysun513 12:7060e287bdae 66 BTN_CHECK_LONG_PRESS
stormysun513 12:7060e287bdae 67 }ButtonStateTypeDef;
stormysun513 12:7060e287bdae 68
stormysun513 12:7060e287bdae 69 typedef enum {
stormysun513 12:7060e287bdae 70 STM32_IDLE = 0, //Cannot receive any byte from UARTRX
stormysun513 12:7060e287bdae 71 STM32_CHECK_HEADER, //Check incoming header and forward to the client
stormysun513 12:7060e287bdae 72 STM32_IMAGE_FORWARDING //Forward image data to the client
stormysun513 12:7060e287bdae 73 }STM32StateTypeDef;
stormysun513 12:7060e287bdae 74
stormysun513 12:7060e287bdae 75 typedef enum {
stormysun513 12:7060e287bdae 76 SYS_HEADER_IMAGE_INFO = 0,
stormysun513 12:7060e287bdae 77 SYS_HEADER_SALIVA_VOLTAGE, // Not used (for debug purpose)
stormysun513 16:5552833a3401 78 SYS_HEADER_MONITORING, // Not used (for debug purpose)
stormysun513 12:7060e287bdae 79 SYS_HEADER_IMAGE_PACKET,
stormysun513 16:5552833a3401 80 SYS_HEADER_DEVICE_INFO,
stormysun513 12:7060e287bdae 81 SYS_HEADER_NO_SPECIFIC
stormysun513 12:7060e287bdae 82 }HeaderTypeDef;
stormysun513 12:7060e287bdae 83
stormysun513 12:7060e287bdae 84 typedef enum {
stormysun513 12:7060e287bdae 85 UARTRX_CHECK_FIRST_PREAMBLE = 0,
stormysun513 12:7060e287bdae 86 UARTRX_CHECK_SECOND_PREAMBLE,
stormysun513 12:7060e287bdae 87 UARTRX_TRANSFERING_DATA
stormysun513 12:7060e287bdae 88 }UARTRxStateTypeDef;
stormysun513 12:7060e287bdae 89
stormysun513 12:7060e287bdae 90 typedef enum {
stormysun513 16:5552833a3401 91 BLE_SWITCH_MONITORING = 0, // Enable/Disable device monitoring
stormysun513 16:5552833a3401 92 BLE_CHANGE_CASSETTE_ID, // Write ID to saliva cassette
stormysun513 12:7060e287bdae 93 BLE_REQUEST_SALIVA_VOLTAGE, // Turn on saliva sampling periodically
stormysun513 12:7060e287bdae 94 BLE_REQUEST_IMAGE_INFO, // Request image header
stormysun513 12:7060e287bdae 95 BLE_REQUEST_IMAGE_BY_INDEX, // Request image data by uart packet IDs
stormysun513 12:7060e287bdae 96 BLE_END_IMAGE_TRANSFER, // Directly terminate image transferring
stormysun513 12:7060e287bdae 97 BLE_DEREQUEST_SALIVA_VOLTAGE, // Turn off saliva sampling periodically
stormysun513 12:7060e287bdae 98 BLE_SHUTDOWN, // Shutdown device
stormysun513 12:7060e287bdae 99 BLE_UNLOCK_BUTTON, // Unlock Button
stormysun513 14:f62d55b7dead 100 BLE_UART_DEBUG, // Reserved for debug Value: 0x09
stormysun513 16:5552833a3401 101 BLE_REQUEST_DEVICE_INFO // Request code version
stormysun513 12:7060e287bdae 102 }BLECommandTypeDef;
stormysun513 12:7060e287bdae 103
stormysun513 12:7060e287bdae 104 /* BLE Callback functions */
stormysun513 12:7060e287bdae 105 void bleInitCompleteCB(struct BLE::InitializationCompleteCallbackContext *context);
stormysun513 12:7060e287bdae 106 void bleOnConnectionCB(const Gap::ConnectionCallbackParams_t *p_conn_param);
stormysun513 12:7060e287bdae 107 void bleDisconnectionCB(const Gap::DisconnectionCallbackParams_t *params);
stormysun513 12:7060e287bdae 108 void bleCharacteristicWrittenCB(const GattWriteCallbackParams *Handler);
stormysun513 12:7060e287bdae 109 void bleReduceConnInterval(void);
dan 0:7dec7e9ac085 110
stormysun513 11:b3929de96933 111 void timerPeriodicalCB(void);
stormysun513 14:f62d55b7dead 112 void timerFlashPowerLed(void);
stormysun513 14:f62d55b7dead 113 void timerFlashInsertionLed(void);
stormysun513 12:7060e287bdae 114 void timerCheckBuffer(void);
stormysun513 12:7060e287bdae 115 void timerRoutineTasks(void);
stormysun513 12:7060e287bdae 116
stormysun513 12:7060e287bdae 117 void uartTxTransmit(uint8_t* buf, uint16_t length);
stormysun513 12:7060e287bdae 118 void uartRxCB(void);
stormysun513 11:b3929de96933 119
stormysun513 12:7060e287bdae 120 void buttonInterruptServiceRoutine(void);
stormysun513 12:7060e287bdae 121 void buttonCheckLongPress(uint8_t counter);
stormysun513 12:7060e287bdae 122 void buttonLongPressVerified(void);
stormysun513 12:7060e287bdae 123 void changeButtonPinToAnalogIn(void);
stormysun513 12:7060e287bdae 124 void changeButtonPinToInterrupt(void);
stormysun513 12:7060e287bdae 125 void shutdownDevice(void);
stormysun513 12:7060e287bdae 126
stormysun513 12:7060e287bdae 127 void resetSTM32StateType(void);
stormysun513 12:7060e287bdae 128 void requestSalivaVoltage(void);
stormysun513 12:7060e287bdae 129 void requestForImagePacket(uint16_t packetId);
stormysun513 12:7060e287bdae 130
stormysun513 12:7060e287bdae 131 BLE ble;
stormysun513 12:7060e287bdae 132 Gap::Handle_t mHandle = 0;
stormysun513 11:b3929de96933 133 Ticker ticker;
stormysun513 12:7060e287bdae 134 Serial pc(UARTTX_PIN, UARTRX_PIN);
stormysun513 12:7060e287bdae 135 I2C *pI2C = NULL;
stormysun513 12:7060e287bdae 136 DigitalOut ledPower(POWER_LED_PIN); //P0_11
stormysun513 12:7060e287bdae 137 DigitalOut *pLedInsertion = NULL; //P0_10 Must be changed
stormysun513 14:f62d55b7dead 138 DigitalOut uartEnable(STM32_ENABLE_PIN); //P0_12
stormysun513 12:7060e287bdae 139 InterruptIn *pButton = NULL; //P0_4
stormysun513 12:7060e287bdae 140 AnalogIn *pButtonAnalogIn = NULL; //P0_4
stormysun513 12:7060e287bdae 141 AnalogIn aniSalivaVolt(ANI_SALIVA_VOLTAGE_PIN); //P0_5
stormysun513 12:7060e287bdae 142 AnalogIn aniUSBChargeVolt(ANI_USB_CHARGE_PIN);
stormysun513 12:7060e287bdae 143 AnalogIn aniBatterVolt(ANI_BATTERY_VOLTAGE_PIN); //P0_2
stormysun513 12:7060e287bdae 144
stormysun513 16:5552833a3401 145 static const uint16_t codeVersion = CODE_VERSION;
stormysun513 15:c099dd509203 146
stormysun513 12:7060e287bdae 147 /* State variables for application status, UARTRX data parsing status,
stormysun513 12:7060e287bdae 148 waiting header type and main loop trigger */
stormysun513 12:7060e287bdae 149 static SystemStateTypeDef systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 150 static ButtonStateTypeDef buttonState = BTN_IDLE;
stormysun513 12:7060e287bdae 151 static STM32StateTypeDef stm32State = STM32_IDLE;
stormysun513 12:7060e287bdae 152 static UARTRxStateTypeDef uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 16:5552833a3401 153 static HeaderTypeDef waitHeader = SYS_HEADER_NO_SPECIFIC;
stormysun513 12:7060e287bdae 154
stormysun513 12:7060e287bdae 155 /* Variables to handle data transferring. */
stormysun513 12:7060e287bdae 156 static uint8_t packetNum = 0;
stormysun513 12:7060e287bdae 157 static uint8_t lastPacketSize = 0;
stormysun513 12:7060e287bdae 158 static uint8_t currentPacketIndex = 0;
stormysun513 12:7060e287bdae 159 static uint8_t targetPayload = 0;
stormysun513 12:7060e287bdae 160 static uint8_t alreadyTransferred = 0;
stormysun513 12:7060e287bdae 161 static uint8_t waitCamera = 0;
stormysun513 12:7060e287bdae 162 static const uint8_t MAX_WAIT_TARGET_PAYLOAD = 2;
stormysun513 12:7060e287bdae 163 static bool isRetransmit = false;
stormysun513 12:7060e287bdae 164 static uint8_t retransmitIndex = 0;
stormysun513 12:7060e287bdae 165 static uint8_t retransmitSize = 0;
stormysun513 12:7060e287bdae 166 static bool setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 167 static bool directForward = false;
stormysun513 11:b3929de96933 168
stormysun513 12:7060e287bdae 169 static bool isCheckBuf = false;
stormysun513 12:7060e287bdae 170 static bool isRoutine = false;
stormysun513 15:c099dd509203 171 static bool isInserted = false;
stormysun513 12:7060e287bdae 172 static bool isEnterMainLoop = false;
stormysun513 16:5552833a3401 173 static bool isMonitoring = false;
stormysun513 12:7060e287bdae 174 static bool salivaPeriodically = false;
stormysun513 12:7060e287bdae 175
stormysun513 12:7060e287bdae 176 static uint8_t checkButtonCounter = 0;
stormysun513 12:7060e287bdae 177
stormysun513 14:f62d55b7dead 178 #ifdef LOW_POWER_CONSTRAINT
stormysun513 14:f62d55b7dead 179 static bool isLowPower = false;
stormysun513 14:f62d55b7dead 180 static uint8_t lowPowerCounter = 0;
stormysun513 14:f62d55b7dead 181 #endif
stormysun513 14:f62d55b7dead 182
stormysun513 12:7060e287bdae 183 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 184 static uint16_t disConnIdleCnt = 0;
stormysun513 12:7060e287bdae 185 static uint16_t connIdleCnt = 0;
stormysun513 14:f62d55b7dead 186 static const uint16_t MAX_DISCONN_IDLE = 300;
stormysun513 14:f62d55b7dead 187 static const uint16_t MAX_CONN_IDLE = 300;
stormysun513 12:7060e287bdae 188 #endif
stormysun513 11:b3929de96933 189
stormysun513 12:7060e287bdae 190 static RingBuffer ringBuffer;
stormysun513 12:7060e287bdae 191 static const uint8_t preamble[] = {0xFF, 0x7F};
stormysun513 12:7060e287bdae 192 uint8_t uartTxPayload[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 193 uint8_t uartRxPayload[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 194 uint8_t retransmitIndices[BLE_RETRANSMISSION_BUF] = {0,};
stormysun513 12:7060e287bdae 195
stormysun513 12:7060e287bdae 196 // The Nordic UART Service
stormysun513 12:7060e287bdae 197 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stormysun513 12:7060e287bdae 198 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stormysun513 12:7060e287bdae 199 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stormysun513 12:7060e287bdae 200 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
stormysun513 12:7060e287bdae 201
stormysun513 12:7060e287bdae 202 /* GATTs */
stormysun513 12:7060e287bdae 203 GattCharacteristic txCharacteristic (uart_tx_uuid, uartTxPayload, 1, BLE_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
stormysun513 12:7060e287bdae 204 GattCharacteristic rxCharacteristic (uart_rx_uuid, uartRxPayload, 1, BLE_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
stormysun513 12:7060e287bdae 205 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
stormysun513 12:7060e287bdae 206 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
dan 0:7dec7e9ac085 207
RedBearLab 10:9e24f9f4ac47 208 int main()
RedBearLab 10:9e24f9f4ac47 209 {
stormysun513 12:7060e287bdae 210 pc.baud(UART_BAUDRATE);
stormysun513 12:7060e287bdae 211 pc.attach(uartRxCB , pc.RxIrq);
stormysun513 12:7060e287bdae 212 pc.set_flow_control(pc.Disabled);
stormysun513 12:7060e287bdae 213 RingBuffer_init(&ringBuffer);
stormysun513 14:f62d55b7dead 214 uartEnable = 1;
stormysun513 12:7060e287bdae 215
stormysun513 12:7060e287bdae 216 pI2C = new I2C(SDA_PIN, SCL_PIN);
stormysun513 12:7060e287bdae 217 pLedInsertion = new DigitalOut(INSERTION_LED_PIN);
stormysun513 14:f62d55b7dead 218 pLedInsertion->write(1);
stormysun513 14:f62d55b7dead 219 ledPower = 0;
stormysun513 12:7060e287bdae 220
stormysun513 11:b3929de96933 221 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 12:7060e287bdae 222 pButton = new InterruptIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 223 pButton->fall(buttonInterruptServiceRoutine);
stormysun513 12:7060e287bdae 224
stormysun513 12:7060e287bdae 225 ble.init(bleInitCompleteCB);
stormysun513 12:7060e287bdae 226 ble.onConnection(bleOnConnectionCB);
stormysun513 12:7060e287bdae 227 ble.onDisconnection(bleDisconnectionCB);
stormysun513 12:7060e287bdae 228 ble.onDataWritten(bleCharacteristicWrittenCB);
stormysun513 14:f62d55b7dead 229
stormysun513 14:f62d55b7dead 230 // Adjust interrupt priority
stormysun513 12:7060e287bdae 231 // NVIC_SetPriority(RADIO_IRQn, 0);
stormysun513 12:7060e287bdae 232 // NVIC_SetPriority(UART0_IRQn, 1);
stormysun513 12:7060e287bdae 233 // NVIC_SetPriority(RTC1_IRQn, 2);
stormysun513 12:7060e287bdae 234
stormysun513 12:7060e287bdae 235 // setup advertising // Restricted to 31 bytes
stormysun513 12:7060e287bdae 236 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
stormysun513 12:7060e287bdae 237 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
stormysun513 12:7060e287bdae 238 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"BLETest", sizeof("BLETest") - 1);
stormysun513 12:7060e287bdae 239 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
stormysun513 12:7060e287bdae 240 ble.gap().setDeviceName("BLETest");
stormysun513 12:7060e287bdae 241
stormysun513 14:f62d55b7dead 242 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
stormysun513 12:7060e287bdae 243 ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms.
stormysun513 12:7060e287bdae 244 ble.addService(uartService);
stormysun513 12:7060e287bdae 245
stormysun513 12:7060e287bdae 246 ble.startAdvertising();
stormysun513 11:b3929de96933 247
stormysun513 11:b3929de96933 248 while(true){
stormysun513 12:7060e287bdae 249
stormysun513 14:f62d55b7dead 250 #ifdef LOW_POWER_CONSTRAINT
stormysun513 14:f62d55b7dead 251 if(lowPowerCounter > MAXIMUM_LOW_POWER_COUNTER){
stormysun513 14:f62d55b7dead 252 shutdownDevice();
stormysun513 14:f62d55b7dead 253 }
stormysun513 14:f62d55b7dead 254 #endif
stormysun513 14:f62d55b7dead 255
stormysun513 12:7060e287bdae 256 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 257 if(disConnIdleCnt >= MAX_DISCONN_IDLE && systemState == SYS_RELAXING_BUTTON){
stormysun513 12:7060e287bdae 258 shutdownDevice();
stormysun513 12:7060e287bdae 259 }
stormysun513 12:7060e287bdae 260 else if(connIdleCnt >= MAX_CONN_IDLE){
stormysun513 13:65c9b7d667f6 261 if(mHandle != 0)
stormysun513 13:65c9b7d667f6 262 ble.disconnect(mHandle, Gap::CONNECTION_TIMEOUT);
stormysun513 12:7060e287bdae 263 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 264 }
stormysun513 12:7060e287bdae 265 #endif
stormysun513 12:7060e287bdae 266
stormysun513 12:7060e287bdae 267 switch (systemState) {
stormysun513 12:7060e287bdae 268 case SYS_POWERSAVING:
stormysun513 12:7060e287bdae 269 case SYS_RELAXING_BUTTON:
stormysun513 12:7060e287bdae 270 switch (buttonState) {
stormysun513 12:7060e287bdae 271 case BTN_CHANGE_ANALOG:
stormysun513 12:7060e287bdae 272 changeButtonPinToAnalogIn();
stormysun513 12:7060e287bdae 273 break;
stormysun513 12:7060e287bdae 274 case BTN_CHECK_LONG_PRESS:
stormysun513 12:7060e287bdae 275 buttonCheckLongPress(checkButtonCounter);
stormysun513 12:7060e287bdae 276 break;
stormysun513 12:7060e287bdae 277 case BTN_IDLE:
stormysun513 12:7060e287bdae 278 default:
stormysun513 12:7060e287bdae 279 break;
stormysun513 12:7060e287bdae 280 }
stormysun513 12:7060e287bdae 281 break;
stormysun513 12:7060e287bdae 282 case SYS_LOCKING_BUTTON:
stormysun513 12:7060e287bdae 283 buttonState = BTN_IDLE;
stormysun513 12:7060e287bdae 284 break;
stormysun513 12:7060e287bdae 285 }
stormysun513 12:7060e287bdae 286
stormysun513 12:7060e287bdae 287 if(isCheckBuf){
stormysun513 12:7060e287bdae 288 isCheckBuf = false;
stormysun513 12:7060e287bdae 289 timerCheckBuffer();
stormysun513 12:7060e287bdae 290 }
stormysun513 12:7060e287bdae 291
stormysun513 12:7060e287bdae 292 if(isRoutine){
stormysun513 12:7060e287bdae 293 isRoutine = false;
stormysun513 12:7060e287bdae 294 timerRoutineTasks();
stormysun513 12:7060e287bdae 295 }
stormysun513 12:7060e287bdae 296
stormysun513 12:7060e287bdae 297 if( isEnterMainLoop ){
stormysun513 12:7060e287bdae 298 switch (stm32State){
stormysun513 16:5552833a3401 299 case STM32_IMAGE_FORWARDING:
stormysun513 16:5552833a3401 300 {
stormysun513 12:7060e287bdae 301 if (setImgPktPayloadByID == false){
stormysun513 12:7060e287bdae 302 uint8_t check = 0;
stormysun513 12:7060e287bdae 303 RingBuffer_read(&ringBuffer, &check, 1);
stormysun513 12:7060e287bdae 304 HeaderTypeDef header = static_cast<HeaderTypeDef>(check);
stormysun513 12:7060e287bdae 305 if (header == SYS_HEADER_IMAGE_PACKET){
stormysun513 12:7060e287bdae 306 if (currentPacketIndex != (packetNum-1))
stormysun513 12:7060e287bdae 307 targetPayload = UART_PACKET_SIZE + 3;
stormysun513 12:7060e287bdae 308 else
stormysun513 12:7060e287bdae 309 targetPayload = lastPacketSize + 3;
stormysun513 12:7060e287bdae 310
stormysun513 12:7060e287bdae 311 alreadyTransferred = 0;
stormysun513 12:7060e287bdae 312 setImgPktPayloadByID = true;
stormysun513 12:7060e287bdae 313 waitCamera = 0;
stormysun513 12:7060e287bdae 314 }
stormysun513 12:7060e287bdae 315 }
stormysun513 12:7060e287bdae 316 else {
stormysun513 12:7060e287bdae 317 uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer);
stormysun513 12:7060e287bdae 318 while((numBytes >= (BLE_BUF_LEN-1)) || (directForward == true)){
stormysun513 12:7060e287bdae 319 waitCamera = 0;
stormysun513 12:7060e287bdae 320 uint16_t diff = numBytes;
stormysun513 12:7060e287bdae 321 if(diff > (BLE_BUF_LEN-1))
stormysun513 12:7060e287bdae 322 diff = (BLE_BUF_LEN-1);
stormysun513 12:7060e287bdae 323 uartRxPayload[0] = static_cast<uint8_t>(SYS_HEADER_IMAGE_PACKET);
stormysun513 12:7060e287bdae 324
stormysun513 12:7060e287bdae 325 RingBuffer_read(&ringBuffer, uartRxPayload+1, diff);
stormysun513 12:7060e287bdae 326
stormysun513 12:7060e287bdae 327 if(mHandle != 0){
stormysun513 12:7060e287bdae 328 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), uartRxPayload, diff+1);
stormysun513 12:7060e287bdae 329 alreadyTransferred += diff;
stormysun513 12:7060e287bdae 330 numBytes -= diff;
stormysun513 12:7060e287bdae 331 }
stormysun513 12:7060e287bdae 332
stormysun513 12:7060e287bdae 333 if(directForward == true || alreadyTransferred >= targetPayload){
stormysun513 12:7060e287bdae 334 __disable_irq(); // Disable Interrupts
stormysun513 12:7060e287bdae 335 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 336 __enable_irq();
stormysun513 12:7060e287bdae 337 directForward = false;
stormysun513 12:7060e287bdae 338 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 339
stormysun513 12:7060e287bdae 340 if(isRetransmit == false){
stormysun513 12:7060e287bdae 341 currentPacketIndex++;
stormysun513 12:7060e287bdae 342 }
stormysun513 12:7060e287bdae 343 else{
stormysun513 12:7060e287bdae 344 retransmitIndex++;
stormysun513 12:7060e287bdae 345 if(retransmitIndex >= retransmitSize){
stormysun513 12:7060e287bdae 346 retransmitSize = 0;
stormysun513 12:7060e287bdae 347 resetSTM32StateType();
stormysun513 12:7060e287bdae 348 break;
stormysun513 12:7060e287bdae 349 }
stormysun513 12:7060e287bdae 350 else{
stormysun513 12:7060e287bdae 351 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 352 }
stormysun513 12:7060e287bdae 353 }
stormysun513 12:7060e287bdae 354
stormysun513 12:7060e287bdae 355 if(currentPacketIndex == packetNum && isRetransmit == false){
stormysun513 12:7060e287bdae 356 /* TODO retransmission */
stormysun513 12:7060e287bdae 357 isRetransmit = true;
stormysun513 12:7060e287bdae 358 if(retransmitSize > 0){
stormysun513 12:7060e287bdae 359 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 360 retransmitIndex = 0;
stormysun513 12:7060e287bdae 361 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 362 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 363 }
stormysun513 12:7060e287bdae 364 else{
stormysun513 12:7060e287bdae 365 resetSTM32StateType();
stormysun513 12:7060e287bdae 366 }
stormysun513 12:7060e287bdae 367 }
stormysun513 12:7060e287bdae 368 else{
stormysun513 12:7060e287bdae 369 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 370 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 371 }
stormysun513 12:7060e287bdae 372 break;
stormysun513 12:7060e287bdae 373 }
stormysun513 12:7060e287bdae 374 }
stormysun513 12:7060e287bdae 375 }
stormysun513 12:7060e287bdae 376 break;
stormysun513 12:7060e287bdae 377 }
stormysun513 16:5552833a3401 378 case STM32_CHECK_HEADER:
stormysun513 16:5552833a3401 379 {
stormysun513 12:7060e287bdae 380 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 381 HeaderTypeDef header;
stormysun513 12:7060e287bdae 382 switch (waitHeader) {
stormysun513 12:7060e287bdae 383 /* Image info */
stormysun513 12:7060e287bdae 384 case SYS_HEADER_IMAGE_INFO:
stormysun513 16:5552833a3401 385 {
stormysun513 12:7060e287bdae 386 RingBuffer_read(&ringBuffer, buf, 1);
stormysun513 12:7060e287bdae 387 header = static_cast<HeaderTypeDef>(buf[0]);
stormysun513 12:7060e287bdae 388 if (header == SYS_HEADER_IMAGE_INFO){
stormysun513 12:7060e287bdae 389 RingBuffer_read(&ringBuffer, buf+1, SINGLE_UINT16_PAYLOAD-1);
stormysun513 12:7060e287bdae 390 if(mHandle != 0)
stormysun513 12:7060e287bdae 391 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 12:7060e287bdae 392
stormysun513 12:7060e287bdae 393 uint16_t totalDataLength = buf[1]*256 + buf[2];
stormysun513 12:7060e287bdae 394 packetNum = totalDataLength/UART_PACKET_SIZE;
stormysun513 12:7060e287bdae 395 lastPacketSize = totalDataLength%UART_PACKET_SIZE;
stormysun513 12:7060e287bdae 396 if(totalDataLength%UART_PACKET_SIZE != 0)
stormysun513 12:7060e287bdae 397 packetNum++;
stormysun513 12:7060e287bdae 398 else
stormysun513 12:7060e287bdae 399 lastPacketSize = UART_PACKET_SIZE;
stormysun513 12:7060e287bdae 400
stormysun513 12:7060e287bdae 401 currentPacketIndex = 0;
stormysun513 12:7060e287bdae 402 resetSTM32StateType();
stormysun513 12:7060e287bdae 403 }
stormysun513 12:7060e287bdae 404 break;
stormysun513 16:5552833a3401 405 }
stormysun513 12:7060e287bdae 406 case SYS_HEADER_SALIVA_VOLTAGE:
stormysun513 16:5552833a3401 407 // {
stormysun513 16:5552833a3401 408 // RingBuffer_read(&ringBuffer, buf, 1);
stormysun513 16:5552833a3401 409 // header = static_cast<HeaderTypeDef>(buf[0]);
stormysun513 16:5552833a3401 410 // if (header == SYS_HEADER_SALIVA_VOLTAGE){
stormysun513 16:5552833a3401 411 // RingBuffer_read(&ringBuffer, buf+1, SINGLE_UINT16_PAYLOAD-1);
stormysun513 16:5552833a3401 412 // if(mHandle != 0)
stormysun513 16:5552833a3401 413 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 16:5552833a3401 414 //
stormysun513 16:5552833a3401 415 // stm32State = STM32_CHECK_HEADER;
stormysun513 16:5552833a3401 416 // uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 16:5552833a3401 417 // }
stormysun513 16:5552833a3401 418 // break;
stormysun513 16:5552833a3401 419 // }
stormysun513 12:7060e287bdae 420 case SYS_HEADER_NO_SPECIFIC:
stormysun513 12:7060e287bdae 421 default:
stormysun513 12:7060e287bdae 422 break;
stormysun513 12:7060e287bdae 423 }
stormysun513 12:7060e287bdae 424 break;
stormysun513 12:7060e287bdae 425 }
stormysun513 12:7060e287bdae 426 case STM32_IDLE:
stormysun513 12:7060e287bdae 427 default:
stormysun513 12:7060e287bdae 428 break;
stormysun513 12:7060e287bdae 429 }
stormysun513 12:7060e287bdae 430 isEnterMainLoop = false;
stormysun513 12:7060e287bdae 431 }
stormysun513 12:7060e287bdae 432 ble.waitForEvent();
stevep 4:81cea7a352b0 433 }
dan 0:7dec7e9ac085 434 }
stormysun513 11:b3929de96933 435
stormysun513 12:7060e287bdae 436 void bleInitCompleteCB(struct BLE::InitializationCompleteCallbackContext *context){
stormysun513 12:7060e287bdae 437 if (context->error != BLE_ERROR_NONE)
stormysun513 12:7060e287bdae 438 return;
stormysun513 12:7060e287bdae 439
stormysun513 12:7060e287bdae 440 if (context->ble.getInstanceID() == BLE::DEFAULT_INSTANCE){
stormysun513 12:7060e287bdae 441 /* use the BLE instance */
stormysun513 12:7060e287bdae 442 return;
stormysun513 12:7060e287bdae 443 }
stormysun513 12:7060e287bdae 444 }
stormysun513 12:7060e287bdae 445
stormysun513 12:7060e287bdae 446 void bleOnConnectionCB(const Gap::ConnectionCallbackParams_t *p_conn_param){
stormysun513 12:7060e287bdae 447 mHandle = p_conn_param->handle;
stormysun513 12:7060e287bdae 448 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 449 connIdleCnt = 0;
stormysun513 12:7060e287bdae 450 #endif
stormysun513 12:7060e287bdae 451 bleReduceConnInterval();
stormysun513 12:7060e287bdae 452 }
stormysun513 12:7060e287bdae 453
stormysun513 12:7060e287bdae 454 void bleDisconnectionCB(const Gap::DisconnectionCallbackParams_t *params){
stormysun513 12:7060e287bdae 455 mHandle = 0;
stormysun513 12:7060e287bdae 456 if(systemState != SYS_POWERSAVING){
stormysun513 12:7060e287bdae 457 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 458 disConnIdleCnt = 0;
stormysun513 12:7060e287bdae 459 #endif
stormysun513 12:7060e287bdae 460 ble.startAdvertising();
stormysun513 12:7060e287bdae 461 }
stormysun513 12:7060e287bdae 462 }
stormysun513 12:7060e287bdae 463
stormysun513 12:7060e287bdae 464 void bleCharacteristicWrittenCB(const GattWriteCallbackParams *Handler){
stormysun513 12:7060e287bdae 465 uint16_t bleBytesRead;
stormysun513 12:7060e287bdae 466 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
stormysun513 12:7060e287bdae 467 {
stormysun513 12:7060e287bdae 468 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 469 connIdleCnt = 0;
stormysun513 12:7060e287bdae 470 #endif
stormysun513 12:7060e287bdae 471 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), uartTxPayload, &bleBytesRead);
stormysun513 12:7060e287bdae 472 if(bleBytesRead == 0)
stormysun513 12:7060e287bdae 473 return;
stormysun513 12:7060e287bdae 474
stormysun513 12:7060e287bdae 475 BLECommandTypeDef command = static_cast<BLECommandTypeDef>(uartTxPayload[0]);
stormysun513 12:7060e287bdae 476 bool isUARTCommand = false;
stormysun513 12:7060e287bdae 477 switch(command){
stormysun513 16:5552833a3401 478 case BLE_SWITCH_MONITORING:
stormysun513 16:5552833a3401 479 isMonitoring = !isMonitoring;
stormysun513 12:7060e287bdae 480 break;
stormysun513 12:7060e287bdae 481 case BLE_REQUEST_SALIVA_VOLTAGE:
stormysun513 12:7060e287bdae 482 waitHeader = SYS_HEADER_SALIVA_VOLTAGE;
stormysun513 12:7060e287bdae 483 salivaPeriodically = true;
stormysun513 12:7060e287bdae 484 systemState = SYS_LOCKING_BUTTON; // Lock Begin
stormysun513 12:7060e287bdae 485 break;
stormysun513 12:7060e287bdae 486 case BLE_DEREQUEST_SALIVA_VOLTAGE:
stormysun513 12:7060e287bdae 487 salivaPeriodically = false;
stormysun513 12:7060e287bdae 488 break;
stormysun513 12:7060e287bdae 489 case BLE_REQUEST_IMAGE_INFO:
stormysun513 12:7060e287bdae 490 waitHeader = SYS_HEADER_IMAGE_INFO;
stormysun513 12:7060e287bdae 491 isUARTCommand = true;
stormysun513 12:7060e287bdae 492 salivaPeriodically = false;
stormysun513 12:7060e287bdae 493 break;
stormysun513 12:7060e287bdae 494 case BLE_END_IMAGE_TRANSFER:
stormysun513 12:7060e287bdae 495 isRetransmit = false;
stormysun513 12:7060e287bdae 496 packetNum = 0;
stormysun513 12:7060e287bdae 497 resetSTM32StateType();
stormysun513 12:7060e287bdae 498 break;
stormysun513 12:7060e287bdae 499 case BLE_SHUTDOWN:
stormysun513 12:7060e287bdae 500 changeButtonPinToInterrupt();
stormysun513 12:7060e287bdae 501 shutdownDevice();
stormysun513 12:7060e287bdae 502 break;
stormysun513 12:7060e287bdae 503 case BLE_UNLOCK_BUTTON:
stormysun513 19:03d39b923919 504 {
stormysun513 12:7060e287bdae 505 systemState = SYS_RELAXING_BUTTON;
stormysun513 19:03d39b923919 506 buttonState = BTN_IDLE;
stormysun513 12:7060e287bdae 507 break;
stormysun513 19:03d39b923919 508 }
stormysun513 16:5552833a3401 509 case BLE_UART_DEBUG:
stormysun513 16:5552833a3401 510 {
stormysun513 12:7060e287bdae 511 uint8_t debugHeader = 0xF1;
stormysun513 12:7060e287bdae 512 uartTxTransmit(&debugHeader, 1);
stormysun513 12:7060e287bdae 513 break;
stormysun513 12:7060e287bdae 514 }
stormysun513 12:7060e287bdae 515 case BLE_REQUEST_IMAGE_BY_INDEX:
stormysun513 12:7060e287bdae 516 if(bleBytesRead >= 2){
stormysun513 12:7060e287bdae 517 uint8_t check = uartTxPayload[1];
stormysun513 12:7060e287bdae 518 if(check == 0){
stormysun513 12:7060e287bdae 519 if(packetNum > 0){
stormysun513 12:7060e287bdae 520 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 521 directForward = false;
stormysun513 12:7060e287bdae 522 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 523 currentPacketIndex = 0;
stormysun513 12:7060e287bdae 524 isRetransmit = false;
stormysun513 12:7060e287bdae 525 retransmitSize = 0;
stormysun513 12:7060e287bdae 526 stm32State = STM32_IMAGE_FORWARDING;
stormysun513 12:7060e287bdae 527 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 528 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 529 }
stormysun513 12:7060e287bdae 530 }
stormysun513 12:7060e287bdae 531 else if(check <= 18){
stormysun513 12:7060e287bdae 532 if(bleBytesRead < check+2)
stormysun513 12:7060e287bdae 533 break;
stormysun513 12:7060e287bdae 534 retransmitSize = check;
stormysun513 12:7060e287bdae 535 memcpy(retransmitIndices, uartTxPayload+2, retransmitSize);
stormysun513 12:7060e287bdae 536 if(isRetransmit == true){
stormysun513 12:7060e287bdae 537 stm32State = STM32_IMAGE_FORWARDING;
stormysun513 12:7060e287bdae 538 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 539 retransmitIndex = 0;
stormysun513 12:7060e287bdae 540 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 541 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 542 }
stormysun513 12:7060e287bdae 543 else{
stormysun513 12:7060e287bdae 544 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 545 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 546 }
stormysun513 12:7060e287bdae 547 }
stormysun513 12:7060e287bdae 548 else{
stormysun513 12:7060e287bdae 549 /* Invalid check number */
stormysun513 12:7060e287bdae 550 }
stormysun513 12:7060e287bdae 551 }
stormysun513 12:7060e287bdae 552 break;
stormysun513 16:5552833a3401 553 case BLE_CHANGE_CASSETTE_ID:
stormysun513 12:7060e287bdae 554 if(bleBytesRead >= 3)
stormysun513 12:7060e287bdae 555 writeAT24EEPROMBuffer(*pI2C, 0, (char*)uartTxPayload+1, 2);
stormysun513 12:7060e287bdae 556 break;
stormysun513 16:5552833a3401 557 case BLE_REQUEST_DEVICE_INFO:
stormysun513 16:5552833a3401 558 {
stormysun513 16:5552833a3401 559 uint8_t buf[SINGLE_UINT16_PAYLOAD] = {0,};
stormysun513 16:5552833a3401 560 buf[0] = SYS_HEADER_DEVICE_INFO;
stormysun513 16:5552833a3401 561 buf[1] = (codeVersion >> 8) & 0xFF;
stormysun513 16:5552833a3401 562 buf[2] = codeVersion & 0xFF;
stormysun513 16:5552833a3401 563 if(mHandle != 0)
stormysun513 16:5552833a3401 564 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 16:5552833a3401 565
stormysun513 16:5552833a3401 566 break;
stormysun513 16:5552833a3401 567 }
stormysun513 12:7060e287bdae 568 default:
stormysun513 12:7060e287bdae 569 break;
stormysun513 12:7060e287bdae 570 }
stormysun513 12:7060e287bdae 571 if(isUARTCommand){
stormysun513 12:7060e287bdae 572 stm32State = STM32_CHECK_HEADER;
stormysun513 12:7060e287bdae 573 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 574 uartTxTransmit(uartTxPayload, 1);
stormysun513 12:7060e287bdae 575 }
stormysun513 12:7060e287bdae 576 }
stormysun513 12:7060e287bdae 577 }
stormysun513 12:7060e287bdae 578
stormysun513 12:7060e287bdae 579 void bleReduceConnInterval(void){
stormysun513 12:7060e287bdae 580 Gap::ConnectionParams_t bleGapConnParamInitStruct;
stormysun513 12:7060e287bdae 581 bleGapConnParamInitStruct.minConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/
stormysun513 12:7060e287bdae 582 bleGapConnParamInitStruct.maxConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/
stormysun513 12:7060e287bdae 583 bleGapConnParamInitStruct.slaveLatency = 0; /*Slave Latency in number of connection events*/
stormysun513 12:7060e287bdae 584 bleGapConnParamInitStruct.connectionSupervisionTimeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; /*Connection Supervision Timeout in 10 ms*/
stormysun513 12:7060e287bdae 585
stormysun513 12:7060e287bdae 586 ble.gap().updateConnectionParams(mHandle, &bleGapConnParamInitStruct);
stormysun513 12:7060e287bdae 587 }
stormysun513 12:7060e287bdae 588
stormysun513 11:b3929de96933 589 void timerPeriodicalCB(void){
stormysun513 11:b3929de96933 590 static uint32_t time = 0;
stormysun513 11:b3929de96933 591 time++;
stormysun513 11:b3929de96933 592
stormysun513 11:b3929de96933 593 if ( time % 100 == 2 ) { // 250ms
stormysun513 12:7060e287bdae 594 if(buttonState == BTN_CHECK_LONG_PRESS)
stormysun513 12:7060e287bdae 595 checkButtonCounter++;
stormysun513 11:b3929de96933 596 }
stormysun513 11:b3929de96933 597
stormysun513 12:7060e287bdae 598 if(systemState != SYS_POWERSAVING){
stormysun513 12:7060e287bdae 599 if ( time % 3 == 1 ) { // 7.5ms
stormysun513 12:7060e287bdae 600 isCheckBuf = true;
stormysun513 12:7060e287bdae 601 }
stormysun513 12:7060e287bdae 602
stormysun513 14:f62d55b7dead 603 // if ( time % 50 == 2 ) { // 250ms
stormysun513 14:f62d55b7dead 604 // if(isInserted)
stormysun513 14:f62d55b7dead 605 // timerFlashPowerLed();
stormysun513 14:f62d55b7dead 606 // }
stormysun513 12:7060e287bdae 607
stormysun513 12:7060e287bdae 608 if ( time % 200 == 3 ) { // 500ms
stormysun513 14:f62d55b7dead 609 #ifdef LOW_POWER_CONSTRAINT
stormysun513 14:f62d55b7dead 610 if(isLowPower){
stormysun513 14:f62d55b7dead 611 timerFlashPowerLed();
stormysun513 14:f62d55b7dead 612 lowPowerCounter++;
stormysun513 14:f62d55b7dead 613 }
stormysun513 14:f62d55b7dead 614 #endif
stormysun513 15:c099dd509203 615
stormysun513 19:03d39b923919 616 if(isInserted && systemState == SYS_LOCKING_BUTTON){
stormysun513 19:03d39b923919 617 timerFlashInsertionLed();
stormysun513 20:f6e313950373 618 }
stormysun513 12:7060e287bdae 619 }
stormysun513 12:7060e287bdae 620
stormysun513 12:7060e287bdae 621 if ( time % 400 == 4 ) { // 1s
stormysun513 12:7060e287bdae 622 isRoutine = true;
stormysun513 12:7060e287bdae 623 #ifdef APPLICATION_TIMEOUT
stormysun513 13:65c9b7d667f6 624 if(mHandle == 0)
stormysun513 12:7060e287bdae 625 disConnIdleCnt++;
stormysun513 12:7060e287bdae 626 else
stormysun513 12:7060e287bdae 627 connIdleCnt++;
stormysun513 12:7060e287bdae 628 #endif
stormysun513 12:7060e287bdae 629 }
stormysun513 12:7060e287bdae 630
stormysun513 12:7060e287bdae 631 if ( time % 800 == 5 ) {
stormysun513 12:7060e287bdae 632 if(salivaPeriodically == true)
stormysun513 12:7060e287bdae 633 requestSalivaVoltage();
stormysun513 12:7060e287bdae 634 }
stormysun513 11:b3929de96933 635 }
stormysun513 11:b3929de96933 636 }
stormysun513 11:b3929de96933 637
stormysun513 14:f62d55b7dead 638 void timerFlashPowerLed(void){
stormysun513 12:7060e287bdae 639 ledPower = !ledPower;
stormysun513 11:b3929de96933 640 }
stormysun513 11:b3929de96933 641
stormysun513 14:f62d55b7dead 642 void timerFlashInsertionLed(void){
stormysun513 14:f62d55b7dead 643 int tmp = pLedInsertion->read();
stormysun513 14:f62d55b7dead 644 pLedInsertion->write(!tmp);
stormysun513 14:f62d55b7dead 645 }
stormysun513 14:f62d55b7dead 646
stormysun513 12:7060e287bdae 647 void timerRoutineTasks(void){
stormysun513 11:b3929de96933 648 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 16:5552833a3401 649 buf[0] = SYS_HEADER_MONITORING;
stormysun513 12:7060e287bdae 650 bool result = readAT24EEPROMBuffer(*pI2C, 0, (char*)buf+1, 2);
stormysun513 11:b3929de96933 651
stormysun513 11:b3929de96933 652 if(result == false){
stormysun513 14:f62d55b7dead 653 pLedInsertion->write(1);
stormysun513 15:c099dd509203 654 isInserted = false;
stormysun513 11:b3929de96933 655 buf[1] = 0xFF;
stormysun513 11:b3929de96933 656 buf[2] = 0xFF;
stormysun513 19:03d39b923919 657
stormysun513 19:03d39b923919 658 // If no ID detected, system will unlock button automatically. 5/17
stormysun513 19:03d39b923919 659 if(systemState == SYS_LOCKING_BUTTON){
stormysun513 19:03d39b923919 660 systemState = SYS_RELAXING_BUTTON;
stormysun513 19:03d39b923919 661 buttonState = BTN_IDLE;
stormysun513 19:03d39b923919 662 }
stormysun513 11:b3929de96933 663 }
stormysun513 11:b3929de96933 664 else{
stormysun513 15:c099dd509203 665 isInserted = true;
stormysun513 15:c099dd509203 666 if(systemState != SYS_LOCKING_BUTTON){
stormysun513 15:c099dd509203 667 pLedInsertion->write(0);
stormysun513 15:c099dd509203 668 }
stormysun513 15:c099dd509203 669
stormysun513 14:f62d55b7dead 670 if(systemState == SYS_POWERSAVING){
stormysun513 14:f62d55b7dead 671 pLedInsertion->write(1);
stormysun513 14:f62d55b7dead 672 }
stormysun513 12:7060e287bdae 673 }
stormysun513 12:7060e287bdae 674
stormysun513 12:7060e287bdae 675 float fvalue = (float)aniBatterVolt.read()*3.3*100;
stormysun513 12:7060e287bdae 676 uint16_t ivalue = (uint16_t)fvalue;
stormysun513 12:7060e287bdae 677 buf[3] = (ivalue >> 8) & 0xFF;
stormysun513 12:7060e287bdae 678 buf[4] = ivalue & 0xFF;
stormysun513 12:7060e287bdae 679
stormysun513 14:f62d55b7dead 680 #ifdef LOW_POWER_CONSTRAINT
stormysun513 14:f62d55b7dead 681 if(!isLowPower && ivalue < (LOW_POWER_THERS-LOW_POWER_THERS_MARGIN)){
stormysun513 14:f62d55b7dead 682 isLowPower = true;
stormysun513 14:f62d55b7dead 683 lowPowerCounter = 0;
stormysun513 14:f62d55b7dead 684 }
stormysun513 14:f62d55b7dead 685 else if(isLowPower && ivalue > (LOW_POWER_THERS+LOW_POWER_THERS_MARGIN)){
stormysun513 14:f62d55b7dead 686 isLowPower = false;
stormysun513 14:f62d55b7dead 687 }
stormysun513 14:f62d55b7dead 688 #endif
stormysun513 14:f62d55b7dead 689
stormysun513 16:5552833a3401 690 if(mHandle != 0 && isMonitoring == true){
stormysun513 16:5552833a3401 691 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, MONITORING_PAYLOAD);
stormysun513 12:7060e287bdae 692 }
stormysun513 12:7060e287bdae 693 }
stormysun513 12:7060e287bdae 694
stormysun513 12:7060e287bdae 695 void uartTxTransmit(uint8_t* buf, uint16_t length){
stormysun513 12:7060e287bdae 696 uint16_t index;
stormysun513 12:7060e287bdae 697 pc.putc(preamble[0]);
stormysun513 12:7060e287bdae 698 pc.putc(preamble[1]);
stormysun513 12:7060e287bdae 699 for(index = 0; index < length; index++)
stormysun513 12:7060e287bdae 700 {
stormysun513 12:7060e287bdae 701 pc.putc(buf[index]);
stormysun513 12:7060e287bdae 702 }
stormysun513 12:7060e287bdae 703 }
stormysun513 12:7060e287bdae 704
stormysun513 12:7060e287bdae 705 // UARTRX Callback function
stormysun513 12:7060e287bdae 706 void uartRxCB(void){
stormysun513 12:7060e287bdae 707 while(pc.readable())
stormysun513 12:7060e287bdae 708 {
stormysun513 12:7060e287bdae 709 uint8_t byteIn = pc.getc();
stormysun513 12:7060e287bdae 710 if(stm32State == STM32_IDLE)
stormysun513 12:7060e287bdae 711 continue;
stormysun513 12:7060e287bdae 712
stormysun513 12:7060e287bdae 713 if(uartRxState == UARTRX_TRANSFERING_DATA){
stormysun513 12:7060e287bdae 714 RingBuffer_writebyte(&ringBuffer, byteIn);
stormysun513 12:7060e287bdae 715 }
stormysun513 12:7060e287bdae 716 else if(uartRxState == UARTRX_CHECK_FIRST_PREAMBLE && byteIn == preamble[0]){
stormysun513 12:7060e287bdae 717 uartRxState = UARTRX_CHECK_SECOND_PREAMBLE;
stormysun513 12:7060e287bdae 718 }
stormysun513 12:7060e287bdae 719 else if(uartRxState == UARTRX_CHECK_SECOND_PREAMBLE && byteIn == preamble[1]){
stormysun513 12:7060e287bdae 720 uartRxState = UARTRX_TRANSFERING_DATA;
stormysun513 12:7060e287bdae 721 }
stormysun513 12:7060e287bdae 722 }
stormysun513 12:7060e287bdae 723 }
stormysun513 12:7060e287bdae 724
stormysun513 12:7060e287bdae 725 void buttonInterruptServiceRoutine(void){
stormysun513 12:7060e287bdae 726 if(buttonState == BTN_IDLE)
stormysun513 12:7060e287bdae 727 buttonState = BTN_CHANGE_ANALOG;
stormysun513 12:7060e287bdae 728
stormysun513 12:7060e287bdae 729 if(systemState == SYS_POWERSAVING)
stormysun513 12:7060e287bdae 730 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 12:7060e287bdae 731 }
stormysun513 12:7060e287bdae 732
stormysun513 12:7060e287bdae 733 void buttonCheckLongPress(uint8_t counter){
stormysun513 12:7060e287bdae 734 if(counter < CHECK_BUTTON_TIMES){
stormysun513 12:7060e287bdae 735 if(pButtonAnalogIn != NULL && (float)pButtonAnalogIn->read() < CHECK_BUTTON_VOLTAGE_LEVEL){
stormysun513 12:7060e287bdae 736 return;
stormysun513 12:7060e287bdae 737 }
stormysun513 12:7060e287bdae 738 else if(systemState == SYS_POWERSAVING){
stormysun513 12:7060e287bdae 739 ticker.detach();
stormysun513 12:7060e287bdae 740 }
stormysun513 12:7060e287bdae 741 }
stormysun513 12:7060e287bdae 742 else{
stormysun513 12:7060e287bdae 743 buttonLongPressVerified();
stormysun513 12:7060e287bdae 744 }
stormysun513 12:7060e287bdae 745 changeButtonPinToInterrupt();
stormysun513 12:7060e287bdae 746 }
stormysun513 12:7060e287bdae 747
stormysun513 12:7060e287bdae 748 void buttonLongPressVerified(void){
stormysun513 12:7060e287bdae 749 checkButtonCounter = 0;
stormysun513 12:7060e287bdae 750 if(systemState == SYS_POWERSAVING){
stormysun513 12:7060e287bdae 751 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 752 ble.startAdvertising();
stormysun513 12:7060e287bdae 753 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 754 disConnIdleCnt = 0;
stormysun513 12:7060e287bdae 755 #endif
stormysun513 12:7060e287bdae 756 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 14:f62d55b7dead 757 ledPower = 0;
stormysun513 14:f62d55b7dead 758 uartEnable = 1;
stormysun513 12:7060e287bdae 759 resetSTM32StateType();
stormysun513 12:7060e287bdae 760 }
stormysun513 12:7060e287bdae 761 else if(systemState == SYS_RELAXING_BUTTON){
stormysun513 12:7060e287bdae 762 shutdownDevice();
stormysun513 12:7060e287bdae 763 }
stormysun513 12:7060e287bdae 764 }
stormysun513 12:7060e287bdae 765
stormysun513 12:7060e287bdae 766 void changeButtonPinToAnalogIn(void){
stormysun513 12:7060e287bdae 767 pButton->fall(NULL);
stormysun513 12:7060e287bdae 768 delete pButton;
stormysun513 12:7060e287bdae 769 checkButtonCounter = 0;
stormysun513 12:7060e287bdae 770 pButtonAnalogIn = new AnalogIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 771 buttonState = BTN_CHECK_LONG_PRESS;
stormysun513 12:7060e287bdae 772 }
stormysun513 12:7060e287bdae 773
stormysun513 12:7060e287bdae 774 void changeButtonPinToInterrupt(void){
stormysun513 12:7060e287bdae 775 delete pButtonAnalogIn;
stormysun513 12:7060e287bdae 776 pButton = new InterruptIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 777 pButton->fall(buttonInterruptServiceRoutine);
stormysun513 12:7060e287bdae 778 float dummy = (float)pButtonAnalogIn->read();
stormysun513 12:7060e287bdae 779 buttonState = BTN_IDLE;
stormysun513 11:b3929de96933 780 }
stormysun513 12:7060e287bdae 781
stormysun513 12:7060e287bdae 782 void shutdownDevice(void){
stormysun513 12:7060e287bdae 783 systemState = SYS_POWERSAVING;
stormysun513 13:65c9b7d667f6 784 if(mHandle != 0)
stormysun513 13:65c9b7d667f6 785 ble.disconnect(mHandle, Gap::REMOTE_USER_TERMINATED_CONNECTION);
stormysun513 14:f62d55b7dead 786 pLedInsertion->write(1);
stormysun513 13:65c9b7d667f6 787 ticker.detach();
stormysun513 12:7060e287bdae 788 ble.gap().stopAdvertising();
stormysun513 12:7060e287bdae 789 ledPower = 1;
stormysun513 14:f62d55b7dead 790 uartEnable = 0;
stormysun513 18:d146cee0a80e 791 isMonitoring = false;
stormysun513 18:d146cee0a80e 792 salivaPeriodically = false;
stormysun513 17:0c7951915608 793 #ifdef LOW_POWER_CONSTRAINT
stormysun513 17:0c7951915608 794 lowPowerCounter = 0;
stormysun513 17:0c7951915608 795 #endif
stormysun513 12:7060e287bdae 796 deepsleep();
stormysun513 12:7060e287bdae 797 }
stormysun513 12:7060e287bdae 798
stormysun513 12:7060e287bdae 799 void resetSTM32StateType(void){
stormysun513 12:7060e287bdae 800 stm32State = STM32_IDLE;
stormysun513 12:7060e287bdae 801 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 802 waitHeader = SYS_HEADER_NO_SPECIFIC;
stormysun513 12:7060e287bdae 803 }
stormysun513 12:7060e287bdae 804
stormysun513 12:7060e287bdae 805 void requestSalivaVoltage(void){
stormysun513 12:7060e287bdae 806 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 807 float fvalue = aniSalivaVolt.read()*3.3*100;
stormysun513 12:7060e287bdae 808 uint16_t ivalue = (uint16_t)fvalue;
stormysun513 12:7060e287bdae 809
stormysun513 12:7060e287bdae 810 buf[0] = SYS_HEADER_SALIVA_VOLTAGE;
stormysun513 12:7060e287bdae 811 buf[1] = (ivalue >> 8) & 0xFF;
stormysun513 12:7060e287bdae 812 buf[2] = ivalue & 0xFF;
stormysun513 12:7060e287bdae 813
stormysun513 16:5552833a3401 814 if(mHandle != 0)
stormysun513 12:7060e287bdae 815 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 12:7060e287bdae 816 }
stormysun513 12:7060e287bdae 817
stormysun513 12:7060e287bdae 818 void timerCheckBuffer(void){
stormysun513 12:7060e287bdae 819 uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer);
stormysun513 12:7060e287bdae 820 switch (stm32State) {
stormysun513 12:7060e287bdae 821 case STM32_CHECK_HEADER:{
stormysun513 12:7060e287bdae 822 if (numBytes >= SINGLE_UINT16_PAYLOAD){
stormysun513 12:7060e287bdae 823 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 824 }
stormysun513 12:7060e287bdae 825 break;
stormysun513 12:7060e287bdae 826 }
stormysun513 19:03d39b923919 827 case STM32_IMAGE_FORWARDING:
stormysun513 19:03d39b923919 828 {
stormysun513 12:7060e287bdae 829 if(setImgPktPayloadByID == false){
stormysun513 12:7060e287bdae 830 if(numBytes >= UART_DATA_PACKET_PAYLOAD){
stormysun513 12:7060e287bdae 831 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 832 }
stormysun513 12:7060e287bdae 833 else{
stormysun513 12:7060e287bdae 834 if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){
stormysun513 12:7060e287bdae 835 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 836 directForward = false;
stormysun513 12:7060e287bdae 837 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 838 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 839 waitCamera = 0;
stormysun513 12:7060e287bdae 840 }
stormysun513 12:7060e287bdae 841 }
stormysun513 12:7060e287bdae 842 }
stormysun513 12:7060e287bdae 843 else {
stormysun513 12:7060e287bdae 844 if((targetPayload-alreadyTransferred) <= (BLE_BUF_LEN-1)){
stormysun513 12:7060e287bdae 845 if(numBytes+alreadyTransferred == targetPayload)
stormysun513 12:7060e287bdae 846 directForward = true;
stormysun513 12:7060e287bdae 847 }
stormysun513 12:7060e287bdae 848
stormysun513 12:7060e287bdae 849 if (numBytes > 0){
stormysun513 12:7060e287bdae 850 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 851 }
stormysun513 12:7060e287bdae 852
stormysun513 12:7060e287bdae 853 else if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){
stormysun513 12:7060e287bdae 854 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 855 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 856 waitCamera = 0;
stormysun513 12:7060e287bdae 857 }
stormysun513 12:7060e287bdae 858
stormysun513 12:7060e287bdae 859 }
stormysun513 12:7060e287bdae 860 break;
stormysun513 12:7060e287bdae 861 }
stormysun513 12:7060e287bdae 862 default:
stormysun513 12:7060e287bdae 863 break;
stormysun513 12:7060e287bdae 864 }
stormysun513 12:7060e287bdae 865 }
stormysun513 12:7060e287bdae 866
stormysun513 12:7060e287bdae 867 void requestForImagePacket(uint16_t packetId){
stormysun513 12:7060e287bdae 868 uint8_t tempBuf[3];
stormysun513 12:7060e287bdae 869 tempBuf[0] = static_cast<uint8_t>(BLE_REQUEST_IMAGE_BY_INDEX);
stormysun513 12:7060e287bdae 870 tempBuf[1] = (packetId >> 8) & 0xFF;
stormysun513 12:7060e287bdae 871 tempBuf[2] = packetId & 0xFF;
stormysun513 12:7060e287bdae 872 uartTxTransmit(tempBuf, 3);
stormysun513 12:7060e287bdae 873 }
stormysun513 12:7060e287bdae 874
stormysun513 12:7060e287bdae 875 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
stormysun513 12:7060e287bdae 876 FlowControl flow_type = (FlowControl)type;
stormysun513 12:7060e287bdae 877 switch(type) {
stormysun513 12:7060e287bdae 878 case RTS:
stormysun513 12:7060e287bdae 879 serial_set_flow_control(&_serial, flow_type, flow1, NC);
stormysun513 12:7060e287bdae 880 break;
stormysun513 12:7060e287bdae 881
stormysun513 12:7060e287bdae 882 case CTS:
stormysun513 12:7060e287bdae 883 serial_set_flow_control(&_serial, flow_type, NC, flow1);
stormysun513 12:7060e287bdae 884 break;
stormysun513 12:7060e287bdae 885
stormysun513 12:7060e287bdae 886 case RTSCTS:
stormysun513 12:7060e287bdae 887 case Disabled:
stormysun513 12:7060e287bdae 888 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
stormysun513 12:7060e287bdae 889 break;
stormysun513 12:7060e287bdae 890
stormysun513 12:7060e287bdae 891 default:
stormysun513 12:7060e287bdae 892 break;
stormysun513 12:7060e287bdae 893 }
stormysun513 12:7060e287bdae 894 }