Commit 505e6d55 authored by ARnaud Blanchard's avatar ARnaud Blanchard

Separate files to improve clarity and speed of comilation

parent fd48881d
......@@ -16,7 +16,7 @@ project(raspinobo)
find_package(blc_channel REQUIRED)
find_package(blc_program REQUIRED)
add_definitions(${BL_DEFINITIONS} -pg -std=c++14 -Wall -W -fdiagnostics-color=always -march=native)
add_definitions(${BL_DEFINITIONS} -std=c++14 -Wall -W -fdiagnostics-color=always -march=native)
include_directories(${BL_INCLUDE_DIRS})
add_executable(raspinobo raspinobo.cpp bl_raspi.cpp bl_raspi_core.cpp)
target_link_libraries(raspinobo ${BL_LIBRARIES})
......
......@@ -24,7 +24,7 @@
using namespace std;
uint32_t it_since_clock=0;
uint32_t current_ns, previous_ns;
//float ns_per_it=1;
float ns_per_it=1;
uint32_t min_ns_per_it=1000;
bl_raspi raspi;
vector <Ohm_input> ohm_inputs;
......@@ -43,19 +43,19 @@ inline void update_time(){
delta_ns=DELTA_NS(current_ns, previous_ns);
if (it_since_clock){
min_ns_per_it=min(min_ns_per_it, (delta_ns+1)/it_since_clock);
ns_per_it=(float)delta_ns/(float)it_since_clock;
// ns_per_it+=(((float)delta_ns/(float)it_since_clock)-ns_per_it)/100.f;
it_since_clock=0;
}
previous_ns=current_ns;
/*
for( auto &input : ohm_inputs){
if (input.activated){
if (input.stop_ns - current_ns> 0) input.activated = (float)(input.stop_ns - current_ns)/ns_per_it;
else input.activated=1;
delta_ns=DELTA_NS(current_ns, input.spike_ns);
if (delta_ns > input.tau) input.activated=1;
else input.activated=(input.tau-delta_ns)/ns_per_it;
}
}*/
}
// fprintf(stderr, "%d %f\n", delta_ns, ns_per_it);
}
......@@ -74,14 +74,13 @@ inline void Ohm_input::rest(){
}
inline void Ohm_input::spike(){
raspi.set_pin_dir(pin, raspi.OUTPUT);
tau=50;
tau=50000;
update_time();
spike_ns=current_ns;
buffer[index]=DELTA_NS(current_ns, rest_ns);
index++;
if (index==buffer_size) index=0;
// stop_ns=current_ns + tau*1000;
activated=(tau*1000+1)/min_ns_per_it+1;
activated=tau/ns_per_it;;
}
......@@ -173,7 +172,7 @@ void *thread_loop(void*){
}*/
bl_raspi_iterations_nb++;
it_since_clock++;
if (it_since_clock == (1<<16)) update_time();
if (it_since_clock == 1024) update_time();
}
//Stop voltages before living for security
for(auto &output : volt_outputs) raspi.set_pin_dir(output.pin, raspi.INPUT);
......
#include "bl_raspi_core.h"
#include "blc_core.h"
//#include "mmapGpio.h"
#include <sys/ioctl.h> //ioctl
#include <sys/mman.h>
#include <fcntl.h> //O_RDWR, ...
#include <unistd.h> //read getpid
#include <libgen.h> //basename
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
#define GPIO_MAX 32
#define FLAG(pos) (1<<pos)
using namespace std;
volatile uint32_t *map_memory(uint32_t base_addr, size_t size){
int mem_fd;
volatile uint32_t *mapped_addr;
SYSTEM_ERROR_CHECK(mem_fd = open("/dev/mem", O_RDWR|O_SYNC), 0, "Maybe you need to be sudo or use set_cap on the binary" );
/* mmap IO */
SYSTEM_ERROR_CHECK(mapped_addr = (volatile unsigned *)mmap(NULL, //Any adddress in our space will do
size, //Map length
PROT_READ|PROT_WRITE|PROT_EXEC,// Enable reading & writting to mapped memory
#ifdef MAP_LOCKED
MAP_SHARED | MAP_LOCKED,//Shared with other processes
#else
MAP_SHARED, // Darwin (OSX) misses MAP_LOCKED
#endif
mem_fd, //File to map
base_addr //Offset to base address
), MAP_FAILED, "Mapping physical memory. You are not on raspberry or you do not have the rights. ( use setcap or sudo).");
SYSTEM_SUCCESS_CHECK(close(mem_fd), 0, "We cannot close fd_meme (i.e. we can close /dev/mem)");
return mapped_addr;
}
bl_raspi::bl_raspi(){
//maps timers
system_timers_map=map_memory(system_timers_base_addr, 0x1C);
ptime_us=system_timers_map+1; //+1 is 4bytes because ptimes is 32(4*8)bits step
//map gpio
gpio_map=map_memory(gpio_base_addr, 0xB4);
gpfsels=gpio_map;
gpfset0=gpio_map+7; // 1C/4 because 32(4*8)bits step
gpclr0=gpio_map+10;
gplev0=gpio_map+13;
gppud=gpio_map+37;//To select Off/PullUp/PullDown
gppudclk0=gpio_map+38;//To update pulling value
}
bl_raspi::~bl_raspi(){
SYSTEM_SUCCESS_CHECK(munmap((void*)system_timers_map, 0x1C), 0, "unmap system_timers_base_addr");
SYSTEM_SUCCESS_CHECK(munmap((void*)gpio_map, 0xB4), 0, "unmap gpio_base_addr");
}
void bl_raspi::set_pin_mode(int pinnum, int mode){
*gppud = mode;
usleep(20); //See the broadcom 2835 documentation
*gppudclk0 = FLAG(pinnum); //We select which pins will be affected
usleep(20);
*gppud = 0;
*gppudclk0 = 0;
}
void bl_raspi::set_pin_dir(int pinnum, int dir){
volatile uint32_t *gpfsel=gpfsels+pinnum/10;
if (dir == OUTPUT){
*gpfsel&=~(unsigned int)(7<<((pinnum%10)*3));
*gpfsel |= (1<<((pinnum%10)*3));
}
else{
*gpfsel &= ~(unsigned int)(7<<((pinnum%10)*3));
}
}
#ifndef BL_RASPI_CORE_H
#define BL_RASPI_CORE_H
#include <stdint.h>
typedef struct bl_raspi{
static const unsigned int system_timers_base_addr = 0x3F003000;
static const unsigned int gpio_base_addr = 0x3F200000;// gpio registers base address for raspberry 2 et 3 0x20200000 sinon
//Three pulling resistor (~50KOhm modes in input direction
static const unsigned int PULL_OFF = 0;
static const unsigned int PULL_DOWN = 1;
static const unsigned int PULL_UP = 2;
// two possible states for pin direction
static const unsigned int INPUT = 0;
static const unsigned int OUTPUT = 1;
// two possible states for output pins
static const unsigned int LOW = 0;
static const unsigned int HIGH = 1;
volatile uint32_t *system_timers_map, *gpio_map;
volatile uint32_t *ptime_us;
volatile uint32_t *gpfsels, *gpfset0, *gpclr0, *gplev0, *gppud, *gppudclk0; //1 could be used if more than 32 pins
bl_raspi();
~bl_raspi();
void set_pin_dir(int pin, int dir); //input or output
void set_pin_mode(int pin, int mode); //pulling resistance
}bl_raspi;
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment