Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
21608 Discussions

A little help with C in NIOS II

Altera_Forum
Honored Contributor II
1,187 Views

I'm hoping someone can give me a bit of help with my C code! 

 

basically i have the audio in and out routed into NIOS. It works fine but i want an if statement around it so the audio out equals audio in only when a switch on the DE2 is on. 

I'm using the DE2 board btw.  

 

Ideally i want to have different switches for different routing (different effects etc) but i think i'm struggling with the C syntax. So if i had an 8 bit wide PIO- 8 switches, is it possible so code something like... IF bit 0 is high then this... if bit 1 is high then this, etc etc ? 

 

Am i thinking about this the wrong way. Maybe i've been looking at VHDl for too long 

 

# define aud_in_ch1 (volatile int*) 0x01101020 # define aud_out_ch1 (int*) 0x01101030 # define aud_in_ch2 (volatile int*) 0x01101040 # define aud_out_ch2 (int*) 0x01101050 # define switches (volatile char*) 0x01101060 int main() { if (*switches == 1) { lcd(); printf("Hello from Nios II!\n"); while(1) { *aud_out_ch1 = *aud_in_ch1; *aud_out_ch2 = *aud_in_ch2; } } //return 0; }
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
500 Views

Try something like this... for the sake of clarity, I broke the code into switch functions. With this, when you press a switch, the function associated with it will run on every cycle until a different switch is pressed. For the sake of illustration, if you press switch 0, audio from channel 1 will go to channel 1. If you press switch 1, channel 2 will be routed. If you press 3, both channels will be routed. Any other switch will stop the routing. This code latches in the current button. If you do not want that, uncomment the switch_state = 0 line. 

 

I did not compile this code, but you get the idea. 

 

# define aud_in_ch1 (volatile int*) 0x01101020# define aud_out_ch1 (int*) 0x01101030# define aud_in_ch2 (volatile int*) 0x01101040# define aud_out_ch2 (int*) 0x01101050# define switches (volatile char*) 0x01101060 void do_switch_0_function (void) { *aud_out_ch1 = *aud_in_ch1; } void do_switch_1_function (void) { *aud_out_ch2 = *aud_in_ch2; } void do_switch_2_function (void) { *aud_out_ch1 = *aud_in_ch1; *aud_out_ch2 = *aud_in_ch2; } void do_switch_3_function (void) { // do something when switch 3 is pressed. } void do_switch_4_function (void) { // do something when switch 4 is pressed. } void do_switch_5_function (void) { // do something when switch 5 is pressed. } void do_switch_6_function (void) { // do something when switch 6 is pressed. } void do_switch_7_function (void) { // do something when switch 7 is pressed. } int main() { int i; int switch_state = 0; printf("Hello from Nios II!\n"); while (1) { lcd(); // I don't know what lcd () does, but I am assuming it refreshes the lcd. // switch_state = 0; // uncomment this line for momentary switch activation. // Change the switch state into an index. // Only one switch is considered. i.e. if two swithces are // pressed at the same time, only the first one matters. for (i = 0; i < 8; ++i) { if (*switches & (1 << i)) { switch_state = i; break; } } switch (switch_state) { case 0: do_switch_0_function (); break; case 1: do_switch_1_function (); break; case 2: do_switch_2_function (); break; case 3: do_switch_3_function (); break; case 4: do_switch_4_function (); break; case 5: do_switch_5_function (); break; case 6: do_switch_6_function (); break; case 7: do_switch_7_function (); break; } } return 0; }
0 Kudos
Altera_Forum
Honored Contributor II
500 Views

THAT was a great reply. Thanks very much! 

 

 

Eventually i will have a selection of hardware audio effect modules routed into nios. I want to be able to be able to connect them in series ( eg output of block 1 to input of block 2) depending on which order the switches are switched - slide switches.  

 

It should be reconfigurable so effect blocks can be rearranged in order - a basic effect chain really 

 

Anyway, this was the first step 

 

 

Cheers
0 Kudos
Reply