Commit fd48881d authored by ARnaud Blanchard's avatar ARnaud Blanchard

Very fast functional version. Need API iprovement

parent 0a41b536
......@@ -16,9 +16,9 @@ project(raspinobo)
find_package(blc_channel REQUIRED)
find_package(blc_program REQUIRED)
add_definitions(${BL_DEFINITIONS} -std=c++14 -Wall -W -fdiagnostics-color=always -march=native)
add_definitions(${BL_DEFINITIONS} -pg -std=c++14 -Wall -W -fdiagnostics-color=always -march=native)
include_directories(${BL_INCLUDE_DIRS})
add_executable(raspinobo raspinobo.cpp bl_raspi.cpp)
add_executable(raspinobo raspinobo.cpp bl_raspi.cpp bl_raspi_core.cpp)
target_link_libraries(raspinobo ${BL_LIBRARIES})
if (NOT APPLE)
......
#include "bl_raspi.h"
#include "bl_raspi_core.h"
#include "blc_core.h"
//#include "mmapGpio.h"
#include <sys/ioctl.h> //ioctl
......@@ -14,41 +16,16 @@
#include <vector>
#define GPIO_MAX 32
#define FLAG(pos) (1<<pos)
using namespace std;
#define DELTA_NS(current_ns, previous_ns) ((previous_ns > current_ns) ? 1000000000-previous_ns+current_ns : current_ns-previous_ns)
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;
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;
vector <Volt_output> volt_outputs;
......@@ -63,42 +40,48 @@ inline void update_time(){
clock_gettime(CLOCK_MONOTONIC, &timestamp);
current_ns=timestamp.tv_nsec;
delta_ns=current_ns-previous_ns;
ns_per_it+=(((float)delta_ns/(float)it_since_clock)-ns_per_it)/10.f;
previous_ns=current_ns;
it_since_clock=0;
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)/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;
}
}
}*/
// fprintf(stderr, "%d %f\n", delta_ns, ns_per_it);
}
Ohm_input::Ohm_input(int pin, int us_tau, uint32_t *buffer):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer), activated(1){
Ohm_input::Ohm_input(int pin, int us_tau, uint32_t *buffer):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer), activated(us_tau*1000){
}
inline void Ohm_input::rest(){
raspi.set_pin_dir(pin, raspi.INPUT);
update_time();
buffer[index]=rest_ns-spike_ns;
buffer[index]=DELTA_NS(current_ns, spike_ns);
index++;
if (index==buffer_size) index=0;
rest_ns=current_ns;
// fprintf(stderr, "%lld %f\n", current_ns-spike_ns, ns_per_it);
}
inline void Ohm_input::spike(){
raspi.set_pin_dir(pin, raspi.OUTPUT);
tau=10000;
tau=50;
update_time();
spike_ns=current_ns;
buffer[index]=current_ns-rest_ns;
buffer[index]=DELTA_NS(current_ns, rest_ns);
index++;
if (index==buffer_size) index=0;
stop_ns=current_ns + tau*1000;
activated=(float)(tau*1000)/ns_per_it;
// stop_ns=current_ns + tau*1000;
activated=(tau*1000+1)/min_ns_per_it+1;
}
......@@ -106,70 +89,6 @@ Volt_output::Volt_output(int pin):index(0), flag(FLAG(pin)), pin(pin), buffer(bu
}
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));
}
}
void bl_raspi_add_RC_input(int pin, int us_tau, uint32_t *buffer){
ohm_inputs.emplace_back(pin, us_tau, buffer);
}
......@@ -205,8 +124,10 @@ void *thread_loop(void*){
raspi.set_pin_mode(output.pin, raspi.PULL_OFF);
raspi.set_pin_dir(output.pin, raspi.INPUT);
}
uint32_t results;
while(run_loop){
// results =*(raspi.gplev0);
for(auto &input : ohm_inputs){
if (input.activated) {
input.activated--;
......
......@@ -29,7 +29,6 @@ typedef struct Volt_output
uint8_t pin;
uint8_t activated;
float average;
FILE *file;
uint8_t activated_input;
int index;
uint32_t *buffer;
......
......@@ -28,12 +28,10 @@ using namespace std;
float gaussian(float mu, float var){
}
static uchar gaussian_cdf(uint32_t s, float mu, float var){
return (1+erf((s-mu)/sqrt(2*var)))*255/2; //var+1 is to be sure it is not null
}
blc_channel motor_channel, sensor_channel, neurons_channel;
int main(int argc, char **argv){
......@@ -132,12 +130,11 @@ int main(int argc, char **argv){
else delay_ns = 1000000000 - previous_ns + timestamp.tv_nsec;
previous_ns=timestamp.tv_nsec;
fprintf(stderr, "%f, Freq:%fMHz\n n0:%d n0mu:%f n0var:%f", (float)delay_ns, (bl_raspi_iterations_nb-previous_iterations_nb)/((float)(delay_ns/1000)), neurons_channel.uchars[2], neurons[0].mu, neurons[0].var);
fprintf(stderr, "Freq:%.4fMHz\n n0:%d n0mu:%f n0var:%f", (float)(bl_raspi_iterations_nb-previous_iterations_nb)/((float)(delay_ns/1000)), neurons_channel.uchars[2], neurons[0].mu, neurons[0].var);
previous_iterations_nb=bl_raspi_iterations_nb;
}
fclose(record_file);
bl_raspi_stop_loop();
return EXIT_SUCCESS;
}
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