I am torn between Keil and Cube that you are using. I had learnt a little about Keil so not sure if I should change. Of course, Keil costs and Cube is free. Any thoughts?Presumably, I can only guess, you are looking at code generated by a code wizard for a RTOS.
If you are now learning how to program in C, this would not be my recommended line of approach.
Begin by programming simple constructs using a generic C platform.
There are C compilers that will run and execute directly on your PC.
It is not any more complicated than Keil.Just wonder - Is Cube easy to learn - not just the C code but the whole system
include <stdio.h>
#include "main.h"
#include "Board_LED.h" /* ::Board Support:LED */
#include "Board_Buttons.h" /* ::Board Support:Buttons */
#include "Board_ADC.h" /* ::Board Support:A/D Converter */
#include "RTE_Components.h" /* Component selection */
#include "stm32g4xx_hal.h" /* Keil::Device:STM32Cube HAL:Common */
// Main stack size must be multiple of 8 Bytes
#define APP_MAIN_STK_SZ (1024U)
uint64_t app_main_stk[APP_MAIN_STK_SZ / 8];
const osThreadAttr_t app_main_attr = {
.stack_mem = &app_main_stk[0],
.stack_size = sizeof(app_main_stk)
};
static volatile uint32_t delay_val = 500U;
static osThreadId_t tid_thrLED; /* Thread id of thread: LED */
static osThreadId_t tid_thrBUT; /* Thread id of thread: BUT */
/*----------------------------------------------------------------------------
thrLED: blink LED
*----------------------------------------------------------------------------*/
__NO_RETURN static void thrLED(void *argument) {
uint32_t led_max = LED_GetCount();
uint32_t led_num = 0U;
(void)argument;
for (;;) {
osThreadFlagsWait(0x0001U, osFlagsWaitAny ,osWaitForever);
LED_On(led_num); /* Turn specified LED on */
osThreadFlagsWait(0x0001U, osFlagsWaitAny ,osWaitForever);
LED_Off(led_num); /* Turn specified LED off */
led_num++; /* Change LED number */
if (led_num >= led_max) {
led_num = 0U; /* Restart with first LED */
}
}
}
/*----------------------------------------------------------------------------
thrBUT: check button state
*----------------------------------------------------------------------------*/
__NO_RETURN static void thrBUT(void *argument) {
uint32_t button_msk = (1U << Buttons_GetCount()) - 1U;
(void)argument;
for (;;) {
osDelay(delay_val); /* Wait */
while (Buttons_GetState() & (button_msk)); /* Wait while holding USER button */
osThreadFlagsSet(tid_thrLED, 0x0001U);
}
}
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
__NO_RETURN void app_main (void *argument) {
(void)argument;
LED_Initialize(); /* initalize LEDs */
Buttons_Initialize(); /* initalize Buttons */
tid_thrBUT = osThreadNew (thrBUT, NULL, NULL); /* create BUT thread */
if (tid_thrBUT == NULL) { /* add error handling */ }
tid_thrLED = osThreadNew (thrLED, NULL, NULL); /* create LED thread */
if (tid_thrLED == NULL) { /* add error handling */ }
for (;;) {}