msp430_common/include/cpu.h
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 
10 #ifndef CPU_H
11 #define CPU_H
12 
24 #include <stdio.h>
25 
26 #include <msp430.h>
27 #include "board.h"
28 
29 #include "sched.h"
30 #include "thread.h"
31 #include "msp430_types.h"
32 #include "cpu_conf.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
41 #define WORDSIZE 16
42 
46 #define ISR(a,b) void __attribute__((naked, interrupt (a))) b(void)
47 
51 static inline void __attribute__((always_inline)) __disable_irq(void)
52 {
53  __asm__ __volatile__("bic %0, r2" : : "i"(GIE));
54  /* this NOP is needed to handle a "delay slot" that all MSP430 MCUs
55  impose silently after messing with the GIE bit, DO NOT REMOVE IT! */
56  __asm__ __volatile__("nop");
57 }
58 
62 static inline void __attribute__((always_inline)) __enable_irq(void)
63 {
64  __asm__ __volatile__("bis %0, r2" : : "i"(GIE));
65  /* this NOP is needed to handle a "delay slot" that all MSP430 MCUs
66  impose silently after messing with the GIE bit, DO NOT REMOVE IT! */
67  __asm__ __volatile__("nop");
68 }
69 
73 extern volatile int __irq_is_in;
74 
78 extern char __isr_stack[ISR_STACKSIZE];
79 
83 static inline void __attribute__((always_inline)) __save_context(void)
84 {
85  __asm__("push r15");
86  __asm__("push r14");
87  __asm__("push r13");
88  __asm__("push r12");
89  __asm__("push r11");
90  __asm__("push r10");
91  __asm__("push r9");
92  __asm__("push r8");
93  __asm__("push r7");
94  __asm__("push r6");
95  __asm__("push r5");
96  __asm__("push r4");
97 
98  __asm__("mov.w r1,%0" : "=r"(sched_active_thread->sp));
99 }
100 
104 static inline void __attribute__((always_inline)) __restore_context(void)
105 {
106  __asm__("mov.w %0,r1" : : "m"(sched_active_thread->sp));
107 
108  __asm__("pop r4");
109  __asm__("pop r5");
110  __asm__("pop r6");
111  __asm__("pop r7");
112  __asm__("pop r8");
113  __asm__("pop r9");
114  __asm__("pop r10");
115  __asm__("pop r11");
116  __asm__("pop r12");
117  __asm__("pop r13");
118  __asm__("pop r14");
119  __asm__("pop r15");
120  __asm__("reti");
121 }
122 
126 static inline void __attribute__((always_inline)) __enter_isr(void)
127 {
128  __save_context();
129  __asm__("mov.w %0,r1" : : "i"(__isr_stack + ISR_STACKSIZE));
130  __irq_is_in = 1;
131 }
132 
136 static inline void __attribute__((always_inline)) __exit_isr(void)
137 {
138  __irq_is_in = 0;
139 
141  sched_run();
142  }
143 
145 }
146 
150 void msp430_cpu_init(void);
151 
157 static inline void cpu_print_last_instruction(void)
158 {
159  puts("n/a");
160 }
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
167 #endif /* CPU_H */
int sched_run(void)
Triggers the scheduler to schedule the next thread.
void msp430_cpu_init(void)
Initialize the cpu.
static void __disable_irq(void)
Globally disable IRQs.
static void __enable_irq(void)
Globally enable IRQs.
static void __exit_isr(void)
Run this code on exiting interrupt routines.
char __isr_stack[ISR_STACKSIZE]
Memory used as stack for the interrupt context.
Threading API.
volatile unsigned int sched_context_switch_request
Flag indicating whether a context switch is necessary after handling an interrupt.
Scheduler API definition.
volatile thread_t * sched_active_thread
Currently active thread.
static void __save_context(void)
Save the current thread context from inside an ISR.
char * sp
thread&#39;s stack pointer
Definition: thread.h:175
static void __enter_isr(void)
Run this code on entering interrupt routines.
#define ISR_STACKSIZE
Stack size used for the exception (ISR) stack.
stdio.h wrapper for MSP430
static void __restore_context(void)
Restore the thread context from inside an ISR.
volatile int __irq_is_in
The current ISR state (inside or not)
static void cpu_print_last_instruction(void)
Print the last instruction&#39;s address.