Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

Push Buttons Adujusting Speed

So I'm using a DE2 board with NIOS and Quartus 9.1 and I'm trying to make a function to adjust the speed of packets sent over Ethernet as a modification of the DE2_NET sample. the problem is that whenever I try to implement push buttons in NIOS it both slows down the packet transmission considerably and doesn't display my test case. right now I have this (Not all the code, just the pertinent section, below it is all the code):


 while (1)
  { 
    bool buttonPressed;
    delayAmount =  handle_key(&buttonPressed);
//    delay = delay + delayAmount;
    Randdata(); //Software randomness causes a 1/3 loss in speed
//    if(delayAmount !=0)
//        msleep(delay);
    TransmitPacket(SND,flenght); // Send repetitively 1468 bytes of data.
    times++;
    IOWR(SEG7_DISPLAY_BASE, 0, times); // Show in hex digits the#  of packets sent.
  }
  return 0;
int handle_key(bool *pButtonPressed){
    bool bSpeedDown, bSpeedUp, bMode;
    *pButtonPressed = FALSE;
    
    # ifdef ENABLE_DEBOUNCE
    static alt_u32 next_active_time = 0;
    if (alt_nticks() < next_active_time ){
        return;
    }
    next_active_time = alt_nticks();# endif   
    
    //alt_u8 *button;
    volatile int* button;
    //button = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
    *button = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0); // clear flag 
    IOWR_ALTERA_AVALON_PIO_DATA(LED_RED_BASE, *button);
    bSpeedDown = (*button & 0x08)?TRUE:FALSE;
    bSpeedUp = (*button & 0x04)?TRUE:FALSE;
    bMode = (*button & 0x02)?TRUE:FALSE;
    
    if(bSpeedDown || bSpeedUp || bMode){ // This if statement introduced a 3 Mbit/s hit in performance
      LCD_Test2();
    
 /*   if (bSpeedDown){
        LCD_Test2();
    }  
     
    else if (bSpeedUp){
        LCD_Test2();
    }
    else{
        LCD_Test2();
      }*/
    }
            # ifdef ENABLE_DEBOUNCE        
    if (bSpeedDown || bSpeedUp || bMode){
        next_active_time +=  alt_ticks_per_second()/5;  // debounce
    }        # endif
    
    
  return 0;
}

# include "alt_types.h"  // alt_u32# include "altera_avalon_pio_regs.h" //IOWR_ALTERA_AVALON_PIO_DATA# include "sys/alt_irq.h"  // interrupt# include <stddef.h># include "sys/alt_flash.h"# include "sys/alt_flash_types.h"# include "sys/alt_alarm.h" // time tick function (alt_nticks(), alt_ticks_per_second())# include "sys/alt_timestamp.h" # include "sys/alt_stdio.h"# include <fcntl.h>
# include <io.h># include <stdio.h># include <unistd.h># include <stdlib.h># include <string.h>
# include "LCD.h"# include "system.h"# include "DM9000A.C"# include "hello_led.h"# include "test.h"
unsigned int aaa,rx_len,i,packet_num, times, counter;
unsigned char RXT;
unsigned char SND; // Payload buffer
unsigned int IPsource_1,IPsource_2,IPsource_3,IPsource_4;
unsigned int IPdestination_1,IPdestination_2,IPdestination_3,IPdestination_4;
unsigned int IPchecksum1,IPchecksum2,IPchecksum3,IPchecksum4,IPchecksum5;
unsigned int Mac_source1, Mac_source2, Mac_source3, Mac_source4, Mac_source5, Mac_source6; 
unsigned int Mac_dest1, Mac_dest2, Mac_dest3, Mac_dest4, Mac_dest5, Mac_dest6;
unsigned int times, lenght_h, lenght_l;
unsigned int flenght, IPlenght_h, IPlenght_l, data_lenght, IPlenght;
int delay, delayAmount;
int main(void)
{
 IPsource_1 = 0xC0;        // Assign ie: 192.168.0.44 IP for the DE2 
 IPsource_2 = 0xA8;
 IPsource_3 = 0x00;
 IPsource_4 = 0x2C;
 IPdestination_1 = 0xC0;   // Insert your IP data here
 IPdestination_2 = 0x11;
 IPdestination_3 = 0xA7;
 IPdestination_4 = 0xD9;
 Mac_dest1 = 0x00;         // Insert your MAC address data here
 Mac_dest2 = 0x03;
 Mac_dest3 = 0x25;
 Mac_dest4 = 0x43;
 Mac_dest5 = 0x6A;
 Mac_dest6 = 0x27;
 Mac_source1 =  0x01;      // Assign a MAC address for DE2
 Mac_source2 =  0x60;
 Mac_source3 =  0x6E;
 Mac_source4 =  0x11;
 Mac_source5 =  0x02;
 Mac_source6 =  0x0F;
 
 delay = 0;
 
 data_lenght = 1468;              // Maximun Data lenght 1468 bytes
 flenght = data_lenght + 0x2E;    //Total packet lenght 
 lenght_h = ((data_lenght+8) & 0xFF00)>>8; // Convert in H byte and L byte
 lenght_l = ((data_lenght+8) & 0x00FF);
 IPlenght = data_lenght + 8 + 20;     // IP Lenght for IP header
 IPlenght_h = (IPlenght & 0xFF00)>>8; // Convert in H byte and L byte
 IPlenght_l = (IPlenght & 0x00FF);
 
 // Calculating the IP checksum
 IPchecksum1 = 0x0000C511 + (IPsource_1<<8)+IPsource_2+(IPsource_3<<8)+IPsource_4+
         (IPdestination_1<<8)+IPdestination_2+(IPdestination_3<<8)+(IPdestination_4)+
         (IPlenght_h<<8) + IPlenght_l;
 IPchecksum2 =  ((IPchecksum1&0x0000FFFF)+(IPchecksum1>>16));
 IPchecksum3 = 0x0000FFFF - IPchecksum2;
 IPchecksum4 = (IPchecksum3 & 0xFF00)>>8; 
 IPchecksum5 = (IPchecksum3 & 0x00FF); 
 
 srand ( time(NULL) );
 
   unsigned char TXT =  { Mac_dest1, Mac_dest2, Mac_dest3, Mac_dest4 ,Mac_dest5, Mac_dest6,
                            Mac_source1, Mac_source2, Mac_source3, Mac_source4, Mac_source5, Mac_source6,
                            0x08, 0x00, 0x45, 0x00, IPlenght_h, IPlenght_l,
                            0x00, 0x00, 0x00, 0x00, 0x80, 0x11,
                            IPchecksum4, IPchecksum5, IPsource_1, IPsource_2, IPsource_3, IPsource_4,
                            IPdestination_1, IPdestination_2, IPdestination_3, IPdestination_4, 0x04, 0x00, 
                            0x04, 0x00, lenght_h, lenght_l, 0x00, 0x00};
  
    for (i = 0; i < 42; i++) // Load the TXT in the SND (ethernet packet).
     SND = TXT;
    
    for (i = 42; i < flenght-4; i++) // generating the data to send.
     SND = i-42;
 
    SND = 0x35;   // This checksum is not correct, but also the net recieve the packets correctly.
    SND = 0x15;   // To do, calculate checksum.
    SND = 0xF0;
    SND = 0x13;
    LCD_Test();
    DM9000_init();  // Initialize the DM9000A.
    handle_button_interrupts();
    init_button_irq();
    basic_test();
    
    //Add ability to change data rates
    //Modes, send out data at 34 Mbit/s, send data out in bursts, storing in RAM until full
    //How to get MAC/IP of other INK Board/s and hard code in or software get
  while (1)
  { 
    bool buttonPressed;
    delayAmount =  handle_key(&buttonPressed);
//    delay = delay + delayAmount;
    Randdata(); //Software randomness causes a 1/3 loss in speed
//    if(delayAmount !=0)
//        msleep(delay);
    TransmitPacket(SND,flenght); // Send repetitively 1468 bytes of data.
    times++;
    IOWR(SEG7_DISPLAY_BASE, 0, times); // Show in hex digits the#  of packets sent.
  }
  return 0;
}
//-------------------------------------------------------------------------
//------------------Helper Functions---------------------------------------
//-------------------------------------------------------------------------
    //Generates random data to be sent via Ethernet packets
    //Every time a new packet is sent new data is generated
    //Slows down transmission, in tests went from 34 Mbit/s to 24 Mbit/s
void  Randdata(){
    
    //Random Data Generated
SND = rand() % 255; SND = rand() % 255; SND = rand() % 255;
SND = rand() % 255; SND = rand() % 255; SND = rand() % 255;
SND = rand() % 255; SND = rand() % 255; SND = rand() % 255; 
SND = rand() % 255; SND = rand() % 255; SND = rand() % 255;
}
  //Handles Button presses
int handle_key(bool *pButtonPressed){
    bool bSpeedDown, bSpeedUp, bMode;
    *pButtonPressed = FALSE;
    
    # ifdef ENABLE_DEBOUNCE
    static alt_u32 next_active_time = 0;
    if (alt_nticks() < next_active_time ){
        return;
    }
    next_active_time = alt_nticks();# endif   
    
    //alt_u8 *button;
    volatile int* button;
    //button = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
    *button = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0); // clear flag 
    IOWR_ALTERA_AVALON_PIO_DATA(LED_RED_BASE, *button);
    bSpeedDown = (*button & 0x08)?TRUE:FALSE;
    bSpeedUp = (*button & 0x04)?TRUE:FALSE;
    bMode = (*button & 0x02)?TRUE:FALSE;
    
    if(bSpeedDown || bSpeedUp || bMode){ // This if statement introduced a 3 Mbit/s hit in performance
      LCD_Test2();
    
 /*   if (bSpeedDown){
        LCD_Test2();
    }  
     
    else if (bSpeedUp){
        LCD_Test2();
    }
    else{
        LCD_Test2();
      }*/
    }
            # ifdef ENABLE_DEBOUNCE        
    if (bSpeedDown || bSpeedUp || bMode){
        next_active_time +=  alt_ticks_per_second()/5;  // debounce
    }        # endif
    
    
  return 0;
}
Whenever I press KEY3-KEY1 the respective red LED goes dark as it should, but the LCD TEST2 case does not display. Can anyone help me out here?

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I think this is a problem:

    volatile int* button;

    *button = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);

    You are using an uninitialized pointer which will corrupt memory somewhere.

    The correct code is the one you commented out (but you need to define alt_u8 button, not alt_u8 *button).

    Moreover, IMHO it was correct to test button switching edge instead of button status.

    Now you always have delay=delayAmount=0 and whenever you press a button the function will keep on retriggering until you release it.