@@ -337,6 +337,7 @@ void i2cInitFix(i2c_t * i2c){
337337 return ;
338338 }
339339 I2C_MUTEX_LOCK ();
340+ i2c -> dev -> ctr .trans_start = 0 ;
340341 i2cResetFiFo (i2c );
341342 i2c -> dev -> int_clr .val = 0xFFFFFFFF ;
342343 i2cSetCmd (i2c , 0 , I2C_CMD_RSTART , 0 , false, false, false);
@@ -347,7 +348,6 @@ void i2cInitFix(i2c_t * i2c){
347348 {
348349 log_e ("Busy at initialization!" );
349350 }
350- i2c -> dev -> ctr .trans_start = 1 ;
351351 uint16_t count = 50000 ;
352352 while ((!i2c -> dev -> command [2 ].done ) && (-- count > 0 ));
353353 I2C_MUTEX_UNLOCK ();
@@ -365,12 +365,20 @@ void i2cReset(i2c_t* i2c){
365365 I2C_MUTEX_UNLOCK ();
366366}
367367
368+ //** 11/2017 Stickbreaker attempt at ISR for I2C hardware
369+ // liberally stolen from ESP_IDF /drivers/i2c.c
370+ esp_err_t i2c_isr_free (intr_handle_t handle ){
371+
372+ return esp_intr_free (handle );
373+ }
374+
368375/* Stickbreaker ISR mode debug support
369376*/
377+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
370378#define INTBUFFMAX 64
371379static uint32_t intBuff [INTBUFFMAX ][3 ];
372380static uint32_t intPos = 0 ;
373-
381+ #endif
374382/* Stickbreaker ISR mode support
375383*/
376384static void IRAM_ATTR fillCmdQueue (i2c_t * i2c , bool INTS ){
@@ -546,7 +554,40 @@ if(INTS){ // don't want to prematurely enable fifo ints until ISR is ready to ha
546554
547555/* Stickbreaker ISR mode debug support
548556*/
549- static void IRAM_ATTR dumpI2c (i2c_t * i2c ){
557+ void i2cDumpDqData (i2c_t * i2c ){
558+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
559+ uint16_t a = 0 ;
560+ char buff [140 ];
561+ I2C_DATA_QUEUE_t * tdq ;
562+ while (a < i2c -> queueCount ){
563+ tdq = & i2c -> dq [a ];
564+ log_e ("[%d] %x %c %s buf@=%p, len=%d, pos=%d, eventH=%p bits=%x" ,a ,tdq -> ctrl .addr ,(tdq -> ctrl .mode )?'R' :'W' ,(tdq -> ctrl .stop )?"STOP" :"" ,tdq -> data ,tdq -> length ,tdq -> position ,tdq -> queueEvent ,(tdq -> queueEvent )?xEventGroupGetBits (tdq -> queueEvent ):0 );
565+ uint16_t offset = 0 ;
566+ while (offset < tdq -> length ){
567+ memset (buff ,' ' ,140 );
568+ buff [139 ]= '\0' ;
569+ uint16_t i = 0 ,j ;
570+ j = sprintf (buff ,"0x%04x: " ,offset );
571+ while ((i < 32 )&& (offset < tdq -> length )){
572+ char ch = tdq -> data [offset ];
573+ sprintf ((char * )& buff [(i * 3 )+ 41 ],"%02x " ,ch );
574+ if ((ch < 32 )|| (ch > 126 )) ch = '.' ;
575+ j += sprintf ((char * )& buff [j ],"%c" ,ch );
576+ buff [j ]= ' ' ;
577+ i ++ ;
578+ offset ++ ;
579+ }
580+ log_e ("%s" ,buff );
581+ }
582+ a ++ ;
583+ }
584+ #else
585+ log_n ("Enable Core Debug Level \"Error\"" );
586+ #endif
587+ }
588+
589+
590+ void i2cDumpI2c (i2c_t * i2c ){
550591log_e ("i2c=%p" ,i2c );
551592log_e ("dev=%p" ,i2c -> dev );
552593log_e ("lock=%p" ,i2c -> lock );
@@ -560,13 +601,7 @@ log_e("dq=%p",i2c->dq);
560601log_e ("queueCount=%d" ,i2c -> queueCount );
561602log_e ("queuePos=%d" ,i2c -> queuePos );
562603log_e ("byteCnt=%d" ,i2c -> byteCnt );
563- uint16_t a = 0 ;
564- I2C_DATA_QUEUE_t * tdq ;
565- while (a < i2c -> queueCount ){
566- tdq = & i2c -> dq [a ];
567- log_e ("[%d] %x %c %s buf@=%p, len=%d, pos=%d, eventH=%p bits=%x" ,a ,tdq -> ctrl .addr ,(tdq -> ctrl .mode )?'R' :'W' ,(tdq -> ctrl .stop )?"STOP" :"" ,tdq -> data ,tdq -> length ,tdq -> position ,tdq -> queueEvent ,(tdq -> queueEvent )?xEventGroupGetBits (tdq -> queueEvent ):0 );
568- a ++ ;
569- }
604+ if (i2c -> dq ) i2cDumpDqData (i2c );
570605}
571606
572607/* Stickbreaker ISR mode debug support
@@ -657,9 +692,12 @@ while((a < i2c->queueCount)&&!(full || readEncountered)){
657692
658693 if (full ) readEncountered = false; //tx possibly needs more
659694
695+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
696+
660697// update debug buffer tx counts
661698 cnt += intBuff [intPos ][1 ]>>16 ;
662699 intBuff [intPos ][1 ] = (intBuff [intPos ][1 ]& 0xFFFF )|(cnt <<16 );
700+ #endif
663701
664702 if (!(full || readEncountered )) a ++ ; // check next buffer for tx
665703 }
@@ -698,9 +736,11 @@ if(tdq->ctrl.mode==1) { // read
698736 moveCnt = (tdq -> length - tdq -> position );
699737 }
700738 }
739+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
701740// update Debug rxCount
702741 cnt += (intBuff [intPos ][1 ])&& 0xffFF ;
703742 intBuff [intPos ][1 ] = (intBuff [intPos ][1 ]& 0xFFFF0000 )|cnt ;
743+ #endif
704744 }
705745else {
706746 log_e ("RxEmpty(%d) call on TxBuffer? dq=%d" ,moveCnt ,i2c -> queuePos );
@@ -766,11 +806,12 @@ if(p_i2c->stage==I2C_DONE){ //get Out
766806 log_e ("eject int=%p, ena=%p" ,activeInt ,p_i2c -> dev -> int_ena .val );
767807 p_i2c -> dev -> int_ena .val = 0 ;
768808 p_i2c -> dev -> int_clr .val = activeInt ; //0x1FFF;
769- dumpI2c (p_i2c );
770- i2cDumpInts ();
809+ // i2cDumpI2c (p_i2c);
810+ // i2cDumpInts();
771811 return ;
772812 }
773813while (activeInt != 0 ) { // Ordering of 'if(activeInt)' statements is important, don't change
814+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
774815 if (activeInt == (intBuff [intPos ][0 ]& 0x1fff )){
775816 intBuff [intPos ][0 ] = (((intBuff [intPos ][0 ]>>16 )+ 1 )<<16 )|activeInt ;
776817 }
@@ -782,7 +823,7 @@ while (activeInt != 0) { // Ordering of 'if(activeInt)' statements is important,
782823 }
783824
784825 intBuff [intPos ][2 ] = xTaskGetTickCountFromISR (); // when IRQ fired
785-
826+ #endif
786827 uint32_t oldInt = activeInt ;
787828
788829 if (activeInt & I2C_TRANS_START_INT_ST_M ) {
@@ -891,12 +932,16 @@ while (activeInt != 0) { // Ordering of 'if(activeInt)' statements is important,
891932}
892933
893934void i2cDumpInts (){
935+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
894936uint32_t b ;
895937log_e ("row count INTR TX RX" );
896938for (uint32_t a = 1 ;a <=INTBUFFMAX ;a ++ ){
897939 b = (a + intPos )%INTBUFFMAX ;
898940 if (intBuff [b ][0 ]!= 0 ) log_e ("[%02d] 0x%04x 0x%04x 0x%04x 0x%04x 0x%08x" ,b ,((intBuff [b ][0 ]>>16 )& 0xFFFF ),(intBuff [b ][0 ]& 0xFFFF ),((intBuff [b ][1 ]>>16 )& 0xFFFF ),(intBuff [b ][1 ]& 0xFFFF ),intBuff [b ][2 ]);
899941 }
942+ #else
943+ log_n ("enable Core Debug Level \"Error\"" );
944+ #endif
900945}
901946
902947i2c_err_t i2cProcQueue (i2c_t * i2c , uint32_t * readCount , uint16_t timeOutMillis ){
@@ -920,11 +965,10 @@ I2C_MUTEX_LOCK();
920965*/
921966i2c -> stage = I2C_DONE ; // until ready
922967
923- for (intPos = 0 ;intPos < INTBUFFMAX ;intPos ++ ){
924- intBuff [intPos ][0 ]= 0 ;
925- intBuff [intPos ][1 ]= 0 ;
926- }
968+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
969+ memset (intBuff ,0 ,sizeof (intBuff ));
927970intPos = 0 ;
971+ #endif
928972
929973if (!i2c -> i2c_event ){
930974 i2c -> i2c_event = xEventGroupCreate ();
@@ -1046,7 +1090,7 @@ if(i2c->exitCode!=eBits){ // try to recover from O/S failure
10461090 }
10471091
10481092if (!(eBits == EVENT_DONE )&& (eBits & ~(EVENT_ERROR_NAK |EVENT_ERROR_DATA_NAK |EVENT_ERROR |EVENT_DONE ))){ // not only Done, therefore error, exclude ADDR NAK, DATA_NAK
1049- dumpI2c (i2c );
1093+ i2cDumpI2c (i2c );
10501094 i2cDumpInts ();
10511095 }
10521096
@@ -1081,7 +1125,7 @@ else { // GROSS timeout, shutdown ISR , report Timeout
10811125 reason = I2C_ERROR_TIMEOUT ;
10821126 eBits = eBits | EVENT_ERROR_TIMEOUT |EVENT_ERROR |EVENT_DONE ;
10831127 log_e (" Gross Timeout Dead st=0x%x, ed=0x%x, =%d, max=%d error=%d" ,tBefore ,tAfter ,(tAfter - tBefore ),ticksTimeOut ,i2c -> error );
1084- dumpI2c (i2c );
1128+ i2cDumpI2c (i2c );
10851129 i2cDumpInts ();
10861130 }
10871131
@@ -1119,8 +1163,8 @@ return reason;
11191163
11201164i2c_err_t i2cReleaseISR (i2c_t * i2c ){
11211165if (i2c -> intr_handle ){
1122- esp_err_t error = esp_intr_free (i2c -> intr_handle );
1123- // log_e("released ISR=%d",error);
1166+ esp_err_t error = i2c_isr_free (i2c -> intr_handle );
1167+ // log_e("released ISR=%d",error);
11241168 i2c -> intr_handle = NULL ;
11251169 }
11261170if (i2c -> i2c_event ){
0 commit comments