cancel
Showing results for 
Search instead for 
Did you mean: 

I2c on stm32f030r8t6

sanjib
Associate III
Posted on May 27, 2014 at 09:03

Before posting here I have gone through stm32f0_stdperiph_lib.zip but the examples which are given are for some different board with different architecture. can anyboduy point me a simple example of communication between two stm32f030r8t6 boards , one will be master and it will be received data from the slave in an way of interrupt not polling. Please help me

7 REPLIES 7
sanjib
Associate III
Posted on May 27, 2014 at 14:27

Hi can anybody help me

Posted on May 27, 2014 at 17:22

Hi can anybody help me

The forum seems to be saying no.

The STM32F0xx_StdPeriph_Lib_V1.3.1\Projects\STM32F0xx_StdPeriph_Examples\I2C\I2C_TwoBoards\readme.txt

is for the EVAL series boards, what prevents you from porting this? Does the F03 I2C bus work differently?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sanjib
Associate III
Posted on May 28, 2014 at 06:31

I dont have LCD in my board and the API's used in this project are not present in my board

stm32f030r8t6
sanjib
Associate III
Posted on May 28, 2014 at 13:55

I have used the fr07 i2c code for reference and writing the fo3o code......but api's like I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&(TimeOut != 0x00))

and others are not in stm32f030 how shud I do that I am attaching my code

________________

Attachments :

I2c.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzzp&d=%2Fa%2F0X0000000bRv%2FgNfi._EVa.ygdrKbxBgXeUdBiRg_cgTREl.KzzVqaDg&asPdf=false
sanjib
Associate III
Posted on May 28, 2014 at 15:16

now im trying in this way.. but something its wrong.. on a terminal im getting only ''error2''.. any ideas? ;)


 #include ''stm32f0xx.h''
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_usart.h''
#include ''stm32f0xx_i2c.h''
#include <
stdio.h
>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
#define MPU6050_RA_WHO_AM_I 0x75
#define Slave_address 0x68
#define mpu6050_FLAG_TIMEOUT ((uint32_t)0x1000)
#define mpu6050_LONG_TIMEOUT ((uint32_t)(500 * mpu6050_FLAG_TIMEOUT))
void USART_send( unsigned char data){
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void USART_print(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;}
}
void frej(uint8_t value)
{
int maska=0x01;
int i;
for(i=7;i>=0;i--){
int l = (((int)value)>>i)&maska;
char t = (char)(l+48);
USART_send(t);
}
USART_send(' ');
}
uint8_t ReadReg(uint8_t RegName)
{
uint8_t mpu6050_BufferRX[2] ={0,0};
uint8_t tmp = 0;
uint32_t DataNum = 0;
int mpu6050_Timeout = 0;
/* Test on BUSY Flag */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error1 '');
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C1, Slave_address, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error2 '');
}
/* Send Register address */
I2C_SendData(I2C1, (uint8_t)RegName);
/* Wait until TC flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TC) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error3 '');
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C1, Slave_address, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
/* Reset local variable */
DataNum = 0;
/* Wait until all data are received */
while (DataNum != 1)
{
/* Wait until RXNE flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error4 '');
}
/* Read data from RXDR */
mpu6050_BufferRX[DataNum]= I2C_ReceiveData(I2C1);
/* Update number of received data */
DataNum++;
}
/* Wait until STOPF flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error5 '');
}
/* Clear STOPF flag */
I2C_ClearFlag(I2C1, I2C_ICR_STOPCF);
// !< Store LM75_I2C received data
//tmp = (uint16_t)(LM75_BufferRX[0] << 8);
//tmp |= LM75_BufferRX[0];
tmp = mpu6050_BufferRX[0];
// return a Reg value
return (uint8_t)tmp;
}
void LED_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void USART_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
//Configure pins: Rx and Tx --------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void I2C_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* Configure the I2C clock source. The clock is derived from the HSI */
RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
//I2C_InitStructure.I2C_Timing = 0xA0120227;
I2C_InitStructure.I2C_Timing = 0x20310A0D;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
int main(void)
{
LED_init();
USART_init();
I2C_init();
int i=0;
while(1)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
ReadReg(MPU6050_RA_WHO_AM_I);
for(i=0;i<10000000;i++);
/*
int i=0;
if(USART_ReceiveData(USART1)=='x'){
GPIO_SetBits(GPIOC, GPIO_Pin_8);
}
for(i=0;i<1000000;i++);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
for(i=0;i<1000000;i++);
*/
}
}

sanjib
Associate III
Posted on May 29, 2014 at 07:22

any body can give me idea

sanjib
Associate III
Posted on May 29, 2014 at 12:05

OK,I found the problem. Flag I2C_ISR_TXIS did not get because the device on the i2c did not respond to the sent address. Need to convert the device address to 7 bits.