If you're using an Arduino.cc's Zero then D2/D5 and D3/D4 make TX/RX sercom pairs, whereas on an Arduino.org's M0/M0 Pro it's D2/D3 and D4/D5. This is because on Arduino.cc's Zero, pins D2 and D4 are swapped.
Assuming that you're using the Arduino.cc's Zero, then D2 and D5 use sercom2, or if you're prepared to forgo the SPI port on sercom4. Pins D3 and D4 use sercom0 or sercom2, however sercom0 is already used by D0 and D1.
The pins you mention read differently in the page reference you linked.
If you look at the bottom of the linked page, you'll find an example that exactly matches your requirement, namely using sercom2 on pins D3 and D4. Here's the code from adafruit's page:
You just need to reverse the pins D2=TX, D5=RX and change the PAD definitions. I've also changed Serial4 to Serial3, although this doesn't really matter:
It's because you're calling the pinPeripheral() function before the Serialx.begin(). Call the begin() function for the serial ports first, then it should work OK.
I'm not sure it is possible as is. if you take a look at HardwareSerial.h, you'll see that the 9N1 config doesn't exist.
You're right, I didn't notice that 9 bits worth of data isn't an option in hardware serial.
Although, it might be possible to disable the UART, set the number of bits to 9 and then restart. I haven't tested it though:
Serial1.begin(115200); // Set-up Serial1 at 115200bps
SERCOM0->USART.CTRLA.reg &= ~SERCOM_USART_CTRLA_ENABLE; // Disable the UART
while(SERCOM0->USART.SYNCBUSY.bit.ENABLE); // Wait for synchronization
SERCOM0->USART.CTRLB.reg |= SERCOM_USART_CTRLB_CHSIZE(1); // Set CHSIZE to 1 = 9 bits per character
SERCOM0->USART.CTRLA.reg |= SERCOM_USART_CTRLA_ENABLE; // Re-enable the UART
while(SERCOM0->USART.SYNCBUSY.bit.ENABLE); // Wait for synchronization
yes, my understanding is that for 9N1, I would have to patch the libraries - those patches exist, but would force me to go back to 1.5.x or I will have to port the patches over. SoftwareSerial is available, just has the dependency on AVR.. Any plans to support ARM targets?
I needed a serial port for output and the only available pin (PA3) didn't have access to a SERCOM, so I gave the M0 library a shot. I can confirm that the SoftwareSerial library works on the Zero and its derivatives.
NB: I ran into an error compiling the example sketch:
In member function 'void SoftwareSerial::setTX(uint8_t)': ../libraries/SoftwareSerial/SoftwareSerial.cpp:169:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile PORT_OUT_Type*' in assignment
_transmitPortRegister = portOutputRegister(port);
^
In member function 'void SoftwareSerial::setRX(uint8_t)':
../libraries/SoftwareSerial/SoftwareSerial.cpp:182:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile PORT_IN_Type*' in assignment
_receivePortRegister = portInputRegister(port);
The fix for this is to change the following lines in SoftwareSerial.cpp:
This allows the arduino.cc IDE to process the library files and then the examples all work fine. I have confirmed it working at 31250 baud for MIDI, I can only imagine that it would also work at other baud rates.
pharaohamps:
I needed a serial port for output and the only available pin (PA3) didn't have access to a SERCOM, so I gave the M0 library a shot. I can confirm that the SoftwareSerial library works on the Zero and its derivatives.
NB: I ran into an error compiling the example sketch:
In member function 'void SoftwareSerial::setTX(uint8_t)': ../libraries/SoftwareSerial/SoftwareSerial.cpp:169:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile PORT_OUT_Type*' in assignment
_transmitPortRegister = portOutputRegister(port);
^
In member function 'void SoftwareSerial::setRX(uint8_t)':
../libraries/SoftwareSerial/SoftwareSerial.cpp:182:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile PORT_IN_Type*' in assignment
_receivePortRegister = portInputRegister(port);
The fix for this is to change the following lines in SoftwareSerial.cpp:
From:
This allows the arduino.cc IDE to process the library files and then the examples all work fine. I have confirmed it working at 31250 baud for MIDI, I can only imagine that it would also work at other baud rates.
Thank you so much for posting the compilation error fix! Works on Arduino Zero for me.