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
Macros | Variables
asc_helpers.h File Reference
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "ascii_serial_com.h"
#include "circular_buffer.h"
Include dependency graph for asc_helpers.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define is_uart_rx_data_waiting(uart_no)   _is_uart_rx_data_waiting(uart_no)
 
#define _is_uart_rx_data_waiting(uart_no)    ((USART_ISR(USART##uart_no) & USART_ISR_RXNE))
 
#define is_uart_ready_to_tx(uart_no)   _is_uart_ready_to_tx(uart_no)
 
#define _is_uart_ready_to_tx(uart_no)    ((USART_ISR(USART##uart_no) & USART_ISR_TXE))
 
#define uart_rx(uart_no, _tmp_byte)   _uart_rx(uart_no, _tmp_byte)
 
#define _uart_rx(uart_no, _tmp_byte)
 
#define uart_rx_blocking(uart_no, _tmp_byte)
 
#define uart_tx(uart_no, data)   _uart_tx(uart_no, data)
 
#define _uart_tx(uart_no, data)   usart_send(USART##uart_no, data)
 
#define uart_tx_blocking(uart_no, _tmp_byte)
 
#define _ATOMIC   CM_ATOMIC_BLOCK()
 
#define uart_rx_to_circ_buf(uart_no, circ_buf_ptr)
 Receive a byte from UART into circular buffer.
 
#define uart_tx_from_circ_buf(uart_no, circ_buf_ptr)
 Transmit a byte from the circular buffer.
 
#define _extraInputBuffer_size_   64
 
#define DECLARE_ASC_DEVICE_W_REGISTER_POINTERS()
 Declarations for ascii_serial_com_device and ascii_serial_com_register_pointers.
 
#define SETUP_ASC_DEVICE_W_REGISTER_POINTERS(register_map, register_write_masks, nRegs)
 Setup for ascii_serial_com_device and ascii_serial_com_register_pointers.
 
#define HANDLE_ASC_COMM_IN_POLLING_LOOP(uart_no)
 Polling for ascii_serial_com_device and ascii_serial_com_register_pointers.
 
#define READY_TO_STREAM_ASC_DEVICE_W_REGISTER_POINTERS
 Check if you should stream a message to the host.
 
#define STREAM_TO_HOST_ASC_DEVICE_W_REGISTER_POINTERS(data, data_size)
 Stream data to the host.
 

Variables

uint8_t ASC_HELPERS_BYTE
 

Detailed Description

Helper macros to help with UARTs, etc.

Definition in file asc_helpers.h.

Macro Definition Documentation

◆ _ATOMIC

#define _ATOMIC   CM_ATOMIC_BLOCK()

Cross-platform atomic block

Definition at line 164 of file asc_helpers.h.

◆ _extraInputBuffer_size_

#define _extraInputBuffer_size_   64

Definition at line 206 of file asc_helpers.h.

◆ _is_uart_ready_to_tx

#define _is_uart_ready_to_tx ( uart_no)     ((USART_ISR(USART##uart_no) & USART_ISR_TXE))

Second level of macro makes sure parameters are expanded before subbing into string concatenation

Definition at line 60 of file asc_helpers.h.

◆ _is_uart_rx_data_waiting

#define _is_uart_rx_data_waiting ( uart_no)     ((USART_ISR(USART##uart_no) & USART_ISR_RXNE))

Second level of macro makes sure parameters are expanded before subbing into string concatenation

Definition at line 35 of file asc_helpers.h.

◆ _uart_rx

#define _uart_rx ( uart_no,
_tmp_byte )
Value:
do { \
_tmp_byte = usart_recv(USART##uart_no); \
} while (0)

Second level of macro makes sure parameters are expanded before subbing into string concatenation

Definition at line 84 of file asc_helpers.h.

◆ _uart_tx

#define _uart_tx ( uart_no,
data )   usart_send(USART##uart_no, data)

Second level of macro makes sure parameters are expanded before subbing into string concatenation

Definition at line 130 of file asc_helpers.h.

◆ DECLARE_ASC_DEVICE_W_REGISTER_POINTERS

#define DECLARE_ASC_DEVICE_W_REGISTER_POINTERS ( )
Value:
void _handle_nf_messages(ascii_serial_com *asc, char ascVersion, \
char appVersion, char command, char *data, \
size_t dataLen, void *state_vp); \
bool streaming_is_on = false; \
circular_buffer_uint8 extraInputBuffer; \
\
uint8_t _tmp_byte; \
uint8_t _extraInputBuffer_raw[_extraInputBuffer_size_]; \
ascii_serial_com_register_pointers _reg_pointers_state; \
ascii_serial_com_device_config _ascd_config = { \
.state_rw = &_reg_pointers_state, \
.func_nf = _handle_nf_messages, \
.state_nf = &streaming_is_on}; \
void _handle_nf_messages(__attribute__((unused)) ascii_serial_com *asc, \
__attribute__((unused)) char ascVersion, \
__attribute__((unused)) char appVersion, \
char command, __attribute__((unused)) char *data, \
__attribute__((unused)) size_t dataLen, \
void *state_vp) { \
bool *state = (bool *)state_vp; \
if (command == 'n') { \
*state = true; \
} else if (command == 'f') { \
*state = false; \
} \
}
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.
ASCII Serial Com Device Config Struct.
void(* func_rw)(ascii_serial_com *, char, char, char, char *data, size_t, void *)
ASCII Serial Com Device State struct.
ASCII Serial Com Interface State struct.
ASCII Serial Com Register Pointers State struct.
circular buffer struct

Declarations for ascii_serial_com_device and ascii_serial_com_register_pointers.

Declarations that should be outside of (and before) main() to ease use of ascii_serial_com_register_pointers with ascii_serial_com_device.

The only "public" variables are:

 bool streaming_is_on;
 circular_buffer_uint8 extraInputBuffer;

Notes

Don't follow this with a semicolon

Parameters

None

Definition at line 228 of file asc_helpers.h.

◆ HANDLE_ASC_COMM_IN_POLLING_LOOP

#define HANDLE_ASC_COMM_IN_POLLING_LOOP ( uart_no)
Value:
do { \
uart_tx_from_circ_buf(uart_no, \
if (!circular_buffer_is_empty_uint8(&extraInputBuffer)) { \
_ATOMIC { \
_tmp_byte = circular_buffer_pop_front_uint8(&extraInputBuffer); \
} \
circular_buffer_push_back_uint8( \
} \
ascii_serial_com_device_receive(&_ascd); \
} while (0)
#define _ATOMIC
circular_buffer_uint8 * ascii_serial_com_device_get_output_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get output buffer.
circular_buffer_uint8 * ascii_serial_com_device_get_input_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get input buffer.
uint8_t circular_buffer_pop_front_uint8(circular_buffer_uint8 *circ_buf)
circular buffer pop front
bool circular_buffer_is_empty_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get if empty

Polling for ascii_serial_com_device and ascii_serial_com_register_pointers.

Within the polling loop, handles transmitting bytes, processing received bytes, and handling received messages. This is part of a group of macros to ease use of ascii_serial_com_register_pointers with ascii_serial_com_device.

Notes

Assumes that something else receives bytes and puts them in extraInputBuffer

Make sure this is inside a Try/Catch block

Follow this with a semicolon

Parameters

uart: the address of the USART used for transmitting bytes. For STM32: USART1, USART2, .... For AVR: UDR0, ....

Definition at line 313 of file asc_helpers.h.

◆ is_uart_ready_to_tx

#define is_uart_ready_to_tx ( uart_no)    _is_uart_ready_to_tx(uart_no)

Helper to check if UART tx buffer is ready for the user to write to it

Use uart_no as 1,2,3, etc.

Definition at line 53 of file asc_helpers.h.

◆ is_uart_rx_data_waiting

#define is_uart_rx_data_waiting ( uart_no)    _is_uart_rx_data_waiting(uart_no)

Helper to check if UART has a received a byte and is waiting on the user to read it

Use uart_no as 1,2,3, etc.

Definition at line 28 of file asc_helpers.h.

◆ READY_TO_STREAM_ASC_DEVICE_W_REGISTER_POINTERS

#define READY_TO_STREAM_ASC_DEVICE_W_REGISTER_POINTERS
Value:
(streaming_is_on && \
circular_buffer_get_size_uint8( \

Check if you should stream a message to the host.

This is a boolean value macro that checks if streaming is enabled and the transmit buffer is empty. If true, the user may stream a message to the host now with STREAM_TO_HOST_ASC_DEVICE_W_REGISTER_POINTERS

This is part of a group of macros to ease use of ascii_serial_com_register_pointers with ascii_serial_com_device.

Notes

Make sure this is inside a Try/Catch block

Definition at line 341 of file asc_helpers.h.

◆ SETUP_ASC_DEVICE_W_REGISTER_POINTERS

#define SETUP_ASC_DEVICE_W_REGISTER_POINTERS ( register_map,
register_write_masks,
nRegs )
Value:
do { \
ascii_serial_com_register_pointers_init( \
&_reg_pointers_state, register_map, register_write_masks, nRegs); \
ascii_serial_com_device_init(&_ascd, &_ascd_config); \
\
circular_buffer_init_uint8(&extraInputBuffer, _extraInputBuffer_size_, \
_extraInputBuffer_raw); \
} while (0)
REGTYPE register_write_masks[nRegs]
Write masks for register_map.
volatile REGTYPE * register_map[nRegs]
Register Map.

Setup for ascii_serial_com_device and ascii_serial_com_register_pointers.

Setup that should be inside main() before the polling loop. This is part of a group of macros to ease use of ascii_serial_com_register_pointers with ascii_serial_com_device.

Notes

Make sure this is inside a Try/Catch block

Follow this with a semicolon

Parameters

register_map: the register map, type: REGTYPE * array

register_write_masks: the register write masks, type: REGTYPE array

nRegs: the length of the two arrays above

Definition at line 280 of file asc_helpers.h.

◆ STREAM_TO_HOST_ASC_DEVICE_W_REGISTER_POINTERS

#define STREAM_TO_HOST_ASC_DEVICE_W_REGISTER_POINTERS ( data,
data_size )
Value:
data, data_size)
void ascii_serial_com_device_put_s_message_in_output_buffer(ascii_serial_com_device *ascd, char ascVersion, char appVersion, char *data, size_t dataLen)
ASCII Serial Com Device put a 's' message in output buffer.

Stream data to the host.

This macro puts a message in the output buffer

This is part of a group of macros to ease use of ascii_serial_com_register_pointers with ascii_serial_com_device.

Notes

Make sure this is inside a Try/Catch block

Parameters

data: a pointer to data or array of data to be sent

data_size: a uint describing the data size in bytes

Definition at line 364 of file asc_helpers.h.

◆ uart_rx

#define uart_rx ( uart_no,
_tmp_byte )   _uart_rx(uart_no, _tmp_byte)

Reads from the UART rx buffer, non-blocking, without checking if data is there

Definition at line 77 of file asc_helpers.h.

◆ uart_rx_blocking

#define uart_rx_blocking ( uart_no,
_tmp_byte )
Value:
do { \
while (1) { \
if (is_uart_rx_data_waiting(uart_no)) { \
uart_rx(uart_no, _tmp_byte); \
break; \
} \
} \
} while (0)
#define is_uart_rx_data_waiting(uart_no)
Definition asc_helpers.h:28

Reads from the UART rx buffer, blocking until data is there

Definition at line 107 of file asc_helpers.h.

◆ uart_rx_to_circ_buf

#define uart_rx_to_circ_buf ( uart_no,
circ_buf_ptr )
Value:
do { \
if (is_uart_rx_data_waiting(uart_no)) { \
uart_rx(uart_no, ASC_HELPERS_BYTE); \
circular_buffer_push_back_uint8(circ_buf_ptr, ASC_HELPERS_BYTE); \
} \
} while (0)

Receive a byte from UART into circular buffer.

Checks if uart has a received byte waiting, and if so, pushes it onto the back of the circular buffer.

Definition at line 179 of file asc_helpers.h.

◆ uart_tx

#define uart_tx ( uart_no,
data )   _uart_tx(uart_no, data)

Writes to the UART tx buffer, non-blocking, without checking if it's ready to receive data

Definition at line 123 of file asc_helpers.h.

◆ uart_tx_blocking

#define uart_tx_blocking ( uart_no,
_tmp_byte )
Value:
do { \
while (1) { \
if (is_uart_ready_to_tx(uart_no)) { \
uart_tx(uart_no, _tmp_byte); \
break; \
} \
} \
} while (0)
#define is_uart_ready_to_tx(uart_no)
Definition asc_helpers.h:53

Writes to the UART tx buffer, blocking until the buffer is ready for writing

Definition at line 148 of file asc_helpers.h.

◆ uart_tx_from_circ_buf

#define uart_tx_from_circ_buf ( uart_no,
circ_buf_ptr )
Value:
do { \
if (is_uart_ready_to_tx(uart_no) && \
!circular_buffer_is_empty_uint8(circ_buf_ptr)) { \
uart_tx(uart_no, circular_buffer_pop_front_uint8(circ_buf_ptr)); \
} \
} while (0)

Transmit a byte from the circular buffer.

Checks if uart can accept a byte and the circular buffer isn't empty, and if so, pops a byte off of the circular buffer and transmits it.

Definition at line 194 of file asc_helpers.h.

Variable Documentation

◆ ASC_HELPERS_BYTE

uint8_t ASC_HELPERS_BYTE

Definition at line 18 of file asc_helpers.h.