Skip to content

Commit e0b767f

Browse files
authored
Merge pull request #88 from Lawlieta/master
【完善】stm32l4_pandora 示例程序
2 parents 4c8c6ac + eddb875 commit e0b767f

File tree

7 files changed

+73
-14
lines changed

7 files changed

+73
-14
lines changed

examples/stm32l4_pandora/adc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from machine import ADC # Import the ADC class from machine
1212

13-
adc = ADC(2, 5) # Creates an ADC object that currently uses the 5 channels of an ADC device numbered 2
14-
adc.read() # Gets the ADC object sampling value
13+
adc = ADC(1, 13) # Creates an ADC object that currently uses the 13 channels of an ADC device numbered 1
14+
print(adc.read()) # Gets the ADC object sampling value, value range 0 to 4096
1515
adc.deinit() # Close ADC object
16-
adc.init(5) # Open and reconfigure the ADC object
16+
adc.init(13) # Open and reconfigure the ADC object

examples/stm32l4_pandora/pwm.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
from machine import PWM # Import PWM class from machine
1212

13-
pwm = PWM(1, 4, 1000, 100) # Create PWM object. Currently, 4 channels of PWM device numbered 1 are used.
13+
pwm = PWM(3, 3, 1000, 100) # Create PWM object. Currently, 3 channels of PWM device numbered 3 are used.
1414
# The initialization frequency is 1000Hz and the duty ratio value is 100 (duty ratio is 100/255 = 39.22%).
1515
pwm.freq(2000) # Set the frequency of PWM object
1616
pwm.freq() # Get the frequency of PWM object
17-
pwm.duty(200) # sets the duty ratio value of PWM object
17+
print(pwm) # Show PWM object information
18+
pwm.duty(200) # Sets the duty ratio value of PWM object
1819
pwm.duty() # Get the duty ratio value of PWM object
19-
pwm.deinit() # close PWM object
20-
pwm.init(4, 1000, 100) # open and reconfigure the PWM object
20+
print(pwm) # Show PWM object information
21+
pwm.deinit() # Close PWM object
22+
pwm.init(3, 1000, 100) # Open and reconfigure the PWM object
23+
print(pwm) # Show PWM object information

examples/stm32l4_pandora/rtc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
from machine import RTC
1212

13-
rtc = RTC() # Create an RTC device object
14-
rtc.init((2019,6,5,2,10,22,30,0)) # Set initialization time
15-
rtc.now() # Get the current time
16-
rtc.deinit() # Reset time to January 1, 2015
17-
rtc.now() # Get the current time
13+
rtc = RTC() # Create an RTC device object
14+
rtc.init((2019,6,5,2,10,22,30,0)) # Set initialization time
15+
print(rtc.now()) # Get the current time
16+
rtc.deinit() # Reset time to January 1, 2015
17+
print(rtc.now()) # Get the current time

examples/stm32l4_pandora/timer.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-29 ChenYong first version
9+
#
10+
11+
def callback_periodic(obj): # defined preiodic mode timeout callback
12+
print("Timer callback periodic test")
13+
14+
def callback_oneshot(obj): # defined ont shot mode timeout callback
15+
print("Timer callback oneshot test")
16+
17+
from machine import Timer
18+
import utime as time
19+
20+
timer = Timer(15) # Create Timer object. Timer device number 15 are used.
21+
timer.init(timer.PERIODIC, 1000, callback_periodic) # Initialize the Timer device object
22+
# Set Timer mode to preiodic mode, set timeout to 1 seconds and set callback fucntion
23+
time.sleep_ms(5500) # Execute 5 times timeout callback in the delay time
24+
timer.init(timer.ONE_SHOT, 1000, callback_oneshot) # Reset initialize the Timer device object
25+
# Set Timer mode to one shot mode, set timeout to 1 seconds and set callback fucntion
26+
time.sleep_ms(1500) # Execute 1 times timeout callback in the delay time
27+
timer.deinit() # Stop and close Timer device object

examples/stm32l4_pandora/wdt.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-29 ChenYong first version
9+
#
10+
11+
from machine import WDT
12+
13+
wdt = WDT(10) # Create an WDT device object, set the timeout to 10 seconds
14+
wdt.feed() # Perform the "feed dog" operation to clear the watchdog device count during the timout period
15+
# If not executed, the system will restart after the timeout
16+
print("reset system after 10 seconds")

port/machine_pwm.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ typedef struct _machine_pwm_obj_t {
5454
uint32_t freq;
5555
} machine_pwm_obj_t;
5656

57+
STATIC void machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
58+
machine_pwm_obj_t *self = self_in;
59+
60+
mp_printf(print, "PWM(%p; ", self);
61+
mp_printf(print, "id=%d, ", self->id);
62+
mp_printf(print, "channel=%d, ", self->channel);
63+
mp_printf(print, "freq=%d, ", self->freq);
64+
mp_printf(print, "duty=%d)", self->duty);
65+
}
66+
5767
STATIC void error_check(bool status, const char *msg) {
5868
if (!status) {
5969
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
@@ -238,6 +248,7 @@ STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict,
238248
const mp_obj_type_t machine_pwm_type = {
239249
{ &mp_type_type },
240250
.name = MP_QSTR_PWM,
251+
.print = machine_pwm_print,
241252
.make_new = machine_pwm_make_new,
242253
.locals_dict = (mp_obj_dict_t *) &machine_pwm_locals_dict,
243254
};

port/machine_timer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
8787
self->base.type = &machine_timer_type;
8888
self->timerid = device_id;
8989
self->timeout = 0;
90+
self->timeout_cb = RT_NULL;
9091
self->is_repeat = RT_TRUE;
9192
self->is_init = RT_FALSE;
9293

9394
// return constant object
9495
return MP_OBJ_FROM_PTR(self);
9596
}
9697

98+
static machine_timer_obj_t *timer_self = RT_NULL;
99+
97100
STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
98101
machine_timer_obj_t *self = self_in;
99102
rt_err_t result = RT_EOK;
@@ -102,14 +105,13 @@ STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
102105
result = rt_device_close(self->timer_device);
103106
error_check(result == RT_EOK, "Timer device close error");
104107
self->is_init = RT_FALSE;
108+
timer_self = RT_NULL;
105109
}
106110

107111
return mp_const_none;
108112
}
109113
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
110114

111-
static machine_timer_obj_t *timer_self = RT_NULL;
112-
113115
STATIC rt_err_t timer_event_handler(rt_device_t dev, rt_size_t size) {
114116
machine_timer_obj_t *self = timer_self;
115117

0 commit comments

Comments
 (0)