cpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014, Freie Universitaet Berlin (FUB) & INRIA.
3  * All rights reserved.
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
21 #ifndef CPU_H
22 #define CPU_H
23 
24 #include <stdio.h>
25 
26 #include <msp430.h>
27 #include "board.h"
28 
29 #include "sched.h"
30 #include "thread.h"
31 #include "cpu_conf.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 #define WORDSIZE 16
41 
45 #define ISR(a,b) void __attribute__((naked, interrupt (a))) b(void)
46 
50 extern volatile int __irq_is_in;
51 
55 static inline void __attribute__((always_inline)) __save_context(void)
56 {
57  __asm__("push r15");
58  __asm__("push r14");
59  __asm__("push r13");
60  __asm__("push r12");
61  __asm__("push r11");
62  __asm__("push r10");
63  __asm__("push r9");
64  __asm__("push r8");
65  __asm__("push r7");
66  __asm__("push r6");
67  __asm__("push r5");
68  __asm__("push r4");
69 
70  __asm__("mov.w r1,%0" : "=r"(thread_get_active()->sp));
71 }
72 
76 static inline void __attribute__((always_inline)) __restore_context(void)
77 {
78  __asm__("mov.w %0,r1" : : "m"(thread_get_active()->sp));
79 
80  __asm__("pop r4");
81  __asm__("pop r5");
82  __asm__("pop r6");
83  __asm__("pop r7");
84  __asm__("pop r8");
85  __asm__("pop r9");
86  __asm__("pop r10");
87  __asm__("pop r11");
88  __asm__("pop r12");
89  __asm__("pop r13");
90  __asm__("pop r14");
91  __asm__("pop r15");
92  __asm__("reti");
93 }
94 
98 static inline void __attribute__((always_inline)) __enter_isr(void)
99 {
100  extern char __stack; /* defined by linker script to end of RAM */
101  __save_context();
102  __asm__("mov.w %0,r1" : : "i"(&__stack));
103  __irq_is_in = 1;
104 }
105 
109 static inline void __attribute__((always_inline)) __exit_isr(void)
110 {
111  __irq_is_in = 0;
112 
114  sched_run();
115  }
116 
118 }
119 
123 void msp430_cpu_init(void);
124 
130 static inline void cpu_print_last_instruction(void)
131 {
132  puts("n/a");
133 }
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* CPU_H */
140 
cpu_conf.h
Common CPU definitions for MSP430.
__restore_context
static void __restore_context(void)
Restore the thread context from inside an ISR.
Definition: cpu.h:76
__enter_isr
static void __enter_isr(void)
Run this code on entering interrupt routines.
Definition: cpu.h:98
sched.h
Scheduler API definition.
sched_run
thread_t * sched_run(void)
Triggers the scheduler to schedule the next thread.
msp430_cpu_init
void msp430_cpu_init(void)
Initialize the cpu.
thread_get_active
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition: thread.h:486
__irq_is_in
volatile int __irq_is_in
The current ISR state (inside or not)
sched_context_switch_request
volatile unsigned int sched_context_switch_request
Flag indicating whether a context switch is necessary after handling an interrupt.
cpu_print_last_instruction
static void cpu_print_last_instruction(void)
Print the last instruction's address.
Definition: cpu.h:130
__exit_isr
static void __exit_isr(void)
Run this code on exiting interrupt routines.
Definition: cpu.h:109
__save_context
static void __save_context(void)
Save the current thread context from inside an ISR.
Definition: cpu.h:55