STM32F1 HAL

Started by tha, June 14, 2022, 09:22:55 AM

Previous topic - Next topic

tha

3.10 HAL configuration

The configuration file, stm32f1xx_hal_conf.h, อนุญาตให้ปรับแต่ง the drivers สำหรับ the user application ได้. การแก้ไขการกำหนดค่านี้ไม่บังคับ: the application สามารถใช้ the default configuration โดยไม่มีการแก้ไขใดๆ. เพื่อกำหนดค่า parameters เหล่านี้, ผู้ใช้ควร enable, disable or modify บางตัวเลือกนี้โดย uncommenting, commenting or modifying ค่าของ the related define statements ดังอธิบายในตารางข้างล่าง:



Note:   The stm32f1xx_hal_conf_template.h file ถูกวางอยู่ใน the HAL drivers Inc folder. มันควรถูกก๊อปปี้ไปยัง the user
            folder, เปลี่ยนชื่อหรือแก้ไขตามที่อธิบายข้างบน.

Note:   โดยค่าเริ่มต้น, the values ที่กำหนดใน the stm32f1xx_hal_conf_template.h file เป็นเหมือนกับที่ใช้สำหรับตัวอย่างและ
            การสาธิต. ทุก HAL include files ถูกเปิดใช้งานดังนั้นพวกมันสามารถถูกใช้ใน the user code โดยปราศจากการแก้ไข

tha

3.11 HAL system peripheral handling

บทนี้ให้ภาพรวมของวิธีที่ the system peripherals ถูกจัดการโดย the HAL drivers. The full API list ถูกจัดให้มีภายในแต่ละ peripheral driver description section.

3.11.1 Clock
Two main functions สามารถถูกใช้เพื่อกำหนดค่า the system clock:
•   HAL_RCC_OscConfig (RCC_OscInitTypeDef *RCC_OscInitStruct). ฟังชั่นนี้กำหนดค่า/เปิดใช้งานหลาย clock sources
     (HSE, HSI, LSE, LSI, PLL).
•   HAL_RCC_ClockConfig (RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency). ฟังชั่นนี้
     –   เลือก the system clock source
     –   กำหนดค่า AHB, APB1 and APB2 clock dividers
     –   กำหนดค่า the number of Flash memory wait states
     –   อัฟเดต the SysTick configuration เมื่อ HCLK clock เปลี่ยน.

บาง peripheral clocks ไม่ได้มาจาก the system clock (อย่างเช่น RTC, USB). ในกรณีนี้, the clock configuration ถูกกระทำโดย an extended API ที่กำหนดใน stm32f1xx_hal_rcc_ex.c: HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit).
Additional RCC HAL driver functions มีให้ใช้ประโยชน์:
•   HAL_RCC_DeInit() Clock de-initialization function ที่คืนกลับ clock configuration ไปเป็น reset state
•   ได้รับ clock functions ที่ยอมให้ดึง various clock configurations (system clock, HCLK, PCLK1, PCLK2, ...)
•   MCO and CSS configuration functions

A set of macros ถูกกำหนดใน stm32f1xx_hal_rcc.h and stm32f1xx_hal_rcc_ex.h. พวกมันอนุญาตให้ปฏิบัติการทำงานเบื้องต้นใน RCC block registers, อย่างเช่น peripherals clock gating/reset control:
•   __HAL_PPP_CLK_ENABLE/__HAL_PPP_CLK_DISABLE to enable/disable the peripheral clock
•   __HAL_PPP_FORCE_RESET/__HAL_PPP_RELEASE_RESET to force/release peripheral reset
•   __HAL_PPP_CLK_SLEEP_ENABLE/__HAL_PPP_CLK_SLEEP_DISABLE to enable/disable the peripheral clock ใน
    ระหว่าง Sleep mode.

tha

3.11.2 GPIOs
GPIO HAL APIs เป็นดังต่อไปนี้:
•   HAL_GPIO_Init() / HAL_GPIO_DeInit()
•   HAL_GPIO_ReadPin() / HAL_GPIO_WritePin()
•   HAL_GPIO_TogglePin ().

นอกเหนือจาก standard GPIO modes (input, output, analog), the pin mode สามาถถูกกำหนดค่าเป็น EXTI พร้อมด้วย interrupt หรือ event generation.
เมื่อเลือก EXTI mode พร้อมด้วย interrupt generation, ผู้ใช้ต้องเรียก HAL_GPIO_EXTI_IRQHandler() จาก stm32f1xx_it.c และจัดให้มีใช้ HAL_GPIO_EXTI_Callback()
ตารางข้างล่างอธิบาย the GPIO_InitTypeDef structure field.


โปรดดูข้างล่าง typical GPIO configuration examples:
•   การกำหนดค่า GPIOs เป็น output push-pull เพื่อขับ external LEDs:


•   การกำหนดค่า PA0 เป็น external interrupt ที่มี falling edge sensitivity:


tha

3.11.3 Cortex® NVIC and SysTick timer
The Cortex® HAL driver, stm32f1xx_hal_cortex.c, จัดให้มี APIs เพื่อจัดการ NVIC and SysTick. The supported APIs รวม:
•   HAL_NVIC_SetPriority()/ HAL_NVIC_SetPriorityGrouping()
•   HAL_NVIC_GetPriority() / HAL_NVIC_GetPriorityGrouping()
•   HAL_NVIC_EnableIRQ()/HAL_NVIC_DisableIRQ()
•   HAL_NVIC_SystemReset()
•   HAL_SYSTICK_IRQHandler()
•   HAL_NVIC_GetPendingIRQ() / HAL_NVIC_SetPendingIRQ () / HAL_NVIC_ClearPendingIRQ()
•   HAL_NVIC_GetActive(IRQn)
•   HAL_SYSTICK_Config()
•   HAL_SYSTICK_CLKSourceConfig()
•   HAL_SYSTICK_Callback()

tha

3.11.4 PWR
The PWR HAL driver จัดการ power management. The features ที่แชร์ระหว่างทุก STM32 Series ถูกแสดงรายการข้างล่าง:
•   PVD configuration, enabling/disabling and interrupt handling
    –   HAL_PWR_ConfigPVD()
    –   HAL_PWR_EnablePVD() / HAL_PWR_DisablePVD()
    –   HAL_PWR_PVD_IRQHandler()
    –   HAL_PWR_PVDCallback()
•   Wakeup pin configuration
    –   HAL_PWR_EnableWakeUpPin() / HAL_PWR_DisableWakeUpPin()
•   Low-power mode entry
    –   HAL_PWR_EnterSLEEPMode()
    –   HAL_PWR_EnterSTOPMode()
    –   HAL_PWR_EnterSTANDBYMode()

tha

3.11.5 EXTI
The EXTI ไม่ถูกถือว่าเป็น a standalone peripheral แต่ค่อนข้างเป็น a service ที่ใช้โดย peripheral อื่น, ที่ถูกจัดการผ่านทาง EXTI HAL APIs. นอกจากนี้, แต่ละ peripheral HAL driver จัดให้มีใช้ the associated EXTI configuration and function เป็น macros ใน header file ของมัน.

The first 16 EXTI lines ที่เชื่อมต่อถึง the GPIOs ถูกจัดการภายใน the GPIO driver. The GPIO_InitTypeDef structure อนุญาตให้การกำหนดค่า an I/O เป็น external interrupt or external event.

The EXTI lines ที่เชื่อมต่อภายในถึง the PVD, RTC, USB, and Ethernet ถูกกำหนดค่าภายใน the HAL drivers ของ peripheral เหล่านั้นผ่านทาง the macros ทีให้ในตารางข้างล่าง.

The EXTI internal connections ขึ้นอยู่กับ the targeted STM32 microcontroller (ดูที่ the product datasheet สำหรับรายละเอียดเพิ่มเติม):



ถ้า the EXTI interrupt mode ถูกเลือก, the user application ต้องเลือก HAL_PPP_FUNCTION_IRQHandler() (ตัวอย่างเช่น HAL_PWR_PVD_IRQHandler()), จาก stm32f1xx_it.c file, และจัดให้มีใช้ HAL_PPP_FUNCTIONCallback() callback function (ตัวอย่างเช่น HAL_PWR_PVDCallback().

tha

3.11.6 DMA
The DMA HAL driver ยอมให้การเปิดใช้งานและการกำหนดค่า the peripheral ถูกเชื่อมต่อถึง the DMA Channels (ยกเว้นสำหรับ internal SRAM/FLASH memory ซึ่งไม่ต้องการการกำหนดค่าเริ่มต้นใดๆ). ดูที่ the product reference manual สำหรับรายละเอียดเกี่ยวกับ the DMA request ที่ตรงกันกับแต่ละ peripheral.

สำหรับ a given channel, HAL_DMA_Init() API ยอมให้การโปรแกรม the required configuration ผ่านทาง the following parameters:
•   Transfer direction
•   Source and destination data formats
•   Circular, Normal or peripheral flow control mode
•   Channel priority level
•   Source and destination Increment mode

Two operating modes มีให้ใช้ประโยชน์:

•   Polling mode I/O operation
    1.   ใช้ HAL_DMA_Start() เพื่อสตาร์ท DMA transfer เมื่อ the source and destination addresses and the Length of
          data ที่จะถูกส่งถ่ายถูกกำหนดค่าแล้ว.
    2.   ใช้ HAL_DMA_PollForTransfer() เพื่อวนตรวจหา the end of current transfer. ในกรณีนี้ a fixed timeout สามารถถูก
          กำหนดค่าขึ้นอยู่กับ the user application.

•   Interrupt mode I/O operation
    1.   กำหนดค่า the DMA interrupt priority โดยใช้ HAL_NVIC_SetPriority()
    2.   เปิดใช้งานe the DMA IRQ handler โดยใช้ HAL_NVIC_EnableIRQ()
    3.   ใช้ HAL_DMA_Start_IT() เพื่อสตาร์ท DMA transfer เมื่อ the source and destination addresses and the length of
          data ที่จะถูกส่งถ่ายถูกกำหนดค่าแล้ว. ในกรณีนี้ the DMA interrupt จะถูกกำหนดค่า.
    4.   ใช้ HAL_DMA_IRQHandler() ที่เรียกภายใต้ DMA_IRQHandler() Interrupt subroutine
    5.   เมื่อ data transfer เสร็จสมบูรณ์, HAL_DMA_IRQHandler() function ถูกปฏิบัติและ a user function สามารถถูกเรียกโดย
          การปรับแต่งตามต้องการ XferCpltCallback and XferErrorCallback function pointer (ตัวอย่างเช่น a member of DMA
          handle structure).

tha

DMA HAL driver macros ที่ใช้มากที่สุดมีดังนี้:
•   __HAL_DMA_ENABLE: enables the specified DMA channel.
•   __HAL_DMA_DISABLE: disables the specified DMA channel.
•   __HAL_DMA_GET_FLAG: gets the DMA channel pending flags.
• _  _HAL_DMA_CLEAR_FLAG: clears the DMA channel pending flags.
•   __HAL_DMA_ENABLE_IT: enables the specified DMA channel interrupts.
•   __HAL_DMA_DISABLE_IT: disables the specified DMA channel interrupts.
•   __HAL_DMA_GET_IT_SOURCE: เช็คว่า the specified DMA channel interrupt ถูกเปิดใช้งานแล้วหรือไม่.

Note:   เมื่อ a peripheral ถูกใช้ใน DMA mode, the DMA initialization ควรถูกทำใน the HAL_PPP_MspInit() callback.
            นอกจากนี้, the user application ควรเชื่อมโยง the DMA handle กับ the PPP handle (ดูที่ section "HAL IO
            operation functions").

Note:   DMA channel callbacks จำเป็นต้องถูกกำหนดค่าเริ่มต้นโดย the user application เฉพาะในกรณีของ memory-to-
            memory transfer. อย่างไรก็ตามเมื่อ peripheral-to-memory transfers ถูกใช้, these callbacks ถูกกำหนดค่าเริ่มต้นโดย
            อัตโนมัติโดยการเรียก a process API function ที่ใช้ the DMA.