APM:Libraries
i2c.h
Go to the documentation of this file.
1 #ifndef _I2C_H
2 #define _I2C_H
3 
4 #include <hal_types.h>
5 #include <hal.h>
6 #include "dma.h"
7 
8 #define I2C_50KHz_SPEED 50000
9 #define I2C_75KHz_SPEED 75000
10 #define I2C_100KHz_SPEED 100000
11 #define I2C_250KHz_SPEED 250000
12 #define I2C_400KHz_SPEED 400000
13 
14 
15 #define I2C_BIT_MASK ((uint32_t)0x00FFFFFF)
16 
17 // SR1 bits
18 #define I2C_BIT_SMBALERT ((uint32_t)0x0008000)
19 #define I2C_BIT_TIMEOUT ((uint32_t)0x4000)
20 #define I2C_BIT_PECERR ((uint32_t)0x1000)
21 #define I2C_BIT_OVR ((uint32_t)0x0800)
22 #define I2C_BIT_AF ((uint32_t)0x0400)
23 #define I2C_BIT_ARLO ((uint32_t)0x0200)
24 #define I2C_BIT_BERR ((uint32_t)0x0100)
25 #define I2C_BIT_TXE ((uint32_t)0x0080)
26 #define I2C_BIT_RXNE ((uint32_t)0x0040)
27 #define I2C_BIT_STOPF ((uint32_t)0x0010)
28 #define I2C_BIT_ADD10 ((uint32_t)0x0008)
29 #define I2C_BIT_BTF ((uint32_t)0x0004)
30 #define I2C_BIT_ADDR ((uint32_t)0x0002)
31 #define I2C_BIT_SB ((uint32_t)0x0001)
32 
33 // interrupt enable bits
34 #define I2C_IE_BUF ((uint16_t)0x0400)
35 #define I2C_IE_EVT ((uint16_t)0x0200)
36 #define I2C_IE_ERR ((uint16_t)0x0100)
37 
38 //SR2 bits
39 #define I2C_BIT_DUALF ((uint32_t)0x0080)
40 #define I2C_BIT_SMBHOST ((uint32_t)0x0040)
41 #define I2C_BIT_SMBDEFAULT ((uint32_t)0x0020)
42 #define I2C_BIT_GENCALL ((uint32_t)0x0010)
43 #define I2C_BIT_TRA ((uint32_t)0x0004)
44 #define I2C_BIT_BUSY ((uint32_t)0x0002)
45 #define I2C_BIT_MSL ((uint32_t)0x0001)
46 
47 
48 #define I2C_NACKPosition_Next ((uint16_t)0x0800)
49 #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
50 
51 #define I2C_Direction_Transmitter ((uint8_t)0x00)
52 #define I2C_Direction_Receiver ((uint8_t)0x01)
53 
54 /* Maximum Timeout values for events waiting loops */
55 
56 #undef I2C_TIMEOUT
57 #define I2C_TIMEOUT (300)// in uS - wait for byte transfer: 10us per bit (100kHz) * 9 bits
58 #define I2C_SMALL_TIMEOUT (50) // in uS - wait for bit
59 
60 
61 #define I2C_OK 0
62 #define I2C_NO_DEVICE 1
63 #define I2C_ERROR 2
64 #define I2C_BUS_BUSY 2
65 #define I2C_ERR_WRITE 6
66 #define I2C_NO_REGISTER 8 // 8 Acknolege Failed - not "no device"! this happens when we try to read non-existent register from chip
67 #define I2C_BUS_ERR 99
68 #define I2C_ERR_STOP 98
69 #define I2C_STOP_BERR 97
70 #define I2C_STOP_BUSY 96
71 #define I2C_ERR_TIMEOUT 95
72 #define I2C_ERR_REGISTER 94
73 #define I2C_ERR_OVERRUN 93
74 #define I2C_ERR_STOP_TIMEOUT 92
75 #define I2C_DMA_BUSY 103
76 #define I2C_PENDING 255
77 #define I2C_DMA_ERROR 100
78 
79 
80 #define DMA_BUFSIZE 8 // we read just 6 bytes from compass
81 
82 typedef struct I2C_DMA {
83  uint32_t channel;
86 } I2C_dma;
87 
88 
89 typedef struct I2c_state {
91  volatile bool busy;
92 } i2c_state;
93 
94 
95 extern uint32_t i2c_bit_time;
96 
100 typedef struct i2c_dev {
101  I2C_TypeDef* I2Cx;
102  uint8_t sda_pin;
103  uint8_t scl_pin;
104  uint32_t clk;
105  uint8_t gpio_af;
106  IRQn_Type ev_nvic_line; /* Event IRQ number */
107  IRQn_Type er_nvic_line; /* Error IRQ number */
108 // I2C_dma dma;
110 } i2c_dev;
111 
112 #ifdef __cplusplus
113  extern "C" {
114 #endif
115 
116 
117 void i2c_init(const i2c_dev *dev, uint16_t address, uint32_t speed);
118 void i2c_deinit(const i2c_dev *dev);
119 
120 uint32_t i2c_write(const i2c_dev *dev, uint8_t addr, const uint8_t *tx_buff, uint8_t txlen);
121 uint32_t i2c_read (const i2c_dev *dev, uint8_t addr, const uint8_t *tx_buff, uint8_t txlen, uint8_t *rx_buff, uint8_t rxlen);
122 
123 void i2c_lowLevel_deinit(const i2c_dev *dev);
124 
125 void i2c_master_release_bus(const i2c_dev *dev);
126 bool i2c_bus_reset(const i2c_dev *dev);
127 
128 static inline void i2c_set_isr_handler(const i2c_dev *dev, Handler h){
129  IRQn_Type irq;
130  dev->state->handler = h;
131 
132  irq=dev->er_nvic_line;
133  enable_nvic_irq(irq, I2C_INT_PRIORITY); // 8 bits * 4uS = 32uS max reaction time
134 
135  irq=dev->ev_nvic_line;
136 
138 }
139 
140 static inline void i2c_clear_isr_handler(const i2c_dev *dev){
141  dev->state->handler=0;
142 }
143 
144 
145 static inline void i2c_send_address(const i2c_dev *dev, uint8_t address, uint8_t direction)
146 {
147  /* Test on the direction to set/reset the read/write bit */
148  if (direction != I2C_Direction_Transmitter) {
149  dev->I2Cx->DR = address | I2C_OAR1_ADD0; /* Set the address bit0 for read */
150  } else {
151  dev->I2Cx->DR = address & (uint8_t)~((uint8_t)I2C_OAR1_ADD0); /* Reset the address bit0 for write */
152  }
153 }
154 
155 #ifdef I2C_DEBUG
156 uint32_t i2c_get_operation_time(uint8_t *psr1);
157 #endif
158 
159 extern const i2c_dev* const _I2C1;
160 extern const i2c_dev* const _I2C2;
161 extern const i2c_dev* const _I2C3;
162 
163 #ifdef __cplusplus
164  }
165 #endif
166 
167 
168 #endif
uint32_t channel
Definition: i2c.h:83
IRQn_Type ev_nvic_line
Definition: i2c.h:106
Handler handler
Definition: i2c.h:90
I2C device type.
Definition: i2c.h:100
uint32_t i2c_bit_time
Definition: i2c.c:80
static void i2c_set_isr_handler(const i2c_dev *dev, Handler h)
Definition: i2c.h:128
#define I2C_INT_PRIORITY
Definition: Config.h:41
const i2c_dev *const _I2C2
uint32_t i2c_write(const i2c_dev *dev, uint8_t addr, const uint8_t *tx_buff, uint8_t txlen)
dma_stream stream_rx
Definition: i2c.h:84
i2c_state * state
Definition: i2c.h:109
uint8_t gpio_af
Definition: i2c.h:105
void i2c_master_release_bus(const i2c_dev *dev)
Definition: i2c.c:248
static AP_HAL::OwnPtr< AP_HAL::Device > dev
Definition: ICM20789.cpp:16
void enable_nvic_irq(uint8_t irq, uint8_t prio)
Definition: nvic.c:8
IRQn_Type er_nvic_line
Definition: i2c.h:107
struct i2c_dev i2c_dev
I2C device type.
static void i2c_send_address(const i2c_dev *dev, uint8_t address, uint8_t direction)
Definition: i2c.h:145
uint32_t clk
Definition: i2c.h:104
struct I2C_DMA I2C_dma
void i2c_deinit(const i2c_dev *dev)
DeInitializes peripherals used by the I2C driver.
Definition: i2c.c:199
uint8_t sda_pin
Definition: i2c.h:102
enum Dma_stream dma_stream
const i2c_dev *const _I2C1
bool i2c_bus_reset(const i2c_dev *dev)
Definition: i2c.c:271
void i2c_init(const i2c_dev *dev, uint16_t address, uint32_t speed)
Definition: i2c.c:166
static void i2c_clear_isr_handler(const i2c_dev *dev)
Definition: i2c.h:140
Definition: i2c.h:89
dma_stream stream_tx
Definition: i2c.h:85
Definition: i2c.h:82
uint8_t scl_pin
Definition: i2c.h:103
#define I2C_Direction_Transmitter
Definition: i2c.h:51
uint64_t Handler
Definition: hal_types.h:19
struct I2c_state i2c_state
uint32_t i2c_read(const i2c_dev *dev, uint8_t addr, const uint8_t *tx_buff, uint8_t txlen, uint8_t *rx_buff, uint8_t rxlen)
I2C_TypeDef * I2Cx
Definition: i2c.h:101
void i2c_lowLevel_deinit(const i2c_dev *dev)
DeInitializes peripherals used by the I2C driver.
Definition: i2c.c:87
volatile bool busy
Definition: i2c.h:91
const i2c_dev *const _I2C3