Browse Source

Support static allocation of objects.

main
David Antliff 8 years ago
parent
commit
5f89347bc8
  1. 4
      main/ds18b20.c
  2. 29
      main/ds18b20_main.c
  3. 29
      main/ds18b20_static.h
  4. 23
      main/owb.c
  5. 4
      main/owb.h
  6. 28
      main/owb_static.h

4
main/ds18b20.c

@ -63,6 +63,7 @@ DS18B20_Info * ds18b20_malloc(void)
if (ds18b20_info != NULL) if (ds18b20_info != NULL)
{ {
memset(ds18b20_info, 0, sizeof(*ds18b20_info)); memset(ds18b20_info, 0, sizeof(*ds18b20_info));
ESP_LOGD(TAG, "malloc %p", ds18b20_info);
} }
else else
{ {
@ -76,7 +77,8 @@ void ds18b20_free(DS18B20_Info ** ds18b20_info)
{ {
if (ds18b20_info != NULL && (*ds18b20_info != NULL)) if (ds18b20_info != NULL && (*ds18b20_info != NULL))
{ {
free(ds18b20_info); ESP_LOGD(TAG, "free %p", *ds18b20_info);
free(*ds18b20_info);
*ds18b20_info = NULL; *ds18b20_info = NULL;
} }
} }

29
main/ds18b20_main.c

@ -4,7 +4,15 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.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) #define GPIO_DS18B20_0 (GPIO_NUM_4)
@ -13,7 +21,13 @@
void app_main() void app_main()
{ {
// Create a 1-Wire bus // 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_init(owb, GPIO_DS18B20_0);
owb_use_crc(owb, true); // enable CRC check for ROM code 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); uint64_t rom_code = owb_read_rom(owb);
printf("1-Wire ROM code 0x%08" PRIx64 "\n", rom_code); 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(ds18b20_info, owb, rom_code); // associate with bus and device
//ds18b20_init_solo(ds18b20_info, owb); // only one device on bus //ds18b20_init_solo(ds18b20_info, owb); // only one device on bus
ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings
@ -39,7 +60,7 @@ void app_main()
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }
//ds18b20_free(&ds18b20_info); ds18b20_free(&ds18b20_info);
owb_free(&owb); owb_free(&owb);
printf("Restarting now.\n"); printf("Restarting now.\n");

29
main/ds18b20_static.h

@ -0,0 +1,29 @@
// Expose structure for stack-based (static) allocation.
#ifndef DS18B20_STATIC_H
#define DS18B20_STATIC_H
#include <stdbool.h>
#include <stdint.h>
#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

23
main/owb.c

@ -8,6 +8,7 @@
#include "driver/gpio.h" #include "driver/gpio.h"
#include "owb.h" #include "owb.h"
#include "owb_static.h"
#define TAG "owb" #define TAG "owb"
@ -31,13 +32,13 @@ static const struct _OneWireBus_Timing _StandardTiming = {
410 * 4, // J 410 * 4, // J
}; };
struct _OneWireBus //struct _OneWireBus
{ //{
bool init; // bool init;
int gpio; // int gpio;
const struct _OneWireBus_Timing * timing; // const struct _OneWireBus_Timing * timing;
bool use_crc; // bool use_crc;
}; //};
static void _tick_delay(int ticks) 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. * @param[in] len Number of bytes to write.
* @return Pointer to write buffer. * @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) for (int i = 0; i < len; ++i)
{ {
@ -253,6 +254,7 @@ OneWireBus * owb_malloc()
if (bus != NULL) if (bus != NULL)
{ {
memset(bus, 0, sizeof(*bus)); memset(bus, 0, sizeof(*bus));
ESP_LOGD(TAG, "malloc %p", bus);
} }
else else
{ {
@ -265,7 +267,8 @@ void owb_free(OneWireBus ** bus)
{ {
if (bus != NULL && (*bus != NULL)) if (bus != NULL && (*bus != NULL))
{ {
free(bus); ESP_LOGD(TAG, "free %p", *bus);
free(*bus);
*bus = NULL; *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); 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); return _write_block(bus, buffer, len);
} }

4
main/owb.h

@ -91,7 +91,7 @@ uint8_t owb_read_byte(const OneWireBus * bus);
* @param[in] len Number of bytes to write. * @param[in] len Number of bytes to write.
* @return Pointer to write buffer. * @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. * @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
#endif // DS18B20_H #endif // ONE_WIRE_BUS_H

28
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 <stdbool.h>
#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
Loading…
Cancel
Save