@@ -108,7 +108,7 @@ void CrorcDmaChannel::startPendingDma()
108108 return ;
109109 }
110110
111- if (mTransferQueue .size () < DMA_START_REQUIRED_SUPERPAGES ) {
111+ if (mTransferQueue .empty () ) { // We should never end up in here
112112 log (" Insufficient superpages to start pending DMA" );
113113 return ;
114114 }
@@ -126,15 +126,16 @@ void CrorcDmaChannel::startPendingDma()
126126 // Resetting the card,according to the RESET LEVEL parameter
127127 deviceResetChannel (mInitialResetLevel );
128128
129+ // TODO: Move datareceiving at deviceStartDma?
129130 // Setting the card to be able to receive data
130131 startDataReceiving ();
131132
132- // Initializing the firmware FIFO, pushing (entries) pages
133- for ( size_t i = 0 ; i < DMA_START_REQUIRED_SUPERPAGES; ++i){
134- getReadyFifoUser ()-> entries [i]. reset ();
135- auto superpage = mTransferQueue [i] ;
136- mReadyQueue . push_back ( );
137- pushFreeFifoPage (i, getBusOffsetAddress (superpage. getOffset ())) ;
133+ auto superpageToPush = mTransferQueue . front (); // Push the first superpage
134+ // After data receiving has begun
135+ for ( size_t j = 0 ; j < READYFIFO_ENTRIES; j++) {
136+ auto offset = superpageToPush. getOffset () + j * mPageSize ;
137+ pushFreeFifoPage (j, getBusOffsetAddress (offset) );
138+ mFifoSize = 128 ;
138139 }
139140
140141 if (mGeneratorEnabled ) {
@@ -155,22 +156,24 @@ void CrorcDmaChannel::startPendingDma()
155156 }
156157 }
157158
158- // / Fixed wait for initial pages TODO polling wait with timeout
159- std::this_thread::sleep_for (10ms);
160- if (dataArrived (READYFIFO_ENTRIES - 1 ) != DataArrivalStatus::WholeArrived) {
159+ std::this_thread::sleep_for (100ms);
160+
161+ // / Fixed wait for initial pages TODO polling wait with timeout
162+ while ((dataArrived (READYFIFO_ENTRIES - 1 ) != DataArrivalStatus::WholeArrived)) {
163+ std::this_thread::sleep_for (100ms);
161164 log (" Initial pages not arrived" , InfoLogger::InfoLogger::Warning);
162165 }
163166
164- for (size_t i = 0 ; i < DMA_START_REQUIRED_SUPERPAGES; ++i){
165- auto superpage = mTransferQueue .front ();
166- mReadyQueue .push_back (superpage);
167- mTransferQueue .pop_front ();
168- }
167+ // TODO: Move this read to fillSuperpages...
168+ auto superpageToReadout = mTransferQueue .front (); // Read the first Superpage
169+ mReadyQueue .push_back (superpageToReadout);
169170
170- getReadyFifoUser ()->reset ();
171- mFifoBack = 0 ;
171+ getReadyFifoUser ()->reset (); // Reset ReadyFifo
172+ mFifoBack = 0 ; // Reset FreeFifo references
172173 mFifoSize = 0 ;
173174
175+ mTransferQueue .pop_front (); // Pop the Superpage after resets; don't introduce race conditions
176+
174177 mPendingDmaStart = false ;
175178 log (" DMA started" );
176179}
@@ -317,11 +320,20 @@ void CrorcDmaChannel::pushSuperpage(Superpage superpage)
317320 BOOST_THROW_EXCEPTION (Exception ()
318321 << ErrorInfo::Message (" Could not push superpage, firmware queue was full (this should never happen)" ));
319322 }
323+
324+ if (mPendingDmaStart ) { // TODO: Don't let someone push more than one superpage at this stage
325+ // TODO: Return a "retry" flag?
326+ log (" DMA has not started, pushSuperpage only updates mTransferQueue" );
327+ mTransferQueue .push_back (superpage);
328+ return ;
329+ }
320330
331+ for (int i = 0 ; i < READYFIFO_ENTRIES; i++) { // TODO: *always* push 128 FIFO entries
332+ auto busAddress = getBusOffsetAddress (superpage.getOffset () + i * mPageSize );
333+ pushFreeFifoPage (i, busAddress);
334+ mFifoSize ++;
335+ }
321336 mTransferQueue .push_back (superpage);
322- auto busAddress = getBusOffsetAddress (superpage.getOffset ());
323- pushFreeFifoPage (getFifoFront (), busAddress);
324- mFifoSize ++;
325337}
326338
327339auto CrorcDmaChannel::popSuperpage () -> Superpage
@@ -337,7 +349,7 @@ auto CrorcDmaChannel::popSuperpage() -> Superpage
337349void CrorcDmaChannel::fillSuperpages ()
338350{
339351 if (mPendingDmaStart ) {
340- if (mTransferQueue .size () >= DMA_START_REQUIRED_SUPERPAGES ) {
352+ if (! mTransferQueue .empty () ) {
341353 startPendingDma ();
342354 } else {
343355 // Waiting on enough superpages to start DMA...
@@ -358,11 +370,13 @@ void CrorcDmaChannel::fillSuperpages()
358370 mFifoSize --;
359371 mFifoBack = (mFifoBack + 1 ) % READYFIFO_ENTRIES;
360372
361- auto superpage = mTransferQueue .front ();
362- superpage.setReceived (length);
363- superpage.setReady (true );
364- mReadyQueue .push_back (superpage);
365- mTransferQueue .pop_front ();
373+ if (mFifoSize == 0 ) { // Push the superpage after all of the 128 fifo entries are pushed
374+ auto superpage = mTransferQueue .front ();
375+ superpage.setReceived (mPageSize * READYFIFO_ENTRIES); // TODO: Always full pages?
376+ superpage.setReady (true );
377+ mReadyQueue .push_back (superpage);
378+ mTransferQueue .pop_front ();
379+ }
366380 } else {
367381 // If the back one hasn't arrived yet, the next ones will certainly not have arrived either...
368382 break ;
0 commit comments