Improved interface and C standard compatibility.

This commit is contained in:
nvt-se 2007-08-16 20:29:18 +00:00
parent 6983417307
commit 9017949535
2 changed files with 59 additions and 65 deletions

View file

@ -32,19 +32,11 @@ whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.
This implementation was developed by the CST group at the FUB.
For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2007
*/
/**
* @file ScatterWeb.Uart.c
* @addtogroup interfaces
* @brief UART interface
* @author Michael Baar <baar@inf.fu-berlin.de>
* \file UART interface
* \author Michael Baar <baar@inf.fu-berlin.de>
*
* UART switch for RS232 and SPI protocols on UART1 written for
* ScatterWeb MSB boards. Compatible to ScatterWeb EOS,
@ -62,37 +54,36 @@ Berlin, 2007
#define U1ME ME2
#endif
void _uart_configure(unsigned char mode);
void _uart_set_mode(unsigned char mode);
static void uart_configure(unsigned char mode);
static void uart_set_mode(unsigned char mode);
volatile unsigned char uart_mode = UART_MODE_RESET;
volatile unsigned char uart_lockcnt = 0;
static volatile unsigned char uart_mode = UART_MODE_RESET;
static volatile unsigned char uart_lockcnt;
static unsigned char _uart_speed_br0[UART_NUM_MODES];
static unsigned char _uart_speed_br1[UART_NUM_MODES];
static unsigned char _uart_speed_bmn[UART_NUM_MODES];
static fp_uart_handler _uart_handler[UART_NUM_MODES] = {NULL, NULL};
static unsigned char uart_speed_br0[UART_NUM_MODES];
static unsigned char uart_speed_br1[UART_NUM_MODES];
static unsigned char uart_speed_bmn[UART_NUM_MODES];
static fp_uart_handler uart_handler[UART_NUM_MODES];
/*---------------------------------------------------------------------------*/
void
uart_set_speed(unsigned char mode, unsigned char ubr0,
unsigned char ubr1, unsigned char umctl)
{
// store setting
_uart_speed_br0[mode] = ubr0; // baudrate
_uart_speed_br1[mode] = ubr1; // baudrate
_uart_speed_bmn[mode] = umctl; // modulation
uart_speed_br0[mode] = ubr0; // baudrate
uart_speed_br1[mode] = ubr1; // baudrate
uart_speed_bmn[mode] = umctl; // modulation
// reconfigure, if mode active
if (uart_mode == mode)
_uart_configure(mode);
uart_configure(mode);
}
/*---------------------------------------------------------------------------*/
void
uart_set_handler(unsigned char mode, fp_uart_handler fpHandler)
{
// store setting
_uart_handler[mode] = fpHandler;
uart_handler[mode] = fpHandler;
if (mode == uart_mode) {
if (fpHandler == NULL)
IE2 &= ~URXIE1; // Disable USART1 RX interrupt
@ -100,30 +91,28 @@ uart_set_handler(unsigned char mode, fp_uart_handler fpHandler)
IE2 |= URXIE1; // Enable USART1 RX interrupt
}
}
/*---------------------------------------------------------------------------*/
char
int
uart_lock(unsigned char mode)
{
// already locked?
if ((mode != uart_mode) && (uart_lockcnt)) {
return 0;
} else {
// increase lock count
uart_lockcnt++;
// switch mode (if neccessary)
_uart_set_mode(mode);
return 1;
return FALSE;
}
// increase lock count
uart_lockcnt++;
// switch mode (if neccessary)
uart_set_mode(mode);
return TRUE;
}
/*---------------------------------------------------------------------------*/
char
int
uart_unlock(unsigned char mode)
{
/*
Do we wan't strict checking?
if( (uart_lockcnt == 0) || (mode != uart_mode) )
return false;
*/
/* Strict checking. */
if (mode != uart_mode)
return FALSE;
// decrement lock
if (uart_lockcnt > 0) {
@ -131,28 +120,26 @@ uart_unlock(unsigned char mode)
// if no more locks, switch back to default mode
if (uart_lockcnt == 0) {
_uart_set_mode(UART_MODE_DEFAULT);
uart_set_mode(UART_MODE_DEFAULT);
}
return 1;
return TRUE;
}
return 0;
return FALSE;
}
/*---------------------------------------------------------------------------*/
void
_uart_configure(unsigned char mode)
static void
uart_configure(unsigned char mode)
{
_DINT(); // disable interrupts
_DINT();
UART_WAIT_TXDONE();
// wait till all buffered data has been transmitted
// configure
if (mode == UART_MODE_RS232) {
P5OUT |= 0x01;
// unselect SPI
P3SEL |= 0xC0;
// select rs232
// to RS232 mode
// select rs232 to RS232 mode
UCTL1 = SWRST | CHAR; // 8-bit character
UTCTL1 |= SSEL1; // UCLK = MCLK
// activate
@ -170,33 +157,32 @@ _uart_configure(unsigned char mode)
}
// restore speed settings
UBR01 = _uart_speed_br0[mode]; // set baudrate
UBR11 = _uart_speed_br1[mode];
UMCTL1 = _uart_speed_bmn[mode]; // set modulation
UBR01 = uart_speed_br0[mode]; // set baudrate
UBR11 = uart_speed_br1[mode];
UMCTL1 = uart_speed_bmn[mode]; // set modulation
UCTL1 &= ~SWRST; // clear reset flag
_EINT(); // enable interrupts
}
/*---------------------------------------------------------------------------*/
void
_uart_set_mode(unsigned char mode)
static void
uart_set_mode(unsigned char mode)
{
// do nothing if mode already set
if( mode == uart_mode )
if (mode == uart_mode )
return;
IE2 &= ~(URXIE1 | UTXIE1); // disable irq
_uart_configure(mode); // configure uart parameters
uart_configure(mode); // configure uart parameters
uart_mode = mode;
if (_uart_handler[mode] != NULL)
if (uart_handler[mode] != NULL)
IE2 |= URXIE1; // Enable USART1 RX interrupt
}
/*---------------------------------------------------------------------------*/
interrupt(UART1RX_VECTOR)_uart_rx(void)
interrupt(UART1RX_VECTOR) uart_rx(void)
{
fp_uart_handler handler = _uart_handler[uart_mode];
fp_uart_handler handler = uart_handler[uart_mode];
/* Check status register for receive errors. - before reading RXBUF since
it clears the error and interrupt flags */
@ -209,3 +195,9 @@ interrupt(UART1RX_VECTOR)_uart_rx(void)
UART_RX;
}
}
int
uart_get_mode(void)
{
return uart_mode;
}

View file

@ -110,9 +110,11 @@ void uart_set_speed(unsigned char mode, unsigned char ubr0, unsigned char ubr1,
void uart_set_handler(unsigned char mode, fp_uart_handler fpHandler);
char uart_lock(unsigned char mode);
int uart_lock(unsigned char mode);
char uart_unlock(unsigned char mode);
int uart_unlock(unsigned char mode);
int uart_get_mode(void);
#endif /* __UART_H__ */