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