WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**************************************************************************//**
* @file     pan_hal_i2c.h
* @version  V0.0.0
* $Revision: 1 $
* $Date:    23/09/10 $
* @brief    Panchip series I2C (Inter-Integrated Circuit) HAL header file.
* @note     Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
*****************************************************************************/
 
#ifndef __PAN_HAL_I2C_H
#define __PAN_HAL_I2C_H
#include "pan_hal_def.h"
 
/**
 * @defgroup pan_hal_i2c I2C driver
 * @{
 * @ingroup pan_i2c
 * @brief   Inter-Integrated Circuit (I2C) peripheral driver.
 */
/**
 * @brief Enumeration of I2C Address Modes.
 *
 * This enumeration defines the possible address modes for I2C communication, such as 7-bit or 10-bit addressing.
 */
typedef enum  {
  I2C_ADDR_7BIT,  /*!< 7-bit I2C address mode */
  I2C_ADDR_10BIT  /*!< 10-bit I2C address mode */
} I2C_AddressMode_Opt;
 
/**
 * @brief Enumeration of I2C Roles.
 *
 * This enumeration defines the possible roles for an I2C device, either a master or a slave.
 */
typedef enum
{
    I2C_ROLE_MASTER = ((uint16_t)0x0021), /*!< I2C master role */
    I2C_ROLE_SLAVE  = ((uint16_t)0x0000)  /*!< I2C slave role */
} I2C_Role_Opt;
 
/**
 * @brief Enumeration of I2C Duty Cycles.
 *
 * This enumeration defines the possible duty cycles for I2C fast mode, such as 16/9 or 2.
 */
typedef enum
{
    I2C_DUTYCYCLE_16_9 = I2C_DutyCycle_16_9, /*!< I2C fast mode duty cycle 16/9 */
    I2C_DUTYCYCLE_2    = I2C_DutyCycle_2     /*!< I2C fast mode duty cycle 2 */
} I2C_DutyCycle_Opt;
 
/**
 * @brief Enumeration of I2C Directions.
 *
 * This enumeration defines the possible data transfer directions for I2C, either transmitter or receiver.
 */
typedef enum
{
    I2C_DIR_TX = ((uint16_t)0x0000), /*!< I2C transmit direction */
    I2C_DIR_RX = ((uint16_t)0x0100)  /*!< I2C receive direction */
} I2C_Direction_Opt;
 
/**
 * @brief Enumeration of I2C Acknowledged Address Modes.
 *
 * This enumeration defines the possible address acknowledgment modes for I2C, such as 7-bit or 10-bit.
 */
typedef enum
{
    I2C_ACK_ADDR_7BIT  = I2C_AcknowledgedAddress_7bit,  /*!< I2C acknowledged address mode 7-bit */
    I2C_ACK_ADDR_10BIT = I2C_AcknowledgedAddress_10bit  /*!< I2C acknowledged address mode 10-bit */
} I2C_AckAddr_Opt;
 
/**
 * @brief Enumeration of I2C Clock Speeds.
 *
 * This enumeration defines the possible clock speeds for I2C communication in terms of bits per second (bps).
 */
typedef enum
{
  I2C_SPEED_100K = (100000), /*!< 100 Kbps I2C clock speed */
  I2C_SPEED_200K = (200000), /*!< 200 Kbps I2C clock speed */
  I2C_SPEED_400K = (400000), /*!< 400 Kbps I2C clock speed */
  I2C_SPEED_500K = (500000), /*!< 500 Kbps I2C clock speed */
  I2C_SPEED_1M   = (1000000),/*!< 1 Mbps I2C clock speed */
  I2C_SPEED_2M   = (2000000) /*!< 2 Mbps I2C clock speed */
} I2C_ClockSpeed_Opt;
 
/**
  * @brief  I2C Init structure definition
  */
 
typedef struct
{
  I2C_ClockSpeed_Opt ClockSpeed;          /*!< Specifies the clock frequency.
                                         This parameter must be set to a value lower than 400kHz */
 
  I2C_Role_Opt Role;                /*!< Specifies the I2C mode.
                                         This parameter can be a value of @ref I2C_mode */
 
  I2C_DutyCycle_Opt DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.
                                         This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
 
  uint16_t OwnAddress;              /*!< Specifies the first device own address.
                                         This parameter can be a 7-bit or 10-bit address. */
 
  I2C_AckAddr_Opt AckAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
                                         This parameter can be a value of @ref I2C_acknowledged_address */
 
  I2C_AddressMode_Opt AddressMode;
}I2C_Init_Opt;
 
typedef enum{
  I2C_Tx_Dir,
  I2C_Rx_Dir
}I2C_Xfer_Dir_Opt;
 
typedef struct
{
  uint8_t pass;
}I2C_Interrupt_Opt;
 
/**
 * @brief I2C Handle Structure Definition
 *
 * This structure defines the configuration and management parameters for I2C communication.
 */
typedef struct
{
    I2C_T* I2Cx; /*!< Base address of the I2C peripheral */
 
    I2C_Init_Opt I2C_InitObj; /*!< I2C communication parameters */
    I2C_Interrupt_Opt InterruptObj; /*!< I2C interrupt configuration */
 
    uint8_t *pTxBuffPtr; /*!< Pointer to the transmit buffer */
    uint16_t TxXferSize; /*!< Transmit data size */
    __IO uint16_t TxXferCount; /*!< Transmit data counter */
 
    uint8_t *pRxBuffPtr; /*!< Pointer to the receive buffer */
    uint16_t RxXferSize; /*!< Receive data size */
    __IO uint16_t RxXferCount; /*!< Receive data counter */
 
    IRQn_Type IRQn; /*!< I2C interrupt request number */
 
    I2C_CallbackFunc RxIntCallback; /*!< Callback function for receive interrupts */
    I2C_CallbackFunc TxIntCallback; /*!< Callback function for transmit interrupts */
 
    uint32_t DMA_SRC; /*!< DMA source configuration for I2C */
    uint32_t DMA_DST; /*!< DMA destination configuration for I2C */
 
    uint8_t StopFlag; /*!< Flag indicating whether to generate a stop condition */
 
} I2C_HandleTypeDef;
 
/**
 * @brief Initialize the I2C peripheral.
 *
 * This function initializes the I2C peripheral based on the configuration provided in the I2C_HandleTypeDef structure.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @return true if initialization is successful, false otherwise.
 */
bool HAL_I2C_Init(I2C_HandleTypeDef *i2c);
 
void HAL_I2C_DeInit(I2C_HandleTypeDef *i2c);
void HAL_I2C_Init_INT(I2C_HandleTypeDef *i2c);
void HAL_I2C_DeInit_INT(I2C_HandleTypeDef *i2c);
 
 
/**
 * @brief Transmit data as a master via I2C.
 *
 * This function sends data to a device with the specified address as a master using I2C communication.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param DevAddress  device address.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be transmitted.
 * @param Timeout Timeout duration for the operation.
 */
void HAL_I2C_Master_SendData(I2C_HandleTypeDef *i2c,uint16_t DevAddress,uint8_t *Buf,uint32_t Size,uint32_t Timeout);
 
/**
 * @brief Receive data as a master via I2C.
 *
 * This function receives data from a device with the specified address as a master using I2C communication.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param DevAddress device address.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be received.
 * @param Timeout Timeout duration for the operation.
 */
void HAL_I2C_Master_ReceiveData(I2C_HandleTypeDef *i2c,uint16_t DevAddress,uint8_t *Buf,uint32_t Size,uint32_t Timeout);
 
/**
 * @brief Transmit data as a master via I2C using interrupts.
 *
 * This function sends data to a device with the specified address as a master using I2C communication with interrupts.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param DevAddress device address.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be transmitted.
 * @param Callback Callback function to be called upon completion.
 */
void HAL_I2C_Master_SendData_INT(I2C_HandleTypeDef *i2c,uint16_t DevAddress,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
 
/**
 * @brief Receive data as a master via I2C using interrupts.
 *
 * This function receives data from a device with the specified address as a master using I2C communication with interrupts.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param DevAddress device address.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be received.
 * @param Callback Callback function to be called upon completion.
 */
void HAL_I2C_Master_ReceiveData_INT(I2C_HandleTypeDef *i2c,uint16_t DevAddress,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
/**
  * @brief  Sends data over I2C using DMA for the master mode.
  * @param  i2c: Pointer to the I2C handle structure.
  * @param  DevAddress: device address to communicate with.
  * @param  Buf: Pointer to the data buffer containing the data to be sent.
  * @param  Size: Number of bytes to send.
  * @param  Callback: Callback function to be executed when the DMA transfer is complete.
  * @retval None
  */
void HAL_I2C_Master_SendData_DMA(I2C_HandleTypeDef *i2c,uint16_t DevAddress,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
/**
 * @brief Receive data as a master via I2C using DMA.
 *
 * This function receives data from a device with the specified address as a master using I2C communication with DMA.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param DevAddress device address.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be received.
 * @param Callback Callback function to be called upon completion.
 */
void HAL_I2C_Master_ReceiveData_DMA(I2C_HandleTypeDef *i2c,uint16_t DevAddress,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
/**
 * @brief Send data as a slave via I2C.
 *
 * This function sends data as a slave in response to read requests from a master device.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be sent.
 * @param Timeout Timeout duration for the operation.
 */
void HAL_I2C_Slave_SendData(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,uint32_t Timeout);
 
/**
 * @brief Receive data as a slave via I2C.
 *
 * This function receives data as a slave in response to read commands from a master device.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be received.
 * @param Timeout Timeout duration for the operation.
 */
void HAL_I2C_Slave_ReceiveData(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,uint32_t Timeout);
 
/**
 * @brief Send data as a slave via I2C using interrupts.
 *
 * This function sends data as a slave in response to read requests from a master device using interrupts.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be sent.
 * @param Callback Callback function to be called upon completion.
 */
void HAL_I2C_Slave_SendData_INT(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
/**
 * @brief Receive data as a slave via I2C using interrupts.
 *
 * This function receives data as a slave in response to read commands from a master device using interrupts.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be received.
 * @param Callback Callback function to be called upon completion.
 */
void HAL_I2C_Slave_ReceiveData_INT(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
// uint32_t HAL_I2C_Slave_SendData_DMA(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
/**
 * @brief Send data as a slave via I2C using DMA.
 *
 * This function sends data as a slave in response to read requests from a master device using DMA.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be sent.
 * @param Callback Callback function to be called upon completion.
 * @return The DMA channel number used for the operation.
 */
void HAL_I2C_Slave_SendData_DMA(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
/**
 * @brief Receive data as a slave via I2C using DMA.
 *
 * This function receives data as a slave in response to read commands from a master device using DMA.
 *
 * @param i2c Pointer to the I2C_HandleTypeDef structure.
 * @param Buf Pointer to the data buffer.
 * @param Size Size of the data to be received.
 * @param Callback Callback function to be called upon completion.
 */
void HAL_I2C_Slave_ReceiveData_DMA(I2C_HandleTypeDef *i2c,uint8_t *Buf,uint32_t Size,I2C_CallbackFunc Callback);
 
 
// void HAL_I2C_Master_SendData_DMA_U16(I2C_HandleTypeDef *i2c, uint16_t DevAddress, uint16_t *Buf, uint32_t Size, I2C_CallbackFunc Callback);
 
 
/** @} */ // end of group
#endif /* __PAN_HAL_I2C_H */