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
arduino_uno_test_device.c
1#include "asc_exception.h"
2#include "asc_helpers.h"
3#include "ascii_serial_com.h"
6#include "avr/avr_uart.h"
7#include "circular_buffer.h"
8
9#include <avr/interrupt.h>
10#include <avr/io.h>
11#include <util/atomic.h>
12
13#define F_CPU 16000000L
14#define BAUD 9600
15#define MYUBRR (F_CPU / 16 / BAUD - 1)
16#define UART_NO 0
17
18char dataBuffer[MAXDATALEN];
19#define nRegs 3
20volatile REGTYPE *regPtrs[nRegs] = {&PORTB, &PORTC, &PORTD};
21
22REGTYPE masks[nRegs] = {
23 1 << 5,
24 0,
25 0,
26};
27
28typedef struct stream_state_struct {
29 uint8_t on;
31void handle_nf_messages(ascii_serial_com *asc, char ascVersion, char appVersion,
32 char command, char *data, size_t dataLen,
33 void *state_vp);
34
35on_off_stream_state stream_state;
36
38ascii_serial_com_register_pointers reg_pointers_state;
41 .state_rw = &reg_pointers_state,
42 .func_nf = handle_nf_messages,
43 .state_nf = &stream_state};
44circular_buffer_uint8 *asc_in_buf;
45circular_buffer_uint8 *asc_out_buf;
46
47#define extraInputBuffer_size 64
48uint8_t extraInputBuffer_raw[extraInputBuffer_size];
49circular_buffer_uint8 extraInputBuffer;
50
51CEXCEPTION_T e;
52
53uint16_t nExceptions;
54uint8_t counter;
55char counter_buffer[2];
56
57int main(void) {
58
59 DDRB |= 1 << 5;
60
61 nExceptions = 0;
62 stream_state.on = 0;
63 counter = 0;
64
65 Try {
66 ascii_serial_com_register_pointers_init(&reg_pointers_state, regPtrs, masks,
67 nRegs);
68 ascii_serial_com_device_init(&ascd, &ascd_config);
71
72 circular_buffer_init_uint8(&extraInputBuffer, extraInputBuffer_size,
73 extraInputBuffer_raw);
74 }
75 Catch(e) { return e; }
76
77 UART_Init(UART_NO, MYUBRR, 1);
78
79 sei();
80
81 while (true) {
82 Try {
83 // if (USART0_can_read_Rx_data) {
84 // circular_buffer_push_back_uint8(asc_in_buf, UDR0);
85 //}
86
87 if (!circular_buffer_is_empty_uint8(&extraInputBuffer)) {
88 uint8_t byte;
89 ATOMIC_BLOCK(ATOMIC_FORCEON) {
90 byte = circular_buffer_pop_front_uint8(&extraInputBuffer);
91 }
92 circular_buffer_push_back_uint8(asc_in_buf, byte);
93 }
94
96
97 if (stream_state.on && circular_buffer_get_size_uint8(asc_out_buf) == 0) {
98 convert_uint8_to_hex(counter, counter_buffer, true);
100 &ascd, '0', '0', counter_buffer, 2);
101 counter++;
102 }
103 uart_tx_from_circ_buf(UART_NO, asc_out_buf);
104 }
105 Catch(e) { nExceptions++; }
106 }
107
108 return 0;
109}
110
111ISR(USART_RX_vect) { uart_rx_to_circ_buf(UART_NO, &extraInputBuffer); }
112
113void handle_nf_messages(__attribute__((unused)) ascii_serial_com *asc,
114 __attribute__((unused)) char ascVersion,
115 __attribute__((unused)) char appVersion, char command,
116 __attribute__((unused)) char *data,
117 __attribute__((unused)) size_t dataLen,
118 void *state_vp) {
119 on_off_stream_state *state = (on_off_stream_state *)state_vp;
120 if (command == 'n') {
121 state->on = 1;
122 } else if (command == 'f') {
123 state->on = 0;
124 }
125}
#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.
void convert_uint8_to_hex(uint8_t num, char *outstr, bool caps)
convert uint8 to hex string
circular_buffer_uint8 * ascii_serial_com_device_get_output_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get output buffer.
void ascii_serial_com_device_receive(ascii_serial_com_device *ascd)
ASCII Serial Com Device receive messages.
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.
circular_buffer_uint8 * ascii_serial_com_device_get_input_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get input buffer.
void ascii_serial_com_device_init(ascii_serial_com_device *ascd, ascii_serial_com_device_config *config)
ASCII Serial Com Device init.
ASCII Serial Com Device.
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.
#define UART_Init(uart_no, ubrr, rxIntEnable)
Initialize USART.
Definition avr_uart.h:20
size_t circular_buffer_get_size_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get number of elements
void circular_buffer_init_uint8(circular_buffer_uint8 *circ_buf, const size_t capacity, uint8_t *buffer)
circular buffer init method
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
void circular_buffer_push_back_uint8(circular_buffer_uint8 *circ_buf, const uint8_t element)
circular buffer push back
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