1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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 genereted 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 "stm32h7xx_hal.h"
130
131 /** @addtogroup STM32H7xx_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 /** @defgroup MDIOS_Private_Define MDIOS Private Define
146 * @{
147 */
148 #define MDIOS_PORT_ADDRESS_SHIFT ((uint32_t)8)
149 #define MDIOS_ALL_REG_FLAG ((uint32_t)0xFFFFFFFFU)
150 #define MDIOS_ALL_ERRORS_FLAG ((uint32_t)(MDIOS_SR_PERF | MDIOS_SR_SERF | MDIOS_SR_TERF))
151
152 #define MDIOS_DIN_BASE_ADDR (MDIOS_BASE + 0x100U)
153 #define MDIOS_DOUT_BASE_ADDR (MDIOS_BASE + 0x180U)
154 /**
155 * @}
156 */
157 /* Private macro -------------------------------------------------------------*/
158 /* Private variables ---------------------------------------------------------*/
159 /* Private function prototypes -----------------------------------------------*/
160 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
161 /** @defgroup MDIOS_Private_Functions MDIOS Private Functions
162 * @{
163 */
164 static void MDIOS_InitCallbacksToDefault(MDIOS_HandleTypeDef *hmdios);
165 /**
166 * @}
167 */
168 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
169 /* Private functions ---------------------------------------------------------*/
170 /* Exported functions --------------------------------------------------------*/
171 /** @defgroup MDIOS_Exported_Functions MDIOS Exported Functions
172 * @{
173 */
174
175 /** @defgroup MDIOS_Exported_Functions_Group1 Initialization/de-initialization functions
176 * @brief Initialization and Configuration functions
177 *
178 @verbatim
179 ===============================================================================
180 ##### Initialization and Configuration functions #####
181 ===============================================================================
182 [..]
183 This subsection provides a set of functions allowing to initialize the MDIOS
184 (+) The following parameters can be configured:
185 (++) Port Address
186 (++) Preamble Check
187
188 @endverbatim
189 * @{
190 */
191
192 /**
193 * @brief Initializes the MDIOS according to the specified parameters in
194 * the MDIOS_InitTypeDef and creates the associated handle .
195 * @param hmdios: pointer to a MDIOS_HandleTypeDef structure that contains
196 * the configuration information for MDIOS module
197 * @retval HAL status
198 */
HAL_MDIOS_Init(MDIOS_HandleTypeDef * hmdios)199 HAL_StatusTypeDef HAL_MDIOS_Init(MDIOS_HandleTypeDef *hmdios)
200 {
201 uint32_t tmpcr;
202
203 /* Check the MDIOS handle allocation */
204 if(hmdios == NULL)
205 {
206 return HAL_ERROR;
207 }
208
209 /* Check the parameters */
210 assert_param(IS_MDIOS_ALL_INSTANCE(hmdios->Instance));
211 assert_param(IS_MDIOS_PORTADDRESS(hmdios->Init.PortAddress));
212 assert_param(IS_MDIOS_PREAMBLECHECK(hmdios->Init.PreambleCheck));
213
214 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
215
216 if(hmdios->State == HAL_MDIOS_STATE_RESET)
217 {
218 MDIOS_InitCallbacksToDefault(hmdios);
219
220 if(hmdios->MspInitCallback == NULL)
221 {
222 hmdios->MspInitCallback = HAL_MDIOS_MspInit;
223 }
224
225 /* Init the low level hardware */
226 hmdios->MspInitCallback(hmdios);
227 }
228
229 #else
230
231 if(hmdios->State == HAL_MDIOS_STATE_RESET)
232 {
233 /* Init the low level hardware */
234 HAL_MDIOS_MspInit(hmdios);
235 }
236
237 #endif /* (USE_HAL_MDIOS_REGISTER_CALLBACKS) */
238
239 /* Change the MDIOS state */
240 hmdios->State = HAL_MDIOS_STATE_BUSY;
241
242 /* Get the MDIOS CR value */
243 tmpcr = hmdios->Instance->CR;
244
245 /* Clear PORT_ADDRESS, DPC and EN bits */
246 tmpcr &= ((uint32_t)~(MDIOS_CR_EN | MDIOS_CR_DPC | MDIOS_CR_PORT_ADDRESS));
247
248 /* Set MDIOS control parametrs and enable the peripheral */
249 tmpcr |= (uint32_t)(((hmdios->Init.PortAddress) << MDIOS_PORT_ADDRESS_SHIFT) |\
250 (hmdios->Init.PreambleCheck) | \
251 (MDIOS_CR_EN));
252
253 /* Write the MDIOS CR */
254 hmdios->Instance->CR = tmpcr;
255
256 hmdios->ErrorCode = HAL_MDIOS_ERROR_NONE;
257
258 /* Change the MDIOS state */
259 hmdios->State = HAL_MDIOS_STATE_READY;
260
261 /* Release Lock */
262 __HAL_UNLOCK(hmdios);
263
264 /* Return function status */
265 return HAL_OK;
266
267 }
268
269 /**
270 * @brief DeInitializes the MDIOS peripheral.
271 * @param hmdios: MDIOS handle
272 * @retval HAL status
273 */
HAL_MDIOS_DeInit(MDIOS_HandleTypeDef * hmdios)274 HAL_StatusTypeDef HAL_MDIOS_DeInit(MDIOS_HandleTypeDef *hmdios)
275 {
276 /* Check the MDIOS handle allocation */
277 if(hmdios == NULL)
278 {
279 return HAL_ERROR;
280 }
281
282 /* Check the parameters */
283 assert_param(IS_MDIOS_ALL_INSTANCE(hmdios->Instance));
284
285 /* Change the MDIOS state */
286 hmdios->State = HAL_MDIOS_STATE_BUSY;
287
288 /* Disable the Peripheral */
289 __HAL_MDIOS_DISABLE(hmdios);
290
291 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
292
293 if(hmdios->MspDeInitCallback == NULL)
294 {
295 hmdios->MspDeInitCallback = HAL_MDIOS_MspDeInit;
296 }
297 /* DeInit the low level hardware */
298 hmdios->MspDeInitCallback(hmdios);
299 #else
300
301 /* DeInit the low level hardware */
302 HAL_MDIOS_MspDeInit(hmdios);
303
304 #endif /* (USE_HAL_MDIOS_REGISTER_CALLBACKS) */
305
306 /* Change the MDIOS state */
307 hmdios->State = HAL_MDIOS_STATE_RESET;
308
309 /* Release Lock */
310 __HAL_UNLOCK(hmdios);
311
312 /* Return function status */
313 return HAL_OK;
314 }
315
316 /**
317 * @brief MDIOS MSP Init
318 * @param hmdios: mdios handle
319 * @retval None
320 */
HAL_MDIOS_MspInit(MDIOS_HandleTypeDef * hmdios)321 __weak void HAL_MDIOS_MspInit(MDIOS_HandleTypeDef *hmdios)
322 {
323 /* Prevent unused argument(s) compilation warning */
324 UNUSED(hmdios);
325
326 /* NOTE : This function should not be modified, when the callback is needed,
327 the HAL_MDIOS_MspInit can be implemented in the user file
328 */
329 }
330
331 /**
332 * @brief MDIOS MSP DeInit
333 * @param hmdios: mdios handle
334 * @retval None
335 */
HAL_MDIOS_MspDeInit(MDIOS_HandleTypeDef * hmdios)336 __weak void HAL_MDIOS_MspDeInit(MDIOS_HandleTypeDef *hmdios)
337 {
338 /* Prevent unused argument(s) compilation warning */
339 UNUSED(hmdios);
340
341 /* NOTE : This function should not be modified, when the callback is needed,
342 the HAL_MDIOS_MspDeInit can be implemented in the user file
343 */
344 }
345
346 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
347 /**
348 * @brief Register a User MDIOS Callback
349 * To be used instead of the weak predefined callback
350 * @param hmdios mdios handle
351 * @param CallbackID ID of the callback to be registered
352 * This parameter can be one of the following values:
353 * @arg @ref HAL_MDIOS_WRITE_COMPLETE_CB_ID Write Complete Callback ID
354 * @arg @ref HAL_MDIOS_READ_COMPLETE_CB_ID Read Complete Callback ID
355 * @arg @ref HAL_MDIOS_ERROR_CB_ID Error Callback ID
356 * @arg @ref HAL_MDIOS_WAKEUP_CB_ID Wake Up Callback ID
357 * @arg @ref HAL_MDIOS_MSPINIT_CB_ID MspInit callback ID
358 * @arg @ref HAL_MDIOS_MSPDEINIT_CB_ID MspDeInit callback ID
359 * @param pCallback pointer to the Callback function
360 * @retval status
361 */
HAL_MDIOS_RegisterCallback(MDIOS_HandleTypeDef * hmdios,HAL_MDIOS_CallbackIDTypeDef CallbackID,pMDIOS_CallbackTypeDef pCallback)362 HAL_StatusTypeDef HAL_MDIOS_RegisterCallback(MDIOS_HandleTypeDef *hmdios, HAL_MDIOS_CallbackIDTypeDef CallbackID, pMDIOS_CallbackTypeDef pCallback)
363 {
364 HAL_StatusTypeDef status = HAL_OK;
365
366 if(pCallback == NULL)
367 {
368 /* Update the error code */
369 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
370
371 return HAL_ERROR;
372 }
373 /* Process locked */
374 __HAL_LOCK(hmdios);
375
376 if(hmdios->State == HAL_MDIOS_STATE_READY)
377 {
378 switch (CallbackID)
379 {
380 case HAL_MDIOS_WRITE_COMPLETE_CB_ID :
381 hmdios->WriteCpltCallback = pCallback;
382 break;
383
384 case HAL_MDIOS_READ_COMPLETE_CB_ID :
385 hmdios->ReadCpltCallback = pCallback;
386 break;
387
388 case HAL_MDIOS_ERROR_CB_ID :
389 hmdios->ErrorCallback = pCallback;
390 break;
391
392 case HAL_MDIOS_WAKEUP_CB_ID :
393 hmdios->WakeUpCallback = pCallback;
394 break;
395
396 case HAL_MDIOS_MSPINIT_CB_ID :
397 hmdios->MspInitCallback = pCallback;
398 break;
399
400 case HAL_MDIOS_MSPDEINIT_CB_ID :
401 hmdios->MspDeInitCallback = pCallback;
402 break;
403
404 default :
405 /* Update the error code */
406 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
407 /* Return error status */
408 status = HAL_ERROR;
409 break;
410 }
411 }
412 else if(hmdios->State == HAL_MDIOS_STATE_RESET)
413 {
414 switch (CallbackID)
415 {
416 case HAL_MDIOS_MSPINIT_CB_ID :
417 hmdios->MspInitCallback = pCallback;
418 break;
419
420 case HAL_MDIOS_MSPDEINIT_CB_ID :
421 hmdios->MspDeInitCallback = pCallback;
422 break;
423
424 default :
425 /* Update the error code */
426 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
427 /* Return error status */
428 status = HAL_ERROR;
429 break;
430 }
431 }
432 else
433 {
434 /* Update the error code */
435 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
436 /* Return error status */
437 status = HAL_ERROR;
438 }
439
440 /* Release Lock */
441 __HAL_UNLOCK(hmdios);
442
443 return status;
444 }
445
446 /**
447 * @brief Unregister an MDIOS Callback
448 * MDIOS callback is redirected to the weak predefined callback
449 * @param hmdios mdios handle
450 * @param CallbackID ID of the callback to be unregistered
451 * This parameter can be one of the following values:
452 * @arg @ref HAL_MDIOS_WRITE_COMPLETE_CB_ID Write Complete Callback ID
453 * @arg @ref HAL_MDIOS_READ_COMPLETE_CB_ID Read Complete Callback ID
454 * @arg @ref HAL_MDIOS_ERROR_CB_ID Error Callback ID
455 * @arg @ref HAL_MDIOS_WAKEUP_CB_ID Wake Up Callback ID
456 * @arg @ref HAL_MDIOS_MSPINIT_CB_ID MspInit callback ID
457 * @arg @ref HAL_MDIOS_MSPDEINIT_CB_ID MspDeInit callback ID
458 * @retval status
459 */
HAL_MDIOS_UnRegisterCallback(MDIOS_HandleTypeDef * hmdios,HAL_MDIOS_CallbackIDTypeDef CallbackID)460 HAL_StatusTypeDef HAL_MDIOS_UnRegisterCallback(MDIOS_HandleTypeDef *hmdios, HAL_MDIOS_CallbackIDTypeDef CallbackID)
461 {
462 HAL_StatusTypeDef status = HAL_OK;
463
464 /* Process locked */
465 __HAL_LOCK(hmdios);
466
467 if(hmdios->State == HAL_MDIOS_STATE_READY)
468 {
469 switch (CallbackID)
470 {
471 case HAL_MDIOS_WRITE_COMPLETE_CB_ID :
472 hmdios->WriteCpltCallback = HAL_MDIOS_WriteCpltCallback;
473 break;
474
475 case HAL_MDIOS_READ_COMPLETE_CB_ID :
476 hmdios->ReadCpltCallback = HAL_MDIOS_ReadCpltCallback;
477 break;
478
479 case HAL_MDIOS_ERROR_CB_ID :
480 hmdios->ErrorCallback = HAL_MDIOS_ErrorCallback;
481 break;
482
483 case HAL_MDIOS_WAKEUP_CB_ID :
484 hmdios->WakeUpCallback = HAL_MDIOS_WakeUpCallback;
485 break;
486
487 case HAL_MDIOS_MSPINIT_CB_ID :
488 hmdios->MspInitCallback = HAL_MDIOS_MspInit;
489 break;
490
491 case HAL_MDIOS_MSPDEINIT_CB_ID :
492 hmdios->MspDeInitCallback = HAL_MDIOS_MspDeInit;
493 break;
494
495 default :
496 /* Update the error code */
497 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
498 /* Return error status */
499 status = HAL_ERROR;
500 break;
501 }
502 }
503 else if(hmdios->State == HAL_MDIOS_STATE_RESET)
504 {
505 switch (CallbackID)
506 {
507 case HAL_MDIOS_MSPINIT_CB_ID :
508 hmdios->MspInitCallback = HAL_MDIOS_MspInit;
509 break;
510
511 case HAL_MDIOS_MSPDEINIT_CB_ID :
512 hmdios->MspDeInitCallback = HAL_MDIOS_MspDeInit;
513 break;
514
515 default :
516 /* Update the error code */
517 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
518 /* Return error status */
519 status = HAL_ERROR;
520 break;
521 }
522 }
523 else
524 {
525 /* Update the error code */
526 hmdios->ErrorCode |= HAL_MDIOS_ERROR_INVALID_CALLBACK;
527 /* Return error status */
528 status = HAL_ERROR;
529 }
530
531 /* Release Lock */
532 __HAL_UNLOCK(hmdios);
533
534 return status;
535 }
536 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
537
538 /**
539 * @}
540 */
541
542 /** @defgroup MDIOS_Exported_Functions_Group2 IO operation functions
543 * @brief MDIOS Read/Write functions
544 *
545 @verbatim
546 ===============================================================================
547 ##### IO operation functions #####
548 ===============================================================================
549 This subsection provides a set of functions allowing to manage the MDIOS
550 read and write operations.
551
552 (#) APIs that allow to the MDIOS to read/write from/to the
553 values of one of the DINn/DOUTn registers:
554 (+) Read the value of a DINn register: HAL_MDIOS_ReadReg()
555 (+) Write a value to a DOUTn register: HAL_MDIOS_WriteReg()
556
557 (#) APIs that provide if there are some Slave registres have been
558 read or written by the Master:
559 (+) DOUTn registers read by Master: HAL_MDIOS_GetReadRegAddress()
560 (+) DINn registers written by Master : HAL_MDIOS_GetWrittenRegAddress()
561
562 (#) APIs that Clear the read/write flags:
563 (+) Clear read registers flags: HAL_MDIOS_ClearReadRegAddress()
564 (+) Clear write registers flags: HAL_MDIOS_ClearWriteRegAddress()
565
566 (#) A set of Callbacks are provided:
567 (+) HAL_MDIOS_WriteCpltCallback()
568 (+) HAL_MDIOS_ReadCpltCallback()
569 (+) HAL_MDIOS_ErrorCallback()
570
571 @endverbatim
572 * @{
573 */
574
575 /**
576 * @brief Writes to an MDIOS output register
577 * @param hmdios: mdios handle
578 * @param RegNum: MDIOS output register address
579 * @param Data: Data to write
580 * @retval HAL status
581 */
HAL_MDIOS_WriteReg(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum,uint16_t Data)582 HAL_StatusTypeDef HAL_MDIOS_WriteReg(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum, uint16_t Data)
583 {
584 uint32_t tmpreg;
585
586 /* Check the parameters */
587 assert_param(IS_MDIOS_REGISTER(RegNum));
588
589 /* Process Locked */
590 __HAL_LOCK(hmdios);
591
592 /* Get the addr of output register to be written by the MDIOS */
593 tmpreg = MDIOS_DOUT_BASE_ADDR + (4U * RegNum);
594
595 /* Write to DOUTn register */
596 *((uint32_t *)tmpreg) = Data;
597
598 /* Process Unlocked */
599 __HAL_UNLOCK(hmdios);
600
601 return HAL_OK;
602 }
603
604 /**
605 * @brief Reads an MDIOS input register
606 * @param hmdios: mdios handle
607 * @param RegNum: MDIOS input register address
608 * @param pData: pointer to Data
609 * @retval HAL status
610 */
HAL_MDIOS_ReadReg(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum,uint16_t * pData)611 HAL_StatusTypeDef HAL_MDIOS_ReadReg(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum, uint16_t *pData)
612 {
613 uint32_t tmpreg;
614
615 /* Check the parameters */
616 assert_param(IS_MDIOS_REGISTER(RegNum));
617
618 /* Process Locked */
619 __HAL_LOCK(hmdios);
620
621 /* Get the addr of input register to be read by the MDIOS */
622 tmpreg = MDIOS_DIN_BASE_ADDR + (4U * RegNum);
623
624 /* Read DINn register */
625 *pData = (uint16_t)(*((uint32_t *)tmpreg));
626
627 /* Process Unlocked */
628 __HAL_UNLOCK(hmdios);
629
630 return HAL_OK;
631 }
632
633 /**
634 * @brief Gets Written registers by MDIO master
635 * @param hmdios: mdios handle
636 * @retval bit map of written registers addresses
637 */
HAL_MDIOS_GetWrittenRegAddress(MDIOS_HandleTypeDef * hmdios)638 uint32_t HAL_MDIOS_GetWrittenRegAddress(MDIOS_HandleTypeDef *hmdios)
639 {
640 return hmdios->Instance->WRFR;
641 }
642
643 /**
644 * @brief Gets Read registers by MDIO master
645 * @param hmdios: mdios handle
646 * @retval bit map of read registers addresses
647 */
HAL_MDIOS_GetReadRegAddress(MDIOS_HandleTypeDef * hmdios)648 uint32_t HAL_MDIOS_GetReadRegAddress(MDIOS_HandleTypeDef *hmdios)
649 {
650 return hmdios->Instance->RDFR;
651 }
652
653 /**
654 * @brief Clears Write registers flag
655 * @param hmdios: mdios handle
656 * @param RegNum: registers addresses to be cleared
657 * @retval HAL status
658 */
HAL_MDIOS_ClearWriteRegAddress(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum)659 HAL_StatusTypeDef HAL_MDIOS_ClearWriteRegAddress(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum)
660 {
661 /* Check the parameters */
662 assert_param(IS_MDIOS_REGISTER(RegNum));
663
664 /* Process Locked */
665 __HAL_LOCK(hmdios);
666
667 /* Clear write registers flags */
668 hmdios->Instance->CWRFR |= (RegNum);
669
670 /* Release Lock */
671 __HAL_UNLOCK(hmdios);
672
673 return HAL_OK;
674 }
675
676 /**
677 * @brief Clears Read register flag
678 * @param hmdios: mdios handle
679 * @param RegNum: registers addresses to be cleared
680 * @retval HAL status
681 */
HAL_MDIOS_ClearReadRegAddress(MDIOS_HandleTypeDef * hmdios,uint32_t RegNum)682 HAL_StatusTypeDef HAL_MDIOS_ClearReadRegAddress(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum)
683 {
684 /* Check the parameters */
685 assert_param(IS_MDIOS_REGISTER(RegNum));
686
687 /* Process Locked */
688 __HAL_LOCK(hmdios);
689
690 /* Clear read registers flags */
691 hmdios->Instance->CRDFR |= (RegNum);
692
693 /* Release Lock */
694 __HAL_UNLOCK(hmdios);
695
696 return HAL_OK;
697 }
698
699 /**
700 * @brief Enables Events for MDIOS peripheral
701 * @param hmdios: mdios handle
702 * @retval HAL status
703 */
HAL_MDIOS_EnableEvents(MDIOS_HandleTypeDef * hmdios)704 HAL_StatusTypeDef HAL_MDIOS_EnableEvents(MDIOS_HandleTypeDef *hmdios)
705 {
706 /* Process Locked */
707 __HAL_LOCK(hmdios);
708
709 /* Enable MDIOS interrupts: Register Write, Register Read and Error ITs */
710 __HAL_MDIOS_ENABLE_IT(hmdios, (MDIOS_IT_WRITE | MDIOS_IT_READ | MDIOS_IT_ERROR));
711
712 /* Process Unlocked */
713 __HAL_UNLOCK(hmdios);
714
715 return HAL_OK;
716 }
717
718 /**
719 * @brief This function handles MDIOS interrupt request.
720 * @param hmdios: MDIOS handle
721 * @retval None
722 */
HAL_MDIOS_IRQHandler(MDIOS_HandleTypeDef * hmdios)723 void HAL_MDIOS_IRQHandler(MDIOS_HandleTypeDef *hmdios)
724 {
725 /* Write Register Interrupt enabled ? */
726 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_WRITE) != (uint32_t)RESET)
727 {
728 /* Write register flag */
729 if(HAL_MDIOS_GetWrittenRegAddress(hmdios) != (uint32_t)RESET)
730 {
731 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
732 /*Call registered Write complete callback*/
733 hmdios->WriteCpltCallback(hmdios);
734 #else
735 /* Write callback function */
736 HAL_MDIOS_WriteCpltCallback(hmdios);
737 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
738
739 /* Clear write register flag */
740 hmdios->Instance->CWRFR |= MDIOS_ALL_REG_FLAG;
741 }
742 }
743
744 /* Read Register Interrupt enabled ? */
745 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_READ) != (uint32_t)RESET)
746 {
747 /* Read register flag */
748 if(HAL_MDIOS_GetReadRegAddress(hmdios) != (uint32_t)RESET)
749 {
750 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
751 /*Call registered Read complete callback*/
752 hmdios->ReadCpltCallback(hmdios);
753 #else
754 /* Read callback function */
755 HAL_MDIOS_ReadCpltCallback(hmdios);
756 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
757
758 /* Clear read register flag */
759 hmdios->Instance->CRDFR |= MDIOS_ALL_REG_FLAG;
760 }
761 }
762
763 /* Error Interrupt enabled ? */
764 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_ERROR) != (uint32_t)RESET)
765 {
766 /* All Errors Flag */
767 if(__HAL_MDIOS_GET_ERROR_FLAG(hmdios, MDIOS_ALL_ERRORS_FLAG) != (uint32_t)RESET)
768 {
769 hmdios->ErrorCode |= HAL_MDIOS_ERROR_DATA;
770
771 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
772 /*Call registered Error callback*/
773 hmdios->ErrorCallback(hmdios);
774 #else
775 /* Error Callback */
776 HAL_MDIOS_ErrorCallback(hmdios);
777 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
778
779 /* Clear errors flag */
780 __HAL_MDIOS_CLEAR_ERROR_FLAG(hmdios, MDIOS_ALL_ERRORS_FLAG);
781 }
782 hmdios->ErrorCode = HAL_MDIOS_ERROR_NONE;
783 }
784 #if defined(DUAL_CORE)
785
786 if (HAL_GetCurrentCPUID() == CM7_CPUID)
787 {
788 if(__HAL_MDIOS_WAKEUP_EXTI_GET_FLAG(MDIOS_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
789 {
790 /* Clear MDIOS WAKEUP Exti pending bit */
791 __HAL_MDIOS_WAKEUP_EXTI_CLEAR_FLAG(MDIOS_WAKEUP_EXTI_LINE);
792
793 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
794 /*Call registered WakeUp callback*/
795 hmdios->WakeUpCallback(hmdios);
796 #else
797 /* MDIOS WAKEUP callback */
798 HAL_MDIOS_WakeUpCallback(hmdios);
799 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
800 }
801 }
802 else
803 {
804 if(__HAL_MDIOS_WAKEUP_EXTID2_GET_FLAG(MDIOS_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
805 {
806 /* Clear MDIOS WAKEUP Exti D2 pending bit */
807 __HAL_MDIOS_WAKEUP_EXTID2_CLEAR_FLAG(MDIOS_WAKEUP_EXTI_LINE);
808 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
809 /*Call registered WakeUp callback*/
810 hmdios->WakeUpCallback(hmdios);
811 #else
812 /* MDIOS WAKEUP callback */
813 HAL_MDIOS_WakeUpCallback(hmdios);
814 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
815 }
816 }
817 #else
818 /* check MDIOS WAKEUP exti flag */
819 if(__HAL_MDIOS_WAKEUP_EXTI_GET_FLAG(MDIOS_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
820 {
821 /* Clear MDIOS WAKEUP Exti pending bit */
822 __HAL_MDIOS_WAKEUP_EXTI_CLEAR_FLAG(MDIOS_WAKEUP_EXTI_LINE);
823 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
824 /*Call registered WakeUp callback*/
825 hmdios->WakeUpCallback(hmdios);
826 #else
827 /* MDIOS WAKEUP callback */
828 HAL_MDIOS_WakeUpCallback(hmdios);
829 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
830 }
831 #endif
832 }
833
834 /**
835 * @brief Write Complete Callback
836 * @param hmdios: mdios handle
837 * @retval None
838 */
HAL_MDIOS_WriteCpltCallback(MDIOS_HandleTypeDef * hmdios)839 __weak void HAL_MDIOS_WriteCpltCallback(MDIOS_HandleTypeDef *hmdios)
840 {
841 /* Prevent unused argument(s) compilation warning */
842 UNUSED(hmdios);
843
844 /* NOTE : This function should not be modified, when the callback is needed,
845 the HAL_MDIOS_WriteCpltCallback can be implemented in the user file
846 */
847 }
848
849 /**
850 * @brief Read Complete Callback
851 * @param hmdios: mdios handle
852 * @retval None
853 */
HAL_MDIOS_ReadCpltCallback(MDIOS_HandleTypeDef * hmdios)854 __weak void HAL_MDIOS_ReadCpltCallback(MDIOS_HandleTypeDef *hmdios)
855 {
856 /* Prevent unused argument(s) compilation warning */
857 UNUSED(hmdios);
858
859 /* NOTE : This function should not be modified, when the callback is needed,
860 the HAL_MDIOS_ReadCpltCallback can be implemented in the user file
861 */
862 }
863
864 /**
865 * @brief Error Callback
866 * @param hmdios: mdios handle
867 * @retval None
868 */
HAL_MDIOS_ErrorCallback(MDIOS_HandleTypeDef * hmdios)869 __weak void HAL_MDIOS_ErrorCallback(MDIOS_HandleTypeDef *hmdios)
870 {
871 /* Prevent unused argument(s) compilation warning */
872 UNUSED(hmdios);
873
874 /* NOTE : This function should not be modified, when the callback is needed,
875 the HAL_MDIOS_ErrorCallback can be implemented in the user file
876 */
877 }
878
879 /**
880 * @brief MDIOS WAKEUP interrupt callback
881 * @param hmdios: mdios handle
882 * @retval None
883 */
HAL_MDIOS_WakeUpCallback(MDIOS_HandleTypeDef * hmdios)884 __weak void HAL_MDIOS_WakeUpCallback(MDIOS_HandleTypeDef *hmdios)
885 {
886 /* Prevent unused argument(s) compilation warning */
887 UNUSED(hmdios);
888
889 /* NOTE : This function Should not be modified, when the callback is needed,
890 the HAL_MDIOS_WakeUpCallback could be implemented in the user file
891 */
892 }
893
894 /**
895 * @}
896 */
897
898 /** @defgroup MDIOS_Exported_Functions_Group3 Peripheral Control functions
899 * @brief MDIOS control functions
900 *
901 @verbatim
902 ===============================================================================
903 ##### Peripheral Control functions #####
904 ===============================================================================
905 [..]
906 This subsection provides a set of functions allowing to control the MDIOS.
907 (+) HAL_MDIOS_GetState() API, helpful to check in run-time the state.
908 (+) HAL_MDIOS_GetError() API, returns the errors code of the HAL state machine.
909
910 @endverbatim
911 * @{
912 */
913
914 /**
915 * @brief Gets MDIOS error code
916 * @param hmdios: mdios handle
917 * @retval mdios error code
918 */
HAL_MDIOS_GetError(MDIOS_HandleTypeDef * hmdios)919 uint32_t HAL_MDIOS_GetError(MDIOS_HandleTypeDef *hmdios)
920 {
921 /* return the error code */
922 return hmdios->ErrorCode;
923 }
924
925 /**
926 * @brief Return the MDIOS HAL state
927 * @param hmdios: mdios handle
928 * @retval HAL state
929 */
HAL_MDIOS_GetState(MDIOS_HandleTypeDef * hmdios)930 HAL_MDIOS_StateTypeDef HAL_MDIOS_GetState(MDIOS_HandleTypeDef *hmdios)
931 {
932 /* Return MDIOS state */
933 return hmdios->State;
934 }
935
936 /**
937 * @}
938 */
939
940 /**
941 * @}
942 */
943
944 #if (USE_HAL_MDIOS_REGISTER_CALLBACKS == 1)
945 /** @addtogroup MDIOS_Private_Functions
946 * @{
947 */
MDIOS_InitCallbacksToDefault(MDIOS_HandleTypeDef * hmdios)948 static void MDIOS_InitCallbacksToDefault(MDIOS_HandleTypeDef *hmdios)
949 {
950 /* Init the MDIOS Callback settings */
951 hmdios->WriteCpltCallback = HAL_MDIOS_WriteCpltCallback; /* Legacy weak WriteCpltCallback */
952 hmdios->ReadCpltCallback = HAL_MDIOS_ReadCpltCallback; /* Legacy weak ReadCpltCallback */
953 hmdios->ErrorCallback = HAL_MDIOS_ErrorCallback; /* Legacy weak ErrorCallback */
954 hmdios->WakeUpCallback = HAL_MDIOS_WakeUpCallback; /* Legacy weak WakeUpCallback */
955 }
956 /**
957 * @}
958 */
959 #endif /* USE_HAL_MDIOS_REGISTER_CALLBACKS */
960 #endif /* HAL_MDIOS_MODULE_ENABLED */
961 /**
962 * @}
963 */
964 #endif /* MDIOS */
965 /**
966 * @}
967 */
968
969