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] 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