fork download
  1. #define _BSD_SOURCE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/mman.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <stdint.h>
  11.  
  12. #define HK_FPGA_BASE_ADDR 0x40000000
  13. #define HK_FPGA_BASE_SIZE 0x30000
  14. #define LED_CONTROL_OFFSET 0x30
  15.  
  16. uint32_t *g_hk_fpga_reg_mem = NULL;
  17. uint32_t *g_hk_fpga_LED_mem = NULL;
  18. /** The memory file descriptor used to mmap() the FPGA space */
  19. int g_hk_fpga_mem_fd = -1;
  20.  
  21. inline void toggle_led(const uint32_t led_pin);
  22. int __hk_fpga_cleanup_mem(void);
  23.  
  24. int __hk_fpga_cleanup_mem(void) {
  25. /* If register structure is NULL we do not need to un-map and clean up */
  26. if(g_hk_fpga_reg_mem) {
  27. if(munmap(g_hk_fpga_reg_mem, HK_FPGA_BASE_SIZE) < 0) {
  28. fprintf(stderr, "munmap() failed: %s\n", strerror(errno));
  29. return -1;
  30. }
  31. g_hk_fpga_reg_mem = NULL;
  32. if(g_hk_fpga_LED_mem)
  33. g_hk_fpga_LED_mem = NULL;
  34. }
  35. if(g_hk_fpga_mem_fd >= 0) {
  36. close(g_hk_fpga_mem_fd);
  37. g_hk_fpga_mem_fd = -1;
  38. }
  39. return 0;
  40. }
  41.  
  42. void toggle_led(const uint32_t led_pin) {
  43. *g_hk_fpga_LED_mem ^= 1 << led_pin;
  44. }
  45.  
  46. int main() {
  47. /* Page variables used to calculate correct mapping addresses */
  48. void *page_ptr;
  49. long page_addr, page_off, page_size = sysconf(_SC_PAGESIZE);
  50.  
  51. /* If module was already initialized, clean all internals */
  52. if(__hk_fpga_cleanup_mem() < 0) {
  53. return -1;
  54. }
  55.  
  56. /* Open /dev/mem to access directly system memory */
  57. g_hk_fpga_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  58. if(g_hk_fpga_mem_fd < 0) {
  59. fprintf(stderr, "open(/dev/mem) failed: %s\n", strerror(errno));
  60. return -1;
  61. }
  62.  
  63. /* Calculate correct page address and offset from HK_FPGA_BASE_ADDR and
  64.   * HK_FPGA_BASE_SIZE
  65.   */
  66. page_addr = HK_FPGA_BASE_ADDR & (~(page_size-1));
  67. page_off = HK_FPGA_BASE_ADDR - page_addr;
  68.  
  69. /* Map FPGA memory space to page_ptr. */
  70. page_ptr = mmap(NULL, HK_FPGA_BASE_SIZE, PROT_READ | PROT_WRITE,
  71. MAP_SHARED, g_hk_fpga_mem_fd, page_addr);
  72. if((void *)page_ptr == MAP_FAILED) {
  73. fprintf(stderr, "mmap() failed: %s\n", strerror(errno));
  74. __hk_fpga_cleanup_mem();
  75. return -1;
  76. }
  77.  
  78. /* Set FPGA HK module pointers to correct values. */
  79. g_hk_fpga_reg_mem = page_ptr + page_off;
  80. g_hk_fpga_LED_mem = g_hk_fpga_reg_mem + (LED_CONTROL_OFFSET / sizeof(uint32_t));
  81.  
  82. for (int i = 0; i < 20; i++) {
  83. toggle_led(3);
  84.  
  85. usleep(200000);
  86. }
  87.  
  88. return 0;
  89. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:82:2: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
  for (int i = 0; i < 20; i++) { 
  ^
prog.c:82:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
stdout
Standard output is empty