Contiki-NG
i2c.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Mehdi Migault
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup cc2538
33  * @{
34  *
35  * \defgroup cc2538-i2c cc2538 I2C Control
36  *
37  * cc2538 I2C Control Module
38  * @{
39  *
40  * \file
41  * Header file with declarations for the I2C Control module
42  *
43  * \author
44  * Mehdi Migault
45  */
46 #ifndef I2C_H_
47 #define I2C_H_
48 
49 #include "reg.h"
50 #include "sys-ctrl.h"
51 #include "gpio.h"
52 #include "ioc.h"
53 #include <stdio.h> /* For debug */
54 #include "clock.h" /* For temporisation */
55 /*---------------------------------------------------------------------------*/
56 /** \name I2C Master commands
57  * @{
58  */
59 #define I2C_MASTER_CMD_SINGLE_SEND 0x00000007
60 #define I2C_MASTER_CMD_SINGLE_RECEIVE 0x00000007
61 #define I2C_MASTER_CMD_BURST_SEND_START 0x00000003
62 #define I2C_MASTER_CMD_BURST_SEND_CONT 0x00000001
63 #define I2C_MASTER_CMD_BURST_SEND_FINISH 0x00000005
64 #define I2C_MASTER_CMD_BURST_SEND_ERROR_STOP 0x00000004
65 #define I2C_MASTER_CMD_BURST_RECEIVE_START 0x0000000b
66 #define I2C_MASTER_CMD_BURST_RECEIVE_CONT 0x00000009
67 #define I2C_MASTER_CMD_BURST_RECEIVE_FINISH 0x00000005
68 #define I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP 0x00000004
69 /** @} */
70 /*---------------------------------------------------------------------------*/
71 /** \name I2C Master status flags
72  * @{
73  */
74 #define I2C_MASTER_ERR_NONE 0
75 #define I2CM_STAT_BUSY 0x00000001
76 #define I2CM_STAT_ERROR 0x00000002
77 #define I2CM_STAT_ADRACK 0x00000004
78 #define I2CM_STAT_DATACK 0x00000008
79 #define I2CM_STAT_ARBLST 0x00000010
80 #define I2CM_STAT_IDLE 0x00000020
81 #define I2CM_STAT_BUSBSY 0x00000040
82 #define I2CM_STAT_INVALID 0x00000080
83 /** @} */
84 /*---------------------------------------------------------------------------*/
85 /** \name I2C registers
86  * @{
87  */
88 #define I2CM_CR 0x40020020 /* I2C master config */
89 #define I2CM_TPR 0x4002000C /* I2C master timer period */
90 #define I2CM_SA 0x40020000 /* I2C master slave address */
91 #define I2CM_DR 0x40020008 /* I2C master data */
92 #define I2CM_CTRL 0x40020004 /* Master control in write */
93 #define I2CM_STAT I2CM_CTRL /* Master status in read */
94 /** @} */
95 /*---------------------------------------------------------------------------*/
96 /** \name I2C Miscellaneous
97  * @{
98  */
99 #define I2C_SCL_NORMAL_BUS_SPEED 100000 /* 100KHz I2C */
100 #define I2C_SCL_FAST_BUS_SPEED 400000 /* 400KHz I2C */
101 #define I2C_RECEIVE 0x01 /* Master receive */
102 #define I2C_SEND 0x00 /* Master send */
103 /** @} */
104 /*---------------------------------------------------------------------------*/
105 /**
106  * \name I2C Functions
107  * @{
108  */
109 
110 /**
111  * \brief Initialize the I2C peripheral and pins
112  * \param port_sda The GPIO number of the pin used fort SDA
113  * \param pin_sda The pin number used for SDA
114  * \param port_scl The GPIO number of the pin used fort SCL
115  * \param pin_scl The pin number used for SCL
116  * \param bus_speed The clock frequency used by I2C module
117  *
118  * \e bus_speed can take the following values:
119  *
120  * - I2C_SCL_NORMAL_BUS_SPEED : 100KHz
121  * - I2C_SCL_FAST_BUS_SPEED : 400KHz
122  */
123 void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl,
124  uint8_t pin_scl, uint32_t bus_speed);
125 
126 /** \brief Enable master I2C module */
127 void i2c_master_enable(void);
128 
129 /** \brief Disable master I2C module */
130 void i2c_master_disable(void);
131 
132 /**
133  * \brief Initialize I2C peripheral clock with given frequency
134  * \param freq The desired frequency
135  *
136  * \e freq can take the following values:
137  *
138  * - I2C_SCL_NORMAL_BUS_SPEED : 100KHz
139  * - I2C_SCL_FAST_BUS_SPEED : 400KHz
140  */
141 void i2c_set_frequency(uint32_t freq);
142 
143 /**
144  * \brief Set the address of slave and access mode for the next I2C communication
145  * \param slave_addr The receiver slave address on 7 bits
146  * \param access_mode The I2C access mode (send/receive)
147  *
148  * \e access_mode can take the following values:
149  *
150  * - I2C_RECEIVE : 1
151  * - I2C_SEND : 0
152  */
153 void i2c_master_set_slave_address(uint8_t slave_addr, uint8_t access_mode);
154 
155 /**
156  * \brief Prepare data to be transmitted
157  * \param data The byte of data to be transmitted from the I2C master
158  */
159 void i2c_master_data_put(uint8_t data);
160 
161 /**
162  * \brief Return received data from I2C
163  * \return The byte received by I2C after à receive command
164  */
165 uint8_t i2c_master_data_get(void);
166 
167 /**
168  * \brief Control the state of the master module for send and receive operations
169  * \param cmd The operation to perform
170  *
171  * \e cmd can take the following values:
172  *
173  * - I2C_MASTER_CMD_SINGLE_SEND
174  * - I2C_MASTER_CMD_SINGLE_RECEIVE
175  * - I2C_MASTER_CMD_BURST_SEND_START
176  * - I2C_MASTER_CMD_BURST_SEND_CONT
177  * - I2C_MASTER_CMD_BURST_SEND_FINISH
178  * - I2C_MASTER_CMD_BURST_SEND_ERROR_STOP
179  * - I2C_MASTER_CMD_BURST_RECEIVE_START
180  * - I2C_MASTER_CMD_BURST_RECEIVE_CONT
181  * - I2C_MASTER_CMD_BURST_RECEIVE_FINISH
182  * - I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP
183  */
184 void i2c_master_command(uint8_t cmd);
185 
186 /**
187  * \brief Return the busy state of I2C module
188  * \retval 0 The I2C module is not busy
189  * \retval 1 The I2C module is busy
190  */
191 uint8_t i2c_master_busy(void);
192 
193 /**
194  * \brief Return the status register if error occurred during last communication
195  * \retval I2C_MASTER_ERR_NONE Return 0 if no error occurred
196  *
197  * If an error occurred, return the status register of the I2C module.
198  * Use the result with the I2CM_STAT_* flags to custom your processing
199  */
200 uint8_t i2c_master_error(void);
201 /**
202  * \brief Perform all operations to send a byte to a slave
203  * \param slave_addr The adress of the slave to which data are sent
204  * \param data The data to send to the slave
205  * \return Return the value of i2c_master_error() after the I2C operation
206  */
207 uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data);
208 
209 /**
210  * \brief Perform all operations to receive a byte from a slave
211  * \param slave_addr The address of the slave from which data are received
212  * \param data A pointer to store the received data
213  * \return Return the value of i2c_master_error() after the I2C operation
214  */
215 uint8_t i2c_single_receive(uint8_t slave_addr, uint8_t *data);
216 /**
217  * \brief Perform all operations to send multiple bytes to a slave
218  * \param slave_addr The address of the slave to which data are sent
219  * \param data A pointer to the data to send to the slave
220  * \param len Number of bytes to send
221  * \return Return the value of i2c_master_error() after the I2C operation
222  */
223 uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len);
224 
225 /**
226  * \brief Perform all operations to receive multiple bytes from a slave
227  * \param slave_addr The address of the slave from which data are received
228  * \param data A pointer to store the received data
229  * \param len Number of bytes to receive
230  * \return Return the value of i2c_master_error() after the I2C operation
231  */
232 uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len);
233 /** @} */
234 
235 #endif /* I2C_H_ */
236 
237 /**
238  * @}
239  * @}
240  */
uint8_t i2c_master_data_get(void)
Return received data from I2C.
Definition: i2c.c:127
uint8_t i2c_master_busy(void)
Return the busy state of I2C module.
Definition: i2c.c:141
Header file for the cc2538 System Control driver.
void i2c_set_frequency(uint32_t freq)
Initialize I2C peripheral clock with given frequency.
Definition: i2c.c:103
void i2c_master_data_put(uint8_t data)
Prepare data to be transmitted.
Definition: i2c.c:121
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
Definition: i2c.c:188
Header file with register and macro declarations for the cc2538 GPIO module.
Header file with declarations for the I/O Control module.
Header file with register manipulation macro definitions.
uint8_t i2c_master_error(void)
Return the status register if error occurred during last communication.
Definition: i2c.c:147
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
Definition: i2c.c:49
void i2c_master_disable(void)
Disable master I2C module.
Definition: i2c.c:97
void i2c_master_command(uint8_t cmd)
Control the state of the master module for send and receive operations.
Definition: i2c.c:133
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
Definition: i2c.c:218
void i2c_master_set_slave_address(uint8_t slave_addr, uint8_t access_mode)
Set the address of slave and access mode for the next I2C communication.
Definition: i2c.c:111
uint8_t i2c_single_receive(uint8_t slave_addr, uint8_t *data)
Perform all operations to receive a byte from a slave.
Definition: i2c.c:172
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.
Definition: i2c.c:159
void i2c_master_enable(void)
Enable master I2C module.
Definition: i2c.c:91