Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
336 | f9daq | 1 | /* |
2 | * data_handling.c |
||
3 | * |
||
4 | * Created on: 18. dec. 2018 |
||
5 | * Author: Domen |
||
6 | */ |
||
7 | |||
8 | #include <stdint.h> |
||
9 | #include "data_handling.h" |
||
10 | #include <sys/mman.h> |
||
11 | |||
12 | |||
13 | /*----------------------------------STATIC FUNCTIONS PROTOTYPES---------------------------------*/ |
||
14 | |||
15 | uint32_t read__value(uint32_t *addr); |
||
16 | void write_value (uint32_t *addr, uint32_t value); |
||
17 | |||
18 | |||
19 | |||
20 | |||
21 | |||
22 | |||
23 | |||
24 | |||
25 | |||
26 | |||
27 | |||
28 | /* -----------------------------------------STATIC (PRIVATE) FUNCTIONS----------------------------------*/ |
||
29 | |||
30 | |||
31 | uint32_t read_value(uint32_t *addr) //mogoce dodaj * |
||
32 | { |
||
33 | |||
34 | |||
35 | uint32_t result = 0; |
||
36 | |||
37 | result = *(addr); |
||
38 | |||
39 | return result; |
||
40 | } |
||
41 | |||
42 | |||
43 | |||
44 | void write_value(uint32_t *addr, uint32_t value) |
||
45 | { |
||
46 | *addr = value; |
||
47 | } |
||
48 | |||
49 | |||
50 | |||
51 | |||
52 | |||
53 | |||
54 | |||
55 | /*-----------------------------------------PUBLIC FUNCTIONS----------------------------------*/ |
||
56 | |||
57 | uint32_t *HW_init(void) |
||
58 | { |
||
59 | |||
60 | void *map_base = (void *)(-1); //naslov, kjer se mi zacne mapiran spomin |
||
61 | static void *virt_addr; |
||
62 | int fd = -1; //file descriptor |
||
63 | |||
64 | |||
65 | |||
66 | //odpiram svoj file |
||
67 | if ( (fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1 ) |
||
68 | { |
||
69 | perror("ERROR: open."); //sporoci napako, ce ni OK odprl |
||
70 | } |
||
71 | |||
72 | |||
73 | //mapiram memmory v zgoraj odprtem file-u in preverjam, ce se je OK odprl |
||
74 | 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 |
||
75 | |||
76 | if(map_base == (void *) -1) |
||
77 | { |
||
78 | perror("ERROR: mmap ni pravilno lociral spomina"); |
||
79 | } |
||
80 | |||
81 | printf("MAP_BASE: %p\n",map_base); |
||
82 | virt_addr = map_base + (HW_address & MAP_MASK); //ne vem, ce to potrebno, ker bi moral ze v map_base imeti pravi naslov |
||
83 | virt_addr = (uint32_t *)virt_addr; //castam na uint32_t, da ga read_value lahko sprejme |
||
84 | |||
85 | |||
86 | /* map base bi mi ze moral vrniti moj pravi HW_naslov, se pravi 0x40500000, ker |
||
87 | * v funkciji mmap dodam offset, kje naj locira pomnilnik: začne na NULL v /dev/mem, offset pa je HW_address & ~MAP_MASK |
||
88 | * kar je enako 0x40500000, maska poskrbi samo za to, da je spodnjih 12 bitov na 0, ker so memory blocks poravnani na |
||
89 | * začetek strani (glej tole: https://www.youtube.com/watch?v=XV5sRaSVtXQ at 5.20) |
||
90 | * |
||
91 | * |
||
92 | */ |
||
93 | |||
94 | |||
95 | |||
96 | return virt_addr; |
||
97 | } |
||
98 | |||
99 | |||
100 | |||
101 | |||
102 | |||
103 | uint8_t HW_comm ( HW_system *strct, uint32_t *reg_addr) |
||
104 | { |
||
105 | uint32_t HW_write = 0; |
||
106 | uint32_t HW_read = 0; |
||
107 | |||
108 | |||
109 | if ( (strct -> HW_config_flag) == 1 ) |
||
110 | { |
||
111 | //pisanje ukazov v register |
||
112 | HW_write = ( (strct -> start) | |
||
113 | ((strct -> reset) << 1) | |
||
114 | ((strct -> threshold) << 8) | |
||
115 | ((strct -> acq_window) << 19) | |
||
116 | ((strct -> sw_trigger) << 27) |
||
117 | ); |
||
118 | //*reg_addr = HW_write; |
||
119 | write_value(reg_addr, HW_write); |
||
120 | //printf("VPIS v HW:0x%08x\n",HW_write); |
||
121 | return 1; //sistem nastavljen |
||
122 | } |
||
123 | |||
124 | //branje histograma iz registra in shranjevanje podatkov v strukturo |
||
125 | else if ( (strct -> send_data_flag) == 1) |
||
126 | { |
||
127 | HW_read = read_value(reg_addr); |
||
128 | |||
129 | (strct -> UC_rd_en_current) = (HW_read & UC_rd_en_current_mask) >> 1; |
||
130 | (strct -> UC_rd_en_final) = (HW_read & UC_rd_en_final_mask); |
||
131 | (strct -> hist_data) = (HW_read & HIST_MASK) >> 8 ; |
||
132 | |||
133 | |||
134 | //printf("PREBRANI PODATKI:0x%08x\n",HW_read); |
||
135 | //printf("UC_rd_en_current: 0x%08x\n ",strct ->UC_rd_en_current); |
||
136 | |||
137 | |||
138 | |||
139 | return 2; //podatki pripravljeni za posiljanje v PC |
||
140 | } |
||
141 | |||
142 | else |
||
143 | return 0; |
||
144 | |||
145 | } |
||
146 | |||
147 | |||
148 | |||
149 | |||
150 | |||
151 | |||
152 | /* |
||
153 | |||
154 | //------------TRENUTNI HISTOGRAM----------- |
||
155 | for (int idx=0; idx<BIN_NUM; idx++) |
||
156 | { |
||
157 | |||
158 | HW_read = read_value(reg_addr); |
||
159 | |||
160 | (strct -> UC_rd_en_current) = HW_read & UC_rd_en_current_mask; |
||
161 | (strct -> UC_rd_en_final) = HW_read & UC_rd_en_final_mask; |
||
162 | |||
163 | |||
164 | if ( (HW_read & UC_rd_en_final_mask) == UC_rd_en_final_mask ) //prisel je koncni histogram, ne vpisuj vec v trenutnega |
||
165 | { |
||
166 | break; |
||
167 | } |
||
168 | else |
||
169 | { |
||
170 | //(strct -> hist_idx) = idx; |
||
171 | (strct -> hist_data+idx) = HW_read & HIST_MASK; |
||
172 | } |
||
173 | |||
174 | |||
175 | if(idx == BIN_NUM -1) |
||
176 | { |
||
177 | return 1; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | |||
182 | //------------------KONCNI HISTOGRAM---------------- |
||
183 | if ( (strct -> UC_rd_en_final) == 1 && (strct -> UC_rd_en_current) == 0 ) |
||
184 | { |
||
185 | (strct -> hist_data + 0) = HW_read & HIST_MASK; //vpis prvega podatka koncnega histograma |
||
186 | |||
187 | for (int idx=1; idx<BIN_NUM; idx++) |
||
188 | { |
||
189 | HW_read = read_value(reg_addr); |
||
190 | |||
191 | (strct -> UC_rd_en_current) = HW_read & UC_rd_en_current_mask; |
||
192 | (strct -> UC_rd_en_final) = HW_read & UC_rd_en_final_mask; |
||
193 | //(strct -> hist_idx) = idx; |
||
194 | (strct -> hist_data+idx) = HW_read & HIST_MASK; |
||
195 | } |
||
196 | |||
197 | |||
198 | if(idx == BIN_NUM -1) |
||
199 | { |
||
200 | return 1; |
||
201 | } |
||
202 | |||
203 | } |
||
204 | */ |
||
205 | |||
206 | |||
207 | // } |
||
208 | |||
209 | //} |
||
210 | |||
211 | |||
212 | |||
213 | |||
214 | |||
215 | |||
216 | |||
217 | |||
218 | |||
219 | |||
220 | |||
221 | |||
222 |