• AVR Freaks

AnsweredHot!reading 24c02 with mcc i2c library failed

Author
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
2020/01/20 08:53:45 (permalink)
5 (1)

reading 24c02 with mcc i2c library failed

Hi,
please can anyboby help me with track not working reading code of 24c02 eeprom?
Using mcc library The PIC10/PIC12/PIC16/PIC18.
Writing code looks good, with returning state completed, but writing fail.
18f47k40
I wrote test functions writeE2 and readE2
 
void writeE2()
{
    status = I2C2_MESSAGE_PENDING;
    uint16_t memAddress=0x10;
    uint8_t datArray[3];
    uint8_t addressEE=0x50;
    uint8_t dataEE=0x08;
    
// datArray[0]=(memAddress >> 8);
// datArray[1]=(uint8_t)(memAddress);
// datArray[2]=dataEE;
    datArray[0]=(uint8_t)(memAddress);
    datArray[1]=dataEE;
    
    I2C2_MasterWrite( datArray, 2, addressEE, &status);
    while(status == I2C2_MESSAGE_PENDING);
}

uint8_t readE2(uint8_t *memData)
{
status = I2C2_MESSAGE_PENDING;
uint16_t memAddress;
uint8_t datArray[2];
uint8_t retArray[2];
uint8_t addressEE=0x50;
uint8_t ret=0;
//uint8_t dataEE=0xcc;

memAddress=0x10;
timeOut=0;
// datArray[0]=(memAddress >> 8);
// datArray[1]=(uint8_t)(memAddress);
datArray[0]=(uint8_t)(memAddress);
datArray[1]=0x00; //dummy byte
while(status != I2C2_MESSAGE_FAIL)
{
I2C2_MasterWrite( datArray, 1, addressEE, &status);
while(status == I2C2_MESSAGE_PENDING);
if (status == I2C2_MESSAGE_COMPLETE){
ret=0xFF; //pokud je zápis OK
break;}
if (timeOut == SLAVE_I2C_GENERIC_RETRY_MAX){
ret= 1; //timeout
break; }
else
timeOut++;
}

if (status == I2C2_MESSAGE_COMPLETE)
{
timeOut=0;
while(status != I2C2_MESSAGE_FAIL)
{
I2C2_MasterRead( memData, 1, addressEE, &status);
while(status == I2C2_MESSAGE_PENDING);

if (status == I2C2_MESSAGE_COMPLETE)
{
ret=0xfe;
break;
}


if (timeOut == SLAVE_I2C_GENERIC_RETRY_MAX)
ret= 2; //timeout
else
timeOut++;
}
if (status == I2C2_MESSAGE_FAIL)
{
ret = 3; //read fail

}
}
return (ret);
}

 
thank you
post edited by flashman - 2020/01/20 08:58:23
#1
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/20 10:28:09 (permalink)
0
Hi
Code shown in message #1 is just a ripped out fragment, and is incomplete.
It cannot be compiled nor tested, without having to redo more work than you have already done.
 
There is some large Rewrite going on in MCC for I2C and external memory stuff,
so in addition to telling that you are using device library for: 
PIC10/PIC12/PIC16/PIC18 microcontrollers, 
You also have to tell which version of device library is used. 
 
I2C  code generated by PIC10/PIC12/PIC16/PIC18 v1.78.x
is very different from previous versions.
 
There is one thread here:   https://www.microchip.com/forums/FindPost/1118171
 
And another thread here, dealing with the earlier generation of driver code for I2C:
https://www.microchip.com...5868    PIC24
https://www.microchip.com...5254    PIC16 and PIC18
 
Could you be able to provide a complete problem program that may be debugged?
In MPLAB X  there is a 'Package'  facility to collect all files needed to restore a MPLAB project:
In 'Projects'  panel, the upper left part of MPLAB window,
point to root of project, Right-click and select 'Package' from the pop-up menu.
 
As a relatively new user, there are various limitations on messages in this Forum.
If you are not allowed to make attachment with the .zip file,
you may send a PM message with email address. Do not publish email address in open forum.
 
Regards,
    Mysil
#2
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/20 11:08:00 (permalink)
0
thank you for fast replay!
Sorry for my newbie question and fragment code.
I will look at the link and post the project as you pointed me.
Lukas
 
#3
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/20 23:02:25 (permalink)
+1 (1)
Hi,
Here is a test program using the code from message #1.
I do not have the same device 24C02C,
so example have been modified for EEPROM device with 2 memory address bytes.
 
There is different I2C driver code from what is created by MCC.
Application interface functions are the same as used for PIC16, PIC18, PIC24 and PIC32MX in thread linked and described in message #2 above. 
Driver code for I2C Master in attached project, is a simplified, slimmed version of code published in the other thread.
Driver code wait for each transfer to complete before returning to the calling program.
This may be preferable when used on small PIC16 or PIC18 microcontroller with limited memory and small hardware stack.
There are functions available to perform combined Write Restart and Read operations,
these may be useful when writing or reading EEPROM data.
 
If a EEPROM write operation is beeing performed, I2C slave will not respond.
Code will then wait a millisecond and try again.
 
There are a number of warnings during compilation, due to functions that are not used.
 
Regards,
    Mysil

Attached Image(s)

#4
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/20 23:40:27 (permalink)
0
Hi, there is some more info:
MCC v3.85.1
library 1.77
driver version 2.02
compiler 2.10 C99
MPLAB 5.30
MCU 18F47K40
 
Project background:
This is a testing project, 6 touch button with backlight, 2x40 LCD.
Next I tryed TEA5767 with no luck, I thought it could be bad chip.
Even with RTC1302 wasn't success. But there is different type of bus. -> this is another story :-)
At the end I started to make it siple as I can, and testing i2c on really basic chip 24c02, that must work.
Now I have working buttons with testing menu on lcd, eeprom read/write is run from process_button, and show state and return (readed) value from eeprom.
 
There is a lot of warnings in compile process, and I know, there is a lot of unclean code, but this is still very testing version.
 
Thank you
Lukas
 
#5
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/21 12:10:30 (permalink)
0
ok, i switched to 1.78.0 library, driver looks all rewritten.
But with your driver I dont need it, so now is replaced from my project.
 
sorry for my newbie question, but you can point me to the best way how to do it:
How can I manage sda scl pins to another pins I choose by pin manager (when no libraray driver selected, sda and scl is not in pin manager table)?
Can it be used for another peripherial like RTC DS1307 or TEA5767? Is there any eeprom specific timing in your driver?
I think, it must be the interrupt stop during the I2c communication, is it right?
 
Thank you
Lukas
#6
ric
Super Member
  • Total Posts : 25592
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: reading 24c02 with mcc i2c library failed 2020/01/21 12:20:13 (permalink)
0
flashman
I think, it must be the interrupt stop during the I2c communication, is it right?

Why?
I2C does not care if the Master pauses for short periods during the transfers.
All I2C timing comes from the Master's SCL clock.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/21 12:52:09 (permalink)
0
oki, I will test.
thank for your replay
Lukas
#8
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/24 08:57:53 (permalink)
0
Hi,
About generating macro definitions for I/O pins  in MCC pin manager:
The I2C code provided in message #4  contain a function to clear/unjam the I2C bus,
in case a previous I2C Read operation have been aborted.
This depend upon 'Custom Name's defined in Pin Manager:  SDA1 and SCL1   or:   SDA2  and   SCL2
To be able to define Custom Names,  select the pins to be used as GPIO Input pins, in Pin manager Grid view.
 
The easiest way to do pin Remapping for I2C, is to select and setup MCC to generate it's own MSSP driver for I2C.
Then when you use the code I have provided,
point to the source file generated by MCC in MPLAB X Project View, Right-Click,
and select: 'Exclude file(s) from current configuration' for those files that are not needed.
Then you may have to make adjustments in:    mcc_generated_files\mcc.h,
   and in: mcc_generated_files\mcc.c
 
Yes, it will be possible to use the same I2C driver code for DS1307 and  TEA5767 
There is nothing in the I2C  driver code that restrict it to EEPROM only.
In application interface code for EEPROM,  writeE2_readE2.c  I did insert a 1 millisecond delay between Retry attempts.
This is for EEPROM devices, that will need between 2 and 5 milliseconds to perform internal write operation,
and do not respond to calls during that time.
If you write to EEPROM immediately after each other, there may be some retry attempts before the next operation.
Same if you try to Read for verification after a Write.
There is no problem with such a wait between retry attempts in code for other devices. 
 
I may try to make some more contributions based on your code,
but do not have the same hardware, so have used some time bypassing the menu and display code.
 
EEPROM example code in MCC header file for I2C is not good.
Real EEPROM devices have Page write buffer boundaries that need to be taken into account.
I will come up with something better, hopefully within a week.
 
Regards,
    Mysil
#9
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/24 15:02:18 (permalink)
0
Hi,
I want to thank you very much to spend your time on this so basic questions!
As I want to learn, I tracking sample codes and reading forums for related topics, but sometimes is tricky for me to solve some problems only by googling. So I appreciate people like you!
 
I look at your project in mcc pin manager and understand to define this pins simply like input and name it, as you explained.
 
Trying to import your code, but my timer2 is used for touch buttons, so I try to use timer1.
Cannot realize your timer setup in mcc, is not configured by mcc.
 
I traying to understand flow code from timer isr, but cannot figurate the chain from timer isr to your driver.
I understand that the timer start counting CountCallBack and when reach TMR2_INTERRUPT_TICKER_FACTOR, call TMR2_CallBack, but I cannot see any relation to your driver code.
 
Can you, please, help me a little bit understand it?
 
Thank you
Lukas
#10
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/24 18:05:43 (permalink)
0
Hi,
You may rather use Timer4 as replacement for Timer2.
Timer1 and Timer3 are different from Timer2 in various ways.
Timer2, Timer4 and Timer6 are identical, so will be easier to substitute.
 
The use of TMR2_CallBack  is  mostly for the non-blocking  interrupt version of the driver,
I2C_Master_SSP.c 
 
In I2C2_Master_SSW2.c  I think the CallBack is not used. 
 
There is also the  global variable:  msTick      /* Global millisecond tick counter. */
that is used for some Retry and time check procedures. 
There is a macro definition that camouflage the msTick  millisecond counter, 
to look like a function call:
  #define    Tick()            msTick        /* Software Tick counter. */
so if msTick  is not incremented at all, program may hang.
 
Regards,
    Mysil
#11
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/26 07:22:03 (permalink)
0
Hi,
now I'm trying to use your driver with timer4, and port code for it.
Set timer4 in mcc for 1ms period, callback same 1ms, isr enabled.
 
I'm in troulbes when compile code with error
main.c:84:30: error: use of undeclared identifier 'TMR4_Interrupt'
    TMR4_SetInterruptHandler(TMR4_Interrupt);
                                           ^
TMR4_Interrupt defined in Timer4c and included, but error remain.
Cannot catch the problem.
I cannot figurate what I miss.
 
Thank you
Lukas
#12
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/26 13:42:23 (permalink)
0
Hi,
Can you please try the modified program attached:
Files  tmr4.h  and  Timer4.h  had the same include-guard macro symbol,  
and seem to have been the major reason for your problem.
 
There was a wrong macro test in I2C2_Master_SSW2.c
Causing code that work on PIC18F26K22 or PIC18F46K40, to fail on PIC18F47K40 or any other device.
For use of I2C2_Master... functions using peripheral MSSP2,
Both macro definitions:  I2Cx_Master_MSSP_Channel   2
and macro definition:    I2C_INSTANCE               2      //  shall have value 2.


To be able to test setup of Timer4  in Simulator,
Sequence of I2C2_Master_Initialize();   and TMR4_SetInterruptHandler(...)  have been changed.
This should not matter when running on a real microcontroller.
 
Regards,
    Mysil
#13
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/27 01:24:13 (permalink)
0
Thank you,
yes, it's perfectly make sense now, why it's not defined.
I will try it on HW PIC today.
L.
#14
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/27 12:54:15 (permalink)
0
Hi, tested today.
I ran into problem, PIC reseting every timer isr called.
After renaming TMR4 prrocedures in TIMER4.c timer looks working now.
 
But cannot complete read write eeprom procedure.
It stay in loop in I2C_MasterAddress do while.
Looks like cannot pass I2CM_Start.
 
Maybe I miss something in timer 4 setup.
By mcc I set FOSC/4, 1ms period.
As I understand timer4 fired isr every 1ms and msTick counts up every period (1ms)?
I will test output lines hook on timer4 isr to know it's output as expected.
 
And about addressing eeprom, it's 7bit (101 0000), so do I use 0x50 or shifted 8bit 0xA0 (1010 0000)?
I was try both, but with same result.
Thank you
Lukas
#15
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/27 14:30:41 (permalink) ☼ Best Answerby flashman 2020/01/30 09:19:41
+1 (1)
Hi,
Could you try to observe PORTD register in debugger Watch, before and after I2CM_Start ?
Also please measure signal level on SDA2 and SCL2 signal lines, before and after Start.
 
Then values of registers SSP2STAT, SSP2CON1, SSP2CON2, SSP2CON3 and SSP2ADD.   
 
Hanging in Start procedure may be caused by a mistake in Port setup, 
or by a wiring problem.
Pins to be used for I2C signals shall be digital input, and not in analog mode.
 
I will  review  PPS settings in Pin_Manager...
There it is: PPS settings for I2C pins are missing!
There should be PPS settings for SDA and SCL signals, both Input and Output direction for both pins.
There are default settings for Input direction, Output direction must be setup in code.
They were correct in your previous package in message #5:
    RD2PPS = 0x12;   //RD2->MSSP2:SDA2;         /* SDA Output. */
    RX1PPS = 0x17;   //RC7->EUSART1:RX1;    
    SSP2DATPPS = 0x1A;   //RD2->MSSP2:SDA2;     /* SDA Input. */
    RD1PPS = 0x11;   //RD1->MSSP2:SCL2;         /* SCL Output. */
    RA2PPS = 0x07;   //RA2->PWM3:PWM3;    
    RC6PPS = 0x09;   //RC6->EUSART1:TX1;    
    SSP2CLKPPS = 0x19;   //RD1->MSSP2:SCL2;     /* SCL Input. */

 
I2C 7-bit address 0x50 is good for addressing EEPROM.
Regardless of what I2C address is used, program shall return to writeE2  function,
with some value in   status variable. 
Either I2C_MESSAGE_COMPLETE  0x84, I2C_ERROR_ADDRESS_NACK  0x86, or something else.
 
Regards,
    Mysil
#16
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/30 09:19:24 (permalink)
0
Hi,
sorry for my mistake,
It's working now!!!
It's safe to remove i2c2 driver, generated by mcc from project, since I only need i2c setting?
 
Thank you!
Lukas
#17
Mysil
Super Member
  • Total Posts : 3642
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/30 10:07:03 (permalink)
+1 (1)
Hi,
You may remove i2c2.c and i2c2.h driver files from project, and delete the files from disk,
but I usually use 'Exclude file(s) from current configuration' and leave the files generated by MCC in place.
You must anyway search for any #include i2c2.h   // and remove or change those lines.
 
    Mysil
 
#18
flashman
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2015/07/11 00:32:53
  • Location: 0
  • Status: offline
Re: reading 24c02 with mcc i2c library failed 2020/01/30 10:11:47 (permalink)
0
my I2C world is open now :-)
Big thank, for your work and help!!!
Lukas
#19
Jump to:
© 2020 APG vNext Commercial Version 4.5