-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathat24_hal_i2c.c
222 lines (201 loc) · 8.58 KB
/
at24_hal_i2c.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* | -------------------------------- R2T Team Libraries -------------------------
* | @Created On Sep,11,2015
* | @File Name : at24_hal_i2c
* | @Brief : STM32 HAL Driver for AT24 eeprom series
* | @Copyright :
* | This program is free software: you can redistribute it and/or modify
* | it under the terms of the GNU General Public License as published by
* | the Free Software Foundation, either version 3 of the License, or
* | any later version.
* |
* | This program is distributed in the hope that it will be useful,
* | but WITHOUT ANY WARRANTY; without even the implied warranty of
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* | GNU General Public License for more details.
* |
* | You should have received a copy of the GNU General Public License
* | along with this program. If not, see <http://www.gnu.org/licenses/>.
* | @Author : Sina Darvishi
* | @Website : R2T.IR
* |
**/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "stm32f0xx_hal_i2c.h"
#include <string.h>
#include <stdio.h>
int at24_HAL_WriteBytes(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint16_t MemAddress, uint8_t *pData,uint16_t TxBufferSize);
int at24_HAL_ReadBytes(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint16_t MemAddress, uint8_t *pData,uint16_t RxBufferSize);
int at24_HAL_SequentialRead(I2C_HandleTypeDef *hi2c ,uint8_t DevAddress,uint16_t MemAddress,uint8_t *pData,uint16_t RxBufferSize);
int at24_HAL_EraseMemFull(I2C_HandleTypeDef *hi2c);
int at24_HAL_WriteString(I2C_HandleTypeDef *hi2c,char *pString ,uint16_t MemAddress ,uint8_t length);
int at24_HAL_ReadString(I2C_HandleTypeDef *hi2c,char *pString,uint16_t MemAddress,uint8_t length);
/**
* @brief : This function handles Writing Array of Bytes on the specific Address .
* This program have this feature that don't force you to use absolute 16 bytes
* you can use more than 16 bytes buffer.
* @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress : specifies the slave address to be programmed(EEPROM ADDRESS).
* @param MemAddress : Internal memory address (WHERE YOU WANNA WRITE TO)
* @param pData : Pointer to data buffer
* @param TxBufferSize : Amount of data you wanna Write
* @retval
*/
int at24_HAL_WriteBytes(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint16_t MemAddress, uint8_t *pData,uint16_t TxBufferSize)
{
/*
* program just get the DevAddress of the Slave (not master) and for the next step
* You know that the most of the EEprom address start with 0xA0
* give MemAddress for the location you want to write to
* give Data buffer so it can write Data on this location
*/
//Note that this function works properly to 31 bytes
if(MemAddress+TxBufferSize > 16)
{
//Write to 16bytes
while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16-MemAddress,1000)!= HAL_OK);
//write remaining bytes
*pData = *pData + (16-MemAddress);
while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)16,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)((MemAddress+TxBufferSize)-16),1000)!= HAL_OK);
}
else
{
while( (TxBufferSize-16)>0 )
{
//if your data is more than 16 bytes,you are here
while(HAL_I2C_Mem_Write(&hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16,1000)!= HAL_OK);
TxBufferSize-=16;
pData+=16;
MemAddress+=16;
}
//remaining data
while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)TxBufferSize,1000)!= HAL_OK);
}
return 1;
}
int at24_HAL_ReadBytes(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint16_t MemAddress, uint8_t *pData,uint16_t RxBufferSize)
{
int TimeOut;
/*
* program just get the DevAddress of the Slave (not master) and for the next step
* You know that the most of the EEprom address start with 0xA0
* get the MemAddress for the location you want to write data on it
* get the Data buffer so it can write Data on this location
*/
//Note that this function works properly to 31bytes
while( (RxBufferSize-16)>0 )
{
//if your data is more than 16 bytes,you are here
TimeOut = 0;
while(HAL_I2C_Mem_Read(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16,1000)!= HAL_OK && TimeOut < 10)
{
TimeOut++;
}
RxBufferSize-=16;
pData+=16;
MemAddress+=16;
}
// //remaining data
TimeOut = 0;
while(HAL_I2C_Mem_Read(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)RxBufferSize,1000)!= HAL_OK && TimeOut < 10)
{
TimeOut++;
}
return 1;
}
/*
* @brief : This function handles Reading Array of Bytes from the specific Address .
* This program have this feature that don't force you to use absolute 16 bytes
* you can use more than 16 bytes buffer.
* @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress : specifies the slave address to be programmed(EEPROM ADDRESS).
* @param MemAddress : Internal memory address (WHERE YOU WANNA READ FROM)
* @param pData : Pointer to data buffer
* @param TxBufferSize : Amount of data to be Read
* @retval
*/
int at24_HAL_SequentialRead(I2C_HandleTypeDef *hi2c ,uint8_t DevAddress,uint16_t MemAddress,uint8_t *pData,uint16_t RxBufferSize)
{
/*
* just like WriteByte but get what it want
* but maybe you should know that the Data is location you want to save data
* for future use
*/
while( (RxBufferSize-16)>0)
{
while(HAL_I2C_Mem_Read(&hi2c, (uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData, (uint16_t)16, (uint32_t)1000)!= HAL_OK);
RxBufferSize-=16;
pData+=16;
MemAddress+=16;
}
while(HAL_I2C_Mem_Read(&hi2c, (uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData, (uint16_t)RxBufferSize, (uint32_t)1000)!= HAL_OK);
/*
* if DataRecive is 0xFF or 255 ,this means that block was empty
*/
return 1;
}
/*
* @brief : This function handles Erase Full chip.
* @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @retval
*/
int at24_HAL_EraseMemFull(I2C_HandleTypeDef *hi2c)
{
/*
* this may take will don't panic
*/
uint8_t EraseBuf[16] ={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
int i;
for (i =0 ; i<1024 ; i+16)
{
/*
* if you know,0xFF means that block is empty
*/
sdI2C_WriteBytes(&hi2c,0xA0,(uint16_t )i,EraseBuf,16);
}
return 1;
}
/**
* @brief : This function handles Writing String on the specific Address .
* This program have this feature that don't force you to use absolute 16 bytes
* you can use more than 16 bytes buffer.
* @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress : specifies the slave address to be programmed(EEPROM ADDRESS).
* @param MemAddress : Internal memory address (WHERE YOU WANNA Write)
* @param pString : Pointer to data buffer(CHAR DATA)
* @param length : Amount of buffer you wanna Write from
* @retval
*/
int at24_HAL_WriteString(I2C_HandleTypeDef *hi2c,char *pString ,uint16_t MemAddress ,uint8_t length)
{
uint8_t pData[length];
int i =0;
while(*pString)
(pData[i++])=(uint8_t)(*pString++);
sdI2C_WriteBytes(&hi2c,0xA0,MemAddress,pData,length);
return 1;
}
/**
* @brief : This function handles Reading String on the specific Address .
* This program have this feature that don't force you to use absolute 16 bytes
* you can use more than 16 bytes buffer.
* @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param MemAddress : Internal memory address (WHERE YOU WANNA READ)
* @param pString : Pointer to data buffer(CHAR DATA)
* @param length : Amount of buffer you wanna Read from
* @retval
*/
int at24_HAL_ReadString(I2C_HandleTypeDef *hi2c,char *pString,uint16_t MemAddress,uint8_t length)
{
uint8_t pData[length];
int i=0;
sdI2C_RandomRead(0xA0,MemAddress,pData,length);
while(pData[i])
(*pString++)=(char)pData[i++];
return 1;
}