1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_hal_mdios.c
4   * @author  MCD Application Team
5   * @brief   MDIOS HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the MDIOS Peripheral.
8   *           + Initialization and de-initialization functions
9   *           + IO operation functions
10   *           + Peripheral Control functions
11   *
12   *
13   ******************************************************************************
14   * @attention
15   *
16   * Copyright (c) 2017 STMicroelectronics.
17   * All rights reserved.
18   *
19   * This software is licensed under terms that can be found in the LICENSE file
20   * in the root directory of this software component.
21   * If no LICENSE file comes with this software, it is provided AS-IS.
22   *
23   ******************************************************************************
24   @verbatim
25  ===============================================================================
26                         ##### How to use this driver #####
27  ===============================================================================
28     [..]
29     The MDIOS HAL driver can be used as follow:
30 
31     (#) Declare a MDIOS_HandleTypeDef handle structure.
32 
33     (#) Initialize the MDIOS low level resources by implementing the HAL_MDIOS_MspInit() API:
34         (##) Enable the MDIOS interface clock.
35         (##) MDIOS pins configuration:
36             (+++) Enable clocks for the MDIOS GPIOs.
37             (+++) Configure the MDIOS pins as alternate function.
38         (##) NVIC configuration if you need to use interrupt process:
39             (+++) Configure the MDIOS interrupt priority.
40             (+++) Enable the NVIC MDIOS IRQ handle.
41 
42     (#) Program the Port Address and the Preamble Check in the Init structure.
43 
44     (#) Initialize the MDIOS registers by calling the HAL_MDIOS_Init() API.
45 
46     (#) Perform direct slave read/write operations using the following APIs:
47         (##) Read the value of a DINn register: HAL_MDIOS_ReadReg()
48         (##) Write a value to a DOUTn register: HAL_MDIOS_WriteReg()
49 
50     (#) Get the Master read/write operations flags using the following APIs:
51         (##) Bit map of DOUTn registers read by Master: HAL_MDIOS_GetReadRegAddress()
52         (##) Bit map of DINn registers written by Master : HAL_MDIOS_GetWrittenRegAddress()
53 
54     (#) Clear the read/write flags using the following APIs:
55         (##) Clear read flags of a set of registers: HAL_MDIOS_ClearReadRegAddress()
56         (##) Clear write flags of a set of registers: HAL_MDIOS_ClearWriteRegAddress()
57 
58     (#) Enable interrupts on events using HAL_MDIOS_EnableEvents(), when called
59         the MDIOS will generate an interrupt in the following cases:
60         (##) a DINn register written by the Master
61         (##) a DOUTn register read by the Master
62         (##) an error occur
63 
64         (@) A callback is executed for each generated interrupt, so the driver provide the following
65             HAL_MDIOS_WriteCpltCallback(), HAL_MDIOS_ReadCpltCallback() and HAL_MDIOS_ErrorCallback()
66         (@) HAL_MDIOS_IRQHandler() must be called from the MDIOS IRQ Handler, to handle the interrupt
67             and execute the previous callbacks
68 
69     (#) Reset the MDIOS peripheral and all related resources by calling the HAL_MDIOS_DeInit() API.
70         (##) HAL_MDIOS_MspDeInit() must be implemented to reset low level resources
71             (GPIO, Clocks, NVIC configuration ...)
72 
73   *** Callback registration ***
74   =============================================
75 
76   The compilation define  USE_HAL_MDIOS_REGISTER_CALLBACKS when set to 1
77   allows the user to configure dynamically the driver callbacks.
78   Use Function @ref HAL_MDIOS_RegisterCallback() to register an interrupt callback.
79 
80   Function @ref HAL_MDIOS_RegisterCallback() allows to register following callbacks:
81     (+) WriteCpltCallback  : Write Complete Callback.
82     (+) ReadCpltCallback   : Read Complete Callback.
83     (+) ErrorCallback      : Error Callback.
84     (+) WakeUpCallback     : Wake UP Callback
85     (+) MspInitCallback    : MspInit Callback.
86     (+) MspDeInitCallback  : MspDeInit Callback.
87 
88   This function takes as parameters the HAL peripheral handle, the Callback ID
89   and a pointer to the user callback function.
90 
91   Use function @ref HAL_MDIOS_UnRegisterCallback() to reset a callback to the default
92   weak function.
93   @ref HAL_MDIOS_UnRegisterCallback takes as parameters the HAL peripheral handle,
94   and the Callback ID.
95   This function allows to reset following callbacks:
96     (+) WriteCpltCallback  : Write Complete Callback.
97     (+) ReadCpltCallback   : Read Complete Callback.
98     (+) ErrorCallback      : Error Callback.
99     (+) WakeUpCallback     : Wake UP Callback
100     (+) MspInitCallback    : MspInit Callback.
101     (+) MspDeInitCallback  : MspDeInit Callback.
102 
103   By default, after the HAL_MDIOS_Init and when the state is HAL_MDIOS_STATE_RESET
104   all callbacks are set to the corresponding weak functions:
105   examples @ref HAL_MDIOS_WriteCpltCallback(), @ref HAL_MDIOS_ReadCpltCallback().
106   Exception done for MspInit and MspDeInit functions that are
107   reset to the legacy weak function in the HAL_MDIOS_Init/ @ref HAL_MDIOS_DeInit only when
108   these callbacks are null (not registered beforehand).
109   if not, MspInit or MspDeInit are not null, the HAL_MDIOS_Init/ @ref HAL_MDIOS_DeInit
110   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
111 
112   Callbacks can be registered/unregistered in HAL_MDIOS_STATE_READY state only.
113   Exception done MspInit/MspDeInit that can be registered/unregistered
114   in HAL_MDIOS_STATE_READY or HAL_MDIOS_STATE_RESET state,
115   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
116   In that case first register the MspInit/MspDeInit user callbacks
117   using @ref HAL_MDIOS_RegisterCallback() before calling @ref HAL_MDIOS_DeInit
118   or HAL_MDIOS_Init function.
119 
120   When The compilation define USE_HAL_MDIOS_REGISTER_CALLBACKS is set to 0 or
121   not defined, the callback registration feature is not available and all callbacks
122   are set to the corresponding weak functions.
123 
124 
125   @endverbatim
126   */
127 
128 /* Includes ------------------------------------------------------------------*/
129 #include "stm32h7rsxx_hal.h"
130 
131 /** @addtogroup STM32H7RSxx_HAL_Driver
132   * @{
133   */
134 #if defined (MDIOS)
135 /** @defgroup MDIOS  MDIOS
136   * @brief HAL MDIOS module driver
137   * @{
138   */
139 #ifdef HAL_MDIOS_MODULE_ENABLED
140 
141 
142 
143 /* Private typedef -----------------------------------------------------------*/
144 /* Private define ------------------------------------------------------------*/
145 #define MDIOS_PORT_ADDRESS_SHIFT        ((uint32_t)8)
146 #define  MDIOS_ALL_REG_FLAG             ((uint32_t)0xFFFFFFFFU)
147 #define  MDIOS_ALL_ERRORS_FLAG          ((uint32_t)(MDIOS_SR_PERF | MDIOS_SR_SERF | MDIOS_SR_TERF))
148 
149 #define MDIOS_DIN_BASE_ADDR             (MDIOS_BASE + 0x100U)
150 #define MDIOS_DOUT_BASE_ADDR            (MDIOS_BASE + 0x180U)
151 
152 /* Private macro -------------------------------------------------------------*/
153 /* Private variables ---------------------------------------------------------*/
154 /* Private function prototypes -----------------------------------------------*/
155 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
156 static void MDIOS_InitCallbacksToDefault(MDIOS_HandleTypeDef *hmdios);
157 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
158 /* Private functions ---------------------------------------------------------*/
159 /* Exported functions --------------------------------------------------------*/
160 /** @defgroup MDIOS_Exported_Functions MDIOS Exported Functions
161   * @{
162   */
163 
164 /** @defgroup MDIOS_Exported_Functions_Group1 Initialization/de-initialization functions
165   *  @brief    Initialization and Configuration functions
166   *
167 @verbatim
168 ===============================================================================
169             ##### Initialization and Configuration functions #####
170  ===============================================================================
171     [..]
172     This subsection provides a set of functions allowing to initialize the MDIOS
173       (+) The following parameters can be configured:
174         (++) Port Address
175         (++) Preamble Check
176 
177 @endverbatim
178   * @{
179   */
180 
181 /**
182   * @brief  Initializes the MDIOS according to the specified parameters in
183   *         the MDIOS_InitTypeDef and creates the associated handle .
184   * @param  hmdios: pointer to a MDIOS_HandleTypeDef structure that contains
185   *         the configuration information for MDIOS module
186   * @retval HAL status
187   */
HAL_MDIOS_Init(MDIOS_HandleTypeDef * hmdios)188 HAL_StatusTypeDef HAL_MDIOS_Init(MDIOS_HandleTypeDef *hmdios)
189 {
190   uint32_t tmpcr;
191 
192   /* Check the MDIOS handle allocation */
193   if (hmdios == NULL)
194   {
195     return HAL_ERROR;
196   }
197 
198   /* Check the parameters */
199   assert_param(IS_MDIOS_ALL_INSTANCE(hmdios->Instance));
200   assert_param(IS_MDIOS_PORTADDRESS(hmdios->Init.PortAddress));
201   assert_param(IS_MDIOS_PREAMBLECHECK(hmdios->Init.PreambleCheck));
202 
203 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
204 
205   if (hmdios->State == HAL_MDIOS_STATE_RESET)
206   {
207     MDIOS_InitCallbacksToDefault(hmdios);
208 
209     if (hmdios->MspInitCallback == NULL)
210     {
211       hmdios->MspInitCallback = HAL_MDIOS_MspInit;
212     }
213 
214     /* Init the low level hardware */
215     hmdios->MspInitCallback(hmdios);
216   }
217 
218 #else
219 
220   if (hmdios->State == HAL_MDIOS_STATE_RESET)
221   {
222     /* Init the low level hardware */
223     HAL_MDIOS_MspInit(hmdios);
224   }
225 
226 #endif /* (USE_HAL_MDIOS_REGISTER_CALLBACKS) */
227 
228   /* Change the MDIOS state */
229   hmdios->State = HAL_MDIOS_STATE_BUSY;
230 
231   /* Get the MDIOS CR value */
232   tmpcr = hmdios->Instance->CR;
233 
234   /* Clear PORT_ADDRESS, DPC and EN bits */
235   tmpcr &= ((uint32_t)~(MDIOS_CR_EN | MDIOS_CR_DPC | MDIOS_CR_PORT_ADDRESS));
236 
237   /* Set MDIOS control parameters and enable the peripheral */
238   tmpcr |= (uint32_t)(((hmdios->Init.PortAddress) << MDIOS_PORT_ADDRESS_SHIFT)    | \
239                       (hmdios->Init.PreambleCheck) | \
240                       (MDIOS_CR_EN));
241 
242   /* Write the MDIOS CR */
243   hmdios->Instance->CR = tmpcr;
244 
245   hmdios->ErrorCode = HAL_MDIOS_ERROR_NONE;
246 
247   /* Change the MDIOS state */
248   hmdios->State = HAL_MDIOS_STATE_READY;
249 
250   /* Release Lock */
251   __HAL_UNLOCK(hmdios);
252 
253   /* Return function status */
254   return HAL_OK;
255 
256 }
257 
258 /**
259   * @brief  DeInitializes the MDIOS peripheral.
260   * @param  hmdios: MDIOS handle
261   * @retval HAL status
262   */
HAL_MDIOS_DeInit(MDIOS_HandleTypeDef * hmdios)263 HAL_StatusTypeDef HAL_MDIOS_DeInit(MDIOS_HandleTypeDef *hmdios)
264 {
265   /* Check the MDIOS handle allocation */
266   if (hmdios == NULL)
267   {
268     return HAL_ERROR;
269   }
270 
271   /* Check the parameters */
272   assert_param(IS_MDIOS_ALL_INSTANCE(hmdios->Instance));
273 
274   /* Change the MDIOS state */
275   hmdios->State = HAL_MDIOS_STATE_BUSY;
276 
277   /* Disable the Peripheral */
278   __HAL_MDIOS_DISABLE(hmdios);
279 
280 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
281 
282   if (hmdios->MspDeInitCallback == NULL)
283   {
284     hmdios->MspDeInitCallback = HAL_MDIOS_MspDeInit;
285   }
286   /* DeInit the low level hardware */
287   hmdios->MspDeInitCallback(hmdios);
288 #else
289 
290   /* DeInit the low level hardware */
291   HAL_MDIOS_MspDeInit(hmdios);
292 
293 #endif /* (USE_HAL_MDIOS_REGISTER_CALLBACKS) */
294 
295   /* Change the MDIOS state */
296   hmdios->State = HAL_MDIOS_STATE_RESET;
297 
298   /* Release Lock */
299   __HAL_UNLOCK(hmdios);
300 
301   /* Return function status */
302   return HAL_OK;
303 }
304 
305 /**
306   * @brief  MDIOS MSP Init
307   * @param  hmdios: mdios handle
308   * @retval None
309   */
HAL_MDIOS_MspInit(MDIOS_HandleTypeDef * hmdios)310 __weak void HAL_MDIOS_MspInit(MDIOS_HandleTypeDef *hmdios)
311 {
312   /* Prevent unused argument(s) compilation warning */
313   UNUSED(hmdios);
314 
315   /* NOTE : This function should not be modified, when the callback is needed,
316             the HAL_MDIOS_MspInit can be implemented in the user file
317    */
318 }
319 
320 /**
321   * @brief  MDIOS MSP DeInit
322   * @param  hmdios: mdios handle
323   * @retval None
324   */
HAL_MDIOS_MspDeInit(MDIOS_HandleTypeDef * hmdios)325 __weak void HAL_MDIOS_MspDeInit(MDIOS_HandleTypeDef *hmdios)
326 {
327   /* Prevent unused argument(s) compilation warning */
328   UNUSED(hmdios);
329 
330   /* NOTE : This function should not be modified, when the callback is needed,
331             the HAL_MDIOS_MspDeInit can be implemented in the user file
332    */
333 }
334 
335 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
336 /**
337   * @brief  Register a User MDIOS Callback
338   *         To be used instead of the weak predefined callback
339   * @param hmdios mdios handle
340   * @param CallbackID ID of the callback to be registered
341   *        This parameter can be one of the following values:
342   *          @arg @ref HAL_MDIOS_WRITE_COMPLETE_CB_ID Write Complete Callback ID
343   *          @arg @ref HAL_MDIOS_READ_COMPLETE_CB_ID  Read Complete Callback ID
344   *          @arg @ref HAL_MDIOS_ERROR_CB_ID          Error Callback ID
345   *          @arg @ref HAL_MDIOS_WAKEUP_CB_ID         Wake Up Callback ID
346   *          @arg @ref HAL_MDIOS_MSPINIT_CB_ID        MspInit callback ID
347   *          @arg @ref HAL_MDIOS_MSPDEINIT_CB_ID      MspDeInit callback ID
348   * @param pCallback pointer to the Callback function
349   * @retval status
350   */
HAL_MDIOS_RegisterCallback(MDIOS_HandleTypeDef * hmdios,HAL_MDIOS_CallbackIDTypeDef CallbackID,pMDIOS_CallbackTypeDef pCallback)351 HAL_StatusTypeDef HAL_MDIOS_RegisterCallback(MDIOS_HandleTypeDef *hmdios, HAL_MDIOS_CallbackIDTypeDef CallbackID,
352                                              pMDIOS_CallbackTypeDef pCallback)
353 {
354   HAL_StatusTypeDef status = HAL_OK;
355 
356   if (pCallback == NULL)
357   {
358     /* Update the error code */
359     hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
360 
361     return HAL_ERROR;
362   }
363   /* Process locked */
364   __HAL_LOCK(hmdios);
365 
366   if (hmdios->State == HAL_MDIOS_STATE_READY)
367   {
368     switch (CallbackID)
369     {
370       case HAL_MDIOS_WRITE_COMPLETE_CB_ID :
371         hmdios->WriteCpltCallback = pCallback;
372         break;
373 
374       case HAL_MDIOS_READ_COMPLETE_CB_ID :
375         hmdios->ReadCpltCallback = pCallback;
376         break;
377 
378       case HAL_MDIOS_ERROR_CB_ID :
379         hmdios->ErrorCallback = pCallback;
380         break;
381 
382       case HAL_MDIOS_WAKEUP_CB_ID :
383         hmdios->WakeUpCallback = pCallback;
384         break;
385 
386       case HAL_MDIOS_MSPINIT_CB_ID :
387         hmdios->MspInitCallback = pCallback;
388         break;
389 
390       case HAL_MDIOS_MSPDEINIT_CB_ID :
391         hmdios->MspDeInitCallback = pCallback;
392         break;
393 
394       default :
395         /* Update the error code */
396         hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
397         /* Return error status */
398         status =  HAL_ERROR;
399         break;
400     }
401   }
402   else if (hmdios->State == HAL_MDIOS_STATE_RESET)
403   {
404     switch (CallbackID)
405     {
406       case HAL_MDIOS_MSPINIT_CB_ID :
407         hmdios->MspInitCallback = pCallback;
408         break;
409 
410       case HAL_MDIOS_MSPDEINIT_CB_ID :
411         hmdios->MspDeInitCallback = pCallback;
412         break;
413 
414       default :
415         /* Update the error code */
416         hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
417         /* Return error status */
418         status =  HAL_ERROR;
419         break;
420     }
421   }
422   else
423   {
424     /* Update the error code */
425     hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
426     /* Return error status */
427     status =  HAL_ERROR;
428   }
429 
430   /* Release Lock */
431   __HAL_UNLOCK(hmdios);
432 
433   return status;
434 }
435 
436 /**
437   * @brief  Unregister an MDIOS Callback
438   *         MDIOS callback is redirected to the weak predefined callback
439   * @param hmdios mdios handle
440   * @param CallbackID ID of the callback to be unregistered
441   *        This parameter can be one of the following values:
442   *          @arg @ref HAL_MDIOS_WRITE_COMPLETE_CB_ID Write Complete Callback ID
443   *          @arg @ref HAL_MDIOS_READ_COMPLETE_CB_ID  Read Complete Callback ID
444   *          @arg @ref HAL_MDIOS_ERROR_CB_ID          Error Callback ID
445   *          @arg @ref HAL_MDIOS_WAKEUP_CB_ID         Wake Up Callback ID
446   *          @arg @ref HAL_MDIOS_MSPINIT_CB_ID        MspInit callback ID
447   *          @arg @ref HAL_MDIOS_MSPDEINIT_CB_ID      MspDeInit callback ID
448   * @retval status
449   */
HAL_MDIOS_UnRegisterCallback(MDIOS_HandleTypeDef * hmdios,HAL_MDIOS_CallbackIDTypeDef CallbackID)450 HAL_StatusTypeDef HAL_MDIOS_UnRegisterCallback(MDIOS_HandleTypeDef *hmdios, HAL_MDIOS_CallbackIDTypeDef CallbackID)
451 {
452   HAL_StatusTypeDef status = HAL_OK;
453 
454   /* Process locked */
455   __HAL_LOCK(hmdios);
456 
457   if (hmdios->State == HAL_MDIOS_STATE_READY)
458   {
459     switch (CallbackID)
460     {
461       case HAL_MDIOS_WRITE_COMPLETE_CB_ID :
462         hmdios->WriteCpltCallback = HAL_MDIOS_WriteCpltCallback;
463         break;
464 
465       case HAL_MDIOS_READ_COMPLETE_CB_ID :
466         hmdios->ReadCpltCallback = HAL_MDIOS_ReadCpltCallback;
467         break;
468 
469       case HAL_MDIOS_ERROR_CB_ID :
470         hmdios->ErrorCallback = HAL_MDIOS_ErrorCallback;
471         break;
472 
473       case HAL_MDIOS_WAKEUP_CB_ID :
474         hmdios->WakeUpCallback = HAL_MDIOS_WakeUpCallback;
475         break;
476 
477       case HAL_MDIOS_MSPINIT_CB_ID :
478         hmdios->MspInitCallback = HAL_MDIOS_MspInit;
479         break;
480 
481       case HAL_MDIOS_MSPDEINIT_CB_ID :
482         hmdios->MspDeInitCallback = HAL_MDIOS_MspDeInit;
483         break;
484 
485       default :
486         /* Update the error code */
487         hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
488         /* Return error status */
489         status =  HAL_ERROR;
490         break;
491     }
492   }
493   else if (hmdios->State == HAL_MDIOS_STATE_RESET)
494   {
495     switch (CallbackID)
496     {
497       case HAL_MDIOS_MSPINIT_CB_ID :
498         hmdios->MspInitCallback = HAL_MDIOS_MspInit;
499         break;
500 
501       case HAL_MDIOS_MSPDEINIT_CB_ID :
502         hmdios->MspDeInitCallback = HAL_MDIOS_MspDeInit;
503         break;
504 
505       default :
506         /* Update the error code */
507         hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
508         /* Return error status */
509         status =  HAL_ERROR;
510         break;
511     }
512   }
513   else
514   {
515     /* Update the error code */
516     hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
517     /* Return error status */
518     status =  HAL_ERROR;
519   }
520 
521   /* Release Lock */
522   __HAL_UNLOCK(hmdios);
523 
524   return status;
525 }
526 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
527 
528 /**
529   * @}
530   */
531 
532 /** @defgroup MDIOS_Exported_Functions_Group2 IO operation functions
533   *  @brief MDIOS Read/Write functions
534   *
535 @verbatim
536  ===============================================================================
537                       ##### IO operation functions #####
538  ===============================================================================
539     This subsection provides a set of functions allowing to manage the MDIOS
540     read and write operations.
541 
542     (#) APIs that allow to the MDIOS to read/write from/to the
543         values of one of the DINn/DOUTn registers:
544         (+) Read the value of a DINn register: HAL_MDIOS_ReadReg()
545         (+) Write a value to a DOUTn register: HAL_MDIOS_WriteReg()
546 
547     (#) APIs that provide if there are some Slave registres have been
548         read or written by the Master:
549         (+) DOUTn registers read by Master: HAL_MDIOS_GetReadRegAddress()
550         (+) DINn registers written by Master : HAL_MDIOS_GetWrittenRegAddress()
551 
552     (#) APIs that Clear the read/write flags:
553         (+) Clear read registers flags: HAL_MDIOS_ClearReadRegAddress()
554         (+) Clear write registers flags: HAL_MDIOS_ClearWriteRegAddress()
555 
556     (#) A set of Callbacks are provided:
557         (+) HAL_MDIOS_WriteCpltCallback()
558         (+) HAL_MDIOS_ReadCpltCallback()
559         (+) HAL_MDIOS_ErrorCallback()
560 
561 @endverbatim
562   * @{
563   */
564 
565 /**
566   * @brief  Writes to an MDIOS output register
567   * @param  hmdios: mdios handle
568   * @param  RegNum: MDIOS output register address
569   * @param  Data: Data to write
570   * @retval HAL status
571   */
HAL_MDIOS_WriteReg(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum,uint16_t Data)572 HAL_StatusTypeDef HAL_MDIOS_WriteReg(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum, uint16_t Data)
573 {
574   uint32_t tmpreg;
575 
576   /* Check the parameters */
577   assert_param(IS_MDIOS_REGISTER(RegNum));
578 
579   /* Process Locked */
580   __HAL_LOCK(hmdios);
581 
582   /* Get the addr of output register to be written by the MDIOS */
583   tmpreg = MDIOS_DOUT_BASE_ADDR + (4U * RegNum);
584 
585   /* Write to DOUTn register */
586   *((uint32_t *)tmpreg) = Data;
587 
588   /* Process Unlocked */
589   __HAL_UNLOCK(hmdios);
590 
591   return HAL_OK;
592 }
593 
594 /**
595   * @brief  Reads an MDIOS input register
596   * @param  hmdios: mdios handle
597   * @param  RegNum: MDIOS input register address
598   * @param  pData: pointer to Data
599   * @retval HAL status
600   */
HAL_MDIOS_ReadReg(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum,uint16_t * pData)601 HAL_StatusTypeDef HAL_MDIOS_ReadReg(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum, uint16_t *pData)
602 {
603   uint32_t tmpreg;
604 
605   /* Check the parameters */
606   assert_param(IS_MDIOS_REGISTER(RegNum));
607 
608   /* Process Locked */
609   __HAL_LOCK(hmdios);
610 
611   /* Get the addr of input register to be read by the MDIOS */
612   tmpreg = MDIOS_DIN_BASE_ADDR + (4U * RegNum);
613 
614   /* Read DINn register */
615   *pData = (uint16_t)(*((uint32_t *)tmpreg));
616 
617   /* Process Unlocked */
618   __HAL_UNLOCK(hmdios);
619 
620   return HAL_OK;
621 }
622 
623 /**
624   * @brief  Gets Written registers by MDIO master
625   * @param  hmdios: mdios handle
626   * @retval bit map of written registers addresses
627   */
HAL_MDIOS_GetWrittenRegAddress(MDIOS_HandleTypeDef * hmdios)628 uint32_t HAL_MDIOS_GetWrittenRegAddress(MDIOS_HandleTypeDef *hmdios)
629 {
630   return hmdios->Instance->WRFR;
631 }
632 
633 /**
634   * @brief  Gets Read registers by MDIO master
635   * @param  hmdios: mdios handle
636   * @retval bit map of read registers addresses
637   */
HAL_MDIOS_GetReadRegAddress(MDIOS_HandleTypeDef * hmdios)638 uint32_t HAL_MDIOS_GetReadRegAddress(MDIOS_HandleTypeDef *hmdios)
639 {
640   return hmdios->Instance->RDFR;
641 }
642 
643 /**
644   * @brief  Clears Write registers flag
645   * @param  hmdios: mdios handle
646   * @param  RegNum: registers addresses to be cleared
647   * @retval HAL status
648   */
HAL_MDIOS_ClearWriteRegAddress(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum)649 HAL_StatusTypeDef HAL_MDIOS_ClearWriteRegAddress(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum)
650 {
651   /* Check the parameters */
652   assert_param(IS_MDIOS_REGISTER(RegNum));
653 
654   /* Process Locked */
655   __HAL_LOCK(hmdios);
656 
657   /* Clear write registers flags */
658   hmdios->Instance->CWRFR |= (RegNum);
659 
660   /* Release Lock */
661   __HAL_UNLOCK(hmdios);
662 
663   return HAL_OK;
664 }
665 
666 /**
667   * @brief  Clears Read register flag
668   * @param  hmdios: mdios handle
669   * @param  RegNum: registers addresses to be cleared
670   * @retval HAL status
671   */
HAL_MDIOS_ClearReadRegAddress(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum)672 HAL_StatusTypeDef HAL_MDIOS_ClearReadRegAddress(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum)
673 {
674   /* Check the parameters */
675   assert_param(IS_MDIOS_REGISTER(RegNum));
676 
677   /* Process Locked */
678   __HAL_LOCK(hmdios);
679 
680   /* Clear read registers flags */
681   hmdios->Instance->CRDFR |= (RegNum);
682 
683   /* Release Lock */
684   __HAL_UNLOCK(hmdios);
685 
686   return HAL_OK;
687 }
688 
689 /**
690   * @brief  Enables Events for MDIOS peripheral
691   * @param  hmdios: mdios handle
692   * @retval HAL status
693   */
HAL_MDIOS_EnableEvents(MDIOS_HandleTypeDef * hmdios)694 HAL_StatusTypeDef HAL_MDIOS_EnableEvents(MDIOS_HandleTypeDef *hmdios)
695 {
696   /* Process Locked */
697   __HAL_LOCK(hmdios);
698 
699   /* Enable MDIOS interrupts: Register Write, Register Read and Error ITs */
700   __HAL_MDIOS_ENABLE_IT(hmdios, (MDIOS_IT_WRITE | MDIOS_IT_READ | MDIOS_IT_ERROR));
701 
702   /* Process Unlocked */
703   __HAL_UNLOCK(hmdios);
704 
705   return HAL_OK;
706 }
707 
708 /**
709   * @brief This function handles MDIOS interrupt request.
710   * @param hmdios: MDIOS handle
711   * @retval None
712   */
HAL_MDIOS_IRQHandler(MDIOS_HandleTypeDef * hmdios)713 void HAL_MDIOS_IRQHandler(MDIOS_HandleTypeDef *hmdios)
714 {
715   /* Write Register Interrupt enabled ? */
716   if (__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_WRITE) != (uint32_t)RESET)
717   {
718     /* Write register flag */
719     if (HAL_MDIOS_GetWrittenRegAddress(hmdios) != (uint32_t)RESET)
720     {
721 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
722       /*Call registered Write complete callback*/
723       hmdios->WriteCpltCallback(hmdios);
724 #else
725       /* Write callback function */
726       HAL_MDIOS_WriteCpltCallback(hmdios);
727 #endif  /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
728 
729       /* Clear write register flag */
730       hmdios->Instance->CWRFR |= MDIOS_ALL_REG_FLAG;
731     }
732   }
733 
734   /* Read Register Interrupt enabled ? */
735   if (__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_READ) != (uint32_t)RESET)
736   {
737     /* Read register flag */
738     if (HAL_MDIOS_GetReadRegAddress(hmdios) != (uint32_t)RESET)
739     {
740 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
741       /*Call registered Read complete callback*/
742       hmdios->ReadCpltCallback(hmdios);
743 #else
744       /* Read callback function  */
745       HAL_MDIOS_ReadCpltCallback(hmdios);
746 #endif  /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
747 
748       /* Clear read register flag */
749       hmdios->Instance->CRDFR |= MDIOS_ALL_REG_FLAG;
750     }
751   }
752 
753   /* Error Interrupt enabled ? */
754   if (__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_ERROR) != (uint32_t)RESET)
755   {
756     /* All Errors Flag */
757     if (__HAL_MDIOS_GET_ERROR_FLAG(hmdios, MDIOS_ALL_ERRORS_FLAG) != (uint32_t)RESET)
758     {
759       hmdios->ErrorCode |= HAL_MDIOS_ERROR_DATA;
760 
761 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
762       /*Call registered Error callback*/
763       hmdios->ErrorCallback(hmdios);
764 #else
765       /* Error Callback */
766       HAL_MDIOS_ErrorCallback(hmdios);
767 #endif  /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
768 
769       /* Clear errors flag */
770       __HAL_MDIOS_CLEAR_ERROR_FLAG(hmdios, MDIOS_ALL_ERRORS_FLAG);
771     }
772     hmdios->ErrorCode = HAL_MDIOS_ERROR_NONE;
773   }
774   /* check MDIOS WAKEUP exti flag */
775   if (__HAL_MDIOS_WAKEUP_EXTI_GET_FLAG(MDIOS_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
776   {
777     /* Clear MDIOS WAKEUP Exti pending bit */
778     __HAL_MDIOS_WAKEUP_EXTI_CLEAR_FLAG(MDIOS_WAKEUP_EXTI_LINE);
779 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
780     /*Call registered WakeUp callback*/
781     hmdios->WakeUpCallback(hmdios);
782 #else
783     /* MDIOS WAKEUP callback */
784     HAL_MDIOS_WakeUpCallback(hmdios);
785 #endif  /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
786   }
787 
788 }
789 
790 /**
791   * @brief  Write Complete Callback
792   * @param  hmdios: mdios handle
793   * @retval None
794   */
HAL_MDIOS_WriteCpltCallback(MDIOS_HandleTypeDef * hmdios)795 __weak void HAL_MDIOS_WriteCpltCallback(MDIOS_HandleTypeDef *hmdios)
796 {
797   /* Prevent unused argument(s) compilation warning */
798   UNUSED(hmdios);
799 
800   /* NOTE : This function should not be modified, when the callback is needed,
801             the HAL_MDIOS_WriteCpltCallback can be implemented in the user file
802    */
803 }
804 
805 /**
806   * @brief  Read Complete Callback
807   * @param  hmdios: mdios handle
808   * @retval None
809   */
HAL_MDIOS_ReadCpltCallback(MDIOS_HandleTypeDef * hmdios)810 __weak void HAL_MDIOS_ReadCpltCallback(MDIOS_HandleTypeDef *hmdios)
811 {
812   /* Prevent unused argument(s) compilation warning */
813   UNUSED(hmdios);
814 
815   /* NOTE : This function should not be modified, when the callback is needed,
816             the HAL_MDIOS_ReadCpltCallback can be implemented in the user file
817    */
818 }
819 
820 /**
821   * @brief Error Callback
822   * @param hmdios: mdios handle
823   * @retval None
824   */
HAL_MDIOS_ErrorCallback(MDIOS_HandleTypeDef * hmdios)825 __weak void HAL_MDIOS_ErrorCallback(MDIOS_HandleTypeDef *hmdios)
826 {
827   /* Prevent unused argument(s) compilation warning */
828   UNUSED(hmdios);
829 
830   /* NOTE : This function should not be modified, when the callback is needed,
831             the HAL_MDIOS_ErrorCallback can be implemented in the user file
832    */
833 }
834 
835 /**
836   * @brief  MDIOS WAKEUP interrupt callback
837   * @param hmdios: mdios handle
838   * @retval None
839   */
HAL_MDIOS_WakeUpCallback(MDIOS_HandleTypeDef * hmdios)840 __weak void HAL_MDIOS_WakeUpCallback(MDIOS_HandleTypeDef *hmdios)
841 {
842   /* Prevent unused argument(s) compilation warning */
843   UNUSED(hmdios);
844 
845   /* NOTE : This function Should not be modified, when the callback is needed,
846             the HAL_MDIOS_WakeUpCallback could be implemented in the user file
847    */
848 }
849 
850 /**
851   * @}
852   */
853 
854 /** @defgroup MDIOS_Exported_Functions_Group3 Peripheral Control functions
855   *  @brief   MDIOS control functions
856   *
857 @verbatim
858  ===============================================================================
859                       ##### Peripheral Control functions #####
860  ===============================================================================
861     [..]
862     This subsection provides a set of functions allowing to control the MDIOS.
863      (+) HAL_MDIOS_GetState() API, helpful to check in run-time the state.
864      (+) HAL_MDIOS_GetError() API, returns the errors code of the HAL state machine.
865 
866 @endverbatim
867   * @{
868   */
869 
870 /**
871   * @brief  Gets MDIOS error code
872   * @param  hmdios: mdios handle
873   * @retval mdios error code
874   */
HAL_MDIOS_GetError(MDIOS_HandleTypeDef * hmdios)875 uint32_t HAL_MDIOS_GetError(MDIOS_HandleTypeDef *hmdios)
876 {
877   /* return the error code */
878   return hmdios->ErrorCode;
879 }
880 
881 /**
882   * @brief  Return the MDIOS HAL state
883   * @param  hmdios: mdios handle
884   * @retval HAL state
885   */
HAL_MDIOS_GetState(MDIOS_HandleTypeDef * hmdios)886 HAL_MDIOS_StateTypeDef HAL_MDIOS_GetState(MDIOS_HandleTypeDef *hmdios)
887 {
888   /* Return MDIOS state */
889   return hmdios->State;
890 }
891 
892 /**
893   * @}
894   */
895 
896 /**
897   * @}
898   */
899 
900 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
MDIOS_InitCallbacksToDefault(MDIOS_HandleTypeDef * hmdios)901 static void MDIOS_InitCallbacksToDefault(MDIOS_HandleTypeDef *hmdios)
902 {
903   /* Init the MDIOS Callback settings */
904   hmdios->WriteCpltCallback  = HAL_MDIOS_WriteCpltCallback;   /* Legacy weak WriteCpltCallback   */
905   hmdios->ReadCpltCallback   = HAL_MDIOS_ReadCpltCallback;    /* Legacy weak ReadCpltCallback   */
906   hmdios->ErrorCallback      = HAL_MDIOS_ErrorCallback;       /* Legacy weak ErrorCallback */
907   hmdios->WakeUpCallback     = HAL_MDIOS_WakeUpCallback;        /* Legacy weak WakeUpCallback   */
908 }
909 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
910 
911 /**
912   * @}
913   */
914 #endif /* HAL_MDIOS_MODULE_ENABLED */
915 /**
916   * @}
917   */
918 #endif /* MDIOS */
919 /**
920   * @}
921   */
922