Prius IPM controller

Dependencies:   mbed

Fork of analoghalls5_5 by N K

Revision:
29:cb03760ba9ea
Parent:
27:846c08fb3697
Child:
30:2b6d426f3bfc
--- a/main.cpp	Sat Mar 14 19:18:34 2015 +0000
+++ b/main.cpp	Sat Mar 14 23:42:46 2015 +0000
@@ -7,66 +7,43 @@
 #include "sensors.h"
 #include "callbacks.h"
 
-//NVIC_SetVector(ADC_IRQn, (uint32_t)ADC_IRQHandler);
-//NVIC->IntSelect &= ~(1 << USB_IRQn);
-//NVIC_EnableIRQ(TIM2_CC_IRQn);
-
-
-
-//NVIC_SetPriority(0); //(uint32_t)SPI_IRQn
-//NVIC_EnableIRQ((uint32_t)SPI_IRQn);
-
-int current1, current2;
+unsigned short global_ib, global_ic;
+unsigned char channel_ib, channel_ic;
 
 extern "C" void TIM2_Derpy_Handler()
 {
-  toggler = !toggler;
-  
-  ADC1->SQR3 = 0;  //clear the conversion sequence register
-  ADC1->SQR3 |= 1;  //set the first bit high (read PA_1 a.k.a ADC1_1 a.k.a. A1
-  ADC1->CR2 |= (1 << 30); //(SWSTART = 1) start the conversion
-  while ((ADC1->SR & 2) == 0){} // while bit 1 of ADC_SR is 0 (not done converting yet), do nothing.
-  current1 = ADC1->DR;
-  
-  ADC1->SQR3 |= 4;  //set the first bit high (read PA_4 a.k.a. ADC1_4 a.k.a A2
-  ADC1->CR2 |= (1 << 30); // (SWSTART = 1) start the conversion
-  while ((ADC1->SR & 2) == 0){} // while bit 1 of ADC_SR is 0 (not done converting yet), do nothing.
-  current2 = ADC1->DR;
-  
-  
-  
-  TIM2->SR &= ~1; //clear the LSB, that is the UIF - update interrupt flag.  The flag is set high each time tim_2 overflows and underflows.  we reset it here.
-  toggler = !toggler;
+    TIM2->SR &= ~1;
+
+    TIM2->DIER = 0x0000;
+    ADC1->SQR3 = channel_ib;
+    ADC1->CR2 |= ADC_CR2_SWSTART;
+    while ((ADC1->SR & 2) == 0){}
+    global_ib = ADC1->DR;
+    
+    ADC1->SQR3 = channel_ic;
+    ADC1->CR2 |= ADC_CR2_SWSTART;
+    while ((ADC1->SR & 2) == 0){}
+    TIM2->DIER = 0x0001;
+    global_ic = ADC1->DR;
 } 
 
-
-
-
-DigitalOut toggler(D7);  //to check loop timing
-
 int main() { 
-    
-/*
-    ADC_SQR1 |= (1 << 20);  //we're doing 2 conversions. (p.231)
-    ADC_CR2 |= (1 << 30); //start conversion mode (p.227)
-*/
     NVIC_EnableIRQ(TIM2_IRQn);
-    NVIC_SetVector(TIM2_IRQn, (uint32_t)TIM2_Derpy_Handler);  //just calling it this so it doesn't blend in with the other cryptic register-looking names.
-
-    
+    NVIC_SetVector(TIM2_IRQn, (uint32_t)TIM2_Derpy_Handler);
+        
     Context *context = new Context();
     context->ConfigureOutputs(D6, D13, D3, D8);
-    context->ConfigureCurrentSensors(A1, A2, 0.01f, 0.7f);  // I guess this configures the ADCs or something 
+    context->ConfigurePositionSensor(A4, A5, 0.249f, 0.497f, 0.231f, 0.499f, 205.0f); 
     context->ConfigureIdPidController(0.0000f, 0.0f, 0.0f, 5.0f, -5.0f);
     context->ConfigureIqPidController(0.0001f, 0.1f, 0.0f, 5.0f, -5.0f);
     context->ConfigureThrottle(A0, 0.8f, 3.0f);
-    context->ConfigurePositionSensor(A4, A5, 0.249f, 0.497f, 0.231f, 0.499f, 205.0f); 
+    context->ConfigureCurrentSensors(A1, A2, 0.01f, 0.7f);  // I guess this configures the ADCs or something 
     context->ConfigureReference(5.0f);
     context->ConfigureDebugger(2, 1000);
     context->AttachCallBack(&fast, 5000);
     context->AttachCallBack(&slow, 10);
     context->AttachCallBack(&debug, 10);
     context->AttachCallBack(&log, 500);
-    
+    context->InitData();
     context->Start();
 }