SWE-350 TOTP Generator Milestone 5
The DE-10 board has six 7-segment displays, this can be used to display and generate a time based one-time pin (TOTP).
Loading...
Searching...
No Matches
increment_leds.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <unistd.h>
3#include <fcntl.h>
4#include <sys/mman.h>
6
7/* Prototypes for functions used to access physical memory addresses */
8int open_physical (int);
9void * map_physical (int, unsigned int, unsigned int);
10void close_physical (int);
11int unmap_physical (void *, unsigned int);
12
13/* This program increments the contents of the red LED parallel port */
14int increment_leds(int total)
15{
16 volatile int * LEDR_ptr; // virtual address pointer to red LEDs
17
18 int fd = -1; // used to open /dev/mem for access to physical addresses
19 void *LW_virtual; // used to map physical addresses for the light-weight bridge
20
21 // Create virtual memory access to the FPGA light-weight bridge
22 if ((fd = open_physical (fd)) == -1)
23 return (-1);
25 return (-1);
26
27 // Set virtual address pointer to I/O port
28 LEDR_ptr = (unsigned int *) (LW_virtual + LEDR_BASE);
29
30 // Add 1 to the I/O register
31 // *LEDR_ptr = *LEDR_ptr + ;
32 *LEDR_ptr = total;
33
34
35 unmap_physical (LW_virtual, LW_BRIDGE_SPAN); // release the physical-memory mapping
36 close_physical (fd); // close /dev/mem
37 //sleep(1);
38 //main();
39 return 0;
40}
41
42// Open /dev/mem, if not already done, to give access to physical addresses
44{
45 if (fd == -1)
46 if ((fd = open( "/dev/mem", (O_RDWR | O_SYNC))) == -1)
47 {
48 printf ("ERROR: could not open \"/dev/mem\"...\n");
49 return (-1);
50 }
51 return fd;
52}
53
54// Close /dev/mem to give access to physical addresses
56{
57 close (fd);
58}
59
60/*
61 * Establish a virtual address mapping for the physical addresses starting at base, and
62 * extending by span bytes.
63 */
64void* map_physical(int fd, unsigned int base, unsigned int span)
65{
66 void *virtual_base;
67
68 // Get a mapping from physical addresses to virtual addresses
69 virtual_base = mmap (NULL, span, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, base);
70 if (virtual_base == MAP_FAILED)
71 {
72 printf ("ERROR: mmap() failed...\n");
73 close (fd);
74 return (NULL);
75 }
76 return virtual_base;
77}
78
79/*
80 * Close the previously-opened virtual address mapping
81 */
82int unmap_physical(void * virtual_base, unsigned int span)
83{
84 if (munmap (virtual_base, span) != 0)
85 {
86 printf ("ERROR: munmap() failed...\n");
87 return (-1);
88 }
89 return 0;
90}
91
void * LW_virtual
Used to map physical addresses for the light-weight bridge Not made by Erick Grant,...
Definition 7seg.c:30
void * virtual_base
Base for the LCD Used to define the address of the LCD.
Definition 7seg.c:25
int fd
Used to open /dev/mem for access to physical addresses Not made by Erick Grant, copyright Terasic Inc...
Definition 7seg.c:18
#define LW_BRIDGE_SPAN
#define LW_BRIDGE_BASE
#define LEDR_BASE
int open_physical(int)
int increment_leds(int total)
void close_physical(int)
int unmap_physical(void *, unsigned int)
void * map_physical(int, unsigned int, unsigned int)