Jared DiCarlo
/
george
motor controller
Diff: io.cpp
- Revision:
- 0:f899a5183b5e
- Child:
- 1:2acd7dfc4b1b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/io.cpp Mon Oct 03 04:56:06 2016 +0000 @@ -0,0 +1,167 @@ +#include "mbed.h" +#include "config.h" +#include "io.h" +#include "foc.h" + +Serial pc_ser(USBTX, USBRX); +Serial debug_ser(PC_10, PC_11); +Serial panel_ser(PC_6, PC_7); + +volatile bool enabled = false; +volatile bool error = false; + +bool getError() +{ + return error; +} + +void setError() +{ + error = true; +} + +bool getEnabled() +{ + return enabled; +} + +void setEnabled(bool _enabled) +{ + enabled = _enabled; +} +void setup() +{ + getSerial(0)->baud(SERIAL_BAUD); //set serial baud + getSerial(1)->baud(SERIAL_BAUD); //set serial baud + getSerial(2)->baud(SERIAL_BAUD); //set serial baud + + getSerial(0)->printf("GEORGE\n"); + getSerial(0)->printf("*******INITIALIZING**********\n"); + + getSerial(0)->printf("Setting up clocks...\n"); + /************************************* + CLOCKS + **************************************/ + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //gpio a + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; //gpio b + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; //gpio c + RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //tim1 + RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //adc1 + RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; //adc2 + RCC->APB1ENR |= RCC_APB1ENR_DACEN; //dac + + getSerial(0)->printf("Setting up interrupts...\n"); + /************************************* + INTERRUPTS + **************************************/ + NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //setup interrupt controller + TIM1->DIER |= TIM_DIER_UIE; //enable timer interrupts + + getSerial(0)->printf("Setting up PWM...\n"); + /************************************ + PWM CONFIGURATION + *************************************/ + //set center aligned mode bits to 10 (might be 01 though). + TIM1->CR1 |= TIM_CR1_CMS_1; //counts up and down, but only interrupts when counting up. + TIM1->EGR |= TIM_EGR_UG; //reference manual says I need to go this. + //enable and set polarity of output compares (using 1, 2, 3) + TIM1->CCER |= TIM_CCER_CC3E; + TIM1->CCER |= TIM_CCER_CC3P; + TIM1->CCER |= TIM_CCER_CC2E; + TIM1->CCER |= TIM_CCER_CC2P; + TIM1->CCER |= TIM_CCER_CC1E; + TIM1->CCER |= TIM_CCER_CC1P; + + getSerial(0)->printf("Setting up Timers...\n"); + /************************************ + TIMER CONFIGURATION + *************************************/ + //buffer the auto reload register (ARR) to update only on update event + TIM1->CR1 |= TIM_CR1_ARPE; + TIM1->RCR |= 0x01; //repetition counter set for 2 repetitions + TIM1->PSC = 0x0; //no prescaler. + //set counter maximum to value for 10 kHz PWM + TIM1->ARR = 0x2328; //this is for 20 kHz, which is think is a needed correction. + //counter needs to do two cycles for 1 full pwm cycle I think. + //this is due to the center aligned mode. + + getSerial(0)->printf("Setting up ADC...\n"); + /************************************ + ADC CONFIG + *************************************/ + ADC->CCR |= 0b110; //set adc 1, 2 to sample together. + ADC1->SQR3 |= 0x4; //on ADC1, sample ADC12_IN channel 4 (PA4) + ADC2->SQR3 |= 0x8; //on ADC2, sample ADC12_IN channel 8 (PB0) + + getSerial(0)->printf("Setting up GPIO...\n"); + getSerial(0)->printf("PWM pins are set up as 'alternate function'...\n"); + getSerial(0)->printf("May eventually need to set speed, pullup...\n"); + /************************************ + GPIO CONFIG + *************************************/ + //pwm outs + //0x2A0000 = 0b 10 10 10 00 00 00 00 00 00 00 00 + GPIOA->MODER |= 0x2A0000; + // 109876543210 + GPIOA->MODER |= 0b001100000000; //PA4 as analog. + GPIOB->MODER |= 0b000000000011; //PB0 as analog. + GPIOA->MODER |= 0b110000000000; //PA5 as analog. + + getSerial(0)->printf("Turning on ADC/DAC...\n"); + /************************************ + ADC/DAC START + *************************************/ + ADC1->CR2 |= ADC_CR2_ADON; //adc 1 on + ADC2->CR2 |= ADC_CR2_ADON; //adc 2 on + DAC->CR |= DAC_CR_EN2; //dac 2 (PA5) on + + getSerial(0)->printf("Turning on TIMER\n"); + /************************************ + TIMER START + *************************************/ + TIM1->CR1 |= TIM_CR1_CEN; //enable! + wait_us(100000); + + /************************************ + ZERO CURRENT + *************************************/ + + /************************************ + ENCODER + *************************************/ + + /************************************ + FRONT PANEL BOARD + *************************************/ + +} + +Serial *getSerial(int port) +{ + switch(port) + { + case 0: + return &pc_ser; + case 1: + return &debug_ser; + case 2: + return &panel_ser; + } + return &pc_ser; +} + +extern "C" void TIM1_UP_TIM10_IRQHandler(void) +{ + if(TIM1->SR & TIM_SR_UIF) { + if(getEnabled() && !getError()) sampleCurrent(); + //else go turn off enabled pin. + } + TIM1->SR = 0; +} + +void set_inverter(*Inverter invt) +{ + TIM1->CCR1 = volts_to_ticks(invt->v_a); + TIM1->CCR2 = volts_to_ticks(invt->v_b); + TIM1->CCR3 = volts_to_ticks(invt->v_c); +} \ No newline at end of file