lock wait function from Michael Baar.

This commit is contained in:
nvt-se 2008-03-28 16:06:28 +00:00
parent bd8809d35d
commit 10cabda581
2 changed files with 25 additions and 11 deletions

View file

@ -121,9 +121,10 @@ uart_set_speed(unsigned mode, unsigned ubr0,
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 mode, fp_uart_handler fpHandler) uart_set_handler(unsigned mode, fp_uart_handler fpHandler)
@ -143,7 +144,7 @@ int
uart_lock(unsigned mode) uart_lock(unsigned mode)
{ {
// already locked? // already locked?
if (uart_lockcnt > 0) { if (uart_mode != mode && uart_lockcnt > 0) {
return 0; return 0;
} }
@ -155,16 +156,26 @@ uart_lock(unsigned mode)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
uart_lock_wait(unsigned mode)
{
while (UART_WAIT_LOCK(mode)) {
_NOP();
}
return uart_lock(mode);
}
/*---------------------------------------------------------------------------*/
int
uart_unlock(unsigned mode) uart_unlock(unsigned mode)
{ {
if ((uart_lockcnt == 0) || (mode != uart_mode)) { if ((uart_lockcnt == 0) || (mode != uart_mode)) {
uart_lockcnt = 0;
uart_set_mode(UART_MODE_DEFAULT);
return 0; return 0;
} }
// decrement lock // decrement lock
if (uart_lockcnt > 0) { if (uart_lockcnt > 0) {
uart_lockcnt--; uart_lockcnt--;
// 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);
@ -178,16 +189,18 @@ void
uart_set_mode(unsigned mode) uart_set_mode(unsigned 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
} }
}
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
uart_get_mode(void) uart_get_mode(void)

View file

@ -99,10 +99,11 @@ typedef unsigned int(*fp_uart_handler)(unsigned char);
*/ */
void uart_init(void); void uart_init(void);
void uart_set_speed(unsigned mode, unsigned ubr0, unsigned ubr1, unsigned umctl); void uart_set_speed(unsigned, unsigned, unsigned, unsigned);
void uart_set_handler(unsigned mode, fp_uart_handler fpHandler); void uart_set_handler(unsigned, fp_uart_handler);
int uart_lock(unsigned mode); int uart_lock(unsigned);
int uart_unlock(unsigned mode); int uart_lock_wait(unsigned);
int uart_unlock(unsigned);
void uart_set_mode(unsigned); void uart_set_mode(unsigned);
int uart_get_mode(void); int uart_get_mode(void);