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
ascii_serial_com_register_block.c
Go to the documentation of this file.
2#include "asc_exception.h"
3#include <assert.h>
4
5/** \file */
6
7#if REGWIDTHBYTES == 1
8#define put_val_into_bytes(x, b) convert_uint8_to_hex(x, (b), true)
9#define get_val_from_bytes(b) convert_hex_to_uint8((b))
10#elif REGWIDTHBYTES == 2
11#define put_val_into_bytes(x, b) convert_uint16_to_hex(x, (b), true)
12#define get_val_from_bytes(b) convert_hex_to_uint16((b))
13#elif REGWIDTHBYTES == 4
14#define put_val_into_bytes(x, b) convert_uint32_to_hex(x, (b), true)
15#define get_val_from_bytes(b) convert_hex_to_uint32((b))
16#endif
17
19 ascii_serial_com_register_block *register_block_state, REGTYPE *block,
20 uint16_t n_regs) {
21 register_block_state->block = block;
22 register_block_state->n_regs = n_regs;
23}
24
26 ascii_serial_com *asc, char ascVersion, char appVersion, char command,
27 char *data, size_t dataLen, void *register_block_state_vp) {
28 if (!register_block_state_vp) {
29 Throw(ASC_ERROR_REG_BLOCK_NULL);
30 }
31 const ascii_serial_com_register_block *register_block_state =
32 (ascii_serial_com_register_block *)register_block_state_vp;
33 if (command != 'r' && command != 'w') {
34 Throw(ASC_ERROR_UNEXPECTED_COMMAND);
35 }
36 if (dataLen < 4) { // need room for reg num
37 Throw(ASC_ERROR_DATA_TOO_SHORT);
38 }
39 uint16_t reg_num = convert_hex_to_uint16(data);
40 if (reg_num >= register_block_state->n_regs) {
41 Throw(ASC_ERROR_REGNUM_OOB);
42 }
43 if (command == 'r') {
44 REGTYPE reg_val = register_block_state->block[reg_num];
45 data[4] = ',';
46 put_val_into_bytes(reg_val, data + 5);
47 dataLen = 5 + REGWIDTHBYTES * 2;
48 } else {
49 if (dataLen < 5 + REGWIDTHBYTES) {
50 Throw(ASC_ERROR_REGVAL_LEN);
51 }
52 REGTYPE new_reg_val = get_val_from_bytes(data + 5);
53 register_block_state->block[reg_num] = new_reg_val;
54 dataLen = 4;
55 }
56 ascii_serial_com_put_message_in_output_buffer(asc, ascVersion, appVersion,
57 command, data, dataLen);
58}
uint16_t convert_hex_to_uint16(const char *instr)
convert hex string to uint16
void ascii_serial_com_put_message_in_output_buffer(ascii_serial_com *asc, char ascVersion, char appVersion, char command, const char *data, size_t dataLen)
ASCII Serial Com Pack and put message in output buffer.
void ascii_serial_com_register_block_handle_message(ascii_serial_com *asc, char ascVersion, char appVersion, char command, char *data, size_t dataLen, void *register_block_state_vp)
ASCII Serial Com Register Block handle message.
void ascii_serial_com_register_block_init(ascii_serial_com_register_block *register_block_state, REGTYPE *block, uint16_t n_regs)
ASCII Serial Com Register Block init.
ASCII Serial Com Register Block.
ASCII Serial Com Interface State struct.
ASCII Serial Com Register Block State struct.