본문 바로가기
Embedded SW/[Infineon] TC275 English Version

09. Before development, let’s organize the SW code (TC275, Infineon)

by 방구석 임베디드 2023. 9. 3.
반응형

Hello.
Today, we are going to proceed with development from now on, but before that, it would be good to first organize the software (SW) code.

 

This article is one of the articles that serialize the overall contents of development.

So, if you look at the previous article, I think it will be more helpful.

I will link the article below. Read it if you need to.

(Of course, there is no big problem if you just read this article.)

 

00. Embedded SW development using TC275 (Prologue)
01. What is embedded SW and what is MCU (Micro Control Unit)?
02. How to purchase an embedded development board (TC275, Infineon)
03. How to set up an embedded SW development environment (what is a source code editor, compiler, and debugger environment)
04. How to create an MCU TC275 project (Infineon)

05. Digital output settings and LED blinking using TC275 (push-pull, open drain)

06. Understanding MCU Clock, Oscillator (Infineon TC275)

07. What is PLL in MCU? PLL and Clock settings on Infineon TC275

08. Let's design a timer using Infineon TC275 MCU


What is code organization? Is it just cleaning up the code?

When developing, the most important part is the software architecture. First, we draw the software architecture before coding. Once the architecture is complete, we organize the features into a folder structure. We'll cover this part later.

What I want to say is, 'How do you judge the maturity of a software developer?' There may be many factors, but it is said that you can gauge the maturity of a software developer by looking at their header files.

Header files?

In fact, the header files contain information on how we will store the necessary data in the C files and how we will divide the functions.

Therefore, organizing header files, dividing them well, and efficiently organizing them is extremely important.

In this article today, I will write about how to best organize header files and C files. Below is the main.c file from a project I wrote about last time. From now on, let's organize this file.

/**********************************************************************************************************************
 * \file Cpu0_Main.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 * 
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of 
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 * 
 * Boost Software License - Version 1.0 - August 17th, 2003
 * 
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and 
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 * 
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all 
 * derivative works of the Software, unless such copies or derivative works are solely in the form of 
 * machine-executable object code generated by a source language processor.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE.
 *********************************************************************************************************************/
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"

#include "IfxPort.h"
#include "IfxPort_PinMap.h"

#include "IfxStm.h"
#include "IfxCpu_Irq.h"

typedef struct
{
    Ifx_STM             *stmSfr;            /**< \brief Pointer to Stm register base */
    IfxStm_CompareConfig stmConfig;         /**< \brief Stm Configuration structure */
    volatile uint8       LedBlink;          /**< \brief LED state variable */
    volatile uint32      counter;           /**< \brief interrupt counter */
} App_Stm;

App_Stm g_Stm; /**< \brief Stm global data */

IfxCpu_syncEvent g_cpuSyncEvent = 0;

void IfxStmDemo_init(void);

int core0_main(void)
{
    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
    
    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

    IfxStmDemo_init();

    /*P00_5    Digital Output*/
    IfxPort_setPinModeOutput(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex, IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);
    IfxPort_setPinLow(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);

    while(1)
    {

    }
    return (1);
}

IFX_INTERRUPT(STM_Int0Handler, 0, 100);

void STM_Int0Handler(void)
{
    static int flag = 0;
    static int cnt = 0;
    
    IfxStm_clearCompareFlag(g_Stm.stmSfr, g_Stm.stmConfig.comparator);
    IfxStm_increaseCompare(g_Stm.stmSfr, g_Stm.stmConfig.comparator, 100000000u);

    cnt++;

    if(flag == 0)
    {
        IfxPort_setPinLow(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);
        flag = 1;
    }
    else
    {
        IfxPort_setPinHigh(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);
        flag = 0;
    }
    
    IfxCpu_enableInterrupts();
}

void IfxStmDemo_init(void)
{
    /* disable interrupts */
    boolean interruptState = IfxCpu_disableInterrupts();

    IfxStm_enableOcdsSuspend(&MODULE_STM0);

    g_Stm.stmSfr = &MODULE_STM0;
    IfxStm_initCompareConfig(&g_Stm.stmConfig);

    g_Stm.stmConfig.triggerPriority = 100u;
    g_Stm.stmConfig.typeOfService   = IfxSrc_Tos_cpu0;
    g_Stm.stmConfig.ticks           = 100000000;

    IfxStm_initCompare(g_Stm.stmSfr, &g_Stm.stmConfig);

    /* enable interrupts again */
    IfxCpu_restoreInterrupts(interruptState);
}

Let us first summarize this as follows.

/***********************************************************************/
/*Include*/ 
/***********************************************************************/
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort.h"
#include "IfxPort_PinMap.h"
#include "IfxStm.h"
#include "IfxCpu_Irq.h"

/***********************************************************************/
/*Typedef*/ 
/***********************************************************************/
typedef struct
{
    Ifx_STM             *stmSfr;            /**< \brief Pointer to Stm register base */
    IfxStm_CompareConfig stmConfig;         /**< \brief Stm Configuration structure */
    volatile uint8       LedBlink;          /**< \brief LED state variable */
    volatile uint32      counter;           /**< \brief interrupt counter */
} App_Stm;

/***********************************************************************/
/*Static Function Prototype*/ 
/***********************************************************************/
void IfxStmDemo_init(void);


/***********************************************************************/
/*Variable*/ 
/***********************************************************************/
App_Stm g_Stm; /**< \brief Stm global data */
IfxCpu_syncEvent g_cpuSyncEvent = 0;


/***********************************************************************/
/*Function*/ 
/***********************************************************************/
int core0_main(void)
{
    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
    
    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

    IfxStmDemo_init();

    /*P00_5    Digital Output*/
    IfxPort_setPinModeOutput(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex, IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);
    IfxPort_setPinLow(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);

    while(1)
    {

    }
    return (1);
}

IFX_INTERRUPT(STM_Int0Handler, 0, 100);

void STM_Int0Handler(void)
{
    static int flag = 0;
    static int cnt = 0;
    
    IfxStm_clearCompareFlag(g_Stm.stmSfr, g_Stm.stmConfig.comparator);
    IfxStm_increaseCompare(g_Stm.stmSfr, g_Stm.stmConfig.comparator, 100000000u);

    cnt++;

    if(flag == 0)
    {
        IfxPort_setPinLow(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);
        flag = 1;
    }
    else
    {
        IfxPort_setPinHigh(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);
        flag = 0;
    }
    
    IfxCpu_enableInterrupts();
}

void IfxStmDemo_init(void)
{
    /* disable interrupts */
    boolean interruptState = IfxCpu_disableInterrupts();

    IfxStm_enableOcdsSuspend(&MODULE_STM0);

    g_Stm.stmSfr = &MODULE_STM0;
    IfxStm_initCompareConfig(&g_Stm.stmConfig);

    g_Stm.stmConfig.triggerPriority = 100u;
    g_Stm.stmConfig.typeOfService   = IfxSrc_Tos_cpu0;
    g_Stm.stmConfig.ticks           = 100000000;

    IfxStm_initCompare(g_Stm.stmSfr, &g_Stm.stmConfig);

    /* enable interrupts again */
    IfxCpu_restoreInterrupts(interruptState);
}

The components we need to create a c file are largely as follows.
I think it would be good to just add more defines here.

 

/***********************************************************************/

/*Include*/

/***********************************************************************/

-> This is to import other functions, variables, and types used when implementing this c file through the header.

/***********************************************************************/

/*Typedef*/

/***********************************************************************/

-> Declare the typedef to use in the c file.

/***********************************************************************/

/*Static Function Prototype*/

/***********************************************************************/

-> Defines the declaration of a static function to be used only within cpine.

/***********************************************************************/

/*Variable*/

/***********************************************************************/

-> Define global variables to be used within this c file.

/***********************************************************************/

/*Function*/

/***********************************************************************/

-> Now let's define the function we want to create.

If you divide and organize roles into one c file like this, the code will be more effective.
However, there is a stm-related function in the main function, and I would like to share it with you.
What should I do?

Two files were added as above.
Then, let’s try writing this file.

First, I created the header file as shown below.
So that this header file can be referenced in the c file only once.
Use the ifndef function.

#ifndef DRIVER_STM
#define DRIVER_STM

/***********************************************************************/
/*Include*/ 
/***********************************************************************/

/***********************************************************************/
/*Typedef*/ 
/***********************************************************************/

/***********************************************************************/
/*Define*/ 
/***********************************************************************/

/***********************************************************************/
/*External Variable*/ 
/***********************************************************************/

/***********************************************************************/
/*Global Function Prototype*/ 
/***********************************************************************/


#endif /* DRIVER_STM */

And the c file was written as follows.

/***********************************************************************/
/*Include*/ 
/***********************************************************************/
#include "Drvier_Stm.h"

/***********************************************************************/
/*Define*/ 
/***********************************************************************/

/***********************************************************************/
/*Typedef*/ 
/***********************************************************************/

/***********************************************************************/
/*Static Function Prototype*/ 
/***********************************************************************/


/***********************************************************************/
/*Variable*/ 
/***********************************************************************/


/***********************************************************************/
/*Function*/ 
/***********************************************************************/

Have you made the frame now?
Let's move the function from the main function.

The transfer has been completed.
Compilation was completed successfully as shown below.

Then, I think we can divide the Port part as well.
I think it would be a good idea for each of you to try it.
First of all, please refer to the file below for the file worked on above.

TC275_Project1_Arrange.zip
4.11MB

If you want to organize things more nicely, it would be a good idea to do so.

For developers, it's great to have your own platform.

With this, I will conclude today’s article.

반응형

댓글