STM32F1 HAL

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

Previous topic - Next topic

tha


tha

3 Overview of HAL drivers

The HAL drivers ถูกออกแบบเพื่อนำเสนอ a rich set of APIs และตอบโต้อย่างง่ายๆกับ the application upper layers. แต่ละ driver ประกอบด้วย a set of functions ครอบคลุม the most common peripheral features. การพัฒนาของแต่ละ driver ถูกขับเคลี่ยนโดย a common API ซึ่งทำให้เป็นมาตรฐาน the driver structure, the functions และ the parameter names.

The HAL drivers จะรวม a set of driver modules, แต่ละ module กำลังถูกเชื่อมต่อกับ a standalone peripheral. อย่างไรก็ตาม, ในบางกรณี, the module ถูกเชื่อมต่อกับ a peripheral functional mode. ตัวอย่างเช่น, หลาย modules มีอยู่สำหรับ the USART peripheral: UART driver module, USART driver module, SMARTCARD driver module and IRDA driver module.

tha

The HAL main features เป็นดังต่อไปนี้:
•   Cross-family portable set of APIs ที่ครอบคลุม the common peripheral features เช่นเดียวกับ extension APIs ในกรณี
     ของ specific peripheral features.
•   สาม API programming models: polling, interrupt and DMA.
•   APIs are RTOS compliant:
    –   Fully reentrant APIs
    –   การใช้ timeouts อย่างเป็นระบบใน polling mode.
•   การรองรับ peripheral multi-instance ช่วยให้สามารถเรียก API พร้อมกันสำหรับหลาย instances ของ a given peripheral
     (USART1, USART2...)
•   ทุก HAL APIs จัดให้มีใช้ user-callback functions mechanism:
    –   Peripheral Init/DeInit HAL APIs สามารถเรียก user-callback functions เพื่อดำเนินการ peripheral system level
         Initialization/De-Initialization (clock, GPIOs, interrupt, DMA)
    –   Peripherals interrupt events
    –   Error events.
•   Object locking mechanism: การเข้าถึงฮาร์ดแวร์อย่างปลอดภัยเพื่อป้องกันการเข้าถึงที่หลอกลวงหลายครั้งใน shared resources
•   Timeout ที่ใช้สำหรับทุก blocking processes: the timeout สามารถเป็น a simple counter หรือ a timebase.


tha

3.1 HAL and user-application files

3.1.1 HAL driver files
A HAL drivers ถูกประกอบด้วยชุดต่อไปนี้ของ files:




tha

3.1.2 User-application files
The minimum files ที่ต้องการเพื่อสร้าง an application โดยใช้ the HAL ถูกแสดงรายการในตารางข้างล่าง:


tha

The STM32Cube package มาพร้อมกับ ready-to-use project templates, หนึ่งโปรเจ็คสำหรับแต่ละ board ที่รองรับ. แต่ละ project บรรจุ the files ที่แสดงรายการไว้ข้างบนและ a preconfigured project สำหรับ the supported toolchains.

แต่ละ project template จัดให้มี empty main loop function และสามารถถูกใช้เป็น a starting point เพื่อให้คุ้นเคยกับ project settings สำหรับ STM32Cube. คุณลักษณะของมันเป็นดังต่อไปนี้:
•   มันบรรจุ the sources of HAL, CMSIS and BSP drivers ซึ่งเป็นส่วนประกอบที่น้อยที่สุดเพื่อพัฒนา a code บน a given board.
•   มันบรรจุ the include paths สำหรับทุก the firmware components.
•   มันกำหนด the STM32 device ที่รองรับ, และยอมให้การกำหนดค่า the CMSIS and HAL drivers สอดคล้องกัน.
•   มันจัดให้มี ready to use user files ที่กำหนดค่าไว้ล่วงหน้าดังที่กำหนดไว้ข้างล่าง:
    –   HAL is initialized
    –   SysTick ISR ที่จัดให้มีใช้สำหรับ HAL_Delay()
    –   System clock ที่กำหนดค่าไว้ด้วย the maximum device frequency.

Note:   ถ้า an existing project ถูกก๊อปปี้ไปวางใส่ที่อื่น, ดังนั้น include paths ต้องถูกอัฟเดต.


tha

3.2 HAL data structures

แต่ละ HAL driver สามารถบรรจุ the following data structures:
•   Peripheral handle structures
•   Initialization and configuration structures
•   Specific process structures.

3.2.1 Peripheral handle structures
The APIs มี a modular generic multi-instance architecture ที่ยอมให้การทำงานกับ several IP instances พร้อมกัน.

PPP_HandleTypeDef *handle เป็น the main structure ที่ถูกจัดให้มีใช้ใน the HAL drivers. มันจัดการ the peripheral/module configuration and registers และฝังทุก the structures and variables ที่จำเป็นต้องตามติด the peripheral device flow.

The peripheral handle ถูกใช้สำหรับจุดมุ่งหมายต่อไปนี้:
•   Multi-instance support: แต่ละ peripheral/module instance มี handle ของตัวเอง. ด้วยเหตุนี้ instance resources จึงเป็น
     อิสระ.
•   Peripheral process intercommunication: the handle ถูกใช้เพื่อจัดการ shared data resources ระหว่าง the process
     routines.
     Example: global pointers, DMA handles, state machine.
•   Storage : handle นี้ถูกใช้เพื่อจัดการ global variables ภายใน a given HAL driver ด้วย.

ตัวอย่างของ peripheral structure ถูกแสดงไว้ข้างล่าง:

tha

Note:  1. The multi-instance feature บอกเป็นนัยว่าทุก the APIs ที่ใช้ใน the application นั้นถูกกลับเข้ามาใหม่และหลีกเลี่ยงการ
              ใช้ global variables เนื่องจาก subroutines สามารถล้มเหลวที่จะถูกกลับเข้ามาใหม่ถ้าพวกมันวางใจ a global variable ว่า
              ยังคงไม่เปลี่ยนแต่ variable นั้นถูกโมดิฟายด์เมื่อ the subroutine ถูกเรียกใช้ซ้ำ ด้วยเหตุนี้ เคารพกฎต่อไปนี้:
              –   Reentrant code ไม่เก็บ any static (or global) non-constant data: reentrant functions สามารถทำงานด้วย
                   global data. ตัวอย่างเช่น, a reentrant interrupt service routine สามารถจับ a piece of hardware status เพื่อ
                   ทำงานด้วย (ตัวอย่างเช่น serial port read buffer) ซึ่งไม่เป็นเพียง global, แต่ volatile. ยังคงใช้งานได้ทั่วไป
                   ของ static variables และ global data ไม่ถูกแนะนำ, ในแง่ที่ว่า มีเพียง atomic read-modify-write instructions
                   ควรถูกใช้ในตัวแปรเหล่านี้. มันไม่ควรเป็นไปได้สำหรับ an interrupt หรือ signal เกิดขึ้นในระหว่างการปฏิบัติของ an
                   instruction ดังกล่าว.
              –   Reentrant code ไม่โมดิฟายด์ code ของตัวเอง.
          2. เมื่อ a peripheral สามารถจัดการหลาย processes พร้อมกันโดยใช้ the DMA (full duplex case), the DMA interface
              รับมือสำหรับแต่ละ process ถูกเพิ่มใน the PPP_HandleTypeDef.
          3. สำหรับ the shared and system peripherals, ไม่มี handle หรือ instance object ถูกใช้. The peripherals ที่เกี่ยวข้อง
              โดยข้อยกเว้นนี้มีดังต่อไปนี้:
              –   GPIO
              –   SYSTICK
              –   NVIC
              –   PWR
              –   RCC
              –   FLASH