mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-08-12 09:39:25 +03:00
fixed some bugs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
BINARY := blink
|
||||
PREFIX := /usr/bin/arm-none-eabi
|
||||
# MCU code
|
||||
MCU := G431xx
|
||||
# change this linking script depending on particular MCU model,
|
||||
|
||||
Binary file not shown.
@@ -38,6 +38,7 @@ void sys_tick_handler(void){
|
||||
|
||||
static void gpio_setup(void){
|
||||
RCC->AHB2ENR = RCC_AHB2ENR_GPIOCEN; // enable PC
|
||||
__DSB();
|
||||
// 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->PUPDR = PUPD_PD(13); // pulldown
|
||||
|
||||
Binary file not shown.
@@ -35,15 +35,13 @@ int main(void){
|
||||
USART_flags_t f = usart_process();
|
||||
if(f.rxovrfl) usart_sendstr("Rx buffer overflow!\n");
|
||||
if(f.txerr) usart_sendstr("Tx error!\n");
|
||||
if(f.gotstring){
|
||||
char *str = usart_getline();
|
||||
if(str){
|
||||
usart_sendstr("Received:\n");
|
||||
usart_sendstr(str);
|
||||
int l = strlen(str);
|
||||
if(str[l-1] != '\n'){
|
||||
usart_sendstr("\n(only part of line)\n");
|
||||
}
|
||||
char *str = usart_getline();
|
||||
if(str){
|
||||
usart_sendstr("Received:\n");
|
||||
usart_sendstr(str);
|
||||
int l = strlen(str);
|
||||
if(str[l-1] != '\n'){
|
||||
usart_sendstr("\n(only part of line)\n");
|
||||
}
|
||||
}
|
||||
if(Tms - Tblink > 499){
|
||||
|
||||
@@ -31,9 +31,9 @@ int RB_datalen(ringbuffer *b){
|
||||
CHK(b);
|
||||
if(0 == datalen(b)) return 0; // don't block for empty RO operations
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
int l = datalen(b);
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
return l;
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ static int hasbyte(ringbuffer *b, uint8_t byte){
|
||||
int RB_hasbyte(ringbuffer *b, uint8_t byte){
|
||||
CHK(b);
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
int ret = hasbyte(b, byte);
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -100,9 +100,9 @@ int RB_read(ringbuffer *b, uint8_t *s, int len){
|
||||
if(!s || len < 1) return -1;
|
||||
if(0 == datalen(b)) return 0;
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
int r = read(b, s, len);
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
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(0 == datalen(b)) return 0;
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
int n = 0;
|
||||
if(s && len > 0){
|
||||
n = readto(b, byte, s, len);
|
||||
}else{
|
||||
incr(b, &b->head, lento(b, byte)); // just throw data out
|
||||
}
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -151,9 +151,9 @@ int RB_datalento(ringbuffer *b, uint8_t byte){
|
||||
CHK(b);
|
||||
if(0 == datalen(b)) return 0;
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
int n = lento(b, byte);
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -184,9 +184,9 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
|
||||
if(!str || l < 1) return -1;
|
||||
if(b->length - datalen(b) < 2) return 0;
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
int w = write(b, str, l);
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
return w;
|
||||
}
|
||||
|
||||
@@ -194,10 +194,10 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
|
||||
int RB_clearbuf(ringbuffer *b){
|
||||
CHK(b);
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->busy = true;
|
||||
b->head = 0;
|
||||
b->tail = 0;
|
||||
memset(b->data, 0, b->length);
|
||||
b->busy = 0;
|
||||
b->busy = false;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ typedef struct{
|
||||
const int length; // its length
|
||||
int head; // head index
|
||||
int tail; // tail index
|
||||
volatile int busy; // == TRUE if buffer is busy now
|
||||
volatile bool busy; // == TRUE if buffer is busy now
|
||||
} ringbuffer;
|
||||
|
||||
int RB_read(ringbuffer *b, uint8_t *s, int len);
|
||||
|
||||
@@ -121,7 +121,7 @@ void usart_setup(uint32_t speed){
|
||||
|
||||
/**
|
||||
* @brief usart_sendbuf - send next data portion
|
||||
* @return TRUE if sent something
|
||||
* @return true if sent something
|
||||
*/
|
||||
static bool usart_sendbuf(){
|
||||
if(!txrdy) return false;
|
||||
@@ -148,6 +148,7 @@ int usart_send(const char *str, int len){
|
||||
if(put < 0) continue; // busy
|
||||
else if(put == 0){
|
||||
usart_sendbuf(); // no place
|
||||
t = Tms;
|
||||
}else{
|
||||
len -= put;
|
||||
sent += put;
|
||||
@@ -162,6 +163,11 @@ int usart_sendstr(const char *str){
|
||||
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
|
||||
USART_flags_t usart_process(){
|
||||
static uint32_t Tlast = 0;
|
||||
@@ -178,7 +184,6 @@ USART_flags_t usart_process(){
|
||||
// add next data portion to RX ring buffer
|
||||
if(available >= (USARTRXDMABUFSZ / 2) || gotstring){
|
||||
// copy data in one or two chunks (wrap handling)
|
||||
bool wrOK = false;
|
||||
// check if we can write to RB `available` bytes
|
||||
int rballow = RxRB.length - 1 - RB_datalen(&RxRB);
|
||||
if(rballow < available){
|
||||
@@ -192,30 +197,35 @@ USART_flags_t usart_process(){
|
||||
available = rballow; // read at least as we can
|
||||
}
|
||||
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
|
||||
int first = USARTRXDMABUFSZ - dma_read_idx;
|
||||
if((first == RB_write(&RxRB, &dmarxbuf[dma_read_idx], first)) &&
|
||||
(available - first) == RB_write(&RxRB, dmarxbuf, available - first)) wrOK = true;
|
||||
}
|
||||
if(wrOK){
|
||||
gotstring = 0;
|
||||
dma_read_idx = write_idx; // update read pointer
|
||||
int written = RB_write(&RxRB, &dmarxbuf[dma_read_idx], first);
|
||||
if(written != first){ // could write only part - just increase read index
|
||||
if(written > 0) addtoreadidx(written);
|
||||
}else{
|
||||
dma_read_idx = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
char *usart_getline(){
|
||||
static char buff[256];
|
||||
static char buff[MAX_INPLEN];
|
||||
int l = RB_datalento(&RxRB, '\n');
|
||||
if(l < 1){
|
||||
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
|
||||
l = 255;
|
||||
if(l > MAX_INPLEN-1){ // overflow -> read at least part of the string
|
||||
l = MAX_INPLEN-1;
|
||||
}
|
||||
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)
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// maximal length of input string (including '\n' and terminating zero)
|
||||
#define MAX_INPLEN (128)
|
||||
// Rx/Tx ring buffer size
|
||||
#define USARTTXBUFSZ (512)
|
||||
#define USARTRXBUFSZ (512)
|
||||
@@ -36,7 +38,6 @@ typedef union{
|
||||
struct{
|
||||
uint8_t txerr : 1; // transmit error
|
||||
uint8_t rxovrfl : 1; // receive buffer overflow
|
||||
uint8_t gotstring : 1; // have new string in buffer
|
||||
};
|
||||
uint8_t all;
|
||||
} USART_flags_t;
|
||||
|
||||
Reference in New Issue
Block a user