usart: move to common code.
This commit is contained in:
@@ -248,12 +248,6 @@ usart_init (uint16_t prio, uintptr_t stack_addr, size_t stack_size,
|
||||
chopstx_create (prio, stack_addr, stack_size, usart_main, NULL);
|
||||
}
|
||||
|
||||
struct brr_setting {
|
||||
uint8_t baud_spec;
|
||||
uint32_t brr_value;
|
||||
};
|
||||
#define NUM_BAUD (int)(sizeof (brr_table) / sizeof (struct brr_setting))
|
||||
|
||||
static int (*ss_notify_callback) (uint8_t dev_no, uint16_t notify_bits);
|
||||
|
||||
static struct chx_poll_head *usart_poll[NUM_USART*2];
|
||||
@@ -398,3 +392,37 @@ usart_write (uint8_t dev_no, char *buf, uint16_t buflen)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
usart_config_baud (uint8_t dev_no, uint8_t baud_spec)
|
||||
{
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
uint32_t save_bits;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_BAUD; i++)
|
||||
if (brr_table[i].baud_spec == baud_spec)
|
||||
break;
|
||||
|
||||
if (i >= NUM_BAUD)
|
||||
return -1;
|
||||
|
||||
save_bits = USARTx->CR1 & (USART_CR1_TE | USART_CR1_RE);
|
||||
USARTx->CR1 &= ~(USART_CR1_TE | USART_CR1_RE | USART_CR1_UE);
|
||||
USARTx->BRR = brr_table[i].brr_value;
|
||||
USARTx->CR1 |= (save_bits | USART_CR1_UE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
usart_config_clken (uint8_t dev_no, int on)
|
||||
{
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
|
||||
if (on)
|
||||
USARTx->CR2 |= (1 << 11);
|
||||
else
|
||||
USARTx->CR2 &= ~(1 << 11);
|
||||
}
|
||||
|
||||
@@ -141,9 +141,12 @@ static int handle_intr (struct USART *USARTx, struct rb *rb2a, struct usart_stat
|
||||
static int handle_tx (struct USART *USARTx, struct rb *rb2h, struct usart_stat *stat);
|
||||
static void usart_config_recv_enable (struct USART *USARTx, int on);
|
||||
|
||||
struct brr_setting {
|
||||
uint8_t baud_spec;
|
||||
uint32_t brr_value;
|
||||
};
|
||||
#define NUM_BAUD (int)(sizeof (brr_table) / sizeof (struct brr_setting))
|
||||
|
||||
#include "usart-common.c"
|
||||
|
||||
/* We assume 36MHz f_PCLK */
|
||||
static const struct brr_setting brr_table[] = {
|
||||
{ B600, (3750 << 4)},
|
||||
@@ -165,28 +168,8 @@ static const struct brr_setting brr_table[] = {
|
||||
{ BSCARD20, ( 11 << 4)|10}, /* 193548 */
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
usart_config_baud (uint8_t dev_no, uint8_t baud_spec)
|
||||
{
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
uint32_t save_bits;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_BAUD; i++)
|
||||
if (brr_table[i].baud_spec == baud_spec)
|
||||
break;
|
||||
|
||||
if (i >= NUM_BAUD)
|
||||
return -1;
|
||||
|
||||
save_bits = USARTx->CR1 & (USART_CR1_TE | USART_CR1_RE);
|
||||
USARTx->CR1 &= ~(USART_CR1_TE | USART_CR1_RE);
|
||||
USARTx->BRR = brr_table[i].brr_value;
|
||||
USARTx->CR1 |= save_bits;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "usart-common.c"
|
||||
|
||||
static void
|
||||
usart_config_recv_enable (struct USART *USARTx, int on)
|
||||
{
|
||||
@@ -196,17 +179,6 @@ usart_config_recv_enable (struct USART *USARTx, int on)
|
||||
USARTx->CR1 &= ~USART_CR1_RE;
|
||||
}
|
||||
|
||||
void
|
||||
usart_config_clken (uint8_t dev_no, int on)
|
||||
{
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
|
||||
if (on)
|
||||
USARTx->CR2 |= (1 << 11);
|
||||
else
|
||||
USARTx->CR2 &= ~(1 << 11);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
usart_config (uint8_t dev_no, uint32_t config_bits)
|
||||
@@ -214,8 +186,7 @@ usart_config (uint8_t dev_no, uint32_t config_bits)
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
uint8_t baud_spec = (config_bits & MASK_BAUD);
|
||||
int i;
|
||||
uint32_t cr1_config = (USART_CR1_UE | USART_CR1_RXNEIE
|
||||
| USART_CR1_TE | USART_CR1_RE);
|
||||
uint32_t cr1_config = (USART_CR1_UE | USART_CR1_RXNEIE | USART_CR1_TE);
|
||||
/* TXEIE/TCIE will be enabled when
|
||||
putting char */
|
||||
/* No CTSIE, PEIE, IDLEIE, LBDIE */
|
||||
@@ -266,6 +237,9 @@ usart_config (uint8_t dev_no, uint32_t config_bits)
|
||||
else
|
||||
USARTx->CR3 = 0;
|
||||
|
||||
if (!(config_bits & MASK_MODE))
|
||||
cr1_config |= USART_CR1_RE;
|
||||
|
||||
USARTx->CR1 = cr1_config;
|
||||
|
||||
/* SCEN (smartcard enable) should be set _after_ CR1. */
|
||||
|
||||
@@ -115,10 +115,13 @@ static int handle_intr (struct USART *USARTx, struct rb *rb2a, struct usart_stat
|
||||
static int handle_tx (struct USART *USARTx, struct rb *rb2h, struct usart_stat *stat);
|
||||
static void usart_config_recv_enable (struct USART *USARTx, int on);
|
||||
|
||||
#include "usart-common.c"
|
||||
|
||||
/* We assume 40MHz f_CK */
|
||||
struct brr_setting {
|
||||
uint8_t baud_spec;
|
||||
uint32_t brr_value;
|
||||
};
|
||||
#define NUM_BAUD (int)(sizeof (brr_table) / sizeof (struct brr_setting))
|
||||
|
||||
/* We assume 40MHz f_CK */
|
||||
static const struct brr_setting brr_table[] = {
|
||||
{ B600, 66667 },
|
||||
{ B1200, 33333 },
|
||||
@@ -139,28 +142,8 @@ static const struct brr_setting brr_table[] = {
|
||||
{ BSCARD20, 186 },
|
||||
};
|
||||
|
||||
int
|
||||
usart_config_baud (uint8_t dev_no, uint8_t baud_spec)
|
||||
{
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
uint32_t save_bits;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_BAUD; i++)
|
||||
if (brr_table[i].baud_spec == baud_spec)
|
||||
break;
|
||||
|
||||
if (i >= NUM_BAUD)
|
||||
return -1;
|
||||
|
||||
save_bits = USARTx->CR1 & (USART_CR1_TE | USART_CR1_RE);
|
||||
USARTx->CR1 &= ~(USART_CR1_TE | USART_CR1_RE | USART_CR1_UE);
|
||||
USARTx->BRR = brr_table[i].brr_value;
|
||||
USARTx->CR1 |= (save_bits | USART_CR1_UE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* XXX: not sure if needed */
|
||||
#include "usart-common.c"
|
||||
|
||||
static void
|
||||
usart_config_recv_enable (struct USART *USARTx, int on)
|
||||
{
|
||||
@@ -175,18 +158,6 @@ usart_config_recv_enable (struct USART *USARTx, int on)
|
||||
USARTx->CR1 &= ~USART_CR1_RE;
|
||||
}
|
||||
|
||||
void
|
||||
usart_config_clken (uint8_t dev_no, int on)
|
||||
{
|
||||
struct USART *USARTx = get_usart_dev (dev_no);
|
||||
|
||||
if (on)
|
||||
USARTx->CR2 |= (1 << 11);
|
||||
else
|
||||
USARTx->CR2 &= ~(1 << 11);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
usart_config (uint8_t dev_no, uint32_t config_bits)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user