diff --git a/F0:F030,F042,F072/3steppersLB/commonproto.c b/F0:F030,F042,F072/3steppersLB/commonproto.c index 76cbb05..b40d885 100644 --- a/F0:F030,F042,F072/3steppersLB/commonproto.c +++ b/F0:F030,F042,F072/3steppersLB/commonproto.c @@ -188,6 +188,7 @@ static errcodes ustepsparser(uint8_t par, int32_t *val){ if(m != 1< PCLK/(MOTORTIM_PSC+1)/(MOTORTIM_ARRMIN+1)) return ERR_BADVAL; the_conf.microsteps[n] = m; + update_stepper(n); } *val = the_conf.microsteps[n]; return ERR_OK; @@ -218,6 +219,7 @@ static errcodes accparser(uint8_t par, int32_t *val){ if(ISSETTER(par)){ if(*val/the_conf.microsteps[n] > ACCELMAXSTEPS || *val < 1) return ERR_BADVAL; the_conf.accel[n] = *val; + update_stepper(n); } *val = the_conf.accel[n]; return ERR_OK; @@ -238,6 +240,7 @@ static errcodes maxspdparser(uint8_t par, int32_t *val){ if(ISSETTER(par)){ if(*val <= the_conf.minspd[n]) return ERR_BADVAL; the_conf.maxspd[n] = getSPD(n, *val); + update_stepper(n); } *val = the_conf.maxspd[n]; return ERR_OK; @@ -248,6 +251,7 @@ static errcodes minspdparser(uint8_t par, int32_t *val){ if(ISSETTER(par)){ if(*val >= the_conf.maxspd[n]) return ERR_BADVAL; the_conf.minspd[n] = getSPD(n, *val); + update_stepper(n); } *val = the_conf.minspd[n]; return ERR_OK; @@ -274,7 +278,7 @@ static errcodes encrevparser(uint8_t par, int32_t *val){ if(ISSETTER(par)){ if(*val < 1 || *val > MAXENCREV) return ERR_BADVAL; the_conf.encrev[n] = *val; - enctimers[n]->ARR = *val; + update_stepper(n); } *val = the_conf.encrev[n]; return ERR_OK; @@ -284,6 +288,7 @@ static errcodes motflagsparser(uint8_t par, int32_t *val){ uint8_t n; CHECKN(n, par); if(ISSETTER(par)){ the_conf.motflags[n] = *((motflags_t*)val); + update_stepper(n); } *(motflags_t*)val = the_conf.motflags[n]; return ERR_OK; @@ -295,6 +300,7 @@ static errcodes eswreactparser(uint8_t par, int32_t *val){ if(ISSETTER(par)){ if(*val < 0 || *val > ESW_AMOUNT-1) return ERR_BADVAL; the_conf.ESW_reaction[n] = *val; + update_stepper(n); } // *val = the_conf.ESW_reaction[n]; *val = geteswreact(n); diff --git a/F0:F030,F042,F072/3steppersLB/flash.c b/F0:F030,F042,F072/3steppersLB/flash.c index 9f958cf..43a1d20 100644 --- a/F0:F030,F042,F072/3steppersLB/flash.c +++ b/F0:F030,F042,F072/3steppersLB/flash.c @@ -123,10 +123,7 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size FLASH->KEYR = FLASH_KEY1; FLASH->KEYR = FLASH_KEY2; } - while (FLASH->SR & FLASH_SR_BSY); - if(FLASH->SR & FLASH_SR_WRPRTERR){ - return 1; // write protection - } + while (FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; // clear all flags FLASH->CR |= FLASH_CR_PG; const uint16_t *data = (const uint16_t*) wrdata; @@ -135,14 +132,14 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size for (i = 0; i < count; ++i){ IWDG->KR = IWDG_REFRESH; *(volatile uint16_t*)(address + i) = data[i]; - while (FLASH->SR & FLASH_SR_BSY); - if(FLASH->SR & FLASH_SR_PGERR){ + while (FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; + if(FLASH->SR & FLASH_SR_PGERR){ + SEND("Prog err\n"); ret = 1; // program error - meet not 0xffff break; - }else while (!(FLASH->SR & FLASH_SR_EOP)); + } FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; } - FLASH->CR |= FLASH_CR_LOCK; // lock it back FLASH->CR &= ~(FLASH_CR_PG); return ret; } @@ -166,39 +163,26 @@ static int erase_flash(const void *start, const void *end){ } nblocks = flsz / blocksize; if(nblocks == 0 || nblocks >= FLASH_SIZE) return 1; + if((FLASH->CR & FLASH_CR_LOCK) != 0){ + FLASH->KEYR = FLASH_KEY1; + FLASH->KEYR = FLASH_KEY2; + } + while(FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; + FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; + FLASH->CR |= FLASH_CR_PER; for(uint32_t i = 0; i < nblocks; ++i){ - IWDG->KR = IWDG_REFRESH; - /* (1) Wait till no operation is on going */ - /* (2) Clear error & EOP bits */ - /* (3) Check that the Flash is unlocked */ - /* (4) Perform unlock sequence */ - while ((FLASH->SR & FLASH_SR_BSY) != 0){} /* (1) */ - FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; /* (2) */ - /* if (FLASH->SR & FLASH_SR_EOP){ - FLASH->SR |= FLASH_SR_EOP; - }*/ - if ((FLASH->CR & FLASH_CR_LOCK) != 0){ /* (3) */ - FLASH->KEYR = FLASH_KEY1; /* (4) */ - FLASH->KEYR = FLASH_KEY2; - } - /* (1) Set the PER bit in the FLASH_CR register to enable page erasing */ - /* (2) Program the FLASH_AR register to select a page to erase */ - /* (3) Set the STRT bit in the FLASH_CR register to start the erasing */ - /* (4) Wait until the EOP flag in the FLASH_SR register set */ - /* (5) Clear EOP flag by software by writing EOP at 1 */ - /* (6) Reset the PER Bit to disable the page erase */ - FLASH->CR |= FLASH_CR_PER; /* (1) */ - FLASH->AR = (uint32_t)Flash_Data + i*blocksize; /* (2) */ - FLASH->CR |= FLASH_CR_STRT; /* (3) */ - while(!(FLASH->SR & FLASH_SR_EOP)); - FLASH->SR |= FLASH_SR_EOP; /* (5)*/ - if(FLASH->SR & FLASH_SR_WRPRTERR){ /* Check Write protection error */ + SEND("Erase block #"); printu(i); newline(); + FLASH->AR = (uint32_t)Flash_Data + i * blocksize; + FLASH->CR |= FLASH_CR_STRT; + while(FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; + FLASH->SR |= FLASH_SR_EOP; + if(FLASH->SR & FLASH_SR_WRPRTERR){ ret = 1; - FLASH->SR |= FLASH_SR_WRPRTERR; /* Clear the flag by software by writing it at 1*/ + FLASH->SR |= FLASH_SR_WRPRTERR; break; } - FLASH->CR &= ~FLASH_CR_PER; /* (6) */ } + FLASH->CR &= ~FLASH_CR_PER; return ret; } diff --git a/F0:F030,F042,F072/3steppersLB/hardware.c b/F0:F030,F042,F072/3steppersLB/hardware.c index 486ac86..dd38f81 100644 --- a/F0:F030,F042,F072/3steppersLB/hardware.c +++ b/F0:F030,F042,F072/3steppersLB/hardware.c @@ -24,7 +24,7 @@ // Buttons: PA10, PA13, PA14, PA15, pullup (0 active) volatile GPIO_TypeDef *BTNports[BTNSNO] = {GPIOA, GPIOA, GPIOA, GPIOA}; const uint32_t BTNpins[BTNSNO] = {1<<10, 1<<13, 1<<14, 1<<15}; -// Limit switches: PC13, PC14, PC15, pulldown (0 active) +// Limit switches: PC13, PC14, PC15, pullup (0 active) volatile GPIO_TypeDef *ESWports[ESWNO] = {GPIOC, GPIOC, GPIOC}; const uint32_t ESWpins[ESWNO] = {1<<13, 1<<14, 1<<15}; // external GPIO @@ -55,7 +55,7 @@ void gpio_setup(void){ GPIO_MODER_MODER10_O | GPIO_MODER_MODER11_O | GPIO_MODER_MODER12_O | GPIO_MODER_MODER13_O | GPIO_MODER_MODER14_O | GPIO_MODER_MODER15_O ; GPIOB->AFR[0] = (1 << (4*4)) | (1 << (5*4)) | (1 << (6*4)) | (1 << (7*4)); - GPIOC->PUPDR = GPIO_PUPDR13_PD | GPIO_PUPDR14_PD | GPIO_PUPDR15_PD; + GPIOC->PUPDR = GPIO_PUPDR13_PU | GPIO_PUPDR14_PU | GPIO_PUPDR15_PU; GPIOF->MODER = GPIO_MODER_MODER0_O; } diff --git a/F0:F030,F042,F072/3steppersLB/hardware.h b/F0:F030,F042,F072/3steppersLB/hardware.h index 267a895..56dbd8b 100644 --- a/F0:F030,F042,F072/3steppersLB/hardware.h +++ b/F0:F030,F042,F072/3steppersLB/hardware.h @@ -22,6 +22,14 @@ #include +#ifndef FALSE +#define FALSE (0) +#endif + +#ifndef TRUE +#define TRUE (1) +#endif + // PCLK frequency #ifndef PCLK #define PCLK (48000000) diff --git a/F0:F030,F042,F072/3steppersLB/main.c b/F0:F030,F042,F072/3steppersLB/main.c index 5212cc0..36f9286 100644 --- a/F0:F030,F042,F072/3steppersLB/main.c +++ b/F0:F030,F042,F072/3steppersLB/main.c @@ -25,7 +25,6 @@ #include "steppers.h" #include "strfunct.h" #include "usb.h" -#include "usb_lib.h" volatile uint32_t Tms = 0; diff --git a/F0:F030,F042,F072/3steppersLB/steppers.bin b/F0:F030,F042,F072/3steppersLB/steppers.bin index 97db3a9..320958b 100755 Binary files a/F0:F030,F042,F072/3steppersLB/steppers.bin and b/F0:F030,F042,F072/3steppersLB/steppers.bin differ diff --git a/F0:F030,F042,F072/3steppersLB/steppers.c b/F0:F030,F042,F072/3steppersLB/steppers.c index 6c7ab24..fdc5599 100644 --- a/F0:F030,F042,F072/3steppersLB/steppers.c +++ b/F0:F030,F042,F072/3steppersLB/steppers.c @@ -92,6 +92,16 @@ TRUE_INLINE void recalcARR(int i){ curspeed[i] = (((PCLK/(MOTORTIM_PSC+1)) / (ARR+1)) >> ustepsshift[i]); // recalculate speed due to new val } +// update stepper's settings +void update_stepper(uint8_t i){ + if(i >= MOTORSNO) return; + accdecsteps[i] = (the_conf.maxspd[i] * the_conf.maxspd[i]) / the_conf.accel[i] / 2; + ustepsshift[i] = MSB(the_conf.microsteps[i]); + encperstep[i] = the_conf.encrev[i] / STEPSPERREV; + enctimers[i]->ARR = the_conf.encrev[i]; + ESW_reaction[i] = the_conf.ESW_reaction[i]; +} + // run this function after each steppers parameters changing void init_steppers(){ timers_setup(); // reinit timers & stop them @@ -101,13 +111,10 @@ void init_steppers(){ stopflag[i] = 0; motdir[i] = 0; curspeed[i] = 0; - accdecsteps[i] = (the_conf.maxspd[i] * the_conf.maxspd[i]) / the_conf.accel[i] / 2; state[i] = STP_RELAX; - ustepsshift[i] = MSB(the_conf.microsteps[i]); - encperstep[i] = the_conf.encrev[i] / STEPSPERREV; if(!the_conf.motflags[i].donthold) MOTOR_EN(i); else MOTOR_DIS(i); - ESW_reaction[i] = the_conf.ESW_reaction[i]; + update_stepper(i); } } diff --git a/F0:F030,F042,F072/3steppersLB/steppers.cflags b/F0:F030,F042,F072/3steppersLB/steppers.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/F0:F030,F042,F072/3steppersLB/steppers.config b/F0:F030,F042,F072/3steppersLB/steppers.config new file mode 100644 index 0000000..ca0621c --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.config @@ -0,0 +1,5 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 +#define EBUG +#define STM32F0 +#define STM32F072xB diff --git a/F0:F030,F042,F072/3steppersLB/steppers.creator b/F0:F030,F042,F072/3steppersLB/steppers.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.creator @@ -0,0 +1 @@ +[General] diff --git a/F0:F030,F042,F072/3steppersLB/steppers.creator.user b/F0:F030,F042,F072/3steppersLB/steppers.creator.user new file mode 100644 index 0000000..819893c --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.creator.user @@ -0,0 +1,160 @@ + + + + + + EnvironmentId + {cf63021e-ef53-49b0-b03b-2f2570cdf3b6} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + false + false + false + 1 + true + true + 0 + 8 + true + false + 2 + true + true + true + *.md, *.MD, Makefile + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + true + Builtin.DefaultTidyAndClazy + 4 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {91347f2c-5221-46a7-80b1-0a054ca02f79} + 0 + 0 + 0 + + /home/eddy/Docs/SAO/ELECTRONICS/STM32/F0-srcs/usbcan_ringbuffer + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/F0:F030,F042,F072/3steppersLB/steppers.cxxflags b/F0:F030,F042,F072/3steppersLB/steppers.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/F0:F030,F042,F072/3steppersLB/steppers.files b/F0:F030,F042,F072/3steppersLB/steppers.files new file mode 100644 index 0000000..94e04bd --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.files @@ -0,0 +1,24 @@ +adc.c +adc.h +buttons.c +buttons.h +can.c +can.h +commonproto.c +commonproto.h +custom_buttons.c +custom_buttons.h +flash.c +flash.h +hardware.c +hardware.h +main.c +steppers.c +steppers.h +strfunct.c +strfunct.h +usb.c +usb_defs.h +usb.h +usb_lib.c +usb_lib.h diff --git a/F0:F030,F042,F072/3steppersLB/steppers.h b/F0:F030,F042,F072/3steppersLB/steppers.h index 00c5e22..f5c42b0 100644 --- a/F0:F030,F042,F072/3steppersLB/steppers.h +++ b/F0:F030,F042,F072/3steppersLB/steppers.h @@ -23,6 +23,14 @@ #include #include "commonproto.h" +#ifndef FALSE +#define FALSE (0) +#endif + +#ifndef TRUE +#define TRUE (1) +#endif + // amount of tries to detect motor stall #define NSTALLEDMAX (5) // amount of steps to detect stalled state @@ -55,6 +63,7 @@ void addmicrostep(uint8_t i); void encoders_UPD(uint8_t i); void init_steppers(); +void update_stepper(uint8_t i); int32_t encoder_position(uint8_t i); int setencpos(uint8_t i, int32_t position); errcodes setmotpos(uint8_t i, int32_t position); diff --git a/F0:F030,F042,F072/3steppersLB/steppers.includes b/F0:F030,F042,F072/3steppersLB/steppers.includes new file mode 100644 index 0000000..06d1130 --- /dev/null +++ b/F0:F030,F042,F072/3steppersLB/steppers.includes @@ -0,0 +1,6 @@ +. +../inc +../inc/Fx +../inc/cm +../inc/ld +../inc/startup diff --git a/F0:F030,F042,F072/3steppersLB/usb.c b/F0:F030,F042,F072/3steppersLB/usb.c index a7b1ee8..e592ab4 100644 --- a/F0:F030,F042,F072/3steppersLB/usb.c +++ b/F0:F030,F042,F072/3steppersLB/usb.c @@ -84,7 +84,7 @@ static int usbwr(const uint8_t *buf, uint16_t l){ } static uint8_t usbbuff[USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP) -static uint8_t buflen = 0; // amount of symbols in usbbuff +static volatile uint8_t buflen = 0; // amount of symbols in usbbuff // send next up to 63 bytes of data in usbbuff static void send_next(){ @@ -172,4 +172,3 @@ uint8_t USB_receive(uint8_t *buf){ rxNE = 0; return sz; } - diff --git a/F0:F030,F042,F072/3steppersLB/usb_lib.c b/F0:F030,F042,F072/3steppersLB/usb_lib.c index 8d1bf6c..58a4110 100644 --- a/F0:F030,F042,F072/3steppersLB/usb_lib.c +++ b/F0:F030,F042,F072/3steppersLB/usb_lib.c @@ -22,11 +22,11 @@ ep_t endpoints[STM32ENDPOINTS]; usb_dev_t USB_Dev; -uint8_t usbON = 0; +volatile uint8_t usbON = 0; static usb_LineCoding lineCoding = {115200, 0, 0, 8}; static config_pack_t setup_packet; static uint8_t ep0databuf[EP0DATABUF_SIZE]; -static uint8_t ep0dbuflen = 0; +static volatile uint8_t ep0dbuflen = 0; usb_LineCoding getLineCoding(){return lineCoding;} diff --git a/F0:F030,F042,F072/3steppersLB/usb_lib.h b/F0:F030,F042,F072/3steppersLB/usb_lib.h index d6fc0cc..1555633 100644 --- a/F0:F030,F042,F072/3steppersLB/usb_lib.h +++ b/F0:F030,F042,F072/3steppersLB/usb_lib.h @@ -166,7 +166,7 @@ typedef struct { extern ep_t endpoints[]; extern usb_dev_t USB_Dev; -extern uint8_t usbON; +extern volatile uint8_t usbON; void USB_Init(); uint8_t USB_GetState(); diff --git a/F0:F030,F042,F072/3steppersLB/version.inc b/F0:F030,F042,F072/3steppersLB/version.inc index 338ce6f..7f30586 100644 --- a/F0:F030,F042,F072/3steppersLB/version.inc +++ b/F0:F030,F042,F072/3steppersLB/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "169" -#define BUILD_DATE "2023-01-11" +#define BUILD_NUMBER "173" +#define BUILD_DATE "2023-02-21"