I2C master/slave help

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

I2C master/slave help

Post by fotios »

Hi there
I have two separate boards, both equipped with P16F1937, and I try to establish I2C communication between them.
One is configured as I2C master and the second as I2C slave.
The two boards are connected with a third board, and as pull-up resistors, on SCL and SDA, I have selected 10KOhms.
The hardware looks OK, and I have checked both the SCL and SDA lines with the scope, and the clock and data signals are indeed active.
Simply, the slave doesn't respond to the program.
I don't know what is the problem, I think the slave doesn't return ACK.
Could you check please the code and configuration in the attached FCF?
Thanks
I2C_Master.fcfx
(10.25 KiB) Downloaded 267 times
I2C_Slave.fcfx
(11.22 KiB) Downloaded 247 times
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

As a thought - do you need to shift the address sent left one bit (bit 0 is used as read/write indicator)

So try sending (0x54 << 1)

Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Thanks a lot, Martin
Though I've read (I don't remember where exactly) within "about" menus, that the address is left-shifted automatically, I was thinking the same and tried instead 0x54 to transmit 0xA8. The same result.
I checked again the SCL and SDA with the scope, more carefully this time, and I noticed two curious things:
a) On the SDA, there is a very hard debounce on the trailing edge of each pulse.
b) The START condition occurs a little bit after the first transition of SCL, i.e. the SDA signal falls from HI to LO when the SCL is in LO state.
I will try to post a capture from the scope screen.
Thanks again
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

I did a simple example using Arduinos at viewtopic.php?f=71&t=20043&hilit=i2c+sl ... 984#p87984

- I'll have a play see if I can get things working again - it was a long time ago when I'd just go FC....

Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

I also realized a very strange thing!
The debounce was simply a very small transition at +5V!
The rest of the pulses are simply... 0-2V transitions!
The micros are working with 5V VDD on both boards.
Thank you again.
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

mnf wrote:I did a simple example using Arduinos at viewtopic.php?f=71&t=20043&hilit=i2c+sl ... 984#p87984

- I'll have a play see if I can get things working again - it was a long time ago when I'd just go FC....

Martin
Actually, my main problem is the communication between an STM32 and a Particle Argon module. The modules of Particle can be programmed with Arduino's language (Wire) in RTOS.
In FC, the slave functionality is still not implemented for STARM.
I've configured the STM32 as master and the Argon as a slave without success.
I've tried many configurations so far.
And so to check the FC consistency I tried the I2C communication among two P16F1937 that I had available.
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

Okay - I have one Arm board I can test... I have an STM32F401RET6 (on a nucleo board) - so as a check I connected an OLED ssd1306 display. I can display on this AOK
- using software mode i2c at 100 and 400khz - so i2c (send at least) is running AOK on the ST board - the Argon uses a similar processor... I don't have anything similar but will test and see if I can get the ST board to receive on i2c.

I have an I2CDriver board - which "should" make it fairly straightforward to send and view i2c data sent to the board....

Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Here is a screenshot of SCL and SDA
Capture-1.jpg
Capture-1.jpg (51.32 KiB) Viewed 16781 times
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

Doesn't look quite right even on the clock - for example 2nd clock (even with stretching) seems to be lower than other pulses and rather uneven. What pull-ups are you using? How long are interconnects too?

Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Yeah, I know.
In the beginning, I used 10KOhms, now 4.7KOhms.
The result is the same.
Interconnections among the two micros are very short, solid wires for both SCL and SDA.
Here is a better screenshot showing all ten SCL transitions.
Capture-1.jpg
Capture-1.jpg (82.12 KiB) Viewed 16748 times
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Here is a screenshot showing the I2C activity of my actual board with the STM32 configured as master and the Particle Argon as slave.
The FC code of master is almost the same as in the previous example with the P16F1937.
Both signals are much more clear. Simply the Argon doesn't respond. Could you see any ACK?
However, the Argon responds occasionally with ACK but in wrong address. I can see that in the Serial Monitor.
Capture-2.jpg
Capture-2.jpg (91.72 KiB) Viewed 16746 times
Capture-3.jpg
Capture-3.jpg (84.83 KiB) Viewed 16738 times
I2C_STM32.fcfx
(12.65 KiB) Downloaded 240 times
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

I got the following trace from my Nucleo board - here it is running the i2c_scanner from the Arduino IDE.. After running a full update on flowcode - the i2c component is failing to compile
C:\Users\mn_fi\DOWNLO~1>arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Os -DSTM32 -DSTM32F4 -DSTM32F401xE -DUSE_HAL_DRIVER -DHSE_VALUE=8000000 -IC:\PROGRA~2\Flowcode\Common\COMPIL~1\starm\batch\..\stm32cubeF4\Drivers\CMSIS\Include -IC:\PROGRA~2\Flowcode\Common\COMPIL~1\starm\batch\..\stm32cubeF4\Drivers\CMSIS\Device\ST\STM32F4xx\Include -IC:\PROGRA~2\Flowcode\Common\COMPIL~1\starm\batch\..\stm32cubeF4\Drivers\STM32F4xx_HAL_Driver\Inc -IC:\PROGRA~2\Flowcode\Common\COMPIL~1\starm\batch\..\stm32cubeF4\Drivers\STM32F4xx_HAL_Driver\Inc\Legacy -IC:\PROGRA~2\Flowcode\Common\COMPIL~1\starm\batch\..\inc -fmessage-length=0 -fdata-sections -ffunction-sections -w -c "arm_i2c.c" -o "arm_i2c_STM32F401xE_8000000\arm_i2c.o"
In file included from C:\ProgramData\MatrixTSL\FlowcodeV8\CAL\includes.c:455:0,
from arm_i2c.c:335:
C:\ProgramData\MatrixTSL\FlowcodeV8\CAL\STARM\STARM_CAL_I2C.c: In function 'FC_CAL_I2C_Transaction_Init_2':
C:\ProgramData\MatrixTSL\FlowcodeV8\CAL\STARM\STARM_CAL_I2C.c:551:23: error: 'I2C_InitTypeDef {aka struct <anonymous>}' has no member named 'OwnAddress2Masks'
MX_I2C_HANDLE_X.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
^
C:\ProgramData\MatrixTSL\FlowcodeV8\CAL\STARM\STARM_CAL_I2C.c:551:43: error: 'I2C_OA2_NOMASK' undeclared (first use in this function)
MX_I2C_HANDLE_X.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
^
C:\ProgramData\MatrixTSL\FlowcodeV8\CAL\STARM\STARM_CAL_I2C.c:551:43: note: each undeclared identifier is reported only once for each function it appears in
Error(s) in build
LabNation_Screenshot4.png
(203.99 KiB) Downloaded 624 times
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Hi Martin
Hmmm, I don't know what happened in your case.
I still use the FC7 and it compiles successfully either the i2c master or the i2c slave functionality. Simply the i2c slave functions are still not implemented for the STARM family, you could see it if you open the STARM CAL folder. However, Benj promised that will try to find a solution (though LeighM is competent for STARM as I think).
To get a better screenshot, I selected purposively as i2c slave address = 0x7C (0111 1100) and as data = 0xFF and bypassed the decision "If ACK=0 then transmit data, i.e. the STM32 transmits continuously the slave address and data. Here the pic:
Capture-1.jpg
Capture-1.jpg (94.88 KiB) Viewed 16720 times
I think the functionality is correct from the FC7 and STM32 side. The START condition is correct (SDA HI->LO when SCL=HI), 1st clock transition-> Address=0, 2nd=1, 3rd=1, 4th=1, 5th=1, 6th=1, 7th=0, 8th=0 (for the 9th clock pulse i think the SDA=HI and normally this might be the ACK=0).
Then it follows the Data 0xFF, SDA=HI for all of the SCL 9 transitions, and then STOP. Although the waveforms are very clear, I couldn't locate the two ACK.
I will try a different i2c slave firmware on Argon that found in GIT and will modify the FC so that the master STM32 will read a sequence of 10 registers that are updated sequentially, and will let you know.
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

LabNation_Screenshot4.png
(203.99 KiB) Downloaded 602 times
Hi Martin and thank you for the picture.
Looking carefully the Arduino i2c_scanner plot, and if I'm not missing something, I think in each 9th clock transition the SDA is in HI state. That means NACK and not ACK, i.e. the slave does not acknowledge the address transmitted by the master.
In the plot section of Address:0x38 which is left-shifted by one = 0111 0000, the START is clear and then in the first SCL1->Data=0, SCL2->Data=1, SCL3->Data=1, SCL4->Data=1, SCL5->Data=0, SCL6->Data=0, SCL7->Data=0, SCL8->Data=0 (i.e. Write), SCL9->Data=1 (i.e. NACK).
In the next section Address:0x39 << 1 = 0111 0010: SCL1->Data=0, SCL2->Data=1, SCL3->Data=1, SCL4->Data=1, SCL5->Data=0, SCL6->Data=0, SCL7->Data=1, SCL8->Data=0 (i.e. Write), SCL9->Data=1 (i.e. again NACK).
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

If a device was connected at the address then it would 'reply' with ACK?

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Yes, when a slave device is connected to the I2C bus and receive its address (or Device ID to not be confused with the Device Register Address), then the master after the transmission of the 7 bits of ID and 1 for R/W it releases (i.e. HI) during the 9th clock pulse the SDA line so that the slave could place it in LO (ACK) or leaving it in HI (NACK) state. ACK means that the slave has acknowledged its ID (address). Here a pic:
Capture-1.jpg
Capture-1.jpg (35.31 KiB) Viewed 16692 times
Please note that when the SCL is HI, any SDA HI->LO transition is recognized as START and any LO->HI as STOP. When the SCL is LO, any SDA HI or LO state is recognized as Data, or Address, or Read, or Write, or ACK, or NACK bit.
I also noticed on the Arduino I2C_Scanner plot, that the SCL pulses are... 10 per each I2C transaction from START to STOP, while in my plots are 9.
Please see this new screenshot where I selected Address = 0x38 (to make a comparison with Arduino plot) and Data = 0xFF
0x38.jpg
0x38.jpg (68.43 KiB) Viewed 16685 times
Last edited by fotios on Wed Apr 29, 2020 10:43 am, edited 5 times in total.
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

6-7 years ago, using the FC5 I had obtained a successful I2C master code for a P16F1939 managing an S/PDIF transceiver and a DAC of Wolfson.
I looked again at this code and the datasheets of WM8805 and WM8741.
For example, the WM8805 has two possible I2C device addresses: 0111 010x or 0111 011x
According to the instructions, for Device ID+Write the master should transmit 0x74 (0111 0100) and for Read 0x75 (0111 0101).
In my code at the time, I had used for Write: Device ID AND 0x7E. For Read I used: Device ID OR 0x01.
I couldn't see the Device ID Left shifted by 1, simply it is an 7-bit address with the LSB placed either to 1 or to 0 according to the instance.
However, in all of the screenshots of my scope, it seems that the slave address (ID) is indeed left-shifted by 1!
The same applies to your Arduino IDE plot!
I'm a bit confused
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

I got the stm32 talking to a second board - in this case an Arduino Nano... (Setup is basic - SDA - A4 SCL to A5 both with 4k7 pullups) Power and gnd to the Arduino connected to 5v/gnd on Nucleo board - this has the advantage that I only need to power one board)

I used your I2C_master code - with the only alteration being that the address needed doubling compared to the receiver (Ie left shifted one bit = 0xA8) - the Arduino set up was just a basic receiver set to address 0x54... I also added a delay after the 'Stop' - to slow things down a bit. The Arduino is running a very basic 'receive' program - it just reads anything presented on i2c and discards.
LabNation_Screenshot5.png
(184.68 KiB) Downloaded 564 times
Here transmitting 'data' at 400kHz.
It should be possible for the i2c_master (slave not supported on the stm32) - to receive data back from the slave (this is how RTC /EEPROMs etc work after all - transmit a request - receive the data)
I'llplay some more... (I used v7 - things seem a bit munged in the latest v8 update?)

Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Hi Martin
At last, I got it working.
I used the address 0x38 in the slave Argon, and in master STM32 I used "transmit address 0x70".
I had tried it 2 days ago (following your suggestion) but failed.
I think had flashed the Argon with different firmware. Now is flashed with the suggested master/slave firmware by Particle.
Here the new screenshot with the ACK clearly visible at the end:
Capture-1.jpg
Capture-1.jpg (79.4 KiB) Viewed 16634 times
Thanks a lot for your precious time.
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

Pleased to hear it's working...

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Hi everyone
I have a new issue. The master STM32 should transmit 2 bytes of data to the slave Argon each time.
From the side of the master (supposing that the right way of transmitting multiple bytes is: START-->TX slave ID-->TX HI Byte-->TX LO Byte-->STOP) I have obtained the related FC code which looks to work properly.
Please see the attached FCF: the variable Data is UINT type while the DataL and DataH are BYTE type.
The problem arises from the side of the slave. It receives the HI Byte but the LO Byte is always 255d (0xFF).
So the result of variable STdataRX is always the HI Byte content.
Here is the Wire code of the slave:

Code: Select all

// Slave Reader running on Device No.2 (Use with corresponding Master Writer running on Device No.1)

// function that executes whenever data is received from master
// this function is registered as an event, see setup()

void receiveEvent(int howMany) {
  while(1 < Wire.available()) { // loop through all but the last
    char c = Wire.read();       // receive byte as a character
    Serial.print(c);            // print the character
  }
  uint8_t STdataH = Wire.read();
  uint8_t STdataL = Wire.read();
  uint16_t STdataRX = (STdataL >> 8) + STdataH;

  Serial.println(STdataH);
  Serial.println(STdataL);
  Serial.println(STdataRX);
}

void setup() {
  Wire.begin(0x38);                // join i2c bus with address 0x38
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}
I2C_STM32.fcfx
(14.18 KiB) Downloaded 165 times
Thanks for any help
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

Ok - I can't compile your example here - I get
Project I2C_STM32
Device STM32F401xE

In file included from C:\Program Files (x86)\Flowcode 7\CAL\includes.c:289:0,
from I2C_STM32.c:157:
C:\Program Files (x86)\Flowcode 7\CAL\STARM\STARM_CAL_I2C.c: In function 'FC_CAL_I2C_Master_Init_1':
C:\Program Files (x86)\Flowcode 7\CAL\STARM\STARM_CAL_I2C.c:262:4: error: #error "ARM does not support I2C in this mode - SW implementation only"
#error "ARM does not support I2C in this mode - SW implementation only"
Despite the i2c being set to software mode

However using the previous code (using the CAL i2c component) - it all works well:
I2C_Master.fcfx
(10.54 KiB) Downloaded 178 times
LabNation_Screenshot6.png
(268.21 KiB) Downloaded 465 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

I think I see the error:
uint8_t STdataH = Wire.read();
uint8_t STdataL = Wire.read();
uint16_t STdataRX = (STdataL >> 8 ) + STdataH;
That should read:

Code: Select all

STdataRX = (STdataH  << 8 ) + STdataL;
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Hi Martin
Thanks for the replies.
The target of my FCF is STM32F303RE.

I tried your correction but still, STdataL is received as 0xFF (255d) by Argon.

Thanks again
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

Just to let you know.
I tried transmitting two single and constant bytes repeatedly, DataH=20 and DataL=150:
STM32: Start-->Tx DataH(20)-->Tx DataL(150)-->Stop
The Argon receives: STdataH=150 and STdataL=255
So the DataL(150) is transmitted by sure but is received as STdataH.
I couldn't be sure if DataH(20) is transmitted correctly by STM32. The Argon receives always 0xFF (255d) as STdataL.
What a mess-up :lol:
Best Regards FOTIS ANAGNOSTOU

Post Reply