Notes
ViliusCh

STM32mp1 I2C oled screen setup

STM32mp1 I2C oled screen setup

Setting up oled screen with I2C connection for CM4 processor

Refferences:
Oled drivers
Youtube guide
back to home


Initial start

For starters you will need: Oled drivers
STM HAL dirvers

Extract oled drivers inside your project directory, extract HAL drivers somewhere where youll always find them (Documents, etc..) because they are used a lot. Link HAL drivers in your projects include paths. Project preporation is complete.


Oled driver configuration

file “ssd1306_conf_template.h” has to be renamed to “ssd1306_conf.h” and line 22 change to #define STM32MP, line 28 change to #define SSD1306_I2C_PORT hi2c5

inside “ssd1306.h” it is mandatory to link stm32mp1 hal drivers since they are not included, add these 2 lines after line 50

#elif defined(STM32MP)
#include "stm32mp1xx_hal.h"

Driver configuration is complete


Main program

inside main include oled drivers, define i2c.

#include "main.h"
#include "ssd1306.h"
#include "ssd1306_fonts.h"
#include "ssd1306_tests.h"


I2C_HandleTypeDef hi2c5;

static void MX_I2C5_Init(void);
static void MX_GPIO_Init(void);

Functions required for I2C initialization add it at the end wheres /* USER CODE BEGIN 4 */ located:

static void MX_I2C5_Init(void)
{

  hi2c5.Instance = I2C5;
  hi2c5.Init.Timing = 0x00701837;
  hi2c5.Init.OwnAddress1 = 0;
  hi2c5.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c5.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c5.Init.OwnAddress2 = 0;
  hi2c5.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c5.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c5.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c5) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure Analogue filter
    */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c5, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure Digital filter
    */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c5, 0) != HAL_OK)
  {
    Error_Handler();
  }

  /**I2C Fast mode Plus enable
  */
  HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C5);
}


/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
     USB_DM1   ------> USBH_HS1_DM
     USB_DP1   ------> USBH_HS1_DP
*/
static void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOI_CLK_ENABLE();
}

/**
  * @brief  I2C error callbacks.
  * @param  I2cHandle: I2C handle
  * @note   This example shows a simple way to report transfer error, and you can
  *         add your own implementation.
  * @retval None
  */
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle)
{
  /** Error_Handler() function is called when error occurs.
    * 1- When Slave doesn't acknowledge its address, Master restarts communication.
    * 2- When Master doesn't acknowledge the last data transferred, Slave doesn't care in this example.
    */
  if (HAL_I2C_GetError(I2cHandle) != HAL_I2C_ERROR_AF)
  {
    Error_Handler();
  }
}

main funtion:

  /* USER CODE BEGIN 2 */

MX_GPIO_Init();
  MX_I2C5_Init();

  if (ssd1306_Init () != 0){
	  Error_Handler();
  }
  ssd1306_Fill(White);
  ssd1306_UpdateScreen();


/* USER CODE END 2 */

In this example the screen shows white color, every function of what the screen driver is able to do is defined in ssd1306.h