7 HAL ADC Generic Driver

Started by tha, June 28, 2022, 08:51:00 AM

Previous topic - Next topic

tha

Deinitialization of ADC
1.   ปิดใช้งาน the ADC interface
     –   ADC clock สามารถถูก hard reset และปิดใช้งานที่ RCC top level.
     –   Hard reset ของ ADC peripherals โดยใช้ macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
     –   ADC clock disable โดยใช้ the equivalent macro/functions ตามขั้นตอนการกำหนดค่านี้.
          ◦   ตัวอย่าง: ลงใน HAL_ADC_MspDeInit() (code location ที่แนะนำ) หรือด้วย device clock parameters
              configuration อื่น:
          ◦   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC
          ◦   PeriphClkInit.AdcClockSelection = RCC_ADCPLLCLK2_OFF
          ◦   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit)
2.   ADC pins configuration
     –   ปิดใช้งาน the clock สำหรับ the ADC GPIOs โดยใช้ macro __HAL_RCC_GPIOx_CLK_DISABLE()
3.   เป็นทางเลือก, ในกรณีของการใช้ของ ADC พร้อมด้วย interruptions:
     –   ปิดใช้งาน the NVIC สำหรับ ADC โดยใช้ function HAL_NVIC_EnableIRQ(ADCx_IRQn)
4.   เป็นทางเลือก, ในกรณีของการใช้ของ DMA:
     –   ยกเลิกการกำหนดค่าเริ่มต้น the DMA โดยใช้ function HAL_DMA_Init().
     –   ปิดใช้งาน the NVIC สำหรับ DMA โดยใช้ function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)

tha

Callback registration
The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, เมื่อเซ็ตเป็น 1, ยอมให้ the user กำหนดค่าแบบไดนามิคส์ the driver callbacks. ใช้ Functions @ref HAL_ADC_RegisterCallback() เพื่อรีจีสเตอร์ an interrupt callback.

Function @ref HAL_ADC_RegisterCallback() ยอมให้รีจีสเตอร์ callbacks ต่อไปนี้:
•   ConvCpltCallback : ADC conversion complete callback
•   ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
•   LevelOutOfWindowCallback : ADC analog watchdog 1 callback
•   ErrorCallback : ADC error callback
•   InjectedConvCpltCallback : ADC group injected conversion complete callback
•   MspInitCallback : ADC Msp Init callback
•   MspDeInitCallback : ADC Msp DeInit callback ฟังก์ชันนี้ใช้เป็น parameters the HAL peripheral handle, the Callback
     ID และ a pointer ไปยัง the user callback function.

tha

ใช้ function @ref HAL_ADC_UnRegisterCallback เพื่อรีเซ็ต a callback กลับเป็น the default weak function. @ref HAL_ADC_UnRegisterCallback ใช้เป็น parameters the HAL peripheral handle, และ the Callback ID. ฟังก์ชันนี้ยอมให้รีเซ็ต callbacks ต่อไปนี้:
•   ConvCpltCallback : ADC conversion complete callback
•   ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
•   LevelOutOfWindowCallback : ADC analog watchdog 1 callback
•   ErrorCallback : ADC error callback
•   InjectedConvCpltCallback : ADC group injected conversion complete callback
•   MspInitCallback : ADC Msp Init callback
•   MspDeInitCallback : ADC Msp DeInit callback


tha

โดยค่าเริ่มต้น, หลังจาก the @ref HAL_ADC_Init() และเมื่อ the state เป็น @ref HAL_ADC_STATE_RESET ทุก callbacks ถูกเซ็ตไปเป็น the corresponding weak functions: ตัวอย่าง @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback(). มีข้อยกเว้นสำหรับ MspInit and MspDeInit functions ที่ถูกรีเซ็ตไปเป็น the legacy weak functions ใน the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() เฉพาะเมื่อ callbacks เหล่านี้เป็น null (ไม่ได้รีจีสเตอร์ไว้ก่อน).

ถ้า MspInit or MspDeInit ไม่เป็น null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() จะเก็บและใช้ the user MspInit/MspDeInit callbacks (รีจีสเตอร์ไว้ก่อน) ไม่ว่า the state เป็นอย่างไร.

Callbacks สามารถถูก registered/unregistered ใน @ref HAL_ADC_STATE_READY state เท่านั้น. ยกเว้น MspInit/MspDeInit functions ที่สามารถถูก registered/unregistered ใน @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state, ดังนั้น registered (user) MspInit/DeInit callbacks สามารถถูกใช้ในระหว่าง the Init/DeInit.

ดังนั้น, the user ก่อนอื่นรีจีสเตอร์ the MspInit/MspDeInit user callbacks โดยใช้ @ref HAL_ADC_RegisterCallback() ก่อนการเรียก @ref HAL_ADC_DeInit() or @ref HAL_ADC_Init() function.

เมื่อ the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS ถูกเซ็ตเป็น 0 หรือไม่ได้กำหนด, the callback registration feature ไม่มีให้ใช้งานและทุก callbacks ถูกเซ็ตไปเป็น the corresponding weak functions.

tha

7.2.3 Initialization and de-initialization functions
section นี้จัดให้มี functions ที่ยอมให้:
•   เริ่มต้นและกำหนดค่า the ADC.
•   ยกเลิกการกำหนดค่าเริ่มต้น the ADC

section นี้บรรจุ the following APIs:
•   HAL_ADC_Init
•   HAL_ADC_DeInit
•   HAL_ADC_MspInit
•   HAL_ADC_MspDeInit

tha


tha


tha

7.2.7 Detailed description of functions

          HAL_ADC_Init
Function name
          HAL_StatusTypeDef HAL_ADC_Init (ADC_HandleTypeDef * hadc)
Function description
          กำหนดค่าเริ่มต้น the ADC peripheral และ regular group สอดคล้องกับ parameters ที่ระบุใน structure
          "ADC_InitTypeDef".
Parameters
          •   hadc: ADC handle
Return values
          •   HAL: status
Notes
          •   ตามข้อกำหนดเบื้องต้น, ADC clock ต้องถูกกำหนดค่าที่ RCC top level (clock source APB2). ดู commented
               example code ข้างล่างที่สามารถถูกคัดลอกและ uncommented ลงใน HAL_ADC_MspInit().
          •   ความสามารถในการอัปเดตพารามิเตอร์ได้ทันที: function นี้กำหนดค่าเริ่มต้น the ADC MSP (HAL_ADC_MspInit()) เฉพาะ
               เมื่อมาจาก ADC state reset. ตามติดการเรียกถึงฟังชั่นนี้สามารถถูกใช้เพื่อกำหนดค่าใหม่บาง parameters of
               ADC_InitTypeDef structure ได้ทันที, โดยไม่มีการแก้ไข MSP configuration. ถ้า ADC MSP ต้องถูกแก้ไขอีกครั้ง,
               HAL_ADC_DeInit() ต้องถูกเรียกก่อน HAL_ADC_Init(). The setting ของ parameters เหล่านี้ถูกปรับสภาพให้เป็น
               ADC state. สำหรับ parameters constraints, ดู comments of structure "ADC_InitTypeDef".
          •   function นี้กำหนดค่า the ADC ภายใน 2 scopes: scope of entire ADC และ scope of regular group. สำหรับ
               parameters details, ดู comments of structure "ADC_InitTypeDef".