Go Down

Topic: Arduino Zero - SoftwareSerial library (Read 26014 times) previous topic - next topic

MartinL

#30
Jan 17, 2017, 06:13 pm Last Edit: Jan 17, 2017, 06:23 pm by MartinL
Hi AloyseTech,

Quote
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:

Code: [Select]
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

mzayats

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?

MartinL

#32
Jan 17, 2017, 11:31 pm Last Edit: Jan 17, 2017, 11:33 pm by MartinL
Hi mzayats,

I believe that a Software Serial library exists for Arduino.org's M0 PRO, (essentially the same as an Arduino Zero). It was included in Arduino.org's IDE from version 1.7.9 onwards: https://github.com/arduino-org/Arduino/releases.

Not sure if it supports 9 bit data, but might be worth a look?

mzayats

Indeed - https://github.com/arduino-org/Arduino/tree/d110edbbfdf48d1b6ffc116ed1e58e742be4cf6e/hardware/arduino/samd/libraries/SoftwareSerial

Thanks!

olavopm

Hey guys, So using this arduino.org library I can use software serial on a M0?

I am trying to compile a sketch on a adafruit feather M0, I dont understand enough to be able to port a software serial to an actual hardware serial.

Can can I go about using this arduino.org software serial library?

Thank!

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:

Code: [Select]
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:

Code: [Select]
169:  _transmitPortRegister = portOutputRegister(port);
182:  _receivePortRegister = portInputRegister(port);


To:

Code: [Select]
169:  _transmitPortRegister = (volatile PORT_OUT_Type*)portOutputRegister(port);
182:  _receivePortRegister = (volatile PORT_IN_Type*)portInputRegister(port);


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.

Meowple

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:

Code: [Select]
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:

Code: [Select]
169:  _transmitPortRegister = portOutputRegister(port);
182:  _receivePortRegister = portInputRegister(port);


To:

Code: [Select]
169:  _transmitPortRegister = (volatile PORT_OUT_Type*)portOutputRegister(port);
182:  _receivePortRegister = (volatile PORT_IN_Type*)portInputRegister(port);


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.

NorseGod

Thank you for this great write-up!

I just want to chime in with some additional lines of code needed for the Arduino M0 Pro and the Atmel Studio 7.

In order to get sercom0 & sercom2 to work, I had to add these lines:

 pinPeripheral(0, PIO_SERCOM); //Assign RX function to pin 0
 pinPeripheral(1, PIO_SERCOM); //Assign TX function to pin 1

 pinPeripheral(5, PIO_SERCOM); //Assign RX function to pin 5
 pinPeripheral(4, PIO_SERCOM); //Assign TX function to pin 4

Hope someone find that helpful :)

Cheers

Go Up