Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LPC1347] PWMout freq on 16 bit timers wrong #2660

Closed
JojoS62 opened this issue Sep 10, 2016 · 5 comments
Closed

[LPC1347] PWMout freq on 16 bit timers wrong #2660

JojoS62 opened this issue Sep 10, 2016 · 5 comments
Assignees

Comments

@JojoS62
Copy link
Contributor

JojoS62 commented Sep 10, 2016

Description

  • Type: Bug
  • Priority: Major

Bug

PWMout frequency is higher than set in pwm.period(). This happens on pwm pins with 16 bit counters CT16B0 / CT16B1, on CT32B0 it works as expected.

Target
LPC1347

Toolchain:
ARM, online compiler

mbed lib 64...125

Expected behavior
pwm.period(0.01) -> 10 ms

Actual behavior
pwm.period(0.01) -> 0.898 ms

Steps to reproduce

#include <mbed.h>

PwmOut pwm_ok(P0_18);
PwmOut pwm_not_ok(P0_9);

int main()
{
    pwm_ok.period(0.01f);       // 10 ms / 100 Hz
    pwm_ok = 0.25f;

    pwm_not_ok.period(0.01f);   // 10 ms / 100 Hz
    pwm_not_ok = 0.25f;

    while(1);
}

pwmout-problemwithct16

@JojoS62
Copy link
Contributor Author

JojoS62 commented Sep 10, 2016

The problem is: clock of 72 MHz / 2^16 gives a max pwm period time of about 0.9 ms.
I've used the solution from LPC11XX that sets a prescaler, initial to 31 to get the default setting of 20 ms pwm period time working. For times >28 ms it is necessary to set a higher prescaler value.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 12, 2016

cc @toyowata

@toyowata
Copy link
Contributor

I got the LPCXpresso1347 board and just started to see detail. I hope same fix as the LPC11XX can be used. I will investigate more.

@toyowata toyowata self-assigned this Sep 20, 2016
@JojoS62
Copy link
Contributor Author

JojoS62 commented Sep 20, 2016

Great,

And thanks for your help.

I have attached the modified source, I don’t know if my pull request did work.

Another thing to note: there is a definition for CT32B1, but this is used for the internal mbed ticker and also for USB, so these PWM pins should not be used.

Then I use the LPC1347 with the LPCXpresso. I have created the startup code for this toolchain and modified the linker script to use the nano lib, otherwise the generated code will not fit into this controller.

Regards,

Johannes

Von: Toyomasa Watarai [mailto:[email protected]]
Gesendet: Dienstag, 20. September 2016 16:29
An: ARMmbed/mbed-os
Cc: JojoS62; Author
Betreff: Re: [ARMmbed/mbed-os] [LPC1347] PWMout freq on 16 bit timers wrong (#2660)

I got the LPCXpresso1347 board and just started to see detail. I hope same fix as the LPC11XX can be used. I will investigate more.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub #2660 (comment) , or mute the thread https://github.com/notifications/unsubscribe-auth/AGdjCvv0pOkIlM8Kguq4UN_NbcRExUEfks5qr-2YgaJpZM4J5sjV . https://github.com/notifications/beacon/AGdjCqcKl9BJXK9NPKa0Wxc4RFHtHw_Aks5qr-2YgaJpZM4J5sjV.gif

/* mbed Microcontroller Library

  • Copyright (c) 2006-2013 ARM Limited
    *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
    *
  • http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */
    #include "mbed_assert.h"
    #include "pwmout_api.h"
    #include "cmsis.h"
    #include "pinmap.h"

#define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002

/* To have a PWM where we can change both the period and the duty cycle,

  • we need an entire timer. With the following conventions:

  • * MR3 is used for the PWM period

    • MR0, MR1, MR2 are used for the duty cycle
      /
      static const PinMap PinMap_PWM[] = {
      /
      CT16B0 /
      {P0_8 , PWM_1, 2}, {P1_13, PWM_1, 2}, /
      MR0 /
      {P0_9 , PWM_2, 2}, {P1_14, PWM_2, 2}, /
      MR1 /
      {P0_10, PWM_3, 3}, {P1_15, PWM_3, 2}, /
      MR2 */

    /* CT16B1 /
    {P0_21, PWM_4, 1}, /
    MR0 /
    {P0_22, PWM_5, 2}, {P1_23, PWM_5, 1}, /
    MR1 */

    /* CT32B0 /
    {P0_18, PWM_6, 2}, {P1_24, PWM_6, 1}, /
    MR0 /
    {P0_19, PWM_7, 2}, {P1_25, PWM_7, 1}, /
    MR1 /
    {P0_1 , PWM_8, 2}, {P1_26, PWM_8, 1}, /
    MR2 */

    /* CT32B1 /
    {P0_13, PWM_9 , 3}, //{P1_0, PWM_9 , 1}, /
    MR0 /
    {P0_14, PWM_10, 3}, //{P1_1, PWM_10, 1}, /
    MR1 /
    {P0_15, PWM_11, 3}, //{P1_2, PWM_11, 1}, /
    MR2 */

    {NC, NC, 0}
    };

typedef struct {
uint8_t timer;
uint8_t mr;
} timer_mr;

static timer_mr pwm_timer_map[11] = {
{0, 0}, {0, 1}, {0, 2},
{1, 0}, {1, 1},
{2, 0}, {2, 1}, {2, 2},
{3, 0}, {3, 1}, {3, 2},
};

static LPC_CTxxBx_Type *Timers[4] = {
LPC_CT16B0, LPC_CT16B1,
LPC_CT32B0, LPC_CT32B1
};

static unsigned int pwm_clock_mhz;

void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
MBED_ASSERT(pwm != (uint32_t)NC);

obj->pwm = pwm;

// Timer registers
timer_mr tid = pwm_timer_map[pwm];
LPC_CTxxBx_Type *timer = Timers[tid.timer];

// Disable timer
timer->TCR = 0;

// Power the correspondent timer
LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);

/* Enable PWM function */
timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);

/* Reset Functionality on MR3 controlling the PWM period */
timer->MCR = 1 << 10;

if (timer == LPC_CT16B0 || timer == LPC_CT16B1) {
/* Set 16-bit timer prescaler to avoid timer expire for default 20ms */
/* This can be also modified by user application, but the prescaler value */
/* might be trade-off to timer accuracy. max Period time is 28 ms */
    timer->PR = 30;
} 

pwm_clock_mhz = SystemCoreClock / 1000000;

// default to 20ms: standard for servos, and fine for e.g. brightness control
pwmout_period_ms(obj, 20);
pwmout_write    (obj, 0);

// Wire pinout
pinmap_pinout(pin, PinMap_PWM);

}

void pwmout_free(pwmout_t* obj) {
// [TODO]
}

void pwmout_write(pwmout_t* obj, float value) {
if (value < 0.0f) {
value = 0.0;
} else if (value > 1.0f) {
value = 1.0;
}

timer_mr tid = pwm_timer_map[obj->pwm];
LPC_CTxxBx_Type *timer = Timers[tid.timer];
uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);

timer->MR[tid.mr] = t_off;

}

float pwmout_read(pwmout_t* obj) {
timer_mr tid = pwm_timer_map[obj->pwm];
LPC_CTxxBx_Type *timer = Timers[tid.timer];

float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
return (v > 1.0f) ? (1.0f) : (v);

}

void pwmout_period(pwmout_t* obj, float seconds) {
pwmout_period_us(obj, seconds * 1000000.0f);
}

void pwmout_period_ms(pwmout_t* obj, int ms) {
pwmout_period_us(obj, ms * 1000);
}

// Set the PWM period, keeping the duty cycle the same.
void pwmout_period_us(pwmout_t* obj, int us) {
int i = 0;
uint32_t period_ticks;

timer_mr tid = pwm_timer_map[obj->pwm];
LPC_CTxxBx_Type *timer = Timers[tid.timer];
uint32_t old_period_ticks = timer->MR3;
period_ticks = (pwm_clock_mhz * us) / (timer->PR + 1);

timer->TCR = TCR_RESET;
timer->MR3 = period_ticks;

// Scale the pulse width to preserve the duty ratio
if (old_period_ticks > 0) {
    for (i=0; i<3; i++) {
        uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
        timer->MR[i] = t_off;
    }
}
timer->TCR = TCR_CNT_EN;

}

void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
}

void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
pwmout_pulsewidth_us(obj, ms * 1000);
}

void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
timer_mr tid = pwm_timer_map[obj->pwm];
LPC_CTxxBx_Type *timer = Timers[tid.timer];
uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1)); ;

timer->TCR = TCR_RESET;
if (t_on > timer->MR3) {
    pwmout_period_us(obj, us);
}
uint32_t t_off = timer->MR3 - t_on;
timer->MR[tid.mr] = t_off;
timer->TCR = TCR_CNT_EN;

}

@toyowata
Copy link
Contributor

@JojoS62 Thanks. I created a PR here: #2810

@JojoS62 JojoS62 closed this as completed Dec 10, 2016
artokin added a commit to artokin/mbed-os that referenced this issue Sep 23, 2021
…..225a4af

225a4af Remove files from tests folder
58d2c8f Merge remote-tracking branch 'origin/release_internal' into release_external
921b4b3 Wi-SUN FAN 1.1 dynamic MDR data request enabler
b8722e8 Corrected BR removing of waiting list entry when supplicant is in key storage
0d54d7a Adjust trace levels (ARMmbed#2692)
681d9ea Added reset for pan id and version to BR network start
30d4fb2 Renaming and cleaning ws bootstrap (ARMmbed#2688)
e0da19d Add Wi-SUN host configuration (ARMmbed#2690)
50ecc3d Refactoring Wi-SUN stack (ARMmbed#2686)
9d2386d Renamed operation mode to operating mode.
2f755bc RF config resolver and some refactoring (ARMmbed#2683)
86c6d19 Fixed WS IE PCAP read operation wrong length usage.
cd3a4c2 Config: Remove additional HAVE_WS_ROUTER (ARMmbed#2684)
cdd7f2d Added API for configure supported Phy capability.
a00a3c0 Wi-SUN FAN 1.1 PCAP IE update
2d063d3 Moved State machine and timer functions to own files
edb8bec Corrected system time check function return values
85358a6 Moved Wi-SUN Bootstrap Event handling to separate device handlers
61cbdde MAC to support mode switch on single channel (ARMmbed#2678)
1006d29 Added storing of PAN ID to NVM in BBR
7bf0028 Corrected system time jump detection on BR startup
e60974d Split Wi-SUN bootstrap to device types
a3f3412 MAC data req: API to support mode switch (ARMmbed#2674)
cad5122 Removed automatic network size configuration (ARMmbed#2673)
35d3132 MAC: Callback set to resolve PHY mode ID (ARMmbed#2672)
0c5faca Added support for large system time changes (e.g. due to NTP) (ARMmbed#2670)
c94b306 LFN version and LGTK Hash IE advertisment and learn
8e07511 Use FAN version constant  instead of pure number
a5566b2 Channel Plan 2 validation and FAN 1.0 reject
42dba41 Wi-Sun IE FAN 1.1 update
1d56070 EU channel plan ids (FAN 1.1) supported (ARMmbed#2668)
fc4f41f Add test API empty function
37efc7e Add version 1.1 basic support
e1558fb Implemented mode switch PHR build and parse (ARMmbed#2665)
cbd8a15 Corrected frame counter storing threshold check
37f7ae9 Time configuration distribution using DHCPv6 vendor data
7415bc7 Added checks for Border Router frame counter space exhaustion (ARMmbed#2660)
f1a65ec Mode switch PHY API (ARMmbed#2663)
e54231b Do not check buffer age when virtual RF driver used (ARMmbed#2662)
cc8c7bd arm_network_certificate_chain_set() returns -2 when PANA is disabled
319dd91 Fix dubious semicolon in #define
2ff51ab Remove extra '\n' in traces
19376c8 Simplify array indexes
c808661 Fix ASAN warnings about overflows in bit shifts
f998008 Fix use-after-free in mac_helper_coordinator_address_set()
4d04541 Wi-SUN header and Paylod IE element lenght future proof update.
935898b Medium network PAN_TIMEOUT changed to 30 minutes
1af7cfe Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
29744e0 If Router Solicitation creation fails no longer tries to retry the RS right away (ARMmbed#2655)
2b889e9 Added automatic test procedure triggering during bootstrap
ed9eb05 GTKs are removed only when fresh GTK hash is received
81ecdc2 Added empty function for test procedure trigger
14439b4 Added support for triggering test procedures
b8a67a9 Update CHANGELOG.md for Nanostack 14.0.0 (ARMmbed#2649)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 225a4af
artokin added a commit to artokin/mbed-os that referenced this issue Sep 23, 2021
…..225a4af

225a4af Remove files from tests folder
58d2c8f Merge remote-tracking branch 'origin/release_internal' into release_external
921b4b3 Wi-SUN FAN 1.1 dynamic MDR data request enabler
b8722e8 Corrected BR removing of waiting list entry when supplicant is in key storage
0d54d7a Adjust trace levels (ARMmbed#2692)
681d9ea Added reset for pan id and version to BR network start
30d4fb2 Renaming and cleaning ws bootstrap (ARMmbed#2688)
e0da19d Add Wi-SUN host configuration (ARMmbed#2690)
50ecc3d Refactoring Wi-SUN stack (ARMmbed#2686)
9d2386d Renamed operation mode to operating mode.
2f755bc RF config resolver and some refactoring (ARMmbed#2683)
86c6d19 Fixed WS IE PCAP read operation wrong length usage.
cd3a4c2 Config: Remove additional HAVE_WS_ROUTER (ARMmbed#2684)
cdd7f2d Added API for configure supported Phy capability.
a00a3c0 Wi-SUN FAN 1.1 PCAP IE update
2d063d3 Moved State machine and timer functions to own files
edb8bec Corrected system time check function return values
85358a6 Moved Wi-SUN Bootstrap Event handling to separate device handlers
61cbdde MAC to support mode switch on single channel (ARMmbed#2678)
1006d29 Added storing of PAN ID to NVM in BBR
7bf0028 Corrected system time jump detection on BR startup
e60974d Split Wi-SUN bootstrap to device types
a3f3412 MAC data req: API to support mode switch (ARMmbed#2674)
cad5122 Removed automatic network size configuration (ARMmbed#2673)
35d3132 MAC: Callback set to resolve PHY mode ID (ARMmbed#2672)
0c5faca Added support for large system time changes (e.g. due to NTP) (ARMmbed#2670)
c94b306 LFN version and LGTK Hash IE advertisment and learn
8e07511 Use FAN version constant  instead of pure number
a5566b2 Channel Plan 2 validation and FAN 1.0 reject
42dba41 Wi-Sun IE FAN 1.1 update
1d56070 EU channel plan ids (FAN 1.1) supported (ARMmbed#2668)
fc4f41f Add test API empty function
37efc7e Add version 1.1 basic support
e1558fb Implemented mode switch PHR build and parse (ARMmbed#2665)
cbd8a15 Corrected frame counter storing threshold check
37f7ae9 Time configuration distribution using DHCPv6 vendor data
7415bc7 Added checks for Border Router frame counter space exhaustion (ARMmbed#2660)
f1a65ec Mode switch PHY API (ARMmbed#2663)
e54231b Do not check buffer age when virtual RF driver used (ARMmbed#2662)
cc8c7bd arm_network_certificate_chain_set() returns -2 when PANA is disabled
319dd91 Fix dubious semicolon in #define
2ff51ab Remove extra '\n' in traces
19376c8 Simplify array indexes
c808661 Fix ASAN warnings about overflows in bit shifts
f998008 Fix use-after-free in mac_helper_coordinator_address_set()
4d04541 Wi-SUN header and Paylod IE element lenght future proof update.
935898b Medium network PAN_TIMEOUT changed to 30 minutes
1af7cfe Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
29744e0 If Router Solicitation creation fails no longer tries to retry the RS right away (ARMmbed#2655)
2b889e9 Added automatic test procedure triggering during bootstrap
ed9eb05 GTKs are removed only when fresh GTK hash is received
81ecdc2 Added empty function for test procedure trigger
14439b4 Added support for triggering test procedures
b8a67a9 Update CHANGELOG.md for Nanostack 14.0.0 (ARMmbed#2649)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 225a4af
artokin added a commit to artokin/mbed-os that referenced this issue Sep 23, 2021
…a3c5c5..225a4af

225a4af Remove files from tests folder
58d2c8f Merge remote-tracking branch 'origin/release_internal' into release_external
921b4b3 Wi-SUN FAN 1.1 dynamic MDR data request enabler
b8722e8 Corrected BR removing of waiting list entry when supplicant is in key storage
0d54d7a Adjust trace levels (ARMmbed#2692)
681d9ea Added reset for pan id and version to BR network start
30d4fb2 Renaming and cleaning ws bootstrap (ARMmbed#2688)
e0da19d Add Wi-SUN host configuration (ARMmbed#2690)
50ecc3d Refactoring Wi-SUN stack (ARMmbed#2686)
9d2386d Renamed operation mode to operating mode.
2f755bc RF config resolver and some refactoring (ARMmbed#2683)
86c6d19 Fixed WS IE PCAP read operation wrong length usage.
cd3a4c2 Config: Remove additional HAVE_WS_ROUTER (ARMmbed#2684)
cdd7f2d Added API for configure supported Phy capability.
a00a3c0 Wi-SUN FAN 1.1 PCAP IE update
2d063d3 Moved State machine and timer functions to own files
edb8bec Corrected system time check function return values
85358a6 Moved Wi-SUN Bootstrap Event handling to separate device handlers
61cbdde MAC to support mode switch on single channel (ARMmbed#2678)
1006d29 Added storing of PAN ID to NVM in BBR
7bf0028 Corrected system time jump detection on BR startup
e60974d Split Wi-SUN bootstrap to device types
a3f3412 MAC data req: API to support mode switch (ARMmbed#2674)
cad5122 Removed automatic network size configuration (ARMmbed#2673)
35d3132 MAC: Callback set to resolve PHY mode ID (ARMmbed#2672)
0c5faca Added support for large system time changes (e.g. due to NTP) (ARMmbed#2670)
c94b306 LFN version and LGTK Hash IE advertisment and learn
8e07511 Use FAN version constant  instead of pure number
a5566b2 Channel Plan 2 validation and FAN 1.0 reject
42dba41 Wi-Sun IE FAN 1.1 update
1d56070 EU channel plan ids (FAN 1.1) supported (ARMmbed#2668)
fc4f41f Add test API empty function
37efc7e Add version 1.1 basic support
e1558fb Implemented mode switch PHR build and parse (ARMmbed#2665)
cbd8a15 Corrected frame counter storing threshold check
37f7ae9 Time configuration distribution using DHCPv6 vendor data
7415bc7 Added checks for Border Router frame counter space exhaustion (ARMmbed#2660)
f1a65ec Mode switch PHY API (ARMmbed#2663)
e54231b Do not check buffer age when virtual RF driver used (ARMmbed#2662)
cc8c7bd arm_network_certificate_chain_set() returns -2 when PANA is disabled
319dd91 Fix dubious semicolon in #define
2ff51ab Remove extra '\n' in traces
19376c8 Simplify array indexes
c808661 Fix ASAN warnings about overflows in bit shifts
f998008 Fix use-after-free in mac_helper_coordinator_address_set()
4d04541 Wi-SUN header and Paylod IE element lenght future proof update.
935898b Medium network PAN_TIMEOUT changed to 30 minutes
1af7cfe Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
29744e0 If Router Solicitation creation fails no longer tries to retry the RS right away (ARMmbed#2655)
2b889e9 Added automatic test procedure triggering during bootstrap
ed9eb05 GTKs are removed only when fresh GTK hash is received
81ecdc2 Added empty function for test procedure trigger
14439b4 Added support for triggering test procedures
b8a67a9 Update CHANGELOG.md for Nanostack 14.0.0 (ARMmbed#2649)

git-subtree-dir: connectivity/nanostack/sal-stack-nanostack
git-subtree-split: 225a4af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants