stm32l4xx_hal_ospi.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_ospi.c
  4. * @author MCD Application Team
  5. * @brief OSPI HAL module driver.
  6. This file provides firmware functions to manage the following
  7. functionalities of the OctoSPI interface (OSPI).
  8. + Initialization and de-initialization functions
  9. + Hyperbus configuration
  10. + Indirect functional mode management
  11. + Memory-mapped functional mode management
  12. + Auto-polling functional mode management
  13. + Interrupts and flags management
  14. + DMA channel configuration for indirect functional mode
  15. + Errors management and abort functionality
  16. + IO manager configuration
  17. @verbatim
  18. ===============================================================================
  19. ##### How to use this driver #####
  20. ===============================================================================
  21. [..]
  22. *** Initialization ***
  23. ======================
  24. [..]
  25. (#) As prerequisite, fill in the HAL_OSPI_MspInit() :
  26. (++) Enable OctoSPI and OctoSPIM clocks interface with __HAL_RCC_OSPIx_CLK_ENABLE().
  27. (++) Reset OctoSPI IP with __HAL_RCC_OSPIx_FORCE_RESET() and __HAL_RCC_OSPIx_RELEASE_RESET().
  28. (++) Enable the clocks for the OctoSPI GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
  29. (++) Configure these OctoSPI pins in alternate mode using HAL_GPIO_Init().
  30. (++) If interrupt or DMA mode is used, enable and configure OctoSPI global
  31. interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  32. (++) If DMA mode is used, enable the clocks for the OctoSPI DMA channel
  33. with __HAL_RCC_DMAx_CLK_ENABLE(), configure DMA with HAL_DMA_Init(),
  34. link it with OctoSPI handle using __HAL_LINKDMA(), enable and configure
  35. DMA channel global interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  36. (#) Configure the fifo threshold, the dual-quad mode, the memory type, the
  37. device size, the CS high time, the free running clock, the clock mode,
  38. the wrap size, the clock prescaler, the sample shifting, the hold delay
  39. and the CS boundary using the HAL_OSPI_Init() function.
  40. (#) When using Hyperbus, configure the RW recovery time, the access time,
  41. the write latency and the latency mode unsing the HAL_OSPI_HyperbusCfg()
  42. function.
  43. *** Indirect functional mode ***
  44. ================================
  45. [..]
  46. (#) In regular mode, configure the command sequence using the HAL_OSPI_Command()
  47. or HAL_OSPI_Command_IT() functions :
  48. (++) Instruction phase : the mode used and if present the size, the instruction
  49. opcode and the DTR mode.
  50. (++) Address phase : the mode used and if present the size, the address
  51. value and the DTR mode.
  52. (++) Alternate-bytes phase : the mode used and if present the size, the
  53. alternate bytes values and the DTR mode.
  54. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  55. (++) Data phase : the mode used and if present the number of bytes and the DTR mode.
  56. (++) Data strobe (DQS) mode : the activation (or not) of this mode
  57. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  58. (++) Flash identifier : in dual-quad mode, indicates which flash is concerned
  59. (++) Operation type : always common configuration
  60. (#) In Hyperbus mode, configure the command sequence using the HAL_OSPI_HyperbusCmd()
  61. function :
  62. (++) Address space : indicate if the access will be done in register or memory
  63. (++) Address size
  64. (++) Number of data
  65. (++) Data strobe (DQS) mode : the activation (or not) of this mode
  66. (#) If no data is required for the command (only for regular mode, not for
  67. Hyperbus mode), it is sent directly to the memory :
  68. (++) In polling mode, the output of the function is done when the transfer is complete.
  69. (++) In interrupt mode, HAL_OSPI_CmdCpltCallback() will be called when the transfer is complete.
  70. (#) For the indirect write mode, use HAL_OSPI_Transmit(), HAL_OSPI_Transmit_DMA() or
  71. HAL_OSPI_Transmit_IT() after the command configuration :
  72. (++) In polling mode, the output of the function is done when the transfer is complete.
  73. (++) In interrupt mode, HAL_OSPI_FifoThresholdCallback() will be called when the fifo threshold
  74. is reached and HAL_OSPI_TxCpltCallback() will be called when the transfer is complete.
  75. (++) In DMA mode, HAL_OSPI_TxHalfCpltCallback() will be called at the half transfer and
  76. HAL_OSPI_TxCpltCallback() will be called when the transfer is complete.
  77. (#) For the indirect read mode, use HAL_OSPI_Receive(), HAL_OSPI_Receive_DMA() or
  78. HAL_OSPI_Receive_IT() after the command configuration :
  79. (++) In polling mode, the output of the function is done when the transfer is complete.
  80. (++) In interrupt mode, HAL_OSPI_FifoThresholdCallback() will be called when the fifo threshold
  81. is reached and HAL_OSPI_RxCpltCallback() will be called when the transfer is complete.
  82. (++) In DMA mode, HAL_OSPI_RxHalfCpltCallback() will be called at the half transfer and
  83. HAL_OSPI_RxCpltCallback() will be called when the transfer is complete.
  84. *** Auto-polling functional mode ***
  85. ====================================
  86. [..]
  87. (#) Configure the command sequence by the same way than the indirect mode
  88. (#) Configure the auto-polling functional mode using the HAL_OSPI_AutoPolling()
  89. or HAL_OSPI_AutoPolling_IT() functions :
  90. (++) The size of the status bytes, the match value, the mask used, the match mode (OR/AND),
  91. the polling interval and the automatic stop activation.
  92. (#) After the configuration :
  93. (++) In polling mode, the output of the function is done when the status match is reached. The
  94. automatic stop is activated to avoid an infinite loop.
  95. (++) In interrupt mode, HAL_OSPI_StatusMatchCallback() will be called each time the status match is reached.
  96. *** Memory-mapped functional mode ***
  97. =====================================
  98. [..]
  99. (#) Configure the command sequence by the same way than the indirect mode except
  100. for the operation type in regular mode :
  101. (++) Operation type equals to read configuration : the command configuration
  102. applies to read access in memory-mapped mode
  103. (++) Operation type equals to write configuration : the command configuration
  104. applies to write access in memory-mapped mode
  105. (++) Both read and write configuration should be performed before activating
  106. memory-mapped mode
  107. (#) Configure the memory-mapped functional mode using the HAL_OSPI_MemoryMapped()
  108. functions :
  109. (++) The timeout activation and the timeout period.
  110. (#) After the configuration, the OctoSPI will be used as soon as an access on the AHB is done on
  111. the address range. HAL_OSPI_TimeOutCallback() will be called when the timeout expires.
  112. *** Errors management and abort functionality ***
  113. =================================================
  114. [..]
  115. (#) HAL_OSPI_GetError() function gives the error raised during the last operation.
  116. (#) HAL_OSPI_Abort() and HAL_OSPI_AbortIT() functions aborts any on-going operation and
  117. flushes the fifo :
  118. (++) In polling mode, the output of the function is done when the transfer
  119. complete bit is set and the busy bit cleared.
  120. (++) In interrupt mode, HAL_OSPI_AbortCpltCallback() will be called when
  121. the transfer complete bit is set.
  122. *** Control functions ***
  123. =========================
  124. [..]
  125. (#) HAL_OSPI_GetState() function gives the current state of the HAL OctoSPI driver.
  126. (#) HAL_OSPI_SetTimeout() function configures the timeout value used in the driver.
  127. (#) HAL_OSPI_SetFifoThreshold() function configures the threshold on the Fifo of the OSPI IP.
  128. (#) HAL_OSPI_GetFifoThreshold() function gives the current of the Fifo's threshold
  129. *** IO manager configuration functions ***
  130. ==========================================
  131. [..]
  132. (#) HAL_OSPIM_Config() function configures the IO manager for the OctoSPI instance.
  133. @endverbatim
  134. ******************************************************************************
  135. * @attention
  136. *
  137. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  138. *
  139. * Redistribution and use in source and binary forms, with or without modification,
  140. * are permitted provided that the following conditions are met:
  141. * 1. Redistributions of source code must retain the above copyright notice,
  142. * this list of conditions and the following disclaimer.
  143. * 2. Redistributions in binary form must reproduce the above copyright notice,
  144. * this list of conditions and the following disclaimer in the documentation
  145. * and/or other materials provided with the distribution.
  146. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  147. * may be used to endorse or promote products derived from this software
  148. * without specific prior written permission.
  149. *
  150. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  151. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  152. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  153. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  154. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  155. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  156. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  157. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  158. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  159. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  160. *
  161. ******************************************************************************
  162. */
  163. /* Includes ------------------------------------------------------------------*/
  164. #include "stm32l4xx_hal.h"
  165. #if defined(OCTOSPI) || defined(OCTOSPI1) || defined(OCTOSPI2)
  166. /** @addtogroup STM32L4xx_HAL_Driver
  167. * @{
  168. */
  169. /** @defgroup OSPI OSPI
  170. * @brief OSPI HAL module driver
  171. * @{
  172. */
  173. #ifdef HAL_OSPI_MODULE_ENABLED
  174. /**
  175. @cond 0
  176. */
  177. /* Private typedef -----------------------------------------------------------*/
  178. /* Private define ------------------------------------------------------------*/
  179. #define OSPI_FUNCTIONAL_MODE_INDIRECT_WRITE ((uint32_t)0x00000000) /*!< Indirect write mode */
  180. #define OSPI_FUNCTIONAL_MODE_INDIRECT_READ ((uint32_t)OCTOSPI_CR_FMODE_0) /*!< Indirect read mode */
  181. #define OSPI_FUNCTIONAL_MODE_AUTO_POLLING ((uint32_t)OCTOSPI_CR_FMODE_1) /*!< Automatic polling mode */
  182. #define OSPI_FUNCTIONAL_MODE_MEMORY_MAPPED ((uint32_t)OCTOSPI_CR_FMODE) /*!< Memory-mapped mode */
  183. #define OSPI_CFG_STATE_MASK 0x00000004U
  184. #define OSPI_BUSY_STATE_MASK 0x00000008U
  185. #define OSPI_NB_INSTANCE 2
  186. #define OSPI_IOM_NB_PORTS 2
  187. /* Private macro -------------------------------------------------------------*/
  188. #define IS_OSPI_FUNCTIONAL_MODE(MODE) (((MODE) == OSPI_FUNCTIONAL_MODE_INDIRECT_WRITE) || \
  189. ((MODE) == OSPI_FUNCTIONAL_MODE_INDIRECT_READ) || \
  190. ((MODE) == OSPI_FUNCTIONAL_MODE_AUTO_POLLING) || \
  191. ((MODE) == OSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  192. /* Private variables ---------------------------------------------------------*/
  193. /* Private function prototypes -----------------------------------------------*/
  194. static void OSPI_DMACplt (DMA_HandleTypeDef *hdma);
  195. static void OSPI_DMAHalfCplt (DMA_HandleTypeDef *hdma);
  196. static void OSPI_DMAError (DMA_HandleTypeDef *hdma);
  197. static void OSPI_DMAAbortCplt (DMA_HandleTypeDef *hdma);
  198. static HAL_StatusTypeDef OSPI_WaitFlagStateUntilTimeout(OSPI_HandleTypeDef *hospi, uint32_t Flag, FlagStatus State, uint32_t Tickstart, uint32_t Timeout);
  199. static HAL_StatusTypeDef OSPI_ConfigCmd (OSPI_HandleTypeDef *hospi, OSPI_RegularCmdTypeDef *cmd);
  200. static HAL_StatusTypeDef OSPIM_GetConfig (uint8_t instance_nb, OSPIM_CfgTypeDef *cfg);
  201. /**
  202. @endcond
  203. */
  204. /* Exported functions --------------------------------------------------------*/
  205. /** @defgroup OSPI_Exported_Functions OSPI Exported Functions
  206. * @{
  207. */
  208. /** @defgroup OSPI_Exported_Functions_Group1 Initialization/de-initialization functions
  209. * @brief Initialization and Configuration functions
  210. *
  211. @verbatim
  212. ===============================================================================
  213. ##### Initialization and Configuration functions #####
  214. ===============================================================================
  215. [..]
  216. This subsection provides a set of functions allowing to :
  217. (+) Initialize the OctoSPI.
  218. (+) De-initialize the OctoSPI.
  219. @endverbatim
  220. * @{
  221. */
  222. /**
  223. * @brief Initialize the OSPI mode according to the specified parameters
  224. * in the OSPI_InitTypeDef and initialize the associated handle.
  225. * @param hospi : OSPI handle
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_OSPI_Init (OSPI_HandleTypeDef *hospi)
  229. {
  230. HAL_StatusTypeDef status = HAL_OK;
  231. uint32_t tickstart = HAL_GetTick();
  232. /* Check the OSPI handle allocation */
  233. if (hospi == NULL)
  234. {
  235. status = HAL_ERROR;
  236. /* No error code can be set set as the handler is null */
  237. }
  238. else
  239. {
  240. /* Check the parameters of the initialization structure */
  241. assert_param(IS_OSPI_FIFO_THRESHOLD (hospi->Init.FifoThreshold));
  242. assert_param(IS_OSPI_DUALQUAD_MODE (hospi->Init.DualQuad));
  243. assert_param(IS_OSPI_MEMORY_TYPE (hospi->Init.MemoryType));
  244. assert_param(IS_OSPI_DEVICE_SIZE (hospi->Init.DeviceSize));
  245. assert_param(IS_OSPI_CS_HIGH_TIME (hospi->Init.ChipSelectHighTime));
  246. assert_param(IS_OSPI_FREE_RUN_CLK (hospi->Init.FreeRunningClock));
  247. assert_param(IS_OSPI_CLOCK_MODE (hospi->Init.ClockMode));
  248. assert_param(IS_OSPI_WRAP_SIZE (hospi->Init.WrapSize));
  249. assert_param(IS_OSPI_CLK_PRESCALER (hospi->Init.ClockPrescaler));
  250. assert_param(IS_OSPI_SAMPLE_SHIFTING(hospi->Init.SampleShifting));
  251. assert_param(IS_OSPI_DHQC (hospi->Init.DelayHoldQuarterCycle));
  252. assert_param(IS_OSPI_CS_BOUNDARY (hospi->Init.ChipSelectBoundary));
  253. /* Initialize error code */
  254. hospi->ErrorCode = HAL_OSPI_ERROR_NONE;
  255. /* Check if the state is the reset state */
  256. if (hospi->State == HAL_OSPI_STATE_RESET)
  257. {
  258. /* Initialization of the low level hardware */
  259. HAL_OSPI_MspInit(hospi);
  260. /* Configure the default timeout for the OSPI memory access */
  261. status = HAL_OSPI_SetTimeout(hospi, HAL_OSPI_TIMEOUT_DEFAULT_VALUE);
  262. }
  263. if (status == HAL_OK)
  264. {
  265. /* Configure memory type, device size, chip select high time, free running clock, clock mode */
  266. MODIFY_REG(hospi->Instance->DCR1, (OCTOSPI_DCR1_MTYP | OCTOSPI_DCR1_DEVSIZE | OCTOSPI_DCR1_CSHT | OCTOSPI_DCR1_FRCK | OCTOSPI_DCR1_CKMODE),
  267. (hospi->Init.MemoryType | ((hospi->Init.DeviceSize - 1) << OCTOSPI_DCR1_DEVSIZE_Pos) |
  268. ((hospi->Init.ChipSelectHighTime - 1) << OCTOSPI_DCR1_CSHT_Pos) | hospi->Init.FreeRunningClock |
  269. hospi->Init.ClockMode));
  270. /* Configure wrap size */
  271. MODIFY_REG(hospi->Instance->DCR2, OCTOSPI_DCR2_WRAPSIZE, hospi->Init.WrapSize);
  272. /* Configure chip select boundary */
  273. hospi->Instance->DCR3 = (hospi->Init.ChipSelectBoundary << OCTOSPI_DCR3_CSBOUND_Pos);
  274. /* Configure FIFO threshold */
  275. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FTHRES, ((hospi->Init.FifoThreshold - 1) << OCTOSPI_CR_FTHRES_Pos));
  276. /* Wait till busy flag is reset */
  277. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, hospi->Timeout);
  278. if (status == HAL_OK)
  279. {
  280. /* Configure clock prescaler */
  281. MODIFY_REG(hospi->Instance->DCR2, OCTOSPI_DCR2_PRESCALER, ((hospi->Init.ClockPrescaler - 1) << OCTOSPI_DCR2_PRESCALER_Pos));
  282. /* Configure Dual Quad mode */
  283. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_DQM, hospi->Init.DualQuad);
  284. /* Configure sample shifting and delay hold quarter cycle */
  285. MODIFY_REG(hospi->Instance->TCR, (OCTOSPI_TCR_SSHIFT | OCTOSPI_TCR_DHQC), (hospi->Init.SampleShifting | hospi->Init.DelayHoldQuarterCycle));
  286. /* Enable OctoSPI */
  287. __HAL_OSPI_ENABLE(hospi);
  288. /* Initialize the OSPI state */
  289. if (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS)
  290. {
  291. hospi->State = HAL_OSPI_STATE_HYPERBUS_INIT;
  292. }
  293. else
  294. {
  295. hospi->State = HAL_OSPI_STATE_READY;
  296. }
  297. }
  298. }
  299. }
  300. /* Return function status */
  301. return status;
  302. }
  303. /**
  304. * @brief Initialize the OSPI MSP.
  305. * @param hospi : OSPI handle
  306. * @retval None
  307. */
  308. __weak void HAL_OSPI_MspInit(OSPI_HandleTypeDef *hospi)
  309. {
  310. /* Prevent unused argument(s) compilation warning */
  311. UNUSED(hospi);
  312. /* NOTE : This function should not be modified, when the callback is needed,
  313. the HAL_OSPI_MspInit can be implemented in the user file
  314. */
  315. }
  316. /**
  317. * @brief De-Initialize the OSPI peripheral.
  318. * @param hospi : OSPI handle
  319. * @retval HAL status
  320. */
  321. HAL_StatusTypeDef HAL_OSPI_DeInit(OSPI_HandleTypeDef *hospi)
  322. {
  323. HAL_StatusTypeDef status = HAL_OK;
  324. /* Check the OSPI handle allocation */
  325. if (hospi == NULL)
  326. {
  327. status = HAL_ERROR;
  328. /* No error code can be set set as the handler is null */
  329. }
  330. else
  331. {
  332. /* Disable OctoSPI */
  333. __HAL_OSPI_DISABLE(hospi);
  334. /* De-initialize the low-level hardware */
  335. HAL_OSPI_MspDeInit(hospi);
  336. /* Reset the driver state */
  337. hospi->State = HAL_OSPI_STATE_RESET;
  338. }
  339. return status;
  340. }
  341. /**
  342. * @brief DeInitialize the OSPI MSP.
  343. * @param hospi : OSPI handle
  344. * @retval None
  345. */
  346. __weak void HAL_OSPI_MspDeInit(OSPI_HandleTypeDef *hospi)
  347. {
  348. /* Prevent unused argument(s) compilation warning */
  349. UNUSED(hospi);
  350. /* NOTE : This function should not be modified, when the callback is needed,
  351. the HAL_OSPI_MspDeInit can be implemented in the user file
  352. */
  353. }
  354. /**
  355. * @}
  356. */
  357. /** @defgroup OSPI_Exported_Functions_Group2 Input and Output operation functions
  358. * @brief OSPI Transmit/Receive functions
  359. *
  360. @verbatim
  361. ===============================================================================
  362. ##### IO operation functions #####
  363. ===============================================================================
  364. [..]
  365. This subsection provides a set of functions allowing to :
  366. (+) Handle the interrupts.
  367. (+) Handle the command sequence (regular and Hyperbus).
  368. (+) Handle the Hyperbus configuration.
  369. (+) Transmit data in blocking, interrupt or DMA mode.
  370. (+) Receive data in blocking, interrupt or DMA mode.
  371. (+) Manage the auto-polling functional mode.
  372. (+) Manage the memory-mapped functional mode.
  373. @endverbatim
  374. * @{
  375. */
  376. /**
  377. * @brief Handle OSPI interrupt request.
  378. * @param hospi : OSPI handle
  379. * @retval None
  380. */
  381. void HAL_OSPI_IRQHandler(OSPI_HandleTypeDef *hospi)
  382. {
  383. __IO uint32_t *data_reg = &hospi->Instance->DR;
  384. uint32_t flag = hospi->Instance->SR;
  385. uint32_t itsource = hospi->Instance->CR;
  386. uint32_t currentstate = hospi->State;
  387. /* OctoSPI fifo threshold interrupt occurred -------------------------------*/
  388. if (((flag & HAL_OSPI_FLAG_FT) != 0) && ((itsource & HAL_OSPI_IT_FT) != 0))
  389. {
  390. if (currentstate == HAL_OSPI_STATE_BUSY_TX)
  391. {
  392. /* Write a data in the fifo */
  393. *(__IO uint8_t *)((__IO void *)data_reg) = *hospi->pBuffPtr++;
  394. hospi->XferCount--;
  395. }
  396. else if (currentstate == HAL_OSPI_STATE_BUSY_RX)
  397. {
  398. /* Read a data from the fifo */
  399. *hospi->pBuffPtr++ = *(__IO uint8_t *)((__IO void *)data_reg);
  400. hospi->XferCount--;
  401. }
  402. if (hospi->XferCount == 0)
  403. {
  404. /* All data have been received or transmitted for the transfer */
  405. /* Disable fifo threshold interrupt */
  406. __HAL_OSPI_DISABLE_IT(hospi, HAL_OSPI_IT_FT);
  407. }
  408. /* Fifo threshold callback */
  409. HAL_OSPI_FifoThresholdCallback(hospi);
  410. }
  411. /* OctoSPI transfer complete interrupt occurred ----------------------------*/
  412. else if (((flag & HAL_OSPI_FLAG_TC) != 0) && ((itsource & HAL_OSPI_IT_TC) != 0))
  413. {
  414. if (currentstate == HAL_OSPI_STATE_BUSY_RX)
  415. {
  416. if (((flag & OCTOSPI_SR_FLEVEL) != 0) && (hospi->XferCount > 0))
  417. {
  418. /* Read the last data received in the fifo */
  419. *hospi->pBuffPtr++ = *(__IO uint8_t *)((__IO void*)data_reg);
  420. hospi->XferCount--;
  421. }
  422. else if(hospi->XferCount == 0)
  423. {
  424. /* Clear flag */
  425. hospi->Instance->FCR = HAL_OSPI_FLAG_TC;
  426. /* Disable the interrupts */
  427. __HAL_OSPI_DISABLE_IT(hospi, HAL_OSPI_IT_TC | HAL_OSPI_IT_FT | HAL_OSPI_IT_TE);
  428. /* Update state */
  429. hospi->State = HAL_OSPI_STATE_READY;
  430. /* RX complete callback */
  431. HAL_OSPI_RxCpltCallback(hospi);
  432. }
  433. }
  434. else
  435. {
  436. /* Clear flag */
  437. hospi->Instance->FCR = HAL_OSPI_FLAG_TC;
  438. /* Disable the interrupts */
  439. __HAL_OSPI_DISABLE_IT(hospi, HAL_OSPI_IT_TC | HAL_OSPI_IT_FT | HAL_OSPI_IT_TE);
  440. /* Update state */
  441. hospi->State = HAL_OSPI_STATE_READY;
  442. if (currentstate == HAL_OSPI_STATE_BUSY_TX)
  443. {
  444. /* TX complete callback */
  445. HAL_OSPI_TxCpltCallback(hospi);
  446. }
  447. else if (currentstate == HAL_OSPI_STATE_BUSY_CMD)
  448. {
  449. /* Command complete callback */
  450. HAL_OSPI_CmdCpltCallback(hospi);
  451. }
  452. else if (currentstate == HAL_OSPI_STATE_ABORT)
  453. {
  454. if (hospi->ErrorCode == HAL_OSPI_ERROR_NONE)
  455. {
  456. /* Abort called by the user */
  457. /* Abort complete callback */
  458. HAL_OSPI_AbortCpltCallback(hospi);
  459. }
  460. else
  461. {
  462. /* Abort due to an error (eg : DMA error) */
  463. /* Error callback */
  464. HAL_OSPI_ErrorCallback(hospi);
  465. }
  466. }
  467. }
  468. }
  469. /* OctoSPI status match interrupt occurred ---------------------------------*/
  470. else if (((flag & HAL_OSPI_FLAG_SM) != 0) && ((itsource & HAL_OSPI_IT_SM) != 0))
  471. {
  472. /* Clear flag */
  473. hospi->Instance->FCR = HAL_OSPI_FLAG_SM;
  474. /* Check if automatic poll mode stop is activated */
  475. if ((hospi->Instance->CR & OCTOSPI_CR_APMS) != 0)
  476. {
  477. /* Disable the interrupts */
  478. __HAL_OSPI_DISABLE_IT(hospi, HAL_OSPI_IT_SM | HAL_OSPI_IT_TE);
  479. /* Update state */
  480. hospi->State = HAL_OSPI_STATE_READY;
  481. }
  482. /* Status match callback */
  483. HAL_OSPI_StatusMatchCallback(hospi);
  484. }
  485. /* OctoSPI transfer error interrupt occurred -------------------------------*/
  486. else if (((flag & HAL_OSPI_FLAG_TE) != 0) && ((itsource & HAL_OSPI_IT_TE) != 0))
  487. {
  488. /* Clear flag */
  489. hospi->Instance->FCR = HAL_OSPI_FLAG_TE;
  490. /* Disable all interrupts */
  491. __HAL_OSPI_DISABLE_IT(hospi, (HAL_OSPI_IT_TO | HAL_OSPI_IT_SM | HAL_OSPI_IT_FT | HAL_OSPI_IT_TC | HAL_OSPI_IT_TE));
  492. /* Set error code */
  493. hospi->ErrorCode = HAL_OSPI_ERROR_TRANSFER;
  494. /* Check if the DMA is enabled */
  495. if ((hospi->Instance->CR & OCTOSPI_CR_DMAEN) != 0)
  496. {
  497. /* Disable the DMA transfer on the OctoSPI side */
  498. CLEAR_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  499. /* Disable the DMA transfer on the DMA side */
  500. hospi->hdma->XferAbortCallback = OSPI_DMAAbortCplt;
  501. HAL_DMA_Abort_IT(hospi->hdma);
  502. }
  503. else
  504. {
  505. /* Update state */
  506. hospi->State = HAL_OSPI_STATE_READY;
  507. /* Error callback */
  508. HAL_OSPI_ErrorCallback(hospi);
  509. }
  510. }
  511. /* OctoSPI timeout interrupt occurred --------------------------------------*/
  512. else if (((flag & HAL_OSPI_FLAG_TO) != 0) && ((itsource & HAL_OSPI_IT_TO) != 0))
  513. {
  514. /* Clear flag */
  515. hospi->Instance->FCR = HAL_OSPI_FLAG_TO;
  516. /* Timeout callback */
  517. HAL_OSPI_TimeOutCallback(hospi);
  518. }
  519. }
  520. /**
  521. * @brief Set the command configuration.
  522. * @param hospi : OSPI handle
  523. * @param cmd : structure that contains the command configuration information
  524. * @param Timeout : Timeout duration
  525. * @retval HAL status
  526. */
  527. HAL_StatusTypeDef HAL_OSPI_Command(OSPI_HandleTypeDef *hospi, OSPI_RegularCmdTypeDef *cmd, uint32_t Timeout)
  528. {
  529. HAL_StatusTypeDef status = HAL_OK;
  530. uint32_t tickstart = HAL_GetTick();
  531. /* Check the parameters of the command structure */
  532. assert_param(IS_OSPI_OPERATION_TYPE(cmd->OperationType));
  533. if (hospi->Init.DualQuad == HAL_OSPI_DUALQUAD_DISABLE)
  534. {
  535. assert_param(IS_OSPI_FLASH_ID(cmd->FlashId));
  536. }
  537. assert_param(IS_OSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  538. if (cmd->InstructionMode != HAL_OSPI_INSTRUCTION_NONE)
  539. {
  540. assert_param(IS_OSPI_INSTRUCTION_SIZE (cmd->InstructionSize));
  541. assert_param(IS_OSPI_INSTRUCTION_DTR_MODE(cmd->InstructionDtrMode));
  542. }
  543. assert_param(IS_OSPI_ADDRESS_MODE(cmd->AddressMode));
  544. if (cmd->AddressMode != HAL_OSPI_ADDRESS_NONE)
  545. {
  546. assert_param(IS_OSPI_ADDRESS_SIZE (cmd->AddressSize));
  547. assert_param(IS_OSPI_ADDRESS_DTR_MODE(cmd->AddressDtrMode));
  548. }
  549. assert_param(IS_OSPI_ALT_BYTES_MODE(cmd->AlternateBytesMode));
  550. if (cmd->AlternateBytesMode != HAL_OSPI_ALTERNATE_BYTES_NONE)
  551. {
  552. assert_param(IS_OSPI_ALT_BYTES_SIZE (cmd->AlternateBytesSize));
  553. assert_param(IS_OSPI_ALT_BYTES_DTR_MODE(cmd->AlternateBytesDtrMode));
  554. }
  555. assert_param(IS_OSPI_DATA_MODE(cmd->DataMode));
  556. if (cmd->DataMode != HAL_OSPI_DATA_NONE)
  557. {
  558. if (cmd->OperationType == HAL_OSPI_OPTYPE_COMMON_CFG)
  559. {
  560. assert_param(IS_OSPI_NUMBER_DATA (cmd->NbData));
  561. }
  562. assert_param(IS_OSPI_DATA_DTR_MODE(cmd->DataDtrMode));
  563. assert_param(IS_OSPI_DUMMY_CYCLES (cmd->DummyCycles));
  564. }
  565. assert_param(IS_OSPI_DQS_MODE (cmd->DQSMode));
  566. assert_param(IS_OSPI_SIOO_MODE(cmd->SIOOMode));
  567. /* Check the state of the driver */
  568. if (((hospi->State == HAL_OSPI_STATE_READY) && (hospi->Init.MemoryType != HAL_OSPI_MEMTYPE_HYPERBUS)) ||
  569. ((hospi->State == HAL_OSPI_STATE_READ_CMD_CFG) && (cmd->OperationType == HAL_OSPI_OPTYPE_WRITE_CFG)) ||
  570. ((hospi->State == HAL_OSPI_STATE_WRITE_CMD_CFG) && (cmd->OperationType == HAL_OSPI_OPTYPE_READ_CFG)))
  571. {
  572. /* Wait till busy flag is reset */
  573. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  574. if (status == HAL_OK)
  575. {
  576. /* Initialize error code */
  577. hospi->ErrorCode = HAL_OSPI_ERROR_NONE;
  578. /* Configure the registers */
  579. status = OSPI_ConfigCmd(hospi, cmd);
  580. if (status == HAL_OK)
  581. {
  582. if (cmd->DataMode == HAL_OSPI_DATA_NONE)
  583. {
  584. /* When there is no data phase, the transfer start as soon as the configuration is done
  585. so wait until TC flag is set to go back in idle state */
  586. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_TC, SET, tickstart, Timeout);
  587. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TC);
  588. }
  589. else
  590. {
  591. /* Update the state */
  592. if (cmd->OperationType == HAL_OSPI_OPTYPE_COMMON_CFG)
  593. {
  594. hospi->State = HAL_OSPI_STATE_CMD_CFG;
  595. }
  596. else if (cmd->OperationType == HAL_OSPI_OPTYPE_READ_CFG)
  597. {
  598. if (hospi->State == HAL_OSPI_STATE_WRITE_CMD_CFG)
  599. {
  600. hospi->State = HAL_OSPI_STATE_CMD_CFG;
  601. }
  602. else
  603. {
  604. hospi->State = HAL_OSPI_STATE_READ_CMD_CFG;
  605. }
  606. }
  607. else
  608. {
  609. if (hospi->State == HAL_OSPI_STATE_READ_CMD_CFG)
  610. {
  611. hospi->State = HAL_OSPI_STATE_CMD_CFG;
  612. }
  613. else
  614. {
  615. hospi->State = HAL_OSPI_STATE_WRITE_CMD_CFG;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. }
  622. else
  623. {
  624. status = HAL_ERROR;
  625. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  626. }
  627. /* Return function status */
  628. return status;
  629. }
  630. /**
  631. * @brief Set the command configuration in interrupt mode.
  632. * @param hospi : OSPI handle
  633. * @param cmd : structure that contains the command configuration information
  634. * @note This function is used only in Indirect Read or Write Modes
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_OSPI_Command_IT(OSPI_HandleTypeDef *hospi, OSPI_RegularCmdTypeDef *cmd)
  638. {
  639. HAL_StatusTypeDef status = HAL_OK;
  640. uint32_t tickstart = HAL_GetTick();
  641. /* Check the parameters of the command structure */
  642. assert_param(IS_OSPI_OPERATION_TYPE(cmd->OperationType));
  643. if (hospi->Init.DualQuad == HAL_OSPI_DUALQUAD_DISABLE)
  644. {
  645. assert_param(IS_OSPI_FLASH_ID(cmd->FlashId));
  646. }
  647. assert_param(IS_OSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  648. if (cmd->InstructionMode != HAL_OSPI_INSTRUCTION_NONE)
  649. {
  650. assert_param(IS_OSPI_INSTRUCTION_SIZE (cmd->InstructionSize));
  651. assert_param(IS_OSPI_INSTRUCTION_DTR_MODE(cmd->InstructionDtrMode));
  652. }
  653. assert_param(IS_OSPI_ADDRESS_MODE(cmd->AddressMode));
  654. if (cmd->AddressMode != HAL_OSPI_ADDRESS_NONE)
  655. {
  656. assert_param(IS_OSPI_ADDRESS_SIZE (cmd->AddressSize));
  657. assert_param(IS_OSPI_ADDRESS_DTR_MODE(cmd->AddressDtrMode));
  658. }
  659. assert_param(IS_OSPI_ALT_BYTES_MODE(cmd->AlternateBytesMode));
  660. if (cmd->AlternateBytesMode != HAL_OSPI_ALTERNATE_BYTES_NONE)
  661. {
  662. assert_param(IS_OSPI_ALT_BYTES_SIZE (cmd->AlternateBytesSize));
  663. assert_param(IS_OSPI_ALT_BYTES_DTR_MODE(cmd->AlternateBytesDtrMode));
  664. }
  665. assert_param(IS_OSPI_DATA_MODE(cmd->DataMode));
  666. if (cmd->DataMode != HAL_OSPI_DATA_NONE)
  667. {
  668. assert_param(IS_OSPI_NUMBER_DATA (cmd->NbData));
  669. assert_param(IS_OSPI_DATA_DTR_MODE(cmd->DataDtrMode));
  670. assert_param(IS_OSPI_DUMMY_CYCLES (cmd->DummyCycles));
  671. }
  672. assert_param(IS_OSPI_DQS_MODE (cmd->DQSMode));
  673. assert_param(IS_OSPI_SIOO_MODE(cmd->SIOOMode));
  674. /* Check the state of the driver */
  675. if ((hospi->State == HAL_OSPI_STATE_READY) && (cmd->OperationType == HAL_OSPI_OPTYPE_COMMON_CFG) &&
  676. (cmd->DataMode == HAL_OSPI_DATA_NONE) && (hospi->Init.MemoryType != HAL_OSPI_MEMTYPE_HYPERBUS))
  677. {
  678. /* Wait till busy flag is reset */
  679. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, hospi->Timeout);
  680. if (status == HAL_OK)
  681. {
  682. /* Initialize error code */
  683. hospi->ErrorCode = HAL_OSPI_ERROR_NONE;
  684. /* Clear flags related to interrupt */
  685. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TE | HAL_OSPI_FLAG_TC);
  686. /* Configure the registers */
  687. status = OSPI_ConfigCmd(hospi, cmd);
  688. if (status == HAL_OK)
  689. {
  690. /* Update the state */
  691. hospi->State = HAL_OSPI_STATE_BUSY_CMD;
  692. /* Enable the transfer complete and transfer error interrupts */
  693. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TC | HAL_OSPI_IT_TE);
  694. }
  695. }
  696. }
  697. else
  698. {
  699. status = HAL_ERROR;
  700. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  701. }
  702. /* Return function status */
  703. return status;
  704. }
  705. /**
  706. * @brief Configure the Hyperbus parameters.
  707. * @param hospi : OSPI handle
  708. * @param cfg : Structure containing the Hyperbus configuration
  709. * @param Timeout : Timeout duration
  710. * @retval HAL status
  711. */
  712. HAL_StatusTypeDef HAL_OSPI_HyperbusCfg(OSPI_HandleTypeDef *hospi, OSPI_HyperbusCfgTypeDef *cfg, uint32_t Timeout)
  713. {
  714. HAL_StatusTypeDef status = HAL_OK;
  715. uint32_t tickstart = HAL_GetTick();
  716. /* Check the parameters of the hyperbus configuration structure */
  717. assert_param(IS_OSPI_RW_RECOVERY_TIME (cfg->RWRecoveryTime));
  718. assert_param(IS_OSPI_ACCESS_TIME (cfg->AccessTime));
  719. assert_param(IS_OSPI_WRITE_ZERO_LATENCY(cfg->WriteZeroLatency));
  720. assert_param(IS_OSPI_LATENCY_MODE (cfg->LatencyMode));
  721. /* Check the state of the driver */
  722. if ((hospi->State == HAL_OSPI_STATE_HYPERBUS_INIT) || (hospi->State == HAL_OSPI_STATE_READY))
  723. {
  724. /* Wait till busy flag is reset */
  725. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  726. if (status == HAL_OK)
  727. {
  728. /* Configure Hyperbus configuration Latency register */
  729. WRITE_REG(hospi->Instance->HLCR, ((cfg->RWRecoveryTime << OCTOSPI_HLCR_TRWR_Pos) |
  730. (cfg->AccessTime << OCTOSPI_HLCR_TACC_Pos) |
  731. cfg->WriteZeroLatency | cfg->LatencyMode));
  732. /* Update the state */
  733. hospi->State = HAL_OSPI_STATE_READY;
  734. }
  735. }
  736. else
  737. {
  738. status = HAL_ERROR;
  739. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  740. }
  741. /* Return function status */
  742. return status;
  743. }
  744. /**
  745. * @brief Set the Hyperbus command configuration.
  746. * @param hospi : OSPI handle
  747. * @param cmd : Structure containing the Hyperbus command
  748. * @param Timeout : Timeout duration
  749. * @retval HAL status
  750. */
  751. HAL_StatusTypeDef HAL_OSPI_HyperbusCmd(OSPI_HandleTypeDef *hospi, OSPI_HyperbusCmdTypeDef *cmd, uint32_t Timeout)
  752. {
  753. HAL_StatusTypeDef status = HAL_OK;
  754. uint32_t tickstart = HAL_GetTick();
  755. /* Check the parameters of the hyperbus command structure */
  756. assert_param(IS_OSPI_ADDRESS_SPACE(cmd->AddressSpace));
  757. assert_param(IS_OSPI_ADDRESS_SIZE (cmd->AddressSize));
  758. assert_param(IS_OSPI_NUMBER_DATA (cmd->NbData));
  759. assert_param(IS_OSPI_DQS_MODE (cmd->DQSMode));
  760. /* Check the state of the driver */
  761. if ((hospi->State == HAL_OSPI_STATE_READY) && (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS))
  762. {
  763. /* Wait till busy flag is reset */
  764. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  765. if (status == HAL_OK)
  766. {
  767. /* Re-initialize the value of the functional mode */
  768. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, 0);
  769. /* Configure the address space in the DCR1 register */
  770. MODIFY_REG(hospi->Instance->DCR1, OCTOSPI_DCR1_MTYP_0, cmd->AddressSpace);
  771. /* Configure the CCR and WCCR registers with the address size and the following configuration :
  772. - DQS signal enabled (used as RWDS)
  773. - DTR mode enabled on address and data
  774. - address and data on 8 lines */
  775. WRITE_REG(hospi->Instance->CCR, (cmd->DQSMode | OCTOSPI_CCR_DDTR | OCTOSPI_CCR_DMODE_2 |
  776. cmd->AddressSize | OCTOSPI_CCR_ADDTR | OCTOSPI_CCR_ADMODE_2));
  777. WRITE_REG(hospi->Instance->WCCR, (cmd->DQSMode | OCTOSPI_WCCR_DDTR | OCTOSPI_WCCR_DMODE_2 |
  778. cmd->AddressSize | OCTOSPI_WCCR_ADDTR | OCTOSPI_WCCR_ADMODE_2));
  779. /* Configure the DLR register with the number of data */
  780. WRITE_REG(hospi->Instance->DLR, (cmd->NbData - 1));
  781. /* Configure the AR register with the address value */
  782. WRITE_REG(hospi->Instance->AR, cmd->Address);
  783. /* Update the state */
  784. hospi->State = HAL_OSPI_STATE_CMD_CFG;
  785. }
  786. }
  787. else
  788. {
  789. status = HAL_ERROR;
  790. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  791. }
  792. /* Return function status */
  793. return status;
  794. }
  795. /**
  796. * @brief Transmit an amount of data in blocking mode.
  797. * @param hospi : OSPI handle
  798. * @param pData : pointer to data buffer
  799. * @param Timeout : Timeout duration
  800. * @note This function is used only in Indirect Write Mode
  801. * @retval HAL status
  802. */
  803. HAL_StatusTypeDef HAL_OSPI_Transmit(OSPI_HandleTypeDef *hospi, uint8_t *pData, uint32_t Timeout)
  804. {
  805. HAL_StatusTypeDef status = HAL_OK;
  806. uint32_t tickstart = HAL_GetTick();
  807. __IO uint32_t *data_reg = &hospi->Instance->DR;
  808. /* Check the data pointer allocation */
  809. if (pData == NULL)
  810. {
  811. status = HAL_ERROR;
  812. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  813. }
  814. else
  815. {
  816. /* Check the state */
  817. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  818. {
  819. /* Configure counters and size */
  820. hospi->XferCount = READ_REG(hospi->Instance->DLR) + 1;
  821. hospi->XferSize = hospi->XferCount;
  822. hospi->pBuffPtr = pData;
  823. /* Configure CR register with functional mode as indirect write */
  824. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, OSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  825. do
  826. {
  827. /* Wait till fifo threshold flag is set to send data */
  828. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_FT, SET, tickstart, Timeout);
  829. if (status != HAL_OK)
  830. {
  831. break;
  832. }
  833. *(__IO uint8_t *)((__IO void *)data_reg) = *hospi->pBuffPtr++;
  834. hospi->XferCount--;
  835. } while (hospi->XferCount > 0);
  836. if (status == HAL_OK)
  837. {
  838. /* Wait till transfer complete flag is set to go back in idle state */
  839. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_TC, SET, tickstart, Timeout);
  840. if (status == HAL_OK)
  841. {
  842. /* Clear transfer complete flag */
  843. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TC);
  844. /* Update state */
  845. hospi->State = HAL_OSPI_STATE_READY;
  846. }
  847. }
  848. }
  849. else
  850. {
  851. status = HAL_ERROR;
  852. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  853. }
  854. }
  855. /* Return function status */
  856. return status;
  857. }
  858. /**
  859. * @brief Receive an amount of data in blocking mode.
  860. * @param hospi : OSPI handle
  861. * @param pData : pointer to data buffer
  862. * @param Timeout : Timeout duration
  863. * @note This function is used only in Indirect Read Mode
  864. * @retval HAL status
  865. */
  866. HAL_StatusTypeDef HAL_OSPI_Receive(OSPI_HandleTypeDef *hospi, uint8_t *pData, uint32_t Timeout)
  867. {
  868. HAL_StatusTypeDef status = HAL_OK;
  869. uint32_t tickstart = HAL_GetTick();
  870. __IO uint32_t *data_reg = &hospi->Instance->DR;
  871. uint32_t addr_reg = hospi->Instance->AR;
  872. uint32_t ir_reg = hospi->Instance->IR;
  873. /* Check the data pointer allocation */
  874. if (pData == NULL)
  875. {
  876. status = HAL_ERROR;
  877. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  878. }
  879. else
  880. {
  881. /* Check the state */
  882. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  883. {
  884. /* Configure counters and size */
  885. hospi->XferCount = READ_REG(hospi->Instance->DLR) + 1;
  886. hospi->XferSize = hospi->XferCount;
  887. hospi->pBuffPtr = pData;
  888. /* Configure CR register with functional mode as indirect read */
  889. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, OSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  890. /* Trig the transfer by re-writing address or instruction register */
  891. if (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS)
  892. {
  893. WRITE_REG(hospi->Instance->AR, addr_reg);
  894. }
  895. else
  896. {
  897. if (READ_BIT(hospi->Instance->CCR, OCTOSPI_CCR_ADMODE) != HAL_OSPI_ADDRESS_NONE)
  898. {
  899. WRITE_REG(hospi->Instance->AR, addr_reg);
  900. }
  901. else
  902. {
  903. WRITE_REG(hospi->Instance->IR, ir_reg);
  904. }
  905. }
  906. do
  907. {
  908. /* Wait till fifo threshold or transfer complete flags are set to read received data */
  909. status = OSPI_WaitFlagStateUntilTimeout(hospi, (HAL_OSPI_FLAG_FT | HAL_OSPI_FLAG_TC), SET, tickstart, Timeout);
  910. if (status != HAL_OK)
  911. {
  912. break;
  913. }
  914. *hospi->pBuffPtr++ = *(__IO uint8_t *)((__IO void *)data_reg);
  915. hospi->XferCount--;
  916. } while(hospi->XferCount > 0);
  917. if (status == HAL_OK)
  918. {
  919. /* Wait till transfer complete flag is set to go back in idle state */
  920. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_TC, SET, tickstart, Timeout);
  921. if (status == HAL_OK)
  922. {
  923. /* Clear transfer complete flag */
  924. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TC);
  925. /* Update state */
  926. hospi->State = HAL_OSPI_STATE_READY;
  927. }
  928. }
  929. }
  930. else
  931. {
  932. status = HAL_ERROR;
  933. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  934. }
  935. }
  936. /* Return function status */
  937. return status;
  938. }
  939. /**
  940. * @brief Send an amount of data in non-blocking mode with interrupt.
  941. * @param hospi : OSPI handle
  942. * @param pData : pointer to data buffer
  943. * @note This function is used only in Indirect Write Mode
  944. * @retval HAL status
  945. */
  946. HAL_StatusTypeDef HAL_OSPI_Transmit_IT(OSPI_HandleTypeDef *hospi, uint8_t *pData)
  947. {
  948. HAL_StatusTypeDef status = HAL_OK;
  949. /* Check the data pointer allocation */
  950. if (pData == NULL)
  951. {
  952. status = HAL_ERROR;
  953. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  954. }
  955. else
  956. {
  957. /* Check the state */
  958. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  959. {
  960. /* Configure counters and size */
  961. hospi->XferCount = READ_REG(hospi->Instance->DLR) + 1;
  962. hospi->XferSize = hospi->XferCount;
  963. hospi->pBuffPtr = pData;
  964. /* Configure CR register with functional mode as indirect write */
  965. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, OSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  966. /* Clear flags related to interrupt */
  967. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TE | HAL_OSPI_FLAG_TC);
  968. /* Update the state */
  969. hospi->State = HAL_OSPI_STATE_BUSY_TX;
  970. /* Enable the transfer complete, fifo threshold and transfer error interrupts */
  971. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TC | HAL_OSPI_IT_FT | HAL_OSPI_IT_TE);
  972. }
  973. else
  974. {
  975. status = HAL_ERROR;
  976. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  977. }
  978. }
  979. /* Return function status */
  980. return status;
  981. }
  982. /**
  983. * @brief Receive an amount of data in non-blocking mode with interrupt.
  984. * @param hospi : OSPI handle
  985. * @param pData : pointer to data buffer
  986. * @note This function is used only in Indirect Read Mode
  987. * @retval HAL status
  988. */
  989. HAL_StatusTypeDef HAL_OSPI_Receive_IT(OSPI_HandleTypeDef *hospi, uint8_t *pData)
  990. {
  991. HAL_StatusTypeDef status = HAL_OK;
  992. uint32_t addr_reg = hospi->Instance->AR;
  993. uint32_t ir_reg = hospi->Instance->IR;
  994. /* Check the data pointer allocation */
  995. if (pData == NULL)
  996. {
  997. status = HAL_ERROR;
  998. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  999. }
  1000. else
  1001. {
  1002. /* Check the state */
  1003. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  1004. {
  1005. /* Configure counters and size */
  1006. hospi->XferCount = READ_REG(hospi->Instance->DLR) + 1;
  1007. hospi->XferSize = hospi->XferCount;
  1008. hospi->pBuffPtr = pData;
  1009. /* Configure CR register with functional mode as indirect read */
  1010. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, OSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1011. /* Clear flags related to interrupt */
  1012. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TE | HAL_OSPI_FLAG_TC);
  1013. /* Update the state */
  1014. hospi->State = HAL_OSPI_STATE_BUSY_RX;
  1015. /* Enable the transfer complete, fifo threshold and transfer error interrupts */
  1016. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TC | HAL_OSPI_IT_FT | HAL_OSPI_IT_TE);
  1017. /* Trig the transfer by re-writing address or instruction register */
  1018. if (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS)
  1019. {
  1020. WRITE_REG(hospi->Instance->AR, addr_reg);
  1021. }
  1022. else
  1023. {
  1024. if (READ_BIT(hospi->Instance->CCR, OCTOSPI_CCR_ADMODE) != HAL_OSPI_ADDRESS_NONE)
  1025. {
  1026. WRITE_REG(hospi->Instance->AR, addr_reg);
  1027. }
  1028. else
  1029. {
  1030. WRITE_REG(hospi->Instance->IR, ir_reg);
  1031. }
  1032. }
  1033. }
  1034. else
  1035. {
  1036. status = HAL_ERROR;
  1037. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1038. }
  1039. }
  1040. /* Return function status */
  1041. return status;
  1042. }
  1043. /**
  1044. * @brief Send an amount of data in non-blocking mode with DMA.
  1045. * @param hospi : OSPI handle
  1046. * @param pData : pointer to data buffer
  1047. * @note This function is used only in Indirect Write Mode
  1048. * @note If DMA peripheral access is configured as halfword, the number
  1049. * of data and the fifo threshold should be aligned on halfword
  1050. * @note If DMA peripheral access is configured as word, the number
  1051. * of data and the fifo threshold should be aligned on word
  1052. * @retval HAL status
  1053. */
  1054. HAL_StatusTypeDef HAL_OSPI_Transmit_DMA(OSPI_HandleTypeDef *hospi, uint8_t *pData)
  1055. {
  1056. HAL_StatusTypeDef status = HAL_OK;
  1057. uint32_t *tmp;
  1058. uint32_t data_size = hospi->Instance->DLR + 1;
  1059. /* Check the data pointer allocation */
  1060. if (pData == NULL)
  1061. {
  1062. status = HAL_ERROR;
  1063. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1064. }
  1065. else
  1066. {
  1067. /* Check the state */
  1068. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  1069. {
  1070. /* Configure counters and size */
  1071. if (hospi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  1072. {
  1073. hospi->XferCount = data_size;
  1074. }
  1075. else if (hospi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
  1076. {
  1077. if (((data_size % 2) != 0) || ((hospi->Init.FifoThreshold % 2) != 0))
  1078. {
  1079. /* The number of data or the fifo threshold is not aligned on halfword
  1080. => no transfer possible with DMA peripheral access configured as halfword */
  1081. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1082. status = HAL_ERROR;
  1083. }
  1084. else
  1085. {
  1086. hospi->XferCount = (data_size >> 1);
  1087. }
  1088. }
  1089. else if (hospi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
  1090. {
  1091. if (((data_size % 4) != 0) || ((hospi->Init.FifoThreshold % 4) != 0))
  1092. {
  1093. /* The number of data or the fifo threshold is not aligned on word
  1094. => no transfer possible with DMA peripheral access configured as word */
  1095. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1096. status = HAL_ERROR;
  1097. }
  1098. else
  1099. {
  1100. hospi->XferCount = (data_size >> 2);
  1101. }
  1102. }
  1103. if (status == HAL_OK)
  1104. {
  1105. hospi->XferSize = hospi->XferCount;
  1106. hospi->pBuffPtr = pData;
  1107. /* Configure CR register with functional mode as indirect write */
  1108. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, OSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  1109. /* Clear flags related to interrupt */
  1110. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TE | HAL_OSPI_FLAG_TC);
  1111. /* Update the state */
  1112. hospi->State = HAL_OSPI_STATE_BUSY_TX;
  1113. /* Set the DMA transfer complete callback */
  1114. hospi->hdma->XferCpltCallback = OSPI_DMACplt;
  1115. /* Set the DMA Half transfer complete callback */
  1116. hospi->hdma->XferHalfCpltCallback = OSPI_DMAHalfCplt;
  1117. /* Set the DMA error callback */
  1118. hospi->hdma->XferErrorCallback = OSPI_DMAError;
  1119. /* Clear the DMA abort callback */
  1120. hospi->hdma->XferAbortCallback = NULL;
  1121. /* Configure the direction of the DMA */
  1122. hospi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
  1123. MODIFY_REG(hospi->hdma->Instance->CCR, DMA_CCR_DIR, hospi->hdma->Init.Direction);
  1124. /* Enable the transmit DMA Channel */
  1125. tmp = (uint32_t*)((void*)&pData);
  1126. HAL_DMA_Start_IT(hospi->hdma, *(uint32_t*)tmp, (uint32_t)&hospi->Instance->DR, hospi->XferSize);
  1127. /* Enable the transfer error interrupt */
  1128. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TE);
  1129. /* Enable the DMA transfer by setting the DMAEN bit */
  1130. SET_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  1131. }
  1132. }
  1133. else
  1134. {
  1135. status = HAL_ERROR;
  1136. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1137. }
  1138. }
  1139. /* Return function status */
  1140. return status;
  1141. }
  1142. /**
  1143. * @brief Receive an amount of data in non-blocking mode with DMA.
  1144. * @param hospi : OSPI handle
  1145. * @param pData : pointer to data buffer.
  1146. * @note This function is used only in Indirect Read Mode
  1147. * @note If DMA peripheral access is configured as halfword, the number
  1148. * of data and the fifo threshold should be aligned on halfword
  1149. * @note If DMA peripheral access is configured as word, the number
  1150. * of data and the fifo threshold should be aligned on word
  1151. * @retval HAL status
  1152. */
  1153. HAL_StatusTypeDef HAL_OSPI_Receive_DMA(OSPI_HandleTypeDef *hospi, uint8_t *pData)
  1154. {
  1155. HAL_StatusTypeDef status = HAL_OK;
  1156. uint32_t *tmp;
  1157. uint32_t data_size = hospi->Instance->DLR + 1;
  1158. uint32_t addr_reg = hospi->Instance->AR;
  1159. uint32_t ir_reg = hospi->Instance->IR;
  1160. /* Check the data pointer allocation */
  1161. if (pData == NULL)
  1162. {
  1163. status = HAL_ERROR;
  1164. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1165. }
  1166. else
  1167. {
  1168. /* Check the state */
  1169. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  1170. {
  1171. /* Configure counters and size */
  1172. if (hospi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  1173. {
  1174. hospi->XferCount = data_size;
  1175. }
  1176. else if (hospi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
  1177. {
  1178. if (((data_size % 2) != 0) || ((hospi->Init.FifoThreshold % 2) != 0))
  1179. {
  1180. /* The number of data or the fifo threshold is not aligned on halfword
  1181. => no transfer possible with DMA peripheral access configured as halfword */
  1182. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1183. status = HAL_ERROR;
  1184. }
  1185. else
  1186. {
  1187. hospi->XferCount = (data_size >> 1);
  1188. }
  1189. }
  1190. else if (hospi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
  1191. {
  1192. if (((data_size % 4) != 0) || ((hospi->Init.FifoThreshold % 4) != 0))
  1193. {
  1194. /* The number of data or the fifo threshold is not aligned on word
  1195. => no transfer possible with DMA peripheral access configured as word */
  1196. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1197. status = HAL_ERROR;
  1198. }
  1199. else
  1200. {
  1201. hospi->XferCount = (data_size >> 2);
  1202. }
  1203. }
  1204. if (status == HAL_OK)
  1205. {
  1206. hospi->XferSize = hospi->XferCount;
  1207. hospi->pBuffPtr = pData;
  1208. /* Configure CR register with functional mode as indirect read */
  1209. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, OSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1210. /* Clear flags related to interrupt */
  1211. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TE | HAL_OSPI_FLAG_TC);
  1212. /* Update the state */
  1213. hospi->State = HAL_OSPI_STATE_BUSY_RX;
  1214. /* Set the DMA transfer complete callback */
  1215. hospi->hdma->XferCpltCallback = OSPI_DMACplt;
  1216. /* Set the DMA Half transfer complete callback */
  1217. hospi->hdma->XferHalfCpltCallback = OSPI_DMAHalfCplt;
  1218. /* Set the DMA error callback */
  1219. hospi->hdma->XferErrorCallback = OSPI_DMAError;
  1220. /* Clear the DMA abort callback */
  1221. hospi->hdma->XferAbortCallback = NULL;
  1222. /* Configure the direction of the DMA */
  1223. hospi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
  1224. MODIFY_REG(hospi->hdma->Instance->CCR, DMA_CCR_DIR, hospi->hdma->Init.Direction);
  1225. /* Enable the transmit DMA Channel */
  1226. tmp = (uint32_t*)((void *)&pData);
  1227. HAL_DMA_Start_IT(hospi->hdma, (uint32_t)&hospi->Instance->DR, *(uint32_t*)tmp, hospi->XferSize);
  1228. /* Enable the transfer error interrupt */
  1229. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TE);
  1230. /* Trig the transfer by re-writing address or instruction register */
  1231. if (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS)
  1232. {
  1233. WRITE_REG(hospi->Instance->AR, addr_reg);
  1234. }
  1235. else
  1236. {
  1237. if (READ_BIT(hospi->Instance->CCR, OCTOSPI_CCR_ADMODE) != HAL_OSPI_ADDRESS_NONE)
  1238. {
  1239. WRITE_REG(hospi->Instance->AR, addr_reg);
  1240. }
  1241. else
  1242. {
  1243. WRITE_REG(hospi->Instance->IR, ir_reg);
  1244. }
  1245. }
  1246. /* Enable the DMA transfer by setting the DMAEN bit */
  1247. SET_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  1248. }
  1249. }
  1250. else
  1251. {
  1252. status = HAL_ERROR;
  1253. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1254. }
  1255. }
  1256. /* Return function status */
  1257. return status;
  1258. }
  1259. /**
  1260. * @brief Configure the OSPI Automatic Polling Mode in blocking mode.
  1261. * @param hospi : OSPI handle
  1262. * @param cfg : structure that contains the polling configuration information.
  1263. * @param Timeout : Timeout duration
  1264. * @note This function is used only in Automatic Polling Mode
  1265. * @note This function should not be used when the memory is in octal mode (see Errata Sheet)
  1266. * @retval HAL status
  1267. */
  1268. HAL_StatusTypeDef HAL_OSPI_AutoPolling(OSPI_HandleTypeDef *hospi, OSPI_AutoPollingTypeDef *cfg, uint32_t Timeout)
  1269. {
  1270. HAL_StatusTypeDef status = HAL_OK;
  1271. uint32_t tickstart = HAL_GetTick();
  1272. uint32_t addr_reg = hospi->Instance->AR;
  1273. uint32_t ir_reg = hospi->Instance->IR;
  1274. /* Check the parameters of the autopolling configuration structure */
  1275. assert_param(IS_OSPI_MATCH_MODE (cfg->MatchMode));
  1276. assert_param(IS_OSPI_AUTOMATIC_STOP (cfg->AutomaticStop));
  1277. assert_param(IS_OSPI_INTERVAL (cfg->Interval));
  1278. assert_param(IS_OSPI_STATUS_BYTES_SIZE(hospi->Instance->DLR+1));
  1279. /* Check the state */
  1280. if ((hospi->State == HAL_OSPI_STATE_CMD_CFG) && (cfg->AutomaticStop == HAL_OSPI_AUTOMATIC_STOP_ENABLE))
  1281. {
  1282. /* Wait till busy flag is reset */
  1283. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  1284. if (status == HAL_OK)
  1285. {
  1286. /* Configure registers */
  1287. WRITE_REG (hospi->Instance->PSMAR, cfg->Match);
  1288. WRITE_REG (hospi->Instance->PSMKR, cfg->Mask);
  1289. WRITE_REG (hospi->Instance->PIR, cfg->Interval);
  1290. MODIFY_REG(hospi->Instance->CR, (OCTOSPI_CR_PMM | OCTOSPI_CR_APMS | OCTOSPI_CR_FMODE),
  1291. (cfg->MatchMode | cfg->AutomaticStop | OSPI_FUNCTIONAL_MODE_AUTO_POLLING));
  1292. /* Trig the transfer by re-writing address or instruction register */
  1293. if (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS)
  1294. {
  1295. WRITE_REG(hospi->Instance->AR, addr_reg);
  1296. }
  1297. else
  1298. {
  1299. if (READ_BIT(hospi->Instance->CCR, OCTOSPI_CCR_ADMODE) != HAL_OSPI_ADDRESS_NONE)
  1300. {
  1301. WRITE_REG(hospi->Instance->AR, addr_reg);
  1302. }
  1303. else
  1304. {
  1305. WRITE_REG(hospi->Instance->IR, ir_reg);
  1306. }
  1307. }
  1308. /* Wait till status match flag is set to go back in idle state */
  1309. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_SM, SET, tickstart, Timeout);
  1310. if (status == HAL_OK)
  1311. {
  1312. /* Clear status match flag */
  1313. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_SM);
  1314. /* Update state */
  1315. hospi->State = HAL_OSPI_STATE_READY;
  1316. }
  1317. }
  1318. }
  1319. else
  1320. {
  1321. status = HAL_ERROR;
  1322. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1323. }
  1324. /* Return function status */
  1325. return status;
  1326. }
  1327. /**
  1328. * @brief Configure the OSPI Automatic Polling Mode in non-blocking mode.
  1329. * @param hospi : OSPI handle
  1330. * @param cfg : structure that contains the polling configuration information.
  1331. * @note This function is used only in Automatic Polling Mode
  1332. * @note This function should not be used when the memory is in octal mode (see Errata Sheet)
  1333. * @retval HAL status
  1334. */
  1335. HAL_StatusTypeDef HAL_OSPI_AutoPolling_IT(OSPI_HandleTypeDef *hospi, OSPI_AutoPollingTypeDef *cfg)
  1336. {
  1337. HAL_StatusTypeDef status = HAL_OK;
  1338. uint32_t tickstart = HAL_GetTick();
  1339. uint32_t addr_reg = hospi->Instance->AR;
  1340. uint32_t ir_reg = hospi->Instance->IR;
  1341. /* Check the parameters of the autopolling configuration structure */
  1342. assert_param(IS_OSPI_MATCH_MODE (cfg->MatchMode));
  1343. assert_param(IS_OSPI_AUTOMATIC_STOP (cfg->AutomaticStop));
  1344. assert_param(IS_OSPI_INTERVAL (cfg->Interval));
  1345. assert_param(IS_OSPI_STATUS_BYTES_SIZE(hospi->Instance->DLR+1));
  1346. /* Check the state */
  1347. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  1348. {
  1349. /* Wait till busy flag is reset */
  1350. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, hospi->Timeout);
  1351. if (status == HAL_OK)
  1352. {
  1353. /* Configure registers */
  1354. WRITE_REG (hospi->Instance->PSMAR, cfg->Match);
  1355. WRITE_REG (hospi->Instance->PSMKR, cfg->Mask);
  1356. WRITE_REG (hospi->Instance->PIR, cfg->Interval);
  1357. MODIFY_REG(hospi->Instance->CR, (OCTOSPI_CR_PMM | OCTOSPI_CR_APMS | OCTOSPI_CR_FMODE),
  1358. (cfg->MatchMode | cfg->AutomaticStop | OSPI_FUNCTIONAL_MODE_AUTO_POLLING));
  1359. /* Clear flags related to interrupt */
  1360. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TE | HAL_OSPI_FLAG_SM);
  1361. /* Update state */
  1362. hospi->State = HAL_OSPI_STATE_BUSY_AUTO_POLLING;
  1363. /* Enable the status match and transfer error interrupts */
  1364. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_SM | HAL_OSPI_IT_TE);
  1365. /* Trig the transfer by re-writing address or instruction register */
  1366. if (hospi->Init.MemoryType == HAL_OSPI_MEMTYPE_HYPERBUS)
  1367. {
  1368. WRITE_REG(hospi->Instance->AR, addr_reg);
  1369. }
  1370. else
  1371. {
  1372. if (READ_BIT(hospi->Instance->CCR, OCTOSPI_CCR_ADMODE) != HAL_OSPI_ADDRESS_NONE)
  1373. {
  1374. WRITE_REG(hospi->Instance->AR, addr_reg);
  1375. }
  1376. else
  1377. {
  1378. WRITE_REG(hospi->Instance->IR, ir_reg);
  1379. }
  1380. }
  1381. }
  1382. }
  1383. else
  1384. {
  1385. status = HAL_ERROR;
  1386. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1387. }
  1388. /* Return function status */
  1389. return status;
  1390. }
  1391. /**
  1392. * @brief Configure the Memory Mapped mode.
  1393. * @param hospi : OSPI handle
  1394. * @param cfg : structure that contains the memory mapped configuration information.
  1395. * @note This function is used only in Memory mapped Mode
  1396. * @retval HAL status
  1397. */
  1398. HAL_StatusTypeDef HAL_OSPI_MemoryMapped(OSPI_HandleTypeDef *hospi, OSPI_MemoryMappedTypeDef *cfg)
  1399. {
  1400. HAL_StatusTypeDef status = HAL_OK;
  1401. uint32_t tickstart = HAL_GetTick();
  1402. /* Check the parameters of the memory-mapped configuration structure */
  1403. assert_param(IS_OSPI_TIMEOUT_ACTIVATION(cfg->TimeOutActivation));
  1404. /* Check the state */
  1405. if (hospi->State == HAL_OSPI_STATE_CMD_CFG)
  1406. {
  1407. /* Wait till busy flag is reset */
  1408. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, hospi->Timeout);
  1409. if (status == HAL_OK)
  1410. {
  1411. /* Update state */
  1412. hospi->State = HAL_OSPI_STATE_BUSY_MEM_MAPPED;
  1413. if (cfg->TimeOutActivation == HAL_OSPI_TIMEOUT_COUNTER_ENABLE)
  1414. {
  1415. assert_param(IS_OSPI_TIMEOUT_PERIOD(cfg->TimeOutPeriod));
  1416. /* Configure register */
  1417. WRITE_REG(hospi->Instance->LPTR, cfg->TimeOutPeriod);
  1418. /* Clear flags related to interrupt */
  1419. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TO);
  1420. /* Enable the timeout interrupt */
  1421. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TO);
  1422. }
  1423. /* Configure CR register with functional mode as memory-mapped */
  1424. MODIFY_REG(hospi->Instance->CR, (OCTOSPI_CR_TCEN | OCTOSPI_CR_FMODE),
  1425. (cfg->TimeOutActivation | OSPI_FUNCTIONAL_MODE_MEMORY_MAPPED));
  1426. }
  1427. }
  1428. else
  1429. {
  1430. status = HAL_ERROR;
  1431. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1432. }
  1433. /* Return function status */
  1434. return status;
  1435. }
  1436. /**
  1437. * @brief Transfer Error callback.
  1438. * @param hospi : OSPI handle
  1439. * @retval None
  1440. */
  1441. __weak void HAL_OSPI_ErrorCallback(OSPI_HandleTypeDef *hospi)
  1442. {
  1443. /* Prevent unused argument(s) compilation warning */
  1444. UNUSED(hospi);
  1445. /* NOTE : This function should not be modified, when the callback is needed,
  1446. the HAL_OSPI_ErrorCallback could be implemented in the user file
  1447. */
  1448. }
  1449. /**
  1450. * @brief Abort completed callback.
  1451. * @param hospi : OSPI handle
  1452. * @retval None
  1453. */
  1454. __weak void HAL_OSPI_AbortCpltCallback(OSPI_HandleTypeDef *hospi)
  1455. {
  1456. /* Prevent unused argument(s) compilation warning */
  1457. UNUSED(hospi);
  1458. /* NOTE: This function should not be modified, when the callback is needed,
  1459. the HAL_OSPI_AbortCpltCallback could be implemented in the user file
  1460. */
  1461. }
  1462. /**
  1463. * @brief FIFO Threshold callback.
  1464. * @param hospi : OSPI handle
  1465. * @retval None
  1466. */
  1467. __weak void HAL_OSPI_FifoThresholdCallback(OSPI_HandleTypeDef *hospi)
  1468. {
  1469. /* Prevent unused argument(s) compilation warning */
  1470. UNUSED(hospi);
  1471. /* NOTE : This function should not be modified, when the callback is needed,
  1472. the HAL_OSPI_FIFOThresholdCallback could be implemented in the user file
  1473. */
  1474. }
  1475. /**
  1476. * @brief Command completed callback.
  1477. * @param hospi : OSPI handle
  1478. * @retval None
  1479. */
  1480. __weak void HAL_OSPI_CmdCpltCallback(OSPI_HandleTypeDef *hospi)
  1481. {
  1482. /* Prevent unused argument(s) compilation warning */
  1483. UNUSED(hospi);
  1484. /* NOTE: This function should not be modified, when the callback is needed,
  1485. the HAL_OSPI_CmdCpltCallback could be implemented in the user file
  1486. */
  1487. }
  1488. /**
  1489. * @brief Rx Transfer completed callback.
  1490. * @param hospi : OSPI handle
  1491. * @retval None
  1492. */
  1493. __weak void HAL_OSPI_RxCpltCallback(OSPI_HandleTypeDef *hospi)
  1494. {
  1495. /* Prevent unused argument(s) compilation warning */
  1496. UNUSED(hospi);
  1497. /* NOTE: This function should not be modified, when the callback is needed,
  1498. the HAL_OSPI_RxCpltCallback could be implemented in the user file
  1499. */
  1500. }
  1501. /**
  1502. * @brief Tx Transfer completed callback.
  1503. * @param hospi : OSPI handle
  1504. * @retval None
  1505. */
  1506. __weak void HAL_OSPI_TxCpltCallback(OSPI_HandleTypeDef *hospi)
  1507. {
  1508. /* Prevent unused argument(s) compilation warning */
  1509. UNUSED(hospi);
  1510. /* NOTE: This function should not be modified, when the callback is needed,
  1511. the HAL_OSPI_TxCpltCallback could be implemented in the user file
  1512. */
  1513. }
  1514. /**
  1515. * @brief Rx Half Transfer completed callback.
  1516. * @param hospi : OSPI handle
  1517. * @retval None
  1518. */
  1519. __weak void HAL_OSPI_RxHalfCpltCallback(OSPI_HandleTypeDef *hospi)
  1520. {
  1521. /* Prevent unused argument(s) compilation warning */
  1522. UNUSED(hospi);
  1523. /* NOTE: This function should not be modified, when the callback is needed,
  1524. the HAL_OSPI_RxHalfCpltCallback could be implemented in the user file
  1525. */
  1526. }
  1527. /**
  1528. * @brief Tx Half Transfer completed callback.
  1529. * @param hospi : OSPI handle
  1530. * @retval None
  1531. */
  1532. __weak void HAL_OSPI_TxHalfCpltCallback(OSPI_HandleTypeDef *hospi)
  1533. {
  1534. /* Prevent unused argument(s) compilation warning */
  1535. UNUSED(hospi);
  1536. /* NOTE: This function should not be modified, when the callback is needed,
  1537. the HAL_OSPI_TxHalfCpltCallback could be implemented in the user file
  1538. */
  1539. }
  1540. /**
  1541. * @brief Status Match callback.
  1542. * @param hospi : OSPI handle
  1543. * @retval None
  1544. */
  1545. __weak void HAL_OSPI_StatusMatchCallback(OSPI_HandleTypeDef *hospi)
  1546. {
  1547. /* Prevent unused argument(s) compilation warning */
  1548. UNUSED(hospi);
  1549. /* NOTE : This function should not be modified, when the callback is needed,
  1550. the HAL_OSPI_StatusMatchCallback could be implemented in the user file
  1551. */
  1552. }
  1553. /**
  1554. * @brief Timeout callback.
  1555. * @param hospi : OSPI handle
  1556. * @retval None
  1557. */
  1558. __weak void HAL_OSPI_TimeOutCallback(OSPI_HandleTypeDef *hospi)
  1559. {
  1560. /* Prevent unused argument(s) compilation warning */
  1561. UNUSED(hospi);
  1562. /* NOTE : This function should not be modified, when the callback is needed,
  1563. the HAL_OSPI_TimeOutCallback could be implemented in the user file
  1564. */
  1565. }
  1566. /**
  1567. * @}
  1568. */
  1569. /** @defgroup OSPI_Exported_Functions_Group3 Peripheral Control and State functions
  1570. * @brief OSPI control and State functions
  1571. *
  1572. @verbatim
  1573. ===============================================================================
  1574. ##### Peripheral Control and State functions #####
  1575. ===============================================================================
  1576. [..]
  1577. This subsection provides a set of functions allowing to :
  1578. (+) Check in run-time the state of the driver.
  1579. (+) Check the error code set during last operation.
  1580. (+) Abort any operation.
  1581. (+) Manage the Fifo threshold.
  1582. (+) Configure the timeout duration used in the driver.
  1583. @endverbatim
  1584. * @{
  1585. */
  1586. /**
  1587. * @brief Abort the current transmission.
  1588. * @param hospi : OSPI handle
  1589. * @retval HAL status
  1590. */
  1591. HAL_StatusTypeDef HAL_OSPI_Abort(OSPI_HandleTypeDef *hospi)
  1592. {
  1593. HAL_StatusTypeDef status = HAL_OK;
  1594. uint32_t tickstart = HAL_GetTick();
  1595. /* Check if the state is in one of the busy or configured states */
  1596. if (((hospi->State & OSPI_BUSY_STATE_MASK) != 0) || ((hospi->State & OSPI_CFG_STATE_MASK) != 0))
  1597. {
  1598. /* Check if the DMA is enabled */
  1599. if ((hospi->Instance->CR & OCTOSPI_CR_DMAEN) != 0)
  1600. {
  1601. /* Disable the DMA transfer on the OctoSPI side */
  1602. CLEAR_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  1603. /* Disable the DMA transfer on the DMA side */
  1604. status = HAL_DMA_Abort(hospi->hdma);
  1605. if (status != HAL_OK)
  1606. {
  1607. hospi->ErrorCode = HAL_OSPI_ERROR_DMA;
  1608. }
  1609. }
  1610. /* Perform an abort of the OctoSPI */
  1611. SET_BIT(hospi->Instance->CR, OCTOSPI_CR_ABORT);
  1612. /* Wait until the transfer complete flag is set to go back in idle state */
  1613. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_TC, SET, tickstart, hospi->Timeout);
  1614. if (status == HAL_OK)
  1615. {
  1616. /* Clear transfer complete flag */
  1617. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TC);
  1618. /* Wait until the busy flag is reset to go back in idle state */
  1619. status = OSPI_WaitFlagStateUntilTimeout(hospi, HAL_OSPI_FLAG_BUSY, RESET, tickstart, hospi->Timeout);
  1620. if (status == HAL_OK)
  1621. {
  1622. /* Update state */
  1623. hospi->State = HAL_OSPI_STATE_READY;
  1624. }
  1625. }
  1626. }
  1627. else
  1628. {
  1629. status = HAL_ERROR;
  1630. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1631. }
  1632. /* Return function status */
  1633. return status;
  1634. }
  1635. /**
  1636. * @brief Abort the current transmission (non-blocking function)
  1637. * @param hospi : OSPI handle
  1638. * @retval HAL status
  1639. */
  1640. HAL_StatusTypeDef HAL_OSPI_Abort_IT(OSPI_HandleTypeDef *hospi)
  1641. {
  1642. HAL_StatusTypeDef status = HAL_OK;
  1643. /* Check if the state is in one of the busy or configured states */
  1644. if (((hospi->State & OSPI_BUSY_STATE_MASK) != 0) || ((hospi->State & OSPI_CFG_STATE_MASK) != 0))
  1645. {
  1646. /* Disable all interrupts */
  1647. __HAL_OSPI_DISABLE_IT(hospi, (HAL_OSPI_IT_TO | HAL_OSPI_IT_SM | HAL_OSPI_IT_FT | HAL_OSPI_IT_TC | HAL_OSPI_IT_TE));
  1648. /* Update state */
  1649. hospi->State = HAL_OSPI_STATE_ABORT;
  1650. /* Check if the DMA is enabled */
  1651. if ((hospi->Instance->CR & OCTOSPI_CR_DMAEN) != 0)
  1652. {
  1653. /* Disable the DMA transfer on the OctoSPI side */
  1654. CLEAR_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  1655. /* Disable the DMA transfer on the DMA side */
  1656. hospi->hdma->XferAbortCallback = OSPI_DMAAbortCplt;
  1657. HAL_DMA_Abort_IT(hospi->hdma);
  1658. }
  1659. else
  1660. {
  1661. /* Clear transfer complete flag */
  1662. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TC);
  1663. /* Enable the transfer complete interrupts */
  1664. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TC);
  1665. /* Perform an abort of the OctoSPI */
  1666. SET_BIT(hospi->Instance->CR, OCTOSPI_CR_ABORT);
  1667. }
  1668. }
  1669. else
  1670. {
  1671. status = HAL_ERROR;
  1672. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1673. }
  1674. /* Return function status */
  1675. return status;
  1676. }
  1677. /** @brief Set OSPI Fifo threshold.
  1678. * @param hospi : OSPI handle.
  1679. * @param Threshold : Threshold of the Fifo.
  1680. * @retval HAL status
  1681. */
  1682. HAL_StatusTypeDef HAL_OSPI_SetFifoThreshold(OSPI_HandleTypeDef *hospi, uint32_t Threshold)
  1683. {
  1684. HAL_StatusTypeDef status = HAL_OK;
  1685. /* Check the state */
  1686. if ((hospi->State & OSPI_BUSY_STATE_MASK) == 0)
  1687. {
  1688. /* Synchronize initialization structure with the new fifo threshold value */
  1689. hospi->Init.FifoThreshold = Threshold;
  1690. /* Configure new fifo threshold */
  1691. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FTHRES, ((hospi->Init.FifoThreshold-1) << OCTOSPI_CR_FTHRES_Pos));
  1692. }
  1693. else
  1694. {
  1695. status = HAL_ERROR;
  1696. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_SEQUENCE;
  1697. }
  1698. /* Return function status */
  1699. return status;
  1700. }
  1701. /** @brief Get OSPI Fifo threshold.
  1702. * @param hospi : OSPI handle.
  1703. * @retval Fifo threshold
  1704. */
  1705. uint32_t HAL_OSPI_GetFifoThreshold(OSPI_HandleTypeDef *hospi)
  1706. {
  1707. return ((READ_BIT(hospi->Instance->CR, OCTOSPI_CR_FTHRES) >> OCTOSPI_CR_FTHRES_Pos) + 1);
  1708. }
  1709. /** @brief Set OSPI timeout.
  1710. * @param hospi : OSPI handle.
  1711. * @param Timeout : Timeout for the memory access.
  1712. * @retval None
  1713. */
  1714. HAL_StatusTypeDef HAL_OSPI_SetTimeout(OSPI_HandleTypeDef *hospi, uint32_t Timeout)
  1715. {
  1716. hospi->Timeout = Timeout;
  1717. return HAL_OK;
  1718. }
  1719. /**
  1720. * @brief Return the OSPI error code.
  1721. * @param hospi : OSPI handle
  1722. * @retval OSPI Error Code
  1723. */
  1724. uint32_t HAL_OSPI_GetError(OSPI_HandleTypeDef *hospi)
  1725. {
  1726. return hospi->ErrorCode;
  1727. }
  1728. /**
  1729. * @brief Return the OSPI handle state.
  1730. * @param hospi : OSPI handle
  1731. * @retval HAL state
  1732. */
  1733. uint32_t HAL_OSPI_GetState(OSPI_HandleTypeDef *hospi)
  1734. {
  1735. /* Return OSPI handle state */
  1736. return hospi->State;
  1737. }
  1738. /**
  1739. * @}
  1740. */
  1741. /** @defgroup OSPI_Exported_Functions_Group4 IO Manager configuration function
  1742. * @brief OSPI IO Manager configuration function
  1743. *
  1744. @verbatim
  1745. ===============================================================================
  1746. ##### IO Manager configuration function #####
  1747. ===============================================================================
  1748. [..]
  1749. This subsection provides a set of functions allowing to :
  1750. (+) Configure the IO manager.
  1751. @endverbatim
  1752. * @{
  1753. */
  1754. /**
  1755. * @brief Configure the OctoSPI IO manager.
  1756. * @param hospi : OSPI handle
  1757. * @param cfg : Configuration of the IO Manager for the instance
  1758. * @param Timeout : Timeout duration
  1759. * @retval HAL status
  1760. */
  1761. HAL_StatusTypeDef HAL_OSPIM_Config(OSPI_HandleTypeDef *hospi, OSPIM_CfgTypeDef *cfg, uint32_t Timeout)
  1762. {
  1763. HAL_StatusTypeDef status = HAL_OK;
  1764. uint32_t instance = 0;
  1765. uint8_t index = 0, ospi_enabled = 0, other_instance = 0;
  1766. OSPIM_CfgTypeDef IOM_cfg[OSPI_NB_INSTANCE];
  1767. /* Check the parameters of the OctoSPI IO Manager configuration structure */
  1768. assert_param(IS_OSPIM_PORT(cfg->ClkPort));
  1769. assert_param(IS_OSPIM_PORT(cfg->DQSPort));
  1770. assert_param(IS_OSPIM_PORT(cfg->NCSPort));
  1771. assert_param(IS_OSPIM_IO_PORT(cfg->IOLowPort));
  1772. assert_param(IS_OSPIM_IO_PORT(cfg->IOHighPort));
  1773. if (hospi->Instance == OCTOSPI1)
  1774. {
  1775. instance = 0;
  1776. other_instance = 1;
  1777. }
  1778. else
  1779. {
  1780. instance = 1;
  1781. other_instance = 0;
  1782. }
  1783. /**************** Get current configuration of the instances ****************/
  1784. for (index = 0; index < OSPI_NB_INSTANCE; index++)
  1785. {
  1786. if (OSPIM_GetConfig(index+1, &(IOM_cfg[index])) != HAL_OK)
  1787. {
  1788. status = HAL_ERROR;
  1789. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  1790. }
  1791. }
  1792. if (status == HAL_OK)
  1793. {
  1794. /********** Disable both OctoSPI to configure OctoSPI IO Manager **********/
  1795. if ((OCTOSPI1->CR & OCTOSPI_CR_EN) != 0)
  1796. {
  1797. CLEAR_BIT(OCTOSPI1->CR, OCTOSPI_CR_EN);
  1798. ospi_enabled |= 0x1;
  1799. }
  1800. if ((OCTOSPI2->CR & OCTOSPI_CR_EN) != 0)
  1801. {
  1802. CLEAR_BIT(OCTOSPI2->CR, OCTOSPI_CR_EN);
  1803. ospi_enabled |= 0x2;
  1804. }
  1805. /***************** Deactivation of previous configuration *****************/
  1806. if (IOM_cfg[instance].ClkPort != 0)
  1807. {
  1808. CLEAR_BIT(OCTOSPIM->PCR[(IOM_cfg[instance].ClkPort-1)], OCTOSPIM_PCR_CLKEN);
  1809. CLEAR_BIT(OCTOSPIM->PCR[(IOM_cfg[instance].DQSPort-1)], OCTOSPIM_PCR_DQSEN);
  1810. CLEAR_BIT(OCTOSPIM->PCR[(IOM_cfg[instance].NCSPort-1)], OCTOSPIM_PCR_NCSEN);
  1811. CLEAR_BIT(OCTOSPIM->PCR[((IOM_cfg[instance].IOLowPort&0xF)-1)], OCTOSPIM_PCR_IOLEN);
  1812. CLEAR_BIT(OCTOSPIM->PCR[((IOM_cfg[instance].IOHighPort&0xF)-1)], OCTOSPIM_PCR_IOHEN);
  1813. }
  1814. /********************* Deactivation of other instance *********************/
  1815. if ((cfg->ClkPort == IOM_cfg[other_instance].ClkPort) || (cfg->DQSPort == IOM_cfg[other_instance].DQSPort) ||
  1816. (cfg->NCSPort == IOM_cfg[other_instance].NCSPort) || (cfg->IOLowPort == IOM_cfg[other_instance].IOLowPort) ||
  1817. (cfg->IOHighPort == IOM_cfg[other_instance].IOHighPort))
  1818. {
  1819. CLEAR_BIT(OCTOSPIM->PCR[(IOM_cfg[other_instance].ClkPort-1)], OCTOSPIM_PCR_CLKEN);
  1820. CLEAR_BIT(OCTOSPIM->PCR[(IOM_cfg[other_instance].DQSPort-1)], OCTOSPIM_PCR_DQSEN);
  1821. CLEAR_BIT(OCTOSPIM->PCR[(IOM_cfg[other_instance].NCSPort-1)], OCTOSPIM_PCR_NCSEN);
  1822. CLEAR_BIT(OCTOSPIM->PCR[((IOM_cfg[other_instance].IOLowPort&0xF)-1)], OCTOSPIM_PCR_IOLEN);
  1823. CLEAR_BIT(OCTOSPIM->PCR[((IOM_cfg[other_instance].IOHighPort&0xF)-1)], OCTOSPIM_PCR_IOHEN);
  1824. }
  1825. /******************** Activation of new configuration *********************/
  1826. MODIFY_REG(OCTOSPIM->PCR[(cfg->ClkPort-1)], (OCTOSPIM_PCR_CLKEN | OCTOSPIM_PCR_CLKSRC), (OCTOSPIM_PCR_CLKEN | (instance << OCTOSPIM_PCR_CLKSRC_Pos)));
  1827. MODIFY_REG(OCTOSPIM->PCR[(cfg->DQSPort-1)], (OCTOSPIM_PCR_DQSEN | OCTOSPIM_PCR_DQSSRC), (OCTOSPIM_PCR_DQSEN | (instance << OCTOSPIM_PCR_DQSSRC_Pos)));
  1828. MODIFY_REG(OCTOSPIM->PCR[(cfg->NCSPort-1)], (OCTOSPIM_PCR_NCSEN | OCTOSPIM_PCR_NCSSRC), (OCTOSPIM_PCR_NCSEN | (instance << OCTOSPIM_PCR_NCSSRC_Pos)));
  1829. if ((cfg->IOLowPort & OCTOSPIM_PCR_IOLEN) != 0)
  1830. {
  1831. MODIFY_REG(OCTOSPIM->PCR[((cfg->IOLowPort&0xF)-1)], (OCTOSPIM_PCR_IOLEN | OCTOSPIM_PCR_IOLSRC),
  1832. (OCTOSPIM_PCR_IOLEN | (instance << POSITION_VAL(OCTOSPIM_PCR_IOLSRC_1))));
  1833. }
  1834. else
  1835. {
  1836. MODIFY_REG(OCTOSPIM->PCR[((cfg->IOLowPort&0xF)-1)], (OCTOSPIM_PCR_IOHEN | OCTOSPIM_PCR_IOHSRC),
  1837. (OCTOSPIM_PCR_IOHEN | (instance << POSITION_VAL(OCTOSPIM_PCR_IOHSRC_1))));
  1838. }
  1839. if ((cfg->IOHighPort & OCTOSPIM_PCR_IOLEN) != 0)
  1840. {
  1841. MODIFY_REG(OCTOSPIM->PCR[((cfg->IOHighPort&0xF)-1)], (OCTOSPIM_PCR_IOLEN | OCTOSPIM_PCR_IOLSRC),
  1842. (OCTOSPIM_PCR_IOLEN | OCTOSPIM_PCR_IOLSRC_0 | (instance << POSITION_VAL(OCTOSPIM_PCR_IOLSRC_1))));
  1843. }
  1844. else
  1845. {
  1846. MODIFY_REG(OCTOSPIM->PCR[((cfg->IOHighPort&0xF)-1)], (OCTOSPIM_PCR_IOHEN | OCTOSPIM_PCR_IOHSRC),
  1847. (OCTOSPIM_PCR_IOHEN | OCTOSPIM_PCR_IOHSRC_0 | (instance << POSITION_VAL(OCTOSPIM_PCR_IOHSRC_1))));
  1848. }
  1849. /******* Re-enable both OctoSPI after configure OctoSPI IO Manager ********/
  1850. if ((ospi_enabled & 0x1) != 0)
  1851. {
  1852. SET_BIT(OCTOSPI1->CR, OCTOSPI_CR_EN);
  1853. }
  1854. if ((ospi_enabled & 0x2) != 0)
  1855. {
  1856. SET_BIT(OCTOSPI2->CR, OCTOSPI_CR_EN);
  1857. }
  1858. }
  1859. /* Return function status */
  1860. return status;
  1861. }
  1862. /**
  1863. * @}
  1864. */
  1865. /**
  1866. @cond 0
  1867. */
  1868. /**
  1869. * @brief DMA OSPI process complete callback.
  1870. * @param hdma : DMA handle
  1871. * @retval None
  1872. */
  1873. static void OSPI_DMACplt(DMA_HandleTypeDef *hdma)
  1874. {
  1875. OSPI_HandleTypeDef* hospi = ( OSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1876. hospi->XferCount = 0;
  1877. /* Disable the DMA transfer on the OctoSPI side */
  1878. CLEAR_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  1879. /* Disable the DMA channel */
  1880. __HAL_DMA_DISABLE(hdma);
  1881. /* Enable the OSPI transfer complete Interrupt */
  1882. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TC);
  1883. }
  1884. /**
  1885. * @brief DMA OSPI process half complete callback.
  1886. * @param hdma : DMA handle
  1887. * @retval None
  1888. */
  1889. static void OSPI_DMAHalfCplt(DMA_HandleTypeDef *hdma)
  1890. {
  1891. OSPI_HandleTypeDef* hospi = ( OSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1892. hospi->XferCount = (hospi->XferCount >> 1);
  1893. if (hospi->State == HAL_OSPI_STATE_BUSY_RX)
  1894. {
  1895. HAL_OSPI_RxHalfCpltCallback(hospi);
  1896. }
  1897. else
  1898. {
  1899. HAL_OSPI_TxHalfCpltCallback(hospi);
  1900. }
  1901. }
  1902. /**
  1903. * @brief DMA OSPI communication error callback.
  1904. * @param hdma : DMA handle
  1905. * @retval None
  1906. */
  1907. static void OSPI_DMAError(DMA_HandleTypeDef *hdma)
  1908. {
  1909. OSPI_HandleTypeDef* hospi = ( OSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1910. hospi->XferCount = 0;
  1911. hospi->ErrorCode = HAL_OSPI_ERROR_DMA;
  1912. /* Disable the DMA transfer on the OctoSPI side */
  1913. CLEAR_BIT(hospi->Instance->CR, OCTOSPI_CR_DMAEN);
  1914. /* Abort the OctoSPI */
  1915. HAL_OSPI_Abort_IT(hospi);
  1916. }
  1917. /**
  1918. * @brief DMA OSPI abort complete callback.
  1919. * @param hdma : DMA handle
  1920. * @retval None
  1921. */
  1922. static void OSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma)
  1923. {
  1924. OSPI_HandleTypeDef* hospi = ( OSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1925. hospi->XferCount = 0;
  1926. /* Check the state */
  1927. if (hospi->State == HAL_OSPI_STATE_ABORT)
  1928. {
  1929. /* DMA abort called by OctoSPI abort */
  1930. /* Clear transfer complete flag */
  1931. __HAL_OSPI_CLEAR_FLAG(hospi, HAL_OSPI_FLAG_TC);
  1932. /* Enable the transfer complete interrupts */
  1933. __HAL_OSPI_ENABLE_IT(hospi, HAL_OSPI_IT_TC);
  1934. /* Perform an abort of the OctoSPI */
  1935. SET_BIT(hospi->Instance->CR, OCTOSPI_CR_ABORT);
  1936. }
  1937. else
  1938. {
  1939. /* DMA abort called due to a transfer error interrupt */
  1940. /* Update state */
  1941. hospi->State = HAL_OSPI_STATE_READY;
  1942. /* Error callback */
  1943. HAL_OSPI_ErrorCallback(hospi);
  1944. }
  1945. }
  1946. /**
  1947. * @brief Wait for a flag state until timeout.
  1948. * @param hospi : OSPI handle
  1949. * @param Flag : Flag checked
  1950. * @param State : Value of the flag expected
  1951. * @param Timeout : Duration of the timeout
  1952. * @param Tickstart : Tick start value
  1953. * @retval HAL status
  1954. */
  1955. static HAL_StatusTypeDef OSPI_WaitFlagStateUntilTimeout(OSPI_HandleTypeDef *hospi, uint32_t Flag,
  1956. FlagStatus State, uint32_t Tickstart, uint32_t Timeout)
  1957. {
  1958. /* Wait until flag is in expected state */
  1959. while((__HAL_OSPI_GET_FLAG(hospi, Flag)) != State)
  1960. {
  1961. /* Check for the Timeout */
  1962. if (Timeout != HAL_MAX_DELAY)
  1963. {
  1964. if((Timeout == 0) || ((HAL_GetTick() - Tickstart) > Timeout))
  1965. {
  1966. hospi->State = HAL_OSPI_STATE_ERROR;
  1967. hospi->ErrorCode |= HAL_OSPI_ERROR_TIMEOUT;
  1968. return HAL_ERROR;
  1969. }
  1970. }
  1971. }
  1972. return HAL_OK;
  1973. }
  1974. /**
  1975. * @brief Configure the registers for the regular command mode.
  1976. * @param hospi : OSPI handle
  1977. * @param cmd : structure that contains the command configuration information
  1978. * @retval HAL status
  1979. */
  1980. static HAL_StatusTypeDef OSPI_ConfigCmd(OSPI_HandleTypeDef *hospi, OSPI_RegularCmdTypeDef *cmd)
  1981. {
  1982. HAL_StatusTypeDef status = HAL_OK;
  1983. __IO uint32_t *ccr_reg, *tcr_reg, *ir_reg, *abr_reg;
  1984. /* Re-initialize the value of the functional mode */
  1985. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FMODE, 0);
  1986. /* Configure the flash ID */
  1987. if (hospi->Init.DualQuad == HAL_OSPI_DUALQUAD_DISABLE)
  1988. {
  1989. MODIFY_REG(hospi->Instance->CR, OCTOSPI_CR_FSEL, cmd->FlashId);
  1990. }
  1991. if (cmd->OperationType != HAL_OSPI_OPTYPE_WRITE_CFG)
  1992. {
  1993. ccr_reg = &(hospi->Instance->CCR);
  1994. tcr_reg = &(hospi->Instance->TCR);
  1995. ir_reg = &(hospi->Instance->IR);
  1996. abr_reg = &(hospi->Instance->ABR);
  1997. }
  1998. else
  1999. {
  2000. ccr_reg = &(hospi->Instance->WCCR);
  2001. tcr_reg = &(hospi->Instance->WTCR);
  2002. ir_reg = &(hospi->Instance->WIR);
  2003. abr_reg = &(hospi->Instance->WABR);
  2004. }
  2005. /* Configure the CCR register with DQS and SIOO modes */
  2006. *ccr_reg = (cmd->DQSMode | cmd->SIOOMode);
  2007. if (cmd->AlternateBytesMode != HAL_OSPI_ALTERNATE_BYTES_NONE)
  2008. {
  2009. /* Configure the ABR register with alternate bytes value */
  2010. *abr_reg = cmd->AlternateBytes;
  2011. /* Configure the CCR register with alternate bytes communication parameters */
  2012. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_ABMODE | OCTOSPI_CCR_ABDTR | OCTOSPI_CCR_ABSIZE),
  2013. (cmd->AlternateBytesMode | cmd->AlternateBytesDtrMode | cmd->AlternateBytesSize));
  2014. }
  2015. /* Configure the TCR register with the number of dummy cycles */
  2016. MODIFY_REG((*tcr_reg), OCTOSPI_TCR_DCYC, cmd->DummyCycles);
  2017. if (cmd->DataMode != HAL_OSPI_DATA_NONE)
  2018. {
  2019. if (cmd->OperationType == HAL_OSPI_OPTYPE_COMMON_CFG)
  2020. {
  2021. /* Configure the DLR register with the number of data */
  2022. hospi->Instance->DLR = (cmd->NbData - 1);
  2023. }
  2024. }
  2025. if (cmd->InstructionMode != HAL_OSPI_INSTRUCTION_NONE)
  2026. {
  2027. if (cmd->AddressMode != HAL_OSPI_ADDRESS_NONE)
  2028. {
  2029. if (cmd->DataMode != HAL_OSPI_DATA_NONE)
  2030. {
  2031. /* ---- Command with instruction, address and data ---- */
  2032. /* Configure the CCR register with all communication parameters */
  2033. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_IMODE | OCTOSPI_CCR_IDTR | OCTOSPI_CCR_ISIZE |
  2034. OCTOSPI_CCR_ADMODE | OCTOSPI_CCR_ADDTR | OCTOSPI_CCR_ADSIZE |
  2035. OCTOSPI_CCR_DMODE | OCTOSPI_CCR_DDTR),
  2036. (cmd->InstructionMode | cmd->InstructionDtrMode | cmd->InstructionSize |
  2037. cmd->AddressMode | cmd->AddressDtrMode | cmd->AddressSize |
  2038. cmd->DataMode | cmd->DataDtrMode));
  2039. }
  2040. else
  2041. {
  2042. /* ---- Command with instruction and address ---- */
  2043. /* Configure the CCR register with all communication parameters */
  2044. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_IMODE | OCTOSPI_CCR_IDTR | OCTOSPI_CCR_ISIZE |
  2045. OCTOSPI_CCR_ADMODE | OCTOSPI_CCR_ADDTR | OCTOSPI_CCR_ADSIZE),
  2046. (cmd->InstructionMode | cmd->InstructionDtrMode | cmd->InstructionSize |
  2047. cmd->AddressMode | cmd->AddressDtrMode | cmd->AddressSize));
  2048. /* The DHQC bit is linked with DDTR bit which should be activated */
  2049. if ((hospi->Init.DelayHoldQuarterCycle == HAL_OSPI_DHQC_ENABLE) &&
  2050. (cmd->InstructionDtrMode == HAL_OSPI_INSTRUCTION_DTR_ENABLE))
  2051. {
  2052. MODIFY_REG((*ccr_reg), OCTOSPI_CCR_DDTR, HAL_OSPI_DATA_DTR_ENABLE);
  2053. }
  2054. }
  2055. /* Configure the IR register with the instruction value */
  2056. *ir_reg = cmd->Instruction;
  2057. /* Configure the AR register with the address value */
  2058. hospi->Instance->AR = cmd->Address;
  2059. }
  2060. else
  2061. {
  2062. if (cmd->DataMode != HAL_OSPI_DATA_NONE)
  2063. {
  2064. /* ---- Command with instruction and data ---- */
  2065. /* Configure the CCR register with all communication parameters */
  2066. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_IMODE | OCTOSPI_CCR_IDTR | OCTOSPI_CCR_ISIZE |
  2067. OCTOSPI_CCR_DMODE | OCTOSPI_CCR_DDTR),
  2068. (cmd->InstructionMode | cmd->InstructionDtrMode | cmd->InstructionSize |
  2069. cmd->DataMode | cmd->DataDtrMode));
  2070. }
  2071. else
  2072. {
  2073. /* ---- Command with only instruction ---- */
  2074. /* Configure the CCR register with all communication parameters */
  2075. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_IMODE | OCTOSPI_CCR_IDTR | OCTOSPI_CCR_ISIZE),
  2076. (cmd->InstructionMode | cmd->InstructionDtrMode | cmd->InstructionSize));
  2077. /* The DHQC bit is linked with DDTR bit which should be activated */
  2078. if ((hospi->Init.DelayHoldQuarterCycle == HAL_OSPI_DHQC_ENABLE) &&
  2079. (cmd->InstructionDtrMode == HAL_OSPI_INSTRUCTION_DTR_ENABLE))
  2080. {
  2081. MODIFY_REG((*ccr_reg), OCTOSPI_CCR_DDTR, HAL_OSPI_DATA_DTR_ENABLE);
  2082. }
  2083. }
  2084. /* Configure the IR register with the instruction value */
  2085. *ir_reg = cmd->Instruction;
  2086. }
  2087. }
  2088. else
  2089. {
  2090. if (cmd->AddressMode != HAL_OSPI_ADDRESS_NONE)
  2091. {
  2092. if (cmd->DataMode != HAL_OSPI_DATA_NONE)
  2093. {
  2094. /* ---- Command with address and data ---- */
  2095. /* Configure the CCR register with all communication parameters */
  2096. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_ADMODE | OCTOSPI_CCR_ADDTR | OCTOSPI_CCR_ADSIZE |
  2097. OCTOSPI_CCR_DMODE | OCTOSPI_CCR_DDTR),
  2098. (cmd->AddressMode | cmd->AddressDtrMode | cmd->AddressSize |
  2099. cmd->DataMode | cmd->DataDtrMode));
  2100. }
  2101. else
  2102. {
  2103. /* ---- Command with only address ---- */
  2104. /* Configure the CCR register with all communication parameters */
  2105. MODIFY_REG((*ccr_reg), (OCTOSPI_CCR_ADMODE | OCTOSPI_CCR_ADDTR | OCTOSPI_CCR_ADSIZE),
  2106. (cmd->AddressMode | cmd->AddressDtrMode | cmd->AddressSize));
  2107. }
  2108. /* Configure the AR register with the instruction value */
  2109. hospi->Instance->AR = cmd->Address;
  2110. }
  2111. else
  2112. {
  2113. /* ---- Invalid command configuration (no instruction, no address) ---- */
  2114. status = HAL_ERROR;
  2115. hospi->ErrorCode = HAL_OSPI_ERROR_INVALID_PARAM;
  2116. }
  2117. }
  2118. /* Return function status */
  2119. return status;
  2120. }
  2121. /**
  2122. * @brief Get the current IOM configuration for an OctoSPI instance.
  2123. * @param instance_nb : number of the instance
  2124. * @param cfg : configuration of the IO Manager for the instance
  2125. * @retval HAL status
  2126. */
  2127. static HAL_StatusTypeDef OSPIM_GetConfig(uint8_t instance_nb, OSPIM_CfgTypeDef *cfg)
  2128. {
  2129. HAL_StatusTypeDef status = HAL_OK;
  2130. uint32_t reg = 0, value = 0;
  2131. uint32_t index = 0;
  2132. if ((instance_nb == 0) || (instance_nb > OSPI_NB_INSTANCE) || (cfg == NULL))
  2133. {
  2134. /* Invalid parameter -> error returned */
  2135. status = HAL_ERROR;
  2136. }
  2137. else
  2138. {
  2139. /* Initialize the structure */
  2140. cfg->ClkPort = cfg->DQSPort = cfg->NCSPort = cfg->IOLowPort = cfg->IOHighPort = 0;
  2141. if (instance_nb == 1)
  2142. {
  2143. value = 0;
  2144. }
  2145. else if (instance_nb == 2)
  2146. {
  2147. value = (OCTOSPIM_PCR_CLKSRC | OCTOSPIM_PCR_DQSSRC | OCTOSPIM_PCR_NCSSRC | OCTOSPIM_PCR_IOLSRC_1 | OCTOSPIM_PCR_IOHSRC_1);
  2148. }
  2149. /* Get the information about the instance */
  2150. for (index = 0; index < OSPI_IOM_NB_PORTS; index ++)
  2151. {
  2152. reg = OCTOSPIM->PCR[index];
  2153. if ((reg & OCTOSPIM_PCR_CLKEN) != 0)
  2154. {
  2155. /* The clock is enabled on this port */
  2156. if ((reg & OCTOSPIM_PCR_CLKSRC) == (value & OCTOSPIM_PCR_CLKSRC))
  2157. {
  2158. /* The clock correspond to the instance passed as parameter */
  2159. cfg->ClkPort = index+1;
  2160. }
  2161. }
  2162. if ((reg & OCTOSPIM_PCR_DQSEN) != 0)
  2163. {
  2164. /* The DQS is enabled on this port */
  2165. if ((reg & OCTOSPIM_PCR_DQSSRC) == (value & OCTOSPIM_PCR_DQSSRC))
  2166. {
  2167. /* The DQS correspond to the instance passed as parameter */
  2168. cfg->DQSPort = index+1;
  2169. }
  2170. }
  2171. if ((reg & OCTOSPIM_PCR_NCSEN) != 0)
  2172. {
  2173. /* The nCS is enabled on this port */
  2174. if ((reg & OCTOSPIM_PCR_NCSSRC) == (value & OCTOSPIM_PCR_NCSSRC))
  2175. {
  2176. /* The nCS correspond to the instance passed as parameter */
  2177. cfg->NCSPort = index+1;
  2178. }
  2179. }
  2180. if ((reg & OCTOSPIM_PCR_IOLEN) != 0)
  2181. {
  2182. /* The IO Low is enabled on this port */
  2183. if ((reg & OCTOSPIM_PCR_IOLSRC_1) == (value & OCTOSPIM_PCR_IOLSRC_1))
  2184. {
  2185. /* The IO Low correspond to the instance passed as parameter */
  2186. if ((reg & OCTOSPIM_PCR_IOLSRC_0) == 0)
  2187. {
  2188. cfg->IOLowPort = (OCTOSPIM_PCR_IOLEN | (index+1));
  2189. }
  2190. else
  2191. {
  2192. cfg->IOLowPort = (OCTOSPIM_PCR_IOHEN | (index+1));
  2193. }
  2194. }
  2195. }
  2196. if ((reg & OCTOSPIM_PCR_IOHEN) != 0)
  2197. {
  2198. /* The IO High is enabled on this port */
  2199. if ((reg & OCTOSPIM_PCR_IOHSRC_1) == (value & OCTOSPIM_PCR_IOHSRC_1))
  2200. {
  2201. /* The IO High correspond to the instance passed as parameter */
  2202. if ((reg & OCTOSPIM_PCR_IOHSRC_0) == 0)
  2203. {
  2204. cfg->IOHighPort = (OCTOSPIM_PCR_IOLEN | (index+1));
  2205. }
  2206. else
  2207. {
  2208. cfg->IOHighPort = (OCTOSPIM_PCR_IOHEN | (index+1));
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. /* Return function status */
  2215. return status;
  2216. }
  2217. /**
  2218. @endcond
  2219. */
  2220. /**
  2221. * @}
  2222. */
  2223. #endif /* HAL_OSPI_MODULE_ENABLED */
  2224. /**
  2225. * @}
  2226. */
  2227. /**
  2228. * @}
  2229. */
  2230. #endif /* OCTOSPI || OCTOSPI1 || OCTOSPI2 */
  2231. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/