Bugfix UARTClass

This commit is contained in:
Cristian Maglie 2011-11-23 23:15:13 +01:00
parent 5c88944e39
commit c8ec267d0b
3 changed files with 50 additions and 62 deletions

View File

@ -23,10 +23,9 @@
// Constructors //////////////////////////////////////////////////////////////// // Constructors ////////////////////////////////////////////////////////////////
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 )
{ {
_rx_buffer = pRx_buffer ; _rx_buffer = pRx_buffer ;
_tx_buffer = pTx_buffer ;
_pUart=pUart ; _pUart=pUart ;
_dwIrq=dwIrq ; _dwIrq=dwIrq ;
@ -37,42 +36,43 @@ UARTClass::UARTClass( Uart* pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* p
void UARTClass::begin( const uint32_t dwBaudRate ) void UARTClass::begin( const uint32_t dwBaudRate )
{ {
/* Configure PMC */ // Configure PMC
PMC_EnablePeripheral( _dwId ) ; PMC_EnablePeripheral( _dwId ) ;
/* Reset and disable receiver & transmitter */ // Disable PDC channel
_pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS ;
/* Configure mode */
_pUart->UART_MR = UART_MR_PAR_NO ;
/* Configure baudrate */
/* Asynchronous, no oversampling */
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4 ;
/* Disable PDC channel */
_pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS ; _pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS ;
/* Configure the ENDRX interrupt */ // Reset and disable receiver and transmitter
_pUart->UART_IER=UART_IER_ENDRX ; _pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS ;
/* Enable UART interrupt in NVIC */ // Configure mode
// NVIC_EnableIRQ( _dwIrq ) ; _pUart->UART_MR = UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL ;
/* Enable receiver and transmitter */ // Configure baudrate (asynchronous, no oversampling)
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4 ;
// Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF;
_pUart->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;
// Enable UART interrupt in NVIC
NVIC_EnableIRQ(_dwIrq);
// 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 )
{ {
// wait for transmission of outgoing data
//while ( _tx_buffer->_iHead != _tx_buffer->_iTail )
//{
//}
// clear any received data // clear any received data
_rx_buffer->_iHead = _rx_buffer->_iTail ; _rx_buffer->_iHead = _rx_buffer->_iTail ;
// Disable UART interrupt in NVIC
NVIC_DisableIRQ( _dwIrq ) ;
// Wait for any outstanding data to be sent
flush();
PMC_DisablePeripheral( _dwId ) ; PMC_DisablePeripheral( _dwId ) ;
} }
@ -84,55 +84,53 @@ int UARTClass::available( 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 ;
}
else 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 ;
}
else
{
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ; uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
return uc ; return uc ;
}
} }
void UARTClass::flush( void ) void UARTClass::flush( void )
{ {
//while ( _tx_buffer->_iHead != _tx_buffer->_iTail ) // Wait for transmission to complete
//{ while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
//} ;
} }
void UARTClass::write( const uint8_t uc_data ) void UARTClass::write( const uint8_t uc_data )
{ {
/* Check if the transmitter is ready */ // Check if the transmitter is ready
while ( (_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY ) while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
; ;
/* Send character */ // Send character
_pUart->UART_THR = uc_data ; _pUart->UART_THR = uc_data;
} }
void UARTClass::IrqHandler( void ) void UARTClass::IrqHandler( void )
{ {
/* Did we receive data ? */ uint32_t status = _pUart->UART_SR;
if ( (_pUart->UART_IER & UART_IER_ENDRX) == UART_IER_ENDRX )
// Did we receive data ?
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
_rx_buffer->store_char(_pUart->UART_RHR);
// Acknowledge errors
if ((status & UART_SR_OVRE) == UART_SR_OVRE ||
(status & UART_SR_FRAME) == UART_SR_FRAME)
{ {
_rx_buffer->store_char( _pUart->UART_RHR ) ; // TODO: error reporting outside ISR
_pUart->UART_CR |= UART_CR_RSTSTA;
} }
} }

View File

@ -25,7 +25,6 @@ class UARTClass : public HardwareSerial
{ {
protected: protected:
RingBuffer *_rx_buffer ; RingBuffer *_rx_buffer ;
RingBuffer *_tx_buffer ;
protected: protected:
Uart* _pUart ; Uart* _pUart ;
@ -33,7 +32,7 @@ class UARTClass : public HardwareSerial
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 ) ;
void begin( const uint32_t dwBaudRate ) ; void begin( const uint32_t dwBaudRate ) ;
void end( void ) ; void end( void ) ;

View File

@ -265,24 +265,15 @@ extern const PinDescription g_APinDescription[]=
* UART objects * UART objects
*/ */
RingBuffer rx_buffer1 ; RingBuffer rx_buffer1 ;
RingBuffer tx_buffer1 ;
UARTClass Serial( UART, UART_IRQn, ID_UART, &rx_buffer1, &tx_buffer1 ) ; UARTClass Serial( UART, UART_IRQn, ID_UART, &rx_buffer1 ) ;
#ifdef __cplusplus
extern "C" {
#endif
// IT handlers // IT handlers
extern void UART_IrqHandler( void ) void UART_IrqHandler(void)
{ {
Serial.IrqHandler() ; Serial.IrqHandler() ;
} }
#ifdef __cplusplus
}
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/* /*
* USART objects * USART objects