@@ -28,13 +28,16 @@ extern "C" {
28
28
static const int CY_US_PER_SECOND = 1000000 ;
29
29
static const int CY_US_PER_MS = 1000 ;
30
30
31
+ static float _percent = 0.0f ;
32
+
31
33
void pwmout_init (pwmout_t * obj , PinName pin )
32
34
{
33
35
if (CY_RSLT_SUCCESS != cyhal_pwm_init (& (obj -> hal_pwm ), pin , NULL )) {
34
36
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER_PWM , MBED_ERROR_CODE_FAILED_OPERATION ), "cyhal_pwm_init" );
35
37
}
36
38
obj -> period_us = 100 ;
37
39
obj -> width_us = 0 ;
40
+ _percent = 0.0f ;
38
41
}
39
42
40
43
void pwmout_free (pwmout_t * obj )
@@ -46,6 +49,7 @@ void pwmout_write(pwmout_t *obj, float percent)
46
49
{
47
50
MBED_ASSERT (percent >= 0.0f && percent <= 1.0f );
48
51
pwmout_pulsewidth_us (obj , (int )(percent * obj -> period_us ));
52
+ _percent = percent ;
49
53
}
50
54
51
55
float pwmout_read (pwmout_t * obj )
@@ -66,6 +70,9 @@ void pwmout_period_ms(pwmout_t *obj, int ms)
66
70
void pwmout_period_us (pwmout_t * obj , int us )
67
71
{
68
72
obj -> period_us = (uint32_t )us ;
73
+ if (_percent != 0.0f ) {
74
+ obj -> width_us = (int )(_percent * obj -> period_us );
75
+ }
69
76
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period (& (obj -> hal_pwm ), obj -> period_us , obj -> width_us )) {
70
77
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER_PWM , MBED_ERROR_CODE_FAILED_OPERATION ), "cyhal_pwm_set_period" );
71
78
}
@@ -91,6 +98,7 @@ void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
91
98
92
99
void pwmout_pulsewidth_us (pwmout_t * obj , int us )
93
100
{
101
+ _percent = 0.0f ;
94
102
obj -> width_us = (uint32_t )us ;
95
103
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period (& (obj -> hal_pwm ), obj -> period_us , obj -> width_us )) {
96
104
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER_PWM , MBED_ERROR_CODE_FAILED_OPERATION ), "cyhal_pwm_set_period" );
0 commit comments