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
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

View File

@ -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 <cstdio>
#include <cstdint>
volatile uint32_t nextCommutationStep;
#include "hifive1_io.h"
std::array<uint32_t, 6> 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<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
SENSU_P, SENSV_N, SENSW_P,
SENSU_N, SENSV_P, SENSW_N
std::array<uint32_t, 6> 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<uint32_t, 6> 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<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{
std::array<unsigned int, 18> 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<<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;
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<<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){
nextCommutationStep = 0;
//Preposition.
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK) | driveTable[nextCommutationStep];
fixed_delay(STARTUP_LOCK_DELAY);
nextCommutationStep++;
next_commutation_step();
auto nextDrivePattern = driveTable[nextCommutationStep];
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)
| nextDrivePattern | nextCommutationStep<<24;
auto channel=senseTable[nextCommutationStep]&0x3;
@ -200,10 +216,7 @@ void start_motor(void){
fixed_delay(startupDelays[i>=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];
}
}

View File

@ -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<<UL)|(1<<UH)| (1<<VL)|(1<<VH)| (1<<WL)|(1<<WH)
};
//! 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
* 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_time_interrupt();