42fb86c5aa9f20883dec9e87cc57e744e7b41e76
2 ******************************************************************************
3 * @file stm32f7xx_hal_mdios.c
4 * @author MCD Application Team
7 * @brief MDIOS HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the MDIOS Peripheral.
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral Control functions
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
21 The MDIOS HAL driver can be used as follow:
23 (#) Declare a MDIOS_HandleTypeDef handle structure.
25 (#) Initialize the MDIOS low level resources by implementing the HAL_MDIOS_MspInit() API:
26 (##) Enable the MDIOS interface clock.
27 (##) MDIOS pins configuration:
28 (+++) Enable clocks for the MDIOS GPIOs.
29 (+++) Configure the MDIOS pins as alternate function.
30 (##) NVIC configuration if you need to use interrupt process:
31 (+++) Configure the MDIOS interrupt priority.
32 (+++) Enable the NVIC MDIOS IRQ handle.
34 (#) Program the Port Address and the Preamble Check in the Init structure.
36 (#) Initialize the MDIOS registers by calling the HAL_MDIOS_Init() API.
38 (#) Perform direct slave read/write operations using the following APIs:
39 (##) Read the value of a DINn register: HAL_MDIOS_ReadReg()
40 (##) Write a value to a DOUTn register: HAL_MDIOS_WriteReg()
42 (#) Get the Master read/write operations flags using the following APIs:
43 (##) Bit map of DOUTn registers read by Master: HAL_MDIOS_GetReadRegAddress()
44 (##) Bit map of DINn registers written by Master : HAL_MDIOS_GetWrittenRegAddress()
46 (#) Clear the read/write flags using the following APIs:
47 (##) Clear read flags of a set of registers: HAL_MDIOS_ClearReadRegAddress()
48 (##) Clear write flags of a set of registers: HAL_MDIOS_ClearWriteRegAddress()
50 (#) Enable interrupts on events using HAL_MDIOS_EnableEvents(), when called
51 the MDIOS will generate an interrupt in the following cases:
52 (##) a DINn register written by the Master
53 (##) a DOUTn register read by the Master
56 (@) A callback is executed for each genereted interrupt, so the driver provide the following
57 HAL_MDIOS_WriteCpltCallback(), HAL_MDIOS_ReadCpltCallback() and HAL_MDIOS_ErrorCallback()
58 (@) HAL_MDIOS_IRQHandler() must be called from the MDIOS IRQ Handler, to handle the interrupt
59 and execute the previous callbacks
61 (#) Reset the MDIOS peripheral and all related ressources by calling the HAL_MDIOS_DeInit() API.
62 (##) HAL_MDIOS_MspDeInit() must be implemented to reset low level ressources
63 (GPIO, Clocks, NVIC configuration ...)
67 ******************************************************************************
70 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
72 * Redistribution and use in source and binary forms, with or without modification,
73 * are permitted provided that the following conditions are met:
74 * 1. Redistributions of source code must retain the above copyright notice,
75 * this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright notice,
77 * this list of conditions and the following disclaimer in the documentation
78 * and/or other materials provided with the distribution.
79 * 3. Neither the name of STMicroelectronics nor the names of its contributors
80 * may be used to endorse or promote products derived from this software
81 * without specific prior written permission.
83 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
84 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
86 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
89 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
90 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
91 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
92 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94 ******************************************************************************
97 /* Includes ------------------------------------------------------------------*/
98 #include "stm32f7xx_hal.h"
100 /** @addtogroup STM32F7xx_HAL_Driver
104 /** @defgroup MDIOS MDIOS
105 * @brief HAL MDIOS module driver
108 #ifdef HAL_MDIOS_MODULE_ENABLED
112 /* Private typedef -----------------------------------------------------------*/
113 /* Private define ------------------------------------------------------------*/
114 #define MDIOS_PORT_ADDRESS_SHIFT ((uint32_t)8)
115 #define MDIOS_ALL_REG_FLAG ((uint32_t)0xFFFFFFFFU)
116 #define MDIOS_ALL_ERRORS_FLAG ((uint32_t)(MDIOS_SR_PERF | MDIOS_SR_SERF | MDIOS_SR_TERF))
118 #define MDIOS_DIN_BASE_ADDR (MDIOS_BASE + 0x100)
119 #define MDIOS_DOUT_BASE_ADDR (MDIOS_BASE + 0x180)
121 /* Private macro -------------------------------------------------------------*/
122 /* Private variables ---------------------------------------------------------*/
123 /* Private function prototypes -----------------------------------------------*/
124 /* Private functions ---------------------------------------------------------*/
125 /* Exported functions --------------------------------------------------------*/
126 /** @defgroup MDIOS_Exported_Functions MDIOS Exported Functions
130 /** @defgroup MDIOS_Exported_Functions_Group1 Initialization/de-initialization functions
131 * @brief Initialization and Configuration functions
134 ===============================================================================
135 ##### Initialization and Configuration functions #####
136 ===============================================================================
138 This subsection provides a set of functions allowing to initialize the MDIOS
139 (+) The following parameters can be configured:
148 * @brief Initializes the MDIOS according to the specified parameters in
149 * the MDIOS_InitTypeDef and creates the associated handle .
150 * @param hmdios: pointer to a MDIOS_HandleTypeDef structure that contains
151 * the configuration information for MDIOS module
154 HAL_StatusTypeDef
HAL_MDIOS_Init(MDIOS_HandleTypeDef
*hmdios
)
158 /* Check the MDIOS handle allocation */
164 /* Check the parameters */
165 assert_param(IS_MDIOS_ALL_INSTANCE(hmdios
->Instance
));
166 assert_param(IS_MDIOS_PORTADDRESS(hmdios
->Init
.PortAddress
));
167 assert_param(IS_MDIOS_PREAMBLECHECK(hmdios
->Init
.PreambleCheck
));
172 if(hmdios
->State
== HAL_MDIOS_STATE_RESET
)
174 /* Init the low level hardware */
175 HAL_MDIOS_MspInit(hmdios
);
178 /* Change the MDIOS state */
179 hmdios
->State
= HAL_MDIOS_STATE_BUSY
;
181 /* Get the MDIOS CR value */
182 tmpcr
= hmdios
->Instance
->CR
;
184 /* Clear PORT_ADDRESS, DPC and EN bits */
185 tmpcr
&= ((uint32_t)~(MDIOS_CR_EN
| MDIOS_CR_DPC
| MDIOS_CR_PORT_ADDRESS
));
187 /* Set MDIOS control parametrs and enable the peripheral */
188 tmpcr
|= (uint32_t)(((hmdios
->Init
.PortAddress
) << MDIOS_PORT_ADDRESS_SHIFT
) |\
189 (hmdios
->Init
.PreambleCheck
) | \
192 /* Write the MDIOS CR */
193 hmdios
->Instance
->CR
= tmpcr
;
195 /* Change the MDIOS state */
196 hmdios
->State
= HAL_MDIOS_STATE_READY
;
199 __HAL_UNLOCK(hmdios
);
201 /* Return function status */
207 * @brief DeInitializes the MDIOS peripheral.
208 * @param hmdios: MDIOS handle
211 HAL_StatusTypeDef
HAL_MDIOS_DeInit(MDIOS_HandleTypeDef
*hmdios
)
213 /* Check the MDIOS handle allocation */
219 /* Check the parameters */
220 assert_param(IS_MDIOS_ALL_INSTANCE(hmdios
->Instance
));
222 /* Change the MDIOS state */
223 hmdios
->State
= HAL_MDIOS_STATE_BUSY
;
225 /* Disable the Peripheral */
226 __HAL_MDIOS_DISABLE(hmdios
);
228 /* DeInit the low level hardware */
229 HAL_MDIOS_MspDeInit(hmdios
);
231 /* Change the MDIOS state */
232 hmdios
->State
= HAL_MDIOS_STATE_RESET
;
235 __HAL_UNLOCK(hmdios
);
237 /* Return function status */
242 * @brief MDIOS MSP Init
243 * @param hmdios: mdios handle
246 __weak
void HAL_MDIOS_MspInit(MDIOS_HandleTypeDef
*hmdios
)
248 /* Prevent unused argument(s) compilation warning */
251 /* NOTE : This function should not be modified, when the callback is needed,
252 the HAL_MDIOS_MspInit can be implemented in the user file
257 * @brief MDIOS MSP DeInit
258 * @param hmdios: mdios handle
261 __weak
void HAL_MDIOS_MspDeInit(MDIOS_HandleTypeDef
*hmdios
)
263 /* Prevent unused argument(s) compilation warning */
266 /* NOTE : This function should not be modified, when the callback is needed,
267 the HAL_MDIOS_MspDeInit can be implemented in the user file
274 /** @defgroup MDIOS_Exported_Functions_Group2 IO operation functions
275 * @brief MDIOS Read/Write functions
278 ===============================================================================
279 ##### IO operation functions #####
280 ===============================================================================
281 This subsection provides a set of functions allowing to manage the MDIOS
282 read and write operations.
284 (#) APIs that allow to the MDIOS to read/write from/to the
285 values of one of the DINn/DOUTn registers:
286 (+) Read the value of a DINn register: HAL_MDIOS_ReadReg()
287 (+) Write a value to a DOUTn register: HAL_MDIOS_WriteReg()
289 (#) APIs that provide if there are some Slave registres have been
290 read or written by the Master:
291 (+) DOUTn registers read by Master: HAL_MDIOS_GetReadRegAddress()
292 (+) DINn registers written by Master : HAL_MDIOS_GetWrittenRegAddress()
294 (#) APIs that Clear the read/write flags:
295 (+) Clear read registers flags: HAL_MDIOS_ClearReadRegAddress()
296 (+) Clear write registers flags: HAL_MDIOS_ClearWriteRegAddress()
298 (#) A set of Callbacks are provided:
299 (+) HAL_MDIOS_WriteCpltCallback()
300 (+) HAL_MDIOS_ReadCpltCallback()
301 (+) HAL_MDIOS_ErrorCallback()
308 * @brief Writes to an MDIOS output register
309 * @param hmdios: mdios handle
310 * @param RegNum: MDIOS input register number
311 * @param Data: Data to write
314 HAL_StatusTypeDef
HAL_MDIOS_WriteReg(MDIOS_HandleTypeDef
*hmdios
, uint32_t RegNum
, uint16_t Data
)
318 /* Check the parameters */
319 assert_param(IS_MDIOS_REGISTER(RegNum
));
324 /* Get the addr of output register to be written by the MDIOS */
325 tmpreg
= MDIOS_DOUT_BASE_ADDR
+ (4 * RegNum
);
327 /* Write to DOUTn register */
328 *((uint32_t *)tmpreg
) = Data
;
330 /* Process Unlocked */
331 __HAL_UNLOCK(hmdios
);
337 * @brief Reads an MDIOS input register
338 * @param hmdios: mdios handle
339 * @param RegNum: MDIOS input register number
340 * @param pData: pointer to Data
343 HAL_StatusTypeDef
HAL_MDIOS_ReadReg(MDIOS_HandleTypeDef
*hmdios
, uint32_t RegNum
, uint16_t *pData
)
347 /* Check the parameters */
348 assert_param(IS_MDIOS_REGISTER(RegNum
));
353 /* Get the addr of input register to be read by the MDIOS */
354 tmpreg
= MDIOS_DIN_BASE_ADDR
+ (4 * RegNum
);
356 /* Read DINn register */
357 *pData
= (uint16_t)(*((uint32_t *)tmpreg
));
359 /* Process Unlocked */
360 __HAL_UNLOCK(hmdios
);
366 * @brief Gets Written registers by MDIO master
367 * @param hmdios: mdios handle
368 * @retval bit map of written registers addresses
370 uint32_t HAL_MDIOS_GetWrittenRegAddress(MDIOS_HandleTypeDef
*hmdios
)
372 return hmdios
->Instance
->WRFR
;
376 * @brief Gets Read registers by MDIO master
377 * @param hmdios: mdios handle
378 * @retval bit map of read registers addresses
380 uint32_t HAL_MDIOS_GetReadRegAddress(MDIOS_HandleTypeDef
*hmdios
)
382 return hmdios
->Instance
->RDFR
;
386 * @brief Clears Write registers flag
387 * @param hmdios: mdios handle
388 * @param RegNum: registers addresses to be cleared
391 HAL_StatusTypeDef
HAL_MDIOS_ClearWriteRegAddress(MDIOS_HandleTypeDef
*hmdios
, uint32_t RegNum
)
393 /* Check the parameters */
394 assert_param(IS_MDIOS_REGISTER(RegNum
));
399 /* Clear write registers flags */
400 hmdios
->Instance
->CWRFR
|= (RegNum
);
403 __HAL_UNLOCK(hmdios
);
409 * @brief Clears Read register flag
410 * @param hmdios: mdios handle
411 * @param RegNum: registers addresses to be cleared
414 HAL_StatusTypeDef
HAL_MDIOS_ClearReadRegAddress(MDIOS_HandleTypeDef
*hmdios
, uint32_t RegNum
)
416 /* Check the parameters */
417 assert_param(IS_MDIOS_REGISTER(RegNum
));
422 /* Clear read registers flags */
423 hmdios
->Instance
->CRDFR
|= (RegNum
);
426 __HAL_UNLOCK(hmdios
);
432 * @brief Enables Events for MDIOS peripheral
433 * @param hmdios: mdios handle
436 HAL_StatusTypeDef
HAL_MDIOS_EnableEvents(MDIOS_HandleTypeDef
*hmdios
)
441 /* Enable MDIOS interrupts: Register Write, Register Read and Error ITs */
442 __HAL_MDIOS_ENABLE_IT(hmdios
, (MDIOS_IT_WRITE
| MDIOS_IT_READ
| MDIOS_IT_ERROR
));
444 /* Process Unlocked */
445 __HAL_UNLOCK(hmdios
);
451 * @brief This function handles MDIOS interrupt request.
452 * @param hmdios: MDIOS handle
455 void HAL_MDIOS_IRQHandler(MDIOS_HandleTypeDef
*hmdios
)
457 /* Write Register Interrupt enabled ? */
458 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios
, MDIOS_IT_WRITE
) != RESET
)
460 /* Write register flag */
461 if(HAL_MDIOS_GetWrittenRegAddress(hmdios
) != RESET
)
463 /* Write callback function */
464 HAL_MDIOS_WriteCpltCallback(hmdios
);
466 /* Clear write register flag */
467 HAL_MDIOS_ClearWriteRegAddress(hmdios
, MDIOS_ALL_REG_FLAG
);
471 /* Read Register Interrupt enabled ? */
472 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios
, MDIOS_IT_READ
) != RESET
)
474 /* Read register flag */
475 if(HAL_MDIOS_GetReadRegAddress(hmdios
) != RESET
)
477 /* Read callback function */
478 HAL_MDIOS_ReadCpltCallback(hmdios
);
480 /* Clear read register flag */
481 HAL_MDIOS_ClearReadRegAddress(hmdios
, MDIOS_ALL_REG_FLAG
);
485 /* Error Interrupt enabled ? */
486 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios
, MDIOS_IT_ERROR
) != RESET
)
488 /* All Errors Flag */
489 if(__HAL_MDIOS_GET_ERROR_FLAG(hmdios
, MDIOS_ALL_ERRORS_FLAG
) !=RESET
)
492 HAL_MDIOS_ErrorCallback(hmdios
);
494 /* Clear errors flag */
495 __HAL_MDIOS_CLEAR_ERROR_FLAG(hmdios
, MDIOS_ALL_ERRORS_FLAG
);
499 /* check MDIOS WAKEUP exti flag */
500 if(__HAL_MDIOS_WAKEUP_EXTI_GET_FLAG() != RESET
)
502 /* MDIOS WAKEUP interrupt user callback */
503 HAL_MDIOS_WakeUpCallback(hmdios
);
505 /* Clear MDIOS WAKEUP Exti pending bit */
506 __HAL_MDIOS_WAKEUP_EXTI_CLEAR_FLAG();
511 * @brief Write Complete Callback
512 * @param hmdios: mdios handle
515 __weak
void HAL_MDIOS_WriteCpltCallback(MDIOS_HandleTypeDef
*hmdios
)
517 /* Prevent unused argument(s) compilation warning */
520 /* NOTE : This function should not be modified, when the callback is needed,
521 the HAL_MDIOS_WriteCpltCallback can be implemented in the user file
526 * @brief Read Complete Callback
527 * @param hmdios: mdios handle
530 __weak
void HAL_MDIOS_ReadCpltCallback(MDIOS_HandleTypeDef
*hmdios
)
532 /* Prevent unused argument(s) compilation warning */
535 /* NOTE : This function should not be modified, when the callback is needed,
536 the HAL_MDIOS_ReadCpltCallback can be implemented in the user file
541 * @brief Error Callback
542 * @param hmdios: mdios handle
545 __weak
void HAL_MDIOS_ErrorCallback(MDIOS_HandleTypeDef
*hmdios
)
547 /* Prevent unused argument(s) compilation warning */
550 /* NOTE : This function should not be modified, when the callback is needed,
551 the HAL_MDIOS_ErrorCallback can be implemented in the user file
556 * @brief MDIOS WAKEUP interrupt callback
557 * @param hmdios: mdios handle
560 __weak
void HAL_MDIOS_WakeUpCallback(MDIOS_HandleTypeDef
*hmdios
)
562 /* Prevent unused argument(s) compilation warning */
565 /* NOTE : This function Should not be modified, when the callback is needed,
566 the HAL_MDIOS_WakeUpCallback could be implemented in the user file
574 /** @defgroup MDIOS_Exported_Functions_Group3 Peripheral Control functions
575 * @brief MDIOS control functions
578 ===============================================================================
579 ##### Peripheral Control functions #####
580 ===============================================================================
582 This subsection provides a set of functions allowing to control the MDIOS.
583 (+) HAL_MDIOS_GetState() API, helpful to check in run-time the state.
584 (+) HAL_MDIOS_GetError() API, returns the errors occured during data transfer.
591 * @brief Gets MDIOS error flags
592 * @param hmdios: mdios handle
593 * @retval bit map of occured errors
595 uint32_t HAL_MDIOS_GetError(MDIOS_HandleTypeDef
*hmdios
)
597 /* return errors flags on status register */
598 return hmdios
->Instance
->SR
;
602 * @brief Return the MDIOS HAL state
603 * @param hmdios: mdios handle
604 * @retval MDIOS state
606 HAL_MDIOS_StateTypeDef
HAL_MDIOS_GetState(MDIOS_HandleTypeDef
*hmdios
)
608 /* Return MDIOS state */
609 return hmdios
->State
;
620 #endif /* HAL_MDIOS_MODULE_ENABLED */
629 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/