mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 18:55:13 +03:00
fixed some problems with USART
This commit is contained in:
parent
a1861bb86d
commit
e959c7dfe2
@ -80,8 +80,7 @@ void can_messages_proc(){
|
||||
#ifdef EBUG
|
||||
SEND("got message, len: "); bufputchar('0' + len);
|
||||
SEND(", data: ");
|
||||
uint8_t ctr;
|
||||
for(ctr = 0; ctr < len; ++ctr){
|
||||
for(int ctr = 0; ctr < len; ++ctr){
|
||||
printuhex(can_mesg->data[ctr]);
|
||||
bufputchar(' ');
|
||||
}
|
||||
|
||||
@ -124,6 +124,10 @@ int main(void){
|
||||
char *txt = NULL;
|
||||
r = usart_getline(&txt);
|
||||
txt[r] = 0;
|
||||
#ifdef EBUG
|
||||
USB_send("\n\nUSART got:\n");
|
||||
USB_send(txt); USB_send("\n\n");
|
||||
#endif
|
||||
cmd_parser(txt, 0);
|
||||
}
|
||||
if(lastB - Tms > 99){ // run `sendbuf` each 100ms
|
||||
|
||||
@ -37,7 +37,13 @@ extern volatile uint8_t canerror;
|
||||
extern volatile uint32_t Tms;
|
||||
|
||||
static char buff[UARTBUFSZ+1], /* +1 - for USB send (it receive \0-terminated line) */ *bptr = buff;
|
||||
static int blen = 0, USBcmd = 0, debugmode = 0;
|
||||
static int blen = 0, USBcmd = 0, debugmode =
|
||||
#ifdef EBUG
|
||||
1
|
||||
#else
|
||||
0
|
||||
#endif
|
||||
;
|
||||
// LEDs are OFF by default
|
||||
uint8_t noLED =
|
||||
#ifdef EBUG
|
||||
@ -70,10 +76,10 @@ void addtobuf(const char *txt){
|
||||
}
|
||||
strcpy(bptr, txt);
|
||||
bptr += l;
|
||||
}
|
||||
*bptr = 0;
|
||||
blen += l;
|
||||
}
|
||||
}
|
||||
|
||||
void bufputchar(char ch){
|
||||
if(blen > UARTBUFSZ-1){
|
||||
|
||||
@ -65,7 +65,13 @@ const char *sensors_get_statename(SensorsState x){
|
||||
|
||||
// TODO: check if we can convert double to float!
|
||||
|
||||
const double mul[5] = {-1.5e-2, 1., -2., 4., -2.};
|
||||
#ifndef EBUG
|
||||
#define TYPE double
|
||||
#else
|
||||
#define TYPE float
|
||||
#endif
|
||||
|
||||
const TYPE mul[5] = {-1.5e-2, 1., -2., 4., -2.};
|
||||
/**
|
||||
* Get temperature & calculate it by polinome
|
||||
* T = (-2) * k4 * 10^{-21} * ADC16^4
|
||||
@ -86,10 +92,10 @@ static uint16_t calc_t(uint32_t t, int i){
|
||||
}
|
||||
if(t < 600000 || t > 30000000) return BAD_TEMPERATURE; // wrong value - too small or too large
|
||||
int j;
|
||||
double d = (double)t/256., tmp = 0.;
|
||||
TYPE d = (TYPE)t/256., tmp = 0.;
|
||||
// k0*(-1.5e-2) + 0.1*1e-5*val*(1*k1 + 1e-5*val*(-2.*k2 + 1e-5*val*(4*k3 + 1e-5*val*(-2*k4))))
|
||||
for(j = 4; j > 0; --j){
|
||||
tmp += mul[j] * (double)coeff[j];
|
||||
tmp += mul[j] * (TYPE)coeff[j];
|
||||
tmp *= 1e-5*d;
|
||||
}
|
||||
tmp = tmp * 10. + 100. * mul[0] * coeff[0];
|
||||
@ -143,8 +149,12 @@ void sensors_start(){
|
||||
Sstate = SENS_START_MSRMNT;
|
||||
break;
|
||||
case SENS_OFF:
|
||||
if(Nsens_present){ // already gon N sensors - use this information
|
||||
overcurnt_ctr = 0;
|
||||
if(sensors_on()) Sstate = SENS_START_MSRMNT;
|
||||
}else{
|
||||
sensors_init();
|
||||
}
|
||||
break;
|
||||
case SENS_OVERCURNT_OFF:
|
||||
sensors_init();
|
||||
@ -314,11 +324,11 @@ void showcoeffs(){
|
||||
void showtemperature(){
|
||||
int a, p;
|
||||
if(Nsens_present == 0){
|
||||
SEND("showtemperature(): no sensors found");
|
||||
SEND("showtemperature(): no sensors found\n");
|
||||
return;
|
||||
}
|
||||
if(Ntemp_measured == 0){
|
||||
SEND("showtemperature(): no temperatures measured");
|
||||
SEND("showtemperature(): no temperatures measured\n");
|
||||
return;
|
||||
}
|
||||
for(a = 0; a <= MUL_MAX_ADDRESS; ++a){
|
||||
@ -338,6 +348,7 @@ void showtemperature(){
|
||||
}
|
||||
printu(t);
|
||||
newline();
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -410,6 +421,12 @@ void sensors_process(){
|
||||
break;
|
||||
case SENS_SENDING_DATA:
|
||||
mesg("SENS_SENDING_DATA");
|
||||
if(Nsens_present == 0){
|
||||
mesg("No sensors found -> off");
|
||||
sensors_off();
|
||||
NsentOverCAN = 0;
|
||||
break;
|
||||
}
|
||||
NsentOverCAN = send_temperatures(NsentOverCAN); // call sending T process
|
||||
if(NsentOverCAN < 0){ // all data sent -> sleep
|
||||
Sstate = SENS_SLEEPING;
|
||||
|
||||
Binary file not shown.
@ -25,6 +25,10 @@
|
||||
#include "hardware.h"
|
||||
#include "usart.h"
|
||||
|
||||
#ifdef EBUG
|
||||
#include "usb.h"
|
||||
#endif
|
||||
|
||||
|
||||
extern volatile uint32_t Tms;
|
||||
static int datalen[2] = {0,0}; // received data line length (including '\n')
|
||||
@ -58,7 +62,14 @@ TXstatus usart_send(const char *str, int len){
|
||||
if(!txrdy) return LINE_BUSY;
|
||||
if(len > UARTBUFSZ) return STR_TOO_LONG;
|
||||
txrdy = 0;
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
#ifdef EBUG
|
||||
USB_send("\n\n\nUSART send:\n");
|
||||
USB_send(str);
|
||||
USB_send("\n\n");
|
||||
#endif
|
||||
memcpy(tbuf, str, len);
|
||||
while(!(USARTX->ISR & USART_ISR_TXE)); // no refresh of WD to prevent weird things
|
||||
#if USARTNUM == 2
|
||||
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
|
||||
DMA1_Channel4->CNDTR = len;
|
||||
@ -77,13 +88,20 @@ TXstatus usart_send_blocking(const char *str, int len){
|
||||
if(!txrdy) return LINE_BUSY;
|
||||
int i;
|
||||
bufovr = 0;
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
while(!(USARTX->ISR & USART_ISR_TXE)); // no refresh of WD to prevent weird things
|
||||
#ifdef EBUG
|
||||
USB_send("\n\n\nUSART send blocking:\n");
|
||||
USB_send(str);
|
||||
USB_send("\n\n");
|
||||
#endif
|
||||
for(i = 0; i < len; ++i){
|
||||
USARTX -> TDR = *str++;
|
||||
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;};
|
||||
}
|
||||
return ALL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
void usart_send_blck(const char *str){
|
||||
while(!txrdy){IWDG->KR = IWDG_REFRESH;}
|
||||
bufovr = 0;
|
||||
@ -91,7 +109,7 @@ void usart_send_blck(const char *str){
|
||||
USARTX -> TDR = *str++;
|
||||
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;};
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
void usart_setup(){
|
||||
// Nucleo's USART2 connected to VCP proxy of st-link
|
||||
@ -199,6 +217,7 @@ void usart1_isr(){
|
||||
#if USARTNUM == 2
|
||||
void dma1_channel4_5_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
|
||||
DMA1_Channel4->CCR &= ~DMA_CCR_EN; // stop DMA
|
||||
DMA1->IFCR |= DMA_IFCR_CTCIF4; // clear TC flag
|
||||
txrdy = 1;
|
||||
}
|
||||
@ -207,6 +226,7 @@ void dma1_channel4_5_isr(){
|
||||
#elif USARTNUM == 1
|
||||
void dma1_channel2_3_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF2){ // Tx
|
||||
DMA1_Channel2->CCR &= ~DMA_CCR_EN; // stop DMA
|
||||
DMA1->IFCR |= DMA_IFCR_CTCIF2; // clear TC flag
|
||||
txrdy = 1;
|
||||
}
|
||||
|
||||
@ -46,6 +46,6 @@ void usart_setup();
|
||||
int usart_getline(char **line);
|
||||
TXstatus usart_send(const char *str, int len);
|
||||
TXstatus usart_send_blocking(const char *str, int len);
|
||||
void usart_send_blck(const char *str);
|
||||
//void usart_send_blck(const char *str);
|
||||
|
||||
#endif // __USART_H__
|
||||
|
||||
@ -33,6 +33,7 @@ static uint8_t incoming_data[IDATASZ];
|
||||
static uint8_t ovfl = 0;
|
||||
static uint16_t idatalen = 0;
|
||||
static int8_t usbON = 0; // ==1 when USB fully configured
|
||||
volatile int8_t usbConn = 0; // ==1 when connected
|
||||
static volatile uint8_t tx_succesfull = 0;
|
||||
|
||||
// interrupt IN handler (never used?)
|
||||
@ -111,6 +112,7 @@ void usb_proc(){
|
||||
}
|
||||
|
||||
void USB_send(const char *buf){
|
||||
if(!usbConn) return;
|
||||
uint16_t l = 0, ctr = 0;
|
||||
const char *p = buf;
|
||||
while(*p++) ++l;
|
||||
@ -143,22 +145,10 @@ int USB_receive(char *buf, int bufsize){
|
||||
}
|
||||
}
|
||||
if(i == idatalen || stlen == 0) return 0;
|
||||
/*
|
||||
char x[] = "USB got x:\n";
|
||||
x[8] = '0' + stlen;
|
||||
usart_send_blck(x);
|
||||
usart_send_blck((char*)incoming_data);
|
||||
usart_send_blck("\n");
|
||||
*/
|
||||
USB->CNTR = 0;
|
||||
int sz = (stlen > bufsize) ? bufsize : stlen, rest = idatalen - sz;
|
||||
memcpy(buf, incoming_data, sz);
|
||||
buf[sz] = 0;
|
||||
/*
|
||||
usart_send_blck("buf:\n");
|
||||
usart_send_blck((char*)buf);
|
||||
usart_send_blck("\n");
|
||||
*/
|
||||
if(rest > 0){
|
||||
memmove(incoming_data, &incoming_data[sz], rest);
|
||||
idatalen = rest;
|
||||
@ -177,7 +167,7 @@ int USB_receive(char *buf, int bufsize){
|
||||
/**
|
||||
* @brief USB_configured
|
||||
* @return 1 if USB is in configured state
|
||||
*/
|
||||
*
|
||||
int USB_configured(){
|
||||
return usbON;
|
||||
}
|
||||
}*/
|
||||
|
||||
@ -32,6 +32,6 @@ void USB_setup();
|
||||
void usb_proc();
|
||||
void USB_send(const char *buf);
|
||||
int USB_receive(char *buf, int bufsize);
|
||||
int USB_configured();
|
||||
//int USB_configured();
|
||||
|
||||
#endif // __USB_H__
|
||||
|
||||
@ -27,11 +27,12 @@
|
||||
#include "usart.h"
|
||||
#include "usb_lib.h"
|
||||
|
||||
|
||||
#ifdef EBUG
|
||||
#undef EBUG
|
||||
#include "usart.h"
|
||||
#define MSG(x) do{usart_send(x, sizeof(x));}while(0)
|
||||
#endif
|
||||
|
||||
|
||||
ep_t endpoints[ENDPOINTS_NUM];
|
||||
|
||||
static usb_dev_t USB_Dev;
|
||||
@ -148,17 +149,17 @@ _USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
|
||||
*/
|
||||
// SET_LINE_CODING
|
||||
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){
|
||||
//MSG("linecoding_handler\n");
|
||||
MSG("linecoding_handler\n");
|
||||
}
|
||||
|
||||
// SET_CONTROL_LINE_STATE
|
||||
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
|
||||
//MSG("clstate_handler\n");
|
||||
MSG("clstate_handler\n");
|
||||
}
|
||||
|
||||
// SEND_BREAK
|
||||
void WEAK break_handler(){
|
||||
//MSG("break_handler\n");
|
||||
MSG("break_handler\n");
|
||||
}
|
||||
|
||||
// handler of vendor requests
|
||||
@ -192,12 +193,6 @@ void WEAK vendor_handler(config_pack_t *packet){
|
||||
}
|
||||
|
||||
|
||||
#ifdef EBUG
|
||||
uint8_t _2wr = 0;
|
||||
#define WRITEDUMP(str) do{MSG(str); _2wr = 1;}while(0)
|
||||
#else
|
||||
#define WRITEDUMP(str)
|
||||
#endif
|
||||
static void wr0(const uint8_t *buf, uint16_t size){
|
||||
if(setup_packet.wLength < size) size = setup_packet.wLength;
|
||||
EP_WriteIRQ(0, buf, size);
|
||||
@ -227,7 +222,7 @@ static inline void get_descriptor(){
|
||||
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
|
||||
break;
|
||||
default:
|
||||
WRITEDUMP("UNK_DES");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -243,11 +238,9 @@ static inline void std_d2h_req(){
|
||||
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered
|
||||
break;
|
||||
case GET_CONFIGURATION:
|
||||
WRITEDUMP("GET_CONFIGURATION");
|
||||
EP_WriteIRQ(0, &configuration, 1);
|
||||
break;
|
||||
default:
|
||||
WRITEDUMP("80:WR_REQ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -264,11 +257,12 @@ static inline void std_h2d_req(){
|
||||
configuration = setup_packet.wValue;
|
||||
break;
|
||||
default:
|
||||
WRITEDUMP("0:WR_REQ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extern volatile int8_t usbConn;
|
||||
/*
|
||||
bmRequestType: 76543210
|
||||
7 direction: 0 - host->device, 1 - device->host
|
||||
@ -303,8 +297,6 @@ static uint16_t EP0_Handler(ep_t ep){
|
||||
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||
epstatus = SET_NAK_RX(epstatus);
|
||||
epstatus = SET_VALID_TX(epstatus);
|
||||
}else{
|
||||
WRITEDUMP("02:WR_REQ");
|
||||
}
|
||||
break;
|
||||
case VENDOR_REQUEST_TYPE:
|
||||
@ -324,9 +316,10 @@ static uint16_t EP0_Handler(ep_t ep){
|
||||
break;
|
||||
case SEND_BREAK:
|
||||
break_handler();
|
||||
usbConn = 0;
|
||||
break;
|
||||
default:
|
||||
WRITEDUMP("undef control req");
|
||||
break;
|
||||
}
|
||||
if(!dev2host) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
|
||||
epstatus = SET_VALID_RX(epstatus);
|
||||
@ -341,8 +334,8 @@ static uint16_t EP0_Handler(ep_t ep){
|
||||
if(ep.rx_cnt){
|
||||
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||
if(setup_packet.bRequest == SET_LINE_CODING){
|
||||
//WRITEDUMP("SET_LINE_CODING");
|
||||
linecoding_handler((usb_LineCoding*)ep0databuf);
|
||||
usbConn = 1; // now we can transmit data: computer have stable connection
|
||||
}
|
||||
}
|
||||
// Close transaction
|
||||
@ -364,32 +357,8 @@ static uint16_t EP0_Handler(ep_t ep){
|
||||
epstatus = SET_VALID_RX(epstatus);
|
||||
epstatus = SET_VALID_TX(epstatus);
|
||||
}
|
||||
#ifdef EBUG
|
||||
if(_2wr){
|
||||
usart_putchar(' ');
|
||||
if (ep.rx_flag) usart_putchar('r');
|
||||
else usart_putchar('t');
|
||||
printu(setup_packet.wLength);
|
||||
if(ep.setup_flag) usart_putchar('s');
|
||||
usart_putchar(' ');
|
||||
usart_putchar('I');
|
||||
printu(setup_packet.wIndex);
|
||||
usart_putchar('V');
|
||||
printu(setup_packet.wValue);
|
||||
usart_putchar('R');
|
||||
printu(setup_packet.bRequest);
|
||||
usart_putchar('T');
|
||||
printu(setup_packet.bmRequestType);
|
||||
usart_putchar(' ');
|
||||
usart_putchar('0' + ep0dbuflen);
|
||||
usart_putchar(' ');
|
||||
hexdump(ep0databuf, ep0dbuflen);
|
||||
usart_putchar('\n');
|
||||
}
|
||||
#endif
|
||||
return epstatus;
|
||||
}
|
||||
#undef WRITEDUMP
|
||||
|
||||
static uint16_t lastaddr = USB_EP0_BASEADDR;
|
||||
/**
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
#define BUILD_NUMBER "48"
|
||||
#define BUILD_DATE "2023-08-29"
|
||||
#define BUILDNO 48
|
||||
#define BUILD_NUMBER "66"
|
||||
#define BUILD_DATE "2023-09-11"
|
||||
#define BUILDNO 66
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user