Skip to content

Instantly share code, notes, and snippets.

@will127534
Created April 29, 2023 23:43
Show Gist options
  • Save will127534/5f0b3d80dc8a3748b83ab74507a4b58b to your computer and use it in GitHub Desktop.
Save will127534/5f0b3d80dc8a3748b83ab74507a4b58b to your computer and use it in GitHub Desktop.
Simple C code to reading and decode UNICAM_STA register
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#define BIT(n) (1 << (n))
#define UNICAM_BASE_ADDR 0xFE801000
#define UNICAM_STA_OFFSET 0x4
#define UNICAM_SYN BIT(0)
#define UNICAM_CS BIT(1)
#define UNICAM_SBE BIT(2)
#define UNICAM_PBE BIT(3)
#define UNICAM_HOE BIT(4)
#define UNICAM_PLE BIT(5)
#define UNICAM_SSC BIT(6)
#define UNICAM_CRCE BIT(7)
#define UNICAM_OES BIT(8)
#define UNICAM_IFO BIT(9)
#define UNICAM_OFO BIT(10)
#define UNICAM_BFO BIT(11)
#define UNICAM_DL BIT(12)
#define UNICAM_PS BIT(13)
#define UNICAM_IS BIT(14)
#define UNICAM_PI0 BIT(15)
#define UNICAM_PI1 BIT(16)
#define UNICAM_FSI_S BIT(17)
#define UNICAM_FEI_S BIT(18)
#define UNICAM_LCI_S BIT(19)
#define UNICAM_BUF0_RDY BIT(20)
#define UNICAM_BUF0_NO BIT(21)
#define UNICAM_BUF1_RDY BIT(22)
#define UNICAM_BUF1_NO BIT(23)
#define UNICAM_DI BIT(24)
void print_flags(uint32_t sta) {
printf("Enabled flags and meanings:\n");
if (sta & UNICAM_SYN) printf("- UNICAM_SYN: Synchronised\n");
if (sta & UNICAM_CS) printf("- UNICAM_CS: Clock Status\n");
if (sta & UNICAM_SBE) printf("- UNICAM_SBE: Single Bit Error\n");
if (sta & UNICAM_PBE) printf("- UNICAM_PBE: Parity Bit Error\n");
if (sta & UNICAM_HOE) printf("- UNICAM_HOE: Higher Order Error\n");
if (sta & UNICAM_PLE) printf("- UNICAM_PLE: Packet Length Error\n");
if (sta & UNICAM_SSC) printf("- UNICAM_SSC: SSC\n");
if (sta & UNICAM_CRCE) printf("- UNICAM_CRCE: CRC Error\n");
if (sta & UNICAM_OES) printf("- UNICAM_OES: OES\n");
if (sta & UNICAM_IFO) printf("- UNICAM_IFO: Input Overflow\n");
if (sta & UNICAM_OFO) printf("- UNICAM_OFO: Output Overflow\n");
if (sta & UNICAM_BFO) printf("- UNICAM_BFO: BFO\n");
if (sta & UNICAM_DL) printf("- UNICAM_DL: DL\n");
if (sta & UNICAM_PS) printf("- UNICAM_PS: Panic Status\n");
if (sta & UNICAM_IS) printf("- UNICAM_IS: IS\n");
// Add other flags and their meanings as needed
}
int main() {
// Open the /dev/mem device
int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
if (mem_fd < 0) {
perror("open /dev/mem");
return 1;
}
// Map UNICAM peripheral memory
volatile uint32_t *unicam_mem = (uint32_t *)mmap(
NULL,
0x1000,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mem_fd,
UNICAM_BASE_ADDR
);
if (unicam_mem == MAP_FAILED) {
perror("mmap");
close(mem_fd);
return 1;
}
// Read UNICAM_STA register
uint32_t unicam_sta = *(unicam_mem + (UNICAM_STA_OFFSET / sizeof(uint32_t)));
printf("UNICAM_STA: 0x%08X\n", unicam_sta);
// Print enabled flags and their meanings
print_flags(unicam_sta);
// Unmap memory and clean up
munmap((void *)unicam_mem, 0x1000);
close(mem_fd);
return 0;
}
@will127534
Copy link
Author

Compile and run the updated code:

gcc readSTA.c -o readSTA
sudo ./readSTA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment