///////////////////////////////////////////////////////////////////////////////
//                                                                            /
// IAR ANSI C/C++ Compiler V6.30.1.53127/W32 for ARM    08/Mar/2012  18:18:20 /
// Copyright 1999-2011 IAR Systems AB.                                        /
//                                                                            /
//    Cpu mode     =  thumb                                                   /
//    Endian       =  little                                                  /
//    Source file  =  D:\Profiles\b01252\My Documents\Freescale\P0\software\s /
//                    ample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_ /
//                    sc\src\projects\TWRK20D50M_Demo\rtc\rtc.c               /
//    Command line =  "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\src\projects\TWRK20D50M_Demo\rtc\rtc.c" -D IAR -D   /
//                    TOWER -D TWR_K20D50M -lCN "D:\Profiles\b01252\My        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\FLASH_128KB_PFLASH\List\" -lB         /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\FLASH_128KB_PFLASH\List\" /
//                     -o "D:\Profiles\b01252\My                              /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\FLASH_128KB_PFLASH\Obj\" --no_cse     /
//                    --no_unroll --no_inline --no_code_motion --no_tbaa      /
//                    --no_clustering --no_scheduling --debug                 /
//                    --endian=little --cpu=Cortex-M4 -e --fpu=None           /
//                    --dlib_config "C:\Program Files\IAR Systems\Embedded    /
//                    Workbench 6.0_5\arm\INC\c\DLib_Config_Normal.h" -I      /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\common\" -I  /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\cpu\" -I     /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\cpu\headers\ /
//                    " -I "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\drivers\uart\" -I        /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\drivers\mcg\ /
//                    " -I "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\drivers\wdog\" -I        /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\drivers\rcm\ /
//                    " -I "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\drivers\lptmr\" -I       /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\platforms\"  /
//                    -I "D:\Profiles\b01252\My Documents\Freescale\P0\softwa /
//                    re\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50 /
//                    MHz_sc\build\iar\TWRK20D50M_Demo\..\" -I                /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\projects\TWR /
//                    K20D50M_Demo\" -I "D:\Profiles\b01252\My                /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \dma\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \adc16\" -I "D:\Profiles\b01252\My                      /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \i2c\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \tsi\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \adc_test\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \angle_calculation\" -I "D:\Profiles\b01252\My          /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \buzzer\" -I "D:\Profiles\b01252\My                     /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \fft_test\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \irda_test\" -I "D:\Profiles\b01252\My                  /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \cmt_test\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \protocol\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \task_mgr\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \TWRK20_specific\" -I "D:\Profiles\b01252\My            /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \rtc\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\CMSIS\DSP_Lib\Lib\interm /
//                    ediateFiles\" -I "D:\Profiles\b01252\My                 /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\CMSIS\DSP_Lib\Include\"  /
//                    -I "D:\Profiles\b01252\My Documents\Freescale\P0\softwa /
//                    re\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50 /
//                    MHz_sc\build\iar\TWRK20D50M_Demo\..\..\..\src\CMSIS\CM4 /
//                    \DeviceSupport\ARM\ARMCM4\" -I "D:\Profiles\b01252\My   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\CMSIS\CM4\CoreSupport\"  /
//                    -I "D:\Profiles\b01252\My Documents\Freescale\P0\softwa /
//                    re\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50 /
//                    MHz_sc\build\iar\TWRK20D50M_Demo\..\..\..\src\CMSIS\CM4 /
//                    \CoreSupportLiker Options-Library\" -I                  /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\CMSIS\DSP_Li /
//                    b\Lib\intermediateFiles\arm_cortexM4_math.lib\" -I      /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\build\" -Ol      /
//                    --use_c++_inline -I "C:\Program Files\IAR               /
//                    Systems\Embedded Workbench 6.0_5\arm\CMSIS\Include\"    /
//                    -D ARM_MATH_CM4                                         /
//    List file    =  D:\Profiles\b01252\My Documents\Freescale\P0\software\s /
//                    ample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_ /
//                    sc\build\iar\TWRK20D50M_Demo\FLASH_128KB_PFLASH\List\rt /
//                    c.s                                                     /
//                                                                            /
//                                                                            /
///////////////////////////////////////////////////////////////////////////////

        NAME rtc

        #define SHT_PROGBITS 0x1

        EXTERN disable_irq
        EXTERN enable_irq
        EXTERN printf

        PUBLIC DAYS_PER_MONTH
        PUBLIC YEAR_ELAPSED_DAY
        PUBLIC clock_service
        PUBLIC current_time
        PUBLIC date_to_seconds
        PUBLIC default_time_date
        PUBLIC deinit_rtc
        PUBLIC rtc_init
        PUBLIC rtc_isrv
        PUBLIC seconds_count
        PUBLIC seconds_to_date
        PUBLIC set_date
        PUBLIC set_daynumber
        PUBLIC set_time

// D:\Profiles\b01252\My Documents\Freescale\P0\software\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\src\projects\TWRK20D50M_Demo\rtc\rtc.c
//    1 
//    2 /**
//    3 Copyright (c) 2008 Freescale Semiconductor
//    4 Freescale Confidential Proprietary
//    5 \file       rtc.h
//    6 \brief      real time clock basic functionality
//    7 \author     Freescale Semiconductor
//    8 \version    1.0
//    9 \date       Sep 14, 2011
//   10 */
//   11 
//   12 #include "common.h"
//   13 #include "rtc.h"
//   14 
//   15 
//   16 /**  rtc_init
//   17  * \brief    Initialize RTC module to count seconds
//   18  * \author   
//   19  * \param none
//   20  * \return none
//   21  */ 
//   22 
//   23 
//   24 
//   25 

        SECTION `.data`:DATA:REORDER:NOROOT(2)
//   26 time_date_t *current_time = (time_date_t *)(0x4003E000); //initialize current time pointer to FVBAT registers
current_time:
        DATA
        DC32 4003E000H
//   27 //time_date_t  RFVBAT_BASE_PTR //0x4003E000u
//   28 
//   29 //#define ap_time (*time_date_t)(time_date_t)FVBAT_BASE_PTR
//   30 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//   31 void default_time_date(void)
//   32  {
default_time_date:
        PUSH     {R7,LR}
//   33 /*   time_date_t  *p_time;
//   34    p_time = (time_date_t *)(0x4003E000);
//   35    p_time->seconds = 59;*/
//   36    
//   37    current_time = (time_date_t *)(0x4003E000); //initialize current time pointer to FVBAT registers
        LDR.W    R0,??DataTable4
        LDR.W    R1,??DataTable4_1  ;; 0x4003e000
        STR      R1,[R0, #+0]
//   38    
//   39    set_time(current_time, 12, 0, 0);
        MOVS     R3,#+0
        MOVS     R2,#+0
        MOVS     R1,#+12
        LDR.W    R0,??DataTable4
        LDR      R0,[R0, #+0]
        BL       set_time
//   40    set_time(current_time, 12, 0, 0);
        MOVS     R3,#+0
        MOVS     R2,#+0
        MOVS     R1,#+12
        LDR.N    R0,??DataTable4
        LDR      R0,[R0, #+0]
        BL       set_time
//   41    set_date(current_time, 14, 2, 2012);
        MOVW     R3,#+2012
        MOVS     R2,#+2
        MOVS     R1,#+14
        LDR.N    R0,??DataTable4
        LDR      R0,[R0, #+0]
        BL       set_date
//   42  }
        POP      {R0,PC}          ;; return
//   43 
//   44 
//   45 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//   46 void set_time(time_date_t * td, char hr, char min, char sec)
//   47 {
//   48   td->hours   = hr;
set_time:
        STRB     R1,[R0, #+2]
//   49   td->minutes = min;
        STRB     R2,[R0, #+1]
//   50   td->seconds = sec;
        STRB     R3,[R0, #+0]
//   51 }
        BX       LR               ;; return
//   52 
//   53 
//   54 /**/

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//   55 void set_date(time_date_t * td, char day, char month, int year)
//   56 {
//   57   td->day   = day;
set_date:
        STRB     R1,[R0, #+4]
//   58   td->month = month;
        STRB     R2,[R0, #+5]
//   59   td->year = year;
        STR      R3,[R0, #+8]
//   60 }
        BX       LR               ;; return
//   61 
//   62 /* day number between 1-7*/

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//   63 void set_daynumber(time_date_t * td, char weekday)
//   64  {
//   65    td->weekday   = weekday;
set_daynumber:
        STRB     R1,[R0, #+3]
//   66  }
        BX       LR               ;; return

        SECTION `.rodata`:CONST:REORDER:NOROOT(2)
`?<Constant "Initialize RTC\\n">`:
        DATA
        DC8 "Initialize RTC\012"

        SECTION `.rodata`:CONST:REORDER:NOROOT(2)
`?<Constant "RTC already initializ...">`:
        DATA
        DC8 "RTC already initialized  \012"
        DC8 0
//   67             
//   68 
//   69 #define YEAR_BASE 2012
//   70 
//   71 /* YEATS */
//   72 typedef struct 
//   73  {
//   74    uint16 daysPerYear;
//   75    uint16 ElapseddaysPerYear;
//   76  } str_daysPerYear;
//   77 
//   78 typedef struct {
//   79         uint8    daysPerMonth;
//   80         uint16   elapsedDays;
//   81         uint8    bisdaysPerMonth; 
//   82         uint16   biselapsedDays;        
//   83        } str_day_per_month;
//   84 
//   85 

        SECTION `.rodata`:CONST:REORDER:NOROOT(2)
//   86 const  str_daysPerYear YEAR_ELAPSED_DAY[] =
YEAR_ELAPSED_DAY:
        DATA
        DC16 366, 366, 365, 731, 365, 1096, 365, 1461, 366, 1827, 365, 2192
        DC16 365, 2557, 365, 2922, 366, 3288, 365, 3653, 365, 4018
//   87 {
//   88   //Days per year,  elapsedYears
//   89       366, 366 ,  //2012
//   90       365, 731 ,  //2013
//   91       365, 1096 ,  //2014
//   92       365, 1461 ,  //2015
//   93       366, 1827 ,  //2016
//   94       365, 2192 ,  //2017
//   95       365, 2557 ,  //2018
//   96       365, 2922 ,  //2019
//   97       366, 3288 ,  //2020
//   98       365, 3653 ,  //2021
//   99       365, 4018 ,  //2022
//  100 };
//  101 
//  102 
//  103 
//  104 

        SECTION `.rodata`:CONST:REORDER:NOROOT(2)
//  105 const str_day_per_month  DAYS_PER_MONTH[] =
DAYS_PER_MONTH:
        DATA
        DC8 31, 0
        DC16 31
        DC8 31, 0
        DC16 31
        DC8 28, 0
        DC16 59
        DC8 29, 0
        DC16 60
        DC8 31, 0
        DC16 90
        DC8 31, 0
        DC16 91
        DC8 30, 0
        DC16 120
        DC8 30, 0
        DC16 121
        DC8 31, 0
        DC16 151
        DC8 31, 0
        DC16 152
        DC8 30, 0
        DC16 181
        DC8 30, 0
        DC16 182
        DC8 31, 0
        DC16 212
        DC8 31, 0
        DC16 213
        DC8 31, 0
        DC16 243
        DC8 31, 0
        DC16 244
        DC8 30, 0
        DC16 273
        DC8 30, 0
        DC16 274
        DC8 31, 0
        DC16 304
        DC8 31, 0
        DC16 305
        DC8 30, 0
        DC16 334
        DC8 30, 0
        DC16 335
        DC8 31, 0
        DC16 365
        DC8 31, 0
        DC16 366
//  106 {
//  107 //DaysPerMonth,ElapDays,BisDaysPerMonth,BisElapDays	
//  108        31,  31,31,  31,  //"offset31
//  109        28,  59,29,  60,  //"offset29
//  110        31,  90,31,  91,  //"offset31
//  111        30,  120,30,  121,  //"offset30
//  112        31,  151,31,  152,  //"offset31
//  113        30,  181,30,  182,  //"offset30
//  114        31,  212,31,  213,  //"offset31
//  115        31,  243,31,  244,  //"offset31
//  116        30,  273,30,  274,  //"offset30
//  117        31,  304,31,  305,  //"offset31
//  118        30,  334,30,  335,  //"offset30
//  119        31,  365,31,  366,  //"offset31
//  120 };
//  121 
//  122 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  123 void seconds_to_date(uint32 elapsed_seconds)
//  124 { uint32 elapsed_days;
seconds_to_date:
        PUSH     {R3-R7,LR}
//  125   uint32 remaining_sec;
//  126   uint32 elapsed_hours;
//  127   uint32 elapsed_min;
//  128   uint8  off_year;
//  129   uint8  off_month;
//  130  
//  131   uint8  bisiesto;
//  132   uint16 year;
//  133   uint16 daysofyear;
//  134   uint16 dayofmonth;  
//  135   
//  136   elapsed_days  = elapsed_seconds/(60*60*24);
        LDR.N    R1,??DataTable4_2  ;; 0x15180
        UDIV     R3,R0,R1
//  137   remaining_sec = elapsed_seconds%(60*60*24);
        LDR.N    R1,??DataTable4_2  ;; 0x15180
        UDIV     R2,R0,R1
        MLS      R2,R1,R2,R0
//  138   
//  139   elapsed_hours = remaining_sec/(60*60);
        MOV      R1,#+3600
        UDIV     R1,R2,R1
//  140   remaining_sec = elapsed_seconds%(60*60);
        MOV      R2,#+3600
        UDIV     R4,R0,R2
        MLS      R2,R2,R4,R0
//  141   
//  142   elapsed_min = remaining_sec/(60);
        MOVS     R4,#+60
        UDIV     R2,R2,R4
//  143   elapsed_seconds = elapsed_seconds%(60);  
        MOVS     R4,#+60
        UDIV     R5,R0,R4
        MLS      R0,R4,R5,R0
//  144   
//  145   //search for year
//  146   off_year=0;   
        MOVS     R5,#+0
        B.N      ??seconds_to_date_0
//  147   while(elapsed_days>YEAR_ELAPSED_DAY[off_year].ElapseddaysPerYear) off_year++;
??seconds_to_date_1:
        ADDS     R5,R5,#+1
??seconds_to_date_0:
        UXTB     R5,R5            ;; ZeroExt  R5,R5,#+24,#+24
        LDR.N    R4,??DataTable4_3
        ADDS     R4,R4,R5, LSL #+2
        LDRH     R4,[R4, #+2]
        CMP      R4,R3
        BCC.N    ??seconds_to_date_1
//  148   year = YEAR_BASE + off_year;
        UXTB     R5,R5            ;; ZeroExt  R5,R5,#+24,#+24
        ADDW     R4,R5,#+2012
//  149   bisiesto = !(YEAR_ELAPSED_DAY[off_year].daysPerYear & 0x1);
        UXTB     R5,R5            ;; ZeroExt  R5,R5,#+24,#+24
        LDR.N    R6,??DataTable4_3
        LDRB     R6,[R6, R5, LSL #+2]
        ANDS     R6,R6,#0x1
        EORS     R6,R6,#0x1
//  150  
//  151   //search for month and day of the month
//  152   daysofyear = elapsed_days - YEAR_ELAPSED_DAY[off_year].daysPerYear;
        UXTB     R5,R5            ;; ZeroExt  R5,R5,#+24,#+24
        LDR.N    R7,??DataTable4_3
        LDRH     R5,[R7, R5, LSL #+2]
        SUBS     R5,R3,R5
//  153   
//  154   if (bisiesto)
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        CMP      R6,#+0
        BEQ.N    ??seconds_to_date_2
//  155    {
//  156      off_month = 0;
        MOVS     R6,#+0
        B.N      ??seconds_to_date_3
//  157      while (daysofyear > DAYS_PER_MONTH[off_month].biselapsedDays)off_month++;
??seconds_to_date_4:
        ADDS     R6,R6,#+1
??seconds_to_date_3:
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        LDR.N    R3,??DataTable4_4
        ADDS     R3,R3,R6, LSL #+3
        LDRH     R3,[R3, #+6]
        UXTH     R5,R5            ;; ZeroExt  R5,R5,#+16,#+16
        CMP      R3,R5
        BCC.N    ??seconds_to_date_4
//  158      dayofmonth = daysofyear;
//  159      if (off_month>0) dayofmonth = daysofyear - DAYS_PER_MONTH[off_month].biselapsedDays;
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        CMP      R6,#+1
        BCC.N    ??seconds_to_date_5
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        LDR.N    R3,??DataTable4_4
        ADDS     R3,R3,R6, LSL #+3
        LDRH     R3,[R3, #+6]
        SUBS     R5,R5,R3
        B.N      ??seconds_to_date_5
//  160    }
//  161   else
//  162   {
//  163      off_month = 0;
??seconds_to_date_2:
        MOVS     R6,#+0
        B.N      ??seconds_to_date_6
//  164      while (daysofyear > DAYS_PER_MONTH[off_month].elapsedDays)off_month++;
??seconds_to_date_7:
        ADDS     R6,R6,#+1
??seconds_to_date_6:
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        LDR.N    R3,??DataTable4_4
        ADDS     R3,R3,R6, LSL #+3
        LDRH     R3,[R3, #+2]
        UXTH     R5,R5            ;; ZeroExt  R5,R5,#+16,#+16
        CMP      R3,R5
        BCC.N    ??seconds_to_date_7
//  165      dayofmonth = daysofyear;
//  166      if (off_month>0) dayofmonth = daysofyear - DAYS_PER_MONTH[off_month].elapsedDays;
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        CMP      R6,#+1
        BCC.N    ??seconds_to_date_5
        UXTB     R6,R6            ;; ZeroExt  R6,R6,#+24,#+24
        LDR.N    R3,??DataTable4_4
        ADDS     R3,R3,R6, LSL #+3
        LDRH     R3,[R3, #+2]
        SUBS     R5,R5,R3
//  167   }
//  168   
//  169    set_time(current_time, elapsed_hours, elapsed_min, elapsed_seconds);
??seconds_to_date_5:
        MOVS     R3,R0
        UXTB     R3,R3            ;; ZeroExt  R3,R3,#+24,#+24
        UXTB     R2,R2            ;; ZeroExt  R2,R2,#+24,#+24
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        LDR.N    R0,??DataTable4
        LDR      R0,[R0, #+0]
        BL       set_time
//  170    set_date(current_time, year, off_month+1, dayofmonth);
        UXTH     R5,R5            ;; ZeroExt  R5,R5,#+16,#+16
        MOVS     R3,R5
        ADDS     R2,R6,#+1
        UXTB     R2,R2            ;; ZeroExt  R2,R2,#+24,#+24
        MOVS     R1,R4
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        LDR.N    R0,??DataTable4
        LDR      R0,[R0, #+0]
        BL       set_date
//  171  }
        POP      {R0,R4-R7,PC}    ;; return
//  172 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  173 uint32 date_to_seconds(time_date_t * td)
//  174  {
date_to_seconds:
        PUSH     {R4}
//  175    
//  176    char bisiesto;
//  177    char off_year;
//  178    uint16 dayofyear;
//  179    uint32 elapsed_seconds=0;
        MOVS     R1,#+0
//  180    uint32 elapsed_days=0;
        MOVS     R2,#+0
//  181    
//  182    
//  183    off_year = td->year - YEAR_BASE;
        LDR      R3,[R0, #+8]
        ADDS     R4,R3,#+36
//  184    bisiesto = !(YEAR_ELAPSED_DAY[off_year].daysPerYear & 0x1);
        UXTB     R4,R4            ;; ZeroExt  R4,R4,#+24,#+24
        LDR.N    R3,??DataTable4_3
        LDRB     R3,[R3, R4, LSL #+2]
        ANDS     R3,R3,#0x1
        EORS     R3,R3,#0x1
//  185    
//  186    if (off_year>0)
        UXTB     R4,R4            ;; ZeroExt  R4,R4,#+24,#+24
        CMP      R4,#+1
        BCC.N    ??date_to_seconds_0
//  187    elapsed_days = YEAR_ELAPSED_DAY[off_year-1].ElapseddaysPerYear;
        UXTB     R4,R4            ;; ZeroExt  R4,R4,#+24,#+24
        LDR.N    R1,??DataTable4_3
        ADDS     R1,R1,R4, LSL #+2
        LDRH     R2,[R1, #-2]
//  188    
//  189    
//  190    if (bisiesto)
??date_to_seconds_0:
        UXTB     R3,R3            ;; ZeroExt  R3,R3,#+24,#+24
        CMP      R3,#+0
        BEQ.N    ??date_to_seconds_1
//  191    {
//  192     dayofyear = DAYS_PER_MONTH[td->month-1].biselapsedDays;
        LDRB     R1,[R0, #+5]
        LDR.N    R3,??DataTable4_4
        ADDS     R1,R3,R1, LSL #+3
        LDRH     R1,[R1, #-2]
//  193     dayofyear += td->day;
        LDRB     R3,[R0, #+4]
        ADDS     R1,R1,R3
        B.N      ??date_to_seconds_2
//  194    }else{
//  195     dayofyear = DAYS_PER_MONTH[td->month-1].biselapsedDays;
??date_to_seconds_1:
        LDRB     R1,[R0, #+5]
        LDR.N    R3,??DataTable4_4
        ADDS     R1,R3,R1, LSL #+3
        LDRH     R1,[R1, #-2]
//  196     dayofyear += td->day;
        LDRB     R3,[R0, #+4]
        ADDS     R1,R1,R3
//  197    }
//  198 
//  199    elapsed_days += dayofyear;
??date_to_seconds_2:
        UXTAH    R2,R2,R1
//  200     
//  201    elapsed_seconds = elapsed_days*(60*60*24) + td->hours*60*60 + td->minutes*60 + td->seconds;
        LDR.N    R1,??DataTable4_2  ;; 0x15180
        LDRB     R3,[R0, #+2]
        MOV      R4,#+3600
        MULS     R3,R4,R3
        MLA      R1,R1,R2,R3
        LDRB     R2,[R0, #+1]
        MOVS     R3,#+60
        MLA      R1,R3,R2,R1
        LDRB     R0,[R0, #+0]
        UXTAB    R1,R1,R0
//  202    return elapsed_seconds;
        MOVS     R0,R1
        POP      {R4}
        BX       LR               ;; return
//  203    
//  204  }
//  205 
//  206 
//  207 /* This funcion must be called every second*/

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  208 void clock_service(time_date_t * td)
//  209 {
//  210    if (++td->seconds >59 )
clock_service:
        LDRB     R1,[R0, #+0]
        ADDS     R1,R1,#+1
        STRB     R1,[R0, #+0]
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        CMP      R1,#+60
        BCC.N    ??clock_service_0
//  211    {
//  212       td->seconds =  0;
        MOVS     R1,#+0
        STRB     R1,[R0, #+0]
//  213       if (++td->minutes >59 ) 
        LDRB     R1,[R0, #+1]
        ADDS     R1,R1,#+1
        STRB     R1,[R0, #+1]
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        CMP      R1,#+60
        BCC.N    ??clock_service_0
//  214       {
//  215         td->minutes =  0;
        MOVS     R1,#+0
        STRB     R1,[R0, #+1]
//  216         if (++td->hours >23 ) 
        LDRB     R1,[R0, #+2]
        ADDS     R1,R1,#+1
        STRB     R1,[R0, #+2]
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        CMP      R1,#+24
        BCC.N    ??clock_service_0
//  217           {  
//  218              td->hours = 0; 
        MOVS     R1,#+0
        STRB     R1,[R0, #+2]
//  219              if (++td->weekday >7 ) 
        LDRB     R1,[R0, #+3]
        ADDS     R1,R1,#+1
        STRB     R1,[R0, #+3]
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        CMP      R1,#+8
        BCC.N    ??clock_service_1
//  220              {
//  221                td->weekday =1;
        MOVS     R1,#+1
        STRB     R1,[R0, #+3]
//  222              } 
//  223              td->day++;             
??clock_service_1:
        LDRB     R1,[R0, #+4]
        ADDS     R1,R1,#+1
        STRB     R1,[R0, #+4]
//  224              if (td->month==2)
        LDRB     R1,[R0, #+5]
        CMP      R1,#+2
        BNE.N    ??clock_service_2
//  225              { 
//  226                if ((td -> year%4)  == 0)
        LDR      R1,[R0, #+8]
        MOVS     R2,#+4
        SDIV     R3,R1,R2
        MLS      R3,R3,R2,R1
        CMP      R3,#+0
        BNE.N    ??clock_service_3
//  227                {
//  228                 if  (td->day > 29)      td->day = 1;
        LDRB     R1,[R0, #+4]
        CMP      R1,#+30
        BCC.N    ??clock_service_4
        MOVS     R1,#+1
        STRB     R1,[R0, #+4]
        B.N      ??clock_service_4
//  229                }
//  230                else  if (td->day > 28) td->day = 1; 
??clock_service_3:
        LDRB     R1,[R0, #+4]
        CMP      R1,#+29
        BCC.N    ??clock_service_4
        MOVS     R1,#+1
        STRB     R1,[R0, #+4]
        B.N      ??clock_service_4
//  231              }  
//  232              else if (td->month==4 || td->month==6 || td->month==9  ||   td->month==11)
??clock_service_2:
        LDRB     R1,[R0, #+5]
        CMP      R1,#+4
        BEQ.N    ??clock_service_5
        LDRB     R1,[R0, #+5]
        CMP      R1,#+6
        BEQ.N    ??clock_service_5
        LDRB     R1,[R0, #+5]
        CMP      R1,#+9
        BEQ.N    ??clock_service_5
        LDRB     R1,[R0, #+5]
        CMP      R1,#+11
        BNE.N    ??clock_service_6
//  233              {
//  234               if (td->day  > 30) td->day = 1;
??clock_service_5:
        LDRB     R1,[R0, #+4]
        CMP      R1,#+31
        BCC.N    ??clock_service_4
        MOVS     R1,#+1
        STRB     R1,[R0, #+4]
        B.N      ??clock_service_4
//  235              }
//  236              else // if (td->month==1 || td->month==3 || td->month==5 ||  td->month==7 || td->month==8  ||   td->month==10     ||  td->month==12)
//  237              { 
//  238                if (td->day > 31) td->day = 1;
??clock_service_6:
        LDRB     R1,[R0, #+4]
        CMP      R1,#+32
        BCC.N    ??clock_service_4
        MOVS     R1,#+1
        STRB     R1,[R0, #+4]
//  239              } 
//  240              if ( td->hours == 0 && td->day==1) 
??clock_service_4:
        LDRB     R1,[R0, #+2]
        CMP      R1,#+0
        BNE.N    ??clock_service_0
        LDRB     R1,[R0, #+4]
        CMP      R1,#+1
        BNE.N    ??clock_service_0
//  241              { 
//  242               if (++td->month > 12) 
        LDRB     R1,[R0, #+5]
        ADDS     R1,R1,#+1
        STRB     R1,[R0, #+5]
        UXTB     R1,R1            ;; ZeroExt  R1,R1,#+24,#+24
        CMP      R1,#+13
        BCC.N    ??clock_service_0
//  243               {
//  244                 td->month = 1; 
        MOVS     R1,#+1
        STRB     R1,[R0, #+5]
//  245                 td->year++;
        LDR      R1,[R0, #+8]
        ADDS     R1,R1,#+1
        STR      R1,[R0, #+8]
//  246               } //year 
//  247              }//month
//  248           }//hour
//  249        }//minutes
//  250       }//seconds
//  251    }
??clock_service_0:
        BX       LR               ;; return
//  252 
//  253 
//  254 
//  255 
//  256 
//  257 
//  258 
//  259 

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//  260 uint32 seconds_count;
seconds_count:
        DS8 4
//  261 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  262 void rtc_init(void)
//  263 {
rtc_init:
        PUSH     {R7,LR}
//  264      SIM_SCGC6|=SIM_SCGC6_RTC_MASK;
        LDR.N    R0,??DataTable4_5  ;; 0x4004803c
        LDR      R0,[R0, #+0]
        ORRS     R0,R0,#0x20000000
        LDR.N    R1,??DataTable4_5  ;; 0x4004803c
        STR      R0,[R1, #+0]
//  265      printf("Initialize RTC\n");
        LDR.N    R0,??DataTable4_6
        BL       printf
//  266     //Check to see if RTC is already enabled, meaning the RTC battery backup
//  267     //  saved the RTC values. If not enabled, then we have to initalize RTC
//  268     if((RTC_SR&RTC_SR_TCE_MASK)==0)  //Time Counter Enable
        LDR.N    R0,??DataTable4_7  ;; 0x4003d014
        LDR      R0,[R0, #+0]
        LSLS     R0,R0,#+27
        BMI.N    ??rtc_init_0
//  269     {
//  270         default_time_date();
        BL       default_time_date
//  271         RTC_TSR = 0x00;
        LDR.N    R0,??DataTable4_8  ;; 0x4003d000
        MOVS     R1,#+0
        STR      R1,[R0, #+0]
//  272         RFVBAT_REG7 = 0x00;
        LDR.N    R0,??DataTable4_9  ;; 0x4003e01c
        MOVS     R1,#+0
        STR      R1,[R0, #+0]
//  273         //set the Timer Alarm Register to 0 to disable RTC flag
//  274         RTC_TAR = 0x01;  // set this register to other than 01 so set TAF
        LDR.N    R0,??DataTable4_10  ;; 0x4003d008
        MOVS     R1,#+1
        STR      R1,[R0, #+0]
//  275        // enable the oscillator and set load capacitors
//  276         RTC_CR |=  RTC_CR_OSCE_MASK ;
        LDR.N    R0,??DataTable4_11  ;; 0x4003d010
        LDR      R0,[R0, #+0]
        MOV      R1,#+256
        ORRS     R0,R1,R0
        LDR.N    R1,??DataTable4_11  ;; 0x4003d010
        STR      R0,[R1, #+0]
        B.N      ??rtc_init_1
//  277     }
//  278     else
//  279     {
//  280         RTC_TAR = RTC_TSR + 1;
??rtc_init_0:
        LDR.N    R0,??DataTable4_8  ;; 0x4003d000
        LDR      R0,[R0, #+0]
        ADDS     R0,R0,#+1
        LDR.N    R1,??DataTable4_10  ;; 0x4003d008
        STR      R0,[R1, #+0]
//  281         printf("RTC already initialized  \n");
        LDR.N    R0,??DataTable4_12
        BL       printf
//  282         RFVBAT_REG7 = RTC_TSR - RFVBAT_REG7;
        LDR.N    R0,??DataTable4_8  ;; 0x4003d000
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable4_9  ;; 0x4003e01c
        LDR      R1,[R1, #+0]
        SUBS     R0,R0,R1
        LDR.N    R1,??DataTable4_9  ;; 0x4003e01c
        STR      R0,[R1, #+0]
//  283         
//  284     }
//  285     enable_irq(INT_RTC-16);
??rtc_init_1:
        MOVS     R0,#+28
        BL       enable_irq
//  286     RTC_SR |= RTC_SR_TCE_MASK; // enable counter
        LDR.N    R0,??DataTable4_7  ;; 0x4003d014
        LDR      R0,[R0, #+0]
        ORRS     R0,R0,#0x10
        LDR.N    R1,??DataTable4_7  ;; 0x4003d014
        STR      R0,[R1, #+0]
//  287    
//  288 }
        POP      {R0,PC}          ;; return
//  289 
//  290 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  291 void deinit_rtc(void)
//  292 {    
deinit_rtc:
        PUSH     {R7,LR}
//  293     disable_irq(INT_RTC-16);
        MOVS     R0,#+28
        BL       disable_irq
//  294 }
        POP      {R0,PC}          ;; return
//  295 
//  296 
//  297 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  298 void rtc_isrv(void)
//  299 {
rtc_isrv:
        PUSH     {R4,LR}
//  300     uint32 rtc_sr = RTC_SR;
        LDR.N    R0,??DataTable4_7  ;; 0x4003d014
        LDR      R4,[R0, #+0]
//  301     
//  302     if(rtc_sr & RTC_SR_TAF_MASK) // RTC timer alarm flag is set
        MOVS     R0,R4
        LSLS     R0,R0,#+29
        BPL.N    ??rtc_isrv_0
//  303     {
//  304       RTC_TAR = RTC_TSR + 1;     // write new value to TAR to clear TAF
        LDR.N    R0,??DataTable4_8  ;; 0x4003d000
        LDR      R0,[R0, #+0]
        ADDS     R0,R0,#+1
        LDR.N    R1,??DataTable4_10  ;; 0x4003d008
        STR      R0,[R1, #+0]
//  305       seconds_count++;
        LDR.N    R0,??DataTable4_13
        LDR      R0,[R0, #+0]
        ADDS     R0,R0,#+1
        LDR.N    R1,??DataTable4_13
        STR      R0,[R1, #+0]
//  306       clock_service(current_time);
        LDR.N    R0,??DataTable4
        LDR      R0,[R0, #+0]
        BL       clock_service
//  307 
//  308       
//  309     }
//  310     
//  311     if(rtc_sr & RTC_SR_TOF_MASK) // RTC timer Overlow flag is set
??rtc_isrv_0:
        MOVS     R0,R4
        LSLS     R0,R0,#+30
        BPL.N    ??rtc_isrv_1
//  312     {
//  313      RTC_SR |= RTC_SR_TOF_MASK;
        LDR.N    R0,??DataTable4_7  ;; 0x4003d014
        LDR      R0,[R0, #+0]
        ORRS     R0,R0,#0x2
        LDR.N    R1,??DataTable4_7  ;; 0x4003d014
        STR      R0,[R1, #+0]
//  314     }
//  315 
//  316     
//  317     if (rtc_sr & RTC_SR_TIF_MASK) // Timer Invalid flag
??rtc_isrv_1:
        LSLS     R0,R4,#+31
        BPL.N    ??rtc_isrv_2
//  318     {
//  319         RTC_SR &= ~RTC_SR_TCE_MASK;  //Disable timer
        LDR.N    R0,??DataTable4_7  ;; 0x4003d014
        LDR      R0,[R0, #+0]
        BICS     R0,R0,#0x10
        LDR.N    R1,??DataTable4_7  ;; 0x4003d014
        STR      R0,[R1, #+0]
//  320         RTC_TSR = 0x00; // write to clear TOF or TIF
        LDR.N    R0,??DataTable4_8  ;; 0x4003d000
        MOVS     R1,#+0
        STR      R1,[R0, #+0]
//  321         RTC_SR |= RTC_SR_TCE_MASK;   //re-enable timer
        LDR.N    R0,??DataTable4_7  ;; 0x4003d014
        LDR      R0,[R0, #+0]
        ORRS     R0,R0,#0x10
        LDR.N    R1,??DataTable4_7  ;; 0x4003d014
        STR      R0,[R1, #+0]
//  322     }
//  323 }
??rtc_isrv_2:
        POP      {R4,PC}          ;; return

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4:
        DC32     current_time

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_1:
        DC32     0x4003e000

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_2:
        DC32     0x15180

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_3:
        DC32     YEAR_ELAPSED_DAY

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_4:
        DC32     DAYS_PER_MONTH

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_5:
        DC32     0x4004803c

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_6:
        DC32     `?<Constant "Initialize RTC\\n">`

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_7:
        DC32     0x4003d014

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_8:
        DC32     0x4003d000

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_9:
        DC32     0x4003e01c

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_10:
        DC32     0x4003d008

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_11:
        DC32     0x4003d010

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_12:
        DC32     `?<Constant "RTC already initializ...">`

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable4_13:
        DC32     seconds_count

        SECTION `.iar_vfe_header`:DATA:REORDER:NOALLOC:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
        DC32 0

        SECTION __DLIB_PERTHREAD:DATA:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, 0

        SECTION __DLIB_PERTHREAD_init:DATA:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, 0

        END
//  324 
// 
//   4 bytes in section .bss
//   4 bytes in section .data
// 184 bytes in section .rodata
// 910 bytes in section .text
// 
// 910 bytes of CODE  memory
// 184 bytes of CONST memory
//   8 bytes of DATA  memory
//
//Errors: none
//Warnings: 1
