cancel
Showing results for 
Search instead for 
Did you mean: 

Help on Porting Code from Keil to CubeIDE

Chao
Senior

Hi,

I have an ethernet application which is working in a Keil project, the app uses F103ZET6 and contains a UART and an SPI, all working in polling mode. 

In order to port it to CubeIDE, I created a Cube project with F103ZET6, it has a RTC, a UART woking in interrupt mode, an SPI and some GPIOs necessary for the ethernet application. The Cube project is working except that the SPI is not tested, but to my understanding on the SPI, the configurations of the 2 SPIs in the Cube project and the Keil project are the same.

However, when I copied the ethernet application files (EXCLUDING the HAL drivers), the Cube project compiles without errors but not working on the same hardware environment on which the Keil project is working.

For identifying where the issue is, I am trying to do the things in the opposite way: copying the files related to the RTC, SPI, and UART from Cube to Keil. First, I copied RTC files (my app files and necessary HAL drivers) and modified the System Clock Configuration for LSI. It did not work. Then after having deleted the modifications for the RTC, I copied Uart related configuration to the Keil project as shown below:

1. the UART configuration to main.c

static void MX_USART1_UART_Init(void)

{

huart1.Instance = USART1;

huart1.Init.BaudRate = 115200;

huart1.Init.WordLength = UART_WORDLENGTH_8B;

huart1.Init.StopBits = UART_STOPBITS_1;

huart1.Init.Parity = UART_PARITY_NONE;

huart1.Init.Mode = UART_MODE_TX_RX;

huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart1.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart1) != HAL_OK)

{

}

}

2. the implementation of HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) to main.c

3. call MX_USART1_UART_Init(void) in main.c

4. copied main.h to the Keil project

5. during writing this post, I happened to know the Cube-generated code in stm32f1xx_hal_msp.c are all project specific, so copied the function HAL_UART_MspInit(huart) to the main.c in Keil project

5. deleted original uart.c and uart.h in Keil project

Unfortunately, this still does not work. I do not see any printed texts on my PC as seen with my Cube project.

Did I miss anything? Are there any other UART relevant code that are generated by Cube, and I need to copy to Keil project?

Regards

Chao

 

3 REPLIES 3
Chao
Senior

The version of the HAL driver in my Cube project is :

#define __STM32F1xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */

#define __STM32F1xx_HAL_VERSION_SUB1 (0x01U) /*!< [23:16] sub1 version */

#define __STM32F1xx_HAL_VERSION_SUB2 (0x09U) /*!< [15:8] sub2 version */

#define __STM32F1xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */

 

While that in Keil project is :

#define __STM32F1xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
#define __STM32F1xx_HAL_VERSION_SUB1 (0x01U) /*!< [23:16] sub1 version */
#define __STM32F1xx_HAL_VERSION_SUB2 (0x07U) /*!< [15:8] sub2 version */

Chao
Senior

found another function I need to copy to Keil project:   void USART1_IRQHandler(void) in stm32f1xx_it.c

but it still does not work after a build.

Now usart1.h has:

void MX_USART1_UART_Init(void);
void HAL_UART_MspInit(UART_HandleTypeDef* huart);
void USART1_IRQHandler(void);

main.h has:

#define DebugTx_Pin GPIO_PIN_9
#define DebugTx_GPIO_Port GPIOA
#define DebugRx_Pin GPIO_PIN_10
#define DebugRx_GPIO_Port GPIOA

and usart1.c has:

#include "usart1.h"
#include "main.h"

UART_HandleTypeDef huart1;

void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==USART1)
{
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = DebugTx_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DebugTx_GPIO_Port, &GPIO_InitStruct);

GPIO_InitStruct.Pin = DebugRx_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DebugRx_GPIO_Port, &GPIO_InitStruct);

/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
}
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
}

VictorD
ST Employee

Hello Chao,

I would advise you to take a look at the STM32CubeF1 firmware on GitHub. You can find an example of UART working in polling mode here : STM32CubeF1/Projects/STM32F103RB-Nucleo/Examples/UART/UART_TwoBoards_ComPolling at master · STMicroelectronics/STM32CubeF1 · GitHub

It also allows you to select an older cube firmware version if needed.

Additionally, you can find one RTC example. If your board has an LSE input clock, it is recommended to use it for the RTC since it provides better accuracy.

Best regards,

Victor