Updated zh_onewire

This commit is contained in:
2024-05-31 10:42:39 +03:00
parent 97c8418bda
commit fe1cabef6d
4 changed files with 54 additions and 19 deletions

View File

@ -13,7 +13,7 @@ In an existing project, run the following command to install the component:
```text
cd ../your_project/components
git clone http://git.zh.com.ru/alexey.zholtikov/zh_onewire.git
git clone https://github.com/aZholtikov/zh_onewire.git
```
In the application, add the component:

View File

@ -20,9 +20,9 @@ extern "C"
#endif
/**
* @brief Initialize 1-Wire interface.
* @brief Initialize 1-Wire interface.
*
* @param[in] pin 1-Wire bus gpio connection.
* @param[in] pin 1-Wire bus gpio connection.
*
* @return
* - ESP_OK if initialization was successful
@ -31,59 +31,61 @@ extern "C"
esp_err_t zh_onewire_init(const uint8_t pin);
/**
* @brief Reset command for all 1-Wire devices on bus.
* @brief Reset command for all 1-Wire devices on bus.
*
* @return
* - ESP_OK if reset was successful
* - ESP_ERR_INVALID_STATE if 1-Wire bus not initialized
* - ESP_ERR_INVALID_RESPONSE if the bus is busy
* - ESP_ERR_TIMEOUT if there are no 1-Wire devices available on the bus or the devices are not responding
*/
esp_err_t zh_onewire_reset(void);
/**
* @brief Send one byte to 1-Wire device.
*
* @param[in] byte Byte value.
* @brief Send one byte to 1-Wire device.
*
* @param[in] byte Byte value.
*/
void zh_onewire_send_byte(uint8_t byte);
/**
* @brief Read one byte from 1-Wire device.
* @brief Read one byte from 1-Wire device.
*
* @return
* - Byte value
* @return Byte value
*/
uint8_t zh_onewire_read_byte(void);
/**
* @brief Initializes the bus master to address all 1-Wire devices on the bus.
* @brief Initialize the bus master to address all 1-Wire devices on the bus.
*
* @return
* - ESP_OK if initialization was successful
* - ESP_ERR_INVALID_STATE if 1-Wire bus not initialized
* - ESP_FAIL if there are no 1-Wire devices available on the bus or the devices are not responding
*/
esp_err_t zh_onewire_skip_rom(void);
/**
* @brief Read rom value if only one 1-Wire device is present on the bus.
* @brief Read rom value if only one 1-Wire device is present on the bus.
*
* @param[out] buf Pointer to a buffer containing an eight-byte rom value.
* @param[out] buf Pointer to a buffer containing an eight-byte rom value.
*
* @return
* - ESP_OK if read was successful
* - ESP_ERR_INVALID_STATE if 1-Wire bus not initialized
* - ESP_FAIL if there are no 1-Wire devices available on the bus or the devices are not responding
* - ESP_ERR_INVALID_CRC if more than one 1-Wire device is present on the bus
*/
esp_err_t zh_onewire_read_rom(uint8_t *buf);
/**
* @brief Initialize the bus master to address a specific 1-Wire device on bus.
* @brief Initialize the bus master to address a specific 1-Wire device on bus.
*
* @param[in] data Pointer to a buffer containing an eight-byte rom value.
* @param[in] data Pointer to a buffer containing an eight-byte rom value.
*
* @return
* - ESP_OK if initialization was successful
* - ESP_ERR_INVALID_STATE if 1-Wire bus not initialized
* - ESP_FAIL if there are no 1-Wire devices available on the bus or the devices are not responding
*/
esp_err_t zh_onewire_match_rom(const uint8_t *data);
@ -93,17 +95,18 @@ extern "C"
*
* @return
* - ESP_OK if initialization was successful
* - ESP_ERR_INVALID_STATE if 1-Wire bus not initialized
*/
esp_err_t zh_onewire_search_rom_init(void);
/**
* @brief Search next 1-Wire device on bus.
* @brief Search next 1-Wire device on bus.
*
* @attention Initialize search 1-Wire devices on bus must be initialized first. @code zh_onewire_search_rom_init() @endcode
* @attention Initialize search 1-Wire devices on bus must be initialized first. @code zh_onewire_search_rom_init() @endcode
*
* @return
* - Pointer to a buffer containing an eight-byte rom value
* - NULL if the search is terminated or if there are no 1-Wire devices available on the bus or the devices are not responding
* - NULL if the search is terminated or if there are no 1-Wire devices available on the bus or the devices are not responding or if 1-Wire bus not initialized
*/
uint8_t *zh_onewire_search_rom_next(void);

View File

@ -1 +1 @@
1.1.4
1.0.0

View File

@ -1,6 +1,7 @@
/**
* @file
* The main code of the zh_onewire component.
*
*/
#include "zh_onewire.h"
@ -38,6 +39,7 @@ static const char *TAG = "zh_onewire";
static uint8_t _pin;
static uint8_t _rom[8];
static uint8_t _rom_fork_bit = 0xFF;
static bool _is_initialized = false;
static const uint8_t _rom_crc_table[] = {
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
@ -72,6 +74,7 @@ esp_err_t zh_onewire_init(const uint8_t pin)
ESP_LOGE(TAG, "Onewire initialization fail. Incorrect GPIO number.");
return ESP_ERR_INVALID_ARG;
}
_is_initialized = true;
ESP_LOGI(TAG, "Onewire initialization success.");
return ESP_OK;
}
@ -79,6 +82,11 @@ esp_err_t zh_onewire_init(const uint8_t pin)
esp_err_t zh_onewire_reset(void)
{
ESP_LOGI(TAG, "Onewire reset begin.");
if (_is_initialized == false)
{
ESP_LOGE(TAG, "Onewire reset fail. Onewire not initialized.");
return ESP_ERR_INVALID_STATE;
}
if (gpio_get_level(_pin) != 1)
{
ESP_LOGE(TAG, "Onewire reset fail. Bus is busy.");
@ -129,6 +137,11 @@ void zh_onewire_send_byte(uint8_t byte)
esp_err_t zh_onewire_skip_rom(void)
{
ESP_LOGI(TAG, "Onewire skip ROM begin.");
if (_is_initialized == false)
{
ESP_LOGE(TAG, "Onewire skip ROM fail. Onewire not initialized.");
return ESP_ERR_INVALID_STATE;
}
if (zh_onewire_reset() != ESP_OK)
{
ESP_LOGE(TAG, "Onewire skip ROM fail.");
@ -142,6 +155,11 @@ esp_err_t zh_onewire_skip_rom(void)
esp_err_t zh_onewire_read_rom(uint8_t *buf)
{
ESP_LOGI(TAG, "Onewire read ROM begin.");
if (_is_initialized == false)
{
ESP_LOGE(TAG, "Onewire read ROM fail. Onewire not initialized.");
return ESP_ERR_INVALID_STATE;
}
if (zh_onewire_reset() != ESP_OK)
{
ESP_LOGE(TAG, "Onewire read ROM fail.");
@ -167,6 +185,11 @@ esp_err_t zh_onewire_read_rom(uint8_t *buf)
esp_err_t zh_onewire_match_rom(const uint8_t *data)
{
ESP_LOGI(TAG, "Onewire match ROM begin.");
if (_is_initialized == false)
{
ESP_LOGE(TAG, "Onewire match ROM fail. Onewire not initialized.");
return ESP_ERR_INVALID_STATE;
}
if (zh_onewire_reset() != ESP_OK)
{
ESP_LOGE(TAG, "Onewire match ROM fail.");
@ -183,6 +206,10 @@ esp_err_t zh_onewire_match_rom(const uint8_t *data)
esp_err_t zh_onewire_search_rom_init(void)
{
if (_is_initialized == false)
{
return ESP_ERR_INVALID_STATE;
}
for (uint8_t i = 0; i < 8; ++i)
{
_rom[i] = 0;
@ -194,6 +221,11 @@ esp_err_t zh_onewire_search_rom_init(void)
uint8_t *zh_onewire_search_rom_next(void)
{
ESP_LOGI(TAG, "Onewire search ROM begin.");
if (_is_initialized == false)
{
ESP_LOGE(TAG, "Onewire search ROM fail. Onewire not initialized.");
return NULL;
}
if (_rom_fork_bit == 0xFF)
{
ESP_LOGE(TAG, "Onewire search ROM not initialized.");