-
Notifications
You must be signed in to change notification settings - Fork 0
/
m24256.c
231 lines (203 loc) · 5.44 KB
/
m24256.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
220
221
222
223
224
225
226
227
228
229
230
231
/**
******************************************************************************
* @file m24256.c
* @author MCD Application Team
* @brief This file provides a set of functions to interface with the M24256
* device.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "m24256.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup Components
* @{
*/
/** @defgroup M24256 M24256
* @{
*/
/** @defgroup M24256_Private_Types M24256 Private Types
* @{
*/
M24256_EEPROM_Drv_t M24256_EEPROM_Driver =
{
M24256_Init,
M24256_DeInit,
M24256_Write,
M24256_Read,
M24256_IsReady
};
/**
* @}
*/
/** @defgroup M24256_Private_FunctionPrototypes M24256 Private Function Prototypes
* @{
*/
static int32_t M24256_ReadWrap(const void *handle, uint16_t Addr, uint8_t *pData, uint16_t Length);
static int32_t M24256_WriteWrap(const void *handle, uint16_t Addr, uint8_t *pData, uint16_t Length);
static int32_t M24256_IsReadyWrap(const void *handle, uint32_t Trials);
/**
* @}
*/
/** @defgroup M24256_Private_Functions M24256 Private Functions
* @{
*/
/**
* @brief Function
* @param Component object pointer
* @retval Component status
*/
int32_t M24256_RegisterBusIO(M24256_Object_t *pObj, M24256_IO_t *pIO)
{
int32_t ret;
if (pObj == NULL)
{
ret = M24256_ERROR;
}
else
{
pObj->IO.Init = pIO->Init;
pObj->IO.DeInit = pIO->DeInit;
pObj->IO.Address = pIO->Address;
pObj->IO.Write = pIO->Write;
pObj->IO.Read = pIO->Read;
pObj->IO.IsReady = pIO->IsReady;
pObj->Ctx.Read = M24256_ReadWrap;
pObj->Ctx.Write = M24256_WriteWrap;
pObj->Ctx.IsReady = M24256_IsReadyWrap;
pObj->Ctx.handle = pObj;
if (pObj->IO.Init != NULL)
{
ret = pObj->IO.Init();
}
else
{
ret = M24256_ERROR;
}
}
return ret;
}
/**
* @brief Initializes the M24256 EEPROM component.
* @param pObj M24256 object
* @retval Component status
*/
int32_t M24256_Init(M24256_Object_t *pObj)
{
if (pObj->IsInitialized != 1U)
{
pObj->IO.Init();
pObj->IsInitialized = 1U;
}
return M24256_OK;
}
/**
* @brief DeInitializes the M24256 EEPROM component.
* @param pObj M24256 object
* @retval Component status
*/
int32_t M24256_DeInit(M24256_Object_t *pObj)
{
if (pObj->IsInitialized != 0U)
{
pObj->IsInitialized = 0U;
}
return M24256_OK;
}
/**
* @brief DeInitializes the M24256 EEPROM component.
* @param pObj M24256 object
* @retval Component status
*/
int32_t M24256_Write(const M24256_Object_t *pObj, uint16_t Addr, uint8_t *pData, uint16_t Length)
{
return pObj->Ctx.Write(pObj->Ctx.handle, Addr, pData, Length);
}
/**
* @brief DeInitializes the M24256 EEPROM component.
* @param pObj M24256 object
* @retval Component status
*/
int32_t M24256_Read(const M24256_Object_t *pObj, uint16_t Addr, uint8_t *pData, uint16_t Length)
{
return pObj->Ctx.Read(pObj->Ctx.handle, Addr, pData, Length);
}
/**
* @brief Check if the M24256 EEPROM component is ready.
* @param pObj M24256 object
* @param Trials The number of trials before returning a timeout error
* @retval Component status
*/
int32_t M24256_IsReady(const M24256_Object_t *pObj, uint32_t Trials)
{
return pObj->Ctx.IsReady(pObj->Ctx.handle, Trials);
}
/**
* @}
*/
/** @addtogroup M24256_Private_FunctionPrototypes
* @{
*/
/**
* @brief Wrap component ReadReg to Bus Read function
* @param handle Component object handle
* @param Addr The target address to read
* @param pData The target register value to be written
* @param Length Buffer size to be written
* @retval Component status
*/
static int32_t M24256_ReadWrap(const void *handle, uint16_t Addr, uint8_t *pData, uint16_t Length)
{
/* Derogation MISRAC2012-Rule-11.5 */
const M24256_Object_t *pObj = (const M24256_Object_t *)handle;
return pObj->IO.Read(pObj->IO.Address, Addr, pData, Length);
}
/**
* @brief Wrap component WriteReg to Bus Write function
* @param handle Component object handle
* @param Addr The target address to write
* @param pData The target register value to be written
* @param Length Buffer size to be written
* @retval Component status
*/
static int32_t M24256_WriteWrap(const void *handle, uint16_t Addr, uint8_t *pData, uint16_t Length)
{
/* Derogation MISRAC2012-Rule-11.5 */
const M24256_Object_t *pObj = (const M24256_Object_t *)handle;
return pObj->IO.Write(pObj->IO.Address, Addr, pData, Length);
}
/**
* @brief Wrap component IsReady to Bus IsReady function
* @param handle Component object handle
* @param Trials The number of trials before returning a timeout error
* @retval Component status
*/
static int32_t M24256_IsReadyWrap(const void *handle, uint32_t Trials)
{
/* Derogation MISRAC2012-Rule-11.5 */
const M24256_Object_t *pObj = (const M24256_Object_t *)handle;
return pObj->IO.IsReady(pObj->IO.Address, Trials);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/