Lindholm Engineers / Mbed 2 deprecated Ultrasonic_Firmware

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TI_Registers.cpp Source File

TI_Registers.cpp

00001 //TI_Registers class
00002 //By Eric Lindholm, created on 7 December 2015
00003 
00004 #include "mbed.h"
00005 #include "TI_Registers.h"
00006 
00007 /** Ultrasonic_Registers class
00008  *   To access the registers of the ultrasonic driver chip: the TDC1000
00009  *   MOSI is the pin which is assigned as serial out from the MCU.
00010  *   MISO is the pin which is assigned as serial in to the MCU.
00011  *   SCLK is the pin which is assigned as serial clock.
00012  *   SPICS is the chip select pin for the SPI connection.
00013  */
00014 TI_Registers::TI_Registers(PinName MOSI, PinName MISO, PinName SCLK, PinName SPICS) : device(MOSI, MISO, SCLK), cs(SPICS,1) {
00015     //Double-ensure that cs is normally HIGH
00016     cs = 1;
00017     
00018     //set the SPI device to use an 8-bit transfer.
00019     device.format(8);
00020     //set the SPI device to use 1 MHz clock.
00021     device.frequency(1000000);
00022 }
00023 
00024 /** registerRead8 function
00025  *   The registerRead functions individually read 1, 2, or 3 bytes of data.
00026  *   address is the address that is included in the TI documentation for the 
00027  *    register in question, and is 6 bits long.
00028  */
00029 int TI_Registers::registerRead8(int address) {
00030     //Create variable for new contents
00031     int contents;
00032     
00033     //start the SPI connection
00034     cs = 0;
00035     
00036     //transfer address
00037     device.write(address);
00038     
00039     //read contents of register
00040     contents = device.write(0x00);
00041     
00042     //Finish SPI connection
00043     cs = 1;
00044     
00045     return contents;
00046 }
00047 
00048 /** registerRead16 function
00049  *   The registerRead functions individually read 1, 2, or 3 bytes of data.
00050  *   address is the address that is included in the TI documentation for the 
00051  *    register in question, and is 6 bits long.
00052  */
00053 int TI_Registers::registerRead16(int address) {
00054     //Create variables for new contents
00055     int content1, content2;
00056     
00057     //start the SPI connection
00058     cs = 0;
00059     
00060     //transfer address
00061     device.write(address);
00062     
00063     //read contents of register
00064     content1 = device.write(0x00);
00065     content2 = device.write(0x00);
00066     
00067     //Finish SPI connection
00068     cs = 1;
00069     
00070     int contents = (content1<<8) + content2;
00071     return contents;
00072 }
00073 
00074 /** registerRead24 function
00075  *   The registerRead functions individually read 1, 2, or 3 bytes of data.
00076  *   address is the address that is included in the TI documentation for the 
00077  *    register in question, and is 6 bits long.
00078  */
00079 int TI_Registers::registerRead24(int address) {
00080     //Create variables for new contents
00081     int content1, content2, content3;
00082     
00083     //start the SPI connection
00084     cs = 0;
00085     
00086     //transfer address
00087     device.write(address);
00088     
00089     //read contents of register
00090     content1 = device.write(0x00);
00091     content2 = device.write(0x00);
00092     content3 = device.write(0x00);
00093     
00094     //Finish SPI connection
00095     cs = 1;
00096     
00097     int contents = (content1<<16) + (content2<<8) + content3;
00098     return contents;
00099 }
00100 
00101 /** registerWrite function 
00102  *   The registerWrite function writes one byte of data to the register 
00103  *   address is the 8-bit address for the register in question, determined
00104  *    from the TI documentation for the chip: 6 bits.
00105  *   contents is the 8 bits of data that will be written to the register.
00106  */
00107 void TI_Registers::registerWrite(int address, int contents) {
00108     //First, we append the write bit to the address.
00109     address += (1<<6);
00110     
00111     //Next, set the SPI connection active
00112     cs = 0;
00113     
00114     //Send the address to the chip
00115     device.write(address);
00116     
00117     //Write the data we need to the chip
00118     device.write(contents);
00119     
00120     //end SPI connection
00121     cs = 1;
00122 }