From cdef16cad15e18b6de50c22d1e5ef77716bb0b89 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Mon, 13 Jul 2026 16:55:49 +0300 Subject: [PATCH] some minor bugs fixed --- F3:F303/Multistepper/adc.c | 28 +------ F3:F303/Multistepper/buttons.c | 11 +-- F3:F303/Multistepper/can.c | 73 ++----------------- F3:F303/Multistepper/commonproto.c | 35 +++++---- F3:F303/Multistepper/esprif.conf | 6 +- F3:F303/Multistepper/flash.c | 25 +++---- F3:F303/Multistepper/main.c | 7 -- .../Multistepper/multistepper.creator.user | 35 ++------- F3:F303/Multistepper/pdnuart.c | 26 ------- F3:F303/Multistepper/proto.c | 15 ---- F3:F303/Multistepper/steppers.c | 66 ++++++++++++----- F3:F303/Multistepper/steppers.h | 2 + F3:F303/Multistepper/version.inc | 4 +- 13 files changed, 105 insertions(+), 228 deletions(-) diff --git a/F3:F303/Multistepper/adc.c b/F3:F303/Multistepper/adc.c index 956b85f..0aec0d3 100644 --- a/F3:F303/Multistepper/adc.c +++ b/F3:F303/Multistepper/adc.c @@ -17,9 +17,6 @@ */ #include "adc.h" -#ifdef EBUG -#include "proto.h" -#endif /** * @brief ADCx_array - arrays for ADC channels with median filtering: @@ -126,39 +123,23 @@ uint16_t getADCval(int nch){ PIX_SORT(p[4], p[2]) ; #undef PIX_SORT #undef PIX_SWAP -/* -#ifdef EBUG - DBG("val: "); printu(p[4]); newline(); -#endif -*/ return p[4]; } // get voltage @input nch (V) *1000V int32_t getADCvoltage(int nch){ - float v = getADCval(nch) * 3.3; - v /= 4.096f; // 12bit ADC -/* -#ifdef EBUG - DBG("v="); printf(v); newline(); -#endif -*/ + float v = getADCval(nch) * getVdd(); + v /= 4096.f; // 12bit ADC return (uint32_t) v; } // return MCU temperature (*1000 degrees of celsius) int32_t getMCUtemp(){ - // make correction on Vdd value int32_t ADval = getADCval(ADC_TS); float temperature = (float) *TEMP30_CAL_ADDR - ADval; temperature *= (110.f - 30.f); temperature /= (float)(*TEMP30_CAL_ADDR - *TEMP110_CAL_ADDR); temperature += 30.f; -/* -#ifdef EBUG - DBG("t="); printf(temperature); newline(); -#endif -*/ return (uint32_t) (temperature*1000.f); } @@ -166,10 +147,5 @@ int32_t getMCUtemp(){ int32_t getVdd(){ float vdd = ((float) *VREFINT_CAL_ADDR) * 3.3f; // 3.3V vdd /= getADCval(ADC_VREF); -/* -#ifdef EBUG - DBG("vdd="); printf(vdd); newline(); -#endif -*/ return (uint32_t) (vdd * 1000.f); } diff --git a/F3:F303/Multistepper/buttons.c b/F3:F303/Multistepper/buttons.c index 8544978..d96cd2c 100644 --- a/F3:F303/Multistepper/buttons.c +++ b/F3:F303/Multistepper/buttons.c @@ -31,9 +31,10 @@ uint32_t lastUnsleep = 0; // last keys activity time void process_keys(){ static uint32_t lastT = 0; - if(Tms == lastT) return; - uint16_t d = (uint16_t)(Tms - lastT); - lastT = Tms; + uint32_t TmsNow = Tms; // make local copy to prevent variable changing + if(TmsNow == lastT) return; + uint16_t d = (uint16_t)(TmsNow - lastT); + lastT = TmsNow; for(int i = 0; i < BTNSNO; ++i){ keybase *k = &allkeys[i]; keyevent e = k->event; @@ -62,8 +63,8 @@ void process_keys(){ } } if(e != k->event){ - k->lastTms = Tms; - lastUnsleep = Tms; + k->lastTms = TmsNow; + lastUnsleep = TmsNow; } } } diff --git a/F3:F303/Multistepper/can.c b/F3:F303/Multistepper/can.c index 5a1a54d..c01c29b 100644 --- a/F3:F303/Multistepper/can.c +++ b/F3:F303/Multistepper/can.c @@ -37,7 +37,6 @@ uint32_t floodT = FLOOD_PERIOD_MS; // flood period in ms static uint8_t incrflood = 0; // ==1 for incremental flooding static uint32_t incrmessagectr = 0; // counter for incremental flooding -static uint32_t last_err_code = 0; static CAN_status can_status = CAN_STOP; static void can_process_fifo(uint8_t fifo_num); @@ -54,17 +53,8 @@ CAN_status CAN_get_status(){ // push next message into buffer; return 1 if buffer overfull static int CAN_messagebuf_push(CAN_message *msg){ //MSG("Try to push\n"); -#ifdef EBUG - USB_sendstr("push: "); - for(int i = 0; i < msg->length; ++i){ - printuhex(msg->data[i]); USB_putbyte(' '); - } - newline(); -#endif + if(first_free_idx == first_nonfree_idx){ -#ifdef EBUG - USB_sendstr("INBUF OVERFULL\n"); -#endif return 1; // no free space } if(first_nonfree_idx < 0) first_nonfree_idx = 0; // first message in empty buffer @@ -77,22 +67,12 @@ static int CAN_messagebuf_push(CAN_message *msg){ // pop message from buffer CAN_message *CAN_messagebuf_pop(){ if(first_nonfree_idx < 0) return NULL; - #ifdef EBUG - //MSG("read from idx "); printu(first_nonfree_idx); NL(); - #endif CAN_message *msg = &messages[first_nonfree_idx++]; if(first_nonfree_idx == CAN_INMESSAGE_SIZE) first_nonfree_idx = 0; if(first_nonfree_idx == first_free_idx){ // buffer is empty - refresh it first_nonfree_idx = -1; first_free_idx = 0; } -#ifdef EBUG - USB_sendstr("pop: "); - for(int i = 0; i < msg->length; ++i){ - printuhex(msg->data[i]); USB_putbyte(' '); - } - newline(); -#endif return msg; } @@ -150,7 +130,7 @@ void CAN_setup(uint16_t speed){ CAN->MCR |= CAN_MCR_INRQ; /* (1) */ while((CAN->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) /* (2) */ if(--tmout == 0) break; - if(tmout==0){ DBG("timeout!\n");} + if(tmout==0){ DBG("Init mode entering timeout!\n");} CAN->MCR &=~ CAN_MCR_SLEEP; /* (3) */ CAN->MCR |= CAN_MCR_ABOM; /* allow automatically bus-off */ @@ -159,7 +139,7 @@ void CAN_setup(uint16_t speed){ tmout = 10000; while(CAN->MSR & CAN_MSR_INAK) /* (6) */ if(--tmout == 0) break; - if(tmout==0){ DBG("timeout!\n");} + if(tmout==0){ DBG("Init mode exiting timeout!\n");} // accept ALL CAN->FMR = CAN_FMR_FINIT; /* (7) */ CAN->FA1R = CAN_FA1R_FACT0 | CAN_FA1R_FACT1; /* (8) */ @@ -182,7 +162,7 @@ void CAN_setup(uint16_t speed){ } void CAN_printerr(){ - if(!last_err_code) last_err_code = CAN->ESR; + uint32_t last_err_code = CAN->ESR; if(!last_err_code){ USB_sendstr("No errors\n"); return; @@ -207,19 +187,10 @@ void CAN_printerr(){ if(last_err_code & CAN_ESR_BOFF) USB_sendstr("Bus off "); if(last_err_code & CAN_ESR_EPVF) USB_sendstr("Passive error limit "); if(last_err_code & CAN_ESR_EWGF) USB_sendstr("Error counter limit"); - last_err_code = 0; USB_putbyte('\n'); } void CAN_proc(){ -#ifdef EBUG - if(last_err_code){ - USB_sendstr("Error, ESR="); - USB_sendstr(u2str(last_err_code)); - USB_putbyte('\n'); - last_err_code = 0; - } -#endif // check for messages in FIFO0 & FIFO1 if(CAN->RF0R & CAN_RF0R_FMP0){ can_process_fifo(0); @@ -254,20 +225,9 @@ CAN_status CAN_send(uint8_t *msg, uint8_t len, uint16_t target_id){ if(CAN->TSR & (CAN_TSR_TME)){ mailbox = (CAN->TSR & CAN_TSR_CODE) >> 24; }else{ // no free mailboxes -#ifdef EBUG - USB_sendstr("No free mailboxes\n"); -#endif return CAN_BUSY; } -#ifdef EBUG - USB_sendstr("Send data. Len="); USB_sendstr(u2str(len)); - USB_sendstr(", tagid="); USB_sendstr(u2str(target_id)); - USB_sendstr(", data="); - for(int i = 0; i < len; ++i){ - USB_sendstr(" "); USB_sendstr(uhex2str(msg[i])); - } - USB_putbyte('\n'); -#endif + CAN_TxMailBox_TypeDef *box = &CAN->sTxMailBox[mailbox]; uint32_t lb = 0, hb = 0; switch(len){ @@ -339,19 +299,9 @@ TRUE_INLINE void formerr(CAN_message *msg, errcodes err){ */ TRUE_INLINE void parseCANcommand(CAN_message *msg){ // we don't check msg here as it cannot be NULL -#ifdef EBUG - DBG("Get data: "); - for(int i = 0; i < msg->length; ++i){ - USB_sendstr(uhex2str(msg->data[i])); USB_putbyte(' '); - } - for(int i = msg->length-1; i < 8; ++i) msg->data[i] = 0; - newline(); -#endif if(msg->length == 0) goto sendmessage; // PING uint16_t Index = *(uint16_t*)msg->data; -#ifdef EBUG - USB_sendstr("Index = "); USB_sendstr(u2str(Index)); newline(); -#endif + if(Index >= CCMD_AMOUNT || !cancmdlist[Index]){ formerr(msg, ERR_BADCMD); goto sendmessage; @@ -369,9 +319,7 @@ TRUE_INLINE void parseCANcommand(CAN_message *msg){ formerr(msg, ERR_WRONGLEN); goto sendmessage; } -#ifdef EBUG - USB_sendstr("Run command\n"); -#endif + errcodes ec = cancmdlist[Index](par, val); if(ec != ERR_OK){ formerr(msg, ec); @@ -387,10 +335,6 @@ static void can_process_fifo(uint8_t fifo_num){ if(fifo_num > 1) return; CAN_FIFOMailBox_TypeDef *box = &CAN->sFIFOMailBox[fifo_num]; volatile uint32_t *RFxR = (fifo_num) ? &CAN->RF1R : &CAN->RF0R; -#ifdef EBUG - USB_sendstr(u2str(*RFxR & CAN_RF0R_FMP0)); USB_sendstr(" messages in FIFO #"); - USB_sendstr(u2str(fifo_num)); newline(); -#endif // read all while(*RFxR & CAN_RF0R_FMP0){ // amount of messages pending // CAN_RDTxR: (16-31) - timestamp, (8-15) - filter match index, (0-3) - data length @@ -456,9 +400,6 @@ void can1_rx1_isr(){ // Rx FIFO1 (overrun) void can1_sce_isr(){ // status changed if(CAN->MSR & CAN_MSR_ERRI){ // Error -#ifdef EBUG - last_err_code = CAN->ESR; -#endif CAN->MSR = CAN_MSR_ERRI; // clear flag // request abort for problem mailbox if(CAN->TSR & CAN_TSR_TERR0) CAN->TSR |= CAN_TSR_ABRQ0; diff --git a/F3:F303/Multistepper/commonproto.c b/F3:F303/Multistepper/commonproto.c index 5e1e756..0b3942e 100644 --- a/F3:F303/Multistepper/commonproto.c +++ b/F3:F303/Multistepper/commonproto.c @@ -55,6 +55,7 @@ errcodes cu_accel(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); errcodes ret = ERR_OK; if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; if(*val/the_conf.microsteps[n] > ACCELMAXSTEPS || *val < 1) return ERR_BADVAL; uint16_t acc = the_conf.accel[n]; the_conf.accel[n] = *val; @@ -113,7 +114,7 @@ errcodes cu_drvtype(uint8_t par, int32_t *val){ uint8_t n; CHECKN(n, par); motflags_t *fl = &the_conf.motflags[n]; if(ISSETTER(par)){ - if(*val >= DRVTYPE_AMOUNT) return ERR_BADVAL; + if(*val >= DRVTYPE_AMOUNT || *val < 0) return ERR_BADVAL; fl->drvtype = *val; } *val = fl->drvtype; @@ -134,6 +135,7 @@ errcodes cu_emstop(uint8_t par, int32_t _U_ *val){ errcodes cu_eraseflash(uint8_t _U_ par, int32_t _U_ *val){ NOPARCHK(par); + if(isanymoving()) return ERR_CANTRUN; if(ISSETTER(par)){ if(erase_storage(*val)) return ERR_BADVAL; }else if(erase_storage(-1)) return ERR_CANTRUN; @@ -151,6 +153,7 @@ errcodes cu_eswreact(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); errcodes ret = ERR_OK; if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; if(*val < 0 || *val > ESW_AMOUNT-1) return ERR_BADVAL; uint8_t react = the_conf.ESW_reaction[n]; the_conf.ESW_reaction[n] = *val; @@ -192,14 +195,7 @@ errcodes cu_gpio(uint8_t par, int32_t *val){ #error "change the code!!!" #endif uint8_t n = PARBASE(par); -#ifdef EBUG - USND("par="); printu(par); - USND(", n="); USB_putbyte('0'+n); newline(); -#endif if(n == CANMESG_NOPAR){ // all -#ifdef EBUG - USND("ALL\n"); -#endif uint8_t g = (uint8_t)*val; if(ISSETTER(par)){ for(int i = 0; i < EXTNO; ++i){ @@ -241,6 +237,7 @@ errcodes cu_maxspeed(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); errcodes ret = ERR_OK; if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; if(*val <= the_conf.minspd[n]) return ERR_BADVAL; uint16_t maxspd = the_conf.maxspd[n]; the_conf.maxspd[n] = getSPD(n, *val); @@ -256,6 +253,7 @@ errcodes cu_maxspeed(uint8_t _U_ par, int32_t _U_ *val){ errcodes cu_maxsteps(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; if(*val < 1) return ERR_BADVAL; the_conf.maxsteps[n] = *val; } @@ -279,6 +277,7 @@ errcodes cu_mcuvdd(uint8_t par, int32_t *val){ errcodes cu_microsteps(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; #if MICROSTEPSMAX > 512 #error "Change the code anywhere!" #endif @@ -305,6 +304,7 @@ errcodes cu_minspeed(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); errcodes ret = ERR_OK; if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; if(*val >= the_conf.maxspd[n] || *val < 0) return ERR_BADVAL; uint16_t minspd = the_conf.minspd[n]; the_conf.minspd[n] = getSPD(n, *val); @@ -320,6 +320,7 @@ errcodes cu_minspeed(uint8_t _U_ par, int32_t _U_ *val){ errcodes cu_motcurrent(uint8_t par, int32_t *val){ uint8_t n; CHECKN(n, par); if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; if(*val < 0 || *val > 31) return ERR_BADVAL; the_conf.motcurrent[n] = *val; motflags_t *f = the_conf.motflags; @@ -335,6 +336,7 @@ errcodes cu_motflags(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n; CHECKN(n, par); errcodes ret = ERR_OK; if(ISSETTER(par)){ + if(ismoving(n)) return ERR_CANTRUN; motflags_t flags = the_conf.motflags[n]; the_conf.motflags[n] = *((motflags_t*)val); if(!update_stepper(n)){ @@ -347,9 +349,10 @@ errcodes cu_motflags(uint8_t _U_ par, int32_t _U_ *val){ } errcodes cu_motno(uint8_t _U_ par, int32_t _U_ *val){ - if(*val < 0 || *val >= MOTORSNO) return ERR_BADVAL; + uint8_t n; CHECKN(n, par); if(ISSETTER(par)){ - if(!pdnuart_setmotno(*val)) return ERR_CANTRUN; + if(ismoving(n)) return ERR_CANTRUN; + if(!pdnuart_setmotno(n)) return ERR_CANTRUN; } *val = pdnuart_getmotno(); return ERR_OK; @@ -362,16 +365,20 @@ errcodes cu_motmul(uint8_t _U_ par, int32_t _U_ *val){ // witout parameter - reinit all steppers; with parameter - just update current errcodes cu_motreinit(uint8_t _U_ par, int32_t _U_ *val){ uint8_t n = PARBASE(par); - if(n == CANMESG_NOPAR) init_steppers(); - else{ + if(n == CANMESG_NOPAR){ + // check that neither motor is moving + if(isanymoving()) return ERR_CANTRUN; + init_steppers(); + }else{ if(n > MOTORSNO - 1) return ERR_BADPAR; - if(!update_stepper(n)) return ERR_CANTRUN; + if(ismoving(n) || !update_stepper(n)) return ERR_CANTRUN; } return ERR_OK; } errcodes cu_pdn(uint8_t par, int32_t *val){ - uint8_t n = PARBASE(par); + uint8_t n; CHECKN(n, par); + if(ismoving(n)) return ERR_CANTRUN; if(ISSETTER(par)){ if(!pdnuart_writereg(n, *val)) return ERR_CANTRUN; } diff --git a/F3:F303/Multistepper/esprif.conf b/F3:F303/Multistepper/esprif.conf index 661e342..47a9909 100644 --- a/F3:F303/Multistepper/esprif.conf +++ b/F3:F303/Multistepper/esprif.conf @@ -34,11 +34,11 @@ motflags3=0x78 eswreact3=0 microsteps4=32 accel4=2000 -maxspeed4=2000 +maxspeed4=1000 minspeed4=20 -maxsteps4=500000 +maxsteps4=40000 motcurrent4=31 -motflags4=0x68 +motflags4=0x78 eswreact4=0 microsteps5=32 accel5=500 diff --git a/F3:F303/Multistepper/flash.c b/F3:F303/Multistepper/flash.c index 22f2f5d..9f29ff2 100644 --- a/F3:F303/Multistepper/flash.c +++ b/F3:F303/Multistepper/flash.c @@ -106,11 +106,13 @@ void flashstorage_init(){ int store_userconf(){ // maxnum - 3 means that there always should be at least one empty record after last data // for binarySearch() checking that there's nothing more after it! - if(currentconfidx > (int)maxCnum - 3){ // there's no more place - currentconfidx = 0; + int curidx = currentconfidx; + if(curidx > (int)maxCnum - 3){ // there's no more place + curidx = 0; if(erase_storage(-1)) return 1; - }else ++currentconfidx; // take next data position (0 - within first run after firmware flashing) - return write2flash((const void*)&Flash_Data[currentconfidx], &the_conf, sizeof(the_conf)); + }else ++curidx; // take next data position (0 - within first run after firmware flashing) + int r = write2flash((const void*)&Flash_Data[curidx], &the_conf, sizeof(the_conf)); + if(0 == r) currentconfidx = curidx; // refresh counter only if succeed } static int write2flash(const void *start, const void *wrdata, uint32_t stor_size){ @@ -135,9 +137,6 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size ret = 1; break; } -#ifdef EBUG - else{ USB_sendstr("Written "); printuhex(data[i]); newline();} -#endif if(FLASH->SR & FLASH_SR_PGERR){ USB_sendstr("Prog err\n"); ret = 1; // program error - meet not 0xffff @@ -152,9 +151,7 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size // erase Nth page of flash storage (flash should be prepared!) static int erase_pageN(int N){ int ret = 0; -//#ifdef EBUG USB_sendstr("Erase block #"); printu(N); newline(); -//#endif FLASH->AR = (uint32_t)Flash_Data + N*FLASH_blocksize; FLASH->CR |= FLASH_CR_STRT; while(FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; @@ -233,13 +230,11 @@ int fn_dumpmot(uint32_t _U_ hash, char _U_ *args){ // "dumpmot" (1224122507) } int fn_dumpconf(uint32_t _U_ hash, char _U_ *args){ // "dumpconf" (3271513185) -#ifdef EBUG - USB_sendstr("flashsize="); printu(FLASH_SIZE); USB_sendstr("kB\nblocksize="); - printu(FLASH_blocksize); - newline(); -#endif - USB_sendstr("userconf_addr="); printuhex((uint32_t)Flash_Data); + USB_sendstr("flashsize="); printu(FLASH_SIZE); + USB_sendstr("kB\nblocksize="); printu(FLASH_blocksize); + USB_sendstr("\nuserconf_addr="); printuhex((uint32_t)Flash_Data); USB_sendstr("\nuserconf_idx="); printi(currentconfidx); + USB_sendstr("\ncapacity="); printu(maxCnum-2); USB_sendstr("\nuserconf_sz="); printu(the_conf.userconf_sz); USB_sendstr("\ncanspeed="); printu(the_conf.CANspeed); USB_sendstr("\ncanid="); printu(the_conf.CANID); diff --git a/F3:F303/Multistepper/main.c b/F3:F303/Multistepper/main.c index a8b23b0..93c3aec 100644 --- a/F3:F303/Multistepper/main.c +++ b/F3:F303/Multistepper/main.c @@ -83,13 +83,6 @@ int main(void){ int l = USB_receivestr(inbuff, MAXSTRLEN); if(l < 0) USB_sendstr("USB_BUF_OVERFLOW\n"); else if(l){ -/* -#ifdef EBUG - USB_sendstr("USB GOT:\n"); - USB_sendstr(inbuff); - USB_sendstr("\n--------\n"); -#endif -*/ const char *ans = cmd_parser(inbuff); if(ans) USB_sendstr(ans); } diff --git a/F3:F303/Multistepper/multistepper.creator.user b/F3:F303/Multistepper/multistepper.creator.user index 6e3f9ac..06282db 100644 --- a/F3:F303/Multistepper/multistepper.creator.user +++ b/F3:F303/Multistepper/multistepper.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -14,8 +14,6 @@ ProjectExplorer.Project.EditorSettings true - true - true Cpp @@ -30,33 +28,15 @@ 2 KOI8-R - false 4 - false - 0 - 80 - true - true 1 - 0 - false false - false 1 - true true - 0 8 true - false - 1 true - true - true - *.md, *.MD, Makefile true - true - true @@ -76,11 +56,6 @@ 0 true - true - true - Builtin.DefaultTidyAndClazy - 4 - true @@ -132,7 +107,7 @@ false false - + По умолчанию GenericProjectManager.GenericBuildConfiguration 0 @@ -155,10 +130,11 @@ true 0 true + 2 - + false -e cpu-cycles --call-graph dwarf,4096 -F 250 @@ -190,10 +166,11 @@ true 0 true + 2 - + false -e cpu-cycles --call-graph dwarf,4096 -F 250 diff --git a/F3:F303/Multistepper/pdnuart.c b/F3:F303/Multistepper/pdnuart.c index fbb50fe..67156fa 100644 --- a/F3:F303/Multistepper/pdnuart.c +++ b/F3:F303/Multistepper/pdnuart.c @@ -94,23 +94,12 @@ static int rwreg(uint8_t reg, uint32_t data, int w){ ++nbytes; for(int i = 0; i < nbytes; ++i){ IWDG->KR = IWDG_REFRESH; - /* -#ifdef EBUG - USB_sendstr("Send byte "); USB_putbyte('0'+i); USB_sendstr(": "); printuhex(outbuf[i]); newline(); -#endif - */ USART[no]->TDR = outbuf[i]; // transmit while(!(USART[no]->ISR & USART_ISR_TXE)); int l = 0; for(; l < 10000; ++l) if(USART[no]->ISR & USART_ISR_RXNE) break; // clear Rx (void) USART[no]->RDR; - /* -#ifdef EBUG - if(l == 10000) USND("Nothing received"); - else {USB_sendstr("Rcv: "); printuhex(USART[no]->RDR); newline();} -#endif - */ } return TRUE; } @@ -134,11 +123,6 @@ int pdnuart_readreg(uint8_t reg, uint32_t *data){ return FALSE; } buf[i] = USART[no]->RDR; -/* -#ifdef EBUG - USB_sendstr("Read byte: "); printuhex(buf[i]); newline(); -#endif -*/ } uint32_t o = 0; for(int i = 3; i < 7; ++i){ @@ -228,13 +212,3 @@ int pdnuart_init(uint8_t no){ return TRUE; } -/* -static void parseRx(int no){ - USB_sendstr("Got from "); - USB_putbyte('#'); printu(curslaveaddr[no] + no*4); USB_sendstr(": "); - for(int i = 0; i < 8; ++i){ - printuhex(inbuf[no][i]); USB_putbyte(' '); - } - newline(); -} -*/ diff --git a/F3:F303/Multistepper/proto.c b/F3:F303/Multistepper/proto.c index 68385ed..aa0c6e3 100644 --- a/F3:F303/Multistepper/proto.c +++ b/F3:F303/Multistepper/proto.c @@ -484,13 +484,6 @@ static int canusb_function(uint32_t hash, char *args){ uint32_t N; int32_t val = 0; uint8_t par = CANMESG_NOPAR; -/* - DBG("CMD: hash="); -#ifdef EBUG - printu(hash); USB_sendstr(", args="); - USND(args); -#endif -*/ if(*args){ const char *n = getnum(args, &N); if(n != args){ // get parameter @@ -509,12 +502,6 @@ static int canusb_function(uint32_t hash, char *args){ } } } -/* -#ifdef EBUG - USB_sendstr("par="); printuhex(par); - USB_sendstr(", val="); printi(val); newline(); -#endif -*/ switch(hash){ case CMD_ADC: e = cu_adc(par, &val); @@ -666,8 +653,6 @@ static int canusb_function(uint32_t hash, char *args){ break; } - //if(e < ERR_OK || e >= ERR_AMOUNT) USND("Bad return code"); - //else if(ERR_OK != e){ USB_sendstr(errtxt[e]); newline(); }else{ diff --git a/F3:F303/Multistepper/steppers.c b/F3:F303/Multistepper/steppers.c index 13df5ec..d49524b 100644 --- a/F3:F303/Multistepper/steppers.c +++ b/F3:F303/Multistepper/steppers.c @@ -72,6 +72,7 @@ static uint32_t Taccel[MOTORSNO] = {0}; // recalculate ARR according to new speed TRUE_INLINE void recalcARR(int i){ + if(curspeed[i] < 1) curspeed[i] = 1; uint32_t ARR = (((PCLK/(MOTORTIM_PSC+1)) / curspeed[i]) >> ustepsshift[i]) - 1; if(ARR < MOTORTIM_ARRMIN) ARR = MOTORTIM_ARRMIN; else if(ARR > 0xffff) ARR = 0xffff; @@ -144,15 +145,7 @@ errcodes getremainsteps(uint8_t i, int32_t *position){ // calculate acceleration/deceleration parameters for motor i static void calcacceleration(uint8_t i){ - switch(state[i]){ // do nothing in case of error/stopping - case STP_ERR: - case STP_RELAX: - case STP_STALL: - return; - break; - default: - break; - } + if(!ismoving(i)) return; // do nothing in non-moving state int32_t delta = targstppos[i] - stppos[i]; if(delta > 0){ // positive direction if(delta > 2*(int32_t)accdecsteps[i]){ // can move by trapezoid @@ -219,15 +212,9 @@ static int esw_block(uint8_t i){ errcodes motor_absmove(uint8_t i, int32_t newpos){ //if(i >= MOTORSNO) return ERR_BADPAR; // bad motor number int8_t dir = (newpos > stppos[i]) ? 1 : -1; - switch(state[i]){ - case STP_ERR: - case STP_RELAX: - break; - case STP_STALL: - break; - default: // moving state - DBG("Is moving"); - return ERR_CANTRUN; + if(ismoving(i)){ + DBG("Is moving"); + return ERR_CANTRUN; } if(newpos > (int32_t)the_conf.maxsteps[i] || newpos < -(int32_t)the_conf.maxsteps[i] || newpos == stppos[i]){ DBG("Too much steps"); @@ -251,6 +238,9 @@ errcodes motor_absmove(uint8_t i, int32_t newpos){ USB_sendstr(", accdecsteps="); printu(accdecsteps[i]); newline(); #endif MOTOR_EN(i); + // clear counter and generate update event to refresh ARR + mottimers[i]->CNT = 0; + mottimers[i]->EGR = TIM_EGR_UG; mottimers[i]->CR1 |= TIM_CR1_CEN; // start timer return ERR_OK; } @@ -282,13 +272,42 @@ void emstopmotor(uint8_t i){ default: break; } - stopflag[i] = 1; + // check that timer is works + if(mottimers[i]->CR1 & TIM_CR1_CEN){ + stopflag[i] = 1; + }else{ + state[i] = STP_RELAX; + stopflag[i] = 0; + } } stp_state getmotstate(uint8_t i){ return state[i]; } +// return TRUE if motor is in moving state +uint8_t ismoving(uint8_t i){ + switch(state[i]){ + case STP_ACCEL: + case STP_MOVE: + case STP_MVSLOW: + case STP_DECEL: + return TRUE; + break; + default: + break; + } + return FALSE; +} + +// return TRUE if any motor is in moving state +uint8_t isanymoving(){ + for(int i = 0; i < MOTORSNO; ++i){ + if(ismoving(i)) return TRUE; + } + return FALSE; +} + // get DIAGN input uint8_t motdiagn(uint8_t i){ if(i > MOTORSNO-1) return 0; @@ -445,6 +464,13 @@ static void chkstepper(int i){ } recalcARR(i); break; + case STP_MVSLOW: + if(!(mottimers[i]->CR1 & TIM_CR1_CEN)){ // timer stopped but state wasn't changed + state[i] = STP_RELAX; + stopflag[i] = 0; + DBG("MVSLOW with timer stopped"); + } + break; default: // do nothing, check mvzerostate break; } @@ -491,7 +517,7 @@ errcodes motor_goto0(uint8_t i){ errcodes e = motor_absmove(i, -the_conf.maxsteps[i]); if(ERR_OK != e){ if(!esw_block(i)) return e; // limit switch not block -> error - }else ESW_reaction[i] = ESW_IGNORE1; + }else ESW_reaction[i] = ESW_IGNORE1; mvzerostate[i] = M0FAST; return e; } diff --git a/F3:F303/Multistepper/steppers.h b/F3:F303/Multistepper/steppers.h index 368bada..62e8bbb 100644 --- a/F3:F303/Multistepper/steppers.h +++ b/F3:F303/Multistepper/steppers.h @@ -69,5 +69,7 @@ uint8_t geteswreact(uint8_t i); void emstopmotor(uint8_t i); void stopmotor(uint8_t i); stp_state getmotstate(uint8_t i); +uint8_t ismoving(uint8_t i); +uint8_t isanymoving(); uint8_t motdiagn(uint8_t i); void process_steppers(); diff --git a/F3:F303/Multistepper/version.inc b/F3:F303/Multistepper/version.inc index c5f29b2..9e48e49 100644 --- a/F3:F303/Multistepper/version.inc +++ b/F3:F303/Multistepper/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "212" -#define BUILD_DATE "2026-01-20" +#define BUILD_NUMBER "215" +#define BUILD_DATE "2026-07-13"