Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-dev-f303 FastPWM3
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->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // enable the clock to GPIOC 00011 RCC->APB1ENR |= 0x00000001; // enable TIM2 clock 00012 RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // enable TIM1 clock 00013 00014 // GPIOC->MODER |= (1 << 10); // set pin 5 to be general purpose output for LED 00015 GPIOB->MODER |= (1); // pB_0, output 00016 GPIOB->MODER |= (1 << 2); // PB_1, output 00017 00018 00019 gpio->enable = new DigitalOut(ENABLE_PIN); 00020 gpio->pwm_u = new FastPWM(PIN_U); 00021 gpio->pwm_v = new FastPWM(PIN_V); 00022 gpio->pwm_w = new FastPWM(PIN_W); 00023 00024 00025 00026 //ISR Setup 00027 00028 NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ 00029 00030 TIM1->DIER |= TIM_DIER_UIE; // enable update interrupt 00031 TIM1->CR1 = 0x40; // CMS = 10, interrupt only when counting up 00032 TIM1->CR1 |= TIM_CR1_UDIS; 00033 TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on, 00034 TIM1->RCR |= 0x001; // update event once per up/down count of tim1 00035 TIM1->EGR |= TIM_EGR_UG; 00036 00037 //PWM Setup 00038 00039 TIM1->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock 00040 TIM1->ARR = PWM_ARR; // set auto reload, 40 khz 00041 TIM1->CCER |= ~(TIM_CCER_CC1NP); // Interupt when low side is on. 00042 TIM1->CR1 |= TIM_CR1_CEN; // enable TIM1 00043 00044 } 00045 00046 void Init_ADC(void){ 00047 // ADC Setup 00048 RCC->APB2ENR |= RCC_APB2ENR_ADC3EN; // clock for ADC3 00049 RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2 00050 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1 00051 00052 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable clock for GPIOC 00053 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Enable clock for GPIOA 00054 00055 ADC->CCR = 0x00000016; // Regular simultaneous mode only 00056 ADC1->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC1 ON 00057 ADC1->SQR3 = 0x000000A; // use PC_0 as input- ADC1_IN0 00058 ADC2->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC2 ON 00059 ADC2->SQR3 = 0x0000000B; // use PC_1 as input - ADC2_IN11 00060 ADC3->CR2 |= ADC_CR2_ADON; // ADC3 ON 00061 // ADC3->SQR3 = 0x00000000; // use PA_0, - ADC3_IN0 00062 ADC3->SQR3 = 0x0000000D; // PC_4 a, ADC3)IN0 00063 // GPIOC->MODER |= 0x0000000f; // Alternate function, PC_0, PC_1 are analog inputs 00064 GPIOC->MODER |= 0x000000cf; 00065 00066 // GPIOA->MODER |= 0x3; // PA_0 as analog input 00067 00068 ADC1->SMPR1 |= 0x1; // 15 cycles on CH_10, 0b 001 00069 ADC2->SMPR1 |= 0x8; // 15 cycles on CH_11, 0b 0001 000 00070 // ADC3->SMPR2 |= 0x1; // 15 cycles on CH_0, 0b 001; 00071 ADC3->SMPR1 |= 0x0200; // 15 cycles on CH_13, 0b 001 000 000 000 00072 00073 00074 00075 } 00076 00077 void Init_DAC(void){ 00078 RCC->APB1ENR |= 0x20000000; // Enable clock for DAC 00079 DAC->CR |= 0x00000001; // DAC control reg, both channels ON 00080 GPIOA->MODER |= 0x00000300; // PA04 as analog output 00081 } 00082 00083 void Init_All_HW(GPIOStruct *gpio){ 00084 Init_PWM(gpio); 00085 Init_ADC(); 00086 gpio->led = new DigitalOut(LED); 00087 //Init_DAC(); 00088 00089 }
Generated on Wed Jul 13 2022 15:44:18 by
1.7.2