Skip to content

Commit 3fa701d

Browse files
authored
Merge pull request #120 from Lawlieta/master
【更新】ADC、PWM、Timer、WDT 驱动模块,添加对设备名称入参判断,更新文档说明
2 parents 350596f + 5a1ec07 commit 3fa701d

File tree

8 files changed

+106
-42
lines changed

8 files changed

+106
-42
lines changed

docs/04-Hardware_Control_Module/08-machine-PWM.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
在给定的总线上构建一个 `PWM` 对象,参数介绍如下:
1818

19-
- **id**:使用的 PWM 设备编号,如 `id = 1` 表示编号为 1 的 PWM 设备;
19+
- **id**:使用的 PWM 设备编号,如 `id = 1` 表示编号为 1 的 PWM 设备,或者表示使用的 PWM 设备名,如 `id = "pwm"` 表示设备名为 `pwm` 的 PWM 设备
2020
- **channel**:使用的 PWM 设备通道号,每个 PWM 设备包含多个通道,范围为 [0, 4]
2121
- **freq**:初始化频率,范围 [1, 156250]
2222
- **duty**:初始化占空比数值,范围 [0 255]

docs/04-Hardware_Control_Module/09-machine-ADC.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#### **class machine.ADC**(id, channel)
1515

16-
- **id**:使用的 ADC 设备编号,`id = 1` 表示编号为 1 的 ADC 设备;
16+
- **id**:使用的 ADC 设备编号,`id = 1` 表示编号为 1 的 ADC 设备,或者表示使用的 ADC 设备名,如 `id = "adc"` 表示设备名为 `adc` 的 ADC 设备
1717
- **channel**:使用的 ADC 设备通道号,每个 ADC 设备对应多个通道;
1818

1919
例如:`ADC(1,4)` 表示当前使用编号为 1 的 ADC 设备的 4 通道。

docs/04-Hardware_Control_Module/10-machine-WDT.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
在 RT-Thread MicroPython 中 `WDT` 对象的构造函数如下:
1616

17-
#### **class machine.WDT**(timeout=5)
17+
#### **class machine.WDT**(id = "wdt", timeout=5)
18+
19+
- **id**: 使用的 WDT 设备编号,`id = 1` 表示编号为 1 的 WDT 设备,或者表示使用的 WDT 设备名,如 `id = "wdt"` 表示设备名为 `wdt` 的 WDT 设备;
1820

1921
- **timeout**:设置看门狗超时时间,单位:秒(s);
2022

docs/04-Hardware_Control_Module/11-machine-Timer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#### **class machine.Timer**(id)
1717

18-
- **id**:使用的 Timer 设备编号,`id = 1` 表示编号为 1 的 Timer 设备;
18+
- **id**:使用的 Timer 设备编号,`id = 1` 表示编号为 1 的 Timer 设备,或者表示使用的 timer 设备名,如 `id = "timer"` 表示设备名为 `timer` 的 Timer 设备
1919

2020
该函数主要用于通过设备编号创建 Timer 设备对象。
2121

port/modules/machine/machine_adc.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type,
6060
struct rt_adc_device *adc_device = RT_NULL;
6161
char adc_dev_name[RT_NAME_MAX] = {0};
6262
rt_err_t result = RT_EOK;
63-
int dev_id = 0;
6463

6564
// init machine adc object information
6665
self->channel = 0;
@@ -69,11 +68,18 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type,
6968

7069
mp_arg_check_num(n_args, n_kw, 1, 2, true);
7170

72-
dev_id = mp_obj_get_int(args[0]);
73-
rt_snprintf(adc_dev_name, sizeof(adc_dev_name), "adc%d", dev_id);
71+
// check input ADC device name or ID
72+
if (mp_obj_is_small_int(args[0])) {
73+
rt_snprintf(adc_dev_name, sizeof(adc_dev_name), "adc%d", mp_obj_get_int(args[0]));
74+
} else if (mp_obj_is_qstr(args[0])) {
75+
rt_strncpy(adc_dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
76+
} else {
77+
error_check(0, "Input ADC device name or ID error.");
78+
}
79+
7480
adc_device = (struct rt_adc_device *) rt_device_find(adc_dev_name);
7581
if (adc_device == RT_NULL || adc_device->parent.type != RT_Device_Class_Miscellaneous) {
76-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
82+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
7783
"ADC(%s) don't exist", adc_dev_name));
7884
}
7985
self->adc_device = adc_device;

port/modules/machine/machine_pwm.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ extern const mp_obj_type_t machine_pwm_type;
4747
typedef struct _machine_pwm_obj_t {
4848
mp_obj_base_t base;
4949
struct rt_device_pwm *pwm_device;
50+
char dev_name[RT_NAME_MAX];
5051
uint8_t is_init;
51-
uint8_t id;
52+
int8_t id;
5253
uint8_t channel;
5354
uint8_t duty;
5455
uint32_t freq;
@@ -58,7 +59,11 @@ STATIC void machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
5859
machine_pwm_obj_t *self = self_in;
5960

6061
mp_printf(print, "PWM(%p; ", self);
61-
mp_printf(print, "id=%d, ", self->id);
62+
if (self->id >= 0) {
63+
mp_printf(print, "pwm_id=%d, ", self->id);
64+
} else {
65+
mp_printf(print, "pwm_name=%s, ", self->dev_name);
66+
}
6267
mp_printf(print, "channel=%d, ", self->channel);
6368
mp_printf(print, "freq=%d, ", self->freq);
6469
mp_printf(print, "duty=%d)", self->duty);
@@ -107,10 +112,15 @@ STATIC void machine_pwm_init_helper(machine_pwm_obj_t *self,
107112
}
108113
self->duty = tval;
109114

110-
snprintf(pwm_dev_name, sizeof(pwm_dev_name), "pwm%d", self->id);
115+
if (self->id >= 0) {
116+
rt_snprintf(pwm_dev_name, sizeof(pwm_dev_name), "pwm%d", self->id);
117+
} else {
118+
rt_strncpy(pwm_dev_name, self->dev_name, RT_NAME_MAX);
119+
}
120+
111121
pwm_device = (struct rt_device_pwm *) rt_device_find(pwm_dev_name);
112122
if (pwm_device == RT_NULL || pwm_device->parent.type != RT_Device_Class_Miscellaneous) {
113-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
123+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
114124
"PWM(%s) don't exist", pwm_dev_name));
115125
}
116126
self->pwm_device = pwm_device;
@@ -137,7 +147,17 @@ STATIC mp_obj_t machine_pwm_make_new(const mp_obj_type_t *type,
137147
machine_pwm_obj_t *self = m_new_obj(machine_pwm_obj_t);
138148
self->base.type = &machine_pwm_type;
139149
self->is_init = RT_FALSE;
140-
self->id = mp_obj_get_int(args[0]);
150+
151+
// check input PWM device name or ID
152+
if (mp_obj_is_small_int(args[0])) {
153+
self->id = mp_obj_get_int(args[0]);
154+
} else if (mp_obj_is_qstr(args[0])) {
155+
self->id = -1;
156+
rt_strncpy(self->dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
157+
} else {
158+
error_check(0, "Input PWM device name or ID error.");
159+
}
160+
141161
self->channel = 0;
142162
self->freq = 1;
143163
self->duty = 0;
@@ -187,16 +207,16 @@ STATIC mp_obj_t machine_pwm_freq(size_t n_args, const mp_obj_t *args) {
187207
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
188208
"Bad frequency %d", tval));
189209
}
190-
210+
191211
// get period number by frequency
192212
period = MP_PWM_PERIOD_GET(tval);
193213
// get pulse number by duty
194214
pulse = MP_PWM_PULSE_GET(period, self->duty);
195-
215+
196216
result = rt_pwm_set(self->pwm_device, self->channel, period, pulse);
197217
error_check(result == RT_EOK, "PWM set information error");
198218
self->freq = tval;
199-
219+
200220
return mp_const_none;
201221
}
202222

@@ -220,12 +240,12 @@ STATIC mp_obj_t machine_pwm_duty(size_t n_args, const mp_obj_t *args) {
220240
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
221241
"Bad duty %d", tval));
222242
}
223-
243+
224244
// get period number by frequency
225245
period = MP_PWM_PERIOD_GET(self->freq);
226246
// get pulse number by duty
227247
pulse = MP_PWM_PULSE_GET(period, tval);
228-
248+
229249
result = rt_pwm_set(self->pwm_device, self->channel, period, pulse);
230250
error_check(result == RT_EOK, "PWM set information error");
231251
self->duty = tval;

port/modules/machine/machine_timer.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
typedef struct _machine_timer_obj_t {
4545
mp_obj_base_t base;
4646
rt_device_t timer_device;
47+
char dev_name[RT_NAME_MAX];
4748
mp_obj_t timeout_cb;
48-
uint8_t timerid;
49+
int8_t timerid;
4950
uint32_t timeout;
5051
rt_bool_t is_repeat;
5152
rt_bool_t is_init;
@@ -64,36 +65,51 @@ STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
6465

6566
mp_printf(print, "Timer(%p; ", self);
6667

67-
mp_printf(print, "timerid=%d, ", self->timerid);
68+
if (self->timerid >= 0) {
69+
mp_printf(print, "timer_id=%d, ", self->timerid);
70+
} else {
71+
mp_printf(print, "timer_name=%s, ", self->dev_name);
72+
}
6873
mp_printf(print, "period=%d, ", self->timeout);
6974
mp_printf(print, "auto_reload=%d)", self->is_repeat);
7075
}
7176

7277
STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
7378
machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
7479
char timer_dev_name[RT_NAME_MAX] = {0};
75-
int device_id = 0;
7680

7781
// check arguments
7882
mp_arg_check_num(n_args, n_kw, 1, 1, true);
79-
device_id = mp_obj_get_int(args[0]);
83+
84+
// check input timer device name or ID
85+
if (mp_obj_is_small_int(args[0])) {
86+
int device_id = mp_obj_get_int(args[0]);
87+
self->timerid = device_id;
88+
self->timer_device->device_id = device_id;
89+
rt_snprintf(timer_dev_name, sizeof(timer_dev_name), "timer%d", mp_obj_get_int(args[0]));
90+
} else if (mp_obj_is_qstr(args[0])) {
91+
static int device_id = 0;
92+
self->timerid = -1;
93+
self->timer_device->device_id = device_id++;
94+
rt_strncpy(self->dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
95+
rt_strncpy(timer_dev_name, self->dev_name, RT_NAME_MAX);
96+
} else {
97+
error_check(0, "Input ADC device name or ID error.");
98+
}
8099

81100
// find timer device
82-
rt_snprintf(timer_dev_name, sizeof(timer_dev_name), "timer%d", device_id);
83101
self->timer_device = rt_device_find(timer_dev_name);
84102
if (self->timer_device == RT_NULL || self->timer_device->type != RT_Device_Class_Timer) {
85-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%s) don't exist", timer_dev_name));
103+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%s) don't exist", timer_dev_name));
86104
}
87105

88106
// initialize timer device
89107
self->base.type = &machine_timer_type;
90-
self->timerid = device_id;
91108
self->timeout = 0;
92109
self->timeout_cb = RT_NULL;
93110
self->is_repeat = RT_TRUE;
94111
self->is_init = RT_FALSE;
95-
self->timer_device->device_id = device_id-1;
96-
112+
97113
// return constant object
98114
return MP_OBJ_FROM_PTR(self);
99115
}
@@ -162,7 +178,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
162178
result = rt_device_open(self->timer_device, RT_DEVICE_OFLAG_RDWR);
163179
error_check(result == RT_EOK, "Timer device open error");
164180
}
165-
181+
166182
if (self->timeout_cb != RT_NULL) {
167183
// set callback timer
168184
if (timer_self[self->timer_device->device_id] && timer_self[self->timer_device->device_id] != self) {
@@ -174,7 +190,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
174190
error_check(result == RT_EOK, "Timer set timout callback error");
175191
}
176192

177-
// set timer mode
193+
// set timer mode
178194
mode = self->is_repeat ? HWTIMER_MODE_PERIOD : HWTIMER_MODE_ONESHOT;
179195
result = rt_device_control(self->timer_device, HWTIMER_CTRL_MODE_SET, &mode);
180196
error_check(result == RT_EOK, "Timer set mode error");
@@ -191,7 +207,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
191207
}
192208

193209
self->is_init = RT_TRUE;
194-
210+
195211
return mp_const_none;
196212
}
197213
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
@@ -200,20 +216,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init)
200216
STATIC mp_obj_t machine_timer_callback(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
201217
machine_timer_obj_t *self = (machine_timer_obj_t *)args[0];
202218
rt_bool_t result = RT_EOK;
203-
219+
204220
static const mp_arg_t allowed_args[] = {
205221
{ MP_QSTR_callback, MP_ARG_OBJ, {.u_obj = mp_const_none} },
206222
};
207223

208224
mp_arg_val_t dargs[MP_ARRAY_SIZE(allowed_args)];
209225
mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, dargs);
210-
226+
211227
self->timeout_cb = dargs[0].u_obj;
212-
228+
213229
if(n_args == 1)
214230
{
215231
self->timeout_cb = RT_NULL;
216-
self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
232+
self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
217233
}
218234
else if(n_args == 2)
219235
{
@@ -226,7 +242,7 @@ STATIC mp_obj_t machine_timer_callback(mp_uint_t n_args, const mp_obj_t *args, m
226242
else
227243
{
228244
self->timeout_cb = RT_NULL;
229-
self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
245+
self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
230246
}
231247
}
232248
return mp_const_none;

port/modules/machine/machine_wdt.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,42 @@ STATIC void error_check(bool status, const char *msg) {
5454
STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
5555
#define MP_WDT_DEV_NAME "wdt"
5656
machine_wdt_obj_t *self = m_new_obj(machine_wdt_obj_t);
57+
char wdt_dev_name[RT_NAME_MAX] = {0};
5758
rt_err_t result = RT_EOK;
5859
mp_int_t timeout = 5;
5960

6061
// check arguments
61-
mp_arg_check_num(n_args, n_kw, 0, 1, false);
62-
63-
if (n_args > 0) {
64-
timeout = mp_obj_get_int(args[0]);
62+
mp_arg_check_num(n_args, n_kw, 0, 2, false);
63+
64+
if (n_args == 2) {
65+
// check input WDT device name or ID
66+
if (mp_obj_is_small_int(args[0])) {
67+
rt_snprintf(wdt_dev_name, sizeof(wdt_dev_name), "wdt%d", mp_obj_get_int(args[0]));
68+
} else if (mp_obj_is_qstr(args[0])) {
69+
rt_strncpy(wdt_dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
70+
} else {
71+
error_check(0, "Input WDT device name or ID error.");
72+
}
73+
timeout = mp_obj_get_int(args[1]);
6574
error_check(timeout >= 1, "input timeout value error");
75+
} else if (n_args == 1) {
76+
if (mp_obj_is_small_int(args[0])) {
77+
timeout = mp_obj_get_int(args[0]);
78+
error_check(timeout >= 1, "input timeout value error");
79+
} else if (mp_obj_is_qstr(args[0])) {
80+
rt_strncpy(wdt_dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
81+
} else {
82+
error_check(0, "Input WDT device name or ID error.");
83+
}
84+
} else {
85+
rt_strncpy(wdt_dev_name, MP_WDT_DEV_NAME, RT_NAME_MAX);
6686
}
6787

6888
self->base.type = &machine_wdt_type;
6989
// find WDT device
70-
self->wdt_device = rt_device_find(MP_WDT_DEV_NAME);
90+
self->wdt_device = rt_device_find(wdt_dev_name);
7191
if (self->wdt_device == RT_NULL || self->wdt_device->type != RT_Device_Class_Miscellaneous) {
72-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "WDT(%s) don't exist", MP_WDT_DEV_NAME));
92+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "WDT(%s) don't exist", wdt_dev_name));
7393
}
7494

7595
result = rt_device_init(self->wdt_device);
@@ -93,7 +113,7 @@ STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) {
93113

94114
result = rt_device_control(self->wdt_device, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL);
95115
error_check(result == RT_EOK, "WDT feed failed");
96-
116+
97117
return mp_const_none;
98118
}
99119
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed);

0 commit comments

Comments
 (0)