FPGA, SoC, And CPLD Boards And Kits
FPGA Evaluation and Development Kits
5915 Discussions

COntrol FPGA component from Linux

User1571932698773355
1,592 Views

I am searching for a example that show me how to control fpga compionent from linux. I use cyclone 5 soc.

is there any example available to show that?

0 Kudos
1 Solution
Fawaz_Al-Jubori
Employee
1,396 Views

Hello,

You can refer to this sample code to control LED on FPGA,

 

#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/mman.h>

#include "hps.h"

#include "stdint.h"

#include <termios.h>

 

 

/* The base address byte offset for the start of the ALT_LWFPGASLVS component. */

#define ALT_LWFPGASLVS_OFST    0xff200000

/* The base address byte offset for the start of the ALT_STM component. */

#define ALT_STM_OFST    0xfc000000

#define ALT_AXI_FPGASLVS_OFST (0xC0000000) // axi_master

 

#define HW_REGS_BASE ( ALT_STM_OFST )

#define HW_REGS_SPAN ( 0x40000000 )

#define HW_REGS_MASK ( HW_REGS_SPAN - 1 )

 

 

 

#define LED_PIO_BASE 0x10040

 

 

 

int main() {

 

void *virtual_base;

int fd, i;

int loop_count;

int led_direction;

int led_mask;

void *h2p_lw_led_addr;

int *ocram_src, *ocram_dest;

int *dma_base;

 

// map the address space for the LED registers into user space so we can interact with them.

// we'll actually map in the entire CSR span of the HPS since we want to access various registers within that span

 

if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 ) {

printf( "ERROR: could not open \"/dev/mem\"...\n" );

return( 1 );

}

 

virtual_base = mmap( NULL, HW_REGS_SPAN, ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, ALT_AXI_FPGASLVS_OFST );

 

if( virtual_base == MAP_FAILED ) {

printf( "ERROR: mmap() failed...\n" );

close( fd );

return( 1 );

}

h2p_lw_led_addr=virtual_base + ( ( unsigned long )( ALT_LWFPGASLVS_OFST + LED_PIO_BASE ) & ( unsigned long)( HW_REGS_MASK ) );

 

   

// toggle the LEDs a bit

 

loop_count = 0;

led_mask = 0x01;

led_direction = 0; // 0: left to right direction

while( loop_count < 60 ) {

// control led

*(uint32_t *)h2p_lw_led_addr = ~led_mask; 

 

// wait 100ms

usleep( 100*1000 );

// update led mask

if (led_direction == 0){

led_mask <<= 1;

if (led_mask == (0x01 << (LED_PIO_DATA_WIDTH-1)))

led_direction = 1;

}else{

led_mask >>= 1;

if (led_mask == 0x01){ 

led_direction = 0;

loop_count++;

}

}

} // while

 

// clean up our memory mapping and exit

if( munmap( virtual_base, HW_REGS_SPAN ) != 0 ) {

printf( "ERROR: munmap() failed...\n" );

close( fd );

return( 1 );

}

 

close( fd );

 

return( 0 );

}

 

View solution in original post

0 Kudos
2 Replies
Fawaz_Al-Jubori
Employee
1,397 Views

Hello,

You can refer to this sample code to control LED on FPGA,

 

#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/mman.h>

#include "hps.h"

#include "stdint.h"

#include <termios.h>

 

 

/* The base address byte offset for the start of the ALT_LWFPGASLVS component. */

#define ALT_LWFPGASLVS_OFST    0xff200000

/* The base address byte offset for the start of the ALT_STM component. */

#define ALT_STM_OFST    0xfc000000

#define ALT_AXI_FPGASLVS_OFST (0xC0000000) // axi_master

 

#define HW_REGS_BASE ( ALT_STM_OFST )

#define HW_REGS_SPAN ( 0x40000000 )

#define HW_REGS_MASK ( HW_REGS_SPAN - 1 )

 

 

 

#define LED_PIO_BASE 0x10040

 

 

 

int main() {

 

void *virtual_base;

int fd, i;

int loop_count;

int led_direction;

int led_mask;

void *h2p_lw_led_addr;

int *ocram_src, *ocram_dest;

int *dma_base;

 

// map the address space for the LED registers into user space so we can interact with them.

// we'll actually map in the entire CSR span of the HPS since we want to access various registers within that span

 

if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 ) {

printf( "ERROR: could not open \"/dev/mem\"...\n" );

return( 1 );

}

 

virtual_base = mmap( NULL, HW_REGS_SPAN, ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, ALT_AXI_FPGASLVS_OFST );

 

if( virtual_base == MAP_FAILED ) {

printf( "ERROR: mmap() failed...\n" );

close( fd );

return( 1 );

}

h2p_lw_led_addr=virtual_base + ( ( unsigned long )( ALT_LWFPGASLVS_OFST + LED_PIO_BASE ) & ( unsigned long)( HW_REGS_MASK ) );

 

   

// toggle the LEDs a bit

 

loop_count = 0;

led_mask = 0x01;

led_direction = 0; // 0: left to right direction

while( loop_count < 60 ) {

// control led

*(uint32_t *)h2p_lw_led_addr = ~led_mask; 

 

// wait 100ms

usleep( 100*1000 );

// update led mask

if (led_direction == 0){

led_mask <<= 1;

if (led_mask == (0x01 << (LED_PIO_DATA_WIDTH-1)))

led_direction = 1;

}else{

led_mask >>= 1;

if (led_mask == 0x01){ 

led_direction = 0;

loop_count++;

}

}

} // while

 

// clean up our memory mapping and exit

if( munmap( virtual_base, HW_REGS_SPAN ) != 0 ) {

printf( "ERROR: munmap() failed...\n" );

close( fd );

return( 1 );

}

 

close( fd );

 

return( 0 );

}

 

0 Kudos
User1571932698773355
1,396 Views

wow, thats beyond my expectations. thank you Fjumaah

0 Kudos
Reply