LoginSignup
0
0

More than 3 years have passed since last update.

2019-10-12 LPC > LPCXpresso54628 > RIT (Repetitive Interrupt Timer ) > 1Hzから2Hzや1/2Hzに変更する

Last updated at Posted at 2019-10-12
動作環境
OS: Ubuntu 18.04 LTS
基板: LPCXpresso54628
IDE: MCUXpresso IDE v11.0.1 [Build 2563] [2019-09-01]

概要

  • SDK example(s)においてrit.cを試した
  • rit: Repetitive Interrupt Timer
    • 周期的にLEDを点滅するサンプルコード
  • 1Hzの周期から2Hzや1/2Hzに変更する

変更後のソースコード

下記にSDK exampleのrit.cを改変したコードを掲載する。

BSD 3条項ライセンス

rit.c
/*
 * Copyright (c) 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 * All rights reserved.
 *
 * Modified by 7of9 (OCt.12, 2019)
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/*
 * Oct. 12, 2019, 7of9
 *   - change timer interval to 2Hz (1/2Hz)
 */

#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_rit.h"

#include "pin_mux.h"
#include <stdbool.h>
/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define RIT_IRQ_ID RIT_IRQn
/* Get source clock for RIT driver */
#define RIT_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_CoreSysClk)
#define LED_INIT() LED1_INIT(LOGIC_LED_ON)
#define LED_TOGGLE() LED1_TOGGLE()
#define APP_RIT_HANDLER RIT_IRQHandler

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/

void APP_RIT_HANDLER(void)
{
    RIT_ClearStatusFlags(RIT, kRIT_TimerFlag);
    LED_TOGGLE();
    __DSB();
}
/*!
 * @brief Main function
 */
int main(void)
{
    /* Structure of initialize RIT */
    rit_config_t ritConfig;

    /* Board pin, clock, debug console init */
    /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
    CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

    CLOCK_EnableClock(kCLOCK_Gpio3);

    BOARD_InitPins();
    BOARD_BootClockPLL220M();
    BOARD_InitDebugConsole();

    /* Init LED. */
    LED_INIT();

    /*
     * ritConfig.enableRunInDebug = false;
     */
    RIT_GetDefaultConfig(&ritConfig);

    /* Init rit module */
    RIT_Init(RIT, &ritConfig);

    /* Set timer period for Compare register. */
    //RIT_SetTimerCompare(RIT, RIT_SOURCE_CLOCK); // 1Hz
    //RIT_SetTimerCompare(RIT, RIT_SOURCE_CLOCK / 2); // 1/2Hz
    RIT_SetTimerCompare(RIT, RIT_SOURCE_CLOCK * 2); // 2Hz

    /* Set to enable the Counter register auto clear to zero when the counter value equals the set period. */
    RIT_SetCountAutoClear(RIT, true);

    PRINTF("RIT Example Start, You will see spcified LED blink at 1s period interval.\r\n");

    /* Start counting */
    RIT_StartTimer(RIT);

    /* Enable at the NVIC */
    EnableIRQ(RIT_IRQ_ID);

    while (1)
    {
    }
}

1Hz動作の実装部分

1Hz動作の設定は下記にて実施される。

#define RIT_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_CoreSysClk)
...
/* Set timer period for Compare register. */
RIT_SetTimerCompare(RIT, RIT_SOURCE_CLOCK); // 1Hz

RIT_SetTimerCompare()はdrivers/fsl_rit.cにて定義されている。

void RIT_SetTimerCompare(RIT_Type *base, uint64_t count)

countの値を2倍にすることで2Hz, 1/2倍にすることで1/2Hzに変更することができる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0