diff --git a/F0-nolib/3steppersLB/can.c b/F0-nolib/3steppersLB/can.c index d506da3..e52f899 100644 --- a/F0-nolib/3steppersLB/can.c +++ b/F0-nolib/3steppersLB/can.c @@ -203,7 +203,7 @@ CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id){ return CAN_BUSY; } #ifdef EBUG - DBG("Send data. Len="); printu(len); + DBG("Send data"); SEND("Len="); printu(len); SEND(", tagid="); printuhex(target_id); SEND(", data="); for(int i = 0; i < len; ++i){ @@ -254,6 +254,10 @@ static void can_process_fifo(uint8_t fifo_num){ // CAN_RDTxR: (16-31) - timestamp, (8-15) - filter match index, (0-3) - data length CAN_message msg; uint8_t *dat = msg.data; + { // set all data to 0 + uint32_t *dptr = (uint32_t*)msg.data; + dptr[0] = dptr[1] = 0; + } uint8_t len = box->RDTR & 0x0f; msg.length = len; msg.ID = box->RIR >> 21; @@ -287,7 +291,7 @@ static void can_process_fifo(uint8_t fifo_num){ dat[0] = lb & 0xff; } } - if(msg.ID == OUTPID) parseCANcommand(&msg); + if(msg.ID == the_conf.CANID) parseCANcommand(&msg); if(CAN_messagebuf_push(&msg)) return; // error: buffer is full, try later *RFxR |= CAN_RF0R_RFOM0; // release fifo for access to next message } @@ -313,14 +317,17 @@ TRUE_INLINE void parseCANcommand(CAN_message *msg){ int N = 1000; // we don't check msg here as it cannot be NULL #ifdef EBUG - SEND("Get data: "); + DBG("Get data"); for(int i = 0; i < msg->length; ++i){ printuhex(msg->data[i]); bufputchar(' '); } - NL(); + newline(); #endif if(msg->length == 0) goto sendmessage; // PING uint16_t Index = *(uint16_t*)msg->data; +#ifdef EBUG + SEND("Index = "); printu(Index); newline(); +#endif if(Index >= CMD_AMOUNT){ formerr(msg, ERR_BADCMD); goto sendmessage; @@ -338,12 +345,17 @@ TRUE_INLINE void parseCANcommand(CAN_message *msg){ formerr(msg, ERR_WRONGLEN); goto sendmessage; } +#ifdef EBUG + SEND("Run command\n"); +#endif errcodes ec = cmdlist[Index](par, val); if(ec != ERR_OK){ formerr(msg, ec); + }else{ + msg->length = 8; } sendmessage: - while(CAN_BUSY == can_send(msg->data, msg->length, OUTPID)) + while(CAN_BUSY == can_send(msg->data, msg->length, the_conf.CANID)) if(--N == 0) break; } diff --git a/F0-nolib/3steppersLB/can.h b/F0-nolib/3steppersLB/can.h index af9724e..ced8329 100644 --- a/F0-nolib/3steppersLB/can.h +++ b/F0-nolib/3steppersLB/can.h @@ -23,9 +23,6 @@ #include "flash.h" #include "hardware.h" -// output messages identifier -#define OUTPID (the_conf.CANID) - // CAN ID mask (11 bits) #define CANIDMASK (0x7ff) diff --git a/F0-nolib/3steppersLB/commonproto.h b/F0-nolib/3steppersLB/commonproto.h index efb5756..07e1c26 100644 --- a/F0-nolib/3steppersLB/commonproto.h +++ b/F0-nolib/3steppersLB/commonproto.h @@ -37,12 +37,12 @@ // error codes for answer message typedef enum{ - ERR_OK, // all OK - ERR_BADPAR, // parameter's value is wrong - ERR_BADVAL, // wrong parameter's value - ERR_WRONGLEN, // wrong message length - ERR_BADCMD, // unknown command - ERR_CANTRUN, // can't run given command due to bad parameters or other + ERR_OK, // 0 - all OK + ERR_BADPAR, // 1 - parameter's value is wrong + ERR_BADVAL, // 2 - wrong parameter's value + ERR_WRONGLEN, // 3 - wrong message length + ERR_BADCMD, // 4 - unknown command + ERR_CANTRUN, // 5 - can't run given command due to bad parameters or other } errcodes; // pointer to function for command execution, both should be non-NULL for common cases diff --git a/F0-nolib/3steppersLB/flash.h b/F0-nolib/3steppersLB/flash.h index a557426..5794a20 100644 --- a/F0-nolib/3steppersLB/flash.h +++ b/F0-nolib/3steppersLB/flash.h @@ -50,7 +50,7 @@ typedef struct{ uint8_t encreverse : 1; // bit1 - reversing encoder rotation TODO: configure encoder's timer to downcounting uint8_t haveencoder : 1; // bit2 - have encoder uint8_t donthold : 1; // bit3 - clear power @ stop (don't hold motor when stopped) - uint8_t eswinv : 1; // bit4 - invers end-switches + uint8_t eswinv : 1; // bit4 - inverse end-switches uint8_t keeppos : 1; // bit5 - keep current position (as servo motor) } motflags_t; diff --git a/F0-nolib/3steppersLB/main.c b/F0-nolib/3steppersLB/main.c index ee016ff..9ac0c20 100644 --- a/F0-nolib/3steppersLB/main.c +++ b/F0-nolib/3steppersLB/main.c @@ -77,7 +77,7 @@ int main(void){ flashstorage_init(); // should be called before any other functions gpio_setup(); USB_setup(); - CAN_setup(DEFAULT_CAN_SPEED); + CAN_setup(the_conf.CANspeed); adc_setup(); init_steppers(); RCC->CSR |= RCC_CSR_RMVF; // remove reset flags @@ -102,19 +102,17 @@ int main(void){ process_steppers(); IWDG->KR = IWDG_REFRESH; while((can_mesg = CAN_messagebuf_pop())){ - if(can_mesg && isgood(can_mesg->ID)){ - if(ShowMsgs){ // new data in buff - IWDG->KR = IWDG_REFRESH; - len = can_mesg->length; - printu(Tms); - SEND(" #"); - printuhex(can_mesg->ID); - for(ctr = 0; ctr < len; ++ctr){ - SEND(" "); - printuhex(can_mesg->data[ctr]); - } - newline(); sendbuf(); + if(can_mesg && ShowMsgs && isgood(can_mesg->ID)){ + IWDG->KR = IWDG_REFRESH; + len = can_mesg->length; + printu(Tms); + SEND(" #"); + printuhex(can_mesg->ID); + for(ctr = 0; ctr < len; ++ctr){ + SEND(" "); + printuhex(can_mesg->data[ctr]); } + newline(); sendbuf(); } } IWDG->KR = IWDG_REFRESH; diff --git a/F0-nolib/3steppersLB/steppers.bin b/F0-nolib/3steppersLB/steppers.bin index b5dbaae..0aaace9 100755 Binary files a/F0-nolib/3steppersLB/steppers.bin and b/F0-nolib/3steppersLB/steppers.bin differ diff --git a/F0-nolib/3steppersLB/strfunct.c b/F0-nolib/3steppersLB/strfunct.c index aa22dc4..5476950 100644 --- a/F0-nolib/3steppersLB/strfunct.c +++ b/F0-nolib/3steppersLB/strfunct.c @@ -356,6 +356,7 @@ void canid(char *txt){ int32_t N; if(eq != getnum(eq, &N) && N > -1 && N < 0xfff){ the_conf.CANID = (uint16_t)N; + CAN_reinit(the_conf.CANspeed); good = TRUE; } } @@ -396,27 +397,38 @@ void getcounter(_U_ char *txt){ void wdcheck(_U_ char *txt){ while(1){nop();} } -/* -void stp_check(char *txt){ - uint8_t N = *txt - '0'; - if(N < 3){ - MOTOR_EN(N); - MOTOR_CW(N); - mottimers[N]->ARR = 300; - mottimers[N]->CR1 |= TIM_CR1_CEN; - }else{ - for(N = 0; N < 3; ++N){ - MOTOR_DIS(N); - MOTOR_CCW(N); - mottimers[N]->CR1 &= ~TIM_CR1_CEN; - } - } -}*/ +typedef struct{ + errcodes code; + const char *descr; +} codetext; +static const codetext errtxt[] = { + {ERR_OK, "all OK"}, + {ERR_BADPAR, "wrong parameter's value"}, + {ERR_BADVAL, "wrong setter of parameter"}, + {ERR_WRONGLEN, "bad message length"}, + {ERR_BADCMD, "unknown command"}, + {ERR_CANTRUN, "temporary can't run given command"}, + {-1, NULL} +}; + +void dumperrcodes(_U_ char *txt){ + const codetext *c = errtxt; + SEND("Error codes:\n"); + while(c->descr){ + printu(c->code); + SEND(" - "); + SEND(c->descr); + newline(); + ++c; + } + sendbuf(); +} typedef void(*specfpointer)(char *arg); enum{ + SCMD_NONE, // omit zero SCMD_IGNORE, SCMD_DELIGNLIST, SCMD_DFU, @@ -431,16 +443,39 @@ enum{ SCMD_DUMPCONF, SCMD_GETCTR, SCMD_WD, + SCMD_DUMPERR, + SCMD_DUMPCMD, //SCMD_ST, SCMD_AMOUNT }; +void dumpcmdcodes(_U_ char *txt); + +static specfpointer speccmdlist[SCMD_AMOUNT] = { + [SCMD_IGNORE] = addIGN, + [SCMD_DELIGNLIST] = delignlist, + [SCMD_DFU] = bootldr, + [SCMD_FILTER] = add_filter, + [SCMD_CANSPEED] = CANini, + [SCMD_CANID] = canid, + [SCMD_LISTFILTERS] = list_filters, + [SCMD_IGNBUF] = print_ign_buf, + [SCMD_PAUSE] = inpause, + [SCMD_RESUME] = inresume, + [SCMD_SEND] = sendCANcommand, + [SCMD_DUMPCONF] = dump_userconf, + [SCMD_GETCTR] = getcounter, + [SCMD_WD] = wdcheck, + [SCMD_DUMPCMD] = dumpcmdcodes, + [SCMD_DUMPERR] = dumperrcodes, + //[SCMD_ST] = stp_check, +}; + typedef struct{ int cmd_code; // CMD_... or <0 for usb-only commands const char *command; // text command (up to 65536 commands) const char *help; // help message for text protocol } commands; - // the main commands list, index is CAN command code static const commands textcommands[] = { // different commands @@ -489,6 +524,8 @@ static const commands textcommands[] = { {-SCMD_CANSPEED, "canspeed", "CAN bus speed"}, {-SCMD_DELIGNLIST, "delignlist", "delete ignore list"}, {-SCMD_DFU, "dfu", "activate DFU mode"}, + {-SCMD_DUMPERR, "dumperr", "dump error codes"}, + {-SCMD_DUMPCMD, "dumpcmd", "dump command codes"}, {-SCMD_DUMPCONF, "dumpconf", "dump current configuration"}, {-SCMD_FILTER, "filter", "add/modify filter, format: bank# FIFO# mode(M/I) num0 [num1 [num2 [num3]]]"}, {-SCMD_GETCTR, "getctr", "get TIM1/2/3 counters"}, @@ -503,24 +540,39 @@ static const commands textcommands[] = { {0, NULL, NULL} }; +void dumpcmdcodes(_U_ char *txt){ + SEND("Commands list:\n"); + for(uint16_t i = 0; i < CMD_AMOUNT; ++i){ + printu(i); + SEND(" - "); + const commands *c = textcommands; + while(c->command){ + if(c->cmd_code == i && *c->command){ + SEND(c->help); break; + } + ++c; + } + newline(); + } + sendbuf(); +} -static specfpointer speccmdlist[SCMD_AMOUNT] = { - [SCMD_IGNORE] = addIGN, - [SCMD_DELIGNLIST] = delignlist, - [SCMD_DFU] = bootldr, - [SCMD_FILTER] = add_filter, - [SCMD_CANSPEED] = CANini, - [SCMD_CANID] = canid, - [SCMD_LISTFILTERS] = list_filters, - [SCMD_IGNBUF] = print_ign_buf, - [SCMD_PAUSE] = inpause, - [SCMD_RESUME] = inresume, - [SCMD_SEND] = sendCANcommand, - [SCMD_DUMPCONF] = dump_userconf, - [SCMD_GETCTR] = getcounter, - [SCMD_WD] = wdcheck, - //[SCMD_ST] = stp_check, -}; +/* +void stp_check(char *txt){ + uint8_t N = *txt - '0'; + if(N < 3){ + MOTOR_EN(N); + MOTOR_CW(N); + mottimers[N]->ARR = 300; + mottimers[N]->CR1 |= TIM_CR1_CEN; + }else{ + for(N = 0; N < 3; ++N){ + MOTOR_DIS(N); + MOTOR_CCW(N); + mottimers[N]->CR1 &= ~TIM_CR1_CEN; + } + } +}*/ static void showHelp(){ SEND("https://github.com/eddyem/stm32samples/tree/master/F0-nolib/3steppersLB build#" BUILD_NUMBER " @ " BUILD_DATE "\n"); diff --git a/F0-nolib/3steppersLB/version.inc b/F0-nolib/3steppersLB/version.inc index 3a5ed44..eb844db 100644 --- a/F0-nolib/3steppersLB/version.inc +++ b/F0-nolib/3steppersLB/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "129" -#define BUILD_DATE "2021-12-01" +#define BUILD_NUMBER "139" +#define BUILD_DATE "2021-12-02"