6 #include <rtsSystems.h>
7 #include <daqFormats.h>
14 static int adcReader(
int sec,
struct PMDADCD *adcd) ;
15 static int pedReader(
int sec,
int type,
struct PMDPEDR *pedr) ;
18 int pmd_reader(
char *m,
struct pmd_t *pmd, u_int driver)
22 struct PMDP *pmdp = 0;
32 int start_sec, stop_sec ;
40 if(pmd_p->max_channels) ;
43 pmd_p->max_channels = 2*PMD_CRAMS_MAX*2*PMD_CRAMS_CH_MAX ;
45 pmd_p->status[0] = pmd_p->status[1] = 0 ;
48 LOG(DBG,
"PMD reader: %p: sector %d",datap,driver) ;
50 if(datap == NULL)
return 0 ;
54 start_sec = stop_sec = driver - 1 ;
64 if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
66 off = qswap32(swapdatap, datap->det[EXT_ID].len) ;
67 if(off == 0)
return 0 ;
69 off = qswap32(swapdatap, datap->det[EXT_ID].off) ;
72 datapx = (
struct DATAPX *)(m + off*4) ;
75 if(checkBank(datapx->bh.bank_type, CHAR_DATAPX) < 0) {
80 if(datapx->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatapx = 1;
82 len = qswap32(swapdatapx, datapx->det[PMD_ID-10].len) * 4 ;
84 if(len == 0)
return 0 ;
87 off = qswap32(swapdatapx, datapx->det[PMD_ID-10].off)*4 ;
89 pmdp = (
struct PMDP *)((
char *)datapx + off) ;
92 if(checkBank(pmdp->bh.bank_type, CHAR_PMDP) < 0) {
101 for(sec=start_sec;sec<=stop_sec;sec++) {
104 LOG(DBG,
"PMD %d, len %d, offset %u",sec,b2h32(pmdp->sec[sec].len),b2h32(pmdp->sec[sec].off),0,0) ;
106 if(pmdp->sec[sec].len == 0) continue ;
109 secp = (
struct PMDSECP *)((
char *)pmdp + b2h32(pmdp->sec[sec].off)*4) ;
112 if(checkBank((
char *)secp,CHAR_PMDSECP) < 0)
return -1 ;
114 pmd_p->status[sec] = b2h32(secp->bh.format_number) ;
116 for(type=0;type<4;type++) {
118 if(secp->type[type].len == 0) continue ;
122 adcd = (
struct PMDADCD *)((
char *)secp + b2h32(secp->type[type].off)*4) ;
123 if(checkBank(adcd->bh.bank_type, CHAR_PMDADCD) < 0)
return -1 ;
125 ret = adcReader(sec, adcd) ;
126 if(ret < 0)
return -1 ;
128 pmd_p->channels += ret ;
132 pedr = (
struct PMDPEDR *)((
char *)secp + b2h32(secp->type[type].off)*4) ;
133 if(checkBank(pedr->bh.bank_type, CHAR_PMDPEDR) < 0)
return -1 ;
135 ret = pedReader(sec, type, pedr) ;
136 if(ret < 0)
return -1 ;
140 pmd_p->channels += ret ;
145 rmsr = (
struct PMDRMSR *)((
char *)secp + b2h32(secp->type[type].off)*4) ;
146 if(checkBank(rmsr->bh.bank_type, CHAR_PMDRMSR) < 0)
return -1 ;
148 ret = pedReader(sec, type, (
struct PMDPEDR *)rmsr) ;
149 if(ret < 0)
return -1 ;
156 thrr = (
struct PMDTHRR *)((
char *)secp + b2h32(secp->type[type].off)*4) ;
157 if(checkBank(thrr->bh.bank_type, CHAR_PMDTHRR) < 0)
return -1 ;
159 ret = pedReader(sec, type, (
struct PMDPEDR *)thrr) ;
160 if(ret < 0)
return -1 ;
175 static int adcReader(
int sec,
struct PMDADCD *adcd)
177 int items = b2h32(adcd->bh.length) -
sizeof(adcd->bh)/4 ;
184 if(items <= 0)
return 0 ;
191 LOG(DBG,
"ADC: PMD sec %d: items %d",sec,items,0,0,0) ;
193 u_int *end_datum = datum + items ;
195 for(end_datum=datum+items;datum<end_datum;) {
196 u_int tmp = b2h32(*datum) ;
200 rb = (tmp & 0xFF000000) >> 24 ;
201 mz = (tmp & 0x00FF0000) >> 16 ;
202 ch = (tmp & 0x0000FFFF) ;
207 if(rb >= PMD_CRAMS_MAX) {
208 LOG(ERR,
"PMD: Bad RB number %d",rb,0,0,0,0) ;
212 if((mz != 0) && (mz != 1)) {
213 LOG(ERR,
"PMD: Bad mezzanine %d in RB %d",mz,rb,0,0,0) ;
217 if(ch > PMD_CRAMS_CH_MAX) {
218 LOG(ERR,
"PMD: too many channels in RB %d, MZ %d: %d",rb,mz,ch,0,0) ;
222 LOG(DBG,
"PMD ADCD: sec %d, rb %d, mz %d, channels %d",sec,rb,mz,ch,0) ;
227 val = b2h32(*datum) ;
231 if(val & 0x40000000) {
232 u_int channel = (val & 0x007ff000) >> 12 ;
237 if(channel >= PMD_CRAMS_CH_MAX) {
238 LOG(ERR,
"PMD: channel too big %d in RB %d, MZ %d: %d",rb,mz,channel,0,0) ;
242 pmd_p->adc[sec][rb][mz][channel] = val ;
246 static const int maxCounts = 13;
247 static int Counts = 0;
248 if (Counts < maxCounts) {
250 LOG(ERR,
"PMD %d: RB %d, MZ %d: illegal value in channel %d: 0x%08X",sec,rb,mz,j,val) ;
251 if (Counts == maxCounts) {LOG(ERR,
"PMD stop print out after %i counts",Counts) ;}
260 static int pedReader(
int sec,
int type,
struct PMDPEDR *pedr)
269 items = (b2h32(pedr->bh.length) -
sizeof(pedr->bh)/4)*2 ;
271 if(items <= 0)
return 0 ;
280 u_short *end_datum = datum + items ;
282 for(end_datum=datum+items;datum<end_datum;) {
284 u_int tmp = b2h16(*datum) ;
287 rb = (tmp & 0x7F00) >> 8 ;
288 mz = (tmp & 0x00FF) ;
290 valid = tmp & 0x8000 ;
293 ch = PMD_CRAMS_CH_MAX ;
296 LOG(WARN,
"Type %d: datum not valid at %u == 0x%04X",type,datum-pedr->data, (u_int)tmp,0,0) ;
303 if(rb >= PMD_CRAMS_MAX) {
304 LOG(ERR,
"PMD: Bad RB number %d",rb,0,0,0,0) ;
308 if((mz != 0) && (mz != 1)) {
309 LOG(ERR,
"PMD: Bad mezzanine %d in RB %d",mz,rb,0,0,0) ;
313 LOG(DBG,
" %d: SEC %d, RB %d, MZ %d: channel count %d...",type,sec,rb,mz,ch) ;
318 for(channel=0;channel<ch;channel++) {
332 pmd_p->ped[sec][rb][mz][channel] = val ;
335 pmd_p->rms[sec][rb][mz][channel] = val ;
338 pmd_p->thr[sec][rb][mz][channel] = val ;