/*
* data_handling.c
*
* Created on: 18. dec. 2018
* Author: Domen
*/
#include <stdint.h>
#include "data_handling.h"
#include <sys/mman.h>
/*----------------------------------STATIC FUNCTIONS PROTOTYPES---------------------------------*/
uint32_t read__value(uint32_t *addr);
void write_value (uint32_t *addr, uint32_t value);
/* -----------------------------------------STATIC (PRIVATE) FUNCTIONS----------------------------------*/
uint32_t read_value(uint32_t *addr) //mogoce dodaj *
{
uint32_t result = 0;
result = *(addr);
return result;
}
void write_value(uint32_t *addr, uint32_t value)
{
*addr = value;
}
/*-----------------------------------------PUBLIC FUNCTIONS----------------------------------*/
uint32_t *HW_init(void)
{
void *map_base = (void *)(-1); //naslov, kjer se mi zacne mapiran spomin
static void *virt_addr;
int fd = -1; //file descriptor
//odpiram svoj file
if ( (fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1 )
{
perror("ERROR: open."); //sporoci napako, ce ni OK odprl
}
//mapiram memmory v zgoraj odprtem file-u in preverjam, ce se je OK odprl
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, HW_address & ~MAP_MASK); //zakaj ni tukaj za offset kar samo HW_address
if(map_base == (void *) -1)
{
perror("ERROR: mmap ni pravilno lociral spomina");
}
printf("MAP_BASE: %p\n",map_base
);
virt_addr = map_base + (HW_address & MAP_MASK); //ne vem, ce to potrebno, ker bi moral ze v map_base imeti pravi naslov
virt_addr = (uint32_t *)virt_addr; //castam na uint32_t, da ga read_value lahko sprejme
/* map base bi mi ze moral vrniti moj pravi HW_naslov, se pravi 0x40500000, ker
* v funkciji mmap dodam offset, kje naj locira pomnilnik: zaène na NULL v /dev/mem, offset pa je HW_address & ~MAP_MASK
* kar je enako 0x40500000, maska poskrbi samo za to, da je spodnjih 12 bitov na 0, ker so memory blocks poravnani na
* zaèetek strani (glej tole: https://www.youtube.com/watch?v=XV5sRaSVtXQ at 5.20)
*
*
*/
return virt_addr;
}
uint8_t HW_comm ( HW_system *strct, uint32_t *reg_addr)
{
uint32_t HW_write = 0;
uint32_t HW_read = 0;
if ( (strct -> HW_config_flag) == 1 )
{
//pisanje ukazov v register
HW_write = ( (strct -> start) |
((strct -> reset) << 1) |
((strct -> threshold) << 8) |
((strct -> acq_window) << 19) |
((strct -> sw_trigger) << 27)
);
//*reg_addr = HW_write;
write_value(reg_addr, HW_write);
//printf("VPIS v HW:0x%08x\n",HW_write);
return 1; //sistem nastavljen
}
//branje histograma iz registra in shranjevanje podatkov v strukturo
else if ( (strct -> send_data_flag) == 1)
{
HW_read = read_value(reg_addr);
(strct -> UC_rd_en_current) = (HW_read & UC_rd_en_current_mask) >> 1;
(strct -> UC_rd_en_final) = (HW_read & UC_rd_en_final_mask);
(strct -> hist_data) = (HW_read & HIST_MASK) >> 8 ;
//printf("PREBRANI PODATKI:0x%08x\n",HW_read);
//printf("UC_rd_en_current: 0x%08x\n ",strct ->UC_rd_en_current);
return 2; //podatki pripravljeni za posiljanje v PC
}
else
return 0;
}
/*
//------------TRENUTNI HISTOGRAM-----------
for (int idx=0; idx<BIN_NUM; idx++)
{
HW_read = read_value(reg_addr);
(strct -> UC_rd_en_current) = HW_read & UC_rd_en_current_mask;
(strct -> UC_rd_en_final) = HW_read & UC_rd_en_final_mask;
if ( (HW_read & UC_rd_en_final_mask) == UC_rd_en_final_mask ) //prisel je koncni histogram, ne vpisuj vec v trenutnega
{
break;
}
else
{
//(strct -> hist_idx) = idx;
(strct -> hist_data+idx) = HW_read & HIST_MASK;
}
if(idx == BIN_NUM -1)
{
return 1;
}
}
//------------------KONCNI HISTOGRAM----------------
if ( (strct -> UC_rd_en_final) == 1 && (strct -> UC_rd_en_current) == 0 )
{
(strct -> hist_data + 0) = HW_read & HIST_MASK; //vpis prvega podatka koncnega histograma
for (int idx=1; idx<BIN_NUM; idx++)
{
HW_read = read_value(reg_addr);
(strct -> UC_rd_en_current) = HW_read & UC_rd_en_current_mask;
(strct -> UC_rd_en_final) = HW_read & UC_rd_en_final_mask;
//(strct -> hist_idx) = idx;
(strct -> hist_data+idx) = HW_read & HIST_MASK;
}
if(idx == BIN_NUM -1)
{
return 1;
}
}
*/
// }
//}