sidestep
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_mdios.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_mdios.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
7 * @brief MDIOS HAL module driver.
8 *
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
14 *
15 *
16 @verbatim
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
20 [..]
21 The MDIOS HAL driver can be used as follow:
22
23 (#) Declare a MDIOS_HandleTypeDef handle structure.
24
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.
33
34 (#) Program the Port Address and the Preamble Check in the Init structure.
35
36 (#) Initialize the MDIOS registers by calling the HAL_MDIOS_Init() API.
37
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()
41
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()
45
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()
49
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
54 (##) an error occur
55
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
60
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 ...)
64
65
66 @endverbatim
67 ******************************************************************************
68 * @attention
69 *
70 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
71 *
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.
82 *
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.
93 *
94 ******************************************************************************
95 */
96
97 /* Includes ------------------------------------------------------------------*/
98 #include "stm32f7xx_hal.h"
99
100 /** @addtogroup STM32F7xx_HAL_Driver
101 * @{
102 */
103
104 /** @defgroup MDIOS MDIOS
105 * @brief HAL MDIOS module driver
106 * @{
107 */
108 #ifdef HAL_MDIOS_MODULE_ENABLED
109
110 #if defined (MDIOS)
111
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))
117
118 #define MDIOS_DIN_BASE_ADDR (MDIOS_BASE + 0x100)
119 #define MDIOS_DOUT_BASE_ADDR (MDIOS_BASE + 0x180)
120
121 /* Private macro -------------------------------------------------------------*/
122 /* Private variables ---------------------------------------------------------*/
123 /* Private function prototypes -----------------------------------------------*/
124 /* Private functions ---------------------------------------------------------*/
125 /* Exported functions --------------------------------------------------------*/
126 /** @defgroup MDIOS_Exported_Functions MDIOS Exported Functions
127 * @{
128 */
129
130 /** @defgroup MDIOS_Exported_Functions_Group1 Initialization/de-initialization functions
131 * @brief Initialization and Configuration functions
132 *
133 @verbatim
134 ===============================================================================
135 ##### Initialization and Configuration functions #####
136 ===============================================================================
137 [..]
138 This subsection provides a set of functions allowing to initialize the MDIOS
139 (+) The following parameters can be configured:
140 (++) Port Address
141 (++) Preamble Check
142
143 @endverbatim
144 * @{
145 */
146
147 /**
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
152 * @retval HAL status
153 */
154 HAL_StatusTypeDef HAL_MDIOS_Init(MDIOS_HandleTypeDef *hmdios)
155 {
156 uint32_t tmpcr = 0;
157
158 /* Check the MDIOS handle allocation */
159 if(hmdios == NULL)
160 {
161 return HAL_ERROR;
162 }
163
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));
168
169 /* Process Locked */
170 __HAL_LOCK(hmdios);
171
172 if(hmdios->State == HAL_MDIOS_STATE_RESET)
173 {
174 /* Init the low level hardware */
175 HAL_MDIOS_MspInit(hmdios);
176 }
177
178 /* Change the MDIOS state */
179 hmdios->State = HAL_MDIOS_STATE_BUSY;
180
181 /* Get the MDIOS CR value */
182 tmpcr = hmdios->Instance->CR;
183
184 /* Clear PORT_ADDRESS, DPC and EN bits */
185 tmpcr &= ((uint32_t)~(MDIOS_CR_EN | MDIOS_CR_DPC | MDIOS_CR_PORT_ADDRESS));
186
187 /* Set MDIOS control parametrs and enable the peripheral */
188 tmpcr |= (uint32_t)(((hmdios->Init.PortAddress) << MDIOS_PORT_ADDRESS_SHIFT) |\
189 (hmdios->Init.PreambleCheck) | \
190 (MDIOS_CR_EN));
191
192 /* Write the MDIOS CR */
193 hmdios->Instance->CR = tmpcr;
194
195 /* Change the MDIOS state */
196 hmdios->State = HAL_MDIOS_STATE_READY;
197
198 /* Release Lock */
199 __HAL_UNLOCK(hmdios);
200
201 /* Return function status */
202 return HAL_OK;
203
204 }
205
206 /**
207 * @brief DeInitializes the MDIOS peripheral.
208 * @param hmdios: MDIOS handle
209 * @retval HAL status
210 */
211 HAL_StatusTypeDef HAL_MDIOS_DeInit(MDIOS_HandleTypeDef *hmdios)
212 {
213 /* Check the MDIOS handle allocation */
214 if(hmdios == NULL)
215 {
216 return HAL_ERROR;
217 }
218
219 /* Check the parameters */
220 assert_param(IS_MDIOS_ALL_INSTANCE(hmdios->Instance));
221
222 /* Change the MDIOS state */
223 hmdios->State = HAL_MDIOS_STATE_BUSY;
224
225 /* Disable the Peripheral */
226 __HAL_MDIOS_DISABLE(hmdios);
227
228 /* DeInit the low level hardware */
229 HAL_MDIOS_MspDeInit(hmdios);
230
231 /* Change the MDIOS state */
232 hmdios->State = HAL_MDIOS_STATE_RESET;
233
234 /* Release Lock */
235 __HAL_UNLOCK(hmdios);
236
237 /* Return function status */
238 return HAL_OK;
239 }
240
241 /**
242 * @brief MDIOS MSP Init
243 * @param hmdios: mdios handle
244 * @retval None
245 */
246 __weak void HAL_MDIOS_MspInit(MDIOS_HandleTypeDef *hmdios)
247 {
248 /* Prevent unused argument(s) compilation warning */
249 UNUSED(hmdios);
250
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
253 */
254 }
255
256 /**
257 * @brief MDIOS MSP DeInit
258 * @param hmdios: mdios handle
259 * @retval None
260 */
261 __weak void HAL_MDIOS_MspDeInit(MDIOS_HandleTypeDef *hmdios)
262 {
263 /* Prevent unused argument(s) compilation warning */
264 UNUSED(hmdios);
265
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
268 */
269 }
270 /**
271 * @}
272 */
273
274 /** @defgroup MDIOS_Exported_Functions_Group2 IO operation functions
275 * @brief MDIOS Read/Write functions
276 *
277 @verbatim
278 ===============================================================================
279 ##### IO operation functions #####
280 ===============================================================================
281 This subsection provides a set of functions allowing to manage the MDIOS
282 read and write operations.
283
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()
288
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()
293
294 (#) APIs that Clear the read/write flags:
295 (+) Clear read registers flags: HAL_MDIOS_ClearReadRegAddress()
296 (+) Clear write registers flags: HAL_MDIOS_ClearWriteRegAddress()
297
298 (#) A set of Callbacks are provided:
299 (+) HAL_MDIOS_WriteCpltCallback()
300 (+) HAL_MDIOS_ReadCpltCallback()
301 (+) HAL_MDIOS_ErrorCallback()
302
303 @endverbatim
304 * @{
305 */
306
307 /**
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
312 * @retval HAL status
313 */
314 HAL_StatusTypeDef HAL_MDIOS_WriteReg(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum, uint16_t Data)
315 {
316 uint32_t tmpreg;
317
318 /* Check the parameters */
319 assert_param(IS_MDIOS_REGISTER(RegNum));
320
321 /* Process Locked */
322 __HAL_LOCK(hmdios);
323
324 /* Get the addr of output register to be written by the MDIOS */
325 tmpreg = MDIOS_DOUT_BASE_ADDR + (4 * RegNum);
326
327 /* Write to DOUTn register */
328 *((uint32_t *)tmpreg) = Data;
329
330 /* Process Unlocked */
331 __HAL_UNLOCK(hmdios);
332
333 return HAL_OK;
334 }
335
336 /**
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
341 * @retval HAL status
342 */
343 HAL_StatusTypeDef HAL_MDIOS_ReadReg(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum, uint16_t *pData)
344 {
345 uint32_t tmpreg;
346
347 /* Check the parameters */
348 assert_param(IS_MDIOS_REGISTER(RegNum));
349
350 /* Process Locked */
351 __HAL_LOCK(hmdios);
352
353 /* Get the addr of input register to be read by the MDIOS */
354 tmpreg = MDIOS_DIN_BASE_ADDR + (4 * RegNum);
355
356 /* Read DINn register */
357 *pData = (uint16_t)(*((uint32_t *)tmpreg));
358
359 /* Process Unlocked */
360 __HAL_UNLOCK(hmdios);
361
362 return HAL_OK;
363 }
364
365 /**
366 * @brief Gets Written registers by MDIO master
367 * @param hmdios: mdios handle
368 * @retval bit map of written registers addresses
369 */
370 uint32_t HAL_MDIOS_GetWrittenRegAddress(MDIOS_HandleTypeDef *hmdios)
371 {
372 return hmdios->Instance->WRFR;
373 }
374
375 /**
376 * @brief Gets Read registers by MDIO master
377 * @param hmdios: mdios handle
378 * @retval bit map of read registers addresses
379 */
380 uint32_t HAL_MDIOS_GetReadRegAddress(MDIOS_HandleTypeDef *hmdios)
381 {
382 return hmdios->Instance->RDFR;
383 }
384
385 /**
386 * @brief Clears Write registers flag
387 * @param hmdios: mdios handle
388 * @param RegNum: registers addresses to be cleared
389 * @retval HAL status
390 */
391 HAL_StatusTypeDef HAL_MDIOS_ClearWriteRegAddress(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum)
392 {
393 /* Check the parameters */
394 assert_param(IS_MDIOS_REGISTER(RegNum));
395
396 /* Process Locked */
397 __HAL_LOCK(hmdios);
398
399 /* Clear write registers flags */
400 hmdios->Instance->CWRFR |= (RegNum);
401
402 /* Release Lock */
403 __HAL_UNLOCK(hmdios);
404
405 return HAL_OK;
406 }
407
408 /**
409 * @brief Clears Read register flag
410 * @param hmdios: mdios handle
411 * @param RegNum: registers addresses to be cleared
412 * @retval HAL status
413 */
414 HAL_StatusTypeDef HAL_MDIOS_ClearReadRegAddress(MDIOS_HandleTypeDef *hmdios, uint32_t RegNum)
415 {
416 /* Check the parameters */
417 assert_param(IS_MDIOS_REGISTER(RegNum));
418
419 /* Process Locked */
420 __HAL_LOCK(hmdios);
421
422 /* Clear read registers flags */
423 hmdios->Instance->CRDFR |= (RegNum);
424
425 /* Release Lock */
426 __HAL_UNLOCK(hmdios);
427
428 return HAL_OK;
429 }
430
431 /**
432 * @brief Enables Events for MDIOS peripheral
433 * @param hmdios: mdios handle
434 * @retval HAL status
435 */
436 HAL_StatusTypeDef HAL_MDIOS_EnableEvents(MDIOS_HandleTypeDef *hmdios)
437 {
438 /* Process Locked */
439 __HAL_LOCK(hmdios);
440
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));
443
444 /* Process Unlocked */
445 __HAL_UNLOCK(hmdios);
446
447 return HAL_OK;
448 }
449
450 /**
451 * @brief This function handles MDIOS interrupt request.
452 * @param hmdios: MDIOS handle
453 * @retval None
454 */
455 void HAL_MDIOS_IRQHandler(MDIOS_HandleTypeDef *hmdios)
456 {
457 /* Write Register Interrupt enabled ? */
458 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_WRITE) != RESET)
459 {
460 /* Write register flag */
461 if(HAL_MDIOS_GetWrittenRegAddress(hmdios) != RESET)
462 {
463 /* Write callback function */
464 HAL_MDIOS_WriteCpltCallback(hmdios);
465
466 /* Clear write register flag */
467 HAL_MDIOS_ClearWriteRegAddress(hmdios, MDIOS_ALL_REG_FLAG);
468 }
469 }
470
471 /* Read Register Interrupt enabled ? */
472 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_READ) != RESET)
473 {
474 /* Read register flag */
475 if(HAL_MDIOS_GetReadRegAddress(hmdios) != RESET)
476 {
477 /* Read callback function */
478 HAL_MDIOS_ReadCpltCallback(hmdios);
479
480 /* Clear read register flag */
481 HAL_MDIOS_ClearReadRegAddress(hmdios, MDIOS_ALL_REG_FLAG);
482 }
483 }
484
485 /* Error Interrupt enabled ? */
486 if(__HAL_MDIOS_GET_IT_SOURCE(hmdios, MDIOS_IT_ERROR) != RESET)
487 {
488 /* All Errors Flag */
489 if(__HAL_MDIOS_GET_ERROR_FLAG(hmdios, MDIOS_ALL_ERRORS_FLAG) !=RESET)
490 {
491 /* Error Callback */
492 HAL_MDIOS_ErrorCallback(hmdios);
493
494 /* Clear errors flag */
495 __HAL_MDIOS_CLEAR_ERROR_FLAG(hmdios, MDIOS_ALL_ERRORS_FLAG);
496 }
497 }
498
499 /* check MDIOS WAKEUP exti flag */
500 if(__HAL_MDIOS_WAKEUP_EXTI_GET_FLAG() != RESET)
501 {
502 /* MDIOS WAKEUP interrupt user callback */
503 HAL_MDIOS_WakeUpCallback(hmdios);
504
505 /* Clear MDIOS WAKEUP Exti pending bit */
506 __HAL_MDIOS_WAKEUP_EXTI_CLEAR_FLAG();
507 }
508 }
509
510 /**
511 * @brief Write Complete Callback
512 * @param hmdios: mdios handle
513 * @retval None
514 */
515 __weak void HAL_MDIOS_WriteCpltCallback(MDIOS_HandleTypeDef *hmdios)
516 {
517 /* Prevent unused argument(s) compilation warning */
518 UNUSED(hmdios);
519
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
522 */
523 }
524
525 /**
526 * @brief Read Complete Callback
527 * @param hmdios: mdios handle
528 * @retval None
529 */
530 __weak void HAL_MDIOS_ReadCpltCallback(MDIOS_HandleTypeDef *hmdios)
531 {
532 /* Prevent unused argument(s) compilation warning */
533 UNUSED(hmdios);
534
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
537 */
538 }
539
540 /**
541 * @brief Error Callback
542 * @param hmdios: mdios handle
543 * @retval None
544 */
545 __weak void HAL_MDIOS_ErrorCallback(MDIOS_HandleTypeDef *hmdios)
546 {
547 /* Prevent unused argument(s) compilation warning */
548 UNUSED(hmdios);
549
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
552 */
553 }
554
555 /**
556 * @brief MDIOS WAKEUP interrupt callback
557 * @param hmdios: mdios handle
558 * @retval None
559 */
560 __weak void HAL_MDIOS_WakeUpCallback(MDIOS_HandleTypeDef *hmdios)
561 {
562 /* Prevent unused argument(s) compilation warning */
563 UNUSED(hmdios);
564
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
567 */
568 }
569
570 /**
571 * @}
572 */
573
574 /** @defgroup MDIOS_Exported_Functions_Group3 Peripheral Control functions
575 * @brief MDIOS control functions
576 *
577 @verbatim
578 ===============================================================================
579 ##### Peripheral Control functions #####
580 ===============================================================================
581 [..]
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.
585
586 @endverbatim
587 * @{
588 */
589
590 /**
591 * @brief Gets MDIOS error flags
592 * @param hmdios: mdios handle
593 * @retval bit map of occured errors
594 */
595 uint32_t HAL_MDIOS_GetError(MDIOS_HandleTypeDef *hmdios)
596 {
597 /* return errors flags on status register */
598 return hmdios->Instance->SR;
599 }
600
601 /**
602 * @brief Return the MDIOS HAL state
603 * @param hmdios: mdios handle
604 * @retval MDIOS state
605 */
606 HAL_MDIOS_StateTypeDef HAL_MDIOS_GetState(MDIOS_HandleTypeDef *hmdios)
607 {
608 /* Return MDIOS state */
609 return hmdios->State;
610 }
611
612 /**
613 * @}
614 */
615
616 /**
617 * @}
618 */
619 #endif /* MDIOS */
620 #endif /* HAL_MDIOS_MODULE_ENABLED */
621 /**
622 * @}
623 */
624
625 /**
626 * @}
627 */
628
629 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/