Skip to content

Instantly share code, notes, and snippets.

@jadonk
Created Jan 29, 2020
Embed
What would you like to do?
////////////////////////////////////////
// bitflip.arm.c
// Demo of shared memory
// Usage: This takes the first word of SHARED_RAM and flips every other bit
// over and over.
// If /dev/uio0 doesn't exist, consider using
// sudo ./bitflip.arm.out /dev/mem 0x4a300000
// Wiring: None
// Setup: Run this on the ARM and run bitflip.pru0.c or bitflip.pru1.c on the PRU
// See:
// PRU:
////////////////////////////////////////
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#define PRUSS_SHARED_RAM_OFFSET 0x10000
int main(int argc, const char** argv) {
unsigned int i, j, mem_dev;
off_t alloc_offset = 0; /* offset must be 0 for /dev/uio0 */
size_t size = 4096; /* grab page of shared dataram, must allocate with offset of 0 */
char * driver = "/dev/uio0";
int access_offset = PRUSS_SHARED_RAM_OFFSET;
/* Open shared memory driver */
if(argc>1)
driver = strdup(argv[1]);
/* Use specified allocaiton offset */
if(argc>2)
alloc_offset = (off_t)strtol(argv[2],0,0);
/* Use specified access offset */
if(argc>3) {
access_offset = (off_t)strtol(argv[3],0,0);
}
/* Use specified size */
if(argc>4) {
size = (off_t)strtol(argv[4],0,0);
}
/* Allocate shared memory pointer to PRU0 DATARAM */
size += access_offset;
printf("Opening %s offset 0x%08x with access offset 0x%08x\n", driver, alloc_offset, access_offset);
mem_dev = open(driver, O_RDWR | O_SYNC);
volatile int *shared_dataram = mmap(NULL,
size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mem_dev,
alloc_offset
);
if(shared_dataram == MAP_FAILED) {
perror("mmap");
exit(-1);
}
shared_dataram += (access_offset/4);
printf("shared_dataram = %p\n", shared_dataram);
j = *shared_dataram;
printf("Read 0x%08x\n", j);
for(i=0; i<10; i++) {
printf("Writing 0x%08x\n", i);
*shared_dataram = i;
usleep(1);
j = *shared_dataram;
printf("Read 0x%08x (%s, Mask=0x%08x)\n", j, j==i?"not flipped":"flipped!", j^i);
}
return(0);
}
@jadonk

This comment has been minimized.

Copy link
Owner Author

@jadonk jadonk commented Jan 29, 2020

debian@beaglebone:/var/lib/cloud9/PocketBeagle/pru$ ./bitflip.arm.out /dev/uio0 0x2000 0 16
Opening /dev/uio0 offset 0x00002000 with access offset 0x00000000
shared_dataram = 0xb6f41000
Read 0x00000000
Writing 0x00000000
Read 0x00000000 (not flipped, Mask=0x00000000)
Writing 0x00000001
Read 0xaaaaaaab (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000002
Read 0xaaaaaaa8 (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000003
Read 0xaaaaaaa9 (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000004
Read 0xaaaaaaae (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000005
Read 0xaaaaaaaf (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000006
Read 0xaaaaaaac (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000007
Read 0xaaaaaaad (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000008
Read 0xaaaaaaa2 (flipped!, Mask=0xaaaaaaaa)
Writing 0x00000009
Read 0xaaaaaaa3 (flipped!, Mask=0xaaaaaaaa)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment