<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -18,11 +18,18 @@
 
 #include &lt;stm32/lib.h&gt;
 
+#include &quot;config.h&quot;
+#include &quot;soft_timer.h&quot;
+
 #include &quot;adc.h&quot;
 
 #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 */
@@ -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(&amp;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, &amp;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, &amp;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;
@@ -78,10 +76,10 @@ void adc_init(void){
     ADC_Init(ADC1, &amp;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);
@@ -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-&gt;SQR3 = channel;
+
+    tmpreg = ADC1-&gt;CR2;
+    tmpreg &amp;= 0xFFF1FFFF; //CR2_EXTSEL_Reset;
+    tmpreg |= trig_channel;
+    ADC1-&gt;CR2 = tmpreg;
+
+    if(clear_comm){
+#if ADC_COMM_TRIGGER == 1
+        GPIOC-&gt;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 &gt; 10){
+        if(adc_rising){
+            if(new_value &lt;= (adc_old_value+6)){
+                if(adc_count &gt; 1){
+                    if(!soft_comm)
+                        TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
+#if ADC_COMM_TRIGGER == 1
+                    GPIOC-&gt;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 &lt; 1){
+                if(adc_count &gt; 1){
+                    if(!soft_comm)
+                        TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
+#if ADC_COMM_TRIGGER == 1
+                    GPIOC-&gt;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;
+
+}</diff>
      <filename>source/breadboard_prototype/src/adc.c</filename>
    </modified>
    <modified>
      <diff>@@ -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 */</diff>
      <filename>source/breadboard_prototype/src/adc.h</filename>
    </modified>
    <modified>
      <diff>@@ -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 */</diff>
      <filename>source/breadboard_prototype/src/config.h</filename>
    </modified>
    <modified>
      <diff>@@ -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);
 }</diff>
      <filename>source/breadboard_prototype/src/main.c</filename>
    </modified>
    <modified>
      <diff>@@ -46,16 +46,19 @@
 #include &lt;stm32/lib.h&gt;
 
 #include &quot;pwm_utils.h&quot;
+#include &quot;adc.h&quot;
 
 #include &quot;pwm_scheme_6step_h_pwm_l_on.h&quot;
 
 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&#186;
         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);
@@ -66,6 +69,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
     case 2: // 060&#186;
         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);
@@ -76,6 +81,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
     case 3: // 120&#186;
         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);
@@ -86,6 +93,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
     case 4: // 180&#186;
         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);
@@ -96,6 +105,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
     case 5: // 220&#186;
         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);
@@ -106,6 +117,8 @@ void pwm_scheme_6step_h_pwm_l_on(void){
     case 6: // 280&#186;
         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);</diff>
      <filename>source/breadboard_prototype/src/pwm_scheme_6step_h_pwm_l_on.c</filename>
    </modified>
    <modified>
      <diff>@@ -18,16 +18,21 @@
 
 #include &lt;stm32/lib.h&gt;
 
+#include &quot;adc.h&quot;
+
 #include &quot;soft_timer.h&quot;
 
 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-&gt;BRR |= 0x00001000;</diff>
      <filename>source/breadboard_prototype/src/soft_timer.c</filename>
    </modified>
    <modified>
      <diff>@@ -20,6 +20,7 @@
 #define __SOFT_TIMER_H
 
 extern volatile u32 comm_timer_reload;
+extern vu8 soft_comm;
 
 void sys_tick_handler(void);
 </diff>
      <filename>source/breadboard_prototype/src/soft_timer.h</filename>
    </modified>
    <modified>
      <diff>@@ -104,11 +104,11 @@ void usart3_irq_handler(void){
             if(pwm_val &lt; 1989) pwm_val+=1;
             break;
         case 'e':
-            if(comm_timer_reload &gt; 72*2) comm_timer_reload-=72*2;
+            if(comm_timer_reload &gt; 72*2) comm_timer_reload-=72*2*2;
             SysTick_SetReload(comm_timer_reload);
             break;
         case 'f':
-            if(comm_timer_reload &lt; 200000-72*2) comm_timer_reload+=72*2;
+            if(comm_timer_reload &lt; 200000-72*2) comm_timer_reload+=72*2*2;
             SysTick_SetReload(comm_timer_reload);
             break;
         case 'g':
@@ -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;
 	}
     }
 </diff>
      <filename>source/breadboard_prototype/src/usart.c</filename>
    </modified>
    <modified>
      <diff>@@ -22,6 +22,7 @@
 #include &quot;pwm.h&quot;
 #include &quot;usart.h&quot;
 #include &quot;soft_timer.h&quot;
+#include &quot;adc.h&quot;
 #include &quot;exceptions.h&quot;
 
 #include &quot;vector_table.h&quot;
@@ -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 */</diff>
      <filename>source/breadboard_prototype/src/vector_table.c</filename>
    </modified>
    <modified>
      <diff>@@ -81,7 +81,8 @@ void inc_rot(){
 }
 
 static void update_display(void) {
-    int i;
+    int i, j;
+    static unsigned int a=0, b=0, c=0;
 
     inc_rot();
     wclear(screen);
@@ -95,8 +96,20 @@ static void update_display(void) {
     mvwprintw(screen,5,2,&quot;Comm Force: %u&quot;, in_data.comm_force_time);
     mvwprintw(screen,6,2,&quot;ADC Mean: %u&quot;, adc_mean);
     mvwprintw(screen,7,2,&quot;ADC Dev: %u&quot;, adc_dev);
-    for(i=0; i&lt;32; i++)
-        mvwprintw(screen,8+i,2,&quot;ADC[%02u]: %u&quot;, i, in_data.adc[i]);
+    #if 0
+    #endif
+    i=0;
+    //while((! (in_data.adc[i] &amp; 0xF000)) &amp;&amp; i &lt; 32) i++;
+    //for(j=0; j&lt;32; j++){
+    //    if(in_data.adc[(i+j)%32] &amp; 0xF000)
+    //        mvwprintw(screen,8+j,2,&quot;ADC[%02u]*: %u&quot;, (i+j)%32, in_data.adc[(i+j)^32]&amp;0xFFF);
+    //    else
+    //        mvwprintw(screen,8+j,2,&quot;ADC[%02u] : %u&quot;, (i+j)%32, in_data.adc[(i+j)%32]);
+    //}
+    mvwprintw(screen,8,2,&quot;ADC[0] : %u&quot;, in_data.adc[0]);
+    mvwprintw(screen,9,2,&quot;ADC[1] : %u&quot;, in_data.adc[1]);
+    mvwprintw(screen,10,2,&quot;ADC[2] : %u&quot;, in_data.adc[2]);
+    mvwprintw(screen,11,2,&quot;ADC[3] : %u&quot;, in_data.adc[3]);
     wrefresh(screen);
     refresh();
 }
@@ -119,7 +132,7 @@ int main(int argc, char **argv){
     nodelay(mainwnd, TRUE);
     refresh();
     wrefresh(mainwnd);
-    screen = newwin(9+32, 27, 1, 1);
+    screen = newwin(9+4, 27, 1, 1);
     box(screen, ACS_VLINE, ACS_HLINE);
 
     ret = s_open(&quot;A6004kZx&quot;, 38400);
@@ -210,13 +223,17 @@ int main(int argc, char **argv){
         getch_ret = getch();
 
         if((getch_ret != ERR) &amp;&amp; (getch_ret != dat1[0])){
-            dat2[0]=getch_ret;
-            s_write(dat2, 1);
-
-            if(ret != 1){
-                printf(&quot;write error\n&quot;);
-                s_close();
-                return 0;
+            if(getch_ret == 'p'){
+                while(getch() != 'p');
+            }else{
+                dat2[0]=getch_ret;
+                s_write(dat2, 1);
+
+                if(ret != 1){
+                    printf(&quot;write error\n&quot;);
+                    s_close();
+                    return 0;
+                }
             }
         }
 </diff>
      <filename>source/obldc-tool/main.c</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>477366d935e3dd8e8033e4d37f1f583c4c672ca3</id>
    </parent>
  </parents>
  <author>
    <name>Piotr Esden-Tempski</name>
    <login>esden</login>
    <email>piotr@esden.net</email>
  </author>
  <url>http://github.com/esden/open-bldc/commit/4ac0bf84802d08cf76c4055c82aa05ea4c983c33</url>
  <id>4ac0bf84802d08cf76c4055c82aa05ea4c983c33</id>
  <committed-date>2009-05-06T17:28:25-07:00</committed-date>
  <authored-date>2009-05-06T17:28:25-07:00</authored-date>
  <message>Commutation detection works! Still the code needs cleanup and a lot of tuning!</message>
  <tree>07d410ec7726a29086fac7c591730321c3b11746</tree>
  <committer>
    <name>Piotr Esden-Tempski</name>
    <login>esden</login>
    <email>piotr@esden.net</email>
  </committer>
</commit>
