• AVR Freaks

Helpful ReplyHot!Any basic example for using static I2C to send/receive data between Pic32 and DSP?

Author
azaman
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2018/06/14 05:08:52
  • Location: 0
  • Status: offline
2018/06/26 05:58:42 (permalink)
0

Any basic example for using static I2C to send/receive data between Pic32 and DSP?

I am trying to send/ receive one byte (e.g 0b01110001) from Pic32 to DSP over I2C bus where Pic32 is master and DSP is the slave. I want to use simple polling method (static model, no interrupt) but I am not able to find any simple example/ code on how I can achieve this in harmony framework. I have tried to set it up using the below code following the harmony help  but I am not sure if this is the right way to go about it.
 
void APP_Tasks ( void )
{
    #define BUFFER_SIZE 1
    #define DSP_SLAVE_ADDRESS 0b01110000

    int myBuffer[BUFFER_SIZE] = {};
    unsigned int numOfBytes;
    DRV_I2C_BUFFER_HANDLE buffHandle_Master;
      
    
    /* Check the application's current state. */
    switch ( appData.state )
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            bool appInitialized = true;
       
        
            if (appInitialized)
            {
            
                appData.state = APP_STATE_I2C_INIT;
            }
            break;
        }

        case APP_STATE_I2C_INIT:
        {
            appData.drvI2CHandle = DRV_I2C_Open(DRV_I2C_INDEX_0, DRV_IO_INTENT_READWRITE); // Open I2C
            
            if (appData.drvI2CHandle != DRV_HANDLE_INVALID)
            {
                appData.state = APP_STATE_MCU_I2C_Write;
            }
            break;
        }
        
        case APP_STATE_MCU_I2C_Write:
        {
            buffHandle_Master = DRV_I2C_Transmit(appData.drvI2CHandle, DSP_SLAVE_ADDRESS, &myBuffer[1], 1, NULL);
            
            appData.state = APP_STATE_DATA_CHECK;
            break;
        }
        
        case APP_STATE_DATA_CHECK:
        {
            if (DRV_I2C_BUFFER_EVENT_COMPLETE == DRV_I2C_TransferStatusGet
                                                 (appData.drvI2CHandle, buffHandle_Master))
            {
                appData.state = APP_STATE_MCU_I2C_Write;
            }
            break;
        }

 
 
Can someone please provide with a simple example code? I am new to harmony and it is causing me a lot of trouble understanding it. Also is it possible to simulate this code before testing on the real hardware?
#1
Freezessc
Junior Member
  • Total Posts : 86
  • Reward points : 0
  • Joined: 2016/11/08 14:36:45
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/26 06:11:56 (permalink)
3 (1)
Hi, a couple things.  one is that your array is of size 1, but arrays start at index 0, when you call your transmit function and pass &myBuffer[1] that should be &myBuffer[0].  Also please be aware that your array &myBuffer is of type int, which means each value stored in the array is of type 32 bit integer or 4 bytes, you pass 1 into the number of bytes, if your value is larger than 8 bits then it wont get the upper 3 bytes.  If you wanted to have an array of bytes I suggest either using a buffer defined as uint8_t or when passing in the number of bytes using the (sizeof()) operator to get the correct number of bytes to send.  
 
#2
azaman
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2018/06/14 05:08:52
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/26 06:24:09 (permalink)
0
Hi Freezessc,
 
Thank you for the reply.
 
I just want to send one byte to dsp every time a push button connected to mcu is pressed. Should I just get rid of the array and use uint8_t?
 
Do you think the remaining code is correct for sending the data over I2C and can I simulate it to verify? 
 
 
#3
Freezessc
Junior Member
  • Total Posts : 86
  • Reward points : 0
  • Joined: 2016/11/08 14:36:45
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/26 06:32:28 (permalink)
0
azaman
Hi Freezessc,
 
Thank you for the reply.
 
I just want to send one byte to dsp every time a push button connected to mcu is pressed. Should I just get rid of the array and use uint8_t?
 
Do you think the remaining code is correct for sending the data over I2C and can I simulate it to verify? 
 
 




Well actually I just read that you're using the static model? I dont think those are the correct functions to call for the static model.  Which version of harmony are you on? assuming you're on 2.0+ I think you want to make calls to 
 
DRV_I2C0_Transmit()
DRV_I2C0_Receive()
 
you can look at the generated function header files by going to the project window and looking under header Files/yourappname/system_config/default(or name of your config)/framework/driver/i2c
 
the functions you have listed are for the dynamic version of the i2c driver.  
 
 
edit:
as far as simulating it goes, I think you can inject bytes into the registers but I dont have much experience using the simulator so hopefully someone else can provide more insight. 
post edited by Freezessc - 2018/06/26 06:34:51
#4
azaman
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2018/06/14 05:08:52
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/26 06:42:02 (permalink)
0
I am using  Harmony version 2.0.5.2
 
Is there any example which I can use for implementing the static model?
#5
Freezessc
Junior Member
  • Total Posts : 86
  • Reward points : 0
  • Joined: 2016/11/08 14:36:45
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/26 07:15:45 (permalink)
0
azaman
I am using  Harmony version 2.0.5.2
 
Is there any example which I can use for implementing the static model?




I think most of the examples use the dynamic version.  If your using the static buffer model though I think you can keep everything the same and just get rid of the DRV_I2C_Open and just replace 
 
 DRV_I2C_Transmit(appData.drvI2CHandle, DSP_SLAVE_ADDRESS, &myBuffer[1], 1, NULL); with  DRV_I2C0_Transmit(DSP_SLAVE_ADDRESS, &myBuffer[0], 1, NULL); the static version just assigns the driver functions to the specific peripheral that your using, so if you had two i2c ports you would have a DRV_I2C0_Transmit and DRV_I2C1_Transmit.
#6
azaman
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2018/06/14 05:08:52
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/26 09:23:55 (permalink)
0
Thanks for the suggestion I will try to implement it today as you said.
If I have any trouble i will bother you again :)
#7
azaman
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2018/06/14 05:08:52
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/27 06:08:05 (permalink)
0
Hi Freezessc,
 
I changed the code like you suggested but I am not able to transmit the data can you please have a look at the code below.
Also can you please explain me how I can send one byte of data (e.g 0b11100001) I defined a uint8_t data[0] and then use pointer later on but I don't get how this works or how my data will be transmitted. My understanding is that the data byte (0b11100001) will be located at data[0] and the pointer is pointing at the first bit of data[0] and using fifo the data is being transmitted. Is this correct?
#define DSP_SLAVE_ADDRESS 0b01110000
uint8_t data[1] = {1010};

case APP_STATE_I2C_INIT:
 {
DRV_I2C0_Initialize();
appData.state = APP_STATE_MCU_I2C_Write;
break;
}
case APP_STATE_MCU_I2C_Write:
{
appData.dataTransmit = DRV_I2C0_Transmit(DSP_SLAVE_ADDRESS, &data[0], 1, NULL);
appData.state = APP_STATE_DATA_CHECK;
  break;
}
case APP_STATE_DATA_CHECK:
{
if (DRV_I2C_BUFFER_EVENT_COMPLETE == DRV_I2C0_TransferStatusGet(appData.dataTransmit))
{
appData.state = APP_STATE_MCU_I2C_Write;
  }
break; 
}
 
post edited by azaman - 2018/06/27 06:43:03
#8
Freezessc
Junior Member
  • Total Posts : 86
  • Reward points : 0
  • Joined: 2016/11/08 14:36:45
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/27 07:01:50 (permalink) ☄ Helpfulby azaman 2018/06/27 07:34:42
3.5 (2)
Hello Azaman,
 
It appears there are a few mistakes in the code that I can see.  when you declare uint8_t data[0] that creates an array with zero indexes.  Typically when you declare an array you state what size you want that array to be and index by (N-1).  That means that if I declare an array of say 10 elements (data[10]) only index 0-9 are valid.  If you want an array of 1 element you would declare it by typing uint8_t data[1], and access the value by saying data[0].  I hope I'm not being too obvious but I've seen you make 2 errors now with indexing an array, if its not clear I suggest taking some time to read up on how array index/declarations work in C. 
 
As far as the rest of your code, I assume that you put some value in data[0] that's not in the code provided? I dont see you entering that value of  0b11100001 in index 0.  one of the first things you do is call DRV_I2C0_Initialize();  If you generated your file from the harmony configurator you shouldn't need to do that as it does it for you in system_init.c. you can verify that by going under sourceFiles->app/system_config/default(or what ever your config name is)/system_init.c, it should be called in the SYS_Initialize routine.  The rest of the code seems ok your just going to keep writing the same thing over and over again which I assume you want to do. 
 
One other place where you can go wrong with the i2c peripheral is that you need to make sure you've assigned the pins for SCK and SDA in the harmony configurator, I dont know what pic your using but make sure that if under the harmony configurator you selected I2C module ID 1 that you have SCK1 and SDA1 configured in the pin settings tab.  
 
Finally to address the last part of your question, the pointer does not point to the first bit, it points to the address that stores the entire byte (or whatever size data type the pointer is).  the Transmit function will take your pointer and load the uint8_t data type into the I2CxTRN register and transmit that out. 
 
azaman
Hi Freezessc,
 
I changed the code like you suggested but I am not able to transmit the data can you please have a look at the code below.
Also can you please explain me how I can send one byte of data (e.g 0b11100001) I defined a uint8_t data[0] and then use pointer later on but I don't get how this works or how my data will be transmitted. My understanding is that the data byte (0b11100001) will be located at data[0] and the pointer is pointing at the first bit of data[0] and using fifo the data is being transmitted. Is this correct?
#define DSP_SLAVE_ADDRESS 0b01110000
uint8_t data[0];

case APP_STATE_I2C_INIT:
 {
DRV_I2C0_Initialize();
appData.state = APP_STATE_MCU_I2C_Write;
break;
}
case APP_STATE_MCU_I2C_Write:
{
appData.dataTransmit = DRV_I2C0_Transmit(DSP_SLAVE_ADDRESS, &data[0], 1, NULL);
appData.state = APP_STATE_DATA_CHECK;
  break;
}
case APP_STATE_DATA_CHECK:
{
if (DRV_I2C_BUFFER_EVENT_COMPLETE == DRV_I2C0_TransferStatusGet(appData.dataTransmit))
{
appData.state = APP_STATE_MCU_I2C_Write;
  }
break; 
}
 




#9
hutorny
Super Member
  • Total Posts : 362
  • Reward points : 0
  • Joined: 2003/11/07 12:47:29
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/28 00:49:08 (permalink) ☄ Helpfulby Timelord 2020/09/20 11:56:34
4 (1)
azaman

void APP_Tasks ( void )
{
    #define BUFFER_SIZE 1
    #define DSP_SLAVE_ADDRESS 0b01110000

    int myBuffer[BUFFER_SIZE] = {};


 
Buffers, passed to Harmony should not be allocated on the stack.
Harmony documentations does not state this explicitly, but if you look at the driver's source code, you'll find something like
drv_i2c.c:2271

        i2cDataObj->txBuffer        = writeBuffer;

They just store the buffer address in the queue.
Therefore your buffer should be available until the transfer completes.
 
 
post edited by hutorny - 2018/06/28 00:50:24

Programming in a small http://hutorny.in.ua
#10
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/28 02:24:49 (permalink)
4 (1)
azaman
...
uint8_t data[1] = {1010};


What is "1010" meant to be?
The way you have written it, it is a decimal value 1010 which equals 0x3F2 which is too large to fit into uint8_t
If you intended a binary value, is should have been
...
uint8_t data[1] = {0xb1010};


This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#11
azaman
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2018/06/14 05:08:52
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/28 02:49:35 (permalink)
0
Hi,
I changed the data type to char and right now I am using
#define BUFFER_SIZE         1
char data[BUFFER_SIZE] = {'a'};
 appData.dataTransmit = DRV_I2C0_Transmit(DSP_SLAVE_ADDRESS, &data[0], 1, NULL);
But every time I run my code I get the value (d "0x64") in appData.dataTransmit. The address of appData.dataTransmit is (0xA000021C) and the appData starts at (0xA0000218).
 
I am not able to get the value of data I intend to receive e.g 'a' but I get 'd' everytime and I do not know where it is coming from. And the register I2C1TRN is always empty does it mean my data is not being transmitted?
#12
muellernick
Super Member
  • Total Posts : 476
  • Reward points : 0
  • Joined: 2015/01/06 23:58:23
  • Location: Germany
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/28 02:59:26 (permalink)
0
AZAMAN!
 
Sorry for the harsh words, but you should learn C first.
 
Nick
#13
Freezessc
Junior Member
  • Total Posts : 86
  • Reward points : 0
  • Joined: 2016/11/08 14:36:45
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2018/06/28 06:25:05 (permalink)
0
azaman
Hi,
I changed the data type to char and right now I am using
#define BUFFER_SIZE 1
char data[BUFFER_SIZE] = {'a'};
 appData.dataTransmit = DRV_I2C0_Transmit(DSP_SLAVE_ADDRESS, &data[0], 1, NULL);

But every time I run my code I get the value (d "0x64") in appData.dataTransmit. The address of appData.dataTransmit is (0xA000021C) and the appData starts at (0xA0000218).
 
I am not able to get the value of data I intend to receive e.g 'a' but I get 'd' everytime and I do not know where it is coming from. And the register I2C1TRN is always empty does it mean my data is not being transmitted?




are you expecting to receive a response from the DSP?  DRV_I2C0_Transmit() only sends, you'll need to call DRV_I2C0_Receive() to actually receive anything over i2c.  appData.dataTransmit will only ever be a pointer to the Buffer handle that DRV_I2C0_Transmit() returns.  
#14
Timelord
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2006/08/24 05:30:31
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2020/09/20 12:01:53 (permalink)
0
hutorny is spot-on: the buffer must not be on the stack.
I have used the Harmony V3 plus PIC32MX to talk to a SSD1306 OLED via I2C and it works a treat!!!
I originally had data on the stack and single-stepping the debugger showed strange data being transmitted.
Email if you need details.
September 2020.
Al the Timelord
#15
vexorg
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2019/09/27 10:59:40
  • Location: 0
  • Status: offline
Re: Any basic example for using static I2C to send/receive data between Pic32 and DSP? 2020/09/20 13:27:15 (permalink)
5 (1)
To be honest, the I2C is so simple to set up from the registers that harmony is a total waste of time. Easy to send and easy to read once you configure the bits.
#16
Jump to:
© 2020 APG vNext Commercial Version 4.5