Skip to content

Commit

Permalink
Commutation detection works! Still the code needs cleanup and a lot o…
Browse files Browse the repository at this point in the history
…f tuning!
  • Loading branch information
esden committed May 7, 2009
1 parent 477366d commit 4ac0bf8
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 46 deletions.
109 changes: 85 additions & 24 deletions source/breadboard_prototype/src/adc.c
Expand Up @@ -18,11 +18,18 @@

#include <stm32/lib.h>

#include "config.h"
#include "soft_timer.h"

#include "adc.h"

#define ADC1_DR_Address ((u32)0x4001244C)

vu16 adc_val[32];
vu8 adc_rising = ADC_RISING;
vu16 adc_old_value = 2;
vu8 adc_count = 0;
vu16 adc_delay_count = 0;

void adc_rcc_init(void){
/* enable DMA clock */
Expand All @@ -33,40 +40,31 @@ void adc_rcc_init(void){
}

void adc_nvic_init(void){
/* nothing to be done yet */
NVIC_InitTypeDef nvic;

/* Configure and enable ADC interrupt */
nvic.NVIC_IRQChannel = ADC1_2_IRQChannel;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
}

void adc_gpio_init(void){
GPIO_InitTypeDef gpio;

/* GPIOA: ADC Channel 1 as analog input */
gpio.GPIO_Pin = GPIO_Pin_1;
gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &gpio);
}

void adc_init(void){
DMA_InitTypeDef dma;
ADC_InitTypeDef adc;

/* Configure DMA1 channel1 to copy the ADC conversion values to
adc_val variable */
DMA_DeInit(DMA1_Channel1);
dma.DMA_PeripheralBaseAddr = ADC1_DR_Address;
dma.DMA_MemoryBaseAddr = (u32)adc_val;
dma.DMA_DIR = DMA_DIR_PeripheralSRC;
dma.DMA_BufferSize = 32;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
dma.DMA_Mode = DMA_Mode_Circular;
dma.DMA_Priority = DMA_Priority_High;
dma.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &dma);

/* Enable DMA1 Channel 1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
adc_val[1]=0;
adc_val[2]=0;
adc_val[3]=0;

/* Configure ADC1 */
adc.ADC_Mode = ADC_Mode_Independent;
Expand All @@ -78,10 +76,10 @@ void adc_init(void){
ADC_Init(ADC1, &adc);

/* Configure ADC1 regular channel1 */
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_28Cycles5);

/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 EOC interrupt */
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);

/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
Expand All @@ -102,3 +100,66 @@ void adc_init(void){
ADC_ExternalTrigConvCmd(ADC1, ENABLE);

}

void adc_set(u8 channel, u32 trig_channel, u8 rising, u8 clear_comm){
u32 tmpreg;

adc_rising = rising;

//ADC_RegularChannelConfig(ADC1, channel, 1,
// ADC_SampleTime_28Cycles5);

ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);

ADC1->SQR3 = channel;

tmpreg = ADC1->CR2;
tmpreg &= 0xFFF1FFFF; //CR2_EXTSEL_Reset;
tmpreg |= trig_channel;
ADC1->CR2 = tmpreg;

if(clear_comm){
#if ADC_COMM_TRIGGER == 1
GPIOC->BRR |= 0x00001000;
#endif
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
adc_old_value = 0;
adc_count = 0;
adc_delay_count = 0;
}

}

void adc1_2_irq_handler(void){
u16 new_value = ADC_GetConversionValue(ADC1);

if(adc_delay_count > 10){
if(adc_rising){
if(new_value <= (adc_old_value+6)){
if(adc_count > 1){
if(!soft_comm)
TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
#if ADC_COMM_TRIGGER == 1
GPIOC->BSRR |= 0x00001000;
#endif
ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);
}else adc_count++;
}else adc_count = 0;
adc_old_value = new_value;
}else{
if(new_value < 1){
if(adc_count > 1){
if(!soft_comm)
TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
#if ADC_COMM_TRIGGER == 1
GPIOC->BSRR |= 0x00001000;
#endif
ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);
}else adc_count++;
}else adc_count = 0;
}
}else adc_delay_count++;

adc_val[0] = new_value;

}
16 changes: 16 additions & 0 deletions source/breadboard_prototype/src/adc.h
Expand Up @@ -19,11 +19,27 @@
#ifndef __ADC_H
#define __ADC_H

#define ADC_CHANNEL_A ADC_Channel_1
#define ADC_CHANNEL_B ADC_Channel_2
#define ADC_CHANNEL_C ADC_Channel_3

#define ADC_TRIG_CHANNEL_A ADC_ExternalTrigConv_T1_CC1
#define ADC_TRIG_CHANNEL_B ADC_ExternalTrigConv_T1_CC2
#define ADC_TRIG_CHANNEL_C ADC_ExternalTrigConv_T1_CC3

#define ADC_FALLIN 0
#define ADC_RISING 1

#define ADC_NO_CLEAR_COMM 0
#define ADC_CLEAR_COMM 1

extern vu16 adc_val[32];

void adc_rcc_init(void);
void adc_nvic_init(void);
void adc_gpio_init(void);
void adc_init(void);
void adc_set(u8 channel, u32 trig_channel, u8 rising, u8 clear_comm);
void adc1_2_irq_handler(void);

#endif /* __ADC_H */
8 changes: 5 additions & 3 deletions source/breadboard_prototype/src/config.h
Expand Up @@ -19,12 +19,14 @@
#ifndef __CONFIG_H
#define __CONFIG_H

#define PWM_PHASE_TRIGGER 5
//#define PWM_PHASE_TRIGGER 5
#define PWM_PHASE_TRIGGER -1
#define ADC_COMM_TRIGGER 1

//#define PWM_SCHEME pwm_scheme_6step_h_pwm_l_on
#define PWM_SCHEME pwm_scheme_6step_h_pwm_l_on
//#define PWM_SCHEME pwm_scheme_6step_h_on_l_pwm
//#define PWM_SCHEME pwm_scheme_6step_on_pwm
//#define PWM_SCHEME pwm_scheme_6step_pwm_on
#define PWM_SCHEME pwm_scheme_12step_pwm_on_pwm
//#define PWM_SCHEME pwm_scheme_12step_pwm_on_pwm

#endif /* __CONFIG_H */
2 changes: 1 addition & 1 deletion source/breadboard_prototype/src/main.c
Expand Up @@ -112,7 +112,7 @@ void sys_tick_init(void){
* We are using the SysTick only to force commutations for now so we trigger
* the SysTick only when we need to commutate.
*/
SysTick_SetReload(72*2*750);
SysTick_SetReload(72*2*750*2);
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick_ITConfig(ENABLE);
}
Expand Down
15 changes: 14 additions & 1 deletion source/breadboard_prototype/src/pwm_scheme_6step_h_pwm_l_on.c
Expand Up @@ -46,16 +46,19 @@
#include <stm32/lib.h>

#include "pwm_utils.h"
#include "adc.h"

#include "pwm_scheme_6step_h_pwm_l_on.h"

void pwm_scheme_6step_h_pwm_l_on(void){
static int pwm_phase =1;
static int pwm_phase = 1;

switch(pwm_phase){
case 1: // 000º
pwm_trigger(1);

adc_set(ADC_CHANNEL_B, ADC_TRIG_CHANNEL_A, ADC_FALLIN, ADC_CLEAR_COMM);

/* Configure step 2 */
pwm_set_pwm_hi(PWM_PHASE_A);
pwm_set_____lo(PWM_PHASE_B);
Expand All @@ -66,6 +69,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
case 2: // 060º
pwm_trigger(2);

adc_set(ADC_CHANNEL_C, ADC_TRIG_CHANNEL_A, ADC_RISING, ADC_CLEAR_COMM);

/* Configure step 3 */
pwm_set____off(PWM_PHASE_A);
pwm_set_____lo(PWM_PHASE_B);
Expand All @@ -76,6 +81,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
case 3: // 120º
pwm_trigger(3);

adc_set(ADC_CHANNEL_A, ADC_TRIG_CHANNEL_C, ADC_FALLIN, ADC_CLEAR_COMM);

/* Configure step 4 */
pwm_set_____lo(PWM_PHASE_A);
pwm_set____off(PWM_PHASE_B);
Expand All @@ -86,6 +93,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
case 4: // 180º
pwm_trigger(4);

adc_set(ADC_CHANNEL_B, ADC_TRIG_CHANNEL_C, ADC_RISING, ADC_CLEAR_COMM);

/* Configure step 4 */
pwm_set_____lo(PWM_PHASE_A);
pwm_set_pwm_hi(PWM_PHASE_B);
Expand All @@ -96,6 +105,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
case 5: // 220º
pwm_trigger(5);

adc_set(ADC_CHANNEL_C, ADC_TRIG_CHANNEL_B, ADC_FALLIN, ADC_CLEAR_COMM);

/* Configure step 4 */
pwm_set____off(PWM_PHASE_A);
pwm_set_pwm_hi(PWM_PHASE_B);
Expand All @@ -106,6 +117,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
case 6: // 280º
pwm_trigger(6);

adc_set(ADC_CHANNEL_A, ADC_TRIG_CHANNEL_B, ADC_RISING, ADC_CLEAR_COMM);

/* Configure step 4 */
pwm_set_pwm_hi(PWM_PHASE_A);
pwm_set____off(PWM_PHASE_B);
Expand Down
11 changes: 8 additions & 3 deletions source/breadboard_prototype/src/soft_timer.c
Expand Up @@ -18,16 +18,21 @@

#include <stm32/lib.h>

#include "adc.h"

#include "soft_timer.h"

int comm_timer = 0;
volatile u32 comm_timer_reload = 72*2*750;
volatile u32 comm_timer_reload = 72*2*750*2;
int cnt = 100;
vu8 soft_comm = 1;

void sys_tick_handler(void){
/* generate a TIM1 COM event */
TIM_GenerateEvent(TIM1, TIM_EventSource_COM | TIM_EventSource_Update);

//TIM_GenerateEvent(TIM1, TIM_EventSource_COM | TIM_EventSource_Update);
if(soft_comm)
TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
//adc_val[((31-DMA_GetCurrDataCounter(DMA1_Channel1))%32)]|=0xF000;
#if 0
if(led_state){
GPIOC->BRR |= 0x00001000;
Expand Down
1 change: 1 addition & 0 deletions source/breadboard_prototype/src/soft_timer.h
Expand Up @@ -20,6 +20,7 @@
#define __SOFT_TIMER_H

extern volatile u32 comm_timer_reload;
extern vu8 soft_comm;

void sys_tick_handler(void);

Expand Down
8 changes: 6 additions & 2 deletions source/breadboard_prototype/src/usart.c
Expand Up @@ -104,11 +104,11 @@ void usart3_irq_handler(void){
if(pwm_val < 1989) pwm_val+=1;
break;
case 'e':
if(comm_timer_reload > 72*2) comm_timer_reload-=72*2;
if(comm_timer_reload > 72*2) comm_timer_reload-=72*2*2;
SysTick_SetReload(comm_timer_reload);
break;
case 'f':
if(comm_timer_reload < 200000-72*2) comm_timer_reload+=72*2;
if(comm_timer_reload < 200000-72*2) comm_timer_reload+=72*2*2;
SysTick_SetReload(comm_timer_reload);
break;
case 'g':
Expand All @@ -122,6 +122,10 @@ void usart3_irq_handler(void){
out_data_counter = 0;
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
break;
case 'h':
if(soft_comm) soft_comm = 0;
else soft_comm = 1;
break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion source/breadboard_prototype/src/vector_table.c
Expand Up @@ -22,6 +22,7 @@
#include "pwm.h"
#include "usart.h"
#include "soft_timer.h"
#include "adc.h"
#include "exceptions.h"

#include "vector_table.h"
Expand Down Expand Up @@ -73,7 +74,7 @@ void (* const vector_table[])(void) = {
null_handler, /* dma1_channel5_irq_handler */
null_handler, /* dma1_channel6_irq_handler */
null_handler, /* dma1_channel7_irq_handler */
null_handler, /* adc1_2_irq_handler */
adc1_2_irq_handler, /* adc1_2_irq_handler */
null_handler, /* usb_hp_can_tx_irq_handler */
null_handler, /* usb_lp_can_rx0_irq_handler */
null_handler, /* can_rx1_irq_handler */
Expand Down

0 comments on commit 4ac0bf8

Please sign in to comment.