Go Down

Topic: void SERCOM2_Handler() (Read 188 times) previous topic - next topic

x-giorgio-x

I have tested the RS485 with this code and it works well.

Code: [Select]
Uart Serial3 (&sercom2, 3, 4, SERCOM_RX_PAD_1, UART_TX_PAD_0);
void SERCOM2_Handler()
{   Serial3.IrqHandler();
// Down the RS485 Tx Enable, when it has finished transmitting
  if (SERCOM2->USART.INTFLAG.bit.TXC)
  {
    res_RS485_TxEn;
    SERCOM2->USART.INTFLAG.bit.TXC = 1;   
    return;
  }
}


But unfortunately, on the PCB, I put the RS485 on Serial1
I believe that "void SERCOM2_Handler()" is used from Arduino IDE
How can I do ?

MartinL

Hi x-giorgio-x,

Serial1 uses SERCOM0 on the Arduino Zero, although this different on the MKR series boards. The Serial1 and interrupt service routine (ISR) for SERCOM0 are declared in the Arduino Zero's "variant.cpp" file.

On my Windows machine this is located at:

C:\Users\Computer\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.6.19\variants\arduino_zero\variant.cpp

The main disadvantage of altering the "variant.cpp" file, is that it will be overwritten each time the Arduino Zero core is updated, currently version 1.6.19.

A possible solution is to create your own custom Arduino core for your board. Although it's somewhat old now, I've detailed how to do this here: https://forum.arduino.cc/index.php?topic=409715.0. The JSON configuration file references to the versions of bossac, openocd and CMSIS might be out of date by now.

x-giorgio-x

To make it easier, can I delete Serial1 and recreate it ?

Or add other type of end transmission interrupt ?

MartinL

#3
Sep 12, 2018, 10:31 am Last Edit: Sep 12, 2018, 10:31 am by MartinL
Yes, it's possible just to comment out or delete the Serial1 declaration and ISR and recreate it in your sketch.

It's also possible declare the function name as a weakly declared linker symbol in the "variant.cpp" file, allowing it to be subsequently overriden in your sketch:

Code: [Select]
void SERCOM0_Handler() __attribute__((weak));
void SERCOM0_Handler()
{
  Serial1.IrqHandler();
}

A custom SERCOM0_Handler() function can now be declared in your sketch.

x-giorgio-x

#4
Sep 12, 2018, 03:02 pm Last Edit: Sep 12, 2018, 03:26 pm by x-giorgio-x
Maybe I did not understand.
This is the code of my sketch, without changing anything in the compiler.
But nothing goes, not even what was going before.

Code: [Select]

#include <Arduino.h>   // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function

#define     res_RS485_TxEn    digitalWrite(A0, LOW);
#define     set_RS485_TxEn    digitalWrite(A0, HIGH);
 
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM1_Handler()  {
  Serial2.IrqHandler();
}
Uart Serial3 (&sercom2, 3, 4, SERCOM_RX_PAD_1, UART_TX_PAD_0);
void SERCOM2_Handler()
{   Serial3.IrqHandler();
}
void SERCOM0_Handler() __attribute__((weak));
void SERCOM0_Handler()
{
  Serial1.IrqHandler();
// Down the RS485 Tx Enable, when it has finished transmitting
  if (SERCOM0->USART.INTFLAG.bit.TXC)
  {
    res_RS485_TxEn;
    SERCOM0->USART.INTFLAG.bit.TXC = 1;   
    return;
  }
}

void setup() {
  Serial.begin(115200);   // ESP01 WiFi
  Serial1.begin(115200);  // RX485
  Serial2.begin(2400);    // M-BUS
  Serial3.begin(115200);  // SIM8000
  SerialUSB.begin(115200);
 
  // Assign pins 10 & 11 SERCOM functionality
  pinPeripheral(10, PIO_SERCOM);
  pinPeripheral(11, PIO_SERCOM);
  // Assign pins 3 & 4 SERCOM functionality
  pinPeripheral(3, PIO_SERCOM_ALT);
  pinPeripheral(4, PIO_SERCOM_ALT);
 
  // Enabling interrupt on TXC trasmiossion complete
  SERCOM0->USART.INTENSET.bit.TXC = 1;
  pinMode(A0, OUTPUT);    // Tx Enable Output Low
  digitalWrite(A0, LOW);
}

MartinL

Hi x-giorgio-x,

The line:

Code: [Select]
void SERCOM0_Handler() __attribute__((weak));
...needs to added above the SERCOM0_Handler() function in the "variant.cpp" file, rather than your sketch.

x-giorgio-x

No, but I meant if there's a way not to change variant.cpp

Delete Serial1 and recreate it ? (Only on sketch)

Or add other type of end transmission interrupt.

MartinL

#7
Sep 12, 2018, 05:56 pm Last Edit: Sep 14, 2018, 08:59 am by MartinL
Quote
No, but I meant if there's a way not to change variant.cpp

Delete Serial1 and recreate it ? (Only on sketch)

Or add other type of end transmission interrupt.
Unfortunately, there's no way of doing that without modifying the "variant.cpp" file.

As Arduino chose not to make the serial handler functions weakly declared linker symbols in the "variant.cpp" file, it's therefore not possible to override these functions in your sketch.

Like I mentioned, the other option is to create and maintain your own board entry and copy of the Arduino core code on the Arduino IDE. It's then possible to make changes to your copy of the "variant.cpp" file without affecting other boards.

Go Up