diff --git a/cpu/cc2538/dev/uart.c b/cpu/cc2538/dev/uart.c index 2354e565c..1ab2f81f1 100644 --- a/cpu/cc2538/dev/uart.c +++ b/cpu/cc2538/dev/uart.c @@ -92,7 +92,7 @@ reset(void) uint32_t lchr; /* Make sure the UART is disabled before trying to configure it */ - REG(UART_BASE | UART_CTL) = UART_CTL_TXE | UART_CTL_RXE; + REG(UART_BASE | UART_CTL) = UART_CTL_VALUE; /* Clear error status */ REG(UART_BASE | UART_ECR) = 0xFF; @@ -165,11 +165,10 @@ uart_init(void) UART_IFLS_RXIFLSEL_1_8 | UART_IFLS_TXIFLSEL_1_2; /* Make sure the UART is disabled before trying to configure it */ - REG(UART_BASE | UART_CTL) = UART_CTL_TXE | UART_CTL_RXE; + REG(UART_BASE | UART_CTL) = UART_CTL_VALUE; /* Baud Rate Generation */ - REG(UART_BASE | UART_IBRD) = UART_CONF_IBRD; - REG(UART_BASE | UART_FBRD) = UART_CONF_FBRD; + uart_set_baudrate(UART_CONF_BAUD_RATE); /* UART Control: 8N1 with FIFOs */ REG(UART_BASE | UART_LCRH) = UART_LCRH_WLEN_8 | UART_LCRH_FEN; diff --git a/cpu/cc2538/dev/uart.h b/cpu/cc2538/dev/uart.h index 8ae5dc486..fbc18fc18 100644 --- a/cpu/cc2538/dev/uart.h +++ b/cpu/cc2538/dev/uart.h @@ -65,50 +65,24 @@ * \name Baud rate defines * * Used in uart_init() to set the values of UART_IBRD and UART_FBRD in order to - * achieve some standard baud rates. These defines assume that the UART is - * clocked at 16MHz and that Clock Div is 16 (UART_CTL:HSE clear) + * achieve some standard baud rates. * @{ */ -#define UART_IBRD_9600 104 /**< IBRD value for baud rate 9600 */ -#define UART_FBRD_9600 11 /**< FBRD value for baud rate 9600 */ -#define UART_IBRD_38400 26 /**< IBRD value for baud rate 38400 */ -#define UART_FBRD_38400 3 /**< FBRD value for baud rate 38400 */ -#define UART_IBRD_57600 17 /**< IBRD value for baud rate 57600 */ -#define UART_FBRD_57600 24 /**< FBRD value for baud rate 57600 */ -#define UART_IBRD_115200 8 /**< IBRD value for baud rate 115200 */ -#define UART_FBRD_115200 44 /**< FBRD value for baud rate 115200 */ -#define UART_IBRD_230400 4 /**< IBRD value for baud rate 230400 */ -#define UART_FBRD_230400 22 /**< FBRD value for baud rate 230400 */ -#define UART_IBRD_460800 2 /**< IBRD value for baud rate 460800 */ -#define UART_FBRD_460800 11 /**< FBRD value for baud rate 460800 */ +#define UART_CLOCK_RATE 16000000 /* 16 MHz */ +#define UART_CTL_HSE_VALUE 0 +#define UART_CTL_VALUE ( UART_CTL_RXE | UART_CTL_TXE | (UART_CTL_HSE_VALUE << 5) ) + +/* DIV_ROUND() divides integers while avoiding a rounding error: */ +#define DIV_ROUND(num, denom) ( ((num) + (denom) / 2) / (denom) ) + +#define BAUD2BRD(baud) DIV_ROUND(UART_CLOCK_RATE << (UART_CTL_HSE_VALUE + 2), (baud)) + +#define uart_set_baudrate(baud) do { \ + REG(UART_BASE | UART_IBRD) = BAUD2BRD(baud) >> 6; \ + REG(UART_BASE | UART_FBRD) = BAUD2BRD(baud) & 0x3f; \ + REG(UART_BASE | UART_LCRH) = REG(UART_BASE | UART_LCRH); \ +} while(0) -#if UART_CONF_BAUD_RATE==9600 -#define UART_CONF_IBRD UART_IBRD_9600 -#define UART_CONF_FBRD UART_FBRD_9600 -#elif UART_CONF_BAUD_RATE==38400 -#define UART_CONF_IBRD UART_IBRD_38400 -#define UART_CONF_FBRD UART_FBRD_38400 -#elif UART_CONF_BAUD_RATE==57600 -#define UART_CONF_IBRD UART_IBRD_57600 -#define UART_CONF_FBRD UART_FBRD_57600 -#elif UART_CONF_BAUD_RATE==115200 -#define UART_CONF_IBRD UART_IBRD_115200 -#define UART_CONF_FBRD UART_FBRD_115200 -#elif UART_CONF_BAUD_RATE==230400 -#define UART_CONF_IBRD UART_IBRD_230400 -#define UART_CONF_FBRD UART_FBRD_230400 -#elif UART_CONF_BAUD_RATE==460800 -#define UART_CONF_IBRD UART_IBRD_460800 -#define UART_CONF_FBRD UART_FBRD_460800 -#else /* Bail out with an error unless the user provided custom values */ -#if !(defined UART_CONF_IBRD && defined UART_CONF_FBRD) -#error "UART baud rate misconfigured and custom IBRD/FBRD values not provided" -#error "Check the value of UART_CONF_BAUD_RATE in contiki-conf.h or project-conf.h" -#error "Supported values are 9600, 38400, 57600, 115200, 230400 and 460800." -#error "Alternatively, you can provide custom values for " -#error "UART_CONF_IBRD and UART_CONF_FBRD" -#endif -#endif /** @} */ /*---------------------------------------------------------------------------*/ /** \name UART Register Offsets diff --git a/platform/cc2538dk/README.md b/platform/cc2538dk/README.md index 04941fddd..0a04d6b9a 100644 --- a/platform/cc2538dk/README.md +++ b/platform/cc2538dk/README.md @@ -367,14 +367,6 @@ By default, the CC2538 UART is configured with a baud rate of 115200. It is easy #define UART_CONF_BAUD_RATE 230400 -Currently, this configuration directive only supports values 115200, 230400 and 460800. Custom baud rates can also be achieved by following the steps below: - -* Configure `UART_CONF_BAUD_RATE` with an unsupported value to prevent it from auto-choosing values for IBRD and FBRD. For instance, in your project-conf.h you can do: - - #define UART_CONF_BAUD_RATE 0 - -* Provide custom values for `UART_CONF_IBRD` and `UART_CONF_FBRD` according to the guidelines in the CC2538 User Guide. - RF and USB DMA -------------- Transfers between RAM and the RF and USB will be conducted with DMA. If for whatever reason you wish to disable this, here are the relevant configuration lines.