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 ```text
cd ../your_project/components 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: In the application, add the component:

View File

@ -35,6 +35,7 @@ extern "C"
* *
* @return * @return
* - ESP_OK if reset was successful * - 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_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_TIMEOUT if there are no 1-Wire devices available on the bus or the devices are not responding
*/ */
@ -44,23 +45,22 @@ extern "C"
* @brief Send one byte to 1-Wire device. * @brief Send one byte to 1-Wire device.
* *
* @param[in] byte Byte value. * @param[in] byte Byte value.
*
*/ */
void zh_onewire_send_byte(uint8_t byte); 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 * @return Byte value
* - Byte value
*/ */
uint8_t zh_onewire_read_byte(void); 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 * @return
* - ESP_OK if initialization was successful * - 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_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); esp_err_t zh_onewire_skip_rom(void);
@ -72,6 +72,7 @@ extern "C"
* *
* @return * @return
* - ESP_OK if read was successful * - 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_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_INVALID_CRC if more than one 1-Wire device is present on the bus
*/ */
@ -84,6 +85,7 @@ extern "C"
* *
* @return * @return
* - ESP_OK if initialization was successful * - 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_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); esp_err_t zh_onewire_match_rom(const uint8_t *data);
@ -93,6 +95,7 @@ extern "C"
* *
* @return * @return
* - ESP_OK if initialization was successful * - 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); esp_err_t zh_onewire_search_rom_init(void);
@ -103,7 +106,7 @@ extern "C"
* *
* @return * @return
* - Pointer to a buffer containing an eight-byte rom value * - 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); 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 * @file
* The main code of the zh_onewire component. * The main code of the zh_onewire component.
*
*/ */
#include "zh_onewire.h" #include "zh_onewire.h"
@ -38,6 +39,7 @@ static const char *TAG = "zh_onewire";
static uint8_t _pin; static uint8_t _pin;
static uint8_t _rom[8]; static uint8_t _rom[8];
static uint8_t _rom_fork_bit = 0xFF; static uint8_t _rom_fork_bit = 0xFF;
static bool _is_initialized = false;
static const uint8_t _rom_crc_table[] = { static const uint8_t _rom_crc_table[] = {
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, 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."); ESP_LOGE(TAG, "Onewire initialization fail. Incorrect GPIO number.");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
_is_initialized = true;
ESP_LOGI(TAG, "Onewire initialization success."); ESP_LOGI(TAG, "Onewire initialization success.");
return ESP_OK; 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_err_t zh_onewire_reset(void)
{ {
ESP_LOGI(TAG, "Onewire reset begin."); 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) if (gpio_get_level(_pin) != 1)
{ {
ESP_LOGE(TAG, "Onewire reset fail. Bus is busy."); 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_err_t zh_onewire_skip_rom(void)
{ {
ESP_LOGI(TAG, "Onewire skip ROM begin."); 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) if (zh_onewire_reset() != ESP_OK)
{ {
ESP_LOGE(TAG, "Onewire skip ROM fail."); 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_err_t zh_onewire_read_rom(uint8_t *buf)
{ {
ESP_LOGI(TAG, "Onewire read ROM begin."); 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) if (zh_onewire_reset() != ESP_OK)
{ {
ESP_LOGE(TAG, "Onewire read ROM fail."); 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_err_t zh_onewire_match_rom(const uint8_t *data)
{ {
ESP_LOGI(TAG, "Onewire match ROM begin."); 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) if (zh_onewire_reset() != ESP_OK)
{ {
ESP_LOGE(TAG, "Onewire match ROM fail."); 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) 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) for (uint8_t i = 0; i < 8; ++i)
{ {
_rom[i] = 0; _rom[i] = 0;
@ -194,6 +221,11 @@ esp_err_t zh_onewire_search_rom_init(void)
uint8_t *zh_onewire_search_rom_next(void) uint8_t *zh_onewire_search_rom_next(void)
{ {
ESP_LOGI(TAG, "Onewire search ROM begin."); 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) if (_rom_fork_bit == 0xFF)
{ {
ESP_LOGE(TAG, "Onewire search ROM not initialized."); ESP_LOGE(TAG, "Onewire search ROM not initialized.");