at24cm0x 1.0
at24cm0x driver library
Loading...
Searching...
No Matches
at24cm0x.c File Reference

Implementation of the AT24CM0X driver functions. More...

#include "at24cm0x.h"
Include dependency graph for at24cm0x.c:

Functions

void at24cm0x_init (void)
 Initializes the AT24CM0X EEPROM driver.
void at24cm0x_device (unsigned char identifier)
 Selects the active AT24CM0X device.
AT24CM0X_Status at24cm0x_write_byte (unsigned long address, unsigned char data)
 Writes a single byte to the AT24CM0X EEPROM.
AT24CM0X_Status at24cm0x_write_page (unsigned int page, unsigned char *data, unsigned int size)
 Writes a sequence of bytes to a single EEPROM page.
AT24CM0X_Status at24cm0x_read_current_byte (unsigned char *data)
 Reads the current byte from the AT24CM0X EEPROM.
AT24CM0X_Status at24cm0x_read_byte (unsigned long address, unsigned char *data)
 Reads a single byte from the AT24CM0X EEPROM.
AT24CM0X_Status at24cm0x_read_sequential (unsigned long address, unsigned char *data, unsigned int size)
 Reads a sequence of bytes from the AT24CM0X EEPROM.

Variables

volatile unsigned char at24cm0x_device_identifier

Detailed Description

Implementation of the AT24CM0X driver functions.

This file contains the implementation of functions to initialize the AT24CM0X device, write and read data from/to eeprom. It utilizes TWI/I2C communication to interact with the AT24CM0X hardware.

Author
g.raf
Date
2026-01-25
Version
1.0 Release
Note
This file is part of a larger project and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/drivers-prom-at24cm0x "AT24CM0X eeprom driver library"

Function Documentation

◆ at24cm0x_device()

void at24cm0x_device ( unsigned char identifier)

Selects the active AT24CM0X device.

This function updates the internal device identifier used by the AT24CM0X driver to communicate with a specific EEPROM device on the I2C bus. The given identifier is masked with the configured AT24CM0X_ADDRESS_MASK and combined with AT24CM0X_BASE_ADDRESS to form the effective 7-bit I2C address stored in the internal at24cm0x_device_identifier.

Parameters
identifierDevice selector value used to derive the target AT24CM0X I2C address.

◆ at24cm0x_init()

void at24cm0x_init ( void )

Initializes the AT24CM0X EEPROM driver.

This function initializes the internal configuration of the AT24CM0X driver. If write-protect control is enabled at compile time (AT24CM0X_WP_CONTROL_EN), it first activates write-protect by calling at24cm0x_wp with AT24CM0X_WP_Mode_Enabled. It then sets the internal device identifier used for I2C communication to either AT24CM0X_BASE_ADDRESS when multi-device support is enabled (AT24CM0X_MULTI_DEVICES), or to AT24CM0X_ADDRESS when operating in single-device mode.

Note
This function should be called once during system startup before any AT24CM0X read or write operations are performed.
Here is the call graph for this function:

◆ at24cm0x_read_byte()

AT24CM0X_Status at24cm0x_read_byte ( unsigned long address,
unsigned char * data )

Reads a single byte from the AT24CM0X EEPROM.

This function reads one byte of data from the specified EEPROM address of the AT24CM0X device. It first checks whether the given address is within the valid memory range defined by AT24CM0X_MEMORY_SIZE; if not, it returns AT24CM0X_Status_Address_Error.

The read operation is performed using a random-read sequence: a start condition is sent, the target address is set via at24cm0x_send_address, and a stop condition is issued. Then a repeated start is generated, the device is addressed in read mode using the current at24cm0x_device_identifier, and a single byte is read with twi_get, followed by a stop condition.

If any TWI communication error occurs, the function returns AT24CM0X_Status_TWI_Error. On success, the received byte is stored at the location pointed to by data and the function returns AT24CM0X_Status_Done.

Parameters
addressEEPROM memory address from which the byte will be read.
dataPointer to a byte where the received data will be stored.
Returns
AT24CM0X_Status Status code indicating the result of the operation.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ at24cm0x_read_current_byte()

AT24CM0X_Status at24cm0x_read_current_byte ( unsigned char * data)

Reads the current byte from the AT24CM0X EEPROM.

This function reads the byte currently addressed by the AT24CM0X internal address counter using a TWI/I2C read operation. It sends a start condition, addresses the device in read mode using the internally stored at24cm0x_device_identifier, reads one byte into the provided buffer, and then issues a stop condition.

If any TWI communication error occurs during the sequence, the function returns AT24CM0X_Status_TWI_Error. On success, the received byte is stored at the location pointed to by data and the function returns AT24CM0X_Status_Done.

Parameters
dataPointer to a byte where the received data will be stored.
Returns
AT24CM0X_Status Status code indicating the result of the operation.
Here is the call graph for this function:

◆ at24cm0x_read_sequential()

AT24CM0X_Status at24cm0x_read_sequential ( unsigned long address,
unsigned char * data,
unsigned int size )

Reads a sequence of bytes from the AT24CM0X EEPROM.

This function reads a contiguous block of data starting at the specified EEPROM address of the AT24CM0X device. It first verifies that the start address is within the valid memory range defined by AT24CM0X_MEMORY_SIZE; if not, it returns AT24CM0X_Status_Address_Error. If the requested size is zero, it returns AT24CM0X_Status_Size_Error.

The read operation is performed using a random-read followed by a sequential read: a start condition is sent, the target address is set via at24cm0x_send_address, and a stop condition is issued. Then a repeated start is generated, the device is addressed in read mode using the current at24cm0x_device_identifier, and size bytes are read into the buffer pointed to by data. All bytes except the last are read with an ACK; the final byte is read with NACK to terminate the transfer, followed by a stop condition.

If any TWI communication error occurs, the function returns AT24CM0X_Status_TWI_Error. On success, the received data block is stored in the provided buffer and the function returns AT24CM0X_Status_Done.

Parameters
addressStart EEPROM memory address from which the data will be read.
dataPointer to the buffer where the received data will be stored.
sizeNumber of bytes to read; must be greater than 0.
Returns
AT24CM0X_Status Status code indicating the result of the operation.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ at24cm0x_write_byte()

AT24CM0X_Status at24cm0x_write_byte ( unsigned long address,
unsigned char data )

Writes a single byte to the AT24CM0X EEPROM.

This function writes one byte of data to the specified EEPROM address of the AT24CM0X device. It first checks whether the given address is within the valid memory range defined by AT24CM0X_MEMORY_SIZE; if not, it returns AT24CM0X_Status_Address_Error. If write-protect control is enabled (AT24CM0X_WP_CONTROL_EN), the function temporarily disables write-protect before issuing the write and re-enables it afterward. The write operation itself is performed using the TWI/I2C interface: a start condition is sent, the device/address sequence is transmitted via at24cm0x_send_address, the data byte is written, and a stop condition is generated.

After the write, the function either performs write acknowledge polling (AT24CM0X_WRITE_ACKNOWLEDGE_POLLING) or waits for a fixed write cycle time (AT24CM0X_WRITE_CYCLE_MS), depending on the compile-time configuration. If any TWI communication error occurs, it returns AT24CM0X_Status_TWI_Error.

When integrity checking is enabled (AT24CM0X_ENABLE_INTEGRITY_CHECK), the function reads back the byte using at24cm0x_read_byte and compares it with the original value; if they do not match, it returns AT24CM0X_Status_Data_Error. On success, the function returns AT24CM0X_Status_Done.

Parameters
addressEEPROM memory address at which the byte will be written.
dataData byte to write to the specified address.
Returns
AT24CM0X_Status Status code indicating the result of the operation.
Here is the call graph for this function:

◆ at24cm0x_write_page()

AT24CM0X_Status at24cm0x_write_page ( unsigned int page,
unsigned char * data,
unsigned int size )

Writes a sequence of bytes to a single EEPROM page.

This function writes a contiguous block of data to one page of the AT24CM0X EEPROM. It first validates the page index against AT24CM0X_PAGES and the data length against AT24CM0X_PAGE_SIZE; if the page is out of range, it returns AT24CM0X_Status_Page_Error, and if the size is zero or not within the allowed page size, it returns AT24CM0X_Status_Size_Error.

The target EEPROM address is calculated from the page index and AT24CM0X_PAGE_SIZE. If write-protect control is enabled (AT24CM0X_WP_CONTROL_EN), write-protect is temporarily disabled before the TWI/I2C transfer and re-enabled afterward. The function then issues a start condition, sends the device/address sequence via at24cm0x_send_address, and transmits each byte from the provided buffer using twi_set, followed by a stop condition.

After the write operation, the function either performs write acknowledge polling (AT24CM0X_WRITE_ACKNOWLEDGE_POLLING) or waits for a fixed write cycle time (AT24CM0X_WRITE_CYCLE_MS), depending on the compile-time configuration. If any TWI error is detected, it returns AT24CM0X_Status_TWI_Error.

When integrity checking is enabled (AT24CM0X_ENABLE_INTEGRITY_CHECK), the function reads back the written data using at24cm0x_read_sequential into a temporary buffer and compares it byte-by-byte with the original data. If any mismatch is found, it returns AT24CM0X_Status_Data_Error. On success, the function returns AT24CM0X_Status_Done.

Parameters
pagePage index to write, in the range [0, AT24CM0X_PAGES - 1].
dataPointer to the buffer containing the data to be written.
sizeNumber of bytes to write; must be greater than 0 and less than AT24CM0X_PAGE_SIZE.
Returns
AT24CM0X_Status Status code indicating the result of the operation.
Here is the call graph for this function:

Variable Documentation

◆ at24cm0x_device_identifier

volatile unsigned char at24cm0x_device_identifier