Skip to content

Commit 5e97168

Browse files
committed
sam: USART modes now fails if used on UART
1 parent 0dbd35a commit 5e97168

File tree

4 files changed

+78
-45
lines changed

4 files changed

+78
-45
lines changed

hardware/arduino/sam/cores/arduino/UARTClass.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *p
3737

3838
void UARTClass::begin( const uint32_t dwBaudRate )
3939
{
40-
begin( dwBaudRate, UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL );
40+
begin(dwBaudRate, Mode_8N1);
4141
}
4242

43-
void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
43+
void UARTClass::begin(const uint32_t dwBaudRate, const UARTModes config)
44+
{
45+
init(dwBaudRate, static_cast<uint32_t>(config));
46+
}
47+
48+
void UARTClass::init(const uint32_t dwBaudRate, const uint32_t config)
4449
{
4550
// Configure PMC
4651
pmc_enable_periph_clk( _dwId );
@@ -52,7 +57,7 @@ void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
5257
_pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
5358

5459
// Configure mode
55-
_pUart->UART_MR = config;
60+
_pUart->UART_MR = config | US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHMODE_NORMAL;
5661

5762
// Configure baudrate (asynchronous, no oversampling)
5863
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4;

hardware/arduino/sam/cores/arduino/UARTClass.h

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,28 @@
2525
// Includes Atmel CMSIS
2626
#include <chip.h>
2727

28+
#define SERIAL_8N1 UARTClass::Mode_8N1
29+
#define SERIAL_8N2 UARTClass::Mode_8N2
30+
#define SERIAL_8E1 UARTClass::Mode_8E1
31+
#define SERIAL_8E2 UARTClass::Mode_8E2
32+
#define SERIAL_8O1 UARTClass::Mode_8O1
33+
#define SERIAL_8O2 UARTClass::Mode_8O2
34+
2835
class UARTClass : public HardwareSerial
2936
{
30-
protected:
31-
RingBuffer *_rx_buffer;
32-
RingBuffer *_tx_buffer;
33-
34-
protected:
35-
Uart* _pUart;
36-
IRQn_Type _dwIrq;
37-
uint32_t _dwId;
38-
3937
public:
38+
enum UARTModes {
39+
Mode_8N1 = US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT,
40+
Mode_8N2 = US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT,
41+
Mode_8E1 = US_MR_CHRL_8_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT,
42+
Mode_8E2 = US_MR_CHRL_8_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT,
43+
Mode_8O1 = US_MR_CHRL_8_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT,
44+
Mode_8O2 = US_MR_CHRL_8_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT,
45+
};
4046
UARTClass(Uart* pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer);
4147

4248
void begin(const uint32_t dwBaudRate);
43-
void begin(const uint32_t dwBaudRate, const uint32_t config);
49+
void begin(const uint32_t dwBaudRate, const UARTModes config);
4450
void end(void);
4551
int available(void);
4652
int availableForWrite(void);
@@ -56,6 +62,17 @@ class UARTClass : public HardwareSerial
5662
void IrqHandler(void);
5763

5864
operator bool() { return true; }; // UART always active
65+
66+
protected:
67+
void init(const uint32_t dwBaudRate, const uint32_t config);
68+
69+
RingBuffer *_rx_buffer;
70+
RingBuffer *_tx_buffer;
71+
72+
Uart* _pUart;
73+
IRQn_Type _dwIrq;
74+
uint32_t _dwId;
75+
5976
};
6077

6178
#endif // _UART_CLASS_

hardware/arduino/sam/cores/arduino/USARTClass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ USARTClass::USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffe
3232

3333
// Public Methods //////////////////////////////////////////////////////////////
3434

35-
void USARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
35+
void USARTClass::begin( const uint32_t dwBaudRate, const USARTModes config )
3636
{
37-
UARTClass::begin(dwBaudRate, config);
37+
UARTClass::init(dwBaudRate, static_cast<uint32_t>(config));
3838
}
3939

hardware/arduino/sam/cores/arduino/USARTClass.h

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,56 @@
2626
#include <chip.h>
2727

2828
// Define config for Serial.begin(baud, config);
29-
#define SERIAL_5N1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_5_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
30-
#define SERIAL_6N1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_6_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
31-
#define SERIAL_7N1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_7_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
32-
#define SERIAL_8N1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
33-
34-
#define SERIAL_5N2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_5_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
35-
#define SERIAL_6N2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_6_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
36-
#define SERIAL_7N2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_7_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
37-
#define SERIAL_8N2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
38-
39-
#define SERIAL_5E1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_5_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
40-
#define SERIAL_6E1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_6_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
41-
#define SERIAL_7E1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_7_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
42-
#define SERIAL_8E1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
43-
44-
#define SERIAL_5E2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_5_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
45-
#define SERIAL_6E2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_6_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
46-
#define SERIAL_7E2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_7_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
47-
#define SERIAL_8E2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
48-
49-
#define SERIAL_5O1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_5_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
50-
#define SERIAL_6O1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_6_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
51-
#define SERIAL_7O1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_7_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
52-
#define SERIAL_8O1 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL)
53-
54-
#define SERIAL_5O2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_5_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
55-
#define SERIAL_6O2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_6_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
56-
#define SERIAL_7O2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_7_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
57-
#define SERIAL_8O2 (US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL)
29+
#define SERIAL_5N1 USARTClass::Mode_5N1
30+
#define SERIAL_6N1 USARTClass::Mode_6N1
31+
#define SERIAL_7N1 USARTClass::Mode_7N1
32+
#define SERIAL_5N2 USARTClass::Mode_5N2
33+
#define SERIAL_6N2 USARTClass::Mode_6N2
34+
#define SERIAL_7N2 USARTClass::Mode_7N2
35+
#define SERIAL_5E1 USARTClass::Mode_5E1
36+
#define SERIAL_6E1 USARTClass::Mode_6E1
37+
#define SERIAL_7E1 USARTClass::Mode_7E1
38+
#define SERIAL_5E2 USARTClass::Mode_5E2
39+
#define SERIAL_6E2 USARTClass::Mode_6E2
40+
#define SERIAL_7E2 USARTClass::Mode_7E2
41+
#define SERIAL_5O1 USARTClass::Mode_5O1
42+
#define SERIAL_6O1 USARTClass::Mode_6O1
43+
#define SERIAL_7O1 USARTClass::Mode_7O1
44+
#define SERIAL_5O2 USARTClass::Mode_5O2
45+
#define SERIAL_6O2 USARTClass::Mode_6O2
46+
#define SERIAL_7O2 USARTClass::Mode_7O2
5847

5948
class USARTClass : public UARTClass
6049
{
6150
protected:
6251
Usart* _pUsart;
6352

6453
public:
54+
// 8 bit modes are inherited from UARTClass
55+
enum USARTModes {
56+
Mode_5N1 = US_MR_CHRL_5_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT,
57+
Mode_6N1 = US_MR_CHRL_6_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT,
58+
Mode_7N1 = US_MR_CHRL_7_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT,
59+
Mode_5N2 = US_MR_CHRL_5_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT,
60+
Mode_6N2 = US_MR_CHRL_6_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT,
61+
Mode_7N2 = US_MR_CHRL_7_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT,
62+
Mode_5E1 = US_MR_CHRL_5_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT,
63+
Mode_6E1 = US_MR_CHRL_6_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT,
64+
Mode_7E1 = US_MR_CHRL_7_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_1_BIT,
65+
Mode_5E2 = US_MR_CHRL_5_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT,
66+
Mode_6E2 = US_MR_CHRL_6_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT,
67+
Mode_7E2 = US_MR_CHRL_7_BIT | US_MR_PAR_EVEN | US_MR_NBSTOP_2_BIT,
68+
Mode_5O1 = US_MR_CHRL_5_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT,
69+
Mode_6O1 = US_MR_CHRL_6_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT,
70+
Mode_7O1 = US_MR_CHRL_7_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_1_BIT,
71+
Mode_5O2 = US_MR_CHRL_5_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT,
72+
Mode_6O2 = US_MR_CHRL_6_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT,
73+
Mode_7O2 = US_MR_CHRL_7_BIT | US_MR_PAR_ODD | US_MR_NBSTOP_2_BIT,
74+
};
75+
6576
USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer );
6677

67-
void begin( const uint32_t dwBaudRate , const uint32_t config );
78+
void begin( const uint32_t dwBaudRate , const USARTModes config );
6879
using UARTClass::begin; // Needed only for polymorphic methods
6980
};
7081

0 commit comments

Comments
 (0)