From f68df3a81c5c16bebf00ffae07db5670d5159cf5 Mon Sep 17 00:00:00 2001 From: Giovanni `evilaliv3` Pellerano Date: Mon, 17 Dec 2012 09:14:23 +0100 Subject: [PATCH] added support for pic32 (PIC32MX795F512L) --- cpu/pic32/Makefile.pic32 | 51 +++++++ cpu/pic32/clock.c | 144 ++++++++++++++++++ cpu/pic32/debug-uart.c | 100 +++++++++++++ cpu/pic32/debug-uart.h | 63 ++++++++ cpu/pic32/dev/uart1.h | 50 +++++++ cpu/pic32/lib/pic32_clock.c | 134 +++++++++++++++++ cpu/pic32/lib/pic32_clock.h | 74 ++++++++++ cpu/pic32/lib/pic32_irq.h | 79 ++++++++++ cpu/pic32/lib/pic32_spi.c | 186 ++++++++++++++++++++++++ cpu/pic32/lib/pic32_spi.h | 114 +++++++++++++++ cpu/pic32/lib/pic32_timer.c | 283 ++++++++++++++++++++++++++++++++++++ cpu/pic32/lib/pic32_timer.h | 143 ++++++++++++++++++ cpu/pic32/lib/pic32_uart.c | 193 ++++++++++++++++++++++++ cpu/pic32/lib/pic32_uart.h | 125 ++++++++++++++++ cpu/pic32/mtarch.h | 59 ++++++++ cpu/pic32/pic32.c | 137 +++++++++++++++++ cpu/pic32/pic32.h | 99 +++++++++++++ cpu/pic32/rtimer-arch.c | 112 ++++++++++++++ cpu/pic32/rtimer-arch.h | 63 ++++++++ cpu/pic32/slip-uart.c | 96 ++++++++++++ cpu/pic32/watchdog.c | 109 ++++++++++++++ 21 files changed, 2414 insertions(+) create mode 100644 cpu/pic32/Makefile.pic32 create mode 100644 cpu/pic32/clock.c create mode 100644 cpu/pic32/debug-uart.c create mode 100644 cpu/pic32/debug-uart.h create mode 100644 cpu/pic32/dev/uart1.h create mode 100644 cpu/pic32/lib/pic32_clock.c create mode 100644 cpu/pic32/lib/pic32_clock.h create mode 100644 cpu/pic32/lib/pic32_irq.h create mode 100644 cpu/pic32/lib/pic32_spi.c create mode 100644 cpu/pic32/lib/pic32_spi.h create mode 100644 cpu/pic32/lib/pic32_timer.c create mode 100644 cpu/pic32/lib/pic32_timer.h create mode 100644 cpu/pic32/lib/pic32_uart.c create mode 100644 cpu/pic32/lib/pic32_uart.h create mode 100644 cpu/pic32/mtarch.h create mode 100644 cpu/pic32/pic32.c create mode 100644 cpu/pic32/pic32.h create mode 100644 cpu/pic32/rtimer-arch.c create mode 100644 cpu/pic32/rtimer-arch.h create mode 100644 cpu/pic32/slip-uart.c create mode 100644 cpu/pic32/watchdog.c diff --git a/cpu/pic32/Makefile.pic32 b/cpu/pic32/Makefile.pic32 new file mode 100644 index 000000000..59894e969 --- /dev/null +++ b/cpu/pic32/Makefile.pic32 @@ -0,0 +1,51 @@ +# +# Makefile for PIC32MX795F512L +# @author Giovanni Pellerano +# +# $Id: Makefile.pic32,v 1.0 2012/12/03 09:00:00 evilaliv3 Exp $ +# + +PIC32_MODEL=32MX795F512L +CFLAGS += -O1 -fno-strict-aliasing -G7 -Wall -I /opt/microchip/xc32/v1.11/pic32mx/include/ $(CONTIKI_PLAT_DEFS) +CFLAGS += -ffunction-sections + +### Define the CPU directory +CONTIKI_CPU=$(CONTIKI)/cpu/pic32 + +### Define the cpu dirs and source files +CONTIKI_CPU_DIRS = . lib + +CPU_LIBS = $(notdir $(wildcard $(CONTIKI_CPU)/lib/*.c)) + +CONTIKI_SOURCEFILES += pic32.c clock.c watchdog.c rtimer-arch.c $(CPU_LIBS) debug-uart.c slip-uart.c + +### Compiler definitions +CC = xc32-gcc +LD = xc32-gcc +AS = xc32-as +AR = xc32-ar +OBJCOPY = xc32-objcopy +STRIP = xc32-strip + +ifneq ($(PIC32_MODEL),) +CFLAGS += -mprocessor=$(PIC32_MODEL) +LDFLAGS += -mprocessor=$(PIC32_MODEL) -Wl,-Map=contiki-$(TARGET).map -s +LDFLAGS += -Wl,--defsym,_min_stack_size=4096 -Wl,--gc-sections +endif + +### Compilation rules + +%.so: $(OBJECTDIR)/%.o + $(LD) -shared -o $@ $^ + +ifdef CORE +.PHONY: symbols.c symbols.h +symbols.c symbols.h: + $(NM) -C $(CORE) | grep -v @ | grep -v dll_crt0 | awk -f $(CONTIKI)/tools/mknmlist > symbols.c +else +symbols.c symbols.h: + cp ${CONTIKI}/tools/empty-symbols.c symbols.c + cp ${CONTIKI}/tools/empty-symbols.h symbols.h +endif + +contiki-$(TARGET).a: ${addprefix $(OBJECTDIR)/,symbols.o} diff --git a/cpu/pic32/clock.c b/cpu/pic32/clock.c new file mode 100644 index 000000000..8139e95d2 --- /dev/null +++ b/cpu/pic32/clock.c @@ -0,0 +1,144 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file clock.c + * \brief Clock routines. + * \author Giovanni Pellerano + * \author Daniele Alessandrelli + * \date 2012-03-21 + */ + +#include +#include +#include + +#include +#include + +#include "contiki.h" + +static volatile clock_time_t ticks; +static volatile unsigned long seconds; + +/*---------------------------------------------------------------------------*/ +static inline void +clock_callback(void) +{ + ++ticks; + + if(etimer_pending()) { + etimer_request_poll(); + } + +#if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0 +#error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...). +#error Change CLOCK_CONF_SECOND in contiki-conf.h. +#endif + + if((ticks % CLOCK_SECOND) == 0) { + ++seconds; + } +} +/*---------------------------------------------------------------------------*/ +clock_time_t +clock_time(void) +{ + return ticks; +} +/*---------------------------------------------------------------------------*/ +unsigned long +clock_seconds(void) +{ + return seconds; +} +/*---------------------------------------------------------------------------*/ +void +clock_set_seconds(unsigned long sec) +{ + seconds = sec; +} +/*---------------------------------------------------------------------------*/ +void +clock_init(void) +{ + ticks = 0; + seconds = 0; + pic32_timer1_init(CLOCK_SECOND); + pic32_timer1_enable_irq(); + pic32_timer1_start(); +} +/*---------------------------------------------------------------------------*/ +void +clock_delay_usec(uint16_t dt) +{ + uint32_t now; + uint32_t stop; + + asm volatile("mfc0 %0, $9" : "=r"(now)); + + /* The Count register is incremented every two system clock (SYSCLK) cycles. */ + + stop = now + dt * ((pic32_clock_get_system_clock() / 1000000) / 2); + + for (;;) { + asm volatile("mfc0 %0, $9" : "=r"(now)); + if ((int32_t) (now - stop) >= 0) { + break; + } + } +} +/*---------------------------------------------------------------------------*/ +/* + * Deprecated platform-specific routines. + * + */ +inline void +clock_delay(unsigned int delay) +{ + clock_delay_usec(delay); +} +/*---------------------------------------------------------------------------*/ + +TIMER_INTERRUPT(1, clock_callback); + +/** @} */ diff --git a/cpu/pic32/debug-uart.c b/cpu/pic32/debug-uart.c new file mode 100644 index 000000000..16c9e03cd --- /dev/null +++ b/cpu/pic32/debug-uart.c @@ -0,0 +1,100 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file debug-uart.h + * \brief Debug output redirection to uart. + * \author Giovanni Pellerano + * \date 2012-03-21 + */ + +#ifdef __USE_UART__ + +#include +#include + +#include + +#define DEBUG 1 +#if DEBUG +#include +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif + +#define DEBUG_UART(XX) \ + void \ + _mon_putc(char c) \ + { \ + pic32_uart##XX##_write(c); \ + } \ + \ + void \ + dbg_setup_uart(unsigned long ubr) \ + { \ + pic32_uart##XX##_init(ubr, 0); \ + \ + PRINTF("Initializing debug uart: %lubps\n", ubr); \ + } \ + \ + UART_INTERRUPT(XX, 2, pic32_uart##XX##_write); + +#ifdef __USE_UART_PORT1A_FOR_DEBUG__ +DEBUG_UART(1A); +#elif defined __USE_UART_PORT1B_FOR_DEBUG__ +DEBUG_UART(1B); +#elif defined __USE_UART_PORT2A_FOR_DEBUG__ +DEBUG_UART(2A); +#elif defined __USE_UART_PORT2B_FOR_DEBUG__ +DEBUG_UART(2B); +#elif defined __USE_UART_PORT3A_FOR_DEBUG__ +DEBUG_UART(3A); +#elif defined __USE_UART_PORT3B_FOR_DEBUG__ +DEBUG_UART(3B); +#else +DEBUG_UART(1A); +#endif + +#endif /* __USE_UART__*/ + +/** @} */ diff --git a/cpu/pic32/debug-uart.h b/cpu/pic32/debug-uart.h new file mode 100644 index 000000000..96f503909 --- /dev/null +++ b/cpu/pic32/debug-uart.h @@ -0,0 +1,63 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file debug-uart.h + * \brief Debug output redirection to uart. + * \author Giovanni Pellerano + * \date 2012-03-21 + */ + +#ifndef __DEBUG_UART_H__ +#define __DEBUG_UART_H__ + +#include + +#ifdef __USE_UART__ + +void dbg_setup_uart(unsigned long ubr); + +#endif /* __USE_UART__ */ + +#endif + +/** @} */ diff --git a/cpu/pic32/dev/uart1.h b/cpu/pic32/dev/uart1.h new file mode 100644 index 000000000..11674c972 --- /dev/null +++ b/cpu/pic32/dev/uart1.h @@ -0,0 +1,50 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * @file uart1.h + * @brief UART1 routines + * @author Giovanni Pellerano + * + * @date 2012-03-23 + */ + +#ifndef DEV_UART1_H +#define DEV_UART1_H + +#define BAUD2UBR(x) x + +#endif diff --git a/cpu/pic32/lib/pic32_clock.c b/cpu/pic32/lib/pic32_clock.c new file mode 100644 index 000000000..e65badc1d --- /dev/null +++ b/cpu/pic32/lib/pic32_clock.c @@ -0,0 +1,134 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_clock.c + * \brief CLOCK interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-04-11 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#include + +#include + +#include + +#define CLOCK_SOURCE 8000000ul + +#define DEVCFG2 0xBFC02FF4 /* virtual address of the register */ + +#define PLLIDIV_MASK 0x7 + +static const uint16_t FPPLIDIV[8] = { 1, 2, 3, 4, 5, 6, 10, 12 }; +static const uint16_t PLLODIV[8] = { 1, 2, 4, 8, 16, 32, 64, 256 }; +static const uint16_t PLLMULT[8] = { 15, 16, 17, 18, 19, 20, 21, 24 }; +static const uint16_t PBDIV[4] = { 1, 2, 4, 8 }; + +/*---------------------------------------------------------------------------*/ +/** + * \brief Calculate the system clock. + * + * \return the system clock value. + */ +uint32_t +pic32_clock_get_system_clock(void) +{ + return CLOCK_SOURCE * + PLLMULT[(uint32_t) OSCCONbits.PLLMULT] / + (FPPLIDIV[(*((uint32_t *) DEVCFG2)) & PLLIDIV_MASK] * + PLLODIV[(uint32_t) OSCCONbits.PLLODIV]); +} +/*---------------------------------------------------------------------------*/ +/** + * \brief Calculate the peripheral clock. + * + * \return the peripheral clock value. + */ +uint32_t +pic32_clock_get_peripheral_clock(void) +{ + return pic32_clock_get_system_clock() / PBDIV[OSCCONbits.PBDIV]; +} +/*---------------------------------------------------------------------------*/ +uint32_t +pic32_clock_calculate_brg(uint32_t mul, uint32_t desired) +{ + uint32_t fp = pic32_clock_get_peripheral_clock(); + + uint32_t brg[3]; + uint32_t obtained[3]; + int32_t err[3]; + + uint32_t min; + + brg[0] = fp / (mul * desired); // +1 + brg[1] = fp / (mul * desired) - 1; // 0 + brg[2] = fp / (mul * desired) - 2; // -1 + + obtained[0] = fp / (mul * (brg[0] + 1)); + obtained[1] = fp / (mul * (brg[1] + 1)); + obtained[2] = fp / (mul * (brg[2] + 1)); + + err[0] = obtained[0] - desired; + err[1] = obtained[1] - desired; + err[2] = obtained[2] - desired; + + err[0] = err[0] < 0 ? -err[0] : err[0]; + err[1] = err[1] < 0 ? -err[1] : err[1]; + err[2] = err[2] < 0 ? -err[2] : err[2]; + + min = 0; + min = err[1] < err[min] ? 1 : min; + min = err[2] < err[min] ? 2 : min; + + return brg[min]; +} +/** @} */ diff --git a/cpu/pic32/lib/pic32_clock.h b/cpu/pic32/lib/pic32_clock.h new file mode 100644 index 000000000..9a3509848 --- /dev/null +++ b/cpu/pic32/lib/pic32_clock.h @@ -0,0 +1,74 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_clock.h + * \brief CLOCK interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-04-11 + */ + +#ifndef __INCLUDE_PIC32_CLOCK_H__ +#define __INCLUDE_PIC32_CLOCK_H__ + +#include + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +/* Function used to calculate the system clock */ +uint32_t pic32_clock_get_system_clock(void); + +/* Function used to calculate the peripherals clock */ +uint32_t pic32_clock_get_peripheral_clock(void); + +/* Function used to calculate the appropriate brg param to approximate desidered frequency */ +uint32_t pic32_clock_calculate_brg(uint32_t mul, uint32_t desired); + +#endif /* __INCLUDE_PIC32_CLOCK_H__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_irq.h b/cpu/pic32/lib/pic32_irq.h new file mode 100644 index 000000000..d47bed11c --- /dev/null +++ b/cpu/pic32/lib/pic32_irq.h @@ -0,0 +1,79 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_irq.h + * \brief INTERRUPT interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-23 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#ifndef __INCLUDE_PIC32_IRQ_H__ +#define __INCLUDE_PIC32_IRQ_H__ + +#define ASM_DIS_INT \ +do { \ + asm volatile("di"); \ +} while(0) + +#define ASM_EN_INT \ +do { \ + asm volatile("ei"); \ +} while(0) + +#define TIMER_ISR(v) \ +void __attribute__((vector(v), interrupt(ipl7))) isr_##v(void) + +#define ISR(v) \ +void __attribute__((vector(v), interrupt(ipl6))) isr_##v(void) + +#endif /* __INCLUDE_PIC32_IRQ_H__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_spi.c b/cpu/pic32/lib/pic32_spi.c new file mode 100644 index 000000000..c495171fc --- /dev/null +++ b/cpu/pic32/lib/pic32_spi.c @@ -0,0 +1,186 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_spi.c + * \brief SPI interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-21 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#define __SPI_CODE_TEST__ 0 + +#if __SPI_CODE_TEST__ +#define __USE_SPI__ 1 +#define __USE_SPI_PORT1__ 1 +#define __USE_SPI_PORT1A__ 1 +#define __USE_SPI_PORT2A__ 1 +#define __USE_SPI_PORT3A__ 1 +#endif /* __SPI_CODE_TEST__ */ + +#ifdef __USE_SPI__ + +#include +#include +#include + +#include + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#define IS_MASTER(flags) ((flags) & SPI_MASTER) + +/*---------------------------------------------------------------------------*/ +#define SPI_PORT(XX, YY) \ + int8_t \ + pic32_spi##XX##_init(uint32_t baudrate, uint32_t flags) \ + { \ + \ + IEC##YY##CLR = _IEC##YY##_SPI##XX##EIE_MASK | \ + _IEC##YY##_SPI##XX##TXIE_MASK | \ + _IEC##YY##_SPI##XX##RXIE_MASK; \ + \ + IFS##YY##CLR = _IFS##YY##_SPI##XX##EIF_MASK | \ + _IFS##YY##_SPI##XX##TXIF_MASK | \ + _IFS##YY##_SPI##XX##RXIF_MASK; \ + \ + SPI##XX##BRG = pic32_clock_calculate_brg(2, baudrate); \ + \ + SPI##XX##CON = 0; \ + \ + /* Flag parsing */ \ + \ + if(!IS_MASTER(flags)) { \ + return -SPI_ERR_UNIMPLEMENTED; \ + } \ + \ + SPI##XX##CON = flags | _SPI##XX##CON_ON_MASK; \ + \ + return SPI_NO_ERRORS; \ + } \ + \ + int8_t \ + pic32_spi##XX##_close() \ + { \ + SPI##XX##CONCLR = _SPI##XX##CON_ON_MASK; \ + return SPI_NO_ERRORS; \ + } \ + \ + int8_t \ + pic32_spi##XX##_write(const uint8_t *data, uint32_t len) \ + { \ + uint32_t i; \ + uint32_t dummy; \ + \ + for(i = 0; i < len; ++i) { \ + SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \ + while(!SPI##XX##STATbits.SPITBE) { \ + ; \ + } \ + ASM_DIS_INT; /* fix errata 44 */ \ + SPI##XX##BUF = data[i]; \ + ASM_EN_INT; /* fix errata 44 */ \ + while(!SPI##XX##STATbits.SPIRBF) { \ + ; \ + } \ + SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \ + dummy = SPI##XX##BUF; \ + } \ + \ + return SPI_NO_ERRORS; \ + } \ + \ + int8_t \ + pic32_spi##XX##_read(uint8_t *data, uint32_t len) \ + { \ + uint32_t i; \ + \ + for(i = 0; i < len; ++i) { \ + SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \ + while(!SPI##XX##STATbits.SPITBE) { \ + ; \ + } \ + SPI##XX##BUF = 0; \ + while(!SPI##XX##STATbits.SPIRBF) { \ + ; \ + } \ + SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \ + data[i] = SPI##XX##BUF & 0x00FF; \ + } \ + \ + return SPI_NO_ERRORS; \ + } +/*---------------------------------------------------------------------------*/ + +#ifdef __USE_SPI_PORT1__ +SPI_PORT(1, 0) +#endif /* __USE_SPI_PORT1__ */ + +#ifdef __USE_SPI_PORT1A__ +SPI_PORT(1A, 0) +#endif /* __USE_SPI_PORT1A__ */ + +#ifdef __USE_SPI_PORT2A__ +SPI_PORT(2A, 1) +#endif /* __USE_SPI_PORT2A__ */ + +#ifdef __USE_SPI_PORT3A__ +SPI_PORT(3A, 1) +#endif /* __USE_SPI_PORT3A__ */ + +#endif /* __USE_SPI__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_spi.h b/cpu/pic32/lib/pic32_spi.h new file mode 100644 index 000000000..cbf6600d2 --- /dev/null +++ b/cpu/pic32/lib/pic32_spi.h @@ -0,0 +1,114 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_spi.h + * \brief SPI interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-21 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#ifndef __INCLUDE_PIC32_SPI_H__ +#define __INCLUDE_PIC32_SPI_H__ + +#ifdef __USE_SPI__ + +#include + +#include + +/* Returned Messages */ +#define SPI_NO_ERRORS 0 +#define SPI_ERR_BAD_PORT 1 +#define SPI_ERR_BAD_ARGS 2 +#define SPI_ERR_BUSY 3 +#define SPI_ERR_UNIMPLEMENTED 10 + +/* + * Configuration Flags + * NOTE: this work under the assumption that all the SPI ports have the same + * control register specification. See pic32mx family reference manual. + */ + +#define SPI_MASTER 0b00000000000000000000000000100000 +#define SPI_CLOCK_IDLE_HIGH 0b00000000000000000000000001000000 +#define SPI_SDO_ON_CLOCK_TO_IDLE 0b00000000000000000000000100000000 +#define SPI_SDI_ON_CLOCK_END 0b00000000000000000000001000000000 + +/* Other Flags */ +#define SPI_DEFAULT (SPI_MASTER | SPI_SDO_ON_CLOCK_TO_IDLE) + +#define SPI_DEF(XX) \ + int8_t pic32_spi##XX##_init(uint32_t baudrate, uint32_t flags); \ + int8_t pic32_spi##XX##_close(); \ + int8_t pic32_spi##XX##_write(const uint8_t *data, uint32_t len);\ + int8_t pic32_spi##XX##_read(uint8_t *data, uint32_t len); + +#ifdef __USE_SPI_PORT1__ +SPI_DEF(1) +#endif /* __USE_SPI_PORT1__ */ + +#ifdef __USE_SPI_PORT1A__ +SPI_DEF(1A) +#endif /* __USE_SPI_PORT1A__ */ + +#ifdef __USE_SPI_PORT2A__ +SPI_DEF(2A) +#endif /* __USE_SPI_PORT2A__ */ + +#ifdef __USE_SPI_PORT3A__ +SPI_DEF(3A) +#endif /* __USE_SPI_PORT3A__ */ + +#endif /* __USE_SPI__ */ + +#endif /* __INCLUDE_PIC32_SPI_H__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_timer.c b/cpu/pic32/lib/pic32_timer.c new file mode 100644 index 000000000..f1284723b --- /dev/null +++ b/cpu/pic32/lib/pic32_timer.c @@ -0,0 +1,283 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_timer.c + * \brief TIMER interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-26 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#define __TIMER_CODE_TEST__ 0 + +#if __TIMER_CODE_TEST__ +#define __USE_TIMER__ 1 +#define __USE_TIMER_1__ 1 +#define __USE_TIMER_2__ 1 +#define __USE_TIMER_3__ 1 +#define __USE_TIMER_4__ 1 +#define __USE_TIMER_5__ 1 +#define __USE_TIMER_23__ 1 +#define __USE_TIMER_45__ 1 +#endif /* __TIMER_CODE_TEST__ */ + +#ifdef __USE_TIMER__ + +#include +#include +#include + +#include + +#include +#include + +/*---------------------------------------------------------------------------*/ +#define TIMERN_16(XX, TT, PP) \ + void \ + pic32_timer##XX##_enable_irq(void) \ + { \ + IFS0CLR = _IFS0_T##XX##IF_MASK; /* Clean Timer IRQ Flag */ \ + IEC0SET = _IEC0_T##XX##IE_MASK; /* Enable Timer IRQ */ \ + } \ + \ + void \ + pic32_timer##XX##_disable_irq(void) \ + { \ + IEC0CLR = _IEC0_T##XX##IE_MASK; /* Disable Timer IRQ */ \ + IFS0CLR = _IFS0_T##XX##IF_MASK; /* Clean Timer IRQ Flag */ \ + } \ + \ + uint8_t \ + pic32_timer##XX##_init(uint32_t frequency) \ + { \ + uint32_t prd = pic32_clock_get_peripheral_clock() / frequency; \ + uint8_t tckps; /* Prescale */ \ + uint8_t ok = 0; \ + \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_##TT##_PRESCALE_1; \ + ok = 1; \ + } else { \ + prd = prd / 8; \ + } \ + \ + if(ok == 0) { \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_##TT##_PRESCALE_8; \ + ok = 1; \ + } else { \ + prd = prd / 8; \ + } \ + } \ + \ + if(ok == 0) { \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_##TT##_PRESCALE_64; \ + ok = 1; \ + } else { \ + prd = prd / 4; \ + } \ + } \ + \ + if(ok == 0) { \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_##TT##_PRESCALE_256; \ + } else { \ + return -TIMER_ERR_BAD_ARGS; \ + } \ + } \ + \ + pic32_timer##XX##_disable_irq(); \ + \ + IPC##XX##CLR = _IPC##XX##_T##XX##IP_MASK | _IPC##XX##_T##XX##IS_MASK; \ + IPC##XX##SET = (7 << _IPC##XX##_T##XX##IP_POSITION) | (PP << _IPC##XX##_T##XX##IS_POSITION); \ + T##XX##CON = 0; \ + T##XX##CONSET = tckps << _T##XX##CON_TCKPS_POSITION; \ + PR##XX = prd; \ + TMR##XX = 0; \ + \ + return TIMER_NO_ERRORS; \ + } \ + \ + void \ + pic32_timer##XX##_start(void) \ + { \ + T##XX##CONSET = _T##XX##CON_ON_MASK; /* Start Timer */ \ + } \ + \ + void \ + pic32_timer##XX##_stop(void) \ + { \ + T##XX##CONCLR = _T##XX##CON_ON_MASK; /* Stop Timer */ \ + } \ + \ + uint16_t \ + pic32_timer##XX##_get_val(void) \ + { \ + return TMR##XX; \ + } +/*---------------------------------------------------------------------------*/ +#define TIMERN_32(XX, YY, PP) \ + \ + void \ + pic32_timer##XX##YY##_enable_irq(void) \ + { \ + pic32_timer##YY##_enable_irq(); \ + } \ + \ + void \ + pic32_timer##XX##YY##_disable_irq(void) \ + { \ + pic32_timer##YY##_disable_irq(); \ + } \ + \ + uint8_t \ + pic32_timer##XX##YY##_init(uint32_t frequency) \ + { \ + uint32_t prd = pic32_clock_get_peripheral_clock() / frequency; \ + uint8_t tckps; /* Prescale */ \ + uint8_t ok = 0; \ + \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_B_PRESCALE_1; \ + ok = 1; \ + } else { \ + prd = prd / 8; \ + } \ + \ + if(ok == 0) { \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_B_PRESCALE_8; \ + ok = 1; \ + } else { \ + prd = prd / 8; \ + } \ + } \ + \ + if(ok == 0) { \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_B_PRESCALE_64; \ + ok = 1; \ + } else { \ + prd = prd / 4; \ + } \ + } \ + \ + if(ok == 0) { \ + if(prd <= UINT16_MAX) { \ + tckps = TIMER_B_PRESCALE_256; \ + } else { \ + return -TIMER_ERR_BAD_ARGS; \ + } \ + } \ + \ + pic32_timer##XX##_disable_irq(); \ + \ + IPC##YY##CLR = _IPC##YY##_T##YY##IP_MASK | _IPC##YY##_T##YY##IS_MASK; \ + IPC##YY##SET = (7 << _IPC##YY##_T##YY##IP_POSITION) | (PP << _IPC##YY##_T##YY##IS_POSITION); \ + T##XX##CON = 0; \ + T##XX##CONSET = _T##XX##CON_T32_MASK | (tckps << _T##XX##CON_TCKPS_POSITION); \ + PR##XX = prd; \ + TMR##XX = 0; \ + \ + return TIMER_NO_ERRORS; \ + } \ + \ + void \ + pic32_timer##XX##YY##_start(void) \ + { \ + T##XX##CONSET = _T##XX##CON_ON_MASK; /* Start Timer */ \ + } \ + \ + void \ + pic32_timer##XX##YY##_stop(void) \ + { \ + T##XX##CONCLR = _T##XX##CON_ON_MASK; /* Stop Timer */ \ + } \ + \ + uint32_t \ + pic32_timer##XX##YY##_get_val(void) \ + { \ + return TMR##XX; \ + } +/*---------------------------------------------------------------------------*/ + +#ifdef __USE_TIMER_1__ +TIMERN_16(1, A, 3) +#endif /* __USE_TIMER_1__ */ + +#if defined(__USE_TIMER_2__) || defined(__USE_TIMER_23__) +TIMERN_16(2, B, 2) +#endif /* __USE_TIMER_2__ */ + +#if defined(__USE_TIMER_3__) || defined(__USE_TIMER_23__) +TIMERN_16(3, B, 1) +#endif /* __USE_TIMER_3__ */ + +#if defined(__USE_TIMER_4__) || defined(__USE_TIMER_45__) +TIMERN_16(4, B, 1) +#endif /* __USE_TIMER_4__ */ + +#if defined(__USE_TIMER_5__) || defined(__USE_TIMER_45__) +TIMERN_16(5, B, 1) +#endif /* __USE_TIMER_5__ */ + +#ifdef __USE_TIMER_23__ +TIMERN_32(2, 3, 3) +#endif /* __USE_TIMER_23__ */ + +#ifdef __USE_TIMER_45__ +TIMERN_32(4, 5, 2) +#endif /* __USE_TIMER_45__ */ + +#endif /* __USE_TIMER__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_timer.h b/cpu/pic32/lib/pic32_timer.h new file mode 100644 index 000000000..bdb0f9b4f --- /dev/null +++ b/cpu/pic32/lib/pic32_timer.h @@ -0,0 +1,143 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_timer.h + * \brief TIMER interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-26 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#ifndef __INCLUDE_PIC32_TIMER_H__ +#define __INCLUDE_PIC32_TIMER_H__ + +#ifdef __USE_TIMER__ + +#include + +#include + +#include + +#define TIMER_NO_ERRORS 0 +#define TIMER_ERR_BAD_ARGS 1 + +#define TIMER_A_PRESCALE_1 0b00 +#define TIMER_A_PRESCALE_8 0b01 +#define TIMER_A_PRESCALE_64 0b10 +#define TIMER_A_PRESCALE_256 0b11 + +#define TIMER_B_PRESCALE_1 0b000 +#define TIMER_B_PRESCALE_2 0b001 +#define TIMER_B_PRESCALE_4 0b010 +#define TIMER_B_PRESCALE_8 0b011 +#define TIMER_B_PRESCALE_16 0b100 +#define TIMER_B_PRESCALE_32 0b101 +#define TIMER_B_PRESCALE_64 0b110 +#define TIMER_B_PRESCALE_256 0b111 + +#define TIMERN_16_DEF(X) \ + uint8_t pic32_timer##X##_init(uint32_t frequency); \ + void pic32_timer##X##_enable_irq(void); \ + void pic32_timer##X##_disable_irq(void); \ + void pic32_timer##X##_start(void); \ + void pic32_timer##X##_stop(void); \ + uint16_t pic32_timer##X##_get_val(void); + +#define TIMERN_32_DEF(XY) \ + uint8_t pic32_timer##XY##_init(uint32_t frequency);\ + void pic32_timer##XY##_enable_irq(void); \ + void pic32_timer##XY##_disable_irq(void); \ + void pic32_timer##XY##_start(void); \ + void pic32_timer##XY##_stop(void); \ + uint32_t pic32_timer##XY##_get_val(void); + +#define TIMER_INTERRUPT(XX, CALLBACK) \ + TIMER_ISR(_TIMER_##XX##_VECTOR) \ + { \ + ENERGEST_ON(ENERGEST_TYPE_IRQ); \ + CALLBACK(); \ + ENERGEST_OFF(ENERGEST_TYPE_IRQ); \ + IFS0CLR = _IFS0_T##XX##IF_MASK; \ + } + +#ifdef __USE_TIMER_1__ +TIMERN_16_DEF(1) +#endif /* __USE_TIMER_1__ */ + +#if defined(__USE_TIMER_2__) || defined(__USE_TIMER_23__) +TIMERN_16_DEF(2) +#endif /* __USE_TIMER_2__ */ + +#if defined(__USE_TIMER_3__) || defined(__USE_TIMER_23__) +TIMERN_16_DEF(3) +#endif /* __USE_TIMER_3__ */ + +#if defined(__USE_TIMER_4__) || defined(__USE_TIMER_45__) +TIMERN_16_DEF(4) +#endif /* __USE_TIMER_4__ */ + +#if defined(__USE_TIMER_5__) || defined(__USE_TIMER_45__) +TIMERN_16_DEF(5) +#endif /* __USE_TIMER_5__ */ + +#ifdef __USE_TIMER_23__ +TIMERN_32_DEF(23) +#endif /* __USE_TIMER_23__ */ + +#ifdef __USE_TIMER_45__ +TIMERN_32_DEF(45) +#endif /* __USE_TIMER_45__ */ + +#endif /* __USE_TIMER__ */ + +#endif /* __INCLUDE_PIC32_TIMER_H__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_uart.c b/cpu/pic32/lib/pic32_uart.c new file mode 100644 index 000000000..3b08f970b --- /dev/null +++ b/cpu/pic32/lib/pic32_uart.c @@ -0,0 +1,193 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_uart.c + * \brief UART Interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-21 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#define __UART_CODE_TEST__ 0 + +#if __UART_CODE_TEST__ +#define __USE_UART__ 1 +#define __USE_UART_PORT1A__ 1 +#define __USE_UART_PORT1B__ 1 +#define __USE_UART_PORT2A__ 1 +#define __USE_UART_PORT2B__ 1 +#define __USE_UART_PORT3A__ 1 +#define __USE_UART_PORT3B__ 1 +#endif /* __UART_CODE_TEST__ */ + +#ifdef __USE_UART__ + +#include +#include +#include + +#include + +#include "contiki.h" +#include "lib/ringbuf.h" + +/*---------------------------------------------------------------------------*/ +#define UART_PORT_INIT_XA(XX, YY, ZZ) \ + int8_t \ + pic32_uart##XX##A_init(uint32_t baudrate, uint16_t byte_format) \ + { \ + /* Disable Interrupts: RX, TX, ERR */ \ + IEC##ZZ##CLR = _IEC##ZZ##_U##XX##ARXIE_MASK; \ + IFS##ZZ##CLR = _IFS##ZZ##_U##XX##AEIF_MASK | _IFS##ZZ##_U##XX##ATXIF_MASK | _IFS##ZZ##_U##XX##ARXIF_MASK; \ + \ + /* Clear thant Set Pri and Sub priority */ \ + IPC##YY##CLR = _IPC##YY##_U##XX##AIP_MASK | _IPC##YY##_U##XX##AIS_MASK; \ + IPC##YY##SET = (6 << _IPC##YY##_U##XX##AIP_POSITION) | (0 << _IPC##YY##_U##XX##AIS_POSITION); \ + \ + /* Mode Register Reset (this also stops UART) */ \ + U##XX##AMODE = 0; \ + \ + /* Use BRGH = 1: 4 divisor */ \ + U##XX##AMODESET = _U##XX##AMODE_BRGH_MASK; \ + U##XX##ABRG = pic32_clock_calculate_brg(4, baudrate); \ + \ + U##XX##AMODESET = byte_format & 0x07; /* Number of bit, Parity and Stop bits */ \ + \ + /* Status bits */ \ + U##XX##ASTA = 0; /* TX & RX interrupt modes */ \ + U##XX##ASTASET = _U##XX##ASTA_UTXEN_MASK | _U##XX##ASTA_URXEN_MASK; /* Enable TX, RX */ \ + \ + IEC##ZZ##SET = _IEC##ZZ##_U##XX##ARXIE_MASK; \ + \ + /* Enable UART port */ \ + U##XX##AMODESET = _U##XX##AMODE_UARTEN_MASK; \ + \ + return UART_NO_ERROR; \ + } +/*---------------------------------------------------------------------------*/ +#define UART_PORT_INIT_XB(XX, YY, ZZ) \ + int8_t \ + pic32_uart##XX##B_init(uint32_t baudrate, uint16_t byte_format) \ + { \ + /* Disable Interrupts: RX, TX, ERR */ \ + IEC##ZZ##CLR = _IEC##ZZ##_U##XX##BRXIE_MASK; \ + IFS##ZZ##CLR = _IFS##ZZ##_U##XX##BEIF_MASK | _IFS##ZZ##_U##XX##BTXIF_MASK | _IFS##ZZ##_U##XX##BRXIF_MASK; \ + \ + /* Clear thant Set Pri and Sub priority */ \ + IPC##YY##CLR = _IPC##YY##_U##XX##BIP_MASK | _IPC##YY##_U##XX##BIS_MASK; \ + IPC##YY##SET = (6 << _IPC##YY##_U##XX##BIP_POSITION) | (0 << _IPC##YY##_U##XX##BIS_POSITION); \ + \ + /* Mode Register Reset (this also stops UART) */ \ + U##XX##BMODE = 0; \ + \ + /* Use BRGH = 1: 4 divisor */ \ + U##XX##BMODESET = _U##XX##BMODE_BRGH_MASK; \ + U##XX##BBRG = pic32_clock_calculate_brg(4, baudrate); \ + \ + U##XX##BMODESET = byte_format & 0x07; /* Number of bit, Parity and Stop bits */ \ + \ + /* Status bits */ \ + U##XX##BSTA = 0; /* TX & RX interrupt modes */ \ + U##XX##BSTASET = _U##XX##BSTA_UTXEN_MASK | _U##XX##BSTA_URXEN_MASK; /* Enable TX, RX */ \ + \ + IEC##ZZ##SET = _IEC##ZZ##_U##XX##BRXIE_MASK; \ + \ + /* Enable UART port */ \ + U##XX##BMODESET = _U##XX##BMODE_UARTEN_MASK; \ + \ + return UART_NO_ERROR; \ + } +/*---------------------------------------------------------------------------*/ +#define UART_PORT(XX, YY) \ + \ + int8_t \ + pic32_uart##XX##_write(uint8_t data) \ + { \ + while (U##XX##STAbits.UTXBF); \ + U##XX##TXREG = data; \ + while (!U##XX##STAbits.TRMT); \ + \ + return UART_NO_ERROR; \ + } +/*---------------------------------------------------------------------------*/ + +#ifdef __USE_UART_PORT1A__ +UART_PORT(1A, 0) +UART_PORT_INIT_XA(1, 6, 0) +#endif /* __USE_UART_PORT1A__ */ + +#ifdef __USE_UART_PORT1B__ +UART_PORT(1B, 2) +UART_PORT_INIT_XB(1, 12, 2) +#endif /* __USE_UART_PORT1B__ */ + +#ifdef __USE_UART_PORT2A__ +UART_PORT(2A, 1) +UART_PORT_INIT_XA(2, 7, 1) +#endif /* __USE_UART_PORT2A__ */ + +#ifdef __USE_UART_PORT2B__ +UART_PORT(2B, 2) +UART_PORT_INIT_XB(2, 12, 2) +#endif /* __USE_UART_PORT2B__ */ + +#ifdef __USE_UART_PORT3A__ +UART_PORT(3A, 1) +UART_PORT_INIT_XA(3, 8, 1) +#endif /* __USE_UART_PORT3A__ */ + +#ifdef __USE_UART_PORT3B__ +UART_PORT(3B, 2) +UART_PORT_INIT_XB(3, 12, 2) +#endif /* __USE_UART_PORT3B__ */ + +#endif /* __USE_UART__ */ + +/** @} */ diff --git a/cpu/pic32/lib/pic32_uart.h b/cpu/pic32/lib/pic32_uart.h new file mode 100644 index 000000000..0821d5489 --- /dev/null +++ b/cpu/pic32/lib/pic32_uart.h @@ -0,0 +1,125 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32_uart.h + * \brief UART Interface for PIC32MX (pic32mx795f512l) + * \author Giovanni Pellerano + * \date 2012-03-21 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#ifndef __INCLUDE_PIC32_UART_H__ +#define __INCLUDE_PIC32_UART_H__ + +#ifdef __USE_UART__ + +#include + +#include + +#include + +#define BAUD2UBR(x) x + +/* Returned Messages */ +#define UART_NO_ERROR 0 +#define UART_ERR_NO_DATA 1 +#define UART_ERR_OVERFLOW 2 + +#define UART_PORT_DEF(XX) \ + int8_t pic32_uart##XX##_init(uint32_t baudrate, uint16_t byte_format); \ + int8_t pic32_uart##XX##_write(uint8_t data); + +#define UART_INTERRUPT(XX, Y, CALLBACK) \ + ISR(_UART_##XX##_VECTOR) \ + { \ + volatile uint8_t byte; \ + if (IFS##Y##bits.U##XX##RXIF) { \ + if ((U##XX##STAbits.PERR == 0) && (U##XX##STAbits.FERR == 0)) { \ + CALLBACK(U##XX##RXREG); \ + } else { \ + byte = U##XX##RXREG; /* NULL READ */ \ + } \ + IFS##Y##CLR = _IFS##Y##_U##XX##RXIF_MASK; \ + } \ + if (U##XX##STAbits.OERR) { \ + U##XX##STACLR = _U##XX##STA_OERR_MASK; \ + } \ + } + +#ifdef __USE_UART_PORT1A__ +UART_PORT_DEF(1A) +#endif /* __USE_UART_PORT1A__ */ + +#ifdef __USE_UART_PORT1B__ +UART_PORT_DEF(1B) +#endif /* __USE_UART_PORT1B__ */ + +#ifdef __USE_UART_PORT2A__ +UART_PORT_DEF(2A) +#endif /* __USE_UART_PORT2A__ */ + +#ifdef __USE_UART_PORT2B__ +UART_PORT_DEF(2B) +#endif /* __USE_UART_PORT2B__ */ + +#ifdef __USE_UART_PORT3A__ +UART_PORT_DEF(3A) +#endif /* __USE_UART_PORT3A__ */ + +#ifdef __USE_UART_PORT3B__ +UART_PORT_DEF(3B) +#endif /* __USE_UART_PORT3B__ */ + +#endif /* __USE_UART__ */ + +#endif /* __INCLUDE_PIC32_UART_H__ */ + +/** @} */ diff --git a/cpu/pic32/mtarch.h b/cpu/pic32/mtarch.h new file mode 100644 index 000000000..433af7ee1 --- /dev/null +++ b/cpu/pic32/mtarch.h @@ -0,0 +1,59 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file mtarch.h + * \brief Implementation of multithreading in PIC32. To be done. + * \author Giovanni Pellerano + * \date 2012-03-23 + */ + +#ifndef __MTARCH_H__ +#define __MTARCH_H__ + +struct mtarch_thread { + short mt_thread; +}; + +#endif /* __MTARCH_H__ */ + +/** @} */ diff --git a/cpu/pic32/pic32.c b/cpu/pic32/pic32.c new file mode 100644 index 000000000..f5a8ed796 --- /dev/null +++ b/cpu/pic32/pic32.c @@ -0,0 +1,137 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32-contiki-port PIC32 Contiki Port + * + * @{ + */ + +/** + * \file mtarch.h + * \brief PIC32MX initialization routines + * \author Giovanni Pellerano + * \author Daniele Alessandrelli + * \date 2012-03-23 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#include + +#include +#include +#include + +#include + +/* General Excepiton Handler: overrides the default handler */ +static enum { + EXCEP_IRQ = 0, /* interrupt */ + EXCEP_AdEL = 4, /* address error exception (load or ifetch) */ + EXCEP_AdES, /* address error exception (store) */ + EXCEP_IBE, /* bus error (ifetch) */ + EXCEP_DBE, /* bus error (load/store) */ + EXCEP_Sys, /* syscall */ + EXCEP_Bp, /* breakpoint */ + EXCEP_RI, /* reserved instruction */ + EXCEP_CpU, /* coprocessor unusable */ + EXCEP_Overflow, /* arithmetic overflow */ + EXCEP_Trap, /* trap (possible divide by zero) */ + EXCEP_IS1 = 16, /* implementation specfic 1 */ + EXCEP_CEU, /* CorExtend unuseable */ + EXCEP_C2E /* coprocessor 2 */ +} cp0_exception_code; + +static unsigned int cp0_exception_cause; /* CP0: CAUSE register */ +static unsigned int cp0_exception_epc; /* CP0: Exception Program Counter */ + +/*---------------------------------------------------------------------------*/ +void +pic32_init(void) +{ + unsigned long int r; + + ASM_DIS_INT; + + /* Disable JTAG Port */ + DDPCONbits.JTAGEN = 0; + + // set the CP0 cause IV bit high + asm volatile("mfc0 %0,$13" : "=r"(r)); + r |= 0x00800000; + asm volatile("mtc0 %0,$13" : "+r"(r)); + + INTCONSET = _INTCON_MVEC_MASK; + + SYSKEY = 0; + SYSKEY = 0xaa996655; + SYSKEY = 0x556699aa; + + /* Enable Sleep Mode */ + OSCCONCLR = 1 << _OSCCON_SLPEN_POSITION; + + SYSKEY = 0; + + ASM_EN_INT; +} + + +/*---------------------------------------------------------------------------*/ +void +_general_exception_handler(void) +{ + asm volatile ("mfc0 %0,$14":"=r" (cp0_exception_epc)); + + asm volatile ("mfc0 %0,$13":"=r" (cp0_exception_cause)); + + cp0_exception_code = (cp0_exception_cause >> 2) & 0x0000001F; + + leds_on(LEDS_ALL); + + while(1){ + ; + } +} +/*---------------------------------------------------------------------------*/ + +/** @} */ diff --git a/cpu/pic32/pic32.h b/cpu/pic32/pic32.h new file mode 100644 index 000000000..f11d19c80 --- /dev/null +++ b/cpu/pic32/pic32.h @@ -0,0 +1,99 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \defgroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file pic32.h + * \brief PIC32MX initialization routines + * \author Giovanni Pellerano + * \date 2012-03-23 + */ + +#ifndef __INCLUDE_PIC32_PIC32_H__ +#define __INCLUDE_PIC32_PIC32_H__ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +/* Initial Configuration + * + * Main Clock -> SYSCLK = (Crystal_Freq / FPLLIDIV) * FPLLMUL / FPLLODIV + * Peripheral Clock -> PBCLK = SYSCLK / FPBDIV + * + * SYSCLK = 80MHz, PBCLK = 40MHz +*/ + +#include + +#pragma config FNOSC = PRIPLL // Oscillator Selection +#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (PIC32 Starter Kit: use divide by 2 only) +#pragma config FPLLMUL = MUL_20 // PLL Multiplier +#pragma config FPLLODIV = DIV_1 // PLL Output Divider +#pragma config FPBDIV = DIV_1 // Peripheral Clock divisor +#pragma config FWDTEN = OFF // Watchdog Timer +#pragma config WDTPS = PS32768 // Watchdog Timer Postscale +#pragma config FCKSM = CSDCMD // Clock Switching & Fail Safe Clock Monitor +#pragma config OSCIOFNC = OFF // CLKO Enable +#pragma config POSCMOD = HS // Primary Oscillator +#pragma config IESO = OFF // Internal/External Switch-over +#pragma config FSOSCEN = OFF // Secondary Oscillator Enable +#pragma config CP = OFF // Code Protect +#pragma config BWP = OFF // Boot Flash Write Protect +#pragma config PWP = OFF // Program Flash Write Protect +#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select +#pragma config DEBUG = OFF // Debugger Disabled for Starter Kit +#pragma config FSRSSEL = PRIORITY_7 + +/* external PHY in RMII/default configuration */ + +#pragma config FMIIEN = OFF +#pragma config FETHIO = ON + +void pic32_init(void); + +#endif /* __INCLUDE_PIC32_PIC32_H__ */ + +/** @} */ diff --git a/cpu/pic32/rtimer-arch.c b/cpu/pic32/rtimer-arch.c new file mode 100644 index 000000000..4a888dc8e --- /dev/null +++ b/cpu/pic32/rtimer-arch.c @@ -0,0 +1,112 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file rtimer-arch.c + * \brief PIC32MX RTIMER routines + * \author Giovanni Pellerano + * \date 2012-04-11 + */ + +/* + * PIC32MX795F512L - Specific Functions + * + * All the functions in this part of the file are specific for the + * pic32mx795f512l that is characterized by registers' name that differ from + * the 3xx and 4xx families of the pic32mx. + */ + +#include + +#include "contiki.h" + +#include +#include + +#include + +#include + +static uint32_t schedule; + +/*---------------------------------------------------------------------------*/ +void +rtimer_callback(void) +{ + pic32_timer23_disable_irq(); + TMR2 = schedule; + + rtimer_run_next(); +} +/*---------------------------------------------------------------------------*/ +void +rtimer_arch_init(void) +{ + pic32_timer23_disable_irq(); + IPC3CLR = _IPC3_T3IP_MASK | _IPC3_T3IS_MASK; + IPC3SET = (7 << _IPC3_T3IP_POSITION) | (3 << _IPC3_T3IS_POSITION); + T2CON = 0; + T3CON = 0; + T2CONSET = _T2CON_T32_MASK | (TIMER_B_PRESCALE_256 << _T2CON_TCKPS_POSITION); + PR2 = 0xFFFFFFFF; + TMR2 = 0; + pic32_timer23_start(); +} +/*---------------------------------------------------------------------------*/ +rtimer_clock_t +rtimer_arch_now(void) +{ + return TMR2; +} +/*---------------------------------------------------------------------------*/ +void +rtimer_arch_schedule(rtimer_clock_t t) +{ + schedule = t; + PR2 = t; + pic32_timer23_enable_irq(); +} +/*---------------------------------------------------------------------------*/ + +TIMER_INTERRUPT(3, rtimer_callback); + +/** @} */ diff --git a/cpu/pic32/rtimer-arch.h b/cpu/pic32/rtimer-arch.h new file mode 100644 index 000000000..2be3702b5 --- /dev/null +++ b/cpu/pic32/rtimer-arch.h @@ -0,0 +1,63 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file rtimer-arch.h + * \brief PIC32MX RTIMER routines + * \author Giovanni Pellerano + * \date 2012-04-11 + */ + +#ifndef __RTIMER_ARCH_H__ +#define __RTIMER_ARCH_H__ + +#include "contiki-conf.h" + +#include + +rtimer_clock_t rtimer_arch_now(void); + +#define RTIMER_ARCH_SECOND 312500 + +#endif /* __RTIMER_ARCH_H__ */ + +/** @} */ diff --git a/cpu/pic32/slip-uart.c b/cpu/pic32/slip-uart.c new file mode 100644 index 000000000..3354c0905 --- /dev/null +++ b/cpu/pic32/slip-uart.c @@ -0,0 +1,96 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file slip-uart.c + * \brief PIC32MX Slip interface routines + * \author Giovanni Pellerano + * \date 2012-03-23 + */ + +#include "contiki.h" +#include "dev/slip.h" + +#include + +#define DEBUG 1 +#if DEBUG +#include +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif + +#define SLIP_UART(XX) \ + void \ + slip_arch_writeb(unsigned char c) \ + { \ + pic32_uart##XX##_write(c); \ + } \ + \ + void \ + slip_arch_init(unsigned long ubr) \ + { \ + pic32_uart##XX##_init(ubr, 0); \ + \ + PRINTF("Initializing slip uart: %lubps\n", ubr); \ + } \ + \ + UART_INTERRUPT(XX, 0, slip_input_byte); + +#ifdef __USE_UART_PORT1A_FOR_SLIP__ +SLIP_UART(1A); +#elif defined __USE_UART_PORT1B_FOR_SLIP__ +SLIP_UART(1B); +#elif defined __USE_UART_PORT2A_FOR_SLIP__ +SLIP_UART(2A); +#elif defined __USE_UART_PORT2B_FOR_SLIP__ +SLIP_UART(2B); +#elif defined __USE_UART_PORT3A_FOR_SLIP__ +SLIP_UART(3A); +#elif defined __USE_UART_PORT3B_FOR_SLIP__ +SLIP_UART(3B); +#else +SLIP_UART(1A); +#endif + +/** @} */ diff --git a/cpu/pic32/watchdog.c b/cpu/pic32/watchdog.c new file mode 100644 index 000000000..fd5654fe5 --- /dev/null +++ b/cpu/pic32/watchdog.c @@ -0,0 +1,109 @@ +/* + * Contiki PIC32 Port project + * + * Copyright (c) 2012, + * Scuola Superiore Sant'Anna (http://www.sssup.it) and + * Consorzio Nazionale Interuniversitario per le Telecomunicazioni + * (http://www.cnit.it). + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \addtogroup pic32 PIC32 Contiki Port + * + * @{ + */ + +/** + * \file watchdog.c + * \brief PIC32MX Watchdog routines + * \author Giovanni Pellerano + * \date 2012-03-23 + */ + +#include "dev/watchdog.h" + +#include + +#include + +/*---------------------------------------------------------------------------*/ +void +watchdog_init(void) +{ + WDTCON = 0; + OSCCONSET = 0x10; /* Set power mode saving to Idle */ +} +/*---------------------------------------------------------------------------*/ +void +watchdog_start(void) +{ + WDTCONSET = _WDTCON_ON_MASK; +} +/*---------------------------------------------------------------------------*/ +void +watchdog_periodic(void) +{ + WDTCONSET = _WDTCON_WDTCLR_MASK; +} +/*---------------------------------------------------------------------------*/ +void +watchdog_stop(void) +{ + WDTCONCLR = _WDTCON_ON_MASK; +} +/*---------------------------------------------------------------------------*/ +void +watchdog_reboot(void) +{ + volatile int *p = (int *)&RSWRST; + + /* Unlock sequence */ + ASM_DIS_INT; + if(!(DMACONbits.SUSPEND)){ + DMACONSET=_DMACON_SUSPEND_MASK; // suspend + while((DMACONbits.DMABUSY)) { + ; // wait to be actually suspended + } + } + + SYSKEY = 0; + SYSKEY = 0xaa996655; + SYSKEY = 0x556699aa; + + RSWRSTSET=_RSWRST_SWRST_MASK; + *p; + + while(1) { + ; + } +} +/*---------------------------------------------------------------------------*/ + +/** @} */