Trident IoT SDK
Loading...
Searching...
No Matches
T32CZ20_i2c.h
Go to the documentation of this file.
1
28
29
30#ifndef T32CZ20_I2C_H_
31#define T32CZ20_I2C_H_
32
33#include "tr_hal_platform.h"
34#include "tr_hal_gpio.h"
35
36
37// ******************************************************************
38// defines used by the I2C module
39// ******************************************************************
40
41
43typedef enum
44{
47
49
50#define TR_HAL_MAX_I2C_CONTROLLER_ID 1
51#define NUM_I2C_CONTROLLER 2
52
53
54// default I2C pins - these can be any of the available pins
55// these have been picked since they are the pins used for I2C
56// on the CM11 (which has a much stricter pin usage)
57#define DEFAULT_I2C_SCL0_PIN 22
58#define DEFAULT_I2C_SDA0_PIN 23
59
60#define DEFAULT_I2C_SCL1_PIN 20
61#define DEFAULT_I2C_SDA1_PIN 21
62
63
64// transmit FIFO is 9x16
65#define I2C_TX_FIFO_BYTES 18
66
67// receive FIFO is 8x16
68#define I2C_RX_FIFO_BYTES 16
69
70
79#ifdef I2C_MASTER0_SECURE_EN
80 #define CHIP_MEMORY_MAP_I2C_CONTROLLER0_BASE (0x5002B000UL)
81#else
82 #define CHIP_MEMORY_MAP_I2C_CONTROLLER0_BASE (0x4002B000UL)
83#endif // I2C_MASTER0_SECURE_EN
84
85#ifdef I2C_MASTER1_SECURE_EN
86 #define CHIP_MEMORY_MAP_I2C_CONTROLLER1_BASE (0x5002C000UL)
87#else
88 #define CHIP_MEMORY_MAP_I2C_CONTROLLER1_BASE (0x4002C000UL)
89#endif // I2C_MASTER1_SECURE_EN
90
91
92
97typedef struct
98{
99 // control: enable, restart, stop, bus clear, FIFO clear
100 __IO uint32_t control; // 0x00
101
102 // target address
103 __IO uint32_t target; // 0x04
104
105 // receive buffer or transmit buffer
106 __IO uint32_t buffer; // 0x08
107
108 // interrupts
109 __IO uint32_t interrupt_status; // 0x0C
110 __IO uint32_t interrupt_enable; // 0x10
111 __IO uint32_t interrupt_raw_status; // 0x14
112 __IO uint32_t interrupt_clear; // 0x18
113
114 // setting up the clock
115 __IO uint32_t clock_divider; // 0x1C
116
118
119
120// *****************************************************************
121// these defines help when dealing with the CONTROL register (0x00)
122
123// bit 0 enables or disables the device for I2C
124#define I2C_CONTROL_ENABLE_CONTROLLER 0x01
125#define I2C_CONTROL_DISABLE_CONTROLLER 0x00
126// bit 1 enables restart
127#define I2C_CONTROL_ENABLE_RESTART 0x02
128// bit 2 stops the current transaction
129#define I2C_CONTROL_STOP_TRANSACTION 0x04
130// bit 3 clears the bus by sending 9 clock pulses
131#define I2C_CONTROL_BUS_CLEAR 0x08
132// bit 4 clears the TX and RX FIFOs
133#define I2C_CONTROL_FIFO_CLEAR 0x10
134
135
136// *****************************************************************
137// these defines help when dealing with the BUFFER register (0x04)
138
139// when receiving data set the register to this
140#define I2C_BUFFER_SET_FOR_READ 0x100
141// when sending data, the bit gets set to 0, plus the data
142#define I2C_BUFFER_SET_FOR_WRITE 0x000
143
144// *****************************************************************
145// these defines help when dealing with the INTERRUPT registers (0x0C, 0x10, 0x14, 0x18)
146
147#define I2C_INTERRUPT_RX_UNDER 0x01
148#define I2C_INTERRUPT_RX_OVER 0x02
149#define I2C_INTERRUPT_RX_FULL 0x04
150#define I2C_INTERRUPT_RX_FINISH 0x08
151#define I2C_INTERRUPT_TX_OVER 0x10
152#define I2C_INTERRUPT_TX_EMPTY 0x20
153#define I2C_INTERRUPT_ABORT_A_NACK 0x40
154#define I2C_INTERRUPT_ABORT_W_NACK 0x80
155#define I2C_INTERRUPT_ABORT_LOST_ARB 0x100
156#define I2C_INTERRUPT_IDLE_STATE 0x200
157
158#define I2C_INTERRUPT_ALL 0x1FF
159
160
161// *****************************************************************
162// helper enums for CLOCK DIVIDER REGISTER (0x1C)
163//
164// I2C system clock frequency is determined by:
165// 32MHz clock / (clock_divider register + 1)
166//
167// although not all I2C sensors can do 1 MHz, we allow 1 MHz,
168// but use 400 KHz as a default since almost all sensors will
169// support this
170//
171// value 0 = 0+1 = 1 --> is 32 Mhz / 1 = 32 MHz
172// --------------------------------------------------
173// value 31 = 31+1 = 32 --> is 32 Mhz / 32 = 1000 KHz = 1 MHz
174// value 79 = 79+1 = 80 --> is 32 Mhz / 80 = 400 KHz
175// value 159 = 159+1 = 160 --> is 32 Mhz / 160 = 200 KHz
176// value 319 = 319+1 = 320 --> is 32 Mhz / 320 = 100 KHz
177// value 639 = 639+1 = 640 --> is 32 Mhz / 640 = 50 KHz
178// value 3199 = 3199+1 = 3200 --> is 32 Mhz / 3200 = 10 KHz
189
190
191// *****************************************************************
192// this orients the 2 structs (for 2 I2C controllers) with the
193// correct addresses, so referencing a field will now read/write
194// the correct chip address
195// *****************************************************************
196#define I2C0_CHIP_REGISTERS ((I2C_REGISTERS_T *) CHIP_MEMORY_MAP_I2C_CONTROLLER0_BASE)
197#define I2C1_CHIP_REGISTERS ((I2C_REGISTERS_T *) CHIP_MEMORY_MAP_I2C_CONTROLLER1_BASE)
198
199
206
207// prototype for callback from the Trident HAL to the app when a byte is received
208typedef void (*tr_hal_i2c_receive_callback_t) (uint8_t received_byte);
209
210// prototype for callback from the Trident HAL to the app when an event happens
211typedef void (*tr_hal_i2c_event_callback_t) (tr_hal_i2c_id_t i2c_id, uint32_t event_bitmask);
212
213
217#define I2C_EVENT_RX_UNDER 0x01
218#define I2C_EVENT_RX_OVER 0x02
219#define I2C_EVENT_RX_FULL 0x04
220#define I2C_EVENT_RX_FINISH 0x08
221#define I2C_EVENT_TX_OVER 0x10
222#define I2C_EVENT_TX_EMPTY 0x20
223#define I2C_EVENT_ABORT_A_NACK 0x40
224#define I2C_EVENT_ABORT_W_NACK 0x80
225#define I2C_EVENT_ABORT_LOST_ARB 0x100
226
227
232typedef struct
233{
234 // this struct is for I2C Controllers ONLY
235
236 // SDA and SCL pin
237 tr_hal_gpio_pin_t sda_pin;
238 tr_hal_gpio_pin_t scl_pin;
239
240 // clock setting
241 tr_hal_i2c_clock_rate_t clock_setting;
242
243 // callback from HAL to App when a byte is received
244 // if the app doesn't want this, then set it to NULL
245 tr_hal_i2c_receive_callback_t rx_handler_function;
246
247 // callback from HAL to App when an event happens
248 // if the app doesn't want this, then set it to NULL
249 tr_hal_i2c_event_callback_t event_handler_fx;
250
251 // are the chip interrupts enabled
252 bool enable_chip_interrupts;
253
254 // set the interrupt priority
255 tr_hal_int_pri_t interrupt_priority;
256
257 // when the I2C is powered off we can choose to DISABLE interrupts, meaning
258 // we will STAY powered off even when events are happening, or we can choose
259 // to KEEP interrupts enabled when powered off. This means we would wake on
260 // interrupt and power the UART back on
261 bool wake_on_interrupt;
262
264
265
270#define I2C_CONFIG_CONTROLLER0 \
271 { \
272 .sda_pin = (tr_hal_gpio_pin_t) { DEFAULT_I2C_SDA0_PIN },\
273 .scl_pin = (tr_hal_gpio_pin_t) { DEFAULT_I2C_SCL0_PIN },\
274 .clock_setting = I2C_CLOCK_100_KHZ, \
275 .rx_handler_function = NULL, \
276 .event_handler_fx = NULL, \
277 .enable_chip_interrupts = true, \
278 .interrupt_priority = TR_HAL_INTERRUPT_PRIORITY_5, \
279 .wake_on_interrupt = false, \
280 }
281
282#define I2C_CONFIG_CONTROLLER1 \
283 { \
284 .sda_pin = (tr_hal_gpio_pin_t) { DEFAULT_I2C_SDA1_PIN },\
285 .scl_pin = (tr_hal_gpio_pin_t) { DEFAULT_I2C_SCL1_PIN },\
286 .clock_setting = I2C_CLOCK_100_KHZ, \
287 .rx_handler_function = NULL, \
288 .event_handler_fx = NULL, \
289 .enable_chip_interrupts = true, \
290 .interrupt_priority = TR_HAL_INTERRUPT_PRIORITY_5, \
291 .wake_on_interrupt = false, \
292 }
293
294
298typedef struct
299{
303 uint32_t count_rx_finish;
305 uint32_t count_tx_empty;
308 uint32_t count_abort_lost_arb;
309 uint32_t count_idle;
310
311 // debug
318
320
321
322#endif // T32CZ20_I2C_H_
323
324
tr_hal_int_pri_t
values and a range checking function for setting the interrupt priority in the Trident HAL APIs
Definition tr_hal_platform.h:34
void(* tr_hal_i2c_event_callback_t)(tr_hal_i2c_id_t i2c_id, uint32_t event_bitmask)
Definition T32CM11_i2c.h:232
tr_hal_i2c_clock_rate_t
Definition T32CM11_i2c.h:201
@ I2C_CLOCK_1_MHZ
Definition T32CM11_i2c.h:202
@ I2C_CLOCK_200_KHZ
Definition T32CM11_i2c.h:205
@ I2C_CLOCK_400_KHZ
Definition T32CM11_i2c.h:204
@ I2C_CLOCK_100_KHZ
Definition T32CM11_i2c.h:206
void(* tr_hal_i2c_receive_callback_t)(uint8_t received_byte)
Definition T32CM11_i2c.h:229
tr_hal_i2c_id_t
On the T32CM11 there is just 1 I2C available to act as a Controller.
Definition T32CM11_i2c.h:43
@ I2C_CTRL_0_ID
Definition T32CM11_i2c.h:44
This file contains the CHIP SPECIFIC types and defines for the T32CZ20.
I2C_REGISTERS_T * tr_hal_i2c_get_controller_register_address(tr_hal_i2c_id_t i2c_id)
@ I2C_CLOCK_10_KHZ
Definition T32CZ20_i2c.h:186
@ I2C_CLOCK_50_KHZ
Definition T32CZ20_i2c.h:185
@ I2C_CTRL_1_ID
Definition T32CZ20_i2c.h:46
Definition T32CM11_i2c.h:87
__IO uint32_t buffer
Definition T32CZ20_i2c.h:106
__IO uint32_t interrupt_raw_status
Definition T32CZ20_i2c.h:111
__IO uint32_t target
Definition T32CZ20_i2c.h:103
__IO uint32_t interrupt_status
Definition T32CM11_i2c.h:101
__IO uint32_t interrupt_clear
Definition T32CZ20_i2c.h:112
__IO uint32_t clock_divider
Definition T32CZ20_i2c.h:115
__IO uint32_t interrupt_enable
Definition T32CM11_i2c.h:98
pin type
Definition tr_hal_platform.h:23
Definition T32CM11_i2c.h:304
uint32_t write_exit_on_flag
Definition T32CZ20_i2c.h:313
uint32_t count_idle
Definition T32CZ20_i2c.h:309
uint32_t write_exit_on_crazy
Definition T32CZ20_i2c.h:314
uint32_t count_abort_a_nack
Definition T32CZ20_i2c.h:306
uint32_t read_exit_on_crazy
Definition T32CZ20_i2c.h:317
uint32_t count_tx_empty
Definition T32CM11_i2c.h:307
uint32_t count_abort_lost_arb
Definition T32CM11_i2c.h:308
uint32_t count_rx_over
Definition T32CZ20_i2c.h:301
uint32_t count_abort_w_nack
Definition T32CZ20_i2c.h:307
uint32_t read_exit_on_int_status
Definition T32CZ20_i2c.h:315
uint32_t count_tx_over
Definition T32CZ20_i2c.h:304
uint32_t count_rx_full
Definition T32CZ20_i2c.h:302
uint32_t read_exit_on_flag
Definition T32CZ20_i2c.h:316
uint32_t count_rx_under
Definition T32CZ20_i2c.h:300
uint32_t write_exit_on_int_status
Definition T32CZ20_i2c.h:312
uint32_t count_rx_finish
Definition T32CM11_i2c.h:306
Definition T32CM11_i2c.h:250
This is the common include file for the Trident HAL GPIO Driver.