Changed naming of constants and formatting

This commit is contained in:
Eyck Jentzsch 2018-10-16 18:51:48 +02:00
parent d6db5f7e04
commit 9f9088a110
4 changed files with 60 additions and 51 deletions

View File

@ -6,7 +6,7 @@ HEADERS = $(wildcard src/*.h)
CFLAGS = -g -fno-builtin-printf -DUSE_PLIC -DUSE_M_TIME -DNO_INIT -I./src CFLAGS = -g -fno-builtin-printf -DUSE_PLIC -DUSE_M_TIME -DNO_INIT -I./src
CXXFLAGS = -fno-use-cxa-atexit CXXFLAGS = -fno-use-cxa-atexit
LDFLAGS = -Wl,--wrap=printf 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 BOARD=freedom-e300-hifive1

View File

@ -7,7 +7,6 @@
//============================================================================ //============================================================================
#include "riscv-bldc.h" #include "riscv-bldc.h"
#include "peripherals.h"
#include "delay.h" #include "delay.h"
#include "bsp.h" #include "bsp.h"
#include "plic/plic_driver.h" #include "plic/plic_driver.h"
@ -15,42 +14,46 @@
#include <cstdio> #include <cstdio>
#include <cstdint> #include <cstdint>
volatile uint32_t nextCommutationStep; #include "hifive1_io.h"
std::array<uint32_t, 6> cwDriveTable { //! Drive pattern for commutation, CW rotation volatile uint32_t nextCommutationStep;
((1 << VH) | (1 << WL)), ((1 << UH) | (1 << WL)), ((1 << UH) | (1 << VL)), /*
((1 << WH) | (1 << VL)), ((1 << WH) | (1 << UL)), ((1 << VH) | (1 << UL)) 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<uint32_t, 6> 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<uint32_t, 6> cwSenseTable { //! channels to sense during the applied pattern std::array<uint32_t, 6> senseTable { //! channels to sense during the applied pattern
SENSU_P, SENSV_N, SENSW_P, SENSW_N, //1
SENSU_N, SENSV_P, SENSW_N SENSU_P, //2
SENSV_N, //3
SENSW_P, //4
SENSU_N, //5
SENSV_P //6
}; };
std::array<uint32_t, 6> ccwDriveTable{ //! Drive pattern for commutation, CCW rotation. std::array<unsigned int, 18> startupDelays{
((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<uint32_t, 6> ccwSenseTable { //! channels to sense during the applied pattern
SENSW_P, SENSV_N, SENSU_P,
SENSW_N, SENSV_P, SENSU_N
};
std::array<unsigned int, 24> startupDelays{
/* /*
200, 150, 100, 80, 70, 65, 200, 150, 100, 80, 70, 65,
60, 55, 50, 45, 40, 35, 60, 55, 50, 45, 40, 35,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25 25, 25, 25, 25, 25, 25
*/ */
150, 90, 70, 50, 50, 50, 150, 90, 70, 50, 40, 35,
50, 50, 50, 40, 40, 40, 30, 25, 25, 25, 25, 25,
40, 40, 40, 30, 30, 30, 25, 25, 25, 25, 25, 25
30, 30, 30, 25, 25, 25,
}; };
bool ccw=false; bool ccw=false;
auto& driveTable = ccw?ccwDriveTable:cwDriveTable;
auto& senseTable = ccw?ccwSenseTable:cwSenseTable;
typedef void (*function_ptr_t) (void); typedef void (*function_ptr_t) (void);
// Instance data for the PLIC. // Instance data for the PLIC.
plic_instance_t g_plic; plic_instance_t g_plic;
@ -157,7 +160,7 @@ void fixed_delay(unsigned short delay){
pwm0::oneshot_delay(STARTUP_DELAY_MULTIPLIER*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; long delay_us = max_delay * STARTUP_DELAY_MULTIPLIER;
auto scaling_factor=0; auto scaling_factor=0;
while(delay_us/(1<<scaling_factor) > std::numeric_limits<unsigned short>::max()){ while(delay_us/(1<<scaling_factor) > std::numeric_limits<unsigned short>::max()){
@ -174,24 +177,37 @@ unsigned short measured_zc_time(unsigned short max_delay, unsigned state){
uint32_t adc_res=0; uint32_t adc_res=0;
do{ do{
adc_res=read_adc(channel); 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; break;
}
} while(pwm0::is_active()); } while(pwm0::is_active());
uint32_t sreg = pwm0::s_reg(); uint32_t sreg = pwm0::s_reg();
pwm0::cfg_reg().enoneshot=false; pwm0::cfg_reg().enoneshot=false;
return sreg*(1<<scaling_factor); return sreg*(1<<scaling_factor);
} }
void next_commutation_step(void) {
if (ccw) {
if (nextCommutationStep == 0)
nextCommutationStep = 0;
else
nextCommutationStep--;
} else {
if (nextCommutationStep == 5)
nextCommutationStep = 0;
else
nextCommutationStep++;
}
}
void start_motor(void){ void start_motor(void){
nextCommutationStep = 0; nextCommutationStep = 0;
//Preposition. //Preposition.
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK) | driveTable[nextCommutationStep]; gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK) | driveTable[nextCommutationStep];
fixed_delay(STARTUP_LOCK_DELAY); fixed_delay(STARTUP_LOCK_DELAY);
nextCommutationStep++; next_commutation_step();
auto nextDrivePattern = driveTable[nextCommutationStep]; auto nextDrivePattern = driveTable[nextCommutationStep];
const size_t size=startupDelays.size(); const size_t size=startupDelays.size();
for (size_t i = 0; i < startupDelays.size()+10; i++){ for (size_t i = 0; i < startupDelays.size()+6*10; i++){
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff) gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff)
| nextDrivePattern | nextCommutationStep<<24; | nextDrivePattern | nextCommutationStep<<24;
auto channel=senseTable[nextCommutationStep]&0x3; auto channel=senseTable[nextCommutationStep]&0x3;
@ -200,10 +216,7 @@ void start_motor(void){
fixed_delay(startupDelays[i>=size?size-1:i]); fixed_delay(startupDelays[i>=size?size-1:i]);
auto bemf_1=read_adc(channel); auto bemf_1=read_adc(channel);
auto bemf = bemf_1>bemf_0?bemf_1-bemf_0:bemf_0-bemf_1; auto bemf = bemf_1>bemf_0?bemf_1-bemf_0:bemf_0-bemf_1;
nextCommutationStep++; next_commutation_step();
if (nextCommutationStep >= 6){
nextCommutationStep = 0;
}
nextDrivePattern = driveTable[nextCommutationStep]; nextDrivePattern = driveTable[nextCommutationStep];
// if(i>12 && bemf>32 && ((zcPolRise && bemf_0<2048 && bemf_1>2047) || (!zcPolRise && bemf_0>2047 && bemf_1<2048))) // if(i>12 && bemf>32 && ((zcPolRise && bemf_0<2048 && bemf_1>2047) || (!zcPolRise && bemf_0>2047 && bemf_1<2048)))
// return; // return;
@ -214,16 +227,14 @@ void run_motor(void){
auto count=0; auto count=0;
auto zc_delay=0U; auto zc_delay=0U;
auto tmp=0U; auto tmp=0U;
auto nextDrivePattern = driveTable[nextCommutationStep];
for(;;){ for(;;){
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff) gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff)
| driveTable[nextCommutationStep] | nextCommutationStep<<24; | nextDrivePattern | nextCommutationStep<<24;
zc_delay=measured_zc_time(50, senseTable[nextCommutationStep]); zc_delay=measured_zc_time(500);
// tmp=zc_delay>>2;
// pwm0::oneshot_delay(zc_delay>tmp?zc_delay:zc_delay/2+zc_delay/4+zc_delay/8);
pwm0::oneshot_delay(zc_delay); pwm0::oneshot_delay(zc_delay);
nextCommutationStep++; next_commutation_step();
if (nextCommutationStep >= 6) nextDrivePattern = driveTable[nextCommutationStep];
nextCommutationStep = 0;
} }
} }

View File

@ -20,24 +20,22 @@ enum {
VH=10, //! Port pin connected to phase V, high side enable switch. VH=10, //! Port pin connected to phase V, high side enable switch.
WL=19, //! Port pin connected to phase W, low 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. WH=20, //! Port pin connected to phase W, high side enable switch.
CW=0, //! Clockwise rotation flag. SENSU_P=0, //! Phase U voltage to sense positive zero cross
CCW=1, //! Counterclockwise rotation flag. SENSV_P=1, //! Phase V voltage to sense positive zero cross
SENSU_P=0, //! Phase U voltage to sense SENSW_P=2, //! Phase W voltage to sense positive zero cross
SENSV_P=1, //! Phase V voltage to sense SENSU_N=4, //! Phase U voltage to sense negative zero cross
SENSW_P=2, //! Phase W voltage to sense SENSV_N=5, //! Phase V voltage to sense negative zero cross
SENSU_N=4, //! Phase U voltage to sense SENSW_N=6, //! Phase W voltage to sense negative zero cross
SENSV_N=5, //! Phase V voltage to sense
SENSW_N=6, //! Phase W voltage to sense
DRIVE_MASK=(1<<UL)|(1<<UH)| (1<<VL)|(1<<VH)| (1<<WL)|(1<<WH) DRIVE_MASK=(1<<UL)|(1<<UH)| (1<<VL)|(1<<VH)| (1<<WL)|(1<<WH)
}; };
//! Startup delays are given in microseconds times STARTUP_DELAY_MULTIPLIER. //! Startup delays are given in microseconds times STARTUP_DELAY_MULTIPLIER.
const auto STARTUP_DELAY_MULTIPLIER=1000; const auto STARTUP_DELAY_MULTIPLIER=150;
/*! /*!
* Number of milliseconds to lock rotor in first commutation step before * Number of milliseconds to lock rotor in first commutation step before
* the timed startup sequence is initiated. * the timed startup sequence is initiated.
*/ */
const auto STARTUP_LOCK_DELAY=200; const auto STARTUP_LOCK_DELAY=1000;
extern "C" void handle_m_ext_interrupt(); extern "C" void handle_m_ext_interrupt();
extern "C" void handle_m_time_interrupt(); extern "C" void handle_m_time_interrupt();