305 lines
13 KiB
C
305 lines
13 KiB
C
/*
|
|
* Протокол периферийного контроллера ПМ35/TMS320F28335: регистры, команды и разбор ответов.
|
|
* Этот профиль имеет собственную адресацию и не должен подменяться протоколом основного
|
|
* TMS320F2812.
|
|
*/
|
|
|
|
#include "periph28335.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include "set_crc.h"
|
|
#include "balsam167_eeprom_block.h"
|
|
|
|
static int eeprom_reader_valid(const periph28335_eeprom_reader *r, unsigned profile)
|
|
|
|
{
|
|
return r && r->controller >= 1U && r->controller <= 255U &&
|
|
r->start < balsam167_eeprom_capacity(profile) && r->length &&
|
|
r->length <= balsam167_eeprom_capacity(profile) - r->start && r->received < r->length;
|
|
}
|
|
|
|
int periph28335_eeprom_begin_profile(periph28335_eeprom_reader *r,
|
|
uint32_t controller, uint32_t start, uint32_t length, unsigned profile)
|
|
{
|
|
if (!r) return PERIPH28335_ERROR_ARGUMENT;
|
|
/* Invalidate first: a rejected new range must not leave an old read active. */
|
|
memset(r, 0, sizeof(*r));
|
|
if (!controller || controller > 255U || start >= balsam167_eeprom_capacity(profile) ||
|
|
!length || length > balsam167_eeprom_capacity(profile) - start)
|
|
return PERIPH28335_ERROR_RANGE;
|
|
r->controller = controller; r->start = start; r->length = length;
|
|
return PERIPH28335_OK;
|
|
}
|
|
|
|
size_t periph28335_eeprom_request(const periph28335_eeprom_reader *r,
|
|
uint8_t *output, size_t capacity)
|
|
{
|
|
uint8_t payload[6];
|
|
uint32_t word;
|
|
if (!eeprom_reader_valid(r, 0)) return 0;
|
|
word = (r->start + r->received) / 2U;
|
|
payload[0] = (uint8_t)r->controller; payload[1] = 56U; /* CMD_PEEK only */
|
|
/* 167 routes words 0..0x3fff to SPI EEPROM. The old terminal's
|
|
* 0x10000000 memory selector is NOT implemented by this firmware. */
|
|
payload[2] = (uint8_t)word; payload[3] = (uint8_t)(word >> 8);
|
|
payload[4] = 0; payload[5] = 0;
|
|
return periph28335_append_crc(payload, sizeof(payload), output, capacity);
|
|
}
|
|
|
|
int periph28335_eeprom_accept(periph28335_eeprom_reader *r,
|
|
const uint8_t *reply, size_t size, uint8_t *output, size_t capacity)
|
|
{
|
|
unsigned odd, count;
|
|
if (!eeprom_reader_valid(r, 0) || !reply || !output) return PERIPH28335_ERROR_ARGUMENT;
|
|
/* The UART layer may strip two legacy zero-padding bytes after CRC. */
|
|
if (size != 8U && size != 10U) return PERIPH28335_ERROR_LENGTH;
|
|
if (periph28335_crc16_modbus(reply, 8U) != 0) return PERIPH28335_ERROR_CRC;
|
|
if (reply[0] != r->controller || reply[1] != 56U) return PERIPH28335_ERROR_HEADER;
|
|
odd = (unsigned)((r->start + r->received) & 1U);
|
|
count = 2U - odd;
|
|
if (count > r->length - r->received) count = (unsigned)(r->length - r->received);
|
|
if (capacity < count) return PERIPH28335_ERROR_CAPACITY;
|
|
output[0] = reply[odd ? 2U : 3U];
|
|
if (count == 2U) output[1] = reply[2];
|
|
r->received += count;
|
|
return (int)count;
|
|
}
|
|
|
|
/* One common codec for all host ports. Physical EEPROM bytes already have
|
|
* network-word order; only trim an odd first/last byte, never swap them. */
|
|
static unsigned eeprom_block_words(const periph28335_eeprom_reader *r, unsigned profile)
|
|
{
|
|
uint32_t n;
|
|
if (!eeprom_reader_valid(r, profile)) return 0;
|
|
n = (r->length - r->received + ((r->start + r->received) & 1U) + 1U) / 2U;
|
|
/* A response cannot cross a bank boundary; trim the last block there. */
|
|
if (n > 32768U - ((r->start+r->received)/2U % 32768U))
|
|
n = 32768U - ((r->start+r->received)/2U % 32768U);
|
|
return (unsigned)(n > BALSAM167_EEPROM_BLOCK_WORDS ? BALSAM167_EEPROM_BLOCK_WORDS : n);
|
|
}
|
|
size_t periph28335_eeprom_block_expected_profile(const periph28335_eeprom_reader *r, unsigned profile)
|
|
{
|
|
unsigned n = eeprom_block_words(r, profile);
|
|
return n ? 5U + 2U*n : 0;
|
|
}
|
|
size_t periph28335_eeprom_block_request_profile(const periph28335_eeprom_reader *r, uint8_t *out, size_t cap, unsigned profile)
|
|
{
|
|
uint8_t p[6];
|
|
unsigned n = eeprom_block_words(r, profile), a, quantity;
|
|
uint32_t word;
|
|
if (!n) return 0;
|
|
word = (r->start + r->received) / 2U;
|
|
a = 0x8000U + (unsigned)(word % 32768U);
|
|
quantity = n | (profile ? (0x8000U | (unsigned)(word/32768U)*256U) : 0U);
|
|
if (!balsam167_eeprom_block_valid_profile(3,a,quantity,profile)) return 0;
|
|
p[0]=(uint8_t)r->controller; p[1]=3;
|
|
p[2]=(uint8_t)(a>>8); p[3]=(uint8_t)a; p[4]=(uint8_t)(quantity>>8); p[5]=(uint8_t)quantity;
|
|
return periph28335_append_crc(p,6,out,cap);
|
|
}
|
|
int periph28335_eeprom_block_accept_profile(periph28335_eeprom_reader *r,
|
|
const uint8_t *reply, size_t size, uint8_t *out, size_t cap, unsigned profile)
|
|
{
|
|
unsigned n=eeprom_block_words(r, profile), odd, count;
|
|
if (!n || !reply || !out) return PERIPH28335_ERROR_ARGUMENT;
|
|
if (size != 5U+2U*n) return PERIPH28335_ERROR_LENGTH;
|
|
if (periph28335_crc16_modbus(reply,size)) return PERIPH28335_ERROR_CRC;
|
|
/* Common address 16 is answered by the board's own Mode, 5 or 6. */
|
|
if ((reply[0] != r->controller && !(r->controller==16 && (reply[0]==5 || reply[0]==6))) ||
|
|
reply[1]!=3 || reply[2]!=2U*n) return PERIPH28335_ERROR_HEADER;
|
|
odd=(unsigned)((r->start+r->received)&1U);
|
|
count=2U*n-odd;
|
|
if (count>r->length-r->received) count=(unsigned)(r->length-r->received);
|
|
if (cap<count) return PERIPH28335_ERROR_CAPACITY;
|
|
memcpy(out,reply+3+odd,count);
|
|
r->received+=count;
|
|
return (int)count;
|
|
}
|
|
|
|
/* Keep all existing exported symbols and the 16-byte reader ABI unchanged. */
|
|
int periph28335_eeprom_begin(periph28335_eeprom_reader *r, uint32_t c, uint32_t a, uint32_t n)
|
|
{ return periph28335_eeprom_begin_profile(r,c,a,n,0); }
|
|
size_t periph28335_eeprom_block_expected(const periph28335_eeprom_reader *r)
|
|
{ return periph28335_eeprom_block_expected_profile(r,0); }
|
|
size_t periph28335_eeprom_block_request(const periph28335_eeprom_reader *r, uint8_t *o, size_t n)
|
|
{ return periph28335_eeprom_block_request_profile(r,o,n,0); }
|
|
int periph28335_eeprom_block_accept(periph28335_eeprom_reader *r, const uint8_t *p, size_t n, uint8_t *o, size_t cap)
|
|
{ return periph28335_eeprom_block_accept_profile(r,p,n,o,cap,0); }
|
|
uint32_t periph28335_eeprom_capacity_profile(unsigned profile)
|
|
{ return (uint32_t)balsam167_eeprom_capacity(profile); }
|
|
/* Legacy EXTEND carries word addresses and octet lengths. A distinct M95
|
|
* subcommand makes a wrong-profile write fail closed on either firmware. */
|
|
size_t periph28335_eeprom_write_profile(uint32_t controller, uint32_t destination,
|
|
uint32_t source, uint32_t length, unsigned profile, uint8_t *out, size_t cap)
|
|
{
|
|
uint8_t p[16]; unsigned i;
|
|
uint32_t capacity = periph28335_eeprom_capacity_profile(profile);
|
|
uint32_t limit = capacity ? capacity-(BALSAM167_EEPROM_PARAM_WORDS+1U)*2U : 0;
|
|
if (!controller || controller>255U || !length || length>199993U ||
|
|
destination>=limit/2U || length>limit-destination*2U) return 0;
|
|
p[0]=(uint8_t)controller; p[1]=60;
|
|
for(i=0;i<4;i++) {
|
|
p[2+i]=(uint8_t)(destination>>(8*i));
|
|
p[6+i]=(uint8_t)(source>>(8*i));
|
|
p[10+i]=(uint8_t)(length>>(8*i));
|
|
}
|
|
p[14]=(uint8_t)(profile ? BALSAM167_EEPROM_WRITE_M95 : 4); p[15]=0;
|
|
return periph28335_append_crc(p,16,out,cap);
|
|
}
|
|
|
|
typedef struct {
|
|
const char *project;
|
|
const char *commands[PERIPH28335_COMMAND_COUNT];
|
|
} periph28335_project_t;
|
|
|
|
static const periph28335_project_t projects[] = {
|
|
{ "По умолчанию", { "Test", "Def", "Save", "Load", "Calibr", "Calcul", "Secret", "Light", "Raw", "-", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "23470", { "Test", "Def", "Save", "Load", "Calibr", "Read", "Secret", "-", "-", "-", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "23550", { "Test", "Def", "Save", "Load", "Calibr", "Read", "Secret", "Send", "-", "-", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "23550.2", { "Test", "Def", "Save", "Load", "Calibr", "Calcul", "Secret", "Send", "Raw", "Beep", "", "", "", "", "Log", "Reset", "Nothing at all" } },
|
|
{ "ICE 22220.1-3", { "Test", "Zero", "Save", "Def", "Calibr", "Read", "ExtLamp", "ExtLite", "-", "-", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "ICE 22220.4-5", { "Test", "Def", "Save", "Load", "Raw", "Read", "ExtLamp", "ExtLite", "No log", "-", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "Бальзам 161", { "Test", "Zero", "Save", "Def", "Calibr", "Clbr 400", "Stop", "Start", "Init", "Secret", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "Бальзам 162", { "Test", "Def", "Save", "Load", "Calibr", "Secret", "Stop", "Start", "Init", "Tune", "-", "-", "-", "-", "-", "Reset", "Nothing at all" } },
|
|
{ "Бальзам 163", { "Test", "Def", "Save", "Load", "Calibr", "Calcul", "Stop", "Start", "Init", "Tune", "Secret", "-", "-", "-", "-", "Reset", "Nothing at all" } }
|
|
};
|
|
|
|
static void put_be16(uint8_t *output, uint16_t value)
|
|
{
|
|
output[0] = (uint8_t)(value >> 8);
|
|
output[1] = (uint8_t)value;
|
|
}
|
|
|
|
static uint16_t get_be16(const uint8_t *input)
|
|
{
|
|
return (uint16_t)(((uint16_t)input[0] << 8) | input[1]);
|
|
}
|
|
|
|
uint16_t periph28335_crc16_modbus(const uint8_t *data, size_t size)
|
|
{
|
|
return set_crc16_modbus(data, size);
|
|
}
|
|
|
|
size_t periph28335_append_crc(const uint8_t *payload, size_t payload_size,
|
|
uint8_t *output, size_t output_size)
|
|
{
|
|
uint16_t crc;
|
|
if ((output == NULL) || ((payload == NULL) && (payload_size != 0U)) ||
|
|
(payload_size > output_size) || (output_size - payload_size < 2U)) {
|
|
return 0U;
|
|
}
|
|
if (payload_size != 0U) memcpy(output, payload, payload_size);
|
|
crc = periph28335_crc16_modbus(payload, payload_size);
|
|
output[payload_size] = (uint8_t)crc;
|
|
output[payload_size + 1U] = (uint8_t)(crc >> 8);
|
|
return payload_size + 2U;
|
|
}
|
|
|
|
size_t periph28335_build_read_registers(
|
|
uint8_t controller, uint16_t start, uint16_t count,
|
|
uint8_t *output, size_t output_size)
|
|
{
|
|
uint8_t payload[6];
|
|
if ((count == 0U) || (count > PERIPH28335_REGISTER_COUNT) ||
|
|
(start >= PERIPH28335_REGISTER_COUNT) ||
|
|
((uint32_t)start + count > PERIPH28335_REGISTER_COUNT)) return 0U;
|
|
payload[0] = controller;
|
|
payload[1] = 3U;
|
|
put_be16(&payload[2], start);
|
|
put_be16(&payload[4], count);
|
|
return periph28335_append_crc(payload, sizeof payload, output, output_size);
|
|
}
|
|
|
|
size_t periph28335_build_write_register(
|
|
uint8_t controller, uint16_t address, uint16_t value,
|
|
uint8_t *output, size_t output_size)
|
|
{
|
|
uint8_t payload[6];
|
|
if (address >= PERIPH28335_REGISTER_COUNT) return 0U;
|
|
payload[0] = controller;
|
|
payload[1] = 6U;
|
|
put_be16(&payload[2], address);
|
|
put_be16(&payload[4], value);
|
|
return periph28335_append_crc(payload, sizeof payload, output, output_size);
|
|
}
|
|
|
|
size_t periph28335_build_command(
|
|
uint8_t controller, uint8_t command_index,
|
|
uint8_t *output, size_t output_size)
|
|
{
|
|
uint16_t value;
|
|
if (command_index >= PERIPH28335_COMMAND_COUNT) return 0U;
|
|
value = command_index < 16U ? (uint16_t)(1UL << command_index) : 0U;
|
|
return periph28335_build_write_register(
|
|
controller, PERIPH28335_REGISTER_COUNT - 1U, value,
|
|
output, output_size);
|
|
}
|
|
|
|
size_t periph28335_expected_read_response_size(uint16_t count)
|
|
{
|
|
return (count >= 1U && count <= PERIPH28335_REGISTER_COUNT)
|
|
? (size_t)count * 2U + 5U : 0U;
|
|
}
|
|
|
|
int periph28335_decode_read_response(
|
|
const uint8_t *data, size_t size, uint8_t controller, uint16_t count,
|
|
uint16_t *output, size_t output_count)
|
|
{
|
|
size_t expected = periph28335_expected_read_response_size(count);
|
|
uint16_t crc;
|
|
size_t index;
|
|
if ((data == NULL) || (output == NULL)) return PERIPH28335_ERROR_ARGUMENT;
|
|
if (expected == 0U) return PERIPH28335_ERROR_RANGE;
|
|
if (size != expected) return PERIPH28335_ERROR_LENGTH;
|
|
if (output_count < count) return PERIPH28335_ERROR_CAPACITY;
|
|
crc = periph28335_crc16_modbus(data, size - 2U);
|
|
if ((data[size - 2U] != (uint8_t)crc) ||
|
|
(data[size - 1U] != (uint8_t)(crc >> 8))) {
|
|
return PERIPH28335_ERROR_CRC;
|
|
}
|
|
if ((data[0] != controller) || (data[1] != 3U) ||
|
|
(data[2] != (uint8_t)(count * 2U))) {
|
|
return PERIPH28335_ERROR_HEADER;
|
|
}
|
|
for (index = 0U; index < count; ++index) {
|
|
output[index] = get_be16(&data[3U + index * 2U]);
|
|
}
|
|
return PERIPH28335_OK;
|
|
}
|
|
|
|
int periph28335_validate_write_response(
|
|
const uint8_t *response, size_t response_size,
|
|
const uint8_t *request, size_t request_size)
|
|
{
|
|
uint16_t crc;
|
|
if ((response == NULL) || (request == NULL)) return PERIPH28335_ERROR_ARGUMENT;
|
|
if ((response_size != PERIPH28335_REQUEST_SIZE) ||
|
|
(request_size != PERIPH28335_REQUEST_SIZE)) return PERIPH28335_ERROR_LENGTH;
|
|
crc = periph28335_crc16_modbus(response, response_size - 2U);
|
|
if ((response[response_size - 2U] != (uint8_t)crc) ||
|
|
(response[response_size - 1U] != (uint8_t)(crc >> 8))) {
|
|
return PERIPH28335_ERROR_CRC;
|
|
}
|
|
return memcmp(response, request, PERIPH28335_REQUEST_SIZE) == 0
|
|
? PERIPH28335_OK : PERIPH28335_ERROR_HEADER;
|
|
}
|
|
|
|
size_t periph28335_project_count(void)
|
|
{
|
|
return sizeof projects / sizeof projects[0];
|
|
}
|
|
|
|
const char *periph28335_project_name(size_t project_index)
|
|
{
|
|
return project_index < periph28335_project_count()
|
|
? projects[project_index].project : NULL;
|
|
}
|
|
|
|
const char *periph28335_command_name(size_t project_index,
|
|
size_t command_index)
|
|
{
|
|
return project_index < periph28335_project_count() &&
|
|
command_index < PERIPH28335_COMMAND_COUNT
|
|
? projects[project_index].commands[command_index] : NULL;
|
|
}
|