Skip to main content
Explorer
June 23, 2024
Question

HAL ErrorCode 4 with RW1063 I2C Screen

  • June 23, 2024
  • 3 replies
  • 4521 views

Hello,

I am coming to you because I have a rather difficult issue to identify.

I have a screen mounted on I2C that originally communicates with an Arduino board via its address 0x3C. I have verified that I can communicate with this address using my Nucleo 64 F401RE board.

However, when I look at the frames, I encounter information that doesn't match up. Worse, when I look at what HAL returns, I get a rather strange error that I would like to submit to you.

In order:

  • I rely on the documentation of the screen RW1063
  • I know the hardware is OK since it responds
  • I know my initialization commands are OK since I can manipulate the screen
  • HAL returns the error Name: ErrorCode / Details:4 / Default:4 / Decimal:4 / Hex:0x4 / Binary:100 / Octal:04
  • The problem is not isolated to a specific part of the code, but I am scrutinizing a particular part of the code when I transmit data

Here is a piece of code in C:

 

 

 

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h" // sprintf()
#include "stdbool.h" // boolean
/* USER CODE END Includes */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define DELAY_MAN 500

#define ADDRESSE_ECRAN 0x3C
#define NB_COLONNES 16
#define NB_LINES 2
/* USER CODE END PD */

/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void RW1063_send_command(uint8_t command);
void RW1063_show_cursor();
void RW1063_hide_cursor();
void RW1063_blink_cursor();
void RW1063_no_blink_cursor();

/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{

/* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */
 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_I2C1_Init();
 MX_USART2_UART_Init();
 /* USER CODE BEGIN 2 */

RW1063_send_command(0x38); // 8-bit mode, 2 lines, 5x8 characters
 HAL_Delay(1);
 RW1063_send_command(0x0C); // Turn on the screen, disable cursor and blinking
 HAL_Delay(1);
 RW1063_send_command(0x01); // Clear the screen
 HAL_Delay(10);
 RW1063_send_command(0x06); // Move the cursor to the right after each character
 HAL_Delay(1);


RW1063_show_cursor();
 HAL_Delay(500);
 RW1063_hide_cursor();
 HAL_Delay(500);
 RW1063_show_cursor();
 RW1063_blink_cursor();
 HAL_Delay(2000);
 RW1063_no_blink_cursor();
 HAL_Delay(500);

/* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
}

}

void RW1063_send_command(uint8_t command) {
 HAL_StatusTypeDef res;

 uint16_t size_buff = 2;
 uint8_t tbl_command[size_buff];

 tbl_command[0] = 0x00;
 tbl_command[1] = command;

 res = HAL_I2C_Master_Transmit(&hi2c1, ADDRESSE_ECRAN << 1, tbl_command, size_buff, HAL_MAX_DELAY);

 if(res != HAL_OK) {
 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
 while(1);
 }
}

void RW1063_show_cursor() {
 RW1063_send_command(0x0E);
}

void RW1063_hide_cursor() {
 RW1063_send_command(0x0C);
}

void RW1063_blink_cursor() {
 RW1063_send_command(0x0F);
}

void RW1063_no_blink_cursor() {
 RW1063_send_command(0x0C);
}

 

 

 

I am also attaching images to illustrate the frames and errors encountered.

Error during debugging, HAL returnError during debugging, HAL return

 

General frameGeneral frame

 

Frame focused on the clockFrame focused on the clock

 

More detailed clock frameMore detailed clock frame

 

I²C configurationI²C configuration

 

I²C configurationI²C configuration

Thank you in advance for your help!

 

 

    This topic has been closed for replies.

    3 replies

    Graduate II
    June 23, 2024

    HAL_I2C_ERROR_AF (4)

    ACK failure from the device

    Might look at how you have this wired up, the pull-ups, the timing. Anything else on the bus it might be interacting with?

    Is this on some board / module? Like to a shop or Amazon, in case someone is working with a similar part.

    Can you clear the error and retry? Sitting in a while loop won't get resolution, but suppose the GPIO here is used as a trigger for the logic analyzer so you can inspect / review the specific failure.

    GeeksAuthor
    Explorer
    June 23, 2024

    The hardware works, the pull-up resistors are phisically present.

    However, I have serious doubts about how the code works. This is all the more true since the screen is the only one connected to the bus.

    There is nothing Amazon here. A STM32 and a Screen obtained from Farnell.

    Any other leads yet?

    Graduate II
    June 23, 2024

    May be you can give a delay of 100ms before the fist command is send to the display, as per the document it says, it takes 40ms after the power supply stabilizes  you have to send the command to set the interface function.

    Graduate II
    June 24, 2024

    check the documentation of your STM32 board to ensure that you are using the correct pins and those signals are routed to the place where you are connecting the display. If possible try to test with another i2c device to  make sure that the I2c bus of the STM32 is working as expected. Are the pull up resistors connected to processor VCC? What is the operating voltage of the Display, if there is a mismatch or not?

    you can try this aswell

    https://deepbluembedded.com/stm32-i2c-scanner-hal-code-example/

    GeeksAuthor
    Explorer
    June 24, 2024

    Thank you very much.

    Thank you for the link. Very interesting.

    My signal is well routed, since we see it appear with a SCL and a SDA with pinoches configured by cubeMX.

    The screen has already worked under STM, but not in a correct and durable way. I didn’t know what error 4 was. Go back to the subject to see the images I posted. It will speak, much more than all my writings.

    In essence, I put the minimum code, I focus on this part to learn more about the HAL. And really, with all your answers, I feel like no one really knows!

    Ultimately, what is missing is a cair array of HAL errors, and probably a warning in cubeMX when the configuration is wrong. In addition it compiles without error in me, so even the compiler agrees and does not see any error. Also, I suppose the HAL is doing its job, but is it doing it right?

    Super User
    June 24, 2024

    @Geeks wrote:

    it compiles without error in me, so even the compiler agrees and does not see any error. 


    The compiler can only tell you that there were no C syntax errors - it has no clue whether that code will actually function correctly!