Make Uart driver universal

This commit is contained in:
Mario Hüttel 2020-02-24 18:50:09 +01:00
parent 4bc85d474f
commit 5012b726cd
6 changed files with 208 additions and 104 deletions

View File

@ -27,6 +27,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <float.h> #include <float.h>
extern struct stm_uart shell_uart;
void calibration_calculate(float low_measured, float low_setpoint, float high_measured, float high_setpoint, void calibration_calculate(float low_measured, float low_setpoint, float high_measured, float high_setpoint,
float *sens_deviation, float *sens_corrected_offset) float *sens_deviation, float *sens_corrected_offset)
{ {
@ -110,7 +112,7 @@ static void wait_for_uart_enter()
int uart_recv_status; int uart_recv_status;
do { do {
uart_recv_status = uart_receive_data_with_dma(&recv_data, &recv_len); uart_recv_status = uart_receive_data_with_dma(&shell_uart, &recv_data, &recv_len);
if (uart_recv_status >= 1) { if (uart_recv_status >= 1) {
for (iter = 0; iter < recv_len; iter++) { for (iter = 0; iter < recv_len; iter++) {
if (recv_data[iter] == '\n' || recv_data[iter] == '\r') if (recv_data[iter] == '\n' || recv_data[iter] == '\r')

View File

@ -18,57 +18,55 @@
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stm32/stm32f4xx.h>
#include <stm-periph/dma-ring-buffer.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#ifndef UART_UART_H_ #ifndef UART_UART_H_
#define UART_UART_H_ #define UART_UART_H_
#define UART_RECEIVE_DMA_STREAM DMA2_Stream5 struct stm_uart {
USART_TypeDef *uart_dev;
uint32_t brr_val;
uint8_t base_dma_num;
DMA_Stream_TypeDef *dma_tx_stream;
uint8_t dma_tx_trigger_channel;
DMA_Stream_TypeDef *dma_rx_stream;
uint8_t dma_rx_trigger_channel;
char *dma_rx_buff;
char *dma_tx_buff;
size_t rx_buff_count;
size_t tx_buff_count;
volatile uint32_t *rcc_reg;
uint8_t rcc_bit_no;
struct dma_ring_buffer_to_mem rx_ring_buff;
struct dma_ring_buffer_to_periph tx_ring_buff;
uint8_t tx : 1;
uint8_t rx : 1;
};
#define UART_SEND_DMA_STREAM DMA2_Stream7 int uart_init(struct stm_uart *uart);
#define UART_PERIPH USART1 void uart_disable(struct stm_uart *uart);
#define UART_RCC_MASK RCC_APB2ENR_USART1EN
#ifdef DEBUGBUILD void uart_send_char(struct stm_uart *uart, char c);
#define UART_PORT GPIOA void uart_send_array(struct stm_uart *uart, const char *data, uint32_t len);
#define UART_PORT_RCC_MASK RCC_AHB1ENR_GPIOAEN
#define UART_RX_PIN 10
#define UART_TX_PIN 9
#define UART_RX_PIN_ALTFUNC 7
#define UART_TX_PIN_ALTFUNC 7
#else
#endif void uart_send_string(struct stm_uart *uart, const char *string);
/* UART_DIV is 45.5625 => 115200 @ 84 MHz */ void uart_send_array_with_dma(struct stm_uart *uart, const char *data, uint32_t len);
#define UART_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */
#define UART_DIV_MANTISSA 45U /* Equals 45 */
#define UART_BRR_REG_VALUE ((UART_DIV_MANTISSA<<4) | UART_DIV_FRACTION); void uart_send_string_with_dma(struct stm_uart *uart, const char *string);
void initUART(); int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t *len);
void sendChar(char c);
void sendString(char* s, int count);
char uart_get_char(struct stm_uart *uart);
void uart_init_with_dma(); int uart_check_rx_avail(struct stm_uart *uart);
void uart_disable(); void uart_tx_dma_complete_int_callback(struct stm_uart *uart);
void uart_send_char(char c);
void uart_send_array(const char *data, uint32_t len);
void uart_send_string(const char *string);
void uart_send_array_with_dma(const char *data, uint32_t len);
void uart_send_string_with_dma(const char *string);
int uart_receive_data_with_dma(const char **data, size_t *len);
#endif /* UART_UART_H_ */ #endif /* UART_UART_H_ */

View File

@ -37,6 +37,8 @@
#include <stm-periph/stm32-gpio-macros.h> #include <stm-periph/stm32-gpio-macros.h>
#include <stm-periph/clock-enable-manager.h> #include <stm-periph/clock-enable-manager.h>
#include <stm-periph/uart.h> #include <stm-periph/uart.h>
#include <reflow-controller/shell-uart-config.h>
#include <helper-macros/helper-macros.h>
static void setup_nvic_priorities() static void setup_nvic_priorities()
{ {
@ -54,6 +56,51 @@ static volatile int pt1000_value_status;
static uint32_t rot; static uint32_t rot;
static inline void uart_gpio_config()
{
#ifdef DEBUGBUILD
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(SHELL_UART_PORT_RCC_MASK));
SHELL_UART_PORT->MODER &= MODER_DELETE(SHELL_UART_TX_PIN) & MODER_DELETE(SHELL_UART_RX_PIN);
SHELL_UART_PORT->MODER |= ALTFUNC(SHELL_UART_RX_PIN) | ALTFUNC(SHELL_UART_TX_PIN);
SETAF(SHELL_UART_PORT, SHELL_UART_RX_PIN, SHELL_UART_RX_PIN_ALTFUNC);
SETAF(SHELL_UART_PORT, SHELL_UART_TX_PIN, SHELL_UART_TX_PIN_ALTFUNC);
#endif
}
static char shell_uart_tx_buff[128];
static char shell_uart_rx_buff[32];
struct stm_uart shell_uart;
static shellmatta_retCode_t write_shell_callback(const char *data, uint32_t len)
{
uart_send_array_with_dma(&shell_uart, data, len);
return SHELLMATTA_OK;
}
static inline void setup_sell_uart(struct stm_uart *uart)
{
uart->rx = 1;
uart->tx = 1;
uart->brr_val = SHELL_UART_BRR_REG_VALUE;
uart->rcc_reg = &SHELL_UART_RCC_REG;
uart->rcc_bit_no = BITMASK_TO_BITNO(SHELL_UART_RCC_MASK);
uart->uart_dev = SHELL_UART_PERIPH;
uart->dma_rx_buff = shell_uart_rx_buff;
uart->dma_tx_buff = shell_uart_tx_buff;
uart->rx_buff_count = sizeof(shell_uart_rx_buff);
uart->tx_buff_count = sizeof(shell_uart_tx_buff);
uart->base_dma_num = 2;
uart->dma_rx_stream = SHELL_UART_RECEIVE_DMA_STREAM;
uart->dma_tx_stream = SHELL_UART_SEND_DMA_STREAM;
uart->dma_rx_trigger_channel = SHELL_UART_RX_DMA_TRIGGER;
uart->dma_tx_trigger_channel = SHELL_UART_TX_DMA_TRIGGER;
uart_init(uart);
NVIC_EnableIRQ(DMA2_Stream7_IRQn);
}
int main() int main()
{ {
const char *uart_input; const char *uart_input;
@ -71,16 +118,27 @@ int main()
loudspeaker_setup(); loudspeaker_setup();
rotary_encoder_setup(); rotary_encoder_setup();
uart_init_with_dma(); uart_gpio_config();
setup_sell_uart(&shell_uart);
shell_handle = shell_init(); shell_handle = shell_init(write_shell_callback);
while (1) { while (1) {
pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value); pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value);
rot = rotary_encoder_get_abs_val(); rot = rotary_encoder_get_abs_val();
uart_receive_status = uart_receive_data_with_dma(&uart_input, &uart_input_len); uart_receive_status = uart_receive_data_with_dma(&shell_uart, &uart_input, &uart_input_len);
if (uart_receive_status >= 1) if (uart_receive_status >= 1)
shell_handle_input(shell_handle, uart_input, uart_input_len); shell_handle_input(shell_handle, uart_input, uart_input_len);
} }
} }
void DMA2_Stream7_IRQHandler()
{
uint32_t hisr = DMA2->HISR;
DMA2->HIFCR = hisr;
if (hisr & DMA_HISR_TCIF7) {
uart_tx_dma_complete_int_callback(&shell_uart);
}
}

View File

@ -41,12 +41,6 @@ static shellmatta_instance_t shell;
static char shell_buffer[512]; static char shell_buffer[512];
static char history_buffer[1024]; static char history_buffer[1024];
static shellmatta_retCode_t write_shell_callback(const char *data, uint32_t len)
{
uart_send_array_with_dma(data, len);
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_ver(const shellmatta_handle_t handle, static shellmatta_retCode_t shell_cmd_ver(const shellmatta_handle_t handle,
const char *arguments, const char *arguments,
uint32_t length) uint32_t length)
@ -324,13 +318,13 @@ static shellmatta_cmd_t cmd[9] = {
} }
}; };
shellmatta_handle_t shell_init(void) shellmatta_handle_t shell_init(shellmatta_write_t write_func)
{ {
shellmatta_handle_t handle; shellmatta_handle_t handle;
shellmatta_retCode_t ret; shellmatta_retCode_t ret;
ret = shellmatta_doInit(&shell, &handle, shell_buffer, sizeof(shell_buffer), history_buffer, sizeof(history_buffer), ret = shellmatta_doInit(&shell, &handle, shell_buffer, sizeof(shell_buffer), history_buffer, sizeof(history_buffer),
"\e[1;32mEnter command:\e[m\r\n", cmd, write_shell_callback); "\e[1;32mEnter command:\e[m\r\n", cmd, write_func);
if (ret != SHELLMATTA_OK) if (ret != SHELLMATTA_OK)
handle = NULL; handle = NULL;

View File

@ -25,99 +25,150 @@
#include <stm-periph/dma-ring-buffer.h> #include <stm-periph/dma-ring-buffer.h>
#include <string.h> #include <string.h>
static struct dma_ring_buffer_to_mem ring_buff_rx; int uart_init(struct stm_uart *uart)
static struct dma_ring_buffer_to_periph ring_buff_tx;
static char uart_rx_buffer[64];
static char uart_tx_buffer[256];
#ifdef DEBUGBUILD
static inline void uart_gpio_config()
{ {
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK)); int ret_val = 0;
UART_PORT->MODER &= MODER_DELETE(UART_TX_PIN) & MODER_DELETE(UART_RX_PIN); uint32_t cr3 = 0;
UART_PORT->MODER |= ALTFUNC(UART_RX_PIN) | ALTFUNC(UART_TX_PIN); uint32_t cr1 = 0;
SETAF(UART_PORT, UART_RX_PIN, UART_RX_PIN_ALTFUNC);
SETAF(UART_PORT, UART_TX_PIN, UART_TX_PIN_ALTFUNC);
}
#endif
void uart_init_with_dma() if (!uart)
{ return -1000;
rcc_manager_enable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK));
#ifdef DEBUGBUILD
uart_gpio_config();
#endif
UART_PERIPH->BRR = UART_BRR_REG_VALUE;
UART_PERIPH->CR3 = USART_CR3_DMAR | USART_CR3_DMAT;
UART_PERIPH->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
dma_ring_buffer_periph_to_mem_initialize(&ring_buff_rx, 2, UART_RECEIVE_DMA_STREAM, sizeof(uart_rx_buffer), 1U, rcc_manager_enable_clock(uart->rcc_reg, uart->rcc_bit_no);
uart_rx_buffer, (char *)&UART_PERIPH->DR, 4);
dma_ring_buffer_mem_to_periph_initialize(&ring_buff_tx, 2, UART_SEND_DMA_STREAM, sizeof(uart_tx_buffer), 1U,
uart_tx_buffer, 4U, (void *)&UART_PERIPH->DR);
NVIC_EnableIRQ(DMA2_Stream7_IRQn); /* Reset all config regs */
uart->uart_dev->CR1 = uart->uart_dev->CR2 = uart->uart_dev->CR3 = 0UL;
/* Set baud rate */
uart->uart_dev->BRR = uart->brr_val;
/* If DMA buffers are present, configure for DMA use */
if (uart->dma_rx_buff && uart->rx) {
cr3 |= USART_CR3_DMAR;
ret_val = dma_ring_buffer_periph_to_mem_initialize(&uart->rx_ring_buff,
uart->base_dma_num,
uart->dma_rx_stream,
uart->rx_buff_count,
1U,
uart->dma_rx_buff,
(char *)&uart->uart_dev->DR,
uart->dma_rx_trigger_channel);
if (ret_val)
return ret_val;
}
if (uart->dma_tx_buff && uart->tx) {
ret_val = dma_ring_buffer_mem_to_periph_initialize(&uart->tx_ring_buff,
uart->base_dma_num,
uart->dma_tx_stream,
uart->tx_buff_count,
1U,
uart->dma_tx_buff,
uart->dma_tx_trigger_channel,
(void *)&uart->uart_dev->DR);
if (ret_val)
return ret_val;
cr3 |= USART_CR3_DMAT;
}
uart->uart_dev->CR3 = cr3;
if (uart->tx)
cr1 |= USART_CR1_TE;
if (uart->rx)
cr1 |= USART_CR1_RE;
/* Enable uart */
cr1 |= USART_CR1_UE;
uart->uart_dev->CR1 = cr1;
return 0;
} }
void uart_disable() void uart_disable(struct stm_uart *uart)
{ {
UART_PERIPH->CR1 = 0; if (!uart)
UART_PERIPH->CR2 = 0; return;
UART_PERIPH->CR3 = 0;
dma_ring_buffer_periph_to_mem_stop(&ring_buff_rx); uart->uart_dev->CR1 = 0;
dma_ring_buffer_mem_to_periph_stop(&ring_buff_tx); uart->uart_dev->CR2 = 0;
#ifdef DEBUGBUILD uart->uart_dev->CR3 = 0;
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK));
#endif if (uart->rx && uart->dma_rx_buff)
rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK)); dma_ring_buffer_periph_to_mem_stop(&uart->rx_ring_buff);
if (uart->dma_tx_buff && uart->tx)
dma_ring_buffer_mem_to_periph_stop(&uart->tx_ring_buff);
rcc_manager_disable_clock(uart->rcc_reg, uart->rcc_bit_no);
} }
void uart_send_char(char c) void uart_send_char(struct stm_uart *uart, char c)
{ {
while(!(UART_PERIPH->SR & USART_SR_TXE)); if (!uart || !uart->uart_dev)
UART_PERIPH->DR = c; return;
while(!(uart->uart_dev->SR & USART_SR_TXE));
uart->uart_dev->DR = c;
} }
void uart_send_array(const char *data, uint32_t len) void uart_send_array(struct stm_uart *uart, const char *data, uint32_t len)
{ {
uint32_t i; uint32_t i;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
uart_send_char(data[i]); uart_send_char(uart, data[i]);
} }
void uart_send_string(const char *string) void uart_send_string(struct stm_uart *uart, const char *string)
{ {
int i; int i;
for (i = 0; string[i] != '\0'; i++) for (i = 0; string[i] != '\0'; i++)
uart_send_char(string[i]); uart_send_char(uart, string[i]);
} }
void uart_send_array_with_dma(const char *data, uint32_t len) void uart_send_array_with_dma(struct stm_uart *uart, const char *data, uint32_t len)
{ {
dma_ring_buffer_mem_to_periph_insert_data(&ring_buff_tx, data, len); if (!uart || !uart->dma_tx_buff)
return;
dma_ring_buffer_mem_to_periph_insert_data(&uart->tx_ring_buff, data, len);
} }
void uart_send_string_with_dma(const char *string) void uart_send_string_with_dma(struct stm_uart *uart, const char *string)
{ {
size_t len; size_t len;
len = strlen(string); len = strlen(string);
uart_send_array_with_dma(string, (uint32_t)len); uart_send_array_with_dma(uart, string, (uint32_t)len);
} }
int uart_receive_data_with_dma(const char **data, size_t *len) int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t *len)
{ {
return dma_ring_buffer_periph_to_mem_get_data(&ring_buff_rx, (const volatile void **)data, len); if (!uart)
return -1000;
return dma_ring_buffer_periph_to_mem_get_data(&uart->rx_ring_buff, (const volatile void **)data, len);
} }
void DMA2_Stream7_IRQHandler() int uart_check_rx_avail(struct stm_uart *uart)
{ {
uint32_t hisr = DMA2->HISR; if (!uart)
DMA2->HIFCR = hisr; return 0;
if (hisr & DMA_HISR_TCIF7) { if (uart->uart_dev->SR & USART_SR_RXNE)
dma_ring_buffer_mem_to_periph_int_callback(&ring_buff_tx); return 1;
} else
return 0;
} }
void uart_tx_dma_complete_int_callback(struct stm_uart *uart)
{
if (!uart)
return;
dma_ring_buffer_mem_to_periph_int_callback(&uart->tx_ring_buff);
}

View File

@ -23,6 +23,7 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
extern struct stm_uart shell_uart;
char* _sbrk(int incr) char* _sbrk(int incr)
{ {
@ -87,6 +88,6 @@ int _read(void)
int _write(int fd, const void *buf, int count) int _write(int fd, const void *buf, int count)
{ {
if (fd == 1) if (fd == 1)
uart_send_array_with_dma((char*)buf, count); uart_send_array_with_dma(&shell_uart, (char *)buf, count);
return count; return count;
} }