Default_Castum_AppFOTA4_Working
Dependencies: mbed BLE_API nRF51822
main.cpp
00001 //To load over FOTA set platform to "Nordic nRF51822 FOTA" and use DFU feature of App "nRF Master Control Panel" (Hex file <100K) 00002 //To use normal mbed loading set platform to "Nordic nRF51822" and copy to mbed drive (Hex File >250KB) 00003 00004 /* mbed Microcontroller Library 00005 * Copyright (c) 2006-2015 Paul Russell 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 00020 //============================== 00021 // Build Options for D_PCB 00022 #define D_MKIT 1 // nRF51822-mkit 00023 #define D_EK 2 // nRF51822-EK 00024 #define D_BEACON 3 // nRF51822-Beacon 00025 #define D_ATOMWEAR 4 // Atomwear 00026 00027 //============================== 00028 // Compile Options: 00029 #define D_BLE_UART 1 // Include BLE UART functionality? 00030 #define D_PING_SEC 1 // Ping Rate in Seconds 00031 00032 #define D_PCB D_MKIT // Select target PCB 00033 //#define D_MkitPROBING 1 // Option: Drive pwm on select header signals to aid probing with scope, comment out this line to disable 00034 //#define D_PCB D_EK // Select target PCB 00035 //#define D_PCB D_BEACON // Select target PCB 00036 //#define D_PCB D_ATOMWEAR // Select target PCB 00037 //#define D_AtomPROBING 1 // Option: Drive pwm on select Debug Header signals to support probing with scope (PWM 10%~80%), comment out this line to disable **Disconnect all modules except Main/Debug/Battery** 00038 00039 //============================== 00040 #include "mbed.h" 00041 #include "BLEDevice.h" 00042 00043 //============================== Process PCB Options: 00044 #if D_PCB==D_MKIT 00045 const char *pcDeviceName = "BasitFOTA4r6m"; 00046 const char *pcPingBLE = "m4\n"; // May need "\n" to push BLE Tx 00047 const char *pcPingUART = "u4\n"; 00048 00049 #define DEBUG(...) { printf(__VA_ARGS__); } //UART: p0.08~p0.11, Debug OK over USB Serial 00050 DigitalIn bB1in(BUTTON1); //p0.16 =Button1 00051 DigitalIn bB2in(BUTTON2); //p0.17 =Button2 00052 InterruptIn B1int(BUTTON1); //p0.16 =Button1 00053 InterruptIn B2int(BUTTON2); //p0.17 =Button2 00054 DigitalOut bL1out(LED1); //p0.18 =LED1 00055 DigitalOut bL2out(LED2); //p0.19 =LED2 00056 //PwmOut fL1pwm(LED1); float fL1level = 0.1;//p0.18 =LED1, BrightnessFloat(0.0~1.0) 00057 //PwmOut fL2pwm(LED2); float fL2level = 0.1;//p0.19 =LED2, BrightnessFloat(0.0~1.0) 00058 //AnalogIn adcA(p1); //p0.01 =AIN2 00059 //AnalogIn adcB(p2); //p0.02 =AIN3 00060 00061 #define D_0_LED_TICKER { bL1out=((uTicker1++)&0x01);} // LED Follows Ticker (May also follow a button) 00062 #define D_1_LED_BUTTON { bL1out = !bB1in;} // LED Follows !Button 00063 #define D_2_LED_BUTTON { bL2out = !bB2in;} // LED Follows !Button 00064 #define D_OPERATION_LED "mkit: LED1=Ticker+Button1, LED2=Button2" 00065 00066 #elif D_PCB==D_EK 00067 const char *pcDeviceName = "FOTA4r6e"; 00068 const char *pcPingBLE = "e4\n"; // May need "\n" to push BLE Tx 00069 00070 #define DEBUG(...) {} //Serial Debug not connected 00071 DigitalIn bB1in(BUTTON1); //p0.16 =Button0 00072 DigitalIn bB2in(BUTTON2); //p0.17 =Button1 00073 InterruptIn B1int(BUTTON1); //p0.16 =Button0 00074 InterruptIn B2int(BUTTON2); //p0.17 =Button1 00075 DigitalOut bL1out(LED1); //p0.18 =LED0 00076 DigitalOut bL2out(LED2); //p0.19 =LED1 00077 //PwmOut fL1pwm(LED1); float fL1level = 0.1;//p0.18 =LED0, BrightnessFloat(0.0~1.0) 00078 //PwmOut fL2pwm(LED2); float fL2level = 0.1;//p0.19 =LED1, BrightnessFloat(0.0~1.0) 00079 //AnalogIn adcA(p1); //p0.01 =AIN2 00080 //AnalogIn adcB(p2); //p0.02 =AIN3 00081 00082 #define D_0_LED_TICKER { bL1out=((uTicker1++)&0x01);} // LED Follows Ticker (May also follow a button) 00083 #define D_1_LED_BUTTON { bL1out = !bB1in;} // LED Follows !Button 00084 #define D_2_LED_BUTTON { bL2out = !bB2in;} // LED Follows !Button 00085 #define D_OPERATION_LED "EK: LED0=Ticker+Button0, LED1=Button1" 00086 00087 #elif D_PCB==D_BEACON 00088 const char *pcDeviceName = "FOTA4r6b"; 00089 const char *pcPingBLE = "b4\n"; // May need "\n" to push BLE Tx 00090 00091 #define DEBUG(...) {} //Serial Debug not connected 00092 DigitalIn bB1in(p8); //p0.08 =SW1 00093 DigitalIn bB2in(p18); //p0.18 =SW2 00094 InterruptIn B1int(p8); //p0.08 =SW1 00095 InterruptIn B2int(p18); //p0.18 =SW2 00096 DigitalOut bLRout(p16); //p0.16 =LEDR 00097 DigitalOut bLGout(p12); //p0.12 =LEDG 00098 DigitalOut bLBout(p15); //p0.15 =LEDB 00099 //PwmOut fLRpwm(p16); float fLRlevel = 0.1; //p0.16 =LEDR, BrightnessFloat(0.0~1.0) 00100 //PwmOut fLGpwm(p12); float fLGlevel = 0.1; //p0.12 =LEDG, BrightnessFloat(0.0~1.0) 00101 //PwmOut fLBpwm(p15); float fLBlevel = 0.1; //p0.15 =LEDB, BrightnessFloat(0.0~1.0) 00102 //AnalogIn adcA(p1); //p0.01 =AIN2 00103 //AnalogIn adcB(p2); //p0.02 =AIN3 00104 00105 #define D_0_LED_TICKER { bLRout=((uTicker1++)&0x01);} // LED Follows Ticker (May also follow a button) 00106 #define D_1_LED_BUTTON { bLGout = bB1in;} // LED Follows Button 00107 #define D_2_LED_BUTTON { bLBout = bB2in;} // LED Follows Button 00108 #define D_OPERATION_LED "Beacon: Red=Ticker, Green=SW1, Blue=SW2" 00109 00110 #elif D_PCB==D_ATOMWEAR // USBSerial=None, ButtonLED=atomwear, LEDx=Buttonx, LEDw=Buttony|Ticker 00111 const char *pcDeviceName = "FOTA4r6a"; 00112 const char *pcPingBLE = "a\n"; // May need "\n" to push BLE Tx 00113 00114 #define DEBUG(...) {} //Serial Debug not connected 00115 DigitalIn bB1in(p29); //p0.29 =S1 00116 InterruptIn B1int(p29); //p0.29 =S1 00117 DigitalOut bLBout(p17); //p0.17 =LEDB 00118 DigitalOut bLWout(p18); //p0.18 =LEDW 00119 //PwmOut fLBpwm(p17); float fLBlevel = 0.1; //p0.17 =LEDB, BrightnessFloat(0.0~1.0) 00120 //PwmOut fLWpwm(p18); float fLWlevel = 0.1; //p0.18 =LEDW, BrightnessFloat(0.0~1.0) 00121 //AnalogIn adcA(p1); //p0.01 =AIN2 00122 //AnalogIn adcB(p2); //p0.02 =AIN3 00123 00124 #define D_0_LED_TICKER { bLBout=((uTicker1++)&0x01);} // LED Follows Ticker (May also follow a button) 00125 #define D_1_LED_BUTTON { bLWout = bB1in;} // LED Follows Button 00126 #define D_OPERATION_LED "Atomwear: Blue=Ticker, White=S1" 00127 00128 #else 00129 #error Unknown PCB 00130 #endif 00131 00132 //============================== BLE 00133 BLEDevice ble; 00134 #if D_BLE_UART==1 00135 #include "UARTService.h" 00136 UARTService *uartServicePtr; 00137 #define D_OPERATION_BLE ", BLE-UART=[Echo typing, and ping on Ticker]\n" 00138 #else 00139 #define D_OPERATION_BLE ", BLE-UART not enabled\n" 00140 #endif 00141 00142 #include "DeviceInformationService.h" 00143 //? #include "DFUService.h" //Required if "DFUService dfu(ble)" is explicitely declared 00144 00145 //============================== Handle Button Activity 00146 void onButton(void) 00147 { // Handle any button changes 00148 DEBUG("B\n"); 00149 00150 D_1_LED_BUTTON; // Update LED from Button 00151 #ifdef D_2_LED_BUTTON 00152 D_2_LED_BUTTON; // Update LED from Button 00153 #endif 00154 }; 00155 00156 //============================== Handle Ticker1 00157 uint8_t uTicker1; 00158 void onTicker1(void) 00159 { 00160 DEBUG(pcPingUART); 00161 D_0_LED_TICKER; // Update LED from Ticker 00162 00163 #if D_BLE_UART==1 00164 if (uartServicePtr != NULL) { 00165 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t *)pcPingBLE, strlen(pcPingBLE)); // BLE UART = Ping Message (Max 20char) 00166 } 00167 #endif 00168 } 00169 00170 //============================== Handle BLE: Disconnect, Written... 00171 void onDisconnect(Gap::Handle_t tHandle, Gap::DisconnectionReason_t eReason) 00172 { 00173 // When BLE Disconnected mkit may output over long DEBUG over USB Serial (Careful of IO clash on other PCBs) 00174 DEBUG("\nBLEi: Callback_BLE_Disconnect(Handle:%d, eReason:0x%02x=", tHandle, eReason );//PR: Occurs properly when click disconnect in App nRFToolbox:HRM 00175 switch(eReason) { 00176 case Gap::REMOTE_USER_TERMINATED_CONNECTION: DEBUG("REMOTE_USER_TERMINATED_CONNECTION"); break; 00177 case Gap::CONN_INTERVAL_UNACCEPTABLE: DEBUG("CONN_INTERVAL_UNACCEPTABLE"); break; 00178 case Gap::LOCAL_HOST_TERMINATED_CONNECTION: DEBUG("LOCAL_HOST_TERMINATED_CONNECTION"); break; 00179 default: DEBUG("UNKNOWN"); break; 00180 } 00181 DEBUG("), Restarting Advertising\n", tHandle, eReason );//PR: Occurs properly when click disconnect in App nRFToolbox:HRM 00182 ble.startAdvertising(); // restart advertising //20150130: Thanks to Wayne Keenan (waynek) for spotting this missing line!! 00183 } 00184 00185 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00186 { 00187 #if D_BLE_UART==1 00188 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { 00189 uint16_t bytesRead = params->len; 00190 DEBUG("BLE-UART Echo Rx[%u]=[%*s]\n", bytesRead, params->len, params->data); 00191 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); 00192 } else { 00193 DEBUG("Unexpected Characteristic Write, Handle:%d\n", params->charHandle); 00194 } 00195 #else 00196 DEBUG("Unexpected Characteristic Write, Handle:%d\n", params->charHandle); 00197 #endif 00198 } 00199 00200 //============================== 00201 int main(void) 00202 { 00203 Ticker ticker1; 00204 ticker1.attach(onTicker1, D_PING_SEC); //Ping Rate 00205 00206 DEBUG("BLE Init %s\n", pcDeviceName); 00207 DEBUG("After FOTA loading any unused IO pins may hold previous state, initialize all pins or reset to clear\n"); 00208 ble.init(); 00209 ble.onDisconnection(onDisconnect); 00210 ble.onDataWritten(onDataWritten); 00211 00212 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00213 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)pcDeviceName, strlen(pcDeviceName)); 00214 00215 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */ 00216 //ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ 00217 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); //PR: Advertise 1sec (1Hz) 00218 ble.startAdvertising(); 00219 00220 //DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1"); 00221 DeviceInformationService deviceInfo(ble, "ARM", pcDeviceName, "SN1000", "hw-rev1", "fw-rev1"); 00222 00223 //Enable FOTA DFU 00224 //? DFUService dfu(ble); // Included implicitely by ble.init() for FOTA platforms, but doing explicitly may be desired for some cases 00225 00226 #if D_BLE_UART==1 00227 UARTService uartService(ble); //Setup BLE UART 00228 uartServicePtr = &uartService; 00229 #endif 00230 00231 DEBUG(D_OPERATION_LED D_OPERATION_BLE); 00232 B1int.fall(&onButton); B1int.rise(&onButton); // Button1 00233 #ifdef D_2_LED_BUTTON 00234 B2int.fall(&onButton); B2int.rise(&onButton); // Button2 00235 #endif 00236 00237 #if D_PCB==D_MKIT 00238 #ifdef D_MkitPROBING 00239 //**Warning:As of 20150219 nRF appears to have max 3 PwmOut channels, others don't output but compile OK. (Maybe see SoftPWM or FastPWM for more channels) 00240 00241 //For IO Pinout check: PWM 10%~80% on Debug Header pins to allow probing verification: 00242 // Signals by schematic (User guide has typos), arranged by header rows from PCB edge 00243 // nRF51 PWM //mkit 00244 PwmOut pwm_p30(p30); pwm_p30=0.1; //P6.7 00245 #endif 00246 #endif 00247 00248 #if D_PCB==D_ATOMWEAR 00249 #ifdef D_AtomPROBING 00250 //**Disconnect all modules except Main/Debug/Battery** 00251 00252 //**Warning:As of 20150219 nRF appears to have max 3 PwmOut channels, others don't output but compile OK. (Maybe see SoftPWM or FastPWM for more channels) 00253 00254 //For IO Pinout check: PWM 10%~80% on Debug Header pins to allow probing verification: 00255 // Signals by schematic (User guide has typos), arranged by header rows from PCB edge 00256 // nRF51 PWM // Schematic //Header Bus //Silkscreen 00257 PwmOut pwm_p13(p13); pwm_p13=0.1;// SPI_SCK1 //P17.2 22 //8 00258 PwmOut pwm_p14(p14); pwm_p14=0.2;// SPI_MOSI1 //P17.4 23 //10 00259 PwmOut pwm_p15(p15); pwm_p15=0.3;// SPI_MISO1 //P17.6 24 //12 00260 00261 // PwmOut pwm_p0(p0); pwm_p0=0.4;// GPIO1 //P17.1 11 //7 00262 // PwmOut pwm_p2(p2); pwm_p2=0.5;// GPIO2 //P17.3 10 //9 00263 // PwmOut pwm_p3(p3); pwm_p3=0.6;// GPIO3 //P17.5 9 //11 00264 00265 // PwmOut pwm_p12(p12); pwm_p12=0.7;// UART_RX //P15.2 21 //2 00266 // PwmOut pwm_p11(p11); pwm_p11=0.8;// UART_TX //P15.4 20 //4 00267 // ---- // VCC Unreg //P15.6 13 //6 ==Unregulated Power from Battery (Regulator on nRF PCB) 00268 00269 // SWDIO // SWDIO //P15.1 19 //1 00270 // SWCLK // SWDCLK //P15.3 18 //3 00271 // GND // GND //P15.5 15 //5 00272 #endif 00273 #endif 00274 00275 00276 for (;;) { 00277 ble.waitForEvent(); 00278 } 00279 } 00280 //========== end: main.cpp ========== 00281
Generated on Mon Aug 29 2022 11:24:08 by 1.7.2