I declare the following template class on my .h file
enum class Hal_uart_id
{
Uart = 0,
Usart0,
Usart1,
Usart2,
Usart3,
UsbUart,
};
template <class T>
class HalUART
{
public:
HalUART(Hal_uart_id uart_id);
private:
Hal_uart_id _uart_id;
T* _p_uart;
}
On my .cpp file I implement the class constructor as follows:
#include "hal_uart_common.h"
#include "Arduino.h"
template <class T>
HalUART<T>::HalUART(Hal_uart_id id)
{
_uart_id = id;
switch (_uart_id)
{
case Hal_uart_id::Uart:
_p_uart = &Serial;
break;
case Hal_uart_id::UsbUart:
_p_uart = &SerialUSB;
break;
case Hal_uart_id::Usart0:
_p_uart = &Serial1;
break;
case Hal_uart_id::Usart1:
_p_uart = &Serial2;
break;
case Hal_uart_id::Usart3:
_p_uart = &Serial3;
break;
default:
break;
}
}
At the end of my .cpp file I instantiate the template class with the USARTClass class
template class HalUART<USARTClass>;
I am getting the following compilation error and I cannot understand why or how to fix it:
src/hal/uart/hal_uart_sam3x.cpp: In instantiation of 'HalUART<T>::HalUART(Hal_uart_id) [with T = USARTClass]':
src/hal/uart/hal_uart_sam3x.cpp:58:16: required from here
src/hal/uart/hal_uart_sam3x.cpp:23:21: error: invalid conversion from 'UARTClass*' to 'USARTClass*' [-fpermissive]
_p_uart = &Serial;
~~~~~~~~^~~~~~~~~
src/hal/uart/hal_uart_sam3x.cpp:26:21: error: cannot convert 'Serial_*' to 'USARTClass*' in assignment
_p_uart = &SerialUSB;
~~~~~~~~^~~~~~~~~~~~
*** [.pio/build/due/src/hal/uart/hal_uart_sam3x.cpp.o] Error 1
These object are defined in the Arduino core
UARTClass Serial;
USARTClass Serial1;
USARTClass Serial2;
USARTClass Serial3;
Serial_ SerialUSB;
For UART/USART class and objects definitions please see: https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/UARTClass.h https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/UARTClass.cpp
https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USARTClass.h https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USARTClass.cpp
https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USB/USBAPI.h https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USB/CDC.cpp