Author Topic: Simple CH558 timer not working  (Read 331 times)

0 Members and 1 Guest are viewing this topic.

Offline dirkxTopic starter

  • Newbie
  • Posts: 1
  • Country: nl
Simple CH558 timer not working
« on: May 18, 2024, 10:19:27 pm »
I've created a ch558 variant for https://github.com/DeqingSun/ch55xduino (https://github.com/dirkx/ch55xduino/tree/FEAT_CH558_Support)  for a small board (https://github.com/dirkx/cheapbot).

This generally works well. But I am having trouble to get Timer3 (the only really 'free' timer) to work. As a pure `fire an IRQ' sort of time.

Most minimal example below. It just flashes an LED.

Does this ring a bell with someone / something obviously wrong ? The example follows the vendor's CH559 example (there is no CH558 example yet).

Dw

#include <Serial.h>

uint32_t tick = 0;



void setup() {

  // Enable the LED
  digitalWrite(40, LOW);
  pinMode(40, OUTPUT);

  // give the Arduino IDE some time to switch.
  delay(3000);
  USBSerial_println("Running " __FILE__ " " __DATE__ " " __TIME__);

#define DIV (1)         // Divisor of F_CPU
#define ENDCOUNT (100)  // Count

  // set divisor
  T3_SETUP |= bT3_EN_CK_SE;  //  Enable to accessing divisor setting register.
  T3_CK_SE_L = DIV & 0xff;
  T3_CK_SE_H = (DIV >> 8) & 0xff;
  T3_SETUP &= ~bT3_EN_CK_SE;  // clear access again.

  T3_CTRL |= bT3_CLR_ALL;   // Force clear FIFO and count of timer3.
  T3_CTRL &= ~bT3_CLR_ALL;  // And go back to reset by software
  T3_SETUP |= bT3_IE_END;   // Enable interruts
  T3_CTRL &= ~bT3_MOD_CAP;  // Timer or PWM mode

  // T3_STAT |= bT3_IF_DMA_END | bT3_IF_FIFO_OV | bT3_IF_FIFO_REQ | bT3_IF_ACT | bT3_IF_END;
  T3_STAT |= bT3_IF_END;  // Interrupt when we pass ENDCOUNT

  T3_END_L = ENDCOUNT & 0xff;
  T3_END_H = (ENDCOUNT >> 8) & 0xff;

  IE_TMR3 = 1;  // Timer3 interrupt is enabled.
  EA = 1;       // Global interrupt enable control bit
}

void Timer3Interrupt(void) __interrupt {
  // clear count-passed endpoint interupt.
  if (T3_STAT & bT3_IF_END)
    T3_STAT |= bT3_IF_END;

  tick ++;
  digitalWrite(40, tick & 1);
}

void loop() {
  {
    static uint32_t last = 0;
    if (last != tick) {
      last = tick;
      USBSerial_print("IRQ seen:");
      USBSerial_println(tick);
      delay(500);
    }
  }
  {
    static uint32_t last;
    if (millis() - last > 2500) {
      last = millis();
      USBSerial_println("Still alive..");
    }
  }
}
 

Offline aliarifat794

  • Regular Contributor
  • *
  • Posts: 130
  • Country: bd
Re: Simple CH558 timer not working
« Reply #1 on: May 23, 2024, 10:38:28 am »
Try to edit your code like this.
#include <Serial.h>

uint32_t tick = 0;

// Define the Timer3 Interrupt Vector
void Timer3Interrupt(void) __interrupt (INT_NO_TMR3) {
  if (T3_STAT & bT3_IF_END) {
    T3_STAT |= bT3_IF_END;  // Clear the interrupt flag
    tick++;
    digitalWrite(40, tick & 1);
  }
}

void setup() {
  // Enable the LED
  pinMode(40, OUTPUT);
  digitalWrite(40, LOW);

  // give the Arduino IDE some time to switch.
  delay(3000);
  USBSerial_println("Running " __FILE__ " " __DATE__ " " __TIME__);

  #define DIV (1)         // Divisor of F_CPU
  #define ENDCOUNT (100)  // Count

  // Set divisor
  T3_SETUP |= bT3_EN_CK_SE;  // Enable accessing divisor setting register.
  T3_CK_SE_L = DIV & 0xff;
  T3_CK_SE_H = (DIV >> 8) & 0xff;
  T3_SETUP &= ~bT3_EN_CK_SE;  // Clear access again.

  T3_CTRL |= bT3_CLR_ALL;   // Force clear FIFO and count of timer3.
  T3_CTRL &= ~bT3_CLR_ALL;  // And go back to reset by software
  T3_SETUP |= bT3_IE_END;   // Enable interrupts
  T3_CTRL &= ~bT3_MOD_CAP;  // Timer or PWM mode

  T3_STAT |= bT3_IF_END;  // Interrupt when we pass ENDCOUNT

  T3_END_L = ENDCOUNT & 0xff;
  T3_END_H = (ENDCOUNT >> 8) & 0xff;

  IE_TMR3 = 1;  // Timer3 interrupt is enabled.
  EA = 1;       // Global interrupt enable control bit
}

void loop() {
  static uint32_t last = 0;
  if (last != tick) {
    last = tick;
    USBSerial_print("IRQ seen:");
    USBSerial_println(tick);
    delay(500);
  }
 
  static uint32_t lastMillis;
  if (millis() - lastMillis > 2500) {
    lastMillis = millis();
    USBSerial_println("Still alive..");
  }
}
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3081
  • Country: us
Re: Simple CH558 timer not working
« Reply #2 on: May 23, 2024, 10:54:49 am »
Try to edit your code like this.
#include <Serial.h>
...
  T3_CK_SE_H = (DIV >> 8) & 0xff;
...
  T3_END_H = (ENDCOUNT >> 8) & 0xff;
...

For code you should enclose it in a [code]...[/code] block.
 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf