• MATLAB
  • i2c error - does not build
2020/10/20 09:30:20
Poley
Hi,
 
I am trying to use the i2c block in my model however when I try to build I get the following error:
 
MCHP_I2C1_Interrupt_data.o
MCHP_I2C1_Interrupt.c: In function '_MI2C1Interrupt':
MCHP_I2C1_Interrupt.c:66:45: error: 'I2C1STATLbits' undeclared (first use in this function) MCHP_I2C1_Interrupt.c:66:45: note: each undeclared identifier is reported only once for each function it appears in
 
Has anyone seen this?
 
Thank you
 
2020/10/20 09:47:05
KTrenholm
Well you're referencing a structure for the I2C1STATL register that appears to not exist for one reason or another.
 
What PIC specifically are you using?  Can you confirm if it has a I2C1STATL register from the datasheet?
If it does have a I2C1STATL register, are you including xc.h for these register struct definitions?
 
Need more info (like what PIC you are using and what compiler/toolchain you are using) to help any further.
2020/10/20 09:49:36
Poley
I am using dsPIC33EV256GM106.
 
It does not appear to have I2C1STATL but it does have I2C1STAT
 
Thanks
2020/10/20 09:55:24
KTrenholm
Is it possible that the code you have was written for a different PIC?  It sounds like whatever it was written for splits I2CSTAT into high and low registers.
 
You may need to convert from whatever PIC this was written for to the dsPIC you are using.
This would mean finding the equivalent bit/register on your part and adjusting your code accordingly.
 
Can you post the code from your _MI2C1Interrupt?
 
Also I just noticed this is the MATLAB board.  I'm not very knowledgeable on MATLAB (and I've never used it with a PIC), so not sure how much actual help I will be able to give.
2020/10/20 10:01:24
Poley
Yeah sure, it is passed the matlab build so it is the code side luckily
 
/* I2C - Interrupt: <S50>/BUS I2C MASTER2 */
/* Implement I2C 1 Interrupts */
void __attribute__((__interrupt__,__shadow__,__no_auto_psv__)) _MI2C1Interrupt
  (void) /* MI2C1 ISR */
{
  boolean_T Continue;
  _MI2C1IF = 0; /* Clear I2C1 Master Interrupt */

  /* Declaration of Variables */
  do {
    Continue = 0; /* By default, execute one sequence only */
    switch (MCHP_I2C1_State)
    {
     case 0: /* Idle */
      /* Check if the next sequence is already in the queue. If so, start it */
      Continue = (MCHP_I2C1_Queue.head != MCHP_I2C1_Queue.tail);
                                    /* One next sequence queued, start it now */
      if (Continue) {
        MCHP_I2C1_State = MCHP_I2C1_Queue.buffer[MCHP_I2C1_Queue.tail];
        if (MCHP_I2C1_Queue.tail >= 3)
                           /* There are 3 I2C1 blocks, max idx for queue is 3 */
          MCHP_I2C1_Queue.tail = 0;
        else
          MCHP_I2C1_Queue.tail++;
      }
      break;

     case 1:
          /* Problem: try Reset of I2C BUS : Repeated Start sequence and Stop */
      TRISBbits.TRISB5 = 1;/* Set I2C Port as Port Input (better for Fj chips)*/
      TRISBbits.TRISB6 = 1;
      I2C1CONLbits.I2CEN = 1; /* Re-Enable I2C*/
      I2C1STAT &= 65526; /* Reset I2C State */
      I2C1CONLbits.RSEN = 1; /* I2C: Restart */
      MCHP_I2C1_State++;
      break;

     case 2: /* Problem: try Reset of I2C BUS : Stop sequence*/
      I2C1STAT &= 65526;
      /* Reset I2C Receive Overflow and I2C Collision during Write or Master operation */
      I2C1CONLbits.PEN = 1; /* I2C: STOP */
      MCHP_I2C1_State++;
      break;

     case 3:
      I2C1STAT &= 65526;
      /* Reset I2C Receive Overflow and I2C Collision during Write or Master operation */
      MCHP_I2C1_State = 0;
      Continue = (MCHP_I2C1_Queue.head != MCHP_I2C1_Queue.tail);/* Any pending sequence ? */
      break;

     case 4: /* Start a new I2C Sequence */
      I2C1CONLbits.SEN = 1; /* I2C: START */
      MCHP_I2C1_State++;
      break;

     case 5:
      I2C1TRN = 0xA0; /* Send I2C Address : A7 A6 A5 A4 A3 A2 A1 A0 RW */
      MCHP_I2C1_State++;
      break;

     case 6:
      MCHP_I2C13_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = 0x1F; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 7:
      MCHP_I2C13_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = 0xF0; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 8:
      MCHP_I2C13_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1CONLbits.RSEN = 1; /* I2C: REPEATED START */
      MCHP_I2C1_State++;
      break;

     case 9:
      I2C1TRN = 0xA1; /* Send I2C Address : A7 A6 A5 A4 A3 A2 A1 A0 RW */
      MCHP_I2C1_State++;
      break;

     case 10:
      MCHP_I2C13_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1CONLbits.RCEN = 1; /* I2C: Receive */
      MCHP_I2C1_State++;
      break;

     case 11:
      I2C13_Buff8[0] = I2C1RCV; /* I2C : Read data */
      I2C1CONLbits.ACKDT = 0; /* set to ACK */
      I2C1CONLbits.ACKEN = 1; /* Start Acknowledge sequence */
      MCHP_I2C1_State++;
      break;

     case 12:
      I2C1CONLbits.RCEN = 1; /* I2C: Receive */
      MCHP_I2C1_State++;
      break;

     case 13:
      I2C13_Buff8[1] = I2C1RCV; /* I2C : Read data */
      I2C1CONLbits.ACKDT = 0; /* set to ACK */
      I2C1CONLbits.ACKEN = 1; /* Start Acknowledge sequence */
      MCHP_I2C1_State++;
      break;

     case 14:
      I2C1CONLbits.RCEN = 1; /* I2C: Receive */
      MCHP_I2C1_State++;
      break;

     case 15:
      I2C13_Buff8[2] = I2C1RCV; /* I2C : Read data */
      I2C1CONLbits.ACKDT = 0; /* set to ACK */
      I2C1CONLbits.ACKEN = 1; /* Start Acknowledge sequence */
      MCHP_I2C1_State++;
      break;

     case 16:
      I2C1CONLbits.RCEN = 1; /* I2C: Receive */
      MCHP_I2C1_State++;
      break;

     case 17:
      I2C13_Buff8[3] = I2C1RCV; /* I2C : Read data */
      I2C1CONLbits.ACKDT = 1; /* set to NACK */
      I2C1CONLbits.ACKEN = 1; /* Start Acknowledge sequence */
      MCHP_I2C1_State++;
      break;

     case 18:
      I2C1CONLbits.PEN = 1; /* I2C: STOP */
      MCHP_I2C1_State++;
      break;

     case 19:
      MCHP_I2C13_SequenceCompleted += 1;
                                    /* is 1 only if no NACK were encountered. */
      MCHP_I2C13_Request = 0; /* This I2C sequence could be Re-Enabled */
      MCHP_I2C1_State = 0;
      Continue = (MCHP_I2C1_Queue.head != MCHP_I2C1_Queue.tail);
                                    /* One next sequence queued, start it now */
      break;

     case 20: /* Start a new I2C Sequence */
      I2C1CONLbits.SEN = 1; /* I2C: START */
      MCHP_I2C1_State++;
      break;

     case 21:
      I2C1TRN = 0xA0; /* Send I2C Address : A7 A6 A5 A4 A3 A2 A1 A0 RW */
      MCHP_I2C1_State++;
      break;

     case 22:
      MCHP_I2C11_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C11_Buff8[0]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 23:
      MCHP_I2C11_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C11_Buff8[1]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 24:
      MCHP_I2C11_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C11_Buff8[2]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 25:
      MCHP_I2C11_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C11_Buff8[3]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 26:
      MCHP_I2C11_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1CONLbits.PEN = 1; /* I2C: STOP */
      MCHP_I2C1_State++;
      break;

     case 27:
      MCHP_I2C11_SequenceCompleted += 1;
                                    /* is 1 only if no NACK were encountered. */
      MCHP_I2C11_Request = 0; /* This I2C sequence could be Re-Enabled */
      MCHP_I2C1_State = 0;
      Continue = (MCHP_I2C1_Queue.head != MCHP_I2C1_Queue.tail);
                                    /* One next sequence queued, start it now */
      break;

     case 28: /* Start a new I2C Sequence */
      I2C1CONLbits.SEN = 1; /* I2C: START */
      MCHP_I2C1_State++;
      break;

     case 29:
      I2C1TRN = 0xA0; /* Send I2C Address : A7 A6 A5 A4 A3 A2 A1 A0 RW */
      MCHP_I2C1_State++;
      break;

     case 30:
      MCHP_I2C12_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C12_Buff8[0]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 31:
      MCHP_I2C12_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C12_Buff8[1]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 32:
      MCHP_I2C12_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C12_Buff8[2]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 33:
      MCHP_I2C12_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1TRN = I2C12_Buff8[3]; /* I2C : Write data */
      MCHP_I2C1_State++;
      break;

     case 34:
      MCHP_I2C12_SequenceCompleted -= (int) I2C1STATLbits.ACKSTAT;
                           /* decrement on NACK: sequence completed remains 0 */
      I2C1CONLbits.PEN = 1; /* I2C: STOP */
      MCHP_I2C1_State++;
      break;

     case 35:
      MCHP_I2C12_SequenceCompleted += 1;
                                    /* is 1 only if no NACK were encountered. */
      MCHP_I2C12_Request = 0; /* This I2C sequence could be Re-Enabled */
      MCHP_I2C1_State = 0;
      Continue = (MCHP_I2C1_Queue.head != MCHP_I2C1_Queue.tail);
                                    /* One next sequence queued, start it now */
      break;

     default: /* Sequence finished */
      MCHP_I2C1_State = 0; /* Should never happend */
      break;
    } /* End of switch case sequence*/
  } while (Continue == 1); /* end do-While */
} /* Enf of interrupt */

 
Thanks 
 
2020/10/20 10:07:37
KTrenholm
OK it looks like a number of them correspond to I2C1CON1.
 
Try:
I2C1CONLbits.I2CEN -> I2C1CON1bits.I2CEN
I2C1CONLbits.RSEN -> I2C1CON1bits.RSEN
etc.
2020/10/20 10:18:16
Poley
I haven't got a clue how to edit the simulink block that produces this .c file to make that change (It is auto generated).
 
Looks like it's when I request a acknowledgement that makes it throw the error. I don't have these bits 
I2C1CONLbits.ACKDT = 0; /* set to ACK */
I2C1CONLbits.ACKEN = 1; /* Start Acknowledge sequence */ 
 
for any I2C1CON#
2020/10/20 11:05:39
KTrenholm
ACKDT and ACKEN should be bits 5 and 4 of I2C1CON1 according to the datasheet
http://ww1.microchip.com/...eviceDoc/70005144e.pdf
I2C Registers begin at page 231.
 
Afraid I can't help much with how to change how simulink generates its code unfortunately.
2020/10/20 11:46:09
Poley
Ah yeah you're right, it does indeed have those included, it must be the wat that file is generated then. 
 
Hopefully Lubin sees this and could offer some insight on how to change I2C1CONL into I2C1CON1
 
Thanks for the help
2020/10/20 11:48:03
Poley
Actually it does have this in the datasheet, even weirder that it's not working.
 
So it is definitely I2C1STATL that is causing the issue (Should be I2C1STAT from the datasheet)
 
#define I2C1CONL I2C1CONL
extern volatile uint16_t I2C1CONL __attribute__((__sfr__));
typedef struct tagI2C1CONLBITS {
  uint16_t SEN:1;
  uint16_t RSEN:1;
  uint16_t PEN:1;
  uint16_t RCEN:1;
  uint16_t ACKEN:1;
  uint16_t ACKDT:1;
  uint16_t STREN:1;
  uint16_t GCEN:1;
  uint16_t SMEN:1;
  uint16_t DISSLW:1;
  uint16_t A10M:1;
  uint16_t STRICT:1;
  uint16_t SCLREL:1;
  uint16_t I2CSIDL:1;
  uint16_t :1;
  uint16_t I2CEN:1;
} I2C1CONLBITS;
extern volatile I2C1CONLBITS I2C1CONLbits __attribute__((__sfr__));

12
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account