Skip to content

Fast reg log.c

Function main()

main: The function of main is to read specific registers from a memory-mapped hardware interface and log the data into a binary file.

parameters: The parameters of this Function. ยท parameter1: None

Code Description: The main function begins by defining the size of the Block RAM (BRAM) to be 64KB, which is aligned with the hardware configuration specified in the device tree. It also defines the physical base address of the BRAM, which is set to 0x83c40000, again aligned with the hardware configuration. The function then declares several variables, including a pointer for the virtual address of the BRAM, file descriptor variables, and an array to store register values.

The function attempts to open the /dev/mem device file, which allows access to physical memory. If successful, it maps the physical address of the BRAM into the process's virtual address space using mmap, allowing the program to read from the hardware registers directly.

A binary file named fast_reg_log.bin is opened for writing. If the file cannot be opened, an error message is printed, and the function exits. The function then enters a loop that iterates 10 times. Within this loop, it reads from two specific registers (registers 57 and 58) of the hardware interface, storing the values in the tsf_reg array. The first register (57) is expected to contain the RSSI (Received Signal Strength Indicator) status, while the second register (58) contains the low 32 bits of the timestamp.

After reading the registers, the function writes the collected data from the tsf_reg array to the binary file. Once all iterations are complete, the file is closed, and the file descriptor for /dev/mem is also closed before the function returns 0, indicating successful completion.

Note: It is important to ensure that the program has the necessary permissions to access /dev/mem, as this typically requires elevated privileges. Additionally, the hardware configuration must match the expected addresses and register definitions for the function to operate correctly.

Output Example: The output of the function is a binary file named fast_reg_log.bin, which contains the logged register values. The file will consist of 10 sets of 524288 * 2 entries, where each entry corresponds to the values read from registers 57 and 58 during each iteration. The structure of the binary data will be a sequence of 32-bit unsigned integers.

graph TD
    A[Start main function] --> B[Initialize bram_size and bram_pbase]
    B --> C[Declare variables]
    C --> D[Open /dev/mem for reading]
    D -->|Success| E[Map BRAM physical address to virtual address]
    E --> F[Open fast_reg_log.bin for writing]
    F -->|File open success| G[Loop 10 times]
    G --> H[Loop through tsf_reg array]
    H --> I[Read values from bram32_vptr]
    I --> J[Write tsf_reg to file]
    J --> H
    H --> K[Close fast_reg_log.bin]
    K --> L[Close /dev/mem]
    L --> M[Return 0]
    D -->|Failure| N[Return 0]
    F -->|File open failure| O[Print fopen error and return 0]