PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
lpm.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 #include "lpm.h"
56 
57 #if (defined(PDL_PERIPHERAL_LPM_ACTIVE))
58 
64 
65 /******************************************************************************/
66 /* Local pre-processor symbols/macros ('#define') */
67 /******************************************************************************/
68 
69 /******************************************************************************/
70 /* Global variable definitions (declared in header file with 'extern') */
71 /******************************************************************************/
72 
73 /******************************************************************************/
74 /* Local type definitions ('typedef') */
75 /******************************************************************************/
76 
77 /******************************************************************************/
78 /* Local function prototypes ('static') */
79 /******************************************************************************/
80 
81 /******************************************************************************/
82 /* Local variable definitions ('static') */
83 /******************************************************************************/
84 
85 /******************************************************************************/
86 /* Function implementation - global ('extern') and local ('static') */
87 /******************************************************************************/
88 
99 {
100  uint8_t u8Counter;
101 
102  // Read Command to VBAT domain
103  FM4_RTC->WTCR20_f.BREAD = 1;
104 
105  // Poll for read completed
106  while(1 == FM4_RTC->WTCR20_f.BREAD)
107  {}
108 
109  for(u8Counter = 0u; u8Counter < PDL_BACK_UP_REGISTERS; u8Counter += 4u)
110  {
111  *(uint32_t*)((uint32_t)(&stcBackUpReg->u8BREG00) + (uint32_t)u8Counter)
112  = *(uint32_t*)((uint32_t)(&FM4_RTC->BREG00_03) + u8Counter);
113  }
114 
115  return Ok;
116 } // ReadBackupRegisters
117 
127 {
128  uint8_t u8Counter;
129 
130  for(u8Counter = 0u; u8Counter < PDL_BACK_UP_REGISTERS; u8Counter += 4u)
131  {
132  *(uint32_t*)((uint32_t)(&FM4_RTC->BREG00_03) + u8Counter)
133  = *(uint32_t*)((uint32_t)(&stcBackUpReg->u8BREG00) + u8Counter);
134  }
135 
136  // Write Command to VBAT domain
137  FM4_RTC->WTCR20_f.BWRITE = 1;
138 
139  // Poll for read completed
140  while(1 == FM4_RTC->WTCR20_f.BWRITE)
141  {}
142 
143  return Ok;
144 } // WriteBackupRegisters
145 
146 // The following code is only compiled, if CLK and EXINT modules are activated
147 #if (defined(PDL_PERIPHERAL_CLK_ACTIVE)) && (defined(PDL_PERIPHERAL_EXINT_ACTIVE))
148 
159  boolean_t bDisableSubClock)
160 {
161  stc_clk_main_config_t stcMainClockConfig;
162 
163  Clk_MainGetParameters(&stcMainClockConfig);
164 
166 
167  if (TRUE == bDisablePllMainClock)
168  {
171  }
172 
173  if (TRUE == bDisableSubClock)
174  {
176  }
177 
178  stcMainClockConfig.enMode = ClkSleep;
179  Clk_SetMode(&stcMainClockConfig);
180 
181  return Ok;
182 } // Lpm_SetHsCrSleep
183 
194  boolean_t bDisableSubClock)
195 {
196  stc_clk_main_config_t stcMainClockConfig;
197 
198  Clk_MainGetParameters(&stcMainClockConfig);
199 
200  FM4_CRG->SCM_CTL_f.MOSCE = 1u; // enable Main Clock
202 
203  if (TRUE == bDisablePllClock)
204  {
206  }
207 
208  if (TRUE == bDisableSubClock)
209  {
211  }
212 
213  stcMainClockConfig.enMode = ClkSleep;
214  Clk_SetMode(&stcMainClockConfig);
215 
216  return Ok;
217 } // Lpm_SetMainSleep
218 
232 {
233  stc_clk_main_config_t stcMainClockConfig;
234 
235  if((1u != FM4_CRG->SCM_STR_f.PLRDY) || // PLL ready?
236  (0x40u != (FM4_CRG->SCM_STR & 0xE0u)) // Main PLL clock?
237  )
238  {
239  return ErrorNotReady; // System not in PLL mode
240  }
241 
242  Clk_MainGetParameters(&stcMainClockConfig);
243 
244  if (TRUE == bDisableSubClock)
245  {
247  }
248 
249  stcMainClockConfig.enMode = ClkSleep;
250  Clk_SetMode(&stcMainClockConfig);
251 
252  return Ok;
253 } // Lpm_SetPllSleep
254 
265  boolean_t bDisableSubClock)
266 {
267  stc_clk_main_config_t stcMainClockConfig;
268 
269  Clk_MainGetParameters(&stcMainClockConfig);
270 
272 
273  if (TRUE == bDisablePllMainClock)
274  {
277  }
278 
279  if (TRUE == bDisableSubClock)
280  {
282  }
283 
284  stcMainClockConfig.enMode = ClkSleep;
285  Clk_SetMode(&stcMainClockConfig);
286 
287  return Ok;
288 } // Lpm_SetLsCrSleep
289 
299 en_result_t Lpm_SetSubSleep(boolean_t bDisablePllMainClock)
300 {
301  stc_clk_main_config_t stcMainClockConfig;
302 
303  if(1u != FM4_CRG->SCM_STR_f.MORDY)
304  {
305  return ErrorNotReady; // No sub clock available
306  }
307 
308  Clk_MainGetParameters(&stcMainClockConfig);
309 
311 
312  if (TRUE == bDisablePllMainClock)
313  {
316  }
317 
318  stcMainClockConfig.enMode = ClkSleep;
319  Clk_SetMode(&stcMainClockConfig);
320 
321  return Ok;
322 } // Lpm_SetLsCrSleep
323 
337 {
338  stc_clk_main_config_t stcMainClockConfig;
339 
340  if((1u != FM4_CRG->SCM_STR_f.PLRDY) || // PLL ready?
341  (0x40u != (FM4_CRG->SCM_STR & 0xE0u)) // Main PLL clock?
342  )
343  {
344  return ErrorNotReady; // System not in PLL mode
345  }
346 
347  Clk_MainGetParameters(&stcMainClockConfig);
348 
349  if (TRUE == bDisableSubClock)
350  {
352  }
353 
354  stcMainClockConfig.enMode = ClkTimer;
355  Clk_SetMode(&stcMainClockConfig);
356 
357  return Ok;
358 } // Lpm_SetPllTimer
359 
370  boolean_t bDisableSubClock)
371 {
372  stc_clk_main_config_t stcMainClockConfig;
373 
374  Clk_MainGetParameters(&stcMainClockConfig);
375 
377 
378  if (TRUE == bDisablePllMainClock)
379  {
382  }
383 
384  if (TRUE == bDisableSubClock)
385  {
387  }
388 
389  stcMainClockConfig.enMode = ClkTimer;
390  Clk_SetMode(&stcMainClockConfig);
391 
392  return Ok;
393 } // Lpm_SetHsCrTimer
394 
405  boolean_t bDisableSubClock)
406 {
407  stc_clk_main_config_t stcMainClockConfig;
408 
409  Clk_MainGetParameters(&stcMainClockConfig);
410 
411  FM4_CRG->SCM_CTL_f.MOSCE = 1u; // enable Main Clock
413 
414  if (TRUE == bDisablePllClock)
415  {
417  }
418 
419  if (TRUE == bDisableSubClock)
420  {
422  }
423 
424  stcMainClockConfig.enMode = ClkTimer;
425  Clk_SetMode(&stcMainClockConfig);
426 
427  return Ok;
428 } // Lpm_SetMainTimer
429 
440  boolean_t bDisableSubClock)
441 {
442  stc_clk_main_config_t stcMainClockConfig;
443 
444  Clk_MainGetParameters(&stcMainClockConfig);
445 
447 
448  if (TRUE == bDisablePllMainClock)
449  {
452  }
453 
454  if (TRUE == bDisableSubClock)
455  {
457  }
458 
459  stcMainClockConfig.enMode = ClkTimer;
460  Clk_SetMode(&stcMainClockConfig);
461 
462  return Ok;
463 } // Lpm_SetLsCrTimer
464 
474 en_result_t Lpm_SetSubTimer(boolean_t bDisablePllMainClock)
475 {
476  stc_clk_main_config_t stcMainClockConfig;
477 
478  if(1u != FM4_CRG->SCM_STR_f.MORDY)
479  {
480  return ErrorNotReady; // No sub clock available
481  }
482 
483  Clk_MainGetParameters(&stcMainClockConfig);
484 
486 
487  if (TRUE == bDisablePllMainClock)
488  {
491  }
492 
493  stcMainClockConfig.enMode = ClkTimer;
494  Clk_SetMode(&stcMainClockConfig);
495 
496  return Ok;
497 } // Lpm_SetSubTimer
498 
508 {
509  stc_clk_main_config_t stcMainClockConfig;
510 
511  stcMainClockConfig.enMode = ClkRtc;
512  Clk_SetMode(&stcMainClockConfig);
513 
514  return Ok;
515 } // Lpm_SetRtcMode
516 
524 {
525  stc_clk_main_config_t stcMainClockConfig;
526 
527  stcMainClockConfig.enMode = ClkStop;
528  Clk_SetMode(&stcMainClockConfig);
529 
530  return Ok;
531 } // Lpm_SetStopMode
532 
542 {
543  stc_clk_main_config_t stcMainClockConfig;
544 
545  stcMainClockConfig.enMode = ClkDeepRtc;
546  Clk_SetMode(&stcMainClockConfig);
547 
548  return Ok;
549 } // Lpm_SetDeepRtcMode
550 
558 {
559  stc_clk_main_config_t stcMainClockConfig;
560 
561  stcMainClockConfig.enMode = ClkDeepStop;
562  Clk_SetMode(&stcMainClockConfig);
563 
564  return Ok;
565 } // Lpm_SetDeepStopMode
566 
567 #endif // #if (defined(PDL_PERIPHERAL_CLK_ACTIVE)) && (defined(PDL_PERIPHERAL_EXINT_ACTIVE))
568 
569 // The following code is always compiled if LPM module (this) is activated
580 {
581  ReadBackupRegisters(stcBackUpReg);
582 
583  return Ok;
584 } // Lpm_ReadBackupRegisters
585 
595 {
596  WriteBackupRegisters(stcBackUpReg);
597 
598  return Ok;
599 } // Lpm_WriteBackupRegisters
600 
611 en_result_t Lpm_Read_u8DataBackupRegister(uint8_t u8AddressOffset, uint8_t* u8Data)
612 {
613  stc_backupreg_t stcBackUpReg;
614 
615  if (u8AddressOffset >= PDL_BACK_UP_REGISTERS)
616  {
617  return ErrorInvalidParameter;
618  }
619 
620  ReadBackupRegisters(&stcBackUpReg);
621 
622  *u8Data = *(uint8_t*)((uint32_t)(&stcBackUpReg.u8BREG00) + (uint32_t)u8AddressOffset);
623 
624  return Ok;
625 } // Lpm_Read_u8DataBackupRegister
626 
637 en_result_t Lpm_Write_u8DataBackupRegister(uint8_t u8AddressOffset, uint8_t u8Data)
638 {
639  stc_backupreg_t stcBackUpReg;
640 
641  if (u8AddressOffset >= PDL_BACK_UP_REGISTERS)
642  {
643  return ErrorInvalidParameter;
644  }
645 
646  ReadBackupRegisters(&stcBackUpReg);
647  *(uint8_t*)((uint32_t)(&stcBackUpReg.u8BREG00) + (uint32_t)u8AddressOffset) = u8Data;
648  WriteBackupRegisters(&stcBackUpReg);
649 
650  return Ok;
651 } // Lpm_Write_u8DataBackupRegister
652 
664 en_result_t Lpm_Read_u16DataBackupRegister (uint8_t u8AddressOffset, uint16_t* u16Data)
665 {
666  stc_backupreg_t stcBackUpReg;
667 
668  if (u8AddressOffset >= PDL_BACK_UP_REGISTERS)
669  {
670  return ErrorInvalidParameter;
671  }
672 
673  if (0 != (u8AddressOffset % 2))
674  {
675  return ErrorAddressAlignment;
676  }
677 
678  ReadBackupRegisters(&stcBackUpReg);
679 
680  *u16Data = *(uint16_t*)((uint32_t)(&stcBackUpReg.u8BREG00) + (uint32_t)u8AddressOffset);
681 
682  return Ok;
683 } // Lpm_Read_u16DataBackupRegister
684 
696 en_result_t Lpm_Write_u16DataBackupRegister(uint8_t u8AddressOffset, uint16_t u16Data)
697 {
698  stc_backupreg_t stcBackUpReg;
699 
700  if (u8AddressOffset >= PDL_BACK_UP_REGISTERS)
701  {
702  return ErrorInvalidParameter;
703  }
704 
705  if (0 != (u8AddressOffset % 2))
706  {
707  return ErrorAddressAlignment;
708  }
709 
710  ReadBackupRegisters(&stcBackUpReg);
711  *(uint16_t*)((uint32_t)(&stcBackUpReg.u8BREG00) + (uint32_t)u8AddressOffset) = u16Data;
712  WriteBackupRegisters(&stcBackUpReg);
713 
714  return Ok;
715 } // Lpm_Write_u16DataBackupRegister
716 
728 en_result_t Lpm_Read_u32DataBackupRegister (uint8_t u8AddressOffset, uint32_t* u32Data)
729 {
730  stc_backupreg_t stcBackUpReg;
731 
732  if (u8AddressOffset >= PDL_BACK_UP_REGISTERS)
733  {
734  return ErrorInvalidParameter;
735  }
736 
737  if (0 != (u8AddressOffset % 4))
738  {
739  return ErrorAddressAlignment;
740  }
741 
742  ReadBackupRegisters(&stcBackUpReg);
743 
744  *u32Data = *(uint32_t*)((uint32_t)(&stcBackUpReg.u8BREG00) + (uint32_t)u8AddressOffset);
745 
746  return Ok;
747 } // Lpm_Read_u32DataBackupRegister
748 
760 en_result_t Lpm_Write_u32DataBackupRegister(uint8_t u8AddressOffset, uint32_t u32Data)
761 {
762  stc_backupreg_t stcBackUpReg;
763 
764  if (u8AddressOffset >= PDL_BACK_UP_REGISTERS)
765  {
766  return ErrorInvalidParameter;
767  }
768 
769  if (0 != (u8AddressOffset % 4))
770  {
771  return ErrorAddressAlignment;
772  }
773 
774  ReadBackupRegisters(&stcBackUpReg);
775  *(uint32_t*)((uint32_t)(&stcBackUpReg.u8BREG00) + (uint32_t)u8AddressOffset) = u32Data;
776  WriteBackupRegisters(&stcBackUpReg);
777 
778  return Ok;
779 } // Lpm_Write_u32DataBackupRegister
780 
782 
783 #endif // #if (defined(PDL_PERIPHERAL_ENABLE_LPM))
784 
785 /******************************************************************************/
786 /* EOF (not truncated) */
787 /******************************************************************************/