Rev 248 | Rev 254 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 248 | Rev 252 | ||
---|---|---|---|
Line 53... | Line 53... | ||
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <string.h> |
57 | #include <string.h> |
- | 58 | #include <stdio.h> |
|
58 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
59 | #include "i2c.h" |
60 | #include "i2c.h" |
60 | #include "uart1.h" |
61 | #include "uart1.h" |
61 | #include "timer1.h" |
62 | #include "timer1.h" |
62 | #include "eeprom.h" |
63 | #include "eeprom.h" |
Line 77... | Line 78... | ||
77 | 78 | ||
78 | // globals for rx handler |
79 | // globals for rx handler |
79 | u8 *EEPROM_pData = 0; |
80 | u8 *EEPROM_pData = 0; |
Line 80... | Line 81... | ||
80 | u16 EEPROM_DataLen = 0; |
81 | u16 EEPROM_DataLen = 0; |
81 | 82 | ||
82 | // rx data handler for eeprom data read |
83 | // rx data handler for eeprom data read access |
83 | void EEPROM_RxData(u8* pRxBuffer, u8 RxBufferSize) |
84 | void EEPROM_RxDataHandler(u8* pRxBuffer, u8 RxBufferSize) |
84 | { // if number of byte are matching |
85 | { // if number of byte are matching |
85 | if(RxBufferSize == EEPROM_DataLen) |
86 | if(RxBufferSize == EEPROM_DataLen) |
86 | { //copy data from primary buffer to target buffer |
87 | { //copy data from primary buffer to target buffer |
87 | if(EEPROM_pData) memcpy(EEPROM_pData, pRxBuffer, sizeof(EEPROM_DataLen)); |
88 | if(EEPROM_pData) memcpy(EEPROM_pData, pRxBuffer, EEPROM_DataLen); |
Line 88... | Line 89... | ||
88 | } |
89 | } |
89 | } |
90 | } |
90 | 91 | ||
91 | // ---------------------------------------------------------------------------------------- |
92 | // ---------------------------------------------------------------------------------------- |
92 | EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen) |
93 | EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen) |
93 | { |
94 | { |
Line 94... | Line 95... | ||
94 | u16 i, TxBytes; |
95 | u16 i, TxBytes = 0; |
95 | u8 retry = 0; |
96 | u8 retry = 0; |
Line 132... | Line 133... | ||
132 | { |
133 | { |
133 | // prepare pointer to rx data |
134 | // prepare pointer to rx data |
134 | EEPROM_pData = pData; |
135 | EEPROM_pData = pData; |
135 | EEPROM_DataLen = DataLen; |
136 | EEPROM_DataLen = DataLen; |
136 | // start transmission |
137 | // start transmission |
137 | if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxData, DataLen)) |
138 | if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxDataHandler, DataLen)) |
138 | { |
139 | { |
139 | return(retval); |
140 | return(retval); |
140 | } |
141 | } |
141 | } |
142 | } |
142 | //wait for end of this transfer |
143 | //wait for end of this transfer |
143 | if(I2C_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT)) |
144 | if(I2C_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT)) |
144 | { |
145 | { |
- | 146 | if(I2C_Error == I2C_ERROR_NONE) retval = EEPROM_SUCCESS; |
|
145 | retval = EEPROM_SUCCESS; |
147 | else retval = EEPROM_ERROR_I2C_TRANSFER_TIMEOUT; |
146 | break; |
148 | break; |
147 | } |
149 | } |
148 | // or retry |
150 | // or retry |
149 | I2C_State = I2C_STATE_IDLE; |
151 | I2C_State = I2C_STATE_IDLE; |
150 | } while(++retry < I2C_ACCESS_RETRYS); |
152 | } while(++retry < I2C_ACCESS_RETRYS); |
Line 163... | Line 165... | ||
163 | len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row |
165 | len = EEPROM_WRITE_BLOCK_SIZE - Address%EEPROM_WRITE_BLOCK_SIZE; // number of bytes to match the next row |
164 | if(len >= DataLen) len = DataLen; // do not write more than neccesarry |
166 | if(len >= DataLen) len = DataLen; // do not write more than neccesarry |
Line 165... | Line 167... | ||
165 | 167 | ||
166 | do |
168 | do |
167 | { |
169 | { |
168 | retval = EEPROM_Transfer(I2C_MODE_TRANSMITTER, Address, pData, len); |
170 | retval = EEPROM_Transfer(EEPROM_WRITE, Address, pData, len); |
169 | Address += len; // Address of the next row |
171 | Address += len; // Address of the next row |
170 | pData += len; // pointer to start of next data block |
172 | pData += len; // pointer to start of next data block |
171 | DataLen -= len; // reduce the number of to be transmitted |
173 | DataLen -= len; // reduce the number of to be transmitted |
172 | if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32 |
174 | if(DataLen > EEPROM_WRITE_BLOCK_SIZE) len = EEPROM_WRITE_BLOCK_SIZE; // limit next block size to 32 |
Line 178... | Line 180... | ||
178 | } |
180 | } |
Line 179... | Line 181... | ||
179 | 181 | ||
180 | // a data block can be subsequently read after setting the start address once |
182 | // a data block can be subsequently read after setting the start address once |
181 | EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen) |
183 | EEPROM_Result_t EEPROM_ReadBlock(u16 Address, u8 *pData, u16 DataLen) |
- | 184 | { |
|
- | 185 | EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN; |
|
- | 186 | u16 AdrOffset = 0; |
|
- | 187 | u16 RxLen; |
|
- | 188 | ||
- | 189 | while(DataLen > 0) |
|
- | 190 | { |
|
- | 191 | if(DataLen > I2C_BUFFER_LEN) RxLen = I2C_BUFFER_LEN; |
|
- | 192 | else RxLen = DataLen; |
|
182 | { |
193 | |
- | 194 | retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen); |
|
- | 195 | if(retval != EEPROM_SUCCESS) break; |
|
- | 196 | AdrOffset += RxLen; |
|
- | 197 | DataLen -= RxLen; |
|
- | 198 | } |
|
183 | return EEPROM_Transfer(I2C_MODE_RECEIVER, Address, pData, DataLen); |
199 | return(retval); |