Contiki 3.x
pic32.c
1 /*
2  * Contiki PIC32 Port project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup pic32 PIC32 Contiki Port
39  *
40  * @{
41  */
42 
43 /**
44  * \file cpu/pic32/mtarch.h
45  * \brief PIC32MX initialization routines
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \author Daniele Alessandrelli <d.alessandrelli@sssup.it>
48  * \date 2012-03-23
49  */
50 
51 /*
52  * PIC32MX795F512L - Specific Functions
53  *
54  * All the functions in this part of the file are specific for the
55  * pic32mx795f512l that is characterized by registers' name that differ from
56  * the 3xx and 4xx families of the pic32mx.
57  */
58 
59 #include <pic32_irq.h>
60 
61 #include <p32xxxx.h>
62 #include <peripheral/system.h>
63 #include <stdint.h>
64 
65 #include <dev/leds.h>
66 
67 /* General Excepiton Handler: overrides the default handler */
68 static enum {
69  EXCEP_IRQ = 0, /* interrupt */
70  EXCEP_AdEL = 4, /* address error exception (load or ifetch) */
71  EXCEP_AdES, /* address error exception (store) */
72  EXCEP_IBE, /* bus error (ifetch) */
73  EXCEP_DBE, /* bus error (load/store) */
74  EXCEP_Sys, /* syscall */
75  EXCEP_Bp, /* breakpoint */
76  EXCEP_RI, /* reserved instruction */
77  EXCEP_CpU, /* coprocessor unusable */
78  EXCEP_Overflow, /* arithmetic overflow */
79  EXCEP_Trap, /* trap (possible divide by zero) */
80  EXCEP_IS1 = 16, /* implementation specfic 1 */
81  EXCEP_CEU, /* CorExtend unuseable */
82  EXCEP_C2E /* coprocessor 2 */
83 } cp0_exception_code;
84 
85 static unsigned int cp0_exception_cause; /* CP0: CAUSE register */
86 static unsigned int cp0_exception_epc; /* CP0: Exception Program Counter */
87 
88 /*---------------------------------------------------------------------------*/
89 void
90 pic32_init(void)
91 {
92  unsigned long int r;
93 
94  ASM_DIS_INT;
95 
96  /* Disable JTAG Port */
97  DDPCONbits.JTAGEN = 0;
98 
99  // set the CP0 cause IV bit high
100  asm volatile("mfc0 %0,$13" : "=r"(r));
101  r |= 0x00800000;
102  asm volatile("mtc0 %0,$13" : "+r"(r));
103 
104  INTCONSET = _INTCON_MVEC_MASK;
105 
106  SYSKEY = 0;
107  SYSKEY = 0xaa996655;
108  SYSKEY = 0x556699aa;
109 
110  /* Enable Sleep Mode */
111  OSCCONCLR = 1 << _OSCCON_SLPEN_POSITION;
112 
113  SYSKEY = 0;
114 
115  ASM_EN_INT;
116 }
117 
118 
119 /*---------------------------------------------------------------------------*/
120 void
121 _general_exception_handler(void)
122 {
123  asm volatile ("mfc0 %0,$14":"=r" (cp0_exception_epc));
124 
125  asm volatile ("mfc0 %0,$13":"=r" (cp0_exception_cause));
126 
127  cp0_exception_code = (cp0_exception_cause >> 2) & 0x0000001F;
128 
129  leds_on(LEDS_ALL);
130 
131  while(1){
132  ;
133  }
134 }
135 /*---------------------------------------------------------------------------*/
136 
137 /** @} */
INTERRUPT interface for PIC32MX (pic32mx795f512l)