I donβt know anything about Arduino, but, as a rule, this is done on most microcontrollers, so this is what you point directly to the perigal register area, in this case the serial port. Suppose your MCU maps these registers as follows:
Then you can monitor the port using a pointer, for example:
#define SERIAL_PORT1 ((volatile uint8_t*)0x1234) #define SERIAL_PORT2 ((volatile uint8_t*)0x2000) typedef volatile uint8_t* serial_port_t; ... serial_port_t port; if (something == somethingElse) port = SERIAL_PORT1; else port = SERIAL_PORT2;
Then it can be expanded so that you can use registers as variables, for example, using macros:
#define SERIAL_CONTROL_REG(offset) (*(offset + 0)) #define SERIAL_DATA_REG(offset) (*(offset + 1)) #define SERIAL_STATUS_REG(offset) (*(offset + 2)) if(SERIAL_STATUS_REG(port) & some_mask) { SERIAL_DATA_REG(port) = 0xAA; }
This is how you usually write common hardware drivers for MCU peripherals with more than one identical port on board.
source share