I2C2_MasterWrite() function in i2c2.c always generates a TRAPS_halt_on_error
I am using code generated by MCC for the dsPIC33EP64GP502 processor on my prototype board. The problem is that the I2C2_MasterWrite() function always generates a TRAPS_halt_on_error with the value of 2 in the following function:
void __attribute__((naked, noreturn, weak)) TRAPS_halt_on_error(uint16_t code)
TRAPS_error_code = code;
MCC generated the files mcc.c and i2c2.c, among others, which inspired me to use the I2C2_MasterWrite() function.
Can anyone help me understand the cause of this trap? I couldn't find any information using Google to understand the error TRAPS_halt_on_error.
I verified the signal integrity of the I2C clock and SDA signals using oscilloscope using a bit banged implementation using standard _LATB14 and _LATB15 functions and saw the SDA and SCL signals wiggle as programmed. However, using the MCC generated functions, these same signals did not even wiggle.
I verified that the slave device I am talking to (PCA9685) is enabled using an oscilloscope on the enable signal.
The pertinent code generating the error is as follows
// initialize the device
/* Specify as outputs */
_TRISB1 = 0; // RB1
_TRISB4 = 0; // RB4
_TRISB13 = 0; // ~OE for PCA9685
_TRISB14 = 0; // RB14
_TRISB15 = 0; // RB15
The code produced by MCC for SYSTEM_Initialize is:
I2C2_MESSAGE_STATUS status = I2C2_MESSAGE_PENDING;
uint16_t slaveDeviceAddress = SLAVE_DEV_ADDRESS;
WriteData = (MODE1 >> 8); // PCA9685 Subaddress
WriteData = (uint8_t)(MODE1);
WriteData = 0b00110001; // Setting mode to sleep
// following the same function call format described in i2c2.h
I2C2_MasterWrite( WriteData, 3, slaveDeviceAddress, &status); // Results in TRAPS_halt_on_error