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 Doug Anson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright C2014 ARM, MIT License
00002  *
00003  * Author: Doug Anson (doug.anson@arm.com)
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00006  * and associated documentation files the "Software", to deal in the Software without restriction,
00007  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00008  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in all copies or
00012  * substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00015  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00016  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00017  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00019  */
00020   
00021  // our Serial port
00022  #include "BufferedSerial.h"
00023  BufferedSerial pc(USBTX, USBRX);
00024  
00025  // Ethernet
00026  #include "EthernetInterface.h"
00027  EthernetInterface ethernet;
00028  
00029  // HTTP 
00030  #include "HTTPClient.h"
00031  HTTPClient http;
00032  
00033  // StatusReporter
00034  #include "StatusReporter.h"
00035  
00036  #if _K64F_PLATFORM
00037  // the K64F does not have a unique MAC address - so we must give it one... 
00038  extern "C" void mbed_mac_address(char *mac)
00039  {
00040     // Fetch word 0
00041     uint32_t word0 = *(uint32_t *)0x40048060;
00042     // Fetch word 1
00043     // we only want bottom 16 bits of word1 (MAC bits 32-47)
00044     // and bit 9 forced to 1, bit 8 forced to 0
00045     // Locally administered MAC, reduced conflicts
00046     // http://en.wikipedia.org/wiki/MAC_address
00047     uint32_t word1 = *(uint32_t *)0x4004805C;
00048     word1 |= 0x00000200;
00049     word1 &= 0x0000FEFF;
00050     
00051     mac[0] = (word1 & 0x000000ff);
00052     mac[1] = (word1 & 0x0000ff00) >> 8;
00053     mac[2] = (word0 & 0xff000000) >> 24;
00054     mac[3] = (word0 & 0x00ff0000) >> 16;
00055     mac[4] = (word0 & 0x0000ff00) >> 8;
00056     mac[5] = (word0 & 0x000000ff);
00057  }
00058  #endif // _K64F_PLATFORM
00059   
00060  // Main Task...
00061  void mainTask(void const *v) {
00062     // create our object instances 
00063     Logger logger(&pc,NULL);
00064     
00065     // announce
00066     logger.log("ARM/DreamForce 2014 mbed Status Reporter v%s",APP_VERSION);
00067     logger.turnLEDBlue();
00068     
00069     // initialize Ethernet
00070     logger.log("Initializing Ethernet...");
00071     ethernet.init();
00072     
00073     // get a DHCP address and bring the network interface up
00074     logger.log("Getting IP Address...");
00075     logger.turnLEDOrange();
00076     if (ethernet.connect() == 0) {
00077         // log our IP address (DHCP)
00078         logger.log("IP Address: %s",ethernet.getIPAddress());
00079         
00080         // create the StatusReporter
00081         StatusReporter status_reporter(&http,&logger);
00082         
00083         // entering main loop
00084         logger.log("Entering Main Loop...\r\nScanning...");
00085         logger.turnLEDGreen();
00086         
00087         // Enter the main loop
00088         while(true) {
00089              // check and report on status updates
00090              status_reporter.checkAndReportOnStatus();
00091         }
00092      }
00093      else {
00094          logger.log("No Network... Exiting...");
00095          logger.turnLEDRed();
00096          exit(1);
00097      }
00098      
00099      // disconnect
00100      logger.log("Disconnecting...");
00101      logger.turnLEDOrange();
00102      ethernet.disconnect();
00103      
00104      // Exit
00105      logger.log("Exiting...");
00106      logger.turnLEDBlue();
00107      exit(1);
00108   }
00109   
00110   // main entry
00111   int main() {
00112      Thread workerTask(mainTask, NULL, osPriorityNormal, STACK_SIZE);
00113      while (true) {
00114         Thread::wait(10*WAIT_TIME_MS);
00115      }
00116   }