RENESAS - 실습1

2022. 2. 2. 20:49전공공부/마이크로프로세서 윈터캠프

1. AGT timer를 사용해서 시간을 설정

 

2. 0.5초마다 LED 4개를 껏다 킴

 

3. SW1을 누르면 7 segment에 지금껏 반복한 사이클 숫자를 표시, 그리고 멈춤 

 

https://youtu.be/96VavJSJw5s

 

 

 

- code

 

#include "hal_data.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
void R_USP_InitLed4();
uint16_t R_USP_GetLed4(uint8_t led);
void R_USP_SetLedOn(int led, bool state);
void R_IRQ_Buttoninterrupt(external_irq_callback_args_t *p_args);
void Segment_Write(int seg_data, int seg_digit);
void Segments_Write(int number);
void R_IRQ_11interrupt(external_irq_callback_args_t *p_args);
void R_USP_SetLed4(uint16_t num);

const uint8_t SEG[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x6F};

/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/

int stopkey;
int cnt;
int agt_counter =0;
bool state;
void hal_entry(void){
    R_AGT_Open(&g_timer0_ctrl, &g_timer0_cfg);
    R_AGT_Start(&g_timer0_ctrl);
    R_IOPORT_Open(&g_ioport_ctrl, &g_bsp_pin_cfg);
    R_ICU_ExternalIrqOpen(&g_external_irq11_ctrl, &g_external_irq11_cfg);
    R_ICU_ExternalIrqEnable(&g_external_irq11_ctrl);
    R_USP_InitLed4();
    stopkey = 0;
    cnt = 0;
    state_ = true;
    // instead of using 'true' -> stopkey
    while(1){
        if(stopkey == 1 || cnt == 10){ // stop
          break;
        }
        for(int i=0; i<4; i++){
            int cur_cnt = agt_counter + 5;
            while(agt_counter != cur_cnt +5){
                R_USP_SetLedOn(i, state_);
            }
        }
        state_ = !state_;
        cnt++;
    }
}

void R_IRQ_11interrupt(external_irq_callback_args_t *p_args){
    FSP_PARAMETER_NOT_USED(p_args);
    stopkey = 1;
    Segments_Write(cnt);
}

int cnt =0;

void Segments_Write(int number){
    int digit[4]= {0,0,0,0};
    digit[0] = number / 1000;
    digit[1] = (number/100) % 10;
    digit[2] = (number%100) / 10;
    digit[3] = number% 10;

    for(int i =0; i<4; i++){
        Segment_Write(SEG[digit[i]], i+1);
        R_BSP_SoftwareDelay(10, BSP_DELAY_UNITS_MILLISECONDS);
    }
    R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
}

// seg_data : number, seg_digit : position
void Segment_Write(int seg_data, int seg_digit){
    if(seg_digit !=0){
        // d1 - col : 5-9
        // D C B A(7654) || DP G F E (14, 13 , 12 ,11)
        R_IOPORT_PortWrite(&g_ioport_ctrl, BSP_IO_PORT_03, (ioport_size_t)(0x0010 << seg_digit), (ioport_size_t)0x01E0);// each digit
        R_IOPORT_PortWrite(&g_ioport_ctrl, BSP_IO_PORT_06, (ioport_size_t)((((~seg_data)&0x0F)<<4)|(((~seg_data)&0xF0)<<7)), (ioport_size_t)0x78F0);
    }
}


void R_AGT0_Interrupt(timer_callback_args_t *p_args){ //when 100ms, interrupt occur
    FSP_PARAMETER_NOT_USED(p_args);
    if(agt_counter >= 1000) agt_counter=0;
    agt_counter++;
}

void R_USP_InitLed4()
{
    R_PORT10->PDR = 0x0700;
    R_PORT11->PDR = 0x0001;

    R_PORT10->PODR = 0x0000;
    R_PORT11->PODR = 0x0000;
}
uint8_t LED_STATE = 0x00;

uint16_t testLevel = 0;
void R_USP_SetLedOn(int led, bool state_)
{

    if(state_ == true)
    {
        // 0이 켜진거, 1이 안켜진거

        switch(led)
        {   // or을 취하면 0 | 1 = 1, 나머지는 1 | 0 = 1 꺼진다.
            case 0 :
                testLevel = (uint16_t)(0x0001 << 8);
                R_PORT10->PODR |= testLevel; // 8th
                break;
            case 1 :
                testLevel = (uint16_t)(0x0001 << 9);
                R_PORT10->PODR |= testLevel; // 9th
                break;
            case 2 :
                testLevel = (uint16_t)(0x0001 << 10);
                R_PORT10->PODR |= testLevel; // 10th
                break;
            case 3 :
                testLevel = (uint16_t)(0x0001 << 0);
                R_PORT11->PODR |= testLevel; // 11th
                break;
        }
    }
    else // state = 1
    {
        switch(led)
        {   // and를 취하면 1&0 = 0, 나머지는 1&1 = 1 켜진다
            case 0 :
                testLevel = (uint16_t)~(0x0001 << 8);
                R_PORT10->PODR &= testLevel;
                break;
            case 1 :
                testLevel = (uint16_t)~(0x0001 << 9);
                R_PORT10->PODR &= testLevel;
                break;
            case 2 :
                testLevel = (uint16_t)~(0x0001 << 10);
                R_PORT10->PODR &= testLevel;
                break;
            case 3 :
                testLevel = (uint16_t)~(0x0001 << 0);
                R_PORT11->PODR &= testLevel;
                break;
        }
    }

}

uint16_t R_USP_GetLed4(uint8_t led)
{
    uint16_t port;
    uint16_t result = 0;

    switch(led)
    {
        case 0 :
            port = R_PORT10->PIDR;
            result = ((port >> 8) & 0x0001);
            break;
        case 1 :
            port = R_PORT10->PIDR;
            result = ((port >> 9)& 0x0001);
            break;
        case 2 :
            port = R_PORT10->PIDR;
            result = ((port >> 10)& 0x0001);
            break;
        case 3 :
            port = R_PORT11->PIDR;
            result = ((port >> 0)& 0x0001);
            break;
    }

    return result;
}

/*******************************************************************************************************************//**
 * This function is called at various points during the startup process.  This implementation uses the event that is
 * called right before main() to set up the pins.
 *
 * @param[in]  event    Where at in the start up process the code is currently at
 **********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
#endif

 

저번에 공부해본 내용을 복습하면서 실습할 수 있었다.