From c923fb3e12b61eb59e6baad8c8762d98fc401049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 22:09:58 +0100 Subject: [PATCH 1/5] Add baudrate change function to uart driver --- stm-firmware/include/stm-periph/uart.h | 2 ++ stm-firmware/stm-periph/uart.c | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/stm-firmware/include/stm-periph/uart.h b/stm-firmware/include/stm-periph/uart.h index b5c7c77..8fa856f 100644 --- a/stm-firmware/include/stm-periph/uart.h +++ b/stm-firmware/include/stm-periph/uart.h @@ -48,6 +48,8 @@ struct stm_uart { int uart_init(struct stm_uart *uart); +void uart_change_brr(struct stm_uart *uart, uint32_t brr); + void uart_disable(struct stm_uart *uart); void uart_send_char(struct stm_uart *uart, char c); diff --git a/stm-firmware/stm-periph/uart.c b/stm-firmware/stm-periph/uart.c index 5bc44b3..88e5cd0 100644 --- a/stm-firmware/stm-periph/uart.c +++ b/stm-firmware/stm-periph/uart.c @@ -86,6 +86,14 @@ int uart_init(struct stm_uart *uart) return 0; } +void uart_change_brr(struct stm_uart *uart, uint32_t brr) +{ + if (!uart || !uart->uart_dev) + return; + + uart->uart_dev->BRR = brr; +} + void uart_disable(struct stm_uart *uart) { if (!uart) From 71e2073a76ddd50eb57ec27645769ed6fbbbfb71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 24 Feb 2020 22:15:36 +0100 Subject: [PATCH 2/5] reflect bitrate change of uart in uart struct --- stm-firmware/stm-periph/uart.c | 1 + 1 file changed, 1 insertion(+) diff --git a/stm-firmware/stm-periph/uart.c b/stm-firmware/stm-periph/uart.c index 88e5cd0..e2ad08d 100644 --- a/stm-firmware/stm-periph/uart.c +++ b/stm-firmware/stm-periph/uart.c @@ -91,6 +91,7 @@ void uart_change_brr(struct stm_uart *uart, uint32_t brr) if (!uart || !uart->uart_dev) return; + uart->brr_val = brr; uart->uart_dev->BRR = brr; } From 0ddaef01c89f7166b7825b1b1cb2c1c3eed6a714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 25 Feb 2020 19:34:23 +0100 Subject: [PATCH 3/5] Start onewire interface. But probably won't finish it --- stm-firmware/Makefile | 2 +- .../include/reflow-controller/onewire-if.h | 34 ------- .../reflow-controller/onewire-temp-sensors.h | 8 +- .../include/reflow-controller/systick.h | 1 + .../include/stm-periph/dma-ring-buffer.h | 16 ++++ stm-firmware/include/stm-periph/uart.h | 3 + stm-firmware/onewire-if.c | 56 ----------- stm-firmware/onewire-temp-sensors.c | 96 ++++++++++++++++++- stm-firmware/stm-periph/dma-ring-buffer.c | 49 +++++++--- stm-firmware/stm-periph/uart.c | 33 +++++++ stm-firmware/systick.c | 5 + 11 files changed, 195 insertions(+), 108 deletions(-) delete mode 100644 stm-firmware/include/reflow-controller/onewire-if.h delete mode 100644 stm-firmware/onewire-if.c diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index 011926c..f675c68 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -49,7 +49,7 @@ CFILES += rotary-encoder.c CFILES += stack-check.c -CFILES += onewire-if.c onewire-temp-sensors.c +CFILES += 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 deleted file mode 100644 index 0d9ee25..0000000 --- a/stm-firmware/include/reflow-controller/onewire-if.h +++ /dev/null @@ -1,34 +0,0 @@ -/* 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, 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__ */ diff --git a/stm-firmware/include/reflow-controller/onewire-temp-sensors.h b/stm-firmware/include/reflow-controller/onewire-temp-sensors.h index 056a29a..9174fbd 100644 --- a/stm-firmware/include/reflow-controller/onewire-temp-sensors.h +++ b/stm-firmware/include/reflow-controller/onewire-temp-sensors.h @@ -32,6 +32,8 @@ #define ONEWIRE_UART_DEV USART3 +enum onewire_temp_resolution {RES_8BIT, RES_12BIT}; + void onewire_temp_sensors_setup_hw(); /** @@ -40,8 +42,10 @@ void onewire_temp_sensors_setup_hw(); * @param list_len * @return Negative: error, >= 0: amount of discovered sensors */ -int onewire_temp_sensors_discover(uint32_t *id_list, size_t list_len); +int onewire_temp_sensors_discover(uint64_t *id_list, size_t list_len); -float onewire_temp_sensor_read_temp(uint32_t id); +int onewire_temp_sensor_config(uint64_t id, enum onewire_temp_resolution resolution); + +float onewire_temp_sensor_read_temp(uint64_t id); #endif /* __ONEWIRE_TEMP_SENSORS_H__ */ diff --git a/stm-firmware/include/reflow-controller/systick.h b/stm-firmware/include/reflow-controller/systick.h index e799cb4..fb7cf2c 100644 --- a/stm-firmware/include/reflow-controller/systick.h +++ b/stm-firmware/include/reflow-controller/systick.h @@ -63,5 +63,6 @@ void systick_setup(void); */ void systick_wait_ms(uint32_t ms); +uint64_t systick_get_global_tick(); #endif /* __SYSTICK_H__ */ diff --git a/stm-firmware/include/stm-periph/dma-ring-buffer.h b/stm-firmware/include/stm-periph/dma-ring-buffer.h index c4449a6..7f4c441 100644 --- a/stm-firmware/include/stm-periph/dma-ring-buffer.h +++ b/stm-firmware/include/stm-periph/dma-ring-buffer.h @@ -96,6 +96,14 @@ int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, */ void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff); +/** + * @brief Get fill level of peripheral to memory DMA ring buffer + * @param buff Buffer + * @param fill_level fill level to write data to + * @return 0 if success + */ +int dma_ring_buffer_periph_to_mem_fill_level(struct dma_ring_buffer_to_mem *buff, size_t *fill_level); + /** * @brief Initialize ring buffer to streaming data from meory to a peripheral * @param[in,out] dma_buffer DMA ring buffer structure @@ -138,6 +146,14 @@ void dma_ring_buffer_mem_to_periph_int_callback(struct dma_ring_buffer_to_periph */ void dma_ring_buffer_mem_to_periph_stop(struct dma_ring_buffer_to_periph *buff); +/** + * @brief Get fill level of mem to periph DMA ring buffer + * @param buff Buffer + * @param fill_level fill level to write data to + * @return 0 if success + */ +int dma_ring_buffer_mem_to_periph_fill_level(struct dma_ring_buffer_to_periph *buff, size_t *fill_level); + /** @} */ #endif /* __DMA_RING_BUFFER_H__ */ diff --git a/stm-firmware/include/stm-periph/uart.h b/stm-firmware/include/stm-periph/uart.h index 8fa856f..dddb94f 100644 --- a/stm-firmware/include/stm-periph/uart.h +++ b/stm-firmware/include/stm-periph/uart.h @@ -70,5 +70,8 @@ int uart_check_rx_avail(struct stm_uart *uart); void uart_tx_dma_complete_int_callback(struct stm_uart *uart); +size_t uart_dma_tx_queue_avail(struct stm_uart *uart); + +size_t uart_dma_rx_queue_avail(struct stm_uart *uart); #endif /* UART_UART_H_ */ diff --git a/stm-firmware/onewire-if.c b/stm-firmware/onewire-if.c deleted file mode 100644 index c96764e..0000000 --- a/stm-firmware/onewire-if.c +++ /dev/null @@ -1,56 +0,0 @@ -/* 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 - -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 7b03827..2c58499 100644 --- a/stm-firmware/onewire-temp-sensors.c +++ b/stm-firmware/onewire-temp-sensors.c @@ -20,13 +20,20 @@ #include #include -#include #include #include #include static struct stm_uart if_uart; +#define ONEWIRE_IF_RESET_BRR_VAL 0x1117UL + +/* UART_DIV is 45.5625 => 115200 @ 84 MHz */ +#define ONEWIRE_IF_COMM_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */ +#define ONEWIRE_IF_COMM_DIV_MANTISSA 45U /* Equals 45 */ + +#define ONEWIRE_IF_COMM_BRR_VAL ((ONEWIRE_IF_COMM_DIV_MANTISSA<<4) | ONEWIRE_IF_COMM_DIV_FRACTION) + void onewire_temp_sensors_setup_hw() { rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ONEWIRE_TEMP_RCC_MASK)); @@ -37,6 +44,91 @@ void onewire_temp_sensors_setup_hw() 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)); + if_uart.rx = 1; + if_uart.tx = 1; + if_uart.brr_val = ONEWIRE_IF_RESET_BRR_VAL; + if_uart.rcc_reg = &RCC->AHB1ENR; + if_uart.rcc_bit_no = BITMASK_TO_BITNO(ONEWIRE_TEMP_RCC_MASK); + if_uart.uart_dev = ONEWIRE_UART_DEV; + if_uart.dma_rx_buff = NULL; + if_uart.dma_tx_buff = NULL; + if_uart.base_dma_num = 1; + if_uart.dma_rx_stream = NULL; + if_uart.dma_tx_stream = NULL; + if_uart.rx_buff_count = 0ULL; + if_uart.tx_buff_count = 0ULL; + + (void)uart_init(&if_uart); } +static void onewire_send_bit(uint8_t bit) +{ + uart_send_char(&if_uart, (bit ? 0xFF : 0x00)); + (void)uart_get_char(&if_uart); +} + +static uint8_t onewire_receive_bit() +{ + unsigned char recv; + + uart_send_char(&if_uart, 0xFF); + recv = (unsigned char)uart_get_char(&if_uart); + + if (recv <= 0xFE) + return 0; + else + return 1; +} + +static void onewire_send_byte(uint8_t byte) +{ + int i; + + for (i = 7; i >= 0; i--) { + onewire_send_bit(byte & 0x80); + byte <<= 1; + } +} + +static uint8_t onewire_receive_byte() +{ + uint8_t byte = 0; + int i; + + for (i = 0; i < 8; i++) { + byte <<= 1; + byte |= (onewire_receive_bit() ? 0x01 : 0x0); + } + + return byte; +} + +static int onewire_temp_init_presence() +{ + unsigned char recv; + + uart_change_brr(&if_uart, ONEWIRE_IF_RESET_BRR_VAL); + uart_send_char(&if_uart, 0xF0); + recv = (unsigned char)uart_get_char(&if_uart); + uart_change_brr(&if_uart, ONEWIRE_IF_COMM_BRR_VAL); + + if (recv >= 0x10 && recv <= 0x90) + return 1; + else + return 0; +} + +int onewire_temp_sensors_discover(uint64_t *id_list, size_t list_len) +{ + +} + +int onewire_temp_sensor_config(uint64_t id, enum onewire_temp_resolution resolution) +{ + +} + +float onewire_temp_sensor_read_temp(uint64_t id) +{ + +} diff --git a/stm-firmware/stm-periph/dma-ring-buffer.c b/stm-firmware/stm-periph/dma-ring-buffer.c index b226090..0cdc5f9 100644 --- a/stm-firmware/stm-periph/dma-ring-buffer.c +++ b/stm-firmware/stm-periph/dma-ring-buffer.c @@ -33,6 +33,19 @@ #include #include +static size_t calculate_ring_buffer_fill_level(size_t buffer_size, size_t get_idx, size_t put_idx) +{ + size_t fill_level; + + if (put_idx >= get_idx) { + fill_level = (put_idx - get_idx); + } else { + fill_level = buffer_size - get_idx + put_idx; + } + + return fill_level; +} + static int dma_ring_buffer_switch_clock_enable(uint8_t base_dma, bool clk_en) { int ret_val; @@ -141,6 +154,19 @@ void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff) memset(buff, 0, sizeof(struct dma_ring_buffer_to_mem)); } +int dma_ring_buffer_periph_to_mem_fill_level(struct dma_ring_buffer_to_mem *buff, size_t *fill_level) +{ + size_t put_idx; + + if (!buff || !fill_level) + return -1000; + + put_idx = buff->buffer_count - buff->dma->NDTR; + *fill_level = calculate_ring_buffer_fill_level(buff->buffer_count, buff->get_idx, put_idx); + + return 0; +} + int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, volatile void *data_buffer, uint8_t dma_trigger_channel, void *dest_reg) { if (!dma_buffer || !dma_stream || !data_buffer || !dest_reg) @@ -163,19 +189,6 @@ int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *d return 0; } -static size_t calculate_ring_buffer_fill_level(size_t buffer_size, size_t get_idx, size_t put_idx) -{ - size_t fill_level; - - if (put_idx >= get_idx) { - fill_level = (put_idx - get_idx); - } else { - fill_level = buffer_size - get_idx + put_idx; - } - - return fill_level; -} - static void queue_or_start_dma_transfer(struct dma_ring_buffer_to_periph *buff) { uint32_t dma_transfer_cnt; @@ -289,4 +302,14 @@ void dma_ring_buffer_mem_to_periph_stop(struct dma_ring_buffer_to_periph *buff) memset(buff, 0, sizeof(struct dma_ring_buffer_to_periph)); } +int dma_ring_buffer_mem_to_periph_fill_level(struct dma_ring_buffer_to_periph *buff, size_t *fill_level) +{ + if (!buff || !fill_level) + return -1000; + + *fill_level = calculate_ring_buffer_fill_level(buff->buffer_count, buff->dma_get_idx_current, buff->sw_put_idx); + + return 0; +} + /** @} */ diff --git a/stm-firmware/stm-periph/uart.c b/stm-firmware/stm-periph/uart.c index e2ad08d..2044f25 100644 --- a/stm-firmware/stm-periph/uart.c +++ b/stm-firmware/stm-periph/uart.c @@ -162,6 +162,16 @@ int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t return dma_ring_buffer_periph_to_mem_get_data(&uart->rx_ring_buff, (const volatile void **)data, len); } +char uart_get_char(struct stm_uart *uart) +{ + if (!uart) + return 0; + /* Wait for data to be available */ + while (!(uart->uart_dev->SR & USART_SR_RXNE)); + + return (char)uart->uart_dev->DR; +} + int uart_check_rx_avail(struct stm_uart *uart) { if (!uart) @@ -181,3 +191,26 @@ void uart_tx_dma_complete_int_callback(struct stm_uart *uart) dma_ring_buffer_mem_to_periph_int_callback(&uart->tx_ring_buff); } +size_t uart_dma_tx_queue_avail(struct stm_uart *uart) +{ + size_t fill_level = 0UL; + + if (!uart) + return 0UL; + + (void)dma_ring_buffer_mem_to_periph_fill_level(&uart->tx_ring_buff, &fill_level); + + return fill_level; +} + +size_t uart_dma_rx_queue_avail(struct stm_uart *uart) +{ + size_t fill_level = 0UL; + + if (!uart) + return 0UL; + + (void)dma_ring_buffer_periph_to_mem_fill_level(&uart->rx_ring_buff, &fill_level); + + return fill_level; +} diff --git a/stm-firmware/systick.c b/stm-firmware/systick.c index 648aaea..55c68a8 100644 --- a/stm-firmware/systick.c +++ b/stm-firmware/systick.c @@ -41,6 +41,11 @@ void systick_wait_ms(uint32_t ms) while (wait_tick_ms < ms); } +uint64_t systick_get_global_tick() +{ + return global_tick_ms; +} + /** * @brief Interrupt Handler for SysTick * From 31769fd42dcbc1092a068b61ccabf17d7480ae75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 25 Feb 2020 19:43:51 +0100 Subject: [PATCH 4/5] remove onewire interface from compilation --- stm-firmware/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index f675c68..d758cb8 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -49,7 +49,7 @@ CFILES += rotary-encoder.c CFILES += stack-check.c -CFILES += onewire-temp-sensors.c +#CFILES += onewire-temp-sensors.c DEFINES += -DDEBUGBUILD From a39fe09de4616341ec0885d838a877b37df563db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 25 Feb 2020 19:48:14 +0100 Subject: [PATCH 5/5] Remove files for onewire sensor. Will add them when necessary. --- .../reflow-controller/onewire-temp-sensors.h | 51 ------- stm-firmware/onewire-temp-sensors.c | 134 ------------------ 2 files changed, 185 deletions(-) delete mode 100644 stm-firmware/include/reflow-controller/onewire-temp-sensors.h delete 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 deleted file mode 100644 index 9174fbd..0000000 --- a/stm-firmware/include/reflow-controller/onewire-temp-sensors.h +++ /dev/null @@ -1,51 +0,0 @@ -/* 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 -#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 - -enum onewire_temp_resolution {RES_8BIT, RES_12BIT}; - -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(uint64_t *id_list, size_t list_len); - -int onewire_temp_sensor_config(uint64_t id, enum onewire_temp_resolution resolution); - -float onewire_temp_sensor_read_temp(uint64_t id); - -#endif /* __ONEWIRE_TEMP_SENSORS_H__ */ diff --git a/stm-firmware/onewire-temp-sensors.c b/stm-firmware/onewire-temp-sensors.c deleted file mode 100644 index 2c58499..0000000 --- a/stm-firmware/onewire-temp-sensors.c +++ /dev/null @@ -1,134 +0,0 @@ -/* 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 -#include -#include -#include -#include - -static struct stm_uart if_uart; - -#define ONEWIRE_IF_RESET_BRR_VAL 0x1117UL - -/* UART_DIV is 45.5625 => 115200 @ 84 MHz */ -#define ONEWIRE_IF_COMM_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */ -#define ONEWIRE_IF_COMM_DIV_MANTISSA 45U /* Equals 45 */ - -#define ONEWIRE_IF_COMM_BRR_VAL ((ONEWIRE_IF_COMM_DIV_MANTISSA<<4) | ONEWIRE_IF_COMM_DIV_FRACTION) - -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); - - if_uart.rx = 1; - if_uart.tx = 1; - if_uart.brr_val = ONEWIRE_IF_RESET_BRR_VAL; - if_uart.rcc_reg = &RCC->AHB1ENR; - if_uart.rcc_bit_no = BITMASK_TO_BITNO(ONEWIRE_TEMP_RCC_MASK); - if_uart.uart_dev = ONEWIRE_UART_DEV; - if_uart.dma_rx_buff = NULL; - if_uart.dma_tx_buff = NULL; - if_uart.base_dma_num = 1; - if_uart.dma_rx_stream = NULL; - if_uart.dma_tx_stream = NULL; - if_uart.rx_buff_count = 0ULL; - if_uart.tx_buff_count = 0ULL; - - (void)uart_init(&if_uart); -} - -static void onewire_send_bit(uint8_t bit) -{ - uart_send_char(&if_uart, (bit ? 0xFF : 0x00)); - (void)uart_get_char(&if_uart); -} - -static uint8_t onewire_receive_bit() -{ - unsigned char recv; - - uart_send_char(&if_uart, 0xFF); - recv = (unsigned char)uart_get_char(&if_uart); - - if (recv <= 0xFE) - return 0; - else - return 1; -} - -static void onewire_send_byte(uint8_t byte) -{ - int i; - - for (i = 7; i >= 0; i--) { - onewire_send_bit(byte & 0x80); - byte <<= 1; - } -} - -static uint8_t onewire_receive_byte() -{ - uint8_t byte = 0; - int i; - - for (i = 0; i < 8; i++) { - byte <<= 1; - byte |= (onewire_receive_bit() ? 0x01 : 0x0); - } - - return byte; -} - -static int onewire_temp_init_presence() -{ - unsigned char recv; - - uart_change_brr(&if_uart, ONEWIRE_IF_RESET_BRR_VAL); - uart_send_char(&if_uart, 0xF0); - recv = (unsigned char)uart_get_char(&if_uart); - uart_change_brr(&if_uart, ONEWIRE_IF_COMM_BRR_VAL); - - if (recv >= 0x10 && recv <= 0x90) - return 1; - else - return 0; -} - -int onewire_temp_sensors_discover(uint64_t *id_list, size_t list_len) -{ - -} - -int onewire_temp_sensor_config(uint64_t id, enum onewire_temp_resolution resolution) -{ - -} - -float onewire_temp_sensor_read_temp(uint64_t id) -{ - -}