CC3000 test App

Dependencies:   CC3000HostDriver mbed

Committer:
dflet
Date:
Sat Sep 14 18:02:36 2013 +0000
Revision:
2:17c37c0b0534
Parent:
0:305844973572
Removed last change as its not required, problems I encountered were due to too many debug printouts.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:305844973572 1 /**************************************************************************
dflet 0:305844973572 2 *
dflet 0:305844973572 3 * CC3000TestApp.cpp - Basic connection test between the TI CC3000
dflet 0:305844973572 4 * and an Mbed.
dflet 0:305844973572 5 *
dflet 0:305844973572 6 * Mbed Version 1.0
dflet 0:305844973572 7 *
dflet 0:305844973572 8 * Copyright (C) 2013
dflet 0:305844973572 9 *
dflet 0:305844973572 10 * Note: Some or all of this software has been modified in some way to meet the
dflet 0:305844973572 11 * requirements of the mbed libs and/or compiler. If you need a fresh copy of the TI
dflet 0:305844973572 12 * software please check TI's website.
dflet 0:305844973572 13 * If you need help please take the time to read Chris's notes below.
dflet 0:305844973572 14 * ( Yes I know they are for an Arduino, but some still do apply.)
dflet 0:305844973572 15 * Most important!
dflet 0:305844973572 16 * The mbed will not supply the current that the CC3000 requires, if you try it will
dflet 0:305844973572 17 * show up as the mbed resetting as soon as spi runs.
dflet 0:305844973572 18 *
dflet 0:305844973572 19 * To associate with your WIFI router you will first need to insert your ssid and key into
dflet 0:305844973572 20 * functions, ManualConnect() and ManualAddProfile()
dflet 0:305844973572 21 *
dflet 0:305844973572 22 * Redistribution and use in source and binary forms, with or without
dflet 0:305844973572 23 * modification, are permitted provided that the following conditions
dflet 0:305844973572 24 * are met:
dflet 0:305844973572 25 *
dflet 0:305844973572 26 * Don't sue me if this code blows up your board and burns down your house and incinerates
dflet 0:305844973572 27 * all life and property in this and any other universe!
dflet 0:305844973572 28 ****************************************************************************\
dflet 0:305844973572 29
dflet 0:305844973572 30 To connect an mbed to the CC3000 you'll need to make these 6 connections
dflet 0:305844973572 31 (in addition to the WiFi antenna, power etc).
dflet 0:305844973572 32
dflet 0:305844973572 33 Name / pin on CC3000EM board / purpose
dflet 0:305844973572 34
dflet 0:305844973572 35 cs / J4-8 SPI Chip Select
dflet 0:305844973572 36 The Mbed will set this pin LOW when it wants to
dflet 0:305844973572 37 exchange data with the CC3000. This is
dflet 0:305844973572 38 mbed pin 8, but any pin can be used. In this
dflet 0:305844973572 39 program it will be called cs.
dflet 0:305844973572 40 This is the mbed's pin 8.
dflet 0:305844973572 41
dflet 0:305844973572 42 MISO / J4-9 Data from the module to the mbed
dflet 0:305844973572 43 This is mbed's MISO pin, and is how the CC3000
dflet 0:305844973572 44 will get bytes to the mbed.
dflet 0:305844973572 45 This is the mbed's pin 6.
dflet 0:305844973572 46
dflet 0:305844973572 47 WLAN_IRQ / J4-10 CC3000 host notify
dflet 0:305844973572 48 The CC3000 will drive this pin LOW to let the mbed
dflet 0:305844973572 49 know it's ready to send data. In
dflet 0:305844973572 50 this program it will be called WLAN_IRQ
dflet 0:305844973572 51 This is the mbed's pin 9.
dflet 0:305844973572 52
dflet 0:305844973572 53 MOSI / J4-11 Data from the Arduino to the CC3000
dflet 0:305844973572 54 This is the Arduino's MOSI pin, and is how the Arduino
dflet 0:305844973572 55 will get bytes to the CC3000.
dflet 0:305844973572 56 This is the mbed's pin 5.
dflet 0:305844973572 57
dflet 0:305844973572 58 SCK / J4-12 SPI clock
dflet 0:305844973572 59 This is the mbed's SCK pin 7.
dflet 0:305844973572 60
dflet 0:305844973572 61 WLAN_EN / J5-5 Module enable
dflet 0:305844973572 62 The Arduino will set this pin HIGH to turn the CC3000
dflet 0:305844973572 63 on. Any pin can be used. In this program it will be
dflet 0:305844973572 64 called WLAN_EN
dflet 0:305844973572 65 This is the mbed's pin 10.
dflet 0:305844973572 66 ***************************************************************************/
dflet 0:305844973572 67
dflet 0:305844973572 68 /**************************************************************************
dflet 0:305844973572 69 *
dflet 0:305844973572 70 * ArduinoCC3000Test.ino - Basic connection test between the TI CC3000
dflet 0:305844973572 71 * and an Arduino.
dflet 0:305844973572 72 *
dflet 0:305844973572 73 * Version 1.0
dflet 0:305844973572 74 *
dflet 0:305844973572 75 * Copyright (C) 2013 Chris Magagna - cmagagna@yahoo.com
dflet 0:305844973572 76 *
dflet 0:305844973572 77 * Redistribution and use in source and binary forms, with or without
dflet 0:305844973572 78 * modification, are permitted provided that the following conditions
dflet 0:305844973572 79 * are met:
dflet 0:305844973572 80 *
dflet 0:305844973572 81 * Don't sue me if my code blows up your board and burns down your house
dflet 0:305844973572 82 *
dflet 0:305844973572 83 ****************************************************************************
dflet 0:305844973572 84
dflet 0:305844973572 85
dflet 0:305844973572 86
dflet 0:305844973572 87 To connect an Arduino to the CC3000 you'll need to make these 6 connections
dflet 0:305844973572 88 (in addition to the WiFi antenna, power etc).
dflet 0:305844973572 89
dflet 0:305844973572 90 Name / pin on CC3000 module / pin on CC3000EM board / purpose
dflet 0:305844973572 91
dflet 0:305844973572 92 SPI_CS / 12 / J4-8 / SPI Chip Select
dflet 0:305844973572 93 The Arduino will set this pin LOW when it wants to
dflet 0:305844973572 94 exchange data with the CC3000. By convention this is
dflet 0:305844973572 95 Arduino pin 10, but any pin can be used. In this
dflet 0:305844973572 96 program it will be called WLAN_CS
dflet 0:305844973572 97
dflet 0:305844973572 98 SPI_DOUT / 13 / J4-9 / Data from the module to the Arduino
dflet 0:305844973572 99 This is Arduino's MISO pin, and is how the CC3000
dflet 0:305844973572 100 will get bytes to the Arduino. For most Arduinos
dflet 0:305844973572 101 MISO is pin 12
dflet 0:305844973572 102
dflet 0:305844973572 103 SPI_IRQ / 14 / J4-10 / CC3000 host notify
dflet 0:305844973572 104 The CC3000 will drive this pin LOW to let the Arduino
dflet 0:305844973572 105 know it's ready to send data. For a regular Arduino
dflet 0:305844973572 106 (Uno, Nano, Leonardo) this will have to be connected
dflet 0:305844973572 107 to pin 2 or 3 so you can use attachInterrupt(). In
dflet 0:305844973572 108 this program it will be called WLAN_IRQ
dflet 0:305844973572 109
dflet 0:305844973572 110 SPI_DIN / 15 / J4-11 Data from the Arduino to the CC3000
dflet 0:305844973572 111 This is the Arduino's MOSI pin, and is how the Arduino
dflet 0:305844973572 112 will get bytes to the CC3000. For most Arduinos
dflet 0:305844973572 113 MOSI is pin 11
dflet 0:305844973572 114
dflet 0:305844973572 115 SPI_CLK / 17 / J4-12 SPI clock
dflet 0:305844973572 116 This is the Arduino's SCK pin. For most Arduinos
dflet 0:305844973572 117 SCK is pin 13
dflet 0:305844973572 118
dflet 0:305844973572 119 VBAT_SW_EN / 26 / J5-5 Module enable
dflet 0:305844973572 120 The Arduino will set this pin HIGH to turn the CC3000
dflet 0:305844973572 121 on. Any pin can be used. In this program it will be
dflet 0:305844973572 122 called WLAN_EN
dflet 0:305844973572 123
dflet 0:305844973572 124
dflet 0:305844973572 125 WARNING #1: The CC3000 runs at 3.6V maximum so you can't run it from your
dflet 0:305844973572 126 regular 5V Arduino power pin. Run it from 3.3V!
dflet 0:305844973572 127
dflet 0:305844973572 128
dflet 0:305844973572 129 WARNING #2: When transmitting the CC3000 will use up to 275mA current. Most
dflet 0:305844973572 130 Arduinos' 3.3V pins can only supply up to 50mA current, so you'll need a
dflet 0:305844973572 131 separate power supply for it (or a voltage regulator like the LD1117V33
dflet 0:305844973572 132 connected to your Arduino's 5V power pin).
dflet 0:305844973572 133
dflet 0:305844973572 134
dflet 0:305844973572 135 WARNING #3: The CC3000's IO pins are not 5V tolerant. If you're using a 5V
dflet 0:305844973572 136 Arduino you will need a level shifter to convert these signals to 3.3V
dflet 0:305844973572 137 so you don't blow up the module.
dflet 0:305844973572 138
dflet 0:305844973572 139 You'll need to shift the pins for WLAN_CS, MOSI, SCK, and WLAN_EN. The other
dflet 0:305844973572 140 2 pins (WLAN_IRQ and MISO) can be connected directly because they're input
dflet 0:305844973572 141 pins for the Arduino, and the Arduino can read 3.3V signals directly.
dflet 0:305844973572 142
dflet 0:305844973572 143 You can use a level shifter chip like the 74LVC245 or TXB0104 or you can use
dflet 0:305844973572 144 a pair of resistors to make a voltage divider like this:
dflet 0:305844973572 145
dflet 0:305844973572 146 Arduino pin -----> 560 Ohm -----> 1K Ohm -----> GND
dflet 0:305844973572 147 |
dflet 0:305844973572 148 |
dflet 0:305844973572 149 +---> CC3000 pin
dflet 0:305844973572 150
dflet 0:305844973572 151
dflet 0:305844973572 152 ****************************************************************************/
dflet 0:305844973572 153
dflet 0:305844973572 154
dflet 0:305844973572 155
dflet 0:305844973572 156 #include "wlan.h"
dflet 0:305844973572 157 #include "evnt_handler.h" // callback function declaration
dflet 0:305844973572 158 #include "nvmem.h"
dflet 0:305844973572 159 #include "socket.h"
dflet 0:305844973572 160 #include "netapp.h"
dflet 0:305844973572 161 #include "host_driver_version.h"
dflet 0:305844973572 162 #include "cc3000.h"
dflet 0:305844973572 163 //#include "common.h"
dflet 0:305844973572 164 //#include "demo_config.h"
dflet 0:305844973572 165 //#include "HttpString.h"
dflet 0:305844973572 166 #include "spi.h"
dflet 0:305844973572 167 #include "CC3000TestApp.h"
dflet 0:305844973572 168 #include "CC3000Core.h"
dflet 0:305844973572 169 //#include <msp430.h>
dflet 0:305844973572 170 #include "mbed.h"
dflet 0:305844973572 171 //#include "Board.h"
dflet 0:305844973572 172 //#include "HttpCore.h"
dflet 0:305844973572 173 //#include "Wheel.h"
dflet 0:305844973572 174 //#include "dispatcher.h"
dflet 0:305844973572 175 #include "DigitalClass.h"
dflet 0:305844973572 176
dflet 0:305844973572 177 #define FALSE 0
dflet 0:305844973572 178 int8_t isInitialized = false;
dflet 0:305844973572 179 InterruptIn intr(p9);
dflet 0:305844973572 180
dflet 0:305844973572 181 DigitalOut ind1(LED1);
dflet 0:305844973572 182 DigitalOut ind2(LED2);
dflet 0:305844973572 183 DigitalOut ind3(LED3);
dflet 0:305844973572 184 DigitalOut ind4(LED4);
dflet 0:305844973572 185
dflet 0:305844973572 186 Serial usb(USBTX, USBRX);
dflet 0:305844973572 187
dflet 0:305844973572 188 void IntSpi()
dflet 0:305844973572 189 {
dflet 0:305844973572 190
dflet 0:305844973572 191 IntSpiGPIOHandler();// spi.cpp
dflet 0:305844973572 192
dflet 0:305844973572 193 }
dflet 0:305844973572 194
dflet 0:305844973572 195 int main()
dflet 0:305844973572 196 {
dflet 0:305844973572 197
dflet 0:305844973572 198 intr.fall(&IntSpi);
dflet 0:305844973572 199 usb.baud(115200);
dflet 0:305844973572 200
dflet 0:305844973572 201 // Start CC3000 State Machine
dflet 0:305844973572 202 resetCC3000StateMachine();
dflet 0:305844973572 203
dflet 0:305844973572 204
dflet 0:305844973572 205 char cmd;
dflet 0:305844973572 206
dflet 0:305844973572 207
dflet 0:305844973572 208 while (1) {
dflet 0:305844973572 209 printf("\r\n");
dflet 0:305844973572 210 printf("+-------------------------------------------+\r\n");
dflet 0:305844973572 211 printf("| Mbed CC3000 Demo Program |\r\n");
dflet 0:305844973572 212 printf("+-------------------------------------------+\r\n");
dflet 0:305844973572 213 printf("\r\n");
dflet 0:305844973572 214 printf(" 1 - Initialize the CC3000\r\n");
dflet 0:305844973572 215 printf(" 2 - Show RX & TX buffer sizes, & free RAM\r\n");
dflet 0:305844973572 216 printf(" 3 - Start Smart Config\r\n");
dflet 0:305844973572 217 printf(" 4 - Manually connect to AP\r\n");
dflet 0:305844973572 218 printf(" 5 - Manually add connection profile\r\n");
dflet 0:305844973572 219 printf(" 6 - List access points\r\n");
dflet 0:305844973572 220 printf(" 7 - Show CC3000 information\r\n");
dflet 0:305844973572 221 printf("\r\n");
dflet 0:305844973572 222
dflet 0:305844973572 223 //for (;;) {
dflet 0:305844973572 224 while (1) {
dflet 0:305844973572 225 if (asyncNotificationWaiting) {
dflet 0:305844973572 226 asyncNotificationWaiting = false;
dflet 0:305844973572 227 AsyncEventPrint();
dflet 0:305844973572 228 }
dflet 0:305844973572 229 // }
dflet 0:305844973572 230 cmd = usb.getc();
dflet 0:305844973572 231 if (cmd!='\n' && cmd!='\r') {
dflet 0:305844973572 232 break;
dflet 0:305844973572 233 }
dflet 0:305844973572 234 }
dflet 0:305844973572 235
dflet 0:305844973572 236
dflet 0:305844973572 237 switch(cmd) {
dflet 0:305844973572 238 case '1':
dflet 0:305844973572 239 IntSpi();
dflet 0:305844973572 240 initDriver();
dflet 0:305844973572 241 isInitialized = true;
dflet 0:305844973572 242 break;
dflet 0:305844973572 243 case '2':
dflet 0:305844973572 244 //ShowBufferSize();
dflet 0:305844973572 245
dflet 0:305844973572 246 break;
dflet 0:305844973572 247 case '3':
dflet 0:305844973572 248 StartSmartConfig();
dflet 0:305844973572 249 break;
dflet 0:305844973572 250 case '4':
dflet 0:305844973572 251 ManualConnect();
dflet 0:305844973572 252 break;
dflet 0:305844973572 253 case '5':
dflet 0:305844973572 254 ManualAddProfile();
dflet 0:305844973572 255 break;
dflet 0:305844973572 256 case '6':
dflet 0:305844973572 257 ListAccessPoints();
dflet 0:305844973572 258 break;
dflet 0:305844973572 259 case '7':
dflet 0:305844973572 260 ShowInformation();
dflet 0:305844973572 261 break;
dflet 0:305844973572 262 default:
dflet 0:305844973572 263 printf("**Unknown command ");
dflet 0:305844973572 264 printf("%d",cmd);
dflet 0:305844973572 265 printf("**\r\n");
dflet 0:305844973572 266 break;
dflet 0:305844973572 267 }
dflet 0:305844973572 268
dflet 0:305844973572 269 };
dflet 0:305844973572 270 return 0;
dflet 0:305844973572 271 }
dflet 0:305844973572 272
dflet 0:305844973572 273
dflet 0:305844973572 274 void AsyncEventPrint()
dflet 0:305844973572 275 {
dflet 0:305844973572 276 switch(lastAsyncEvent) {
dflet 0:305844973572 277 printf("CC3000 Async event: Simple config done\r\n");
dflet 0:305844973572 278 break;
dflet 0:305844973572 279
dflet 0:305844973572 280 case HCI_EVNT_WLAN_UNSOL_CONNECT:
dflet 0:305844973572 281 printf("CC3000 Async event: Unsolicited connect\r\n");
dflet 0:305844973572 282 break;
dflet 0:305844973572 283
dflet 0:305844973572 284 case HCI_EVNT_WLAN_UNSOL_DISCONNECT:
dflet 0:305844973572 285 printf("CC3000 Async event: Unsolicted disconnect\r\n");
dflet 0:305844973572 286 break;
dflet 0:305844973572 287
dflet 0:305844973572 288 case HCI_EVNT_WLAN_UNSOL_DHCP:
dflet 0:305844973572 289 printf("CC3000 Async event: Got IP address via DHCP: ");
dflet 0:305844973572 290 printf("%d",dhcpIPAddress[0]);
dflet 0:305844973572 291 printf(".");
dflet 0:305844973572 292 printf("%d",dhcpIPAddress[1]);
dflet 0:305844973572 293 printf(".");
dflet 0:305844973572 294 printf("%d",dhcpIPAddress[2]);
dflet 0:305844973572 295 printf(".");
dflet 0:305844973572 296 printf("%d\r\n",dhcpIPAddress[3]);
dflet 0:305844973572 297 break;
dflet 0:305844973572 298
dflet 0:305844973572 299 case HCI_EVENT_CC3000_CAN_SHUT_DOWN:
dflet 0:305844973572 300 printf("CC3000 Async event: OK to shut down\r\n");
dflet 0:305844973572 301 break;
dflet 0:305844973572 302
dflet 0:305844973572 303 case HCI_EVNT_WLAN_KEEPALIVE:
dflet 0:305844973572 304 // Once initialized, the CC3000 will send these keepalive events
dflet 0:305844973572 305 // every 20 seconds.
dflet 0:305844973572 306 //printf(F("CC3000 Async event: Keepalive"));
dflet 0:305844973572 307 //return;
dflet 0:305844973572 308 break;
dflet 0:305844973572 309
dflet 0:305844973572 310 default:
dflet 0:305844973572 311 printf("AsyncCallback called with unhandled event! ");
dflet 0:305844973572 312 printf("%x",lastAsyncEvent);
dflet 0:305844973572 313 printf("\r\n");
dflet 0:305844973572 314 break;
dflet 0:305844973572 315 }
dflet 0:305844973572 316
dflet 0:305844973572 317 printf("\r\n");
dflet 0:305844973572 318 }
dflet 0:305844973572 319 /*
dflet 0:305844973572 320 This is an example of how you'd connect the CC3000 to an AP without using
dflet 0:305844973572 321 Smart Config or a stored profile.
dflet 0:305844973572 322
dflet 0:305844973572 323 All the code above wlan_connect() is just for this demo program; if you're
dflet 0:305844973572 324 always going to connect to your network this way you wouldn't need it.
dflet 0:305844973572 325 */
dflet 0:305844973572 326
dflet 0:305844973572 327 void ManualConnect() {
dflet 0:305844973572 328
dflet 0:305844973572 329 char ssidName[] = "***********";
dflet 0:305844973572 330 char AP_KEY[] = "************";
dflet 0:305844973572 331 int8_t rval;
dflet 0:305844973572 332
dflet 0:305844973572 333 if (!isInitialized) {
dflet 0:305844973572 334 printf("CC3000 not initialized; can't run manual connect.\r\n");
dflet 0:305844973572 335 return;
dflet 0:305844973572 336 }
dflet 0:305844973572 337
dflet 0:305844973572 338 printf("Starting manual connect...\r\n");
dflet 0:305844973572 339
dflet 0:305844973572 340 printf((" Disabling auto-connect policy...\r\n"));
dflet 0:305844973572 341 rval = wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE);
dflet 0:305844973572 342
dflet 0:305844973572 343 printf(" Deleting all existing profiles...\r\n");
dflet 0:305844973572 344 rval = wlan_ioctl_del_profile(255);
dflet 0:305844973572 345
dflet 0:305844973572 346 printf(" Waiting until disconnected...\r\n");
dflet 0:305844973572 347 while (ulCC3000Connected == 1) {
dflet 0:305844973572 348 wait_ms(100);
dflet 0:305844973572 349 }
dflet 0:305844973572 350
dflet 0:305844973572 351 printf(" Manually connecting...\r\n");
dflet 0:305844973572 352
dflet 0:305844973572 353 // Parameter 1 is the security type: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
dflet 0:305844973572 354 // WLAN_SEC_WPA or WLAN_SEC_WPA2
dflet 0:305844973572 355 // Parameter 3 is the MAC adddress of the AP. All the TI examples
dflet 0:305844973572 356 // use NULL. I suppose you would want to specify this
dflet 0:305844973572 357 // if you were security paranoid.
dflet 0:305844973572 358 rval = wlan_connect(WLAN_SEC_WPA2,
dflet 0:305844973572 359 ssidName,
dflet 0:305844973572 360 strlen(ssidName),
dflet 0:305844973572 361 NULL,
dflet 0:305844973572 362 (unsigned char *)AP_KEY,
dflet 0:305844973572 363 strlen(AP_KEY));
dflet 0:305844973572 364
dflet 0:305844973572 365 if (rval==0) {
dflet 0:305844973572 366 printf(" Manual connect success.\r\n");
dflet 0:305844973572 367 }
dflet 0:305844973572 368 else {
dflet 0:305844973572 369 printf(" Unusual return value: ");
dflet 0:305844973572 370 printf("%d\r\n",rval);
dflet 0:305844973572 371 }
dflet 0:305844973572 372 return;
dflet 0:305844973572 373 }
dflet 0:305844973572 374 /*
dflet 0:305844973572 375 This is an example of manually adding a WLAN profile to the CC3000. See
dflet 0:305844973572 376 wlan_ioctl_set_connection_policy() for more details of how profiles are
dflet 0:305844973572 377 used but basically there's 7 slots where you can store AP info and if
dflet 0:305844973572 378 the connection policy is set to auto_start then the CC3000 will go
dflet 0:305844973572 379 through its profile table and try to auto-connect to something it knows
dflet 0:305844973572 380 about after it boots up.
dflet 0:305844973572 381
dflet 0:305844973572 382 Note the API documentation for wlan_add_profile is wrong. It says it
dflet 0:305844973572 383 returns 0 on success and -1 on failure. What it really returns is
dflet 0:305844973572 384 the stored profile number (0-6, since the CC3000 can store 7) or
dflet 0:305844973572 385 255 on failure.
dflet 0:305844973572 386
dflet 0:305844973572 387 Unfortunately the API doesn't give you any way to see how many profiles
dflet 0:305844973572 388 are in use or which profile is stored in which slot, so if you want to
dflet 0:305844973572 389 manage multiple profiles you'll need to do that yourself.
dflet 0:305844973572 390 */
dflet 0:305844973572 391
dflet 0:305844973572 392 void ManualAddProfile() {
dflet 0:305844973572 393 char ssidName[] = "*****************";
dflet 0:305844973572 394 char AP_KEY[] = "***********";
dflet 0:305844973572 395
dflet 0:305844973572 396 if (!isInitialized) {
dflet 0:305844973572 397 printf("CC3000 not initialized; can't run manual add profile.\r\n");
dflet 0:305844973572 398 return;
dflet 0:305844973572 399 }
dflet 0:305844973572 400
dflet 0:305844973572 401 printf("Starting manual add profile...\r\n");
dflet 0:305844973572 402
dflet 0:305844973572 403 printf(" Disabling auto connection...\r\n");
dflet 0:305844973572 404 wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE);
dflet 0:305844973572 405
dflet 0:305844973572 406 printf(" Adding profile...\r\n");
dflet 0:305844973572 407 int8_t rval = wlan_add_profile (
dflet 0:305844973572 408 WLAN_SEC_WPA2, // WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
dflet 0:305844973572 409 (unsigned char *)ssidName,
dflet 0:305844973572 410 strlen(ssidName),
dflet 0:305844973572 411 NULL, // BSSID, TI always uses NULL
dflet 0:305844973572 412 0, // profile priority
dflet 0:305844973572 413 0x18, // key length for WEP security, undocumented why this needs to be 0x18
dflet 0:305844973572 414 0x1e, // key index, undocumented why this needs to be 0x1e
dflet 0:305844973572 415 0x2, // key management, undocumented why this needs to be 2
dflet 0:305844973572 416 (unsigned char *)AP_KEY, // WPA security key
dflet 0:305844973572 417 strlen(AP_KEY) // WPA security key length
dflet 0:305844973572 418 );
dflet 0:305844973572 419
dflet 0:305844973572 420 if (rval!=255) {
dflet 0:305844973572 421
dflet 0:305844973572 422 // This code is lifted from http://e2e.ti.com/support/low_power_rf/f/851/p/180859/672551.aspx;
dflet 0:305844973572 423 // the actual API documentation on wlan_add_profile doesn't specify any of this....
dflet 0:305844973572 424
dflet 0:305844973572 425 printf(" Manual add profile success, stored in profile: ");
dflet 0:305844973572 426 printf("%d\r\n",rval);
dflet 0:305844973572 427
dflet 0:305844973572 428 printf(" Enabling auto connection...\r\n");
dflet 0:305844973572 429 wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE);
dflet 0:305844973572 430
dflet 0:305844973572 431 printf(" Stopping CC3000...\r\n");
dflet 0:305844973572 432 wlan_stop();
dflet 0:305844973572 433
dflet 0:305844973572 434 printf(" Stopping for 5 seconds...\r\n");
dflet 0:305844973572 435 wait_ms(5000);
dflet 0:305844973572 436
dflet 0:305844973572 437 printf(" Restarting CC3000...\r\n");
dflet 0:305844973572 438 wlan_start(0);
dflet 0:305844973572 439
dflet 0:305844973572 440 printf(" Manual add profile done!\r\n");
dflet 0:305844973572 441
dflet 0:305844973572 442 }
dflet 0:305844973572 443 else {
dflet 0:305844973572 444 printf(" Manual add profile failured (all profiles full?).\r\n");
dflet 0:305844973572 445 }
dflet 0:305844973572 446 return;
dflet 0:305844973572 447 }
dflet 0:305844973572 448
dflet 0:305844973572 449
dflet 0:305844973572 450
dflet 0:305844973572 451 /*
dflet 0:305844973572 452 All the data in all the fields from netapp_ipconfig() are reversed,
dflet 0:305844973572 453 e.g. an IP address is read via bytes 3,2,1,0 instead of bytes
dflet 0:305844973572 454 0,1,2,3 and the MAC address is read via bytes 5,4,3,2,1,0 instead
dflet 0:305844973572 455 of 0,1,2,3,4,5.
dflet 0:305844973572 456
dflet 0:305844973572 457 N.B. TI is inconsistent here; nvmem_get_mac_address() returns them in
dflet 0:305844973572 458 the right order etc.
dflet 0:305844973572 459 */
dflet 0:305844973572 460
dflet 0:305844973572 461 void ShowInformation() {
dflet 0:305844973572 462
dflet 0:305844973572 463 tNetappIpconfigRetArgs inf;
dflet 0:305844973572 464 char localB[33];
dflet 0:305844973572 465
dflet 0:305844973572 466 if (!isInitialized) {
dflet 0:305844973572 467 printf("CC3000 not initialized; can't get information.\r\n");
dflet 0:305844973572 468 return;
dflet 0:305844973572 469 }
dflet 0:305844973572 470
dflet 0:305844973572 471 printf("CC3000 information:");
dflet 0:305844973572 472
dflet 0:305844973572 473 netapp_ipconfig(&inf);
dflet 0:305844973572 474
dflet 0:305844973572 475 printf(" IP address: ");
dflet 0:305844973572 476 PrintIPBytes(inf.aucIP);
dflet 0:305844973572 477
dflet 0:305844973572 478 printf(" Subnet mask: ");
dflet 0:305844973572 479 PrintIPBytes(inf.aucSubnetMask);
dflet 0:305844973572 480
dflet 0:305844973572 481 printf(" Gateway: ");
dflet 0:305844973572 482 PrintIPBytes(inf.aucDefaultGateway);
dflet 0:305844973572 483
dflet 0:305844973572 484 printf(" DHCP server: ");
dflet 0:305844973572 485 PrintIPBytes(inf.aucDHCPServer);
dflet 0:305844973572 486
dflet 0:305844973572 487 printf(" DNS server: ");
dflet 0:305844973572 488 PrintIPBytes(inf.aucDNSServer);
dflet 0:305844973572 489
dflet 0:305844973572 490 printf(" MAC address: ");
dflet 0:305844973572 491 for (int i=(MAC_ADDR_LEN-1); i>=0; i--) {
dflet 0:305844973572 492 if (i!=(MAC_ADDR_LEN-1)) {
dflet 0:305844973572 493 printf(":");
dflet 0:305844973572 494 }
dflet 0:305844973572 495 printf("%x",inf.uaMacAddr[i]);
dflet 0:305844973572 496 }
dflet 0:305844973572 497 printf("\r\n");
dflet 0:305844973572 498
dflet 0:305844973572 499 memset(localB, 0, 32);
dflet 0:305844973572 500 memcpy(localB, inf.uaSSID, 32);
dflet 0:305844973572 501
dflet 0:305844973572 502 printf(" Connected to SSID: ");
dflet 0:305844973572 503 printf("%s",localB);
dflet 0:305844973572 504
dflet 0:305844973572 505 }
dflet 0:305844973572 506
dflet 0:305844973572 507 void PrintIPBytes(unsigned char *ipBytes) {
dflet 0:305844973572 508
dflet 0:305844973572 509 printf("%d",ipBytes[3]);
dflet 0:305844973572 510 printf(".");
dflet 0:305844973572 511 printf("%d",ipBytes[2]);
dflet 0:305844973572 512 printf(".");
dflet 0:305844973572 513 printf("%d",ipBytes[1]);
dflet 0:305844973572 514 printf(".");
dflet 0:305844973572 515 printf("%d\r\n",ipBytes[0]);
dflet 0:305844973572 516 }
dflet 0:305844973572 517
dflet 0:305844973572 518 /*
dflet 0:305844973572 519 The call wlan_ioctl_get_scan_results returns this structure. I couldn't
dflet 0:305844973572 520 find it in the TI library so it's defined here. It's 50 bytes with
dflet 0:305844973572 521 a semi weird arrangement but fortunately it's not as bad as it looks.
dflet 0:305844973572 522
dflet 0:305844973572 523 numNetworksFound - 4 bytes - On the first call to wlan_ioctl_get_scan_results
dflet 0:305844973572 524 this will be set to how many APs the CC3000 sees. Although
dflet 0:305844973572 525 with 4 bytes the CC3000 could see 4 billion APs in my testing
dflet 0:305844973572 526 this number was always 20 or less so there's probably an
dflet 0:305844973572 527 internal memory limit.
dflet 0:305844973572 528
dflet 0:305844973572 529 results - 4 bytes - 0=aged results, 1=results valid, 2=no results. Why TI
dflet 0:305844973572 530 used 32 bits to store something that could be done in 2,
dflet 0:305844973572 531 and how this field is different than isValid below, is
dflet 0:305844973572 532 a mystery to me so I just igore this field completely.
dflet 0:305844973572 533
dflet 0:305844973572 534 isValid & rssi - 1 byte - a packed structure. The top bit (isValid)
dflet 0:305844973572 535 indicates whether or not this structure has valid data,
dflet 0:305844973572 536 the bottom 7 bits (rssi) are the signal strength of this AP.
dflet 0:305844973572 537
dflet 0:305844973572 538 securityMode & ssidLength - 1 byte - another packed structure. The top 2
dflet 0:305844973572 539 bits (securityMode) show how the AP is configured:
dflet 0:305844973572 540 0 - open / no security
dflet 0:305844973572 541 1 - WEP
dflet 0:305844973572 542 2 - WPA
dflet 0:305844973572 543 3 - WPA2
dflet 0:305844973572 544 ssidLength is the lower 6 bytes and shows how many characters
dflet 0:305844973572 545 (up to 32) of the ssid_name field are valid
dflet 0:305844973572 546
dflet 0:305844973572 547 frameTime - 2 bytes - how long, in seconds, since the CC3000 saw this AP
dflet 0:305844973572 548 beacon
dflet 0:305844973572 549
dflet 0:305844973572 550 ssid_name - 32 bytes - The ssid name for this AP. Note that this isn't a
dflet 0:305844973572 551 regular null-terminated C string so you can't use it
dflet 0:305844973572 552 directly with a strcpy() or printf() etc. and you'll
dflet 0:305844973572 553 need a 33-byte string to store it (32 valid characters +
dflet 0:305844973572 554 null terminator)
dflet 0:305844973572 555
dflet 0:305844973572 556 bssid - 6 bytes - the MAC address of this AP
dflet 0:305844973572 557 */
dflet 0:305844973572 558
dflet 0:305844973572 559 typedef struct scanResults {
dflet 0:305844973572 560 unsigned long numNetworksFound;
dflet 0:305844973572 561 unsigned long results;
dflet 0:305844973572 562 unsigned isValid:1;
dflet 0:305844973572 563 unsigned rssi:7;
dflet 0:305844973572 564 unsigned securityMode:2;
dflet 0:305844973572 565 unsigned ssidLength:6;
dflet 0:305844973572 566 unsigned short frameTime;
dflet 0:305844973572 567 unsigned char ssid_name[32];
dflet 0:305844973572 568 unsigned char bssid[6];
dflet 0:305844973572 569 } scanResults;
dflet 0:305844973572 570
dflet 0:305844973572 571 #define NUM_CHANNELS 16
dflet 0:305844973572 572
dflet 0:305844973572 573 void ListAccessPoints() {
dflet 0:305844973572 574 unsigned long aiIntervalList[NUM_CHANNELS];
dflet 0:305844973572 575 int8_t rval;
dflet 0:305844973572 576 scanResults sr;
dflet 0:305844973572 577 int apCounter;
dflet 0:305844973572 578 char localB[33];
dflet 0:305844973572 579
dflet 0:305844973572 580 if (!isInitialized) {
dflet 0:305844973572 581 printf(("CC3000 not initialized; can't list access points.\r\n"));
dflet 0:305844973572 582 return;
dflet 0:305844973572 583 }
dflet 0:305844973572 584
dflet 0:305844973572 585 printf(("List visible access points\r\n"));
dflet 0:305844973572 586
dflet 0:305844973572 587 printf((" Setting scan paramters...\r\n"));
dflet 0:305844973572 588
dflet 0:305844973572 589 for (int i=0; i<NUM_CHANNELS; i++) {
dflet 0:305844973572 590 aiIntervalList[i] = 2000;
dflet 0:305844973572 591 }
dflet 0:305844973572 592
dflet 0:305844973572 593 rval = wlan_ioctl_set_scan_params(
dflet 0:305844973572 594 1000, // enable start application scan
dflet 0:305844973572 595 100, // minimum dwell time on each channel
dflet 0:305844973572 596 100, // maximum dwell time on each channel
dflet 0:305844973572 597 5, // number of probe requests
dflet 0:305844973572 598 0x7ff, // channel mask
dflet 0:305844973572 599 -80, // RSSI threshold
dflet 0:305844973572 600 0, // SNR threshold
dflet 0:305844973572 601 205, // probe TX power
dflet 0:305844973572 602 aiIntervalList // table of scan intervals per channel
dflet 0:305844973572 603 );
dflet 0:305844973572 604 if (rval!=0) {
dflet 0:305844973572 605 printf(" Got back unusual result from wlan_ioctl_set_scan_params, can't continue: ");
dflet 0:305844973572 606 printf("%d\r\n",rval);
dflet 0:305844973572 607 return;
dflet 0:305844973572 608 }
dflet 0:305844973572 609
dflet 0:305844973572 610 printf(" Sleeping 5 seconds to let the CC3000 discover APs...");
dflet 0:305844973572 611 wait_ms(5000);
dflet 0:305844973572 612
dflet 0:305844973572 613 printf(" Getting AP count...\r\n");
dflet 0:305844973572 614
dflet 0:305844973572 615 // On the first call to get_scan_results, sr.numNetworksFound will return the
dflet 0:305844973572 616 // actual # of APs currently seen. Get that # then loop through and print
dflet 0:305844973572 617 // out what's found.
dflet 0:305844973572 618
dflet 0:305844973572 619 if ((rval=wlan_ioctl_get_scan_results(2000, (unsigned char *)&sr))!=0) {
dflet 0:305844973572 620 printf(" Got back unusual result from wlan_ioctl_get scan results, can't continue: ");
dflet 0:305844973572 621 printf("%d\r\n",rval);
dflet 0:305844973572 622 return;
dflet 0:305844973572 623 }
dflet 0:305844973572 624
dflet 0:305844973572 625 apCounter = sr.numNetworksFound;
dflet 0:305844973572 626 printf((" Number of APs found: "));
dflet 0:305844973572 627 printf("%d\r\n",apCounter);
dflet 0:305844973572 628
dflet 0:305844973572 629 do {
dflet 0:305844973572 630 if (sr.isValid) {
dflet 0:305844973572 631 printf((" "));
dflet 0:305844973572 632 switch(sr.securityMode) {
dflet 0:305844973572 633 case WLAN_SEC_UNSEC: // 0
dflet 0:305844973572 634 printf(("OPEN "));
dflet 0:305844973572 635 break;
dflet 0:305844973572 636 case WLAN_SEC_WEP: // 1
dflet 0:305844973572 637 printf(("WEP "));
dflet 0:305844973572 638 break;
dflet 0:305844973572 639 case WLAN_SEC_WPA: // 2
dflet 0:305844973572 640 printf(("WPA "));
dflet 0:305844973572 641 break;
dflet 0:305844973572 642 case WLAN_SEC_WPA2: // 3
dflet 0:305844973572 643 printf(("WPA2 "));
dflet 0:305844973572 644 break;
dflet 0:305844973572 645 }
dflet 0:305844973572 646 sprintf(localB, "%3d ", sr.rssi);
dflet 0:305844973572 647 printf(localB);
dflet 0:305844973572 648 memset(localB, 0, 33);
dflet 0:305844973572 649 memcpy(localB, sr.ssid_name, sr.ssidLength);
dflet 0:305844973572 650 printf(localB);
dflet 0:305844973572 651 }
dflet 0:305844973572 652
dflet 0:305844973572 653 if (--apCounter>0) {
dflet 0:305844973572 654 if ((rval=wlan_ioctl_get_scan_results(2000, (unsigned char *)&sr))!=0) {
dflet 0:305844973572 655 printf((" Got back unusual result from wlan_ioctl_get scan results, can't continue: "));
dflet 0:305844973572 656 printf("%d\r\n",rval);
dflet 0:305844973572 657 return;
dflet 0:305844973572 658 }
dflet 0:305844973572 659 }
dflet 0:305844973572 660 } while (apCounter>0);
dflet 0:305844973572 661
dflet 0:305844973572 662 printf((" Access Point list finished."));
dflet 0:305844973572 663 }
dflet 0:305844973572 664
dflet 0:305844973572 665
dflet 0:305844973572 666 //*****************************************************************************
dflet 0:305844973572 667 //
dflet 0:305844973572 668 //! turnLedOn
dflet 0:305844973572 669 //!
dflet 0:305844973572 670 //! @param ledNum is the LED Number
dflet 0:305844973572 671 //!
dflet 0:305844973572 672 //! @return none
dflet 0:305844973572 673 //!
dflet 0:305844973572 674 //! @brief Turns a specific LED on
dflet 0:305844973572 675 //
dflet 0:305844973572 676 //*****************************************************************************
dflet 0:305844973572 677 void turnLedOn(char ledNum)
dflet 0:305844973572 678 {
dflet 0:305844973572 679 switch(ledNum)
dflet 0:305844973572 680 {
dflet 0:305844973572 681 case 1:
dflet 0:305844973572 682 ind1 = 1;
dflet 0:305844973572 683 break;
dflet 0:305844973572 684 case 2:
dflet 0:305844973572 685 ind2 = 1;
dflet 0:305844973572 686 break;
dflet 0:305844973572 687 case 3:
dflet 0:305844973572 688 ind3 = 1;
dflet 0:305844973572 689 break;
dflet 0:305844973572 690 case 4:
dflet 0:305844973572 691 ind4 = 1;
dflet 0:305844973572 692 break;
dflet 0:305844973572 693 case 5:
dflet 0:305844973572 694 ind1 = 1;
dflet 0:305844973572 695 break;
dflet 0:305844973572 696 case 6:
dflet 0:305844973572 697 ind4 = 1;
dflet 0:305844973572 698 break;
dflet 0:305844973572 699 case 7:
dflet 0:305844973572 700
dflet 0:305844973572 701 break;
dflet 0:305844973572 702 case 8:
dflet 0:305844973572 703
dflet 0:305844973572 704 break;
dflet 0:305844973572 705 }
dflet 0:305844973572 706
dflet 0:305844973572 707 }
dflet 0:305844973572 708
dflet 0:305844973572 709 //*****************************************************************************
dflet 0:305844973572 710 //
dflet 0:305844973572 711 //! turnLedOff
dflet 0:305844973572 712 //!
dflet 0:305844973572 713 //! @param ledNum is the LED Number
dflet 0:305844973572 714 //!
dflet 0:305844973572 715 //! @return none
dflet 0:305844973572 716 //!
dflet 0:305844973572 717 //! @brief Turns a specific LED Off
dflet 0:305844973572 718 //
dflet 0:305844973572 719 //*****************************************************************************
dflet 0:305844973572 720 void turnLedOff(char ledNum)
dflet 0:305844973572 721 {
dflet 0:305844973572 722 switch(ledNum)
dflet 0:305844973572 723 {
dflet 0:305844973572 724 case 1:
dflet 0:305844973572 725 ind1 = 0;
dflet 0:305844973572 726 break;
dflet 0:305844973572 727 case 2:
dflet 0:305844973572 728 ind2 = 0;
dflet 0:305844973572 729 break;
dflet 0:305844973572 730 case 3:
dflet 0:305844973572 731 ind3 = 0;
dflet 0:305844973572 732 break;
dflet 0:305844973572 733 case 4:
dflet 0:305844973572 734 ind4 = 0;
dflet 0:305844973572 735 break;
dflet 0:305844973572 736 case 5:
dflet 0:305844973572 737 ind1 = 0;
dflet 0:305844973572 738 break;
dflet 0:305844973572 739 case 6:
dflet 0:305844973572 740 ind4 = 0;
dflet 0:305844973572 741 break;
dflet 0:305844973572 742 case 7:
dflet 0:305844973572 743
dflet 0:305844973572 744 break;
dflet 0:305844973572 745 case 8:
dflet 0:305844973572 746
dflet 0:305844973572 747 break;
dflet 0:305844973572 748 }
dflet 0:305844973572 749 }
dflet 0:305844973572 750
dflet 0:305844973572 751 //*****************************************************************************
dflet 0:305844973572 752 //
dflet 0:305844973572 753 //! toggleLed
dflet 0:305844973572 754 //!
dflet 0:305844973572 755 //! @param ledNum is the LED Number
dflet 0:305844973572 756 //!
dflet 0:305844973572 757 //! @return none
dflet 0:305844973572 758 //!
dflet 0:305844973572 759 //! @brief Toggles a board LED
dflet 0:305844973572 760 //
dflet 0:305844973572 761 //*****************************************************************************
dflet 0:305844973572 762
dflet 0:305844973572 763 void toggleLed(char ledNum)
dflet 0:305844973572 764 {
dflet 0:305844973572 765 switch(ledNum)
dflet 0:305844973572 766 {
dflet 0:305844973572 767 case 1:
dflet 0:305844973572 768 ind1 = 0;
dflet 0:305844973572 769 break;
dflet 0:305844973572 770 case 2:
dflet 0:305844973572 771 ind2 = 0;
dflet 0:305844973572 772 break;
dflet 0:305844973572 773 case 3:
dflet 0:305844973572 774 ind3 = 0;
dflet 0:305844973572 775 break;
dflet 0:305844973572 776 case 4:
dflet 0:305844973572 777 ind4 = 0;
dflet 0:305844973572 778 break;
dflet 0:305844973572 779 case 5:
dflet 0:305844973572 780 ind1 = 0;
dflet 0:305844973572 781 break;
dflet 0:305844973572 782 case 6:
dflet 0:305844973572 783 ind4 = 0;
dflet 0:305844973572 784 break;
dflet 0:305844973572 785 case 7:
dflet 0:305844973572 786
dflet 0:305844973572 787 break;
dflet 0:305844973572 788 case 8:
dflet 0:305844973572 789
dflet 0:305844973572 790 break;
dflet 0:305844973572 791 }
dflet 0:305844973572 792
dflet 0:305844973572 793 }