reflow-oven-control-sw/stm-firmware/onewire-temp-sensors.c

135 lines
3.4 KiB
C
Raw Normal View History

/* 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-temp-sensors.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm-periph/stm32-gpio-macros.h>
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)
{
}