HWRacing Formula Student Team / DS3231
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS3231.cpp Source File

DS3231.cpp

00001 #include "mbed.h"
00002 #include "DS3231.h"
00003 
00004 DS3231::DS3231(PinName sda,PinName scl)
00005 {
00006     _DS3231 = new I2C(sda,scl) ;  
00007 }
00008 
00009 void DS3231::readControl()
00010 {
00011     int *a;
00012     a = DS3231::readBytes(MSB_OF_TEMP,1);
00013     int b = *a;
00014     printf("Control: %d\r\n",b);
00015 }
00016 
00017 
00018 
00019 /*----------------------------------------- I2C Functions -----------------------------------------*/
00020 
00021 int DS3231::readByte(uint8_t registerAddress)
00022 {
00023     char address[1];
00024     address[0] = registerAddress;
00025     if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,1))
00026     {
00027         return -2;
00028     }
00029     else
00030     {
00031         uint8_t inBuffer[1];
00032         if(_DS3231 -> read(DS3231_ADDRESS,(char *)inBuffer,SINGLE_BYTE,0))
00033         {
00034             return -1;
00035         }
00036         else
00037         {
00038             return inBuffer[0];
00039         }
00040     }
00041 }
00042 
00043 int *DS3231::readBytes(uint8_t registerAddress, uint8_t numBytes)
00044 {
00045     int g = -1;
00046     int *gPtr = &g;
00047         
00048     char address[1];
00049     address[0] = registerAddress;
00050     if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,TRUE))
00051     {
00052         return gPtr;
00053     }
00054     else
00055     {
00056         uint8_t inBuffer[numBytes];
00057         uint8_t *inBufferPtr = &inBuffer[0];
00058         if(_DS3231 -> read(DS3231_ADDRESS,(char *)inBuffer,numBytes,FALSE))
00059         {
00060             return gPtr;
00061         }
00062         else
00063         {
00064             printf("Inside: %d\r\n",inBuffer[0]);
00065             return (int *)inBufferPtr;
00066         }
00067     }
00068     
00069 }
00070 
00071 int DS3231::writeByte(uint8_t registerAddress,uint8_t outBuffer)
00072 {
00073     char address[1];
00074     address[0] = registerAddress;
00075     if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,TRUE))
00076     {
00077         return 1;
00078     }
00079     else
00080     {    
00081     char buffer[1];
00082     buffer[0] = outBuffer;
00083         if(_DS3231 -> write(DS3231_ADDRESS,buffer,SINGLE_BYTE,FALSE))
00084         {
00085             return 1;
00086         }
00087         else
00088         {
00089             return 0;
00090         }
00091     }
00092 }
00093 
00094 //int DS3231::writeBytes(uint8_t registerAddress, uint8_t *outBuffer, uint8_t numBytes)
00095 //{
00096 //    if(_DS3231 -> write(DS3231_ADDRESS,(char *)registerAddress,SINGLE_BYTE,TRUE))
00097 //    {
00098 //        return 1;
00099 //    }
00100 //    else
00101 //    {
00102 //        if(_DS3231 -> write(DS3231_ADDRESS,(char *)outBuffer,numBytes,FALSE))
00103 //        {
00104 //            return 1;
00105 //        }
00106 //        else
00107 //        {
00108 //            return 0;
00109 //        }
00110 //    }