Преглед изворни кода

Merge pull request #88 from Lawlieta/master

【完善】stm32l4_pandora 示例程序
朱天龙 (Armink) пре 6 година
родитељ
комит
e0b767fe0a

+ 3 - 3
examples/stm32l4_pandora/adc.py

@@ -10,7 +10,7 @@
 
 from machine import ADC     # Import the ADC class from machine
 
-adc = ADC(2, 5)             # Creates an ADC object that currently uses the 5 channels of an ADC device numbered 2
-adc.read()                  # Gets the ADC object sampling value
+adc = ADC(1, 13)            # Creates an ADC object that currently uses the 13 channels of an ADC device numbered 1
+print(adc.read())           # Gets the ADC object sampling value, value range 0 to 4096
 adc.deinit()                # Close ADC object
-adc.init(5)                 # Open and reconfigure the ADC object
+adc.init(13)                # Open and reconfigure the ADC object

+ 7 - 4
examples/stm32l4_pandora/pwm.py

@@ -10,11 +10,14 @@
 
 from machine import PWM     # Import PWM class from machine
 
-pwm = PWM(1, 4, 1000, 100)  # Create PWM object. Currently, 4 channels of PWM device numbered 1 are used. 
+pwm = PWM(3, 3, 1000, 100)  # Create PWM object. Currently, 3 channels of PWM device numbered 3 are used. 
                             # The initialization frequency is 1000Hz and the duty ratio value is 100 (duty ratio is 100/255 = 39.22%).
 pwm.freq(2000)              # Set the frequency of PWM object
 pwm.freq()                  # Get the frequency of PWM object
-pwm.duty(200)               # sets the duty ratio value of PWM object
+print(pwm)                  # Show PWM object information
+pwm.duty(200)               # Sets the duty ratio value of PWM object
 pwm.duty()                  # Get the duty ratio value of PWM object
-pwm.deinit()                # close PWM object
-pwm.init(4, 1000, 100)      # open and reconfigure the PWM object
+print(pwm)                  # Show PWM object information
+pwm.deinit()                # Close PWM object
+pwm.init(3, 1000, 100)      # Open and reconfigure the PWM object
+print(pwm)                  # Show PWM object information

+ 5 - 5
examples/stm32l4_pandora/rtc.py

@@ -10,8 +10,8 @@
 
 from machine import RTC
 
-rtc = RTC()                        # Create an RTC device object
-rtc.init((2019,6,5,2,10,22,30,0))  # Set initialization time
-rtc.now()                          # Get the current time
-rtc.deinit()                       # Reset time to January 1, 2015
-rtc.now()                          # Get the current time
+rtc = RTC()                            # Create an RTC device object
+rtc.init((2019,6,5,2,10,22,30,0))      # Set initialization time
+print(rtc.now())                       # Get the current time
+rtc.deinit()                           # Reset time to January 1, 2015
+print(rtc.now())                       # Get the current time

+ 27 - 0
examples/stm32l4_pandora/timer.py

@@ -0,0 +1,27 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-29     ChenYong     first version
+#
+
+def callback_periodic(obj):                                # defined  preiodic mode timeout callback
+    print("Timer callback periodic test")
+
+def callback_oneshot(obj):                                 # defined  ont shot mode timeout callback
+    print("Timer callback oneshot test")
+
+from machine import Timer
+import utime as time
+
+timer = Timer(15)                                          # Create Timer object. Timer device number 15 are used. 
+timer.init(timer.PERIODIC, 1000, callback_periodic)        # Initialize the Timer device object
+                                                           # Set Timer mode to preiodic mode, set timeout to 1 seconds and set callback fucntion
+time.sleep_ms(5500)                                        # Execute 5 times timeout callback in the delay time
+timer.init(timer.ONE_SHOT, 1000, callback_oneshot)         # Reset initialize the Timer device object
+                                                           # Set Timer mode to one shot mode, set timeout to 1 seconds and set callback fucntion
+time.sleep_ms(1500)                                        # Execute 1 times timeout callback in the delay time
+timer.deinit()                                             # Stop and close Timer device object

+ 16 - 0
examples/stm32l4_pandora/wdt.py

@@ -0,0 +1,16 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-29     ChenYong     first version
+#
+
+from machine import WDT
+
+wdt = WDT(10)                # Create an WDT device object, set the timeout to 10 seconds
+wdt.feed()                   # Perform the "feed dog" operation to clear the watchdog device count during the timout period
+                             # If not executed, the system will restart after the timeout
+print("reset system after 10 seconds")

+ 11 - 0
port/machine_pwm.c

@@ -54,6 +54,16 @@ typedef struct _machine_pwm_obj_t {
     uint32_t freq;
 } machine_pwm_obj_t;
 
+STATIC void machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+    machine_pwm_obj_t *self = self_in;
+
+    mp_printf(print, "PWM(%p; ", self);
+    mp_printf(print, "id=%d, ", self->id);
+    mp_printf(print, "channel=%d, ", self->channel);
+    mp_printf(print, "freq=%d, ", self->freq);
+    mp_printf(print, "duty=%d)", self->duty);
+}
+
 STATIC void error_check(bool status, const char *msg) {
     if (!status) {
         nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
@@ -238,6 +248,7 @@ STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict,
 const mp_obj_type_t machine_pwm_type = {
     { &mp_type_type },
     .name = MP_QSTR_PWM,
+    .print = machine_pwm_print,
     .make_new = machine_pwm_make_new,
     .locals_dict = (mp_obj_dict_t *) &machine_pwm_locals_dict,
 };

+ 4 - 2
port/machine_timer.c

@@ -87,6 +87,7 @@ STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
     self->base.type = &machine_timer_type;
     self->timerid = device_id;
     self->timeout = 0;
+    self->timeout_cb = RT_NULL;
     self->is_repeat = RT_TRUE;
     self->is_init = RT_FALSE;
     
@@ -94,6 +95,8 @@ STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
     return MP_OBJ_FROM_PTR(self);
 }
 
+static machine_timer_obj_t *timer_self = RT_NULL;
+
 STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
     machine_timer_obj_t *self = self_in;
     rt_err_t result = RT_EOK;
@@ -102,14 +105,13 @@ STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
         result = rt_device_close(self->timer_device);
         error_check(result == RT_EOK, "Timer device close error");
         self->is_init = RT_FALSE;
+        timer_self = RT_NULL;
     }
 
     return mp_const_none;
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
 
-static machine_timer_obj_t *timer_self = RT_NULL;
-
 STATIC rt_err_t timer_event_handler(rt_device_t dev, rt_size_t size) {
     machine_timer_obj_t *self = timer_self;