@@ -68,13 +68,10 @@ static PIOProgram *_getRxProgram(int bits) {
68
68
}
69
69
// ------------------------------------------------------------------------
70
70
71
- // TODO - this works, but there must be a faster/better way...
72
- static int __not_in_flash_func (_parity)(int bits, int data) {
73
- int p = 0 ;
74
- for (int b = 0 ; b < bits; b++) {
75
- p ^= (data & (1 << b)) ? 1 : 0 ;
76
- }
77
- return p;
71
+ static int __not_in_flash_func (_parity)(int data) {
72
+ data ^= data >> 4 ;
73
+ data &= 0xf ;
74
+ return (0x6996 >> data) & 1 ;
78
75
}
79
76
80
77
// We need to cache generated SerialPIOs so we can add data to them from
@@ -100,14 +97,14 @@ void __not_in_flash_func(SerialPIO::_handleIRQ)() {
100
97
uint32_t decode = _rxPIO->rxf [_rxSM];
101
98
uint32_t val = decode >> (32 - _rxBits - 1 );
102
99
if (_parity == UART_PARITY_EVEN) {
103
- int p = ::_parity (_bits, val);
100
+ int p = ::_parity (val);
104
101
int r = (val & (1 << _bits)) ? 1 : 0 ;
105
102
if (p != r) {
106
103
// TODO - parity error
107
104
continue ;
108
105
}
109
106
} else if (_parity == UART_PARITY_ODD) {
110
- int p = ::_parity (_bits, val);
107
+ int p = ::_parity (val);
111
108
int r = (val & (1 << _bits)) ? 1 : 0 ;
112
109
if (p == r) {
113
110
// TODO - parity error
@@ -374,10 +371,10 @@ size_t SerialPIO::write(uint8_t c) {
374
371
if (_parity == UART_PARITY_NONE) {
375
372
val |= 7 << _bits; // Set 2 stop bits, the HW will only transmit the required number
376
373
} else if (_parity == UART_PARITY_EVEN) {
377
- val |= ::_parity (_bits, c) << _bits;
374
+ val |= ::_parity (c) << _bits;
378
375
val |= 7 << (_bits + 1 );
379
376
} else {
380
- val |= (1 ^ ::_parity (_bits, c)) << _bits;
377
+ val |= (1 ^ ::_parity (c)) << _bits;
381
378
val |= 7 << (_bits + 1 );
382
379
}
383
380
val <<= 1 ; // Start bit = low
0 commit comments