Ted Grosch / Mbed 2 deprecated Nucleo_TOF_I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hostController.cpp Source File

hostController.cpp

Go to the documentation of this file.
00001 /*!
00002 * \file hostController.cpp
00003 * \author  Karthik Rajagopal <krthik@ti.com>
00004 * \version 0.9.1
00005 *
00006 * \section COPYRIGHT
00007 * TEXAS INSTRUMENTS TEXT FILE LICENSE
00008 * Copyright (c) 2018 Texas Instruments Incorporated
00009 * All rights reserved not granted herein.
00010 * Limited License.
00011 * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive license under copyrights and patents it now or hereafter owns or controls to make, have made, use, import, offer to sell and sell ("Utilize") this software subject to the terms herein.  With respect to the foregoing patent license, such license is granted  solely to the extent that any such patent is necessary to Utilize the software alone.  The patent license shall not apply to any combinations which include this software, other than combinations with devices manufactured by or for TI ("TI Devices").  No hardware patent is licensed hereunder.
00012 * Redistributions must preserve existing copyright notices and reproduce this license (including the above copyright notice and the disclaimer and (if applicable) source code license limitations below) in the documentation and/or other materials provided with the distribution
00013 * Redistribution and use in binary form, without modification, are permitted provided that the following conditions are met:
00014 * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any software provided in binary form.
00015 * * any redistribution and use are licensed by TI for use only with TI Devices.
00016 * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
00017 * If software source code is provided to you, modification and redistribution of the source code are permitted provided that the following conditions are met:
00018 * * any redistribution and use of the source code, including any resulting derivative works, are licensed by TI for use only with TI Devices.
00019 * * any redistribution and use of any object code compiled from the source code and any resulting derivative works, are licensed by TI for use only with TI Devices.
00020 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or promote products derived from this software without specific prior written permission.
00021 * DISCLAIMER.
00022 * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00023 *
00024 * \section DESCRIPTION
00025 * This file contains the hostController class methods
00026 */
00027 #include "mbed.h"
00028 #include "hostController.h "
00029 #include "string.h"
00030 #include <stdarg.h>
00031 
00032 //#ifdef _WIN32
00033 //#ifdef OPT3101_USE_SERIALLIB
00034 //#define WINPAUSE system("pause")
00035 //#endif
00036 //#endif
00037 
00038 
00039 hostController host;
00040 
00041 
00042 //#ifdef OPT3101_USE_SERIALLIB
00043 /** \brief Serial Command Port  declaration
00044 This global variable declaration with name OPT3101commandPort of class serial::Serial is used by class like OPT3101::deviceRegister for I2C read and writes.
00045 */
00046 //serial::Serial OPT3101commandPort("COM4", 9600, serial::Timeout::simpleTimeout(1000));
00047 //#ifdef linux
00048 //serial::Serial OPT3101I2CCommandPort("/dev/ttyACM0", 9600, serial::Timeout::simpleTimeout(1000));
00049 //#endif
00050 
00051 //#ifdef _WIN32
00052 //serial::Serial OPT3101I2CCommandPort("COM20", 9600, serial::Timeout::simpleTimeout(1000));
00053 //#endif //WIN32
00054 //#endif //SERIALLIB
00055 
00056 
00057 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00058 volatile uint8_t bCDCDataReceived_event = false; // Indicates data has been rx'ed without an open rx operation
00059 volatile uint8_t bDataReceiveCompleted_event = false;
00060 volatile uint8_t bDataSendCompleted_event = false;
00061 //#endif
00062 
00063 hostController::hostController(void){
00064 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00065     this->initialize();
00066 //#endif;
00067 }
00068 
00069 void hostController::writeI2C(uint8_t address, uint32_t data) {
00070     /// <b>Algorithm of the method is as follows</b>
00071 //#if defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
00072 //  std::string returnValue;
00073 //  char writeData[20];
00074 //  sprintf(writeData, "REGWx%02xx%06x\r", address, data); /// * Creates WRITE I2C command to send to h/w with address and data specified in arguments
00075 //  OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Writes the WRITE I2C command to h/w
00076 //  returnValue = OPT3101I2CCommandPort.readline();
00077 //#endif
00078 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00079     this->i2c.write(address,data);
00080 //#endif
00081 
00082 }
00083 uint32_t hostController::readI2C(uint8_t address) {
00084     uint32_t i2cReadValue=0;
00085 //#if defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
00086 //  char writeData[10];
00087     //std::string returnValue;
00088 //  uint8_t c0;
00089     
00090     /// <b>Algorithm of the method is as follows</b>
00091 //  sprintf(writeData, "REGRx%02x\r", address); /// * Creates READ I2C command to send to h/w with address and data specified in arguments
00092 
00093 //  i2cReadValue = 0;
00094 
00095 //  OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Writes the READ I2C command to h/w
00096 //  returnValue = OPT3101I2CCommandPort.readline(); /// * Waits and receives response from h/w
00097 //  if (returnValue.length() == 18) {
00098 //      returnValue = returnValue.substr(10, 6);
00099 //      c0 = 0;
00100 //      for (std::string::iterator it = returnValue.begin(); it != returnValue.end(); ++it) { /// * Converters the received string output from h/w to uint32_t value
00101 //          if (*it >= '0' && *it <= '9')
00102 //              i2cReadValue += ((*it) - 48) << ((5 - c0) * 4);
00103 //          else if (*it >= 'A' && *it <= 'F')
00104 //              i2cReadValue += ((*it) - 65 + 10) << ((5 - c0) * 4);
00105 //          else if (*it >= 'a' && *it <= 'f')
00106 //              i2cReadValue += ((*it) - 97 + 10) << ((5 - c0) * 4);
00107 //          c0++;
00108 //      }
00109 //  }
00110 //#endif
00111 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00112     this->i2c.read(address,&i2cReadValue);
00113 //#endif
00114     return i2cReadValue; /// * Returns the data in uint32_t format
00115 }
00116 
00117 void hostController::sleep(uint32_t timeInMilliSeconds) {
00118     /// <b>Algorithm of the method is as follows</b>
00119 
00120 //#if defined(HOST_PC) && defined(_WIN32) && defined(OPT3101_USE_SERIALLIB) 
00121 //  Sleep(timeInMilliSeconds);/// * Sleeps for the time specified in the argument timeInMilliSeconds
00122 //#endif
00123 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00124 //  uint32_t c;
00125 //  for(c=0;c<timeInMilliSeconds;c++)
00126 //      __delay_cycles(24000);
00127 //
00128 //#endif
00129     wait_ms(timeInMilliSeconds);
00130 
00131 }
00132 void hostController::sleepDataReadyCounts(uint16_t dataReadyCounts) {
00133     /// <b>Algorithm of the method is as follows</b>
00134     /// * Currently empty function needs to be implemented by user. Based on interrupts from data ready signal from OPT3101, the host needs to count those pulses and wait until dataReadyCounts have reached. 
00135 }
00136 
00137 void hostController::pause()
00138 {
00139     /// <b>Algorithm of the method is as follows</b>
00140     char c;
00141     
00142     host.printf("Press any Key to continue:\r\n");
00143     gets(&c);
00144 
00145 //#if defined(HOST_PC) && defined(_WIN32) && defined(OPT3101_USE_SERIALLIB)
00146 //  WINPAUSE;   /// * Pause for user input
00147 //#endif
00148 //
00149 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00150 //    while(!bCDCDataReceived_event);
00151 //  bCDCDataReceived_event=false;
00152 //    if(USBCDC_getBytesInUSBBuffer(CDC0_INTFNUM))
00153  //       USBCDC_rejectData(CDC0_INTFNUM);
00154 
00155 //#endif
00156 
00157 }
00158 
00159 
00160 void hostController::printfSetColor(uint8_t color){
00161 #ifdef PRINT_COLOR_TERMINAL
00162     switch(color){
00163         case 0xFF:
00164             host.printf("\u001b[0m");
00165             break;
00166         case 0b000:
00167             host.printf("\u001b[30m");
00168             break;
00169         case 0b100:
00170             host.printf("\u001b[31m");
00171             break;
00172         case 0b010:
00173             host.printf("\u001b[32m");
00174             break;
00175         case 0b001:
00176             host.printf("\u001b[34m");
00177             break;
00178         case 0b110:
00179             host.printf("\u001b[33m");
00180             break;
00181         case 0b101:
00182             host.printf("\u001b[35m");
00183             break;
00184         case 0b011:
00185             host.printf("\u001b[36m");
00186             break;
00187         case 0b111:
00188             host.printf("\u001b[37m");
00189             break;
00190         default:
00191             host.printf("\u001b[0m");
00192             break;
00193     }
00194 #endif
00195 }
00196 
00197 
00198 
00199 void hostController::resetDevice() {
00200     // These comments rest the device on power-up
00201 #if defined(HOST_PC) && defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
00202     char writeData[10];
00203     /// <b>Algorithm of the method is as follows</b>
00204     sprintf(writeData, "DEVR\r"); /// * Creates a command which specifies host to send RESET Pulse to OPT3101 h/w 
00205     OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Send the RESET command to the h/w
00206 #endif
00207 #ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00208     this->gpio.rstz=0;
00209     this->sleep(1);
00210     this->gpio.rstz=1;
00211 #endif
00212 
00213 }
00214 
00215 void hostController::initialize(){
00216 #if defined(HOST_PC) && defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
00217     char writeData[16];
00218     /// <b>Algorithm of the method is as follows</b>
00219     sprintf(writeData, "DEVAx%02x\r", OPT3101_I2C_SLAVEADDRESS); /// * Creates a command which specifies host to send RESET Pulse to OPT3101 h/w 
00220     OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Send the RESET command to the h/w
00221 #endif
00222 
00223 #ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00224     WDT_A_hold(WDT_A_BASE);
00225     USB_setup(true,true); // Enables the USP with event handling
00226     PMM_setVCore(PMM_CORE_LEVEL_3); // Minumum Vcore setting required for the USB API is PMM_CORE_LEVEL_2 .
00227     USBHAL_initClocks(24000000);     // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz
00228     UCS_turnOnXT2(UCS_XT2_DRIVE_4MHZ_8MHZ); // Turns the XT2 crystal clock ON
00229     UCS_turnOnSMCLK(); // Turns the SMCLK On
00230     UCS_initClockSignal(
00231                      UCS_ACLK,
00232                      UCS_XT2CLK_SELECT,
00233                      UCS_CLOCK_DIVIDER_32); // Enabling clock to come on pin 14 P1.0/ACLK. This enables 4MHz/32 which is 125KHz on the ACLK pin
00234     this->i2c.init();
00235     this->gpio.init();
00236     this->gpio.rstz=1;
00237 
00238     __enable_interrupt();  // Enable interrupts globally
00239 #endif
00240 
00241 
00242 }
00243 
00244 
00245 void hostController::printf(const char *fmt, ...){ //TODO use the Serial pc(SERIAL_TX, SERIAL_RX) here
00246 #ifdef VERBOSE_MODE
00247     va_list args;
00248     va_start(args, fmt);
00249     char oBuffer[256];
00250     vsnprintf(oBuffer, sizeof oBuffer, fmt, args);
00251 
00252 #ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00253     USBCDC_sendDataInBackground((uint8_t*)oBuffer,strlen(oBuffer),CDC0_INTFNUM,0xFF);
00254     __delay_cycles(24000);
00255 #endif
00256 #ifdef HOST_PC
00257     std::printf("%s",oBuffer);
00258 #endif
00259     va_end(args);
00260 #endif // VERBOSE MODE
00261 
00262 }
00263 
00264