A quick rewrite of imu_SPIreadBytes and imu_SPIwriteByte to something that compiles and should work for a PIC32.
I don't have the IMU module installed but the MOSI output looks like we have data going out during the IMU init function. The modules uses 3-wire SPI bidirectional data line so a resistor will be needed on
the controller MOSI.
C:
/*
* PIC32: changed to use hardware SPI module and plib_gpio pin functions
*/
void imu_SPIreadBytes(unsigned char csPin, unsigned char subAddress, unsigned char *dest, unsigned char count)
{
int i;
// To indicate a read, set bit 0 (msb) of first byte to 1
unsigned char rAddress = 0x80 | (subAddress & 0x3F);
// Mag SPI port is different. If we're reading multiple bytes,
// set bit 1 to 1. The remaining six bytes are the address to be read
if ((csPin == __pinM) && count > 1) rAddress |= 0x40;
// low(csPin);
if (csPin == __pinM)
csPin_m_Clear();
else
csPin_ag_Clear();
// shift_out(__pinSDIO, __pinSCL, MSBFIRST, 8, rAddress);
SPI2_Write(&rAddress, 1);
for (i = 0; i < count; i++) {
// dest[i] = shift_in(__pinSDIO, __pinSCL, MSBPRE, 8);
SPI2_Read(&dest[i], 1);
}
// high(csPin);
if (csPin == __pinM)
csPin_m_Set();
else
csPin_ag_Set();
}
/*
* PIC32: changed to use hardware SPI module and plib_gpio pin functions
*/
void imu_SPIwriteByte(unsigned char csPin, unsigned char subAddress, unsigned char data)
{
uint8_t tmp = subAddress & 0x3F;
// low(csPin);
if (csPin == __pinM)
csPin_m_Clear();
else
csPin_ag_Clear();
// shift_out(__pinSDIO, __pinSCL, MSBFIRST, 8, subAddress & 0x3F);
SPI2_Write(&tmp, 1);
// shift_out(__pinSDIO, __pinSCL, MSBFIRST, 8, data);
SPI2_Write(&data, 1);
// high(csPin);
if (csPin == __pinM)
csPin_m_Set();
else
csPin_ag_Set();
}
the controller MOSI.
Last edited: