You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

958 lines
29 KiB

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