M24FC256.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: Beerware
2 /**
3  * @file M24FC256.c
4  * @brief 24FC256 driver
5  * @details 256Kb I²C compatible 2-wire Serial EEPROM with 1MHz clock
6  * compatibility
7  * @ingroup 24FC256
8  * @defgroup 24FC256 24FC256 I²C compatible 2-wire Serial EEPROM
9  * @remark The driver does currently only support page-wise read and
10  * write operations. For byte-wise operations the datasheet
11  * states as follows: When doing a write of less than 64
12  * bytes the data in the rest of the page is refreshed along
13  * with the data bytes being written. This will force the
14  * entire page to endure a write cycle, for this reason
15  * endurance is specified per page.
16  * @author Michael Fitzmayer
17  * @copyright "THE BEER-WARE LICENCE" (Revision 42)
18  */
19 
20 #ifdef USE_M24FC256
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "M24FC256.h"
25 #include "MCAL.h"
26 
27 /**
28  * @brief Read data from 24FC256 EEPROM
29  * @param u16Address
30  * Address to specify where to read the data from
31  * @param pu8RxBuffer
32  * Pointer to receive buffer
33  * @param u8Pages
34  * Number of pages to read
35  * @return Error code
36  * @retval 0: OK
37  * @retval -1: Error
38  * @remark The user has to ensure that the size of the buffer where he
39  * wants to store the requested EEPROM content is big enough to
40  * store the number of requested pages. Or the CPU will crash.
41  */
42 int M24FC256_Read(uint16_t u16Address, uint8_t *pu8RxBuffer, uint8_t u8Pages)
43 {
44  int sRemainingBytes = u8Pages * M24FC256_PAGESIZE;
45  int sMemoryAddress = 0;
46 
47  if (NULL == pu8RxBuffer)
48  {
49  return -1;
50  }
51 
53 
54  while(sRemainingBytes > 0)
55  {
56  int nError = I2C_Receive(
58  u16Address + sMemoryAddress,
60  pu8RxBuffer + sMemoryAddress,
62 
63  if (0 != nError)
64  {
65  return nError;
66  }
67 
69 
70  sRemainingBytes -= M24FC256_PAGESIZE;
71  sMemoryAddress += M24FC256_PAGESIZE;
72  }
73 
74  return 0;
75 }
76 
77 /**
78  * @brief Write data to 24FC256 EEPROM
79  * @param u16Address
80  * Address to specify where to write the data
81  * @param pu8TxBuffer
82  * Pointer to transmit buffer
83  * @param u8Pages
84  * Number of pages to write
85  * @return Error code
86  * @retval 0: OK
87  * @retval -1: Error
88  */
89 int M24FC256_Write(uint16_t u16Address, uint8_t *pu8TxBuffer, uint8_t u8Pages)
90 {
91  int sRemainingBytes = u8Pages * M24FC256_PAGESIZE;
92  int sMemoryAddress = 0;
93 
94  if (NULL == pu8TxBuffer)
95  {
96  return -1;
97  }
98 
99  while(sRemainingBytes > 0)
100  {
101  int nError = I2C_Transmit(
103  u16Address + sMemoryAddress,
105  pu8TxBuffer + sMemoryAddress,
107 
108  if (0 != nError)
109  {
110  return nError;
111  }
112 
114 
115  sRemainingBytes -= M24FC256_PAGESIZE;
116  sMemoryAddress += M24FC256_PAGESIZE;
117  }
118 
119  return 0;
120 }
121 
122 #endif // USE_M24FC256
int M24FC256_Write(uint16_t u16Address, uint8_t *pu8TxBuffer, uint8_t u8Pages)
Write data to 24FC256 EEPROM.
Definition: M24FC256.c:89
void I2C_WaitUntilReady(uint16_t u16DevAddress)
Wait for the end of the transfer.
Definition: MCAL.c:186
int M24FC256_Read(uint16_t u16Address, uint8_t *pu8RxBuffer, uint8_t u8Pages)
Read data from 24FC256 EEPROM.
Definition: M24FC256.c:42
24FC256 driver
int I2C_Receive(uint16_t u16DevAddress, uint16_t u16MemAddress, I2CMemAddSize eMemAddSize, uint8_t *pu8RxBuffer, uint16_t u16Size)
Receive an amount via I²C.
Definition: MCAL.c:105
Microcontroller Abstraction Layer.
@ I2C_MEMSIZE_16BIT
I²C memory address size (16-Bit)
Definition: MCAL.h:33
int I2C_Transmit(uint16_t u16DevAddress, uint16_t u16MemAddress, I2CMemAddSize eMemAddSize, uint8_t *pu8TxBuffer, uint16_t u16Size)
Transmit an amount via I²C.
Definition: MCAL.c:149
#define M24FC256_ADDRESS
4-bit control code for read- and write operations
Definition: M24FC256.h:13
#define M24FC256_PAGESIZE
EEPROM page size.
Definition: M24FC256.h:14