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:
Sun May 01 09:45:51 2016 +0000
Revision:
18:d146cee0a80e
Parent:
17:0c7951915608
Child:
19:03d39b923919
Fix bug: termination without reset flag

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 18:d146cee0a80e 14 #define DEVICE_NAME "ket_041"
stormysun513 18:d146cee0a80e 15 #define CODE_VERSION 5
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 14:f62d55b7dead 32 #define LOW_POWER_THERS 100
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 12:7060e287bdae 504 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 505 break;
stormysun513 16:5552833a3401 506 case BLE_UART_DEBUG:
stormysun513 16:5552833a3401 507 {
stormysun513 12:7060e287bdae 508 uint8_t debugHeader = 0xF1;
stormysun513 12:7060e287bdae 509 uartTxTransmit(&debugHeader, 1);
stormysun513 12:7060e287bdae 510 break;
stormysun513 12:7060e287bdae 511 }
stormysun513 12:7060e287bdae 512 case BLE_REQUEST_IMAGE_BY_INDEX:
stormysun513 12:7060e287bdae 513 if(bleBytesRead >= 2){
stormysun513 12:7060e287bdae 514 uint8_t check = uartTxPayload[1];
stormysun513 12:7060e287bdae 515 if(check == 0){
stormysun513 12:7060e287bdae 516 if(packetNum > 0){
stormysun513 12:7060e287bdae 517 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 518 directForward = false;
stormysun513 12:7060e287bdae 519 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 520 currentPacketIndex = 0;
stormysun513 12:7060e287bdae 521 isRetransmit = false;
stormysun513 12:7060e287bdae 522 retransmitSize = 0;
stormysun513 12:7060e287bdae 523 stm32State = STM32_IMAGE_FORWARDING;
stormysun513 12:7060e287bdae 524 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 525 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 526 }
stormysun513 12:7060e287bdae 527 }
stormysun513 12:7060e287bdae 528 else if(check <= 18){
stormysun513 12:7060e287bdae 529 if(bleBytesRead < check+2)
stormysun513 12:7060e287bdae 530 break;
stormysun513 12:7060e287bdae 531 retransmitSize = check;
stormysun513 12:7060e287bdae 532 memcpy(retransmitIndices, uartTxPayload+2, retransmitSize);
stormysun513 12:7060e287bdae 533 if(isRetransmit == true){
stormysun513 12:7060e287bdae 534 stm32State = STM32_IMAGE_FORWARDING;
stormysun513 12:7060e287bdae 535 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 536 retransmitIndex = 0;
stormysun513 12:7060e287bdae 537 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 538 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 539 }
stormysun513 12:7060e287bdae 540 else{
stormysun513 12:7060e287bdae 541 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 542 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 543 }
stormysun513 12:7060e287bdae 544 }
stormysun513 12:7060e287bdae 545 else{
stormysun513 12:7060e287bdae 546 /* Invalid check number */
stormysun513 12:7060e287bdae 547 }
stormysun513 12:7060e287bdae 548 }
stormysun513 12:7060e287bdae 549 break;
stormysun513 16:5552833a3401 550 case BLE_CHANGE_CASSETTE_ID:
stormysun513 12:7060e287bdae 551 if(bleBytesRead >= 3)
stormysun513 12:7060e287bdae 552 writeAT24EEPROMBuffer(*pI2C, 0, (char*)uartTxPayload+1, 2);
stormysun513 12:7060e287bdae 553 break;
stormysun513 16:5552833a3401 554 case BLE_REQUEST_DEVICE_INFO:
stormysun513 16:5552833a3401 555 {
stormysun513 16:5552833a3401 556 uint8_t buf[SINGLE_UINT16_PAYLOAD] = {0,};
stormysun513 16:5552833a3401 557 buf[0] = SYS_HEADER_DEVICE_INFO;
stormysun513 16:5552833a3401 558 buf[1] = (codeVersion >> 8) & 0xFF;
stormysun513 16:5552833a3401 559 buf[2] = codeVersion & 0xFF;
stormysun513 16:5552833a3401 560 if(mHandle != 0)
stormysun513 16:5552833a3401 561 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 16:5552833a3401 562
stormysun513 16:5552833a3401 563 break;
stormysun513 16:5552833a3401 564 }
stormysun513 12:7060e287bdae 565 default:
stormysun513 12:7060e287bdae 566 break;
stormysun513 12:7060e287bdae 567 }
stormysun513 12:7060e287bdae 568 if(isUARTCommand){
stormysun513 12:7060e287bdae 569 stm32State = STM32_CHECK_HEADER;
stormysun513 12:7060e287bdae 570 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 571 uartTxTransmit(uartTxPayload, 1);
stormysun513 12:7060e287bdae 572 }
stormysun513 12:7060e287bdae 573 }
stormysun513 12:7060e287bdae 574 }
stormysun513 12:7060e287bdae 575
stormysun513 12:7060e287bdae 576 void bleReduceConnInterval(void){
stormysun513 12:7060e287bdae 577 Gap::ConnectionParams_t bleGapConnParamInitStruct;
stormysun513 12:7060e287bdae 578 bleGapConnParamInitStruct.minConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/
stormysun513 12:7060e287bdae 579 bleGapConnParamInitStruct.maxConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/
stormysun513 12:7060e287bdae 580 bleGapConnParamInitStruct.slaveLatency = 0; /*Slave Latency in number of connection events*/
stormysun513 12:7060e287bdae 581 bleGapConnParamInitStruct.connectionSupervisionTimeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; /*Connection Supervision Timeout in 10 ms*/
stormysun513 12:7060e287bdae 582
stormysun513 12:7060e287bdae 583 ble.gap().updateConnectionParams(mHandle, &bleGapConnParamInitStruct);
stormysun513 12:7060e287bdae 584 }
stormysun513 12:7060e287bdae 585
stormysun513 11:b3929de96933 586 void timerPeriodicalCB(void){
stormysun513 11:b3929de96933 587 static uint32_t time = 0;
stormysun513 11:b3929de96933 588 time++;
stormysun513 11:b3929de96933 589
stormysun513 11:b3929de96933 590 if ( time % 100 == 2 ) { // 250ms
stormysun513 12:7060e287bdae 591 if(buttonState == BTN_CHECK_LONG_PRESS)
stormysun513 12:7060e287bdae 592 checkButtonCounter++;
stormysun513 11:b3929de96933 593 }
stormysun513 11:b3929de96933 594
stormysun513 12:7060e287bdae 595 if(systemState != SYS_POWERSAVING){
stormysun513 12:7060e287bdae 596 if ( time % 3 == 1 ) { // 7.5ms
stormysun513 12:7060e287bdae 597 isCheckBuf = true;
stormysun513 12:7060e287bdae 598 }
stormysun513 12:7060e287bdae 599
stormysun513 14:f62d55b7dead 600 // if ( time % 50 == 2 ) { // 250ms
stormysun513 14:f62d55b7dead 601 // if(isInserted)
stormysun513 14:f62d55b7dead 602 // timerFlashPowerLed();
stormysun513 14:f62d55b7dead 603 // }
stormysun513 12:7060e287bdae 604
stormysun513 12:7060e287bdae 605 if ( time % 200 == 3 ) { // 500ms
stormysun513 14:f62d55b7dead 606 #ifdef LOW_POWER_CONSTRAINT
stormysun513 14:f62d55b7dead 607 if(isLowPower){
stormysun513 14:f62d55b7dead 608 timerFlashPowerLed();
stormysun513 14:f62d55b7dead 609 lowPowerCounter++;
stormysun513 14:f62d55b7dead 610 }
stormysun513 14:f62d55b7dead 611 #endif
stormysun513 15:c099dd509203 612
stormysun513 15:c099dd509203 613 if(isInserted && systemState == SYS_LOCKING_BUTTON)
stormysun513 14:f62d55b7dead 614 timerFlashInsertionLed();
stormysun513 12:7060e287bdae 615 }
stormysun513 12:7060e287bdae 616
stormysun513 12:7060e287bdae 617 if ( time % 400 == 4 ) { // 1s
stormysun513 12:7060e287bdae 618 isRoutine = true;
stormysun513 12:7060e287bdae 619 #ifdef APPLICATION_TIMEOUT
stormysun513 13:65c9b7d667f6 620 if(mHandle == 0)
stormysun513 12:7060e287bdae 621 disConnIdleCnt++;
stormysun513 12:7060e287bdae 622 else
stormysun513 12:7060e287bdae 623 connIdleCnt++;
stormysun513 12:7060e287bdae 624 #endif
stormysun513 12:7060e287bdae 625 }
stormysun513 12:7060e287bdae 626
stormysun513 12:7060e287bdae 627 if ( time % 800 == 5 ) {
stormysun513 12:7060e287bdae 628 if(salivaPeriodically == true)
stormysun513 12:7060e287bdae 629 requestSalivaVoltage();
stormysun513 12:7060e287bdae 630 }
stormysun513 11:b3929de96933 631 }
stormysun513 11:b3929de96933 632 }
stormysun513 11:b3929de96933 633
stormysun513 14:f62d55b7dead 634 void timerFlashPowerLed(void){
stormysun513 12:7060e287bdae 635 ledPower = !ledPower;
stormysun513 11:b3929de96933 636 }
stormysun513 11:b3929de96933 637
stormysun513 14:f62d55b7dead 638 void timerFlashInsertionLed(void){
stormysun513 14:f62d55b7dead 639 int tmp = pLedInsertion->read();
stormysun513 14:f62d55b7dead 640 pLedInsertion->write(!tmp);
stormysun513 14:f62d55b7dead 641 }
stormysun513 14:f62d55b7dead 642
stormysun513 12:7060e287bdae 643 void timerRoutineTasks(void){
stormysun513 11:b3929de96933 644 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 16:5552833a3401 645 buf[0] = SYS_HEADER_MONITORING;
stormysun513 12:7060e287bdae 646 bool result = readAT24EEPROMBuffer(*pI2C, 0, (char*)buf+1, 2);
stormysun513 11:b3929de96933 647
stormysun513 11:b3929de96933 648 if(result == false){
stormysun513 14:f62d55b7dead 649 pLedInsertion->write(1);
stormysun513 15:c099dd509203 650 isInserted = false;
stormysun513 11:b3929de96933 651 buf[1] = 0xFF;
stormysun513 11:b3929de96933 652 buf[2] = 0xFF;
stormysun513 11:b3929de96933 653 }
stormysun513 11:b3929de96933 654 else{
stormysun513 15:c099dd509203 655 isInserted = true;
stormysun513 15:c099dd509203 656 if(systemState != SYS_LOCKING_BUTTON){
stormysun513 15:c099dd509203 657 pLedInsertion->write(0);
stormysun513 15:c099dd509203 658 }
stormysun513 15:c099dd509203 659
stormysun513 14:f62d55b7dead 660 if(systemState == SYS_POWERSAVING){
stormysun513 14:f62d55b7dead 661 pLedInsertion->write(1);
stormysun513 14:f62d55b7dead 662 }
stormysun513 12:7060e287bdae 663 }
stormysun513 12:7060e287bdae 664
stormysun513 12:7060e287bdae 665 float fvalue = (float)aniBatterVolt.read()*3.3*100;
stormysun513 12:7060e287bdae 666 uint16_t ivalue = (uint16_t)fvalue;
stormysun513 12:7060e287bdae 667 buf[3] = (ivalue >> 8) & 0xFF;
stormysun513 12:7060e287bdae 668 buf[4] = ivalue & 0xFF;
stormysun513 12:7060e287bdae 669
stormysun513 14:f62d55b7dead 670 #ifdef LOW_POWER_CONSTRAINT
stormysun513 14:f62d55b7dead 671 if(!isLowPower && ivalue < (LOW_POWER_THERS-LOW_POWER_THERS_MARGIN)){
stormysun513 14:f62d55b7dead 672 isLowPower = true;
stormysun513 14:f62d55b7dead 673 lowPowerCounter = 0;
stormysun513 14:f62d55b7dead 674 }
stormysun513 14:f62d55b7dead 675 else if(isLowPower && ivalue > (LOW_POWER_THERS+LOW_POWER_THERS_MARGIN)){
stormysun513 14:f62d55b7dead 676 isLowPower = false;
stormysun513 14:f62d55b7dead 677 }
stormysun513 14:f62d55b7dead 678 #endif
stormysun513 14:f62d55b7dead 679
stormysun513 16:5552833a3401 680 if(mHandle != 0 && isMonitoring == true){
stormysun513 16:5552833a3401 681 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, MONITORING_PAYLOAD);
stormysun513 12:7060e287bdae 682 }
stormysun513 12:7060e287bdae 683 }
stormysun513 12:7060e287bdae 684
stormysun513 12:7060e287bdae 685 void uartTxTransmit(uint8_t* buf, uint16_t length){
stormysun513 12:7060e287bdae 686 uint16_t index;
stormysun513 12:7060e287bdae 687 pc.putc(preamble[0]);
stormysun513 12:7060e287bdae 688 pc.putc(preamble[1]);
stormysun513 12:7060e287bdae 689 for(index = 0; index < length; index++)
stormysun513 12:7060e287bdae 690 {
stormysun513 12:7060e287bdae 691 pc.putc(buf[index]);
stormysun513 12:7060e287bdae 692 }
stormysun513 12:7060e287bdae 693 }
stormysun513 12:7060e287bdae 694
stormysun513 12:7060e287bdae 695 // UARTRX Callback function
stormysun513 12:7060e287bdae 696 void uartRxCB(void){
stormysun513 12:7060e287bdae 697 while(pc.readable())
stormysun513 12:7060e287bdae 698 {
stormysun513 12:7060e287bdae 699 uint8_t byteIn = pc.getc();
stormysun513 12:7060e287bdae 700 if(stm32State == STM32_IDLE)
stormysun513 12:7060e287bdae 701 continue;
stormysun513 12:7060e287bdae 702
stormysun513 12:7060e287bdae 703 if(uartRxState == UARTRX_TRANSFERING_DATA){
stormysun513 12:7060e287bdae 704 RingBuffer_writebyte(&ringBuffer, byteIn);
stormysun513 12:7060e287bdae 705 }
stormysun513 12:7060e287bdae 706 else if(uartRxState == UARTRX_CHECK_FIRST_PREAMBLE && byteIn == preamble[0]){
stormysun513 12:7060e287bdae 707 uartRxState = UARTRX_CHECK_SECOND_PREAMBLE;
stormysun513 12:7060e287bdae 708 }
stormysun513 12:7060e287bdae 709 else if(uartRxState == UARTRX_CHECK_SECOND_PREAMBLE && byteIn == preamble[1]){
stormysun513 12:7060e287bdae 710 uartRxState = UARTRX_TRANSFERING_DATA;
stormysun513 12:7060e287bdae 711 }
stormysun513 12:7060e287bdae 712 }
stormysun513 12:7060e287bdae 713 }
stormysun513 12:7060e287bdae 714
stormysun513 12:7060e287bdae 715 void buttonInterruptServiceRoutine(void){
stormysun513 12:7060e287bdae 716 if(buttonState == BTN_IDLE)
stormysun513 12:7060e287bdae 717 buttonState = BTN_CHANGE_ANALOG;
stormysun513 12:7060e287bdae 718
stormysun513 12:7060e287bdae 719 if(systemState == SYS_POWERSAVING)
stormysun513 12:7060e287bdae 720 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 12:7060e287bdae 721 }
stormysun513 12:7060e287bdae 722
stormysun513 12:7060e287bdae 723 void buttonCheckLongPress(uint8_t counter){
stormysun513 12:7060e287bdae 724 if(counter < CHECK_BUTTON_TIMES){
stormysun513 12:7060e287bdae 725 if(pButtonAnalogIn != NULL && (float)pButtonAnalogIn->read() < CHECK_BUTTON_VOLTAGE_LEVEL){
stormysun513 12:7060e287bdae 726 return;
stormysun513 12:7060e287bdae 727 }
stormysun513 12:7060e287bdae 728 else if(systemState == SYS_POWERSAVING){
stormysun513 12:7060e287bdae 729 ticker.detach();
stormysun513 12:7060e287bdae 730 }
stormysun513 12:7060e287bdae 731 }
stormysun513 12:7060e287bdae 732 else{
stormysun513 12:7060e287bdae 733 buttonLongPressVerified();
stormysun513 12:7060e287bdae 734 }
stormysun513 12:7060e287bdae 735 changeButtonPinToInterrupt();
stormysun513 12:7060e287bdae 736 }
stormysun513 12:7060e287bdae 737
stormysun513 12:7060e287bdae 738 void buttonLongPressVerified(void){
stormysun513 12:7060e287bdae 739 checkButtonCounter = 0;
stormysun513 12:7060e287bdae 740 if(systemState == SYS_POWERSAVING){
stormysun513 12:7060e287bdae 741 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 742 ble.startAdvertising();
stormysun513 12:7060e287bdae 743 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 744 disConnIdleCnt = 0;
stormysun513 12:7060e287bdae 745 #endif
stormysun513 12:7060e287bdae 746 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 14:f62d55b7dead 747 ledPower = 0;
stormysun513 14:f62d55b7dead 748 uartEnable = 1;
stormysun513 12:7060e287bdae 749 resetSTM32StateType();
stormysun513 12:7060e287bdae 750 }
stormysun513 12:7060e287bdae 751 else if(systemState == SYS_RELAXING_BUTTON){
stormysun513 12:7060e287bdae 752 shutdownDevice();
stormysun513 12:7060e287bdae 753 }
stormysun513 12:7060e287bdae 754 }
stormysun513 12:7060e287bdae 755
stormysun513 12:7060e287bdae 756 void changeButtonPinToAnalogIn(void){
stormysun513 12:7060e287bdae 757 pButton->fall(NULL);
stormysun513 12:7060e287bdae 758 delete pButton;
stormysun513 12:7060e287bdae 759 checkButtonCounter = 0;
stormysun513 12:7060e287bdae 760 pButtonAnalogIn = new AnalogIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 761 buttonState = BTN_CHECK_LONG_PRESS;
stormysun513 12:7060e287bdae 762 }
stormysun513 12:7060e287bdae 763
stormysun513 12:7060e287bdae 764 void changeButtonPinToInterrupt(void){
stormysun513 12:7060e287bdae 765 delete pButtonAnalogIn;
stormysun513 12:7060e287bdae 766 pButton = new InterruptIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 767 pButton->fall(buttonInterruptServiceRoutine);
stormysun513 12:7060e287bdae 768 float dummy = (float)pButtonAnalogIn->read();
stormysun513 12:7060e287bdae 769 buttonState = BTN_IDLE;
stormysun513 11:b3929de96933 770 }
stormysun513 12:7060e287bdae 771
stormysun513 12:7060e287bdae 772 void shutdownDevice(void){
stormysun513 12:7060e287bdae 773 systemState = SYS_POWERSAVING;
stormysun513 13:65c9b7d667f6 774 if(mHandle != 0)
stormysun513 13:65c9b7d667f6 775 ble.disconnect(mHandle, Gap::REMOTE_USER_TERMINATED_CONNECTION);
stormysun513 14:f62d55b7dead 776 pLedInsertion->write(1);
stormysun513 13:65c9b7d667f6 777 ticker.detach();
stormysun513 12:7060e287bdae 778 ble.gap().stopAdvertising();
stormysun513 12:7060e287bdae 779 ledPower = 1;
stormysun513 14:f62d55b7dead 780 uartEnable = 0;
stormysun513 18:d146cee0a80e 781 isMonitoring = false;
stormysun513 18:d146cee0a80e 782 salivaPeriodically = false;
stormysun513 17:0c7951915608 783 #ifdef LOW_POWER_CONSTRAINT
stormysun513 17:0c7951915608 784 lowPowerCounter = 0;
stormysun513 17:0c7951915608 785 #endif
stormysun513 12:7060e287bdae 786 deepsleep();
stormysun513 12:7060e287bdae 787 }
stormysun513 12:7060e287bdae 788
stormysun513 12:7060e287bdae 789 void resetSTM32StateType(void){
stormysun513 12:7060e287bdae 790 stm32State = STM32_IDLE;
stormysun513 12:7060e287bdae 791 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 792 waitHeader = SYS_HEADER_NO_SPECIFIC;
stormysun513 12:7060e287bdae 793 }
stormysun513 12:7060e287bdae 794
stormysun513 12:7060e287bdae 795 void requestSalivaVoltage(void){
stormysun513 12:7060e287bdae 796 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 797 float fvalue = aniSalivaVolt.read()*3.3*100;
stormysun513 12:7060e287bdae 798 uint16_t ivalue = (uint16_t)fvalue;
stormysun513 12:7060e287bdae 799
stormysun513 12:7060e287bdae 800 buf[0] = SYS_HEADER_SALIVA_VOLTAGE;
stormysun513 12:7060e287bdae 801 buf[1] = (ivalue >> 8) & 0xFF;
stormysun513 12:7060e287bdae 802 buf[2] = ivalue & 0xFF;
stormysun513 12:7060e287bdae 803
stormysun513 16:5552833a3401 804 if(mHandle != 0)
stormysun513 12:7060e287bdae 805 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 12:7060e287bdae 806 }
stormysun513 12:7060e287bdae 807
stormysun513 12:7060e287bdae 808 void timerCheckBuffer(void){
stormysun513 12:7060e287bdae 809 uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer);
stormysun513 12:7060e287bdae 810 switch (stm32State) {
stormysun513 12:7060e287bdae 811 case STM32_CHECK_HEADER:{
stormysun513 12:7060e287bdae 812 if (numBytes >= SINGLE_UINT16_PAYLOAD){
stormysun513 12:7060e287bdae 813 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 814 }
stormysun513 12:7060e287bdae 815 break;
stormysun513 12:7060e287bdae 816 }
stormysun513 12:7060e287bdae 817 case STM32_IMAGE_FORWARDING:{
stormysun513 12:7060e287bdae 818 if(setImgPktPayloadByID == false){
stormysun513 12:7060e287bdae 819 if(numBytes >= UART_DATA_PACKET_PAYLOAD){
stormysun513 12:7060e287bdae 820 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 821 }
stormysun513 12:7060e287bdae 822 else{
stormysun513 12:7060e287bdae 823 if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){
stormysun513 12:7060e287bdae 824 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 825 directForward = false;
stormysun513 12:7060e287bdae 826 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 827 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 828 waitCamera = 0;
stormysun513 12:7060e287bdae 829 }
stormysun513 12:7060e287bdae 830 }
stormysun513 12:7060e287bdae 831 }
stormysun513 12:7060e287bdae 832 else {
stormysun513 12:7060e287bdae 833 if((targetPayload-alreadyTransferred) <= (BLE_BUF_LEN-1)){
stormysun513 12:7060e287bdae 834 if(numBytes+alreadyTransferred == targetPayload)
stormysun513 12:7060e287bdae 835 directForward = true;
stormysun513 12:7060e287bdae 836 }
stormysun513 12:7060e287bdae 837
stormysun513 12:7060e287bdae 838 if (numBytes > 0){
stormysun513 12:7060e287bdae 839 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 840 }
stormysun513 12:7060e287bdae 841
stormysun513 12:7060e287bdae 842 else if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){
stormysun513 12:7060e287bdae 843 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 844 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 845 waitCamera = 0;
stormysun513 12:7060e287bdae 846 }
stormysun513 12:7060e287bdae 847
stormysun513 12:7060e287bdae 848 }
stormysun513 12:7060e287bdae 849 break;
stormysun513 12:7060e287bdae 850 }
stormysun513 12:7060e287bdae 851 default:
stormysun513 12:7060e287bdae 852 break;
stormysun513 12:7060e287bdae 853 }
stormysun513 12:7060e287bdae 854 }
stormysun513 12:7060e287bdae 855
stormysun513 12:7060e287bdae 856 void requestForImagePacket(uint16_t packetId){
stormysun513 12:7060e287bdae 857 uint8_t tempBuf[3];
stormysun513 12:7060e287bdae 858 tempBuf[0] = static_cast<uint8_t>(BLE_REQUEST_IMAGE_BY_INDEX);
stormysun513 12:7060e287bdae 859 tempBuf[1] = (packetId >> 8) & 0xFF;
stormysun513 12:7060e287bdae 860 tempBuf[2] = packetId & 0xFF;
stormysun513 12:7060e287bdae 861 uartTxTransmit(tempBuf, 3);
stormysun513 12:7060e287bdae 862 }
stormysun513 12:7060e287bdae 863
stormysun513 12:7060e287bdae 864 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
stormysun513 12:7060e287bdae 865 FlowControl flow_type = (FlowControl)type;
stormysun513 12:7060e287bdae 866 switch(type) {
stormysun513 12:7060e287bdae 867 case RTS:
stormysun513 12:7060e287bdae 868 serial_set_flow_control(&_serial, flow_type, flow1, NC);
stormysun513 12:7060e287bdae 869 break;
stormysun513 12:7060e287bdae 870
stormysun513 12:7060e287bdae 871 case CTS:
stormysun513 12:7060e287bdae 872 serial_set_flow_control(&_serial, flow_type, NC, flow1);
stormysun513 12:7060e287bdae 873 break;
stormysun513 12:7060e287bdae 874
stormysun513 12:7060e287bdae 875 case RTSCTS:
stormysun513 12:7060e287bdae 876 case Disabled:
stormysun513 12:7060e287bdae 877 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
stormysun513 12:7060e287bdae 878 break;
stormysun513 12:7060e287bdae 879
stormysun513 12:7060e287bdae 880 default:
stormysun513 12:7060e287bdae 881 break;
stormysun513 12:7060e287bdae 882 }
stormysun513 12:7060e287bdae 883 }