Bien, el proyecto generado por el CubeMX es un proyecto funcional con el que podríamos trabajar. A mí hay varias cosas que no me gustan, primero no puedo poner las HAL como librerías compartidas con todos los proyectos del workspace, por lo que cada proyecto tiene una copia de dichas librerías y son bastante pesadas. Cuando estoy trabajando con un proyecto suelo ir poniendo en marcha de aun periférico y cuando tengo el proyecto listo, funcionando, suelo hacer una copia y empiezao a agregar el código del siguiente periférico. Siempre me queda una copia de lo que venía haciedo. Los proyectos generados con cubemx no pueden copiarse/pegarse. Tampoco puedo renombrar el proyecto. La estructura de directorios dentro del eclipse es medio rara, son carpetas virtuales, agergar un archivo fuente suele ser medio complicado. No obstante todo eso, paso a explicar como está pensado el proyect creado paraa agregar código en él.
La primera parte de nuestro main luce así:
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
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();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
El cubemx tiene una técnica para que podamos modificar las configuraciones dentro del CubeMX sin perder el código que agregamos al proyecto. Para eso nos pone un grupo de comentarios, donde nosotros podemos escrivir código, por ejemplo:
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
se supone que si yo quiero incluir algún archivo propio, debo poner los include dentro de esos comentarios, de modo tal que si tengo que regenerar el proyecto desde el cubemx, no pierda esas líneas. Cualquier código que coloque fuera de esos comentarios el cubemx los eliminará.
Bien dentro della funcion main tenemos las 2 funciones principales para poner al micro a funcionar con las HAL:
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
La primera, inicializa todo lo necesario para que las librerías HAL puedan funcionar:
* @brief This function is used to initialize the HAL Library; it must be the first
* instruction to be executed in the main program (before to call any other
* HAL function), it performs the following:
* Configure the Flash prefetch, instruction and Data caches.
* Configures the SysTick to generate an interrupt each 1 millisecond,
* which is clocked by the HSI (at this stage, the clock is not yet
* configured and thus the system is running from the internal HSI at 16 MHz).
* Set NVIC Group Priority to 4.
* Calls the HAL_MspInit() callback function defined in user file
* "stm32f4xx_hal_msp.c" to do the global low level hardware initialization
Luego llamamos a la funcion que se encarga de "acelerar" el clock del micro a la frecuencia configurada en el cubemx. Hasta acá el micro corría a 16MHz usando reloj RC interno (HSI). Al pasar por dicha funcion el micro ya correrá a los 168MHz configurados.
Estas dos funciones son las que usaremos de entrada nuestro segundo workspace con un proyecto limpio.
Luego viene el siguiente código:
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Aquí tenemos otra funcion que generó el MX y que se encargará de configurar el GPIO de acuerdo a lo elegido en el cubemx:
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOH_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOD_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PD12 PD13 PD14 PD15 */
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
Como ven se configura el PA0 como entrada (pulsador azul) y los PD12 a 15 como salidas (los 4 led's)
Entramos en el bucle infinito donde pondremos nuestro código para jugar con los led y el pulsador.
Las placas de evaluacion vienen con un conjunto de librerías para usar el hardware que traen (BSP). Por lo que veo el cubemx no incluye un juego de esas librerías por lo que deberemos hacer las cosas usando las HAL directamente.
Para ello tenemos la funcion HAL_GPIO_WritePin.
Agreguemos el siguiente código:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
}
/* USER CODE END 3 */
Ahora veamos como hacer el debug del proyecto.
Primero debemos hacer un build del proyecto. Botón derecho sobre el proyecto y elegimos BUILD. Luego debemos hacer que el binario creado aparezca en el proyecto (otro bug del sistema) boton derecho sobre el proyecto y elegimos REFRESH. Luego apretamos en el pequeño triangulito al costado del bichito verde y elegimos como en la siguiente image:

con esto se crea de forma automática (algo ue se resolvió en la nueva version del SW, la 1.3) el script para el debug.
Se carga el programa y se nos detiene en la instruccion HAL_Init. Le damos a Resume y el código empieza a funcionar...
El resultado?
este:
en la proxima veremos como pasar esto al proyecto creado sin el cube y avanzar con la programacion.
Saludos!