libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
i2c_common_v2.h
Go to the documentation of this file.
1/** @addtogroup i2c_defines
2 *
3 */
4
5/*
6 * This file is part of the libopencm3 project.
7 *
8 * This library is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22/* THIS FILE SHOULD NOT BE INCLUDED DIRECTLY, BUT ONLY VIA I2C.H
23The order of header inclusion is important. i2c.h includes the device
24specific memorymap.h header before including this header file.*/
25
26/**@{*/
27
28/** @cond */
29#ifdef LIBOPENCM3_I2C_H
30/** @endcond */
31#ifndef LIBOPENCM3_I2C_COMMON_V2_H
32#define LIBOPENCM3_I2C_COMMON_V2_H
33
34#include <stddef.h>
35#include <stdint.h>
36
37/* --- Convenience macros -------------------------------------------------- */
38
39/* I2C register base addresses (for convenience) */
40/*****************************************************************************/
41/** @defgroup i2c_reg_base I2C register base address
42 * @ingroup i2c_defines
43 * @{*/
44#define I2C1 I2C1_BASE
45#define I2C2 I2C2_BASE
46#ifdef I2C3_BASE
47#define I2C3 I2C3_BASE
48#endif
49#ifdef I2C4_BASE
50#define I2C4 I2C4_BASE
51#endif
52/**@}*/
53
54/* --- I2C registers ------------------------------------------------------- */
55
56/* Control register 1 (I2Cx_CR1) */
57#define I2C_CR1(i2c_base) MMIO32((i2c_base) + 0x00)
58#define I2C1_CR1 I2C_CR1(I2C1)
59#define I2C2_CR1 I2C_CR1(I2C2)
60
61/* Control register 2 (I2Cx_CR2) */
62#define I2C_CR2(i2c_base) MMIO32((i2c_base) + 0x04)
63#define I2C1_CR2 I2C_CR2(I2C1)
64#define I2C2_CR2 I2C_CR2(I2C2)
65
66/* Own address register 1 (I2Cx_OAR1) */
67#define I2C_OAR1(i2c_base) MMIO32((i2c_base) + 0x08)
68#define I2C1_OAR1 I2C_OAR1(I2C1)
69#define I2C2_OAR1 I2C_OAR1(I2C2)
70
71/* Own address register 2 (I2Cx_OAR2) */
72#define I2C_OAR2(i2c_base) MMIO32((i2c_base) + 0x0c)
73#define I2C1_OAR2 I2C_OAR2(I2C1)
74#define I2C2_OAR2 I2C_OAR2(I2C2)
75
76/* Timing register (I2Cx_TIMINGR) */
77#define I2C_TIMINGR(i2c_base) MMIO32((i2c_base) + 0x10)
78#define I2C1_TIMINGR I2C_TIMINGR(I2C1)
79#define I2C2_TIMINGR I2C_TIMINGR(I2C2)
80
81/* Timeout register (I2Cx_TIMEOUTR) */
82#define I2C_TIMEOUTR(i2c_base) MMIO32((i2c_base) + 0x14)
83#define I2C1_TIMEOUTR I2C_TIMEOUTR(I2C1)
84#define I2C2_TIMEOUTR I2C_TIMEOUTR(I2C2)
85
86/* Interrupt and Status register (I2Cx_ISR) */
87#define I2C_ISR(i2c_base) MMIO32((i2c_base) + 0x18)
88#define I2C1_ISR I2C_ISR(I2C1)
89#define I2C2_ISR I2C_ISR(I2C2)
90
91/* Interrupt clear register (I2Cx_ICR) */
92#define I2C_ICR(i2c_base) MMIO32((i2c_base) + 0x1C)
93#define I2C1_ICR I2C_ICR(I2C1)
94#define I2C2_ICR I2C_ICR(I2C2)
95
96/* PEC register (I2Cx_PECR) */
97#define I2C_PECR(i2c_base) MMIO32((i2c_base) + 0x20)
98#define I2C1_PECR I2C_PECR(I2C1)
99#define I2C2_PECR I2C_PECR(I2C2)
100
101/* Receive data register (I2Cx_RXDR) */
102#define I2C_RXDR(i2c_base) MMIO32((i2c_base) + 0x24)
103#define I2C1_RXDR I2C_RXDR(I2C1)
104#define I2C2_RXDR I2C_RXDR(I2C2)
105
106/* Transmit data register (I2Cx_TXDR) */
107#define I2C_TXDR(i2c_base) MMIO32((i2c_base) + 0x28)
108#define I2C1_TXDR I2C_TXDR(I2C1)
109#define I2C2_TXDR I2C_TXDR(I2C2)
110
111/* Not all parts have i2c3 */
112#ifdef I2C3_BASE
113#define I2C3_CR1 I2C_CR1(I2C3)
114#define I2C3_CR2 I2C_CR2(I2C3)
115#define I2C3_OAR1 I2C_OAR1(I2C3)
116#define I2C3_OAR2 I2C_OAR2(I2C3)
117#define I2C3_DR I2C_DR(I2C3)
118#define I2C3_SR1 I2C_SR1(I2C3)
119#define I2C3_SR2 I2C_SR2(I2C3)
120#define I2C3_CCR I2C_CCR(I2C3)
121#define I2C3_TRISE I2C_TRISE(I2C3)
122#endif
123
124/* --- I2Cx_CR1 values ----------------------------------------------------- */
125
126/* PECEN: PEC enable */
127#define I2C_CR1_PECEN (1 << 23)
128
129/* ALERTEN: SMBus alert enable */
130#define I2C_CR1_ALERTEN (1 << 22)
131
132/* SMBDEN: SMBus Device Default address enable */
133#define I2C_CR1_SMBDEN (1 << 21)
134
135/* SMBHEN: SMBus Host address enable */
136#define I2C_CR1_SMBHEN (1 << 20)
137
138/* GCEN: General call enable */
139#define I2C_CR1_GCEN (1 << 19)
140
141/* WUPEN: Wakeup from STOP enable */
142#define I2C_CR1_WUPEN (1 << 18)
143
144/* NOSTRETCH: Clock stretching disable */
145#define I2C_CR1_NOSTRETCH (1 << 17)
146
147/* SBC: Slave byte control */
148#define I2C_CR1_SBC (1 << 16)
149
150/* RXDMAEN: DMA reception requests enable */
151#define I2C_CR1_RXDMAEN (1 << 15)
152
153/* TXDMAEN: DMA transmission requests enable */
154#define I2C_CR1_TXDMAEN (1 << 14)
155
156/* ANFOFF: Analog noise filter OFF */
157#define I2C_CR1_ANFOFF (1 << 12)
158
159/** DNF[3:0]: Digital noise filter. */
160#define I2C_CR1_DNF_MASK 0xF
161#define I2C_CR1_DNF_SHIFT 8
162
163/* ERRIE: Error interrupts enable */
164#define I2C_CR1_ERRIE (1 << 7)
165
166/* TCIE: Transfer Complete interrupt enable */
167#define I2C_CR1_TCIE (1 << 6)
168
169/* STOPIE: STOP detection Interrupt enable */
170#define I2C_CR1_STOPIE (1 << 5)
171
172/* NACKIE: Not acknowledge received Interrupt enable */
173#define I2C_CR1_NACKIE (1 << 4)
174
175/* ADDRIE: Address match Interrupt enable (slave only) */
176#define I2C_CR1_ADDRIE (1 << 3)
177
178/* RXIE: RX Interrupt enable */
179#define I2C_CR1_RXIE (1 << 2)
180
181/* TXIE: TX Interrupt enable */
182#define I2C_CR1_TXIE (1 << 1)
183
184/* PE: Peripheral enable */
185#define I2C_CR1_PE (1 << 0)
186
187/* --- I2Cx_CR2 values ----------------------------------------------------- */
188
189/* PECBYTE: Packet error checking byte */
190#define I2C_CR2_PECBYTE (1 << 26)
191
192/* AUTOEND: Automatic end mode (master mode) */
193#define I2C_CR2_AUTOEND (1 << 25)
194
195/* RELOAD: NBYTES reload mode */
196#define I2C_CR2_RELOAD (1 << 24)
197
198/* NBYTES[7:0]: Number of bytes (23,16) */
199#define I2C_CR2_NBYTES_SHIFT 16
200#define I2C_CR2_NBYTES_MASK (0xFF << I2C_CR2_NBYTES_SHIFT)
201
202/* NACK: NACK generation (slave mode) */
203#define I2C_CR2_NACK (1 << 15)
204
205/* STOP: Stop generation (master mode) */
206#define I2C_CR2_STOP (1 << 14)
207
208/* START: Start generation */
209#define I2C_CR2_START (1 << 13)
210
211/* HEAD10R: 10-bit address header only read direction (master receiver mode) */
212#define I2C_CR2_HEAD10R (1 << 12)
213
214/* ADD10: 10-bit addressing mode (master mode) */
215#define I2C_CR2_ADD10 (1 << 11)
216
217/* RD_WRN: Transfer direction (master mode) */
218#define I2C_CR2_RD_WRN (1 << 10)
219
220#define I2C_CR2_SADD_7BIT_SHIFT 1
221#define I2C_CR2_SADD_10BIT_SHIFT 0
222#define I2C_CR2_SADD_7BIT_MASK (0x7F << I2C_CR2_SADD_7BIT_SHIFT)
223#define I2C_CR2_SADD_10BIT_MASK 0x3FF
224
225/* --- I2Cx_OAR1 values ---------------------------------------------------- */
226
227/* OA1EN: Own Address 1 enable */
228#define I2C_OAR1_OA1EN_DISABLE (0x0 << 15)
229#define I2C_OAR1_OA1EN_ENABLE (0x1 << 15)
230
231/* OA1MODE Own Address 1 10-bit mode */
232#define I2C_OAR1_OA1MODE (1 << 10)
233#define I2C_OAR1_OA1MODE_7BIT 0
234#define I2C_OAR1_OA1MODE_10BIT 1
235
236/* OA1[9:8]: Interface address */
237
238/* OA1[7:1]: Interface address */
239
240/* OA1[0]: Interface address */
241#define I2C_OAR1_OA1 (1 << 10)
242#define I2C_OAR1_OA1_7BIT 0
243#define I2C_OAR1_OA1_10BIT 1
244
245/* --- I2Cx_OAR2 values ---------------------------------------------------- */
246
247/* OA2EN: Own Address 2 enable */
248#define I2C_OAR2_OA2EN (1 << 15)
249
250/* OA2MSK[2:0]: Own Address 2 masks */
251#define I2C_OAR2_OA2MSK_NO_MASK (0x0 << 8)
252#define I2C_OAR2_OA2MSK_OA2_7_OA2_2 (0x1 << 8)
253#define I2C_OAR2_OA2MSK_OA2_7_OA2_3 (0x2 << 8)
254#define I2C_OAR2_OA2MSK_OA2_7_OA2_4 (0x3 << 8)
255#define I2C_OAR2_OA2MSK_OA2_7_OA2_5 (0x4 << 8)
256#define I2C_OAR2_OA2MSK_OA2_7_OA2_6 (0x5 << 8)
257#define I2C_OAR2_OA2MSK_OA2_7 (0x6 << 8)
258#define I2C_OAR2_OA2MSK_NO_CMP (0x7 << 8)
259
260/* OA2[7:1]: Interface address */
261
262/* --- I2Cx_TIMINGR values ------------------------------------------------- */
263
264/* PRESC[3:0]: Timing prescaler (31,28) */
265#define I2C_TIMINGR_PRESC_SHIFT 28
266#define I2C_TIMINGR_PRESC_MASK (0xF << 28)
267
268/* SCLDEL[3:0]: Data setup time (23,20) */
269#define I2C_TIMINGR_SCLDEL_SHIFT 20
270#define I2C_TIMINGR_SCLDEL_MASK (0xF << I2C_TIMINGR_SCLDEL_SHIFT)
271
272/* SDADEL[3:0]: Data hold time (19,16) */
273#define I2C_TIMINGR_SDADEL_SHIFT 16
274#define I2C_TIMINGR_SDADEL_MASK (0xF << I2C_TIMINGR_SDADEL_SHIFT)
275
276/* SCLH[7:0]: SCL high period (master mode) (15,8) */
277#define I2C_TIMINGR_SCLH_SHIFT 8
278#define I2C_TIMINGR_SCLH_MASK (0xFF << I2C_TIMINGR_SCLH_SHIFT)
279
280/* SCLL[7:0]: SCL low period (master mode) (7,0) */
281#define I2C_TIMINGR_SCLL_SHIFT 0
282#define I2C_TIMINGR_SCLL_MASK (0xFF << I2C_TIMINGR_SCLL_SHIFT)
283
284/* --- I2Cx_TIEMOUTR values ------------------------------------------------ */
285
286/* TEXTEN: Extended clock timeout enable */
287#define I2C_TIEMOUTR_TEXTEN (1 << 31)
288
289/* XXX: Not clear yet. */
290/* TIMEOUTB[11:0]: Bus timeout B */
291
292/* TIMOUTEN: Clock timeout enable */
293#define I2C_TIEMOUTR_TIMOUTEN (1 << 15)
294
295/* TIDLE: Idle clock timeout detection */
296#define I2C_TIEMOUTR_TIDLE_SCL_LOW (0x0 << 12)
297#define I2C_TIEMOUTR_TIDLE_SCL_SDA_HIGH (0x1 << 12)
298
299/* XXX: Not clear yet. */
300/* TIMEOUTA[11:0]: Bus Timeout A */
301
302/* --- I2Cx_ISR values ----------------------------------------------------- */
303
304/* Bits 31:24 Reserved, must be kept at reset value */
305
306/* XXX: Not clear yet. */
307/* ADDCODE[6:0]: Address match code (Slave mode) */
308
309/* DIR: Transfer direction (Slave mode) */
310#define I2C_ISR_DIR_READ (0x1 << 16)
311#define I2C_ISR_DIR_WRITE (0x0 << 16)
312
313/* BUSY: Bus busy */
314#define I2C_ISR_BUSY (1 << 15)
315
316/* ALERT: SMBus alert */
317#define I2C_ISR_ALERT (1 << 13)
318
319/* TIMEOUT: Timeout or tLOW detection flag */
320#define I2C_ISR_TIMEOUT (1 << 12)
321
322/* PECERR: PEC Error in reception */
323#define I2C_ISR_PECERR (1 << 11)
324
325/* OVR: Overrun/Underrun (slave mode) */
326#define I2C_ISR_OVR (1 << 10)
327
328/* ARLO: Arbitration lost */
329#define I2C_ISR_ARLO (1 << 9)
330
331/* BERR: Bus error */
332#define I2C_ISR_BERR (1 << 8)
333
334/* TCR: Transfer Complete Reload */
335#define I2C_ISR_TCR (1 << 7)
336
337/* TC: Transfer Complete (master mode) */
338#define I2C_ISR_TC (1 << 6)
339
340/* STOPF: Stop detection flag */
341#define I2C_ISR_STOPF (1 << 5)
342
343/* NACKF: Not Acknowledge received flag */
344#define I2C_ISR_NACKF (1 << 4)
345
346/* ADDR: Address matched (slave mode) */
347#define I2C_ISR_ADDR (1 << 3)
348
349/* RXNE: Receive data register not empty (receivers) */
350#define I2C_ISR_RXNE (1 << 2)
351
352/* TXIS: Transmit interrupt status (transmitters) */
353#define I2C_ISR_TXIS (1 << 1)
354
355/* TXE: Transmit data register empty (transmitters) */
356#define I2C_ISR_TXE (1 << 0)
357
358/* --- I2Cx_ICR values ----------------------------------------------------- */
359
360/* ALERTCF: Alert flag clear */
361#define I2C_ICR_ALERTCF (1 << 13)
362
363/* TIMOUTCF: Timeout detection flag clear */
364#define I2C_ICR_TIMOUTCF (1 << 12)
365
366/* PECCF: PEC Error flag clear */
367#define I2C_ICR_PECCF (1 << 11)
368
369/* OVRCF: Overrun/Underrun flag clear */
370#define I2C_ICR_OVRCF (1 << 10)
371
372/* ARLOCF: Arbitration Lost flag clear */
373#define I2C_ICR_ARLOCF (1 << 9)
374
375/* BERRCF: Bus error flag clear */
376#define I2C_ICR_BERRCF (1 << 8)
377
378/* STOPCF: Stop detection flag clear */
379#define I2C_ICR_STOPCF (1 << 5)
380
381/* NACKCF: Not Acknowledge flag clear */
382#define I2C_ICR_NACKCF (1 << 4)
383
384/* ADDRCF: Address Matched flag clear */
385#define I2C_ICR_ADDRCF (1 << 3)
386
387/* --- I2Cx_PECR values ---------------------------------------------------- */
388
389/* PEC[7:0] Packet error checking register */
390
391/* --- I2C function prototypes---------------------------------------------- */
392
393/**
394 * I2C speed modes.
395 */
402
404
405void i2c_peripheral_enable(uint32_t i2c);
406void i2c_peripheral_disable(uint32_t i2c);
407void i2c_send_start(uint32_t i2c);
408void i2c_send_stop(uint32_t i2c);
409void i2c_clear_stop(uint32_t i2c);
410void i2c_set_own_7bit_slave_address(uint32_t i2c, uint8_t slave);
411void i2c_set_own_10bit_slave_address(uint32_t i2c, uint16_t slave);
412void i2c_send_data(uint32_t i2c, uint8_t data);
413uint8_t i2c_get_data(uint32_t i2c);
414
415void i2c_enable_analog_filter(uint32_t i2c);
416void i2c_disable_analog_filter(uint32_t i2c);
417void i2c_set_digital_filter(uint32_t i2c, uint8_t dnf_setting);
418void i2c_set_prescaler(uint32_t i2c, uint8_t presc);
419void i2c_set_data_setup_time(uint32_t i2c, uint8_t s_time);
420void i2c_set_data_hold_time(uint32_t i2c, uint8_t h_time);
421void i2c_set_scl_high_period(uint32_t i2c, uint8_t period);
422void i2c_set_scl_low_period(uint32_t i2c, uint8_t period);
423void i2c_enable_stretching(uint32_t i2c);
424void i2c_disable_stretching(uint32_t i2c);
425void i2c_set_7bit_addr_mode(uint32_t i2c);
426void i2c_set_10bit_addr_mode(uint32_t i2c);
427void i2c_set_7bit_address(uint32_t i2c, uint8_t addr);
428void i2c_set_10bit_address(uint32_t i2c, uint16_t addr);
429void i2c_set_write_transfer_dir(uint32_t i2c);
430void i2c_set_read_transfer_dir(uint32_t i2c);
431void i2c_set_bytes_to_transfer(uint32_t i2c, uint32_t n_bytes);
432bool i2c_is_start(uint32_t i2c);
433void i2c_enable_autoend(uint32_t i2c);
434void i2c_disable_autoend(uint32_t i2c);
435bool i2c_nack(uint32_t i2c);
436bool i2c_busy(uint32_t i2c);
437bool i2c_transmit_int_status(uint32_t i2c);
438bool i2c_transfer_complete(uint32_t i2c);
439bool i2c_received_data(uint32_t i2c);
440void i2c_enable_interrupt(uint32_t i2c, uint32_t interrupt);
441void i2c_disable_interrupt(uint32_t i2c, uint32_t interrupt);
442void i2c_enable_rxdma(uint32_t i2c);
443void i2c_disable_rxdma(uint32_t i2c);
444void i2c_enable_txdma(uint32_t i2c);
445void i2c_disable_txdma(uint32_t i2c);
446void i2c_transfer7(uint32_t i2c, uint8_t addr, const uint8_t *w, size_t wn, uint8_t *r, size_t rn);
447void i2c_set_speed(uint32_t i2c, enum i2c_speeds speed, uint32_t clock_megahz);
448
450
451#endif
452/** @cond */
453#else
454#warning "i2c_common_v2.h should not be included explicitly, only via i2c.h"
455#endif
456/** @endcond */
457/**@}*/
458
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void i2c_disable_interrupt(uint32_t i2c, uint32_t interrupt)
I2C Disable Interrupt.
void i2c_set_7bit_address(uint32_t i2c, uint8_t addr)
void i2c_enable_analog_filter(uint32_t i2c)
void i2c_enable_interrupt(uint32_t i2c, uint32_t interrupt)
I2C Enable Interrupt.
void i2c_set_digital_filter(uint32_t i2c, uint8_t dnf_setting)
Set the I2C digital filter.
void i2c_set_10bit_addr_mode(uint32_t i2c)
void i2c_set_7bit_addr_mode(uint32_t i2c)
void i2c_disable_analog_filter(uint32_t i2c)
void i2c_set_10bit_address(uint32_t i2c, uint16_t addr)
void i2c_set_prescaler(uint32_t i2c, uint8_t presc)
void i2c_set_data_setup_time(uint32_t i2c, uint8_t s_time)
void i2c_disable_stretching(uint32_t i2c)
void i2c_send_start(uint32_t i2c)
I2C Send Start Condition.
Definition: i2c_common_v2.c:63
void i2c_peripheral_disable(uint32_t i2c)
I2C Peripheral Disable.
Definition: i2c_common_v2.c:48
void i2c_set_bytes_to_transfer(uint32_t i2c, uint32_t n_bytes)
bool i2c_is_start(uint32_t i2c)
void i2c_set_write_transfer_dir(uint32_t i2c)
bool i2c_received_data(uint32_t i2c)
void i2c_set_own_7bit_slave_address(uint32_t i2c, uint8_t slave)
I2C Set the 7 bit Slave Address for the Peripheral.
void i2c_set_scl_high_period(uint32_t i2c, uint8_t period)
void i2c_disable_autoend(uint32_t i2c)
void i2c_send_stop(uint32_t i2c)
I2C Send Stop Condition.
Definition: i2c_common_v2.c:77
void i2c_peripheral_enable(uint32_t i2c)
I2C Peripheral Enable.
Definition: i2c_common_v2.c:33
void i2c_enable_rxdma(uint32_t i2c)
I2C Enable reception DMA.
void i2c_set_data_hold_time(uint32_t i2c, uint8_t h_time)
i2c_speeds
I2C speed modes.
void i2c_set_own_10bit_slave_address(uint32_t i2c, uint16_t slave)
I2C Set the 10 bit Slave Address for the Peripheral.
bool i2c_transmit_int_status(uint32_t i2c)
void i2c_disable_rxdma(uint32_t i2c)
I2C Disable reception DMA.
void i2c_enable_autoend(uint32_t i2c)
void i2c_enable_stretching(uint32_t i2c)
void i2c_set_read_transfer_dir(uint32_t i2c)
void i2c_send_data(uint32_t i2c, uint8_t data)
I2C Send Data.
uint8_t i2c_get_data(uint32_t i2c)
I2C Get Data.
void i2c_clear_stop(uint32_t i2c)
I2C Clear Stop Flag.
Definition: i2c_common_v2.c:89
bool i2c_busy(uint32_t i2c)
void i2c_set_scl_low_period(uint32_t i2c, uint8_t period)
void i2c_disable_txdma(uint32_t i2c)
I2C Disable transmission DMA.
void i2c_enable_txdma(uint32_t i2c)
I2C Enable transmission DMA.
bool i2c_transfer_complete(uint32_t i2c)
void i2c_transfer7(uint32_t i2c, uint8_t addr, const uint8_t *w, size_t wn, uint8_t *r, size_t rn)
Run a write/read transaction to a given 7bit i2c address If both write & read are provided,...
bool i2c_nack(uint32_t i2c)
void i2c_set_speed(uint32_t i2c, enum i2c_speeds speed, uint32_t clock_megahz)
Set the i2c communication speed.
@ i2c_speed_fmp_1m
@ i2c_speed_unknown
@ i2c_speed_fm_400k
@ i2c_speed_sm_100k