00001
00007 #include <util/twi.h>
00008
00009 #include "../twi_basics/twi_basics.h"
00010 #include "ds1338.h"
00011
00015 void ds1338_setup()
00016 {
00017 twi_set_bitrate(TWI_BITRATE);
00018 twi_init();
00019 }
00020
00026 static ds1338_status ds1338_writePointer(uint8_t location)
00027 {
00028 if(location<64)
00029 {
00030 switch (twi_start()) {
00031 case TW_START:
00032
00033 break;
00034 default:
00035 return BUS_ERROR;
00036 }
00037
00038 switch (twi_send(DEVICE|WRITE)) {
00039 case TW_MT_SLA_ACK:
00040
00041 break;
00042 case TW_MT_SLA_NACK:
00043 return NO_DEVICE;
00044 default:
00045 return BUS_ERROR;
00046 }
00047
00048 switch (twi_send(location)) {
00049 case TW_MT_DATA_ACK:
00050
00051 break;
00052 case TW_MT_DATA_NACK:
00053 return DEVICE_BUSY;
00054 default:
00055 return BUS_ERROR;
00056 }
00057 return NO_ERROR;
00058 }
00059 else
00060 return LOCATION_OUTOFBOUNDS;
00061 }
00062
00067 static ds1338_status ds1338_restartToRead()
00068 {
00069 switch (twi_start()) {
00070 case TW_REP_START:
00071
00072 break;
00073 default:
00074 return BUS_ERROR;
00075 }
00076 switch (twi_send(DEVICE|READ)) {
00077 case TW_MR_SLA_ACK:
00078
00079 break;
00080 case TW_MR_SLA_NACK:
00081 return NO_DEVICE;
00082 default:
00083 return BUS_ERROR;
00084 }
00085 return NO_ERROR;
00086 }
00092 ds1338_status ds1338_readTimeDateControl(ds1338TDC * data)
00093 {
00094 return ds1338_readBufferFromNVRAM(0x00,8,data->rawData);
00095 }
00096
00102 ds1338_status ds1338_writeTimeDateControl(ds1338TDC * data)
00103 {
00104 return ds1338_writeBufferToNVRAM(0x00,8,data->rawData);
00105 }
00106
00114 ds1338_status ds1338_writeBufferToNVRAM(uint8_t location,uint8_t lenght, uint8_t * data)
00115 {
00116 ds1338_status status;
00117 if((status=ds1338_writePointer(location)) != NO_ERROR)
00118 return status;
00119 if(location+lenght >64)
00120 return RANGE_OUTOFBOUNDS;
00121 int i=0;
00122 do
00123 {
00124 if(twi_send(data[i++])!=TW_MT_DATA_ACK)
00125 return DEVICE_BUSY;
00126 }while(i<lenght);
00127 twi_stop();
00128 return NO_ERROR;
00129 }
00130
00138 ds1338_status ds1338_readBufferFromNVRAM(uint8_t location,uint8_t lenght, uint8_t * data)
00139 {
00140 ds1338_status status;
00141 if((status=ds1338_writePointer(location)) != NO_ERROR)
00142 return status;
00143 if(location+lenght >64)
00144 return RANGE_OUTOFBOUNDS;
00145 if((status=ds1338_restartToRead()) != NO_ERROR)
00146 return status;
00147
00148 lenght--;
00149 int i=0;
00150 do
00151 {
00152 twi_receive(&data[i],lenght-i);
00153 i++;
00154 }while(i<=lenght);
00155 twi_stop();
00156 return NO_ERROR;
00157 }
00158
00165 ds1338_status ds1338_writeByteToNVRAM(uint8_t location, uint8_t data)
00166 {
00167 ds1338_status status;
00168 if((status=ds1338_writePointer(location)) != NO_ERROR)
00169 return status;
00170
00171 if(twi_send(data)!=TW_MT_DATA_ACK)
00172 return DEVICE_BUSY;
00173
00174 twi_stop();
00175 return NO_ERROR;
00176
00177 }
00184 ds1338_status ds1338_readByteFromNVRAM(uint8_t location, uint8_t * data)
00185 {
00186 ds1338_status status;
00187 if((status=ds1338_writePointer(location)) != NO_ERROR)
00188 return status;
00189 if((status=ds1338_restartToRead()) != NO_ERROR)
00190 return status;
00191 twi_receive(data,0);
00192 twi_stop();
00193 return NO_ERROR;
00194 }