Library for inclusion of main.cpp -forked
Dependents: df-2014-workshop-rfid-case-generator-k64f df-2014-rfid-case-gen-k64f-complete df-2014-rfid-case-gen-k64f-exercise
Fork of EndpointMain by
main.cpp@3:1d8589377a86, 2014-10-07 (annotated)
- Committer:
- ansond
- Date:
- Tue Oct 07 18:42:47 2014 +0000
- Revision:
- 3:1d8589377a86
- Parent:
- 2:55b1877a3a14
updates for mac generation on K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:8f99b3f2c3a8 | 1 | /* Copyright C2014 ARM, MIT License |
ansond | 0:8f99b3f2c3a8 | 2 | * |
ansond | 2:55b1877a3a14 | 3 | * Author: Doug Anson (doug.anson@arm.com) |
ansond | 2:55b1877a3a14 | 4 | * |
ansond | 0:8f99b3f2c3a8 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:8f99b3f2c3a8 | 6 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 0:8f99b3f2c3a8 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:8f99b3f2c3a8 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:8f99b3f2c3a8 | 9 | * furnished to do so, subject to the following conditions: |
ansond | 0:8f99b3f2c3a8 | 10 | * |
ansond | 0:8f99b3f2c3a8 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:8f99b3f2c3a8 | 12 | * substantial portions of the Software. |
ansond | 0:8f99b3f2c3a8 | 13 | * |
ansond | 0:8f99b3f2c3a8 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:8f99b3f2c3a8 | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:8f99b3f2c3a8 | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:8f99b3f2c3a8 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:8f99b3f2c3a8 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:8f99b3f2c3a8 | 19 | */ |
ansond | 2:55b1877a3a14 | 20 | |
ansond | 0:8f99b3f2c3a8 | 21 | // our Serial port |
ansond | 0:8f99b3f2c3a8 | 22 | #include "BufferedSerial.h" |
ansond | 0:8f99b3f2c3a8 | 23 | BufferedSerial pc(USBTX, USBRX); |
ansond | 0:8f99b3f2c3a8 | 24 | |
ansond | 0:8f99b3f2c3a8 | 25 | // Ethernet |
ansond | 0:8f99b3f2c3a8 | 26 | #include "EthernetInterface.h" |
ansond | 0:8f99b3f2c3a8 | 27 | EthernetInterface ethernet; |
ansond | 0:8f99b3f2c3a8 | 28 | |
ansond | 0:8f99b3f2c3a8 | 29 | // HTTP |
ansond | 0:8f99b3f2c3a8 | 30 | #include "HTTPClient.h" |
ansond | 0:8f99b3f2c3a8 | 31 | HTTPClient http; |
ansond | 0:8f99b3f2c3a8 | 32 | |
ansond | 2:55b1877a3a14 | 33 | // StatusReporter |
ansond | 2:55b1877a3a14 | 34 | #include "StatusReporter.h" |
ansond | 3:1d8589377a86 | 35 | |
ansond | 3:1d8589377a86 | 36 | #if _K64F_PLATFORM |
ansond | 3:1d8589377a86 | 37 | // the K64F does not have a unique MAC address - so we must give it one... |
ansond | 3:1d8589377a86 | 38 | extern "C" void mbed_mac_address(char *mac) |
ansond | 3:1d8589377a86 | 39 | { |
ansond | 3:1d8589377a86 | 40 | // Fetch word 0 |
ansond | 3:1d8589377a86 | 41 | uint32_t word0 = *(uint32_t *)0x40048060; |
ansond | 3:1d8589377a86 | 42 | // Fetch word 1 |
ansond | 3:1d8589377a86 | 43 | // we only want bottom 16 bits of word1 (MAC bits 32-47) |
ansond | 3:1d8589377a86 | 44 | // and bit 9 forced to 1, bit 8 forced to 0 |
ansond | 3:1d8589377a86 | 45 | // Locally administered MAC, reduced conflicts |
ansond | 3:1d8589377a86 | 46 | // http://en.wikipedia.org/wiki/MAC_address |
ansond | 3:1d8589377a86 | 47 | uint32_t word1 = *(uint32_t *)0x4004805C; |
ansond | 3:1d8589377a86 | 48 | word1 |= 0x00000200; |
ansond | 3:1d8589377a86 | 49 | word1 &= 0x0000FEFF; |
ansond | 3:1d8589377a86 | 50 | |
ansond | 3:1d8589377a86 | 51 | mac[0] = (word1 & 0x000000ff); |
ansond | 3:1d8589377a86 | 52 | mac[1] = (word1 & 0x0000ff00) >> 8; |
ansond | 3:1d8589377a86 | 53 | mac[2] = (word0 & 0xff000000) >> 24; |
ansond | 3:1d8589377a86 | 54 | mac[3] = (word0 & 0x00ff0000) >> 16; |
ansond | 3:1d8589377a86 | 55 | mac[4] = (word0 & 0x0000ff00) >> 8; |
ansond | 3:1d8589377a86 | 56 | mac[5] = (word0 & 0x000000ff); |
ansond | 3:1d8589377a86 | 57 | } |
ansond | 3:1d8589377a86 | 58 | #endif // _K64F_PLATFORM |
ansond | 2:55b1877a3a14 | 59 | |
ansond | 0:8f99b3f2c3a8 | 60 | // Main Task... |
ansond | 0:8f99b3f2c3a8 | 61 | void mainTask(void const *v) { |
ansond | 0:8f99b3f2c3a8 | 62 | // create our object instances |
ansond | 2:55b1877a3a14 | 63 | Logger logger(&pc,NULL); |
ansond | 0:8f99b3f2c3a8 | 64 | |
ansond | 0:8f99b3f2c3a8 | 65 | // announce |
ansond | 0:8f99b3f2c3a8 | 66 | logger.log("ARM/DreamForce 2014 mbed Status Reporter v%s",APP_VERSION); |
ansond | 0:8f99b3f2c3a8 | 67 | logger.turnLEDBlue(); |
ansond | 0:8f99b3f2c3a8 | 68 | |
ansond | 0:8f99b3f2c3a8 | 69 | // initialize Ethernet |
ansond | 0:8f99b3f2c3a8 | 70 | logger.log("Initializing Ethernet..."); |
ansond | 0:8f99b3f2c3a8 | 71 | ethernet.init(); |
ansond | 0:8f99b3f2c3a8 | 72 | |
ansond | 0:8f99b3f2c3a8 | 73 | // get a DHCP address and bring the network interface up |
ansond | 0:8f99b3f2c3a8 | 74 | logger.log("Getting IP Address..."); |
ansond | 0:8f99b3f2c3a8 | 75 | logger.turnLEDOrange(); |
ansond | 0:8f99b3f2c3a8 | 76 | if (ethernet.connect() == 0) { |
ansond | 0:8f99b3f2c3a8 | 77 | // log our IP address (DHCP) |
ansond | 0:8f99b3f2c3a8 | 78 | logger.log("IP Address: %s",ethernet.getIPAddress()); |
ansond | 0:8f99b3f2c3a8 | 79 | |
ansond | 0:8f99b3f2c3a8 | 80 | // create the StatusReporter |
ansond | 2:55b1877a3a14 | 81 | StatusReporter status_reporter(&http,&logger); |
ansond | 0:8f99b3f2c3a8 | 82 | |
ansond | 0:8f99b3f2c3a8 | 83 | // entering main loop |
ansond | 0:8f99b3f2c3a8 | 84 | logger.log("Entering Main Loop...\r\nScanning..."); |
ansond | 0:8f99b3f2c3a8 | 85 | logger.turnLEDGreen(); |
ansond | 0:8f99b3f2c3a8 | 86 | |
ansond | 0:8f99b3f2c3a8 | 87 | // Enter the main loop |
ansond | 0:8f99b3f2c3a8 | 88 | while(true) { |
ansond | 0:8f99b3f2c3a8 | 89 | // check and report on status updates |
ansond | 0:8f99b3f2c3a8 | 90 | status_reporter.checkAndReportOnStatus(); |
ansond | 2:55b1877a3a14 | 91 | } |
ansond | 0:8f99b3f2c3a8 | 92 | } |
ansond | 0:8f99b3f2c3a8 | 93 | else { |
ansond | 0:8f99b3f2c3a8 | 94 | logger.log("No Network... Exiting..."); |
ansond | 0:8f99b3f2c3a8 | 95 | logger.turnLEDRed(); |
ansond | 0:8f99b3f2c3a8 | 96 | exit(1); |
ansond | 0:8f99b3f2c3a8 | 97 | } |
ansond | 0:8f99b3f2c3a8 | 98 | |
ansond | 0:8f99b3f2c3a8 | 99 | // disconnect |
ansond | 0:8f99b3f2c3a8 | 100 | logger.log("Disconnecting..."); |
ansond | 0:8f99b3f2c3a8 | 101 | logger.turnLEDOrange(); |
ansond | 0:8f99b3f2c3a8 | 102 | ethernet.disconnect(); |
ansond | 0:8f99b3f2c3a8 | 103 | |
ansond | 0:8f99b3f2c3a8 | 104 | // Exit |
ansond | 0:8f99b3f2c3a8 | 105 | logger.log("Exiting..."); |
ansond | 0:8f99b3f2c3a8 | 106 | logger.turnLEDBlue(); |
ansond | 0:8f99b3f2c3a8 | 107 | exit(1); |
ansond | 0:8f99b3f2c3a8 | 108 | } |
ansond | 0:8f99b3f2c3a8 | 109 | |
ansond | 0:8f99b3f2c3a8 | 110 | // main entry |
ansond | 0:8f99b3f2c3a8 | 111 | int main() { |
ansond | 2:55b1877a3a14 | 112 | Thread workerTask(mainTask, NULL, osPriorityNormal, STACK_SIZE); |
ansond | 2:55b1877a3a14 | 113 | while (true) { |
ansond | 0:8f99b3f2c3a8 | 114 | Thread::wait(10*WAIT_TIME_MS); |
ansond | 2:55b1877a3a14 | 115 | } |
ansond | 0:8f99b3f2c3a8 | 116 | } |