at24c.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <cfg/os.h>
00037 #include <cfg/memory.h>
00038
00039 #include <sys/timer.h>
00040
00041 #include <stdlib.h>
00042
00043 #include <dev/twif.h>
00044 #include <dev/at24c.h>
00045
00046 #define AT24C_AT91
00047
00048 #define AT24C_DEBUG
00049
00050 #ifdef AT24C_DEBUG
00051 #include <stdio.h>
00052 #endif
00053
00064 static int lld_at24_write( struct at24c *at24cs, uint8_t *buffer, uint32_t len, uint32_t addr)
00065 {
00066 uint8_t retry = at24cs->Timeout;
00067 int tme;
00068
00069 do {
00070 TwMasterRegWrite( at24cs->SlaveAddress, addr, at24cs->IAddrW, buffer, len, 500 );
00071
00072 tme = TwMasterError();
00073 if( tme == TWERR_OK)
00074 return 0;
00075
00076 #ifdef AT24C_DEBUG
00077 printf("MRW %lu: TME:%d t:%d\n", len, tme, TwMasterIndexes( 0));
00078 #endif
00079 if( tme >= TWERR_SLA_NACK) {
00080
00081 --retry;
00082
00083 #ifdef AT24C_DEBUG
00084 printf("RW %u\n", retry);
00085 #else
00086 NutSleep(1);
00087 #endif
00088 }
00089 else {
00090
00091 return -1;
00092 }
00093 }
00094 while( retry);
00095
00096 return -2;
00097 }
00098
00109 static int lld_at24_read( struct at24c *at24cs, uint8_t *buffer, uint32_t len, uint32_t addr)
00110 {
00111 uint8_t retry = at24cs->Timeout;
00112 int tme;
00113
00114 do {
00115 TwMasterRegRead( at24cs->SlaveAddress, addr, at24cs->IAddrW, buffer, len, 500 );
00116 tme = TwMasterError();
00117 #ifdef AT24C_DEBUG
00118 printf("MRD l=%lu: TME %d, mt %u, mr %u\n", len, tme, TwMasterIndexes( 0), TwMasterIndexes(1));
00119 #endif
00120
00121 if( tme == TWERR_SLA_NACK) {
00122 --retry;
00123 #ifdef AT24C_DEBUG
00124 printf("RR %u\n", retry);
00125 #else
00126 NutSleep(1);
00127 #endif
00128 }
00129 else return -2;
00130
00131 } while( retry);
00132
00133 return -1;
00134 }
00135
00146
00147 int At24cRead( struct at24c *at24cs, uint8_t *buffer, uint32_t len, uint32_t addr )
00148
00149 {
00150
00151 return lld_at24_read( at24cs, buffer, len, addr);
00152 }
00153
00164
00165 int At24cWrite( struct at24c *at24cs, uint8_t *buffer, uint32_t len, uint32_t addr)
00166
00167 {
00168 int rc = 0;
00169 uint8_t *ptr = buffer;
00170 uint32_t bulk;
00171
00172 while( (len>0) && (rc>=0))
00173 {
00174 bulk = at24cs->PageSize-(addr%at24cs->PageSize);
00175 if( bulk > len) bulk = len;
00176 rc = lld_at24_write( at24cs, ptr, bulk, addr );
00177 ptr+=bulk; addr+=bulk; len-=bulk;
00178 }
00179 return rc;
00180 }
00181