Funpack2-2 TC275 任务一 多核点灯
使用TC275的三个核心,轮流休眠待机,分别控制板卡上的LED灯,如core0检测按键按下,唤醒core1翻转LED1,一秒后,唤醒core2翻转LED2,系统休眠
标签
嵌入式系统
DayDay
更新2022-10-10
509

简介

该项目是Funpack第二季第二期任务一:使用TC275的三个核心,轮流休眠待机,分别控制板卡上的LED灯,如core0检测按键按下,唤醒core1翻转LED1,一秒后,唤醒core2翻转LED2,系统休眠。

 

主要功能代码及说明

三个CPU的代码分别如下:

CPU0

#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort.h"
#include "Bsp.h"
IfxCpu_syncEvent g_cpuSyncEvent = 0;
IfxCpu_mutexLock core0;
IfxCpu_mutexLock core1;
IfxCpu_mutexLock core2;
#define BTN &MODULE_P00,7
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());
    IfxCpu_acquireMutex(&core2);
    IfxCpu_acquireMutex(&core1);
    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);
    IfxPort_setPinModeInput(BTN,IfxPort_InputMode_noPullDevice);

    while(1)
    {
        if(IfxCpu_acquireMutex(&core0))
        {
            while(IfxPort_getPinState(BTN)==1);
            IfxCpu_releaseMutex(&core1);
        }
    }
    return (1);
}

CPU1

#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort.h"
#include "Bsp.h"
extern IfxCpu_syncEvent g_cpuSyncEvent;
extern IfxCpu_mutexLock core0;
extern IfxCpu_mutexLock core1;
extern IfxCpu_mutexLock core2;

#define LED1 &MODULE_P00,5
int core1_main(void)
{
    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG1 IS DISABLED HERE!!
     * Enable the watchdog and service it periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());

    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);
    IfxPort_setPinModeOutput(LED1,IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);

    while(1)
    {
        if(IfxCpu_acquireMutex(&core1))
        {
            IfxPort_togglePin(LED1);
            waitTime(IfxStm_getTicksFromMilliseconds(BSP_DEFAULT_TIMER, 1000));
            IfxCpu_releaseMutex(&core2);
        }
    }
    return (1);
}

CPU2

#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort.h"
#include "Bsp.h"
extern IfxCpu_syncEvent g_cpuSyncEvent;
extern IfxCpu_mutexLock core0;
extern IfxCpu_mutexLock core1;
extern IfxCpu_mutexLock core2;
#define LED2 &MODULE_P00,6
int core2_main(void)
{
    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG2 IS DISABLED HERE!!
     * Enable the watchdog and service it periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());

    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);
    IfxPort_setPinModeOutput(LED2,IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);

    while(1)
    {
        if(IfxCpu_acquireMutex(&core2))
        {
            IfxPort_togglePin(LED2);
            IfxCpu_releaseMutex(&core0);
        }
    }
    return (1);
}

该任务的逻辑并不复杂,就是CPU0监测到按键GPIO的下降沿后,通知CPU1翻转LED1 GPIO电平,1s后,CPU1再通知CPU2翻转LED2 GPIO电平,1s后,CPU2再通知CPU0开始继续检测按键GPIO下降沿,如此往复。

但在TC275的iLLD驱动库中,没有提供直接用于CPU核间通知的API。核间同步API只有Event,Mutex和Spinlock。由于Event仅用于所有内核的同步,无法灵活实现0->1,1->2,2->0,所以选用Mutex来模拟这种通知。

 

功能演示

首先CPU0在初始化时,持有1、2号锁,进入大循环,尝试获取0号锁,首次运行,可以正确获取,等待按键GPIO电平满足要求,释放1号锁。CPU1在大循环中,尝试获取1号锁,获取到后,翻转LED1电平,等待1s,释放2号锁。CPU2在大循环中,尝试获取2号锁,获取到后,翻转LED2电平,等待1s,释放0号锁。这样就可以实现CPU0-1-2的轮流工作。

视频中可见,按键按下,LED1亮灭翻转,1s后,LED2亮灭翻转,再按按键,前面过程重复。

 

心得体会

参加本次活动,是想要了解一下多核MCU核间通讯的相关机制。通过完成该任务,也达成了最初的目的。但TC275的库在核间通讯上面,并不很完整,这个功能实现起来,还是比较绕的。

最后希望Funpack继续办好!

附件下载
test.7z
团队介绍
个人
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号