uart: port to STM32F4
This commit is contained in:
parent
aae4009fbe
commit
2249f460e3
28
lib/uart.c
28
lib/uart.c
@ -8,7 +8,6 @@
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdio.h> // standard I/O facilities
|
||||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
@ -25,9 +24,12 @@
|
||||
* @{
|
||||
*/
|
||||
#define UART_ID 1 /**< USART peripheral */
|
||||
#define UART_TX PA9 /**< pin used for USART TX */
|
||||
#define UART_RX PA10 /**< pin used for USART RX */
|
||||
#define UART_AF GPIO_AF7 /**< alternate function for UART pins */
|
||||
/** @} */
|
||||
|
||||
#define UART_BAUDRATE 921600 /**< serial baud rate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */
|
||||
#define UART_BAUDRATE 115200 /**< serial baud rate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */
|
||||
|
||||
/* output ring buffer, indexes, and available memory */
|
||||
static volatile uint8_t tx_buffer[128] = {0}; /**< ring buffer for data to transmit (size must be a power of 2) */
|
||||
@ -36,16 +38,18 @@ static volatile uint16_t tx_used = 0; /**< how much data needs to be transmitted
|
||||
|
||||
void uart_setup(void)
|
||||
{
|
||||
/* enable UART I/O peripheral */
|
||||
rcc_periph_clock_enable(RCC_USART_PORT(UART_ID)); // enable clock for UART port peripheral
|
||||
rcc_periph_clock_enable(RCC_USART(UART_ID)); // enable clock for UART peripheral
|
||||
rcc_periph_reset_pulse(RST_USART(UART_ID)); // reset peripheral
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (UART)
|
||||
gpio_set_mode(USART_TX_PORT(UART_ID), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_TX_PIN(UART_ID)); // setup GPIO pin UART transmit
|
||||
gpio_set_mode(USART_RX_PORT(UART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(UART_ID)); // setup GPIO pin UART receive
|
||||
gpio_set(USART_RX_PORT(UART_ID), USART_RX_PIN(UART_ID)); // pull up to avoid noise when not connected
|
||||
// configure pins
|
||||
rcc_periph_clock_enable(GPIO_RCC(UART_TX)); // enable clock for USART TX pin port peripheral
|
||||
gpio_mode_setup(GPIO_PORT(UART_TX), GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN(UART_TX)); // set TX pin to alternate function
|
||||
gpio_set_output_options(GPIO_PORT(UART_TX), GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO_PIN(UART_TX)); // set TX pin output as push-pull
|
||||
gpio_set_af(GPIO_PORT(UART_TX), UART_AF, GPIO_PIN(UART_TX)); // set alternate function to USART
|
||||
rcc_periph_clock_enable(GPIO_RCC(UART_RX)); // enable clock for USART RX pin port peripheral
|
||||
gpio_mode_setup(GPIO_PORT(UART_RX), GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN(UART_RX)); // set GPIO to alternate function, with pull up to avoid noise in case it is not connected
|
||||
gpio_set_af(GPIO_PORT(UART_RX), UART_AF, GPIO_PIN(UART_RX)); // set alternate function to USART
|
||||
|
||||
/* setup UART parameters */
|
||||
// configure USART
|
||||
rcc_periph_clock_enable(RCC_USART(UART_ID)); // enable clock for USART peripheral
|
||||
rcc_periph_reset_pulse(RST_USART(UART_ID)); // reset peripheral
|
||||
usart_set_baudrate(USART(UART_ID), UART_BAUDRATE);
|
||||
usart_set_databits(USART(UART_ID), 8);
|
||||
usart_set_stopbits(USART(UART_ID), USART_STOPBITS_1);
|
||||
@ -57,7 +61,7 @@ void uart_setup(void)
|
||||
usart_enable_rx_interrupt(USART(UART_ID)); // enable receive interrupt
|
||||
usart_enable(USART(UART_ID)); // enable UART
|
||||
|
||||
/* reset buffer states */
|
||||
// reset buffer states
|
||||
tx_i = 0;
|
||||
tx_used = 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user