Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12596 Discussions

Read problem by using MicroC/OS-II

Altera_Forum
Honored Contributor II
1,297 Views

Hi, 

I'm a new in using mutitasking tasks. I have the following code: 

int startTheSystem=0; int systemWasBooted=0; int controlTheSystem=0; char befehl; long int zahl=0; void system_Monitoring(alt_u32 pe_address,alt_u32 mme_address, alt_u32 spdm_address){...} void zahl_Eingabe(){ if(startTheSystem==0){ printf("Das System wird gestartet.\n"); printf("Bitte geben Sie die Frequenz für das FAD ein.\n"); scanf("%ld", &zahl); frequencyFromFAD=zahl; printf("Bitte geben Sie die Amplitude für das FAD ein.\n"); scanf("%ld", &zahl); amplitudeFromFAD=zahl; printf("Bitte geben Sie den Schwellwert für das PSD ein.\n"); scanf("%ld",&zahl); thresholdForThePSD=zahl; startTheSystem=1; } if(controlTheSystem==0){ scanf("%s %ld",befehl,&zahl); //write the frequency to FAD if(strcmp(befehl,"w1")==0){ send_16bit_Data_To_SPI_Slave(SPI_MASTER_BASE,0x1,zahl); frequencyFromFAD=zahl; printf("Neue Fequenz: %ld\n",zahl); } //write the amplitude to the FAD if(strcmp(befehl,"w2")==0){ send_16bit_Data_To_SPI_Slave(SPI_MASTER_BASE,0x1,zahl); printf("Neue Amplitude: %ld\n", zahl); amplitudeFromFAD=zahl; } //write the threshold to the PSD if(strcmp(befehl,"w3")==0){ send_16bit_Data_To_SPI_Slave(SPI_MASTER_BASE,0x2,zahl); //take care about the slave_select_address printf("Neuer Schwellwert: %ld\n", zahl); thresholdForThePSD=zahl; } //read the frequency from the FAD which is generated now if(strcmp(befehl,"r1")==0){ double countedFreq=0; countedFreq =count_The_Frequency_Of_The_FAD(); printf("Die Frequenz vom FAD ist momentan auf: %e Hz\n", countedFreq); countedFreq=0; } //read the amplitude from the FAD which was give in before if(strcmp(befehl,"r2")==0){ printf("Die Amplitude vom FAD beträgt momentan: %ld °\n", amplitudeFromFAD); } //read the threshold from the PSD which was give in before if(strcmp(befehl,"r3")==0){ printf("Der Schwellwert vom PSD beträgt momentan: %ld\n", thresholdForThePSD); } zahl=0; befehl=0; controlTheSystem=1; } } /* Definition of Task Stacks */# define TASK_STACKSIZE 2048 OS_STK task1_stk; OS_STK task2_stk; /* Definition of Task Priorities */ # define TASK1_PRIORITY 1# define TASK2_PRIORITY 2 void task1(void* pdata) { while (1) { if((startTheSystem==1) & (systemWasBooted==0)){ printf("Das System wird nun gebootet!\n"); systemWasBooted=1; } if(((controlTheSystem==0) & (systemWasBooted==1)) | ((controlTheSystem==1) & (systemWasBooted==1))){ controlTheSystem=0; printf("Controlflag: %d\n", controlTheSystem); system_Monitoring(PE_PIO_BASE,MME_PIO_BASE,SPDM_PIO_BASE); } // printf("Task 1 nochmal.\n"); // if(startTheSystem==1){ // printf("Die Zahl war: %ld\n",zahl); // zahl=0; // startTheSystem=0; // } OSTimeDlyHMSM(0, 0, 2, 0); } } void task2(void* pdata) { while (1) { zahl_Eingabe(); // printf("Zahl eingeben!\n"); // scanf("%ld", &zahl); // if(zahl!=0){ // printf("Frequenz:%ld\n", zahl); // startTheSystem=1; // } OSTimeDlyHMSM(0, 0, 2, 0); } } /* The main function creates two task and starts multi-tasking */ int main(void) { OSTaskCreateExt(task1, NULL, (void *)&task1_stk, TASK1_PRIORITY, TASK1_PRIORITY, task1_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task2, NULL, (void *)&task2_stk, TASK2_PRIORITY, TASK2_PRIORITY, task2_stk, TASK_STACKSIZE, NULL, 0); OSStart(); return 0; }  

 

My Probelm is now: I need type in the instruction to read and then I need to typ in something else to read out the value I want to have by using the second task? 

You can see it in the console: 

 

 

--- Quote Start ---  

 

Controlflag: 0 

Systemüberwachung gestartet! 

 

r2 

Controlflag: 0 

Systemüberwachung gestartet! 

 

 

Die Amplitude vom FAD beträgt momentan: 10 ° 

 

--- Quote End ---  

 

 

Why it doesn't work by the first time because if I use the write instruction it works well. 

 

Thanks for the help in advance!
0 Kudos
8 Replies
Altera_Forum
Honored Contributor II
459 Views

Try inserting a TK_SLEEP(10) between printf and next scanf. 

This will force the task to momentarily release the control, allowing the OS to schedule other lower priority tasks in execution state, in particular the one which sends data to the console through jtag uart.
0 Kudos
Altera_Forum
Honored Contributor II
459 Views

TK_SLEEP() is not declared. In which header is it? 

I also recognized an other probelm. I can read the valu with the instruction "r2" and "r3" but if I read the value "r1" over the PIO my system hang and nothing more happens. What made I'm wrong that this happens if I try to measure the frequency over the "r1" instruction? 

 

Thanks in advance for the help!
0 Kudos
Altera_Forum
Honored Contributor II
459 Views

 

--- Quote Start ---  

TK_SLEEP() is not declared. In which header is it? 

 

--- Quote End ---  

 

Can't remember  

Try # include <ucos_ii.h> 

Or maybe # include "osport.h" 

 

 

--- Quote Start ---  

I also recognized an other probelm. I can read the valu with the instruction "r2" and "r3" but if I read the value "r1" over the PIO my system hang and nothing more happens. What made I'm wrong that this happens if I try to measure the frequency over the "r1" instruction? 

 

--- Quote End ---  

 

 

You definitely have a problem in your function count_The_Frequency_Of_The_FAD(). 

I can't see the function body, but I guess it contains an infinite loop.
0 Kudos
Altera_Forum
Honored Contributor II
459 Views

Hi cris, 

 

here is my count_The_Frequency_Of_The_FAD() method: 

 

double count_The_Frequency_Of_The_FAD() 

double count_timeH_before=0; 

double count_timeL_before=0; 

double total_time_steps=0; 

double calculated_time=0; 

double calculated_frequency=0; 

alt_u32 count_step_1=0x0; 

alt_u32 count_step_2=0x0; 

 

int a=0; 

 

//start address of the counter 

IOWR_ALTERA_AVALON_TIMER_PERIODH(SYS_CLK_TIMER_BASE,0xFFFF); 

IOWR_ALTERA_AVALON_TIMER_PERIODL(SYS_CLK_TIMER_BASE,0xFFFF); 

 

IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,ALTERA_AVALON_TIMER_CONTROL_STOP_MSK); 

 

//total_time is in seconds but is measured in ms 

//the range depends on the frequency you want to have 

//while((total_time<=10) & (total_time>=5)){ //while((total_time<=0.001) & (total_time>=0.00065)){ 

 

//by writing to the snapregister you get the data from the counter 

IOWR_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE,0x1); 

IOWR_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE,0x2); 

 

//start the counter 

//IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x06); 

//printf("Control : %x\n",IORD_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE)); 

//printf("Status: %x\n", IORD_ALTERA_AVALON_TIMER_STATUS(SYS_CLK_TIMER_BASE)); 

count_timeL_before= IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE); 

//printf("SnapL : %e\n", count_timeL_before); 

count_timeH_before= IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE); 

//printf("SnapH : %e\n", count_timeH_before); 

 

IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x06); 

//wait till the FAD gives back the right frequency 

int j; 

for ( j = 0; j < 2; ++j) { 

push_The_Button(FAD_CLOCK_BASE); 

a=a+1; 

IOWR_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE,0x1); 

if(a==1){ 

count_step_1 = IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)<<16; 

count_step_1 =count_step_1|(IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE)); 

if(a==2){ 

count_step_2 = IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)<<16; 

count_step_2 =count_step_2|(IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE)); 

 

//printf("Flanke Nummer %d \n", j); 

 

 

//printf("SnapH : %e\n", (double)IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)); 

//stop the counter 

IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x8); 

 

//printf("1; %x\n", count_step_1); 

// printf("2: %x \n", count_step_2); 

 

total_time_steps= (count_step_1-count_step_2); 

 

calculated_time= total_time_steps/(50000000); 

 

calculated_frequency=1/calculated_time; 

 

// printf("Frequenz: %e\n", calculated_frequency); 

 

return calculated_frequency; 

 

And it works if I call it in the main function without any crashes.
0 Kudos
Altera_Forum
Honored Contributor II
459 Views

And what I forget the both headers what you've told me have not the TK_SLEEP() methode.

0 Kudos
Altera_Forum
Honored Contributor II
459 Views

You CAN NOT stop or change sys_clk_timer !!!! 

This is reserved to OS for task scheduling and all other OS services. 

If you need it, add another interval timer to the system. 

 

For TK_SLEEP, try: # include "tk_crnos.h" 

You can possibly use OSTimeDly (), which is the same as TK_SLEEP
0 Kudos
Altera_Forum
Honored Contributor II
459 Views

You are right with teh sys_clk_timer! I changed it and now it doesn't crash any more! Thanks! 

 

I can't find the # include "tk_crnos.h". 

 

I aslo tried something else in my main() function. My code here: 

 

 

--- Quote Start ---  

OSInit(); 

OSTaskCreateExt(task1, 

NULL, 

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

TASK1_PRIORITY, 

TASK1_PRIORITY, 

task1_stk, 

TASK_STACKSIZE, 

NULL, 

0); 

 

 

OSTaskCreateExt(task2, 

NULL, 

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

TASK2_PRIORITY, 

TASK2_PRIORITY, 

task2_stk, 

TASK_STACKSIZE, 

NULL, 

0); 

 

OSStart(); 

--- Quote End ---  

 

 

And with the OSinit() I just typ in the read instruction just one time but the problem is that after that the system will read every time if the task is started. Here is my console outpu: 

 

 

--- Quote Start ---  

 

 

r1 

Neue Fequenz: 0 

Systemüberwachung gestartet! 

Der Chris hat keine Ahnung und der Sämann erst recht! 

Die Frequenz vom FAD ist momentan auf: 1.306336e+03 Hz 

Systemüberwachung gestartet! 

Der Chris hat keine Ahnung und der Sämann erst recht! 

Die Frequenz vom FAD ist momentan auf: 1.307019e+03 Hz 

Systemüberwachung gestartet! 

Der Chris hat keine Ahnung und der Sämann erst recht! 

Die Frequenz vom FAD ist momentan auf: 1.307702e+03 Hz 

 

--- Quote End ---  

 

 

Why this happens?  

 

Thanks in advance for the help!
0 Kudos
Altera_Forum
Honored Contributor II
459 Views

If you want people to read code then: 

1) Use the# CODE tags so that the indention is kept. 

2) Keep the fragments reasonably small.
0 Kudos
Reply