clockstuff too
[pyfrprog.git] / pkernel / start91460.asm
1 #set    OFF             0
2 #set    ON              1
3 #set    DEFAULT         2
4 ;=========================================================================================
5 ; 4.1  Controller Device
6 ;=========================================================================================
7 #set    MB91464A        2                       ; MB91460 series
8 ;
9 #set    MB91467B       10                       ; MB91460 series
10 ;
11 #set    MB91467C       11                       ; MB91460 series
12 ;
13 #set    MB91467D        4                       ; MB91460 series
14 ;
15 #set    MB91469G        6                       ; MB91460 series
16 ;
17 #set    MB91465K        3                       ; MB91460 series
18 ;
19 #set    MB91463N        8                       ; MB91460 series
20 ;
21 #set    MB91461R        1                       ; MB91460 series
22 #set    MB91467R        5                       ; MB91460 series
23 ;
24 #set    MB91465X        9                       ; MB91460 series
25 ;
26 #set    others          7                       ; MB91460 series
27 ;
28 ;
29 #set    DEVICE          MB91465K                ; <<< select device
30 ;
31 #set    BOOT_FLASH_SEC  OFF                     ; <<< BOOT and Flash Security Vector    
32 ;=========================================================================================
33 ; 4.3  Stack Type and Stack Size
34 ;=========================================================================================
35 ;
36 #set    USRSTACK        0                       ; user stack:   for main program
37 #set    SYSSTACK        1                       ; system stack: for main program and 
38 ;                                               ;               interrupts
39 ;
40 #set    STACKUSE        SYSSTACK                ; <<< set active stack
41 ;
42 #set    STACK_RESERVE   ON                      ; <<< reserve stack area in 
43 ;                                               ;     this module
44 #set    STACK_SYS_SIZE  0x400-4                 ; <<< byte size of System stack
45 #set    STACK_USR_SIZE  0x2                     ; <<< byte size of User stack 
46 ;
47 ; - If the active stack is set to SYSSTACK,  it is used for main program and interrupts. 
48 ;   In this case,  the user stack  could be set to a dummy size.  If the active stack is 
49 ;   set  to  user  stack,  it is  used  for the  main  program  but the  system stack is 
50 ;   automatically activated,  if an interrupt is serviced.  Both stack areas must have a 
51 ;   reasonable size.
52 ; - If STACK_RESERVE is ON,  the sections USTACK and SSTACK are reserved in this module. 
53 ;   Otherwise, they have to be reserved in other modules.  If STACK_RESERVE is OFF,  the 
54 ;   size definitions STACK_SYS_SIZE and STACK_USR_SIZE have no meaning.
55 ; - Even if  they  are reverved  in other modules,  they are  still initialised  in this 
56 ;   start-up file.
57 ;
58 ; Note: Several library functions require quite a big stack (due to ANSI). 
59 ;       Check the stack information files (*.stk) in the LIB\911 directory.
60 ;
61 ;=========================================================================================
62 ; 4.4  Copy code from Flash to I-RAM
63 ;=========================================================================================
64 ;
65 #set    I_RAM           ON                      ; <<< select  if  code  in  section IRAM
66 ;                                                     should be copied
67 ; If this option is activated code located in the  section IRAM is copied during startup 
68 ; from ROM to the instruction-RAM. The code is linked for the instruction-RAM.
69 ;
70 ;=========================================================================================
71 ; 4.7  Clock Configuration
72 ;=========================================================================================
73 ;=========================================================================================
74 ; 4.7.1  Clock Selection
75 ;=========================================================================================
76 ;
77 ; No clock settings
78 #set    NO_CLOCK                                               0x01
79 ;
80 ; Sub-oscillation input: 32 kHz 
81 #set    SUB_32KHZ_CPU__32KHZ_PER_32KHZ_EXT_32KHZ_CAN__2MHZ     0x11
82 ;
83 ; Oscillation input: 4 MHz 
84 #set    PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ     0x23
85 ;
86 ; MB91461R only: Oscillation input: 10 MHz
87 #set    PLL_10MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ     0x41
88 ;
89 ; MB91461R only: Oscillation input: 20 MHz
90 #set    PLL_20MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ     0x51
91 ;
92 ; User settings
93 #set    CLOCK_USER                                             0x61
94 ;
95 ;
96 ;
97 #set    CLOCKSPEED      PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ
98 ;                                               ; <<< Select clock configuration 
99 ;
100 ;=========================================================================================
101 ; 4.7.2  Select Clock Modulator  
102 ;=========================================================================================
103 ;
104 #set    CLOMO           OFF                     ; <<< Enable /disable clock modulator      
105 ;
106 #set    CMPR            0x026F                  ; <<< Ref. to the data sheet, CMPR
107
108 ; Please  refer  to the data sheet  of the device  if you  enable clock modulation.  The 
109 ; register CMPR dependant on the PLL-Clock.
110 ;
111 ; Note: If the CLKCAN source is set either to main oscillator or to PLL  output then the
112 ;       clock  for  the CAN  is not influenced by  the clock  modulation.  If the CLKCAN 
113 ;       source is set CPU clock (CLKB) then the clock for the CAN is also modulated  (if 
114 ;       the clock modulator is enabled).
115 ;
116 ; Note: If the clock modulator is enabled,  the wait states  of the  internal flash wait 
117 ;       states  must  be  adapted  to  maximum frequency.  Please check the  wait states 
118 ;       settings.
119 ;
120 ; Note: This feature  is not supported by every device,  e.g. MB91461.  Please check the 
121 ;       data sheet.
122 ;
123 ;=========================================================================================
124 ; 4.8  External Bus Interface
125 ;
126 ;      The rest of the configuration is only applicable for devices with an external bus 
127 ;      interface.
128 ;
129 ;      If the device does not offer an external bus interface,  the configuration can be 
130 ;      stoped at this point.
131 ;
132 ;=========================================================================================
133 ; 5  Definition of Configurations
134 ;=========================================================================================
135 ;
136 #set    NOCLOCK         0                       ; do not touch CKSCR register
137 #set    MAINCLOCK       1                       ; select main clock 
138 ;                                               ; MB91461R : 1/4 of oscillation input
139 ;                                               ; Others:    1/2 of oscillation input
140 #set    MAINPLLCLOCK    2                       ; select main clock with PLL
141 #set    SUBCLOCK        3                       ; select subclock (if available)
142 ;
143 #set    PSCLOCK_CLKB    0x00                    ; select core clock (initial)
144 #set    PSCLOCK_PLL     0x10                    ; select PLL output (x)
145 #set    PSCLOCK_MAIN    0x30                    ; select Main Oscillation
146 ;
147 ;=========================================================================================
148 ; 5.2  CLOCKSPEED == NO_CLOCK
149 ;=========================================================================================
150 ;
151 #if (CLOCKSPEED == NO_CLOCK )
152     #set CLOCKSOURCE       NOCLOCK 
153 #endif      
154 ;=========================================================================================
155 ; 5.5  CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ 
156 ;=========================================================================================
157 ;
158 #if (CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ )
159 ;
160 ; Start restriction; Maximum frequency
161   #if (DEVICE == MB91461R) 
162      #error: Frequency is not supported by this device.
163   #endif 
164 ; End restriction
165 ;
166   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
167   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
168   #set  PLLSPEED        0x010F                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  64 MHz
169   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
170   #set  MUL_G           0x0F                    ; 0x48Fh: PLLMULG;     
171   ; Clock Divider
172   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  64 MHz       
173   #set  PERCLOCK        0x03                    ; 0x486h: DIV0R_P;    => /4    ;  16 MHz 
174   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  32 MHz 
175   ; CAN Clock
176   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 128 MHz
177   #set  PSDVC           0x07                    ; 0x4C0h: CANPRE_DVC; => /8    ;  16 MHz
178   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
179   ; Voltage Regulator 
180   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
181   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
182   ; Memory Controller
183   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
184   #set  FLASHREADT      0xC413                  ; 0x7004h: FMWT;
185   #set  FLASHMWT2       0x10                    ; 0x7006h: FMWT2;
186 #endif  
187 ;
188 ;=========================================================================================
189 ; 6  Section and Data Declaration
190 ;=========================================================================================
191         .export __start             
192         .import _main
193         .import _RAM_INIT
194         .import _ROM_INIT
195 ;=========================================================================================
196 ; 6.1  Define Stack Size
197 ;=========================================================================================
198  .SECTION  SSTACK, STACK, ALIGN=4
199 #if STACK_RESERVE == ON
200         .EXPORT         __systemstack, __systemstack_top
201  __systemstack:
202         .RES.B          STACK_SYS_SIZE
203  __systemstack_top: 
204 #endif
205  
206         .SECTION  USTACK, STACK, ALIGN=4
207 #if STACK_RESERVE == ON
208          .EXPORT        __userstack, __userstack_top
209  __userstack:
210         .RES.B          STACK_USR_SIZE
211  __userstack_top:
212  
213 #endif
214 ;=========================================================================================
215 ; 6.2  Define Sections
216 ;=========================================================================================
217         .section        DATA,  data,  align=4
218         .section        INIT,  data,  align=4
219         .section        IRAM,  code,  align=4
220         .section        CONST, const, align=4
221         .section        INTVECT, const, align=4 
222         
223 #if I_RAM 
224         .import _RAM_IRAM
225         .import _ROM_IRAM
226 #endif
227                     
228 #if (BOOT_FLASH_SEC == OFF)        
229         .data.w 0xFFFFFFFF
230         .data.w 0xFFFFFFFF
231         .data.w 0xFFFFFFFF
232         .data.w 0xFFFFFFFF       
233 #else
234         .res.w          4
235 #endif         
236    
237 ;-----------------------------------------------------------------------------------------
238 ; MACRO Clear RC Watchdog
239 ;-----------------------------------------------------------------------------------------
240 #macro  ClearRCwatchdog
241         LDI             #0x4C7,R7               ; clear RC watchdog
242         BANDL           #0x7,@R7
243 #endm
244 ;-----------------------------------------------------------------------------------------
245 ; MACRO WAIT_LOOP
246 ;-----------------------------------------------------------------------------------------
247 #macro wait_loop loop_number
248 #local _wait64_loop
249         LDI             #loop_number, R0
250 _wait64_loop:
251         ADD             #-1, R0
252         BNE             _wait64_loop
253 #endm
254         .section        CODE, code, align=4
255         .section        CODE_START, code, align=4
256 #pragma section CODE=IRAM,attr=CODE
257
258
259 ;=========================================================================================
260 ; 7.  S T A R T 
261 ;=========================================================================================
262 __start:                                        ; start point   
263 startnop: 
264         NOP       
265 ;   
266         ANDCCR          #0xEF                   ; disable interrupts   
267         STILM           #31                         ; set interrupt level to low prior
268         ClearRCwatchdog                         ; clear harware watchdog
269
270 ;=========================================================================================
271 ; 7.1  Initialise Stack Pointer and Table Base Register
272 ;=========================================================================================
273 #if STACKUSE == SYSSTACK       
274         ORCCR           #0x20
275         LDI             #__userstack_top, SP    ; initialize SP
276         ANDCCR          #0xDF
277         LDI             #__systemstack_top, SP  ; initialize SP
278 #endif
279
280 #if STACKUSE == USRSTACK
281         ANDCCR          #0xDF
282         LDI             #__systemstack_top, SP  ; initialize SP
283         ORCCR           #0x20
284         LDI             #__userstack_top, SP    ; initialize SP
285 #endif
286
287         LDI             #INTVECT, R0            ; set Table Base
288 smd_tbr: 
289         MOV             R0, TBR         
290
291 #if (CLOCKSOURCE != NOCLOCK)                                          
292 ;=========================================================================================
293 ; 7.2  Check for CSV reset and set CSV
294 ;=========================================================================================
295 ; Start restriction; No clock supervisor (CSV)
296 #if (DEVICE != MB91461R) && (DEVICE != MB91467R) && (DEVICE != MB91463N)
297 ; End restriction
298         LDI:20          #0x04AD, R0             ; CSVCR
299         BORL            #0x8, @R0               ; Enable Main Osc CSV
300         BTSTH           #0x4, @R0               ; Check for Main Osc missing
301         BEQ             NoMAINCSVreset          ; Main osc available -> branch 
302                                                 ;   to NoCSVreset
303         BANDL           #0x7, @R0               ; Disable Main Osc CSV
304         
305         LDI             #noClockStartup, R0     ; Main Clock missing -> no
306         JMP             @R0                     ; clock startup
307                                                 
308 NoMAINCSVreset: 
309
310
311         BORL            #0x4, @R0               ; Enable Sub Osc CSV
312         BTSTH           #0x2, @R0               ; Check for Sub Osc missing
313         BEQ             NoSUBCSVreset           ; Sub osc available -> branch 
314                                                 ;   to NoCSVreset
315         BANDL           #0xB, @R0               ; Disable Sub Osc SCSV
316 #if (CLOCKSOURCE == SUBCLOCK)
317         LDI             #noClockStartup, R0     ; Sub Clock missing -> no
318         JMP             @R0                     ; clock startup
319 #endif                                                
320 NoSUBCSVreset:       
321 #endif        
322 ;=========================================================================================
323 ; 7.3  Check Clock Condition
324 ;=========================================================================================
325         LDI             #0x484, R0              ; Check for Default Values
326         LDI             #0x0F, R1               
327         ANDB            R1, @R0
328         BEQ             clock_startup 
329
330 ;=========================================================================================
331 ; 7.4  Restore Default Settings after Reset
332 ;=========================================================================================
333 ;=========================================================================================
334 ; 7.4.1  Disable Clock Modulator
335 ;=========================================================================================
336         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
337         BANDL           #0xD, @R0               ; Disable Frequency modulation
338 FMODwait:        
339         BTSTL           #8, @R0                 ; Wait until Frequency modulation
340         BNE             FMODwait                ; is disabled
341         
342         BANDL           #0xE, @R0               ; Power down clock modulator
343
344 ;=========================================================================================
345 ; 7.4.2  Check if running on Sub Clock, change to Main Clock
346 ;=========================================================================================
347         LDI:20          #0x0484,R12             ; Check if running on sub clock
348         LDUB            @R12,R0
349         LDI:8           #0x3,R1
350         AND             R1,R0
351         CMP             #0x3,R0
352         BNE             notOnSubClock
353         
354         LDI:20          #0x04CC,R12             ; Check if Main Clock is stopped
355         BTSTL           #1, @R12
356         BEQ             mainNotStopped
357
358         BANDL           #0xE, @R12              ; Start Main Oscillation
359                         
360         LDI             #0x4C8, R0              ; Main Stabilisation Wait Time
361         LDI             #0x04, R1               ; 32.7 ms
362         AND             R1, @R0  
363         BORH            #0x02, @R0      
364         
365         mainStabTime:                           ; Wait for stabilisation time
366         ClearRCwatchdog                         ; clear harware watchdog
367         BTSTH           #8, @R0
368         BEQ             mainStabTime
369         LDI             #0x0, R1
370         STB             R1, @R0
371
372 mainNotStopped:        
373         LDI:20          #0x0484, R12            ; disable sub clock as source
374         BANDL           #0xD, @R12              ; Clock source = 0x01 (Main/2)  
375                                                        
376 notOnSubClock:
377 ;=========================================================================================
378 ; 7.4.3  Disable Sub Clock
379 ;=========================================================================================
380 #if ENABLE_SUBCLOCK != ON
381         LDI             #0x0484, R0             ; Clock source control reg CLKR
382         BANDL           #0x7, @R0               ; Disable PLL
383 #endif       
384
385 ;=========================================================================================
386 ; 7.4.4  Check if running on PLL, Gear Down PLL
387 ;=========================================================================================
388         LDI:20          #0x0484,R12             ; Check if running on PLL
389         LDUB            @R12,R0
390         LDI:8           #0x3,R1
391         AND             R1,R0
392         CMP             #0x2,R0
393         BNE             notOnPll
394                     
395         LDI:20          #0x0490, R11            ; clear flags  
396         LDI:8           #0x0,R1        
397         STB             R1, @R11
398         LDI             #0x04,R1
399         STB             R1, @R11                ; Set Flag for Simulator; no Effekt on
400                                                 ; Emulator      
401
402         BANDL           #0xC, @R12              ; disable PLL as clock source  
403                                                 ; Clock Source = 0x00 (Main/2)
404                                                     
405         LDI:20          #0x048E,R12             ; check if DivG != 0
406         LDUB            @R12, R0
407         LDI:8           #0xFF,R1
408         AND             R1,R0
409         BEQ             notOnPll
410                                                                                           
411 gearDownLoop:    
412         ClearRCwatchdog                         ; clear harware watchdog
413         BTSTL           #4, @R11                ; Gear Down
414         BEQ             gearDownLoop            ; 
415  
416         LDI             #0x00,R1                ; Clear Flags
417         STB             R1, @R11                ;       
418         
419 notOnPll:
420 ;=========================================================================================
421 ; 7.4.5  Disable PLL
422 ;=========================================================================================
423         LDI             #0x0484, R0             ; Clock source control reg CLKR
424         BANDL           #0xB, @R0               ; Disable PLL
425         
426 ;=========================================================================================
427 ; 7.4.6  Set to Main Clock
428 ;=========================================================================================
429         LDI:20          #0x0484,R12             ; Check if running on PLL
430         BANDL           #0xC, @R12              ; disable PLL as clock source  
431                                                 ; Clock Source = 0x00 (Main/2)
432
433 clock_startup:
434 ;=========================================================================================
435 ; 7.5  Set Memory Controller
436 ;=========================================================================================
437 ; Start restriction; No embedded flash
438 #if DEVICE != MB91461R
439 ; End restriction
440         LDI             #0x7002, R1             ; FLASH Controller Reg.
441         LDI             #FLASHCONTROL, R2       ; Flash Controller Settings
442         STH             R2, @R1                 ; set register
443         LDI             #0x7004, R1             ; FLASH Memory Wait Timing Reg.
444         LDI             #FLASHREADT, R2         ; wait settings
445         STH             R2, @R1                 ; set register
446         LDI             #0x7006, R1             ; FLASH Memory Wait Timing Reg.
447         LDI             #FLASHMWT2, R2          ; wait settings
448         STB             R2, @R1                 ; set register               
449 #endif                
450         ClearRCwatchdog   
451                                                        
452 ;=========================================================================================
453 ; 7.6  Clock startup
454 ;=========================================================================================
455 ;=========================================================================================
456 ; 7.6.1  Set Voltage Regulator Settings
457 ;=========================================================================================
458 ; Start restriction; No regulator settings
459 #if DEVICE != MB91461R
460 ; End restriction
461         LDI             #0x04CF, R0             ; REGCTR
462         LDI             #REGULATORCTRL, R1
463         STB             R1, @R0
464
465         LDI             #0x04CE, R0             ; REGSEL
466         LDI             #REGULATORSEL, R1
467         STB             R1, @R0
468 #endif
469
470 ;=========================================================================================
471 ; 7.6.2  Power on Clock Modulator - Clock Modulator Part I
472 ;=========================================================================================
473 #if CLOMO == ON 
474         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
475         LDI             #0x11, R1               ; Load value to Power on CM
476         ORB             R1, @R0                 ; Power on clock modulaor
477 #endif
478
479 ;=========================================================================================
480 ; 7.6.3  Set CLKR Register w/o Clock Mode
481 ;=========================================================================================
482 ; Set Clock source (Base Clock) for the three clock tree selections
483 ; This select Base clock is used to select afterwards the 3
484 ; Clocks for the diffenrent internal trees.
485 ; When PLL is used, first pll multiplication ratio is set and PLL is
486 ; enabled. After waiting the PLL stabilisation time via timebase
487 ; timer, PLL clock is selected as clock source. 
488         LDI             #0x048C, R0             ; PLL Cntl Reg. PLLDIVM/N
489         LDI:20          #PLLSPEED, R1
490         STH             R1, @R0
491
492         LDI             #0x048E, R0             ; PLL Cntl Reg. PLLDIVG
493         LDI             #DIV_G, R1
494         STB             R1, @R0
495
496         LDI             #0x048F, R0             ; PLL Cntl Reg. PLLMULG
497         LDI             #MUL_G, R1
498         STB             R1, @R0
499
500 ;=========================================================================================
501 ; 7.6.4  Start PLL 
502 ;=========================================================================================
503 #if ( ( CLOCKSOURCE == MAINPLLCLOCK ) || ( PSCLOCKSOURCE == PSCLOCK_PLL ) )
504         LDI             #0x0484, R0             ; Clock source control reg CLKR
505         LDI             #0x04, R1               ; Use PLL x1, enable PLL 
506         ORB             R1, @R0                 ; store data to CLKR register
507 #endif
508        
509 ;=========================================================================================
510 ; 7.6.5  Wait for PLL oscillation stabilisation
511 ;=========================================================================================
512 #if ((CLOCKSOURCE==MAINPLLCLOCK)||(PSCLOCKSOURCE==PSCLOCK_PLL))
513         LDI             #0x0482, R12            ; TimeBaseTimer TBCR
514         LDI             #0x00, R1               ; set 1024 us @ 2 MHz 
515         STB             R1, @R12
516
517         BANDH           #7, @R12                ; clear interrupt flag
518         
519         LDI             #0x0483, R0             ; clearTimeBaseTimer CTBR
520         LDI             #0xA5, R1                 
521         STB             R1, @R0
522         LDI             #0x5A, R1                 
523         STB             R1, @R0
524         
525         BANDH           #7, @R12                ; clear interrupt flag
526         BORH            #8, @R12                ; set interrupt flag for simulator
527
528 PLLwait:        
529         ClearRCwatchdog                         ; clear harware watchdog
530         BTSTH           #8, @R12
531         BEQ             PLLwait
532 #endif
533
534 ;=========================================================================================
535 ; 7.6.6  Set clocks 
536 ;=========================================================================================
537 ;=========================================================================================
538 ; 7.6.6.1  Set CPU and peripheral clock 
539 ;=========================================================================================
540 ; CPU and peripheral clock are set in one register
541         LDI             #0x0486, R2             ; Set DIVR0 (CPU-clock (CLKB)  
542         LDI             #((CPUCLOCK << 4) + PERCLOCK), R3 ; Load CPU clock setting
543         STB             R3, @R2               
544 ;=========================================================================================
545 ; 7.6.6.2  Set External Bus interface clock
546 ;=========================================================================================
547 ; set External Bus clock
548 ; Be aware to do smooth clock setting, to avoid wrong clock setting
549 ; Take care, always write 0 to the lower 4 bits of DIVR1 register
550         LDI             #0x0487, R2             ; Set DIVR1  
551         LDI             #(EXTBUSCLOCK << 4), R3 ; Load Peripheral clock setting
552         STB             R3, @R2 
553         
554 ;=========================================================================================
555 ; 7.6.6.3  Set CAN clock prescaler
556 ;=========================================================================================
557 ; Set CAN Prescaler, only clock relevant parameter 
558         LDI             #0x04C0, R0             ; Set CAN ClockParameter Register
559         LDI             #(PSCLOCKSOURCE + PSDVC), R1     ; Load Divider
560         STB             R1, @R0                          ; Set Divider
561 ; enable CAN clocks
562         LDI             #0x04c1, R0             ; Set CAN Clock enable Register
563         LDI             #CANCLOCK, R1           ; Load CANCLOCK
564         STB             R1, @R0                 ; set CANCLOCK
565
566 ;=========================================================================================
567 ; 7.6.7  Switch to PLL Mode
568 ;=========================================================================================
569 #if ( (CLOCKSOURCE == MAINPLLCLOCK) )
570
571 #if (DIV_G != 0x00)
572         LDI             #0x0490, R0             ; PLL Ctrl Register   
573         LDI             #0x00,R1
574         STB             R1, @R0                 ; Clear Flag
575         LDI             #0x01,R1
576         STB             R1, @R0                 ; Set Flag for Simulator; no Effekt on
577 #endif                                                ; Emulator
578  
579         LDI             #0x0484, R3             ; Clock source control reg CLKR
580         BORL            #0x2, @R3               ; enable PLL as clock source                                               
581                                                 
582 #if (DIV_G != 0x00)                                                
583 gearUpLoop:    
584         ClearRCwatchdog                         ; clear harware watchdog
585         LDUB            @R0, R2                 ; LOAD PLLCTR to R2
586         AND             R1, R2                  ; GRUP, counter reach 0
587         BEQ             gearUpLoop
588
589         LDI             #0x00,R1
590         STB             R1, @R0                 ; Clear Gear-Up Flag
591 #endif         
592      
593 #endif
594
595 ;=========================================================================================
596 ; 7.6.8  Enable Frequncy Modulation - Clock Modulator Part II
597 ;=========================================================================================
598 #if CLOMO == ON                                 ; Only applicable if Modulator is on
599         LDI             #0x04B8, R0             ; Clock Modulation Parameter Reg
600         LDI             #CMPR, R1               ; Load CMP value
601         STH             R1, @R0                 ; Store CMP value in CMPR
602
603         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
604         LDI             #0x13, R1               ; Load value to FM on CM
605         ORB             R1, @R0                 ; FM on 
606 #endif
607
608 #endif
609 noClockStartup:
610         ClearRCwatchdog
611
612 ;=========================================================================================
613 ; 7.8  Copy code from Flash to I-RAM 
614 ;=========================================================================================
615 #if I_RAM == ON
616         LDI             #_RAM_IRAM, R0
617         LDI             #_ROM_IRAM, R1
618         LDI             #sizeof(IRAM), R13
619         CMP             #0, R13
620         BEQ             copy_iram_end
621 copy_iram1: 
622         ADD             #-1, R13
623         LDUB            @(R13, R1), R12
624         BNE:D           copy_iram1
625         STB             R12, @(R13, R0)
626 copy_iram_end: 
627         ClearRCwatchdog
628 #endif
629
630 ;=========================================================================================
631 ; Standard C startup
632 ;=========================================================================================
633 ;=========================================================================================
634 ; 7.10  Clear data 
635 ;=========================================================================================
636 ; clear DATA section
637 ; According to ANSI, the DATA section must be cleared during start-up
638         LDI:8           #0, R0
639         LDI             #sizeof DATA &~0x3, R1
640         LDI             #DATA, R13
641         CMP             #0, R1
642         BEQ             data_clr1
643 data_clr0:
644         ADD2            #-4, R1
645         BNE:D           data_clr0
646         ST              R0, @(R13, R1)
647 data_clr1:
648         LDI:8           #sizeof DATA & 0x3, R1
649         LDI             #DATA + (sizeof DATA & ~0x3), R13
650
651         CMP             #0, R1
652         BEQ             data_clr_end
653 data_clr2:
654         ADD2            #-1, R1
655         BNE:D           data_clr2
656         STB             R0, @(R13, R1)
657 data_clr_end:
658         ClearRCwatchdog
659         
660 ;=========================================================================================
661 ; 7.11  Copy Init section from ROM to RAM
662 ;=========================================================================================
663 ; copy rom
664 ; All initialised data's (e.g. int i=1) must be stored in ROM/FLASH area. 
665 ; (start value)
666 ; The Application must copy the Section (Init) into the RAM area.
667         LDI             #_RAM_INIT, R0
668         LDI             #_ROM_INIT, R1
669         LDI             #sizeof(INIT), R2
670         CMP             #0, R2
671         BEQ:D           copy_rom_end
672         LDI             #3, R12
673         AND             R2, R12
674         BEQ:D           copy_rom2
675         MOV             R2, R13
676         MOV             R2, R3
677         SUB             R12, R3
678 copy_rom1:
679         ADD             #-1, R13
680         LDUB            @(R13, R1), R12
681         CMP             R3, R13
682         BHI:D           copy_rom1
683         STB             R12, @(R13, R0)
684         CMP             #0, R3
685         BEQ:D           copy_rom_end
686 copy_rom2:
687         ADD             #-4, R13
688         LD              @(R13, R1), R12
689         BGT:D           copy_rom2
690         ST              R12, @(R13, R0)
691 copy_rom_end:
692         ClearRCwatchdog
693
694 start_main:
695 ;=========================================================================================
696 ; 7.14  call main routine
697 ;=========================================================================================
698        ClearRCwatchdog                            ; clear harware watchdog
699        LDI:8            #0, r4                    ; Set the 1st parameter for main to 0.
700        CALL32:d         _main, r12
701        LDI:8            #0, r5                    ; Set the 2nd parameter for main to 0.
702 ;=========================================================================================
703 ; 7.15  Return from main function
704 ;=========================================================================================
705 end: 
706         BRA            end  
707         .end            __start