RTC Driver
DS3231.cpp@0:4399479f355f, 2016-02-20 (annotated)
- Committer:
- sk398
- Date:
- Sat Feb 20 13:32:01 2016 +0000
- Revision:
- 0:4399479f355f
Initial commit.; ; Much work still needs done to interface the DS3231 correctly. ; ; I2C read/write functions work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:4399479f355f | 1 | #include "mbed.h" |
sk398 | 0:4399479f355f | 2 | #include "DS3231.h" |
sk398 | 0:4399479f355f | 3 | |
sk398 | 0:4399479f355f | 4 | DS3231::DS3231(PinName sda,PinName scl) |
sk398 | 0:4399479f355f | 5 | { |
sk398 | 0:4399479f355f | 6 | _DS3231 = new I2C(sda,scl) ; |
sk398 | 0:4399479f355f | 7 | } |
sk398 | 0:4399479f355f | 8 | |
sk398 | 0:4399479f355f | 9 | void DS3231::readControl() |
sk398 | 0:4399479f355f | 10 | { |
sk398 | 0:4399479f355f | 11 | int *a; |
sk398 | 0:4399479f355f | 12 | a = DS3231::readBytes(MSB_OF_TEMP,1); |
sk398 | 0:4399479f355f | 13 | int b = *a; |
sk398 | 0:4399479f355f | 14 | printf("Control: %d\r\n",b); |
sk398 | 0:4399479f355f | 15 | } |
sk398 | 0:4399479f355f | 16 | |
sk398 | 0:4399479f355f | 17 | |
sk398 | 0:4399479f355f | 18 | |
sk398 | 0:4399479f355f | 19 | /*----------------------------------------- I2C Functions -----------------------------------------*/ |
sk398 | 0:4399479f355f | 20 | |
sk398 | 0:4399479f355f | 21 | int DS3231::readByte(uint8_t registerAddress) |
sk398 | 0:4399479f355f | 22 | { |
sk398 | 0:4399479f355f | 23 | char address[1]; |
sk398 | 0:4399479f355f | 24 | address[0] = registerAddress; |
sk398 | 0:4399479f355f | 25 | if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,1)) |
sk398 | 0:4399479f355f | 26 | { |
sk398 | 0:4399479f355f | 27 | return -2; |
sk398 | 0:4399479f355f | 28 | } |
sk398 | 0:4399479f355f | 29 | else |
sk398 | 0:4399479f355f | 30 | { |
sk398 | 0:4399479f355f | 31 | uint8_t inBuffer[1]; |
sk398 | 0:4399479f355f | 32 | if(_DS3231 -> read(DS3231_ADDRESS,(char *)inBuffer,SINGLE_BYTE,0)) |
sk398 | 0:4399479f355f | 33 | { |
sk398 | 0:4399479f355f | 34 | return -1; |
sk398 | 0:4399479f355f | 35 | } |
sk398 | 0:4399479f355f | 36 | else |
sk398 | 0:4399479f355f | 37 | { |
sk398 | 0:4399479f355f | 38 | return inBuffer[0]; |
sk398 | 0:4399479f355f | 39 | } |
sk398 | 0:4399479f355f | 40 | } |
sk398 | 0:4399479f355f | 41 | } |
sk398 | 0:4399479f355f | 42 | |
sk398 | 0:4399479f355f | 43 | int *DS3231::readBytes(uint8_t registerAddress, uint8_t numBytes) |
sk398 | 0:4399479f355f | 44 | { |
sk398 | 0:4399479f355f | 45 | int g = -1; |
sk398 | 0:4399479f355f | 46 | int *gPtr = &g; |
sk398 | 0:4399479f355f | 47 | |
sk398 | 0:4399479f355f | 48 | char address[1]; |
sk398 | 0:4399479f355f | 49 | address[0] = registerAddress; |
sk398 | 0:4399479f355f | 50 | if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,TRUE)) |
sk398 | 0:4399479f355f | 51 | { |
sk398 | 0:4399479f355f | 52 | return gPtr; |
sk398 | 0:4399479f355f | 53 | } |
sk398 | 0:4399479f355f | 54 | else |
sk398 | 0:4399479f355f | 55 | { |
sk398 | 0:4399479f355f | 56 | uint8_t inBuffer[numBytes]; |
sk398 | 0:4399479f355f | 57 | uint8_t *inBufferPtr = &inBuffer[0]; |
sk398 | 0:4399479f355f | 58 | if(_DS3231 -> read(DS3231_ADDRESS,(char *)inBuffer,numBytes,FALSE)) |
sk398 | 0:4399479f355f | 59 | { |
sk398 | 0:4399479f355f | 60 | return gPtr; |
sk398 | 0:4399479f355f | 61 | } |
sk398 | 0:4399479f355f | 62 | else |
sk398 | 0:4399479f355f | 63 | { |
sk398 | 0:4399479f355f | 64 | printf("Inside: %d\r\n",inBuffer[0]); |
sk398 | 0:4399479f355f | 65 | return (int *)inBufferPtr; |
sk398 | 0:4399479f355f | 66 | } |
sk398 | 0:4399479f355f | 67 | } |
sk398 | 0:4399479f355f | 68 | |
sk398 | 0:4399479f355f | 69 | } |
sk398 | 0:4399479f355f | 70 | |
sk398 | 0:4399479f355f | 71 | int DS3231::writeByte(uint8_t registerAddress,uint8_t outBuffer) |
sk398 | 0:4399479f355f | 72 | { |
sk398 | 0:4399479f355f | 73 | char address[1]; |
sk398 | 0:4399479f355f | 74 | address[0] = registerAddress; |
sk398 | 0:4399479f355f | 75 | if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,TRUE)) |
sk398 | 0:4399479f355f | 76 | { |
sk398 | 0:4399479f355f | 77 | return 1; |
sk398 | 0:4399479f355f | 78 | } |
sk398 | 0:4399479f355f | 79 | else |
sk398 | 0:4399479f355f | 80 | { |
sk398 | 0:4399479f355f | 81 | char buffer[1]; |
sk398 | 0:4399479f355f | 82 | buffer[0] = outBuffer; |
sk398 | 0:4399479f355f | 83 | if(_DS3231 -> write(DS3231_ADDRESS,buffer,SINGLE_BYTE,FALSE)) |
sk398 | 0:4399479f355f | 84 | { |
sk398 | 0:4399479f355f | 85 | return 1; |
sk398 | 0:4399479f355f | 86 | } |
sk398 | 0:4399479f355f | 87 | else |
sk398 | 0:4399479f355f | 88 | { |
sk398 | 0:4399479f355f | 89 | return 0; |
sk398 | 0:4399479f355f | 90 | } |
sk398 | 0:4399479f355f | 91 | } |
sk398 | 0:4399479f355f | 92 | } |
sk398 | 0:4399479f355f | 93 | |
sk398 | 0:4399479f355f | 94 | //int DS3231::writeBytes(uint8_t registerAddress, uint8_t *outBuffer, uint8_t numBytes) |
sk398 | 0:4399479f355f | 95 | //{ |
sk398 | 0:4399479f355f | 96 | // if(_DS3231 -> write(DS3231_ADDRESS,(char *)registerAddress,SINGLE_BYTE,TRUE)) |
sk398 | 0:4399479f355f | 97 | // { |
sk398 | 0:4399479f355f | 98 | // return 1; |
sk398 | 0:4399479f355f | 99 | // } |
sk398 | 0:4399479f355f | 100 | // else |
sk398 | 0:4399479f355f | 101 | // { |
sk398 | 0:4399479f355f | 102 | // if(_DS3231 -> write(DS3231_ADDRESS,(char *)outBuffer,numBytes,FALSE)) |
sk398 | 0:4399479f355f | 103 | // { |
sk398 | 0:4399479f355f | 104 | // return 1; |
sk398 | 0:4399479f355f | 105 | // } |
sk398 | 0:4399479f355f | 106 | // else |
sk398 | 0:4399479f355f | 107 | // { |
sk398 | 0:4399479f355f | 108 | // return 0; |
sk398 | 0:4399479f355f | 109 | // } |
sk398 | 0:4399479f355f | 110 | // } |