Using I2C on the MINI M4STM32

General discussion on mikroPascal PRO for ARM.
Post Reply
Author
Message
ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Using I2C on the MINI M4STM32

#1 Post by ITDesigns » 26 Nov 2014 06:20

Hi
I'm struggling to get an I2C interface up and running.
I am working with mikroPascal 4.2.0 and using the MINI M4 STM32.

First of all I can't see whether I need pull up resistors or not ?
Last edited by ITDesigns on 12 Jul 2016 02:45, edited 1 time in total.

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: Using I2C on the MINI M4STM32

#2 Post by ITDesigns » 27 Nov 2014 01:52

More follow up

I have installed mikroPascal for ARM 4.5.0

The problem as described above is still present.
I notice in the help you now indicate that
I2C1 : PB6/PB7 - all Cortex M0, M3 and M4 MCUs. (which includes the MINI M4 STM32)
I2C2 : PB10/PB11 - all Cortex M0 and M3 MCUs. (which DOES NOT include the MINI M4 STM32)
I2C3 : PD0/PD1 - STM32F401x and STM32F411x Cortex M4 MCUs. (which DOES NOT include the MINI M4 STM32)

OK so I tried to use "I2C2_Init_Advanced" to allow me to direct the I2C2 to PB10 and PB11; (code below)

The result was the same - with I2C2_start and I2C2_write compiled, the LED blinking never starts

Procedure init_I2C_stuff;
begin
{ GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_ALL);{}
GPIO_Digital_Output(@GPIOB_BASE, _GPIO_PINMASK_10 or _GPIO_PINMASK_11 or
_GPIO_PINMASK_6 or _GPIO_PINMASK_7 );{}
GPIOB_ODR := 0xFFFF; {}
delay_ms(100);
I2C1_Init(); {on PB10 and PB11}

I2C2_Init_Advanced(100000,@_GPIO_MODULE_I2C2_PB10_11);

delay_ms(1000);
I2C_data[0] := $03; {config register}
I2C_data[1] := $0F; {b0..b3 inputs(1) b4..b7 outputs (0)}

I2C2_Start(); // issue I2C start signal
delay_ms(100);
I2C2_Write(0x20,@I2C_data,2,END_MODE_STOP); {}
{ I2C1_Start(); // issue I2C start signal
delay_ms(100);
I2C1_Write(0x20,@I2C_data,2,END_MODE_STOP); {}
delay_ms(1000);
(* *)
end;

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: Using I2C on the MINI M4STM32

#3 Post by ITDesigns » 28 Nov 2014 00:59

Still pursuing this problem
Last edited by ITDesigns on 12 Jul 2016 02:46, edited 2 times in total.

Pelikan
Posts: 219
Joined: 22 Nov 2012 07:58

Re: Using I2C on the MINI M4STM32

#4 Post by Pelikan » 28 Nov 2014 07:56

Hello ITDesigns,

with me did not work on a STM32F407 the I2C example.
Since the source code "Top Secret" is, I write down the necessary units themselves.

Please check once the project settings, I always take with me the:
// ------------------------------------------------ ------------------------------
// System STM32F407ZG on Olimex STM32E407
// XTAL = 12 MHz
// M = 12 VCO -> 1MHz
// N = 336 PLL -> 336 MHz
// P = 2 CLK -> 168MHz
// Q = 7 EXT -> 48MHz
// APB1 = 4 -> 42MHz
// APB2 = 2 -> 84MHz
// CPU = 168 Mhz
// ------------------------------------------------ ------------------------------

With Greeting
Peter

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Using I2C on the MINI M4STM32

#5 Post by filip » 28 Nov 2014 13:11

Hi,
First of all I can't see whether I need pull up resistors or not
The pull-up resistor are absolutely necessary for the I2C communication, you have came to this conclusion by yourself already.

The explanation about the I2C pin mapping is corrected in the new release of the compiler.

As for the PCA9554, I have never worked with it before, but we have several I2C devices with examples that may serve as a little guidance :
http://www.libstock.com/projects/view/1 ... ck-example
http://www.libstock.com/projects/view/2 ... ck-example

Regards,
Filip.

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: Using I2C on the MINI M4STM32

#6 Post by ITDesigns » 02 Dec 2014 02:22

Thanks for the replies Filip and Peter

BTW The PCA9554 is very simple IO device from NXP nothing unusual about it at all.

I have looked at the two project examples and they basically have implemented what I have implemented at a code level, ie
I2C1_Start(); // Issue start signal
I2C1_Write(RTC_ADDR, @buffer, 2, END_MODE_STOP);
This suggests to me that it is something to do with project settings

Both those projects are for the STM32F107VC.
The STM32F415RG (MINI M4 device) has a different set of project settings so it is difficult to know what I should be looking at . . .
Thanks for your suggestions for the project settings, Peter.

Do mikroE have any tested project examples using the I2C bus on MINI M4 STM32?
Or can you suggest which of the project settings are critical ?


Regards

Geoff

Pelikan
Posts: 219
Joined: 22 Nov 2012 07:58

Re: Using I2C on the MINI M4STM32

#7 Post by Pelikan » 02 Dec 2014 09:02

Hi Geoff,

my settings are for a 12MHz quartz, the MiniM4 is PLLM = 16.
All other settings are the same.

With Greeting
Peter

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: Using I2C on the MINI M4STM32

#8 Post by ITDesigns » 03 Dec 2014 04:56

OK -I 'm getting somewhere.

A few more errors on my part and only one thing I am stuck with !
The address of my device is $20 - the acknowledge is a low, pull down, condition -whoops I had forgotten that !

The code below works - except for if there is a fault in the I2C protocol

If for example I disconnect the SDA line from the PCA9554, the processor hangs.
My attempted solution :-
if (I2C_Start() = 0) AND
(I2C_Write(PCA9554_Addr,@I2C_data,2,END_MODE_STOP)=0)
then begin end
else init_I2C_stuff_2;

doesn't work.

Why is the software hanging if there is an I2C miscommunication ?
What can I do about it ?

I am using an I2C device that may or may not be connected to the system so I do need to be able to recover if it is absent


I was wondering if the ITERREN bit was set and the IVT_INT_I2C2_ER had not been assigned ?
(Is 50 the correct value for IVT_INT_I2C2_ER - the DM00031020 in table 45 has its value as 34 - are these the same thing)


Filip can you get a techy at mikroE to look at this one ?
Cheers

Geoff







Procedure init_I2C_stuff_2;
begin
I2C2_Init_Advanced(100000,@_GPIO_MODULE_I2C2_PB10_11); {}
delay_us(150);
I2C_Set_Active(@I2C2_Start, @I2C2_Read, @I2C2_Write); // Sets the I2C2 module active
end;


Procedure PCA9554_init;
begin
I2C_data[0] := $03; {config register}
I2C_data[1] := $0F; {b0..b3 inputs(1) b4..b7 outputs (0)}
if (I2C_Start() = 0) AND
(I2C_Write(PCA9554_Addr,@I2C_data,2,END_MODE_STOP)=0)
then begin end
else init_I2C_stuff_2;
end;


Procedure PCA9554_high;
begin
I2C_data[0] := $01; {output port register}
I2C_data[1] := $FF; { active outputs high }
if (I2C_Start() = 0) AND
(I2C_Write(PCA9554_Addr,@I2C_data,2,END_MODE_STOP)=0)
then begin end
else init_I2C_stuff_2;end;

Procedure PCA9554_low;
begin
I2C_data[0] := $01; {output port register}
I2C_data[1] := $00; { active outputs high }
if (I2C_Start() = 0) AND
(I2C_Write(PCA9554_Addr,@I2C_data,2,END_MODE_STOP)=0)
then begin end
else init_I2C_stuff_2;
end;



procedure Wait();
begin
Delay_ms(500);
end;

begin
Init_status_LEDs;
init_I2C_stuff_2; {}
PCA9554_init;
while (TRUE) do
begin
Toggle_status_LED;
PCA9554_high; {}
Wait(); // 500ms pause
toggle_data_LED;
PCA9554_low;{}
Wait(); // 500ms pause
end;
end.
Attachments
Addr and 2 byte write to a PCA9554 addr 20h.png
Addr and 2 byte write to a PCA9554 addr 20h.png (45.8 KiB) Viewed 8056 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Using I2C on the MINI M4STM32

#9 Post by filip » 03 Dec 2014 15:06

Hi,

Why is the software hanging if there is an I2C miscommunication ?
What can I do about it ?

I am using an I2C device that may or may not be connected to the system so I do need to be able to recover if it is absent
Please, have a look at the following topic :
http://www.mikroe.com/forum/viewtopic.php?f=178&t=54794

Regards,
Filip.

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: Using I2C on the MINI M4STM32

#10 Post by ITDesigns » 12 Jul 2016 02:43

Hi
I have posted a solution to the problem of the software hanging if there is no I2C device present :-
1468287584_i2c_library_func_mikropascal_arm
I hope it is useful to anyone else who has had this problem.

Geoff

Post Reply

Return to “mikroPascal PRO for ARM General”