Code:
uint16 HalSamM8qGetFrame(uint8 *gnrmcFrame_tu8, uint8 maxFrameLength_u8)
{
uint8 highBytes_u8;
uint8 lowBytes_u8;
uint16 frameLength_u16 = 0u;
uint16 counter_u16 = 0u;
uint8 i2cBuffer_tu8[GPSRECEIVER_MAXFRAMESIZE] = {0u};
i2cBuffer_tu8[0] = 0xFDu; /* The address 0xFD is containing the higher byte of the frame length */
HalI2cWrite(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Setting the pointer to the address */
os_delay_us(500u);
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Reading register 0xFD */
highBytes_u8 = i2cBuffer_tu8[0];
i2cBuffer_tu8[0] = 0xFEu; /* The address 0xFE is containing the lower byte of the frame length */
HalI2cWrite(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Setting the pointer to the address */
os_delay_us(500u);
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Reading register 0xFE */
lowBytes_u8 = i2cBuffer_tu8[0];
frameLength_u16 = (uint16)((highBytes_u8 << 8u) + lowBytes_u8);
if(frameLength_u16 != 0xFFFFu)
{
i2cBuffer_tu8[0] = 0xFFu;
HalI2cWrite(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Setting the pointer to the address */
os_delay_us(500u);
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Reading register 0xFF and so on, to see the frame */
while((i2cBuffer_tu8[0] != '$') && (counter_u16 < frameLength_u16)) /* We have to read all the buffer to try to find the '$' */
{
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u);
counter_u16++;
}
if(counter_u16 < frameLength_u16) /* We don't want to read anything more if we haven't found the '$' in frameLength_u16 tries */
{
counter_u16 = 0u;
gnrmcFrame_tu8[counter_u16] = i2cBuffer_tu8[0];
while((i2cBuffer_tu8[0] != '*') && (counter_u16 < (maxFrameLength_u8 - 2u))) /* Reading and saving all the bytes until we found the "*" */
{
counter_u16++;
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u);
gnrmcFrame_tu8[counter_u16] = i2cBuffer_tu8[0u];
system_soft_wdt_feed();
}
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* First byte of the checksum */
counter_u16++;
gnrmcFrame_tu8[counter_u16] = i2cBuffer_tu8[0u];
counter_u16++;
HalI2cRead(GPSRECEIVER_I2C_ADDR, i2cBuffer_tu8, 1u); /* Second byte of the checksum */
gnrmcFrame_tu8[counter_u16] = i2cBuffer_tu8[0u];
counter_u16++;
}
else
{
counter_u16 = GPSRECEIVER_MAXFRAMESIZE; /* Setting the length of the frame to its max frame
otherwise we have counter_u16 = frameLength_u16 */
}
}
else
{
counter_u16 = GPSRECEIVER_MAXFRAMESIZE; /* Setting the length of the frame to its max frame
otherwise we have counter_u16 = 0u */
}
return counter_u16;
}
Statistics: Posted by Clancys — Tue Jul 10, 2018 5:19 pm