You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/04/08 20:23:35 UTC

incubator-mynewt-core git commit: modified the hal_pwm API to support the comments from the code review

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 251520894 -> d5c39d424


modified the hal_pwm API to support the comments from the code review

1) enable automatically
2) remove wave since its not useful to most


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/d5c39d42
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/d5c39d42
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/d5c39d42

Branch: refs/heads/develop
Commit: d5c39d4247711a5f0be7a88b3a1ad2a6f6ef947d
Parents: 2515208
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Tue Apr 5 17:06:49 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Fri Apr 8 11:21:51 2016 -0700

----------------------------------------------------------------------
 hw/hal/include/hal/hal_pwm.h     | 62 +++++++++++++----------------------
 hw/hal/include/hal/hal_pwm_int.h | 14 ++++----
 hw/hal/src/hal_pwm.c             | 31 ++++++------------
 hw/mcu/native/src/hal_pwm.c      | 34 +++----------------
 4 files changed, 46 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5c39d42/hw/hal/include/hal/hal_pwm.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_pwm.h b/hw/hal/include/hal/hal_pwm.h
index ddbb994..8eabc70 100644
--- a/hw/hal/include/hal/hal_pwm.h
+++ b/hw/hal/include/hal/hal_pwm.h
@@ -29,58 +29,42 @@
 struct hal_pwm;
 
 /* Initialize a new PWM device with the given system id.
- * Returns negative on error, 0 on success*/
+ * Returns negative on error, 0 on success. */
 struct hal_pwm*
 hal_pwm_init(enum system_device_id sysid);
 
-/* enables the PWM corresponding to PWM *ppwm.*/
-int
-hal_pwm_on(struct hal_pwm *ppwm);
+/* gets the underlying clock driving the PWM output. Return value
+ * is in Hz. Returns negative on error */
+int 
+hal_pwm_get_source_clock_freq(struct hal_pwm *ppwm);
 
-/* disables the PWM corresponding to PWM *ppwm.*/
+/* gets the resolution of the PWM in bits.  An N-bit PWM can have 
+ * on values between 0 and 2^bits - 1. Returns negative on error  */
 int
-hal_pwm_off(struct hal_pwm *ppwm);
+hal_pwm_get_resolution_bits(struct hal_pwm *ppwm);
 
-/* There are two APIs for setting the PWM waveform.  You can use one 
- *  or both in your programs, but only the last one called will apply
- *
- * hal_pwm_set_duty_cycle -- sets the PWM waveform for a specific duty cycle
- *          It uses a 255 clock period and sets the on and off time 
- *          according to the argument
- * 
- * hal_pwm_set_waveform -- sets the PWM waveform for a specific on time
- *          and period specified in PWM clocks.  its intended for more
- *          fine-grained control of the PWM waveform.  
- *
- */
+/* turns off the PWM channel */
+int
+hal_pwm_disable(struct hal_pwm *ppwm);
 
-/* sets the duty cycle of the PWM output. This duty cycle is 
- * a fractional duty cycle where 0 == off, 255 = on, and 
+/* enables the PWM with duty cycle specified. This duty cycle is 
+ * a fractional duty cycle where 0 == off, 65535=on, and 
  * any value in between is on for fraction clocks and off
- * for 255-fraction clocks. 
- * When you are looking for more fine-grained control over
- * the PWM, use the API set_waveform below.
+ * for 65535-fraction clocks.  
  */
 int
-hal_pwm_set_duty_cycle(struct hal_pwm *ppwm, uint8_t fraction);
+hal_pwm_enable_duty_cycle(struct hal_pwm *ppwm, uint16_t fraction);
 
-
-/* Sets the pwm waveform period and on-time in units of the PWM clock 
-  (see below).  Period_clocks and on_clocks cannot exceed the 
- * 2^N-1 where N is the resolution of the PWM channel  */
+/* 
+ * This frequency must be between 1/2 the clock frequency and 
+ * the clock divided by the resolution. NOTE: This may affect
+ * other PWM channels.
+ */
 int
-hal_pwm_set_waveform(struct hal_pwm *ppwm, uint32_t period_clocks, uint32_t on_clocks);
-
-
-/* gets the underlying clock driving the PWM output. Return value
- * is in Hz. Returns negative on error */
-int 
-hal_pwm_get_clock_freq(struct hal_pwm *ppwm);
+hal_pwm_set_frequency(struct hal_pwm *ppwm, uint32_t freq_hz);
 
-/* gets the resolution of the PWM in bits.  An N-bit PWM can have 
- * period and on values between 0 and 2^bits - 1. Returns negative on error  */
-int
-hal_pwm_get_resolution_bits(struct hal_pwm *ppwm);
+/* NOTE: If you know the resolution and clock frequency, you can
+ * compute the period of the PWM Its 2^resolution/clock_freq */
 
 
 #endif /* _HAL_HAL_PWM_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5c39d42/hw/hal/include/hal/hal_pwm_int.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_pwm_int.h b/hw/hal/include/hal/hal_pwm_int.h
index 3570484..f47233c 100644
--- a/hw/hal/include/hal/hal_pwm_int.h
+++ b/hw/hal/include/hal/hal_pwm_int.h
@@ -31,12 +31,14 @@ struct hal_pwm;
 
 struct hal_pwm_funcs 
 {
-    int (*hpwm_on)        (struct hal_pwm *ppwm);
-    int (*hpwm_off)       (struct hal_pwm *ppwm);
-    int (*hpwm_get_bits)  (struct hal_pwm *ppwm);
-    int (*hpwm_get_clk)   (struct hal_pwm *ppwm);
-    int (*hpwm_set_duty)  (struct hal_pwm *ppwm, uint8_t frac_duty);
-    int (*hpwm_set_wave)  (struct hal_pwm *ppwm, uint32_t period, uint32_t on);
+
+    /* the low level hal API */
+    int     (*hpwm_get_bits)        (struct hal_pwm *ppwm);
+    int     (*hpwm_get_clk)         (struct hal_pwm *ppwm);
+    int     (*hpwm_disable)         (struct hal_pwm *ppwm);
+    int     (*hpwm_ena_duty)  (struct hal_pwm *ppwm, uint16_t frac_duty);
+    int     (*hpwm_set_freq)  (struct hal_pwm *ppwm, uint32_t freq_hz);
+    
 };
 
 struct hal_pwm 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5c39d42/hw/hal/src/hal_pwm.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_pwm.c b/hw/hal/src/hal_pwm.c
index 34c30fa..b20b2e0 100644
--- a/hw/hal/src/hal_pwm.c
+++ b/hw/hal/src/hal_pwm.c
@@ -26,40 +26,31 @@ hal_pwm_init(enum system_device_id sysid)
     return bsp_get_hal_pwm_driver(sysid);
 }
 
-int
-hal_pwm_on(struct hal_pwm *ppwm) 
-{
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_on ) 
-    {
-        return ppwm->driver_api->hpwm_on(ppwm);
-    }
-    return -1;  
-}
 
 int
-hal_pwm_off(struct hal_pwm *ppwm) 
+hal_pwm_disable(struct hal_pwm *ppwm) 
 {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_off ) 
+    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_disable ) 
     {
-        return ppwm->driver_api->hpwm_off(ppwm);
+        return ppwm->driver_api->hpwm_disable(ppwm);
     }
     return -1;      
 }
 
 int
-hal_pwm_set_duty_cycle(struct hal_pwm *ppwm, uint8_t fraction) {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_set_duty ) 
+hal_pwm_enable_duty_cycle(struct hal_pwm *ppwm, uint16_t fraction) {
+    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_ena_duty ) 
     {
-        return ppwm->driver_api->hpwm_set_duty(ppwm, fraction);
+        return ppwm->driver_api->hpwm_ena_duty(ppwm, fraction);
     }
     return -1;      
 }
 
 int
-hal_pwm_set_waveform(struct hal_pwm *ppwm, uint32_t period_clocks, uint32_t on_clocks) {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_set_wave ) 
+hal_pwm_set_frequency(struct hal_pwm *ppwm, uint32_t freq_hz) {
+    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_set_freq) 
     {
-        return ppwm->driver_api->hpwm_set_wave(ppwm, period_clocks, on_clocks);
+        return ppwm->driver_api->hpwm_set_freq(ppwm, freq_hz);
     }
     return -1;       
 }
@@ -73,8 +64,6 @@ hal_pwm_get_clock_freq(struct hal_pwm *ppwm) {
     return -1;     
 }
 
-/* gets the resolution of the PWM in bits.  An N-bit PWM can have 
- * period and on values between 0 and 2^bits - 1 */
 int
 hal_pwm_get_resolution_bits(struct hal_pwm *ppwm) {
     if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_get_bits ) 
@@ -82,4 +71,4 @@ hal_pwm_get_resolution_bits(struct hal_pwm *ppwm) {
         return ppwm->driver_api->hpwm_get_bits(ppwm);
     }
     return -1;        
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5c39d42/hw/mcu/native/src/hal_pwm.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_pwm.c b/hw/mcu/native/src/hal_pwm.c
index 36951d2..5d0688b 100644
--- a/hw/mcu/native/src/hal_pwm.c
+++ b/hw/mcu/native/src/hal_pwm.c
@@ -37,9 +37,7 @@ struct native_pwm_drv {
 };
 
 static int native_pwm_off(struct hal_pwm *ppwm);
-static int native_pwm_on(struct hal_pwm *ppwm);
-static int native_pwm_set_wave(struct hal_pwm *ppwm, uint32_t period, uint32_t on);
-static int native_pwm_set_duty(struct hal_pwm *ppwm, uint8_t frac_duty);
+static int native_pwm_enable_duty(struct hal_pwm *ppwm, uint16_t frac_duty);
 static int native_pwm_get_bits(struct hal_pwm *ppwm);
 static int native_pwm_get_clock(struct hal_pwm *ppwm);
 
@@ -48,10 +46,8 @@ static const struct hal_pwm_funcs  native_pwm_funcs =
 {
     .hpwm_get_bits = &native_pwm_get_bits,
     .hpwm_get_clk = &native_pwm_get_clock,
-    .hpwm_off = &native_pwm_off,
-    .hpwm_on = &native_pwm_on,
-    .hpwm_set_duty = &native_pwm_set_duty,
-    .hpwm_set_wave = &native_pwm_set_wave,
+    .hpwm_disable = &native_pwm_off,
+    .hpwm_ena_duty = &native_pwm_enable_duty,
 };
 
 struct hal_pwm *
@@ -122,36 +118,16 @@ static int native_pwm_get_clock(struct hal_pwm *ppwm) {
 }
 
 int 
-native_pwm_set_duty(struct hal_pwm *ppwm, uint8_t fraction)
+native_pwm_enable_duty(struct hal_pwm *ppwm, uint16_t fraction)
 {
     struct native_pwm_drv *pn = (struct native_pwm_drv *) ppwm;
  
     if(pn) 
     {    
-        pn->period_ticks = 255;
+        pn->period_ticks = 65535;
         pn->on_ticks = fraction;
         return 0;
     }
     return -2;
 }
 
-int 
-native_pwm_set_wave(struct hal_pwm *ppwm, uint32_t period, uint32_t on)
-{
-    struct native_pwm_drv *pn = (struct native_pwm_drv *) ppwm;
- 
-    if(pn) 
-    {    
-        if(period > ((1 << NATIVE_PWM_BITS) - 1)) {
-            return -2;
-        }
-        if(on > ((1 << NATIVE_PWM_BITS) - 1)) {
-            return -3;
-        }
-        pn->period_ticks = period;
-        pn->on_ticks = on;
-        return 0;
-    }
-    return -4;
-}
-