I like it how Arduino knows which pins to enable for which SPI/USART/TWI/etc..
But I'm using Atmel Studio and I'm writing a program to make it flexible for the user to select which USARTMSPI they want to use to interface with a display via my driver. In order to initialize USART, I need to mark the USART pins as output. That works for my fixed configuration, but there's more combinations of USARTs and multiple pinouts from multiple XMEGAs and XMEGA boards that people work on. It's not always going to be USARTC0 that's available, but to some, it will be USARTC1, to some USARTF1, etc.. So how am I going to predict which pins I need set as output?
USART_t* TermUART = &USARTE0; void termuart_init() { PORTE_OUTSET=1<<3; PORTE_DIRSET=1<<3; TermUART->BAUDCTRLA=17; TermUART->BAUDCTRLB=0; TermUART->CTRLC=USART_CMODE_ASYNCHRONOUS_gc|USART_PMODE_DISABLED_gc|USART_CHSIZE_8BIT_gc; TermUART->CTRLB=USART_RXEN_bm|USART_TXEN_bm; }
Here's my code for the normal USART I use for PC communication. How am I supposed to predict which port's OUTSET and DIRSET needs to be configured for any XMEGA?
Is there an already written code that maps I/O pins to their pin_mask_t-s and PORT_t-s?
Here's how it's done in Arduino (I've removed all the #define-s that aren't of AVR)
if(hwSPI) { // Using hardware SPI SPI.begin(); SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed) SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE0); } else { pinMode(_sclk, OUTPUT); pinMode(_sid , OUTPUT); clkport = portOutputRegister(digitalPinToPort(_sclk)); dataport = portOutputRegister(digitalPinToPort(_sid)); clkpinmask = digitalPinToBitMask(_sclk); datapinmask = digitalPinToBitMask(_sid); *clkport &= ~clkpinmask; *dataport &= ~datapinmask; }