fixed some bugs

This commit is contained in:
Edward Emelianov
2026-07-16 22:42:30 +03:00
parent f2a444c90e
commit 013a0b041c
9 changed files with 50 additions and 39 deletions

View File

@@ -1,4 +1,5 @@
BINARY := blink BINARY := blink
PREFIX := /usr/bin/arm-none-eabi
# MCU code # MCU code
MCU := G431xx MCU := G431xx
# change this linking script depending on particular MCU model, # change this linking script depending on particular MCU model,

Binary file not shown.

View File

@@ -38,6 +38,7 @@ void sys_tick_handler(void){
static void gpio_setup(void){ static void gpio_setup(void){
RCC->AHB2ENR = RCC_AHB2ENR_GPIOCEN; // enable PC RCC->AHB2ENR = RCC_AHB2ENR_GPIOCEN; // enable PC
__DSB();
// set PC6 as push-pull output, PC13 is pulldown input, other as default (AIN) // set PC6 as push-pull output, PC13 is pulldown input, other as default (AIN)
GPIOC->MODER = (0xffffffff & ~(GPIO_MODER_MODE6 | GPIO_MODER_MODE13)) | MODER_O(6) | MODER_I(13); GPIOC->MODER = (0xffffffff & ~(GPIO_MODER_MODE6 | GPIO_MODER_MODE13)) | MODER_O(6) | MODER_I(13);
GPIOC->PUPDR = PUPD_PD(13); // pulldown GPIOC->PUPDR = PUPD_PD(13); // pulldown

Binary file not shown.

View File

@@ -35,7 +35,6 @@ int main(void){
USART_flags_t f = usart_process(); USART_flags_t f = usart_process();
if(f.rxovrfl) usart_sendstr("Rx buffer overflow!\n"); if(f.rxovrfl) usart_sendstr("Rx buffer overflow!\n");
if(f.txerr) usart_sendstr("Tx error!\n"); if(f.txerr) usart_sendstr("Tx error!\n");
if(f.gotstring){
char *str = usart_getline(); char *str = usart_getline();
if(str){ if(str){
usart_sendstr("Received:\n"); usart_sendstr("Received:\n");
@@ -45,7 +44,6 @@ int main(void){
usart_sendstr("\n(only part of line)\n"); usart_sendstr("\n(only part of line)\n");
} }
} }
}
if(Tms - Tblink > 499){ if(Tms - Tblink > 499){
LED_TOGG(); LED_TOGG();
Tblink = Tms; Tblink = Tms;

View File

@@ -31,9 +31,9 @@ int RB_datalen(ringbuffer *b){
CHK(b); CHK(b);
if(0 == datalen(b)) return 0; // don't block for empty RO operations if(0 == datalen(b)) return 0; // don't block for empty RO operations
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
int l = datalen(b); int l = datalen(b);
b->busy = 0; b->busy = false;
return l; return l;
} }
@@ -59,9 +59,9 @@ static int hasbyte(ringbuffer *b, uint8_t byte){
int RB_hasbyte(ringbuffer *b, uint8_t byte){ int RB_hasbyte(ringbuffer *b, uint8_t byte){
CHK(b); CHK(b);
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
int ret = hasbyte(b, byte); int ret = hasbyte(b, byte);
b->busy = 0; b->busy = false;
return ret; return ret;
} }
@@ -100,9 +100,9 @@ int RB_read(ringbuffer *b, uint8_t *s, int len){
if(!s || len < 1) return -1; if(!s || len < 1) return -1;
if(0 == datalen(b)) return 0; if(0 == datalen(b)) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
int r = read(b, s, len); int r = read(b, s, len);
b->busy = 0; b->busy = false;
return r; return r;
} }
@@ -136,14 +136,14 @@ int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
if(!s || len < 1) return -1; if(!s || len < 1) return -1;
if(0 == datalen(b)) return 0; if(0 == datalen(b)) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
int n = 0; int n = 0;
if(s && len > 0){ if(s && len > 0){
n = readto(b, byte, s, len); n = readto(b, byte, s, len);
}else{ }else{
incr(b, &b->head, lento(b, byte)); // just throw data out incr(b, &b->head, lento(b, byte)); // just throw data out
} }
b->busy = 0; b->busy = false;
return n; return n;
} }
@@ -151,9 +151,9 @@ int RB_datalento(ringbuffer *b, uint8_t byte){
CHK(b); CHK(b);
if(0 == datalen(b)) return 0; if(0 == datalen(b)) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
int n = lento(b, byte); int n = lento(b, byte);
b->busy = 0; b->busy = false;
return n; return n;
} }
@@ -184,9 +184,9 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
if(!str || l < 1) return -1; if(!str || l < 1) return -1;
if(b->length - datalen(b) < 2) return 0; if(b->length - datalen(b) < 2) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
int w = write(b, str, l); int w = write(b, str, l);
b->busy = 0; b->busy = false;
return w; return w;
} }
@@ -194,10 +194,10 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
int RB_clearbuf(ringbuffer *b){ int RB_clearbuf(ringbuffer *b){
CHK(b); CHK(b);
if(b->busy) return -1; if(b->busy) return -1;
b->busy = 1; b->busy = true;
b->head = 0; b->head = 0;
b->tail = 0; b->tail = 0;
memset(b->data, 0, b->length); memset(b->data, 0, b->length);
b->busy = 0; b->busy = false;
return 1; return 1;
} }

View File

@@ -24,7 +24,7 @@ typedef struct{
const int length; // its length const int length; // its length
int head; // head index int head; // head index
int tail; // tail index int tail; // tail index
volatile int busy; // == TRUE if buffer is busy now volatile bool busy; // == TRUE if buffer is busy now
} ringbuffer; } ringbuffer;
int RB_read(ringbuffer *b, uint8_t *s, int len); int RB_read(ringbuffer *b, uint8_t *s, int len);

View File

@@ -121,7 +121,7 @@ void usart_setup(uint32_t speed){
/** /**
* @brief usart_sendbuf - send next data portion * @brief usart_sendbuf - send next data portion
* @return TRUE if sent something * @return true if sent something
*/ */
static bool usart_sendbuf(){ static bool usart_sendbuf(){
if(!txrdy) return false; if(!txrdy) return false;
@@ -148,6 +148,7 @@ int usart_send(const char *str, int len){
if(put < 0) continue; // busy if(put < 0) continue; // busy
else if(put == 0){ else if(put == 0){
usart_sendbuf(); // no place usart_sendbuf(); // no place
t = Tms;
}else{ }else{
len -= put; len -= put;
sent += put; sent += put;
@@ -162,6 +163,11 @@ int usart_sendstr(const char *str){
return usart_send(str, l); return usart_send(str, l);
} }
static void addtoreadidx(int adder){
dma_read_idx += adder;
if(dma_read_idx >= USARTRXDMABUFSZ) dma_read_idx -= USARTRXDMABUFSZ;
}
// return current flags // return current flags
USART_flags_t usart_process(){ USART_flags_t usart_process(){
static uint32_t Tlast = 0; static uint32_t Tlast = 0;
@@ -178,7 +184,6 @@ USART_flags_t usart_process(){
// add next data portion to RX ring buffer // add next data portion to RX ring buffer
if(available >= (USARTRXDMABUFSZ / 2) || gotstring){ if(available >= (USARTRXDMABUFSZ / 2) || gotstring){
// copy data in one or two chunks (wrap handling) // copy data in one or two chunks (wrap handling)
bool wrOK = false;
// check if we can write to RB `available` bytes // check if we can write to RB `available` bytes
int rballow = RxRB.length - 1 - RB_datalen(&RxRB); int rballow = RxRB.length - 1 - RB_datalen(&RxRB);
if(rballow < available){ if(rballow < available){
@@ -192,30 +197,35 @@ USART_flags_t usart_process(){
available = rballow; // read at least as we can available = rballow; // read at least as we can
} }
if(dma_read_idx + available <= USARTRXDMABUFSZ){ // head before tail if(dma_read_idx + available <= USARTRXDMABUFSZ){ // head before tail
if(available == RB_write(&RxRB, &dmarxbuf[dma_read_idx], available)) wrOK = true; int written = RB_write(&RxRB, &dmarxbuf[dma_read_idx], available);
if(written == available && dmarxbuf[dma_read_idx+available-1] == '\n') gotstring = 0;
if(written > 0) addtoreadidx(written);
}else{ // head after tail - two chunks }else{ // head after tail - two chunks
int first = USARTRXDMABUFSZ - dma_read_idx; int first = USARTRXDMABUFSZ - dma_read_idx;
if((first == RB_write(&RxRB, &dmarxbuf[dma_read_idx], first)) && int written = RB_write(&RxRB, &dmarxbuf[dma_read_idx], first);
(available - first) == RB_write(&RxRB, dmarxbuf, available - first)) wrOK = true; if(written != first){ // could write only part - just increase read index
} if(written > 0) addtoreadidx(written);
if(wrOK){ }else{
gotstring = 0; dma_read_idx = 0;
dma_read_idx = write_idx; // update read pointer int last = available - first;
written = RB_write(&RxRB, dmarxbuf, last);
if(written == last && dmarxbuf[last-1] == '\n') gotstring = 0;
if(written > 0) addtoreadidx(written);
}
} }
} }
if(RB_hasbyte(&RxRB, '\n')) flags.gotstring = 1;
return flags; return flags;
} }
char *usart_getline(){ char *usart_getline(){
static char buff[256]; static char buff[MAX_INPLEN];
int l = RB_datalento(&RxRB, '\n'); int l = RB_datalento(&RxRB, '\n');
if(l < 1){ if(l < 1){
l = RB_datalen(&RxRB); // Rx ringbuffer could be near overflow but without '\n' l = RB_datalen(&RxRB); // Rx ringbuffer could be near overflow but without '\n'
if(l < 255) return NULL; // allow to wait for last symbols if(l < MAX_INPLEN-1) return NULL; // allow to wait for last symbols
} }
if(l > 255){ // overflow -> read at least part of the string if(l > MAX_INPLEN-1){ // overflow -> read at least part of the string
l = 255; l = MAX_INPLEN-1;
} }
if(l != RB_read(&RxRB, (uint8_t*)buff, l)) return NULL; if(l != RB_read(&RxRB, (uint8_t*)buff, l)) return NULL;
buff[l] = 0; // return with '\n' at end of line (so user can detect non-finished overflowed lines) buff[l] = 0; // return with '\n' at end of line (so user can detect non-finished overflowed lines)

View File

@@ -20,6 +20,8 @@
#include <stdint.h> #include <stdint.h>
// maximal length of input string (including '\n' and terminating zero)
#define MAX_INPLEN (128)
// Rx/Tx ring buffer size // Rx/Tx ring buffer size
#define USARTTXBUFSZ (512) #define USARTTXBUFSZ (512)
#define USARTRXBUFSZ (512) #define USARTRXBUFSZ (512)
@@ -36,7 +38,6 @@ typedef union{
struct{ struct{
uint8_t txerr : 1; // transmit error uint8_t txerr : 1; // transmit error
uint8_t rxovrfl : 1; // receive buffer overflow uint8_t rxovrfl : 1; // receive buffer overflow
uint8_t gotstring : 1; // have new string in buffer
}; };
uint8_t all; uint8_t all;
} USART_flags_t; } USART_flags_t;