I’ve got a situation that is - to me at least - rather complicated. I have two interfaces that are bit-bashed lighting interfaces. There is a Tx pin and an Rx pin that are controlled by timer interrupts and pin change interrupts. They are Manchester encoded interfaces that need to act completely independently to each other.
That in itself is not so complicated; I have written code to do one interface that works nicely and I could duplicate this code using different a timer and a different pin change interrupt for the second interface.
However, what is not at all clear to me is how much code I can share in terms of sending messages in and out of the two interfaces. I will attempt to explain the kind of thing I mean.
The send message is like this, presented here in a simplified manner:
int16_t SendMessage(uint8_t Address, uint8_t Data, uint8_t ReceiveData)
int16_t RetVal; uint8_t Data;
// Here Address and Data variables are turned into a sixteen bit value to send out
// This sixteen bit value is a static variable to this module
DisableReception(); // Disable Rx pin interrpt
// The timer bit-bashes the data and enables the Rx pin interrupt if necessary
// when the sixteen bits have been sent out
if (ReceiveData == TRUE)
if (xQueueReceive(xRxQueue,Data,TIMEOUT_VALUE) == pdTRUE)
RetVal = (int16_t)Data; // Pin int fills xRxQueue
RetVal = NO_DATA_RETURN_REQUESTED;
The xRxQueue is filled by the pin interrupt if a slave is [supposed to be] returning data.
This works a treat for the one interface. I can easily write a version for the second interface, to end up with two functions:
int16_t SendMessageA(uint8_t Address, uint8_t Data, uint8_t ReceiveData);
int16_t SendMessageB(uint8_t Address, uint8_t Data, uint8_t ReceiveData);
The two functions would have their own RxQueue, use their own timer and own pin interrupt of course.
I have a commands.c/commands.h module which calls this SendMessage() function. Here is an example:
uint8_t ProgramShortAddress(uint8_t NewShortAddress)
int16_t Reply; uint8_t RetVal; NewShortAddress <<= 1; NewShortAddress |= 1; Reply = SendMessage(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE); if (Reply == 0xFF) RetVal = TRUE; else RetVal = FALSE; return RetVal;
Now, I would like to mod this and all the other similar commands this so I pass an extra variable in to the function, which specifies which of the two interfaces I am addressing. It would become:
uint8_t ProgramShortAddress(uint8_t NewShortAddress, uint8_t Interface)
// All stays the same inside the function except: if (Interface == INTERFACE_A) Reply = SendMessageA(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE); else if (Interface == INTERFACE_B) Reply = SendMessageB(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE);
So, here is the crux of the matter:
If I have two threads, one running for interface A and one for interface B, one calls ProgramShortAddress(), its time slice ends while in this function and the other interface calls ProgramShortAddress(), is this going to be an issue? It’s not like having an SPI port and locking it out with a mutex as far as I can see, as it’s going to talk to one of two interfaces. There would never be an occasion when two threads would try to access the same interface.
Does this make sense? Have I explained it clearly enough? Am I worrying over nothing or maybe I am tackling this in completely the wrong manner?