Channels as Interrupts

New to XMOS and XCore? Get started here.
Post Reply
dimitris
Active Member
Posts: 37
Joined: Tue Feb 19, 2013 5:07 pm

Channels as Interrupts

Post by dimitris »

Hi there,

I am trying to come up with a concept of a top application, controlling the execution of other threads. These "other threads" are while loops with different timing constraints, I was wondering if channels can be used to switch those threads on and off but without blocking eachother. In fact I want to use reading/writing to channels as interrupts to break while loops.

Initially i was thinking something like the pseudocode below but this will block processes while trying to read/write on the channels. Streaming channels won't help either since their size is much smaller than the one needed considering the difference in the timing requirements (eg: control takes 1s while loop1 completes loop every 1 ms.

I really hope I have made myself clear. Thank you in advance.
Regards,
Dimitris

Code: Select all

void control(){
    while(1){
        do{...}
        ch1 <: 1;
        ch2 <: 1;
        do{...}    // 1 s
    }
}

void loop1(channend ch1){
    while(1){
        ch1:>k;
        do{}        // 1 ms
    }
}

void loop2(channend ch2){
    while(1){
        ch2:>k;
        do{}        // 10 ms
    }
}


User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post by TSC »

Hi Dimitris

Correct me if I misunderstand what you want to do. I think select/case statements will help you. Example for a worker thread given below.

Code: Select all

void loop1(chanend ch1) {
	
	int controlInstruct = 1;
	
	while (1) {
		select {
		case ch1 :> controlInstruct:
			break;
		
		default:
			if (controlInstruct == 1){
				do{}	// return after 10 ms
			}
			else{
				// do nothing
			}
			break;
		}
	}
}
dimitris
Active Member
Posts: 37
Joined: Tue Feb 19, 2013 5:07 pm

Post by dimitris »

Thank you TSC,
that was exactly my workaround too after all.

I was expecting that the select statement could interrupt each case but it turns out that the code inside a case is protected and won't be interrupted until the case is completed. So, a slow while loop inside a case will actually break the functionality.

My case is that I have different threads running, for example a PWM and a slow sensor reading process. Once the PWM is started, i need to interrupt it to make it stop. (I was also searching whether things such as PWMs can be implemented directly in hardware (by clock configuring functions) but I think these can only output clock signals.)
So the question is, if you need a fast PWM signal, do you really need a whole processor thread to run it? Isn't that some kind of an overkill or am I - once more... - missing something?
Post Reply