Export to MBED Studio

Dependencies:   mbed

Committer:
tgrosch
Date:
Sun Oct 25 00:58:24 2020 +0000
Revision:
0:62b846b3988a
Child:
1:8567a29d4699
First successful compile.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tgrosch 0:62b846b3988a 1 /*!
tgrosch 0:62b846b3988a 2 * \file hostController.cpp
tgrosch 0:62b846b3988a 3 * \author Karthik Rajagopal <krthik@ti.com>
tgrosch 0:62b846b3988a 4 * \version 0.9.1
tgrosch 0:62b846b3988a 5 *
tgrosch 0:62b846b3988a 6 * \section COPYRIGHT
tgrosch 0:62b846b3988a 7 * TEXAS INSTRUMENTS TEXT FILE LICENSE
tgrosch 0:62b846b3988a 8 * Copyright (c) 2018 Texas Instruments Incorporated
tgrosch 0:62b846b3988a 9 * All rights reserved not granted herein.
tgrosch 0:62b846b3988a 10 * Limited License.
tgrosch 0:62b846b3988a 11 * 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.
tgrosch 0:62b846b3988a 12 * 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
tgrosch 0:62b846b3988a 13 * Redistribution and use in binary form, without modification, are permitted provided that the following conditions are met:
tgrosch 0:62b846b3988a 14 * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any software provided in binary form.
tgrosch 0:62b846b3988a 15 * * any redistribution and use are licensed by TI for use only with TI Devices.
tgrosch 0:62b846b3988a 16 * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
tgrosch 0:62b846b3988a 17 * If software source code is provided to you, modification and redistribution of the source code are permitted provided that the following conditions are met:
tgrosch 0:62b846b3988a 18 * * any redistribution and use of the source code, including any resulting derivative works, are licensed by TI for use only with TI Devices.
tgrosch 0:62b846b3988a 19 * * 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.
tgrosch 0:62b846b3988a 20 * 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.
tgrosch 0:62b846b3988a 21 * DISCLAIMER.
tgrosch 0:62b846b3988a 22 * 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.
tgrosch 0:62b846b3988a 23 *
tgrosch 0:62b846b3988a 24 * \section DESCRIPTION
tgrosch 0:62b846b3988a 25 * This file contains the hostController class methods
tgrosch 0:62b846b3988a 26 */
tgrosch 0:62b846b3988a 27 #include "mbed.h"
tgrosch 0:62b846b3988a 28 #include "hostController.h"
tgrosch 0:62b846b3988a 29 #include "string.h"
tgrosch 0:62b846b3988a 30 #include <stdarg.h>
tgrosch 0:62b846b3988a 31
tgrosch 0:62b846b3988a 32 //#ifdef _WIN32
tgrosch 0:62b846b3988a 33 //#ifdef OPT3101_USE_SERIALLIB
tgrosch 0:62b846b3988a 34 //#define WINPAUSE system("pause")
tgrosch 0:62b846b3988a 35 //#endif
tgrosch 0:62b846b3988a 36 //#endif
tgrosch 0:62b846b3988a 37
tgrosch 0:62b846b3988a 38
tgrosch 0:62b846b3988a 39 hostController host;
tgrosch 0:62b846b3988a 40
tgrosch 0:62b846b3988a 41
tgrosch 0:62b846b3988a 42 //#ifdef OPT3101_USE_SERIALLIB
tgrosch 0:62b846b3988a 43 /** \brief Serial Command Port declaration
tgrosch 0:62b846b3988a 44 This global variable declaration with name OPT3101commandPort of class serial::Serial is used by class like OPT3101::deviceRegister for I2C read and writes.
tgrosch 0:62b846b3988a 45 */
tgrosch 0:62b846b3988a 46 //serial::Serial OPT3101commandPort("COM4", 9600, serial::Timeout::simpleTimeout(1000));
tgrosch 0:62b846b3988a 47 //#ifdef linux
tgrosch 0:62b846b3988a 48 //serial::Serial OPT3101I2CCommandPort("/dev/ttyACM0", 9600, serial::Timeout::simpleTimeout(1000));
tgrosch 0:62b846b3988a 49 //#endif
tgrosch 0:62b846b3988a 50
tgrosch 0:62b846b3988a 51 //#ifdef _WIN32
tgrosch 0:62b846b3988a 52 //serial::Serial OPT3101I2CCommandPort("COM20", 9600, serial::Timeout::simpleTimeout(1000));
tgrosch 0:62b846b3988a 53 //#endif //WIN32
tgrosch 0:62b846b3988a 54 //#endif //SERIALLIB
tgrosch 0:62b846b3988a 55
tgrosch 0:62b846b3988a 56
tgrosch 0:62b846b3988a 57 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 58 volatile uint8_t bCDCDataReceived_event = false; // Indicates data has been rx'ed without an open rx operation
tgrosch 0:62b846b3988a 59 volatile uint8_t bDataReceiveCompleted_event = false;
tgrosch 0:62b846b3988a 60 volatile uint8_t bDataSendCompleted_event = false;
tgrosch 0:62b846b3988a 61 //#endif
tgrosch 0:62b846b3988a 62
tgrosch 0:62b846b3988a 63 hostController::hostController(void){
tgrosch 0:62b846b3988a 64 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 65 this->initialize();
tgrosch 0:62b846b3988a 66 //#endif;
tgrosch 0:62b846b3988a 67 }
tgrosch 0:62b846b3988a 68
tgrosch 0:62b846b3988a 69 void hostController::writeI2C(uint8_t address, uint32_t data) {
tgrosch 0:62b846b3988a 70 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 71 //#if defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
tgrosch 0:62b846b3988a 72 // std::string returnValue;
tgrosch 0:62b846b3988a 73 // char writeData[20];
tgrosch 0:62b846b3988a 74 // sprintf(writeData, "REGWx%02xx%06x\r", address, data); /// * Creates WRITE I2C command to send to h/w with address and data specified in arguments
tgrosch 0:62b846b3988a 75 // OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Writes the WRITE I2C command to h/w
tgrosch 0:62b846b3988a 76 // returnValue = OPT3101I2CCommandPort.readline();
tgrosch 0:62b846b3988a 77 //#endif
tgrosch 0:62b846b3988a 78 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 79 this->i2c.write(address,data);
tgrosch 0:62b846b3988a 80 //#endif
tgrosch 0:62b846b3988a 81
tgrosch 0:62b846b3988a 82 }
tgrosch 0:62b846b3988a 83 uint32_t hostController::readI2C(uint8_t address) {
tgrosch 0:62b846b3988a 84 uint32_t i2cReadValue=0;
tgrosch 0:62b846b3988a 85 //#if defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
tgrosch 0:62b846b3988a 86 // char writeData[10];
tgrosch 0:62b846b3988a 87 //std::string returnValue;
tgrosch 0:62b846b3988a 88 // uint8_t c0;
tgrosch 0:62b846b3988a 89
tgrosch 0:62b846b3988a 90 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 91 // sprintf(writeData, "REGRx%02x\r", address); /// * Creates READ I2C command to send to h/w with address and data specified in arguments
tgrosch 0:62b846b3988a 92
tgrosch 0:62b846b3988a 93 // i2cReadValue = 0;
tgrosch 0:62b846b3988a 94
tgrosch 0:62b846b3988a 95 // OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Writes the READ I2C command to h/w
tgrosch 0:62b846b3988a 96 // returnValue = OPT3101I2CCommandPort.readline(); /// * Waits and receives response from h/w
tgrosch 0:62b846b3988a 97 // if (returnValue.length() == 18) {
tgrosch 0:62b846b3988a 98 // returnValue = returnValue.substr(10, 6);
tgrosch 0:62b846b3988a 99 // c0 = 0;
tgrosch 0:62b846b3988a 100 // for (std::string::iterator it = returnValue.begin(); it != returnValue.end(); ++it) { /// * Converters the received string output from h/w to uint32_t value
tgrosch 0:62b846b3988a 101 // if (*it >= '0' && *it <= '9')
tgrosch 0:62b846b3988a 102 // i2cReadValue += ((*it) - 48) << ((5 - c0) * 4);
tgrosch 0:62b846b3988a 103 // else if (*it >= 'A' && *it <= 'F')
tgrosch 0:62b846b3988a 104 // i2cReadValue += ((*it) - 65 + 10) << ((5 - c0) * 4);
tgrosch 0:62b846b3988a 105 // else if (*it >= 'a' && *it <= 'f')
tgrosch 0:62b846b3988a 106 // i2cReadValue += ((*it) - 97 + 10) << ((5 - c0) * 4);
tgrosch 0:62b846b3988a 107 // c0++;
tgrosch 0:62b846b3988a 108 // }
tgrosch 0:62b846b3988a 109 // }
tgrosch 0:62b846b3988a 110 //#endif
tgrosch 0:62b846b3988a 111 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 112 this->i2c.read(address,&i2cReadValue);
tgrosch 0:62b846b3988a 113 //#endif
tgrosch 0:62b846b3988a 114 return i2cReadValue; /// * Returns the data in uint32_t format
tgrosch 0:62b846b3988a 115 }
tgrosch 0:62b846b3988a 116
tgrosch 0:62b846b3988a 117 void hostController::sleep(uint32_t timeInMilliSeconds) {
tgrosch 0:62b846b3988a 118 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 119
tgrosch 0:62b846b3988a 120 //#if defined(HOST_PC) && defined(_WIN32) && defined(OPT3101_USE_SERIALLIB)
tgrosch 0:62b846b3988a 121 // Sleep(timeInMilliSeconds);/// * Sleeps for the time specified in the argument timeInMilliSeconds
tgrosch 0:62b846b3988a 122 //#endif
tgrosch 0:62b846b3988a 123 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 124 // uint32_t c;
tgrosch 0:62b846b3988a 125 // for(c=0;c<timeInMilliSeconds;c++)
tgrosch 0:62b846b3988a 126 // __delay_cycles(24000);
tgrosch 0:62b846b3988a 127 //
tgrosch 0:62b846b3988a 128 //#endif
tgrosch 0:62b846b3988a 129 wait_ms(timeInMilliSeconds);
tgrosch 0:62b846b3988a 130
tgrosch 0:62b846b3988a 131 }
tgrosch 0:62b846b3988a 132 void hostController::sleepDataReadyCounts(uint16_t dataReadyCounts) {
tgrosch 0:62b846b3988a 133 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 134 /// * 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.
tgrosch 0:62b846b3988a 135 }
tgrosch 0:62b846b3988a 136
tgrosch 0:62b846b3988a 137 void hostController::pause()
tgrosch 0:62b846b3988a 138 {
tgrosch 0:62b846b3988a 139 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 140 char c;
tgrosch 0:62b846b3988a 141
tgrosch 0:62b846b3988a 142 host.printf("Press any Key to continue:\r\n");
tgrosch 0:62b846b3988a 143 gets(&c);
tgrosch 0:62b846b3988a 144
tgrosch 0:62b846b3988a 145 //#if defined(HOST_PC) && defined(_WIN32) && defined(OPT3101_USE_SERIALLIB)
tgrosch 0:62b846b3988a 146 // WINPAUSE; /// * Pause for user input
tgrosch 0:62b846b3988a 147 //#endif
tgrosch 0:62b846b3988a 148 //
tgrosch 0:62b846b3988a 149 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 150 // while(!bCDCDataReceived_event);
tgrosch 0:62b846b3988a 151 // bCDCDataReceived_event=false;
tgrosch 0:62b846b3988a 152 // if(USBCDC_getBytesInUSBBuffer(CDC0_INTFNUM))
tgrosch 0:62b846b3988a 153 // USBCDC_rejectData(CDC0_INTFNUM);
tgrosch 0:62b846b3988a 154
tgrosch 0:62b846b3988a 155 //#endif
tgrosch 0:62b846b3988a 156
tgrosch 0:62b846b3988a 157 }
tgrosch 0:62b846b3988a 158
tgrosch 0:62b846b3988a 159
tgrosch 0:62b846b3988a 160 void hostController::printfSetColor(uint8_t color){
tgrosch 0:62b846b3988a 161 #ifdef PRINT_COLOR_TERMINAL
tgrosch 0:62b846b3988a 162 switch(color){
tgrosch 0:62b846b3988a 163 case 0xFF:
tgrosch 0:62b846b3988a 164 host.printf("\u001b[0m");
tgrosch 0:62b846b3988a 165 break;
tgrosch 0:62b846b3988a 166 case 0b000:
tgrosch 0:62b846b3988a 167 host.printf("\u001b[30m");
tgrosch 0:62b846b3988a 168 break;
tgrosch 0:62b846b3988a 169 case 0b100:
tgrosch 0:62b846b3988a 170 host.printf("\u001b[31m");
tgrosch 0:62b846b3988a 171 break;
tgrosch 0:62b846b3988a 172 case 0b010:
tgrosch 0:62b846b3988a 173 host.printf("\u001b[32m");
tgrosch 0:62b846b3988a 174 break;
tgrosch 0:62b846b3988a 175 case 0b001:
tgrosch 0:62b846b3988a 176 host.printf("\u001b[34m");
tgrosch 0:62b846b3988a 177 break;
tgrosch 0:62b846b3988a 178 case 0b110:
tgrosch 0:62b846b3988a 179 host.printf("\u001b[33m");
tgrosch 0:62b846b3988a 180 break;
tgrosch 0:62b846b3988a 181 case 0b101:
tgrosch 0:62b846b3988a 182 host.printf("\u001b[35m");
tgrosch 0:62b846b3988a 183 break;
tgrosch 0:62b846b3988a 184 case 0b011:
tgrosch 0:62b846b3988a 185 host.printf("\u001b[36m");
tgrosch 0:62b846b3988a 186 break;
tgrosch 0:62b846b3988a 187 case 0b111:
tgrosch 0:62b846b3988a 188 host.printf("\u001b[37m");
tgrosch 0:62b846b3988a 189 break;
tgrosch 0:62b846b3988a 190 default:
tgrosch 0:62b846b3988a 191 host.printf("\u001b[0m");
tgrosch 0:62b846b3988a 192 break;
tgrosch 0:62b846b3988a 193 }
tgrosch 0:62b846b3988a 194 #endif
tgrosch 0:62b846b3988a 195 }
tgrosch 0:62b846b3988a 196
tgrosch 0:62b846b3988a 197
tgrosch 0:62b846b3988a 198
tgrosch 0:62b846b3988a 199 void hostController::resetDevice() {
tgrosch 0:62b846b3988a 200 // These comments rest the device on power-up
tgrosch 0:62b846b3988a 201 #if defined(HOST_PC) && defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
tgrosch 0:62b846b3988a 202 char writeData[10];
tgrosch 0:62b846b3988a 203 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 204 sprintf(writeData, "DEVR\r"); /// * Creates a command which specifies host to send RESET Pulse to OPT3101 h/w
tgrosch 0:62b846b3988a 205 OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Send the RESET command to the h/w
tgrosch 0:62b846b3988a 206 #endif
tgrosch 0:62b846b3988a 207 #ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 208 this->gpio.rstz=0;
tgrosch 0:62b846b3988a 209 this->sleep(1);
tgrosch 0:62b846b3988a 210 this->gpio.rstz=1;
tgrosch 0:62b846b3988a 211 #endif
tgrosch 0:62b846b3988a 212
tgrosch 0:62b846b3988a 213 }
tgrosch 0:62b846b3988a 214
tgrosch 0:62b846b3988a 215 void hostController::initialize(){
tgrosch 0:62b846b3988a 216 #if defined(HOST_PC) && defined(OPT3101_USE_STDIOLIB) && defined(OPT3101_USE_SERIALLIB)
tgrosch 0:62b846b3988a 217 char writeData[16];
tgrosch 0:62b846b3988a 218 /// <b>Algorithm of the method is as follows</b>
tgrosch 0:62b846b3988a 219 sprintf(writeData, "DEVAx%02x\r", OPT3101_I2C_SLAVEADDRESS); /// * Creates a command which specifies host to send RESET Pulse to OPT3101 h/w
tgrosch 0:62b846b3988a 220 OPT3101I2CCommandPort.write((uint8_t*)writeData, strlen(writeData)); /// * Send the RESET command to the h/w
tgrosch 0:62b846b3988a 221 #endif
tgrosch 0:62b846b3988a 222
tgrosch 0:62b846b3988a 223 #ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 224 WDT_A_hold(WDT_A_BASE);
tgrosch 0:62b846b3988a 225 USB_setup(true,true); // Enables the USP with event handling
tgrosch 0:62b846b3988a 226 PMM_setVCore(PMM_CORE_LEVEL_3); // Minumum Vcore setting required for the USB API is PMM_CORE_LEVEL_2 .
tgrosch 0:62b846b3988a 227 USBHAL_initClocks(24000000); // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz
tgrosch 0:62b846b3988a 228 UCS_turnOnXT2(UCS_XT2_DRIVE_4MHZ_8MHZ); // Turns the XT2 crystal clock ON
tgrosch 0:62b846b3988a 229 UCS_turnOnSMCLK(); // Turns the SMCLK On
tgrosch 0:62b846b3988a 230 UCS_initClockSignal(
tgrosch 0:62b846b3988a 231 UCS_ACLK,
tgrosch 0:62b846b3988a 232 UCS_XT2CLK_SELECT,
tgrosch 0:62b846b3988a 233 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
tgrosch 0:62b846b3988a 234 this->i2c.init();
tgrosch 0:62b846b3988a 235 this->gpio.init();
tgrosch 0:62b846b3988a 236 this->gpio.rstz=1;
tgrosch 0:62b846b3988a 237
tgrosch 0:62b846b3988a 238 __enable_interrupt(); // Enable interrupts globally
tgrosch 0:62b846b3988a 239 #endif
tgrosch 0:62b846b3988a 240
tgrosch 0:62b846b3988a 241
tgrosch 0:62b846b3988a 242 }
tgrosch 0:62b846b3988a 243
tgrosch 0:62b846b3988a 244
tgrosch 0:62b846b3988a 245 void hostController::printf(const char *fmt, ...){
tgrosch 0:62b846b3988a 246 #ifdef VERBOSE_MODE
tgrosch 0:62b846b3988a 247 va_list args;
tgrosch 0:62b846b3988a 248 va_start(args, fmt);
tgrosch 0:62b846b3988a 249 char oBuffer[256];
tgrosch 0:62b846b3988a 250 vsnprintf(oBuffer, sizeof oBuffer, fmt, args);
tgrosch 0:62b846b3988a 251
tgrosch 0:62b846b3988a 252 #ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
tgrosch 0:62b846b3988a 253 USBCDC_sendDataInBackground((uint8_t*)oBuffer,strlen(oBuffer),CDC0_INTFNUM,0xFF);
tgrosch 0:62b846b3988a 254 __delay_cycles(24000);
tgrosch 0:62b846b3988a 255 #endif
tgrosch 0:62b846b3988a 256 #ifdef HOST_PC
tgrosch 0:62b846b3988a 257 std::printf("%s",oBuffer);
tgrosch 0:62b846b3988a 258 #endif
tgrosch 0:62b846b3988a 259 va_end(args);
tgrosch 0:62b846b3988a 260 #endif // VERBOSE MODE
tgrosch 0:62b846b3988a 261
tgrosch 0:62b846b3988a 262 }
tgrosch 0:62b846b3988a 263
tgrosch 0:62b846b3988a 264