SLUAAC4 October 2021 TPS92682-Q1
Create a valid frame based on address and data with parity.
Uint16 assembleSPICmd_682(Uint16 write, Uint16 address, Uint8 data)
{
Uint16 assembledCmd = 0; // Build this to shift through parity calculation
Uint16 parity = 0; // Parity bit calculated here
Uint16 packet = 0; // This will be what we send
if(write)
{
assembledCmd |= 0x8000; // Set CMD = 1
}
assembledCmd |= (((address << 9) & 0x7E00) | (Uint16)(data & 0x00FF));
packet = assembledCmd;
// Calculate parity
while(assembledCmd > 0)
{
// Count the number of 1s in the LSb
if(assembledCmd & 0x0001)
{
parity++;
}
// Shift right
assembledCmd >>= 1;
}
// If the LSb is a 0 (even # of 1s), we need to add the odd parity bit
if(!(parity & 0x0001))
{
packet |= (1 << 8);
}
return(packet);
}