ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
uart1_timer2_rx.c
Go to the documentation of this file.
1// Copyright 2021 IOsetting <iosetting(at)outlook.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "fw_hal.h"
16
17#define RING_BUFFER_SIZE 0x20
18
20static volatile uint16_t from, to;
21
22void ring_buffer_reset(void);
27
28
30{
31 uint8_t c;
32
33 if (RI)
34 {
36 c = SBUF;
37 if (c == '\r' || c == '\n')
38 {
39 c = '\0';
41 }
42 else if (c == 0x7F)
43 {
45 }
46 else
47 {
49 }
50 }
51}
52
53void main(void)
54{
55 uint8_t msg[RING_BUFFER_SIZE], size;
56
58 // UART1, baud 115200, baud source Timer2, 1T mode, interrupt on
61 // Enable UART1 interrupt
64 // Initialize ring buffer
66
67 while(1)
68 {
69 size = ring_buffer_read((uint8_t *)msg);
70 if (size > 0)
71 {
73 UART1_TxString(msg);
74 UART1_TxString("\r\n");
75 }
76 SYS_Delay(500);
77 }
78}
79
85{
86 from = 0;
87 to = 0;
88}
89
91{
92 ring_buffer[to++] = c;
93 if (to >= RING_BUFFER_SIZE)
94 {
95 to = 0;
96 }
97 if (to == from)
98 {
99 from++;
100 if (from >= RING_BUFFER_SIZE)
101 {
102 from = 0;
103 }
104 }
105}
106
108{
109 if (to != from)
110 {
111 to = (to > 0)? to - 1 : RING_BUFFER_SIZE - 1;
112 *c = ring_buffer[to];
113 return 1;
114 }
115 else
116 {
117 return 0;
118 }
119}
120
122{
123 if (to >= from)
124 {
125 return to - from;
126 }
127 else
128 {
129 return RING_BUFFER_SIZE - from + to;
130 }
131}
132
134{
136 if (to >= from)
137 {
138 pos = from;
139 while(pos < to)
140 {
141 *buf++ = ring_buffer[pos++];
142 }
143 return to - from;
144 }
145 else
146 {
147 pos = from;
148 while(pos < RING_BUFFER_SIZE)
149 {
150 *buf++ = ring_buffer[pos++];
151 }
152 pos = 0;
153 while(pos < to)
154 {
155 *buf++ = ring_buffer[pos++];
156 }
157 return RING_BUFFER_SIZE - from + to;
158 }
159}
uint8_t pos
#define EXTI_VectUART1
Definition: fw_exti.h:41
#define EXTI_Global_SetIntState(__STATE__)
Definition: fw_exti.h:115
#define EXTI_UART1_SetIntState(__STATE__)
Definition: fw_exti.h:119
void SYS_SetClock(void)
Definition: fw_sys.c:40
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
unsigned short uint16_t
Definition: fw_types.h:19
@ HAL_State_ON
Definition: fw_types.h:71
unsigned char uint8_t
Definition: fw_types.h:18
#define UART1_ClearRxInterrupt()
Definition: fw_uart.h:53
void UART1_TxString(uint8_t *str)
Definition: fw_uart.c:85
@ UART1_BaudSource_Timer2
Definition: fw_uart.h:34
void UART1_Config8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate)
Definition: fw_uart.c:56
#define UART1_SetRxState(__STATE__)
Definition: fw_uart.h:51
static volatile uint16_t to
uint16_t ring_buffer_read(uint8_t *buf)
uint8_t ring_buffer_pop(uint8_t *c)
void ring_buffer_reset(void)
__IDATA uint8_t ring_buffer[RING_BUFFER_SIZE]
void main(void)
#define RING_BUFFER_SIZE
uint16_t ring_buffer_size(void)
static volatile uint16_t from
void ring_buffer_push(uint8_t c)
INTERRUPT(UART1_Routine, EXTI_VectUART1)