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 )
{
_rx_buffer = pRx_buffer ;
_rx_buffer = pRx_buffer;
_tx_buffer = pTx_buffer;
_pUart=pUart ;
_dwIrq=dwIrq ;
_dwId=dwId ;
_pUart=pUart;
_dwIrq=dwIrq;
_dwId=dwId;
}
// Public Methods //////////////////////////////////////////////////////////////
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 )
{
// Configure PMC
pmc_enable_periph_clk( _dwId ) ;
pmc_enable_periph_clk( _dwId );
// 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
_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
_pUart->UART_MR = config ;
_pUart->UART_MR = config;
// Configure baudrate (asynchronous, no oversampling)
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4 ;
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4;
// Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF;
@ -66,41 +64,41 @@ void UARTClass::begin( const uint32_t dwBaudRate, const uint32_t config )
// Enable UART interrupt in NVIC
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;
_tx_buffer->_iHead = _tx_buffer->_iTail = 0;
// 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 )
{
// clear any received data
_rx_buffer->_iHead = _rx_buffer->_iTail ;
// Clear any received data
_rx_buffer->_iHead = _rx_buffer->_iTail;
// Wait for any outstanding data to be sent
flush();
// 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)
{
NVIC_SetPriority(_dwIrq, priority & 0x0F);
NVIC_SetPriority(_dwIrq, priority & 0x0F);
}
uint32_t UARTClass::getInterruptPriority()
{
return NVIC_GetPriority(_dwIrq);
return NVIC_GetPriority(_dwIrq);
}
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)
@ -114,20 +112,20 @@ int UARTClass::availableForWrite(void)
int UARTClass::peek( void )
{
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 )
{
// if the head isn't ahead of the tail, we don't have any characters
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
return -1 ;
return -1;
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
return uc ;
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
return uc;
}
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
// Wait for transmission to complete
while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
;
;
}
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
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
// If busy we buffer
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
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
_tx_buffer->_iHead = l;
_pUart->UART_IER = UART_IER_TXRDY; //make sure TX interrupt is enabled
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
_tx_buffer->_iHead = l;
// Make sure TX interrupt is enabled
_pUart->UART_IER = UART_IER_TXRDY;
}
else
{
// Send character
_pUart->UART_THR = uc_data ;
// Bypass buffering and send character directly
_pUart->UART_THR = uc_data;
}
return 1;
}
@ -162,28 +164,28 @@ void UARTClass::IrqHandler( void )
{
uint32_t status = _pUart->UART_SR;
// Did we receive data ?
// Did we receive data?
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
_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 (_tx_buffer->_iTail != _tx_buffer->_iHead) { //just in case
_pUart->UART_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail];
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
}
else
{
_pUart->UART_IDR = UART_IDR_TXRDY; //mask off transmit interrupt so we don't get it anymore
}
if (_tx_buffer->_iTail != _tx_buffer->_iHead) {
_pUart->UART_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail];
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
}
else
{
// Mask off transmit interrupt so we don't get it anymore
_pUart->UART_IDR = UART_IDR_TXRDY;
}
}
// Acknowledge errors
if ((status & UART_SR_OVRE) == UART_SR_OVRE ||
(status & UART_SR_FRAME) == UART_SR_FRAME)
if ((status & UART_SR_OVRE) == UART_SR_OVRE || (status & UART_SR_FRAME) == UART_SR_FRAME)
{
// TODO: error reporting outside ISR
// TODO: error reporting outside ISR
_pUart->UART_CR |= UART_CR_RSTSTA;
}
}

View File

@ -28,36 +28,36 @@
class UARTClass : public HardwareSerial
{
protected:
RingBuffer *_rx_buffer ;
RingBuffer *_tx_buffer;
RingBuffer *_rx_buffer;
RingBuffer *_tx_buffer;
protected:
Uart* _pUart ;
IRQn_Type _dwIrq ;
uint32_t _dwId ;
Uart* _pUart;
IRQn_Type _dwIrq;
uint32_t _dwId;
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 , const uint32_t config ) ;
void end( void ) ;
int available( void ) ;
int availableForWrite(void);
int peek( void ) ;
int read( void ) ;
void flush( void ) ;
size_t write( const uint8_t c ) ;
void setInterruptPriority(uint32_t priority);
uint32_t getInterruptPriority();
void begin(const uint32_t dwBaudRate);
void begin(const uint32_t dwBaudRate, const uint32_t config);
void end(void);
int available(void);
int availableForWrite(void);
int peek(void);
int read(void);
void flush(void);
size_t write(const uint8_t c);
void setInterruptPriority(uint32_t priority);
uint32_t getInterruptPriority();
void IrqHandler( void ) ;
void IrqHandler( void );
#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+ */
// virtual void write( const char *str ) ;
// virtual void write( const uint8_t *buffer, size_t size ) ;
// virtual void write( const char *str );
// virtual void write( const uint8_t *buffer, size_t size );
#endif
operator bool() { return true; }; // UART always active

View File

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

View File

@ -59,24 +59,24 @@
class USARTClass : public UARTClass
{
protected:
Usart* _pUsart ;
Usart* _pUsart;
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 , const uint32_t config ) ;
void end( void ) ;
void flush( void ) ;
size_t write( const uint8_t c ) ;
void begin( const uint32_t dwBaudRate );
void begin( const uint32_t dwBaudRate , const uint32_t config );
void end( void );
void flush( void );
size_t write( const uint8_t c );
void IrqHandler( void ) ;
void IrqHandler( void );
#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+ */
// virtual void write( const char *str ) ;
// virtual void write( const uint8_t *buffer, size_t size ) ;
// virtual void write( const char *str );
// virtual void write( const uint8_t *buffer, size_t size );
#endif
operator bool() { return true; }; // USART always active