summaryrefslogtreecommitdiff
path: root/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/include/bflb_i2c.h
blob: 3395e833a13eea0751e6cd3e2a5569318dc6083c (plain)
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
#ifndef _BFLB_I2C_H
#define _BFLB_I2C_H

#include "bflb_core.h"

/** @addtogroup LHAL
  * @{
  */

/** @addtogroup I2C
  * @{
  */

/* Bit definitions for the flags field in struct bflb_i2c_msg_s
 *
 * START/STOP Rules:
 *
 * 1. The lower half I2C driver will always issue the START condition at the
 *    beginning of a message unless I2C_M_NOSTART flag is set in the
 *    message.
 *
 * 2. The lower half I2C driver will always issue the STOP condition at the
 *    end of the messages unless:
 *
 *    a. The I2C_M_NOSTOP flag is set in the message, OR
 *    b. The following message has the I2C_M_NOSTART flag set (meaning
 *       that following message is simply a continuation of the transfer).
 *
 * A proper I2C repeated start would then have I2C_M_NOSTOP set on msg[n]
 * and I2C_M_NOSTART *not* set on msg[n+1].  See the following table:
 *
 *   msg[n].flags  msg[n+1].flags Behavior
 *   ------------ --------------- -----------------------------------------
 *   0            0                Two normal, separate messages with STOP
 *                                 on msg[n] then START on msg[n+1]
 *   0*           I2C_M_NOSTART    Continuation of the same transfer (must
 *                                 be the same direction).  See NOTE below.
 *   NO_STOP      0                No STOP on msg[n]; repeated START on
 *                                 msg[n+1].
 *
 * * NOTE: NO_STOP is implied in this case and may or not be explicitly
 *   included in the msg[n] flags
 */

#define I2C_M_READ    0x0001 /* Read data, from slave to master */
#define I2C_M_TEN     0x0002 /* Ten bit address */
#define I2C_M_DMA     0x0004 /* Enable dma mode */
#define I2C_M_NOSTOP  0x0040 /* Message should not end with a STOP */
#define I2C_M_NOSTART 0x0080 /* Message should not begin with a START */

/** @defgroup I2C_INTSTS i2c interrupt status definition
  * @{
  */
#define I2C_INTSTS_END     (1 << 0) /* Transfer end interrupt */
#define I2C_INTSTS_TX_FIFO (1 << 1) /* TX FIFO ready interrupt */
#define I2C_INTSTS_RX_FIFO (1 << 2) /* RX FIFO ready interrupt */
#define I2C_INTSTS_NACK    (1 << 3) /* NACK interrupt */
#define I2C_INTSTS_ARB     (1 << 4) /* Arbitration lost interrupt */
#define I2C_INTSTS_FER     (1 << 5) /* TX/RX FIFO error interrupt */
/**
 * @}
 */

/** @defgroup I2C_INTCLR i2c interrupt clear definition
  * @{
  */
#define I2C_INTCLR_END  (1 << 0) /* Transfer end interrupt */
#define I2C_INTCLR_NACK (1 << 3) /* NACK interrupt */
#define I2C_INTCLR_ARB  (1 << 4) /* Arbitration lost interrupt */
/**
 * @}
 */

/** @defgroup I2C_INTEN i2c interrupt enable definition
  * @{
  */
#define I2C_INTEN_END     (1 << 0) /* Transfer end interrupt */
#define I2C_INTEN_TX_FIFO (1 << 1) /* TX FIFO ready interrupt */
#define I2C_INTEN_RX_FIFO (1 << 2) /* RX FIFO ready interrupt */
#define I2C_INTEN_NACK    (1 << 3) /* NACK interrupt */
#define I2C_INTEN_ARB     (1 << 4) /* Arbitration lost interrupt */
#define I2C_INTEN_FER     (1 << 5) /* TX/RX FIFO error interrupt */
/**
 * @}
 */

/**
 * @brief I2C message structure
 *
 * @param addr          Slave address (7- or 10-bit)
 * @param flags         See I2C_M_* definitions
 * @param buffer        Buffer to be transferred
 * @param length        Length of the buffer in bytes, should be less than 256.
 */
struct bflb_i2c_msg_s {
    uint16_t addr;
    uint16_t flags;
    uint8_t *buffer;
    uint16_t length;
};

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Initialize i2c.
 *
 * @param [in] dev device handle
 * @param [in] frequency i2c frequency, range from 305Hz to 400KHz
 */
void bflb_i2c_init(struct bflb_device_s *dev, uint32_t frequency);

/**
 * @brief Deinitialize i2c.
 *
 * @param [in] dev device handle
 */
void bflb_i2c_deinit(struct bflb_device_s *dev);

/**
 * @brief Enable i2c tx dma.
 *
 * @param [in] dev device handle
 * @param [in] enable true means enable, otherwise disable.
 */
void bflb_i2c_link_txdma(struct bflb_device_s *dev, bool enable);

/**
 * @brief Enable i2c rx dma.
 *
 * @param [in] dev device handle
 * @param [in] enable true means enable, otherwise disable.
 */
void bflb_i2c_link_rxdma(struct bflb_device_s *dev, bool enable);

/**
 * @brief Start transferring i2c message.
 *
 * @param [in] dev device handle
 * @param [in] msgs pointer to i2c message
 * @param [in] count message count
 * @return A negated errno value on failure.
 */
int bflb_i2c_transfer(struct bflb_device_s *dev, struct bflb_i2c_msg_s *msgs, int count);

/**
 * @brief Enable or disable i2c interrupt.
 *
 * @param [in] dev device handle
 * @param [in] int_type interrupt type ,use @ref I2C_INTEN
 * @param [in] mask true means disable, false means enable
 */
void bflb_i2c_int_mask(struct bflb_device_s *dev, uint32_t int_type, bool mask);

/**
 * @brief Clear i2c interrupt status.
 *
 * @param [in] dev device handle
 * @param [in] int_clear clear value, use @ref I2C_INTCLR
 */
void bflb_i2c_int_clear(struct bflb_device_s *dev, uint32_t int_clear);

/**
 * @brief Get i2c interrupt status.
 *
 * @param [in] dev device handle
 * @return interrupt status value, use @ref I2C_INTSTS
 */
uint32_t bflb_i2c_get_intstatus(struct bflb_device_s *dev);

/**
 * @brief Control i2c feature.
 *
 * @param [in] dev device handle
 * @param [in] cmd feature command
 * @param [in] arg user data
 * @return A negated errno value on failure.
 */
int bflb_i2c_feature_control(struct bflb_device_s *dev, int cmd, size_t arg);

#ifdef __cplusplus
}
#endif

/**
  * @}
  */

/**
  * @}
  */

#endif