Skip to content

Commit 4f45643

Browse files
committed
Modified, refactored, and added tests for the EEPROM module.
1 parent c3a84f5 commit 4f45643

File tree

5 files changed

+111
-54
lines changed

5 files changed

+111
-54
lines changed

HAL/EEPROM/EEPROM_interface.h

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
/**
22
* @file EEPROM_interface.h
33
* @author Abdulrahman Aboghanima ([email protected])
4-
* @brief
5-
* @version 0.1
4+
* @brief Contains functions declaration for the EEPROM module
5+
* @version 0.2
66
* @copyright Copyright (c) 2022
77
*/
88

9-
/** @addtogroup HAL_drivers
10-
* @{
11-
* @addtogroup EEPROM_drivers
12-
* @{
13-
*/
149

1510
#ifndef _EEPROM_INTERFACE_H_
1611
#define _EEPROM_INTERFACE_H_
@@ -21,15 +16,15 @@
2116
* @param data
2217
* @param address
2318
*/
24-
void EEPROM_writeByte(const uint8_t data, const uint16_t address);
19+
extern void EEPROM_writeByte(const uint8_t data, const uint16_t address);
2520

2621
/**
2722
* @brief Read a byte of data from the EEPROM, the byte will be stored
2823
* in the memory location addressed by `var`.
2924
* @param var
3025
* @param address
3126
*/
32-
void EEPROM_readByte(uint8_t * const var, const uint16_t address);
27+
extern void EEPROM_readByte(uint8_t * const var, const uint16_t address);
3328

3429
/**
3530
* @brief Send sequence of data addressed by `sequence` to the EEPROM across the I^{2}C bus
@@ -39,24 +34,25 @@ void EEPROM_readByte(uint8_t * const var, const uint16_t address);
3934
* @param sequenceSize
4035
* @note The data that will be stored is addressed by `sequence` and `fisrtAddress` is
4136
* the first address in the EEPROM of the data that will be stored
42-
* @code EEPROM_sendSequence("CoolStuff", 10, 9);
37+
* @code EEPROM_writePage("CoolStuff", 10, 9);
4338
* @endcode
4439
*/
45-
void EEPROM_writeSequence(uint8_t * const sequence, const uint16_t firstAddress, const uint16_t sequenceSize);
40+
extern void EEPROM_writePage(const uint8_t * const sequence, const uint16_t firstAddress, const uint8_t pageSize);
4641

4742
/**
4843
* @brief Read sequence of data from the EEPROM and store them in a memory location addressed by `sequence`
4944
* @param sequence
5045
* @param firstAddress
51-
* @param sequenceSize
46+
* @param pageSize
5247
* @note In the code bellow the data will be stored in the `buff` starting from the
5348
* address 20 in the EEPROM till address 1019
5449
* @code
5550
* uint8_t buff[1000];
5651
* EEPROM_readSequence(buff, 20, 1000);
5752
* @endcode
53+
* @note The maximum size of the page must be 16-byte
5854
*/
59-
void EEPROM_readSequence(uint8_t * const sequence, const uint16_t firstAddress, const uint16_t sequenceSize);
55+
extern void EEPROM_readSequence(uint8_t * const sequence, const uint16_t firstAddress, const uint16_t sequenceSize);
56+
6057

6158
#endif /* _EEPROM_INTERFACE_H_ */
62-
/*@}@}*/

HAL/EEPROM/EEPROM_program.c

+20-40
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @file EEPROM_program.c
33
* @author Abdulrahman Aboghanima ([email protected])
4-
* @brief
5-
* @version 0.1
4+
* @brief Contains functions declaration for the EEPROM module
5+
* @version 0.2
66
* @date 2022-02-03
77
* @todo Fix or remove the delays. Include the in the I2C driver
88
* @copyright Copyright (c) 2022
@@ -13,9 +13,10 @@
1313
#include "../../LIB/STD_TYPES.h"
1414
#include <util/delay.h>
1515

16-
#define EEPROM_ADDRESS 0b1010000
16+
#define EEPROM_ADDRESS ( 0b1010000 | (A2_PIN<<2) )
17+
1718
/*
18-
Suppos you would like to access memory which address is : X9X8X7X6X5X4X3X2X1X0
19+
Suppose you would like to access memory which address is : X9X8X7X6X5X4X3X2X1X0
1920
So in order to access that location you have to through the following address in the I^{2}C Bus
2021
|---+---+---+---+----+----+----|
2122
| 1 | 0 | 1 | 0 | A2 | X9 | X8 |
@@ -30,72 +31,51 @@ So in order to access that location you have to through the following address in
3031
*/
3132

3233

33-
void EEPROM_writeByte(const uint8_t data, const uint16_t address)
34+
extern void EEPROM_writeByte(const uint8_t data, const uint16_t address)
3435
{
3536
TWI_sendStartCondition();
36-
_delay_ms(50);
37-
TWI_sendSlaveAddressWithWrite( EEPROM_ADDRESS | (A2_PIN<<2) |(address>>8) );
38-
_delay_ms(50);
37+
/*Send slave address (which consists also of the number of block)*/
38+
TWI_sendSlaveAddressWithWrite( EEPROM_ADDRESS | (address>>8) );
39+
/*Send the address of the byte in the block(block consists of 256 bytes)*/
3940
TWI_masterWrite( (uint8_t) address);
40-
_delay_ms(50);
4141
TWI_masterWrite(data);
42-
_delay_ms(50);
4342
TWI_sendStopCondition();
44-
_delay_ms(50);
4543
}
4644

47-
void EEPROM_readByte(uint8_t * const var, const uint16_t address)
45+
extern void EEPROM_readByte(uint8_t * const var, const uint16_t address)
4846
{
4947
TWI_sendStartCondition();
50-
_delay_ms(50);
51-
TWI_sendSlaveAddressWithWrite(EEPROM_ADDRESS | (A2_PIN<<2) | (address>>8));
52-
_delay_ms(50);
48+
TWI_sendSlaveAddressWithWrite(EEPROM_ADDRESS | (address>>8));
5349
TWI_masterWrite((uint8_t) address);
54-
_delay_ms(50);
5550
TWI_sendRepeatedStartCondition();
56-
_delay_ms(50);
57-
TWI_sendSlaveAddressWithRead(EEPROM_ADDRESS | (A2_PIN<<2) | (address>>8));
58-
_delay_ms(50);
51+
TWI_sendSlaveAddressWithRead(EEPROM_ADDRESS | (address>>8));
5952
TWI_masterRead(var, 1);
60-
_delay_ms(50);
6153
TWI_sendStopCondition();
62-
_delay_ms(100);
63-
6454
}
6555

66-
void EEPROM_writeSequence(uint8_t * const sequence, const uint16_t firstAddress, const uint16_t sequenceSize)
56+
extern void EEPROM_writePage(const uint8_t * const sequence, const uint16_t firstAddress, const uint8_t pageSize)
6757
{
6858
TWI_sendStartCondition();
69-
_delay_ms(20);
70-
TWI_sendSlaveAddressWithWrite(EEPROM_ADDRESS|(A2_PIN<<2)|(firstAddress>>8));
71-
_delay_ms(20);
59+
/*Send slave address (which consists also of the number of block)*/
60+
TWI_sendSlaveAddressWithWrite(EEPROM_ADDRESS | (firstAddress>>8));
61+
/*Send the address of the byte in the block (block consists of 256 bytes)*/
7262
TWI_masterWrite((uint8_t)firstAddress);
73-
_delay_ms(20);
74-
for(uint8_t i=0; i<sequenceSize; i++){
63+
for(uint8_t i=0; i<pageSize; i++){
7564
TWI_masterWrite(sequence[i]);
76-
_delay_ms(20);
7765
}
7866
TWI_sendStopCondition();
79-
_delay_ms(100);
8067
}
8168

82-
void EEPROM_readSequence(uint8_t * const sequence, const uint16_t firstAddress, const uint16_t sequenceSize)
69+
extern void EEPROM_readSequence(uint8_t * const sequence, const uint16_t firstAddress, const uint16_t sequenceSize)
8370
{
8471
TWI_sendStartCondition();
85-
_delay_ms(20);
86-
TWI_sendSlaveAddressWithWrite(( EEPROM_ADDRESS | (A2_PIN<<2) | (firstAddress>>8) ));
87-
_delay_ms(20);
72+
TWI_sendSlaveAddressWithWrite(( EEPROM_ADDRESS | (firstAddress>>8) ));
8873
TWI_masterWrite( (uint8_t) firstAddress);
89-
_delay_ms(20);
9074
TWI_sendRepeatedStartCondition();
91-
_delay_ms(20);
92-
TWI_sendSlaveAddressWithRead( EEPROM_ADDRESS | (A2_PIN<<2) | (firstAddress>>8) );
93-
_delay_ms(20);
75+
TWI_sendSlaveAddressWithRead( EEPROM_ADDRESS | (firstAddress>>8) );
9476
for(uint16_t i=0; i<sequenceSize; i++){
9577
TWI_masterRead(sequence+i, sequenceSize-i);
96-
_delay_ms(100);
9778
}
9879

9980
TWI_sendStopCondition();
100-
_delay_ms(100);
10181
}

tests/EEPROM_output.gif

125 KB
Loading

tests/EEPROM_test.c

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
*
3+
* @file EEPROM_test.c
4+
* @brief Contains some tests for the EEPROM module
5+
* @author Abdulrahman Aboghanima
6+
* @date Oct 15 20:48:29 2021
7+
* @copyright Copyright (c) 2022
8+
* @version 0.2
9+
*
10+
*/
11+
12+
#include "../LIB/STD_TYPES.h"
13+
#include "../MCAL/DIO/DIO_interface.h"
14+
#include "../MCAL/USART/USART_interface.h"
15+
#include "../MCAL/TWI/TWI_interface.h"
16+
#include "../HAL/EEPROM/EEPROM_interface.h"
17+
#include <util/delay.h>
18+
19+
20+
char *data="My name is Abdulrahman.\nIlove Embedded Systems So much. :) ;)\n";
21+
char buffer[1024];
22+
23+
int main(void)
24+
{
25+
DIO_SetPinDirection(DIO_PORTD, DIO_PIN0, DIO_PIN_INPUT);
26+
DIO_SetPinDirection(DIO_PORTD, DIO_PIN1, DIO_PIN_OUTPUT);
27+
28+
DIO_SetPortDirection(DIO_PORTC, DIO_PORT_OUTPUT);
29+
30+
TWI_initMaster();
31+
USART_init();
32+
USART_sendStream("Testing EEPROM\n\n");
33+
34+
35+
USART_sendStream("Reading the Entire EEPROM:\n");
36+
/*Reading the entire contents of the EEPROM and saving it into `buffer`*/
37+
EEPROM_readSequence((uint8_t*)buffer, 0, 1024);
38+
39+
for(uint16_t i=0; i<1024; i++){
40+
USART_send(buffer[i]);
41+
if(i%64==0)
42+
USART_send('\n');
43+
}
44+
45+
46+
EEPROM_writePage((uint8_t*)data, 0, 16);
47+
EEPROM_writePage((uint8_t*)&data[16], 16, 16);
48+
EEPROM_writePage((uint8_t*)&data[32], 32, 16);
49+
50+
EEPROM_readSequence((uint8_t*)buffer, 0, 48);
51+
52+
53+
while(1){
54+
55+
_delay_ms(6000);
56+
USART_sendStream("\n\nRead from EEPROM:\n\n");
57+
58+
for(uint8_t i=0; i<48; i++)
59+
USART_send(buffer[i]);
60+
61+
USART_sendStream("\n\n");
62+
63+
}
64+
65+
return 0;
66+
}

tests/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,19 @@ So the period time of the signal = 256 * 8 uS = 2.048 mS as shown in the figure.
120120
In this program, I used an ADC resolution of 10 bits, and used a potentiometer as
121121
an analog signal source. After the conversion, the 10 bits are displayed using 10 bits;
122122
8 from `PORTC` and 2 from `PORTD`.
123+
123124
![Simulation of the ADC](./ADC_output.gif)
125+
126+
## I2C (Inter Integrated Circuit) or TWI (Two Wire Interface)
127+
128+
### Interfacing With EEPROM (`AT24C08`)
129+
130+
In this test, I used `At24C08` with which them micro controller communicates using I2C bus.
131+
132+
I read the entire contents of the EEPROM, saved it into `buffer[1024]`,
133+
and sent it into the terminal using the USART.
134+
135+
The content of the EEPROM is written by me before.
136+
137+
138+
![Simulation of the EEPROM](./EEPROM_output.gif)

0 commit comments

Comments
 (0)