SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
sys-i2c.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0+ */
2
3#ifndef __SYS_I2C_H__
4#define __SYS_I2C_H__
5
6#include <io.h>
7#include <stdarg.h>
8#include <stdbool.h>
9#include <stddef.h>
10#include <stdint.h>
11#include <types.h>
12
13#include <sys-clk.h>
14#include <sys-gpio.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif// __cplusplus
19
31
47
53enum {
56};
57
64enum {
74};
75
83 volatile uint32_t addr;
84 volatile uint32_t xaddr;
85 volatile uint32_t data;
86 volatile uint32_t ctl;
87 volatile uint32_t status;
88 volatile uint32_t clk;
89 volatile uint32_t srst;
90 volatile uint32_t eft;
91 volatile uint32_t lcr;
92 volatile uint32_t dvfs;
93};
94
95
96/* TWI extend address register */
97/* 7:0bits for extend slave address */
98#define TWI_XADDR_MASK (0xff)
99/* 31:8bits reserved */
100
101/* TWI Data register default is 0x0000_0000 */
102/* 7:0bits for send or received */
103#define TWI_DATA_MASK (0xff)
104
105/* TWI Control Register Bit Fields & Masks, default value: 0x0000_0000*/
106/* 1:0 bits reserved */
107/* set 1 to send A_ACK,then low level on SDA */
108#define TWI_CTL_ACK (0x1 << 2)
109/* INT_FLAG,interrupt status flag: set '1' when interrupt coming */
110#define TWI_CTL_INTFLG (0x1 << 3)
111#define TWI_CTL_STP (0x1 << 4) /* M_STP,Automatic clear 0 */
112#define TWI_CTL_STA (0x1 << 5) /* M_STA,atutomatic clear 0 */
113/* BUS_EN, master mode should be set 1.*/
114#define TWI_CTL_BUSEN (0x1 << 6)
115#define TWI_CTL_INTEN (0x1 << 7) /* INT_EN */
116/* 31:8 bit reserved */
117
118/* TWI Clock Register Bit Fields & Masks,default value:0x0000_0000 */
119/*
120 * Fin is APB CLOCK INPUT;
121 * Fsample = F0 = Fin/2^CLK_N;
122 * F1 = F0/(CLK_M+1);
123 *
124 * Foscl = F1/10 = Fin/(2^CLK_N * (CLK_M+1)*10);
125 * Foscl is clock SCL;standard mode:100KHz or fast mode:400KHz
126 */
127#define TWI_CLK_DUTY_30_EN (0x1 << 8) /* 8bit */
128#define TWI_CLK_DUTY (0x1 << 7) /* 7bit */
129#define TWI_CLK_DIV_M (0xf << 3) /* 6:3bit */
130#define TWI_CLK_DIV_N (0x7 << 0) /* 2:0bit */
131#define TWI_LCR_WMASK (TWI_CTL_STA | TWI_CTL_STP | TWI_CTL_INTFLG)
132
133/* CCU */
134#define TWI_DEFAULT_CLK_RST_OFFSET(x) (x + 16)
135#define TWI_DEFAULT_CLK_GATE_OFFSET(x) (x)
136
137void sunxi_i2c_init(sunxi_i2c_t *i2c_dev);
138
148int sunxi_i2c_write(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t data);
149
159int sunxi_i2c_read(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t *data);
160
161/* status or interrupt source */
162/*------------------------------------------------------------------------------
163 * Code Status
164 * 00h Bus error
165 * 08h START condition transmitted
166 * 10h Repeated START condition transmitted
167 * 18h Address + Write bit transmitted, ACK received
168 * 20h Address + Write bit transmitted, ACK not received
169 * 28h Data byte transmitted in master mode, ACK received
170 * 30h Data byte transmitted in master mode, ACK not received
171 * 38h Arbitration lost in address or data byte
172 * 40h Address + Read bit transmitted, ACK received
173 * 48h Address + Read bit transmitted, ACK not received
174 * 50h Data byte received in master mode, ACK transmitted
175 * 58h Data byte received in master mode, not ACK transmitted
176 * 60h Slave address + Write bit received, ACK transmitted
177 * 68h Arbitration lost in address as master, slave address + Write bit received, ACK transmitted
178 * 70h General Call address received, ACK transmitted
179 * 78h Arbitration lost in address as master, General Call address received, ACK transmitted
180 * 80h Data byte received after slave address received, ACK transmitted
181 * 88h Data byte received after slave address received, not ACK transmitted
182 * 90h Data byte received after General Call received, ACK transmitted
183 * 98h Data byte received after General Call received, not ACK transmitted
184 * A0h STOP or repeated START condition received in slave mode
185 * A8h Slave address + Read bit received, ACK transmitted
186 * B0h Arbitration lost in address as master, slave address + Read bit received, ACK transmitted
187 * B8h Data byte transmitted in slave mode, ACK received
188 * C0h Data byte transmitted in slave mode, ACK not received
189 * C8h Last byte transmitted in slave mode, ACK received
190 * D0h Second Address byte + Write bit transmitted, ACK received
191 * D8h Second Address byte + Write bit transmitted, ACK not received
192 * F8h No relevant status information or no interrupt
193 *-----------------------------------------------------------------------------*/
194
195#ifdef __cplusplus
196}
197#endif// __cplusplus
198
199#endif// __SYS_I2C_H__
u32_t uint32_t
Definition stdint.h:13
u8_t uint8_t
Definition stdint.h:7
Definition sys-gpio.h:68
Definition sys-clk.h:17
Structure representing the GPIO configuration for I2C.
Definition sys-i2c.h:27
gpio_mux_t gpio_sda
GPIO configuration for the SDA line.
Definition sys-i2c.h:29
gpio_mux_t gpio_scl
GPIO configuration for the SCL line.
Definition sys-i2c.h:28
Structure representing an I2C device configuration.
Definition sys-i2c.h:39
uint32_t base
Base address of the I2C hardware registers.
Definition sys-i2c.h:40
uint8_t id
ID of the I2C device.
Definition sys-i2c.h:41
uint32_t speed
Desired I2C speed (in Hz).
Definition sys-i2c.h:42
bool status
Operational status of the I2C device.
Definition sys-i2c.h:45
sunxi_clk_t i2c_clk
Clock configuration for the I2C device.
Definition sys-i2c.h:44
sunxi_i2c_gpio_t gpio
GPIO configuration for the I2C lines.
Definition sys-i2c.h:43
Structure representing the registers of the Sunxi TWI (Two Wire Interface).
Definition sys-i2c.h:82
volatile uint32_t addr
Slave address register.
Definition sys-i2c.h:83
volatile uint32_t status
Status register for monitoring the I2C state.
Definition sys-i2c.h:87
volatile uint32_t srst
Soft reset register for resetting the I2C controller.
Definition sys-i2c.h:89
volatile uint32_t clk
Clock configuration register.
Definition sys-i2c.h:88
volatile uint32_t lcr
Line control register for managing line states.
Definition sys-i2c.h:91
volatile uint32_t dvfs
Dynamic Voltage and Frequency Scaling control register.
Definition sys-i2c.h:92
volatile uint32_t ctl
Control register for managing I2C operations.
Definition sys-i2c.h:86
volatile uint32_t xaddr
Extended address register.
Definition sys-i2c.h:84
volatile uint32_t eft
Enhanced future technology control register.
Definition sys-i2c.h:90
volatile uint32_t data
Data register for sending and receiving data.
Definition sys-i2c.h:85
@ SUNXI_I2C1
I2C device 1.
Definition sys-i2c.h:66
@ SUNXI_I2C0
I2C device 0.
Definition sys-i2c.h:65
@ SUNXI_I2C2
I2C device 2.
Definition sys-i2c.h:67
@ SUNXI_R_I2C1
Reserved I2C device 1.
Definition sys-i2c.h:72
@ SUNXI_I2C3
I2C device 3.
Definition sys-i2c.h:68
@ SUNXI_I2C4
I2C device 4.
Definition sys-i2c.h:69
@ SUNXI_R_I2C0
Reserved I2C device 0.
Definition sys-i2c.h:71
@ SUNXI_I2C5
I2C device 5.
Definition sys-i2c.h:70
@ SUNXI_I2C_BUS_MAX
Maximum number of I2C buses.
Definition sys-i2c.h:73
int sunxi_i2c_read(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t *data)
sunxi_i2c read function
Definition sys-i2c.c:584
void sunxi_i2c_init(sunxi_i2c_t *i2c_dev)
Initialize I2C controller and bus.
Definition sys-i2c.c:762
@ SUNXI_I2C_SPEED_400K
400 kHz I2C speed.
Definition sys-i2c.h:55
@ SUNXI_I2C_SPEED_100K
100 kHz I2C speed.
Definition sys-i2c.h:54
int sunxi_i2c_write(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t data)
sunxi_i2c write function
Definition sys-i2c.c:568