A lightweight alternative to the standard C library’s stdio, designed for extremely constrained embedded systems and devices where memory and code size are critical concerns.
Overview
LightIO provides essential input/output functionality without the overhead of standard C library implementations. It supports only integers and strings with minimal formatting, for a higher efficiency and a smaller footprint.
Features
Minimal footprint: No buffering, no dynamic memory allocation.
Simple integration: Only requires getch() and putch() implementations.
Integer IO: Input and output int32_t, uint32_t, int64_t and uint64_t, in binary, decimal and hexadecimal.
String IO: Basic string input and output functions.
No dependencies: Works without standard C library or OS support.
Usage
Simply build this project with make. The static library liblightio.a will result in the build directory. If you want a “sysroot” with all libraries and headers ready, install the built library along with headers into the “sysroot” with make DESTDIR=/path/to/sysroot install.
You can optionally provide a toolchain file to specify build target and options, with make TOOLCHAIN_FILE=/path/to/toochain/file. Toolchain files in Sonnet SKD are also applicable here. This is an example toolchain file:
CC := clang
AR := llvm-ar
CFLAGS += --target=riscv32-unknown-elf -mabi=ilp32 -march=rv32im -ffreestanding -nostdlib -O3 -g
You must provide two functions, getch() and putch(), that interface with your console/serial hardware. If you are using Sonnet SDK, they are already implemented. If you are using AM, putch() is already implemented. Here are examples of the two functions:
// Get a single character from input (blocking)
char getch(void) {
// Your implementation here
// Example: wait for UART receive register
while (!(UART_STATUS & UART_RX_READY));
return UART_DATA;
}
// Output a single character
void putch(char ch) {
// Your implementation here
// Example: wait for UART transmit ready and send
while (!(UART_STATUS & UART_TX_READY));
UART_DATA = ch;
}
API Reference
Primitive Functions (User-Provided)
char getch(void); // Read one character from input
void putch(char ch); // Write one character to output
String Input Functions
size_t input_word(char* dest, size_t max_n); // reads a word into buffer
size_t input_line(char* dest, size_t max_n); // reads a line into buffer
No additional format checking, wrong input format causes undefined behavior.
Integer Output Functions
// 32-bit integers
void output_int32_bin(int32_t value, unsigned int width);
void output_uint32_bin(uint32_t value, unsigned int width);
void output_int32_dec(int32_t value, unsigned int width);
void output_uint32_dec(uint32_t value, unsigned int width);
void output_int32_hex(int32_t value, unsigned int width);
void output_uint32_hex(uint32_t value, unsigned int width);
// 64-bit integers
void output_int64_bin(int64_t value, unsigned int width);
void output_uint64_bin(uint64_t value, unsigned int width);
void output_int64_dec(int64_t value, unsigned int width);
void output_uint64_dec(uint64_t value, unsigned int width);
void output_int64_hex(int64_t value, unsigned int width);
void output_uint64_hex(uint64_t value, unsigned int width);
Parameters:
value: The integer to output.
width: Minimum output width (0 = auto-size, no padding).
Behavior:
Negative values output with leading ‘-‘ sign.
Positive width pads with leading zeros to reach minimum width.
Hex output uses lowercase letters (‘a’-‘f’).
关于
A lightweight alternative to the standard C library's `stdio`, designed for extremely constrained embedded systems and devices where memory and code size are critical concerns.
LightIO
A lightweight alternative to the standard C library’s
stdio, designed for extremely constrained embedded systems and devices where memory and code size are critical concerns.Overview
LightIO provides essential input/output functionality without the overhead of standard C library implementations. It supports only integers and strings with minimal formatting, for a higher efficiency and a smaller footprint.
Features
getch()andputch()implementations.int32_t,uint32_t,int64_tanduint64_t, in binary, decimal and hexadecimal.Usage
Simply build this project with
make. The static libraryliblightio.awill result in thebuilddirectory. If you want a “sysroot” with all libraries and headers ready, install the built library along with headers into the “sysroot” withmake DESTDIR=/path/to/sysroot install.You can optionally provide a toolchain file to specify build target and options, with
make TOOLCHAIN_FILE=/path/to/toochain/file. Toolchain files in Sonnet SKD are also applicable here. This is an example toolchain file:You must provide two functions,
getch()andputch(), that interface with your console/serial hardware. If you are using Sonnet SDK, they are already implemented. If you are using AM,putch()is already implemented. Here are examples of the two functions:API Reference
Primitive Functions (User-Provided)
String Input Functions
String Output
Integer Input Functions
Behavior:
+or-for only signed variants.Integer Output Functions
Parameters:
value: The integer to output.width: Minimum output width (0 = auto-size, no padding).Behavior: