Skip to content

esp_type_utils esp‐idf component

Koios edited this page Nov 9, 2025 · 1 revision

ESP-IDF Type Utilities Driver (type_utils.h)

Documentation for the type and conversion utilities for ESP32-S3, as implemented in type_utils.h.


Overview

This utility component provides a set of helpers for type-safe conversions and manipulations commonly required in embedded and sensor projects. It includes buffer type definitions for various bit-widths, conversion routines for numbers to and from bytes, as well as binary string representations for diagnostic and serialization use cases.


License

MIT License
Copyright (c) 2024 Eric Gionet
See LICENSE for details.


Features

  • Buffer typedefs for various bit-widths (8, 16, ..., 104)
  • Union types for endian-safe conversion between byte arrays and values (float/double/int/uint)
  • Fetch unique ESP chip ID and MAC address
  • Typed binary string conversions
  • Byte-array to value conversions (uint/int/float/double) and vice-versa, endianness selectable
  • Utility helpers for copying bytes

Usage Example

#include "type_utils.h"

uint32_t chip_id = get_uint32_chip_id();
uint64_t mac = get_efuse_mac();

uint32_t value;
uint8_t bytes[4];
value = 0x12345678;
uint32_to_bytes(value, bytes, true); // Little-endian byte array

const char* binstr = uint32_to_binary(value); // Get string "00010010001101000101011001111000"

Macros

  • Buffer sizes:
    • BIT8_UINT8_BUFFER_SIZE, BIT16_UINT8_BUFFER_SIZE, ..., BIT104_UINT8_BUFFER_SIZE
    • BIN8_CHAR_BUFFER_SIZE, BIN16_CHAR_BUFFER_SIZE, ..., BIN64_CHAR_BUFFER_SIZE

Buffer Types

  • bit8_uint8_buffer_t, bit16_uint8_buffer_t, bit24_uint8_buffer_t, ... up to bit104_uint8_buffer_t
  • bin8_char_buffer_t, bin16_char_buffer_t, bin32_char_buffer_t, bin64_char_buffer_t

Union Types (Little Endian Conversions)

  • bytes_to_float_t
  • bytes_to_double_t
  • bytes_to_uint64_t, bytes_to_int64_t
  • bytes_to_uint32_t, bytes_to_int32_t
  • bytes_to_uint16_t, bytes_to_int16_t

API Reference

Chip ID & MAC

  • uint32_t get_uint32_chip_id(void);

    • Get a unique 32-bit chip ID derived from e-Fuse MAC.
  • uint64_t get_uint64_chip_id(void);

    • Get unique 64-bit chip ID from MAC.
  • uint64_t get_efuse_mac(void);

    • Get the chip's MAC address.

Binary String Conversion

  • const char* uint8_to_binary(uint8_t value);
  • const char* int8_to_binary(int8_t value);
  • const char* uint16_to_binary(uint16_t value);
  • const char* int16_to_binary(int16_t value);
  • const char* uint32_to_binary(uint32_t value);
  • const char* int32_to_binary(int32_t value);
  • const char* uint64_to_binary(uint64_t value);
  • const char* int64_to_binary(int64_t value);

Value / Byte Array Conversion

  • uint16_t bytes_to_uint16(const uint8_t* bytes, bool little_endian);

  • uint32_t bytes_to_uint32(const uint8_t* bytes, bool little_endian);

  • uint64_t bytes_to_uint64(const uint8_t* bytes, bool little_endian);

  • int16_t bytes_to_int16(const uint8_t* bytes, bool little_endian);

  • int32_t bytes_to_int32(const uint8_t* bytes, bool little_endian);

  • int64_t bytes_to_int64(const uint8_t* bytes, bool little_endian);

  • Serialize value to bytes (endianness selectable):

    • void uint16_to_bytes(uint16_t value, uint8_t* bytes, bool little_endian);
    • void uint32_to_bytes(uint32_t value, uint8_t* bytes, bool little_endian);
    • void uint64_to_bytes(uint64_t value, uint8_t* bytes, bool little_endian);
    • void int16_to_bytes(int16_t value, uint8_t* bytes, bool little_endian);
    • void int32_to_bytes(int32_t value, uint8_t* bytes, bool little_endian);
    • void int64_to_bytes(int64_t value, uint8_t* bytes, bool little_endian);
    • void float_to_bytes(float value, uint8_t* bytes, bool little_endian);
    • void double_to_bytes(double value, uint8_t* bytes, bool little_endian);

Byte Copy Utility

  • void copy_bytes(const uint8_t* source, uint8_t* destination, size_t size);

Firmware Version

  • const char* type_utils_get_fw_version(void);
  • int32_t type_utils_get_fw_version_number(void);

Dependencies

  • <esp_mac.h>
  • <stdint.h>, <stdbool.h>
  • "type_utils_version.h"

References

  • ESP-IDF Documentation
  • MIT License

Clone this wiki locally