• AVR Freaks

Helpful ReplyHot!XC8/MCC I2C Foundation Services Library Documentation and/or Examples

Page: 12 > Showing page 1 of 2
Author
btribley
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2018/04/13 14:43:12
  • Location: 0
  • Status: offline
2018/04/13 21:23:38 (permalink)
5 (1)

XC8/MCC I2C Foundation Services Library Documentation and/or Examples

MCC version 3.55.1     
MPLAB X version: 4.05
Operating System and version:   MAC OS X 10.13.3)
Compiler version: XC8 1.45
Device name:  PIC16F1847


I am having big-time trouble getting MSSP1 I2C Master working. I have almost all other peripherals down cold. I tried the generic MSSP1(pic10/pic12/pic16/pic18 MCUs by Microchip Technology Inc. without success, both I2C lines high and interrupts not firing. I have 5.6K pullups, no off-board cables, verified no shorts and all I2C hardware is connected, SDA and SCK are going to the correct peripheral chip pins. I can drive the pins high and low independently if configured as outputs.  Found the post describing the bug in the interrupt kickoff, interrupts fire now but still no data. Reading from single byte input expanders, so it's a simple read transaction, nothing like a EEPROM where you have to send the address of the data to read, this is just tell the hardware to send the one and only byte it has. I verified that the chips have unique addresses using a logic probe on the address pins themselves (not the PCB pad), all OK.  I tried coding up a plain bitbanger that does not use the MSSP1 which works unreliably. 
 
So I want to try the Foundation Services Library. Not one comment in the generated code. It's obvious that some kind of callback is needed. I have not found one shred of documentation or an example so I am at a loss as to how to use this. 
 
I am just about at the point of trying to read and figure out how to manually run the MSSP1 hardware. This is going to be bad, I would rather run a Microchip library that hides the details of wrangling the hardware.
 
Any help appreciated!
Best regards,
Bill
#1
Maldus
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2016/08/17 09:55:57
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 09:15:39 (permalink)
0
This is still relevant. I would like to know as well how to use said library.
 
I have my own I2C eeprom library that builds itself on the basic I2C operations (start, stop, send byte, read byte) and I'll need to drastically restructure it if I am forced to use the generic one.
 
What's the point of writing a library if it's undocumented?
#2
NKurzman
A Guy on the Net
  • Total Posts : 18180
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 10:13:54 (permalink)
4 (4)
You are Not Forced to use any MCC Library.
Use your code if you chose.
The MCC group appears to believe the code is self documenting.  Some is, some is not.
#3
Mysil
Super Member
  • Total Posts : 3568
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 11:42:48 (permalink) ☄ Helpfulby Maldus 2018/08/01 12:13:51
4.83 (6)
Hi,
Code for I2C seem to be generally low priority in Microchip.
 
Once upon a time, there was a set of primitives and example code for simple I2C  blocking operations, available as part of compiler / Plib installation. 
With introduction of MCC, these have not been reproduced,
instead, the driver in MCC PIC device libraries, is a non-blocking interrupt driver with task queue, that is more complicated to use on the application level.
It did work for me, the first time I tried, but I have found several bugs and vulnerabilities,
and made a lot of modifications. You may see this thread:
https://www.microchip.com/forums/FindPost/985254
 
For simpler purposes, threads in these forum, indicate that many users struggle with implementing their own code to control the MSSP I2C hardware. There is a lot of code examples around, but most have shortcuts and mistakes.
This is unfortunate, but not unexpected.
I2C is a communication protocol that require quite some careful software support, in order to function reliably and robust.  But most I2C code provided by Microchip, or contributed in these fora, is weak on error checking and recovery from disturbances that may and will happen. 
 
I2C communication concepts seem simple when presented and explained for simple purposes.
It is however a 'Listen before you talk' communication protocol, where  hardware check that signaling procees correctly. If there is some disturbance, hardware will set a status flag or interrupt flag, and refuse any further transmission, until the mess is cleared up.
This require software support and quite a lot of testing.
Some conditions may be recovered, should be retried in driver level code,
but some conditions must be reported to Application code, and handled there.
 
I2C specification also hold several advanced features and alternatives, that require even more software support.
 
I did try the Foundation Library code for I2C about a year ago, and was not impressed.
It seem nothing have happened since then.
The first time I tried, I knowingly called a slave that was not connected. This is a case that should be expected, and handled and reported.
But example code went into a infinite loop, jammed the bus with continous transmission, 
and never returned to application example.
 
When running on a small microcontroller with only one interrupt priority level,
a non-blocking interrupt driver, like in my link above, may not be the most suitable.
Code that wait for each transfer to complete, and do proper error cheching and recovery, may be suitable in many cases.
 
There are some code examples in: www.embeddedcodesource.com
 
If you provide a complete problem example, I may be able to try it on PIC16F1825 or PIC16F1829.
 
   Mysil
#4
Maldus
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2016/08/17 09:55:57
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 12:13:39 (permalink)
3 (1)

There are some code examples in: ...


Unfortunately, most examples there use the legacy peripheral libraries. I've resorted to MCC specifically because the MCU I'm using (pic24FJ64ga702) does not support them. Those were decent at least.


If I have to chose between rewriting basic I2C functions from scratch and using some contrived code from MCC I'll reluctantly go with the latter. It's pointless for me to create a library that will become obsolete in my next project.

It's just frustrating to know that the Foundation library IS THERE (making the bold assumption that it works) but I can't use it because there is not a single hint on how it works; moreover, no one from Microchip can be bothered to explain it.


You are right, I2C is a complex protocol... In the top 1% of situations. I've worked in this field for two years now (not a lot, but still) and everything I've seen is single-master, no-disturbances scenarios. I would assume that the basic solution is the first one to be provided, not that the whole library needs to be 100 times more complicated to catch some extreme case.


Anyway, thank you for the time you took answering me. I really don't want to sound smug or rude, I'm just very frustrated.
#5
NKurzman
A Guy on the Net
  • Total Posts : 18180
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 13:12:32 (permalink)
0
 "It's pointless for me to create a library that will become obsolete in my next project."
No, if you create a project it will not become obsolete.  Have Your Functions set the Registers directly.  That never goes out of style.  You can copy many of the functions from the PLIB Source if you like them.
#6
Maldus
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2016/08/17 09:55:57
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 14:21:30 (permalink)
0
I don't think your suggestion is the most convenient course of action.
I should basically replace Plib with my own functions, rewriting them totally or partially (in any case I'd have to thoroughly study all the I2C registers for pic24FJ64ga702), fully knowing that when I'll switch to a new PIC I'll find myself in one of the following situations     a. it still supports Plib, thus making my own library useless (I'm guessing this will become less probable in the future since MCC is the preferred choice)     b. it doesn't support Plib, but I still have to adapt my own library to the MCU's specific I2C registers (big changes? Small tweaks? Who knows?) This, instead of trying to use MCC generated library which whole point is to be the same through different devices and should be the working standard. I've considered both options and in the end I went with the latter anyway; it was annoying because I had to fight with the library awful's interface, but it works without changing too much of my previous code.Last but not Least I would hate to reinvent the wheel since the whole point of this rant is that the functions I'm looking for are there, in the Foundation library; they are just undocumented and thus unusable.
post edited by Maldus - 2018/08/01 14:33:34
#7
JorgeF
Super Member
  • Total Posts : 3341
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 16:31:08 (permalink)
4 (2)
Hi
 
Lucky me.
In the 8bit world I live in for most of my projects, we can code and test the whole thing in less than the 7 hours already elapsed since this thread was bumped by Maldus. ;)

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
#8
NKurzman
A Guy on the Net
  • Total Posts : 18180
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 16:38:29 (permalink)
3 (1)
The low level Functions for I2C are pretty small and simple.  They tend to be similar for Many chips since microchip tends to reuse peripherals. It is how every PIC16 was done since Microchip never publish libraries for them. and Most PIC18s since PLIB was not universally liked. ( Bugs not fixed, poor documentation, poor samples).
I personal never used them and my I2C code was similar from PIC16877A to PIC18, to PIC16F1.
The Task is smaller than you think.
#9
Mysil
Super Member
  • Total Posts : 3568
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/01 20:29:59 (permalink)
5 (1)
Almost all PIC24..., dsPIC33..., PIC32MX have the same I2C peripheral.
Some devices have 2 instances of the peripheral, some have 3, and some have 5,  but mostly they function the same way.
Some of the latest generation of devices have some additional control bits for Slave mode operations, 
and some of the latest generation families have PPS remapping for I2C peripheral also.
 
PIC32MZ also have the same I2C peripheral, but it have additional bugs in hardware, that doesn't manifest in PIC24 or dsPIC33 devices.
 
In my experience, problems with I2C do not come from differences between devices and families,
problems come from there beeing no robust driver code with consistent checking and recovery in the outset.
 
Code in Plib for 16 bit PIC devices, wasn't bad, but did only handle the lowest level functions,
leaving all the complicated checking and recovery unattended.
Source code is there, and may be ported to any 16 bit PIC family devices, and kept together with project source code, instead of relying on the precompiled library archive files. 
 
The purpose of library code, generally is to provide a programming interface, that may be uniform over different devices, but libraries provided by Microchip for their I2C peripherals, have created more differences in software, than there actually are in hardware.
 
It doesn't seem to get better either: Harmony and the 2 different implementations in MCC are more divergent than ever.
Code in the link given in message #4, have the same application interface for all device families from PIC16 to PIC32.
 
   Mysil
 
#10
Mysil
Super Member
  • Total Posts : 3568
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/08/02 04:35:56 (permalink)
5 (1)
Back to the original question:
Documentation I cannot help much with,
Examples using Foundation Services library, are in Click Examples collection: 
https://www.microchip.com/mplab/mplab-code-configurator/click-into-mcc 
code for about any Click board using a I2C sensor, have example code using foundation services code.
 
There is another thread here:
http://www.microchip.com/forums/FindPost/1050653
Foundation services driver use Callback functions to report function completion, and for condition handling.
In example in link above, default action in case of NACK response from I2C bus,
is to retry immediately, and without any limitation.
 
With no description, as pointed out in messages #1 and #2,
there is no explaination of the callback system, or its intended use, either.
Just providing a callback in the driver, does not help making driver code robust and reliable. Instead, it only push the problem back to application programmer, and without careful descriptions of the driver system,
application programmer is clueless about what is needed.
 
Implementation of the I2C driver in foundation services, look like a study in layered driver development,
and use a lot of small functions to operate SFR registers.
While this may look nice in a design plan, it may create a deep call tree, and may cause problems at least on 8-bit PIC devices. In 16 bit PIC devices with stack in RAM, it is maybe not a problem, just inefficient.
 
  Mysil
#11
pchidley
Super Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2009/11/24 13:23:55
  • Location: Calgary, Alberta, Canada
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2018/10/04 14:49:03 (permalink)
5 (4)
Steve Sanghi (Microchip Chairman) once said in the keynote speech at Masters about 6 or 7 years ago;
 
"Microchip is a hardware company that needs to realize its customers only buy the hardware because it is supported by software.  So MCHP has to do a better job with the software."  (maybe not word for word)
 
Unfortunately it seems no one from MCHP was listening and software to support the chips is at an all time low.
The old I2C drivers do work but only after you spend a lot of time figuring out the details that are not documented.
However they do no error checking and include lots of dead end loops to hang the code if the hardware doesn't work as expected.
#12
meraviglioso
Junior Member
  • Total Posts : 41
  • Reward points : 0
  • Joined: 2019/01/01 15:15:35
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/01/18 20:58:46 (permalink)
5 (1)
Sorry for reviving an old thread but this is extremely relevant for us users new to the Microchip world.  All of the examples I find seem to be for the old compilers.  When someone asks a beginner question about how to implement some of these "library" type items they are flamed.  "Read the documentation" "Do a google search"... unfortunately since the compilers have changed and the push toward MCC this leaves many of us completely in the dark.  MCC generates code that is completely incomprehensible for the average new user.  There is no such thing as self documenting code.  It would be really nice if someone **ahem, Microchip**  would release some documentation on the usage of the MCC code.  Honestly if they would just add comments like the old PLIB functions have it would go a very long way.  Anyway sorry for the rant.  I have just been boiling over this for a while now.
#13
btribley
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2018/04/13 14:43:12
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/01/18 21:46:26 (permalink)
4 (1)
I finally got it working and rock solid. Have been waiting to do more testing before releasing it broadly. This was a real battle. I finally decided to use the hardware directly with no interrupts for a simple I2C master and found some example code that I adapted and strengthened the error handling on. I have tested with PIC16 and PIC18, both work well. 
#14
jstanle1_home
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2008/12/18 14:51:47
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/01/21 12:58:05 (permalink)
0
Glad to hear you have this working.  I'm fighting with MCC I2C code now for pic32mx150f128b processor -- looking forward to seeing your working code.  Thanks very much.
#15
SohlMan
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2011/03/23 05:27:18
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/06/03 18:23:24 (permalink)
0
Bill, glad to hear you got it working. Can you post your code? I'd love to see it. 
Thanks. 
#16
Mildly Clueless
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/06/05 23:27:10
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/07/06 21:38:24 (permalink)
0
btribley
I finally got it working and rock solid. Have been waiting to do more testing before releasing it broadly. This was a real battle. 

Would this be considered a code tease?


#17
ric
Super Member
  • Total Posts : 25214
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/07/07 00:00:16 (permalink)
4 (1)
It would appear that the OP has not logged back into this board since making those two posts in January.
 

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!
#18
Arnold4720
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/05/06 20:20:51
  • Location: 0
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/12/11 15:09:37 (permalink)
0
Hey, 
did any one sucessfull use foundation I2C Slave. Till now Microchip didn´t learn that the quality of the lib is directly related to the quality of the documentation
#19
katela
Super Member
  • Total Posts : 1518
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: XC8/MCC I2C Foundation Services Library Documentation and/or Examples 2019/12/11 16:23:08 (permalink)
4 (1)
Arnold4720
Hey, 
did any one sucessfull use foundation I2C Slave. Till now Microchip didn´t learn that the quality of the lib is directly related to the quality of the documentation


It's better if you could start your own thread. 

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5