Trident IoT SDK
Loading...
Searching...
No Matches
T32CM11_i2c.h
Go to the documentation of this file.
1
27
28
29#ifndef T32CM11_I2C_H_
30#define T32CM11_I2C_H_
31
32#include "tr_hal_platform.h"
33#include "tr_hal_gpio.h"
34
35
36// ******************************************************************
37// defines used by the I2C module
38// ******************************************************************
39
40
42typedef enum
43{
45
47
48#define TR_HAL_MAX_I2C_CONTROLLER_ID 0
49#define NUM_I2C_CONTROLLER 1
50
51
52// where to put the I2C pins
53#define I2C_SCL_PIN_OPTION1 20
54#define I2C_SDA_PIN_OPTION1 21
55#define I2C_SCL_PIN_OPTION2 22
56#define I2C_SDA_PIN_OPTION2 23
57
58// defaults
59#define DEFAULT_I2C_SCL_PIN I2C_SCL_PIN_OPTION2
60#define DEFAULT_I2C_SDA_PIN I2C_SDA_PIN_OPTION2
61
62
63// command/transmit FIFO is 32
64#define I2C_COMMAND_FIFO 32
65
66// receive FIFO is 16 bytes
67#define I2C_RX_FIFO_BYTES 16
68
69
78
79#define CHIP_MEMORY_MAP_I2C_CONTROLLER_BASE 0xA0100000
80
81
86typedef struct
87{
88 // I2C status: TX/RX FIFO and error conditions
89 __IO uint32_t status; // 0x00
90
91 // data read from target
92 __IO uint32_t read_data; // 0x04
93
94 // set this to an I2C command
95 __IO uint32_t command; // 0x08
96
97 // enable interrupts
98 __IO uint32_t interrupt_enable; // 0x0C
99
100 // interrupt status and interrupt clear
101 __IO uint32_t interrupt_status; // 0x10
102
103 // control: enable, clock source, and lower 6 bits of clock divider
104 __IO uint32_t control; // 0x14
105
106 // for setting up the clock, contains upper 8 bits of divider
107 __IO uint32_t prescale; // 0x18
108
110
111
112// *****************************************************************
113// these defines help when dealing with the STATUS register (0x00)
114
115// read only: 1 = command FIFO empty
116#define I2C_STATUS_COMMAND_FIFO_EMPTY 0x01
117// read only: 1 = Read Data FIFO not empty
118#define I2C_STATUS_READ_DATA_FIFO_NOT_EMPTY 0x02
119// 1 = no ack received
120// write 1 to clear this condition
121#define I2C_STATUS_ERROR_NO_ACK 0x04
122// 1 = lost arbitration
123// write 1 to clear this condition
124#define I2C_STATUS_ERROR_LOST_ARBITRATION 0x08
125// 1 = Read Data underflow
126#define I2C_STATUS_ERROR_READ_DATA_UNDERFLOW 0x10
127// 1 = command FIFO overflow
128#define I2C_STATUS_COMMAND_FIFO_OVERFLOW 0x20
129// 1 = command FIFO full
130#define I2C_STATUS_COMMAND_FIFO_FULL 0x40
131// 1 = transfer in progress
132#define I2C_STATUS_TRANSFER_IN_PROGRESS 0x80
133// 1 = read data FIFO overflow
134#define I2C_STATUS_READ_DATA_FIFO_OVERFLOW 0x100
135// 1 = command data FIFO ubderflow
136#define I2C_STATUS_COMMAND_FIFO_UNDERFLOW 0x200
137// clear FIFO: write 1 initiates clear to both FIFOs
138#define I2C_STATUS_CLEAR_FIFO 0x400
139
140
141// *****************************************************************
142// these defines help when dealing with the COMMAND register (0x08)
143
144#define I2C_COMMAND_NULL 0x00
145#define I2C_COMMAND_WRITE_DATA_0 0x10
146#define I2C_COMMAND_WRITE_DATA_1 0x11
147#define I2C_COMMAND_WRITE_DATA_8 0x12
148#define I2C_COMMAND_READ_DATA_8 0x13
149#define I2C_COMMAND_STOP 0x14
150#define I2C_COMMAND_START 0x15
151#define I2C_COMMAND_VACK 0x16
152
153#define I2C_WRITE_DATA_BIT 0x00
154#define I2C_READ_DATA_BIT 0x01
155
156// *****************************************************************
157// these defines help when dealing with the INTERRUPT registers (0x0C, 0x10)
158
159#define I2C_INTERRUPT_COMMAND_FIFO_EMPTY 0x01
160#define I2C_INTERRUPT_READ_DATA_FIFO_NOT_EMPTY 0x02
161#define I2C_INTERRUPT_ERROR 0x04
162#define I2C_INTERRUPT_LOSS_OF_ARBITRATION 0x08
163#define I2C_INTERRUPT_COMMAND_FIFO_DONE 0x10
164
165#define I2C_INTERRUPT_ALL 0x1F
166
167// *****************************************************************
168// these defines help when dealing with the CONTROL register (0x14)
169
170// bit 7 = enable/disable I2C
171#define I2C_CONTROL_REG_I2C_ENABLE 0x80
172#define I2C_CONTROL_REG_I2C_DISABLE 0x00
173
174// bit 6 = which clock, 1=PCK
175#define I2C_CONTROL_REG_CLOCK_SOURCE_APB 0x40
176#define I2C_CONTROL_REG_CLOCK_SOURCE_EXT 0x00
177
178// bit 0-5 lower 6 bits of clock divider
179#define I2C_CONTROL_REG_CLOCK_DIV_MASK 0x3F
180
181
182
183// *****************************************************************
184// helper enums for CLOCK DIVIDER REGISTER
185//
186// I2C system clock frequency is determined by:
187// 32MHz clock / ((clock_divider register + 1) * 4)
188//
189// although not all I2C sensors can do 1 MHz, we allow 1 MHz,
190// but use 400 KHz as a default since almost all sensors will
191// support this
192//
193// value 0 = 0+1 = 1 --> is 32 Mhz / 1 = 32 MHz
194// --------------------------------------------------
195// value 7 = 7+1 = 8 --> is 32 MHz / ( 8*4) = 1000 KHz = 1MHz
196// value 9 = 9+1 = 10 --> is 32 MHz / (10*4) = 800 KHz
197// value 19 = 19+1 = 20 --> is 32 MHz / (20*4) = 400 KHz
198// value 39 = 39+1 = 40 --> is 32 MHz / (40*4) = 200 KHz
199// value 79 = 79+1 = 80 --> is 32 MHz / (80*4) = 100 KHz
209
210#define CLOCK_LOWER_SIX_BITS 0x2F
211
212
213// *****************************************************************
214// this orients the struct (for the I2C controllers) with the
215// correct addresses, so referencing a field will now read/write
216// the correct chip address
217// *****************************************************************
218#define I2C_CHIP_REGISTERS ((I2C_REGISTERS_T *) CHIP_MEMORY_MAP_I2C_CONTROLLER_BASE)
219
220
227
228// prototype for callback from the Trident HAL to the app when a byte is received
229typedef void (*tr_hal_i2c_receive_callback_t) (uint8_t received_byte);
230
231// prototype for callback from the Trident HAL to the app when an event happens
232typedef void (*tr_hal_i2c_event_callback_t) (tr_hal_i2c_id_t i2c_id, uint32_t event_bitmask);
233
234
238#define I2C_EVENT_TX_EMPTY 0x01
239#define I2C_EVENT_RX_FINISH 0x02
240#define I2C_EVENT_ERROR 0x04
241#define I2C_EVENT_ABORT_LOST_ARB 0x08
242#define I2C_EVENT_COMMAND_DONE 0x10
243
244
249typedef struct
250{
251 // this struct is for I2C Controllers ONLY
252
253 // SDA and SCL pin
256
257 // clock setting
259
260 // callback from HAL to App when a byte is received
261 // if the app doesn't want this, then set it to NULL
263
264 // callback from HAL to App when an event happens
265 // if the app doesn't want this, then set it to NULL
267
268 // are the chip interrupts enabled
270
271 // set the interrupt priority
273
274 // when the I2C is powered off we can choose to DISABLE interrupts, meaning
275 // we will STAY powered off even when events are happening, or we can choose
276 // to KEEP interrupts enabled when powered off. This means we would wake on
277 // interrupt and power the UART back on
279
281
282
287#define I2C_CONFIG_CONTROLLER0 \
288 { \
289 .sda_pin = (tr_hal_gpio_pin_t) { DEFAULT_I2C_SDA_PIN }, \
290 .scl_pin = (tr_hal_gpio_pin_t) { DEFAULT_I2C_SCL_PIN }, \
291 .clock_setting = I2C_CLOCK_100_KHZ, \
292 .rx_handler_function = NULL, \
293 .event_handler_fx = NULL, \
294 .enable_chip_interrupts = true, \
295 .interrupt_priority = TR_HAL_INTERRUPT_PRIORITY_5, \
296 .wake_on_interrupt = false, \
297 }
298
299
303typedef struct
304{
305 // to be determined
309 uint32_t count_error;
311
313
314
315#endif // T32CM11_I2C_H_
316
317
This file contains the CHIP SPECIFIC types and defines for the T32CM11.
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
I2C_REGISTERS_T * tr_hal_i2c_get_controller_register_address(tr_hal_i2c_id_t i2c_id)
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_800_KHZ
Definition T32CM11_i2c.h:203
@ 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
void(* tr_hal_i2c_event_callback_t)(tr_hal_i2c_id_t i2c_id, uint32_t event_bitmask)
Definition T32CZ20_i2c.h:211
tr_hal_i2c_clock_rate_t
Definition T32CZ20_i2c.h:180
void(* tr_hal_i2c_receive_callback_t)(uint8_t received_byte)
Definition T32CZ20_i2c.h:208
Definition T32CM11_i2c.h:87
__IO uint32_t prescale
Definition T32CM11_i2c.h:107
__IO uint32_t interrupt_status
Definition T32CM11_i2c.h:101
__IO uint32_t status
Definition T32CM11_i2c.h:89
__IO uint32_t control
Definition T32CM11_i2c.h:104
__IO uint32_t interrupt_enable
Definition T32CM11_i2c.h:98
__IO uint32_t command
Definition T32CM11_i2c.h:95
__IO uint32_t read_data
Definition T32CM11_i2c.h:92
pin type
Definition tr_hal_platform.h:23
Definition T32CM11_i2c.h:304
uint32_t count_error
Definition T32CM11_i2c.h:309
uint32_t count_tx_empty
Definition T32CM11_i2c.h:307
uint32_t count_command_done
Definition T32CM11_i2c.h:310
uint32_t count_abort_lost_arb
Definition T32CM11_i2c.h:308
uint32_t count_rx_finish
Definition T32CM11_i2c.h:306
Definition T32CM11_i2c.h:250
tr_hal_i2c_clock_rate_t clock_setting
Definition T32CM11_i2c.h:258
tr_hal_i2c_receive_callback_t rx_handler_function
Definition T32CM11_i2c.h:262
tr_hal_i2c_event_callback_t event_handler_fx
Definition T32CM11_i2c.h:266
tr_hal_gpio_pin_t sda_pin
Definition T32CM11_i2c.h:254
bool wake_on_interrupt
Definition T32CM11_i2c.h:278
tr_hal_int_pri_t interrupt_priority
Definition T32CM11_i2c.h:272
tr_hal_gpio_pin_t scl_pin
Definition T32CM11_i2c.h:255
bool enable_chip_interrupts
Definition T32CM11_i2c.h:269
This is the common include file for the Trident HAL GPIO Driver.