Ted Grosch / Mbed 2 deprecated Nucleo_TOF_I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers register.cpp Source File

register.cpp

00001 /*
00002 * \file register.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 * The file contains class descriptions and methods for register control for OPT3101 device
00026 */
00027 
00028 
00029 #include "register.h "
00030 
00031 /** \brief hostController declaration
00032 This global variable declaration with name host of hostController is used by various methods to send specific control commands to the host
00033 */
00034 
00035 OPT3101::deviceRegister::deviceRegister(uint8_t size){
00036     /// <b>Algorithm of the method is as follows</b>
00037     this->size=size; /// * Assigns size from input argument to the class member OPT3101::deviceRegister::size
00038 }
00039 void OPT3101::deviceRegister::write(int32_t value){
00040     uint8_t i;
00041     uint32_t mask;
00042     uint32_t readValue;
00043     uint32_t writeValue;
00044     uint32_t regmask;
00045     uint8_t size;
00046     /// <b>Algorithm of the method is as follows</b>
00047     value=(uint32_t) value; /// * Converts the input value to uint32_t 
00048     for(i=0;i<this->size;i++){ /// * Loops though the number of OPT3101::deviceRegister::address fields based on OPT3101::deviceRegister::size
00049         readValue=this->read(i); /// * Invokes hostController::readI2C() method to get register value from h/w
00050         size=this->msb[i]+1-this->lsb[i];
00051         mask=~(((uint32_t) -1)<<(size));
00052         writeValue=value&mask;
00053         value=value>>size;
00054         regmask=(~((((uint32_t) -1)<<(this->msb[i]+1))^(((uint32_t) -1)<<this->lsb[i])))&0xFFFFFF; /// * Masks the bits for the new value to be written
00055         writeValue=(writeValue<<this->lsb[i])|(readValue&regmask);/// * Creates a new register value to be written to h/w based on created mask and data to be written
00056         if(readValue!=writeValue)
00057             this->writeI2C(this->address[i],writeValue); /// * Initiates hostController::writeI2C function with address and new register value
00058     }
00059 }
00060 
00061 void OPT3101::deviceRegister::operator=(int32_t value){
00062     this->write(value);
00063 }
00064 
00065 uint32_t OPT3101::deviceRegister::read(){
00066     int8_t i;
00067     uint32_t readValue;
00068     uint32_t regmask;
00069     uint8_t size;
00070     uint8_t bitPos;
00071     uint32_t returnValue;
00072 
00073     returnValue=0;
00074     bitPos=0;
00075     /// <b>Algorithm of the method is as follows</b>
00076     for(i=0;i<this->size;i++){ /// * Loops though the number of OPT3101::deviceRegister::address fields based on  OPT3101::deviceRegister::size
00077         readValue=this->read(i); ///    * Invokes hostController::readI2C() method to get register value from h/w
00078         size=this->msb[i]+1-this->lsb[i];
00079         regmask=(((((uint32_t) -1)<<(this->msb[i]+1))^(((uint32_t) -1)<<this->lsb[i])))&0xFFFFFF; /// * Masks the bits for the register value to be reported based on register positional information
00080         //printf("Read mask for register 0x%02x is 0x%08x\n",this->address[i],regmask);
00081         returnValue|=((readValue&regmask)>>this->lsb[i])<<bitPos; /// * Assembles the value of the register
00082         bitPos+=size;
00083     }
00084     //printf("Resolved Value is %d",returnValue);
00085     return returnValue; /// * Returns the value read from the h/w for the register name specified
00086 }
00087 
00088 uint32_t OPT3101::deviceRegister::read(uint8_t index){
00089     /// <b>Algorithm of the method is as follows</b>
00090     return this->readI2C(this->address[index]); /// * Returns the value read from the h/w for the register name specified
00091 }
00092 
00093 uint32_t OPT3101::deviceRegister::readI2C(uint8_t address){
00094     /// <b>Algorithm of the method is as follows</b>
00095     return host.readI2C(address); /// * Invokes hostController::readI2C and returns the value read from the h/w d
00096 }
00097 
00098 void OPT3101::deviceRegister::writeI2C(uint8_t address,uint32_t data){
00099     /// <b>Algorithm of the method is as follows</b>
00100     host.writeI2C(address, data); /// * Invokes hostController::writeI2C with address and data specified as arguments
00101 }
00102 
00103 
00104 OPT3101::deviceRegister2::deviceRegister2(uint8_t size){
00105     /// <b>Algorithm of the method is as follows</b>
00106     this->size=size; /// * Assigns size from input argument to the class member OPT3101::deviceRegister::size
00107 }
00108 void OPT3101::deviceRegister2::write(int32_t value){
00109     uint8_t i;
00110     uint32_t mask;
00111     uint32_t readValue;
00112     uint32_t writeValue;
00113     uint32_t regmask;
00114     uint8_t size;
00115     /// <b>Algorithm of the method is as follows</b>
00116     value=(uint32_t) value; /// * Converts the input value to uint32_t
00117     for(i=0;i<this->size;i++){ /// * Loops though the number of OPT3101::deviceRegister::address fields based on OPT3101::deviceRegister::size
00118         readValue=this->read(i); /// * Invokes hostController::readI2C() method to get register value from h/w
00119         size=this->msb[i]+1-this->lsb[i];
00120         mask=~(((uint32_t) -1)<<(size));
00121         writeValue=value&mask;
00122         value=value>>size;
00123         regmask=(~((((uint32_t) -1)<<(this->msb[i]+1))^(((uint32_t) -1)<<this->lsb[i])))&0xFFFFFF; /// * Masks the bits for the new value to be written
00124         writeValue=(writeValue<<this->lsb[i])|(readValue&regmask);/// * Creates a new register value to be written to h/w based on created mask and data to be written
00125         if(readValue!=writeValue)
00126             this->writeI2C(this->address[i],writeValue); /// * Initiates hostController::writeI2C function with address and new register value
00127     }
00128 }
00129 
00130 void OPT3101::deviceRegister2::operator=(int32_t value){
00131     this->write(value);
00132 }
00133 
00134 uint32_t OPT3101::deviceRegister2::read(){
00135     uint8_t i;
00136     uint32_t readValue;
00137     uint32_t regmask;
00138     uint8_t size;
00139     uint8_t bitPos;
00140     uint32_t returnValue;
00141 
00142     returnValue=0;
00143     bitPos=0;
00144     /// <b>Algorithm of the method is as follows</b>
00145     for(i=0;i<this->size;i++){ /// * Loops though the number of OPT3101::deviceRegister::address fields based on  OPT3101::deviceRegister::size
00146         readValue=this->read(i); ///    * Invokes hostController::readI2C() method to get register value from h/w
00147         size=this->msb[i]+1-this->lsb[i];
00148         regmask=(((((uint32_t) -1)<<(this->msb[i]+1))^(((uint32_t) -1)<<this->lsb[i])))&0xFFFFFF; /// * Masks the bits for the register value to be reported based on register positional information
00149         //printf("Read mask for register 0x%02x is 0x%08x\n",this->address[i],regmask);
00150         returnValue|=((readValue&regmask)>>this->lsb[i])<<bitPos; /// * Assembles the value of the register
00151         bitPos+=size;
00152     }
00153     //printf("Resolved Value is %d",returnValue);
00154     return returnValue; /// * Returns the value read from the h/w for the register name specified
00155 }
00156 
00157 uint32_t OPT3101::deviceRegister2::read(uint8_t index){
00158     /// <b>Algorithm of the method is as follows</b>
00159     return this->readI2C(this->address[index]); /// * Returns the value read from the h/w for the register name specified
00160 }
00161 
00162 uint32_t OPT3101::deviceRegister2::readI2C(uint8_t address){
00163     /// <b>Algorithm of the method is as follows</b>
00164     return host.readI2C(address); /// * Invokes hostController::readI2C and returns the value read from the h/w d
00165 }
00166 
00167 void OPT3101::deviceRegister2::writeI2C(uint8_t address,uint32_t data){
00168     /// <b>Algorithm of the method is as follows</b>
00169     host.writeI2C(address, data); /// * Invokes hostController::writeI2C with address and data specified as arguments
00170 }
00171 
00172 //printf("Dummy I2C read  from OPT3101 register addr:0x%02x data:0x%06x\n",address,i2cReadValue);
00173 //printf("Dummy I2C write to   OPT3101 register addr:0x%02x data:0x%06x\n",address,data);
00174