This is the code I have for sending reading a character from the MMA7455 i2c accelerometer. I need to do the usual which is to send the slave address wait for the ACK, then send the register start address in the MMA7455 to read from. This all works under CW6.3 without MQX, I can't get the driver to work under CW10.1 and MQX.
The write hangs and I am not sure if the IO_IOCTL_I2C_SET_DESTINATION_ADDRESS is sent out by the driver or not since I always get a 0 on the read result and a read buffer of zeros. We need to read 20 characters to verify the device. If we implement a fwrite the code just hangs, probably waiting on an ACK.
There is something wrong with the basic concept but I can't figure it out from the example code. Any help would be appreciated.
/*FUNCTION*-----------------------------------------------------------------------------------------
(((((((((((((((((((((((((((((((((( I2C_Task )))))))))))))))))))))))))))))))))))
----------------------------------------------------------------------------------------------------
vFn_i2c_test test driver fcn
Input Parameter: void
Return Type: none
Notes:
--------------------------------------------------------------------------------------------------*/
void I2C_task(uint_32)
{
MQX_FILE_PTR fd;
uint_32 param, result;
I2C_STATISTICS_STRUCT stats;
uchar_ptr recv_buffer;
uint8 buffer[30];
/* Allocate receive buffer */
recv_buffer = _mem_alloc_zero (RECV_BUFFER_SIZE);
if (recv_buffer == NULL){
printf ("ERROR getting receive buffer!\n");
_task_block ();
}
/* Open the I2C driver */
fd = fopen ("i2c0:", NULL);
if (fd == NULL){
printf ("ERROR opening the I2C driver!\n");
_time_delay (200L);
_mqx_exit (1L);
}
/* Test ioctl commands */
param = 100000;
if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_BAUD, ¶m)){ // Set current baud rate to param
printf ("ERROR\n");
}
if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_MASTER_MODE, NULL)){ // printf ("Set master mode ... ");
printf ("OK\n");
}
param = 0x69; // slave address of accelerometer
if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_STATION_ADDRESS, ¶m)){ // printf ("Set destination address to 0x%02x ... ", param);
printf ("ERROR\n");
}
param = 0x00;
if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, ¶m)){ // printf ("Set destination address to 0x%02x ... ", param);
printf ("ERROR\n");
}
/* Test request 1 byte, read 1 byte */
param = 20;
if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_RX_REQUEST, ¶m)){ // printf ("Test request 1 byte, read 1 byte ... ");
printf ("\n ERROR during set rx request\n");
}
result = fread (&buffer, 1, 1, fd);
if (I2C_OK != ioctl (fd, IO_IOCTL_FLUSH_OUTPUT, ¶m)){
printf ("\n ERROR during flush\n");
}
if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_STOP, NULL)){
printf ("\n ERROR during stop\n");
}
_task_block();
}