diff --git a/riscv-bldc-forced-commutation/Makefile b/riscv-bldc-forced-commutation/Makefile index cb58bf6..efccd86 100644 --- a/riscv-bldc-forced-commutation/Makefile +++ b/riscv-bldc-forced-commutation/Makefile @@ -6,7 +6,7 @@ HEADERS = $(wildcard src/*.h) CFLAGS = -g -fno-builtin-printf -DUSE_PLIC -DUSE_M_TIME -DNO_INIT -I./src CXXFLAGS = -fno-use-cxa-atexit LDFLAGS = -Wl,--wrap=printf -LDFLAGS = -g -lstdc++ -fno-use-cxa-atexit -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -mcmodel=medany +LDFLAGS += -g -lstdc++ -fno-use-cxa-atexit -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -mcmodel=medany BOARD=freedom-e300-hifive1 diff --git a/riscv-bldc-forced-commutation/riscv-bldc b/riscv-bldc-forced-commutation/riscv-bldc index c3e3e47..9e9e2f6 100755 Binary files a/riscv-bldc-forced-commutation/riscv-bldc and b/riscv-bldc-forced-commutation/riscv-bldc differ diff --git a/riscv-bldc-forced-commutation/src/riscv-bldc.cpp b/riscv-bldc-forced-commutation/src/riscv-bldc.cpp index 4ad06e5..390f24d 100644 --- a/riscv-bldc-forced-commutation/src/riscv-bldc.cpp +++ b/riscv-bldc-forced-commutation/src/riscv-bldc.cpp @@ -7,7 +7,6 @@ //============================================================================ #include "riscv-bldc.h" -#include "peripherals.h" #include "delay.h" #include "bsp.h" #include "plic/plic_driver.h" @@ -15,42 +14,46 @@ #include #include -volatile uint32_t nextCommutationStep; +#include "hifive1_io.h" -std::array cwDriveTable { //! Drive pattern for commutation, CW rotation - ((1 << VH) | (1 << WL)), ((1 << UH) | (1 << WL)), ((1 << UH) | (1 << VL)), - ((1 << WH) | (1 << VL)), ((1 << WH) | (1 << UL)), ((1 << VH) | (1 << UL)) +volatile uint32_t nextCommutationStep; +/* +Kommutierungsblöcke + 1 2 3 4 5 6 +U 0 z +1 +1 z 0 +V +1 +1 z 0 0 z +W z 0 0 z +1 +1 +*/ +std::array driveTable { //! Drive pattern for commutation, CW rotation + ((1 << VH) | (1 << UL)), //1 + ((1 << VH) | (1 << WL)), //2 + ((1 << UH) | (1 << WL)), //3 + ((1 << UH) | (1 << VL)), //4 + ((1 << WH) | (1 << VL)), //5 + ((1 << WH) | (1 << UL)) //6 }; -std::array cwSenseTable { //! channels to sense during the applied pattern - SENSU_P, SENSV_N, SENSW_P, - SENSU_N, SENSV_P, SENSW_N +std::array senseTable { //! channels to sense during the applied pattern + SENSW_N, //1 + SENSU_P, //2 + SENSV_N, //3 + SENSW_P, //4 + SENSU_N, //5 + SENSV_P //6 }; -std::array ccwDriveTable{ //! Drive pattern for commutation, CCW rotation. - ((1 << UL) | (1 << VH)), ((1 << UL) | (1 << WH)), ((1 << VL) | (1 << WH)), - ((1 << VL) | (1 << UH)), ((1 << WL) | (1 << UH)), ((1 << WL) | (1 << VH)) -}; -std::array ccwSenseTable { //! channels to sense during the applied pattern - SENSW_P, SENSV_N, SENSU_P, - SENSW_N, SENSV_P, SENSU_N -}; -std::array startupDelays{ +std::array startupDelays{ /* 200, 150, 100, 80, 70, 65, 60, 55, 50, 45, 40, 35, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25 */ - 150, 90, 70, 50, 50, 50, - 50, 50, 50, 40, 40, 40, - 40, 40, 40, 30, 30, 30, - 30, 30, 30, 25, 25, 25, + 150, 90, 70, 50, 40, 35, + 30, 25, 25, 25, 25, 25, + 25, 25, 25, 25, 25, 25 }; bool ccw=false; -auto& driveTable = ccw?ccwDriveTable:cwDriveTable; -auto& senseTable = ccw?ccwSenseTable:cwSenseTable; - typedef void (*function_ptr_t) (void); // Instance data for the PLIC. plic_instance_t g_plic; @@ -157,7 +160,7 @@ void fixed_delay(unsigned short delay){ pwm0::oneshot_delay(STARTUP_DELAY_MULTIPLIER*delay); } -unsigned short measured_zc_time(unsigned short max_delay, unsigned state){ +unsigned short measured_zc_time(unsigned short max_delay){ long delay_us = max_delay * STARTUP_DELAY_MULTIPLIER; auto scaling_factor=0; while(delay_us/(1< std::numeric_limits::max()){ @@ -174,24 +177,37 @@ unsigned short measured_zc_time(unsigned short max_delay, unsigned state){ uint32_t adc_res=0; do{ adc_res=read_adc(channel); - if((zc_neg && adc_res<2048) || (!zc_neg && adc_res>2047)){ + if((zc_neg && adc_res<2048) || (!zc_neg && adc_res>2047)) break; - } } while(pwm0::is_active()); uint32_t sreg = pwm0::s_reg(); pwm0::cfg_reg().enoneshot=false; return sreg*(1<=size?size-1:i]); auto bemf_1=read_adc(channel); auto bemf = bemf_1>bemf_0?bemf_1-bemf_0:bemf_0-bemf_1; - nextCommutationStep++; - if (nextCommutationStep >= 6){ - nextCommutationStep = 0; - } + next_commutation_step(); nextDrivePattern = driveTable[nextCommutationStep]; // if(i>12 && bemf>32 && ((zcPolRise && bemf_0<2048 && bemf_1>2047) || (!zcPolRise && bemf_0>2047 && bemf_1<2048))) // return; @@ -214,16 +227,14 @@ void run_motor(void){ auto count=0; auto zc_delay=0U; auto tmp=0U; + auto nextDrivePattern = driveTable[nextCommutationStep]; for(;;){ gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff) - | driveTable[nextCommutationStep] | nextCommutationStep<<24; - zc_delay=measured_zc_time(50, senseTable[nextCommutationStep]); -// tmp=zc_delay>>2; -// pwm0::oneshot_delay(zc_delay>tmp?zc_delay:zc_delay/2+zc_delay/4+zc_delay/8); + | nextDrivePattern | nextCommutationStep<<24; + zc_delay=measured_zc_time(500); pwm0::oneshot_delay(zc_delay); - nextCommutationStep++; - if (nextCommutationStep >= 6) - nextCommutationStep = 0; + next_commutation_step(); + nextDrivePattern = driveTable[nextCommutationStep]; } } diff --git a/riscv-bldc-forced-commutation/src/riscv-bldc.h b/riscv-bldc-forced-commutation/src/riscv-bldc.h index a19bd6c..bf91dd7 100644 --- a/riscv-bldc-forced-commutation/src/riscv-bldc.h +++ b/riscv-bldc-forced-commutation/src/riscv-bldc.h @@ -20,24 +20,22 @@ enum { VH=10, //! Port pin connected to phase V, high side enable switch. WL=19, //! Port pin connected to phase W, low side enable switch. WH=20, //! Port pin connected to phase W, high side enable switch. - CW=0, //! Clockwise rotation flag. - CCW=1, //! Counterclockwise rotation flag. - SENSU_P=0, //! Phase U voltage to sense - SENSV_P=1, //! Phase V voltage to sense - SENSW_P=2, //! Phase W voltage to sense - SENSU_N=4, //! Phase U voltage to sense - SENSV_N=5, //! Phase V voltage to sense - SENSW_N=6, //! Phase W voltage to sense + SENSU_P=0, //! Phase U voltage to sense positive zero cross + SENSV_P=1, //! Phase V voltage to sense positive zero cross + SENSW_P=2, //! Phase W voltage to sense positive zero cross + SENSU_N=4, //! Phase U voltage to sense negative zero cross + SENSV_N=5, //! Phase V voltage to sense negative zero cross + SENSW_N=6, //! Phase W voltage to sense negative zero cross DRIVE_MASK=(1<