Commits

tuck...@gmail.com  committed 900a822 Draft

add new params to pwm mode

  • Participants
  • Parent commits 260fca0

Comments (0)

Files changed (1)

File src/platform/stm32f4/gpio.py

   PmReturn_t retval = PM_RET_OK;
 
   /* compute the prescaler value */
+  // Get clock to 1 MHz on STM32F4
+  uint16_t prescalerValue = (uint16_t) ((SystemCoreClock / 1000000)) - 1;
+  pPmObj_t p_period;
 
-  // Get clock to 1 MHz on STM32F4
-  uint16_t prescalerValue = (uint16_t) ((SystemCoreClock / 1000000) / 2) - 1;
+  // check if third parameter is the period
+  // for example period can be set to 20000 (20 KHz)
+  p_period = NATIVE_GET_LOCAL(2);
+  if (OBJ_GET_TYPE(p_period) != OBJ_TYPE_INT)
+  {
+    PM_RAISE(retval, PM_RET_EX_TYPE);
+    return retval;
+  }
 
   // 20 KHz for 1MHz prescaled
-  uint32_t period = 1000000 / 20000;
-
+  uint32_t period = (1000000 / ((pPmInt_t)p_period)->val ) - 1;
 
   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   /* Time base configuration */
-  TIM_TimeBaseStructure.TIM_Period = period - 1;
+  TIM_TimeBaseStructure.TIM_Period = period;
   TIM_TimeBaseStructure.TIM_Prescaler = prescalerValue;
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
         return retval;
   }
   TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);
-  /* Enable TIM 1/8 Preload register on ARR */
+  /* Enable TIM 1 or 8 Preload register on ARR */
   TIM_ARRPreloadConfig(TIMx, ENABLE);
 }
 
   TIM_OCInitTypeDef TIM_OCInitStructure;
 
   // The difference between PWM1 and PWM2 is whether you are controlling the ON or OFF periods
-  // We are going to controle ON
-  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
+  // We are going to controle OFF
+  // this means, if value grow, led will shine :)
+  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 
   // The TIM_Pulse defines the delay value 
         return retval;
   }
 
-  /* TIM 1/8 Main Output Enable */
+  /* TIM 1 or 8 Main Output Enable */
   TIM_CtrlPWMOutputs(TIMx, ENABLE);
   TIM_Cmd(TIMx, ENABLE);
 
 }
 
 /**
-*   set the output value for the timer
+*   set the output value for the timer  this is shortcut for :
+*       normaly period should be computed like so :
+*       pulse = (percent_value * (period - 1)) / 100
+*       TIMx->CCRx = value;
+*       where TIMx is TIM1 for example
+*            and CCRx is CCR1 for channel 1 for example
 *
 **/
 void _set_time_compare(TIM_TypeDef* TIMx, uint8_t channel_num, uint32_t value){
+  uint32_t current_period = TIMx->ARR;
+  uint32_t pulse = (value*(current_period-1))/100;
+
   if (TIMx == TIM1){
     if (channel_num==1){
-        TIM_SetCompare1(TIM1, value);
+        TIM_SetCompare1(TIM1, pulse);
     } else if (channel_num==2){
-        TIM_SetCompare2(TIM1, value);
+        TIM_SetCompare2(TIM1, pulse);
     } else if (channel_num==3){
-        TIM_SetCompare3(TIM1, value);
+        TIM_SetCompare3(TIM1, pulse);
     } else if (channel_num==4){
-        TIM_SetCompare4(TIM1, value);
+        TIM_SetCompare4(TIM1, pulse);
     }
   } else if (TIMx == TIM8){
     if (channel_num==1){
-        TIM_SetCompare1(TIM8, value);
+        TIM_SetCompare1(TIM8, pulse);
     } else if (channel_num==2){
-        TIM_SetCompare2(TIM8, value);
+        TIM_SetCompare2(TIM8, pulse);
     } else if (channel_num==3){
-        TIM_SetCompare3(TIM8, value);
+        TIM_SetCompare3(TIM8, pulse);
     } else if (channel_num==4){
-        TIM_SetCompare4(TIM8, value);
+        TIM_SetCompare4(TIM8, pulse);
     }
   }
 }
     """
     pass
 
-def _set_pin_pwm(port, pin):
+def _set_pin_pwm(port, pin, period):
     """__NATIVE__
     GPIO_TypeDef *port;
     uint16_t pin_mask;
     uint8_t pin_num;
     TIM_TypeDef* TIMx;
+    uint32_t period;
     uint8_t channel;
     uint8_t GPIO_AF_TIM;
     
     PmReturn_t retval = PM_RET_OK;
     
-    if(NATIVE_GET_NUM_ARGS() != 2)
+    if(NATIVE_GET_NUM_ARGS() != 3)
     {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
     GPIO_InitTypeDef GPIO_InitStructure;
     GPIO_InitStructure.GPIO_Pin = (uint16_t)pin_num;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
     """
     pass
 
-def set_pin_pwm(pin):
+def set_pin_pwm(pin, period=20000):
     """
+    :param pin: tuple for example ('A',8), which shuld be pin.PA8
+            (see pin.py and stm32f4 documentation for more details)
+    :param period: value in Hz : 20000 for 20 KHz
     """
-    _set_pin_pwm(pin[0],pin[1])
+    _set_pin_pwm(pin[0],pin[1],period)
 
-def _digital_pwm_write(port, pin, value):
+def _digital_pwm_write(port, pin, percent_pulse):
     """__NATIVE__
     GPIO_TypeDef *port;
     uint16_t pin_mask;
     pc = NATIVE_GET_LOCAL(2);
 
     /* If the arg is not an integer, raise TypeError */
-    if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT && OBJ_GET_TYPE(pc) != OBJ_TYPE_BOOL)
+    if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT)
     {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
     """
     pass
 
-def digital_pwm_write(pin, value):
+def digital_pwm_write(pin, percent_pulse):
     """
+    pulse should be an integer and a percent
+    => between 1 and 100
     """
-    _digital_pwm_write(pin[0],pin[1], value)
+    if percent_pulse < 0 :
+       percent_pulse = 0
+    if percent_pulse > 100 :
+       percent_pulse = 100
+    _digital_pwm_write(pin[0], pin[1], percent_pulse)
 
 # Reads a single pin of a particular STM32 port
 #