libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
i2c_common.c
Go to the documentation of this file.
1/** @addtogroup i2c_file I2C peripheral API
2 *
3 * @brief <b>Access functions for the I2C Controller</b>
4 *
5 * @ingroup peripheral_apis
6 * LGPL License Terms @ref lgpl_license
7 * @author @htmlonly &copy; @endhtmlonly 2016
8 * Maxim Sloyko <maxims@google.com>
9 * @author @htmlonly &copy; @endhtmlonly 2021 - 2022
10 * Eduard Drusa <ventyl86(at)netkosice(dot)sk>
11 *
12 */
13
14/*
15 * This file is part of the libopencm3 project.
16 *
17 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
18 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
19 *
20 * This library is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public License
31 * along with this library. If not, see <http://www.gnu.org/licenses/>.
32 */
33
34#include <libopencm3/nrf/i2c.h>
35
36/**@{*/
37
38/** @brief Enable I2C peripheral
39 *
40 * @param[in] i2c i2c peripheral base, see @ref i2c_block
41 * @param[in] mode i2c @ref i2c_mode
42 */
43void i2c_enable(uint32_t i2c, uint32_t mode)
44{
45 I2C_ENABLE(i2c) = mode;
46}
47
48/** @brief Disable I2C peripheral
49 *
50 * @param[in] i2c i2c peripheral base, see @ref i2c_block
51 */
52void i2c_disable(uint32_t i2c)
53{
54 I2C_ENABLE(i2c) = 0;
55}
56
57/** @brief Start I2C transmission.
58 *
59 * Starts STARTTX task, which generates start condition on I2C bus and
60 * transmits address previously configured by @ref i2c_set_address.
61 *
62 * @param[in] i2c i2c peripheral base, see @ref i2c_block.
63 */
64void i2c_start_tx(uint32_t i2c)
65{
66 I2C_TASK_STARTTX(i2c) = 1;
67}
68
69/** @brief Start I2C reception.
70 *
71 * @param[in] i2c i2c peripheral base, see @ref i2c_block.
72 */
73void i2c_start_rx(uint32_t i2c)
74{
75 I2C_TASK_STARTRX(i2c) = 1;
76}
77
78/** @brief Signal stop on I2C line.
79 *
80 * @param[in] i2c i2c peripheral base, see @ref i2c_block.
81 */
82void i2c_send_stop(uint32_t i2c)
83{
84 I2C_TASK_STOP(i2c) = 1;
85}
86
87/** @brief Select Fast (400kHz) mode.
88 *
89 * @param[in] i2c i2c peripheral base, see @ref i2c_block.
90 */
91void i2c_set_fast_mode(uint32_t i2c)
92{
94}
95
96/** @brief Select Standard (100kHz) mode.
97 *
98 * @param[in] i2c i2c peripheral base, see @ref i2c_block.
99 */
100void i2c_set_standard_mode(uint32_t i2c)
101{
103}
104
105/** @brief Set I2C frequency.
106 *
107 * In addition to Standard (100kHz) and Fast (400kHz) modes
108 * this peripheral also supports 250kHz mode.
109 *
110 * @param[in] i2c i2c peripheral base, see @ref i2c_block
111 * @param[in] freq frequency constant. See @ref i2c_freq_const for details
112 * and note that this is not actually a frequency in Hz or kHz.
113 */
114void i2c_set_frequency(uint32_t i2c, uint32_t freq)
115{
116 I2C_FREQUENCY(i2c) = freq;
117}
118
119/** @brief Write Data to TXD register to be sent.
120 *
121 * Writes one byte into transmission buffer. This API is only
122 * available if @ref I2C_MODE_LEGACY is activated.
123 *
124 * @param[in] i2c i2c peripheral base, see @ref i2c_block
125 * @param[in] data byte to send next.
126 */
127void i2c_send_data(uint32_t i2c, uint8_t data)
128{
129 I2C_TXD(i2c) = data;
130}
131
132/** @brief Read Data from RXD register.
133 *
134 * Reads one byte from reception buffer. This API is only
135 * available if @ref I2C_MODE_LEGACY is activated.
136 *
137 * @param[in] i2c i2c peripheral base, see @ref i2c_block
138 * @returns data from RXD register.
139 */
140uint8_t i2c_get_data(uint32_t i2c)
141{
142 return (uint8_t)I2C_RXD(i2c);
143}
144
145/** @brief Select GPIO pins to be used by this peripheral.
146 *
147 * Configures GPIO pins assigned to SCL and SDA signals. These pins are only occupied
148 * by I2C peripheral whenever it is enabled using @ref i2c_enable. It is possible to
149 * ignore any given signal and not map it to pin by using special value of
150 * @ref GPIO_UNCONNECTED instead of @ref gpio_pin_id values.
151 *
152 * This needs to be configured when no transaction is in progress.
153 *
154 * @param[in] i2c i2c peripheral base, see @ref i2c_block
155 * @param[in] scl_pin GPIO pin used for SCL signal
156 * @param[in] sda_pin GPIO pin used for SDA signal
157 */
158void i2c_select_pins(uint32_t i2c, uint32_t scl_pin, uint32_t sda_pin)
159{
160 if (scl_pin != GPIO_UNCONNECTED) {
161 I2C_PSELSCL(i2c) = __GPIO2PIN(scl_pin);
162 } else {
163 I2C_PSELSCL(i2c) = scl_pin;
164 }
165
166 if (sda_pin != GPIO_UNCONNECTED) {
167 I2C_PSELSDA(i2c) = __GPIO2PIN(sda_pin);
168 } else {
169 I2C_PSELSDA(i2c) = sda_pin;
170 }
171}
172
173/** @brief Set 7bit I2C address of the device you wish to communicate with.
174 *
175 * @param[in] i2c i2c peripheral base, see @ref i2c_block
176 * @param[in] addr device address (7bit).
177 */
178void i2c_set_address(uint32_t i2c, uint8_t addr)
179{
180 I2C_ADDRESS(i2c) = addr;
181}
182
183/** @brief Resume I2C transaction.
184 *
185 * This function is unusual, but required to implement
186 * i2c exchange with this peripheral.
187 *
188 * @param[in] i2c i2c peripheral base, see @ref i2c_block
189 */
190void i2c_resume(uint32_t i2c)
191{
193}
194
195/** Configure event -> task shortcuts
196 *
197 * Sets new shortcut configuration bitmask for I2C peripheral.
198 *
199 * @param[in] i2c i2c peripheral base, see @ref i2c_block
200 * @param[in] shorts @ref i2c_shorts activated
201 */
202void i2c_set_shorts(uint32_t i2c, uint32_t shorts)
203{
204 I2C_SHORTS(i2c) = shorts;
205}
206/**@}*/
#define __GPIO2PIN(x)
This is an approximation of log2.
#define PERIPH_TRIGGER_TASK(task)
#define GPIO_UNCONNECTED
Mark the signal as not connected to any pin.
#define I2C_RXD(i2c)
Definition: common/i2c.h:70
#define I2C_PSELSDA(i2c)
Definition: common/i2c.h:69
#define I2C_PSELSCL(i2c)
Definition: common/i2c.h:68
#define I2C_TASK_STARTTX(i2c)
Definition: common/i2c.h:48
#define I2C_TASK_RESUME(i2c)
Definition: common/i2c.h:51
#define I2C_TXD(i2c)
Definition: common/i2c.h:71
#define I2C_FREQUENCY(i2c)
Definition: common/i2c.h:72
#define I2C_ENABLE(i2c)
Definition: common/i2c.h:67
#define I2C_TASK_STOP(i2c)
Definition: common/i2c.h:49
#define I2C_ADDRESS(i2c)
Definition: common/i2c.h:73
#define I2C_TASK_STARTRX(i2c)
Definition: common/i2c.h:47
#define I2C_SHORTS(i2c)
Definition: common/i2c.h:62
void i2c_start_rx(uint32_t i2c)
Start I2C reception.
Definition: i2c_common.c:73
void i2c_resume(uint32_t i2c)
Resume I2C transaction.
Definition: i2c_common.c:190
void i2c_disable(uint32_t i2c)
Disable I2C peripheral.
Definition: i2c_common.c:52
void i2c_select_pins(uint32_t i2c, uint32_t scl_pin, uint32_t sda_pin)
Select GPIO pins to be used by this peripheral.
Definition: i2c_common.c:158
void i2c_set_fast_mode(uint32_t i2c)
Select Fast (400kHz) mode.
Definition: i2c_common.c:91
void i2c_enable(uint32_t i2c, uint32_t mode)
Enable I2C peripheral.
Definition: i2c_common.c:43
void i2c_set_frequency(uint32_t i2c, uint32_t freq)
Set I2C frequency.
Definition: i2c_common.c:114
void i2c_start_tx(uint32_t i2c)
Start I2C transmission.
Definition: i2c_common.c:64
void i2c_set_standard_mode(uint32_t i2c)
Select Standard (100kHz) mode.
Definition: i2c_common.c:100
void i2c_send_stop(uint32_t i2c)
Signal stop on I2C line.
Definition: i2c_common.c:82
void i2c_set_shorts(uint32_t i2c, uint32_t shorts)
Configure event -> task shortcuts.
Definition: i2c_common.c:202
void i2c_send_data(uint32_t i2c, uint8_t data)
Write Data to TXD register to be sent.
Definition: i2c_common.c:127
uint8_t i2c_get_data(uint32_t i2c)
Read Data from RXD register.
Definition: i2c_common.c:140
void i2c_set_address(uint32_t i2c, uint8_t addr)
Set 7bit I2C address of the device you wish to communicate with.
Definition: i2c_common.c:178
#define I2C_FREQUENCY_100K
100kHz
Definition: common/i2c.h:122
#define I2C_FREQUENCY_400K
400kHz
Definition: common/i2c.h:136