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_pointers.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_pointers *register_pointers_state,
20 volatile REGTYPE **pointers, REGTYPE *write_masks, uint16_t n_regs) {
21 register_pointers_state->pointers = pointers;
22 register_pointers_state->write_masks = write_masks;
23 register_pointers_state->n_regs = n_regs;
24}
25
27 ascii_serial_com *asc, char ascVersion, char appVersion, char command,
28 char *data, size_t dataLen, void *register_pointers_state_vp) {
29 if (!register_pointers_state_vp) {
30 ascii_serial_com_put_error_in_output_buffer(asc, ascVersion, appVersion,
31 command, data, dataLen,
32 ASC_ERROR_REG_BLOCK_NULL);
33 return;
34 }
35 const ascii_serial_com_register_pointers *register_pointers_state =
36 (ascii_serial_com_register_pointers *)register_pointers_state_vp;
37 if (command != 'r' && command != 'w') {
38 ascii_serial_com_put_error_in_output_buffer(asc, ascVersion, appVersion,
39 command, data, dataLen,
40 ASC_ERROR_UNEXPECTED_COMMAND);
41 return;
42 }
43 if (dataLen < 4) { // need room for reg num
44 ascii_serial_com_put_error_in_output_buffer(asc, ascVersion, appVersion,
45 command, data, dataLen,
46 ASC_ERROR_DATA_TOO_SHORT);
47 return;
48 }
49 uint16_t reg_num = convert_hex_to_uint16(data);
50 if (reg_num >= register_pointers_state->n_regs) {
51 ascii_serial_com_put_error_in_output_buffer(asc, ascVersion, appVersion,
52 command, data, dataLen,
53 ASC_ERROR_REGNUM_OOB);
54 return;
55 }
56 volatile REGTYPE *pointer = register_pointers_state->pointers[reg_num];
57 if (command == 'r') {
58 REGTYPE reg_val = 0;
59 if (pointer) {
60 reg_val = *pointer;
61 }
62 data[4] = ',';
63 put_val_into_bytes(reg_val, data + 5);
64 dataLen = 5 + REGWIDTHBYTES * 2;
65 } else {
66 if (dataLen < 5 + REGWIDTHBYTES) {
67 ascii_serial_com_put_error_in_output_buffer(asc, ascVersion, appVersion,
68 command, data, dataLen,
69 ASC_ERROR_REGVAL_LEN);
70 return;
71 }
72 if (pointer) {
73 REGTYPE new_reg_val = get_val_from_bytes(data + 5);
74 REGTYPE mask = register_pointers_state->write_masks[reg_num];
75 *pointer = (new_reg_val & mask) | (*pointer & ~mask);
76 }
77 dataLen = 4;
78 }
79 ascii_serial_com_put_message_in_output_buffer(asc, ascVersion, appVersion,
80 command, data, dataLen);
81}
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_put_error_in_output_buffer(ascii_serial_com *asc, char ascVersion, char appVersion, char command, char *data, size_t dataLen, enum asc_exception errorCode)
ASCII Serial Com put error message in out buffer.
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.
ASCII Serial Com Interface State struct.
ASCII Serial Com Register Pointers State struct.