Skip to content

Commit 69c763c

Browse files
jaenrig-ifxactions-user
authored andcommitted
psoc6: Machine class func renames and pwm improvements.
Signed-off-by: jaenrig-ifx <[email protected]>
1 parent 0e5ce56 commit 69c763c

File tree

14 files changed

+102
-101
lines changed

14 files changed

+102
-101
lines changed

ports/psoc6/machine_adcblock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ static mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_
314314
}
315315
static MP_DEFINE_CONST_FUN_OBJ_KW(machine_adcblock_connect_obj, 2, machine_adcblock_connect);
316316

317-
void mod_adcblock_deinit(void) {
317+
void machine_adcblock_deinit_all(void) {
318318
for (uint8_t i = 0; i < MAX_BLOCKS; i++) {
319319
if (adc_block[i] != NULL) {
320320
machine_adcblock_deinit(adc_block[i]);

ports/psoc6/machine_adcblock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ typedef struct
5454
uint32_t pin;
5555
}adc_block_channel_pin_map_t;
5656

57-
void mod_adcblock_deinit(void);
57+
void machine_adcblock_deinit_all(void);
5858
#endif // MICROPY_INCLUDED_MACHINE_ADCBLOCK_H

ports/psoc6/machine_i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ static mp_obj_t machine_i2c_slave_irq_disable(mp_obj_t self_in) {
319319
}
320320
static MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_slave_irq_disable_obj, machine_i2c_slave_irq_disable);
321321

322-
void mod_i2c_deinit() {
322+
void machine_i2c_deinit_all() {
323323
for (uint8_t i = 0; i < MAX_I2C; i++) {
324324
if (i2c_obj[i] != NULL) {
325325
machine_i2c_deinit((mp_obj_base_t *)(i2c_obj[i]));

ports/psoc6/machine_pin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_
469469
}
470470
static MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq);
471471

472-
void mod_pin_deinit() {
472+
void machine_pin_deinit_all() {
473473
for (uint8_t i = 0; i < machine_pin_num_of_cpu_pins; i++) {
474474
if (pin_io[i] != NULL) {
475475
machine_pin_obj_deinit(pin_io[i]);

ports/psoc6/machine_pwm.c

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "py/runtime.h"
28-
#include "py/mphal.h"
29-
#include "modmachine.h"
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "modmachine.h"
3030

3131
// port-specific includes
32-
#include "machine_pin_phy.h"
33-
#include "mplogger.h"
32+
#include "machine_pin_phy.h"
33+
#include "mplogger.h"
34+
35+
#define pwm_assert_raise_val(msg, ret) if (ret != CY_RSLT_SUCCESS) { \
36+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT(msg), ret); \
37+
}
3438

3539
typedef struct _machine_pwm_obj_t {
3640
mp_obj_base_t base;
@@ -39,14 +43,12 @@ typedef struct _machine_pwm_obj_t {
3943
uint32_t fz;
4044
uint8_t duty_type;
4145
mp_int_t duty;
42-
// bool invert;
4346
} machine_pwm_obj_t;
4447

4548
static machine_pwm_obj_t *pwm_obj[MAX_PWM_OBJS] = { NULL };
4649

4750
static inline machine_pwm_obj_t *pwm_obj_alloc() {
48-
for (uint8_t i = 0; i < MAX_PWM_OBJS; i++)
49-
{
51+
for (uint8_t i = 0; i < MAX_PWM_OBJS; i++) {
5052
if (pwm_obj[i] == NULL) {
5153
pwm_obj[i] = mp_obj_malloc(machine_pwm_obj_t, &machine_pwm_type);
5254
return pwm_obj[i];
@@ -57,8 +59,7 @@ static inline machine_pwm_obj_t *pwm_obj_alloc() {
5759
}
5860

5961
static inline void pwm_obj_free(machine_pwm_obj_t *pwm_obj_ptr) {
60-
for (uint8_t i = 0; i < MAX_PWM_OBJS; i++)
61-
{
62+
for (uint8_t i = 0; i < MAX_PWM_OBJS; i++) {
6263
if (pwm_obj[i] == pwm_obj_ptr) {
6364
pwm_obj[i] = NULL;
6465
}
@@ -72,74 +73,83 @@ enum {
7273
DUTY_NS
7374
};
7475

75-
static void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq);
76+
/* Unit conversion macros */
77+
#define pwm_duty_cycle_ns_to_u16(duty_ns, fz) ((int)(((float)(duty_ns * fz) / (float)1000000000) * (float)65536) - 1)
78+
#define pwm_duty_cycle_u16_to_ns(duty_u16, fz) ((int)(((float)(duty_u16 + 1) / (float)65536) * ((float)1000000000 / (float)fz)))
79+
#define pwm_duty_cycle_u16_to_percent(duty_u16) ((float)((duty_u16) * 100) / (float)65535)
80+
#define pwm_freq_to_period_us(fz) ((uint32_t)(1000000 / fz))
81+
#define pwm_period_ns_to_us(period_ns) ((uint32_t)(period_ns / 1000))
82+
83+
static void pwm_duty_ns_assert(mp_int_t duty_ns, uint32_t fz) {
84+
if (duty_ns > (int)(1000000000 / fz)) {
85+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("PWM duty in ns is larger than the period %d ns"), (int)(1000000000 / fz));
86+
}
87+
}
7688

77-
static cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty_cycle) {
78-
return cyhal_pwm_set_duty_cycle(pwm_obj, duty_cycle * 100, fz); // duty_cycle in percentage
89+
static inline void pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, mp_int_t duty_cycle) {
90+
cy_rslt_t ret = cyhal_pwm_set_duty_cycle(pwm_obj, pwm_duty_cycle_u16_to_percent(duty_cycle), fz);
91+
pwm_assert_raise_val("PWM frequency and duty cycle set failed with return code %lx !", ret);
7992
}
8093

81-
static inline cy_rslt_t pwm_duty_set_ns(cyhal_pwm_t *pwm_obj, uint32_t fz, uint32_t pulse_width) {
82-
return cyhal_pwm_set_period(pwm_obj, 1000000 / fz, pulse_width / 1000); // !# * --> /
94+
static void pwm_duty_set_ns(cyhal_pwm_t *pwm_obj, uint32_t fz, uint32_t pulse_width) {
95+
cy_rslt_t ret = cyhal_pwm_set_period(pwm_obj, pwm_freq_to_period_us(fz), pwm_period_ns_to_us(pulse_width));
96+
pwm_assert_raise_val("PWM period set failed with return code %lx !", ret);
8397
}
8498

85-
/*STATIC inline cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj) {
86-
return cyhal_pwm_init_adv(&machine_pwm_obj->pwm_obj, machine_pwm_obj->pin, NC, CYHAL_PWM_LEFT_ALIGN, true, 0, true, NULL); // complimentary pin set as not connected
87-
}*/
99+
static void pwm_config(machine_pwm_obj_t *self) {
100+
if (self->duty_type == DUTY_U16) {
101+
pwm_freq_duty_set(&self->pwm_obj, self->fz, self->duty);
102+
} else {
103+
pwm_duty_set_ns(&self->pwm_obj, self->fz, self->duty);
104+
}
105+
}
88106

89107
static void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
90108
machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
91-
mp_printf(print, "frequency=%u duty_cycle=%f", self->fz, (double)self->duty);
109+
mp_obj_t pin = mp_obj_new_int(self->pin);
110+
const char *pin_name = mp_obj_str_get_str(pin_name_by_addr(pin));
111+
mp_printf(print, "PWM(\"%s\",freq=%u, duty_u16=%f)", pin_name, self->fz, (double)self->duty);
92112
}
93113

94114
static void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
95115
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
96116
enum { ARG_freq, ARG_duty_u16, ARG_duty_ns};
97-
// enum { ARG_freq, ARG_duty_u16, ARG_duty_ns, ARG_invert };
117+
98118
static const mp_arg_t allowed_args[] = {
99-
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = VALUE_NOT_SET} },
119+
{ MP_QSTR_freq, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = VALUE_NOT_SET} },
100120
{ MP_QSTR_duty_u16, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = VALUE_NOT_SET} },
101121
{ MP_QSTR_duty_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = VALUE_NOT_SET} },
102-
// { MP_QSTR_invert, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = VALUE_NOT_SET} },
103122
};
104123

105124
// Parse the arguments.
106125
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
107126
mp_arg_parse_all(n_args, pos_args, kw_args,
108127
MP_ARRAY_SIZE(allowed_args), allowed_args, args);
109-
// self->active = 1;
110128

111129
if ((args[ARG_freq].u_int != VALUE_NOT_SET)) {
112-
// pwm_freq_duty_set(&self->pwm_obj, args[ARG_freq].u_int, self->duty);
113130
self->fz = args[ARG_freq].u_int;
114131
}
115132

116-
if ((args[ARG_duty_u16].u_int != VALUE_NOT_SET)) {
117-
float val = (float)(args[ARG_duty_u16].u_int) / (float)65535;
118-
pwm_freq_duty_set(&self->pwm_obj, self->fz, val);
119-
self->duty = args[ARG_duty_u16].u_int;
120-
self->duty_type = DUTY_U16;
133+
if ((args[ARG_duty_u16].u_int != VALUE_NOT_SET) &&
134+
(args[ARG_duty_ns].u_int != VALUE_NOT_SET)) {
135+
mp_raise_ValueError(MP_ERROR_TEXT("PWM duty should be specified only in one format"));
121136
}
122137

123-
if (args[ARG_duty_ns].u_int != VALUE_NOT_SET) {
124-
pwm_duty_set_ns(&self->pwm_obj, self->fz, args[ARG_duty_ns].u_int);
138+
if ((args[ARG_duty_u16].u_int != VALUE_NOT_SET)) {
139+
self->duty = args[ARG_duty_u16].u_int > 65535 ? 65535 : args[ARG_duty_u16].u_int;
140+
self->duty_type = DUTY_U16;
141+
} else if (args[ARG_duty_ns].u_int != VALUE_NOT_SET) {
142+
pwm_duty_ns_assert(args[ARG_duty_ns].u_int, self->fz);
125143
self->duty = args[ARG_duty_ns].u_int;
126144
self->duty_type = DUTY_NS;
145+
} else {
146+
mp_raise_ValueError(MP_ERROR_TEXT("PWM duty should be specified in either ns or u16"));
127147
}
128148

129-
// inverts the respective output if the value is True
130-
/*if (args[ARG_invert].u_int != VALUE_NOT_SET) {
131-
self->invert = args[ARG_invert].u_int;
132-
if (self->invert == 1) {
133-
cyhal_pwm_free(&self->pwm_obj);
134-
cy_rslt_t result = pwm_advanced_init(self);
135-
if (result != CY_RSLT_SUCCESS) {
136-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("PWM initialisation failed with return code %lx ! and invert output is not available"), result);
137-
}
138-
self->duty_type = DUTY_U16;
139-
self->duty = ((1) - ((self->duty) / 65535)) * 65535;
140-
}
141-
}*/
142-
cyhal_pwm_start(&self->pwm_obj);
149+
pwm_config(self);
150+
151+
cy_rslt_t result = cyhal_pwm_start(&self->pwm_obj);
152+
pwm_assert_raise_val("PWM start failed with return code %lx !", result);
143153
}
144154

145155
static mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
@@ -151,16 +161,10 @@ static mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args
151161
self->pin = pin_addr_by_name(all_args[0]);
152162
self->duty_type = DUTY_NOT_SET;
153163
self->fz = -1;
154-
// self->invert = -1;
155164

156-
// Initialize PWM
157165
cy_rslt_t result = cyhal_pwm_init(&self->pwm_obj, self->pin, NULL);
158-
159-
// To check whether PWM init is successful
160-
if (result != CY_RSLT_SUCCESS) {
161-
assert_pin_phy_used(result);
162-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("PWM initialisation failed with return code %lx !"), result);
163-
}
166+
assert_pin_phy_used(result);
167+
pwm_assert_raise_val("PWM initialisation failed with return code %lx !", result);
164168

165169
// Process the remaining parameters.
166170
mp_map_t kw_args;
@@ -171,17 +175,17 @@ static mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args
171175
}
172176

173177
static void mp_machine_pwm_deinit(machine_pwm_obj_t *self) {
174-
cyhal_pwm_stop(&self->pwm_obj);
178+
cy_rslt_t result = cyhal_pwm_stop(&self->pwm_obj);
179+
pwm_assert_raise_val("PWM stop failed with return code %lx !", result);
175180
cyhal_pwm_free(&self->pwm_obj);
176181
pwm_obj_free(self);
177182
}
178183

179184
static mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) {
180185
if (self->duty_type == DUTY_NS) {
181-
// duty_cycle = pulsewidth(ns)*freq(hz);
182-
return mp_obj_new_float(((self->duty) * (self->fz) * 65535) / 1000000000 - 1);
186+
return mp_obj_new_int(pwm_duty_cycle_ns_to_u16(self->duty, self->fz));
183187
} else {
184-
return mp_obj_new_float(self->duty);
188+
return mp_obj_new_int(self->duty);
185189
}
186190
}
187191

@@ -190,39 +194,38 @@ static void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty_u
190194
// Check the value is more than the max value
191195
self->duty = duty_u16 > 65535 ? 65535 : duty_u16;
192196
self->duty_type = DUTY_U16;
193-
pwm_freq_duty_set(&self->pwm_obj, self->fz, (float)(self->duty) / (float)65535); // s conversion of duty_u16 into dutyu16/65535
197+
pwm_config(self);
194198
}
195199

196200
static mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) {
197201
if (self->duty_type == DUTY_U16) {
198-
return mp_obj_new_float(((self->duty) * 1000000000) / ((self->fz) * 65535)); // pw (ns) = duty_cycle*10^9/fz
202+
return mp_obj_new_int(pwm_duty_cycle_u16_to_ns(self->duty, self->fz));
199203
} else {
200-
return mp_obj_new_float(self->duty);
204+
return mp_obj_new_int(self->duty);
201205
}
202206
}
203207

204208
// sets the pulse width in nanoseconds
205209
static void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns) {
210+
pwm_duty_ns_assert(duty_ns, self->fz);
206211
self->duty = duty_ns;
207212
self->duty_type = DUTY_NS;
208-
pwm_duty_set_ns(&self->pwm_obj, self->fz, duty_ns);
213+
pwm_config(self);
209214
}
210215

211216
static mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) {
212217
return MP_OBJ_NEW_SMALL_INT(self->fz);
213-
214218
}
215219

216220
static void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) {
217-
self->fz = freq;
218-
pwm_freq_duty_set(&self->pwm_obj, freq, self->duty);
219221
if (self->duty_type == DUTY_NS) {
220-
self->duty = ((self->duty) * (self->fz) * 65535) / 1000000000;
221-
mp_machine_pwm_duty_set_ns(self, self->duty);
222+
pwm_duty_ns_assert(self->duty, freq);
222223
}
224+
self->fz = freq;
225+
pwm_config(self);
223226
}
224227

225-
void mod_pwm_deinit() {
228+
void machine_pwm_deinit_all() {
226229
for (uint8_t i = 0; i < MAX_PWM_OBJS; i++) {
227230
if (pwm_obj[i] != NULL) {
228231
mp_machine_pwm_deinit(pwm_obj[i]);

ports/psoc6/machine_rtc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ bool rtc_memory_write_enabled() {
8787
}
8888

8989
/* This function is run from main.c to init the RTC at boot time. This will set the RTC to PSoC default time: 1st Jan 2000*/
90-
void mod_rtc_init(void) {
90+
void machine_rtc_init_all(void) {
9191
cy_rslt_t result = cyhal_rtc_init(&psoc6_rtc);
9292
rtc_assert_raise("cyhal_rtc_init failed !", result);
9393
}
9494

95-
void mod_rtc_deinit() {
95+
void machine_rtc_deinit_all() {
9696
if (rtc_memory_write_enabled() == false) {
9797
cyhal_rtc_free(&psoc6_rtc);
9898
}

ports/psoc6/machine_sdcard.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static mp_obj_t machine_sdcard_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t
315315
static MP_DEFINE_CONST_FUN_OBJ_3(machine_sdcard_ioctl_obj, machine_sdcard_ioctl);
316316

317317

318-
void mod_sdcard_deinit() {
318+
void machine_sdcard_deinit_all() {
319319
for (uint8_t i = 0; i < MAX_SDHC_SLOT; i++) {
320320
if (sdhc_obj[i] != NULL) {
321321
machine_sdcard_deinit(MP_OBJ_FROM_PTR(sdhc_obj[i]));

ports/psoc6/machine_spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
419419

420420
#endif
421421

422-
void mod_spi_deinit() {
422+
void machine_spi_deinit_all() {
423423
for (uint8_t i = 0; i < MAX_SPI; i++) {
424424
if (spi_obj[i] != NULL) {
425425
machine_spi_deinit((mp_obj_base_t *)(spi_obj[i]));

ports/psoc6/machine_timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
246246
);
247247

248248

249-
void mod_timer_deinit() {
249+
void machine_timer_deinit_all() {
250250
for (uint8_t i = 0; i < MAX_TIMER; i++) {
251251
if (timer_obj[i] != NULL) {
252252
machine_timer_deinit((mp_obj_base_t *)(timer_obj[i]));

ports/psoc6/machine_uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ static mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint
483483
return ret;
484484
}
485485

486-
void mod_uart_deinit() {
486+
void machine_uart_deinit_all() {
487487
for (uint8_t i = 0; i < MAX_UART; i++) {
488488
if (uart_obj[i] != NULL) {
489489
mp_machine_uart_deinit(uart_obj[i]);

0 commit comments

Comments
 (0)