PIC 18C Microcontoller Program Flow Method

The code on this page and on "Simple C18 Cooperative Multitasking" /techref/microchip/language/c/MultiTask.c.htm only works with the PIC18Cxx chips. For other chips, see

Preemptive real-time multitasking kernel

Bob Ammerman [rammerman at prodigy.net] of RAm Systems says

The PIC18Cxx2 chips contain quite a few nice new features. One of the neatest is programmable access to the stack. This feature allows many powerful tricks.

Notes:

  1. this code has _NOT_ been tested!
  2. all multi-byte values are little-endian
  3. I assume the code space never exceeds 16 bits worth (which it never will on the 242, 252, 442 or 452 chips).
Preemptive real-time multitasking kernel

(: you didn't really think you'd find a whole kernel here, did you? :)

Extending the idea in "F", it would be relatively simple to create a tiny
kernel that did preemptive multitasking.

You would do this by creating a "task control block" for each task which
would hold the STKPTR and critical register values for that task (at least
WREG and STATUS, maybe BSR, one or more FSR's, PCLATH, etc).

Task switching would involve saving the outgoing task's registers in its
"task control block" and then loading everything up from the incoming task's
"task control block".

; Assumption: FSR2 is reserved for use by the kernel (it should't be used by
tasks)

; On entry: FSR2 points to the task's TCB

SAVE_TASK MACRO
 movwf POSTINC2,A
 movff STATUS,POSTINC2
 movff BSR,POSTINC2
 movff STKPTR,POSTINC2
 ENDM

; On entry: FSR2 points to the task's TCB

RESTORE_TASK MACRO
 movf POSTINC2,W,A
 movff POSTINC2,STATUS
 movff POSTINC2,BSR
 movff POSTINC2,STKPTR
 ENDM

 CBLOCK
   curr_task  ; Pointer to current task's TCB (assumed in bank 5)
   new_task  ; Pointer to new task's TCB (assumed in bank 5)
 ENDC


; Assumption: FSR2H contains a 5 at all times (for accessing kernel data
structures)

; On entry:
; 'curr_task' is pointer to current task's TCB (assumed in bank 5)
; 'new_task' is pointer to new task's TCB (assumed in bank 5)

switch_task:
 movff curr_task,FSR2L
 SAVE_TASK

 movff new_task,FSR2L
 RESTORE_TASK

 movff new_task,curr_task
 return  ; will return to current location in new task

Questions: