added support for pic32 (PIC32MX795F512L)

This commit is contained in:
Giovanni `evilaliv3` Pellerano 2012-12-17 09:14:23 +01:00
parent 70e46f3248
commit f68df3a81c
21 changed files with 2414 additions and 0 deletions

134
cpu/pic32/lib/pic32_clock.c Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <pic32_clock.h>
#include <p32xxxx.h>
#include <stdint.h>
#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];
}
/** @} */

View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \date 2012-04-11
*/
#ifndef __INCLUDE_PIC32_CLOCK_H__
#define __INCLUDE_PIC32_CLOCK_H__
#include <stdint.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.
*/
/* 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__ */
/** @} */

79
cpu/pic32/lib/pic32_irq.h Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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__ */
/** @} */

186
cpu/pic32/lib/pic32_spi.c Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <pic32_spi.h>
#include <pic32_clock.h>
#include <pic32_irq.h>
#include <p32xxxx.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.
*/
#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__ */
/** @} */

114
cpu/pic32/lib/pic32_spi.h Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <p32xxxx.h>
#include <stdint.h>
/* 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__ */
/** @} */

283
cpu/pic32/lib/pic32_timer.c Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <pic32_timer.h>
#include <pic32_clock.h>
#include <pic32_irq.h>
#include <p32xxxx.h>
#include <stdint.h>
#include <stddef.h>
/*---------------------------------------------------------------------------*/
#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__ */
/** @} */

143
cpu/pic32/lib/pic32_timer.h Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <pic32_irq.h>
#include <p32xxxx.h>
#include <stdint.h>
#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__ */
/** @} */

193
cpu/pic32/lib/pic32_uart.c Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <pic32_uart.h>
#include <pic32_clock.h>
#include <pic32_irq.h>
#include <p32xxxx.h>
#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__ */
/** @} */

125
cpu/pic32/lib/pic32_uart.h Normal file
View file

@ -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 <giovanni.pellerano@evilaliv3.org>
* \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 <pic32_irq.h>
#include <p32xxxx.h>
#include <stdint.h>
#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__ */
/** @} */