用Microblaze设置定时器?

时间:2017-02-16 07:50:39

标签: c timer fpga xilinx microblaze

使用Microblaze创建计时器的最佳方法是什么,这样我可以让它在更传统的脚本中更像delay_ms()sleep()这样的函数?

很容易,我可以像这样创建一个愚蠢的函数:

void delay_ms(int i) {
    //mind that I am doing this on the top of my head
    for(delays=0; delay<(i*((1/frequency of the device)/2)); delays++) {
    }
}

...但是只有处理器处理才能完成,直到它完成,而实际上我需要它让函数允许我在一段时间内停止一个进程,而另一个进程继续工作。

这样的事情是可能的,毫无疑问,但这个问题最简单的解决方案是什么?

(我使用的是Spartan-3A,但我相信该解决方案适用于不同的套件,FPGA。)

1 个答案:

答案 0 :(得分:2)

<强> TL; DR

使用微型操作系统,例如FreeRTOS

答案错误

好吧,如果你没有操作系统,没有任务换向,但有一个外部计时器,你可以 使用以下方法:

启用硬件计时器中断,并管理由此中断驱动的计数器:

你应该有像

这样的东西
/**timer.c**/

/* The internal counters
 * each task have its counter
 */
static int s_timers[NUMBER_OF_TASKS] = {0,0};

/* on each time tick, decrease timers */
void timer_interrupt()
{
    int i;
    for (i = 0; i < NUMBER_OF_TASKS; ++i)
    {
        if (s_timer[i] > 0)
        {
            s_timer[i]--;
        }
    }
}

/* set wait counter:
 * each task says how tick it want to wait
 */
void timer_set_wait(int task_num, int tick_to_wait)
{
    s_timer[task_num] = tick_to_wait;
}

/**
 * each task can ask if its time went out
 */
int timer_timeout(int task_num)
{
    return (0 == s_timer[task_num]);
}

一旦你有像计时器这样的东西(上面的代码很容易完成), 计划你的任务:

/**task-1.c**/

/*TASK ID must be valid and unique in s_timer */
#define TASK_1_ID 0

void task_1()
{
    if (timer_timeout(TASK_1_ID))
    {
        /* task has wait long enough, it can run again */

        /* DO TASK 1 STUFF */
        printf("hello from task 1\n");

        /* Ask to wait for 150 ticks */
        timer_set_wait(TASK_1_ID, 150);
    }
}

/**task-2.c**/

/*TASK ID must be valid and unique in s_timer */
#define TASK_2_ID 1

void task_2()
{
    if (timer_timeout(TASK_2_ID))
    {
        /* task has wait long enough, it can run again */

        /* DO TASK 2 STUFF */
        printf("hello from task 2\n");

        /* Ask to wait for 250 ticks */
        timer_set_wait(TASK_2_ID, 250);
    }
}

安排(这里一个大词)任务:

/** main.c **/

int main()
{
    /* init the program, like set up the timer interruption */
    init() 

    /* do tasks, for ever*/
    while(1)
    {
        task_1();
        task_2();
    }
    return 0;
}

我认为我所描述的是一种不应该被认真使用的蹩脚解决方案。

我给出的代码充满了问题,比如如果任务变得缓慢执行会发生什么......

相反,你 - 可以 - 应该使用一些RT Os,比如FreeRTOS,这对这类问题非常有帮助。

相关问题