Added doxygen comments and refactored a few functions
This commit is contained in:
parent
188bd2b5dd
commit
a96cb14dcf
|
@ -16,14 +16,15 @@
|
||||||
|
|
||||||
#include "hifive1_io.h"
|
#include "hifive1_io.h"
|
||||||
|
|
||||||
volatile uint32_t nextCommutationStep;
|
volatile uint32_t nextCommutationStep=0;
|
||||||
/*
|
|
||||||
Kommutierungsblöcke
|
/* commutation blocks
|
||||||
1 2 3 4 5 6
|
* 1 2 3 4 5 6
|
||||||
U 0 z +1 +1 z 0
|
* U 0 z +1 +1 z 0
|
||||||
V +1 +1 z 0 0 z
|
* V +1 +1 z 0 0 z
|
||||||
W z 0 0 z +1 +1
|
* W z 0 0 z +1 +1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
std::array<uint32_t, 6> driveTable { //! Drive pattern for commutation, CW rotation
|
std::array<uint32_t, 6> driveTable { //! Drive pattern for commutation, CW rotation
|
||||||
((1 << VH) | (1 << UL)), //1
|
((1 << VH) | (1 << UL)), //1
|
||||||
((1 << VH) | (1 << WL)), //2
|
((1 << VH) | (1 << WL)), //2
|
||||||
|
@ -32,6 +33,7 @@ std::array<uint32_t, 6> driveTable { //! Drive pattern for commutation, CW rotat
|
||||||
((1 << WH) | (1 << VL)), //5
|
((1 << WH) | (1 << VL)), //5
|
||||||
((1 << WH) | (1 << UL)) //6
|
((1 << WH) | (1 << UL)) //6
|
||||||
};
|
};
|
||||||
|
|
||||||
std::array<uint32_t, 6> senseTable { //! channels to sense during the applied pattern
|
std::array<uint32_t, 6> senseTable { //! channels to sense during the applied pattern
|
||||||
SENSW_N, //1
|
SENSW_N, //1
|
||||||
SENSU_P, //2
|
SENSU_P, //2
|
||||||
|
@ -44,10 +46,14 @@ std::array<uint32_t, 6> senseTable { //! channels to sense during the applied pa
|
||||||
bool ccw=false;
|
bool ccw=false;
|
||||||
|
|
||||||
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;
|
||||||
std::array<function_ptr_t,PLIC_NUM_INTERRUPTS> g_ext_interrupt_handlers;
|
std::array<function_ptr_t,PLIC_NUM_INTERRUPTS> g_ext_interrupt_handlers;
|
||||||
|
/*! \brief external interrupt handler
|
||||||
|
*
|
||||||
|
* routes the peripheral interrupts to the the respective handler
|
||||||
|
*
|
||||||
|
*/
|
||||||
extern "C" void handle_m_ext_interrupt() {
|
extern "C" void handle_m_ext_interrupt() {
|
||||||
plic_source int_num = PLIC_claim_interrupt(&g_plic);
|
plic_source int_num = PLIC_claim_interrupt(&g_plic);
|
||||||
if ((int_num >=1 ) && (int_num < PLIC_NUM_INTERRUPTS))
|
if ((int_num >=1 ) && (int_num < PLIC_NUM_INTERRUPTS))
|
||||||
|
@ -56,8 +62,9 @@ extern "C" void handle_m_ext_interrupt() {
|
||||||
exit(1 + (uintptr_t) int_num);
|
exit(1 + (uintptr_t) int_num);
|
||||||
PLIC_complete_interrupt(&g_plic, int_num);
|
PLIC_complete_interrupt(&g_plic, int_num);
|
||||||
}
|
}
|
||||||
|
/*! \brief mtime interval interrupt
|
||||||
// 1sec interval interrupt
|
*
|
||||||
|
*/
|
||||||
extern "C" void handle_m_time_interrupt(){
|
extern "C" void handle_m_time_interrupt(){
|
||||||
clear_csr(mie, MIP_MTIP);
|
clear_csr(mie, MIP_MTIP);
|
||||||
// Reset the timer for 3s in the future.
|
// Reset the timer for 3s in the future.
|
||||||
|
@ -70,9 +77,13 @@ extern "C" void handle_m_time_interrupt(){
|
||||||
// Re-enable the timer interrupt.
|
// Re-enable the timer interrupt.
|
||||||
set_csr(mie, MIP_MTIP);
|
set_csr(mie, MIP_MTIP);
|
||||||
}
|
}
|
||||||
|
/*! \brief dummy interrupt handler
|
||||||
|
*
|
||||||
|
*/
|
||||||
void no_interrupt_handler (void) {};
|
void no_interrupt_handler (void) {};
|
||||||
|
/*! \brief configure the per-interrupt handler
|
||||||
|
*
|
||||||
|
*/
|
||||||
void configure_irq(size_t irq_num, function_ptr_t handler, unsigned char prio=1) {
|
void configure_irq(size_t irq_num, function_ptr_t handler, unsigned char prio=1) {
|
||||||
g_ext_interrupt_handlers[irq_num] = handler;
|
g_ext_interrupt_handlers[irq_num] = handler;
|
||||||
// Priority must be set > 0 to trigger the interrupt.
|
// Priority must be set > 0 to trigger the interrupt.
|
||||||
|
@ -80,7 +91,9 @@ void configure_irq(size_t irq_num, function_ptr_t handler, unsigned char prio=1)
|
||||||
// Have to enable the interrupt both at the GPIO level, and at the PLIC level.
|
// Have to enable the interrupt both at the GPIO level, and at the PLIC level.
|
||||||
PLIC_enable_interrupt(&g_plic, irq_num);
|
PLIC_enable_interrupt(&g_plic, irq_num);
|
||||||
}
|
}
|
||||||
|
/*!\brief initializes platform
|
||||||
|
*
|
||||||
|
*/
|
||||||
void platform_init(){
|
void platform_init(){
|
||||||
// configure clocks
|
// configure clocks
|
||||||
PRCI_use_hfxosc(1); // is equivalent to
|
PRCI_use_hfxosc(1); // is equivalent to
|
||||||
|
@ -125,7 +138,9 @@ void platform_init(){
|
||||||
// Enable interrupts in general.
|
// Enable interrupts in general.
|
||||||
set_csr(mstatus, MSTATUS_MIE);
|
set_csr(mstatus, MSTATUS_MIE);
|
||||||
}
|
}
|
||||||
|
/*! \brief reads adc channel and returns measured value
|
||||||
|
*
|
||||||
|
*/
|
||||||
unsigned read_adc(unsigned channel){
|
unsigned read_adc(unsigned channel){
|
||||||
std::array<uint8_t, 3> bytes{
|
std::array<uint8_t, 3> bytes{
|
||||||
uint8_t(0x06 | (channel>>2 & 0x1)), /* start bit, single ended measurement, channel[2] */
|
uint8_t(0x06 | (channel>>2 & 0x1)), /* start bit, single ended measurement, channel[2] */
|
||||||
|
@ -137,13 +152,8 @@ unsigned read_adc(unsigned channel){
|
||||||
qspi1::transfer(bytes);
|
qspi1::transfer(bytes);
|
||||||
return (bytes[1]&0xf)*256+bytes[2];
|
return (bytes[1]&0xf)*256+bytes[2];
|
||||||
}
|
}
|
||||||
|
/*! \brief waits for zero crossing and measures time until
|
||||||
/*! \brief Generates a delay used during startup
|
|
||||||
*
|
*
|
||||||
* This functions is used to generate a delay during the startup procedure.
|
|
||||||
* The length of the delay equals delay * STARTUP_DELAY_MULTIPLIER microseconds.
|
|
||||||
* Since Timer/Counter1 is used in this function, it must never be called when
|
|
||||||
* sensorless operation is running.
|
|
||||||
*/
|
*/
|
||||||
unsigned short measured_zc_time(unsigned short max_delay){
|
unsigned short measured_zc_time(unsigned short max_delay){
|
||||||
long delay_us = max_delay;
|
long delay_us = max_delay;
|
||||||
|
@ -169,21 +179,25 @@ unsigned short measured_zc_time(unsigned short max_delay){
|
||||||
pwm0::cfg_reg().enoneshot=false;
|
pwm0::cfg_reg().enoneshot=false;
|
||||||
return sreg*(1<<scaling_factor);
|
return sreg*(1<<scaling_factor);
|
||||||
}
|
}
|
||||||
|
/*! \brief calculates the next commutation step
|
||||||
void next_commutation_step(void) {
|
*
|
||||||
|
*/
|
||||||
|
inline void next_commutation_step(void) {
|
||||||
if (ccw) {
|
if (ccw) {
|
||||||
if (nextCommutationStep == 0)
|
nextCommutationStep = nextCommutationStep == 0? 5 : nextCommutationStep-1;
|
||||||
nextCommutationStep = 0;
|
|
||||||
else
|
|
||||||
nextCommutationStep--;
|
|
||||||
} else {
|
} else {
|
||||||
if (nextCommutationStep == 5)
|
nextCommutationStep = nextCommutationStep == 5? 0 : nextCommutationStep+1;
|
||||||
nextCommutationStep = 0;
|
|
||||||
else
|
|
||||||
nextCommutationStep++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*! \brief write the drive pattern to gpio
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void setDrivePattern(uint32_t nextDrivePattern) {
|
||||||
|
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff) | nextDrivePattern | nextCommutationStep << 24;
|
||||||
|
}
|
||||||
|
/*! \brief open-loop commutation to start the motor
|
||||||
|
*
|
||||||
|
*/
|
||||||
void start_open_loop(void){
|
void start_open_loop(void){
|
||||||
auto delay = 30120U;
|
auto delay = 30120U;
|
||||||
std::array<double, 2> multiplier={0.83, 1.0};
|
std::array<double, 2> multiplier={0.83, 1.0};
|
||||||
|
@ -195,8 +209,7 @@ void start_open_loop(void){
|
||||||
next_commutation_step();
|
next_commutation_step();
|
||||||
auto nextDrivePattern = driveTable[nextCommutationStep];
|
auto nextDrivePattern = driveTable[nextCommutationStep];
|
||||||
for (size_t i = 0; i < 12; i++){
|
for (size_t i = 0; i < 12; i++){
|
||||||
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff)
|
setDrivePattern(nextDrivePattern);
|
||||||
| nextDrivePattern | nextCommutationStep<<24;
|
|
||||||
auto channel=senseTable[nextCommutationStep]&0x3;
|
auto channel=senseTable[nextCommutationStep]&0x3;
|
||||||
auto zcPolRise = senseTable[nextCommutationStep]<4;
|
auto zcPolRise = senseTable[nextCommutationStep]<4;
|
||||||
auto bemf_0=read_adc(channel);
|
auto bemf_0=read_adc(channel);
|
||||||
|
@ -208,21 +221,24 @@ void start_open_loop(void){
|
||||||
nextDrivePattern = driveTable[nextCommutationStep];
|
nextDrivePattern = driveTable[nextCommutationStep];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*! \brief closed-loop commutation to run the motor
|
||||||
|
*
|
||||||
|
*/
|
||||||
void run_closed_loop(void){
|
void run_closed_loop(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];
|
auto nextDrivePattern = driveTable[nextCommutationStep];
|
||||||
for(;;){
|
for(;;){
|
||||||
gpio0::port_reg() = (gpio0::port_reg() & ~DRIVE_MASK & 0x00ffffff)
|
setDrivePattern(nextDrivePattern);
|
||||||
| nextDrivePattern | nextCommutationStep<<24;
|
|
||||||
zc_delay=measured_zc_time(50000);
|
zc_delay=measured_zc_time(50000);
|
||||||
next_commutation_step();
|
next_commutation_step();
|
||||||
nextDrivePattern = driveTable[nextCommutationStep];
|
nextDrivePattern = driveTable[nextCommutationStep];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*! \brief main function
|
||||||
|
*
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
platform_init();
|
platform_init();
|
||||||
printf("Starting motor\n");
|
printf("Starting motor\n");
|
||||||
|
|
Loading…
Reference in New Issue