mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 10:45:11 +03:00
add debug flag, remove one bug (echo to one interface in another's interrupt); still didn't found why there's a trash of output in input buffer of interface 0
This commit is contained in:
parent
233eed8b12
commit
962d181e89
Binary file not shown.
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!DOCTYPE QtCreatorProject>
|
||||||
<!-- Written by QtCreator 17.0.0, 2025-07-15T19:51:56. -->
|
<!-- Written by QtCreator 17.0.0, 2025-08-24T23:38:47. -->
|
||||||
<qtcreator>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>EnvironmentId</variable>
|
<variable>EnvironmentId</variable>
|
||||||
|
|||||||
@ -38,6 +38,7 @@ typedef struct{
|
|||||||
uint8_t CPHA : 1;
|
uint8_t CPHA : 1;
|
||||||
uint8_t BR : 3;
|
uint8_t BR : 3;
|
||||||
uint8_t monit: 1; // auto monitoring of encoder each `monittime` milliseconds
|
uint8_t monit: 1; // auto monitoring of encoder each `monittime` milliseconds
|
||||||
|
uint8_t debug: 1; // debug output of read data
|
||||||
} flags_t;
|
} flags_t;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -54,7 +54,7 @@ static void proc_enc(uint8_t idx){
|
|||||||
char ifacechr = idx ? 'Y' : 'X';
|
char ifacechr = idx ? 'Y' : 'X';
|
||||||
if(CDCready[iface]){
|
if(CDCready[iface]){
|
||||||
int l = USB_receivestr(iface, inbuff, RBINSZ);
|
int l = USB_receivestr(iface, inbuff, RBINSZ);
|
||||||
if(CDCready[I_CMD]){
|
if(CDCready[I_CMD] && the_conf.flags.debug){
|
||||||
if(l){
|
if(l){
|
||||||
CMDWR("Enc"); USB_putbyte(I_CMD, ifacechr);
|
CMDWR("Enc"); USB_putbyte(I_CMD, ifacechr);
|
||||||
CMDWR(": ");
|
CMDWR(": ");
|
||||||
@ -96,7 +96,7 @@ static void proc_enc(uint8_t idx){
|
|||||||
if(str) ++gotgood[idx];
|
if(str) ++gotgood[idx];
|
||||||
else ++gotwrong[idx];
|
else ++gotwrong[idx];
|
||||||
}
|
}
|
||||||
}else if(!the_conf.flags.monit){
|
}else if(!the_conf.flags.monit && the_conf.flags.debug){
|
||||||
printResult(&result);
|
printResult(&result);
|
||||||
CMDWR("ENC"); USB_putbyte(I_CMD, ifacechr);
|
CMDWR("ENC"); USB_putbyte(I_CMD, ifacechr);
|
||||||
USB_putbyte(I_CMD, '=');
|
USB_putbyte(I_CMD, '=');
|
||||||
@ -118,6 +118,7 @@ static void proc_enc(uint8_t idx){
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
uint32_t lastT = 0, usartT = 0;
|
uint32_t lastT = 0, usartT = 0;
|
||||||
|
uint8_t oldCDCready[bTotNumEndpoints] = {0};
|
||||||
StartHSE();
|
StartHSE();
|
||||||
flashstorage_init();
|
flashstorage_init();
|
||||||
hw_setup();
|
hw_setup();
|
||||||
@ -139,6 +140,18 @@ int main(){
|
|||||||
int l = USB_receivestr(I_CMD, inbuff, RBINSZ);
|
int l = USB_receivestr(I_CMD, inbuff, RBINSZ);
|
||||||
if(l < 0) CMDWRn("ERROR: CMD USB buffer overflow or string was too long");
|
if(l < 0) CMDWRn("ERROR: CMD USB buffer overflow or string was too long");
|
||||||
else if(l) parse_cmd(inbuff);
|
else if(l) parse_cmd(inbuff);
|
||||||
|
// check if interface connected/disconnected
|
||||||
|
// (we CAN'T do much debug output in interrupt functions like linecoding_handler etc, so do it here)
|
||||||
|
for(int i = 0; i < bTotNumEndpoints; ++i){
|
||||||
|
if(oldCDCready[i] != CDCready[i]){
|
||||||
|
CMDWR("Interface ");
|
||||||
|
CMDWR(u2str(i));
|
||||||
|
USB_putbyte(I_CMD, ' ');
|
||||||
|
if(CDCready[i] == 0) CMDWR("dis");
|
||||||
|
CMDWRn("connected");
|
||||||
|
oldCDCready[i] = CDCready[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
proc_enc(0);
|
proc_enc(0);
|
||||||
proc_enc(1);
|
proc_enc(1);
|
||||||
|
|||||||
@ -80,6 +80,7 @@ typedef enum{
|
|||||||
C_amperiod,
|
C_amperiod,
|
||||||
C_usart,
|
C_usart,
|
||||||
C_ssii,
|
C_ssii,
|
||||||
|
C_debug,
|
||||||
C_AMOUNT
|
C_AMOUNT
|
||||||
} cmd_e;
|
} cmd_e;
|
||||||
|
|
||||||
@ -268,8 +269,6 @@ static errcode_e setuintpar(cmd_e idx, char *par){
|
|||||||
return ERR_BADCMD;
|
return ERR_BADCMD;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CMDWR(commands[idx].cmd);
|
|
||||||
USB_putbyte(I_CMD, '=');
|
|
||||||
switch(idx){
|
switch(idx){
|
||||||
case C_br:
|
case C_br:
|
||||||
val = the_conf.flags.BR;
|
val = the_conf.flags.BR;
|
||||||
@ -295,6 +294,8 @@ static errcode_e setuintpar(cmd_e idx, char *par){
|
|||||||
default:
|
default:
|
||||||
return ERR_BADCMD;
|
return ERR_BADCMD;
|
||||||
}
|
}
|
||||||
|
CMDWR(commands[idx].cmd);
|
||||||
|
USB_putbyte(I_CMD, '=');
|
||||||
CMDWR(u2str(val));
|
CMDWR(u2str(val));
|
||||||
CMDn();
|
CMDn();
|
||||||
return ERR_SILENCE;
|
return ERR_SILENCE;
|
||||||
@ -323,6 +324,9 @@ static errcode_e setboolpar(cmd_e idx, char *par){
|
|||||||
case C_autom:
|
case C_autom:
|
||||||
the_conf.flags.monit = val;
|
the_conf.flags.monit = val;
|
||||||
break;
|
break;
|
||||||
|
case C_debug:
|
||||||
|
the_conf.flags.debug = val;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return ERR_BADCMD;
|
return ERR_BADCMD;
|
||||||
}
|
}
|
||||||
@ -343,6 +347,9 @@ static errcode_e setboolpar(cmd_e idx, char *par){
|
|||||||
case C_autom:
|
case C_autom:
|
||||||
val = the_conf.flags.monit;
|
val = the_conf.flags.monit;
|
||||||
break;
|
break;
|
||||||
|
case C_debug:
|
||||||
|
val = the_conf.flags.debug;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return ERR_BADCMD;
|
return ERR_BADCMD;
|
||||||
}
|
}
|
||||||
@ -364,6 +371,7 @@ static errcode_e dumpconf(cmd_e _U_ idx, char _U_ *par){
|
|||||||
setuintpar(C_br, NULL);
|
setuintpar(C_br, NULL);
|
||||||
setboolpar(C_cpha, NULL);
|
setboolpar(C_cpha, NULL);
|
||||||
setboolpar(C_cpol, NULL);
|
setboolpar(C_cpol, NULL);
|
||||||
|
setboolpar(C_debug, NULL);
|
||||||
setuintpar(C_encbits, NULL);
|
setuintpar(C_encbits, NULL);
|
||||||
setuintpar(C_encbufsz, NULL);
|
setuintpar(C_encbufsz, NULL);
|
||||||
setuintpar(C_maxzeros, NULL);
|
setuintpar(C_maxzeros, NULL);
|
||||||
@ -410,6 +418,7 @@ static const funcdescr_t commands[C_AMOUNT] = {
|
|||||||
[C_amperiod] = {"amperiod", setuintpar},
|
[C_amperiod] = {"amperiod", setuintpar},
|
||||||
[C_usart] = {"usart", usart},
|
[C_usart] = {"usart", usart},
|
||||||
[C_ssii] = {"ssii", setuintpar},
|
[C_ssii] = {"ssii", setuintpar},
|
||||||
|
[C_debug] = {"debug", setboolpar},
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
@ -434,6 +443,7 @@ static const help_t helpmessages[] = {
|
|||||||
{C_br, "change SPI BR register (1 - 18MHz ... 7 - 281kHz)"},
|
{C_br, "change SPI BR register (1 - 18MHz ... 7 - 281kHz)"},
|
||||||
{C_cpha, "change CPHA value (0/1)"},
|
{C_cpha, "change CPHA value (0/1)"},
|
||||||
{C_cpol, "change CPOL value (0/1)"},
|
{C_cpol, "change CPOL value (0/1)"},
|
||||||
|
{C_debug, "turn on debugging output of read encoders' values"},
|
||||||
{C_dumpconf, "dump current configuration"},
|
{C_dumpconf, "dump current configuration"},
|
||||||
{C_encbits, "set encoder data bits amount (26/32)"},
|
{C_encbits, "set encoder data bits amount (26/32)"},
|
||||||
{C_encbufsz, "change encoder auxiliary buffer size (8..32 bytes)"},
|
{C_encbufsz, "change encoder auxiliary buffer size (8..32 bytes)"},
|
||||||
@ -486,6 +496,7 @@ static errcode_e help(_U_ cmd_e idx, _U_ char* par){
|
|||||||
void parse_cmd(char *cmd){
|
void parse_cmd(char *cmd){
|
||||||
errcode_e ecode = ERR_BADCMD;
|
errcode_e ecode = ERR_BADCMD;
|
||||||
// command and its parameter
|
// command and its parameter
|
||||||
|
CMDWRn(cmd);
|
||||||
char *cmdstart = omit_spaces(cmd), *parstart = NULL;
|
char *cmdstart = omit_spaces(cmd), *parstart = NULL;
|
||||||
if(!cmdstart) goto retn;
|
if(!cmdstart) goto retn;
|
||||||
char *ptr = cmdstart;
|
char *ptr = cmdstart;
|
||||||
@ -506,5 +517,9 @@ void parse_cmd(char *cmd){
|
|||||||
if(idx >= C_AMOUNT) goto retn; // not found
|
if(idx >= C_AMOUNT) goto retn; // not found
|
||||||
ecode = commands[idx].handler(idx, parstart);
|
ecode = commands[idx].handler(idx, parstart);
|
||||||
retn:
|
retn:
|
||||||
if(ecode != ERR_SILENCE) CMDWRn(errors[ecode]);
|
if(ecode == ERR_BADCMD){
|
||||||
|
CMDWR(cmd);
|
||||||
|
USB_putbyte(I_CMD, '=');
|
||||||
|
CMDWRn(errors[ERR_BADCMD]);
|
||||||
|
} else if(ecode != ERR_SILENCE) CMDWRn(errors[ecode]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,6 +38,8 @@
|
|||||||
#define CONTROL_DTR 0x01
|
#define CONTROL_DTR 0x01
|
||||||
#define CONTROL_RTS 0x02
|
#define CONTROL_RTS 0x02
|
||||||
|
|
||||||
|
// It's good to use debug here ONLY to debug into USART!
|
||||||
|
// never try to debug USB into USB!!!
|
||||||
#undef DBG
|
#undef DBG
|
||||||
#define DBG(x)
|
#define DBG(x)
|
||||||
#undef DBGs
|
#undef DBGs
|
||||||
@ -141,8 +143,17 @@ void WEAK linecoding_handler(uint8_t ifno, usb_LineCoding *lc){
|
|||||||
lineCoding[ifno] = *lc;
|
lineCoding[ifno] = *lc;
|
||||||
DBG("linecoding_handler");
|
DBG("linecoding_handler");
|
||||||
DBGs(uhex2str(ifno));
|
DBGs(uhex2str(ifno));
|
||||||
CMDWR("Interface "); CMDWR(u2str(ifno));
|
}
|
||||||
CMDWR(" got linecoding with speed "); CMDWRn(u2str(lc->dwDTERate));
|
|
||||||
|
static void clearbufs(uint8_t ifno){
|
||||||
|
uint32_t T0 = Tms;
|
||||||
|
while(Tms - T0 < 10){ // wait no more than 10ms
|
||||||
|
if(1 == RB_clearbuf((ringbuffer*)&rbin[ifno])) break;
|
||||||
|
}
|
||||||
|
T0 = Tms;
|
||||||
|
while(Tms - T0 < 10){
|
||||||
|
if(1 == RB_clearbuf((ringbuffer*)&rbout[ifno])) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET_CONTROL_LINE_STATE
|
// SET_CONTROL_LINE_STATE
|
||||||
@ -152,19 +163,14 @@ void WEAK clstate_handler(uint8_t ifno, uint16_t val){
|
|||||||
DBGs(uhex2str(val));
|
DBGs(uhex2str(val));
|
||||||
CDCready[ifno] = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected
|
CDCready[ifno] = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected
|
||||||
lastdsz[ifno] = -1;
|
lastdsz[ifno] = -1;
|
||||||
if(val == 0) CMDWR("dis");
|
if(val) clearbufs(ifno);
|
||||||
CMDWR("connected interface ");
|
|
||||||
CMDWRn(u2str(ifno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SEND_BREAK
|
// SEND_BREAK - disconnect interface and clear its buffers
|
||||||
void WEAK break_handler(uint8_t ifno){
|
void WEAK break_handler(uint8_t ifno){
|
||||||
CDCready[ifno] = 0;
|
CDCready[ifno] = 0;
|
||||||
lastdsz[ifno] = -1;
|
|
||||||
DBG("break_handler()");
|
DBG("break_handler()");
|
||||||
DBGs(uhex2str(ifno));
|
DBGs(uhex2str(ifno));
|
||||||
CMDWR("Disconnected interface ");
|
|
||||||
CMDWRn(u2str(ifno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -297,7 +303,7 @@ int USB_receive(uint8_t ifno, uint8_t *buf, int len){
|
|||||||
if(bufovrfl[ifno]){
|
if(bufovrfl[ifno]){
|
||||||
DBG("Buffer overflow");
|
DBG("Buffer overflow");
|
||||||
DBGs(uhex2str(ifno));
|
DBGs(uhex2str(ifno));
|
||||||
while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno]));
|
while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno])); // run watchdog in case of problems
|
||||||
bufovrfl[ifno] = 0;
|
bufovrfl[ifno] = 0;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
#define BUILD_NUMBER "111"
|
#define BUILD_NUMBER "126"
|
||||||
#define BUILD_DATE "2025-07-15"
|
#define BUILD_DATE "2025-08-24"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user