diff --git a/main/ds18b20.c b/main/ds18b20.c index e0e2178..f202652 100644 --- a/main/ds18b20.c +++ b/main/ds18b20.c @@ -5,8 +5,6 @@ #include #include // for PRIu64 -#include "ds18b20.h" - #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" @@ -14,14 +12,11 @@ #include "esp_log.h" #include "sdkconfig.h" +#include "ds18b20.h" +#include "owb.h" + #define TAG "ds18b20" -// ROM commands -#define DS18B20_ROM_SEARCH 0xF0 -#define DS18B20_ROM_READ 0x33 -#define DS18B20_ROM_MATCH 0x55 -#define DS18B20_ROM_SKIP 0xCC -#define DS18B20_ROM_SEARCH_ALARM 0xEC // Function commands #define DS18B20_FUNCTION_TEMP_CONVERT 0x44 @@ -32,66 +27,23 @@ #define DS18B20_FUNCTION_POWER_SUPPLY_READ 0xB4 -struct _DS18B20_Timing -{ - int A, B, C, D, E, F, G, H, I, J; -}; - -// 1-Wire timing delays (standard) in ticks (quarter-microseconds). -static const struct _DS18B20_Timing _DS18B20_StandardTiming = { - 6 * 4, - 64 * 4, - 60 * 4, - 10 * 4, - 9 * 4, - 55 * 4, - 0, // G - 480 * 4, // H - 70 * 4, // I - 410 * 4, // J -}; - struct _DS18B20_Info { bool init; - int gpio; - const struct _DS18B20_Timing * timing; + bool use_crc; + OneWireBus * bus; + uint64_t rom_code; }; -static void _tick_delay(int ticks) +static bool _is_init(const DS18B20_Info * ds18b20_info) { - // Each tick is 0.25 microseconds. - float time_us = ticks / 4.0; - ets_delay_us(time_us); -} - -/** - * @brief Generate a 1-Wire reset. - * @param[in] ds18b20_info Initialised device info instance. - * @return true if device is present, otherwise false. - */ -static bool _reset(DS18B20_Info * ds18b20_info) -{ - bool present = false; + bool ok = false; if (ds18b20_info != NULL) { if (ds18b20_info->init) { - gpio_set_direction(ds18b20_info->gpio, GPIO_MODE_OUTPUT); - - _tick_delay(ds18b20_info->timing->G); - gpio_set_level(ds18b20_info->gpio, 0); // Drive DQ low - _tick_delay(ds18b20_info->timing->H); - gpio_set_level(ds18b20_info->gpio, 1); // Release the bus - _tick_delay(ds18b20_info->timing->I); - - gpio_set_direction(ds18b20_info->gpio, GPIO_MODE_INPUT); - int level1 = gpio_get_level(ds18b20_info->gpio); - _tick_delay(ds18b20_info->timing->J); // Complete the reset sequence recovery - int level2 = gpio_get_level(ds18b20_info->gpio); - - present = level1 == 0 && level2 == 1; // Sample for presence pulse from slave - ESP_LOGD(TAG, "reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present); + // OK + ok = true; } else { @@ -102,153 +54,40 @@ static bool _reset(DS18B20_Info * ds18b20_info) { ESP_LOGE(TAG, "ds18b20_info is NULL"); } - return present; + return ok; } -/** - * @brief Send a 1-Wire write bit, with recovery time. - * @param[in] ds18b20_info Initialised device info instance. - * @param[in] bit The value to send. - */ -static void _write_bit(DS18B20_Info * ds18b20_info, int bit) +DS18B20_Info * ds18b20_malloc(void) { + DS18B20_Info * ds18b20_info = malloc(sizeof(*ds18b20_info)); if (ds18b20_info != NULL) { - if (ds18b20_info->init) - { - int delay1 = bit ? ds18b20_info->timing->A : ds18b20_info->timing->C; - int delay2 = bit ? ds18b20_info->timing->B : ds18b20_info->timing->D; - gpio_set_direction(ds18b20_info->gpio, GPIO_MODE_OUTPUT); - gpio_set_level(ds18b20_info->gpio, 0); // Drive DQ low - _tick_delay(delay1); - gpio_set_level(ds18b20_info->gpio, 1); // Release the bus - _tick_delay(delay2); - } - else - { - ESP_LOGE(TAG, "ds18b20_info is not initialised"); - } + memset(ds18b20_info, 0, sizeof(*ds18b20_info)); } else { - ESP_LOGE(TAG, "ds18b20_info is NULL"); - } -} - -/** - * @brief Read a bit from the 1-Wire bus and return the value, with recovery time. - * @param[in] ds18b20_info Initialised device info instance. - */ -static int _read_bit(DS18B20_Info * ds18b20_info) -{ - int result = 0; - if (ds18b20_info != NULL) - { - if (ds18b20_info->init) - { - gpio_set_direction(ds18b20_info->gpio, GPIO_MODE_OUTPUT); - gpio_set_level(ds18b20_info->gpio, 0); // Drive DQ low - _tick_delay(ds18b20_info->timing->A); - gpio_set_level(ds18b20_info->gpio, 1); // Release the bus - _tick_delay(ds18b20_info->timing->E); - - gpio_set_direction(ds18b20_info->gpio, GPIO_MODE_INPUT); - int level = gpio_get_level(ds18b20_info->gpio); - _tick_delay(ds18b20_info->timing->F); // Complete the timeslot and 10us recovery - result = level & 0x01; - } - else - { - ESP_LOGE(TAG, "ds18b20_info is not initialised"); - } - } - else - { - ESP_LOGE(TAG, "ds18b20_info is NULL"); - } - return result; -} - -/** - * @brief Write 1-Wire data byte. - * @param[in] ds18b20_info Initialised device info instance. - * @param[in] data Value to write. - */ -static void _write_byte(DS18B20_Info * ds18b20_info, uint8_t data) -{ - if (ds18b20_info != NULL) - { - if (ds18b20_info->init) - { - ESP_LOGD(TAG, "write 0x%02x", data); - for (int i = 0; i < 8; ++i) - { - _write_bit(ds18b20_info, data & 0x01); - data >>= 1; - } - } - else - { - ESP_LOGE(TAG, "ds18b20_info is not initialised"); - } - } - else - { - ESP_LOGE(TAG, "ds18b20_info is NULL"); + ESP_LOGE(TAG, "malloc failed"); } -} -/** - * @brief Read 1-Wire data byte from device. - * @param[in] ds18b20_info Initialised device info instance. - * @return Byte value read from device. - */ -static uint8_t _read_byte(DS18B20_Info * ds18b20_info) -{ - uint8_t result = 0; - - if (ds18b20_info != NULL) - { - if (ds18b20_info->init) - { - for (int i = 0; i < 8; ++i) - { - result >>= 1; - if (_read_bit(ds18b20_info)) - { - result |= 0x80; - } - } - ESP_LOGD(TAG, "read 0x%02x", result); - } - else - { - ESP_LOGE(TAG, "ds18b20_info is not initialised"); - } - } - else - { - ESP_LOGE(TAG, "ds18b20_info is NULL"); - } - return result; + return ds18b20_info; } -static uint8_t * _read_block(DS18B20_Info * ds18b20_info, uint8_t * buffer, unsigned int len) +void ds18b20_free(DS18B20_Info ** ds18b20_info) { - for (int i = 0; i < len; ++i) + if (ds18b20_info != NULL && (*ds18b20_info != NULL)) { - *buffer++ = _read_byte(ds18b20_info); + free(ds18b20_info); + *ds18b20_info = NULL; } - return buffer; } -void ds18b20_init(DS18B20_Info * ds18b20_info, int gpio) +void ds18b20_init(DS18B20_Info * ds18b20_info, OneWireBus * bus, uint64_t rom_code) { if (ds18b20_info != NULL) { - gpio_pad_select_gpio(gpio); - ds18b20_info->gpio = gpio; - ds18b20_info->timing = &_DS18B20_StandardTiming; + ds18b20_info->bus = bus; + ds18b20_info->rom_code = rom_code; + ds18b20_info->use_crc = false; ds18b20_info->init = true; } else @@ -257,145 +96,74 @@ void ds18b20_init(DS18B20_Info * ds18b20_info, int gpio) } } -DS18B20_Info * ds18b20_new(void) +void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc) { - DS18B20_Info * ds18b20 = malloc(sizeof(*ds18b20)); - if (ds18b20 != NULL) + if (_is_init(ds18b20_info)) { - memset(ds18b20, 0, sizeof(*ds18b20)); + ds18b20_info->use_crc = use_crc; + ESP_LOGD(TAG, "use_crc %d", ds18b20_info->use_crc); } - else - { - ESP_LOGE(TAG, "malloc failed"); - } - - return ds18b20; -} - -static uint8_t _calc_crc(uint8_t crc, uint8_t data) -{ - // https://www.maximintegrated.com/en/app-notes/index.mvp/id/27 - static const uint8_t table[256] = { - 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 - }; - - return table[crc ^ data]; -} - -uint64_t ds18b20_read_rom(DS18B20_Info * ds18b20_info) -{ - uint64_t rom_code = 0; - if (ds18b20_info != NULL) - { - if (ds18b20_info->init) - { - if (_reset(ds18b20_info)) - { - uint8_t buffer[8] = { 0 }; - _write_byte(ds18b20_info, DS18B20_ROM_READ); - _read_block(ds18b20_info, buffer, 8); - - // device provides LSB first - for (int i = 7; i >= 0; --i) - { - // watch out for integer promotion - rom_code |= ((uint64_t)buffer[i] << (8 * i)); - } - ESP_LOGD(TAG, "rom_code 0x%08" PRIx64, rom_code); - - // check CRC - uint8_t crc = 0; - for (int i = 0; i < 8; ++i) - { - crc = _calc_crc(crc, buffer[i]); - ESP_LOGD(TAG, "crc 0x%02x", crc); - } - - } - else - { - ESP_LOGE(TAG, "ds18b20 device not responding"); - } - } - else - { - ESP_LOGE(TAG, "ds18b20_info is not initialised"); - } - } - else - { - ESP_LOGE(TAG, "ds18b20_info is NULL"); - } - return rom_code; } float ds18b20_get_temp(DS18B20_Info * ds18b20_info) { float temp = 0.0f; - if (ds18b20_info != NULL) + if (_is_init(ds18b20_info)) { - if (ds18b20_info->init) + OneWireBus * bus = ds18b20_info->bus; + if (owb_reset(bus)) { - if (_reset(ds18b20_info)) + //owb_write_byte(bus, OWB_ROM_SKIP); + owb_write_byte(bus, OWB_ROM_MATCH); + owb_write_rom_code(bus, ds18b20_info->rom_code); + owb_write_byte(bus, DS18B20_FUNCTION_TEMP_CONVERT); + vTaskDelay(750 / portTICK_PERIOD_MS); + + // reset + owb_reset(bus); + owb_write_byte(bus, OWB_ROM_SKIP); + owb_write_byte(bus, DS18B20_FUNCTION_SCRATCHPAD_READ); + + uint8_t temp_LSB = 0; + uint8_t temp_MSB = 0; + if (ds18b20_info->use_crc) { - _write_byte(ds18b20_info, DS18B20_ROM_SKIP); - _write_byte(ds18b20_info, DS18B20_FUNCTION_TEMP_CONVERT); - vTaskDelay(750 / portTICK_PERIOD_MS); - - // reset - _reset(ds18b20_info); - _write_byte(ds18b20_info, DS18B20_ROM_SKIP); - _write_byte(ds18b20_info, DS18B20_FUNCTION_SCRATCHPAD_READ); - // Without CRC: - //uint8_t temp1 = _read_byte(ds18b20_info); - //uint8_t temp2 = _read_byte(ds18b20_info); - //_reset(ds18b20_info); // terminate early - + temp_LSB = owb_read_byte(bus); + temp_MSB = owb_read_byte(bus); + owb_reset(bus); // terminate early + } + else + { // with CRC: uint8_t buffer[9]; - _read_block(ds18b20_info, buffer, 9); + owb_read_bytes(bus, buffer, 9); + + temp_LSB = buffer[0]; + temp_MSB = buffer[1]; + uint8_t crc = 0; for (int i = 0; i < 9; ++i) { - crc = _calc_crc(crc, buffer[i]); - ESP_LOGD(TAG, "crc 0x%02x", crc); + crc = owb_crc8(crc, buffer[i]); } - uint8_t temp1 = buffer[0]; - uint8_t temp2 = buffer[1]; + ESP_LOGD(TAG, "crc 0x%02x", crc); - ESP_LOGD(TAG, "temp1 0x%02x, temp2 0x%02x", temp1, temp2); - temp = (float)(((temp2 << 8) + temp1) >> 4); - } - else - { - ESP_LOGE(TAG, "ds18b20 device not responding"); + if (crc != 0) + { + ESP_LOGE(TAG, "CRC failed"); + temp_LSB = temp_MSB = 0; + } } + + ESP_LOGD(TAG, "temp_LSB 0x%02x, temp_MSB 0x%02x", temp_LSB, temp_MSB); + temp = (float)(((temp_MSB << 8) + temp_LSB) >> 4); } else { - ESP_LOGE(TAG, "ds18b20_info is not initialised"); + ESP_LOGE(TAG, "ds18b20 device not responding"); } } - else - { - ESP_LOGE(TAG, "ds18b20_info is NULL"); - } return temp; } diff --git a/main/ds18b20.h b/main/ds18b20.h index 228c4cf..f5f4d34 100644 --- a/main/ds18b20.h +++ b/main/ds18b20.h @@ -1,6 +1,8 @@ #ifndef DS18B20_H #define DS18B20_H +#include "owb.h" + #ifdef __cplusplus extern "C" { #endif @@ -8,18 +10,33 @@ extern "C" { typedef struct _DS18B20_Info DS18B20_Info; +/** + * @brief Construct a new device info instance. + * New instance should be initialised before calling other functions. + * @return Pointer to new device info instance, or NULL if it cannot be created. + */ +DS18B20_Info * ds18b20_malloc(void); + +/** + * @brief Delete an existing device info instance. + * @param[in] ds18b20_info Pointer to device info instance. + * @param[in,out] ds18b20_info Pointer to device info instance that will be freed and set to NULL. + */ +void ds18b20_free(DS18B20_Info ** ds18b20_info); + /** * @brief Initialise a device info instance with the specified GPIO. * @param[in] ds18b20_info Pointer to device info instance. - * @param[in] gpio GPIO number to associate with device. + * @param[in] bus Pointer to initialised 1-Wire bus instance. */ -void ds18b20_init(DS18B20_Info * ds18b20_info, int gpio); +void ds18b20_init(DS18B20_Info * ds18b20_info, OneWireBus * bus, uint64_t rom_code); /** - * @brief Construct a new device info instance. - * @return Pointer to new device info instance, or NULL if it cannot be created. + * @brief Enable or disable use of CRC checks on device communications. + * @param[in] ds18b20_info Pointer to device info instance. + * @param[in] use_crc True to enable CRC checks, false to disable. */ -DS18B20_Info * ds18b20_new(void); +void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc); /** * @brief Read 64-bit ROM code from device - only works when there is a single device on the bus. diff --git a/main/ds18b20_main.c b/main/ds18b20_main.c index 280041d..911853b 100644 --- a/main/ds18b20_main.c +++ b/main/ds18b20_main.c @@ -12,16 +12,25 @@ void app_main() { - DS18B20_Info * ds18b20_info = ds18b20_new(); - ds18b20_init(ds18b20_info, GPIO_DS18B20_0); + // Create a 1-Wire bus + OneWireBus * owb = owb_malloc(); + owb_init(owb, GPIO_DS18B20_0); + owb_use_crc(owb, true); // enable CRC check for ROM code - uint64_t rom_code = ds18b20_read_rom(ds18b20_info); - printf("ROM code = 0x%08" PRIx64 "\n", rom_code); + //owb_search(); // find all connected devices + //OneWireBusROMCode rom_code = 0x1162e87ccee28; + uint64_t rom_code = owb_read_rom(owb); + printf("1-Wire ROM code 0x%08" PRIx64 "\n", rom_code); + + DS18B20_Info * ds18b20_info = ds18b20_malloc(); + ds18b20_init(ds18b20_info, owb, rom_code); // associate with bus and device + //ds18b20_init_solo(ds18b20_info, owb); // only one device on bus + ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings while (1) { float temp = ds18b20_get_temp(ds18b20_info); - printf("Temp = %.1f degrees C\n", temp); + printf("Temp %.1f degrees C\n", temp); vTaskDelay(1000 / portTICK_PERIOD_MS); } @@ -29,6 +38,10 @@ void app_main() printf("Restarting in %d seconds...\n", i); vTaskDelay(1000 / portTICK_PERIOD_MS); } + + //ds18b20_free(&ds18b20_info); + owb_free(&owb); + printf("Restarting now.\n"); fflush(stdout); esp_restart(); diff --git a/main/owb.c b/main/owb.c new file mode 100644 index 0000000..c035e02 --- /dev/null +++ b/main/owb.c @@ -0,0 +1,391 @@ +#include +#include +#include +#include + +#include "esp_log.h" +#include "sdkconfig.h" +#include "driver/gpio.h" + +#include "owb.h" + +#define TAG "owb" + + +struct _OneWireBus_Timing +{ + int A, B, C, D, E, F, G, H, I, J; +}; + +// 1-Wire timing delays (standard) in ticks (quarter-microseconds). +static const struct _OneWireBus_Timing _StandardTiming = { + 6 * 4, + 64 * 4, + 60 * 4, + 10 * 4, + 9 * 4, + 55 * 4, + 0, // G + 480 * 4, // H + 70 * 4, // I + 410 * 4, // J +}; + +struct _OneWireBus +{ + bool init; + int gpio; + const struct _OneWireBus_Timing * timing; + bool use_crc; +}; + +static void _tick_delay(int ticks) +{ + // Each tick is 0.25 microseconds. + float time_us = ticks / 4.0; + ets_delay_us(time_us); +} + +static bool _is_init(const OneWireBus * bus) +{ + bool ok = false; + if (bus != NULL) + { + if (bus->init) + { + // OK + ok = true; + } + else + { + ESP_LOGE(TAG, "bus is not initialised"); + } + } + else + { + ESP_LOGE(TAG, "bus is NULL"); + } + return ok; +} + + +/** + * @brief Generate a 1-Wire reset. + * @param[in] bus Initialised bus instance. + * @return true if device is present, otherwise false. + */ +static bool _reset(const OneWireBus * bus) +{ + bool present = false; + if (_is_init(bus)) + { + gpio_set_direction(bus->gpio, GPIO_MODE_OUTPUT); + + _tick_delay(bus->timing->G); + gpio_set_level(bus->gpio, 0); // Drive DQ low + _tick_delay(bus->timing->H); + gpio_set_level(bus->gpio, 1); // Release the bus + _tick_delay(bus->timing->I); + + gpio_set_direction(bus->gpio, GPIO_MODE_INPUT); + int level1 = gpio_get_level(bus->gpio); + _tick_delay(bus->timing->J); // Complete the reset sequence recovery + int level2 = gpio_get_level(bus->gpio); + + present = (level1 == 0) && (level2 == 1); // Sample for presence pulse from slave + ESP_LOGD(TAG, "reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present); + } + return present; +} + +/** + * @brief Send a 1-Wire write bit, with recovery time. + * @param[in] bus Initialised bus instance. + * @param[in] bit The value to send. + */ +static void _write_bit(const OneWireBus * bus, int bit) +{ + if (_is_init(bus)) + { + int delay1 = bit ? bus->timing->A : bus->timing->C; + int delay2 = bit ? bus->timing->B : bus->timing->D; + gpio_set_direction(bus->gpio, GPIO_MODE_OUTPUT); + gpio_set_level(bus->gpio, 0); // Drive DQ low + _tick_delay(delay1); + gpio_set_level(bus->gpio, 1); // Release the bus + _tick_delay(delay2); + } +} + +/** + * @brief Read a bit from the 1-Wire bus and return the value, with recovery time. + * @param[in] bus Initialised bus instance. + */ +static int _read_bit(const OneWireBus * bus) +{ + int result = 0; + if (_is_init(bus)) + { + gpio_set_direction(bus->gpio, GPIO_MODE_OUTPUT); + gpio_set_level(bus->gpio, 0); // Drive DQ low + _tick_delay(bus->timing->A); + gpio_set_level(bus->gpio, 1); // Release the bus + _tick_delay(bus->timing->E); + + gpio_set_direction(bus->gpio, GPIO_MODE_INPUT); + int level = gpio_get_level(bus->gpio); + _tick_delay(bus->timing->F); // Complete the timeslot and 10us recovery + result = level & 0x01; + } + return result; +} + +/** + * @brief Write 1-Wire data byte. + * @param[in] bus Initialised bus instance. + * @param[in] data Value to write. + */ +static void _write_byte(const OneWireBus * bus, uint8_t data) +{ + if (_is_init(bus)) + { + ESP_LOGD(TAG, "write 0x%02x", data); + for (int i = 0; i < 8; ++i) + { + _write_bit(bus, data & 0x01); + data >>= 1; + } + } +} + +/** + * @brief Read 1-Wire data byte from bus. + * @param[in] bus Initialised bus instance. + * @return Byte value read from bus. + */ +static uint8_t _read_byte(const OneWireBus * bus) +{ + uint8_t result = 0; + if (_is_init(bus)) + { + for (int i = 0; i < 8; ++i) + { + result >>= 1; + if (_read_bit(bus)) + { + result |= 0x80; + } + } + ESP_LOGD(TAG, "read 0x%02x", result); + } + return result; +} + +/** + * @param Read a block of bytes from 1-Wire bus. + * @param[in] bus Initialised bus instance. + * @param[in,out] buffer Pointer to buffer to receive read data. + * @param[in] len Number of bytes to read, must not exceed length of receive buffer. + * @return Pointer to receive buffer. + */ +static uint8_t * _read_block(const OneWireBus * bus, uint8_t * buffer, unsigned int len) +{ + for (int i = 0; i < len; ++i) + { + *buffer++ = _read_byte(bus); + } + return buffer; +} + +/** + * @param Write a block of bytes from 1-Wire bus. + * @param[in] bus Initialised bus instance. + * @param[in] buffer Pointer to buffer to write data from. + * @param[in] len Number of bytes to write. + * @return Pointer to write buffer. + */ +static uint8_t * _write_block(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) +{ + for (int i = 0; i < len; ++i) + { + _write_byte(bus, buffer[i]); + } + return buffer; +} + +/** + * @brief 1-Wire 8-bit CRC lookup. + * @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. + * @param[in] data Byte to feed into CRC. + * @return Resultant CRC value. + */ +static uint8_t _calc_crc(uint8_t crc, uint8_t data) +{ + // https://www.maximintegrated.com/en/app-notes/index.mvp/id/27 + static const uint8_t table[256] = { + 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 + }; + + return table[crc ^ data]; +} + + +// Public API + +OneWireBus * owb_malloc() +{ + OneWireBus * bus = malloc(sizeof(*bus)); + if (bus != NULL) + { + memset(bus, 0, sizeof(*bus)); + } + else + { + ESP_LOGE(TAG, "malloc failed"); + } + return bus; +} + +void owb_free(OneWireBus ** bus) +{ + if (bus != NULL && (*bus != NULL)) + { + free(bus); + *bus = NULL; + } +} + +void owb_init(OneWireBus * bus, int gpio) +{ + if (bus != NULL) + { + bus->gpio = gpio; + bus->timing = &_StandardTiming; + bus->init = true; + + // platform specific: + gpio_pad_select_gpio(bus->gpio); + } + else + { + ESP_LOGE(TAG, "bus is NULL"); + } +} + +void owb_use_crc(OneWireBus * bus, bool use_crc) +{ + if (_is_init(bus)) + { + bus->use_crc = use_crc; + ESP_LOGD(TAG, "use_crc %d", bus->use_crc); + } +} + +int owb_rom_search(OneWireBus * bus) +{ + // TODO + return 0; +} + +uint64_t owb_read_rom(const OneWireBus * bus) +{ + uint64_t rom_code = 0; + if (_is_init(bus)) + { + if (_reset(bus)) + { + uint8_t buffer[8] = { 0 }; + _write_byte(bus, OWB_ROM_READ); + _read_block(bus, buffer, 8); + + // device provides LSB first + for (int i = 7; i >= 0; --i) + { + // watch out for integer promotion + rom_code |= ((uint64_t)buffer[i] << (8 * i)); + } + + if (bus->use_crc) + { + // check CRC + uint8_t crc = 0; + for (int i = 0; i < 8; ++i) + { + crc = _calc_crc(crc, buffer[i]); + } + ESP_LOGD(TAG, "crc 0x%02x", crc); + + if (crc != 0) + { + ESP_LOGE(TAG, "CRC failed"); + rom_code = 0; + } + + ESP_LOGD(TAG, "rom_code 0x%08" PRIx64, rom_code); + } + } + else + { + ESP_LOGE(TAG, "ds18b20 device not responding"); + } + } + return rom_code; +} + +bool owb_reset(const OneWireBus * bus) +{ + return _reset(bus); +} + +void owb_write_byte(const OneWireBus * bus, uint8_t data) +{ + _write_byte(bus, data); +} + +uint8_t owb_read_byte(const OneWireBus * bus) +{ + return _read_byte(bus); +} + +uint8_t * owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len) +{ + return _read_block(bus, buffer, len); +} + +uint8_t * owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) +{ + return _write_block(bus, buffer, len); +} + +void owb_write_rom_code(const OneWireBus * bus, uint64_t rom_code) +{ + uint8_t buffer[sizeof(uint64_t)] = {0}; + for (int i = 0; i < sizeof(buffer); ++i) + { + // LSB first + buffer[i] = rom_code & 0xFF; + rom_code >>= 8; + } + _write_block(bus, buffer, sizeof(buffer)); +} + +uint8_t owb_crc8(uint8_t crc, uint8_t data) +{ + return _calc_crc(crc, data); +} + diff --git a/main/owb.h b/main/owb.h new file mode 100644 index 0000000..3ff69bc --- /dev/null +++ b/main/owb.h @@ -0,0 +1,116 @@ +#ifndef ONE_WIRE_BUS_H +#define ONE_WIRE_BUS_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +// ROM commands +#define OWB_ROM_SEARCH 0xF0 +#define OWB_ROM_READ 0x33 +#define OWB_ROM_MATCH 0x55 +#define OWB_ROM_SKIP 0xCC +#define OWB_ROM_SEARCH_ALARM 0xEC + + +typedef struct _OneWireBus OneWireBus; +typedef uint64_t OneWireBusROMCode; + +/** + * @brief Construct a new 1-Wire bus instance. + * New instance should be initialised before calling other functions. + * @return Pointer to new bus instance, or NULL if it cannot be created. + */ +OneWireBus * owb_malloc(void); + +/** + * @brief Delete an existing device info instance. + * @param[in] bus Pointer to bus instance. + * @param[in,out] ds18b20_info Pointer to device info instance that will be freed and set to NULL. + */ +void owb_free(OneWireBus ** bus); + +/** + * @brief Initialise a 1-Wire bus instance with the specified GPIO. + * @param[in] bus Pointer to bus instance. + * @param[in] gpio GPIO number to associate with device. + */ +void owb_init(OneWireBus * bus, int gpio); + +/** + * @brief Enable or disable use of CRC checks on device communications. + * @param[in] bus Pointer to initialised bus instance. + * @param[in] use_crc True to enable CRC checks, false to disable. + */ +void owb_use_crc(OneWireBus * bus, bool use_crc); + +/** + * @brief Read 64-bit ROM code from device - only works when there is a single device on the bus. + * @param[in] bus Pointer to initialised bus instance. + * @return The 64-bit value read from the device's ROM. + */ +uint64_t owb_read_rom(const OneWireBus * bus); + +/** + * @brief Reset the 1-Wire bus. + * @param[in] bus Pointer to initialised bus instance. + * @return True if at least one device is present on the bus. + */ +bool owb_reset(const OneWireBus * bus); + +/** + * @brief Write a single byte to the 1-Wire bus. + * @param[in] bus Pointer to initialised bus instance. + * @param[in] data Byte value to write to bus. + */ +void owb_write_byte(const OneWireBus * bus, uint8_t data); + +/** + * @brief Read a single byte from the 1-Wire bus. + * @param[in] bus Pointer to initialised bus instance. + * @return The byte value read from the bus. + */ +uint8_t owb_read_byte(const OneWireBus * bus); + +/** + * @brief Read a number of bytes from the 1-Wire bus. + * @param[in] bus Pointer to initialised bus instance. + * @param[in, out] buffer Pointer to buffer to receive read data. + * @param[in] len Number of bytes to read, must not exceed length of receive buffer. + * @return Pointer to receive buffer. + */ + uint8_t * owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len); + + /** + * @brief Write a number of bytes to the 1-Wire bus. + * @param[in] bus Pointer to initialised bus instance. + * @param[in] buffer Pointer to buffer to write data from. + * @param[in] len Number of bytes to write. + * @return Pointer to write buffer. + */ +uint8_t * owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len); + +/** + * @brief Write a ROM code to the 1-Wire bus ensuring LSB is sent first. + * @param[in] bus Pointer to initialised bus instance. + * @param[in] rom_code ROM code to write to bus. + */ +void owb_write_rom_code(const OneWireBus * bus, uint64_t rom_code); + + /** + * @brief 1-Wire 8-bit CRC lookup. + * @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. + * @param[in] data Byte to feed into CRC. + * @return Resultant CRC value. + */ + uint8_t owb_crc8(uint8_t crc, uint8_t data); + + +#ifdef __cplusplus +} +#endif + +#endif // DS18B20_H diff --git a/sdkconfig b/sdkconfig index b9c2fbc..8b7e1ce 100644 --- a/sdkconfig +++ b/sdkconfig @@ -261,10 +261,10 @@ CONFIG_TIMER_QUEUE_LENGTH=10 # CONFIG_LOG_DEFAULT_LEVEL_NONE is not set # CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set # CONFIG_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_LOG_DEFAULT_LEVEL_INFO=y -# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set +# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set +CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set -CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_LOG_DEFAULT_LEVEL=4 CONFIG_LOG_COLORS=y #