mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 18:55:13 +03:00
fix problems with CANbus, add USB-dump of error & command codes for CAN
This commit is contained in:
parent
b24111f884
commit
867abe359a
@ -203,7 +203,7 @@ CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id){
|
|||||||
return CAN_BUSY;
|
return CAN_BUSY;
|
||||||
}
|
}
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
DBG("Send data. Len="); printu(len);
|
DBG("Send data"); SEND("Len="); printu(len);
|
||||||
SEND(", tagid="); printuhex(target_id);
|
SEND(", tagid="); printuhex(target_id);
|
||||||
SEND(", data=");
|
SEND(", data=");
|
||||||
for(int i = 0; i < len; ++i){
|
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_RDTxR: (16-31) - timestamp, (8-15) - filter match index, (0-3) - data length
|
||||||
CAN_message msg;
|
CAN_message msg;
|
||||||
uint8_t *dat = msg.data;
|
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;
|
uint8_t len = box->RDTR & 0x0f;
|
||||||
msg.length = len;
|
msg.length = len;
|
||||||
msg.ID = box->RIR >> 21;
|
msg.ID = box->RIR >> 21;
|
||||||
@ -287,7 +291,7 @@ static void can_process_fifo(uint8_t fifo_num){
|
|||||||
dat[0] = lb & 0xff;
|
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
|
if(CAN_messagebuf_push(&msg)) return; // error: buffer is full, try later
|
||||||
*RFxR |= CAN_RF0R_RFOM0; // release fifo for access to next message
|
*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;
|
int N = 1000;
|
||||||
// we don't check msg here as it cannot be NULL
|
// we don't check msg here as it cannot be NULL
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
SEND("Get data: ");
|
DBG("Get data");
|
||||||
for(int i = 0; i < msg->length; ++i){
|
for(int i = 0; i < msg->length; ++i){
|
||||||
printuhex(msg->data[i]); bufputchar(' ');
|
printuhex(msg->data[i]); bufputchar(' ');
|
||||||
}
|
}
|
||||||
NL();
|
newline();
|
||||||
#endif
|
#endif
|
||||||
if(msg->length == 0) goto sendmessage; // PING
|
if(msg->length == 0) goto sendmessage; // PING
|
||||||
uint16_t Index = *(uint16_t*)msg->data;
|
uint16_t Index = *(uint16_t*)msg->data;
|
||||||
|
#ifdef EBUG
|
||||||
|
SEND("Index = "); printu(Index); newline();
|
||||||
|
#endif
|
||||||
if(Index >= CMD_AMOUNT){
|
if(Index >= CMD_AMOUNT){
|
||||||
formerr(msg, ERR_BADCMD);
|
formerr(msg, ERR_BADCMD);
|
||||||
goto sendmessage;
|
goto sendmessage;
|
||||||
@ -338,12 +345,17 @@ TRUE_INLINE void parseCANcommand(CAN_message *msg){
|
|||||||
formerr(msg, ERR_WRONGLEN);
|
formerr(msg, ERR_WRONGLEN);
|
||||||
goto sendmessage;
|
goto sendmessage;
|
||||||
}
|
}
|
||||||
|
#ifdef EBUG
|
||||||
|
SEND("Run command\n");
|
||||||
|
#endif
|
||||||
errcodes ec = cmdlist[Index](par, val);
|
errcodes ec = cmdlist[Index](par, val);
|
||||||
if(ec != ERR_OK){
|
if(ec != ERR_OK){
|
||||||
formerr(msg, ec);
|
formerr(msg, ec);
|
||||||
|
}else{
|
||||||
|
msg->length = 8;
|
||||||
}
|
}
|
||||||
sendmessage:
|
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;
|
if(--N == 0) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,9 +23,6 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
// output messages identifier
|
|
||||||
#define OUTPID (the_conf.CANID)
|
|
||||||
|
|
||||||
// CAN ID mask (11 bits)
|
// CAN ID mask (11 bits)
|
||||||
#define CANIDMASK (0x7ff)
|
#define CANIDMASK (0x7ff)
|
||||||
|
|
||||||
|
|||||||
@ -37,12 +37,12 @@
|
|||||||
|
|
||||||
// error codes for answer message
|
// error codes for answer message
|
||||||
typedef enum{
|
typedef enum{
|
||||||
ERR_OK, // all OK
|
ERR_OK, // 0 - all OK
|
||||||
ERR_BADPAR, // parameter's value is wrong
|
ERR_BADPAR, // 1 - parameter's value is wrong
|
||||||
ERR_BADVAL, // wrong parameter's value
|
ERR_BADVAL, // 2 - wrong parameter's value
|
||||||
ERR_WRONGLEN, // wrong message length
|
ERR_WRONGLEN, // 3 - wrong message length
|
||||||
ERR_BADCMD, // unknown command
|
ERR_BADCMD, // 4 - unknown command
|
||||||
ERR_CANTRUN, // can't run given command due to bad parameters or other
|
ERR_CANTRUN, // 5 - can't run given command due to bad parameters or other
|
||||||
} errcodes;
|
} errcodes;
|
||||||
|
|
||||||
// pointer to function for command execution, both should be non-NULL for common cases
|
// pointer to function for command execution, both should be non-NULL for common cases
|
||||||
|
|||||||
@ -50,7 +50,7 @@ typedef struct{
|
|||||||
uint8_t encreverse : 1; // bit1 - reversing encoder rotation TODO: configure encoder's timer to downcounting
|
uint8_t encreverse : 1; // bit1 - reversing encoder rotation TODO: configure encoder's timer to downcounting
|
||||||
uint8_t haveencoder : 1; // bit2 - have encoder
|
uint8_t haveencoder : 1; // bit2 - have encoder
|
||||||
uint8_t donthold : 1; // bit3 - clear power @ stop (don't hold motor when stopped)
|
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)
|
uint8_t keeppos : 1; // bit5 - keep current position (as servo motor)
|
||||||
} motflags_t;
|
} motflags_t;
|
||||||
|
|
||||||
|
|||||||
@ -77,7 +77,7 @@ int main(void){
|
|||||||
flashstorage_init(); // should be called before any other functions
|
flashstorage_init(); // should be called before any other functions
|
||||||
gpio_setup();
|
gpio_setup();
|
||||||
USB_setup();
|
USB_setup();
|
||||||
CAN_setup(DEFAULT_CAN_SPEED);
|
CAN_setup(the_conf.CANspeed);
|
||||||
adc_setup();
|
adc_setup();
|
||||||
init_steppers();
|
init_steppers();
|
||||||
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||||
@ -102,8 +102,7 @@ int main(void){
|
|||||||
process_steppers();
|
process_steppers();
|
||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
while((can_mesg = CAN_messagebuf_pop())){
|
while((can_mesg = CAN_messagebuf_pop())){
|
||||||
if(can_mesg && isgood(can_mesg->ID)){
|
if(can_mesg && ShowMsgs && isgood(can_mesg->ID)){
|
||||||
if(ShowMsgs){ // new data in buff
|
|
||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
len = can_mesg->length;
|
len = can_mesg->length;
|
||||||
printu(Tms);
|
printu(Tms);
|
||||||
@ -116,7 +115,6 @@ int main(void){
|
|||||||
newline(); sendbuf();
|
newline(); sendbuf();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
if((txt = get_USB())){
|
if((txt = get_USB())){
|
||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
|||||||
Binary file not shown.
@ -356,6 +356,7 @@ void canid(char *txt){
|
|||||||
int32_t N;
|
int32_t N;
|
||||||
if(eq != getnum(eq, &N) && N > -1 && N < 0xfff){
|
if(eq != getnum(eq, &N) && N > -1 && N < 0xfff){
|
||||||
the_conf.CANID = (uint16_t)N;
|
the_conf.CANID = (uint16_t)N;
|
||||||
|
CAN_reinit(the_conf.CANspeed);
|
||||||
good = TRUE;
|
good = TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -396,27 +397,38 @@ void getcounter(_U_ char *txt){
|
|||||||
void wdcheck(_U_ char *txt){
|
void wdcheck(_U_ char *txt){
|
||||||
while(1){nop();}
|
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);
|
typedef void(*specfpointer)(char *arg);
|
||||||
|
|
||||||
enum{
|
enum{
|
||||||
|
SCMD_NONE, // omit zero
|
||||||
SCMD_IGNORE,
|
SCMD_IGNORE,
|
||||||
SCMD_DELIGNLIST,
|
SCMD_DELIGNLIST,
|
||||||
SCMD_DFU,
|
SCMD_DFU,
|
||||||
@ -431,16 +443,39 @@ enum{
|
|||||||
SCMD_DUMPCONF,
|
SCMD_DUMPCONF,
|
||||||
SCMD_GETCTR,
|
SCMD_GETCTR,
|
||||||
SCMD_WD,
|
SCMD_WD,
|
||||||
|
SCMD_DUMPERR,
|
||||||
|
SCMD_DUMPCMD,
|
||||||
//SCMD_ST,
|
//SCMD_ST,
|
||||||
SCMD_AMOUNT
|
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{
|
typedef struct{
|
||||||
int cmd_code; // CMD_... or <0 for usb-only commands
|
int cmd_code; // CMD_... or <0 for usb-only commands
|
||||||
const char *command; // text command (up to 65536 commands)
|
const char *command; // text command (up to 65536 commands)
|
||||||
const char *help; // help message for text protocol
|
const char *help; // help message for text protocol
|
||||||
} commands;
|
} commands;
|
||||||
|
|
||||||
// the main commands list, index is CAN command code
|
// the main commands list, index is CAN command code
|
||||||
static const commands textcommands[] = {
|
static const commands textcommands[] = {
|
||||||
// different commands
|
// different commands
|
||||||
@ -489,6 +524,8 @@ static const commands textcommands[] = {
|
|||||||
{-SCMD_CANSPEED, "canspeed", "CAN bus speed"},
|
{-SCMD_CANSPEED, "canspeed", "CAN bus speed"},
|
||||||
{-SCMD_DELIGNLIST, "delignlist", "delete ignore list"},
|
{-SCMD_DELIGNLIST, "delignlist", "delete ignore list"},
|
||||||
{-SCMD_DFU, "dfu", "activate DFU mode"},
|
{-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_DUMPCONF, "dumpconf", "dump current configuration"},
|
||||||
{-SCMD_FILTER, "filter", "add/modify filter, format: bank# FIFO# mode(M/I) num0 [num1 [num2 [num3]]]"},
|
{-SCMD_FILTER, "filter", "add/modify filter, format: bank# FIFO# mode(M/I) num0 [num1 [num2 [num3]]]"},
|
||||||
{-SCMD_GETCTR, "getctr", "get TIM1/2/3 counters"},
|
{-SCMD_GETCTR, "getctr", "get TIM1/2/3 counters"},
|
||||||
@ -503,24 +540,39 @@ static const commands textcommands[] = {
|
|||||||
{0, NULL, NULL}
|
{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,
|
void stp_check(char *txt){
|
||||||
[SCMD_DELIGNLIST] = delignlist,
|
uint8_t N = *txt - '0';
|
||||||
[SCMD_DFU] = bootldr,
|
if(N < 3){
|
||||||
[SCMD_FILTER] = add_filter,
|
MOTOR_EN(N);
|
||||||
[SCMD_CANSPEED] = CANini,
|
MOTOR_CW(N);
|
||||||
[SCMD_CANID] = canid,
|
mottimers[N]->ARR = 300;
|
||||||
[SCMD_LISTFILTERS] = list_filters,
|
mottimers[N]->CR1 |= TIM_CR1_CEN;
|
||||||
[SCMD_IGNBUF] = print_ign_buf,
|
}else{
|
||||||
[SCMD_PAUSE] = inpause,
|
for(N = 0; N < 3; ++N){
|
||||||
[SCMD_RESUME] = inresume,
|
MOTOR_DIS(N);
|
||||||
[SCMD_SEND] = sendCANcommand,
|
MOTOR_CCW(N);
|
||||||
[SCMD_DUMPCONF] = dump_userconf,
|
mottimers[N]->CR1 &= ~TIM_CR1_CEN;
|
||||||
[SCMD_GETCTR] = getcounter,
|
}
|
||||||
[SCMD_WD] = wdcheck,
|
}
|
||||||
//[SCMD_ST] = stp_check,
|
}*/
|
||||||
};
|
|
||||||
|
|
||||||
static void showHelp(){
|
static void showHelp(){
|
||||||
SEND("https://github.com/eddyem/stm32samples/tree/master/F0-nolib/3steppersLB build#" BUILD_NUMBER " @ " BUILD_DATE "\n");
|
SEND("https://github.com/eddyem/stm32samples/tree/master/F0-nolib/3steppersLB build#" BUILD_NUMBER " @ " BUILD_DATE "\n");
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
#define BUILD_NUMBER "129"
|
#define BUILD_NUMBER "139"
|
||||||
#define BUILD_DATE "2021-12-01"
|
#define BUILD_DATE "2021-12-02"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user