New fast digital I/O and Software SPI libraries

Can't it just be on me to not make such stuipid errors in the first case?

Yes, but we all make mistakes or forget to allow for boundary conditions or out of range values.

My example cannot be detected at compile time of course but a simple defensive coding is not about supervisory code, it's just applying sanity tests to incoming data. Here's an example from the framework I'm currently working on

	if (pin >= NATIVE_PINS) {
		SYS_ERROR (ERR_INV_PIN);
		return ERROR;
	}

It adds a single test and if there is a run-time problem it's reported with an error mechanism.

That's better than turning on a semi-random pin, maybe one that has a critical purpose. Here's another example

uint32 stringLoadFromArray(string * s, char * c) {

	VERIFY_OBJECT(s, OBJID_STRING)
        // OK, the struct is good we can use it with a reasonable amount of confidence

The function requires a pointer to a "string" structure and most code will simply take if for granted that "s" does in fact point to the right place. The VERIFY_OBJECT macro tests for two special bytes in the structure, if they don't conform to a particular format this is considered to be a fatal error, it is reported and the processor enters a while(1) loop.

This does add overhead that's for sure, and I see this as being a good reason for using the new Due because the overhead will be easily absorbed in the extra speed and memory.

One more for the road

uint32 serialSetUartPins(serialConnection * s, uint32 location) {

	uint8 txPin;
	uint8 rxPin;

	VERIFY_OBJECT (s, ERR_BAD_OBJECT);

	ASSERT_RETERR (location > 3, ERR_SERIAL_BAD_PORT);

	location <<= 1;
	rxPin = __uart_logical_pins[s->port][location];
	txPin = __uart_logical_pins[s->port][location+1];


	ASSERT_RETERR ((rxPin == ERROR) || (txPin == ERROR), ERR_SERIAL_BAD_LOCATION);

	TRY
		pinFunc (rxPin, (s->port == 0) ? FUNC_RXD0 : FUNC_RXD1);
		pinFunc (txPin, (s->port == 0) ? FUNC_TXD0 : FUNC_TXD1);
	CATCH_RETERR

	s->txPin = txPin;
	s->rxPin = rxPin;

	return NOERROR;
}

This function is mostly error checking code, an equivalent Arduino function would probably just write into some registers without question and you spend 3 hours wondering why the serial port doesn't work because you "know" that "location == 2".

Overkill? Maybe but there won't be much get through that shouldn't :slight_smile:

EDIT: After writing all that I saw your last paragraph :slight_smile:


Rob