0
\$\begingroup\$

I have created a library for ILI ILI9341 LCD that can do lot's of stuffs. It's made for STM32 and it can do:

  • Plots
  • Number pad
  • Icons 16 bit
  • Windows 3.11 Question dialog
  • Frames

But when I try to load this library into RTOS at STM32. I get hard fault error, and I don't know why.

Here is one part of code.

/**
 * @brief  Function implementing the LCD thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_LCDTask */
void LCDTask(void *argument)
{
  /* USER CODE BEGIN 5 */
    // Init LCD
    ILI9341_Init(&spi, &hspi2, LCD_CS_GPIO_Port, LCD_CS_Pin, LCD_DC_GPIO_Port, LCD_DC_Pin, LCD_RST_GPIO_Port, LCD_RST_Pin, TS_CS_GPIO_Port, TS_CS_Pin);

    // Calibrate the touch if blue Nucleo board button is pressed
    if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) {
        TSC2046_Calibrate(&spi);
    }

    // Create initial frame
    create_num_pad_frame(&spi, false, false);
    create_main_frame(&spi, closedloop_on, only_change_loop_icon);

    /* Infinite loop */
    for (;;) {

Both create_num_pad_frame(&spi, false, false); and create_main_frame(&spi, closedloop_on, only_change_loop_icon); contains HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); like this:

// Create the main frame
void create_main_frame(ILI9341_SPI* spi, bool closedloop_on, bool only_change_loop_icon){
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Hard fault - Why???

But this function, that have the SAME header file included, does not give hard fault.

// Create numpad
void create_num_pad_frame(ILI9341_SPI* spi, bool decimalbutton_show, bool minusbutton_show){
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // LED light up!

But there is ONLY the function create_main_frame that cause Hard Fault when I try to call another function inside create_main_frame.

So why does this happen? My example on my GitHub shows the same example I'm using.

I'm using Nucleo F401RE with STM32CubeIDE 1.0.3 and Firmware 1.25.0 version and here is my RTOS setup:

enter image description here

My complete thread

/* USER CODE BEGIN Header_LCDTask */
/**
 * @brief  Function implementing the LCD thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_LCDTask */
void LCDTask(void *argument)
{
  /* USER CODE BEGIN 5 */
    // Init LCD
    ILI9341_Init(&spi, &hspi2, LCD_CS_GPIO_Port, LCD_CS_Pin, LCD_DC_GPIO_Port, LCD_DC_Pin, LCD_RST_GPIO_Port, LCD_RST_Pin, TS_CS_GPIO_Port, TS_CS_Pin);

    // Calibrate the touch if blue Nucleo board button is pressed
    if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) {
        TSC2046_Calibrate(&spi);
    }

    // Create initial frame
    create_num_pad_frame(&spi, false, false);
    create_main_frame(&spi, closedloop_on, only_change_loop_icon);

    /* Infinite loop */
    for (;;) {
        // Read the LCD touch
        TSC2046_GetTouchData(&spi);
        uint8_t isPressed = spi.myTsData.isPressed;
        uint16_t X = spi.myTsData.X;
        uint16_t Y = spi.myTsData.Y;
        if (isPressed == true) {
            // Check which button we are pressing on
            if (X >= 8 && X <= 37 && Y >= 8 && Y <= 37) {
                // Set point
                decimalbutton_show = true;
                minusbutton_show = true;
                create_num_pad_frame(&spi, decimalbutton_show, minusbutton_show);
                status = num_pad_logic(&spi, decimalbutton_show, minusbutton_show, &number_value);
                only_change_loop_icon = false;
                create_main_frame(&spi, closedloop_on, only_change_loop_icon);
            } else if (X >= 8 && X <= 37 && Y >= 55 && Y <= 94) {
                // Set horizon
                decimalbutton_show = false;
                minusbutton_show = false;
                create_num_pad_frame(&spi, decimalbutton_show, minusbutton_show);
                status = num_pad_logic(&spi, decimalbutton_show, minusbutton_show, &number_value);
                only_change_loop_icon = false;
                create_main_frame(&spi, closedloop_on, only_change_loop_icon);
            } else if (X >= 8 && X <= 37 && Y >= 100 && Y <= 139) {
                // Model order
                decimalbutton_show = false;
                minusbutton_show = false;
                create_num_pad_frame(&spi, decimalbutton_show, minusbutton_show);
                status = num_pad_logic(&spi, decimalbutton_show, minusbutton_show, &number_value);
                only_change_loop_icon = false;
                create_main_frame(&spi, closedloop_on, only_change_loop_icon);
            } else if (X >= 8 && X <= 37 && Y >= 146 && Y <= 185) {
                // Open loop or closed loop
                if (closedloop_on == true)
                    closedloop_on = false;
                else
                    closedloop_on = true;
                only_change_loop_icon = true;
                create_main_frame(&spi, closedloop_on, only_change_loop_icon);
            } else if (X >= 8 && X <= 37 && Y >= 193 && Y <= 232) {
                // Question dialog
                show_question_dialog(&spi);
                uint8_t choice = question_dialog_logic(&spi); // Return 1 as YES and 0 as NO
                only_change_loop_icon = false;
                create_main_frame(&spi, closedloop_on, only_change_loop_icon);
            }
        } else {
            // Plot
            uint16_t new_input = (uint16_t) rand() % 205;
            uint16_t new_output = (uint16_t) rand() % 205;
            change_plot_values(&spi, new_input, new_output, only_move_array);
        }

        osDelay(1);
    }
  /* USER CODE END 5 */ 
}
\$\endgroup\$
  • \$\begingroup\$ What happens if you call create_main_frame first and create_num_pad_frame second? Have you given your LCDTask task a reasonable stack to work with? \$\endgroup\$ – brhans May 24 at 2:15
  • \$\begingroup\$ @brhans then I still get hard Fault. Stack? You mean stack size? \$\endgroup\$ – Daniel Mårtensson May 24 at 9:32
  • \$\begingroup\$ You still get a hard fault in the same function, or you still get a hard fault in the second function? And yes, how much memory have you allocated for the stack for this task? \$\endgroup\$ – brhans May 24 at 12:37
  • \$\begingroup\$ @brhans 128 Words. I solved the issue. It was a lack of stack size. So I moved around some code so I don't declare 500*83 arrays in the rtos-thread :) \$\endgroup\$ – Daniel Mårtensson May 24 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.