1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-30 16:24:09 +03:00

sam: fix code format and indent in UART/USART class

This commit is contained in:
Cristian Maglie
2014-12-29 17:27:43 +01:00
parent cabfd8ed21
commit 16d836108f
4 changed files with 94 additions and 91 deletions

View File

@ -25,39 +25,37 @@
UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *pRx_buffer, RingBuffer *pTx_buffer ) UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *pRx_buffer, RingBuffer *pTx_buffer )
{ {
_rx_buffer = pRx_buffer ; _rx_buffer = pRx_buffer;
_tx_buffer = pTx_buffer; _tx_buffer = pTx_buffer;
_pUart=pUart ; _pUart=pUart;
_dwIrq=dwIrq ; _dwIrq=dwIrq;
_dwId=dwId ; _dwId=dwId;
} }
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
void UARTClass::begin( const uint32_t dwBaudRate ) void UARTClass::begin( const uint32_t dwBaudRate )
{ {
begin( dwBaudRate, UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL ); begin( dwBaudRate, UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL );
} }
void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config ) void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
{ {
// Configure PMC // Configure PMC
pmc_enable_periph_clk( _dwId ) ; pmc_enable_periph_clk( _dwId );
// Disable PDC channel // Disable PDC channel
_pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS ; _pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
// Reset and disable receiver and transmitter // Reset and disable receiver and transmitter
_pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS ; _pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
// Configure mode // Configure mode
_pUart->UART_MR = config ; _pUart->UART_MR = config;
// Configure baudrate (asynchronous, no oversampling) // Configure baudrate (asynchronous, no oversampling)
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4 ; _pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4;
// Configure interrupts // Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF; _pUart->UART_IDR = 0xFFFFFFFF;
@ -66,41 +64,41 @@ void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
// Enable UART interrupt in NVIC // Enable UART interrupt in NVIC
NVIC_EnableIRQ(_dwIrq); NVIC_EnableIRQ(_dwIrq);
//make sure both ring buffers are initialized back to empty. // Make sure both ring buffers are initialized back to empty.
_rx_buffer->_iHead = _rx_buffer->_iTail = 0; _rx_buffer->_iHead = _rx_buffer->_iTail = 0;
_tx_buffer->_iHead = _tx_buffer->_iTail = 0; _tx_buffer->_iHead = _tx_buffer->_iTail = 0;
// Enable receiver and transmitter // Enable receiver and transmitter
_pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN ; _pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
} }
void UARTClass::end( void ) void UARTClass::end( void )
{ {
// clear any received data // Clear any received data
_rx_buffer->_iHead = _rx_buffer->_iTail ; _rx_buffer->_iHead = _rx_buffer->_iTail;
// Wait for any outstanding data to be sent // Wait for any outstanding data to be sent
flush(); flush();
// Disable UART interrupt in NVIC // Disable UART interrupt in NVIC
NVIC_DisableIRQ( _dwIrq ) ; NVIC_DisableIRQ( _dwIrq );
pmc_disable_periph_clk( _dwId ) ; pmc_disable_periph_clk( _dwId );
} }
void UARTClass::setInterruptPriority(uint32_t priority) void UARTClass::setInterruptPriority(uint32_t priority)
{ {
NVIC_SetPriority(_dwIrq, priority & 0x0F); NVIC_SetPriority(_dwIrq, priority & 0x0F);
} }
uint32_t UARTClass::getInterruptPriority() uint32_t UARTClass::getInterruptPriority()
{ {
return NVIC_GetPriority(_dwIrq); return NVIC_GetPriority(_dwIrq);
} }
int UARTClass::available( void ) int UARTClass::available( void )
{ {
return (uint32_t)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE ; return (uint32_t)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE;
} }
int UARTClass::availableForWrite(void) int UARTClass::availableForWrite(void)
@ -114,20 +112,20 @@ int UARTClass::availableForWrite(void)
int UARTClass::peek( void ) int UARTClass::peek( void )
{ {
if ( _rx_buffer->_iHead == _rx_buffer->_iTail ) if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
return -1 ; return -1;
return _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ; return _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
} }
int UARTClass::read( void ) int UARTClass::read( void )
{ {
// if the head isn't ahead of the tail, we don't have any characters // if the head isn't ahead of the tail, we don't have any characters
if ( _rx_buffer->_iHead == _rx_buffer->_iTail ) if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
return -1 ; return -1;
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ; uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ; _rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
return uc ; return uc;
} }
void UARTClass::flush( void ) void UARTClass::flush( void )
@ -135,25 +133,29 @@ void UARTClass::flush( void )
while (_tx_buffer->_iHead != _tx_buffer->_iTail); //wait for transmit data to be sent while (_tx_buffer->_iHead != _tx_buffer->_iTail); //wait for transmit data to be sent
// Wait for transmission to complete // Wait for transmission to complete
while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY) while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
; ;
} }
size_t UARTClass::write( const uint8_t uc_data ) size_t UARTClass::write( const uint8_t uc_data )
{ {
if (((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY) | (_tx_buffer->_iTail != _tx_buffer->_iHead)) //is the hardware currently busy? // Is the hardware currently busy?
if (((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY) |
(_tx_buffer->_iTail != _tx_buffer->_iHead))
{ {
//if busy we buffer // If busy we buffer
unsigned int l = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE; unsigned int l = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE;
while (_tx_buffer->_iTail == l); //spin locks if we're about to overwrite the buffer. This continues once the data is sent while (_tx_buffer->_iTail == l)
; // Spin locks if we're about to overwrite the buffer. This continues once the data is sent
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data; _tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
_tx_buffer->_iHead = l; _tx_buffer->_iHead = l;
_pUart->UART_IER = UART_IER_TXRDY; //make sure TX interrupt is enabled // Make sure TX interrupt is enabled
_pUart->UART_IER = UART_IER_TXRDY;
} }
else else
{ {
// Send character // Bypass buffering and send character directly
_pUart->UART_THR = uc_data ; _pUart->UART_THR = uc_data;
} }
return 1; return 1;
} }
@ -162,28 +164,28 @@ void UARTClass::IrqHandler( void )
{ {
uint32_t status = _pUart->UART_SR; uint32_t status = _pUart->UART_SR;
// Did we receive data ? // Did we receive data?
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY) if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
_rx_buffer->store_char(_pUart->UART_RHR); _rx_buffer->store_char(_pUart->UART_RHR);
//Do we need to keep sending data? // Do we need to keep sending data?
if ((status & UART_SR_TXRDY) == UART_SR_TXRDY) if ((status & UART_SR_TXRDY) == UART_SR_TXRDY)
{ {
if (_tx_buffer->_iTail != _tx_buffer->_iHead) { //just in case if (_tx_buffer->_iTail != _tx_buffer->_iHead) {
_pUart->UART_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail]; _pUart->UART_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail];
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE; _tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
} }
else else
{ {
_pUart->UART_IDR = UART_IDR_TXRDY; //mask off transmit interrupt so we don't get it anymore // Mask off transmit interrupt so we don't get it anymore
} _pUart->UART_IDR = UART_IDR_TXRDY;
}
} }
// Acknowledge errors // Acknowledge errors
if ((status & UART_SR_OVRE) == UART_SR_OVRE || if ((status & UART_SR_OVRE) == UART_SR_OVRE || (status & UART_SR_FRAME) == UART_SR_FRAME)
(status & UART_SR_FRAME) == UART_SR_FRAME)
{ {
// TODO: error reporting outside ISR // TODO: error reporting outside ISR
_pUart->UART_CR |= UART_CR_RSTSTA; _pUart->UART_CR |= UART_CR_RSTSTA;
} }
} }

View File

@ -28,36 +28,36 @@
class UARTClass : public HardwareSerial class UARTClass : public HardwareSerial
{ {
protected: protected:
RingBuffer *_rx_buffer ; RingBuffer *_rx_buffer;
RingBuffer *_tx_buffer; RingBuffer *_tx_buffer;
protected: protected:
Uart* _pUart ; Uart* _pUart;
IRQn_Type _dwIrq ; IRQn_Type _dwIrq;
uint32_t _dwId ; uint32_t _dwId;
public: public:
UARTClass( Uart* pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer) ; UARTClass(Uart* pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer);
void begin( const uint32_t dwBaudRate ) ; void begin(const uint32_t dwBaudRate);
void begin( const uint32_t dwBaudRate , const uint32_t config ) ; void begin(const uint32_t dwBaudRate, const uint32_t config);
void end( void ) ; void end(void);
int available( void ) ; int available(void);
int availableForWrite(void); int availableForWrite(void);
int peek( void ) ; int peek(void);
int read( void ) ; int read(void);
void flush( void ) ; void flush(void);
size_t write( const uint8_t c ) ; size_t write(const uint8_t c);
void setInterruptPriority(uint32_t priority); void setInterruptPriority(uint32_t priority);
uint32_t getInterruptPriority(); uint32_t getInterruptPriority();
void IrqHandler( void ) ; void IrqHandler( void );
#if defined __GNUC__ /* GCC CS3 */ #if defined __GNUC__ /* GCC CS3 */
using Print::write ; // pull in write(str) and write(buf, size) from Print using Print::write; // pull in write(str) and write(buf, size) from Print
#elif defined __ICCARM__ /* IAR Ewarm 5.41+ */ #elif defined __ICCARM__ /* IAR Ewarm 5.41+ */
// virtual void write( const char *str ) ; // virtual void write( const char *str );
// virtual void write( const uint8_t *buffer, size_t size ) ; // virtual void write( const uint8_t *buffer, size_t size );
#endif #endif
operator bool() { return true; }; // UART always active operator bool() { return true; }; // UART always active

View File

@ -23,41 +23,42 @@
// Constructors //////////////////////////////////////////////////////////////// // Constructors ////////////////////////////////////////////////////////////////
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) 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)
{ {
// In case anyone needs USART specific functionality in the future
_pUsart=pUsart ; //In case anyone needs USART specific functionality in the future _pUsart=pUsart;
} }
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
void USARTClass::begin( const uint32_t dwBaudRate ) void USARTClass::begin( const uint32_t dwBaudRate )
{ {
begin( dwBaudRate, SERIAL_8N1 ); begin( dwBaudRate, SERIAL_8N1 );
} }
void USARTClass::begin( const uint32_t dwBaudRate, const uint32_t config ) void USARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
{ {
UARTClass::begin(dwBaudRate, config); UARTClass::begin(dwBaudRate, config);
} }
void USARTClass::end( void ) void USARTClass::end( void )
{ {
UARTClass::end(); UARTClass::end();
} }
void USARTClass::flush( void ) void USARTClass::flush( void )
{ {
UARTClass::flush(); UARTClass::flush();
} }
size_t USARTClass::write( const uint8_t uc_data ) size_t USARTClass::write( const uint8_t uc_data )
{ {
return UARTClass::write(uc_data); return UARTClass::write(uc_data);
} }
void USARTClass::IrqHandler( void ) void USARTClass::IrqHandler( void )
{ {
UARTClass::IrqHandler(); UARTClass::IrqHandler();
} }

View File

@ -59,24 +59,24 @@
class USARTClass : public UARTClass class USARTClass : public UARTClass
{ {
protected: protected:
Usart* _pUsart ; Usart* _pUsart;
public: public:
USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer ) ; USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer );
void begin( const uint32_t dwBaudRate ) ; void begin( const uint32_t dwBaudRate );
void begin( const uint32_t dwBaudRate , const uint32_t config ) ; void begin( const uint32_t dwBaudRate , const uint32_t config );
void end( void ) ; void end( void );
void flush( void ) ; void flush( void );
size_t write( const uint8_t c ) ; size_t write( const uint8_t c );
void IrqHandler( void ) ; void IrqHandler( void );
#if defined __GNUC__ /* GCC CS3 */ #if defined __GNUC__ /* GCC CS3 */
using Print::write ; // pull in write(str) and write(buf, size) from Print using Print::write; // pull in write(str) and write(buf, size) from Print
#elif defined __ICCARM__ /* IAR Ewarm 5.41+ */ #elif defined __ICCARM__ /* IAR Ewarm 5.41+ */
// virtual void write( const char *str ) ; // virtual void write( const char *str );
// virtual void write( const uint8_t *buffer, size_t size ) ; // virtual void write( const uint8_t *buffer, size_t size );
#endif #endif
operator bool() { return true; }; // USART always active operator bool() { return true; }; // USART always active