CommunityGD32Cores / gd32-pio-projects

PlatformIO test projects for the new GD32 platform and arduino core
44 stars 10 forks source link

pwm demo can not work #2

Closed be-engineer closed 2 years ago

be-engineer commented 2 years ago

I try to use this demo gd32-arduino-pwm-out.It compile with no error,but PWM4 has no wave output. My MCU is GD32F303CC.

#include <Arduino.h>

#if defined(PA6) && defined(PC1)
#define PWM_OUT_PIN PA6
#define LED_PIN PC13
#else
#define PWM_OUT_PIN PWM4
#define LED_PIN LED_BUILTIN
#endif

PWM pwm_output(PWM_OUT_PIN);

void captureCompareCallback()
{
  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}

void setup()
{
  /* starts a PWM wave with 500ms total period, 300ms of which are on (aka 60% duty cycle) */
  /* also uses the capture compare callback to additionally toggle an LED in the same rythm. */
  /* we cannot yet use analogWrite() for this as it is not implemented for PWM. */
  pinMode(LED_PIN, OUTPUT);
  pwm_output.setPeriodCycle(5, 3, FORMAT_MS);
  pwm_output.start();
  pwm_output.attachInterrupt(captureCompareCallback);
}

void loop()
{
}
maxgerhardt commented 2 years ago

You might be running in the issue https://github.com/CommunityGD32Cores/ArduinoCore-GD32/issues/4.

Also, due to the code block

#if defined(PA6) && defined(PC1)
#define PWM_OUT_PIN PA6
#define LED_PIN PC13
#else
#define PWM_OUT_PIN PWM4
#define LED_PIN LED_BUILTIN
#endif

and the GD32F303CC has both PA6 and PC1 defined (source), it will attempt the PWM output on PA6, not PWM4 which is equal to PA15. ARe you measuring on PA6?

be-engineer commented 2 years ago

I tested it again with the following code,from PWM0-PWM5, but there is no output.,even PB6, PB5, etc.

#include <Arduino.h>
PWM pwm_output(PWM2);

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");
  pwm_output.setPeriodCycle(500, 250, FORMAT_MS);
  pwm_output.start();
}

void loop() {}
maxgerhardt commented 2 years ago

I'll check it out now -- the GD32F303CC variant may not yet have its pinmap updated.

maxgerhardt commented 2 years ago

This was a bug on our side in the enabling of the timer clocks -- it has been fixed now.

grafik

In https://github.com/CommunityGD32Cores/ArduinoCore-GD32/commit/0f7e9e5c1dd51447677e06799daa8405e78cb6c9 and https://github.com/CommunityGD32Cores/ArduinoCore-GD32/commit/d142fc696944b6396c252ba16fe08ff5e8be76c7.

I've changed the PWMx default definitions to not collide with the default UART pins anymore (PA9 and PA10), see

https://github.com/CommunityGD32Cores/ArduinoCore-GD32/blob/d142fc696944b6396c252ba16fe08ff5e8be76c7/variants/GD32F303CC_GENERIC/variant.h#L102-L107

for the current definitions.

I have personally tested every PWM0 to PWM5 output pin to be correct.

To update, please open a CLI and execute pio platform update gd32, it should pull the newest framwork-arduinogd32 version.