아두이노우노 R4

Arduino UNO R4의 FspTimer 라이브러리 사용법

지구빵집 2024. 3. 26. 17:14
반응형

 

 

1. 시작하기

 

Arduino UNO R4의 보급이 활발해져서 이미 구입하신 분들도 많을 것으로 생각됩니다. 이 보드에서 타이머 인터럽트를 사용하는 방법을 알아보다가 FspTimer라는 라이브러리가 표준으로 제공된다는 것을 알게 되어 사용법을 알아보았습니다.

 

2. FspTimer 구현

 

FspTimer의 헤더 파일과 소스 파일은 다음 위치에 있습니다.

 

C:\Users\%USERNAME%\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.2\cores\arduino\FspTimer.h

 

C:\Users\%USERNAME%\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.2\cores\arduino\FspTimer.cpp

 

사용법은? 찾았는데, API에 대한 설명은 찾아볼 수 없었습니다.

 

3. API 조사

 

우선 아래 소스에서 사용되는 것부터 사용법을 알아보겠습니다.

 

Arduino_LED_Matrix.h 

 

3.1 get_available_timer

 

형식

 

static int8_t get_available_timer(uint8_t &type, bool force = false);

 

설명

 

사용하지 않는 타이머 채널 번호와 타이머 종류를 가져온다. 타이머 채널 번호는 반환값으로, 타이머 종류는 첫 번째 인자로 가져옵니다. 타이머 종류는 GPT_TIMER 또는 AGT_TIMER가 반환됩니다. 처리를 보면 사용하지 않는 타이머는 GPT_TIMER에서 검색되며, 둘 중 하나를 지정해서 가져오는 것은 불가능해 보입니다.

 

사용 예시

 

 uint8_t timer_type;
    int8_t timer_ch = FspTimer::get_available_timer(timer_type);
    if (timer_ch < 0) {
        // 할당 실패
    }

 

3.2 begin

 

형식

 

bool begin(timer_mode_t mode, uint8_t type, uint8_t type, uint8_t channel, float freq_hz, float duty_perc, GPTimerCbk_f cbk = nullptr , void *ctx = nullptr );

 

설명

 

FspTimer의 인스턴스에 파라미터를 설정한다. 첫 번째 인수의 mode에 지정할 수 있는 것은 다음과 같다. 현재 어떤 동작이 될지는 미확인입니다. 

 

설명
TIMER_MODE_PERIODIC  주기적 타이머 
TIMER_MODE_ONE_SHOT  원샷 타이머 
TIMER_MODE_PWM  PWM 모드 
TIMER_MODE_ONE_SHOT_PULSE  원샷 펄스 모드 
TIMER_MODE_TRIANGLE_WAVE_SYMMETRIC_PWM  대칭 삼각파 PWM 모드 
TIMER_MODE_TRIANGLE_WAVE_ASYMMETRIC_PWM  비대칭 삼각파 PWM 모드 
TIMER_MODE_TRIANGLE_WAVE_ASYMMETRIC_PWM_MODE3  비대칭 삼각파 PWM 모드 3 

 

  • type과 channel은 앞서 설명한 get_available_timer()에서 가져온 값을 지정한다.
  • freq_hz에는 주파수를 float 형식으로 지정한다.
  • duty_perc에는 듀티비를 float 형식으로 지정한다.
  • cbk에는 인터럽트 발생 시 호출할 콜백 함수를 지정한다.
  • ctx에는 위의 콜백 함수에 전달할 인수를 지정합니다. 

 

사용 사례  

 

void timer_callback(timer_callback_args_t *arg);
uint8_t timer_type;
int8_t timer_ch
FspTimer fsp_timer;

fsp_timer.begin(TIMER_MODE_PERIODIC, timer_type, static_cast<uint8_t>(timer_ch), 1000.0, 25.0, timer_callback, nullptr);

 

 

FspTimer 클래스에는 다음과 같은 begin 메서드도 정의되어 있으므로, freq_hz와 duty_perc는 float로 지정한다. 

 

bool begin(timer_mode_t mode, uint8_t type, uint8_t channel,  uint32_t period, uint32_t pulse, timer_source_div_t sd, GPTimerCbk_f cbk = nullptr , void *ctx = nullptr);

 

 

3.3 setup_overflow_irq

 

형식

 

bool setup_overflow_irq(uint8_t priority = 12, Irq_f isr_fnc = nullptr );

 

설명

 

타이머 오버플로우 인터럽트를 활성화한다. 두 번째 인자 isr_fnc는 사용하지 않는 것으로 보인다.

 

사용 예시

 

FspTimer fsp_timer;

fsp_timer.setup_overflow_irq();

 

 

3.4 open

 

형식

 

bool open();

 

설명

 

해당 타이머의 채널을 활성화한다.

 

사용 예시 FspTimer fsp_timer; fsp_timer.open();

 

3.5 start

 

형식

 

bool start();

타이머를 시작합니다.

 

사용 예시

 

FspTimer fsp_timer;

fsp_timer.start();

 

 

4. 프로그램 예시 타이머를 사용하여 LED를 점멸시키는 프로그램입니다. 

 

#include <FspTimer.h>

static constexpr uint32_t led_on_ms = 100;
static constexpr uint32_t led_off_ms = 400; 

static FspTimer fsp_timer;

static bool is_led_on;

void
timer_callback([[maybe_unused]]timer_callback_args_t *arg)
{
  static uint32_t cnt = 0;

  cnt++;
  if (is_led_on) {
    if (cnt > led_on_ms) {
      digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
      is_led_on = false;
      cnt = 0;
    }
  } else {
    if (cnt > led_off_ms) {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on
      is_led_on = true;
      cnt = 0;
    }
  }
}

void
setup()
{
  while (!Serial) {
    ;
  }
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on
  is_led_on = true;

  uint8_t timer_type;
  int8_t timer_ch = FspTimer::get_available_timer(timer_type);
  if (timer_ch < 0) {
    Serial.println("get_available_timer() failed.");
    return;
  }
  fsp_timer.begin(TIMER_MODE_PERIODIC, timer_type, static_cast<uint8_t>(timer_ch), 1000.0, 25.0, timer_callback, nullptr);
  fsp_timer.setup_overflow_irq();
  fsp_timer.open();
  fsp_timer.start();
  Serial.println("started.");
}

void
loop()
{
  __WFI(); 
}

 

 

5. 앞으로의 방침 다음에는 FspTimer의 PWM 기능을 알아보고 싶습니다. 

 

 

참고 문서 

 

잘 정리한 일본어 사이트 

위 내용을 쓴 개발자 사이트 - 좋은 내용 많네. 

 

 

 

 

 

반응형