Problem using Device Initialisation fin Freescale CodeWarrior 5

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem using Device Initialisation fin Freescale CodeWarrior 5

11,283 Views
mylim
Contributor IV

I've some question to ask about using the device initialisation from Freescale CodeWarrior 5.

1. I initialise the MCU using device initialisation. (It will generate a file called MCUinit.asm under Generated Code)

2. I'm using a SW2 of DEMO9S08QG8 as KBIP2.

3. When interupt it jump to isrVkeyboard at MCUinit.asm.

4. I dun intend to do the entire routine under isrVkeyboard.

5. I intend to set a flag at isrVkeyboard then RTI. (the flag is declared in main.asm)

As I compile it, it couldn't be recognised at MCUinit.asm (undeclared user defined symbol). May I know how do I share the flag I declared in main.asm in MCUinit.asm. Thanks.

Message Edited by mingyee on 03-01-200605:38 AM

Labels (1)
0 Kudos
Reply
10 Replies

2,219 Views
CrasyCat
Specialist III
In fact you should add the XDEF in the assembler file where you have defined the flag. Definition is probably done using DS directive
 
Not sure where this is.
 
CrasyCat
0 Kudos
Reply

2,219 Views
mylim
Contributor IV
I declared my flags as below;
 
<main.asm>
 
;constant
sw0     equ   0
sw1     equ   1
sw2     equ   2
 
flag      ds     1
 
main
;main program goes here
mainend      jmp     main
 
 
----------------------------------------------------------------------------------
<MCUinit.asm>
;all the I/O inits & interupt vectors are here & other interupt routines.
 
XDEF    isrVkeyboard
isrVkeyboard:
 ; Write your interrupt code here ...
       
       bclr     $02,KBISC        ;clr the interupt flag
       bset    sw0,flag        
       rti
 
actually where should i put it? under my main.asm or MCUinit.asm? Thanks.
 
 
0 Kudos
Reply

2,219 Views
rocco
Senior Contributor II
Hi, Mingyee:

Whenever a symbol is DEFINED in one module, but REFERENCED in a different module, the assembler need to be told about it, using XDEF and XREF.

To tell the assembler that a DEFINITION of a symbol in this module needs to be referenced by other modules, you would use XDEF.

To tell the assembler that a REFERENCE to a symbol in this module is defined in another module, you would use XREF.

In your case, the variable "flag" is defined in one module, but referenced in another. So your code becomes:


;constant
XDEF flag
sw0 equ 0
sw1 equ 1
sw2 equ 2
flag ds 1
main
;main program goes here
mainend jmp main
----------------------------------------------------------------------------------
;all the I/O inits & interrupt vectors are here & other interrupt routines.
XREF flag
isrVkeyboard:
; Write your interrupt code here ...

bclr $02,KBISC ;clr the interrupt flag
bset sw0,flag
rti


Sorry if the board messed up your formatting. I have not figured out the HTML yet.
0 Kudos
Reply

2,219 Views
mylim
Contributor IV

Hi Rocco, thanks for you advise. I would liek to ask you further as I've tried as you recommended. I'm able to trigger the irq. However when it's in the IRQ routine, it seems to stuck in an infinite loop. Where it hit RTI it does not return to the main program instead it loop back to the irq routine again. May I know wut's going wrong? Thanks.

<MAIN.asm>

           XDEF _Startup, main,FLAG,SW2ON
           
 XREF MCU_init

           XREF __SEG_END_SSTACK   ; symbol defined by the linker for the end of the stack

           include 'derivative.inc'

; variable/data section
MY_ZEROPAGE: section  short         ; Insert here your data definition
;*******************************************************************************
; constants
;*******************************************************************************
SW1ON       equ    0 
SW2ON       equ    1
KBIP2       equ    2

;*******************************************************************************
; start of page zero ram
;*******************************************************************************
CNT         ds     1
FLAG        ds     1

; code section
MyCode:     section
main:
_Startup:
           ldhx   #__SEG_END_SSTACK ; initialize the stack pointer
           txs
            jsr    MCU_init
           clr    CNT
           clr    FLAG
           cli                     ; enable interrupts
mainLoop:
           lda   #$00
           feed_watchdog
           
           jsr   tsec

detectsw2   brset  KBIP2,FLAG,detectsw2a         ;flag from irq

mainend   jmp main

detectsw2a  bset   PTBD_PTBD7,PTBD              ;on LED

<MCU_init.asm>    

     XDEF    isrVkeyboard
       XREF    FLAG,SW2ON
isrVkeyboard:

       bclr    KBISC_KBACK,KBISC
       bset    SW2ON,FLAG
    
       rti

 

0 Kudos
Reply

2,219 Views
bigmac
Specialist III

Hello mingyee,

There seems to be a couple of problems with the code:

  • I notice that you are looping back to main rather than mainloop, as I might have expected.  So the _Startup section is repetitively executed.
  • It is not clear what you do after the instruction to turn on the LED.  The program should also eventually loop back to mainloop from this branch.

Regards,
Mac

 

0 Kudos
Reply

2,219 Views
mylim
Contributor IV

Sorry.. I was rather sleepy when i posted tat question. So I screwed up a lil.. the actual code is

 

           XDEF _Startup, main

           XDEF FLAG,SW2ON
            XREF MCU_init

           XREF __SEG_END_SSTACK   ; symbol defined by the linker for the end of the stack

           include 'derivative.inc'

MY_ZEROPAGE: section  short  

; constants
SW1ON       equ    0 
SW2ON       equ    1
KBIP2       equ    2

; start of page zero ram

CNT         ds     1
FLAG        ds     1

;code section
MyCode:     section
main:
_Startup:
           ldhx   #__SEG_END_SSTACK ; initialize the stack pointer
           txs
            jsr    MCU_init
           clr    CNT
           clr    FLAG
           cli                     ; enable interrupts
mainLoop:
           lda   #$00
           feed_watchdog
           
           jsr   tsec

detectsw2   brset  KBIP2,FLAG,detectsw2a         ;flag from irq

detectsw2a  bset   PTBD_PTBD7,PTBD              ;on LED

mainend   jmp  mainLoop

<MCU_init.asm> 

      XDEF    isrVkeyboard
       XREF    FLAG,SW2ON
isrVkeyboard:

       bclr    KBISC_KBACK,KBISC
       bset    $01,FLAG
        
       rti

0 Kudos
Reply

2,219 Views
bigmac
Specialist III

Hello Mingyee,

The primary reason for the continuously repeating interrupt would seem to be that you are not clearing the KBI flag.  You need to write a 1 to the KBACK bit.  However, even with this corrected, there will still be multiple interrupts due to switch bounce.  To prevent this, you need to disable the keyboard interrupt for the necessary de-bounce period, and then re-enable.

There also seemed to be other confusion with identification of flag bits.  Also I could not understand what you were trying to achieve with the LED.  So I have taken the liberty to modify your code to something that addresses these issues.  For the purpose of demonstration, pressing S1 should turn-on the LED,  and pressing S2 should turn-off the LED.

   XDEF _Startup, main
   XDEF FLAG,SW2ON

   XREF MCU_init
   XREF __SEG_END_SSTACK ; end of stack

   include 'derivative.inc'

MY_ZEROPAGE: section  short 

; Constants
SW1ON     equ    0
SW2ON     equ    1
LED       equ    PTBD_PTBD7

KBIP1     equ    1
KBIP2     equ    2

; Start of page zero RAM
CNT:      ds     1
FLAG:     ds     1

; Code section
MyCode:   section
main:
_Startup:
   ldhx   #__SEG_END_SSTACK ; initialize stack pointer
   txs
   jsr    MCU_init
   clr    CNT
   clr    FLAG
   cli                   ; enable interrupts
mainLoop:
   feed_watchdog
   brclr  KBIP1,FLAG,cont_1
   bclr   KBIP1,FLAG     ; Clear flag
   bset   LED,PTBD       ; Turn-on LED
   jsr    tsec           ; Key de-bounce delay
   bset   KBISC_KBACK,KBISC; Clear KBI flag
   bset   KBISC_KBIE,KBISC ; Re-enable KBI interrupt
   bra    mainLoop
cont_1:
   brclr  KBIP2,FLAG,cont_2
   bclr   KBIP2,FLAG     ; Clear flag
   bclr   LED,PTBD       ; Turn-off LED
   jsr    tsec           ; Key de-bounce delay
   bset   KBISC_KBACK,KBISC; Clear KBI flag
   bset   KBISC_KBIE,KBISC ; Re-enable KBI interrupt
   bra    mainLoop
cont_2:
   ; Other code within mainLoop could go here.

mainend:
   jmp    mainLoop

<MCU_init.asm>

   XDEF   isrVkeyboard
   XREF   FLAG,SW1ON,SW2ON,KBIP1,KBIP2

isrVkeyboard:
   bset   KBISC_KBACK,KBISC; Clear KBI flag
   bclr   KBISC_KBIE,KBISC ; Disable KBI interrupts
   brclr  SW1ON,PTAD,*+5   ; Skip next if not SW1
   bset   KBIP1,FLAG       ; Set SW1 flag
   brclr  SW2ON,PTAD,*+5   ; Skip next if not SW2
   bset   KBIP2,FLAG       ; Set SW2 flag
   rti

It is assumed that the routine MCU_init correctly initializes the KBI module, and that the routine tsec provides a suitable de-bounce delay (20-100 milliseconds), and is actually located in the main .asm file. Another assumption is that the switches pull the input high when activated.

Regards,
Mac

 

0 Kudos
Reply

2,219 Views
mylim
Contributor IV
Once again, thank you very much bigmac. I finally got it. :smileyvery-happy:
0 Kudos
Reply

2,219 Views
CrasyCat
Specialist III
Hello
 
I assume you are using CodeWarrior to build. AM I right?
Did you tell the assembler to make the flag visible from outside of the assembly module.
 
You just need to add the directive XDEF for the flag at the beginning of the file.
 
For example if the flag is called myFlag, add the following at the beginning of the
assembly source file:
  XDEF myFlag
 
I hope this helps
 
CrasyCat
0 Kudos
Reply

2,219 Views
mylim
Contributor IV
Thanks for you reply. You are right. I'm using CodeWarrior 5. The XDEF command i should put it in my 'main.asm' or my generated file by Device Initialisation 'MCUinit.asm' ?Thanks.
0 Kudos
Reply