From 4bc85d474f989177d8863aad22d3812c46f8334d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 18:48:26 +0100 Subject: [PATCH 1/5] c --- .../reflow-controller/shell-uart-config.h | 33 +++++++++++++++++++ .../include/reflow-controller/shell.h | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 stm-firmware/include/reflow-controller/shell-uart-config.h diff --git a/stm-firmware/include/reflow-controller/shell-uart-config.h b/stm-firmware/include/reflow-controller/shell-uart-config.h new file mode 100644 index 0000000..b9141c4 --- /dev/null +++ b/stm-firmware/include/reflow-controller/shell-uart-config.h @@ -0,0 +1,33 @@ +#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__ */ diff --git a/stm-firmware/include/reflow-controller/shell.h b/stm-firmware/include/reflow-controller/shell.h index a93c9aa..72a94d2 100644 --- a/stm-firmware/include/reflow-controller/shell.h +++ b/stm-firmware/include/reflow-controller/shell.h @@ -24,7 +24,7 @@ #include #include -shellmatta_handle_t shell_init(void); +shellmatta_handle_t shell_init(shellmatta_write_t write_func); void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len); From 5012b726cdac86a5b981dedd04abbf8b4020056a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 18:50:09 +0100 Subject: [PATCH 2/5] Make Uart driver universal --- stm-firmware/calibration.c | 4 +- stm-firmware/include/stm-periph/uart.h | 66 +++++----- stm-firmware/main.c | 64 +++++++++- stm-firmware/shell.c | 10 +- stm-firmware/stm-periph/uart.c | 165 ++++++++++++++++--------- stm-firmware/syscalls.c | 3 +- 6 files changed, 208 insertions(+), 104 deletions(-) diff --git a/stm-firmware/calibration.c b/stm-firmware/calibration.c index 5246099..373aa35 100644 --- a/stm-firmware/calibration.c +++ b/stm-firmware/calibration.c @@ -27,6 +27,8 @@ #include #include +extern struct stm_uart shell_uart; + void calibration_calculate(float low_measured, float low_setpoint, float high_measured, float high_setpoint, float *sens_deviation, float *sens_corrected_offset) { @@ -110,7 +112,7 @@ static void wait_for_uart_enter() int uart_recv_status; 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) { for (iter = 0; iter < recv_len; iter++) { if (recv_data[iter] == '\n' || recv_data[iter] == '\r') diff --git a/stm-firmware/include/stm-periph/uart.h b/stm-firmware/include/stm-periph/uart.h index 79c012a..b5c7c77 100644 --- a/stm-firmware/include/stm-periph/uart.h +++ b/stm-firmware/include/stm-periph/uart.h @@ -18,57 +18,55 @@ * If not, see . */ +#include +#include #include #include #ifndef 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 -#define UART_RCC_MASK RCC_APB2ENR_USART1EN +void uart_disable(struct stm_uart *uart); -#ifdef DEBUGBUILD +void uart_send_char(struct stm_uart *uart, char c); -#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_array(struct stm_uart *uart, const char *data, uint32_t len); -#endif +void uart_send_string(struct stm_uart *uart, const char *string); -/* 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_array_with_dma(struct stm_uart *uart, const char *data, uint32_t len); -#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(); -void sendChar(char c); -void sendString(char* s, int count); +int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t *len); +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_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); +void uart_tx_dma_complete_int_callback(struct stm_uart *uart); #endif /* UART_UART_H_ */ diff --git a/stm-firmware/main.c b/stm-firmware/main.c index 3512e2a..daa8f3b 100644 --- a/stm-firmware/main.c +++ b/stm-firmware/main.c @@ -37,6 +37,8 @@ #include #include #include +#include +#include static void setup_nvic_priorities() { @@ -54,6 +56,51 @@ static volatile int pt1000_value_status; 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() { const char *uart_input; @@ -71,16 +118,27 @@ int main() loudspeaker_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) { pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value); 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) 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); + } +} diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index ccbe1d0..95e145e 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -41,12 +41,6 @@ static shellmatta_instance_t shell; static char shell_buffer[512]; 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, const char *arguments, 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_retCode_t ret; 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) handle = NULL; diff --git a/stm-firmware/stm-periph/uart.c b/stm-firmware/stm-periph/uart.c index 1276982..5bc44b3 100644 --- a/stm-firmware/stm-periph/uart.c +++ b/stm-firmware/stm-periph/uart.c @@ -25,99 +25,150 @@ #include #include -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 uart_init(struct stm_uart *uart) { - rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK)); - UART_PORT->MODER &= MODER_DELETE(UART_TX_PIN) & MODER_DELETE(UART_RX_PIN); - 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 + int ret_val = 0; + uint32_t cr3 = 0; + uint32_t cr1 = 0; -void uart_init_with_dma() -{ - 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; + if (!uart) + return -1000; - 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); + rcc_manager_enable_clock(uart->rcc_reg, uart->rcc_bit_no); - 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; - UART_PERIPH->CR2 = 0; - UART_PERIPH->CR3 = 0; - dma_ring_buffer_periph_to_mem_stop(&ring_buff_rx); - dma_ring_buffer_mem_to_periph_stop(&ring_buff_tx); -#ifdef DEBUGBUILD - rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK)); -#endif - rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK)); + if (!uart) + return; + + uart->uart_dev->CR1 = 0; + uart->uart_dev->CR2 = 0; + uart->uart_dev->CR3 = 0; + + if (uart->rx && uart->dma_rx_buff) + 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)); - UART_PERIPH->DR = c; + if (!uart || !uart->uart_dev) + 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; 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; 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; 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; - DMA2->HIFCR = hisr; + if (!uart) + return 0; - if (hisr & DMA_HISR_TCIF7) { - dma_ring_buffer_mem_to_periph_int_callback(&ring_buff_tx); - } + if (uart->uart_dev->SR & USART_SR_RXNE) + 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); +} + diff --git a/stm-firmware/syscalls.c b/stm-firmware/syscalls.c index 34fa4af..42c63b3 100644 --- a/stm-firmware/syscalls.c +++ b/stm-firmware/syscalls.c @@ -23,6 +23,7 @@ #include #include +extern struct stm_uart shell_uart; char* _sbrk(int incr) { @@ -87,6 +88,6 @@ int _read(void) int _write(int fd, const void *buf, int count) { if (fd == 1) - uart_send_array_with_dma((char*)buf, count); + uart_send_array_with_dma(&shell_uart, (char *)buf, count); return count; } From f09877921a31372f9c9cb778d5978dd4b5e7bcbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 19:16:46 +0100 Subject: [PATCH 3/5] start onewire interface --- .../include/reflow-controller/onewire-if.h | 32 +++++++++++++++++++ stm-firmware/onewire-if.c | 21 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 stm-firmware/include/reflow-controller/onewire-if.h create mode 100644 stm-firmware/onewire-if.c diff --git a/stm-firmware/include/reflow-controller/onewire-if.h b/stm-firmware/include/reflow-controller/onewire-if.h new file mode 100644 index 0000000..07d4137 --- /dev/null +++ b/stm-firmware/include/reflow-controller/onewire-if.h @@ -0,0 +1,32 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2020 Mario Hüttel +* +* 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 . +*/ + +#ifndef __ONEWIRE_IF_H__ +#define __ONEWIRE_IF_H__ + +#include + +int onewire_if_init_uart(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__ */ diff --git a/stm-firmware/onewire-if.c b/stm-firmware/onewire-if.c new file mode 100644 index 0000000..67ebccc --- /dev/null +++ b/stm-firmware/onewire-if.c @@ -0,0 +1,21 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2020 Mario Hüttel +* +* 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 . +*/ + +#include From 26b8ad852ed96139473d734750a5fc16aa3f1baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 19:21:29 +0100 Subject: [PATCH 4/5] Add static module for onewire temperature sensors --- .../reflow-controller/onewire-temp-sensors.h | 25 +++++++++++++++++++ stm-firmware/onewire-temp-sensors.c | 22 ++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 stm-firmware/include/reflow-controller/onewire-temp-sensors.h create mode 100644 stm-firmware/onewire-temp-sensors.c diff --git a/stm-firmware/include/reflow-controller/onewire-temp-sensors.h b/stm-firmware/include/reflow-controller/onewire-temp-sensors.h new file mode 100644 index 0000000..799896f --- /dev/null +++ b/stm-firmware/include/reflow-controller/onewire-temp-sensors.h @@ -0,0 +1,25 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2020 Mario Hüttel +* +* 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 . +*/ + + +#ifndef __ONEWIRE_TEMP_SENSORS_H__ +#define __ONEWIRE_TEMP_SENSORS_H__ + +#endif /* __ONEWIRE_TEMP_SENSORS_H__ */ diff --git a/stm-firmware/onewire-temp-sensors.c b/stm-firmware/onewire-temp-sensors.c new file mode 100644 index 0000000..7864718 --- /dev/null +++ b/stm-firmware/onewire-temp-sensors.c @@ -0,0 +1,22 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2020 Mario Hüttel +* +* 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 . +*/ + + +#include \ No newline at end of file From 1ef77133519f83670a45193cf8de2e4ccfd7f30e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 20:02:37 +0100 Subject: [PATCH 5/5] Start implementation of onewire temperature sensor interface --- stm-firmware/Makefile | 2 ++ .../include/reflow-controller/onewire-if.h | 4 ++- .../reflow-controller/onewire-temp-sensors.h | 22 ++++++++++++ .../include/stm-periph/stm32-gpio-macros.h | 1 + stm-firmware/onewire-if.c | 35 +++++++++++++++++++ stm-firmware/onewire-temp-sensors.c | 22 +++++++++++- 6 files changed, 84 insertions(+), 2 deletions(-) diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index 4f915bf..011926c 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -49,6 +49,8 @@ CFILES += rotary-encoder.c CFILES += stack-check.c +CFILES += onewire-if.c onewire-temp-sensors.c + DEFINES += -DDEBUGBUILD ################################################################################### diff --git a/stm-firmware/include/reflow-controller/onewire-if.h b/stm-firmware/include/reflow-controller/onewire-if.h index 07d4137..0d9ee25 100644 --- a/stm-firmware/include/reflow-controller/onewire-if.h +++ b/stm-firmware/include/reflow-controller/onewire-if.h @@ -23,7 +23,9 @@ #include -int onewire_if_init_uart(struct stm_uart *uart); +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); diff --git a/stm-firmware/include/reflow-controller/onewire-temp-sensors.h b/stm-firmware/include/reflow-controller/onewire-temp-sensors.h index 799896f..056a29a 100644 --- a/stm-firmware/include/reflow-controller/onewire-temp-sensors.h +++ b/stm-firmware/include/reflow-controller/onewire-temp-sensors.h @@ -18,8 +18,30 @@ * If not, see . */ +#include +#include #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__ */ diff --git a/stm-firmware/include/stm-periph/stm32-gpio-macros.h b/stm-firmware/include/stm-periph/stm32-gpio-macros.h index 438ba03..2c95d38 100644 --- a/stm-firmware/include/stm-periph/stm32-gpio-macros.h +++ b/stm-firmware/include/stm-periph/stm32-gpio-macros.h @@ -28,6 +28,7 @@ #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 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: \ x&0x10?4:x&0x20?5:x&0x40?6:x&0x80?7: \ diff --git a/stm-firmware/onewire-if.c b/stm-firmware/onewire-if.c index 67ebccc..c96764e 100644 --- a/stm-firmware/onewire-if.c +++ b/stm-firmware/onewire-if.c @@ -19,3 +19,38 @@ */ #include + +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) +{ + +} diff --git a/stm-firmware/onewire-temp-sensors.c b/stm-firmware/onewire-temp-sensors.c index 7864718..7b03827 100644 --- a/stm-firmware/onewire-temp-sensors.c +++ b/stm-firmware/onewire-temp-sensors.c @@ -18,5 +18,25 @@ * If not, see . */ +#include +#include +#include +#include +#include +#include + +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)); +} -#include \ No newline at end of file