Compare commits

..

No commits in common. "49835d92135cc4c6ff4bd477c48c8f7d94d287c2" and "5948ac2897eb3b45b25ec8bc30f04f68536cce45" have entirely different histories.

14 changed files with 105 additions and 424 deletions

View File

@ -51,8 +51,6 @@ CFILES += stack-check.c
CFILES += ui/lcd.c ui/menu.c CFILES += ui/lcd.c ui/menu.c
CFILES += onewire-if.c onewire-temp-sensors.c
DEFINES += -DDEBUGBUILD DEFINES += -DDEBUGBUILD
################################################################################### ###################################################################################

View File

@ -27,8 +27,6 @@
#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)
{ {
@ -112,7 +110,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(&shell_uart, &recv_data, &recv_len); uart_recv_status = uart_receive_data_with_dma(&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

@ -1,34 +0,0 @@
/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ONEWIRE_IF_H__
#define __ONEWIRE_IF_H__
#include <stm-periph/uart.h>
int onewire_if_init_uart(struct stm_uart *uart, uint32_t brr_val, USART_TypeDef *onewire_uart, volatile uint32_t *rcc_reg, uint8_t rcc_bit_num);
void onewire_if_disable(struct stm_uart *uart);
int onewire_if_send_byte(struct stm_uart *uart);
int onewire_if_receive_byte(struct stm_uart *uart);
#endif /* __ONEWIRE_IF_H__ */

View File

@ -1,47 +0,0 @@
/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stddef.h>
#include <stdint.h>
#ifndef __ONEWIRE_TEMP_SENSORS_H__
#define __ONEWIRE_TEMP_SENSORS_H__
#define ONEWIRE_TEMP_PORT GPIOB
#define ONEWIRE_TEMP_RCC_MASK RCC_AHB1ENR_GPIOBEN
#define ONEWIRE_TEMP_TX_PIN 10U
#define ONEWIRE_TEMP_RX_PIN 11U
#define ONEWIRE_TEMP_AF_NUM 7U
#define ONEWIRE_UART_DEV USART3
void onewire_temp_sensors_setup_hw();
/**
* @brief onewire_temp_sensors_discover
* @param id_list
* @param list_len
* @return Negative: error, >= 0: amount of discovered sensors
*/
int onewire_temp_sensors_discover(uint32_t *id_list, size_t list_len);
float onewire_temp_sensor_read_temp(uint32_t id);
#endif /* __ONEWIRE_TEMP_SENSORS_H__ */

View File

@ -1,33 +0,0 @@
#ifndef __SHELL_UART_CONFIG_H__
#define __SHELL_UART_CONFIG_H__
#define SHELL_UART_RECEIVE_DMA_STREAM DMA2_Stream5
#define SHELL_UART_SEND_DMA_STREAM DMA2_Stream7
#define SHELL_UART_PERIPH USART1
#define SHELL_UART_RCC_REG RCC->APB2ENR
#define SHELL_UART_RCC_MASK RCC_APB2ENR_USART1EN
#define SHELL_UART_RX_DMA_TRIGGER 4U
#define SHELL_UART_TX_DMA_TRIGGER 4U
#ifdef DEBUGBUILD
#define SHELL_UART_PORT GPIOA
#define SHELL_UART_PORT_RCC_MASK RCC_AHB1ENR_GPIOAEN
#define SHELL_UART_RX_PIN 10
#define SHELL_UART_TX_PIN 9
#define SHELL_UART_RX_PIN_ALTFUNC 7
#define SHELL_UART_TX_PIN_ALTFUNC 7
#else
#endif
/* UART_DIV is 45.5625 => 115200 @ 84 MHz */
#define SHELL_UART_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */
#define SHELL_UART_DIV_MANTISSA 45U /* Equals 45 */
#define SHELL_UART_BRR_REG_VALUE ((SHELL_UART_DIV_MANTISSA<<4) | SHELL_UART_DIV_FRACTION);
#endif /* __SHELL_UART_CONFIG_H__ */

View File

@ -24,7 +24,7 @@
#include <stddef.h> #include <stddef.h>
#include <shellmatta.h> #include <shellmatta.h>
shellmatta_handle_t shell_init(shellmatta_write_t write_func); shellmatta_handle_t shell_init(void);
void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len); void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len);

View File

@ -29,7 +29,6 @@
#define PINMASK(pin) ((0x3) << (pin * 2)) #define PINMASK(pin) ((0x3) << (pin * 2))
#define SETAF(PORT,PIN,AF) PORT->AFR[(PIN < 8 ? 0 : 1)] |= AF << ((PIN < 8 ? PIN : (PIN - 8)) * 4) #define SETAF(PORT,PIN,AF) PORT->AFR[(PIN < 8 ? 0 : 1)] |= AF << ((PIN < 8 ? PIN : (PIN - 8)) * 4)
#define ANALOG(pin) (0x03 << (pin * 2)) #define ANALOG(pin) (0x03 << (pin * 2))
#define OTYP_OPENDRAIN(pin) (0x1U << (pin))
#define BITMASK_TO_BITNO(x) (x&0x1?0:x&0x2?1:x&0x4?2:x&0x8?3: \ #define BITMASK_TO_BITNO(x) (x&0x1?0:x&0x2?1:x&0x4?2:x&0x8?3: \
x&0x10?4:x&0x20?5:x&0x40?6:x&0x80?7: \ x&0x10?4:x&0x20?5:x&0x40?6:x&0x80?7: \

View File

@ -18,55 +18,57 @@
* 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_
struct stm_uart { #define UART_RECEIVE_DMA_STREAM DMA2_Stream5
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;
};
int uart_init(struct stm_uart *uart); #define UART_SEND_DMA_STREAM DMA2_Stream7
void uart_disable(struct stm_uart *uart); #define UART_PERIPH USART1
#define UART_RCC_MASK RCC_APB2ENR_USART1EN
void uart_send_char(struct stm_uart *uart, char c); #ifdef DEBUGBUILD
void uart_send_array(struct stm_uart *uart, const char *data, uint32_t len); #define UART_PORT GPIOA
#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
void uart_send_string(struct stm_uart *uart, const char *string); #endif
void uart_send_array_with_dma(struct stm_uart *uart, const char *data, uint32_t len); /* UART_DIV is 45.5625 => 115200 @ 84 MHz */
#define UART_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */
#define UART_DIV_MANTISSA 45U /* Equals 45 */
void uart_send_string_with_dma(struct stm_uart *uart, const char *string); #define UART_BRR_REG_VALUE ((UART_DIV_MANTISSA<<4) | UART_DIV_FRACTION);
int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t *len); void initUART();
void sendChar(char c);
void sendString(char* s, int count);
char uart_get_char(struct stm_uart *uart);
int uart_check_rx_avail(struct stm_uart *uart); void uart_init_with_dma();
void uart_tx_dma_complete_int_callback(struct stm_uart *uart); void uart_disable();
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,8 +37,6 @@
#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()
{ {
@ -56,51 +54,6 @@ 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;
@ -118,27 +71,16 @@ int main()
loudspeaker_setup(); loudspeaker_setup();
rotary_encoder_setup(); rotary_encoder_setup();
uart_gpio_config(); uart_init_with_dma();
setup_sell_uart(&shell_uart);
shell_handle = shell_init(write_shell_callback); shell_handle = shell_init();
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(&shell_uart, &uart_input, &uart_input_len); uart_receive_status = uart_receive_data_with_dma(&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

@ -1,56 +0,0 @@
/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <reflow-controller/onewire-if.h>
int onewire_if_init_uart(struct stm_uart *uart, uint32_t brr_val, USART_TypeDef *onewire_uart, volatile uint32_t *rcc_reg, uint8_t rcc_bit_num)
{
if (!uart)
return -1000;
uart->rx = 1;
uart->tx = 1;
uart->brr_val = brr_val;
uart->rcc_reg = rcc_reg;
uart->uart_dev = onewire_uart;
uart->rcc_bit_no = rcc_bit_num;
uart->dma_rx_buff = NULL;
uart->dma_tx_buff = NULL;
return uart_init(uart);
}
void onewire_if_disable(struct stm_uart *uart)
{
if (!uart)
return;
uart_disable(uart);
}
int onewire_if_send_byte(struct stm_uart *uart)
{
}
int onewire_if_receive_byte(struct stm_uart *uart)
{
}

View File

@ -1,42 +0,0 @@
/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stm32/stm32f4xx.h>
#include <stm-periph/uart.h>
#include <reflow-controller/onewire-if.h>
#include <reflow-controller/onewire-temp-sensors.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm-periph/stm32-gpio-macros.h>
static struct stm_uart if_uart;
void onewire_temp_sensors_setup_hw()
{
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ONEWIRE_TEMP_RCC_MASK));
ONEWIRE_TEMP_PORT->OTYPER |= OTYP_OPENDRAIN(ONEWIRE_TEMP_TX_PIN);
ONEWIRE_TEMP_PORT->MODER &= MODER_DELETE(ONEWIRE_TEMP_RX_PIN) & MODER_DELETE(ONEWIRE_TEMP_TX_PIN);
ONEWIRE_TEMP_PORT->MODER |= ALTFUNC(ONEWIRE_TEMP_RX_PIN) | ALTFUNC(ONEWIRE_TEMP_TX_PIN);
SETAF(ONEWIRE_TEMP_PORT, ONEWIRE_TEMP_RX_PIN, ONEWIRE_TEMP_AF_NUM);
SETAF(ONEWIRE_TEMP_PORT, ONEWIRE_TEMP_TX_PIN, ONEWIRE_TEMP_AF_NUM);
(void)onewire_if_init_uart(&if_uart, 1, ONEWIRE_UART_DEV, &RCC->APB1ENR, BITMASK_TO_BITNO(RCC_APB1ENR_USART3EN));
}

View File

@ -41,6 +41,12 @@ 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)
@ -319,13 +325,13 @@ static shellmatta_cmd_t cmd[9] = {
} }
}; };
shellmatta_handle_t shell_init(shellmatta_write_t write_func) shellmatta_handle_t shell_init(void)
{ {
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_func); "\e[1;32mEnter command:\e[m\r\n", cmd, write_shell_callback);
if (ret != SHELLMATTA_OK) if (ret != SHELLMATTA_OK)
handle = NULL; handle = NULL;

View File

@ -25,150 +25,99 @@
#include <stm-periph/dma-ring-buffer.h> #include <stm-periph/dma-ring-buffer.h>
#include <string.h> #include <string.h>
int uart_init(struct stm_uart *uart) static struct dma_ring_buffer_to_mem ring_buff_rx;
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()
{ {
int ret_val = 0; rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK));
uint32_t cr3 = 0; UART_PORT->MODER &= MODER_DELETE(UART_TX_PIN) & MODER_DELETE(UART_RX_PIN);
uint32_t cr1 = 0; UART_PORT->MODER |= ALTFUNC(UART_RX_PIN) | ALTFUNC(UART_TX_PIN);
SETAF(UART_PORT, UART_RX_PIN, UART_RX_PIN_ALTFUNC);
SETAF(UART_PORT, UART_TX_PIN, UART_TX_PIN_ALTFUNC);
}
#endif
if (!uart) void uart_init_with_dma()
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;
rcc_manager_enable_clock(uart->rcc_reg, uart->rcc_bit_no); dma_ring_buffer_periph_to_mem_initialize(&ring_buff_rx, 2, UART_RECEIVE_DMA_STREAM, sizeof(uart_rx_buffer), 1U,
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);
/* Reset all config regs */ NVIC_EnableIRQ(DMA2_Stream7_IRQn);
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(struct stm_uart *uart) void uart_disable()
{ {
if (!uart) UART_PERIPH->CR1 = 0;
return; UART_PERIPH->CR2 = 0;
UART_PERIPH->CR3 = 0;
uart->uart_dev->CR1 = 0; dma_ring_buffer_periph_to_mem_stop(&ring_buff_rx);
uart->uart_dev->CR2 = 0; dma_ring_buffer_mem_to_periph_stop(&ring_buff_tx);
uart->uart_dev->CR3 = 0; #ifdef DEBUGBUILD
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK));
if (uart->rx && uart->dma_rx_buff) #endif
dma_ring_buffer_periph_to_mem_stop(&uart->rx_ring_buff); rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK));
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(struct stm_uart *uart, char c) void uart_send_char(char c)
{ {
if (!uart || !uart->uart_dev) while(!(UART_PERIPH->SR & USART_SR_TXE));
return; UART_PERIPH->DR = c;
while(!(uart->uart_dev->SR & USART_SR_TXE));
uart->uart_dev->DR = c;
} }
void uart_send_array(struct stm_uart *uart, const char *data, uint32_t len) void uart_send_array(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(uart, data[i]); uart_send_char(data[i]);
} }
void uart_send_string(struct stm_uart *uart, const char *string) void uart_send_string(const char *string)
{ {
int i; int i;
for (i = 0; string[i] != '\0'; i++) for (i = 0; string[i] != '\0'; i++)
uart_send_char(uart, string[i]); uart_send_char(string[i]);
} }
void uart_send_array_with_dma(struct stm_uart *uart, const char *data, uint32_t len) void uart_send_array_with_dma(const char *data, uint32_t len)
{ {
if (!uart || !uart->dma_tx_buff) dma_ring_buffer_mem_to_periph_insert_data(&ring_buff_tx, data, len);
return;
dma_ring_buffer_mem_to_periph_insert_data(&uart->tx_ring_buff, data, len);
} }
void uart_send_string_with_dma(struct stm_uart *uart, const char *string) void uart_send_string_with_dma(const char *string)
{ {
size_t len; size_t len;
len = strlen(string); len = strlen(string);
uart_send_array_with_dma(uart, string, (uint32_t)len); uart_send_array_with_dma(string, (uint32_t)len);
} }
int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t *len) int uart_receive_data_with_dma(const char **data, size_t *len)
{ {
if (!uart) return dma_ring_buffer_periph_to_mem_get_data(&ring_buff_rx, (const volatile void **)data, len);
return -1000;
return dma_ring_buffer_periph_to_mem_get_data(&uart->rx_ring_buff, (const volatile void **)data, len);
} }
int uart_check_rx_avail(struct stm_uart *uart) void DMA2_Stream7_IRQHandler()
{ {
if (!uart) uint32_t hisr = DMA2->HISR;
return 0; DMA2->HIFCR = hisr;
if (uart->uart_dev->SR & USART_SR_RXNE) if (hisr & DMA_HISR_TCIF7) {
return 1; dma_ring_buffer_mem_to_periph_int_callback(&ring_buff_tx);
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,7 +23,6 @@
#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)
{ {
@ -88,6 +87,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(&shell_uart, (char *)buf, count); uart_send_array_with_dma((char*)buf, count);
return count; return count;
} }