2011-06-01 01:54:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "USART.h"
|
|
|
|
|
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-09 19:15:21 +02:00
|
|
|
USARTClass::USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, RingBuffer* pTx_buffer )
|
2011-06-01 01:54:47 +02:00
|
|
|
{
|
2011-07-11 01:51:24 +02:00
|
|
|
_rx_buffer = pRx_buffer ;
|
|
|
|
_tx_buffer = pTx_buffer ;
|
2011-06-01 01:54:47 +02:00
|
|
|
|
|
|
|
_pUsart=pUsart ;
|
|
|
|
_dwIrq=dwIrq ;
|
|
|
|
_dwId=dwId ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void USARTClass::begin( const uint32_t dwBaudRate )
|
|
|
|
{
|
2011-07-11 01:51:24 +02:00
|
|
|
/* Configure PMC */
|
2011-06-01 01:54:47 +02:00
|
|
|
PMC_EnablePeripheral( _dwId ) ;
|
|
|
|
|
|
|
|
/* Reset and disable receiver & transmitter */
|
|
|
|
_pUsart->US_CR = US_CR_RSTRX | US_CR_RSTTX | US_CR_RXDIS | US_CR_TXDIS ;
|
|
|
|
|
|
|
|
/* Configure mode */
|
2011-09-09 19:15:21 +02:00
|
|
|
_pUsart->US_MR = US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_NO |
|
2011-06-01 01:54:47 +02:00
|
|
|
US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL;
|
|
|
|
|
|
|
|
/* Configure baudrate */
|
|
|
|
/* Asynchronous, no oversampling */
|
2011-09-09 19:15:21 +02:00
|
|
|
_pUsart->US_BRGR = (SystemCoreClock / dwBaudRate) / 16 ;
|
2011-06-01 01:54:47 +02:00
|
|
|
|
|
|
|
/* Disable PDC channel */
|
|
|
|
_pUsart->US_PTCR = US_PTCR_RXTDIS | US_PTCR_TXTDIS ;
|
|
|
|
|
|
|
|
/* Enable receiver and transmitter */
|
|
|
|
_pUsart->US_CR = US_CR_RXEN | US_CR_TXEN ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void USARTClass::end()
|
|
|
|
{
|
2011-07-11 01:51:24 +02:00
|
|
|
// wait for transmission of outgoing data
|
2011-09-09 19:15:21 +02:00
|
|
|
while ( _tx_buffer->_iHead != _tx_buffer->_iTail )
|
2011-07-11 01:51:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear any received data
|
2011-09-09 19:15:21 +02:00
|
|
|
_rx_buffer->_iHead = _rx_buffer->_iTail ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
|
|
|
PMC_DisablePeripheral( _dwId ) ;
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int USARTClass::available( void )
|
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE ;
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int USARTClass::peek( void )
|
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
|
2011-07-11 01:51:24 +02:00
|
|
|
{
|
|
|
|
return -1 ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
return _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
|
2011-07-11 01:51:24 +02:00
|
|
|
}
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int USARTClass::read( void )
|
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
// if the _iHead isn't ahead of the _iTail, we don't have any characters
|
|
|
|
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
|
2011-07-11 01:51:24 +02:00
|
|
|
{
|
|
|
|
return -1 ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
unsigned char c = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
2011-09-09 19:15:21 +02:00
|
|
|
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
|
|
|
return c ;
|
|
|
|
}
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void USARTClass::flush( void )
|
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
while ( _tx_buffer->_iHead != _tx_buffer->_iTail )
|
2011-07-11 01:51:24 +02:00
|
|
|
{
|
|
|
|
}
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 19:15:21 +02:00
|
|
|
void USARTClass::write( uint8_t uc )
|
2011-06-01 01:54:47 +02:00
|
|
|
{
|
2011-09-09 19:15:21 +02:00
|
|
|
int i = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
|
|
|
// If the output buffer is full, there's nothing for it other than to
|
|
|
|
// wait for the interrupt handler to empty it a bit
|
2011-09-09 19:15:21 +02:00
|
|
|
while ( i == _tx_buffer->_iTail )
|
2011-07-11 01:51:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-09 19:15:21 +02:00
|
|
|
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc ;
|
|
|
|
_tx_buffer->_iHead = i ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
|
|
|
/* Wait for the transmitter to be ready */
|
|
|
|
while ( (_pUsart->US_CSR & US_CSR_TXEMPTY) == 0 ) ;
|
|
|
|
|
|
|
|
/* Send character */
|
2011-09-09 19:15:21 +02:00
|
|
|
_pUsart->US_THR=uc ;
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void USARTClass::IrqHandler( void )
|
|
|
|
{
|
2011-07-11 01:51:24 +02:00
|
|
|
// RX char IT
|
2011-09-09 19:15:21 +02:00
|
|
|
uint8_t uc = _pUsart->US_RHR ;
|
|
|
|
|
|
|
|
_rx_buffer->store_char( uc ) ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
|
|
|
// TX FIFO empty IT
|
2011-09-09 19:15:21 +02:00
|
|
|
if ( _tx_buffer->_iHead == _tx_buffer->_iTail )
|
2011-07-11 01:51:24 +02:00
|
|
|
{
|
|
|
|
// Buffer empty, so disable interrupts
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There is more data in the output buffer. Send the next byte
|
2011-09-09 19:15:21 +02:00
|
|
|
uc = _tx_buffer->_aucBuffer[_tx_buffer->_iTail] ;
|
|
|
|
_tx_buffer->_iTail = (_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
|
2011-07-11 01:51:24 +02:00
|
|
|
|
2011-09-09 19:15:21 +02:00
|
|
|
_pUsart->US_THR = uc ;
|
2011-07-11 01:51:24 +02:00
|
|
|
}
|
2011-06-01 01:54:47 +02:00
|
|
|
}
|
|
|
|
|