fillon.nicolas

STM8 I2C Master Software Implementation

Blog Post created by fillon.nicolas Employee on Mar 9, 2017

I2C master

 

Hello,

 

I just finished porting a software implementation of I2C master to a STM8S.

 

It implements the read and write registers functions:

 

You will find the functions located in BB_I2C.c:

 

The functions read and write 16 bit data:

 

Read register function:

BB_I2C_Result BB_I2C_ReadReg (uint8_t regAddress, uint16_t* pValue, uint8_t SlaveAddressshiftedByOne)

Parameters are:

regAddress: 8 bit internal register address where to write

pValue: 16 bit address of the variable where the read will be stored

SlaveAddressshiftedByOne: 8 bit I2C address of the device you want to address over I2C shifted by one bit to the left

 

Return:

The function return a Boolean

BB_I2C_OK: Pass

BB_I2C_ERR: Error

 

 

Write register function:

BB_I2C_Result BB_I2C_WriteReg (unsigned char regAddress, uint16_t value, uint8_t SlaveAddressshiftedByOne)

 

Parameters are:

regAddress: 8 bit internal register address where to write

value: 16 bit value to write

SlaveAddressshiftedByOne: 8 bit I2C address of the device you want to address over I2C shifted by one bit to the left

 

Return:

The function return 1 or 0:

BB_I2C_OK: Pass

BB_I2C_ERR: Error

 

 

In your application code you need first to call: BB_I2C_Init();

This function will initialize the IOs.

 

You will also need to include BB_I2C.h:

#include "BB_i2c.h"

 

To customize the IOs used you will need to edit: BB_I2C.h

#define BB_I2C_PORT GPIOD
#define BB_I2C_SDA GPIO_PIN_6
#define BB_I2C_SCL GPIO_PIN_7

 

You can also edit the I2C slave address (address NOT shifted by one bit to the left) just below:

#define SLAVE_ADDRESS 0x28 // I2C address of the STUSB device

 

I attaching below a project example:

The zip file contains the project files and user files.

It needs to be unzip in the STM8S/A Standard Peripheral Library (I used version 2.2.0) at the following location:

STM8S_StdPeriph_Lib\Project

 

I used STVD and Cosmic so the Project is located at:

STM8S_StdPeriph_Lib\Project\STM8S_SW_I2C_2\STVD\Cosmic

 

I used the project "stm8s103"

 

For hardware I used a STM8S-Discovery board.

 

Here is my set-up:

 

STM8S I2C Master Software

 

In my set-up PD6 is I2C SDA and PD7 is I2C SCL.

 

I connected those IOs to SDA and SCL of the Slave device (a STUSB device).

 

I connected also the ground.

 

You need to make sure that the pull-ups are installed on SCL and SDA.

 

Here is the test I did:
 
 BB_I2C_WriteReg (0x81, 0xAA55, 0x50);
 
BB_I2C_ReadReg (0x81, &Register_readvalue_2, 0x50);

 

 

I wrote value 0xAA55 at register address 0x81 of the STUSB chip which I2C address is 0x50.

 

I then wrote it back and checked that the return value is indeed 0xAA55 and it was the case.

 

Please see my code attached.

 

BR,

 

Nicolas.

Attachments

Outcomes