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:
Mon Feb 22 08:50:39 2016 +0000
Revision:
12:7060e287bdae
Parent:
11:b3929de96933
Child:
13:65c9b7d667f6
Workable version 1

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 12:7060e287bdae 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 12:7060e287bdae 196
stormysun513 11:b3929de96933 197 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 12:7060e287bdae 198 pButton = new InterruptIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 199 pButton->fall(buttonInterruptServiceRoutine);
stormysun513 12:7060e287bdae 200
stormysun513 12:7060e287bdae 201 ble.init(bleInitCompleteCB);
stormysun513 12:7060e287bdae 202 ble.onConnection(bleOnConnectionCB);
stormysun513 12:7060e287bdae 203 ble.onDisconnection(bleDisconnectionCB);
stormysun513 12:7060e287bdae 204 ble.onDataWritten(bleCharacteristicWrittenCB);
stormysun513 12:7060e287bdae 205
stormysun513 12:7060e287bdae 206 // NVIC_SetPriority(RADIO_IRQn, 0);
stormysun513 12:7060e287bdae 207 // NVIC_SetPriority(UART0_IRQn, 1);
stormysun513 12:7060e287bdae 208 // NVIC_SetPriority(RTC1_IRQn, 2);
stormysun513 12:7060e287bdae 209
stormysun513 12:7060e287bdae 210 // setup advertising // Restricted to 31 bytes
stormysun513 12:7060e287bdae 211 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
stormysun513 12:7060e287bdae 212 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
stormysun513 12:7060e287bdae 213 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"BLETest", sizeof("BLETest") - 1);
stormysun513 12:7060e287bdae 214 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
stormysun513 12:7060e287bdae 215 ble.gap().setDeviceName("BLETest");
stormysun513 12:7060e287bdae 216
stormysun513 12:7060e287bdae 217 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)"myble2", sizeof("myble2") - 1);
stormysun513 12:7060e287bdae 218 ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms.
stormysun513 12:7060e287bdae 219 ble.addService(uartService);
stormysun513 12:7060e287bdae 220
stormysun513 12:7060e287bdae 221 ble.startAdvertising();
stormysun513 11:b3929de96933 222
stormysun513 11:b3929de96933 223 while(true){
stormysun513 12:7060e287bdae 224
stormysun513 12:7060e287bdae 225 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 226 if(disConnIdleCnt >= MAX_DISCONN_IDLE && systemState == SYS_RELAXING_BUTTON){
stormysun513 12:7060e287bdae 227 shutdownDevice();
stormysun513 12:7060e287bdae 228 }
stormysun513 12:7060e287bdae 229 else if(connIdleCnt >= MAX_CONN_IDLE){
stormysun513 12:7060e287bdae 230 if(bleHandle != 0)
stormysun513 12:7060e287bdae 231 ble.disconnect(bleHandle, Gap::CONNECTION_TIMEOUT);
stormysun513 12:7060e287bdae 232 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 233 }
stormysun513 12:7060e287bdae 234 #endif
stormysun513 12:7060e287bdae 235
stormysun513 12:7060e287bdae 236 switch (systemState) {
stormysun513 12:7060e287bdae 237 case SYS_POWERSAVING:
stormysun513 12:7060e287bdae 238 case SYS_RELAXING_BUTTON:
stormysun513 12:7060e287bdae 239 switch (buttonState) {
stormysun513 12:7060e287bdae 240 case BTN_CHANGE_ANALOG:
stormysun513 12:7060e287bdae 241 changeButtonPinToAnalogIn();
stormysun513 12:7060e287bdae 242 break;
stormysun513 12:7060e287bdae 243 case BTN_CHECK_LONG_PRESS:
stormysun513 12:7060e287bdae 244 buttonCheckLongPress(checkButtonCounter);
stormysun513 12:7060e287bdae 245 break;
stormysun513 12:7060e287bdae 246 case BTN_IDLE:
stormysun513 12:7060e287bdae 247 default:
stormysun513 12:7060e287bdae 248 break;
stormysun513 12:7060e287bdae 249 }
stormysun513 12:7060e287bdae 250 break;
stormysun513 12:7060e287bdae 251 case SYS_LOCKING_BUTTON:
stormysun513 12:7060e287bdae 252 buttonState = BTN_IDLE;
stormysun513 12:7060e287bdae 253 break;
stormysun513 12:7060e287bdae 254 }
stormysun513 12:7060e287bdae 255
stormysun513 12:7060e287bdae 256 if(isCheckBuf){
stormysun513 12:7060e287bdae 257 isCheckBuf = false;
stormysun513 12:7060e287bdae 258 timerCheckBuffer();
stormysun513 12:7060e287bdae 259 }
stormysun513 12:7060e287bdae 260
stormysun513 12:7060e287bdae 261 if(isRoutine){
stormysun513 12:7060e287bdae 262 isRoutine = false;
stormysun513 12:7060e287bdae 263 timerRoutineTasks();
stormysun513 12:7060e287bdae 264 }
stormysun513 12:7060e287bdae 265
stormysun513 12:7060e287bdae 266 if( isEnterMainLoop ){
stormysun513 12:7060e287bdae 267 switch (stm32State){
stormysun513 12:7060e287bdae 268 case STM32_IMAGE_FORWARDING:{
stormysun513 12:7060e287bdae 269 if (setImgPktPayloadByID == false){
stormysun513 12:7060e287bdae 270 uint8_t check = 0;
stormysun513 12:7060e287bdae 271 RingBuffer_read(&ringBuffer, &check, 1);
stormysun513 12:7060e287bdae 272 HeaderTypeDef header = static_cast<HeaderTypeDef>(check);
stormysun513 12:7060e287bdae 273 if (header == SYS_HEADER_IMAGE_PACKET){
stormysun513 12:7060e287bdae 274 if (currentPacketIndex != (packetNum-1))
stormysun513 12:7060e287bdae 275 targetPayload = UART_PACKET_SIZE + 3;
stormysun513 12:7060e287bdae 276 else
stormysun513 12:7060e287bdae 277 targetPayload = lastPacketSize + 3;
stormysun513 12:7060e287bdae 278
stormysun513 12:7060e287bdae 279 alreadyTransferred = 0;
stormysun513 12:7060e287bdae 280 setImgPktPayloadByID = true;
stormysun513 12:7060e287bdae 281 waitCamera = 0;
stormysun513 12:7060e287bdae 282 }
stormysun513 12:7060e287bdae 283 }
stormysun513 12:7060e287bdae 284 else {
stormysun513 12:7060e287bdae 285 uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer);
stormysun513 12:7060e287bdae 286 while((numBytes >= (BLE_BUF_LEN-1)) || (directForward == true)){
stormysun513 12:7060e287bdae 287 waitCamera = 0;
stormysun513 12:7060e287bdae 288 uint16_t diff = numBytes;
stormysun513 12:7060e287bdae 289 if(diff > (BLE_BUF_LEN-1))
stormysun513 12:7060e287bdae 290 diff = (BLE_BUF_LEN-1);
stormysun513 12:7060e287bdae 291 uartRxPayload[0] = static_cast<uint8_t>(SYS_HEADER_IMAGE_PACKET);
stormysun513 12:7060e287bdae 292
stormysun513 12:7060e287bdae 293 RingBuffer_read(&ringBuffer, uartRxPayload+1, diff);
stormysun513 12:7060e287bdae 294
stormysun513 12:7060e287bdae 295 if(mHandle != 0){
stormysun513 12:7060e287bdae 296 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), uartRxPayload, diff+1);
stormysun513 12:7060e287bdae 297 alreadyTransferred += diff;
stormysun513 12:7060e287bdae 298 numBytes -= diff;
stormysun513 12:7060e287bdae 299 }
stormysun513 12:7060e287bdae 300
stormysun513 12:7060e287bdae 301 if(directForward == true || alreadyTransferred >= targetPayload){
stormysun513 12:7060e287bdae 302 __disable_irq(); // Disable Interrupts
stormysun513 12:7060e287bdae 303 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 304 __enable_irq();
stormysun513 12:7060e287bdae 305 directForward = false;
stormysun513 12:7060e287bdae 306 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 307
stormysun513 12:7060e287bdae 308 if(isRetransmit == false){
stormysun513 12:7060e287bdae 309 currentPacketIndex++;
stormysun513 12:7060e287bdae 310 }
stormysun513 12:7060e287bdae 311 else{
stormysun513 12:7060e287bdae 312 retransmitIndex++;
stormysun513 12:7060e287bdae 313 if(retransmitIndex >= retransmitSize){
stormysun513 12:7060e287bdae 314 retransmitSize = 0;
stormysun513 12:7060e287bdae 315 resetSTM32StateType();
stormysun513 12:7060e287bdae 316 break;
stormysun513 12:7060e287bdae 317 }
stormysun513 12:7060e287bdae 318 else{
stormysun513 12:7060e287bdae 319 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 320 }
stormysun513 12:7060e287bdae 321 }
stormysun513 12:7060e287bdae 322
stormysun513 12:7060e287bdae 323 if(currentPacketIndex == packetNum && isRetransmit == false){
stormysun513 12:7060e287bdae 324 /* TODO retransmission */
stormysun513 12:7060e287bdae 325 isRetransmit = true;
stormysun513 12:7060e287bdae 326 if(retransmitSize > 0){
stormysun513 12:7060e287bdae 327 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 328 retransmitIndex = 0;
stormysun513 12:7060e287bdae 329 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 330 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 331 }
stormysun513 12:7060e287bdae 332 else{
stormysun513 12:7060e287bdae 333 resetSTM32StateType();
stormysun513 12:7060e287bdae 334 }
stormysun513 12:7060e287bdae 335 }
stormysun513 12:7060e287bdae 336 else{
stormysun513 12:7060e287bdae 337 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 338 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 339 }
stormysun513 12:7060e287bdae 340 break;
stormysun513 12:7060e287bdae 341 }
stormysun513 12:7060e287bdae 342 }
stormysun513 12:7060e287bdae 343 }
stormysun513 12:7060e287bdae 344 break;
stormysun513 12:7060e287bdae 345 }
stormysun513 12:7060e287bdae 346 case STM32_CHECK_HEADER:{
stormysun513 12:7060e287bdae 347 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 348 HeaderTypeDef header;
stormysun513 12:7060e287bdae 349 switch (waitHeader) {
stormysun513 12:7060e287bdae 350 /* Image info */
stormysun513 12:7060e287bdae 351 case SYS_HEADER_IMAGE_INFO:
stormysun513 12:7060e287bdae 352 RingBuffer_read(&ringBuffer, buf, 1);
stormysun513 12:7060e287bdae 353 header = static_cast<HeaderTypeDef>(buf[0]);
stormysun513 12:7060e287bdae 354 if (header == SYS_HEADER_IMAGE_INFO){
stormysun513 12:7060e287bdae 355 RingBuffer_read(&ringBuffer, buf+1, SINGLE_UINT16_PAYLOAD-1);
stormysun513 12:7060e287bdae 356 if(mHandle != 0)
stormysun513 12:7060e287bdae 357 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 12:7060e287bdae 358
stormysun513 12:7060e287bdae 359 uint16_t totalDataLength = buf[1]*256 + buf[2];
stormysun513 12:7060e287bdae 360 packetNum = totalDataLength/UART_PACKET_SIZE;
stormysun513 12:7060e287bdae 361 lastPacketSize = totalDataLength%UART_PACKET_SIZE;
stormysun513 12:7060e287bdae 362 if(totalDataLength%UART_PACKET_SIZE != 0)
stormysun513 12:7060e287bdae 363 packetNum++;
stormysun513 12:7060e287bdae 364 else
stormysun513 12:7060e287bdae 365 lastPacketSize = UART_PACKET_SIZE;
stormysun513 12:7060e287bdae 366
stormysun513 12:7060e287bdae 367 currentPacketIndex = 0;
stormysun513 12:7060e287bdae 368 resetSTM32StateType();
stormysun513 12:7060e287bdae 369 }
stormysun513 12:7060e287bdae 370 break;
stormysun513 12:7060e287bdae 371 case SYS_HEADER_SALIVA_VOLTAGE:
stormysun513 12:7060e287bdae 372 RingBuffer_read(&ringBuffer, buf, 1);
stormysun513 12:7060e287bdae 373 header = static_cast<HeaderTypeDef>(buf[0]);
stormysun513 12:7060e287bdae 374 if (header == SYS_HEADER_SALIVA_VOLTAGE){
stormysun513 12:7060e287bdae 375 RingBuffer_read(&ringBuffer, buf+1, SINGLE_UINT16_PAYLOAD-1);
stormysun513 12:7060e287bdae 376 if(mHandle != 0)
stormysun513 12:7060e287bdae 377 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 12:7060e287bdae 378
stormysun513 12:7060e287bdae 379 stm32State = STM32_CHECK_HEADER;
stormysun513 12:7060e287bdae 380 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 381 }
stormysun513 12:7060e287bdae 382 break;
stormysun513 12:7060e287bdae 383 case SYS_HEADER_NO_SPECIFIC:
stormysun513 12:7060e287bdae 384 default:
stormysun513 12:7060e287bdae 385 break;
stormysun513 12:7060e287bdae 386 }
stormysun513 12:7060e287bdae 387 break;
stormysun513 12:7060e287bdae 388 }
stormysun513 12:7060e287bdae 389 case STM32_IDLE:
stormysun513 12:7060e287bdae 390 default:
stormysun513 12:7060e287bdae 391 break;
stormysun513 12:7060e287bdae 392 }
stormysun513 12:7060e287bdae 393 isEnterMainLoop = false;
stormysun513 12:7060e287bdae 394 }
stormysun513 12:7060e287bdae 395 ble.waitForEvent();
stevep 4:81cea7a352b0 396 }
dan 0:7dec7e9ac085 397 }
stormysun513 11:b3929de96933 398
stormysun513 12:7060e287bdae 399 void bleInitCompleteCB(struct BLE::InitializationCompleteCallbackContext *context){
stormysun513 12:7060e287bdae 400 if (context->error != BLE_ERROR_NONE)
stormysun513 12:7060e287bdae 401 return;
stormysun513 12:7060e287bdae 402
stormysun513 12:7060e287bdae 403 if (context->ble.getInstanceID() == BLE::DEFAULT_INSTANCE){
stormysun513 12:7060e287bdae 404 /* use the BLE instance */
stormysun513 12:7060e287bdae 405 return;
stormysun513 12:7060e287bdae 406 }
stormysun513 12:7060e287bdae 407 }
stormysun513 12:7060e287bdae 408
stormysun513 12:7060e287bdae 409 void bleOnConnectionCB(const Gap::ConnectionCallbackParams_t *p_conn_param){
stormysun513 12:7060e287bdae 410 mHandle = p_conn_param->handle;
stormysun513 12:7060e287bdae 411 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 412 connIdleCnt = 0;
stormysun513 12:7060e287bdae 413 #endif
stormysun513 12:7060e287bdae 414 bleReduceConnInterval();
stormysun513 12:7060e287bdae 415 }
stormysun513 12:7060e287bdae 416
stormysun513 12:7060e287bdae 417 void bleDisconnectionCB(const Gap::DisconnectionCallbackParams_t *params){
stormysun513 12:7060e287bdae 418 mHandle = 0;
stormysun513 12:7060e287bdae 419 if(systemState != SYS_POWERSAVING){
stormysun513 12:7060e287bdae 420 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 421 disConnIdleCnt = 0;
stormysun513 12:7060e287bdae 422 #endif
stormysun513 12:7060e287bdae 423 ble.startAdvertising();
stormysun513 12:7060e287bdae 424 }
stormysun513 12:7060e287bdae 425 }
stormysun513 12:7060e287bdae 426
stormysun513 12:7060e287bdae 427 void bleCharacteristicWrittenCB(const GattWriteCallbackParams *Handler){
stormysun513 12:7060e287bdae 428 uint16_t bleBytesRead;
stormysun513 12:7060e287bdae 429 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
stormysun513 12:7060e287bdae 430 {
stormysun513 12:7060e287bdae 431 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 432 connIdleCnt = 0;
stormysun513 12:7060e287bdae 433 #endif
stormysun513 12:7060e287bdae 434 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), uartTxPayload, &bleBytesRead);
stormysun513 12:7060e287bdae 435 if(bleBytesRead == 0)
stormysun513 12:7060e287bdae 436 return;
stormysun513 12:7060e287bdae 437
stormysun513 12:7060e287bdae 438 BLECommandTypeDef command = static_cast<BLECommandTypeDef>(uartTxPayload[0]);
stormysun513 12:7060e287bdae 439 bool isUARTCommand = false;
stormysun513 12:7060e287bdae 440 switch(command){
stormysun513 12:7060e287bdae 441 case BLE_SWITCH_NOTIFICATION_MODE:
stormysun513 12:7060e287bdae 442 infoNotification = !infoNotification;
stormysun513 12:7060e287bdae 443 break;
stormysun513 12:7060e287bdae 444 case BLE_REQUEST_SALIVA_VOLTAGE:
stormysun513 12:7060e287bdae 445 waitHeader = SYS_HEADER_SALIVA_VOLTAGE;
stormysun513 12:7060e287bdae 446 salivaPeriodically = true;
stormysun513 12:7060e287bdae 447 systemState = SYS_LOCKING_BUTTON; // Lock Begin
stormysun513 12:7060e287bdae 448 break;
stormysun513 12:7060e287bdae 449 case BLE_DEREQUEST_SALIVA_VOLTAGE:
stormysun513 12:7060e287bdae 450 salivaPeriodically = false;
stormysun513 12:7060e287bdae 451 systemState = SYS_RELAXING_BUTTON; // for debug (Lock end)
stormysun513 12:7060e287bdae 452 break;
stormysun513 12:7060e287bdae 453 case BLE_REQUEST_IMAGE_INFO:
stormysun513 12:7060e287bdae 454 waitHeader = SYS_HEADER_IMAGE_INFO;
stormysun513 12:7060e287bdae 455 isUARTCommand = true;
stormysun513 12:7060e287bdae 456 salivaPeriodically = false;
stormysun513 12:7060e287bdae 457 break;
stormysun513 12:7060e287bdae 458 case BLE_END_IMAGE_TRANSFER:
stormysun513 12:7060e287bdae 459 isRetransmit = false;
stormysun513 12:7060e287bdae 460 packetNum = 0;
stormysun513 12:7060e287bdae 461 resetSTM32StateType();
stormysun513 12:7060e287bdae 462 break;
stormysun513 12:7060e287bdae 463 case BLE_SHUTDOWN:
stormysun513 12:7060e287bdae 464 changeButtonPinToInterrupt();
stormysun513 12:7060e287bdae 465 shutdownDevice();
stormysun513 12:7060e287bdae 466 break;
stormysun513 12:7060e287bdae 467 case BLE_UNLOCK_BUTTON:
stormysun513 12:7060e287bdae 468 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 469 break;
stormysun513 12:7060e287bdae 470 case BLE_UART_DEBUG:{
stormysun513 12:7060e287bdae 471 uint8_t debugHeader = 0xF1;
stormysun513 12:7060e287bdae 472 uartTxTransmit(&debugHeader, 1);
stormysun513 12:7060e287bdae 473 break;
stormysun513 12:7060e287bdae 474 }
stormysun513 12:7060e287bdae 475 case BLE_REQUEST_IMAGE_BY_INDEX:
stormysun513 12:7060e287bdae 476 if(bleBytesRead >= 2){
stormysun513 12:7060e287bdae 477 salivaPeriodically = false;
stormysun513 12:7060e287bdae 478 uint8_t check = uartTxPayload[1];
stormysun513 12:7060e287bdae 479 if(check == 0){
stormysun513 12:7060e287bdae 480 if(packetNum > 0){
stormysun513 12:7060e287bdae 481 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 482 directForward = false;
stormysun513 12:7060e287bdae 483 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 484 currentPacketIndex = 0;
stormysun513 12:7060e287bdae 485 isRetransmit = false;
stormysun513 12:7060e287bdae 486 retransmitSize = 0;
stormysun513 12:7060e287bdae 487 stm32State = STM32_IMAGE_FORWARDING;
stormysun513 12:7060e287bdae 488 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 489 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 490 }
stormysun513 12:7060e287bdae 491 }
stormysun513 12:7060e287bdae 492 else if(check <= 18){
stormysun513 12:7060e287bdae 493 if(bleBytesRead < check+2)
stormysun513 12:7060e287bdae 494 break;
stormysun513 12:7060e287bdae 495 retransmitSize = check;
stormysun513 12:7060e287bdae 496 memcpy(retransmitIndices, uartTxPayload+2, retransmitSize);
stormysun513 12:7060e287bdae 497 if(isRetransmit == true){
stormysun513 12:7060e287bdae 498 stm32State = STM32_IMAGE_FORWARDING;
stormysun513 12:7060e287bdae 499 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 500 retransmitIndex = 0;
stormysun513 12:7060e287bdae 501 currentPacketIndex = retransmitIndices[retransmitIndex];
stormysun513 12:7060e287bdae 502 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 503 }
stormysun513 12:7060e287bdae 504 else{
stormysun513 12:7060e287bdae 505 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 506 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 507 }
stormysun513 12:7060e287bdae 508 }
stormysun513 12:7060e287bdae 509 else{
stormysun513 12:7060e287bdae 510 /* Invalid check number */
stormysun513 12:7060e287bdae 511 }
stormysun513 12:7060e287bdae 512 }
stormysun513 12:7060e287bdae 513 break;
stormysun513 12:7060e287bdae 514 case BLE_CHANGE_DEVICE_ID:
stormysun513 12:7060e287bdae 515 if(bleBytesRead >= 3)
stormysun513 12:7060e287bdae 516 writeAT24EEPROMBuffer(*pI2C, 0, (char*)uartTxPayload+1, 2);
stormysun513 12:7060e287bdae 517 break;
stormysun513 12:7060e287bdae 518 default:
stormysun513 12:7060e287bdae 519 break;
stormysun513 12:7060e287bdae 520 }
stormysun513 12:7060e287bdae 521 if(isUARTCommand){
stormysun513 12:7060e287bdae 522 stm32State = STM32_CHECK_HEADER;
stormysun513 12:7060e287bdae 523 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 524 uartTxTransmit(uartTxPayload, 1);
stormysun513 12:7060e287bdae 525 }
stormysun513 12:7060e287bdae 526 }
stormysun513 12:7060e287bdae 527 }
stormysun513 12:7060e287bdae 528
stormysun513 12:7060e287bdae 529 void bleReduceConnInterval(void){
stormysun513 12:7060e287bdae 530 Gap::ConnectionParams_t bleGapConnParamInitStruct;
stormysun513 12:7060e287bdae 531 bleGapConnParamInitStruct.minConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/
stormysun513 12:7060e287bdae 532 bleGapConnParamInitStruct.maxConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/
stormysun513 12:7060e287bdae 533 bleGapConnParamInitStruct.slaveLatency = 0; /*Slave Latency in number of connection events*/
stormysun513 12:7060e287bdae 534 bleGapConnParamInitStruct.connectionSupervisionTimeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; /*Connection Supervision Timeout in 10 ms*/
stormysun513 12:7060e287bdae 535
stormysun513 12:7060e287bdae 536 ble.gap().updateConnectionParams(mHandle, &bleGapConnParamInitStruct);
stormysun513 12:7060e287bdae 537 }
stormysun513 12:7060e287bdae 538
stormysun513 11:b3929de96933 539 void timerPeriodicalCB(void){
stormysun513 11:b3929de96933 540 static uint32_t time = 0;
stormysun513 11:b3929de96933 541 time++;
stormysun513 11:b3929de96933 542
stormysun513 11:b3929de96933 543 if ( time % 100 == 2 ) { // 250ms
stormysun513 12:7060e287bdae 544 if(buttonState == BTN_CHECK_LONG_PRESS)
stormysun513 12:7060e287bdae 545 checkButtonCounter++;
stormysun513 11:b3929de96933 546 }
stormysun513 11:b3929de96933 547
stormysun513 12:7060e287bdae 548 if(systemState != SYS_POWERSAVING){
stormysun513 12:7060e287bdae 549 if ( time % 3 == 1 ) { // 7.5ms
stormysun513 12:7060e287bdae 550 isCheckBuf = true;
stormysun513 12:7060e287bdae 551 }
stormysun513 12:7060e287bdae 552
stormysun513 12:7060e287bdae 553 if ( time % 50 == 2 ) { // 250ms
stormysun513 12:7060e287bdae 554 if(isInserted)
stormysun513 12:7060e287bdae 555 timerFlashLed();
stormysun513 12:7060e287bdae 556 }
stormysun513 12:7060e287bdae 557
stormysun513 12:7060e287bdae 558 if ( time % 200 == 3 ) { // 500ms
stormysun513 12:7060e287bdae 559 if(!isInserted)
stormysun513 12:7060e287bdae 560 timerFlashLed();
stormysun513 12:7060e287bdae 561 }
stormysun513 12:7060e287bdae 562
stormysun513 12:7060e287bdae 563 if ( time % 400 == 4 ) { // 1s
stormysun513 12:7060e287bdae 564 isRoutine = true;
stormysun513 12:7060e287bdae 565 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 566 if(bleHandle == 0)
stormysun513 12:7060e287bdae 567 disConnIdleCnt++;
stormysun513 12:7060e287bdae 568 else
stormysun513 12:7060e287bdae 569 connIdleCnt++;
stormysun513 12:7060e287bdae 570 #endif
stormysun513 12:7060e287bdae 571 }
stormysun513 12:7060e287bdae 572
stormysun513 12:7060e287bdae 573 if ( time % 800 == 5 ) {
stormysun513 12:7060e287bdae 574 if(salivaPeriodically == true)
stormysun513 12:7060e287bdae 575 requestSalivaVoltage();
stormysun513 12:7060e287bdae 576 }
stormysun513 11:b3929de96933 577 }
stormysun513 11:b3929de96933 578 }
stormysun513 11:b3929de96933 579
stormysun513 11:b3929de96933 580 void timerFlashLed(void){
stormysun513 12:7060e287bdae 581 ledPower = !ledPower;
stormysun513 11:b3929de96933 582 }
stormysun513 11:b3929de96933 583
stormysun513 12:7060e287bdae 584 void timerRoutineTasks(void){
stormysun513 11:b3929de96933 585 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 586 buf[0] = SYS_HEADER_DEVICE_INFO;
stormysun513 12:7060e287bdae 587 bool result = readAT24EEPROMBuffer(*pI2C, 0, (char*)buf+1, 2);
stormysun513 11:b3929de96933 588
stormysun513 11:b3929de96933 589 if(result == false){
stormysun513 12:7060e287bdae 590 pLedInsertion->write(1);
stormysun513 12:7060e287bdae 591 isInserted = false;
stormysun513 11:b3929de96933 592 buf[1] = 0xFF;
stormysun513 11:b3929de96933 593 buf[2] = 0xFF;
stormysun513 11:b3929de96933 594 }
stormysun513 11:b3929de96933 595 else{
stormysun513 12:7060e287bdae 596 isInserted = true;
stormysun513 12:7060e287bdae 597 pLedInsertion->write(0);
stormysun513 12:7060e287bdae 598 }
stormysun513 12:7060e287bdae 599
stormysun513 12:7060e287bdae 600 float fvalue = (float)aniBatterVolt.read()*3.3*100;
stormysun513 12:7060e287bdae 601 uint16_t ivalue = (uint16_t)fvalue;
stormysun513 12:7060e287bdae 602 buf[3] = (ivalue >> 8) & 0xFF;
stormysun513 12:7060e287bdae 603 buf[4] = ivalue & 0xFF;
stormysun513 12:7060e287bdae 604
stormysun513 12:7060e287bdae 605 // if(mHandle != 0 && infoNotification == true){
stormysun513 12:7060e287bdae 606 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, DEVICE_INFO_PAYLOAD);
stormysun513 12:7060e287bdae 607 // }
stormysun513 12:7060e287bdae 608
stormysun513 12:7060e287bdae 609 if(mHandle != 0 && infoNotification == true){
stormysun513 12:7060e287bdae 610 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, DEVICE_INFO_PAYLOAD);
stormysun513 12:7060e287bdae 611 }
stormysun513 12:7060e287bdae 612 }
stormysun513 12:7060e287bdae 613
stormysun513 12:7060e287bdae 614 void uartTxTransmit(uint8_t* buf, uint16_t length){
stormysun513 12:7060e287bdae 615 uint16_t index;
stormysun513 12:7060e287bdae 616 pc.putc(preamble[0]);
stormysun513 12:7060e287bdae 617 pc.putc(preamble[1]);
stormysun513 12:7060e287bdae 618 for(index = 0; index < length; index++)
stormysun513 12:7060e287bdae 619 {
stormysun513 12:7060e287bdae 620 pc.putc(buf[index]);
stormysun513 12:7060e287bdae 621 }
stormysun513 12:7060e287bdae 622 }
stormysun513 12:7060e287bdae 623
stormysun513 12:7060e287bdae 624 // UARTRX Callback function
stormysun513 12:7060e287bdae 625 void uartRxCB(void){
stormysun513 12:7060e287bdae 626 while(pc.readable())
stormysun513 12:7060e287bdae 627 {
stormysun513 12:7060e287bdae 628 uint8_t byteIn = pc.getc();
stormysun513 12:7060e287bdae 629 if(stm32State == STM32_IDLE)
stormysun513 12:7060e287bdae 630 continue;
stormysun513 12:7060e287bdae 631
stormysun513 12:7060e287bdae 632 if(uartRxState == UARTRX_TRANSFERING_DATA){
stormysun513 12:7060e287bdae 633 RingBuffer_writebyte(&ringBuffer, byteIn);
stormysun513 12:7060e287bdae 634 }
stormysun513 12:7060e287bdae 635 else if(uartRxState == UARTRX_CHECK_FIRST_PREAMBLE && byteIn == preamble[0]){
stormysun513 12:7060e287bdae 636 uartRxState = UARTRX_CHECK_SECOND_PREAMBLE;
stormysun513 12:7060e287bdae 637 }
stormysun513 12:7060e287bdae 638 else if(uartRxState == UARTRX_CHECK_SECOND_PREAMBLE && byteIn == preamble[1]){
stormysun513 12:7060e287bdae 639 uartRxState = UARTRX_TRANSFERING_DATA;
stormysun513 12:7060e287bdae 640 }
stormysun513 12:7060e287bdae 641 }
stormysun513 12:7060e287bdae 642 }
stormysun513 12:7060e287bdae 643
stormysun513 12:7060e287bdae 644 void buttonInterruptServiceRoutine(void){
stormysun513 12:7060e287bdae 645 if(buttonState == BTN_IDLE)
stormysun513 12:7060e287bdae 646 buttonState = BTN_CHANGE_ANALOG;
stormysun513 12:7060e287bdae 647
stormysun513 12:7060e287bdae 648 if(systemState == SYS_POWERSAVING)
stormysun513 12:7060e287bdae 649 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 12:7060e287bdae 650 }
stormysun513 12:7060e287bdae 651
stormysun513 12:7060e287bdae 652 void buttonCheckLongPress(uint8_t counter){
stormysun513 12:7060e287bdae 653 if(counter < CHECK_BUTTON_TIMES){
stormysun513 12:7060e287bdae 654 if(pButtonAnalogIn != NULL && (float)pButtonAnalogIn->read() < CHECK_BUTTON_VOLTAGE_LEVEL){
stormysun513 12:7060e287bdae 655 return;
stormysun513 12:7060e287bdae 656 }
stormysun513 12:7060e287bdae 657 else if(systemState == SYS_POWERSAVING){
stormysun513 12:7060e287bdae 658 ticker.detach();
stormysun513 12:7060e287bdae 659 }
stormysun513 12:7060e287bdae 660 }
stormysun513 12:7060e287bdae 661 else{
stormysun513 12:7060e287bdae 662 buttonLongPressVerified();
stormysun513 12:7060e287bdae 663 }
stormysun513 12:7060e287bdae 664 changeButtonPinToInterrupt();
stormysun513 12:7060e287bdae 665 }
stormysun513 12:7060e287bdae 666
stormysun513 12:7060e287bdae 667 void buttonLongPressVerified(void){
stormysun513 12:7060e287bdae 668 checkButtonCounter = 0;
stormysun513 12:7060e287bdae 669 if(systemState == SYS_POWERSAVING){
stormysun513 12:7060e287bdae 670 systemState = SYS_RELAXING_BUTTON;
stormysun513 12:7060e287bdae 671 ble.startAdvertising();
stormysun513 12:7060e287bdae 672 #ifdef APPLICATION_TIMEOUT
stormysun513 12:7060e287bdae 673 disConnIdleCnt = 0;
stormysun513 12:7060e287bdae 674 #endif
stormysun513 12:7060e287bdae 675 ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD);
stormysun513 12:7060e287bdae 676 resetSTM32StateType();
stormysun513 12:7060e287bdae 677 }
stormysun513 12:7060e287bdae 678 else if(systemState == SYS_RELAXING_BUTTON){
stormysun513 12:7060e287bdae 679 shutdownDevice();
stormysun513 12:7060e287bdae 680 }
stormysun513 12:7060e287bdae 681 }
stormysun513 12:7060e287bdae 682
stormysun513 12:7060e287bdae 683 void changeButtonPinToAnalogIn(void){
stormysun513 12:7060e287bdae 684 pButton->fall(NULL);
stormysun513 12:7060e287bdae 685 delete pButton;
stormysun513 12:7060e287bdae 686 checkButtonCounter = 0;
stormysun513 12:7060e287bdae 687 pButtonAnalogIn = new AnalogIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 688 buttonState = BTN_CHECK_LONG_PRESS;
stormysun513 12:7060e287bdae 689 }
stormysun513 12:7060e287bdae 690
stormysun513 12:7060e287bdae 691 void changeButtonPinToInterrupt(void){
stormysun513 12:7060e287bdae 692 delete pButtonAnalogIn;
stormysun513 12:7060e287bdae 693 pButton = new InterruptIn(BTN_POWER_ON_PIN);
stormysun513 12:7060e287bdae 694 pButton->fall(buttonInterruptServiceRoutine);
stormysun513 12:7060e287bdae 695 float dummy = (float)pButtonAnalogIn->read();
stormysun513 12:7060e287bdae 696 buttonState = BTN_IDLE;
stormysun513 11:b3929de96933 697 }
stormysun513 12:7060e287bdae 698
stormysun513 12:7060e287bdae 699 void shutdownDevice(void){
stormysun513 12:7060e287bdae 700 systemState = SYS_POWERSAVING;
stormysun513 12:7060e287bdae 701 ble.gap().stopAdvertising();
stormysun513 12:7060e287bdae 702 ledPower = 1;
stormysun513 12:7060e287bdae 703 pLedInsertion->write(1);
stormysun513 12:7060e287bdae 704 ticker.detach();
stormysun513 12:7060e287bdae 705 if(mHandle != 0)
stormysun513 12:7060e287bdae 706 ble.disconnect(mHandle, Gap::REMOTE_USER_TERMINATED_CONNECTION);
stormysun513 12:7060e287bdae 707 deepsleep();
stormysun513 12:7060e287bdae 708 }
stormysun513 12:7060e287bdae 709
stormysun513 12:7060e287bdae 710 void resetSTM32StateType(void){
stormysun513 12:7060e287bdae 711 stm32State = STM32_IDLE;
stormysun513 12:7060e287bdae 712 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 713 waitHeader = SYS_HEADER_NO_SPECIFIC;
stormysun513 12:7060e287bdae 714 }
stormysun513 12:7060e287bdae 715
stormysun513 12:7060e287bdae 716 void requestSalivaVoltage(void){
stormysun513 12:7060e287bdae 717 uint8_t buf[BLE_BUF_LEN] = {0,};
stormysun513 12:7060e287bdae 718 float fvalue = aniSalivaVolt.read()*3.3*100;
stormysun513 12:7060e287bdae 719 uint16_t ivalue = (uint16_t)fvalue;
stormysun513 12:7060e287bdae 720
stormysun513 12:7060e287bdae 721 buf[0] = SYS_HEADER_SALIVA_VOLTAGE;
stormysun513 12:7060e287bdae 722 buf[1] = (ivalue >> 8) & 0xFF;
stormysun513 12:7060e287bdae 723 buf[2] = ivalue & 0xFF;
stormysun513 12:7060e287bdae 724
stormysun513 12:7060e287bdae 725 if(mHandle != 0){
stormysun513 12:7060e287bdae 726 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD);
stormysun513 12:7060e287bdae 727 }
stormysun513 12:7060e287bdae 728 }
stormysun513 12:7060e287bdae 729
stormysun513 12:7060e287bdae 730 void timerCheckBuffer(void){
stormysun513 12:7060e287bdae 731 uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer);
stormysun513 12:7060e287bdae 732 switch (stm32State) {
stormysun513 12:7060e287bdae 733 case STM32_CHECK_HEADER:{
stormysun513 12:7060e287bdae 734 if (numBytes >= SINGLE_UINT16_PAYLOAD){
stormysun513 12:7060e287bdae 735 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 736 }
stormysun513 12:7060e287bdae 737 break;
stormysun513 12:7060e287bdae 738 }
stormysun513 12:7060e287bdae 739 case STM32_IMAGE_FORWARDING:{
stormysun513 12:7060e287bdae 740 if(setImgPktPayloadByID == false){
stormysun513 12:7060e287bdae 741 if(numBytes >= UART_DATA_PACKET_PAYLOAD){
stormysun513 12:7060e287bdae 742 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 743 }
stormysun513 12:7060e287bdae 744 else{
stormysun513 12:7060e287bdae 745 if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){
stormysun513 12:7060e287bdae 746 setImgPktPayloadByID = false;
stormysun513 12:7060e287bdae 747 directForward = false;
stormysun513 12:7060e287bdae 748 RingBuffer_init(&ringBuffer);
stormysun513 12:7060e287bdae 749 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 750 waitCamera = 0;
stormysun513 12:7060e287bdae 751 }
stormysun513 12:7060e287bdae 752 }
stormysun513 12:7060e287bdae 753 }
stormysun513 12:7060e287bdae 754 else {
stormysun513 12:7060e287bdae 755 if((targetPayload-alreadyTransferred) <= (BLE_BUF_LEN-1)){
stormysun513 12:7060e287bdae 756 if(numBytes+alreadyTransferred == targetPayload)
stormysun513 12:7060e287bdae 757 directForward = true;
stormysun513 12:7060e287bdae 758 }
stormysun513 12:7060e287bdae 759
stormysun513 12:7060e287bdae 760 if (numBytes > 0){
stormysun513 12:7060e287bdae 761 isEnterMainLoop = true;
stormysun513 12:7060e287bdae 762 }
stormysun513 12:7060e287bdae 763
stormysun513 12:7060e287bdae 764 else if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){
stormysun513 12:7060e287bdae 765 uartRxState = UARTRX_CHECK_FIRST_PREAMBLE;
stormysun513 12:7060e287bdae 766 requestForImagePacket(currentPacketIndex);
stormysun513 12:7060e287bdae 767 waitCamera = 0;
stormysun513 12:7060e287bdae 768 }
stormysun513 12:7060e287bdae 769
stormysun513 12:7060e287bdae 770 }
stormysun513 12:7060e287bdae 771 break;
stormysun513 12:7060e287bdae 772 }
stormysun513 12:7060e287bdae 773 default:
stormysun513 12:7060e287bdae 774 break;
stormysun513 12:7060e287bdae 775 }
stormysun513 12:7060e287bdae 776 }
stormysun513 12:7060e287bdae 777
stormysun513 12:7060e287bdae 778 void requestForImagePacket(uint16_t packetId){
stormysun513 12:7060e287bdae 779 uint8_t tempBuf[3];
stormysun513 12:7060e287bdae 780 tempBuf[0] = static_cast<uint8_t>(BLE_REQUEST_IMAGE_BY_INDEX);
stormysun513 12:7060e287bdae 781 tempBuf[1] = (packetId >> 8) & 0xFF;
stormysun513 12:7060e287bdae 782 tempBuf[2] = packetId & 0xFF;
stormysun513 12:7060e287bdae 783 uartTxTransmit(tempBuf, 3);
stormysun513 12:7060e287bdae 784 }
stormysun513 12:7060e287bdae 785
stormysun513 12:7060e287bdae 786 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
stormysun513 12:7060e287bdae 787 FlowControl flow_type = (FlowControl)type;
stormysun513 12:7060e287bdae 788 switch(type) {
stormysun513 12:7060e287bdae 789 case RTS:
stormysun513 12:7060e287bdae 790 serial_set_flow_control(&_serial, flow_type, flow1, NC);
stormysun513 12:7060e287bdae 791 break;
stormysun513 12:7060e287bdae 792
stormysun513 12:7060e287bdae 793 case CTS:
stormysun513 12:7060e287bdae 794 serial_set_flow_control(&_serial, flow_type, NC, flow1);
stormysun513 12:7060e287bdae 795 break;
stormysun513 12:7060e287bdae 796
stormysun513 12:7060e287bdae 797 case RTSCTS:
stormysun513 12:7060e287bdae 798 case Disabled:
stormysun513 12:7060e287bdae 799 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
stormysun513 12:7060e287bdae 800 break;
stormysun513 12:7060e287bdae 801
stormysun513 12:7060e287bdae 802 default:
stormysun513 12:7060e287bdae 803 break;
stormysun513 12:7060e287bdae 804 }
stormysun513 12:7060e287bdae 805 }