![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
richlink
main.cpp@33:e09fff6b149f, 2021-06-16 (annotated)
- Committer:
- wright0418
- Date:
- Wed Jun 16 11:05:30 2021 +0000
- Revision:
- 33:e09fff6b149f
- Parent:
- 30:46a585864956
2 test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wright0418 | 33:e09fff6b149f | 1 | /* ePy-Lite BLE usage example |
wright0418 | 33:e09fff6b149f | 2 | * Copyright (c) 2021 Richlink Technology |
wright0418 | 33:e09fff6b149f | 3 | * |
wright0418 | 33:e09fff6b149f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
wright0418 | 33:e09fff6b149f | 5 | * you may not use this file except in compliance with the License. |
wright0418 | 33:e09fff6b149f | 6 | * You may obtain a copy of the License at |
wright0418 | 33:e09fff6b149f | 7 | * |
wright0418 | 33:e09fff6b149f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
wright0418 | 33:e09fff6b149f | 9 | * |
wright0418 | 33:e09fff6b149f | 10 | * Unless required by applicable law or agreed to in writing, software |
wright0418 | 33:e09fff6b149f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
wright0418 | 33:e09fff6b149f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
wright0418 | 33:e09fff6b149f | 13 | * See the License for the specific language governing permissions and |
wright0418 | 33:e09fff6b149f | 14 | * limitations under the License. |
wright0418 | 33:e09fff6b149f | 15 | */ |
cyliang | 0:2198c8de64fe | 16 | |
wright0418 | 33:e09fff6b149f | 17 | #include "mbed.h" |
wright0418 | 33:e09fff6b149f | 18 | #include "USBSerial.h" |
wright0418 | 33:e09fff6b149f | 19 | #include "epy_lite_io.h" |
cyliang | 0:2198c8de64fe | 20 | |
wright0418 | 33:e09fff6b149f | 21 | #if MBED_MAJOR_VERSION < 6 |
wright0418 | 33:e09fff6b149f | 22 | #error "Mbed OS version requires 6 or later." |
cyliang | 0:2198c8de64fe | 23 | #endif |
cyliang | 0:2198c8de64fe | 24 | |
wright0418 | 33:e09fff6b149f | 25 | #include "SPIFBlockDevice.h" |
wright0418 | 33:e09fff6b149f | 26 | |
wright0418 | 33:e09fff6b149f | 27 | // Disable Write Protection and Hold functions |
wright0418 | 33:e09fff6b149f | 28 | DigitalOut spif_wp(SPIFLASH_WP, 1); |
wright0418 | 33:e09fff6b149f | 29 | DigitalOut spif_hold(SPIFLASH_HOLD, 1); |
wright0418 | 33:e09fff6b149f | 30 | |
wright0418 | 33:e09fff6b149f | 31 | /* |
wright0418 | 33:e09fff6b149f | 32 | BlockDevice *bd = new SPIFBlockDevice(MBED_CONF_SPIF_DRIVER_SPI_MOSI, |
wright0418 | 33:e09fff6b149f | 33 | MBED_CONF_SPIF_DRIVER_SPI_MISO, |
wright0418 | 33:e09fff6b149f | 34 | MBED_CONF_SPIF_DRIVER_SPI_CLK, |
wright0418 | 33:e09fff6b149f | 35 | MBED_CONF_SPIF_DRIVER_SPI_CS); |
wright0418 | 33:e09fff6b149f | 36 | */ |
wright0418 | 33:e09fff6b149f | 37 | BlockDevice *bd = new SPIFBlockDevice(SPIFLASH_MOSI, |
wright0418 | 33:e09fff6b149f | 38 | SPIFLASH_MISO, |
wright0418 | 33:e09fff6b149f | 39 | SPIFLASH_CLK, |
wright0418 | 33:e09fff6b149f | 40 | SPIFLASH_CS); |
wright0418 | 33:e09fff6b149f | 41 | |
wright0418 | 33:e09fff6b149f | 42 | // This example uses LittleFileSystem as the default file system |
wright0418 | 33:e09fff6b149f | 43 | #include "LittleFileSystem.h" |
wright0418 | 33:e09fff6b149f | 44 | LittleFileSystem fs("fs"); |
wright0418 | 33:e09fff6b149f | 45 | |
wright0418 | 33:e09fff6b149f | 46 | // Uncomment the following two lines and comment the previous two to use FAT file system. |
wright0418 | 33:e09fff6b149f | 47 | // #include "FATFileSystem.h" |
wright0418 | 33:e09fff6b149f | 48 | // FATFileSystem fs("fs"); |
cyliang | 0:2198c8de64fe | 49 | |
cyliang | 0:2198c8de64fe | 50 | |
wright0418 | 33:e09fff6b149f | 51 | USBSerial *_console; |
wright0418 | 33:e09fff6b149f | 52 | |
wright0418 | 33:e09fff6b149f | 53 | BufferedSerial *_ble_serial; |
wright0418 | 33:e09fff6b149f | 54 | ATCmdParser *_parser; |
cyliang | 0:2198c8de64fe | 55 | |
wright0418 | 33:e09fff6b149f | 56 | Ticker flipper; |
wright0418 | 33:e09fff6b149f | 57 | DigitalOut _led(LED_Y, 0); |
wright0418 | 33:e09fff6b149f | 58 | DigitalOut _ledr(LED_R, 0); |
wright0418 | 33:e09fff6b149f | 59 | DigitalOut _ledb(LED_B, 0); |
cyliang | 0:2198c8de64fe | 60 | |
wright0418 | 33:e09fff6b149f | 61 | void flip_led() |
wright0418 | 33:e09fff6b149f | 62 | { |
wright0418 | 33:e09fff6b149f | 63 | _led = !_led; |
cyliang | 0:2198c8de64fe | 64 | } |
cyliang | 0:2198c8de64fe | 65 | |
wright0418 | 33:e09fff6b149f | 66 | void ble_recv() |
wright0418 | 33:e09fff6b149f | 67 | { |
wright0418 | 33:e09fff6b149f | 68 | unsigned char c; |
wright0418 | 33:e09fff6b149f | 69 | |
wright0418 | 33:e09fff6b149f | 70 | while(_ble_serial->readable()) { |
wright0418 | 33:e09fff6b149f | 71 | _ledb = !_ledb; |
wright0418 | 33:e09fff6b149f | 72 | _ble_serial->read(&c, 1); |
wright0418 | 33:e09fff6b149f | 73 | _console->write(&c, 1); |
wright0418 | 33:e09fff6b149f | 74 | } |
cyliang | 0:2198c8de64fe | 75 | } |
cyliang | 0:2198c8de64fe | 76 | |
wright0418 | 33:e09fff6b149f | 77 | void console_recv() |
wright0418 | 33:e09fff6b149f | 78 | { |
wright0418 | 33:e09fff6b149f | 79 | static bool pt_mode = false; |
wright0418 | 33:e09fff6b149f | 80 | unsigned char c; |
cyliang | 30:46a585864956 | 81 | |
wright0418 | 33:e09fff6b149f | 82 | while(_console->readable()) { |
wright0418 | 33:e09fff6b149f | 83 | _ledr = !_ledr; |
wright0418 | 33:e09fff6b149f | 84 | _console->read(&c, 1); |
wright0418 | 33:e09fff6b149f | 85 | if (pt_mode == true) |
wright0418 | 33:e09fff6b149f | 86 | { |
wright0418 | 33:e09fff6b149f | 87 | if (c == '\r') |
wright0418 | 33:e09fff6b149f | 88 | { |
wright0418 | 33:e09fff6b149f | 89 | _console->write("\r\n", 2); // echo to console |
wright0418 | 33:e09fff6b149f | 90 | _ble_serial->write("\r\n", 2); |
wright0418 | 33:e09fff6b149f | 91 | } |
wright0418 | 33:e09fff6b149f | 92 | else if (c == 0x03) // Ctrl-C |
wright0418 | 33:e09fff6b149f | 93 | { |
wright0418 | 33:e09fff6b149f | 94 | pt_mode = false; |
wright0418 | 33:e09fff6b149f | 95 | _console->printf("\r\nExit pass-through Mode\r\n"); |
wright0418 | 33:e09fff6b149f | 96 | } |
wright0418 | 33:e09fff6b149f | 97 | else |
wright0418 | 33:e09fff6b149f | 98 | { |
wright0418 | 33:e09fff6b149f | 99 | _console->write(&c, 1); // echo to console |
wright0418 | 33:e09fff6b149f | 100 | _ble_serial->write(&c, 1); |
wright0418 | 33:e09fff6b149f | 101 | } |
wright0418 | 33:e09fff6b149f | 102 | } |
wright0418 | 33:e09fff6b149f | 103 | else { |
wright0418 | 33:e09fff6b149f | 104 | if (c == '?') |
wright0418 | 33:e09fff6b149f | 105 | { |
wright0418 | 33:e09fff6b149f | 106 | _console->printf("\r\n ? : print this menu"); |
wright0418 | 33:e09fff6b149f | 107 | _console->printf("\r\n p : enter pass-through mode. Press CTRL-C to exit the mode"); |
wright0418 | 33:e09fff6b149f | 108 | _console->printf("\r\n v : send AT+VERSION"); |
wright0418 | 33:e09fff6b149f | 109 | _console->printf("\r\n a : send AT+ADDR=?"); |
wright0418 | 33:e09fff6b149f | 110 | _console->printf("\r\n r : send AT+RESET"); |
wright0418 | 33:e09fff6b149f | 111 | _console->printf("\r\n d : send AT+MODE_DATA"); |
wright0418 | 33:e09fff6b149f | 112 | _console->printf("\r\n c : send !CCMD@"); |
wright0418 | 33:e09fff6b149f | 113 | _console->printf("\r\n"); |
wright0418 | 33:e09fff6b149f | 114 | } |
wright0418 | 33:e09fff6b149f | 115 | else if (c == 'p') |
wright0418 | 33:e09fff6b149f | 116 | { |
wright0418 | 33:e09fff6b149f | 117 | pt_mode = true; |
wright0418 | 33:e09fff6b149f | 118 | _console->printf("\r\nEnter pass-through mode...\r\n"); |
wright0418 | 33:e09fff6b149f | 119 | } |
wright0418 | 33:e09fff6b149f | 120 | else if (c == 'v') |
wright0418 | 33:e09fff6b149f | 121 | { |
wright0418 | 33:e09fff6b149f | 122 | //_ble_serial->write("AT+VERSION\r\n", 12); |
wright0418 | 33:e09fff6b149f | 123 | _ble_serial->write("AT+VERSION", 10); |
wright0418 | 33:e09fff6b149f | 124 | ThisThread::sleep_for(2ms); |
wright0418 | 33:e09fff6b149f | 125 | _ble_serial->write("\r\n", 2); |
wright0418 | 33:e09fff6b149f | 126 | } |
wright0418 | 33:e09fff6b149f | 127 | else if (c == 'a') |
wright0418 | 33:e09fff6b149f | 128 | { |
wright0418 | 33:e09fff6b149f | 129 | _ble_serial->write("AT+ADDR=?\r\n", 11); |
wright0418 | 33:e09fff6b149f | 130 | } |
wright0418 | 33:e09fff6b149f | 131 | else if (c == 'r') |
wright0418 | 33:e09fff6b149f | 132 | { |
wright0418 | 33:e09fff6b149f | 133 | _ble_serial->write("AT+RESET\r\n", 10); |
wright0418 | 33:e09fff6b149f | 134 | } |
wright0418 | 33:e09fff6b149f | 135 | else if (c == 'd') |
wright0418 | 33:e09fff6b149f | 136 | { |
wright0418 | 33:e09fff6b149f | 137 | _ble_serial->write("AT+MODE_DATA\r\n", 14); |
wright0418 | 33:e09fff6b149f | 138 | } |
wright0418 | 33:e09fff6b149f | 139 | else if (c == 'c') |
wright0418 | 33:e09fff6b149f | 140 | { |
wright0418 | 33:e09fff6b149f | 141 | _ble_serial->write("!CCMD@", 6); |
wright0418 | 33:e09fff6b149f | 142 | } |
wright0418 | 33:e09fff6b149f | 143 | } |
wright0418 | 33:e09fff6b149f | 144 | } |
wright0418 | 33:e09fff6b149f | 145 | } |
cyliang | 0:2198c8de64fe | 146 | |
wright0418 | 33:e09fff6b149f | 147 | int main() |
wright0418 | 33:e09fff6b149f | 148 | { |
wright0418 | 33:e09fff6b149f | 149 | flipper.attach(&flip_led, 1s); |
wright0418 | 33:e09fff6b149f | 150 | |
wright0418 | 33:e09fff6b149f | 151 | _console = new USBSerial(true); |
wright0418 | 33:e09fff6b149f | 152 | _ble_serial = new BufferedSerial(BLE_UART_TX, BLE_UART_RX, BLE_DEFAULT_BAUD_RATE); |
wright0418 | 33:e09fff6b149f | 153 | _parser = new ATCmdParser(_ble_serial, "\r\n"); |
wright0418 | 33:e09fff6b149f | 154 | //_parser->debug_on( 0 ); |
wright0418 | 33:e09fff6b149f | 155 | //_parser->set_delimiter( "\r\n" ); |
wright0418 | 33:e09fff6b149f | 156 | |
wright0418 | 33:e09fff6b149f | 157 | #if 1 |
wright0418 | 33:e09fff6b149f | 158 | // Try SPI Flash |
cyliang | 0:2198c8de64fe | 159 | |
wright0418 | 33:e09fff6b149f | 160 | bd->init(); |
wright0418 | 33:e09fff6b149f | 161 | _console->printf("spif size: %llu\r\n", bd->size()); |
wright0418 | 33:e09fff6b149f | 162 | _console->printf("spif read size: %llu\r\n", bd->get_read_size()); |
wright0418 | 33:e09fff6b149f | 163 | _console->printf("spif program size: %llu\r\n", bd->get_program_size()); |
wright0418 | 33:e09fff6b149f | 164 | _console->printf("spif erase size: %llu\r\n", bd->get_erase_size()); |
wright0418 | 33:e09fff6b149f | 165 | _console->printf("Mounting the filesystem... "); |
wright0418 | 33:e09fff6b149f | 166 | int err = fs.mount(bd); |
wright0418 | 33:e09fff6b149f | 167 | _console->printf("%s\r\n", (err ? "Fail :(" : "OK")); |
wright0418 | 33:e09fff6b149f | 168 | if (err) { |
wright0418 | 33:e09fff6b149f | 169 | // Reformat if we can't mount the filesystem |
wright0418 | 33:e09fff6b149f | 170 | // this should only happen on the first boot |
wright0418 | 33:e09fff6b149f | 171 | _console->printf("No filesystem found, formatting... "); |
wright0418 | 33:e09fff6b149f | 172 | err = fs.reformat(bd); |
wright0418 | 33:e09fff6b149f | 173 | _console->printf("%s\r\n", (err ? "Fail :(" : "OK")); |
wright0418 | 33:e09fff6b149f | 174 | if (err) { |
wright0418 | 33:e09fff6b149f | 175 | //error("error: %s (%d)\r\n", strerror(-err), err); |
wright0418 | 33:e09fff6b149f | 176 | _console->printf("error: %s (%d)\r\n", strerror(-err), err); |
wright0418 | 33:e09fff6b149f | 177 | } |
ccli8 |
24:38e186a10abe | 178 | } |
cyliang | 0:2198c8de64fe | 179 | |
wright0418 | 33:e09fff6b149f | 180 | #endif |
wright0418 | 33:e09fff6b149f | 181 | |
wright0418 | 33:e09fff6b149f | 182 | #if 1 |
wright0418 | 33:e09fff6b149f | 183 | // _console->attach(&console_recv); |
wright0418 | 33:e09fff6b149f | 184 | // _ble_serial->attach(&ble_recv, Serial::RxIrq); |
wright0418 | 33:e09fff6b149f | 185 | _console->printf("\r\nStart ..."); |
wright0418 | 33:e09fff6b149f | 186 | _console->printf("\r\nPress ? to show menu ...\r\n"); |
wright0418 | 33:e09fff6b149f | 187 | |
wright0418 | 33:e09fff6b149f | 188 | ThisThread::sleep_for(1s); |
wright0418 | 33:e09fff6b149f | 189 | |
wright0418 | 33:e09fff6b149f | 190 | //_ble_serial->write("AT+ADDR=?\r\n", 11); |
wright0418 | 33:e09fff6b149f | 191 | //ThisThread::sleep_for(1s); |
wright0418 | 33:e09fff6b149f | 192 | //_ble_serial->write("AT+VERSION\r\n", 12); |
cyliang | 0:2198c8de64fe | 193 | |
wright0418 | 33:e09fff6b149f | 194 | while(1) |
wright0418 | 33:e09fff6b149f | 195 | { |
wright0418 | 33:e09fff6b149f | 196 | console_recv(); |
wright0418 | 33:e09fff6b149f | 197 | ble_recv(); |
wright0418 | 33:e09fff6b149f | 198 | } |
wright0418 | 33:e09fff6b149f | 199 | #endif |
wright0418 | 33:e09fff6b149f | 200 | |
wright0418 | 33:e09fff6b149f | 201 | _console->printf("\r\nATCmdParser on ePy-Lite example\r\n"); |
wright0418 | 33:e09fff6b149f | 202 | ThisThread::sleep_for(2s); |
wright0418 | 33:e09fff6b149f | 203 | |
wright0418 | 33:e09fff6b149f | 204 | #if 0 |
wright0418 | 33:e09fff6b149f | 205 | if (_parser->recv("READY OK")) |
wright0418 | 33:e09fff6b149f | 206 | { |
wright0418 | 33:e09fff6b149f | 207 | _console->printf("\r\nModule is ready!"); |
wright0418 | 33:e09fff6b149f | 208 | } |
wright0418 | 33:e09fff6b149f | 209 | #endif |
cyliang | 0:2198c8de64fe | 210 | |
wright0418 | 33:e09fff6b149f | 211 | #if 0 |
wright0418 | 33:e09fff6b149f | 212 | // Assume BLE module is in DATA mode, switch to CMD mode |
wright0418 | 33:e09fff6b149f | 213 | if (_parser->send("!CCMD@") && _parser->recv("OK")) |
wright0418 | 33:e09fff6b149f | 214 | { |
wright0418 | 33:e09fff6b149f | 215 | _console->printf("\r\nSwitch to CMD mode success."); |
wright0418 | 33:e09fff6b149f | 216 | } |
wright0418 | 33:e09fff6b149f | 217 | else { |
wright0418 | 33:e09fff6b149f | 218 | _console->printf("\r\nSwitch to Cmd mode timeout."); |
wright0418 | 33:e09fff6b149f | 219 | // Assume already in CMD mode |
wright0418 | 33:e09fff6b149f | 220 | } |
cyliang | 21:662058dda3b1 | 221 | #endif |
wright0418 | 33:e09fff6b149f | 222 | |
wright0418 | 33:e09fff6b149f | 223 | //Now get the FW version number of BLE module by sending an AT command |
wright0418 | 33:e09fff6b149f | 224 | _console->printf("\r\nATCmdParser: Retrieving FW version"); |
wright0418 | 33:e09fff6b149f | 225 | |
wright0418 | 33:e09fff6b149f | 226 | #if 0 |
wright0418 | 33:e09fff6b149f | 227 | while(true) |
wright0418 | 33:e09fff6b149f | 228 | { |
wright0418 | 33:e09fff6b149f | 229 | char s[256]; |
wright0418 | 33:e09fff6b149f | 230 | if(_parser->read(s, 256) < 0) |
wright0418 | 33:e09fff6b149f | 231 | break; |
cyliang | 0:2198c8de64fe | 232 | |
wright0418 | 33:e09fff6b149f | 233 | _console->printf("%s\r\n", s); |
wright0418 | 33:e09fff6b149f | 234 | } |
wright0418 | 33:e09fff6b149f | 235 | _parser->write("AT+VERSION\r\n",12); |
wright0418 | 33:e09fff6b149f | 236 | #endif |
cyliang | 0:2198c8de64fe | 237 | |
wright0418 | 33:e09fff6b149f | 238 | _parser->write("AT+VERSION\r\n",12); |
wright0418 | 33:e09fff6b149f | 239 | |
wright0418 | 33:e09fff6b149f | 240 | int ver_major, ver_minor, ver_patch; |
wright0418 | 33:e09fff6b149f | 241 | if(_parser->recv("VERSION %d.%d.%d OK", &ver_major, &ver_minor, &ver_patch) /* && _parser->recv("OK") */) { |
wright0418 | 33:e09fff6b149f | 242 | _console->printf("\r\nATCmdParser: FW version: %d.%d.%d", ver_major, ver_minor, ver_patch); |
wright0418 | 33:e09fff6b149f | 243 | _console->printf("\r\nATCmdParser: Retrieving FW version success"); |
wright0418 | 33:e09fff6b149f | 244 | } else { |
wright0418 | 33:e09fff6b149f | 245 | _console->printf("\r\nATCmdParser: Retrieving FW version failed"); |
wright0418 | 33:e09fff6b149f | 246 | return -1; |
cyliang | 0:2198c8de64fe | 247 | } |
cyliang | 0:2198c8de64fe | 248 | |
wright0418 | 33:e09fff6b149f | 249 | _parser->write("AT+ADDR=?\r\n",11); |
wright0418 | 33:e09fff6b149f | 250 | |
wright0418 | 33:e09fff6b149f | 251 | #if 1 |
wright0418 | 33:e09fff6b149f | 252 | if (_parser->recv("MAC_ADD")) |
wright0418 | 33:e09fff6b149f | 253 | { |
wright0418 | 33:e09fff6b149f | 254 | char mac_addr[20]; |
wright0418 | 33:e09fff6b149f | 255 | _parser->read(mac_addr, 14); |
wright0418 | 33:e09fff6b149f | 256 | _console->printf("\r\nMAC Addr is %s\r\n", mac_addr); |
wright0418 | 33:e09fff6b149f | 257 | |
wright0418 | 33:e09fff6b149f | 258 | } |
wright0418 | 33:e09fff6b149f | 259 | #else |
wright0418 | 33:e09fff6b149f | 260 | long int mac_addr; |
wright0418 | 33:e09fff6b149f | 261 | if(_parser->recv("MAC_ADDR %lx OK", &mac_addr) /* && _parser->recv("OK") */) |
wright0418 | 33:e09fff6b149f | 262 | { |
wright0418 | 33:e09fff6b149f | 263 | _console->printf("\r\nATCmdParser: MAC Address : %lx", mac_addr); |
wright0418 | 33:e09fff6b149f | 264 | _console->printf("\r\nATCmdParser: Retrieving MAC Address success"); |
wright0418 | 33:e09fff6b149f | 265 | } else { |
wright0418 | 33:e09fff6b149f | 266 | _console->printf("\r\nATCmdParser: Retrieving FW version failed"); |
wright0418 | 33:e09fff6b149f | 267 | return -1; |
wright0418 | 33:e09fff6b149f | 268 | } |
cyliang | 0:2198c8de64fe | 269 | #endif |
ccli8 |
24:38e186a10abe | 270 | |
wright0418 | 33:e09fff6b149f | 271 | _console->printf("\r\nDone\r\n"); |
cyliang | 0:2198c8de64fe | 272 | } |