diff --git a/main/ds18b20.c b/main/ds18b20.c index f202652..14d54e6 100644 --- a/main/ds18b20.c +++ b/main/ds18b20.c @@ -63,6 +63,7 @@ DS18B20_Info * ds18b20_malloc(void) if (ds18b20_info != NULL) { memset(ds18b20_info, 0, sizeof(*ds18b20_info)); + ESP_LOGD(TAG, "malloc %p", ds18b20_info); } else { @@ -76,7 +77,8 @@ void ds18b20_free(DS18B20_Info ** ds18b20_info) { if (ds18b20_info != NULL && (*ds18b20_info != NULL)) { - free(ds18b20_info); + ESP_LOGD(TAG, "free %p", *ds18b20_info); + free(*ds18b20_info); *ds18b20_info = NULL; } } diff --git a/main/ds18b20_main.c b/main/ds18b20_main.c index 911853b..b7d5573 100644 --- a/main/ds18b20_main.c +++ b/main/ds18b20_main.c @@ -4,7 +4,15 @@ #include "freertos/task.h" #include "esp_system.h" -#include "ds18b20.h" +//#define USE_STATIC 1 + +#ifdef USE_STATIC +# include "owb_static.h" +# include "ds18b20_static.h" +#else +# include "owb.h" +# include "ds18b20.h" +#endif #define GPIO_DS18B20_0 (GPIO_NUM_4) @@ -13,7 +21,13 @@ void app_main() { // Create a 1-Wire bus - OneWireBus * owb = owb_malloc(); +#ifdef USE_STATIC + OneWireBus_Static owb_static; // static allocation + OneWireBus * owb = &owb_static; +#else + OneWireBus * owb = owb_malloc(); // heap allocation +#endif + owb_init(owb, GPIO_DS18B20_0); owb_use_crc(owb, true); // enable CRC check for ROM code @@ -22,7 +36,14 @@ void app_main() 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(); + // Create a DS18B20 device on the 1-Wire bus +#ifdef USE_STATIC + DS18B20_Info_Static ds18b20_info_static; // static allocation + DS18B20_Info * ds18b20_info = &ds18b20_info_static; +#else + DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation +#endif + 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 @@ -39,7 +60,7 @@ void app_main() vTaskDelay(1000 / portTICK_PERIOD_MS); } - //ds18b20_free(&ds18b20_info); + ds18b20_free(&ds18b20_info); owb_free(&owb); printf("Restarting now.\n"); diff --git a/main/ds18b20_static.h b/main/ds18b20_static.h new file mode 100644 index 0000000..3501098 --- /dev/null +++ b/main/ds18b20_static.h @@ -0,0 +1,29 @@ +// Expose structure for stack-based (static) allocation. + +#ifndef DS18B20_STATIC_H +#define DS18B20_STATIC_H + +#include +#include + +#include "ds18b20.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct _DS18B20_Info +{ + bool init; + bool use_crc; + OneWireBus * bus; + uint64_t rom_code; +}; + +typedef struct _DS18B20_Info DS18B20_Info_Static; + +#ifdef __cplusplus +} +#endif + +#endif // DS18B20_STATIC_H diff --git a/main/owb.c b/main/owb.c index c035e02..1fba20b 100644 --- a/main/owb.c +++ b/main/owb.c @@ -8,6 +8,7 @@ #include "driver/gpio.h" #include "owb.h" +#include "owb_static.h" #define TAG "owb" @@ -31,13 +32,13 @@ static const struct _OneWireBus_Timing _StandardTiming = { 410 * 4, // J }; -struct _OneWireBus -{ - bool init; - int gpio; - const struct _OneWireBus_Timing * timing; - bool use_crc; -}; +//struct _OneWireBus +//{ +// bool init; +// int gpio; +// const struct _OneWireBus_Timing * timing; +// bool use_crc; +//}; static void _tick_delay(int ticks) { @@ -204,7 +205,7 @@ static uint8_t * _read_block(const OneWireBus * bus, uint8_t * buffer, unsigned * @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) +static const uint8_t * _write_block(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) { for (int i = 0; i < len; ++i) { @@ -253,6 +254,7 @@ OneWireBus * owb_malloc() if (bus != NULL) { memset(bus, 0, sizeof(*bus)); + ESP_LOGD(TAG, "malloc %p", bus); } else { @@ -265,7 +267,8 @@ void owb_free(OneWireBus ** bus) { if (bus != NULL && (*bus != NULL)) { - free(bus); + ESP_LOGD(TAG, "free %p", *bus); + free(*bus); *bus = NULL; } } @@ -367,7 +370,7 @@ uint8_t * owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int return _read_block(bus, buffer, len); } -uint8_t * owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) +const uint8_t * owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) { return _write_block(bus, buffer, len); } diff --git a/main/owb.h b/main/owb.h index 3ff69bc..f28693a 100644 --- a/main/owb.h +++ b/main/owb.h @@ -91,7 +91,7 @@ uint8_t owb_read_byte(const OneWireBus * bus); * @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); +const 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. @@ -113,4 +113,4 @@ void owb_write_rom_code(const OneWireBus * bus, uint64_t rom_code); } #endif -#endif // DS18B20_H +#endif // ONE_WIRE_BUS_H diff --git a/main/owb_static.h b/main/owb_static.h new file mode 100644 index 0000000..17a8b2e --- /dev/null +++ b/main/owb_static.h @@ -0,0 +1,28 @@ +// Expose structure for stack-based (static) allocation. + +#ifndef ONE_WIRE_BUS_STATIC_H +#define ONE_WIRE_BUS_STATIC_H + +#include + +#include "owb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct _OneWireBus +{ + bool init; + int gpio; + const struct _OneWireBus_Timing * timing; + bool use_crc; +}; + +typedef struct _OneWireBus OneWireBus_Static; + +#ifdef __cplusplus +} +#endif + +#endif // ONE_WIRE_BUS_STATIC_H