[DONE] I2C driver for STM32F0

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: I2C driver for STM32F0

Post by goeck »

Ok, I have another one for here.
Just found out, that my timing wasn't chosen that well. Was wondering about the SCL signal, that had the shape of a PWM with 70% duty cycle (beeing low 70% of the time). Now that I used the ST I2C config tool again, it spit out this timing value:

Code: Select all

0x00901D2B

Using this value I have a nicely shaped clock signal with a 50% duty cycle :-) at a speed of 97kHz (where 100kHz were desired).
Now, I tried to send out the same command bytes to four of my slaves in a row using a for loop. Unfortunately when commenting out the sleep command it would just stop after one command has been send out to the first slave and the first byte (which is the slave address) has been put on the lines. Then, stop condition, nothing happens furthermore.
Including any kind of delay in between heals the situation... Would like to know the cause.

Code: Select all

static uint8_t addr[] = {0xC0, 0xC2, 0xC4, 0xC6};
static uint8_t txBuf[8] = {0x89, 0xFF, 0xFF, 0xE2, 0xF1, 0x02, 0x00, 0x0C};

...

i2cAcquireBus(&I2CD1);
int i;
for(i=0; i<4; i++){
   i2cMasterTransmitTimeout(&I2CD1, addr[i]>>1, txBuf, 8, rxBuf, 0, TIME_INFINITE);
    chThdSleepMicroseconds(1);
}
i2cReleaseBus(&I2CD1);


Cheers
vibrolax
Posts: 9
Joined: Sat Jan 05, 2013 3:23 pm

Re: I2C driver for STM32F0

Post by vibrolax »

I put in a brief effort to use doceme's I2CV2 driver on my STM32F3Discovery, without success.

I hope I have some time this weekend to sort it out on my own, but I am posting my initial experience in case someone has already succeeded in using the I2CV2
driver with the STM32F3 discovery.

First I applied doceme's patch to my chibios sources (about a month-old SVN) using the TortoiseMerge utility.

Then I applied the equivalent changes to the platform.mk STM32F30x that were made to platform.mk in STM32F0xx

I added STM32_I2C_USE_I2C1 and STM32_I2C_USE_I2C2 to the mcuconf.h

So far I have encountered these problems:
symbols not found compiling os/hal/platforms/STM32/I2Cv2/i2c_lld.c:
I2C2_IRQn
I2C1_IRQn

Regards,
Jon
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: I2C driver for STM32F0

Post by goeck »

Jon, do you still have nothing going on?

I could actually use it almost straight away on STM32F0*.
I added the following to mcuconf.h

Code: Select all

/*
 * I2C driver system settings.
 */
#define STM32_I2C_USE_I2C1                TRUE
#define STM32_I2C_USE_I2C2                 FALSE
#define STM32_I2C_I2C1_IRQ_PRIORITY        10
#define STM32_I2C_I2C2_IRQ_PRIORITY        10
#define STM32_I2C_I2C1_DMA_PRIORITY         1
#define STM32_I2C_I2C2_DMA_PRIORITY         1
#define STM32_I2C_DMA_ERROR_HOOK(i2cp)     chSysHalt()
#define STM32_I2C_I2C1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
#define STM32_I2C_I2C1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
#define STM32_I2C_I2C2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_I2C_I2C2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 5)


Actually, these are all just default values extracted from i2c_llld.c
Patches were applied to the platform.mk automatically, I didn't have to something there manually...Only work with my code.
Maybe something went wrong
vibrolax
Posts: 9
Joined: Sat Jan 05, 2013 3:23 pm

Re: I2C driver for STM32F0

Post by vibrolax »

Hi goeck,

This past weekend I did get a bit further along. The reason I had to fix the platform.mk manually is because I'm using the STM32F3, not the F0,
and the F3's platform.mk did not build I2C.

I have found that there are other differences between the I2C implementation on the F0 and F3 platforms, as can be seen by examining the STM I2C CPAL libraries for the respective platforms. For example, the DMA channels for I2C1 are not the same for F0 and F3. The macro that checks for DMA stream conflict also needs looking at.

I am at work now, so I can't remember the specific differences I found - I will follow up tonight from home.

I succeeded in compiling (for the F3Discovery) a Chibios AHRS (attitude and heading reference system) for a custom F1 board that uses the same accelerometer and gyros chips that are on the STM32F3Discovery. I haven't tried to execute it yet. I still have to resolve differences in the I2C initialization structures between I2Cv1 and I2Cv2 (timing, CR1, Cr2).

I won't have much time to work on it again this week (or weekend), but I will get back to it again next week.

Regards,
Jon
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: I2C driver for STM32F0

Post by goeck »

Hey,

here I found something strange.
When using a setup like this I cant get a for loop to spit out the N I2C commands to my slave, only the first command make its way to the bus.

Code: Select all

   //init
   i2cconfig.clock_timing = 0x00901D2B;               // comes from ST I2C config tool (xls)
   i2cInit();
   palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(1));   // I2C SCL
   palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(1));   // I2C SDA
   i2cStart(&I2CD1, &i2cconfig);

... some lines of code later ...

Code: Select all

   //Send command byte
   i2cStart(&I2CD1, &i2cconfig);
    i2cAcquireBus(&I2CD1);
   i2cMasterTransmitTimeout(&I2CD1, addr>>1, command, txLength, buffer, rxLength, TIME_INFINITE);
    i2cReleaseBus(&I2CD1);
   i2cStop(&I2CD1);

I have to get rid of the exclusive I2C usage, done by i2cAcquireBus(&I2CD1); and i2cReleaseBus(&I2CD1);. Then, everything gets pumped out just fine.
I had a look into the routines, and saw, I2C_USE_MUTUAL_EXCLUSION has to be enabled...*looking in halconf.h*...it is.
Now I saw, that I have both usages enabled: Mutexes and Semaphores. Is that kind of disturbing here?

Would appreciate any hint on that topic.
Thanks
Stefan

PS: @Jon, nice to see you figured out something on the F3. Did everything work out now? Could be pushed back to SVN then, maybe?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2C driver for STM32F0

Post by Giovanni »

Hi,

The mutual exclusion is done in the exactly same way in most drivers, it is not source of problems. It is done using mutexes if enabled else semaphores as fallback.

Giovanni
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: I2C driver for STM32F0

Post by goeck »

Hey Giovanni,
thanks for the quick reply. So everything seems to be fine here, right? Good to know...Also I now know how MUTEX is done ... still haven't really touched that part of the game.
Let's see what turns out to be the real deal with this I2C stuff.

THX
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: I2C driver for STM32F0

Post by steved »

Shortage of DMA controllers on the 32F051 - each serial port in UART mode uses two, plus two for each I2C port....

My serial comms is slow, so I've added an option to make the UART driver use interrupts rather than DMA.
Code here: viewtopic.php?f=14&t=1002

Possibly a bit OT, but might help someone realise why their I2C isn't working!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: [TODO] I2C driver for STM32F0

Post by Giovanni »

Hi,

I merged this driver, what is the meaning of this?

Code: Select all

#if defined(STM32F0XX_I2C)
  chDbgCheck((rxbytes > 1), "i2c_lld_master_receive_timeout");
#endif


Is the F0 a special case?

Also, the test application is missing on the target platforms: F0, F30x, F37x.

Giovanni
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: [TODO] I2C driver for STM32F0

Post by goeck »

Hey guys,

I updated the ChibiOS source through SVN today, since a bunch of weeks. Now the linker tells me this:

Code: Select all

Linking build/ch.elf
build/obj/i2c_lld.o: In function `Vector9C':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:292: multiple definition of `Vector9C'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:292: first defined here
build/obj/i2c_lld.o: In function `i2c_lld_init':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:336: multiple definition of `i2c_lld_init'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:336: first defined here
build/obj/i2c_lld.o: In function `i2c_lld_start':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:362: multiple definition of `i2c_lld_start'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:362: first defined here
build/obj/i2c_lld.o: In function `i2c_lld_stop':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:453: multiple definition of `i2c_lld_stop'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:453: first defined here
build/obj/i2c_lld.o: In function `i2c_lld_master_receive_timeout':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:504: multiple definition of `i2c_lld_master_receive_timeout'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:504: first defined here
build/obj/i2c_lld.o: In function `i2c_lld_master_transmit_timeout':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:596: multiple definition of `i2c_lld_master_transmit_timeout'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:596: first defined here
build/obj/i2c_lld.o: In function `i2c_lld_abort_operation':
/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:120: multiple definition of `I2CD1'
build/obj/i2c_lld.o:/os/hal/platforms/STM32/I2Cv2/i2c_lld.c:120: first defined here
collect2.exe: error: ld returned 1 exit status
make: *** [build/ch.elf] Error 1

I cleaned everything prior to build.Does somebody has a hint for me?
Should I open a ticket or is it just me, doing something wrong here?

Cheers
Stefan
Post Reply