Files
templates/c/set-protocol/tests/test_tms2812.c

76 lines
3.6 KiB
C

/*
* Проверки tms2812: фиксируют ожидаемое поведение на воспроизводимых входных данных.
* Сценарии для навигации по файлу:
* test_terminal
* Тестовые значения и подмены окружения относятся к проверке контракта; они не являются
* настройками рабочего устройства.
*/
#include <stdio.h>
#include <string.h>
#include "tms2812.h"
static int test_terminal(void)
{
uint8_t output[20], digits[12] = {0x81, 2, 4, 8, 9, 0x20, 0x10, 0x20, 0x40, 0x80, 3, 4};
const uint8_t ping[] = {0x0a, 0x38, 0, 1, 0, 0, 0xf0, 0xb4};
uint8_t reply[] = {0x0a, 0x38, 0x65, 0xa5, 0, 0, 0xaf, 0x9b, 0, 0};
const uint8_t test_prefix[] = {7, 66, 25, 0, 18, 0};
size_t size;
if (tms2812_build_memory_request(10, 0, 0, 0, output, sizeof output) != 8U ||
memcmp(output, ping, sizeof ping) != 0) return 10;
if (tms2812_build_memory_request(10, TMS2812_CMD_POKE, 0x12345678UL, 0xABCD,
output, sizeof output) != 12U || output[2] != 0x78 || output[5] != 0x12 ||
output[6] != 0xCD || output[7] != 0xAB || output[8] != 0 || output[9] != 0 ||
tms2812_crc16(output, 12U) != 0U) return 11;
if (tms2812_build_memory_request(10, 99, 0, 0, output, sizeof output) != 0U ||
tms2812_build_memory_request(10, 56, 0, 0, output, 7) != 0U) return 12;
if (tms2812_build_test_request(7, 25, 18, digits, 12, output, sizeof output) != 20U ||
memcmp(output, test_prefix, sizeof test_prefix) || memcmp(output + 6, digits, 12) ||
tms2812_crc16(output, 20) != 0U) return 13;
if (tms2812_build_test_request(7, 25, 18, digits, 11, output, sizeof output) != 0U ||
tms2812_build_fixed_request(7, 65, NULL, 1, output, sizeof output) != 0U) return 14;
for (size = 8; size <= 10; ++size)
if (tms2812_validate_response(reply, size, 10, 56, 10, 1) != TMS2812_OK) return 15;
if (tms2812_validate_response(reply, 7, 10, 56, 10, 1) != TMS2812_ERROR_LENGTH ||
tms2812_validate_response(reply, 8, 10, 56, 10, 0) != TMS2812_ERROR_LENGTH ||
tms2812_validate_response(reply, 10, 11, 56, 10, 1) != TMS2812_ERROR_HEADER) return 16;
reply[2] ^= 1;
if (tms2812_validate_response(reply, 10, 10, 56, 10, 1) != TMS2812_ERROR_CRC) return 17;
return 0;
}
int main(void)
{
int terminal_status = test_terminal();
if (terminal_status != 0) return terminal_status;
uint8_t request[TMS2812_UPLOAD_REQUEST_SIZE];
static const uint8_t prefix[] = {
0x05U, 0x34U, 0x78U, 0x56U, 0x34U, 0x12U,
0x00U, 0x10U, 0x00U, 0x00U
};
uint8_t reply[4U + TMS2812_UPLOAD_RESPONSE_OVERHEAD];
uint8_t decoded[4];
uint16_t crc;
if (tms2812_build_upload_request(5U, 0x12345678UL, 0x1000U,
request, sizeof request) != sizeof request ||
memcmp(request, prefix, sizeof prefix) != 0) return 1;
reply[0] = 5U;
reply[1] = TMS2812_CMD_UPLOAD;
reply[2] = 0x10U; reply[3] = 0x20U; reply[4] = 0x30U; reply[5] = 0x40U;
crc = tms2812_crc16(reply, 6U);
reply[6] = (uint8_t)crc;
reply[7] = (uint8_t)(crc >> 8);
memset(&reply[8], 0, 4U);
if (tms2812_decode_upload_response(reply, sizeof reply, 5U, 4U,
decoded, sizeof decoded) != TMS2812_OK ||
memcmp(decoded, &reply[2], sizeof decoded) != 0) return 2;
reply[6] ^= 1U;
if (tms2812_validate_upload_response(reply, sizeof reply, 5U, 4U)
!= TMS2812_ERROR_CRC) return 3;
puts("PM67/TMS320F2812 upload protocol tests passed");
return 0;
}