TMS320F28335 Library  1.0
Documentation
i2c.c
Go to the documentation of this file.
1 
10 #include "i2c.h"
11 
12 __interrupt void I2C_masterISR(void);
13 __interrupt void I2C_slaveISR(void);
14 
24  switch(I2C.sda){
25  case 32: switch(I2C.scl){
26  case 33: EALLOW;
27  TMC_BASE_PIO->CTL[1].PUD &= ~(0x1); // Enable pull-up for GPIO32 (SDAA)
28  TMC_BASE_PIO->CTL[1].QSEL1 &= ~(0x3<<0); // Synchronize to SYSCLKOUT GPIO32 (SDAA)
29  TMC_BASE_PIO->CTL[1].MUX1 |= (0x1<<0); // Configure GPIO32 to SDAA
30 
31  TMC_BASE_PIO->CTL[1].PUD &= ~(0x1); // Enable pull-up for GPIO33 (SCLA)
32  TMC_BASE_PIO->CTL[1].QSEL1 &= ~(0x3<<2); // Synchronize to SYSCLKOUT GPIO33 (SCLA)
33  TMC_BASE_PIO->CTL[1].MUX1 |= (0x1<<2); // Configure GPIO33 to SCLA
34  EDIS;
35 
36  TMC_BASE_I2C->MDR &= ~(0x0020); // Put module in reset
37 
38  TMC_BASE_I2C->PSC = 0xE; // Configure module clock to 10 MHz
39  TMC_BASE_I2C->CLKL = 35; // Clock low-time divide-down value --> 100 kHz on SCL
40  TMC_BASE_I2C->CLKH = 55; // Clock high-time divide-down value --> 100 kHz on SCL
41 
42  TMC_BASE_I2C->IER = (0x1 << 1); // Enable NACK interrupts
43  TMC_BASE_I2C->MDR |= (0x1<<10); // Set master mode
44 
45  TMC_BASE_I2C->FFTX |= (0x1<<14); // Enable Transmit Fifo Mode
46  TMC_BASE_I2C->FFTX |= (0x1<<13); // Enable Transmit Fifo operation
47 
48  TMC_BASE_I2C->FFRX |= (0x1<<13); // Enable Receive FIFO Operation
49  TMC_BASE_I2C->FFRX |= (0x1); // Set Flag when there is 1 or more Byte in Receive FIFO
50  TMC_BASE_I2C->FFRX |= (0x1 << 6); // clear RXFFINT Flag
51 
52  DINT; // Disable Interrupts
53  EALLOW; // Allow register-access
54  TMC_BASE_INT_REGS->I2CINT1A = (Uint32)&I2C_masterISR; // Configure interrupt
55  EDIS; // Protect register
56  TMC_BASE_PIE->CTRL |= 0x01; // Enable PIE block
57  TMC_BASE_PIE->GROUP[7].IER |= 0x1; // Enable I2CINT1A in the PIE: Group 8 interrupt 1
58  IER |= 0x0080; // Enable IER Block
59  EINT; // Enable Interrupts
60 
61  TMC_BASE_I2C->MDR |= (0x1 << 5); // Release I2C from reset --> I2C module is enabled.
62 
63  break;
64  default: return -2;
65  }
66  break;
67  default: return -1;
68  }
69  return 0;
70 }
71 
82  switch(I2C.sda){
83  case 32: switch(I2C.scl){
84  case 33: EALLOW;
85  TMC_BASE_PIO->CTL[1].PUD &= ~(0x1); // Enable pull-up for GPIO32 (SDAA)
86  TMC_BASE_PIO->CTL[1].QSEL1 |= (0x3<<0); // Asynch input GPIO32 (SDAA)
87  TMC_BASE_PIO->CTL[1].MUX1 |= (0x1<<0); // Configure GPIO32 to SDAA
88 
89  TMC_BASE_PIO->CTL[1].PUD &= ~(0x1<<1); // Enable pull-up for GPIO33 (SCLA)
90  TMC_BASE_PIO->CTL[1].QSEL1 |= (0x3<<2); // Asynch input GPIO33 (SCLA)
91  TMC_BASE_PIO->CTL[1].MUX1 |= (0x1<<2); // Configure GPIO33 to SCLA
92  EDIS;
93 
94  TMC_BASE_I2C->MDR &= ~(0x0020); // Put module in reset
95 
96  TMC_BASE_I2C->PSC = 0xE; // Configure module clock to 10 MHz
97  TMC_BASE_I2C->CLKL = 35; // Clock low-time divide-down value --> 100 kHz on SCL
98  TMC_BASE_I2C->CLKH = 55; // Clock high-time divide-down value --> 100 kHz on SCL
99  TMC_BASE_I2C->IER = (0x1 << 6); // Enable Adressed as Slave Interrupt
100 
101  TMC_BASE_I2C->FFTX |= (0x1<<14); // Enable Transmit Fifo Mode
102  TMC_BASE_I2C->FFTX |= (0x1<<13); // Enable Transmit Fifo operation
103  TMC_BASE_I2C->FFTX &= ~(0x1F); // Set Flag when Transmit FIFO is empty
104  TMC_BASE_I2C->FFTX |= (0x1 << 6); // clear TXFFINT Flag
105 
106  TMC_BASE_I2C->FFRX |= (0x1<<13); // Enable Receive FIFO Operation
107  TMC_BASE_I2C->FFRX |= (0x1); // Set Flag when there is 1 or more Byte in Receive FIFO
108  TMC_BASE_I2C->FFRX |= (0x1 << 6); // clear RXFFINT Flag
109 
110  TMC_BASE_I2C->OAR = slave_address; // 7 Bit slave address
111 
112  TMC_BASE_I2C->MDR &= ~(0x1<<3); // Free data format disabled
113  TMC_BASE_I2C->MDR &= ~(0x1<<8); // 7 bit address-mode
114  TMC_BASE_I2C->MDR &= ~(0x1<<10); // Slave mode
115 
116  DINT; // Disable Interrupts
117  EALLOW; // Allow register-access
118  TMC_BASE_INT_REGS->I2CINT1A = (Uint32)&I2C_slaveISR; // Configure interrupt
119  EDIS; // Protect register
120  TMC_BASE_PIE->CTRL |= 0x01; // Enable PIE block
121  TMC_BASE_PIE->GROUP[7].IER |= 0x1; // Enable I2CINT1A in the PIE: Group 8 interrupt 1
122  IER |= 0x0080; // Enable IER Block
123  EINT; // Enable Interrupts
124 
125  TMC_BASE_I2C->MDR |= 0x0020; // Release I2C from reset --> I2C module is enabled.
126 
127  break;
128  default: return -2;
129  }
130  break;
131  default: return -1;
132  }
133  return 0;
134 }
135 
144  Uint32 i;
145 
146  if(!(TMC_BASE_I2C->MDR & 0x0020)){
147  return -18; // Return -1: I2C module is disabled
148  }
149 
150  while(TMC_BASE_I2C->STR & 0x1000); // Wait for end of transmission
151 
152  TMC_BASE_I2C->MDR |= (0x1<<10); // Master mode
153  TMC_BASE_I2C->MDR |= (0x1<<9); // Transmitter mode
154  TMC_BASE_I2C->SAR = msg->SlaveAddress; // Setup slave address
155  TMC_BASE_I2C->CNT = msg->DataLength+msg->RegisterAddressLength; // Setup I2CCNT (number of bytes to send = Datalength + Addresslength)
156 
157 
158  if(msg->RegisterAddressLength == 2){
159  TMC_BASE_I2C->DXR = msg->RegisterAddressHigh; // Write Adress High to DXR
160  }
161  if(msg->RegisterAddressLength >= 1){
162  TMC_BASE_I2C->DXR = msg->RegisterAddressLow; // Write Adress Low to DXR
163  }
164  for (i=0; i<msg->DataLength; i++)
165  {
166  TMC_BASE_I2C->DXR = *(msg->Data+i); // Write Data to DXR
167  }
168 
169  TMC_BASE_I2C->MDR |= (0x1<<11); // Setup STOP-Condition Bit
170  TMC_BASE_I2C->MDR |= (0x1<<13); // Generate START-condition on the I2C-bus and send data from DXR
171 
172  for(i=0; i<800; i++){
173  __asm(" RPT #255 || NOP"); // Wait for transmission
174  }
175 
176  while(TMC_BASE_I2C->STR & 0x1000); // While BB --> Wait for end of transmission
177 
178  if(TMC_BASE_I2C->FFTX & 0x1F00){ // If Transmit FIFO not empty
179  TMC_BASE_I2C->FFTX &= ~(0x1<<14); // Disable Transmit Fifo Mode
180  TMC_BASE_I2C->FFTX &= ~(0x1<<13); // Disable Transmit Fifo operation
181  TMC_BASE_I2C->FFTX |= (0x1<<14); // Enable Transmit Fifo Mode
182  TMC_BASE_I2C->FFTX |= (0x1<<13); // Enable Transmit Fifo operation
183  }
184  return 0;
185 }
186 
195  int i=0;
196 
197  if(!(TMC_BASE_I2C->MDR & 0x0020)){
198  return -18; // Return -1: I2C module is disabled
199  }
200 
201  while(TMC_BASE_I2C->STR & 0x1000); // Wait for end of transmission
202 
203  TMC_BASE_I2C->MDR |= (0x1<<10); // Master mode
204  TMC_BASE_I2C->MDR |= (0x1<<9); // Transmitter mode
205  TMC_BASE_I2C->SAR = msg->SlaveAddress; // Setup slave address
206  TMC_BASE_I2C->CNT = msg->RegisterAddressLength; // Setup I2CCNT (number of bytes to send = Addresslength)
207 
208  if(msg->RegisterAddressLength == 2){
209  TMC_BASE_I2C->DXR = msg->RegisterAddressHigh; // Write Adress High to DXR
210  }
211  if(msg->RegisterAddressLength >= 1){
212  TMC_BASE_I2C->DXR = msg->RegisterAddressLow; // Write Adress Low to DXR
213  }
214 
215  TMC_BASE_I2C->MDR |= (0x1<<13); // Set STT to 1 --> I2C module generates a START-condition on the I2C-bus
216 
217  while((TMC_BASE_I2C->FFTX & 0x1F00) > 0); // Wait while not all Bytes have been sent (Transmit FIFO contains >0 bytes)
218 
219  TMC_BASE_I2C->CNT = msg->DataLength; // Set up I2CCNT for Receiving bytes
220  TMC_BASE_I2C->MDR &= ~(0x1<<9); // Receive mode
221  TMC_BASE_I2C->MDR |= (0x1<<11); // Send STOP-Condition after all data has been received
222  TMC_BASE_I2C->MDR |= (0x1<<13); // Send repeated start bit
223 
224  for(i=0; i < msg->DataLength; i++)
225  {
226  while(!(TMC_BASE_I2C->FFRX & 0x0080)){ // Wait for 1 or more byte in receive FIFO
227 
228  }
229  msg->Data[i] = TMC_BASE_I2C->DRR; // Get Data from Receive FIFO
230  __asm(" RPT #255 || NOP");
231  TMC_BASE_I2C->FFRX |= (0x1 << 6); // Clear flag
232  }
233 
234  return 0;
235 
236 }
237 
243 __interrupt void I2C_masterISR(void){
244  //NACK detected --> abort transmission and release bus
245  do{
246  TMC_BASE_I2C->MDR &= ~(0x1 << 5); // Release Bus (sets Bus Busy to 0)
247  TMC_BASE_I2C->MDR |= (0x1 << 5);
248 
249  TMC_BASE_I2C->FFTX &= ~(0x1<<14); // Disable Transmit FIFO Mode
250  TMC_BASE_I2C->FFTX &= ~(0x1<<13); // Disable Transmit FIFO operation
251  TMC_BASE_I2C->FFTX |= (0x1<<14); // Enable Transmit FIFO Mode
252  TMC_BASE_I2C->FFTX |= (0x1<<13); // Enable Transmit FIFO operation
253  }
254  while(TMC_BASE_I2C->STR & 0x1000);
255  TMC_BASE_PIE->ACK |= 0x0080; // Enable future I2C (PIE Group 8) interrupts
256 }
257 
265 __interrupt void I2C_slaveISR(void)
266 {
267  TMPS_I2C_Reg TMC_BASE_I2C_register;
268  Uint16 IntSource;
269  Uint32 i=0;
270  Uint16 z=0;
271  static Uint16 previous_register_address = 0x0000;
272  Uint16 register_address = 0x0000;
273  Uint16 register_value = 0x0000;
274  Uint16 stop = 0;
275 
276  IntSource = (TMC_BASE_I2C->ISRC & 0x0007); // Read interrupt source and clear it via CPU read
277  if(IntSource == 7) // Interrupt source = addressed as slave
278  {
279  for(i=0; i<2; i++){
280  while(!(TMC_BASE_I2C->FFRX & 0x0080) && !stop){ // Wait, while no byte in receive FIFO
281  if(TMC_BASE_I2C->STR & 0x0020){ // break if Stop condition detected
282  stop=1;
283  break;
284  }
285  }
286  if(stop){
287  break;
288  }
289  register_address |= TMC_BASE_I2C->DRR; // Read byte from Rx-FIFO
290  if(i==0){
291  register_address = (register_address<<8); // Shift High Address Byte
292  }
293  TMC_BASE_I2C->FFRX |= (0x1 << 6); // Clear Flag
294  for (z=0;z<50;z++){
295  __asm(" RPT #255 || NOP");
296  }
297  }
298 
299  if(!(TMC_BASE_I2C->STR & 0x4000)){ // If not Addressed as Slave Transmitter
300  TMC_BASE_I2C_register = (TM_CAST(TMPS_I2C_Reg) register_address); // Cast Addressbytes for register-access
301  for(i=0; i<2; i++){
302  while(!(TMC_BASE_I2C->FFRX & 0x0080) && !stop){ // Wait, while no byte in receive FIFO
303  if(TMC_BASE_I2C->STR & 0x0020){ // Break if Stop condition detected
304  stop=1;
305  break;
306  }
307  }
308  if(stop){
309  break;
310  }
311  if(i==0){
312  register_value = (Uint16) (TMC_BASE_I2C->DRR); // Receive first Databyte
313  }
314  else{
315  register_value |= ((Uint16) TMC_BASE_I2C->DRR<<8); // Receive second databyte
316  TMC_BASE_I2C_register->register_adress = register_value; // Write datavalue into register
317  break;
318  }
319  TMC_BASE_I2C->FFRX |= (0x1 << 6); // Clear Flag
320  for (z=0;z<50;z++){
321  __asm(" RPT #255 || NOP");
322  }
323  }
324  }
325  else{
326  TMC_BASE_I2C_register = (TM_CAST(TMPS_I2C_Reg) previous_register_address); // Cast Addressbytes for register-access
327  TMC_BASE_I2C->MDR |= (0x1<<9); // Transmitter mode
328  TMC_BASE_I2C->DXR = ((TMC_BASE_I2C_register->register_adress) >> 8); // Slave writes data to bus
329  TMC_BASE_I2C->DXR = (TMC_BASE_I2C_register->register_adress); // Slave writes data to bus
330  while(TMC_BASE_I2C->FFTX & 0x0080){ // wait, while no byte in transmit FIFO
331  if(TMC_BASE_I2C->STR & 0x0020){ // break if Stop condition detected
332  TMC_BASE_I2C->MDR &= ~(0x1<<9); // Receiver mode
333  break;
334  }
335  }
336  TMC_BASE_I2C->MDR &= ~(0x1<<9); // Receiver mode
337  }
338  }
339  else
340  {
341  __asm(" ESTOP0"); // Generate some error due to invalid interrupt source
342  }
343  previous_register_address = register_address;
344  TMC_BASE_PIE->ACK |= 0x0080; // Enable future I2C (PIE Group 8) interrupts
345 }
346 
I2C_slaveISR
__interrupt void I2C_slaveISR(void)
I2C ISR-Routine in slave mode when Adressed detected.
Definition: i2c.c:265
TMC_BASE_PIE.h
PIE-Register header file.
int16
int int16
16 Bit Variable: - 32.768 .. 32.767
Definition: global_defines.h:17
IER
cregister volatile unsigned int IER
Generate reference to Interrupt Enable Register.
init_I2CMasterMode
int16 init_I2CMasterMode(I2CInterface I2C)
Initialization of I2C Interface master mode.
Definition: i2c.c:23
I2C_readDataAsMaster
int16 I2C_readDataAsMaster(I2CMSG_transmit *msg)
Read data in I2C master mode from slave.
Definition: i2c.c:194
I2C_masterISR
__interrupt void I2C_masterISR(void)
I2C ISR-Routine in master mode when NACK detected.
Definition: i2c.c:243
_I2CInterface
I2C Interface: Assign GPIOs to I2C module.
Definition: i2c.h:12
_I2CInterface::scl
Uint16 scl
I2C-SCL-GPIO (GPIO33)
Definition: i2c.h:14
_I2CMSG_transmit::RegisterAddressLow
Uint16 RegisterAddressLow
Write second byte of Registeradress. Example: 0x12.
Definition: i2c.h:29
_I2CInterface::sda
Uint16 sda
I2C-SDA-GPIO (GPIO32)
Definition: i2c.h:13
_I2CMSG_transmit
I2C Transmit-Message structure.
Definition: i2c.h:25
_TMS_I2C_Reg
I2C Register structure for slave receive and transmit operations.
Definition: i2c.h:19
I2C_writeDataAsMaster
int16 I2C_writeDataAsMaster(I2CMSG_transmit *msg)
Send data in I2C master mode to slave.
Definition: i2c.c:143
TMC_BASE_XINT.h
External-Interrupt-Register header file.
_I2CMSG_transmit::RegisterAddressHigh
Uint16 RegisterAddressHigh
Write first byte of Registeradress. Example: 0xFF.
Definition: i2c.h:28
Uint16
unsigned int Uint16
16 Bit Variable: 0 .. 65.535
Definition: global_defines.h:21
_I2CMSG_transmit::SlaveAddress
Uint16 SlaveAddress
Write 7-Bit Slave Adress. Example: 0x3.
Definition: i2c.h:26
_I2CMSG_transmit::RegisterAddressLength
Uint16 RegisterAddressLength
Write length of Registeradress in Byte(max. 2). Example: 2 (for 0xFF12)
Definition: i2c.h:27
TMC_BASE_INT_REGS.h
Interupt-Register header file.
_I2CMSG_transmit::Data
Uint16 Data[16]
Write data to be sent into Data-Array. Read Data from Array after Read-operations.
Definition: i2c.h:31
_I2CMSG_transmit::DataLength
Uint16 DataLength
Write length of databytes to receive or to transmit. Example: 2 (for transmission of 0x6688)
Definition: i2c.h:30
init_I2CSlaveMode
int16 init_I2CSlaveMode(I2CInterface I2C, Uint16 slave_address)
Initialization of I2C Interface slave mode.
Definition: i2c.c:81
_TMS_I2C_Reg::register_adress
TM_REG16 register_adress
Used by Slave ISR for Registeraccess.
Definition: i2c.h:20
TMC_BASE_PIO.h
GPIO-Register header file.
i2c.h
Header file for I2C module.
TMC_BASE_I2C.h
I2C-Register header file.
Uint32
unsigned long Uint32
32 Bit Variable: 0 .. 4.294.967.295
Definition: global_defines.h:22