Hi I have been working on a game for my FPGA. I am using message queues here and my problem is that when I want to print the values from an array I always get the same result even when I put different values in them. May be I am printing them out wrong or maybe they get reset when the task is running.
#define MSG_QUEUE_SIZE 4
OS_EVENT *msgqueue;
void *msgqueue_tbl[MSG_QUEUE_SIZE];
int say_array[MSG_QUEUE_SIZE];
int idx,x;
// Interrupt Service Routine for KEY0-KEY3 IRQ
void isr_pio_key(void *context, alt_u32 id)
{
INT32U msg = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_KEY_BASE);
// Post message in queue
OSQPost(msgqueue, (void *)msg);
// Reset EDGE CAP
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_KEY_BASE, 0x01);
}
void key_pressed_task(void *pdata)
{
INT8U error_code = OS_NO_ERR;
INT32U msg;
OS_Q_DATA queue_data;
INT16U num_msgs;
int say_array[MSG_QUEUE_SIZE];
for(;;) {
srand(time(NULL));
int led = rand()%4;
if(led == 3){
IOWR_32DIRECT(PIO_LEDG_BASE,0,0x08);
say_array[3] = led;
idx++;
}
if(led == 2){
IOWR_32DIRECT(PIO_LEDG_BASE,0,0x04);
say_array[2] = led;
idx++;
}
if(led == 1){
IOWR_32DIRECT(PIO_LEDG_BASE,0,0x02);
say_array[1] = led;
idx++;
}
if(led == 0){
IOWR_32DIRECT(PIO_LEDG_BASE,0,0x01);
say_array[0] = led;
idx++;
}
printf("%d",led);
if(idx==4)
{
printf("\nSimon said: ");
for(x=0;x<4;x++)
printf("%d",say_array[x]);
printf("\n");
idx=0;
for(;;)
{
OSQQuery(msgqueue, &queue_data);
num_msgs = queue_data.OSNMsgs;
if(num_msgs > 0)
{
msg = (INT32U)OSQPend(msgqueue, 4, &error_code);
printf("msg: %d\n", msg);
if((!error_code) && (msg == 1))
printf("(KEY_PRESSED_TASK) KEY0 Pressed\n");break;
if((!error_code) && (msg == 2))
printf("(KEY_PRESSED_TASK) KEY1 Pressed\n");break;
if((!error_code) && (msg == 4))
printf("(KEY_PRESSED_TASK) KEY2 Pressed\n");break;
if((!error_code) && (msg == 8))
printf("(KEY_PRESSED_TASK) KEY3 Pressed\n");break;
msg = 0;
}
}
}
OSTimeDlyHMSM(0,0,1,0);
}
}
int main()
{
// Create message queue
msgqueue = OSQCreate(&msgqueue_tbl[0], MSG_QUEUE_SIZE);
// Set up interrupts (mask KEY0)
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_KEY_BASE, 0x0F);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_KEY_BASE, 0x01);
alt_ic_isr_register(PIO_KEY_IRQ_INTERRUPT_CONTROLLER_ID, PIO_KEY_IRQ, (void *)isr_pio_key, NULL, NULL);
// Create task
OSTaskCreate(key_pressed_task, NULL, &KEY_PRESSED_TASK_STACK[STACKSIZE-1], KEY_PRESSED_TASK_PRIO);
// Start uC/OS-II
OSStart();
// Shouldn't reach this point
return -1;
}
The part that I am wondering about is this:
printf("\nSimon said: ");
for(x=0;x<4;x++)
printf("%d",say_array[x]);
printf("\n");
The code above should print the values that are set where the LEDs are turned on. But it always prints 0123
Any ideas?
In your for(;;)
loop you always set say_array[led] = led;
. I.e. when led==3
you set say_array[3] = 3
. Try making the array an array of boolean values and toggling the element, e.g. say_array[led] = !say_array[led];
Also remove one of the say_array definitions, as you have one at file scope and a second at function scope.