Skip to content

Commit 80f524b

Browse files
committed
sam: fix code format and indent in UART/USART class
1 parent 766ac46 commit 80f524b

File tree

4 files changed

+95
-92
lines changed

4 files changed

+95
-92
lines changed

cores/arduino/UARTClass.cpp

+53-51
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,37 @@
2525

2626
UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *pRx_buffer, RingBuffer *pTx_buffer )
2727
{
28-
_rx_buffer = pRx_buffer ;
28+
_rx_buffer = pRx_buffer;
2929
_tx_buffer = pTx_buffer;
3030

31-
_pUart=pUart ;
32-
_dwIrq=dwIrq ;
33-
_dwId=dwId ;
31+
_pUart=pUart;
32+
_dwIrq=dwIrq;
33+
_dwId=dwId;
3434
}
3535

3636
// Public Methods //////////////////////////////////////////////////////////////
3737

38-
39-
4038
void UARTClass::begin( const uint32_t dwBaudRate )
4139
{
42-
begin( dwBaudRate, UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL );
40+
begin( dwBaudRate, UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL );
4341
}
4442

4543
void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
4644
{
4745
// Configure PMC
48-
pmc_enable_periph_clk( _dwId ) ;
46+
pmc_enable_periph_clk( _dwId );
4947

5048
// Disable PDC channel
51-
_pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS ;
49+
_pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
5250

5351
// Reset and disable receiver and transmitter
54-
_pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS ;
52+
_pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
5553

5654
// Configure mode
57-
_pUart->UART_MR = config ;
55+
_pUart->UART_MR = config;
5856

5957
// Configure baudrate (asynchronous, no oversampling)
60-
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4 ;
58+
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4;
6159

6260
// Configure interrupts
6361
_pUart->UART_IDR = 0xFFFFFFFF;
@@ -66,41 +64,41 @@ void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
6664
// Enable UART interrupt in NVIC
6765
NVIC_EnableIRQ(_dwIrq);
6866

69-
//make sure both ring buffers are initialized back to empty.
67+
// Make sure both ring buffers are initialized back to empty.
7068
_rx_buffer->_iHead = _rx_buffer->_iTail = 0;
7169
_tx_buffer->_iHead = _tx_buffer->_iTail = 0;
7270

7371
// Enable receiver and transmitter
74-
_pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN ;
72+
_pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
7573
}
7674

7775
void UARTClass::end( void )
7876
{
79-
// clear any received data
80-
_rx_buffer->_iHead = _rx_buffer->_iTail ;
77+
// Clear any received data
78+
_rx_buffer->_iHead = _rx_buffer->_iTail;
8179

8280
// Wait for any outstanding data to be sent
8381
flush();
8482

8583
// Disable UART interrupt in NVIC
86-
NVIC_DisableIRQ( _dwIrq ) ;
84+
NVIC_DisableIRQ( _dwIrq );
8785

88-
pmc_disable_periph_clk( _dwId ) ;
86+
pmc_disable_periph_clk( _dwId );
8987
}
9088

9189
void UARTClass::setInterruptPriority(uint32_t priority)
9290
{
93-
NVIC_SetPriority(_dwIrq, priority & 0x0F);
91+
NVIC_SetPriority(_dwIrq, priority & 0x0F);
9492
}
9593

9694
uint32_t UARTClass::getInterruptPriority()
9795
{
98-
return NVIC_GetPriority(_dwIrq);
96+
return NVIC_GetPriority(_dwIrq);
9997
}
10098

10199
int UARTClass::available( void )
102100
{
103-
return (uint32_t)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE ;
101+
return (uint32_t)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE;
104102
}
105103

106104
int UARTClass::availableForWrite(void)
@@ -114,46 +112,50 @@ int UARTClass::availableForWrite(void)
114112
int UARTClass::peek( void )
115113
{
116114
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
117-
return -1 ;
115+
return -1;
118116

119-
return _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
117+
return _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
120118
}
121119

122120
int UARTClass::read( void )
123121
{
124122
// if the head isn't ahead of the tail, we don't have any characters
125123
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
126-
return -1 ;
124+
return -1;
127125

128-
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
129-
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
130-
return uc ;
126+
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
127+
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
128+
return uc;
131129
}
132130

133131
void UARTClass::flush( void )
134132
{
135133
while (_tx_buffer->_iHead != _tx_buffer->_iTail); //wait for transmit data to be sent
136134
// Wait for transmission to complete
137135
while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
138-
;
136+
;
139137
}
140138

141139
size_t UARTClass::write( const uint8_t uc_data )
142140
{
143-
if (((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY) | (_tx_buffer->_iTail != _tx_buffer->_iHead)) //is the hardware currently busy?
141+
// Is the hardware currently busy?
142+
if (((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY) |
143+
(_tx_buffer->_iTail != _tx_buffer->_iHead))
144144
{
145-
//if busy we buffer
146-
unsigned int l = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE;
147-
while (_tx_buffer->_iTail == l); //spin locks if we're about to overwrite the buffer. This continues once the data is sent
148-
149-
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
150-
_tx_buffer->_iHead = l;
151-
_pUart->UART_IER = UART_IER_TXRDY; //make sure TX interrupt is enabled
145+
// If busy we buffer
146+
unsigned int l = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE;
147+
while (_tx_buffer->_iTail == l)
148+
; // Spin locks if we're about to overwrite the buffer. This continues once the data is sent
149+
150+
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
151+
_tx_buffer->_iHead = l;
152+
// Make sure TX interrupt is enabled
153+
_pUart->UART_IER = UART_IER_TXRDY;
152154
}
153155
else
154156
{
155-
// Send character
156-
_pUart->UART_THR = uc_data ;
157+
// Bypass buffering and send character directly
158+
_pUart->UART_THR = uc_data;
157159
}
158160
return 1;
159161
}
@@ -162,28 +164,28 @@ void UARTClass::IrqHandler( void )
162164
{
163165
uint32_t status = _pUart->UART_SR;
164166

165-
// Did we receive data ?
167+
// Did we receive data?
166168
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
167169
_rx_buffer->store_char(_pUart->UART_RHR);
168170

169-
//Do we need to keep sending data?
171+
// Do we need to keep sending data?
170172
if ((status & UART_SR_TXRDY) == UART_SR_TXRDY)
171173
{
172-
if (_tx_buffer->_iTail != _tx_buffer->_iHead) { //just in case
173-
_pUart->UART_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail];
174-
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
175-
}
176-
else
177-
{
178-
_pUart->UART_IDR = UART_IDR_TXRDY; //mask off transmit interrupt so we don't get it anymore
179-
}
174+
if (_tx_buffer->_iTail != _tx_buffer->_iHead) {
175+
_pUart->UART_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail];
176+
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
177+
}
178+
else
179+
{
180+
// Mask off transmit interrupt so we don't get it anymore
181+
_pUart->UART_IDR = UART_IDR_TXRDY;
182+
}
180183
}
181184

182185
// Acknowledge errors
183-
if ((status & UART_SR_OVRE) == UART_SR_OVRE ||
184-
(status & UART_SR_FRAME) == UART_SR_FRAME)
186+
if ((status & UART_SR_OVRE) == UART_SR_OVRE || (status & UART_SR_FRAME) == UART_SR_FRAME)
185187
{
186-
// TODO: error reporting outside ISR
188+
// TODO: error reporting outside ISR
187189
_pUart->UART_CR |= UART_CR_RSTSTA;
188190
}
189191
}

cores/arduino/UARTClass.h

+21-21
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,36 @@
2828
class UARTClass : public HardwareSerial
2929
{
3030
protected:
31-
RingBuffer *_rx_buffer ;
32-
RingBuffer *_tx_buffer;
31+
RingBuffer *_rx_buffer;
32+
RingBuffer *_tx_buffer;
3333

3434
protected:
35-
Uart* _pUart ;
36-
IRQn_Type _dwIrq ;
37-
uint32_t _dwId ;
35+
Uart* _pUart;
36+
IRQn_Type _dwIrq;
37+
uint32_t _dwId;
3838

3939
public:
40-
UARTClass( Uart* pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer) ;
40+
UARTClass(Uart* pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer);
4141

42-
void begin( const uint32_t dwBaudRate ) ;
43-
void begin( const uint32_t dwBaudRate , const uint32_t config ) ;
44-
void end( void ) ;
45-
int available( void ) ;
46-
int availableForWrite(void);
47-
int peek( void ) ;
48-
int read( void ) ;
49-
void flush( void ) ;
50-
size_t write( const uint8_t c ) ;
51-
void setInterruptPriority(uint32_t priority);
52-
uint32_t getInterruptPriority();
42+
void begin(const uint32_t dwBaudRate);
43+
void begin(const uint32_t dwBaudRate, const uint32_t config);
44+
void end(void);
45+
int available(void);
46+
int availableForWrite(void);
47+
int peek(void);
48+
int read(void);
49+
void flush(void);
50+
size_t write(const uint8_t c);
51+
void setInterruptPriority(uint32_t priority);
52+
uint32_t getInterruptPriority();
5353

54-
void IrqHandler( void ) ;
54+
void IrqHandler( void );
5555

5656
#if defined __GNUC__ /* GCC CS3 */
57-
using Print::write ; // pull in write(str) and write(buf, size) from Print
57+
using Print::write; // pull in write(str) and write(buf, size) from Print
5858
#elif defined __ICCARM__ /* IAR Ewarm 5.41+ */
59-
// virtual void write( const char *str ) ;
60-
// virtual void write( const uint8_t *buffer, size_t size ) ;
59+
// virtual void write( const char *str );
60+
// virtual void write( const uint8_t *buffer, size_t size );
6161
#endif
6262

6363
operator bool() { return true; }; // UART always active

cores/arduino/USARTClass.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,42 @@
2323

2424
// Constructors ////////////////////////////////////////////////////////////////
2525

26-
USARTClass::USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer ) : UARTClass((Uart*)pUsart, dwIrq, dwId, pRx_buffer, pTx_buffer)
26+
USARTClass::USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer )
27+
: UARTClass((Uart*)pUsart, dwIrq, dwId, pRx_buffer, pTx_buffer)
2728
{
28-
29-
_pUsart=pUsart ; //In case anyone needs USART specific functionality in the future
29+
// In case anyone needs USART specific functionality in the future
30+
_pUsart=pUsart;
3031
}
3132

3233
// Public Methods //////////////////////////////////////////////////////////////
3334

3435
void USARTClass::begin( const uint32_t dwBaudRate )
3536
{
36-
begin( dwBaudRate, SERIAL_8N1 );
37+
begin( dwBaudRate, SERIAL_8N1 );
3738
}
3839

3940
void USARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
4041
{
41-
UARTClass::begin(dwBaudRate, config);
42+
UARTClass::begin(dwBaudRate, config);
4243
}
4344

4445
void USARTClass::end( void )
4546
{
46-
UARTClass::end();
47+
UARTClass::end();
4748
}
4849

4950
void USARTClass::flush( void )
5051
{
51-
UARTClass::flush();
52+
UARTClass::flush();
5253
}
5354

5455
size_t USARTClass::write( const uint8_t uc_data )
5556
{
56-
return UARTClass::write(uc_data);
57+
return UARTClass::write(uc_data);
5758
}
5859

5960
void USARTClass::IrqHandler( void )
6061
{
61-
UARTClass::IrqHandler();
62+
UARTClass::IrqHandler();
6263
}
6364

cores/arduino/USARTClass.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,24 @@
5959
class USARTClass : public UARTClass
6060
{
6161
protected:
62-
Usart* _pUsart ;
62+
Usart* _pUsart;
6363

6464
public:
65-
USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer ) ;
65+
USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer );
6666

67-
void begin( const uint32_t dwBaudRate ) ;
68-
void begin( const uint32_t dwBaudRate , const uint32_t config ) ;
69-
void end( void ) ;
70-
void flush( void ) ;
71-
size_t write( const uint8_t c ) ;
67+
void begin( const uint32_t dwBaudRate );
68+
void begin( const uint32_t dwBaudRate , const uint32_t config );
69+
void end( void );
70+
void flush( void );
71+
size_t write( const uint8_t c );
7272

73-
void IrqHandler( void ) ;
73+
void IrqHandler( void );
7474

7575
#if defined __GNUC__ /* GCC CS3 */
76-
using Print::write ; // pull in write(str) and write(buf, size) from Print
76+
using Print::write; // pull in write(str) and write(buf, size) from Print
7777
#elif defined __ICCARM__ /* IAR Ewarm 5.41+ */
78-
// virtual void write( const char *str ) ;
79-
// virtual void write( const uint8_t *buffer, size_t size ) ;
78+
// virtual void write( const char *str );
79+
// virtual void write( const uint8_t *buffer, size_t size );
8080
#endif
8181

8282
operator bool() { return true; }; // USART always active

0 commit comments

Comments
 (0)