1

用 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。)

4

1 回答 1

2

TL;博士

使用微型操作系统,例如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 操作系统,比如FreeRTOS,它对这类问题非常有帮助。

于 2017-02-16T13:40:50.487 回答