STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_sdram.c
Go to the documentation of this file.
1
114/* Includes ------------------------------------------------------------------*/
115#include "stm32f4xx_hal.h"
116
117#if defined(FMC_Bank5_6)
118
123#ifdef HAL_SDRAM_MODULE_ENABLED
124
130/* Private typedef -----------------------------------------------------------*/
131/* Private define ------------------------------------------------------------*/
132/* Private macro -------------------------------------------------------------*/
133/* Private variables ---------------------------------------------------------*/
134/* Private function prototypes -----------------------------------------------*/
138static void SDRAM_DMACplt(DMA_HandleTypeDef *hdma);
139static void SDRAM_DMACpltProt(DMA_HandleTypeDef *hdma);
140static void SDRAM_DMAError(DMA_HandleTypeDef *hdma);
145/* Exported functions --------------------------------------------------------*/
172HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)
173{
174 /* Check the SDRAM handle parameter */
175 if (hsdram == NULL)
176 {
177 return HAL_ERROR;
178 }
179
180 if (hsdram->State == HAL_SDRAM_STATE_RESET)
181 {
182 /* Allocate lock resource and initialize it */
183 hsdram->Lock = HAL_UNLOCKED;
184#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
185 if (hsdram->MspInitCallback == NULL)
186 {
187 hsdram->MspInitCallback = HAL_SDRAM_MspInit;
188 }
189 hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
190 hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
191 hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
192
193 /* Init the low level hardware */
194 hsdram->MspInitCallback(hsdram);
195#else
196 /* Initialize the low level hardware (MSP) */
197 HAL_SDRAM_MspInit(hsdram);
198#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
199 }
200
201 /* Initialize the SDRAM controller state */
202 hsdram->State = HAL_SDRAM_STATE_BUSY;
203
204 /* Initialize SDRAM control Interface */
205 (void)FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));
206
207 /* Initialize SDRAM timing Interface */
208 (void)FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank);
209 /* Update the SDRAM controller state */
210 hsdram->State = HAL_SDRAM_STATE_READY;
211
212 return HAL_OK;
213}
214
221HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)
222{
223#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
224 if (hsdram->MspDeInitCallback == NULL)
225 {
226 hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
227 }
228
229 /* DeInit the low level hardware */
230 hsdram->MspDeInitCallback(hsdram);
231#else
232 /* Initialize the low level hardware (MSP) */
233 HAL_SDRAM_MspDeInit(hsdram);
234#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
235
236 /* Configure the SDRAM registers with their reset values */
237 (void)FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);
238
239 /* Reset the SDRAM controller state */
240 hsdram->State = HAL_SDRAM_STATE_RESET;
241
242 /* Release Lock */
243 __HAL_UNLOCK(hsdram);
244
245 return HAL_OK;
246}
247
254__weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
255{
256 /* Prevent unused argument(s) compilation warning */
257 UNUSED(hsdram);
258
259 /* NOTE: This function Should not be modified, when the callback is needed,
260 the HAL_SDRAM_MspInit could be implemented in the user file
261 */
262}
263
270__weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
271{
272 /* Prevent unused argument(s) compilation warning */
273 UNUSED(hsdram);
274
275 /* NOTE: This function Should not be modified, when the callback is needed,
276 the HAL_SDRAM_MspDeInit could be implemented in the user file
277 */
278}
279
286void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)
287{
288 /* Check SDRAM interrupt Rising edge flag */
289 if (__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))
290 {
291 /* SDRAM refresh error interrupt callback */
292#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
293 hsdram->RefreshErrorCallback(hsdram);
294#else
295 HAL_SDRAM_RefreshErrorCallback(hsdram);
296#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
297
298 /* Clear SDRAM refresh error interrupt pending bit */
299 __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);
300 }
301}
302
309__weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)
310{
311 /* Prevent unused argument(s) compilation warning */
312 UNUSED(hsdram);
313
314 /* NOTE: This function Should not be modified, when the callback is needed,
315 the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
316 */
317}
318
325__weak void HAL_SDRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
326{
327 /* Prevent unused argument(s) compilation warning */
328 UNUSED(hdma);
329
330 /* NOTE: This function Should not be modified, when the callback is needed,
331 the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
332 */
333}
334
340__weak void HAL_SDRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
341{
342 /* Prevent unused argument(s) compilation warning */
343 UNUSED(hdma);
344
345 /* NOTE: This function Should not be modified, when the callback is needed,
346 the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
347 */
348}
349
377HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer,
378 uint32_t BufferSize)
379{
380 uint32_t size;
381 __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
382 uint8_t *pdestbuff = pDstBuffer;
383 HAL_SDRAM_StateTypeDef state = hsdram->State;
384
385 /* Check the SDRAM controller state */
386 if (state == HAL_SDRAM_STATE_BUSY)
387 {
388 return HAL_BUSY;
389 }
390 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
391 {
392 /* Process Locked */
393 __HAL_LOCK(hsdram);
394
395 /* Update the SDRAM controller state */
396 hsdram->State = HAL_SDRAM_STATE_BUSY;
397
398 /* Read data from source */
399 for (size = BufferSize; size != 0U; size--)
400 {
401 *pdestbuff = *(__IO uint8_t *)pSdramAddress;
402 pdestbuff++;
403 pSdramAddress++;
404 }
405
406 /* Update the SDRAM controller state */
407 hsdram->State = state;
408
409 /* Process Unlocked */
410 __HAL_UNLOCK(hsdram);
411 }
412 else
413 {
414 return HAL_ERROR;
415 }
416
417 return HAL_OK;
418}
419
429HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer,
430 uint32_t BufferSize)
431{
432 uint32_t size;
433 __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
434 uint8_t *psrcbuff = pSrcBuffer;
435
436 /* Check the SDRAM controller state */
437 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
438 {
439 return HAL_BUSY;
440 }
441 else if (hsdram->State == HAL_SDRAM_STATE_READY)
442 {
443 /* Process Locked */
444 __HAL_LOCK(hsdram);
445
446 /* Update the SDRAM controller state */
447 hsdram->State = HAL_SDRAM_STATE_BUSY;
448
449 /* Write data to memory */
450 for (size = BufferSize; size != 0U; size--)
451 {
452 *(__IO uint8_t *)pSdramAddress = *psrcbuff;
453 psrcbuff++;
454 pSdramAddress++;
455 }
456
457 /* Update the SDRAM controller state */
458 hsdram->State = HAL_SDRAM_STATE_READY;
459
460 /* Process Unlocked */
461 __HAL_UNLOCK(hsdram);
462 }
463 else
464 {
465 return HAL_ERROR;
466 }
467
468 return HAL_OK;
469}
470
480HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer,
481 uint32_t BufferSize)
482{
483 uint32_t size;
484 __IO uint32_t *pSdramAddress = pAddress;
485 uint16_t *pdestbuff = pDstBuffer;
486 HAL_SDRAM_StateTypeDef state = hsdram->State;
487
488 /* Check the SDRAM controller state */
489 if (state == HAL_SDRAM_STATE_BUSY)
490 {
491 return HAL_BUSY;
492 }
493 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
494 {
495 /* Process Locked */
496 __HAL_LOCK(hsdram);
497
498 /* Update the SDRAM controller state */
499 hsdram->State = HAL_SDRAM_STATE_BUSY;
500
501 /* Read data from memory */
502 for (size = BufferSize; size >= 2U ; size -= 2U)
503 {
504 *pdestbuff = (uint16_t)((*pSdramAddress) & 0x0000FFFFU);
505 pdestbuff++;
506 *pdestbuff = (uint16_t)(((*pSdramAddress) & 0xFFFF0000U) >> 16U);
507 pdestbuff++;
508 pSdramAddress++;
509 }
510
511 /* Read last 16-bits if size is not 32-bits multiple */
512 if ((BufferSize % 2U) != 0U)
513 {
514 *pdestbuff = (uint16_t)((*pSdramAddress) & 0x0000FFFFU);
515 }
516
517 /* Update the SDRAM controller state */
518 hsdram->State = state;
519
520 /* Process Unlocked */
521 __HAL_UNLOCK(hsdram);
522 }
523 else
524 {
525 return HAL_ERROR;
526 }
527
528 return HAL_OK;
529}
530
540HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer,
541 uint32_t BufferSize)
542{
543 uint32_t size;
544 __IO uint32_t *psdramaddress = pAddress;
545 uint16_t *psrcbuff = pSrcBuffer;
546
547 /* Check the SDRAM controller state */
548 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
549 {
550 return HAL_BUSY;
551 }
552 else if (hsdram->State == HAL_SDRAM_STATE_READY)
553 {
554 /* Process Locked */
555 __HAL_LOCK(hsdram);
556
557 /* Update the SDRAM controller state */
558 hsdram->State = HAL_SDRAM_STATE_BUSY;
559
560 /* Write data to memory */
561 for (size = BufferSize; size >= 2U ; size -= 2U)
562 {
563 *psdramaddress = (uint32_t)(*psrcbuff);
564 psrcbuff++;
565 *psdramaddress |= ((uint32_t)(*psrcbuff) << 16U);
566 psrcbuff++;
567 psdramaddress++;
568 }
569
570 /* Write last 16-bits if size is not 32-bits multiple */
571 if ((BufferSize % 2U) != 0U)
572 {
573 *psdramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psdramaddress) & 0xFFFF0000U);
574 }
575
576 /* Update the SDRAM controller state */
577 hsdram->State = HAL_SDRAM_STATE_READY;
578
579 /* Process Unlocked */
580 __HAL_UNLOCK(hsdram);
581 }
582 else
583 {
584 return HAL_ERROR;
585 }
586
587 return HAL_OK;
588}
589
599HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer,
600 uint32_t BufferSize)
601{
602 uint32_t size;
603 __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
604 uint32_t *pdestbuff = pDstBuffer;
605 HAL_SDRAM_StateTypeDef state = hsdram->State;
606
607 /* Check the SDRAM controller state */
608 if (state == HAL_SDRAM_STATE_BUSY)
609 {
610 return HAL_BUSY;
611 }
612 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
613 {
614 /* Process Locked */
615 __HAL_LOCK(hsdram);
616
617 /* Update the SDRAM controller state */
618 hsdram->State = HAL_SDRAM_STATE_BUSY;
619
620 /* Read data from source */
621 for (size = BufferSize; size != 0U; size--)
622 {
623 *pdestbuff = *(__IO uint32_t *)pSdramAddress;
624 pdestbuff++;
625 pSdramAddress++;
626 }
627
628 /* Update the SDRAM controller state */
629 hsdram->State = state;
630
631 /* Process Unlocked */
632 __HAL_UNLOCK(hsdram);
633 }
634 else
635 {
636 return HAL_ERROR;
637 }
638
639 return HAL_OK;
640}
641
651HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer,
652 uint32_t BufferSize)
653{
654 uint32_t size;
655 __IO uint32_t *pSdramAddress = pAddress;
656 uint32_t *psrcbuff = pSrcBuffer;
657
658 /* Check the SDRAM controller state */
659 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
660 {
661 return HAL_BUSY;
662 }
663 else if (hsdram->State == HAL_SDRAM_STATE_READY)
664 {
665 /* Process Locked */
666 __HAL_LOCK(hsdram);
667
668 /* Update the SDRAM controller state */
669 hsdram->State = HAL_SDRAM_STATE_BUSY;
670
671 /* Write data to memory */
672 for (size = BufferSize; size != 0U; size--)
673 {
674 *pSdramAddress = *psrcbuff;
675 psrcbuff++;
676 pSdramAddress++;
677 }
678
679 /* Update the SDRAM controller state */
680 hsdram->State = HAL_SDRAM_STATE_READY;
681
682 /* Process Unlocked */
683 __HAL_UNLOCK(hsdram);
684 }
685 else
686 {
687 return HAL_ERROR;
688 }
689
690 return HAL_OK;
691}
692
702HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer,
703 uint32_t BufferSize)
704{
705 HAL_StatusTypeDef status;
706 HAL_SDRAM_StateTypeDef state = hsdram->State;
707
708 /* Check the SDRAM controller state */
709 if (state == HAL_SDRAM_STATE_BUSY)
710 {
711 status = HAL_BUSY;
712 }
713 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
714 {
715 /* Process Locked */
716 __HAL_LOCK(hsdram);
717
718 /* Update the SDRAM controller state */
719 hsdram->State = HAL_SDRAM_STATE_BUSY;
720
721 /* Configure DMA user callbacks */
722 if (state == HAL_SDRAM_STATE_READY)
723 {
724 hsdram->hdma->XferCpltCallback = SDRAM_DMACplt;
725 }
726 else
727 {
728 hsdram->hdma->XferCpltCallback = SDRAM_DMACpltProt;
729 }
730 hsdram->hdma->XferErrorCallback = SDRAM_DMAError;
731
732 /* Enable the DMA Stream */
733 status = HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
734
735 /* Process Unlocked */
736 __HAL_UNLOCK(hsdram);
737 }
738 else
739 {
740 status = HAL_ERROR;
741 }
742
743 return status;
744}
745
755HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer,
756 uint32_t BufferSize)
757{
758 HAL_StatusTypeDef status;
759
760 /* Check the SDRAM controller state */
761 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
762 {
763 status = HAL_BUSY;
764 }
765 else if (hsdram->State == HAL_SDRAM_STATE_READY)
766 {
767 /* Process Locked */
768 __HAL_LOCK(hsdram);
769
770 /* Update the SDRAM controller state */
771 hsdram->State = HAL_SDRAM_STATE_BUSY;
772
773 /* Configure DMA user callbacks */
774 hsdram->hdma->XferCpltCallback = SDRAM_DMACplt;
775 hsdram->hdma->XferErrorCallback = SDRAM_DMAError;
776
777 /* Enable the DMA Stream */
778 status = HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
779
780 /* Process Unlocked */
781 __HAL_UNLOCK(hsdram);
782 }
783 else
784 {
785 status = HAL_ERROR;
786 }
787
788 return status;
789}
790
791#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
804HAL_StatusTypeDef HAL_SDRAM_RegisterCallback(SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId,
805 pSDRAM_CallbackTypeDef pCallback)
806{
807 HAL_StatusTypeDef status = HAL_OK;
808 HAL_SDRAM_StateTypeDef state;
809
810 if (pCallback == NULL)
811 {
812 return HAL_ERROR;
813 }
814
815 state = hsdram->State;
816 if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
817 {
818 switch (CallbackId)
819 {
820 case HAL_SDRAM_MSP_INIT_CB_ID :
821 hsdram->MspInitCallback = pCallback;
822 break;
823 case HAL_SDRAM_MSP_DEINIT_CB_ID :
824 hsdram->MspDeInitCallback = pCallback;
825 break;
826 case HAL_SDRAM_REFRESH_ERR_CB_ID :
827 hsdram->RefreshErrorCallback = pCallback;
828 break;
829 default :
830 /* update return status */
831 status = HAL_ERROR;
832 break;
833 }
834 }
835 else if (hsdram->State == HAL_SDRAM_STATE_RESET)
836 {
837 switch (CallbackId)
838 {
839 case HAL_SDRAM_MSP_INIT_CB_ID :
840 hsdram->MspInitCallback = pCallback;
841 break;
842 case HAL_SDRAM_MSP_DEINIT_CB_ID :
843 hsdram->MspDeInitCallback = pCallback;
844 break;
845 default :
846 /* update return status */
847 status = HAL_ERROR;
848 break;
849 }
850 }
851 else
852 {
853 /* update return status */
854 status = HAL_ERROR;
855 }
856
857 return status;
858}
859
873HAL_StatusTypeDef HAL_SDRAM_UnRegisterCallback(SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId)
874{
875 HAL_StatusTypeDef status = HAL_OK;
876 HAL_SDRAM_StateTypeDef state;
877
878 state = hsdram->State;
879 if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
880 {
881 switch (CallbackId)
882 {
883 case HAL_SDRAM_MSP_INIT_CB_ID :
884 hsdram->MspInitCallback = HAL_SDRAM_MspInit;
885 break;
886 case HAL_SDRAM_MSP_DEINIT_CB_ID :
887 hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
888 break;
889 case HAL_SDRAM_REFRESH_ERR_CB_ID :
890 hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
891 break;
892 case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
893 hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
894 break;
895 case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
896 hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
897 break;
898 default :
899 /* update return status */
900 status = HAL_ERROR;
901 break;
902 }
903 }
904 else if (hsdram->State == HAL_SDRAM_STATE_RESET)
905 {
906 switch (CallbackId)
907 {
908 case HAL_SDRAM_MSP_INIT_CB_ID :
909 hsdram->MspInitCallback = HAL_SDRAM_MspInit;
910 break;
911 case HAL_SDRAM_MSP_DEINIT_CB_ID :
912 hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
913 break;
914 default :
915 /* update return status */
916 status = HAL_ERROR;
917 break;
918 }
919 }
920 else
921 {
922 /* update return status */
923 status = HAL_ERROR;
924 }
925
926 return status;
927}
928
940HAL_StatusTypeDef HAL_SDRAM_RegisterDmaCallback(SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId,
941 pSDRAM_DmaCallbackTypeDef pCallback)
942{
943 HAL_StatusTypeDef status = HAL_OK;
944 HAL_SDRAM_StateTypeDef state;
945
946 if (pCallback == NULL)
947 {
948 return HAL_ERROR;
949 }
950
951 /* Process locked */
952 __HAL_LOCK(hsdram);
953
954 state = hsdram->State;
955 if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
956 {
957 switch (CallbackId)
958 {
959 case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
960 hsdram->DmaXferCpltCallback = pCallback;
961 break;
962 case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
963 hsdram->DmaXferErrorCallback = pCallback;
964 break;
965 default :
966 /* update return status */
967 status = HAL_ERROR;
968 break;
969 }
970 }
971 else
972 {
973 /* update return status */
974 status = HAL_ERROR;
975 }
976
977 /* Release Lock */
978 __HAL_UNLOCK(hsdram);
979 return status;
980}
981#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
982
1008HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)
1009{
1010 /* Check the SDRAM controller state */
1011 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
1012 {
1013 return HAL_BUSY;
1014 }
1015 else if (hsdram->State == HAL_SDRAM_STATE_READY)
1016 {
1017 /* Update the SDRAM state */
1018 hsdram->State = HAL_SDRAM_STATE_BUSY;
1019
1020 /* Enable write protection */
1021 (void)FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);
1022
1023 /* Update the SDRAM state */
1024 hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
1025 }
1026 else
1027 {
1028 return HAL_ERROR;
1029 }
1030
1031 return HAL_OK;
1032}
1033
1040HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)
1041{
1042 HAL_SDRAM_StateTypeDef state = hsdram->State;
1043
1044 /* Check the SDRAM controller state */
1045 if (state == HAL_SDRAM_STATE_BUSY)
1046 {
1047 return HAL_BUSY;
1048 }
1049 else if (state == HAL_SDRAM_STATE_WRITE_PROTECTED)
1050 {
1051 /* Update the SDRAM state */
1052 hsdram->State = HAL_SDRAM_STATE_BUSY;
1053
1054 /* Disable write protection */
1055 (void)FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);
1056
1057 /* Update the SDRAM state */
1058 hsdram->State = HAL_SDRAM_STATE_READY;
1059 }
1060 else
1061 {
1062 return HAL_ERROR;
1063 }
1064
1065 return HAL_OK;
1066}
1067
1076HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command,
1077 uint32_t Timeout)
1078{
1079 HAL_SDRAM_StateTypeDef state = hsdram->State;
1080
1081 /* Check the SDRAM controller state */
1082 if (state == HAL_SDRAM_STATE_BUSY)
1083 {
1084 return HAL_BUSY;
1085 }
1086 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_PRECHARGED))
1087 {
1088 /* Update the SDRAM state */
1089 hsdram->State = HAL_SDRAM_STATE_BUSY;
1090
1091 /* Send SDRAM command */
1092 (void)FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);
1093
1094 /* Update the SDRAM controller state state */
1095 if (Command->CommandMode == FMC_SDRAM_CMD_PALL)
1096 {
1097 hsdram->State = HAL_SDRAM_STATE_PRECHARGED;
1098 }
1099 else
1100 {
1101 hsdram->State = HAL_SDRAM_STATE_READY;
1102 }
1103 }
1104 else
1105 {
1106 return HAL_ERROR;
1107 }
1108
1109 return HAL_OK;
1110}
1111
1119HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)
1120{
1121 /* Check the SDRAM controller state */
1122 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
1123 {
1124 return HAL_BUSY;
1125 }
1126 else if (hsdram->State == HAL_SDRAM_STATE_READY)
1127 {
1128 /* Update the SDRAM state */
1129 hsdram->State = HAL_SDRAM_STATE_BUSY;
1130
1131 /* Program the refresh rate */
1132 (void)FMC_SDRAM_ProgramRefreshRate(hsdram->Instance, RefreshRate);
1133
1134 /* Update the SDRAM state */
1135 hsdram->State = HAL_SDRAM_STATE_READY;
1136 }
1137 else
1138 {
1139 return HAL_ERROR;
1140 }
1141
1142 return HAL_OK;
1143}
1144
1152HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)
1153{
1154 /* Check the SDRAM controller state */
1155 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
1156 {
1157 return HAL_BUSY;
1158 }
1159 else if (hsdram->State == HAL_SDRAM_STATE_READY)
1160 {
1161 /* Update the SDRAM state */
1162 hsdram->State = HAL_SDRAM_STATE_BUSY;
1163
1164 /* Set the Auto-Refresh number */
1165 (void)FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance, AutoRefreshNumber);
1166
1167 /* Update the SDRAM state */
1168 hsdram->State = HAL_SDRAM_STATE_READY;
1169 }
1170 else
1171 {
1172 return HAL_ERROR;
1173 }
1174
1175 return HAL_OK;
1176}
1177
1184uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)
1185{
1186 /* Return the SDRAM memory current mode */
1187 return (FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));
1188}
1189
1215HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)
1216{
1217 return hsdram->State;
1218}
1219
1236static void SDRAM_DMACplt(DMA_HandleTypeDef *hdma)
1237{
1238 SDRAM_HandleTypeDef *hsdram = (SDRAM_HandleTypeDef *)(hdma->Parent);
1239
1240 /* Disable the DMA channel */
1241 __HAL_DMA_DISABLE(hdma);
1242
1243 /* Update the SDRAM controller state */
1244 hsdram->State = HAL_SDRAM_STATE_READY;
1245
1246#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
1247 hsdram->DmaXferCpltCallback(hdma);
1248#else
1249 HAL_SDRAM_DMA_XferCpltCallback(hdma);
1250#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
1251}
1252
1258static void SDRAM_DMACpltProt(DMA_HandleTypeDef *hdma)
1259{
1260 SDRAM_HandleTypeDef *hsdram = (SDRAM_HandleTypeDef *)(hdma->Parent);
1261
1262 /* Disable the DMA channel */
1263 __HAL_DMA_DISABLE(hdma);
1264
1265 /* Update the SDRAM controller state */
1266 hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
1267
1268#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
1269 hsdram->DmaXferCpltCallback(hdma);
1270#else
1271 HAL_SDRAM_DMA_XferCpltCallback(hdma);
1272#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
1273}
1274
1280static void SDRAM_DMAError(DMA_HandleTypeDef *hdma)
1281{
1282 SDRAM_HandleTypeDef *hsdram = (SDRAM_HandleTypeDef *)(hdma->Parent);
1283
1284 /* Disable the DMA channel */
1285 __HAL_DMA_DISABLE(hdma);
1286
1287 /* Update the SDRAM controller state */
1288 hsdram->State = HAL_SDRAM_STATE_ERROR;
1289
1290#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
1291 hsdram->DmaXferErrorCallback(hdma);
1292#else
1293 HAL_SDRAM_DMA_XferErrorCallback(hdma);
1294#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
1295}
1296
1304#endif /* HAL_SDRAM_MODULE_ENABLED */
1305
1310#endif /* FMC_Bank5_6 */
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
#define __HAL_DMA_DISABLE(__HANDLE__)
Disable the specified DMA Stream.
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition
@ HAL_ERROR
@ HAL_OK
@ HAL_BUSY
#define UNUSED(X)
#define __HAL_UNLOCK(__HANDLE__)
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
DMA handle Structure definition.