Files
templates/c/set-protocol/ports/example-emulator/service.c
T
Andrey e93bd3b6aa style: унифицируй форматирование C и C++
Закрепи табуляцию и размещение скобок в настройках проекта. Примени единый стиль к собственным исходникам.
2026-12-02 11:33:41 +03:00

57 lines
2.2 KiB
C

/*
* Пример композиции сервисов эмулятора поверх общей карты регистров. Он показывает подключение
* контекста приложения к переносимому обработчику, а не задаёт обязательную карту для всех
* устройств.
*/
/* Minimal board-independent composition. This compiles without any MCU headers.
* Adapt these entry points to the selected board's network/USB/UART/CAN port.
* Run every call in one task (or protect the entire call with the same lock). */
#include "set_emu_igbt.h"
static set_igbt_emu drivers[2];
static set_reg_region regions[2];
static set_regmap map;
unsigned example_service_init(void)
{
unsigned i;
for (i = 0; i < 2; ++i)
{
set_reg_region *r = &regions[i];
set_igbt_init(&drivers[i]);
r->base = (uint16_t)(0x300 + i * 0x100);
r->words = SET_IGBT_WORDS;
r->kind = 4;
r->version = 2;
r->instance = (uint16_t)i;
r->flags = 3;
r->context = &drivers[i];
r->read = set_igbt_read;
r->write = set_igbt_write;
}
return set_regmap_init(&map, regions, 2);
}
void example_service_tick(uint8_t inputs0, uint8_t inputs1, uint8_t outputs[2])
{
set_igbt_tick(&drivers[0], inputs0);
set_igbt_tick(&drivers[1], inputs1);
outputs[0] = drivers[0].rx;
outputs[1] = drivers[1].rx;
}
unsigned example_custom_protocol(unsigned function, uint16_t address, uint16_t value,
uint16_t *words, size_t capacity, size_t *count)
{
/* Caller has already decoded/authenticated its own transport envelope. */
return set_regmap_request(&map, function, address, value, words, capacity, count);
}
size_t example_uart_or_usb(const uint8_t *frame, size_t length, uint8_t *reply, size_t capacity)
{
return set_regmap_rtu(&map, 16, frame, length, reply, capacity);
}
size_t example_can(const uint8_t frame[8], uint16_t *snapshot, size_t capacity,
unsigned *token, unsigned *error)
{
/* Select the CAN request ID outside the model; send the returned snapshot
* using set_regmap_can_fragment and the board's chosen response CAN ID. */
return set_regmap_can(&map, frame, 8, snapshot, capacity, token, error);
}