STM32 I2C Lecture 26 – Exercise: Coding Part 1

  • Post author:
  • Post category:Blog

 

Exercise: Coding Part 1

 

 


Let’s implement the master and slave communication exercise.

1. Go to the source and create a new file, as shown in Figure 1.

Exercise: Coding Part 1.
Figure 1. Creation of new source file.

 

2. After clicking the finish button, a new source file is created, as shown in Figure 2.

Exercise: Coding Part 1: I2C Communication Between STM32 Master and Arduino Slave
Figure 2. New source file.

 

3. Copy some necessary code from the old source file that may be header, delay function, and init functions, as shown in Figures 3.1, 3.2, and 3.3.

Exercise: Coding Part 1: I2C Communication Between STM32 Master and Arduino Slave
Figure 3.1. Headers and delay function copied from old source file.

 

Exercise: Coding Part 1: I2C Communication Between STM32 Master and Arduino Slave
Figure 3.2. Inits functions copied from old source file.

 

Exercise: I2C Communication Between STM32 Master and Arduino Slave - Part 1
Figure 3.3. GPIO_ButtonInit function copied from the old source file.

 

4. Create the main function, as shown in Figure 4.

Exercise: Coding Part 1.I2C Communication Between STM32 Master and Arduino Slave
Figure 4. Main function.

 

Code for I2C1_GPIOInits():

Here the pins are configured to behave as I2C pins (Figure 5).

  • Initialize the pin.
  • Configure the pin and set the mode to alternate function mode.
  • Configure the output type of the pin as the open drain (OD).
  • Add the internal pull-up by using the GPIO_PinPuPdControl function since the I2C pins must be in an open drain with the pull-up activated.
  • Set the alternate function mode as 4.
  • Configure the speed of communication.
  • Configure the SCL pin details and initialize it.
  • Configure the SDA pin details and initialize it.
Exercise: I2C Communication Between STM32 Master and Arduino Slave - Part 1
Figure 5. Code for I2C1_GPIOInits().    
  • Call the I2C1_GPIOInits() function from the main, as shown in Figure 8.

Here there is no need to turn on the clock for GPIOB because a driver will do that.

 

Code for I2C1_Inits(): (Figure 6)

  • Create the handle of I2C as a global variable.
  • Configure the peripheral base address.
  • Enable Ack control.
  • Assign the device address. It is an optional step since the device is in master mode. The device address is necessary only when the device is in slave mode. Remember to refer to the specification while assigning the device address because some reserved addresses (Figure 7) cannot be used as a device address.
  • Configure other fields’ duty cycle as FM_DUTY_2.
  • Since we are using the standard mode here, use the speed in Standard mode, which means initialize the SCL speed to 100KHz.
  • Call the I2C_Init() and send the address of the handle.

 

I2C master and slave communication
Figure 6. Code for I2C1_Inits().

 

Reserved addresses table
Figure 7. Reserved addresses table.

 

  • Call the I2C1_Inits() from the main, as shown in Figure 8.
I2C master and slave communication
Figure 8. Calling I2C1_GPIOInits() and I2C1_Inits() from main.

 

5. Enable the clock for the I2C peripheral in I2C_Init(), as shown in Figure 9.

I2C master and slave communication
Figure 9. Code to enable the I2C peripheral clock.

 

6.Enable the I2C peripheral, as shown in Figure 10.

Code to enable the I2C peripheral
Figure 10. Code to enable the I2C peripheral.

 

7. Send the data to the slave:

a. Create some data in the global space. some_data[] = “We are testing I2C master TX\n”.

Note: On the Arduino side, the sketch is written using the Arduino wire library. The wire library has limitations on how many bytes can be transferred or received in single I2C transactions. The limit is 32 bytes. So, don’t send/receive more than 32 bytes in a single I2C transaction. You may split into multiple I2C transactions.

b. Send the data using I2C_MasterSendData(), as shown in Figure 11.

Code to send data using I2C_MasterSendData()
Figure 11. Code to send data using I2C_MasterSendData().

 

c.  Define the slave address: The slave address is nothing but the address of the Arduino board. First, to find out the slave address, open the Arduino sketch, compile it, and then click on the upload button to download your code into the board. After that, go to tools and open the serial monitor, which shows the address of the slave, i.e., 68. Now set the slave address to 0x68.

8. Build the project (Figure 12).

Build the project
Figure 12. Build the project.

The project is built successfully without any errors and warnings.

 

9.Wait for a button press, as shown in Figure 13.

Code to wait for button press
Figure 13. Code to wait for button press.

Now compile once again.

In the following article, let’s test the master to slave communication application.

 

FastBit Embedded Brain Academy Courses,

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.