arm - How to initialize I2C on STM32F0? -
arm - How to initialize I2C on STM32F0? -
recently i've been trying i2c bus working on stm32f030f4p6 mcu, little luck.
i'm using stm32f0 module , have found plenty of resources stm32f1 module i2c initialization, nil specific stm32f0 initialization/transfer process.
here's initialization code:
void i2c_init(uint8_t ownaddress) { gpio_inittypedef gpio_initstructure; i2c_inittypedef i2c_initstructure; // enable gpioa clocks rcc_apb2periphclockcmd(rcc_ahbperiph_gpioa, enable); // configure i2c1 clock , gpio gpio_structinit(&gpio_initstructure); /* i2c1 clock enable */ rcc_apb1periphclockcmd(rcc_apb1periph_i2c1, enable); /* i2c1 sda , scl configuration */ gpio_initstructure.gpio_pin = gpio_pin_9 | gpio_pin_10; gpio_initstructure.gpio_speed = gpio_speed_2mhz; gpio_initstructure.gpio_mode = gpio_mode_af; gpio_initstructure.gpio_pupd = gpio_pupd_nopull; gpio_initstructure.gpio_otype = gpio_otype_od; gpio_init(gpioa, &gpio_initstructure); /* i2c1 reset */ rcc_apb1periphresetcmd(rcc_apb1periph_i2c1, enable); rcc_apb1periphresetcmd(rcc_apb1periph_i2c1, disable); /* configure i2c1 */ i2c_initstructure.i2c_analogfilter = i2c_analogfilter_enable; i2c_structinit(&i2c_initstructure); i2c_initstructure.i2c_mode = i2c_mode_i2c; //i2c_initstructure.i2c_dutycycle = i2c_dutycycle_2; i2c_initstructure.i2c_ownaddress1 = ownaddress; i2c_initstructure.i2c_ack = i2c_ack_enable; i2c_initstructure.i2c_acknowledgedaddress = i2c_acknowledgedaddress_7bit; //i2c_initstructure.i2c_clockspeed = clockspeed; i2c_init(i2c1, &i2c_initstructure); i2c_cmd(i2c1, enable); }
in order test see if setup right designed i2c transmission code transfer info in never ending loop. here's code that:
while(1) { i2c_slaveaddressconfig(i2c1, regname); i2c_generatestart(i2c1, enable); i2c_numberofbytesconfig(i2c1, 8); i2c_senddata(i2c1,0b00000000); i2c_generatestop(i2c1, enable); }
where:
regname = 0x75
sda = gpio_pin_10 on gpioa
scl = gpio_pin_9 on gpioa
i2c = i2c1
ownadrress = 0x68
when scope i2c lines after start code floating voltage around 160mv. when step through code every 1 of i2c function calls happen , complete, that's why thinking had more initialization of pins themselves.
my problem similar thread, never answered:
https://my.st.com/public/ste2ecommunities/mcu/lists/cortex_mx_stm32/flat.aspx?rootfolder=https%3a%2f%2fmy%2est%2ecom%2fpublic%2fste2ecommunities%2fmcu%2flists%2fcortex_mx_stm32%2fstm32f0%20i2c%20code%20doesn%27t%20work&folderctid=0x01200200770978c69a1141439fe559eb459d7580009c4e14902c3cde46a77f0ffd06506f5b¤tviews=1342
edit 1: here's latest code; still not working (edit 2: updated have; moved things more right locations):
void i2c_init(uint8_t ownaddress) { /* typedefs gpioa , i2c1 */ gpio_inittypedef gpio_initstructure; i2c_inittypedef i2c_initstructure; /* enable gpioa clocks , i2c1 clock enable */ rcc_ahbperiphclockcmd(rcc_ahbperiph_gpioa, enable); rcc_apb1periphclockcmd(rcc_apb1periph_i2c1, enable); /* configure i2c1 clock , gpio */ gpio_structinit(&gpio_initstructure); /* i2c1 sda , scl configuration */ gpio_initstructure.gpio_pin = gpio_pin_9 | gpio_pin_10; gpio_initstructure.gpio_speed = gpio_speed_2mhz; gpio_initstructure.gpio_mode = gpio_mode_af; gpio_initstructure.gpio_pupd = gpio_pupd_nopull; //up gpio_initstructure.gpio_otype = gpio_otype_od; /* gpio af configuration -> gpio_af_1: usart2, cec, tim3, usart1, usart2,eventout, i2c1, i2c2, tim15 */ gpio_pinafconfig(gpioa, gpio_pinsource9, gpio_af_1); gpio_pinafconfig(gpioa, gpio_pinsource10, gpio_af_1); gpio_init(gpioa, &gpio_initstructure); /* i2c1 reset */ rcc_apb1periphresetcmd(rcc_apb1periph_i2c1, enable); rcc_apb1periphresetcmd(rcc_apb1periph_i2c1, disable); //i2c_deinit(i2c1); /* configure i2c1 */ i2c_structinit(&i2c_initstructure); i2c_initstructure.i2c_analogfilter = i2c_analogfilter_enable; i2c_initstructure.i2c_mode = i2c_mode_i2c; i2c_initstructure.i2c_ownaddress1 = ownaddress; i2c_initstructure.i2c_ack = i2c_ack_enable; i2c_initstructure.i2c_acknowledgedaddress = i2c_acknowledgedaddress_7bit; i2c_init(i2c1, &i2c_initstructure); i2c_cmd(i2c1, enable); //i2c_acknowledgeconfig(i2c1, enable); }
as can see added 2 lines gpio_pinafconfig(gpioa, gpio_pinsource9, gpio_af_1);
, gpio_pinafconfig(gpioa, gpio_pinsource10, gpio_af_1);
found gpio_af_1 had alternate function of i2c1 the stm32f0 standard peripheral bibliotheek.
any other ideas? i've been playing around clocks see if changed , have been adding snippets of other people's code see if has effect on output of device.
edit 3: have tried pulling both sda , scl lines vcc 1kohm resistor instructed guide: http://electronics.stackexchange.com/questions/57121/i2c-pullup-resistor-calculations
->(3.3v-0.4)/3ma ~= 1kohm
edit 4: after going through code line line tried outputting various flag bit registers. these registers: isr = i2c1->isr;
, cr1 = i2c1->cr1;
, , cr2 = i2c1->cr2;
the flag after initiating i2c transfer handling i2c_transferhandling(i2c1, 0x02, 1, i2c_autoend_mode, i2c_generate_start_write);
0x8001
can deciphered downwards 2 errors:
#define i2c_isr_busy ((uint32_t)0x00008000) /*!< bus busy */
and
#define i2c_isr_txe ((uint32_t)0x00000001) /*!< transmit info register empty */
i've found work arounds @ link here (remove space after https: go link -> stack overflow won't allow me post more 1 link reason): https: //my.st.com/public/ste2ecommunities/mcu/lists/cortex_mx_stm32/flat.aspx?rootfolder=%2fpublic%2fste2ecommunities%2fmcu%2flists%2fcortex_mx_stm32%2fstm32l151rb%20i2c%20busy%20flag%20always%20set&folderctid=0x01200200770978c69a1141439fe559eb459d7580009c4e14902c3cde46a77f0ffd06506f5b¤tviews=690 i'm looking implement , study seek them out.
the problem don't configure proper af (alternate function) source i2c pins. after reset registers configure af 0, , i2c's function 1, i2c peripheral in fact disconnected gpios.
technically makes no difference when configure that, it's best before gpio configuration minimize unwanted transitions on pins.
edit: must have pullups on i2c pins - without them have "low" level there, , i2c peripheral detects bus error, prevents working properly. either connect external resistors, or @ to the lowest degree enable internal pullups instead of "no pullups/no pulldowns" configuration.
moreover - it's not possible "open drain" pin work without pullup.
arm stm32 i2c
Comments
Post a Comment