• AVR Freaks

AnsweredHot!Simple I2C address scanner using MSSP drivers in v1.78 library

Author
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
2019/11/12 13:04:31 (permalink)
0

Simple I2C address scanner using MSSP drivers in v1.78 library

I am struggling to create a simple I2C address scanner using the new MSSP driver library found in the most recent v1.78 update to the PIC10/PIC12/PIC16/PIC18 library. I thought I could pass a series of addresses through the I2C1_Open function and use the return values that it produces to determine if an address was found. I think I'm missing something though because it only reports I2C1_NOERR regardless if a device with an address is connected or not. So, my question is this: if no address is detected using the I2C1_Open function it should return a busy or a fail, and conversely if an address is detected it should return a noerror, right?

Attached Image(s)

#1
Mysil
Super Member
  • Total Posts : 3476
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/12 13:50:54 (permalink)
+1 (1)
Hi,
The  I2C1_Open()  function does not do anything on the bus.
It isn't until you try to transfer at least a byte that something get sent on the bus,
and it is possible to see if there is any response.
You may look at   void I2C_WriteNBytes(i2c_address_t address, uint8_t* data, size_t len)
in  i2c_master_examples.c
You may even try to set the len argument to 0;
You may have to catch the last return value from the loop calling to  I2C_Close();  
You may try something like:
    ...
    i2c_error_t returnValue;
    while(I2C_BUSY == (returnValue = I2C_Close())); // sit here until finished.
    return returnValue;
}   

 
    Mysil



  
#2
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/12 14:23:15 (permalink)
0
Mysil,
Thanks for the reply! I tried to do that by copying the I2C1_WriteNBytes parameters, but it just hangs on the first address check. Any ideas? I created a function to which all of the addresses from 1 to 127 (decimal) will be passed and the return value should be the error flag given by the I2C.

Attached Image(s)

#3
Mysil
Super Member
  • Total Posts : 3476
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/12 17:00:49 (permalink)
0
Yes,
I have some ideas, but not that I am sure will work.
Am experimenting with something very similar,
so may do something to find out if it will work.
It involve making your own callback handler for use with the I2C_Check()   function:
static uint8_t   NackFlag = 0; 
/* This is a private  callback handler for use with I2C_check(...) function  */
static  i2c_operations_t  NackAddressHandler(void *Ptr)
{    /* Do something to notice that Address Nack have occurred. */
    NackFlag = 1;
    return I2C_STOP;          /* Let driver proceed with Stop signal. */
}
 
uint8_t I2C_check( uint8_t  device address)  
{    NackFlag = 0;        /* Reset the flag, */
//  ...
//  snip
//  ...
    I2C1_SetAddressNackCallback(NackAddressHandler,NULL);
    I2C1_MasterWrite();
    while(I2C1_BUSY ==  I2C1_Close()); // sit here until finished.
    return NackFlag;
}

Also  look in i2c1_master.c    how the default  I2C_CallbackReturnStop(...)  function is made.
 
Regards,
    Mysil
#4
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/15 15:06:33 (permalink)
0
Hello Mysl,
I tried the above version and some variations to it, but the bus wouldn't ever close. It is still getting hung up.
#5
Mysil
Super Member
  • Total Posts : 3476
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/15 18:12:30 (permalink) ☼ Best Answerby cgaufin 2019/11/18 10:40:26
+1 (1)
Hi,
Have done some experiments in the meantime.
It seem that my suggestion in message above was too optimistic:
You may even try to set the len argument to 0;

Do not work unless there is also made changes in: i2c_master.c
 
I will try to attach my current version.
 
Edit:
Code generated by MCC is for PIC16F1619, that is a controller with only one I2C peripheral.
Device support file <xc.h> provide alias names: SSP1STAT alias SSPSTAT; SSP1CON1 alias SSPCON1; SSP1CON2 alias SSPCON2; and so on ...
 
But for enumeration symbols and function names generated by MCC,
it seem to use names with prefix 'I2C_'  while for other devices, it might generate names prefixed with 'I2C1_' 
Changes I have made in generated code is generally commented with:  /* Comment, Mysil. */
 
I have also noted some strange effects, in addition to the problem with zero length data transfer.
I have run the code with I2C bus connected to other devices where there are already communication running by other Master nodes.
The callback driver provided by device library v1.78.0 seem to make mistakes,
when Start signal have been sent by another Master, and bus is already active.
Function  I2C_Check( deviceAddress); // then report I2C address responding, also when there is no device with such address connected.
 
Regards,
    Mysil
post edited by Mysil - 2019/11/16 01:00:29
#6
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/15 20:20:42 (permalink)
0
Can you tell me which controller you are using.In MPLAB there are 2 library it support
I2c1.c and I2cdriver.c  I2c driver.c functions works well PIC18/PIC12 series. product. Check if using MCC  generated file which .c file created.
 

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#7
Mysil
Super Member
  • Total Posts : 3476
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/16 02:13:31 (permalink)
0
Hi AMPS,
As stated by cgaufin in message #1,
this thread is about I2C driver code generated by MCC Device library v1.78 :
pic10-pic12-pic16-pic18-1.78.0.jar
 
In this release of the library, there is only 1 driver alternative for I2C generated by MCC.
There is: ./mcc_generated_files/i2c_master.c
and:     ./mcc_generated_files/examples/i2c_master_example.c
and corresponding header files.
 
This is the callback driver, very similar to the driver provided by  Foundation Services library v0.34 and earlier.
i2c_master_example.c  have application interface functions, very similar to i2c_simple_master.c
 
It is also possible to install Foundation services library v1.0.0
This may provide I2C_Simple  and generate:  ./mcc_generated_files/drivers/i2c_simple_master.c
It provide application interface functions compatible with earlier releases,
but now use: ./mcc_generated_files/i2c_master.c   generated by Device library v1.78
 
The task queue driver for MSSP may no longer be generated by Device library v1.78
 
The callback driver  provided by Device library v1.78 have some corrections and improvements over earlier Foundation Services driver,
and a lot of names have changed to have more uppercase letters,
but it generally have the same limitations, and mostly the same mistakes.
 
It may work for simple use, but there are missing features, and mistakes that show up when used on a bus where other master devices make traffic.
 
    Mysil
post edited by Mysil - 2019/11/16 03:04:44
#8
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Simple I2C address scanner using MSSP drivers in v1.78 library 2019/11/18 10:39:49 (permalink)
0
Thank you, Mysil! That did the job. I appreciate your help on this!
#9
Jump to:
© 2019 APG vNext Commercial Version 4.5