Forum Discussion

RyuKucchan's avatar
RyuKucchan
Icon for New Contributor rankNew Contributor
6 years ago

Why doesn't my task work when a timer is added. (with MicroC/OS-II)

As shown in the following source code, if a timer is added, the timer interrupt operation will be performed periodically, but Task1 will not work.

If I do not execute init_timer (), Task1 is working reliably.

(*) It is necessary to turn off and on for some reason.

Please tell me the cause and the solution.

// ***** main.c *****

#include <stdio.h>

#include "includes.h"

#include "altera_avalon_timer_regs.h"

#include "sys/alt_irq.h"

#define TASK_STACKSIZE 2048

OS_STK Task1_stk[TASK_STACKSIZE];

#define TASK1_PRIORITY 1

volatile int m_context_Timer;

int Count_Task1 = 0;

int Count_Timer = 0;

void Task1(void* pdata)

{

while (1)

{

Count_Task1++;

printf("Hello from Task1 = %d\n",Count_Task1);

OSTimeDlyHMSM(0, 0, 2, 0);

}

}

static void timer1sec_interrupts(void* context)

{

IOWR_ALTERA_AVALON_TIMER_STATUS(USER_TIMER_BASE,0);

Count_Timer++;

printf("Hello from Timer = %d\n",Count_Timer);

}

void init_timer()

{

alt_u32 count;

void* timer_ptr=(void*)&m_context_Timer;

alt_ic_isr_register(USER_TIMER_IRQ_INTERRUPT_CONTROLLER_ID,USER_TIMER_IRQ,timer1sec_interrupts,timer_ptr,0x0);

count = 50000000 - 1; // 1sec

IOWR_ALTERA_AVALON_TIMER_PERIODL(USER_TIMER_BASE, count & 0xffff);

IOWR_ALTERA_AVALON_TIMER_PERIODH(USER_TIMER_BASE, (count >> 16) & 0xffff);

IOWR_ALTERA_AVALON_TIMER_STATUS(USER_TIMER_BASE,0);

IOWR_ALTERA_AVALON_TIMER_CONTROL(USER_TIMER_BASE,7);

}

int main(void)

{

printf("MicroC/OS-II Licensing Terms\n");

printf("============================\n");

#if(1)

init_timer();

#endif

OSTaskCreateExt(Task1,

NULL,

(void *)&Task1_stk[TASK_STACKSIZE-1],

TASK1_PRIORITY,

TASK1_PRIORITY,

Task1_stk,

TASK_STACKSIZE,

NULL,

0);

OSStart();

return 0;

}

// ***** system.h *****

#define ALT_MODULE_CLASS_user_timer altera_avalon_timer

#define USER_TIMER_ALWAYS_RUN 0

#define USER_TIMER_BASE 0x2000

#define USER_TIMER_COUNTER_SIZE 32

#define USER_TIMER_FIXED_PERIOD 0

#define USER_TIMER_FREQ 50000000

#define USER_TIMER_IRQ 15

#define USER_TIMER_IRQ_INTERRUPT_CONTROLLER_ID 0

#define USER_TIMER_LOAD_VALUE 49999

#define USER_TIMER_MULT 0.001

#define USER_TIMER_NAME "/dev/user_timer"

#define USER_TIMER_PERIOD 1

#define USER_TIMER_PERIOD_UNITS "ms"

#define USER_TIMER_RESET_OUTPUT 0

#define USER_TIMER_SNAPSHOT 1

#define USER_TIMER_SPAN 32

#define USER_TIMER_TICKS_PER_SEC 1000

#define USER_TIMER_TIMEOUT_PULSE_OUTPUT 0

#define USER_TIMER_TYPE "altera_avalon_timer"

// ***** Execution result (Use init_timer) *****

MicroC/OS-II Licensing Terms

============================

Hello from Task1 = 1

Hello from Timer = 1

Hello from Timer = 2

Hello from Timer = 3

Hello from Timer = 4

Hello from Timer = 5

Hello from Timer = 6

// ***** Execution result (Not use init_timer) *****

MicroC/OS-II Licensing Terms

============================

Hello from Task1 = 1

Hello from Task1 = 2

Hello from Task1 = 3

Hello from Task1 = 4

Hello from Task1 = 5

Hello from Task1 = 6

2 Replies

  • RyuKucchan's avatar
    RyuKucchan
    Icon for New Contributor rankNew Contributor

    Thanks for your comment.

    Yes,I using the the intel's timer IP.

    The information of system.h about USER_TIMER was described at the previous question.

    Please let me know any information.