fixed some problems with USART

This commit is contained in:
Edward Emelianov 2023-09-11 22:09:22 +03:00
parent a1861bb86d
commit e959c7dfe2
11 changed files with 81 additions and 76 deletions

View File

@ -80,8 +80,7 @@ void can_messages_proc(){
#ifdef EBUG #ifdef EBUG
SEND("got message, len: "); bufputchar('0' + len); SEND("got message, len: "); bufputchar('0' + len);
SEND(", data: "); SEND(", data: ");
uint8_t ctr; for(int ctr = 0; ctr < len; ++ctr){
for(ctr = 0; ctr < len; ++ctr){
printuhex(can_mesg->data[ctr]); printuhex(can_mesg->data[ctr]);
bufputchar(' '); bufputchar(' ');
} }

View File

@ -124,6 +124,10 @@ int main(void){
char *txt = NULL; char *txt = NULL;
r = usart_getline(&txt); r = usart_getline(&txt);
txt[r] = 0; txt[r] = 0;
#ifdef EBUG
USB_send("\n\nUSART got:\n");
USB_send(txt); USB_send("\n\n");
#endif
cmd_parser(txt, 0); cmd_parser(txt, 0);
} }
if(lastB - Tms > 99){ // run `sendbuf` each 100ms if(lastB - Tms > 99){ // run `sendbuf` each 100ms

View File

@ -37,7 +37,13 @@ extern volatile uint8_t canerror;
extern volatile uint32_t Tms; extern volatile uint32_t Tms;
static char buff[UARTBUFSZ+1], /* +1 - for USB send (it receive \0-terminated line) */ *bptr = buff; 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 // LEDs are OFF by default
uint8_t noLED = uint8_t noLED =
#ifdef EBUG #ifdef EBUG
@ -70,9 +76,9 @@ void addtobuf(const char *txt){
} }
strcpy(bptr, txt); strcpy(bptr, txt);
bptr += l; bptr += l;
*bptr = 0;
blen += l;
} }
*bptr = 0;
blen += l;
} }
void bufputchar(char ch){ void bufputchar(char ch){

View File

@ -65,7 +65,13 @@ const char *sensors_get_statename(SensorsState x){
// TODO: check if we can convert double to float! // 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 * Get temperature & calculate it by polinome
* T = (-2) * k4 * 10^{-21} * ADC16^4 * 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 if(t < 600000 || t > 30000000) return BAD_TEMPERATURE; // wrong value - too small or too large
int j; 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)))) // 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){ for(j = 4; j > 0; --j){
tmp += mul[j] * (double)coeff[j]; tmp += mul[j] * (TYPE)coeff[j];
tmp *= 1e-5*d; tmp *= 1e-5*d;
} }
tmp = tmp * 10. + 100. * mul[0] * coeff[0]; tmp = tmp * 10. + 100. * mul[0] * coeff[0];
@ -143,8 +149,12 @@ void sensors_start(){
Sstate = SENS_START_MSRMNT; Sstate = SENS_START_MSRMNT;
break; break;
case SENS_OFF: case SENS_OFF:
overcurnt_ctr = 0; if(Nsens_present){ // already gon N sensors - use this information
if(sensors_on()) Sstate = SENS_START_MSRMNT; overcurnt_ctr = 0;
if(sensors_on()) Sstate = SENS_START_MSRMNT;
}else{
sensors_init();
}
break; break;
case SENS_OVERCURNT_OFF: case SENS_OVERCURNT_OFF:
sensors_init(); sensors_init();
@ -314,11 +324,11 @@ void showcoeffs(){
void showtemperature(){ void showtemperature(){
int a, p; int a, p;
if(Nsens_present == 0){ if(Nsens_present == 0){
SEND("showtemperature(): no sensors found"); SEND("showtemperature(): no sensors found\n");
return; return;
} }
if(Ntemp_measured == 0){ if(Ntemp_measured == 0){
SEND("showtemperature(): no temperatures measured"); SEND("showtemperature(): no temperatures measured\n");
return; return;
} }
for(a = 0; a <= MUL_MAX_ADDRESS; ++a){ for(a = 0; a <= MUL_MAX_ADDRESS; ++a){
@ -338,6 +348,7 @@ void showtemperature(){
} }
printu(t); printu(t);
newline(); newline();
IWDG->KR = IWDG_REFRESH;
} }
} }
} }
@ -410,6 +421,12 @@ void sensors_process(){
break; break;
case SENS_SENDING_DATA: case SENS_SENDING_DATA:
mesg("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 NsentOverCAN = send_temperatures(NsentOverCAN); // call sending T process
if(NsentOverCAN < 0){ // all data sent -> sleep if(NsentOverCAN < 0){ // all data sent -> sleep
Sstate = SENS_SLEEPING; Sstate = SENS_SLEEPING;

View File

@ -25,6 +25,10 @@
#include "hardware.h" #include "hardware.h"
#include "usart.h" #include "usart.h"
#ifdef EBUG
#include "usb.h"
#endif
extern volatile uint32_t Tms; extern volatile uint32_t Tms;
static int datalen[2] = {0,0}; // received data line length (including '\n') 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(!txrdy) return LINE_BUSY;
if(len > UARTBUFSZ) return STR_TOO_LONG; if(len > UARTBUFSZ) return STR_TOO_LONG;
txrdy = 0; 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); memcpy(tbuf, str, len);
while(!(USARTX->ISR & USART_ISR_TXE)); // no refresh of WD to prevent weird things
#if USARTNUM == 2 #if USARTNUM == 2
DMA1_Channel4->CCR &= ~DMA_CCR_EN; DMA1_Channel4->CCR &= ~DMA_CCR_EN;
DMA1_Channel4->CNDTR = len; DMA1_Channel4->CNDTR = len;
@ -77,13 +88,20 @@ TXstatus usart_send_blocking(const char *str, int len){
if(!txrdy) return LINE_BUSY; if(!txrdy) return LINE_BUSY;
int i; int i;
bufovr = 0; 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){ for(i = 0; i < len; ++i){
USARTX -> TDR = *str++; USARTX -> TDR = *str++;
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;}; while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;};
} }
return ALL_OK; return ALL_OK;
} }
/*
void usart_send_blck(const char *str){ void usart_send_blck(const char *str){
while(!txrdy){IWDG->KR = IWDG_REFRESH;} while(!txrdy){IWDG->KR = IWDG_REFRESH;}
bufovr = 0; bufovr = 0;
@ -91,7 +109,7 @@ void usart_send_blck(const char *str){
USARTX -> TDR = *str++; USARTX -> TDR = *str++;
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;}; while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;};
} }
} }*/
void usart_setup(){ void usart_setup(){
// Nucleo's USART2 connected to VCP proxy of st-link // Nucleo's USART2 connected to VCP proxy of st-link
@ -199,6 +217,7 @@ void usart1_isr(){
#if USARTNUM == 2 #if USARTNUM == 2
void dma1_channel4_5_isr(){ void dma1_channel4_5_isr(){
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
DMA1_Channel4->CCR &= ~DMA_CCR_EN; // stop DMA
DMA1->IFCR |= DMA_IFCR_CTCIF4; // clear TC flag DMA1->IFCR |= DMA_IFCR_CTCIF4; // clear TC flag
txrdy = 1; txrdy = 1;
} }
@ -207,6 +226,7 @@ void dma1_channel4_5_isr(){
#elif USARTNUM == 1 #elif USARTNUM == 1
void dma1_channel2_3_isr(){ void dma1_channel2_3_isr(){
if(DMA1->ISR & DMA_ISR_TCIF2){ // Tx if(DMA1->ISR & DMA_ISR_TCIF2){ // Tx
DMA1_Channel2->CCR &= ~DMA_CCR_EN; // stop DMA
DMA1->IFCR |= DMA_IFCR_CTCIF2; // clear TC flag DMA1->IFCR |= DMA_IFCR_CTCIF2; // clear TC flag
txrdy = 1; txrdy = 1;
} }

View File

@ -46,6 +46,6 @@ void usart_setup();
int usart_getline(char **line); int usart_getline(char **line);
TXstatus usart_send(const char *str, int len); TXstatus usart_send(const char *str, int len);
TXstatus usart_send_blocking(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__ #endif // __USART_H__

View File

@ -33,6 +33,7 @@ static uint8_t incoming_data[IDATASZ];
static uint8_t ovfl = 0; static uint8_t ovfl = 0;
static uint16_t idatalen = 0; static uint16_t idatalen = 0;
static int8_t usbON = 0; // ==1 when USB fully configured 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; static volatile uint8_t tx_succesfull = 0;
// interrupt IN handler (never used?) // interrupt IN handler (never used?)
@ -111,6 +112,7 @@ void usb_proc(){
} }
void USB_send(const char *buf){ void USB_send(const char *buf){
if(!usbConn) return;
uint16_t l = 0, ctr = 0; uint16_t l = 0, ctr = 0;
const char *p = buf; const char *p = buf;
while(*p++) ++l; while(*p++) ++l;
@ -143,22 +145,10 @@ int USB_receive(char *buf, int bufsize){
} }
} }
if(i == idatalen || stlen == 0) return 0; 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; USB->CNTR = 0;
int sz = (stlen > bufsize) ? bufsize : stlen, rest = idatalen - sz; int sz = (stlen > bufsize) ? bufsize : stlen, rest = idatalen - sz;
memcpy(buf, incoming_data, sz); memcpy(buf, incoming_data, sz);
buf[sz] = 0; buf[sz] = 0;
/*
usart_send_blck("buf:\n");
usart_send_blck((char*)buf);
usart_send_blck("\n");
*/
if(rest > 0){ if(rest > 0){
memmove(incoming_data, &incoming_data[sz], rest); memmove(incoming_data, &incoming_data[sz], rest);
idatalen = rest; idatalen = rest;
@ -177,7 +167,7 @@ int USB_receive(char *buf, int bufsize){
/** /**
* @brief USB_configured * @brief USB_configured
* @return 1 if USB is in configured state * @return 1 if USB is in configured state
*/ *
int USB_configured(){ int USB_configured(){
return usbON; return usbON;
} }*/

View File

@ -32,6 +32,6 @@ void USB_setup();
void usb_proc(); void usb_proc();
void USB_send(const char *buf); void USB_send(const char *buf);
int USB_receive(char *buf, int bufsize); int USB_receive(char *buf, int bufsize);
int USB_configured(); //int USB_configured();
#endif // __USB_H__ #endif // __USB_H__

View File

@ -27,11 +27,12 @@
#include "usart.h" #include "usart.h"
#include "usb_lib.h" #include "usb_lib.h"
#ifdef EBUG #ifdef EBUG
#undef EBUG #include "usart.h"
#define MSG(x) do{usart_send(x, sizeof(x));}while(0)
#endif #endif
ep_t endpoints[ENDPOINTS_NUM]; ep_t endpoints[ENDPOINTS_NUM];
static usb_dev_t USB_Dev; static usb_dev_t USB_Dev;
@ -148,17 +149,17 @@ _USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
*/ */
// SET_LINE_CODING // SET_LINE_CODING
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){ void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){
//MSG("linecoding_handler\n"); MSG("linecoding_handler\n");
} }
// SET_CONTROL_LINE_STATE // SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){ void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
//MSG("clstate_handler\n"); MSG("clstate_handler\n");
} }
// SEND_BREAK // SEND_BREAK
void WEAK break_handler(){ void WEAK break_handler(){
//MSG("break_handler\n"); MSG("break_handler\n");
} }
// handler of vendor requests // 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){ static void wr0(const uint8_t *buf, uint16_t size){
if(setup_packet.wLength < size) size = setup_packet.wLength; if(setup_packet.wLength < size) size = setup_packet.wLength;
EP_WriteIRQ(0, buf, size); EP_WriteIRQ(0, buf, size);
@ -227,7 +222,7 @@ static inline void get_descriptor(){
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]); wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
break; break;
default: default:
WRITEDUMP("UNK_DES");
break; break;
} }
} }
@ -243,11 +238,9 @@ static inline void std_d2h_req(){
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered
break; break;
case GET_CONFIGURATION: case GET_CONFIGURATION:
WRITEDUMP("GET_CONFIGURATION");
EP_WriteIRQ(0, &configuration, 1); EP_WriteIRQ(0, &configuration, 1);
break; break;
default: default:
WRITEDUMP("80:WR_REQ");
break; break;
} }
} }
@ -264,11 +257,12 @@ static inline void std_h2d_req(){
configuration = setup_packet.wValue; configuration = setup_packet.wValue;
break; break;
default: default:
WRITEDUMP("0:WR_REQ");
break; break;
} }
} }
extern volatile int8_t usbConn;
/* /*
bmRequestType: 76543210 bmRequestType: 76543210
7 direction: 0 - host->device, 1 - device->host 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); EP_WriteIRQ(0, (uint8_t *)0, 0);
epstatus = SET_NAK_RX(epstatus); epstatus = SET_NAK_RX(epstatus);
epstatus = SET_VALID_TX(epstatus); epstatus = SET_VALID_TX(epstatus);
}else{
WRITEDUMP("02:WR_REQ");
} }
break; break;
case VENDOR_REQUEST_TYPE: case VENDOR_REQUEST_TYPE:
@ -324,9 +316,10 @@ static uint16_t EP0_Handler(ep_t ep){
break; break;
case SEND_BREAK: case SEND_BREAK:
break_handler(); break_handler();
usbConn = 0;
break; break;
default: default:
WRITEDUMP("undef control req"); break;
} }
if(!dev2host) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement if(!dev2host) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
epstatus = SET_VALID_RX(epstatus); epstatus = SET_VALID_RX(epstatus);
@ -341,8 +334,8 @@ static uint16_t EP0_Handler(ep_t ep){
if(ep.rx_cnt){ if(ep.rx_cnt){
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
if(setup_packet.bRequest == SET_LINE_CODING){ if(setup_packet.bRequest == SET_LINE_CODING){
//WRITEDUMP("SET_LINE_CODING");
linecoding_handler((usb_LineCoding*)ep0databuf); linecoding_handler((usb_LineCoding*)ep0databuf);
usbConn = 1; // now we can transmit data: computer have stable connection
} }
} }
// Close transaction // Close transaction
@ -364,32 +357,8 @@ static uint16_t EP0_Handler(ep_t ep){
epstatus = SET_VALID_RX(epstatus); epstatus = SET_VALID_RX(epstatus);
epstatus = SET_VALID_TX(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; return epstatus;
} }
#undef WRITEDUMP
static uint16_t lastaddr = USB_EP0_BASEADDR; static uint16_t lastaddr = USB_EP0_BASEADDR;
/** /**

View File

@ -1,3 +1,3 @@
#define BUILD_NUMBER "48" #define BUILD_NUMBER "66"
#define BUILD_DATE "2023-08-29" #define BUILD_DATE "2023-09-11"
#define BUILDNO 48 #define BUILDNO 66