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:
Tue Feb 23 14:49:05 2016 +0000
Revision:
13:65c9b7d667f6
Parent:
12:7060e287bdae
Child:
14:f62d55b7dead
Ketdiary workable version for commanding from central and forwarding data from nRF51822

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