Code: Select all
#include <platform.h>
#define BIT_RATE 115200
#define BIT_TIME XS1_TIMER_HZ / BIT_RATE
#define TDELAY 1000000
#define TLENGTH 50
#define FLASH_PERIOD 1000000
#define CYCLE_PERIOD 6000000
in port buttons = PORT_BUTTON;
out port speaker = PORT_SPEAKER;
out port cled0 = PORT_CLOCKLED_0;
out port cled1 = PORT_CLOCKLED_1;
out port cled2 = PORT_CLOCKLED_2;
out port cledG = PORT_CLOCKLED_SELG;
out port cledR = PORT_CLOCKLED_SELR;
void buttonListener(in port b, out port speaker, chanend c);
void speakerListener(chanend c,out port speaker);
int cycleLED(out port cled0, out port cled1,out port cled2,
out port cledG,out port cledR, chanend c);
int main(void) {
chan c;
par {
cycleLED(cled0, cled1, cled2, cledG, cledR, c);
buttonListener(buttons, speaker, c);
}
return 0;
}
void buttonListener(in port b, out port spkr, chanend c) {
timer tmr;
int t, isOn = 1;
int buttonNumber;
while (1) {
b when pinsneq(0xf) :> buttonNumber;
c <: 0;
switch(buttonNumber){
case 0x7 :
tmr :> t;
for (int i=0; i<TLENGTH; i++) {
isOn = !isOn;
t += TDELAY;
tmr when timerafter(t) :> void;
spkr <: isOn;
}
break;
case 0xb :
tmr :> t;
for (int i=0; i<(TLENGTH*2); i++) {
isOn = !isOn;
t += TDELAY/2;
tmr when timerafter(t) :> void;
spkr <: isOn;
}
break;
case 0xd :
tmr :> t;
for (int i=0; i<(TLENGTH*3); i++) {
isOn = !isOn;
t += TDELAY/3;
tmr when timerafter(t) :> void;
spkr <: isOn;
}
break;
case 0xe :
tmr :> t;
for (int i=0; i<(TLENGTH*4); i++) {
isOn = !isOn;
t += TDELAY/4;
tmr when timerafter(t) :> void;
spkr <: isOn;
}
break;
}
}
}
int cycleLED(out port cled0, out port cled1,out port cled2,
out port cledG,out port cledR, chanend c) {
unsigned ledOn = 1;
unsigned ledVal = 1;
unsigned isGreen = 1;
timer tmrF, tmrC;
unsigned timeF, timeC;
tmrF :> timeF;
tmrC :> timeC;
while (1) {
select {
case tmrF when timerafter(timeF) :> void :
ledOn = !ledOn;
cledR <: ledOn && !isGreen;
cledG <: ledOn && isGreen;
timeF += FLASH_PERIOD;
break;
case tmrC when timerafter(timeC) :> void :
cled0 <: ledVal;
cled1 <: (ledVal >> 4);
cled2 <: (ledVal >> 8);
ledVal <<= 1;
if (ledVal == 0x1000)
ledVal = 1;
timeC += CYCLE_PERIOD;
break;
case c :> int :
isGreen = !isGreen;
break;
}
}
return 0;
}
Code: Select all
--- main_nocore.xc 2010-10-16 23:46:34.000000000 +0100
+++ main_core.xc 2010-10-16 23:40:12.000000000 +0100
@@ -8,14 +8,14 @@
#define FLASH_PERIOD 1000000
#define CYCLE_PERIOD 6000000
-in port buttons = PORT_BUTTON;
-out port speaker = PORT_SPEAKER;
+on stdcore[0] : in port buttons = PORT_BUTTON;
+on stdcore[0] : out port speaker = PORT_SPEAKER;
-out port cled0 = PORT_CLOCKLED_0;
-out port cled1 = PORT_CLOCKLED_1;
-out port cled2 = PORT_CLOCKLED_2;
-out port cledG = PORT_CLOCKLED_SELG;
-out port cledR = PORT_CLOCKLED_SELR;
+on stdcore[0] : out port cled0 = PORT_CLOCKLED_0;
+on stdcore[0] : out port cled1 = PORT_CLOCKLED_1;
+on stdcore[0] : out port cled2 = PORT_CLOCKLED_2;
+on stdcore[0] : out port cledG = PORT_CLOCKLED_SELG;
+on stdcore[0] : out port cledR = PORT_CLOCKLED_SELR;
void buttonListener(in port b, out port speaker, chanend c);
void speakerListener(chanend c,out port speaker);
@@ -25,8 +25,8 @@
int main(void) {
chan c;
par {
- cycleLED(cled0, cled1, cled2, cledG, cledR, c);
- buttonListener(buttons, speaker, c);
+ on stdcore[0] : cycleLED(cled0, cled1, cled2, cledG, cledR, c);
+ on stdcore[0] : buttonListener(buttons, speaker, c);
}
return 0;
}