Skip to content

Commit 286a4e4

Browse files
author
Bogdan Marinescu
committed
[K64F] Fix PWM implementation
Also change the pin for the PWM LED test.
1 parent 6a7b119 commit 286a4e4

File tree

2 files changed

+23
-9
lines changed
  • libraries
    • mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F
    • tests/mbed/pwm_led

2 files changed

+23
-9
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/pwmout_api.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static const PinMap PinMap_PWM[] = {
6969
{NC , NC , 0}
7070
};
7171

72-
static float pwm_clock;
72+
static float pwm_clock_mhz;
7373

7474
void pwmout_init(pwmout_t* obj, PinName pin) {
7575
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
@@ -78,7 +78,9 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
7878
}
7979
obj->pwm_name = pwm;
8080

81-
float clkval = clock_hal_get_fllclk() / 1000000.0f;
81+
uint32_t pwm_base_clock;
82+
clock_manager_get_frequency(kBusClock, &pwm_base_clock);
83+
float clkval = (float)pwm_base_clock / 1000000.0f;
8284
uint32_t clkdiv = 0;
8385
while (clkval > 1) {
8486
clkdiv++;
@@ -88,17 +90,23 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
8890
}
8991
}
9092

91-
pwm_clock = clkval;
93+
pwm_clock_mhz = clkval;
9294
uint32_t channel = pwm & 0xF;
9395
uint32_t instance = pwm >> TPM_SHIFT;
9496
clock_manager_set_gate(kClockModuleFTM, instance, true);
9597
ftm_hal_set_tof_frequency(instance, 3);
96-
ftm_hal_set_clock_ps(instance, (ftm_clock_ps_t)clkdiv);
9798
ftm_hal_set_clock_source(instance, kClock_source_FTM_SystemClk);
98-
ftm_hal_set_channel_edge_level(instance, channel, 2);
99+
ftm_hal_set_clock_ps(instance, (ftm_clock_ps_t)clkdiv);
100+
ftm_hal_set_counter_init_val(instance, 0);
99101
// default to 20ms: standard for servos, and fine for e.g. brightness control
100102
pwmout_period_ms(obj, 20);
101103
pwmout_write (obj, 0);
104+
ftm_config_t config = {
105+
.mode = kFtmEdgeAlignedPWM,
106+
.channel = channel,
107+
.edge_mode = {.ftm_pwm_edge_mode = kFtmHighTrue}
108+
};
109+
ftm_hal_enable_pwm_mode(instance, &config);
102110

103111
// Wire pinout
104112
pinmap_pinout(pin, PinMap_PWM);
@@ -116,11 +124,14 @@ void pwmout_write(pwmout_t* obj, float value) {
116124
uint16_t mod = ftm_hal_get_mod(obj->pwm_name >> TPM_SHIFT);
117125
uint32_t new_count = (uint32_t)((float)(mod) * value);
118126
ftm_hal_set_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, new_count);
127+
ftm_hal_set_counter(obj->pwm_name >> TPM_SHIFT, 0);
119128
}
120129

121130
float pwmout_read(pwmout_t* obj) {
122131
uint16_t count = ftm_hal_get_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, 0);
123132
uint16_t mod = ftm_hal_get_mod(obj->pwm_name >> TPM_SHIFT);
133+
if (mod == 0)
134+
return 0.0;
124135
float v = (float)(count) / (float)(mod);
125136
return (v > 1.0f) ? (1.0f) : (v);
126137
}
@@ -135,10 +146,13 @@ void pwmout_period_ms(pwmout_t* obj, int ms) {
135146

136147
// Set the PWM period, keeping the duty cycle the same.
137148
void pwmout_period_us(pwmout_t* obj, int us) {
149+
uint32_t instance = obj->pwm_name >> TPM_SHIFT;
138150
float dc = pwmout_read(obj);
139-
ftm_hal_set_mod(obj->pwm_name >> TPM_SHIFT, (uint32_t)(pwm_clock * (float)us));
151+
// Stop FTM clock to ensure instant update of MOD register
152+
ftm_hal_set_clock_source(instance, kClock_source_FTM_None);
153+
ftm_hal_set_mod(instance, (uint32_t)(pwm_clock_mhz * (float)us) - 1);
140154
pwmout_write(obj, dc);
141-
155+
ftm_hal_set_clock_source(instance, kClock_source_FTM_SystemClk);
142156
}
143157

144158
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
@@ -150,6 +164,6 @@ void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
150164
}
151165

152166
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
153-
uint32_t value = (uint32_t)(pwm_clock * (float)us);
167+
uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us);
154168
ftm_hal_set_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, value);
155169
}

libraries/tests/mbed/pwm_led/pwm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "mbed.h"
22

33
#if defined(TARGET_K64F)
4-
#define TEST_LED D5
4+
#define TEST_LED D9
55

66
#elif defined(TARGET_NUCLEO_F103RB)
77
#define TEST_LED D3

0 commit comments

Comments
 (0)