The purpose of this hands-on is to use "Code Preview" to add a functionality to an existing project without regenerating the code. By following the steps outlined below, user will learn how this feature improve efficiency.
- STM32CubeMX2 1.0.0
- VSCode (with STM32Cube for VSCode extension)
- Nucleo-C562RE
- USB type C cable
Let's discover one of the eventual usage of the "Code preview" feature.
Use case : From an existing project, how can I associate the ADC with a DMA transfert feature without regenerating the code?
To answer to this question, we'll follow the steps below:
- Run STM32CubeMX2 1.0.0
Code : copy the code below in the file mx_adc1.c
static hal_dma_node_t DMA_Node_ADC1;
static hal_dma_handle_t hLPDMA1_CH0;Code : copy the code below in the file mx_adc1.c
hal_adc_handle_t *mx_adc1_init(void)
{....
/* Configure DMA */
if (HAL_DMA_Init(&hLPDMA1_CH0, HAL_LPDMA1_CH0) != HAL_OK)
{
return NULL;
}
HAL_RCC_LPDMA1_EnableClock();
hal_dma_direct_xfer_config_t xfer_cfg_adc1_dma;
xfer_cfg_adc1_dma.request = HAL_LPDMA1_REQUEST_ADC1;
xfer_cfg_adc1_dma.direction = HAL_DMA_DIRECTION_PERIPH_TO_MEMORY;
xfer_cfg_adc1_dma.src_inc = HAL_DMA_SRC_ADDR_FIXED;
xfer_cfg_adc1_dma.dest_inc = HAL_DMA_DEST_ADDR_INCREMENTED;
xfer_cfg_adc1_dma.src_data_width = HAL_DMA_SRC_DATA_WIDTH_HALFWORD;
xfer_cfg_adc1_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_HALFWORD;
xfer_cfg_adc1_dma.priority = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;
if (HAL_DMA_SetConfigPeriphLinkedListCircularXfer(&hLPDMA1_CH0, &DMA_Node_ADC1, &xfer_cfg_adc1_dma) != HAL_OK)
{
return NULL;
}
/* Enable the interruption for LPDMA1_CH0 */
HAL_CORTEX_NVIC_SetPriority(LPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(LPDMA1_CH0_IRQn);
/* Link DMA handle to ADC handle */
HAL_ADC_REG_SetDMA(&hADC1, &hLPDMA1_CH0);
...}Code : copy the code below in the file mx_adc1.c
void mx_adc1_deinit(void)
{...
/* De-initialize the DMA channel */
HAL_DMA_DeInit(&hLPDMA1_CH0);
/* Disable the interruption for DMA */
HAL_CORTEX_NVIC_DisableIRQ(LPDMA1_CH0_IRQn);
}Code : copy the code below in the file mx_adc1.c
void LPDMA1_CH0_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}Code : copy the code below in the file mx_adc1.h
/******************************************************************************/
/* LPDMA1 channel0 global interrupt */
/******************************************************************************/
void LPDMA1_CH0_IRQHandler(void);- Set
USE_HAL_DMAto 1U in the file "stm32C5xx_hal_conf.h"
- Set
USE_HAL_DMA_LINKEDLISTto 1U in the file "stm32C5xx_hal_conf.h"












