Advertisement
abdullahkahraman

RTOS implementation header file - EE.StackExchange

Jun 21st, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * File:   RTOS.h
  3.  * Author: abdullah
  4.  *
  5.  * Created on 21 Haziran 2012 Perşembe, 10:51
  6.  */
  7.  
  8. #ifndef RTOS_H
  9. #define RTOS_H
  10.  
  11. #define OS_yield(); {\
  12. asm("movlw   _OS_tasks            ; Store the address of tasks, which is the start address of our task 'array'."); \
  13. asm("addwf   _OS_currentTask, w   ; Add current task's index to the start address."); \
  14. asm("movwf   fsr                  ; We have the index of current task in W. Copy it to FSR"); \
  15. asm("movf    pclath, w            ; Copy PCLATH register's contents to W register."); \
  16. asm("movwf   indf                 ; Copy W to current task's first item. We now store PCLATH of the current state of the task."); \
  17. asm("incf    fsr, f               ; Increment index, so that we will point to the next item of current task."); \
  18. asm("movlw   low($+3)             ; Copy PCL+3 to W register. This will let us save the PCL of the current state of the task."); \
  19. asm("movwf   indf                 ; Copy W to task's next item. With that, we will initialize the current task."); \
  20. asm("goto    _OS_taskswitcher     ; Yield the CPU to the awaiting task by going to task switcher."); \
  21. }
  22. #define OS_initializeTask(); {\
  23. asm("movlw   _OS_tasks            ; Store the address of tasks, which is the start address of our task 'array'."); \
  24. asm("addwf   _OS_currentTask, w   ; Add current task's index to the start address."); \
  25. asm("movwf   fsr                  ; We have the index of current task in W. Copy it to FSR"); \
  26. asm("movf    pclath, w            ; Copy PCLATH register's contents to W register."); \
  27. asm("movwf   indf                 ; Copy W to current task's first item. We now store PCLATH."); \
  28. asm("incf    fsr,f                ; Increment index, so that we will point to the next item of current task."); \
  29. asm("movlw   low($+3)             ; Copy PCL+3 to W register. This will let us save the PCL of the start of the task."); \
  30. asm("movwf   indf                 ; Copy W to task's next item. With that, we will initialize the current task."); \
  31. asm("return                       ; We have gathered our initialazation information. Return back to main."); \
  32. }
  33. #define OS_runTasks(numberOfTasks); {\
  34. asm("_OS_taskswitcher"); \
  35. asm("movlw   0x02                 ; W = 2"); \
  36. asm("addwf   _OS_currentTask, f   ; Add 2 to currentTask, store it in currentTask."); \
  37. asm("movlw   "#numberOfTasks"     ; W = numOfTasks"); \
  38. asm("subwf   _OS_currentTask, w   ; w= f - w"); \
  39. asm("btfsc   status, 0            ; If currentTask >= numOfTasks"); \
  40. asm("clrf    _OS_currentTask      ; Clear currentTask"); \
  41. asm("movlw   _OS_tasks            ; Store the address of tasks, which is the start address of our task 'array'."); \
  42. asm("addwf   _OS_currentTask, w   ; Add current task's index to the start address."); \
  43. asm("movwf   fsr                  ; We have the index of current task in W. Copy it to FSR"); \
  44. asm("movf    indf, w              ; Copy the contents of current task's first item to W"); \
  45. asm("movwf   pclath               ; Copy W to PCLATH. As a result, current task's PCLATH will be in PCLATH register."); \
  46. asm("incf    fsr, f               ; Increment index, so that we will point to the next item of current task."); \
  47. asm("movf    indf, w              ; Copy the contents of current task's second item to W."); \
  48. asm("movwf   pcl                  ; Copy W to PCL. Finally, current task's PCL will be in PCL register."); \
  49. }
  50.  
  51. #endif  /* RTOS_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement