new upgrade
This commit is contained in:
@@ -6,13 +6,11 @@ set(srcs
|
||||
"src/ac_relay.c"
|
||||
"src/socket_lock.c"
|
||||
"src/rcm.c"
|
||||
"src/onewire.c"
|
||||
"src/ds18x20.c"
|
||||
"src/temp_sensor.c"
|
||||
"src/ntc_sensor.c"
|
||||
)
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES nvs_flash driver esp_adc esp_timer
|
||||
REQUIRES config evse ntc_driver spi_bus_manager)
|
||||
PRIV_REQUIRES driver esp_adc esp_timer
|
||||
REQUIRES config evse ntc_driver spi_bus_manager storage_service)
|
||||
|
||||
@@ -1,12 +1,35 @@
|
||||
// components/peripherals/include/adc121s021_dma.h
|
||||
#ifndef ADC_DMA_H_
|
||||
#define ADC_DMA_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Inicializa o ADC121S021 no barramento SPI partilhado.
|
||||
*
|
||||
* - Garante que o spi_bus_manager foi inicializado.
|
||||
* - Regista o dispositivo ADC no bus.
|
||||
*/
|
||||
void adc121s021_dma_init(void);
|
||||
|
||||
/**
|
||||
* @brief Lê uma única amostra (12 bits) do ADC121S021.
|
||||
*
|
||||
* Esta função faz uma transação SPI bloqueante (polling), suficientemente
|
||||
* rápida para uso em burst (100 amostras em ~2–3 ms).
|
||||
*
|
||||
* @param[out] sample Ponteiro onde será escrito o valor lido (0..4095).
|
||||
* @return true em caso de sucesso, false se ocorrer erro.
|
||||
*/
|
||||
bool adc121s021_dma_get_sample(uint16_t *sample);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ADC_DMA_h_ */
|
||||
#endif /* ADC_DMA_H_ */
|
||||
|
||||
@@ -1,254 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Grzegorz Hetman <ghetman@gmail.com>
|
||||
* Copyright (c) 2016 Alex Stewart <foogod@gmail.com>
|
||||
* Copyright (c) 2018 Ruslan V. Uss <unclerus@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of itscontributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _DS18X20_H
|
||||
#define _DS18X20_H
|
||||
|
||||
#include <esp_err.h>
|
||||
#include "onewire.h"
|
||||
|
||||
typedef onewire_addr_t ds18x20_addr_t;
|
||||
|
||||
/** An address value which can be used to indicate "any device on the bus" */
|
||||
#define DS18X20_ANY ONEWIRE_NONE
|
||||
|
||||
/** Family ID (lower address byte) of DS18B20 sensors */
|
||||
#define DS18B20_FAMILY_ID 0x28
|
||||
|
||||
/** Family ID (lower address byte) of DS18S20 sensors */
|
||||
#define DS18S20_FAMILY_ID 0x10
|
||||
|
||||
/**
|
||||
* @brief Find the addresses of all ds18x20 devices on the bus.
|
||||
*
|
||||
* Scans the bus for all devices and places their addresses in the supplied
|
||||
* array. If there are more than `addr_count` devices on the bus, only the
|
||||
* first `addr_count` are recorded.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 bus
|
||||
* @param addr_list A pointer to an array of ::ds18x20_addr_t values.
|
||||
* This will be populated with the addresses of the found
|
||||
* devices.
|
||||
* @param addr_count Number of slots in the `addr_list` array. At most this
|
||||
* many addresses will be returned.
|
||||
* @param found The number of devices found. Note that this may be less
|
||||
* than, equal to, or more than `addr_count`, depending on
|
||||
* how many ds18x20 devices are attached to the bus.
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18x20_scan_devices(gpio_num_t pin, ds18x20_addr_t *addr_list, size_t addr_count, size_t *found);
|
||||
|
||||
/**
|
||||
* @brief Tell one or more sensors to perform a temperature measurement and
|
||||
* conversion (CONVERT_T) operation.
|
||||
*
|
||||
* This operation can take up to 750ms to complete.
|
||||
*
|
||||
* If `wait=true`, this routine will automatically drive the pin high for the
|
||||
* necessary 750ms after issuing the command to ensure parasitically-powered
|
||||
* devices have enough power to perform the conversion operation (for
|
||||
* non-parasitically-powered devices, this is not necessary but does not
|
||||
* hurt). If `wait=false`, this routine will drive the pin high, but will
|
||||
* then return immediately. It is up to the caller to wait the requisite time
|
||||
* and then depower the bus using onewire_depower() or by issuing another
|
||||
* command once conversion is done.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device on the bus. This can be set
|
||||
* to ::DS18X20_ANY to send the command to all devices on the bus
|
||||
* at the same time.
|
||||
* @param wait Whether to wait for the necessary 750ms for the ds18x20 to
|
||||
* finish performing the conversion before returning to the
|
||||
* caller (You will normally want to do this).
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18x20_measure(gpio_num_t pin, ds18x20_addr_t addr, bool wait);
|
||||
|
||||
/**
|
||||
* @brief Read the value from the last CONVERT_T operation.
|
||||
*
|
||||
* This should be called after ds18x20_measure() to fetch the result of the
|
||||
* temperature measurement.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param temperature The temperature in degrees Celsius
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18x20_read_temperature(gpio_num_t pin, ds18x20_addr_t addr, int16_t *temperature);
|
||||
|
||||
/**
|
||||
* @brief Read the value from the last CONVERT_T operation (ds18b20 version).
|
||||
*
|
||||
* This should be called after ds18x20_measure() to fetch the result of the
|
||||
* temperature measurement.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param temperature The temperature in degrees Celsius
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18b20_read_temperature(gpio_num_t pin, ds18x20_addr_t addr, int16_t *temperature);
|
||||
|
||||
/**
|
||||
* @brief Read the value from the last CONVERT_T operation (ds18s20 version).
|
||||
*
|
||||
* This should be called after ds18x20_measure() to fetch the result of the
|
||||
* temperature measurement.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param temperature The temperature in degrees Celsius
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18s20_read_temperature(gpio_num_t pin, ds18x20_addr_t addr, int16_t *temperature);
|
||||
|
||||
/**
|
||||
* @brief Read the value from the last CONVERT_T operation for multiple devices.
|
||||
*
|
||||
* This should be called after ds18x20_measure() to fetch the result of the
|
||||
* temperature measurement.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 bus
|
||||
* @param addr_list A list of addresses for devices to read.
|
||||
* @param addr_count The number of entries in `addr_list`.
|
||||
* @param result_list An array of int16_ts to hold the returned temperature
|
||||
* values. It should have at least `addr_count` entries.
|
||||
*
|
||||
* @returns `ESP_OK` if all temperatures were fetched successfully
|
||||
*/
|
||||
esp_err_t ds18x20_read_temp_multi(gpio_num_t pin, ds18x20_addr_t *addr_list, size_t addr_count, int16_t *result_list);
|
||||
|
||||
/** Perform a ds18x20_measure() followed by ds18s20_read_temperature()
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18s20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param temperature The temperature in degrees Celsius
|
||||
*/
|
||||
esp_err_t ds18s20_measure_and_read(gpio_num_t pin, ds18x20_addr_t addr, int16_t *temperature);
|
||||
|
||||
/** Perform a ds18x20_measure() followed by ds18b20_read_temperature()
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param temperature The temperature in degrees Celsius
|
||||
*/
|
||||
esp_err_t ds18b20_measure_and_read(gpio_num_t pin, ds18x20_addr_t addr, int16_t *temperature);
|
||||
|
||||
/** Perform a ds18x20_measure() followed by ds18x20_read_temperature()
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param temperature The temperature in degrees Celsius
|
||||
*/
|
||||
esp_err_t ds18x20_measure_and_read(gpio_num_t pin, ds18x20_addr_t addr, int16_t *temperature);
|
||||
|
||||
/**
|
||||
* @brief Perform a ds18x20_measure() followed by ds18x20_read_temp_multi()
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 bus
|
||||
* @param addr_list A list of addresses for devices to read.
|
||||
* @param addr_count The number of entries in `addr_list`.
|
||||
* @param result_list An array of int16_ts to hold the returned temperature
|
||||
* values. It should have at least `addr_count` entries.
|
||||
*
|
||||
* @returns `ESP_OK` if all temperatures were fetched successfully
|
||||
*/
|
||||
esp_err_t ds18x20_measure_and_read_multi(gpio_num_t pin, ds18x20_addr_t *addr_list, size_t addr_count, int16_t *result_list);
|
||||
|
||||
/**
|
||||
* @brief Read the scratchpad data for a particular ds18x20 device.
|
||||
*
|
||||
* This is not generally necessary to do directly. It is done automatically
|
||||
* as part of ds18x20_read_temperature().
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param buffer An 8-byte buffer to hold the read data.
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18x20_read_scratchpad(gpio_num_t pin, ds18x20_addr_t addr, uint8_t *buffer);
|
||||
|
||||
/**
|
||||
* @brief Write the scratchpad data for a particular ds18x20 device.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to write. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param buffer An 3-byte buffer to hold the data to write
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18x20_write_scratchpad(gpio_num_t pin, ds18x20_addr_t addr, uint8_t *buffer);
|
||||
|
||||
/**
|
||||
* @brief Issue the copy scratchpad command, copying current scratchpad to
|
||||
* EEPROM.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the ds18x20 device
|
||||
* @param addr The 64-bit address of the device to command. This can be set
|
||||
* to ::DS18X20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
*
|
||||
* @returns `ESP_OK` if the command was successfully issued
|
||||
*/
|
||||
esp_err_t ds18x20_copy_scratchpad(gpio_num_t pin, ds18x20_addr_t addr);
|
||||
|
||||
|
||||
#endif /* _DS18X20_H */
|
||||
@@ -1,277 +0,0 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 zeroday nodemcu.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* -------------------------------------------------------------------------------
|
||||
* Portions copyright (C) 2000 Dallas Semiconductor Corporation, under the
|
||||
* following additional terms:
|
||||
*
|
||||
* Except as contained in this notice, the name of Dallas Semiconductor
|
||||
* shall not be used except as stated in the Dallas Semiconductor
|
||||
* Branding Policy.
|
||||
*/
|
||||
|
||||
#ifndef ONEWIRE_H_
|
||||
#define ONEWIRE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "driver/gpio.h"
|
||||
|
||||
/**
|
||||
* Type used to hold all 1-Wire device ROM addresses (64-bit)
|
||||
*/
|
||||
typedef uint64_t onewire_addr_t;
|
||||
|
||||
/**
|
||||
* Structure to contain the current state for onewire_search_next(), etc
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t rom_no[8];
|
||||
uint8_t last_discrepancy;
|
||||
bool last_device_found;
|
||||
} onewire_search_t;
|
||||
|
||||
/**
|
||||
* ::ONEWIRE_NONE is an invalid ROM address that will never occur in a device
|
||||
* (CRC mismatch), and so can be useful as an indicator for "no-such-device",
|
||||
* etc.
|
||||
*/
|
||||
#define ONEWIRE_NONE ((onewire_addr_t)(0xffffffffffffffffLL))
|
||||
|
||||
/**
|
||||
* @brief Perform a 1-Wire reset cycle.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
*
|
||||
* @return `true` if at least one device responds with a presence pulse,
|
||||
* `false` if no devices were detected (or the bus is shorted, etc)
|
||||
*/
|
||||
bool onewire_reset(gpio_num_t pin);
|
||||
|
||||
/**
|
||||
* @brief Issue a 1-Wire "ROM select" command to select a particular device.
|
||||
*
|
||||
* It is necessary to call ::onewire_reset() before calling this function.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
* @param addr The ROM address of the device to select
|
||||
*
|
||||
* @return `true` if the "ROM select" command could be successfully issued,
|
||||
* `false` if there was an error.
|
||||
*/
|
||||
bool onewire_select(gpio_num_t pin, const onewire_addr_t addr);
|
||||
|
||||
/**
|
||||
* @brief Issue a 1-Wire "skip ROM" command to select *all* devices on the bus.
|
||||
*
|
||||
* It is necessary to call ::onewire_reset() before calling this function.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
*
|
||||
* @return `true` if the "skip ROM" command could be successfully issued,
|
||||
* `false` if there was an error.
|
||||
*/
|
||||
bool onewire_skip_rom(gpio_num_t pin);
|
||||
|
||||
/**
|
||||
* @brief Write a byte on the onewire bus.
|
||||
*
|
||||
* The writing code uses open-drain mode and expects the pullup resistor to
|
||||
* pull the line high when not driven low. If you need strong power after the
|
||||
* write (e.g. DS18B20 in parasite power mode) then call ::onewire_power()
|
||||
* after this is complete to actively drive the line high.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
* @param v The byte value to write
|
||||
*
|
||||
* @return `true` if successful, `false` on error.
|
||||
*/
|
||||
bool onewire_write(gpio_num_t pin, uint8_t v);
|
||||
|
||||
/**
|
||||
* @brief Write multiple bytes on the 1-Wire bus.
|
||||
*
|
||||
* See ::onewire_write() for more info.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
* @param buf A pointer to the buffer of bytes to be written
|
||||
* @param count Number of bytes to write
|
||||
*
|
||||
* @return `true` if all bytes written successfully, `false` on error.
|
||||
*/
|
||||
bool onewire_write_bytes(gpio_num_t pin, const uint8_t *buf, size_t count);
|
||||
|
||||
/**
|
||||
* @brief Read a byte from a 1-Wire device.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
*
|
||||
* @return the read byte on success, negative value on error.
|
||||
*/
|
||||
int onewire_read(gpio_num_t pin);
|
||||
|
||||
/**
|
||||
* @brief Read multiple bytes from a 1-Wire device.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
* @param[out] buf A pointer to the buffer to contain the read bytes
|
||||
* @param count Number of bytes to read
|
||||
*
|
||||
* @return `true` on success, `false` on error.
|
||||
*/
|
||||
bool onewire_read_bytes(gpio_num_t pin, uint8_t *buf, size_t count);
|
||||
|
||||
/**
|
||||
* @brief Actively drive the bus high to provide extra power for certain
|
||||
* operations of parasitically-powered devices.
|
||||
*
|
||||
* For parasitically-powered devices which need more power than can be
|
||||
* provided via the normal pull-up resistor, it may be necessary for some
|
||||
* operations to drive the bus actively high. This function can be used to
|
||||
* perform that operation.
|
||||
*
|
||||
* The bus can be depowered once it is no longer needed by calling
|
||||
* ::onewire_depower(), or it will be depowered automatically the next time
|
||||
* ::onewire_reset() is called to start another command.
|
||||
*
|
||||
* @note Make sure the device(s) you are powering will not pull more current
|
||||
* than the ESP32/ESP8266 is able to supply via its GPIO pins (this is
|
||||
* especially important when multiple devices are on the same bus and
|
||||
* they are all performing a power-intensive operation at the same time
|
||||
* (i.e. multiple DS18B20 sensors, which have all been given a
|
||||
* "convert T" operation by using ::onewire_skip_rom())).
|
||||
*
|
||||
* @note This routine will check to make sure that the bus is already high
|
||||
* before driving it, to make sure it doesn't attempt to drive it high
|
||||
* while something else is pulling it low (which could cause a reset or
|
||||
* damage the ESP32/ESP8266).
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
*
|
||||
* @return `true` on success, `false` on error.
|
||||
*/
|
||||
bool onewire_power(gpio_num_t pin);
|
||||
|
||||
/**
|
||||
* @brief Stop forcing power onto the bus.
|
||||
*
|
||||
* You only need to do this if you previously called ::onewire_power() to drive
|
||||
* the bus high and now want to allow it to float instead. Note that
|
||||
* onewire_reset() will also automatically depower the bus first, so you do
|
||||
* not need to call this first if you just want to start a new operation.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the 1-Wire bus.
|
||||
*/
|
||||
void onewire_depower(gpio_num_t pin);
|
||||
|
||||
/**
|
||||
* @brief Clear the search state so that it will start from the beginning on
|
||||
* the next call to ::onewire_search_next().
|
||||
*
|
||||
* @param[out] search The onewire_search_t structure to reset.
|
||||
*/
|
||||
void onewire_search_start(onewire_search_t *search);
|
||||
|
||||
/**
|
||||
* @brief Setup the search to search for devices with the specified
|
||||
* "family code".
|
||||
*
|
||||
* @param[out] search The onewire_search_t structure to update.
|
||||
* @param family_code The "family code" to search for.
|
||||
*/
|
||||
void onewire_search_prefix(onewire_search_t *search, uint8_t family_code);
|
||||
|
||||
/**
|
||||
* @brief Search for the next device on the bus.
|
||||
*
|
||||
* The order of returned device addresses is deterministic. You will always
|
||||
* get the same devices in the same order.
|
||||
*
|
||||
* @note It might be a good idea to check the CRC to make sure you didn't get
|
||||
* garbage.
|
||||
*
|
||||
* @return the address of the next device on the bus, or ::ONEWIRE_NONE if
|
||||
* there is no next address. ::ONEWIRE_NONE might also mean that
|
||||
* the bus is shorted, there are no devices, or you have already
|
||||
* retrieved all of them.
|
||||
*/
|
||||
onewire_addr_t onewire_search_next(onewire_search_t *search, gpio_num_t pin);
|
||||
|
||||
/**
|
||||
* @brief Compute a Dallas Semiconductor 8 bit CRC.
|
||||
*
|
||||
* These are used in the ROM address and scratchpad registers to verify the
|
||||
* transmitted data is correct.
|
||||
*/
|
||||
uint8_t onewire_crc8(const uint8_t *data, uint8_t len);
|
||||
|
||||
/**
|
||||
* @brief Compute the 1-Wire CRC16 and compare it against the received CRC.
|
||||
*
|
||||
* Example usage (reading a DS2408):
|
||||
* @code{.c}
|
||||
* // Put everything in a buffer so we can compute the CRC easily.
|
||||
* uint8_t buf[13];
|
||||
* buf[0] = 0xF0; // Read PIO Registers
|
||||
* buf[1] = 0x88; // LSB address
|
||||
* buf[2] = 0x00; // MSB address
|
||||
* onewire_write_bytes(pin, buf, 3); // Write 3 cmd bytes
|
||||
* onewire_read_bytes(pin, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
|
||||
* if (!onewire_check_crc16(buf, 11, &buf[11])) {
|
||||
* // TODO: Handle error.
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @param input Array of bytes to checksum.
|
||||
* @param len Number of bytes in `input`
|
||||
* @param inverted_crc The two CRC16 bytes in the received data.
|
||||
* This should just point into the received data,
|
||||
* *not* at a 16-bit integer.
|
||||
* @param crc_iv The crc starting value (optional)
|
||||
*
|
||||
* @return `true` if the CRC matches, `false` otherwise.
|
||||
*/
|
||||
bool onewire_check_crc16(const uint8_t* input, size_t len, const uint8_t* inverted_crc, uint16_t crc_iv);
|
||||
|
||||
/**
|
||||
* @brief Compute a Dallas Semiconductor 16 bit CRC.
|
||||
*
|
||||
* This is required to check the integrity of data received from many 1-Wire
|
||||
* devices. Note that the CRC computed here is *not* what you'll get from the
|
||||
* 1-Wire network, for two reasons:
|
||||
*
|
||||
* 1. The CRC is transmitted bitwise inverted.
|
||||
* 2. Depending on the endian-ness of your processor, the binary
|
||||
* representation of the two-byte return value may have a different
|
||||
* byte order than the two bytes you get from 1-Wire.
|
||||
*
|
||||
* @param input Array of bytes to checksum.
|
||||
* @param len How many bytes are in `input`.
|
||||
* @param crc_iv The crc starting value (optional)
|
||||
*
|
||||
* @return the CRC16, as defined by Dallas Semiconductor.
|
||||
*/
|
||||
uint16_t onewire_crc16(const uint8_t* input, size_t len, uint16_t crc_iv);
|
||||
|
||||
|
||||
#endif /* ONEWIRE_H_ */
|
||||
@@ -1,28 +1,41 @@
|
||||
// components/peripherals/src/adc121s021_dma.c
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "adc121s021_dma.h"
|
||||
#include "spi_bus_manager.h"
|
||||
|
||||
#define TAG "adc_dma"
|
||||
|
||||
#define PIN_NUM_CS 5
|
||||
#define SAMPLE_SIZE_BYTES 2
|
||||
#define ADC_BITS 12
|
||||
#define SPI_CLOCK_HZ (6 * 1000 * 1000) // 6 MHz
|
||||
// Pino de chip-select do ADC121S021 (ajusta se necessário)
|
||||
#define PIN_NUM_CS 5
|
||||
|
||||
// ADC é 12-bit, mas transferimos 16 bits via SPI
|
||||
#define ADC_BITS 12
|
||||
|
||||
// Clock SPI: 1 MHz → ~16 µs de transferência por amostra.
|
||||
// Com um pequeno delay entre leituras, 100 amostras ficam em ~2–3 ms,
|
||||
// o que é perfeito para analisar um PWM de 1 kHz a cada 100 ms.
|
||||
#define SPI_CLOCK_HZ (1 * 1000 * 1000)
|
||||
|
||||
static spi_device_handle_t adc_spi = NULL;
|
||||
|
||||
void adc121s021_dma_init(void)
|
||||
{
|
||||
if (adc_spi) {
|
||||
if (adc_spi)
|
||||
{
|
||||
ESP_LOGW(TAG, "ADC121S021 já foi inicializado.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!spi_bus_manager_is_initialized()) {
|
||||
// Garante que o SPI bus partilhado está configurado
|
||||
if (!spi_bus_manager_is_initialized())
|
||||
{
|
||||
ESP_LOGI(TAG, "SPI bus não inicializado. Inicializando...");
|
||||
esp_err_t err = spi_bus_manager_init(); // 🔧 CORRIGIDO: sem argumentos
|
||||
if (err != ESP_OK) {
|
||||
esp_err_t err = spi_bus_manager_init();
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Falha ao inicializar o SPI bus: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
@@ -32,44 +45,55 @@ void adc121s021_dma_init(void)
|
||||
.clock_speed_hz = SPI_CLOCK_HZ,
|
||||
.mode = 0,
|
||||
.spics_io_num = PIN_NUM_CS,
|
||||
.queue_size = 2,
|
||||
.queue_size = 2, // suficiente para uso em burst
|
||||
.flags = SPI_DEVICE_NO_DUMMY,
|
||||
.pre_cb = NULL,
|
||||
.post_cb = NULL,
|
||||
};
|
||||
|
||||
esp_err_t err = spi_bus_add_device(spi_bus_manager_get_host(), &devcfg, &adc_spi);
|
||||
if (err != ESP_OK) {
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Falha ao registrar ADC121S021 no SPI: %s", esp_err_to_name(err));
|
||||
adc_spi = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "ADC121S021 registrado no SPI com sucesso.");
|
||||
ESP_LOGI(TAG, "ADC121S021 registrado no SPI (CS=%d, fSPI=%d Hz).",
|
||||
PIN_NUM_CS, SPI_CLOCK_HZ);
|
||||
}
|
||||
|
||||
bool adc121s021_dma_get_sample(uint16_t *sample)
|
||||
{
|
||||
if (!adc_spi) {
|
||||
ESP_LOGE(TAG, "ADC SPI não inicializado!");
|
||||
if (!sample)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t tx_buffer[2] = {0x00, 0x00}; // Dummy
|
||||
if (!adc_spi)
|
||||
{
|
||||
ESP_LOGE(TAG, "ADC SPI não inicializado! Chama adc121s021_dma_init() primeiro.");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t tx_buffer[2] = {0x00, 0x00}; // Dummy (ADC só precisa de clock)
|
||||
uint8_t rx_buffer[2] = {0};
|
||||
|
||||
spi_transaction_t t = {
|
||||
.length = 16,
|
||||
.length = 16, // 16 bits
|
||||
.tx_buffer = tx_buffer,
|
||||
.rx_buffer = rx_buffer,
|
||||
.flags = 0
|
||||
};
|
||||
.flags = 0};
|
||||
|
||||
esp_err_t err = spi_device_transmit(adc_spi, &t);
|
||||
if (err != ESP_OK) {
|
||||
// Polling transmit → menor overhead que fila + espera.
|
||||
esp_err_t err = spi_device_polling_transmit(adc_spi, &t);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Erro na transmissão SPI: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
*sample = ((rx_buffer[0] << 8) | rx_buffer[1]) & 0x0FFF;
|
||||
// ADC121S021 devolve os 12 bits mais significativos em 16 bits.
|
||||
*sample = (uint16_t)(((rx_buffer[0] << 8) | rx_buffer[1]) & 0x0FFF);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,265 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Grzegorz Hetman <ghetman@gmail.com>
|
||||
* Copyright (c) 2016 Alex Stewart <foogod@gmail.com>
|
||||
* Copyright (c) 2018 Ruslan V. Uss <unclerus@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of itscontributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <math.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include "ds18x20.h"
|
||||
|
||||
#define ds18x20_WRITE_SCRATCHPAD 0x4E
|
||||
#define ds18x20_READ_SCRATCHPAD 0xBE
|
||||
#define ds18x20_COPY_SCRATCHPAD 0x48
|
||||
#define ds18x20_READ_EEPROM 0xB8
|
||||
#define ds18x20_READ_PWRSUPPLY 0xB4
|
||||
#define ds18x20_SEARCHROM 0xF0
|
||||
#define ds18x20_SKIP_ROM 0xCC
|
||||
#define ds18x20_READROM 0x33
|
||||
#define ds18x20_MATCHROM 0x55
|
||||
#define ds18x20_ALARMSEARCH 0xEC
|
||||
#define ds18x20_CONVERT_T 0x44
|
||||
|
||||
#define CHECK(x) do { esp_err_t __; if ((__ = x) != ESP_OK) return __; } while (0)
|
||||
#define CHECK_ARG(VAL) do { if (!(VAL)) return ESP_ERR_INVALID_ARG; } while (0)
|
||||
|
||||
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
static const char* TAG = "ds18x20";
|
||||
|
||||
esp_err_t ds18x20_measure(gpio_num_t pin, ds18x20_addr_t addr, bool wait)
|
||||
{
|
||||
if (!onewire_reset(pin))
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
|
||||
if (addr == DS18X20_ANY)
|
||||
onewire_skip_rom(pin);
|
||||
else
|
||||
onewire_select(pin, addr);
|
||||
|
||||
portENTER_CRITICAL(&mux);
|
||||
onewire_write(pin, ds18x20_CONVERT_T);
|
||||
// For parasitic devices, power must be applied within 10us after issuing
|
||||
// the convert command.
|
||||
onewire_power(pin);
|
||||
portEXIT_CRITICAL(&mux);
|
||||
|
||||
if (wait){
|
||||
vTaskDelay(pdMS_TO_TICKS(750));
|
||||
onewire_depower(pin);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_read_scratchpad(gpio_num_t pin, ds18x20_addr_t addr, uint8_t* buffer)
|
||||
{
|
||||
CHECK_ARG(buffer);
|
||||
|
||||
uint8_t crc;
|
||||
uint8_t expected_crc;
|
||||
|
||||
if (!onewire_reset(pin))
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
|
||||
if (addr == DS18X20_ANY)
|
||||
onewire_skip_rom(pin);
|
||||
else
|
||||
onewire_select(pin, addr);
|
||||
onewire_write(pin, ds18x20_READ_SCRATCHPAD);
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
buffer[i] = onewire_read(pin);
|
||||
crc = onewire_read(pin);
|
||||
|
||||
expected_crc = onewire_crc8(buffer, 8);
|
||||
if (crc != expected_crc)
|
||||
{
|
||||
ESP_LOGE(TAG, "CRC check failed reading scratchpad: %02x %02x %02x %02x %02x %02x %02x %02x : %02x (expected %02x)", buffer[0], buffer[1],
|
||||
buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], crc, expected_crc);
|
||||
return ESP_ERR_INVALID_CRC;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_write_scratchpad(gpio_num_t pin, ds18x20_addr_t addr, uint8_t* buffer)
|
||||
{
|
||||
CHECK_ARG(buffer);
|
||||
|
||||
if (!onewire_reset(pin))
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
|
||||
if (addr == DS18X20_ANY)
|
||||
onewire_skip_rom(pin);
|
||||
else
|
||||
onewire_select(pin, addr);
|
||||
onewire_write(pin, ds18x20_WRITE_SCRATCHPAD);
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
onewire_write(pin, buffer[i]);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_copy_scratchpad(gpio_num_t pin, ds18x20_addr_t addr)
|
||||
{
|
||||
if (!onewire_reset(pin))
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
|
||||
if (addr == DS18X20_ANY)
|
||||
onewire_skip_rom(pin);
|
||||
else
|
||||
onewire_select(pin, addr);
|
||||
|
||||
portENTER_CRITICAL(&mux);
|
||||
onewire_write(pin, ds18x20_COPY_SCRATCHPAD);
|
||||
// For parasitic devices, power must be applied within 10us after issuing
|
||||
// the convert command.
|
||||
onewire_power(pin);
|
||||
portEXIT_CRITICAL(&mux);
|
||||
|
||||
// And then it needs to keep that power up for 10ms.
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
onewire_depower(pin);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18b20_read_temperature(gpio_num_t pin, ds18x20_addr_t addr, int16_t* temperature)
|
||||
{
|
||||
CHECK_ARG(temperature);
|
||||
|
||||
uint8_t scratchpad[8];
|
||||
int16_t temp;
|
||||
|
||||
CHECK(ds18x20_read_scratchpad(pin, addr, scratchpad));
|
||||
|
||||
temp = scratchpad[1] << 8 | scratchpad[0];
|
||||
|
||||
*temperature = ((int16_t)temp * 625.0) / 100;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18s20_read_temperature(gpio_num_t pin, ds18x20_addr_t addr, int16_t* temperature)
|
||||
{
|
||||
CHECK_ARG(temperature);
|
||||
|
||||
uint8_t scratchpad[8];
|
||||
int16_t temp;
|
||||
|
||||
CHECK(ds18x20_read_scratchpad(pin, addr, scratchpad));
|
||||
|
||||
temp = scratchpad[1] << 8 | scratchpad[0];
|
||||
temp = ((temp & 0xfffe) << 3) + (16 - scratchpad[6]) - 4;
|
||||
|
||||
*temperature = (temp * 625) / 100 - 25;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_read_temperature(gpio_num_t pin, ds18x20_addr_t addr, int16_t* temperature)
|
||||
{
|
||||
if ((uint8_t)addr == DS18B20_FAMILY_ID) {
|
||||
return ds18b20_read_temperature(pin, addr, temperature);
|
||||
} else {
|
||||
return ds18s20_read_temperature(pin, addr, temperature);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t ds18b20_measure_and_read(gpio_num_t pin, ds18x20_addr_t addr, int16_t* temperature)
|
||||
{
|
||||
CHECK_ARG(temperature);
|
||||
|
||||
CHECK(ds18x20_measure(pin, addr, true));
|
||||
return ds18b20_read_temperature(pin, addr, temperature);
|
||||
}
|
||||
|
||||
esp_err_t ds18s20_measure_and_read(gpio_num_t pin, ds18x20_addr_t addr, int16_t* temperature)
|
||||
{
|
||||
CHECK_ARG(temperature);
|
||||
|
||||
CHECK(ds18x20_measure(pin, addr, true));
|
||||
return ds18s20_read_temperature(pin, addr, temperature);
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_measure_and_read(gpio_num_t pin, ds18x20_addr_t addr, int16_t* temperature)
|
||||
{
|
||||
CHECK_ARG(temperature);
|
||||
|
||||
CHECK(ds18x20_measure(pin, addr, true));
|
||||
return ds18x20_read_temperature(pin, addr, temperature);
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_measure_and_read_multi(gpio_num_t pin, ds18x20_addr_t* addr_list, size_t addr_count, int16_t* result_list)
|
||||
{
|
||||
CHECK_ARG(result_list && addr_count);
|
||||
|
||||
CHECK(ds18x20_measure(pin, DS18X20_ANY, true));
|
||||
|
||||
return ds18x20_read_temp_multi(pin, addr_list, addr_count, result_list);
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_scan_devices(gpio_num_t pin, ds18x20_addr_t* addr_list, size_t addr_count, size_t* found)
|
||||
{
|
||||
CHECK_ARG(addr_list && addr_count);
|
||||
|
||||
onewire_search_t search;
|
||||
onewire_addr_t addr;
|
||||
|
||||
*found = 0;
|
||||
onewire_search_start(&search);
|
||||
while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE)
|
||||
{
|
||||
uint8_t family_id = (uint8_t)addr;
|
||||
if (family_id == DS18B20_FAMILY_ID || family_id == DS18S20_FAMILY_ID)
|
||||
{
|
||||
if (*found < addr_count)
|
||||
addr_list[*found] = addr;
|
||||
*found += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ds18x20_read_temp_multi(gpio_num_t pin, ds18x20_addr_t* addr_list, size_t addr_count, int16_t* result_list)
|
||||
{
|
||||
CHECK_ARG(result_list);
|
||||
|
||||
esp_err_t res = ESP_OK;
|
||||
for (size_t i = 0; i < addr_count; i++)
|
||||
{
|
||||
esp_err_t tmp = ds18x20_read_temperature(pin, addr_list[i], &result_list[i]);
|
||||
if (tmp != ESP_OK)
|
||||
res = tmp;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -1,498 +0,0 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 zeroday nodemcu.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* -------------------------------------------------------------------------------
|
||||
* Portions copyright (C) 2000 Dallas Semiconductor Corporation, under the
|
||||
* following additional terms:
|
||||
*
|
||||
* Except as contained in this notice, the name of Dallas Semiconductor
|
||||
* shall not be used except as stated in the Dallas Semiconductor
|
||||
* Branding Policy.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include "rom/ets_sys.h"
|
||||
|
||||
#include "onewire.h"
|
||||
|
||||
#define ONEWIRE_SELECT_ROM 0x55
|
||||
#define ONEWIRE_SKIP_ROM 0xcc
|
||||
#define ONEWIRE_SEARCH 0xf0
|
||||
#define ONEWIRE_CRC8_TABLE
|
||||
|
||||
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
// Waits up to `max_wait` microseconds for the specified pin to go high.
|
||||
// Returns true if successful, false if the bus never comes high (likely
|
||||
// shorted).
|
||||
static inline bool _onewire_wait_for_bus(gpio_num_t pin, int max_wait)
|
||||
{
|
||||
bool state;
|
||||
for (int i = 0; i < ((max_wait + 4) / 5); i++) {
|
||||
if (gpio_get_level(pin))
|
||||
break;
|
||||
ets_delay_us(5);
|
||||
}
|
||||
state = gpio_get_level(pin);
|
||||
// Wait an extra 1us to make sure the devices have an adequate recovery
|
||||
// time before we drive things low again.
|
||||
ets_delay_us(1);
|
||||
return state;
|
||||
}
|
||||
|
||||
static void setup_pin(gpio_num_t pin, bool open_drain)
|
||||
{
|
||||
gpio_set_direction(pin, open_drain ? GPIO_MODE_INPUT_OUTPUT_OD : GPIO_MODE_OUTPUT);
|
||||
// gpio_set_pull_mode(pin, GPIO_PULLUP_ONLY);
|
||||
}
|
||||
|
||||
// Perform the onewire reset function. We will wait up to 250uS for
|
||||
// the bus to come high, if it doesn't then it is broken or shorted
|
||||
// and we return false;
|
||||
//
|
||||
// Returns true if a device asserted a presence pulse, false otherwise.
|
||||
//
|
||||
bool onewire_reset(gpio_num_t pin)
|
||||
{
|
||||
setup_pin(pin, true);
|
||||
|
||||
gpio_set_level(pin, 1);
|
||||
// wait until the wire is high... just in case
|
||||
if (!_onewire_wait_for_bus(pin, 250))
|
||||
return false;
|
||||
|
||||
gpio_set_level(pin, 0);
|
||||
ets_delay_us(480);
|
||||
|
||||
portENTER_CRITICAL(&mux);
|
||||
gpio_set_level(pin, 1); // allow it to float
|
||||
ets_delay_us(70);
|
||||
bool r = !gpio_get_level(pin);
|
||||
portEXIT_CRITICAL(&mux);
|
||||
|
||||
// Wait for all devices to finish pulling the bus low before returning
|
||||
if (!_onewire_wait_for_bus(pin, 410))
|
||||
return false;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static bool _onewire_write_bit(gpio_num_t pin, bool v)
|
||||
{
|
||||
if (!_onewire_wait_for_bus(pin, 10))
|
||||
return false;
|
||||
|
||||
portENTER_CRITICAL(&mux);
|
||||
if (v) {
|
||||
gpio_set_level(pin, 0); // drive output low
|
||||
ets_delay_us(10);
|
||||
gpio_set_level(pin, 1); // allow output high
|
||||
ets_delay_us(55);
|
||||
} else {
|
||||
gpio_set_level(pin, 0); // drive output low
|
||||
ets_delay_us(65);
|
||||
gpio_set_level(pin, 1); // allow output high
|
||||
}
|
||||
ets_delay_us(1);
|
||||
portEXIT_CRITICAL(&mux);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int _onewire_read_bit(gpio_num_t pin)
|
||||
{
|
||||
if (!_onewire_wait_for_bus(pin, 10))
|
||||
return -1;
|
||||
|
||||
portENTER_CRITICAL(&mux);
|
||||
gpio_set_level(pin, 0);
|
||||
ets_delay_us(2);
|
||||
gpio_set_level(pin, 1); // let pin float, pull up will raise
|
||||
ets_delay_us(11);
|
||||
int r = gpio_get_level(pin); // Must sample within 15us of start
|
||||
ets_delay_us(48);
|
||||
portEXIT_CRITICAL(&mux);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
// Write a byte. The writing code uses open-drain mode and expects the pullup
|
||||
// resistor to pull the line high when not driven low. If you need strong
|
||||
// power after the write (e.g. DS18B20 in parasite power mode) then call
|
||||
// onewire_power() after this is complete to actively drive the line high.
|
||||
//
|
||||
bool onewire_write(gpio_num_t pin, uint8_t v)
|
||||
{
|
||||
for (uint8_t bitMask = 0x01; bitMask; bitMask <<= 1)
|
||||
if (!_onewire_write_bit(pin, (bitMask & v)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onewire_write_bytes(gpio_num_t pin, const uint8_t* buf, size_t count)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
if (!onewire_write(pin, buf[i]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Read a byte
|
||||
//
|
||||
int onewire_read(gpio_num_t pin)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
for (uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
|
||||
int bit = _onewire_read_bit(pin);
|
||||
if (bit < 0)
|
||||
return -1;
|
||||
else if (bit)
|
||||
r |= bitMask;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
bool onewire_read_bytes(gpio_num_t pin, uint8_t* buf, size_t count)
|
||||
{
|
||||
size_t i;
|
||||
int b;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
b = onewire_read(pin);
|
||||
if (b < 0)
|
||||
return false;
|
||||
buf[i] = b;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onewire_select(gpio_num_t pin, onewire_addr_t addr)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
if (!onewire_write(pin, ONEWIRE_SELECT_ROM))
|
||||
return false;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (!onewire_write(pin, addr & 0xff))
|
||||
return false;
|
||||
addr >>= 8;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onewire_skip_rom(gpio_num_t pin)
|
||||
{
|
||||
return onewire_write(pin, ONEWIRE_SKIP_ROM);
|
||||
}
|
||||
|
||||
bool onewire_power(gpio_num_t pin)
|
||||
{
|
||||
// Make sure the bus is not being held low before driving it high, or we
|
||||
// may end up shorting ourselves out.
|
||||
if (!_onewire_wait_for_bus(pin, 10))
|
||||
return false;
|
||||
|
||||
setup_pin(pin, false);
|
||||
gpio_set_level(pin, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void onewire_depower(gpio_num_t pin)
|
||||
{
|
||||
setup_pin(pin, true);
|
||||
}
|
||||
|
||||
void onewire_search_start(onewire_search_t* search)
|
||||
{
|
||||
// reset the search state
|
||||
memset(search, 0, sizeof(*search));
|
||||
}
|
||||
|
||||
void onewire_search_prefix(onewire_search_t* search, uint8_t family_code)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
search->rom_no[0] = family_code;
|
||||
for (i = 1; i < 8; i++) {
|
||||
search->rom_no[i] = 0;
|
||||
}
|
||||
search->last_discrepancy = 64;
|
||||
search->last_device_found = false;
|
||||
}
|
||||
|
||||
// Perform a search. If the next device has been successfully enumerated, its
|
||||
// ROM address will be returned. If there are no devices, no further
|
||||
// devices, or something horrible happens in the middle of the
|
||||
// enumeration then ONEWIRE_NONE is returned. Use OneWire::reset_search() to
|
||||
// start over.
|
||||
//
|
||||
// --- Replaced by the one from the Dallas Semiconductor web site ---
|
||||
//--------------------------------------------------------------------------
|
||||
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
|
||||
// search state.
|
||||
// Return 1 : device found, ROM number in ROM_NO buffer
|
||||
// 0 : device not found, end of search
|
||||
//
|
||||
onewire_addr_t onewire_search_next(onewire_search_t* search, gpio_num_t pin)
|
||||
{
|
||||
//TODO: add more checking for read/write errors
|
||||
uint8_t id_bit_number;
|
||||
uint8_t last_zero, search_result;
|
||||
int rom_byte_number;
|
||||
int8_t id_bit, cmp_id_bit;
|
||||
onewire_addr_t addr;
|
||||
unsigned char rom_byte_mask;
|
||||
bool search_direction;
|
||||
|
||||
// initialize for search
|
||||
id_bit_number = 1;
|
||||
last_zero = 0;
|
||||
rom_byte_number = 0;
|
||||
rom_byte_mask = 1;
|
||||
search_result = 0;
|
||||
|
||||
// if the last call was not the last one
|
||||
if (!search->last_device_found) {
|
||||
// 1-Wire reset
|
||||
if (!onewire_reset(pin)) {
|
||||
// reset the search
|
||||
search->last_discrepancy = 0;
|
||||
search->last_device_found = false;
|
||||
return ONEWIRE_NONE;
|
||||
}
|
||||
|
||||
// issue the search command
|
||||
onewire_write(pin, ONEWIRE_SEARCH);
|
||||
|
||||
// loop to do the search
|
||||
do {
|
||||
// read a bit and its complement
|
||||
id_bit = _onewire_read_bit(pin);
|
||||
cmp_id_bit = _onewire_read_bit(pin);
|
||||
|
||||
if ((id_bit == 1) && (cmp_id_bit == 1))
|
||||
break;
|
||||
else {
|
||||
// all devices coupled have 0 or 1
|
||||
if (id_bit != cmp_id_bit)
|
||||
search_direction = id_bit; // bit write value for search
|
||||
else {
|
||||
// if this discrepancy if before the Last Discrepancy
|
||||
// on a previous next then pick the same as last time
|
||||
if (id_bit_number < search->last_discrepancy)
|
||||
search_direction = ((search->rom_no[rom_byte_number] & rom_byte_mask) > 0);
|
||||
else
|
||||
// if equal to last pick 1, if not then pick 0
|
||||
search_direction = (id_bit_number == search->last_discrepancy);
|
||||
|
||||
// if 0 was picked then record its position in LastZero
|
||||
if (!search_direction)
|
||||
last_zero = id_bit_number;
|
||||
}
|
||||
|
||||
// set or clear the bit in the ROM byte rom_byte_number
|
||||
// with mask rom_byte_mask
|
||||
if (search_direction)
|
||||
search->rom_no[rom_byte_number] |= rom_byte_mask;
|
||||
else
|
||||
search->rom_no[rom_byte_number] &= ~rom_byte_mask;
|
||||
|
||||
// serial number search direction write bit
|
||||
_onewire_write_bit(pin, search_direction);
|
||||
|
||||
// increment the byte counter id_bit_number
|
||||
// and shift the mask rom_byte_mask
|
||||
id_bit_number++;
|
||||
rom_byte_mask <<= 1;
|
||||
|
||||
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
|
||||
if (rom_byte_mask == 0) {
|
||||
rom_byte_number++;
|
||||
rom_byte_mask = 1;
|
||||
}
|
||||
}
|
||||
} while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
|
||||
|
||||
// if the search was successful then
|
||||
if (!(id_bit_number < 65)) {
|
||||
// search successful so set last_discrepancy,last_device_found,search_result
|
||||
search->last_discrepancy = last_zero;
|
||||
|
||||
// check for last device
|
||||
if (search->last_discrepancy == 0)
|
||||
search->last_device_found = true;
|
||||
|
||||
search_result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// if no device found then reset counters so next 'search' will be like a first
|
||||
if (!search_result || !search->rom_no[0]) {
|
||||
search->last_discrepancy = 0;
|
||||
search->last_device_found = false;
|
||||
return ONEWIRE_NONE;
|
||||
} else {
|
||||
addr = 0;
|
||||
for (rom_byte_number = 7; rom_byte_number >= 0; rom_byte_number--) {
|
||||
addr = (addr << 8) | search->rom_no[rom_byte_number];
|
||||
}
|
||||
//printf("Ok I found something at %08x%08x...\n", (uint32_t)(addr >> 32), (uint32_t)addr);
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
// The 1-Wire CRC scheme is described in Maxim Application Note 27:
|
||||
// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
|
||||
//
|
||||
|
||||
#ifdef ONEWIRE_CRC8_TABLE
|
||||
// This table comes from Dallas sample code where it is freely reusable,
|
||||
// though Copyright (c) 2000 Dallas Semiconductor Corporation
|
||||
static const uint8_t dscrc_table[] = {
|
||||
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
|
||||
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
|
||||
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
|
||||
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
|
||||
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
|
||||
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
|
||||
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
|
||||
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
|
||||
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
|
||||
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
|
||||
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
|
||||
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
|
||||
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
|
||||
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
|
||||
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
|
||||
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
|
||||
};
|
||||
|
||||
//
|
||||
// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM
|
||||
// and the registers. (note: this might better be done without to
|
||||
// table, it would probably be smaller and certainly fast enough
|
||||
// compared to all those delayMicrosecond() calls. But I got
|
||||
// confused, so I use this table from the examples.)
|
||||
//
|
||||
uint8_t onewire_crc8(const uint8_t* data, uint8_t len)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
|
||||
while (len--)
|
||||
crc = dscrc_table[crc ^ *data++];
|
||||
|
||||
return crc;
|
||||
}
|
||||
#else
|
||||
//
|
||||
// Compute a Dallas Semiconductor 8 bit CRC directly.
|
||||
// this is much slower, but much smaller, than the lookup table.
|
||||
//
|
||||
uint8_t onewire_crc8(const uint8_t* data, uint8_t len)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
|
||||
while (len--)
|
||||
{
|
||||
uint8_t inbyte = *data++;
|
||||
for (int i = 8; i; i--)
|
||||
{
|
||||
uint8_t mix = (crc ^ inbyte) & 0x01;
|
||||
crc >>= 1;
|
||||
if (mix)
|
||||
crc ^= 0x8C;
|
||||
inbyte >>= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
#endif /* ONEWIRE_CRC8_TABLE */
|
||||
|
||||
// Compute the 1-Wire CRC16 and compare it against the received CRC.
|
||||
// Example usage (reading a DS2408):
|
||||
// // Put everything in a buffer so we can compute the CRC easily.
|
||||
// uint8_t buf[13];
|
||||
// buf[0] = 0xF0; // Read PIO Registers
|
||||
// buf[1] = 0x88; // LSB address
|
||||
// buf[2] = 0x00; // MSB address
|
||||
// WriteBytes(net, buf, 3); // Write 3 cmd bytes
|
||||
// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
|
||||
// if (!CheckCRC16(buf, 11, &buf[11])) {
|
||||
// // Handle error.
|
||||
// }
|
||||
//
|
||||
// @param input - Array of bytes to checksum.
|
||||
// @param len - How many bytes to use.
|
||||
// @param inverted_crc - The two CRC16 bytes in the received data.
|
||||
// This should just point into the received data,
|
||||
// *not* at a 16-bit integer.
|
||||
// @param crc - The crc starting value (optional)
|
||||
// @return 1, iff the CRC matches.
|
||||
bool onewire_check_crc16(const uint8_t* input, size_t len, const uint8_t* inverted_crc, uint16_t crc_iv)
|
||||
{
|
||||
uint16_t crc = ~onewire_crc16(input, len, crc_iv);
|
||||
return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
|
||||
}
|
||||
|
||||
// Compute a Dallas Semiconductor 16 bit CRC. This is required to check
|
||||
// the integrity of data received from many 1-Wire devices. Note that the
|
||||
// CRC computed here is *not* what you'll get from the 1-Wire network,
|
||||
// for two reasons:
|
||||
// 1) The CRC is transmitted bitwise inverted.
|
||||
// 2) Depending on the endian-ness of your processor, the binary
|
||||
// representation of the two-byte return value may have a different
|
||||
// byte order than the two bytes you get from 1-Wire.
|
||||
// @param input - Array of bytes to checksum.
|
||||
// @param len - How many bytes to use.
|
||||
// @param crc - The crc starting value (optional)
|
||||
// @return The CRC16, as defined by Dallas Semiconductor.
|
||||
uint16_t onewire_crc16(const uint8_t* input, size_t len, uint16_t crc_iv)
|
||||
{
|
||||
uint16_t crc = crc_iv;
|
||||
static const uint8_t oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
|
||||
|
||||
uint16_t i;
|
||||
for (i = 0; i < len; i++) {
|
||||
// Even though we're just copying a byte from the input,
|
||||
// we'll be doing 16-bit computation with it.
|
||||
uint16_t cdata = input[i];
|
||||
cdata = (cdata ^ crc) & 0xff;
|
||||
crc >>= 8;
|
||||
|
||||
if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4])
|
||||
crc ^= 0xC001;
|
||||
|
||||
cdata <<= 6;
|
||||
crc ^= cdata;
|
||||
cdata <<= 1;
|
||||
crc ^= cdata;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "peripherals.h"
|
||||
#include "adc.h"
|
||||
//#include "led.h"
|
||||
// #include "buzzer.h"
|
||||
#include "proximity.h"
|
||||
#include "ac_relay.h"
|
||||
#include "socket_lock.h"
|
||||
@@ -11,13 +9,9 @@
|
||||
void peripherals_init(void)
|
||||
{
|
||||
ac_relay_init();
|
||||
// led_init();
|
||||
// buzzer_init();
|
||||
adc_init();
|
||||
proximity_init();
|
||||
// socket_lock_init();
|
||||
// rcm_init();
|
||||
// energy_meter_init();
|
||||
// aux_init();
|
||||
ntc_sensor_init();
|
||||
}
|
||||
@@ -4,12 +4,15 @@
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "nvs.h"
|
||||
|
||||
#include "socket_lock.h"
|
||||
#include "board_config.h"
|
||||
|
||||
// NEW:
|
||||
#include "storage_service.h"
|
||||
|
||||
#define NVS_NAMESPACE "socket_lock"
|
||||
#define NVS_OPERATING_TIME "op_time"
|
||||
#define NVS_BREAK_TIME "break_time"
|
||||
@@ -27,20 +30,68 @@
|
||||
|
||||
static const char* TAG = "socket_lock";
|
||||
|
||||
static nvs_handle_t nvs;
|
||||
// Storage timeouts (ajusta se quiseres)
|
||||
#define STORE_TO pdMS_TO_TICKS(800)
|
||||
#define STORE_FLUSH_TO pdMS_TO_TICKS(2000)
|
||||
|
||||
static uint16_t operating_time = 300;
|
||||
|
||||
static uint16_t break_time = 1000;
|
||||
|
||||
static bool detection_high;
|
||||
|
||||
static bool detection_high = false;
|
||||
static uint8_t retry_count = 5;
|
||||
|
||||
static socket_lock_status_t status;
|
||||
|
||||
static TaskHandle_t socket_lock_task;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Helpers storage (best effort) - iguais ao estilo do wifi.c
|
||||
// -----------------------------------------------------------------------------
|
||||
static esp_err_t store_flush_best_effort(void)
|
||||
{
|
||||
esp_err_t e = storage_flush_sync(STORE_FLUSH_TO);
|
||||
if (e != ESP_OK)
|
||||
ESP_LOGW(TAG, "storage_flush_sync failed: %s", esp_err_to_name(e));
|
||||
return e;
|
||||
}
|
||||
|
||||
static esp_err_t store_set_u8_best_effort(const char *ns, const char *key, uint8_t v)
|
||||
{
|
||||
for (int attempt = 0; attempt < 3; ++attempt)
|
||||
{
|
||||
esp_err_t e = storage_set_u8_async(ns, key, v);
|
||||
if (e == ESP_OK) return ESP_OK;
|
||||
|
||||
if (e == ESP_ERR_TIMEOUT)
|
||||
{
|
||||
(void)store_flush_best_effort();
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
continue;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
static esp_err_t store_set_u16_best_effort(const char *ns, const char *key, uint16_t v)
|
||||
{
|
||||
for (int attempt = 0; attempt < 3; ++attempt)
|
||||
{
|
||||
esp_err_t e = storage_set_u16_async(ns, key, v);
|
||||
if (e == ESP_OK) return ESP_OK;
|
||||
|
||||
if (e == ESP_ERR_TIMEOUT)
|
||||
{
|
||||
(void)store_flush_best_effort();
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
continue;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Lock logic
|
||||
// -----------------------------------------------------------------------------
|
||||
static bool is_locked(void)
|
||||
{
|
||||
gpio_set_level(board_config.socket_lock_a_gpio, 1);
|
||||
@@ -58,31 +109,42 @@ bool socket_lock_is_locked_state(void)
|
||||
|
||||
static void socket_lock_task_func(void* param)
|
||||
{
|
||||
uint32_t notification;
|
||||
(void)param;
|
||||
|
||||
uint32_t notification;
|
||||
TickType_t previous_tick = 0;
|
||||
uint8_t attempt = 0;
|
||||
|
||||
while (true) {
|
||||
if (xTaskNotifyWait(0x00, 0xff, ¬ification, portMAX_DELAY)) {
|
||||
if (notification & (LOCK_BIT | UNLOCK_BIT)) {
|
||||
while (true)
|
||||
{
|
||||
if (xTaskNotifyWait(0x00, 0xff, ¬ification, portMAX_DELAY))
|
||||
{
|
||||
if (notification & (LOCK_BIT | UNLOCK_BIT))
|
||||
{
|
||||
attempt = retry_count;
|
||||
}
|
||||
|
||||
if (notification & (UNLOCK_BIT | REPEAT_UNLOCK_BIT)) {
|
||||
if (notification & (UNLOCK_BIT | REPEAT_UNLOCK_BIT))
|
||||
{
|
||||
gpio_set_level(board_config.socket_lock_a_gpio, 0);
|
||||
gpio_set_level(board_config.socket_lock_b_gpio, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(operating_time));
|
||||
|
||||
if (!is_locked()) {
|
||||
if (!is_locked())
|
||||
{
|
||||
ESP_LOGI(TAG, "Unlock OK");
|
||||
status = SOCKED_LOCK_STATUS_IDLE;
|
||||
} else {
|
||||
if (attempt > 1) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (attempt > 1)
|
||||
{
|
||||
ESP_LOGW(TAG, "Not unlocked yet, repeating...");
|
||||
attempt--;
|
||||
xTaskNotify(socket_lock_task, REPEAT_UNLOCK_BIT, eSetBits);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Not unlocked");
|
||||
status = SOCKED_LOCK_STATUS_UNLOCKING_FAIL;
|
||||
}
|
||||
@@ -90,23 +152,33 @@ static void socket_lock_task_func(void* param)
|
||||
|
||||
gpio_set_level(board_config.socket_lock_a_gpio, 0);
|
||||
gpio_set_level(board_config.socket_lock_b_gpio, 0);
|
||||
} else if (notification & (LOCK_BIT | REPEAT_LOCK_BIT)) {
|
||||
if (notification & LOCK_BIT) {
|
||||
vTaskDelay(pdMS_TO_TICKS(LOCK_DELAY)); //delay before first lock attempt
|
||||
}
|
||||
else if (notification & (LOCK_BIT | REPEAT_LOCK_BIT))
|
||||
{
|
||||
if (notification & LOCK_BIT)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(LOCK_DELAY)); // delay before first lock attempt
|
||||
}
|
||||
|
||||
gpio_set_level(board_config.socket_lock_a_gpio, 1);
|
||||
gpio_set_level(board_config.socket_lock_b_gpio, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(operating_time));
|
||||
|
||||
if (is_locked()) {
|
||||
if (is_locked())
|
||||
{
|
||||
ESP_LOGI(TAG, "Lock OK");
|
||||
status = SOCKED_LOCK_STATUS_IDLE;
|
||||
} else {
|
||||
if (attempt > 1) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (attempt > 1)
|
||||
{
|
||||
ESP_LOGW(TAG, "Not locked yet, repeating...");
|
||||
attempt--;
|
||||
xTaskNotify(socket_lock_task, REPEAT_LOCK_BIT, eSetBits);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Not locked");
|
||||
status = SOCKED_LOCK_STATUS_LOCKING_FAIL;
|
||||
}
|
||||
@@ -117,7 +189,8 @@ static void socket_lock_task_func(void* param)
|
||||
}
|
||||
|
||||
TickType_t delay_tick = xTaskGetTickCount() - previous_tick;
|
||||
if (delay_tick < pdMS_TO_TICKS(break_time)) {
|
||||
if (delay_tick < pdMS_TO_TICKS(break_time))
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(break_time) - delay_tick);
|
||||
}
|
||||
previous_tick = xTaskGetTickCount();
|
||||
@@ -125,34 +198,56 @@ static void socket_lock_task_func(void* param)
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Init / API pública
|
||||
// -----------------------------------------------------------------------------
|
||||
void socket_lock_init(void)
|
||||
{
|
||||
if (board_config.socket_lock) {
|
||||
ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs));
|
||||
if (!board_config.socket_lock)
|
||||
return;
|
||||
|
||||
nvs_get_u16(nvs, NVS_OPERATING_TIME, &operating_time);
|
||||
// garante storage pronto
|
||||
esp_err_t se = storage_service_init();
|
||||
if (se != ESP_OK)
|
||||
ESP_LOGW(TAG, "storage_service_init failed: %s", esp_err_to_name(se));
|
||||
|
||||
nvs_get_u16(nvs, NVS_BREAK_TIME, &break_time);
|
||||
// Load config (best effort; se não existir, fica default)
|
||||
{
|
||||
uint16_t u16 = 0;
|
||||
uint8_t u8 = 0;
|
||||
|
||||
nvs_get_u8(nvs, NVS_RETRY_COUNT, &retry_count);
|
||||
esp_err_t e = storage_get_u16_sync(NVS_NAMESPACE, NVS_OPERATING_TIME, &u16, STORE_TO);
|
||||
if (e == ESP_OK) operating_time = u16;
|
||||
else if (e != ESP_ERR_NOT_FOUND)
|
||||
ESP_LOGW(TAG, "load %s failed: %s", NVS_OPERATING_TIME, esp_err_to_name(e));
|
||||
|
||||
uint8_t u8;
|
||||
if (nvs_get_u8(nvs, NVS_DETECTION_HIGH, &u8) == ESP_OK) {
|
||||
detection_high = u8;
|
||||
}
|
||||
e = storage_get_u16_sync(NVS_NAMESPACE, NVS_BREAK_TIME, &u16, STORE_TO);
|
||||
if (e == ESP_OK) break_time = u16;
|
||||
else if (e != ESP_ERR_NOT_FOUND)
|
||||
ESP_LOGW(TAG, "load %s failed: %s", NVS_BREAK_TIME, esp_err_to_name(e));
|
||||
|
||||
gpio_config_t io_conf = {};
|
||||
e = storage_get_u8_sync(NVS_NAMESPACE, NVS_RETRY_COUNT, &u8, STORE_TO);
|
||||
if (e == ESP_OK) retry_count = u8;
|
||||
else if (e != ESP_ERR_NOT_FOUND)
|
||||
ESP_LOGW(TAG, "load %s failed: %s", NVS_RETRY_COUNT, esp_err_to_name(e));
|
||||
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
io_conf.pin_bit_mask = BIT64(board_config.socket_lock_a_gpio) | BIT64(board_config.socket_lock_b_gpio);
|
||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = BIT64(board_config.socket_lock_detection_gpio);
|
||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||
|
||||
xTaskCreate(socket_lock_task_func, "socket_lock_task", 2 * 1024, NULL, 10, &socket_lock_task);
|
||||
e = storage_get_u8_sync(NVS_NAMESPACE, NVS_DETECTION_HIGH, &u8, STORE_TO);
|
||||
if (e == ESP_OK) detection_high = (u8 != 0);
|
||||
else if (e != ESP_ERR_NOT_FOUND)
|
||||
ESP_LOGW(TAG, "load %s failed: %s", NVS_DETECTION_HIGH, esp_err_to_name(e));
|
||||
}
|
||||
|
||||
gpio_config_t io_conf = {};
|
||||
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
io_conf.pin_bit_mask = BIT64(board_config.socket_lock_a_gpio) | BIT64(board_config.socket_lock_b_gpio);
|
||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = BIT64(board_config.socket_lock_detection_gpio);
|
||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||
|
||||
xTaskCreate(socket_lock_task_func, "socket_lock_task", 2 * 1024, NULL, 2, &socket_lock_task);
|
||||
}
|
||||
|
||||
bool socket_lock_is_detection_high(void)
|
||||
@@ -164,8 +259,11 @@ void socket_lock_set_detection_high(bool _detection_high)
|
||||
{
|
||||
detection_high = _detection_high;
|
||||
|
||||
nvs_set_u8(nvs, NVS_DETECTION_HIGH, detection_high);
|
||||
nvs_commit(nvs);
|
||||
esp_err_t e = store_set_u8_best_effort(NVS_NAMESPACE, NVS_DETECTION_HIGH, detection_high ? 1 : 0);
|
||||
if (e != ESP_OK)
|
||||
ESP_LOGW(TAG, "persist detect_hi failed: %s", esp_err_to_name(e));
|
||||
|
||||
(void)store_flush_best_effort();
|
||||
}
|
||||
|
||||
uint16_t socket_lock_get_operating_time(void)
|
||||
@@ -175,15 +273,22 @@ uint16_t socket_lock_get_operating_time(void)
|
||||
|
||||
esp_err_t socket_lock_set_operating_time(uint16_t _operating_time)
|
||||
{
|
||||
if (_operating_time < OPERATING_TIME_MIN || _operating_time > OPERATING_TIME_MAX) {
|
||||
if (_operating_time < OPERATING_TIME_MIN || _operating_time > OPERATING_TIME_MAX)
|
||||
{
|
||||
ESP_LOGE(TAG, "Operating time out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
operating_time = _operating_time;
|
||||
nvs_set_u16(nvs, NVS_OPERATING_TIME, operating_time);
|
||||
nvs_commit(nvs);
|
||||
|
||||
esp_err_t e = store_set_u16_best_effort(NVS_NAMESPACE, NVS_OPERATING_TIME, operating_time);
|
||||
if (e != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "persist op_time failed: %s", esp_err_to_name(e));
|
||||
return e;
|
||||
}
|
||||
|
||||
(void)store_flush_best_effort();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -195,8 +300,12 @@ uint8_t socket_lock_get_retry_count(void)
|
||||
void socket_lock_set_retry_count(uint8_t _retry_count)
|
||||
{
|
||||
retry_count = _retry_count;
|
||||
nvs_set_u8(nvs, NVS_RETRY_COUNT, retry_count);
|
||||
nvs_commit(nvs);
|
||||
|
||||
esp_err_t e = store_set_u8_best_effort(NVS_NAMESPACE, NVS_RETRY_COUNT, retry_count);
|
||||
if (e != ESP_OK)
|
||||
ESP_LOGW(TAG, "persist retry_count failed: %s", esp_err_to_name(e));
|
||||
|
||||
(void)store_flush_best_effort();
|
||||
}
|
||||
|
||||
uint16_t socket_lock_get_break_time(void)
|
||||
@@ -206,15 +315,22 @@ uint16_t socket_lock_get_break_time(void)
|
||||
|
||||
esp_err_t socket_lock_set_break_time(uint16_t _break_time)
|
||||
{
|
||||
if (_break_time < board_config.socket_lock_min_break_time) {
|
||||
ESP_LOGE(TAG, "Operating time out of range");
|
||||
if (_break_time < board_config.socket_lock_min_break_time)
|
||||
{
|
||||
ESP_LOGE(TAG, "Break time out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
break_time = _break_time;
|
||||
nvs_set_u16(nvs, NVS_BREAK_TIME, break_time);
|
||||
nvs_commit(nvs);
|
||||
|
||||
esp_err_t e = store_set_u16_best_effort(NVS_NAMESPACE, NVS_BREAK_TIME, break_time);
|
||||
if (e != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "persist break_time failed: %s", esp_err_to_name(e));
|
||||
return e;
|
||||
}
|
||||
|
||||
(void)store_flush_best_effort();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -229,4 +345,4 @@ void socket_lock_set_locked(bool locked)
|
||||
socket_lock_status_t socket_lock_get_status(void)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ void temp_sensor_init(void)
|
||||
|
||||
lm75a_init();
|
||||
|
||||
xTaskCreate(temp_sensor_task_func, "temp_sensor_task", 5 * 1024, NULL, 5, NULL);
|
||||
xTaskCreate(temp_sensor_task_func, "temp_sensor_task", 5 * 1024, NULL, 2, NULL);
|
||||
}
|
||||
|
||||
uint8_t temp_sensor_get_count(void)
|
||||
|
||||
Reference in New Issue
Block a user