Skip to content

Instantly share code, notes, and snippets.

@thyyppa thyyppa/main.cpp
Created Feb 10, 2017

Embed
What would you like to do?
#include "main.h"
unsigned long int *regAddress = nullptr;
unsigned long int regVal;
int main( int argc, char *argv[] )
{
regAddress = setRegistryAddress();
std::string in;
while ( true )
{
std::cin >> in;
if ( in == "1" )
{
pinSet( LED_WHITE );
continue;
}
pinClear( LED_WHITE );
}
}
unsigned long int *setRegistryAddress()
{
unsigned long int *registryAddress;
int memory = open( "/dev/mem", O_RDWR );
registryAddress = (unsigned long *) mmap(
NULL,
REGISTER_BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED,
memory,
REGISTER_BLOCK_ADDR
);
close( memory );
return registryAddress;
}
void pinClear( int pin )
{
SETBIT( regVal, pin, 1 );
WRITEREG( REGISTER_OE_OFFSET, regVal );
WRITEREG( REGISTER_CLEAR_OFFSET, ( 0x1 << pin ));
}
void pinSet( int pin )
{
SETBIT( regVal, LED_WHITE, 1 );
WRITEREG( REGISTER_OE_OFFSET, regVal );
WRITEREG( REGISTER_SET_OFFSET, ( 0x1 << pin ));
}
#ifndef ONIONTEST_MAIN_H
#define ONIONTEST_MAIN_H
#include <unistd.h>
#include <mhash.h>
#include <fcntl.h>
#include <iostream>
#define LED_WHITE 1
#define LED_RED 17
#define LED_GREEN 16
#define LED_BLUE 15
#define REGISTER_BLOCK_ADDR 0x18040000
#define REGISTER_BLOCK_SIZE 0x30
#define REGISTER_OE_OFFSET 0
#define REGISTER_IN_OFFSET 1
#define REGISTER_OUT_OFFSET 2
#define REGISTER_SET_OFFSET 3
#define REGISTER_CLEAR_OFFSET 4
#define WRITEREG( regOff, regVal ) (*(regAddress + regOff) = regVal)
#define READREG( regVal, regOff ) (regVal = *(regAddress + regOff))
#define SETBIT( regVal, bit, val ) (regVal ^= (-val ^ regVal) & (1 << bit))
#define GETBIT( regval, bit ) ((regval >> bit) & 0x1)
void pinSet( int pin );
void pinClear( int pin );
unsigned long int *setRegistryAddress();
#endif //ONIONTEST_MAIN_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.