1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv_iic.h
7  * @brief    header file for iic driver
8  * @version  V1.0
9  * @date     02. June 2017
10  * @model    iic
11  ******************************************************************************/
12 
13 #ifndef _CSI_IIC_H_
14 #define _CSI_IIC_H_
15 
16 
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <drv_common.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 /// definition for iic handle.
25 typedef void *iic_handle_t;
26 
27 /*----- IIC Control Codes: Mode -----*/
28 typedef enum {
29     IIC_MODE_MASTER,             ///< IIC Master
30     IIC_MODE_SLAVE               ///< IIC Slave
31 } iic_mode_e;
32 
33 /*----- IIC Control Codes: IIC Bus Speed -----*/
34 typedef enum {
35     IIC_BUS_SPEED_STANDARD  = 0, ///< Standard Speed (100kHz)
36     IIC_BUS_SPEED_FAST      = 1, ///< Fast Speed     (400kHz)
37     IIC_BUS_SPEED_FAST_PLUS = 2, ///< Fast+ Speed    (  1MHz)
38     IIC_BUS_SPEED_HIGH      = 3  ///< High Speed     (3.4MHz)
39 } iic_speed_e;
40 
41 /*----- IIC Control Codes: IIC Address Mode -----*/
42 typedef enum {
43     IIC_ADDRESS_7BIT        = 0,  ///< 7-bit address mode
44     IIC_ADDRESS_10BIT       = 1   ///< 10-bit address mode
45 } iic_address_mode_e;
46 
47 /**
48 \brief IIC Status
49 */
50 typedef struct {
51     uint32_t busy             : 1;        ///< Transmitter/Receiver busy flag
52     uint32_t mode             : 1;        ///< Mode: 0=Slave, 1=Master
53     uint32_t direction        : 1;        ///< Direction: 0=Transmitter, 1=Receiver
54     uint32_t general_call     : 1;        ///< General Call(address 0) indication (cleared on start of next Slave operation)
55     uint32_t arbitration_lost : 1;        ///< Master lost arbitration(in case of multi-masters) (cleared on start of next Master operation)
56     uint32_t bus_error        : 1;        ///< Bus error detected (cleared on start of next Master/Slave operation)
57 } iic_status_t;
58 
59 /****** IIC Event *****/
60 typedef enum {
61     IIC_EVENT_TRANSFER_DONE        = 0,  ///< Master/Slave Transmit/Receive finished
62     IIC_EVENT_TRANSFER_INCOMPLETE  = 1,  ///< Master/Slave Transmit/Receive incomplete transfer
63     IIC_EVENT_SLAVE_TRANSMIT       = 2,  ///< Slave Transmit operation requested
64     IIC_EVENT_SLAVE_RECEIVE        = 3,  ///< Slave Receive operation requested
65     IIC_EVENT_ADDRESS_NACK         = 4,  ///< Address not acknowledged from Slave
66     IIC_EVENT_GENERAL_CALL         = 5,  ///< General Call indication
67     IIC_EVENT_ARBITRATION_LOST     = 6,  ///< Master lost arbitration
68     IIC_EVENT_BUS_ERROR            = 7,  ///< Bus error detected (START/STOP at illegal position)
69     IIC_EVENT_BUS_CLEAR            = 8   ///< Bus clear finished
70 } iic_event_e;
71 
72 typedef void (*iic_event_cb_t)(int32_t idx, iic_event_e event);  ///< Pointer to \ref iic_event_cb_t : IIC Event call back.
73 
74 /**
75 \brief IIC Driver Capabilities.
76 */
77 typedef struct  {
78     uint32_t address_10_bit : 1;          ///< supports 10-bit addressing
79 } iic_capabilities_t;
80 
81 /**
82   \brief       Initialize IIC Interface specified by pins. \n
83                1. Initializes the resources needed for the IIC interface 2.registers event callback function
84   \param[in]   idx iic index
85   \param[in]   cb_event event callback function \ref iic_event_cb_t
86   \return      0 for success, negative for error code
87 */
88 iic_handle_t csi_iic_initialize(int32_t idx, iic_event_cb_t cb_event);
89 
90 /**
91   \brief       De-initialize IIC Interface. stops operation and releases the software resources used by the interface
92   \param[in]   handle  iic handle to operate.
93   \return      0 for success, negative for error code
94 */
95 int32_t csi_iic_uninitialize(iic_handle_t handle);
96 
97 /**
98   \brief       Get driver capabilities.
99   \param[in]   idx iic index
100   \return      \ref iic_capabilities_t
101 */
102 iic_capabilities_t csi_iic_get_capabilities(int32_t idx);
103 
104 /**
105   \brief       config iic attributes.
106   \param[in]   handle    iic handle to operate.
107   \param[in]   mode      iic mode \ref iic_mode_e. if negative, then this attribute not changed.
108   \param[in]   speed     iic speed \ref iic_speed_e. if negative, then this attribute not changed.
109   \param[in]   addr_mode iic address mode \ref iic_address_mode_e. if negative, then this attribute not changed.
110   \param[in]   slave_addr iic address of slave. if negative, then this attribute not changed.
111   \return      0 for success, negative for error code
112 */
113 int32_t csi_iic_config(iic_handle_t handle,
114                        iic_mode_e mode,
115                        iic_speed_e speed,
116                        iic_address_mode_e addr_mode,
117                        int32_t slave_addr);
118 
119 /**
120   \brief       Start transmitting data as IIC Master.
121                This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
122                \ref csi_iic_get_status can get operating status.
123   \param[in]   handle         iic handle to operate.
124   \param[in]   devaddr        iic addrress of slave device. |_BIT[7:1]devaddr_|_BIT[0]R/W_|
125                               eg: BIT[7:0] = 0xA0, devaddr = 0x50.
126   \param[in]   data           data to send to IIC Slave
127   \param[in]   num            Number of data items to send
128   \param[in]   xfer_pending   Transfer operation is pending - Stop condition will not be generated
129                               Master generates STOP condition (if xfer_pending is "false")
130   \return      0 for success, negative for error code
131 */
132 int32_t csi_iic_master_send(iic_handle_t handle, uint32_t devaddr, const void *data, uint32_t num, bool xfer_pending);
133 
134 /**
135   \brief       Start receiving data as IIC Master.
136                This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
137                \ref csi_iic_get_status can get operating status.
138   \param[in]   handle  iic handle to operate.
139   \param[in]   devaddr        iic addrress of slave device.
140   \param[out]  data    Pointer to buffer for data to receive from IIC receiver
141   \param[in]   num     Number of data items to receive
142   \param[in]   xfer_pending   Transfer operation is pending - Stop condition will not be generated
143   \return      0 for success, negative for error code
144 */
145 int32_t csi_iic_master_receive(iic_handle_t handle, uint32_t devaddr, void *data, uint32_t num, bool xfer_pending);
146 
147 /**
148   \brief       Start transmitting data as IIC Slave.
149                This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
150                \ref csi_iic_get_status can get operating status.
151   \param[in]   handle  iic handle to operate.
152   \param[in]   data  Pointer to buffer with data to transmit to IIC Master
153   \param[in]   num   Number of data items to send
154   \return      0 for success, negative for error code
155 */
156 int32_t csi_iic_slave_send(iic_handle_t handle, const void *data, uint32_t num);
157 
158 /**
159   \brief       Start receiving data as IIC Slave.
160                This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
161                \ref csi_iic_get_status can get operating status.
162   \param[in]   handle  iic handle to operate.
163   \param[out]  data  Pointer to buffer for data to receive from IIC Master
164   \param[in]   num   Number of data items to receive
165   \return      0 for success, negative for error code
166 */
167 int32_t csi_iic_slave_receive(iic_handle_t handle, void *data, uint32_t num);
168 
169 /**
170   \brief       abort transfer.
171   \param[in]   handle  iic handle to operate.
172   \return      0 for success, negative for error code
173 */
174 int32_t csi_iic_abort_transfer(iic_handle_t handle);
175 
176 /**
177   \brief       Get IIC status.
178   \param[in]   handle  iic handle to operate.
179   \return      IIC status \ref iic_status_t
180 */
181 iic_status_t csi_iic_get_status(iic_handle_t handle);
182 
183 /**
184   \brief       control IIC power.
185   \param[in]   handle  iic handle to operate.
186   \param[in]   state   power state.\ref csi_power_stat_e.
187   \return      error code
188 */
189 int32_t csi_iic_power_control(iic_handle_t handle, csi_power_stat_e state);
190 
191 /**
192   \brief       config iic mode.
193   \param[in]   handle  iic handle to operate.
194   \param[in]   mode      \ref iic_mode_e
195   \return      error code
196 */
197 int32_t csi_iic_config_mode(iic_handle_t handle, iic_mode_e mode);
198 
199 /**
200   \brief       config iic speed.
201   \param[in]   handle  iic handle to operate.
202   \param[in]   speed     \ref iic_speed_e
203   \return      error code
204 */
205 int32_t csi_iic_config_speed(iic_handle_t handle, iic_speed_e speed);
206 
207 /**
208   \brief       config iic address mode.
209   \param[in]   handle  iic handle to operate.
210   \param[in]   addr_mode \ref iic_address_mode_e
211   \return      error code
212 */
213 int32_t csi_iic_config_addr_mode(iic_handle_t handle, iic_address_mode_e addr_mode);
214 
215 
216 /**
217   \brief       config iic slave address.
218   \param[in]   handle  iic handle to operate.
219   \param[in]   slave_addr slave address
220   \return      error code
221 */
222 int32_t csi_iic_config_slave_addr(iic_handle_t handle, int32_t slave_addr);
223 
224 /**
225   \brief       Get IIC transferred data count.
226   \param[in]   handle  iic handle to operate.
227   \return      the number of the currently transferred data items
228 */
229 uint32_t csi_iic_get_data_count(iic_handle_t handle);
230 
231 /**
232   \brief       Send START command.
233   \param[in]   handle  iic handle to operate.
234   \return      error code
235 */
236 int32_t csi_iic_send_start(iic_handle_t handle);
237 
238 /**
239   \brief       Send STOP command.
240   \param[in]   handle  iic handle to operate.
241   \return      error code
242 */
243 int32_t csi_iic_send_stop(iic_handle_t handle);
244 
245 /**
246   \brief       Reset IIC peripheral.
247   \param[in]   handle  iic handle to operate.
248   \return      error code
249 */
250 int32_t csi_iic_reset(iic_handle_t handle);
251 
252 
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 #endif /* _CSI_IIC_H_ */
258