-
Notifications
You must be signed in to change notification settings - Fork 6
/
at24cxx.c
164 lines (153 loc) · 4.99 KB
/
at24cxx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "at24cxx.h"
#include "i2c.h"
#include "iwdg.h"
#if (_EEPROM_USE_FREERTOS == 1)
#include "cmsis_os.h"
#define at24_delay(x) osDelay(x)
#else
#define at24_delay(x) HAL_Delay(x)
#endif
#if (_EEPROM_SIZE_KBIT == 1) || (_EEPROM_SIZE_KBIT == 2)
#define _EEPROM_PSIZE 8
#elif (_EEPROM_SIZE_KBIT == 4) || (_EEPROM_SIZE_KBIT == 8) || (_EEPROM_SIZE_KBIT == 16)
#define _EEPROM_PSIZE 16
#else
#define _EEPROM_PSIZE 32
#endif
static uint8_t at24_lock = 0;
/**
* @brief Checks if memory device is ready for communication.
* @param none
* @retval bool status
*/
bool at24_isConnected(void)
{
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO,_EEPROM_WP_PIN,GPIO_PIN_SET);
#endif
if (HAL_I2C_IsDeviceReady(&_EEPROM_I2C, _EEPROM_ADDRESS, 2, 100) == HAL_OK)
return true;
else
return false;
}
/**
* @brief Write an amount of data in blocking mode to a specific memory address
* @param address Internal memory address
* @param data Pointer to data buffer
* @param len Amount of data to be sent
* @param timeout Timeout duration
* @retval bool status
*/
bool at24_write(uint16_t address, uint8_t *data, size_t len, uint32_t timeout)
{
if (at24_lock == 1)
return false;
at24_lock = 1;
uint16_t w;
uint32_t startTime = HAL_GetTick();
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN,GPIO_PIN_RESET);
#endif
while (1)
{
HAL_IWDG_Refresh(&_EEPROM_IWDG);
w = _EEPROM_PSIZE - (address % _EEPROM_PSIZE);
if (w > len)
w = len;
#if ((_EEPROM_SIZE_KBIT==1) || (_EEPROM_SIZE_KBIT==2))
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==4)
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0100) >> 7), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==8)
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0300) >> 7), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==16)
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0700) >> 7), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#else
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_16BIT, data, w, 100) == HAL_OK)
#endif
{
at24_delay(10);
len -= w;
data += w;
address += w;
if (len == 0)
{
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN, GPIO_PIN_SET);
#endif
at24_lock = 0;
return true;
}
if (HAL_GetTick() - startTime >= timeout)
{
at24_lock = 0;
return false;
}
}
else
{
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN, GPIO_PIN_SET);
#endif
at24_lock = 0;
return false;
}
}
}
/**
* @brief Read an amount of data in blocking mode to a specific memory address
* @param address Internal memory address
* @param data Pointer to data buffer
* @param len Amount of data to be sent
* @param timeout Timeout duration
* @retval bool status
*/
bool at24_read(uint16_t address, uint8_t *data, size_t len, uint32_t timeout)
{
HAL_IWDG_Refresh(&_EEPROM_IWDG);
if (at24_lock == 1)
return false;
at24_lock = 1;
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN, GPIO_PIN_SET);
#endif
#if ((_EEPROM_SIZE_KBIT==1) || (_EEPROM_SIZE_KBIT==2))
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT == 4)
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0100) >> 7), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT == 8)
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0300) >> 7), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==16)
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0700) >> 7), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#else
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_16BIT, data, len, timeout) == HAL_OK)
#endif
{
at24_lock = 0;
return true;
}
else
{
at24_lock = 0;
return false;
}
}
/**
* @brief Erase memory.
* @param none
* @retval bool status
*/
bool at24_eraseChip(void)
{
const uint8_t eraseData[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF\
, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint32_t bytes = 0;
while ( bytes < (_EEPROM_SIZE_KBIT * 128))
{
if (at24_write(bytes, (uint8_t*)eraseData, sizeof(eraseData), 100) == false)
return false;
bytes += sizeof(eraseData);
}
return true;
}
/* EOF */