-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.h
73 lines (59 loc) · 3.1 KB
/
i2c.h
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
////////////////////////////////////////////////////////////////////////////////
/// @file
/// @brief Inter-Integrated Circuit (I2C) driver.
////////////////////////////////////////////////////////////////////////////////
#ifndef I2C_H_
#define I2C_H_
// *****************************************************************************
// ************************** System Include Files *****************************
// *****************************************************************************
#include <xc.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// *****************************************************************************
// ************************** User Include Files *******************************
// *****************************************************************************
// *****************************************************************************
// ************************** Defines ******************************************
// *****************************************************************************
typedef enum {
I2C_DRV_STATUS_WAIT = 0, // Waiting for I2C bus access.
I2C_DRV_STATUS_BUSY, // Transfer in progress.
I2C_DRV_STATUS_DONE, // Transfer complete.
I2C_DRV_STATUS_ERROR, // Transfer error.
} I2C_DRV_STATUS;
typedef struct {
uint8_t dev; // I2C bus target device, address dependent.
uint32_t freq; // I2C clock frequency.
uint8_t rw; // 0 = Write, 1 = Read
uint8_t addr; // I2C register address.
uint8_t numBytes; // Number of bytes to write/read.
uint8_t *buf; // Pointer to transmit/receive buffer.
I2C_DRV_STATUS *status; // Pointer to status flag.
} I2C_TRANSFER;
// *****************************************************************************
// ************************** Declarations *************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Function Prototypes ******************************
// *****************************************************************************
////////////////////////////////////////////////////////////////////////////////
/// @brief Queue an I2C transfer (read or write).
///
/// @param xfer
/// Pointer to control and communication data.
///
/// @return Identification of transfer queue success.
/// -1 - \p xfer data is invalid.
/// 0 - I2C transfer already in progress.
/// 1 - Queue of I2C transfer successful.
///
/// This function queues I2C data for transfer. The calling function must not
/// modify data reference by \p xfer until the transfer is complete. Transfer
/// completion can be identified by the calling function by inspecting
/// \p xfer->status. A value of \e DONE or \e ERROR identifies transfer
/// completion.
////////////////////////////////////////////////////////////////////////////////
int16_t I2CXfer( I2C_TRANSFER *xfer );
#endif // I2C_H_