libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
common/i2c.h
Go to the documentation of this file.
1/** @addtogroup i2c_defines
2 *
3 * @author @htmlonly &copy; @endhtmlonly 2016 Maxim Sloyko <maxims@google.com>
4 * @author @htmlonly &copy; @endhtmlonly 2021 Eduard Drusa <ventyl86 at netkosice dot sk>
5 *
6 **/
7
8/*
9 * This file is part of the libopencm3 project.
10 *
11 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
12 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29
33/**@{*/
34
35/* I2C bus */
36/** @addtogroup i2c_block I2C instances
37 * @{
38 */
39
40#define I2C0 I2C0_BASE
41#define I2C1 I2C1_BASE
42
43/**@}*/
44
45/* Tasks */
46
47#define I2C_TASK_STARTRX(i2c) MMIO32((i2c) + 0x000)
48#define I2C_TASK_STARTTX(i2c) MMIO32((i2c) + 0x008)
49#define I2C_TASK_STOP(i2c) MMIO32((i2c) + 0x014)
50#define I2C_TASK_SUSPEND(i2c) MMIO32((i2c) + 0x01c)
51#define I2C_TASK_RESUME(i2c) MMIO32((i2c) + 0x020)
52/* Events */
53
54#define I2C_EVENT_STOPPED(i2c) MMIO32((i2c) + 0x104)
55#define I2C_EVENT_RXDREADY(i2c) MMIO32((i2c) + 0x108)
56#define I2C_EVENT_TXDSENT(i2c) MMIO32((i2c) + 0x11c)
57#define I2C_EVENT_ERROR(i2c) MMIO32((i2c) + 0x124)
58#define I2C_EVENT_BB(i2c) MMIO32((i2c) + 0x138)
59#define I2C_EVENT_SUSPENDED(i2c) MMIO32((i2c) + 0x148)
60/* Registers */
61
62#define I2C_SHORTS(i2c) MMIO32((i2c) + 0x200)
63#define I2C_INTEN(i2c) MMIO32((i2c) + 0x300)
64#define I2C_INTENSET(i2c) MMIO32((i2c) + 0x304)
65#define I2C_INTENCLR(i2c) MMIO32((i2c) + 0x308)
66#define I2C_ERRORSRC(i2c) MMIO32((i2c) + 0x4c4)
67#define I2C_ENABLE(i2c) MMIO32((i2c) + 0x500)
68#define I2C_PSELSCL(i2c) MMIO32((i2c) + 0x508)
69#define I2C_PSELSDA(i2c) MMIO32((i2c) + 0x50c)
70#define I2C_RXD(i2c) MMIO32((i2c) + 0x518)
71#define I2C_TXD(i2c) MMIO32((i2c) + 0x51c)
72#define I2C_FREQUENCY(i2c) MMIO32((i2c) + 0x524)
73#define I2C_ADDRESS(i2c) MMIO32((i2c) + 0x588)
74
75/* Register Contents */
76
77/** @addtogroup i2c_shorts I2C event -> task shortcuts
78 * The effect of activated shortcut is, that upon I2C event
79 * triggering, the hardware will automatically start chosen
80 * task without intervention of the software.
81 * @{
82 */
83
84/** On byte boundary, activate suspend task. */
85#define I2C_SHORTS_BB_SUSPEND (1 << 0)
86
87/** On byte boundary, activate stop task. */
88#define I2C_SHORTS_BB_STOP (1 << 1)
89
90/**@}*/
91
92/** @addtogroup i2c_interrupts I2C interrupts
93 * @{
94 */
95#define I2C_INTEN_STOPPED (1 << 1)
96#define I2C_INTEN_RXDREADY (1 << 2)
97#define I2C_INTEN_TXDSENT (1 << 7)
98#define I2C_INTEN_ERROR (1 << 9)
99#define I2C_INTEN_BB (1 << 14)
100
101/**@}*/
102
103#define I2C_ERRORSRC_OVERRUN (1 << 0)
104#define I2C_ERRORSRC_ANACK (1 << 1)
105#define I2C_ERRORSRC_DNACK (1 << 2)
106
107/** @addtogroup i2c_mode I2C peripheral mode
108 * @{
109 */
110/** NRF51 legacy mode.
111 * On NRF51, this is the only mode available.
112 * On NRF52, this mode does not support EasyDMA.
113 */
114#define I2C_MODE_LEGACY (5)
115/**@}*/
116
117/** @addtogroup i2c_freq_const I2C frequency constants
118 * @{
119 */
120
121/** 100kHz */
122#define I2C_FREQUENCY_100K (0x01980000)
123/** 250kHz */
124#define I2C_FREQUENCY_250K (0x04000000)
125/** 390kHz
126 * @note: This value is not documented in datasheet. It provides
127 * ~390kHz clock with correct timing.
128 */
129#define I2C_FREQUENCY_390K (0x06200000)
130/** 400kHz
131 * @note: According to datasheet, there is HW bug which prevents
132 * MCU from generating correct timings, therefore it might be
133 * unusable. Use @ref I2C_FREQUENCY_390K instead, if this affects
134 * you.
135 */
136#define I2C_FREQUENCY_400K (0x06680000)
137/**@}*/
138
139#define I2C_PSEL_OFF (0xffffffff)
140
141/**@}*/
142
144
145void i2c_enable(uint32_t i2c, uint32_t mode);
146void i2c_disable(uint32_t i2c);
147void i2c_start_tx(uint32_t i2c);
148void i2c_start_rx(uint32_t i2c);
149void i2c_send_stop(uint32_t i2c);
150void i2c_set_fast_mode(uint32_t i2c);
151void i2c_set_standard_mode(uint32_t i2c);
152void i2c_set_frequency(uint32_t i2c, uint32_t freq);
153void i2c_send_data(uint32_t i2c, uint8_t data);
154uint8_t i2c_get_data(uint32_t i2c);
155void i2c_select_pins(uint32_t i2c, uint32_t scl_pin, uint32_t sda_pin);
156void i2c_set_address(uint32_t i2c, uint8_t addr);
157void i2c_resume(uint32_t i2c);
158void i2c_set_shorts(uint32_t i2c, uint32_t shorts);
159
161
162
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
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