Page 1 of 1

Parity bit

Posted: Wed Apr 08, 2026 4:31 am
by viktor_aust
Hi

Tuss4470 IC uses SPI to communicate with the Master.
Data format: (Reading the register)
msb = 1 r/w bit + 6 regi-addr bits + parity bit
lsb = 0x00

Odd parity bit should be calculated.
The open source project uses the next:

byte tuss4470Parity(byte* spi16Val)
{
return parity16(BitShiftCombine(spi16Val[0], spi16Val[1]));
}
unsigned int BitShiftCombine(unsigned char x_high, unsigned char x_low) {
return (x_high << 8) | x_low; // Combine high and low bytes
}

byte parity16(unsigned int val) {
byte ones = 0;
for (uint8_t i = 0; i < 16; i++) {
if ((val >> i) & 1) {
ones++;
}
}
return (ones + 1) % 2; // Odd parity calculation
}


Is it possible to calculate the parity bit for msb only (as the lsb = 0x00)?
Is it better to calculate the parity bit for integer (msb << 8) + lsb ?
Thanks

Re: Parity bit

Posted: Wed Apr 08, 2026 6:29 am
by viktor_aust
Example
Data (16-bit): 1010 0101 1100 0011
Count of 1s: 8 (even)
Odd Parity Bit: 1
Resulting frame (with parity): 1010 0101 1100 0011 + 1

Re: Parity bit

Posted: Wed Apr 08, 2026 6:49 am
by mnfisher
I would probably count the bits in both MSB and LSB. If a byte (LSB or MSB) is 0 - it won't affect the parity value - but if there is an error in transmission then it might not be 0 - and will affect the final result (if you use (MSB << 8) + LSB)

Alternatively - if LSB must be 0 - ignore it - and use result = (MSB << 8)

I think option '1' would be more likely to detect errors (due to noise) on the line?

Martin

Re: Parity bit

Posted: Wed Apr 08, 2026 7:31 am
by viktor_aust
Hi
Thanks Martin
However I do something wrong.
On the attached example the
byte = 10011100 (4 ones)
however the parity check = 5
---
As I use the byte only, this byte has: 1bit (r/w)+ 6 bits (regi addr) + 1bit (parity).
Should I remove the parity bit during the calculations?

Re: Parity bit

Posted: Wed Apr 08, 2026 8:51 am
by mnfisher
Rather than using an if .... statement to calculate the parity bit (and brackets might change the result here if (.x == 1) || (.x == 3) etc

Depending on whether you need odd or even parity:

Use .parity = .x & 1 Returns .parity = 1 if an odd number of bits set

Or if it needs to be 1 for an even number of bits:

.parity = (.x + 1) & 1

Where .x is the number of bits set.

Martin