sachukol
Newbie level 1
I am having issues with I2C communications protocol while communicating with gyroscope that is part of an IMU breakout board. I am able to initialize and start transfer for I2C between PIC32 and IMU but when sending in the regaddress byte it runs into a master bus collision. I have included the following code. If you could please help me resolve this issue it would be greatly helpful for my project.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 /* * File: imumain.c * Author: harsh * * Created on April 14, 2012, 10:42 AM */ //#include <stdio.h> //#include <stdlib.h> #include <plib.h> #include <stdint.h> #include <stdbool.h> #define SystemClock() (80000000) #define PeripheralClock() (GetSystemClock()/(1<<OSCCONbite.PBDIV)) #define I2C_CLOCK_FREQ 100000 #define ACCEL_ADDRESS 0x53 #define MAG_ADDRESS 0x34 BOOL StartTransfer(BOOL, I2C_MODULE); BOOL TransmitOneByte(UINT8, I2C_MODULE); BOOL I2CSingleByteRead(UINT8, UINT8, UINT8*, I2C_MODULE); BOOL ReceiveOneByte(UINT8*, I2C_MODULE ); VOID StopTransfer(I2C_MODULE); /* * */ int main(void) { //#ifndef PIC32_STARTER_KIT // DDPCONbits.JTAGEN = 0; //#endif UINT32 actualClock; //CONFIGURE I2C I2CConfigure(I2C1, I2C_ENABLE_SLAVE_CLOCK_STRETCHING|I2C_ENABLE_HIGH_SPEED); if ( abs(actualClock-I2C_CLOCK_FREQ) > I2C_CLOCK_FREQ/10 ) { DBPRINTF("Error: I2C1 clock frequency (%u) error exceeds 10%%.\n", (unsigned)actualClock); } I2CEnable(I2C1,TRUE); I2CClearStatus(I2C1,I2C_TRANSMITTER_FULL); UINT8 tOutData = 0X00; BOOL bothWork = TRUE; if (I2CSingleByteRead(MAG_ADDRESS,0X00, &tOutData, I2C1)) { if (tOutData != 34) bothWork = FALSE; } return (EXIT_SUCCESS); } /******************************************************************************* Function: BOOL StartTransfer( BOOL restart ) Summary: Starts (or restarts) a transfer to/from the EEPROM. Description: This routine starts (or restarts) a transfer to/from the EEPROM, waiting (in a blocking loop) until the start (or re-start) condition has completed. Precondition: The I2C module must have been initialized. Parameters: restart - If FALSE, send a "Start" condition - If TRUE, send a "Restart" condition Returns: TRUE - If successful FALSE - If a collision occured during Start signaling Example: <code> StartTransfer(FALSE); </code> Remarks: This is a blocking routine that waits for the bus to be idle and the Start (or Restart) signal to complete. *****************************************************************************/ BOOL StartTransfer( BOOL restart, I2C_MODULE i2cselect ) { I2C_STATUS status; // Send the Start (or Restart) signal if(restart) { I2CRepeatStart(i2cselect); } else { // Wait for the bus to be idle, then start the transfer while( !I2CBusIsIdle(i2cselect) ); if(I2CStart(i2cselect) != I2C_SUCCESS) { DBPRINTF("Error: Bus collision during transfer Start\n"); return FALSE; } } // Wait for the signal to complete do { status = I2CGetStatus(i2cselect); } while ( !(status & I2C_START) ); return TRUE; } /******************************************************************************* Function: BOOL TransmitOneByte( UINT8 data ) Summary: This transmits one byte to the EEPROM. Description: This transmits one byte to the EEPROM, and reports errors for any bus collisions. Precondition: The transfer must have been previously started. Parameters: data - Data byte to transmit Returns: TRUE - Data was sent successfully FALSE - A bus collision occured Example: <code> TransmitOneByte(0xAA); </code> Remarks: This is a blocking routine that waits for the transmission to complete. *****************************************************************************/ BOOL TransmitOneByte( UINT8 data, I2C_MODULE i2cselect ) { //I2C_RESULT result; // Wait for the transmitter to be ready I2C_STATUS status; while(!I2CTransmitterIsReady(i2cselect)); // Transmit the byte if( I2CSendByte(i2cselect, data) == I2C_MASTER_BUS_COLLISION) { DBPRINTF("Error: I2C Master Bus Collision\n"); return FALSE; } status = I2CGetStatus(I2C1); // Wait for the transmission to finish while(!I2CTransmissionHasCompleted(i2cselect)); //Verify that the byte was acknowledged if (!I2CByteWasAcknowledged(i2cselect)) { DBPRINTF("Error: Sent byte was not acknowledged\n"); return FALSE; } return TRUE; } /******************************************************************************* Function: BOOL ReceiveOneByte( UINT8* data ) Summary: Get one byte of data. Description: This routine checks for recieve overflow if not if data is available get * byte. Precondition: The I2C module must have been initialized & a transfer started. Parameters: None. Returns: None. Example: <code> if(!RecieveOneByte(data)) * Success = FALSE; </code> Remarks: This is a blocking routine that waits for the Stop signal to complete. *****************************************************************************/ BOOL ReceiveOneByte(UINT8* data, I2C_MODULE i2cselect) { if (!I2CReceiverEnable(I2C1, TRUE) == I2C_RECEIVE_OVERFLOW) return FALSE; else { while(!I2CReceivedDataIsAvailable(I2C1)); *(data) = I2CGetByte(I2C1); } return TRUE; } /******************************************************************************* Function: void StopTransfer( void ) Summary: Stops a transfer to/from the EEPROM. Description: This routine Stops a transfer to/from the EEPROM, waiting (in a blocking loop) until the Stop condition has completed. Precondition: The I2C module must have been initialized & a transfer started. Parameters: None. Returns: None. Example: <code> StopTransfer(); </code> Remarks: This is a blocking routine that waits for the Stop signal to complete. *****************************************************************************/ void StopTransfer(I2C_MODULE i2c ) { I2C_STATUS status; // Send the Stop signal I2CStop(i2c); // Wait for the signal to complete do { status = I2CGetStatus(i2c); } while ( !(status & I2C_STOP) ); } BOOL I2CSingleByteRead(UINT8 target, UINT8 regaddress, UINT8* output, I2C_MODULE i2c) { if(!StartTransfer(FALSE, i2c)) return FALSE; I2C_7_BIT_ADDRESS SlaveAddress; I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, target, I2C_WRITE); if(!TransmitOneByte(SlaveAddress.byte, i2c)) return FALSE; if(!TransmitOneByte(regaddress, i2c)) return FALSE; if(!StartTransfer(TRUE, i2c)) return FALSE; I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, target, I2C_READ); if(!TransmitOneByte(SlaveAddress.byte, i2c)) return FALSE; if (!ReceiveOneByte(output,i2c)) return FALSE; //Send NAK and wait to finish I2CAcknowledgeByte(i2c,FALSE); I2CAcknowledgeHasCompleted(i2c); StopTransfer(i2c); return TRUE; }
Last edited by a moderator: