I have two projects where I am in the need of hardware flow control. One is a DNC drip feeder to read a program off an SD card and drip feed a CNC. Yes, I know I can buy one but that's no fun:). The second is to interface with an on older wx station (rain gauge).
I have read what I could and tried to understand as best I can so far. As I understand it, it is very feasible to have full hardware flow control on the SAMD21 with a little tweaking. https://forum.arduino.cc/index.php?topic=543949.0
In reading here about creating new uarts on the SERCOM, it looks like we need to modify the variant.h and variant.cpp
OK great, so it looks possible.
Looking at the Zero, I am thinking of this:
PA04 Sercom 0 pad 0 tx
PA05 Sercom 0 pad 1 rx
PA10 Sercom 0 pad 2 cts
PA11 Sercom 0 pad 3 rts
All of those pins are exposed on the Zero right? Great!
Now, here's where I get hung up:
Do I have to modify the Sercom.h to allow this change? I didn't want to go around playing in things I don't need or want to.
Newby question but what does 0x0ul and 0ul in regards to pins in variant.h mean?
Let me know if I am barking up the right tree or if someone has done this before?
That did help. What I found by looking into the macros, setting it up is fairly easy once you sort of have a handle on what's going on (I am still confused but getting better). See example code below.
NOW, I do have one issue and that is the CTS logic is backwards. If the CTS pin state is LOW, it will send data. If it is HIGH, it does not. I am assuming this logic is because of in instance where RS232 levels would be inverted because of a MAX232 IC. Seeings I am TTL level, I need to have the logic inverted for the CTS in the code. Anyone else can help with that?
/*
Setup for hardware flow control (RTS/CTS) on SAMD21 boards
On SAMD21 SparkFun breakout board
D0 is CTS
D1 is RTS
D3 is RX
D4 is TX
Does a loop back to the serial monitor. Jumper pins D3 and D4. You can toggle the RTS and CTS states manually to test desired flow control.
*/
#include <Arduino.h>
#include "wiring_private.h" // pinPeripheral() function
Uart mySerial (&sercom0, 4, 3, SERCOM_RX_PAD_1, UART_TX_RTS_CTS_PAD_0_2_3, 0, 1 ); // Create the new UART instance assigning pins to the functions necessary.
void setup() {
// initialize both serial ports:
Serial.begin(115200); // start USB serial monitor on SAMD21 boards
mySerial.begin(115200); // Start our Serial port with flow control
pinPeripheral(3, PIO_SERCOM);
pinPeripheral(4, PIO_SERCOM);
pinPeripheral(0, PIO_SERCOM);
pinPeripheral(1, PIO_SERCOM);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Startup");
}
void loop() {
if (Serial.available()) {
int inByte = Serial.read();
mySerial.write(inByte);
}
if (mySerial.available()) {
int inByte = mySerial.read();
Serial.write(inByte);
}
}
void SERCOM0_Handler()
{
mySerial.IrqHandler();
}
my wiring was TTL. MKR Zero with Wemos D1 mini with AT firmware.
see SAMD21G datasheet section 25.6.3.2 Hardware Handshaking:
The receiver drives its RTS pin high when disabled, or when the receive FIFO is full. This indicates to the remote device that it must stop transmitting after the ongoing transmission is complete.
The current CTS level is available in the STATUS register (STATUS.CTS). Character transmission will only start if CTS is low. When CTS goes high, the transmitter will stop transmitting after the ongoing transmission is complete
note that the Arduino library doesn't use the RTS support of SERCOM. RTS is implemented in Arduino core on the software RX buffer logic. (so it can use any pin)
Thank you for your thorough explanation. In my previous post, I had the RTS/CTS mixed up which is a very rookie mistake and why I was seeing the pin states abnormal (I think). With your help earlier, and your explanation I have it running now. I think I learned a lot today. Thank you and have a great day.
Thank you for your thorough explanation. In my previous post, I had the RTS/CTS mixed up which is a very rookie mistake and why I was seeing the pin states abnormal (I think). With your help earlier, and your explanation I have it running now. I think I learned a lot today. Thank you and have a great day.
CTS is input signal "Clear to send?"
RTS is output signal "Ready to receive"
if one end TX is "Clear to send" only if the other end RX is "Ready to receive"