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