Files
templates/c/set-protocol/include/set_regmap.h

49 lines
2.3 KiB
C

/* Transport-independent 16-bit word address space. No heap, clocks or board headers. */
#ifndef SET_REGMAP_H
#define SET_REGMAP_H
#include "pcan_abi.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SET_REG_MAX_WORDS 124u
#define SET_REG_READ 1u
#define SET_REG_WRITE 2u
enum { SET_REG_OK=0, SET_REG_FUNCTION=1, SET_REG_ADDRESS=2,
SET_REG_VALUE=3, SET_REG_FAILURE=4, SET_REG_BUSY=6 };
typedef unsigned (*set_reg_read_fn)(void *, uint16_t, uint16_t, uint16_t *);
typedef unsigned (*set_reg_write_fn)(void *, uint16_t, uint16_t);
typedef struct {
uint16_t base, words, kind, version, instance, flags;
void *context;
set_reg_read_fn read;
set_reg_write_fn write;
} set_reg_region;
typedef struct {
const set_reg_region *regions;
size_t count;
} set_regmap;
/* Directory at 0: 8-word header, then 8 words per region. Reject overlaps,
* wraparound, invalid callbacks and overlap with the directory itself.
* Regions and contexts must outlive the map and remain unchanged. */
PCAN_ABI_API unsigned set_regmap_init(set_regmap *, const set_reg_region *, size_t);
/* One region per request. Preflight all bounds/capacity before calling a module.
* Writes return absolute address/value echo. Reads must be side-effect free.
* A module validates the entire write before mutation. Caller serializes access. */
PCAN_ABI_API unsigned set_regmap_request(const set_regmap *, unsigned function,
uint16_t address, uint16_t value, uint16_t *out, size_t capacity, size_t *count);
/* Complete Modbus RTU FC03/06 frame; no broadcast. Capacity >=253 bytes.
* CRC/address/capacity validation precedes dispatch. */
PCAN_ABI_API size_t set_regmap_rtu(const set_regmap *, unsigned device,
const uint8_t *, size_t, uint8_t *, size_t);
/* Legacy UMP CAN envelope, usable by any region: BE address/value/1F00/token/fn.
* Returns padded EVEN words, last padding word=0. Caller retains original
* requested count; token/error accompany fragments. Output capacity >=124. */
PCAN_ABI_API size_t set_regmap_can(const set_regmap *, const uint8_t *, size_t,
uint16_t *, size_t, unsigned *token, unsigned *error);
PCAN_ABI_API size_t set_regmap_can_fragment(const uint16_t *, size_t count,
size_t index, unsigned token, unsigned error, uint8_t *, size_t capacity);
#ifdef __cplusplus
}
#endif
#endif