cancel
Showing results for 
Search instead for 
Did you mean: 

i2c- stm32f429 disco with arduino mega adk

Daattavya Aggarwal
Associate II
Posted on September 07, 2017 at 21:53

Hi. I am trying to perform i2c communication between a stm32f429 discovery board and an arduino mega adk. The stm board is the master transmitter and the arduino is the slave receiver. Currently, nothing is printed on the arduino serial monitor. I am using the example code for slave receiver for the arduino. Any help would be greatly appreciated.

void scl_pin()

{

RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

GPIOB ->MODER |= GPIO_MODER_MODER6_1;

GPIOB ->AFR[0] = GPIO_AF4_I2C1<<24;

}

void sda_pin()

{

RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

GPIOB ->MODER |= GPIO_MODER_MODER7_1;

GPIOB ->OTYPER |= GPIO_OTYPER_OT_7;

GPIOB ->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR7_1;

GPIOB ->PUPDR |= GPIO_PUPDR_PUPDR7_0;

GPIOB ->AFR[0] = GPIO_AF4_I2C1<<28;

}

void i2c_setup()

{

// uint16_t freqrange = 0;

// uint16_t pclk1 = 0;

// pclk1 = RCC_CLOCKS.PCLK1_Frequency;

// freqrange = (uint16_t)(pclk1 / 1000000);

uint32_t frequency = 0;

uint32_t read_reg = 0;

frequency = SystemCoreClock;

I2C1 ->CR2 |= I2C_CR2_FREQ_4;

I2C1 ->CCR |= 0x50;

I2C1 ->TRISE = 0x11;

I2C1->CR1 |= I2C_CR1_ACK;

I2C1 ->CR1 |= I2C_CR1_PE;

I2C1 ->CR1 |= I2C_CR1_START;

read_reg = I2C1->SR1;

I2C1->DR = 0x08;

read_reg = I2C1->SR1;

read_reg = I2C1->SR2;

}

void send_data(uint8_t data)

{

// while(!(I2C1->SR1 & I2C_SR1_TXE))

I2C1 ->DR = data;

}

int main(void)

{

i2c_enable();

led_init1();

led_init2();

scl_pin();

sda_pin();

i2c_setup();

// if(I2C1->CR1 & I2C_CR1_ACK)

// GPIOG ->BSRRL |= GPIO_BSRR_BS_13;

while(1)

{

send_data(5);

}

}

I have only posted the code relevant to i2c. 

For arduino:

&sharpinclude <Wire.h>

void setup() {

Wire.begin(8); // join i2c bus with address &sharp8

Wire.onReceive(receiveEvent); // register event

Serial.begin(9600); // start serial for output

}

void loop() {

Serial.print('Entered Loop');

delay(100);

// print the integer

}

void receiveEvent(int howMany) {

while (1 < Wire.available())

{

char c = Wire.read(); // receive byte as a character

Serial.print(c); // print the character

}

}

Thanks

#stm32f429 #mega-adk #i2c #arduino
1 REPLY 1
Posted on September 07, 2017 at 23:28

GPIOB ->AFR[0] = GPIO_AF4_I2C1<<24; // trashes entire register, register level coding requires rtfm

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..