Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/utility/HardwareSerialRS485_Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ extern volatile unsigned long timer0_overflow_count; // ticks every 64*256 cycle
DDR<R> = mask; // activate pins for output:
PORT<R> = mask; // Sets output values, note: setting a disabled output high will enable the internal pull-up register.
where <R> is
D digital pins 7 to 0 (bank D)
B digital pins 13 to 8 (bank B)
C analogue pins 5 to 0 (bank C)
D digital pins 7 to 0 (bank D)
B digital pins 13 to 8 (bank B)
C analogue pins 5 to 0 (bank C)
*/

/* TRxControl class doc
Expand Down Expand Up @@ -78,15 +78,43 @@ class TRxControl
#pragma push_macro("_MMIO_BYTE") // was _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
#undef _MMIO_BYTE
#define _MMIO_BYTE
#define Atmega328p_Ports(__p) __p == 'B' ? PORTB : __p == 'C' ? PORTC : __p == 'D' ? PORTD :
#ifdef USBCON
#define Atmega348u_Ports(__p) __p == 'E' ? PORTE : __p == 'E' ? PORTF :
#else
#define Atmega348u_Ports(__p)

const static uint16_t TRx_PORT =
#ifdef PORTA
T__TRx_PORT == 'A' ? PORTA :
#endif
#ifdef PORTB
T__TRx_PORT == 'B' ? PORTB :
#endif
#ifdef PORTC
T__TRx_PORT == 'C' ? PORTC :
#endif
#ifdef PORTA
T__TRx_PORT == 'D' ? PORTD :
#endif
#ifdef PORTE
T__TRx_PORT == 'E' ? PORTE :
#endif
#ifdef PORTF
T__TRx_PORT == 'F' ? PORTF :
#endif
#ifdef PORTG
T__TRx_PORT == 'G' ? PORTG :
#endif
#ifdef PORTH
T__TRx_PORT == 'H' ? PORTH :
#endif
#ifdef PORTJ
T__TRx_PORT == 'J' ? PORTJ :
#endif
#ifdef PORTK
T__TRx_PORT == 'K' ? PORTK :
#endif
#ifdef PORTL
T__TRx_PORT == 'L' ? PORTL :
#endif
//TODO try a cleanup with a consexpr function, i.e. a function that is evaluatd at compile time.
const static unsigned char TRx_PORT = (Atmega328p_Ports(T__TRx_PORT) Atmega348u_Ports(T__TRx_PORT) PORTB);
const static unsigned char DDR = (DDRB-PORTB) + TRx_PORT; // this works because DDR registers are always located at a fixed offset to the PORT registers
PORTB;
const static uint16_t DDR = (DDRB-PORTB) + TRx_PORT; // this works because DDR registers are always located at a fixed offset to the PORT registers
#pragma pop_macro("_MMIO_BYTE")


Expand Down Expand Up @@ -317,7 +345,7 @@ state transitions for txState
user transitions
() *->0x00
begin()
txReady() test if txState {0x10, 0x11, 0x12, 0x13 or 0x*0}
txReady() test if txState {0x10, 0x11, 0x12, 0x13 or 0x*0}
- write()
setTxMode()

Expand Down Expand Up @@ -457,7 +485,7 @@ Transfer completed states:

if(false) { /* lekker puh */ }
else if( txState == 0x00) { /* nothing */ } // we are waiting (or not) for Rx idle and [sigh] we just received another byte
else if( (txState | 0x01) == 0x13) // txState {0x12, 0x13} we are expecting data echoed back
else if( (txState | 0x01) == 0x13) // txState {0x12, 0x13} we are expecting data echoed back
{
if(error) txState|=0x0c; // frame error, mark the state // fix txState+=0x0c;
else if(inHwShiftRegister!=c) txState|=0x08; // not the data we expected, mark the state // fix txState+=0x0a;
Expand Down