4 ;=========================================================================================
5 ; 4.1 Controller Device
6 ;=========================================================================================
7 #set MB91464A 2 ; MB91460 series
9 #set MB91467B 10 ; MB91460 series
11 #set MB91467C 11 ; MB91460 series
13 #set MB91467D 4 ; MB91460 series
15 #set MB91469G 6 ; MB91460 series
17 #set MB91465K 3 ; MB91460 series
19 #set MB91463N 8 ; MB91460 series
21 #set MB91461R 1 ; MB91460 series
22 #set MB91467R 5 ; MB91460 series
24 #set MB91465X 9 ; MB91460 series
26 #set others 7 ; MB91460 series
29 #set DEVICE MB91465X ; <<< select device
30 ;=========================================================================================
31 ; 4.3 Stack Type and Stack Size
32 ;=========================================================================================
34 #set USRSTACK 0 ; user stack: for main program
35 #set SYSSTACK 1 ; system stack: for main program and
38 #set STACKUSE SYSSTACK ; <<< set active stack
40 #set STACK_RESERVE ON ; <<< reserve stack area in
42 #set STACK_SYS_SIZE 0x400-4 ; <<< byte size of System stack
43 #set STACK_USR_SIZE 0x2 ; <<< byte size of User stack
45 ; - If the active stack is set to SYSSTACK, it is used for main program and interrupts.
46 ; In this case, the user stack could be set to a dummy size. If the active stack is
47 ; set to user stack, it is used for the main program but the system stack is
48 ; automatically activated, if an interrupt is serviced. Both stack areas must have a
50 ; - If STACK_RESERVE is ON, the sections USTACK and SSTACK are reserved in this module.
51 ; Otherwise, they have to be reserved in other modules. If STACK_RESERVE is OFF, the
52 ; size definitions STACK_SYS_SIZE and STACK_USR_SIZE have no meaning.
53 ; - Even if they are reverved in other modules, they are still initialised in this
56 ; Note: Several library functions require quite a big stack (due to ANSI).
57 ; Check the stack information files (*.stk) in the LIB\911 directory.
59 ;=========================================================================================
60 ; 4.4 Copy code from Flash to I-RAM
61 ;=========================================================================================
63 #set I_RAM ON ; <<< select if code in section IRAM
65 ; If this option is activated code located in the section IRAM is copied during startup
66 ; from ROM to the instruction-RAM. The code is linked for the instruction-RAM.
68 ;=========================================================================================
69 ; 4.7 Clock Configuration
70 ;=========================================================================================
71 ;=========================================================================================
72 ; 4.7.1 Clock Selection
73 ;=========================================================================================
78 ; Sub-oscillation input: 32 kHz
79 #set SUB_32KHZ_CPU__32KHZ_PER_32KHZ_EXT_32KHZ_CAN__2MHZ 0x11
81 ; Oscillation input: 4 MHz
82 #set PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ 0x23
84 ; MB91461R only: Oscillation input: 10 MHz
85 #set PLL_10MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ 0x41
87 ; MB91461R only: Oscillation input: 20 MHz
88 #set PLL_20MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ 0x51
95 #set CLOCKSPEED PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ
96 ; ; <<< Select clock configuration
98 ;=========================================================================================
99 ; 4.7.2 Select Clock Modulator
100 ;=========================================================================================
102 #set CLOMO OFF ; <<< Enable /disable clock modulator
104 #set CMPR 0x026F ; <<< Ref. to the data sheet, CMPR
106 ; Please refer to the data sheet of the device if you enable clock modulation. The
107 ; register CMPR dependant on the PLL-Clock.
109 ; Note: If the CLKCAN source is set either to main oscillator or to PLL output then the
110 ; clock for the CAN is not influenced by the clock modulation. If the CLKCAN
111 ; source is set CPU clock (CLKB) then the clock for the CAN is also modulated (if
112 ; the clock modulator is enabled).
114 ; Note: If the clock modulator is enabled, the wait states of the internal flash wait
115 ; states must be adapted to maximum frequency. Please check the wait states
118 ; Note: This feature is not supported by every device, e.g. MB91461. Please check the
121 ;=========================================================================================
122 ; 4.8 External Bus Interface
124 ; The rest of the configuration is only applicable for devices with an external bus
127 ; If the device does not offer an external bus interface, the configuration can be
128 ; stoped at this point.
130 ;=========================================================================================
131 ; 5 Definition of Configurations
132 ;=========================================================================================
134 #set NOCLOCK 0 ; do not touch CKSCR register
135 #set MAINCLOCK 1 ; select main clock
136 ; ; MB91461R : 1/4 of oscillation input
137 ; ; Others: 1/2 of oscillation input
138 #set MAINPLLCLOCK 2 ; select main clock with PLL
139 #set SUBCLOCK 3 ; select subclock (if available)
141 #set PSCLOCK_CLKB 0x00 ; select core clock (initial)
142 #set PSCLOCK_PLL 0x10 ; select PLL output (x)
143 #set PSCLOCK_MAIN 0x30 ; select Main Oscillation
145 ;=========================================================================================
146 ; 5.2 CLOCKSPEED == NO_CLOCK
147 ;=========================================================================================
149 #if (CLOCKSPEED == NO_CLOCK )
150 #set CLOCKSOURCE NOCLOCK
152 ;=========================================================================================
153 ; 5.5 CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ
154 ;=========================================================================================
156 #if (CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ )
158 ; Start restriction; Maximum frequency
159 #if (DEVICE == MB91461R)
160 #error: Frequency is not supported by this device.
164 #set CLOCKSOURCE MAINPLLCLOCK ; Clocksource
165 #set ENABLE_SUBCLOCK OFF ; Subclock: ON/OFF
166 #set PLLSPEED 0x010F ; 0x48Ch, 0x48Dh: PLLDIVM/N ; 64 MHz
167 #set DIV_G 0x0F ; 0x48Eh: PLLDIVG;
168 #set MUL_G 0x0F ; 0x48Fh: PLLMULG;
170 #set CPUCLOCK 0x00 ; 0x486h: DIV0R_B; => /1 ; 64 MHz
171 #set PERCLOCK 0x03 ; 0x486h: DIV0R_P; => /4 ; 16 MHz
172 #set EXTBUSCLOCK 0x01 ; 0x487h: DIV1R_T; => /2 ; 32 MHz
174 #set PSCLOCKSOURCE PSCLOCK_PLL ; 0x4C0h: CANPRE; => PLLx ; 128 MHz
175 #set PSDVC 0x07 ; 0x4C0h: CANPRE_DVC; => /8 ; 16 MHz
176 #set CANCLOCK 0x00 ; 0x4C1h: CANCKD; all CAN Clocks enabled
178 #set REGULATORSEL 0x06 ; 0x4CEh: REGSEL;
179 #set REGULATORCTRL 0x00 ; 0x4CFh: REGCTR;
181 #set FLASHCONTROL 0x032 ; 0x7002h: FCHCR;
182 #set FLASHREADT 0xC413 ; 0x7004h: FMWT;
183 #set FLASHMWT2 0x10 ; 0x7006h: FMWT2;
186 ;=========================================================================================
187 ; 6 Section and Data Declaration
188 ;=========================================================================================
193 ;=========================================================================================
194 ; 6.1 Define Stack Size
195 ;=========================================================================================
196 .SECTION SSTACK, STACK, ALIGN=4
197 #if STACK_RESERVE == ON
198 .EXPORT __systemstack, __systemstack_top
200 .RES.B STACK_SYS_SIZE
204 ;=========================================================================================
205 ; 6.2 Define Sections
206 ;=========================================================================================
207 .section DATA, data, align=4
208 .section INIT, data, align=4
209 .section IRAM, code, align=4
216 ;-----------------------------------------------------------------------------------------
217 ; MACRO Clear RC Watchdog
218 ;-----------------------------------------------------------------------------------------
219 #macro ClearRCwatchdog
220 LDI #0x4C7,R7 ; clear RC watchdog
223 .section CODE, code, align=4
224 .section CODE_START, code, align=4
225 #pragma section CODE=IRAM,attr=CODE
227 ;=========================================================================================
229 ;=========================================================================================
230 __start: ; start point
234 ANDCCR #0xEF ; disable interrupts
235 STILM #31 ; set interrupt level to low prior
236 ClearRCwatchdog ; clear harware watchdog
238 ;=========================================================================================
239 ; 7.1 Initialise Stack Pointer
240 ;=========================================================================================
241 #if STACKUSE == SYSSTACK
243 LDI #__systemstack_top, SP ; initialize SP
246 #if (CLOCKSOURCE != NOCLOCK)
247 ;=========================================================================================
248 ; 7.2 Check for CSV reset and set CSV
249 ;=========================================================================================
250 ; Start restriction; No clock supervisor (CSV)
251 #if (DEVICE != MB91461R) && (DEVICE != MB91467R) && (DEVICE != MB91463N)
253 LDI:20 #0x04AD, R0 ; CSVCR
254 BORL #0x8, @R0 ; Enable Main Osc CSV
255 BTSTH #0x4, @R0 ; Check for Main Osc missing
256 BEQ NoMAINCSVreset ; Main osc available -> branch
258 BANDL #0x7, @R0 ; Disable Main Osc CSV
260 LDI #noClockStartup, R0 ; Main Clock missing -> no
261 JMP @R0 ; clock startup
266 BORL #0x4, @R0 ; Enable Sub Osc CSV
267 BTSTH #0x2, @R0 ; Check for Sub Osc missing
268 BEQ NoSUBCSVreset ; Sub osc available -> branch
270 BANDL #0xB, @R0 ; Disable Sub Osc SCSV
271 #if (CLOCKSOURCE == SUBCLOCK)
272 LDI #noClockStartup, R0 ; Sub Clock missing -> no
273 JMP @R0 ; clock startup
277 ;=========================================================================================
278 ; 7.3 Check Clock Condition
279 ;=========================================================================================
280 LDI #0x484, R0 ; Check for Default Values
285 ;=========================================================================================
286 ; 7.4 Restore Default Settings after Reset
287 ;=========================================================================================
288 ;=========================================================================================
289 ; 7.4.1 Disable Clock Modulator
290 ;=========================================================================================
291 LDI #0x04BB, R0 ; Clock Modulator Control Reg
292 BANDL #0xD, @R0 ; Disable Frequency modulation
294 BTSTL #8, @R0 ; Wait until Frequency modulation
295 BNE FMODwait ; is disabled
297 BANDL #0xE, @R0 ; Power down clock modulator
299 ;=========================================================================================
300 ; 7.4.2 Check if running on Sub Clock, change to Main Clock
301 ;=========================================================================================
302 LDI:20 #0x0484,R12 ; Check if running on sub clock
309 LDI:20 #0x04CC,R12 ; Check if Main Clock is stopped
313 BANDL #0xE, @R12 ; Start Main Oscillation
315 LDI #0x4C8, R0 ; Main Stabilisation Wait Time
316 LDI #0x04, R1 ; 32.7 ms
320 mainStabTime: ; Wait for stabilisation time
321 ClearRCwatchdog ; clear harware watchdog
328 LDI:20 #0x0484, R12 ; disable sub clock as source
329 BANDL #0xD, @R12 ; Clock source = 0x01 (Main/2)
332 ;=========================================================================================
333 ; 7.4.3 Disable Sub Clock
334 ;=========================================================================================
335 #if ENABLE_SUBCLOCK != ON
336 LDI #0x0484, R0 ; Clock source control reg CLKR
337 BANDL #0x7, @R0 ; Disable PLL
340 ;=========================================================================================
341 ; 7.4.4 Check if running on PLL, Gear Down PLL
342 ;=========================================================================================
343 LDI:20 #0x0484,R12 ; Check if running on PLL
350 LDI:20 #0x0490, R11 ; clear flags
354 STB R1, @R11 ; Set Flag for Simulator; no Effekt on
357 BANDL #0xC, @R12 ; disable PLL as clock source
358 ; Clock Source = 0x00 (Main/2)
360 LDI:20 #0x048E,R12 ; check if DivG != 0
367 ClearRCwatchdog ; clear harware watchdog
368 BTSTL #4, @R11 ; Gear Down
371 LDI #0x00,R1 ; Clear Flags
375 ;=========================================================================================
377 ;=========================================================================================
378 LDI #0x0484, R0 ; Clock source control reg CLKR
379 BANDL #0xB, @R0 ; Disable PLL
381 ;=========================================================================================
382 ; 7.4.6 Set to Main Clock
383 ;=========================================================================================
384 LDI:20 #0x0484,R12 ; Check if running on PLL
385 BANDL #0xC, @R12 ; disable PLL as clock source
386 ; Clock Source = 0x00 (Main/2)
389 ;=========================================================================================
390 ; 7.5 Set Memory Controller
391 ;=========================================================================================
392 ; Start restriction; No embedded flash
393 #if DEVICE != MB91461R
395 LDI #0x7002, R1 ; FLASH Controller Reg.
396 LDI #FLASHCONTROL, R2 ; Flash Controller Settings
397 STH R2, @R1 ; set register
398 LDI #0x7004, R1 ; FLASH Memory Wait Timing Reg.
399 LDI #FLASHREADT, R2 ; wait settings
400 STH R2, @R1 ; set register
401 LDI #0x7006, R1 ; FLASH Memory Wait Timing Reg.
402 LDI #FLASHMWT2, R2 ; wait settings
403 STB R2, @R1 ; set register
407 ;=========================================================================================
409 ;=========================================================================================
410 ;=========================================================================================
411 ; 7.6.1 Set Voltage Regulator Settings
412 ;=========================================================================================
413 ; Start restriction; No regulator settings
414 #if DEVICE != MB91461R
416 LDI #0x04CF, R0 ; REGCTR
417 LDI #REGULATORCTRL, R1
420 LDI #0x04CE, R0 ; REGSEL
421 LDI #REGULATORSEL, R1
425 ;=========================================================================================
426 ; 7.6.2 Power on Clock Modulator - Clock Modulator Part I
427 ;=========================================================================================
429 LDI #0x04BB, R0 ; Clock Modulator Control Reg
430 LDI #0x11, R1 ; Load value to Power on CM
431 ORB R1, @R0 ; Power on clock modulaor
434 ;=========================================================================================
435 ; 7.6.3 Set CLKR Register w/o Clock Mode
436 ;=========================================================================================
437 ; Set Clock source (Base Clock) for the three clock tree selections
438 ; This select Base clock is used to select afterwards the 3
439 ; Clocks for the diffenrent internal trees.
440 ; When PLL is used, first pll multiplication ratio is set and PLL is
441 ; enabled. After waiting the PLL stabilisation time via timebase
442 ; timer, PLL clock is selected as clock source.
443 LDI #0x048C, R0 ; PLL Cntl Reg. PLLDIVM/N
447 LDI #0x048E, R0 ; PLL Cntl Reg. PLLDIVG
451 LDI #0x048F, R0 ; PLL Cntl Reg. PLLMULG
455 ;=========================================================================================
457 ;=========================================================================================
458 #if ( ( CLOCKSOURCE == MAINPLLCLOCK ) || ( PSCLOCKSOURCE == PSCLOCK_PLL ) )
459 LDI #0x0484, R0 ; Clock source control reg CLKR
460 LDI #0x04, R1 ; Use PLL x1, enable PLL
461 ORB R1, @R0 ; store data to CLKR register
464 ;=========================================================================================
465 ; 7.6.5 Wait for PLL oscillation stabilisation
466 ;=========================================================================================
467 #if ((CLOCKSOURCE==MAINPLLCLOCK)||(PSCLOCKSOURCE==PSCLOCK_PLL))
468 LDI #0x0482, R12 ; TimeBaseTimer TBCR
469 LDI #0x00, R1 ; set 1024 us @ 2 MHz
472 BANDH #7, @R12 ; clear interrupt flag
474 LDI #0x0483, R0 ; clearTimeBaseTimer CTBR
480 BANDH #7, @R12 ; clear interrupt flag
481 BORH #8, @R12 ; set interrupt flag for simulator
484 ClearRCwatchdog ; clear harware watchdog
489 ;=========================================================================================
491 ;=========================================================================================
492 ;=========================================================================================
493 ; 7.6.6.1 Set CPU and peripheral clock
494 ;=========================================================================================
495 ; CPU and peripheral clock are set in one register
496 LDI #0x0486, R2 ; Set DIVR0 (CPU-clock (CLKB)
497 LDI #((CPUCLOCK << 4) + PERCLOCK), R3 ; Load CPU clock setting
499 ;=========================================================================================
500 ; 7.6.6.2 Set External Bus interface clock
501 ;=========================================================================================
502 ; set External Bus clock
503 ; Be aware to do smooth clock setting, to avoid wrong clock setting
504 ; Take care, always write 0 to the lower 4 bits of DIVR1 register
505 LDI #0x0487, R2 ; Set DIVR1
506 LDI #(EXTBUSCLOCK << 4), R3 ; Load Peripheral clock setting
509 ;=========================================================================================
510 ; 7.6.6.3 Set CAN clock prescaler
511 ;=========================================================================================
512 ; Set CAN Prescaler, only clock relevant parameter
513 LDI #0x04C0, R0 ; Set CAN ClockParameter Register
514 LDI #(PSCLOCKSOURCE + PSDVC), R1 ; Load Divider
515 STB R1, @R0 ; Set Divider
517 LDI #0x04c1, R0 ; Set CAN Clock enable Register
518 LDI #CANCLOCK, R1 ; Load CANCLOCK
519 STB R1, @R0 ; set CANCLOCK
521 ;=========================================================================================
522 ; 7.6.7 Switch to PLL Mode
523 ;=========================================================================================
524 #if ( (CLOCKSOURCE == MAINPLLCLOCK) )
527 LDI #0x0490, R0 ; PLL Ctrl Register
529 STB R1, @R0 ; Clear Flag
531 STB R1, @R0 ; Set Flag for Simulator; no Effekt on
534 LDI #0x0484, R3 ; Clock source control reg CLKR
535 BORL #0x2, @R3 ; enable PLL as clock source
539 ClearRCwatchdog ; clear harware watchdog
540 LDUB @R0, R2 ; LOAD PLLCTR to R2
541 AND R1, R2 ; GRUP, counter reach 0
545 STB R1, @R0 ; Clear Gear-Up Flag
550 ;=========================================================================================
551 ; 7.6.8 Enable Frequncy Modulation - Clock Modulator Part II
552 ;=========================================================================================
553 #if CLOMO == ON ; Only applicable if Modulator is on
554 LDI #0x04B8, R0 ; Clock Modulation Parameter Reg
555 LDI #CMPR, R1 ; Load CMP value
556 STH R1, @R0 ; Store CMP value in CMPR
558 LDI #0x04BB, R0 ; Clock Modulator Control Reg
559 LDI #0x13, R1 ; Load value to FM on CM
567 ;=========================================================================================
568 ; 7.8 Copy code from Flash to I-RAM
569 ;=========================================================================================
573 LDI #sizeof(IRAM), R13
585 ;=========================================================================================
587 ;=========================================================================================
588 ;=========================================================================================
590 ;=========================================================================================
592 ; According to ANSI, the DATA section must be cleared during start-up
594 LDI #sizeof DATA &~0x3, R1
603 LDI:8 #sizeof DATA & 0x3, R1
604 LDI #DATA + (sizeof DATA & ~0x3), R13
615 ;=========================================================================================
616 ; 7.11 Copy Init section from ROM to RAM
617 ;=========================================================================================
619 ; All initialised data's (e.g. int i=1) must be stored in ROM/FLASH area.
621 ; The Application must copy the Section (Init) into the RAM area.
624 LDI #sizeof(INIT), R2
650 ;=========================================================================================
651 ; 7.14 call main routine
652 ;=========================================================================================
653 ClearRCwatchdog ; clear harware watchdog
654 LDI:8 #0, r4 ; Set the 1st parameter for main to 0.
656 LDI:8 #0, r5 ; Set the 2nd parameter for main to 0.
657 ;=========================================================================================
658 ; 7.15 Return from main function
659 ;=========================================================================================