Skip to content

Error Types

Overview

tiny_error_type.h defines the unified error code system for TinySHM middleware. Every function in TinyMath, TinyDSP, and the platform layer returns a tiny_error_t value, making error handling consistent across the entire codebase.

The error codes follow a modular namespace design: each major module is allocated its own base offset, and individual error conditions are defined as offsets relative to that base.


Error Code Architecture

%%{init: {'themeVariables': {'fontSize': '22px'}}}%%
graph LR
    GEN["0x00xx<br/><b>General</b>"]
    WIFI["0x30xx<br/>WiFi"]
    MESH["0x40xx<br/>Mesh"]
    FLASH["0x60xx<br/>Flash"]
    CRYPTO["0xC0xx<br/>Crypto"]
    MEM["0xD0xx<br/>MemProt"]
    MATH["0x70000<br/><b>TinyMath</b>"]
    DSP["0x80000<br/><b>TinyDSP</b>"]

    style MATH fill:#eef2ff,stroke:#4f6af5,stroke-width:2px
    style DSP fill:#eef2ff,stroke:#4f6af5,stroke-width:2px

General Error Codes

These are the basic error codes used across all middleware layers:

Code Macro Description
0 TINY_OK Success — no error
-1 TINY_FAIL Generic failure
0x101 TINY_ERR_NO_MEM Out of memory
0x102 TINY_ERR_INVALID_ARG Invalid argument
0x103 TINY_ERR_INVALID_STATE Invalid state
0x104 TINY_ERR_INVALID_SIZE Invalid size
0x105 TINY_ERR_NOT_FOUND Resource not found
0x106 TINY_ERR_NOT_SUPPORTED Operation not supported
0x107 TINY_ERR_TIMEOUT Operation timed out
0x108 TINY_ERR_INVALID_RESPONSE Invalid response received
0x109 TINY_ERR_INVALID_CRC CRC or checksum mismatch
0x10A TINY_ERR_INVALID_VERSION Invalid version
0x10B TINY_ERR_INVALID_MAC Invalid MAC address
0x10C TINY_ERR_NOT_FINISHED Operation not fully completed
0x10D TINY_ERR_NOT_ALLOWED Operation not allowed

Other Module Bases

These base addresses are reserved for other TinySHM middleware modules (not part of TinyMath/TinyDSP):

Base Macro Module
0x3000 TINY_ERR_WIFI_BASE WiFi subsystem
0x4000 TINY_ERR_MESH_BASE Mesh networking
0x6000 TINY_ERR_FLASH_BASE Flash storage
0xC000 TINY_ERR_HW_CRYPTO_BASE Hardware cryptography
0xD000 TINY_ERR_MEMPROT_BASE Memory protection

Math Error Codes [0x70000]

Base: TINY_ERR_MATH_BASE = 0x70000

Errors specific to TinyMath operations:

Code Macro Trigger Condition
0x70001 TINY_ERR_MATH_INVALID_LENGTH Array or vector length is zero or exceeds limits
0x70002 TINY_ERR_MATH_INVALID_PARAM Parameter value is out of valid range
0x70003 TINY_ERR_MATH_PARAM_OUTOFRANGE Parameter exceeds allowed bounds
0x70004 TINY_ERR_MATH_UNINITIALIZED Module or data structure used before initialization
0x70005 TINY_ERR_MATH_REINITIALIZED Module or data structure initialised more than once
0x70006 TINY_ERR_MATH_ARRAY_NOT_ALIGNED Array pointer not aligned to required boundary
0x70007 TINY_ERR_MATH_NULL_POINTER Null pointer passed where a valid pointer is required
0x70008 TINY_ERR_MATH_ZERO_DIVISION Division by zero occurred
0x70009 TINY_ERR_MATH_NEGATIVE_SQRT Square root of a negative value attempted

DSP Error Codes [0x80000]

Base: TINY_ERR_DSP_BASE = 0x80000

Errors specific to TinyDSP operations:

Code Macro Trigger Condition
0x80001 TINY_ERR_DSP_INVALID_LENGTH Signal or window length is zero or exceeds limits
0x80002 TINY_ERR_DSP_INVALID_PARAM Parameter value is out of valid range
0x80003 TINY_ERR_DSP_PARAM_OUTOFRANGE Parameter exceeds allowed bounds
0x80004 TINY_ERR_DSP_UNINITIALIZED DSP module used before initialisation
0x80005 TINY_ERR_DSP_REINITIALIZED DSP module initialised more than once
0x80006 TINY_ERR_DSP_ARRAY_NOT_ALIGNED Buffer pointer not aligned to required boundary
0x80007 TINY_ERR_DSP_NULL_POINTER Null pointer passed where a valid pointer is required
0x80008 TINY_ERR_DSP_ZERO_DIVISION Division by zero occurred
0x80009 TINY_ERR_DSP_NEGATIVE_SQRT Square root of a negative value attempted
0x8000A TINY_ERR_DSP_MISMATCH Data format or parameter mismatch
0x8000B TINY_ERR_DSP_INVALID_MODE Invalid operation mode selected
0x8000C TINY_ERR_DSP_MEMORY_ALLOC Memory allocation failed during DSP operation

Usage Pattern

#include "tiny_math.h"

tiny_error_t err = tiny_vec_add_f32(a, b, c, len, 1, 1, 1);

if (err != TINY_OK) {
    switch (err) {
        case TINY_ERR_MATH_NULL_POINTER:
            // Handle null pointer
            break;
        case TINY_ERR_MATH_INVALID_LENGTH:
            // Handle invalid length
            break;
        default:
            // Generic error handling
            break;
    }
}

Checking vs. TINY_OK

Always check against TINY_OK (0) rather than == false. Some error codes have non-zero values that could be mistaken for truthy in boolean contexts.


Source Code

/**
 * @file tiny_error_type.h
 * @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg)
 * @brief The configuration file for the tiny_math middleware.
 * @version 1.0
 * @date 2025-04-15
 * @copyright Copyright (c) 2025
 *
 */

#pragma once

#ifdef __cplusplus
extern "C"
{
#endif

    /* TYPE DEFINITIONS */
    typedef int tiny_error_t; // Error type for the tiny_math middleware

/* MACROS */
/* Definitions for error constants. */
#define TINY_OK 0    /*!< tiny_err_t value indicating success (no error) */
#define TINY_FAIL -1 /*!< Generic tiny_err_t code indicating failure */

#define TINY_ERR_NO_MEM 0x101           /*!< Out of memory */
#define TINY_ERR_INVALID_ARG 0x102      /*!< Invalid argument */
#define TINY_ERR_INVALID_STATE 0x103    /*!< Invalid state */
#define TINY_ERR_INVALID_SIZE 0x104     /*!< Invalid size */
#define TINY_ERR_NOT_FOUND 0x105        /*!< Requested resource not found */
#define TINY_ERR_NOT_SUPPORTED 0x106    /*!< Operation or feature not supported */
#define TINY_ERR_TIMEOUT 0x107          /*!< Operation timed out */
#define TINY_ERR_INVALID_RESPONSE 0x108 /*!< Received response was invalid */
#define TINY_ERR_INVALID_CRC 0x109      /*!< CRC or checksum was invalid */
#define TINY_ERR_INVALID_VERSION 0x10A  /*!< Version was invalid */
#define TINY_ERR_INVALID_MAC 0x10B      /*!< MAC address was invalid */
#define TINY_ERR_NOT_FINISHED 0x10C     /*!< Operation has not fully completed */
#define TINY_ERR_NOT_ALLOWED 0x10D      /*!< Operation is not allowed */

#define TINY_ERR_WIFI_BASE 0x3000      /*!< Starting number of WiFi error codes */
#define TINY_ERR_MESH_BASE 0x4000      /*!< Starting number of MESH error codes */
#define TINY_ERR_FLASH_BASE 0x6000     /*!< Starting number of flash error codes */
#define TINY_ERR_HW_CRYPTO_BASE 0xc000 /*!< Starting number of HW cryptography module error codes */
#define TINY_ERR_MEMPROT_BASE 0xd000   /*!< Starting number of Memory Protection API error codes */

#define TINY_ERR_MATH_BASE 0x70000
#define TINY_ERR_MATH_INVALID_LENGTH (TINY_ERR_MATH_BASE + 1)
#define TINY_ERR_MATH_INVALID_PARAM (TINY_ERR_MATH_BASE + 2)
#define TINY_ERR_MATH_PARAM_OUTOFRANGE (TINY_ERR_MATH_BASE + 3)
#define TINY_ERR_MATH_UNINITIALIZED (TINY_ERR_MATH_BASE + 4)
#define TINY_ERR_MATH_REINITIALIZED (TINY_ERR_MATH_BASE + 5)
#define TINY_ERR_MATH_ARRAY_NOT_ALIGNED (TINY_ERR_MATH_BASE + 6)
#define TINY_ERR_MATH_NULL_POINTER (TINY_ERR_MATH_BASE + 7)
#define TINY_ERR_MATH_ZERO_DIVISION (TINY_ERR_MATH_BASE + 8)
#define TINY_ERR_MATH_NEGATIVE_SQRT (TINY_ERR_MATH_BASE + 9)

#define TINY_ERR_DSP_BASE 0x80000
#define TINY_ERR_DSP_INVALID_LENGTH (TINY_ERR_DSP_BASE + 1)
#define TINY_ERR_DSP_INVALID_PARAM (TINY_ERR_DSP_BASE + 2)
#define TINY_ERR_DSP_PARAM_OUTOFRANGE (TINY_ERR_DSP_BASE + 3)
#define TINY_ERR_DSP_UNINITIALIZED (TINY_ERR_DSP_BASE + 4)
#define TINY_ERR_DSP_REINITIALIZED (TINY_ERR_DSP_BASE + 5)
#define TINY_ERR_DSP_ARRAY_NOT_ALIGNED (TINY_ERR_DSP_BASE + 6)
#define TINY_ERR_DSP_NULL_POINTER (TINY_ERR_DSP_BASE + 7)
#define TINY_ERR_DSP_ZERO_DIVISION (TINY_ERR_DSP_BASE + 8)
#define TINY_ERR_DSP_NEGATIVE_SQRT (TINY_ERR_DSP_BASE + 9)
#define TINY_ERR_DSP_MISMATCH (TINY_ERR_DSP_BASE + 10)
#define TINY_ERR_DSP_INVALID_MODE (TINY_ERR_DSP_BASE + 11)
#define TINY_ERR_DSP_MEMORY_ALLOC (TINY_ERR_DSP_BASE + 12)

#ifdef __cplusplus
}
#endif