ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
test_ascii_serial_com_register_pointers.c
1#include "asc_exception.h"
3#include "externals/unity.h"
4#include <inttypes.h>
5#include <stdio.h>
6#include <string.h>
7
8CEXCEPTION_T e1;
9// CEXCEPTION_T e2;
10
11#if REGWIDTHBYTES == 1
12#define zerohex "00"
13#define onehex "01"
14#define maxhex "FF"
15#define regFmtStr "%02zX"
16#elif REGWIDTHBYTES == 2
17#define zerohex "0000"
18#define onehex "0001"
19#define maxhex "FFFF"
20#define regFmtStr "%04zX"
21#elif REGWIDTHBYTES == 4
22#define zerohex "00000000"
23#define onehex "00000001"
24#define maxhex "FFFFFFFF"
25#define regFmtStr "%08zX"
26#endif
27
28#define nRegs 5
29REGTYPE r1, r2, r3, r4, r5;
30volatile REGTYPE *regPtrs[nRegs] = {&r1, &r2, &r3, &r4, &r5};
31REGTYPE masks[nRegs] = {
32 0, 0, 0, 0, 0,
33};
34
35void setUp(void) {
36 // set stuff up here
37 for (size_t i = 0; i < nRegs; i++) {
38 *regPtrs[i] = 0;
39 masks[i] = 0;
40 }
41}
42
43void tearDown(void) {
44 // clean stuff up here
45}
46
47void test_ascii_serial_com_register_pointers_write(void) {
48 for (size_t iReg = 0; iReg < nRegs; iReg++) {
49 masks[iReg] = 0xFF;
50 }
51 Try {
55
57 ascii_serial_com_register_pointers_init(&ascrb, regPtrs, masks, nRegs);
58
59 char ascVersion, appVersion, command, data[MAXDATALEN + 1];
60 size_t dataLen;
61
62 char validationStr[MAXMESSAGELEN + 1];
63 for (size_t iReg = 0; iReg < nRegs; iReg++) {
64 for (size_t iVal = 0; iVal < 0x100; iVal++) {
65 // printf("iReg: %02zX iVal: %02zX\n",iReg,iVal);
66 ascVersion = '0';
67 appVersion = '0';
68 command = 'w';
69 sprintf(data, "%04zX," regFmtStr, iReg, iVal);
70 dataLen = 5 + REGWIDTHBYTES * 2;
71 sprintf(validationStr, ">%c%c%c%04zX.", ascVersion, appVersion, command,
72 iReg);
74 &asc, ascVersion, appVersion, command, data, dataLen, &ascrb);
75 TEST_ASSERT_EQUAL_size_t(10 + 4,
77 TEST_ASSERT_EQUAL_CHAR_ARRAY(validationStr, out_buf->buffer, 5 + 4);
79
80 TEST_ASSERT_EQUAL_MESSAGE(iVal, *regPtrs[iReg],
81 "Register didn't get set to correct value!");
82
83 command = 'r';
84 sprintf(data, "%04zX", iReg);
85 dataLen = 4;
86 sprintf(validationStr, ">%c%c%c%04zX," regFmtStr ".", ascVersion,
87 appVersion, command, iReg, iVal);
89 &asc, ascVersion, appVersion, command, data, dataLen, &ascrb);
90 TEST_ASSERT_EQUAL_size_t(15 + REGWIDTHBYTES * 2,
92 TEST_ASSERT_EQUAL_CHAR_ARRAY(validationStr, out_buf->buffer,
93 10 + REGWIDTHBYTES * 2);
95 }
96 }
97 }
98 Catch(e1) {
99 printf("Uncaught exception: %u\n", e1);
100 TEST_FAIL_MESSAGE("Uncaught exception!");
101 }
102}
103
104void test_ascii_serial_com_register_pointers_write_masked(void) {
105 Try {
109
111 ascii_serial_com_register_pointers_init(&ascrb, regPtrs, masks, nRegs);
112
113 char ascVersion, appVersion, command, data[MAXDATALEN + 1];
114 size_t dataLen;
115
116 char validationStr[MAXMESSAGELEN + 1];
117 for (size_t iReg = 0; iReg < nRegs; iReg++) {
118 masks[iReg] = 0x35;
119 }
120 for (size_t iReg = 0; iReg < nRegs; iReg++) {
121 for (size_t iVal = 0; iVal < 0x100; iVal++) {
122 // printf("iReg: %02zX iVal: %02zX\n",iReg,iVal);
123 ascVersion = '0';
124 appVersion = '0';
125 command = 'w';
126 sprintf(data, "%04zX," regFmtStr, iReg, iVal);
127 dataLen = 5 + REGWIDTHBYTES * 2;
128 sprintf(validationStr, ">%c%c%c%04zX.", ascVersion, appVersion, command,
129 iReg);
131 &asc, ascVersion, appVersion, command, data, dataLen, &ascrb);
132 TEST_ASSERT_EQUAL_size_t(10 + 4,
134 TEST_ASSERT_EQUAL_CHAR_ARRAY(validationStr, out_buf->buffer, 5 + 4);
136
137 TEST_ASSERT_EQUAL_MESSAGE(iVal & 0x35, *regPtrs[iReg],
138 "Register didn't get set to correct value!");
139
140 command = 'r';
141 sprintf(data, "%04zX", iReg);
142 dataLen = 4;
143 sprintf(validationStr, ">%c%c%c%04zX," regFmtStr ".", ascVersion,
144 appVersion, command, iReg, iVal & 0x35);
146 &asc, ascVersion, appVersion, command, data, dataLen, &ascrb);
147 TEST_ASSERT_EQUAL_size_t(15 + REGWIDTHBYTES * 2,
149 TEST_ASSERT_EQUAL_CHAR_ARRAY(validationStr, out_buf->buffer,
150 10 + REGWIDTHBYTES * 2);
152 }
153 }
154 for (size_t iReg = 0; iReg < nRegs; iReg++) {
155 *regPtrs[iReg] = 0xFF;
156 }
157 for (size_t iReg = 0; iReg < nRegs; iReg++) {
158 for (size_t iVal = 0; iVal < 0x100; iVal++) {
159 // printf("iReg: %02zX iVal: %02zX\n",iReg,iVal);
160 ascVersion = '0';
161 appVersion = '0';
162 command = 'w';
163 sprintf(data, "%04zX," regFmtStr, iReg, iVal);
164 dataLen = 5 + REGWIDTHBYTES * 2;
165 sprintf(validationStr, ">%c%c%c%04zX.", ascVersion, appVersion, command,
166 iReg);
168 &asc, ascVersion, appVersion, command, data, dataLen, &ascrb);
169 TEST_ASSERT_EQUAL_size_t(10 + 4,
171 TEST_ASSERT_EQUAL_CHAR_ARRAY(validationStr, out_buf->buffer, 5 + 4);
173
174 TEST_ASSERT_EQUAL_MESSAGE((iVal & 0x35) | (0xFF & ~0x35),
175 *regPtrs[iReg],
176 "Register didn't get set to correct value!");
177
178 command = 'r';
179 sprintf(data, "%04zX", iReg);
180 dataLen = 4;
181 sprintf(validationStr, ">%c%c%c%04zX," regFmtStr ".", ascVersion,
182 appVersion, command, iReg, (iVal & 0x35) | (0xFF & ~0x35));
184 &asc, ascVersion, appVersion, command, data, dataLen, &ascrb);
185 TEST_ASSERT_EQUAL_size_t(15 + REGWIDTHBYTES * 2,
187 TEST_ASSERT_EQUAL_CHAR_ARRAY(validationStr, out_buf->buffer,
188 10 + REGWIDTHBYTES * 2);
190 }
191 }
192 }
193 Catch(e1) {
194 printf("Uncaught exception: %u\n", e1);
195 TEST_FAIL_MESSAGE("Uncaught exception!");
196 }
197}
198
199int main(void) {
200 UNITY_BEGIN();
201 RUN_TEST(test_ascii_serial_com_register_pointers_write);
202 RUN_TEST(test_ascii_serial_com_register_pointers_write_masked);
203 return UNITY_END();
204}
circular_buffer_uint8 * ascii_serial_com_get_output_buffer(ascii_serial_com *asc)
ASCII Serial Com get output buffer.
void ascii_serial_com_init(ascii_serial_com *asc)
ASCII Serial Com Interface init method.
void ascii_serial_com_register_pointers_handle_message(ascii_serial_com *asc, char ascVersion, char appVersion, char command, char *data, size_t dataLen, void *register_pointers_state_vp)
ASCII Serial Com Register Pointers handle message.
void ascii_serial_com_register_pointers_init(ascii_serial_com_register_pointers *register_pointers_state, volatile REGTYPE **pointers, REGTYPE *write_masks, uint16_t n_regs)
ASCII Serial Com Register Pointers init.
ASCII Serial Com Register Pointers.
size_t circular_buffer_get_size_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get number of elements
void circular_buffer_clear_uint8(circular_buffer_uint8 *circ_buf)
circular buffer clear
ASCII Serial Com Interface State struct.
ASCII Serial Com Register Pointers State struct.
circular buffer struct