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
stm32f091nucleo64_asc_loopback.c
1#include <libopencm3/cm3/cortex.h>
2#include <libopencm3/cm3/nvic.h>
3#include <libopencm3/stm32/gpio.h>
4#include <libopencm3/stm32/rcc.h>
5#include <libopencm3/stm32/usart.h>
6
7#include "arm/stm_usart.h"
8#include "asc_exception.h"
9#include "ascii_serial_com.h"
10#include "circular_buffer.h"
11
12// USART2 should be connected through USB
13#define ASC_USART USART2
14
15char dataBuffer[MAXDATALEN];
17circular_buffer_uint8 *asc_in_buf;
18circular_buffer_uint8 *asc_out_buf;
19
20#define extraInputBuffer_size 64
21uint8_t extraInputBuffer_raw[extraInputBuffer_size];
22circular_buffer_uint8 extraInputBuffer;
23
24CEXCEPTION_T e;
25char ascVersion, appVersion, command;
26size_t dataLen;
27
28uint16_t nExceptions;
29
30uint8_t tmp_byte = 0;
31
32int main(void) {
33
34 Try {
36 ascii_serial_com_set_ignore_CRC_mismatch(&asc);
37 asc_in_buf = ascii_serial_com_get_input_buffer(&asc);
38 asc_out_buf = ascii_serial_com_get_output_buffer(&asc);
39
40 circular_buffer_init_uint8(&extraInputBuffer, extraInputBuffer_size,
41 extraInputBuffer_raw);
42
43 // USART2 TX: PA2 AF1
44 // USART2 RX: PA3 AF1
45 rcc_periph_clock_enable(RCC_GPIOA);
46 rcc_periph_clock_enable(RCC_USART2);
47 setup_usart(ASC_USART, 9600, GPIOA, GPIO2, GPIO_AF1, GPIOA, GPIO3,
48 GPIO_AF1);
49 nvic_enable_irq(NVIC_USART2_IRQ);
50 usart_enable_rx_interrupt(ASC_USART);
51 usart_enable(ASC_USART);
52 }
53 Catch(e) { return e; }
54
55 while (1) {
56 Try {
57 // Move data from extraInputBuffer to asc_in_buf
58 if (!circular_buffer_is_empty_uint8(&extraInputBuffer)) {
59 CM_ATOMIC_BLOCK() {
60 tmp_byte = circular_buffer_pop_front_uint8(&extraInputBuffer);
61 }
62 circular_buffer_push_back_uint8(asc_in_buf, tmp_byte);
63 }
64
65 // Process in messages and loop them back to out
66 if (!circular_buffer_is_empty_uint8(asc_in_buf)) {
68 &asc, &ascVersion, &appVersion, &command, dataBuffer, &dataLen);
69 if (command != '\0') {
71 &asc, ascVersion, appVersion, command, dataBuffer, dataLen);
72 }
73 }
74
75 // Write data from asc_out_buf to serial
76 if (!circular_buffer_is_empty_uint8(asc_out_buf) &&
77 (USART_ISR(ASC_USART) & USART_ISR_TXE)) {
78 tmp_byte = circular_buffer_pop_front_uint8(asc_out_buf);
79 usart_send(ASC_USART, tmp_byte);
80 }
81 }
82 Catch(e) { nExceptions++; }
83 }
84
85 return 0;
86}
87
88def_usart_isr_push_rx_to_circ_buf(usart2_isr, ASC_USART, &extraInputBuffer)
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_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_get_message_from_input_buffer(ascii_serial_com *asc, char *ascVersion, char *appVersion, char *command, char *data, size_t *dataLen)
ASCII Serial Com pop message from input buffer and unpack.
circular_buffer_uint8 * ascii_serial_com_get_input_buffer(ascii_serial_com *asc)
ASCII Serial Com get input buffer.
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
To be used with the USART peripherals on STM32 microcontrollers.
#define setup_usart(usart, baud, tx_port, tx_pin, tx_af, rx_port, rx_pin, rx_af)
Setup a USART.
Definition stm_usart.h:52
#define def_usart_isr_push_rx_to_circ_buf(isr_name, usart, circular_buffer)
Define the ISR for a USART to push rx bytes to a circular buffer.
Definition stm_usart.h:95
ASCII Serial Com Interface State struct.
circular buffer struct