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

CH32X035F8U6 hardwaretimer PWM bug #162

Open
0x0fe opened this issue Nov 12, 2024 · 0 comments
Open

CH32X035F8U6 hardwaretimer PWM bug #162

0x0fe opened this issue Nov 12, 2024 · 0 comments

Comments

@0x0fe
Copy link

0x0fe commented Nov 12, 2024

So apparently there is a bug in hardwaretimer PWM mode 1 for CH32X035F8U6 :

The first bug is that CH_PIN_CHANNEL(pinmap_function(digitalPinToPinName(PA7), PinMap_PWM)); will return the value 1, but it should return the value 2 (PA7 is T3C2).

The second bug is that if srvTim->setMode(channel2, TIMER_OUTPUT_COMPARE_PWM1, PA7); is called, it will just crash the system.
When only srvTim->setMode(channel1, TIMER_OUTPUT_COMPARE_PWM1, PA6); is called, the PWM on PA6 works normally.

HardwareTimer *srvTim;
uint32_t channel1,channel2;

void setup() { 
  Serial.begin(115200);
  Serial.println("\n\nSystem init");
  
  TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(PA6), PinMap_PWM);
  channel1 = 1;//CH_PIN_CHANNEL(pinmap_function(digitalPinToPinName(PA6), PinMap_PWM));
  channel2 = 2;//CH_PIN_CHANNEL(pinmap_function(digitalPinToPinName(PA7), PinMap_PWM)); // BUG returns 1 instead of 2
  srvTim = new HardwareTimer(Instance);
  srvTim->setOverflow(100, HERTZ_FORMAT); 
  srvTim->setMode(channel1, TIMER_OUTPUT_COMPARE_PWM1, PA6);
  //srvTim->setMode(channel2, TIMER_OUTPUT_COMPARE_PWM1, PA7); // BUG crashes if enabled
 }
void loop() {
  // Interval::handler();
  Serial.printf("PWM A\n");
  for(int i=0; i<320; i++){
    time = map(i, 0, 320, 500, 2500); 
    srvTim->setCaptureCompare(channel1, time,  MICROSEC_COMPARE_FORMAT);
    delay(8);
  }
  for(int i=320; i>0; i--){
    time = map(i, 0, 320, 500, 2500); 
    srvTim->setCaptureCompare(channel1, time,  MICROSEC_COMPARE_FORMAT);
    delay(8);    
  }
  Serial.printf("PWM B\n");
  for(int i=0; i<320; i++){
    time = map(i, 0, 320, 500, 2500); 
    srvTim->setCaptureCompare(channel2, time,  MICROSEC_COMPARE_FORMAT);
    delay(8);
  }
  for(int i=320; i>0; i--){
    time = map(i, 0, 320, 500, 2500);
    srvTim->setCaptureCompare(channel2, time,  MICROSEC_COMPARE_FORMAT);
    delay(8);    
  }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant