Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 76c366d

Browse files
committed
stmhal: Add machine.WDT class.
Usage: import machine wdt = machine.WDT(0, 5000) # 5 second timeout wdt.feed() Thanks to Moritz for the initial implementation.
1 parent f7c4611 commit 76c366d

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

stmhal/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ SRC_C = \
138138
uart.c \
139139
can.c \
140140
usb.c \
141+
wdt.c \
141142
gccollect.c \
142143
pybstdio.c \
143144
help.c \

stmhal/modmachine.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "rtc.h"
4545
#include "i2c.h"
4646
#include "spi.h"
47+
#include "wdt.h"
4748

4849
// machine.info([dump_alloc_table])
4950
// Print out lots of information about the board.
@@ -490,10 +491,10 @@ STATIC const mp_map_elem_t machine_module_globals_table[] = {
490491
// initialize master mode on the peripheral.
491492
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&machine_i2c_type },
492493
{ MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&pyb_spi_type },
494+
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&pyb_wdt_type },
493495
#if 0
494496
{ MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
495497
{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
496-
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&pyb_wdt_type },
497498
{ MP_OBJ_NEW_QSTR(MP_QSTR_HeartBeat), (mp_obj_t)&pyb_heartbeat_type },
498499
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sd_type },
499500

stmhal/wdt.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
29+
#include STM32_HAL_H
30+
31+
#include "py/runtime.h"
32+
#include "wdt.h"
33+
34+
typedef struct _pyb_wdt_obj_t {
35+
mp_obj_base_t base;
36+
} pyb_wdt_obj_t;
37+
38+
STATIC pyb_wdt_obj_t pyb_wdt = {{&pyb_wdt_type}};
39+
40+
STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
41+
// check arguments
42+
mp_arg_check_num(n_args, n_kw, 2, 2, false);
43+
44+
mp_int_t id = mp_obj_get_int(args[0]);
45+
if (id != 0) {
46+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "WDT(%d) does not exist", id));
47+
}
48+
49+
// timeout is in milliseconds
50+
mp_int_t timeout = mp_obj_get_int(args[1]);
51+
52+
// compute prescaler
53+
uint32_t prescaler;
54+
for (prescaler = 0; prescaler < 6 && timeout >= 512; ++prescaler, timeout /= 2) {
55+
}
56+
57+
// convert milliseconds to ticks
58+
timeout *= 8; // 32kHz / 4 = 8 ticks per millisecond (approx)
59+
if (timeout <= 0) {
60+
mp_raise_ValueError("WDT timeout too short");
61+
} else if (timeout > 0xfff) {
62+
mp_raise_ValueError("WDT timeout too long");
63+
}
64+
timeout -= 1;
65+
66+
// set the reload register
67+
while (IWDG->SR & 2) {
68+
}
69+
IWDG->KR = 0x5555;
70+
IWDG->RLR = timeout;
71+
72+
// set the prescaler
73+
while (IWDG->SR & 1) {
74+
}
75+
IWDG->KR = 0x5555;
76+
IWDG->PR = prescaler;
77+
78+
// start the watch dog
79+
IWDG->KR = 0xcccc;
80+
81+
return (mp_obj_t)&pyb_wdt;
82+
}
83+
84+
STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self_in) {
85+
(void)self_in;
86+
IWDG->KR = 0xaaaa;
87+
return mp_const_none;
88+
}
89+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed);
90+
91+
STATIC const mp_map_elem_t pyb_wdt_locals_dict_table[] = {
92+
{ MP_OBJ_NEW_QSTR(MP_QSTR_feed), (mp_obj_t)&pyb_wdt_feed_obj },
93+
};
94+
95+
STATIC MP_DEFINE_CONST_DICT(pyb_wdt_locals_dict, pyb_wdt_locals_dict_table);
96+
97+
const mp_obj_type_t pyb_wdt_type = {
98+
{ &mp_type_type },
99+
.name = MP_QSTR_WDT,
100+
.make_new = pyb_wdt_make_new,
101+
.locals_dict = (mp_obj_t)&pyb_wdt_locals_dict,
102+
};

stmhal/wdt.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
extern const mp_obj_type_t pyb_wdt_type;

0 commit comments

Comments
 (0)