|
|
||
|---|---|---|
| include | ||
| src | ||
| .gitignore | ||
| Makefile | ||
| README.md | ||
| module.mk | ||
README.md
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
- Minimal footprint: No buffering, no dynamic memory allocation.
- Simple integration: Only requires
getch()andputch()implementations. - Integer IO: Input and output
int32_t,uint32_t,int64_tanduint64_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
All functions are documented with Doxygen-style comments in include/lightio.h. For detailed parameter descriptions and behavior, refer to the header file.
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
String Output
size_t output_str(const char* str, unsigned int width); // Output null-terminated string
Integer Input Functions
// 32-bit integers
int32_t input_int32_bin(void);
uint32_t input_uint32_bin(void);
int32_t input_int32_dec(void);
uint32_t input_uint32_dec(void);
int32_t input_int32_hex(void);
uint32_t input_uint32_hex(void);
// 64-bit integers
int64_t input_int64_bin(void);
uint64_t input_uint64_bin(void);
int64_t input_int64_dec(void);
uint64_t input_uint64_dec(void);
int64_t input_int64_hex(void);
uint64_t input_uint64_hex(void);
Behavior:
- Skips leading whitespace.
- Reads until next whitespace character.
- Supports optional leading
+or-for only signed variants. - Reads binary (0/1), decimal (0-9), or hex (0-9, a-f, A-F) format.
- No additional format checking, wrong input format causes undefined behavior.
Integer Output Functions
// 32-bit integers
size_t output_int32_bin(int32_t value, unsigned int width);
size_t output_uint32_bin(uint32_t value, unsigned int width);
size_t output_int32_dec(int32_t value, unsigned int width);
size_t output_uint32_dec(uint32_t value, unsigned int width);
size_t output_int32_hex(int32_t value, unsigned int width);
size_t output_uint32_hex(uint32_t value, unsigned int width);
// 64-bit integers
size_t output_int64_bin(int64_t value, unsigned int width);
size_t output_uint64_bin(uint64_t value, unsigned int width);
size_t output_int64_dec(int64_t value, unsigned int width);
size_t output_uint64_dec(uint64_t value, unsigned int width);
size_t output_int64_hex(int64_t value, unsigned int width);
size_t 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').
- Returns the actual number of output characters.