Ben Katz / Mbed 2 deprecated Hobbyking_Cheetah_F334

Dependencies:   CANnucleo FastPWM3 mbed

Fork of Hobbyking_Cheetah_Compact by Ben Katz

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hw_setup.cpp Source File

hw_setup.cpp

00001 
00002 #include "mbed.h"
00003 #include "hw_setup.h"
00004 #include "hw_config.h"
00005 #include "structs.h"
00006 #include "FastPWM.h"
00007 
00008 void Init_PWM(GPIOStruct *gpio){
00009     
00010     //RCC->AHBENR |= RCC_AHBENR_GPIOCEN; // enable the clock to GPIOC
00011     //RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // enable TIM2 clock
00012     //RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // enable TIM1 clock
00013 
00014     //GPIOC->MODER = (1 << 8); // set pin 4 to be general purpose output
00015     gpio->enable = new DigitalOut(ENABLE_PIN);
00016     gpio->pwm_u = new PwmOut(PIN_U);
00017     gpio->pwm_v = new PwmOut(PIN_V);
00018     gpio->pwm_w = new PwmOut(PIN_W);
00019     
00020      //ISR Setup     
00021     
00022     NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);   //Enable TIM1 IRQ
00023 
00024     TIM1->DIER |= TIM_DIER_UIE; // enable update interrupt
00025     TIM1->CR1 |= 0x40;//CMS = 10, interrupt only when counting up
00026     TIM1->CR1 |= TIM_CR1_UDIS;
00027     TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on, 
00028     TIM1->RCR |= 0x001; // update event once per up/down count of4 tim1 
00029     TIM1->EGR |= TIM_EGR_UG;
00030  
00031     //PWM Setup
00032 
00033     TIM1->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
00034     TIM1->ARR = 0x708;    //40 khz
00035     TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
00036     TIM1->CR1 |= TIM_CR1_CEN;    
00037     
00038     }
00039 
00040 void Init_ADC(void){
00041         // ADC Setup
00042      RCC->AHBENR |= RCC_AHBENR_ADC12EN; // clock for ADC1,2
00043      RCC->AHBENR |= RCC_AHBENR_GPIOCEN; // enable the clock to GPIOC
00044      GPIOC->MODER |= 0x0000000f; // PC_0, PC_1 are analog inputs 
00045      ADC12_COMMON->CCR |=  0x6; // Regular simultaneous mode only
00046      ADC12_COMMON->CCR |= 0x0000; //CKMODE = 1;
00047      
00048      
00049      ADC1->SQR1 = 0x180; // use PC_0 as input - CH6
00050      //ADC1->IER |= ADC_IER_EOCIE; //EOC flag on
00051      
00052      ADC2->SQR1 = 0x1C0; // use PC_1 as input - CH7
00053 
00054      ADC1->SMPR1 |= (0x7<<18); //Sample time
00055      ADC2->SMPR1 |= (0x7<<21); //Sample time
00056      
00057      ADC1->CR |= ADC_CR_ADEN; // ADC1 ON
00058      ADC2->CR |= ADC_CR_ADEN; // ADC2 ON
00059      
00060      GPIOC->MODER = (1 << 8); // set pin 4 to be general purpose output
00061      }
00062 
00063 void Init_DAC(void){
00064      RCC->APB1ENR |= RCC_APB1ENR_DAC1EN; // Enable clock for DAC
00065      DAC->CR |= DAC_CR_EN1; // DAC control reg, both channels ON
00066      GPIOA->MODER |= 0x00000300; // PA04 as analog output  
00067     }
00068 
00069 void Init_All_HW(GPIOStruct *gpio){
00070     Init_PWM(gpio);
00071     Init_ADC();
00072     //Init_DAC();
00073     
00074     }