diff --git a/cpu/mc1322x/dev/uart1.h b/cpu/mc1322x/dev/uart1.h new file mode 100644 index 000000000..3fdf31361 --- /dev/null +++ b/cpu/mc1322x/dev/uart1.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) and Contiki. + * + * 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. + * + * This file is part of the Contiki OS. + * + * $Id: uart1.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +/* + + This file exists as a work-around for the hardware dependant calls + to slip_arch_init. + + Current the prototype for slip_arch_init is slip_arch_init(urb) + + and a typical call is something like + slip_arch_init(BAUD2URB(115200)) + + BAUD2UBR is hardware specific, however. Furthermore, for the sky + platform it's typically defined with #include "dev/uart1.h" (see + rpl-boarder-router/slip-bridge.c), a sky specific file. dev/uart1.h + includes msp430.h which includes the sky contiki-conf.h which + defines BAUD2UBR. + + To me, the correct think to pass is simply the baudrate and have the + hardware specific conversion happen inside slip_arch_init. + + Notably, most implementations just ignore the passed parameter + anyway. (except AVR) + + */ + +#ifndef DEV_UART1_H +#define DEV_UART1_H + +#define BAUD2UBR(x) x + +#endif diff --git a/cpu/mc1322x/lib/Makefile.lib b/cpu/mc1322x/lib/Makefile.lib new file mode 100644 index 000000000..51ce030c0 --- /dev/null +++ b/cpu/mc1322x/lib/Makefile.lib @@ -0,0 +1,8 @@ +# -*- makefile -*- + +CFLAGS += -I$(LIBMC1322X)/include + +LIBOBJS = $(patsubst %.c,%.o,$(wildcard $(LIBMC1322X)/*.c)) + +$(LIBMC1322X)/libmc1322x.a: $(LIBOBJS) + $(AR) rcs $(LIBMC1322X)/libmc1322x.a $(LIBOBJS) diff --git a/cpu/mc1322x/lib/gpio.c b/cpu/mc1322x/lib/gpio.c new file mode 100644 index 000000000..4e2eb51b6 --- /dev/null +++ b/cpu/mc1322x/lib/gpio.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: gpio.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +inline void gpio_pad_dir(volatile uint64_t data) +{ + *GPIO_PAD_DIR0 = (data & 0xffffffff); + *GPIO_PAD_DIR1 = (data >> 32); +} + +inline void gpio_data(volatile uint64_t data) +{ + *GPIO_DATA0 = (data & 0xffffffff); + *GPIO_DATA1 = (data >> 32); +} + +inline uint64_t gpio_data_get(volatile uint64_t bits) { + uint64_t rdata = 0; + + rdata = *GPIO_DATA0 & (bits & 0xffffffff); + rdata |= (*GPIO_DATA1 & (bits >> 32)) << 32; + + return rdata; +} + +inline void gpio_pad_pu_en(volatile uint64_t data) +{ + *GPIO_PAD_PU_EN0 = (data & 0xffffffff); + *GPIO_PAD_PU_EN1 = (data >> 32); +} + +inline void gpio_data_sel(volatile uint64_t data) +{ + *GPIO_DATA_SEL0 = (data & 0xffffffff); + *GPIO_DATA_SEL1 = (data >> 32); +} + +inline void gpio_pad_pu_sel(volatile uint64_t data) +{ + *GPIO_PAD_PU_SEL0 = (data & 0xffffffff); + *GPIO_PAD_PU_SEL1 = (data >> 32); +} + +inline void gpio_data_set(volatile uint64_t data) +{ + *GPIO_DATA_SET0 = (data & 0xffffffff); + *GPIO_DATA_SET1 = (data >> 32); +} + +inline void gpio_data_reset(volatile uint64_t data) +{ + *GPIO_DATA_RESET0 = (data & 0xffffffff); + *GPIO_DATA_RESET1 = (data >> 32); +} + +inline void gpio_pad_dir_set(volatile uint64_t data) +{ + *GPIO_PAD_DIR_SET0 = (data & 0xffffffff); + *GPIO_PAD_DIR_SET1 = (data >> 32); +} + +inline void gpio_pad_dir_reset(volatile uint64_t data) +{ + *GPIO_PAD_DIR_RESET0 = (data & 0xffffffff); + *GPIO_PAD_DIR_RESET1 = (data >> 32); +} diff --git a/cpu/mc1322x/lib/include/crm.h b/cpu/mc1322x/lib/include/crm.h new file mode 100644 index 000000000..e2b71b99b --- /dev/null +++ b/cpu/mc1322x/lib/include/crm.h @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: crm.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef CRM_H +#define CRM_H + +#include + +#define CRM_BASE (0x80003000) +#define CRM_SYS_CNTL ((volatile uint32_t *) (CRM_BASE+0x00)) +#define CRM_WU_CNTL ((volatile uint32_t *) (CRM_BASE+0x04)) +#define CRM_SLEEP_CNTL ((volatile uint32_t *) (CRM_BASE+0x08)) +#define CRM_BS_CNTL ((volatile uint32_t *) (CRM_BASE+0x0c)) +#define CRM_COP_CNTL ((volatile uint32_t *) (CRM_BASE+0x10)) +#define CRM_COP_SERVICE ((volatile uint32_t *) (CRM_BASE+0x14)) +#define CRM_STATUS ((volatile uint32_t *) (CRM_BASE+0x18)) +#define CRM_MOD_STATUS ((volatile uint32_t *) (CRM_BASE+0x1c)) +#define CRM_WU_COUNT ((volatile uint32_t *) (CRM_BASE+0x20)) +#define CRM_WU_TIMEOUT ((volatile uint32_t *) (CRM_BASE+0x24)) +#define CRM_RTC_COUNT ((volatile uint32_t *) (CRM_BASE+0x28)) +#define CRM_RTC_TIMEOUT ((volatile uint32_t *) (CRM_BASE+0x2c)) +#define CRM_CAL_CNTL ((volatile uint32_t *) (CRM_BASE+0x34)) +#define CRM_CAL_COUNT ((volatile uint32_t *) (CRM_BASE+0x38)) +#define CRM_RINGOSC_CNTL ((volatile uint32_t *) (CRM_BASE+0x3c)) +#define CRM_XTAL_CNTL ((volatile uint32_t *) (CRM_BASE+0x40)) +#define CRM_XTAL32_CNTL ((volatile uint32_t *) (CRM_BASE+0x44)) +#define CRM_VREG_CNTL ((volatile uint32_t *) (CRM_BASE+0x48)) +#define CRM_SW_RST ((volatile uint32_t *) (CRM_BASE+0x50)) + +/* CRM_SYS_CNTL bit locations */ +#define XTAL32_EXISTS 5 + +/* CRM_WU_CNTL bit locations */ +#define EXT_WU_IEN 20 /* 4 bits */ +#define EXT_WU_EN 4 /* 4 bits */ +#define EXT_WU_EDGE 8 /* 4 bits */ +#define EXT_WU_POL 12 /* 4 bits */ +#define TIMER_WU_EN 0 +#define RTC_WU_EN 1 +#define TIMER_WU_IEN 16 +#define RTC_WU_IEN 17 + +/* CRM_STATUS bit locations */ +#define EXT_WU_EVT 4 /* 4 bits, rw1c */ +#define RTC_WU_EVT 3 /* rw1c */ + +/* RINGOSC_CNTL bit locations */ +#define ROSC_CTUNE 9 /* 4 bits */ +#define ROSC_FTUNE 4 /* 4 bits */ +#define ROSC_EN 0 + +#define ring_osc_on() (set_bit(*CRM_RINGOSC_CNTL,ROSC_EN)) +#define ring_osc_off() (clear_bit(*CRM_RINGOSC_CNTL,ROSC_EN)) + +#define REF_OSC 24000000ULL /* reference osc. frequency */ +#define NOMINAL_RING_OSC_SEC 2000 /* nominal ring osc. frequency */ +extern uint32_t cal_rtc_secs; /* calibrated 2khz rtc seconds */ + +/* XTAL32_CNTL bit locations */ +#define XTAL32_GAIN 4 /* 2 bits */ +#define XTAL32_EN 0 + +#define xtal32_on() (set_bit(*CRM_XTAL32_CNTL,XTAL32_EN)) +#define xtal32_off() (clear_bit(*CRM_XTAL32_CNTL,XTAL32_EN)) +#define xtal32_exists() (set_bit(*CRM_SYS_CNTL,XTAL32_EXISTS)) + +/* enable external wake-ups on kbi 4-7 */ +/* see kbi.h for other kbi specific macros */ +#define enable_ext_wu(kbi) (set_bit(*CRM_WU_CNTL,(EXT_WU_EN+kbi-4))) +#define disable_ext_wu(kbi) (clear_bit(*CRM_WU_CNTL,(EXT_WU_EN+kbi-4))) + +#define is_ext_wu_evt(kbi) (bit_is_set(*CRM_STATUS,(EXT_WU_EVT+kbi-4))) +#define clear_ext_wu_evt(kbi) (set_bit(*CRM_STATUS,(EXT_WU_EVT+kbi-4))) /* r1wc bit */ + +/* enable wake-up timer */ +#define enable_timer_wu_irq() ((set_bit(*CRM_WU_CNTL,(TIMER_WU_IEN)))) +#define disable_timer_wu_irq() ((clear_bit(*CRM_WU_CNTL,(TIMER_WU_IEN)))) + +#define enable_timer_wu() ((set_bit(*CRM_WU_CNTL,(TIMER_WU_EN)))) +#define disable_timer_wu() ((clear_bit(*CRM_WU_CNTL,(TIMER_WU_EN)))) + +/* enable wake-up from RTC compare */ +#define enable_rtc_wu_irq() (set_bit(*CRM_WU_CNTL,RTC_WU_IEN)) +#define disable_rtc_wu_irq() (clear_bit(*CRM_WU_CNTL,RTC_WU_IEN)) + +#define enable_rtc_wu() ((set_bit(*CRM_WU_CNTL,(RTC_WU_EN)))) +#define disable_rtc_wu() ((clear_bit(*CRM_WU_CNTL,(RTC_WU_EN)))) + +#define clear_rtc_wu_evt() (set_bit(*CRM_STATUS,RTC_WU_EVT)) +#define rtc_wu_evt() (bit_is_set(*CRM_STATUS,RTC_WU_EVT)) + +#define SLEEP_MODE_HIBERNATE bit(0) +#define SLEEP_MODE_DOZE bit(1) + +#define SLEEP_PAD_PWR bit(7) +#define SLEEP_RETAIN_MCU bit(6) +#define sleep_ram_retain(x) (x<<4) /* 0-3 */ +#define SLEEP_RAM_8K sleep_ram_retain(0) +#define SLEEP_RAM_32K sleep_ram_retain(1) +#define SLEEP_RAM_64K sleep_ram_retain(2) +#define SLEEP_RAM_96K sleep_ram_retain(3) + +#define pack_XTAL_CNTL(ctune4pf, ctune, ftune, ibias) \ + (*CRM_XTAL_CNTL = ((ctune4pf << 25) | (ctune << 21) | ( ftune << 16) | (ibias << 8) | 0x52)) + +#endif diff --git a/cpu/mc1322x/lib/include/gpio.h b/cpu/mc1322x/lib/include/gpio.h new file mode 100644 index 000000000..e4e2ff35e --- /dev/null +++ b/cpu/mc1322x/lib/include/gpio.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: gpio.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef GPIO_H +#define GPIO_H + +#define GPIO_PAD_DIR0 ((volatile uint32_t *) 0x80000000) +#define GPIO_PAD_DIR1 ((volatile uint32_t *) 0x80000004) +#define GPIO_DATA0 ((volatile uint32_t *) 0x80000008) +#define GPIO_DATA1 ((volatile uint32_t *) 0x8000000c) +#define GPIO_PAD_PU_EN0 ((volatile uint32_t *) 0x80000010) +#define GPIO_PAD_PU_EN1 ((volatile uint32_t *) 0x80000014) +#define GPIO_FUNC_SEL0 ((volatile uint32_t *) 0x80000018) /* GPIO 15 - 0; 2 bit blocks */ +#define GPIO_FUNC_SEL1 ((volatile uint32_t *) 0x8000001c) /* GPIO 16 - 31; 2 bit blocks */ +#define GPIO_FUNC_SEL2 ((volatile uint32_t *) 0x80000020) /* GPIO 32 - 47; 2 bit blocks */ +#define GPIO_FUNC_SEL3 ((volatile uint32_t *) 0x80000024) /* GPIO 48 - 63; 2 bit blocks */ +#define GPIO_DATA_SEL0 ((volatile uint32_t *) 0x80000028) +#define GPIO_DATA_SEL1 ((volatile uint32_t *) 0x8000002c) +#define GPIO_PAD_PU_SEL0 ((volatile uint32_t *) 0x80000030) +#define GPIO_PAD_PU_SEL1 ((volatile uint32_t *) 0x80000034) + +#define GPIO_DATA_SET0 ((volatile uint32_t *) 0x80000048) +#define GPIO_DATA_SET1 ((volatile uint32_t *) 0x8000004c) +#define GPIO_DATA_RESET0 ((volatile uint32_t *) 0x80000050) +#define GPIO_DATA_RESET1 ((volatile uint32_t *) 0x80000054) +#define GPIO_PAD_DIR_SET0 ((volatile uint32_t *) 0x80000058) +#define GPIO_PAD_DIR_SET1 ((volatile uint32_t *) 0x8000005c) +#define GPIO_PAD_DIR_RESET0 ((volatile uint32_t *) 0x80000060) +#define GPIO_PAD_DIR_RESET1 ((volatile uint32_t *) 0x80000064) + +inline void gpio_pad_dir(volatile uint64_t data); +inline void gpio_data(volatile uint64_t data); +inline uint64_t gpio_data_get(volatile uint64_t bits); +inline void gpio_pad_pu_en(volatile uint64_t data); +inline void gpio_data_sel(volatile uint64_t data); +inline void gpio_data_pu_sel(volatile uint64_t data); +inline void gpio_data_set(volatile uint64_t data); +inline void gpio_data_reset(volatile uint64_t data); +inline void gpio_pad_dir_set(volatile uint64_t data); +inline void gpio_pad_dir_reset(volatile uint64_t data); + +/* select pullup or pulldown for GPIO 0-31 (b=0-31) */ +#define gpio_sel0_pullup(b) (set_bit(*GPIO_PAD_PU_SEL0,b)) +#define gpio_sel0_pulldown(b) (clear_bit(*GPIO_PAD_PU_SEL0,b)) + +/* select pullup or pulldown for GPIO 32-63 (b=32-63) */ +#define gpio_sel1_pullup(b) (set_bit(*GPIO_PAD_PU_SEL1,b-32)) +#define gpio_sel1_pulldown(b) (clear_bit(*GPIO_PAD_PU_SEL1,b-32)) + +/* enable/disable pullup for GPIO 0-31 (b=0-31) */ +#define gpio_pu0_enable(b) (set_bit(*GPIO_PAD_PU_EN0,b)) +#define gpio_pu0_disable(b) (clear_bit(*GPIO_PAD_PU_EN0,b)) + +/* enable/disable pullup for GPIO 32-63 (b=32-63) */ +#define gpio_pu1_enable(b) (set_bit(*GPIO_PAD_PU_EN1,b-32)) +#define gpio_pu1_disable(b) (clear_bit(*GPIO_PAD_PU_EN1,b-32)) + +#endif diff --git a/cpu/mc1322x/lib/include/isr.h b/cpu/mc1322x/lib/include/isr.h new file mode 100644 index 000000000..8e6039373 --- /dev/null +++ b/cpu/mc1322x/lib/include/isr.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: isr.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef ISR_H +#define ISR_H + +#define INTBASE (0x80020000) +#define INTCNTL_OFF (0x0) +#define INTENNUM_OFF (0x8) +#define INTDISNUM_OFF (0xC) +#define INTENABLE_OFF (0x10) +#define INTSRC_OFF (0x30) +#define INTFRC_OFF (0x34) +#define NIPEND_OFF (0x38) + +#define INTCNTL ((volatile uint32_t *) (INTBASE + INTCNTL_OFF)) +#define INTENNUM ((volatile uint32_t *) (INTBASE + INTENNUM_OFF)) +#define INTDISNUM ((volatile uint32_t *) (INTBASE + INTDISNUM_OFF)) +#define INTENABLE ((volatile uint32_t *) (INTBASE + INTENABLE_OFF)) +#define INTSRC ((volatile uint32_t *) (INTBASE + INTSRC_OFF)) +#define INTFRC ((volatile uint32_t *) (INTBASE + INTFRC_OFF)) +#define NIPEND ((volatile uint32_t *) (INTBASE + NIPEND_OFF)) + +enum interrupt_nums { + INT_NUM_ASM = 0, + INT_NUM_UART1, + INT_NUM_UART2, + INT_NUM_CRM, + INT_NUM_I2C, + INT_NUM_TMR, + INT_NUM_SPIF, + INT_NUM_MACA, + INT_NUM_SSI, + INT_NUM_ADC, + INT_NUM_SPI, +}; + +#define global_irq_disable() (set_bit(*INTCNTL,20)) +#define global_irq_enable() (clear_bit(*INTCNTL,20)) + +#define enable_irq(irq) (*INTENNUM = INT_NUM_##irq) +#define disable_irq(irq) (*INTDISNUM = INT_NUM_##irq) + +#define safe_irq_disable(x) volatile uint32_t saved_irq; saved_irq = *INTENABLE; disable_irq(x) +#define irq_restore() *INTENABLE = saved_irq + +extern void tmr0_isr(void) __attribute__((weak)); +extern void tmr1_isr(void) __attribute__((weak)); +extern void tmr2_isr(void) __attribute__((weak)); +extern void tmr3_isr(void) __attribute__((weak)); + +extern void rtc_isr(void) __attribute__((weak)); +extern void kbi4_isr(void) __attribute__((weak)); +extern void kbi5_isr(void) __attribute__((weak)); +extern void kbi6_isr(void) __attribute__((weak)); +extern void kbi7_isr(void) __attribute__((weak)); + +extern void uart1_isr(void) __attribute__((weak)); + +extern void maca_isr(void) __attribute__((weak)); + + +#endif + diff --git a/cpu/mc1322x/lib/include/kbi.h b/cpu/mc1322x/lib/include/kbi.h new file mode 100644 index 000000000..cc4cb7d15 --- /dev/null +++ b/cpu/mc1322x/lib/include/kbi.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: kbi.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef KBI_H +#define KBI_H + +#define enable_irq_kbi(k) (set_bit(*CRM_WU_CNTL,(EXT_WU_IEN+k-4))) +#define disable_irq_kbi(k) (clear_bit(*CRM_WU_CNTL,(EXT_WU_IEN+k-4))) + +#define kbi_evnt(k) (bit_is_set(*CRM_STATUS,(EXT_WU_EVT+k-4))) + +#define kbi_edge(k) (set_bit(*CRM_WU_CNTL,(EXT_WU_EDGE+k-4))) +#define kbi_level(k) (clear_bit(*CRM_WU_CNTL,(EXT_WU_EDGE+k-4))) + +#define kbi_pol_neg(k) (clear_bit(*CRM_WU_CNTL,(EXT_WU_POL+k-4))) +#define kbi_pol_pos(k) (set_bit(*CRM_WU_CNTL,(EXT_WU_POL+k-4))) + +/* you have to clear these events by writing a one to them */ + +#define clear_kbi_evnt(k) (set_bit(*CRM_STATUS,(EXT_WU_EVT+k-4))) + +#endif diff --git a/cpu/mc1322x/lib/include/maca.h b/cpu/mc1322x/lib/include/maca.h new file mode 100644 index 000000000..a86ab5481 --- /dev/null +++ b/cpu/mc1322x/lib/include/maca.h @@ -0,0 +1,517 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: maca.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef _MACA_H_ +#define _MACA_H_ + +#include +#include +#include + +/* maca initialization and on off routines */ +void maca_init(void); +void maca_off(void); +void maca_on(void); + +/* run periodically to make sure the maca is still doing right */ +void check_maca(void); + +/* maca configuration interface */ +void set_power(uint8_t power); +void set_channel(uint8_t chan); + +#define DEMOD_DCD 1 /* -96dBm, 22.2mA */ +#define DEMOD_NCD 0 /* -100dBm, 24.2mA */ +void set_demodulator_type(uint8_t demod); + +/* set_fcs_mode(NO_FCS) to disable checksum filtering */ +extern volatile uint8_t fcs_mode; +#define set_fcs_mode(x) fcs_mode = (x) + +/* maca packet interface */ +void tx_packet(volatile packet_t *p); +volatile packet_t* rx_packet(void); +volatile packet_t* get_free_packet(void); +void free_packet(volatile packet_t *p); +void free_all_packets(void); + +extern volatile packet_t *rx_head, *tx_head; +extern volatile uint32_t maca_entry; + +extern void maca_rx_callback(volatile packet_t *p) __attribute__((weak)); +extern void maca_tx_callback(volatile packet_t *p) __attribute__((weak)); + +/* maca lowlevel routines */ +/* most applications won't need to use them */ +void reset_maca(void); +void init_phy(void); +void flyback_init(void); +void ResumeMACASync(void); +void radio_init(void); +uint32_t init_from_flash(uint32_t addr); + +#define MAX_PACKET_SIZE (MAX_PAYLOAD_SIZE + 2) /* packet includes 2 bytes of checksum */ + +/* maca register and field defines */ + +#define MACA_BASE (0x80004000) +#define MACA_RESET ((volatile uint32_t *) (MACA_BASE+0x04)) +#define MACA_RANDOM ((volatile uint32_t *) (MACA_BASE+0x08)) +#define MACA_CONTROL ((volatile uint32_t *) (MACA_BASE+0x0c)) + +/* MACA_CONTROL bits and fields */ +#define ISM 20 +#define PRECOUNT 16 /* preamble reapeat counter */ +#define PRECOUNT_MASK bit_mask(4,PRECOUNT) +#define RTSO 15 /* reset slot counter */ +#define ROLE 13 /* set if PAN coordinator */ +#define NOFC 12 /* set to disable FCS */ +enum { + USE_FCS = 0, + NO_FCS = 1, +}; +#define PRM 11 /* set for promiscuous mode */ +#define REL 10 /* 1 for relative, 0 for absolute */ +#define ASAP 9 /* 1 start now, 0 timer start */ +#define BCN 8 /* 1 beacon only, 0 for a */ +#define AUTO 7 /* 1 continuous rx, rx only once */ +#define LFSR 6 /* 1 use polynomial for Turbolink */ +#define TM 5 + +#define MODE 3 +#define MODE_MASK bit_mask(2,MODE) +#define NO_CCA 0 +#define NO_SLOT_CCA 1 +#define SLOT_CCA 2 + +#define SEQUENCE 0 +#define SEQUENCE_MASK bit_mask(3,SEQUENCE) +/* end of MACA_CONTROL bits and fields */ + +#define MACA_STATUS ((volatile uint32_t *) (MACA_BASE+0x10)) +/* MACA_STATUS bits and fields */ +#define STATUS_TIMEOUT 15 +#define CRC 14 +#define BUSY 13 +#define OVR 12 +#define CODE 0 +#define CODE_MASK bit_mask(4,CODE) +/* status codes */ +#define SUCCESS 0 +#define CODE_TIMEOUT 1 +#define CHANNEL_BUSY 2 +#define CRC_FAILED 3 +#define ABORTED 4 +#define NO_ACK 5 +#define NO_DATA 6 +#define LATE_START 7 +#define EXT_TIMEOUT 8 +#define EXT_PND_TIMEOUT 9 +#define PLL_UNLOCK 12 +#define EXTERNAL_ABORT 13 +#define NOT_COMPLETED 14 +#define DMA_BUS_ERROR 15 +/* end of MACA_CONTROL bits and fields */ + +#define MACA_FRMPND ((volatile uint32_t *) (MACA_BASE+0x14)) +#define MACA_TMREN ((volatile uint32_t *) (MACA_BASE+0x40)) +#define MACA_TMRDIS ((volatile uint32_t *) (MACA_BASE+0x44)) +#define MACA_CLK ((volatile uint32_t *) (MACA_BASE+0x48)) +#define MACA_STARTCLK ((volatile uint32_t *) (MACA_BASE+0x4c)) +#define MACA_CPLCLK ((volatile uint32_t *) (MACA_BASE+0x50)) +#define MACA_SFTCLK ((volatile uint32_t *) (MACA_BASE+0x54)) +#define MACA_CLKOFFSET ((volatile uint32_t *) (MACA_BASE+0x58)) +#define MACA_RELCLK ((volatile uint32_t *) (MACA_BASE+0x5c)) +#define MACA_CPLTIM ((volatile uint32_t *) (MACA_BASE+0x60)) +#define MACA_SLOTOFFSET ((volatile uint32_t *) (MACA_BASE+0x64)) +#define MACA_TIMESTAMP ((volatile uint32_t *) (MACA_BASE+0x68)) +#define MACA_DMARX ((volatile uint32_t *) (MACA_BASE+0x80)) +#define MACA_DMATX ((volatile uint32_t *) (MACA_BASE+0x84)) +#define MACA_DMAPOLL ((volatile uint32_t *) (MACA_BASE+0x88)) +#define MACA_TXLEN ((volatile uint32_t *) (MACA_BASE+0x8c)) +#define MACA_TXSEQNR ((volatile uint32_t *) (MACA_BASE+0x90)) +#define MACA_SETRXLVL ((volatile uint32_t *) (MACA_BASE+0x94)) +#define MACA_GETRXLVL ((volatile uint32_t *) (MACA_BASE+0x98)) +#define MACA_IRQ ((volatile uint32_t *) (MACA_BASE+0xc0)) +#define MACA_CLRIRQ ((volatile uint32_t *) (MACA_BASE+0xc4)) +#define MACA_SETIRQ ((volatile uint32_t *) (MACA_BASE+0xc8)) +#define MACA_MASKIRQ ((volatile uint32_t *) (MACA_BASE+0xcc)) +#define MACA_MACPANID ((volatile uint32_t *) (MACA_BASE+0x100)) +#define MACA_MAC16ADDR ((volatile uint32_t *) (MACA_BASE+0x104)) +#define MACA_MAC64HI ((volatile uint32_t *) (MACA_BASE+0x108)) +#define MACA_MAC64LO ((volatile uint32_t *) (MACA_BASE+0x10c)) +#define MACA_FLTREJ ((volatile uint32_t *) (MACA_BASE+0x110)) +#define MACA_CLKDIV ((volatile uint32_t *) (MACA_BASE+0x114)) +#define MACA_WARMUP ((volatile uint32_t *) (MACA_BASE+0x118)) +#define MACA_PREAMBLE ((volatile uint32_t *) (MACA_BASE+0x11c)) +#define MACA_WHITESEED ((volatile uint32_t *) (MACA_BASE+0x120)) +#define MACA_FRAMESYNC0 ((volatile uint32_t *) (MACA_BASE+0x124)) +#define MACA_FRAMESYNC1 ((volatile uint32_t *) (MACA_BASE+0x128)) +#define MACA_TXACKDELAY ((volatile uint32_t *) (MACA_BASE+0x140)) +#define MACA_RXACKDELAY ((volatile uint32_t *) (MACA_BASE+0x144)) +#define MACA_EOFDELAY ((volatile uint32_t *) (MACA_BASE+0x148)) +#define MACA_CCADELAY ((volatile uint32_t *) (MACA_BASE+0x14c)) +#define MACA_RXEND ((volatile uint32_t *) (MACA_BASE+0x150)) +#define MACA_TXCCADELAY ((volatile uint32_t *) (MACA_BASE+0x154)) +#define MACA_KEY3 ((volatile uint32_t *) (MACA_BASE+0x158)) +#define MACA_KEY2 ((volatile uint32_t *) (MACA_BASE+0x15c)) +#define MACA_KEY1 ((volatile uint32_t *) (MACA_BASE+0x160)) +#define MACA_KEY0 ((volatile uint32_t *) (MACA_BASE+0x164)) +#define MACA_OPTIONS ((volatile uint32_t *) (MACA_BASE+0x180)) + + +/******************************************************************************/ +/* everything under this comment is messy, needs cleaning, and will */ +/* probably change in the future */ +/******************************************************************************/ + +#define control_pre_count (7<<16) /* preamble reapeat counter */ +#define control_rst_slot (1<<15) /* reset slot counter */ +#define control_role (1<<13) /* set if PAN coordinator */ +#define control_nofc (1<<12) /* set to disable FCS */ +#define control_prm (1<<11) /* set for promiscuous mode */ +#define control_relative (1<<10) /* 1 for relative, 0 for absolute */ +#define control_asap (1<<9) /* 1 start now, 0 timer start */ +#define control_bcn (1<<8) /* 1 beacon only, 0 for a */ +#define control_auto (1<<7) /* 1 continuous rx, rx only once */ +#define control_lfsr (1<<6) /* 1 use polynomial for Turbolink */ + +#define gMACA_Clock_DIV_c 95 + +//rom_base_adr equ 0x00000000 ; rom base address +//ram_base_adr equ 0x00400000 ; ram base address +//ram0_base_adr equ 0x00400000 ; ram0 base address (2K words = 8K +//bytes) +//ram1_base_adr equ 0x00402000 ; ram1 base address (6K words = 24K +//bytes) +//ram2_base_adr equ 0x00408000 ; ram2 base address (8K words = 32K +//bytes) +//ram3_base_adr equ 0x00410000 ; ram3 base address (8K words +enum { + cc_success = 0, + cc_timeout = 1, + cc_channel_busy = 2, + cc_crc_fail = 3, + cc_aborted = 4, + cc_no_ack = 5, + cc_no_data = 6, + cc_late_start = 7, + cc_ext_timeout = 8, + cc_ext_pnd_timeout = 9, + cc_nc1 = 10, + cc_nc2 = 11, + cc_nc3 = 12, + cc_cc_external_abort= 13, + cc_not_completed = 14, + cc_bus_error = 15 +}; +//control codes for mode bits + +enum { + control_mode_no_cca = 0, + control_mode_non_slotted = (1<<3), + control_mode_slotted = (1<<4) +}; +//control codes for sequence bits +enum { + control_seq_nop = 0, + control_seq_abort = 1, + control_seq_wait = 2, + control_seq_tx = 3, + control_seq_rx = 4, + control_seq_txpoll = 5, + control_seq_cca = 6, + control_seq_ed = 7 +}; + +#define maca_version (*((volatile uint32_t *)(0x80004000))) +#define maca_reset (*((volatile uint32_t *)(0x80004004))) +#define maca_random (*((volatile uint32_t *)(0x80004008))) +#define maca_control (*((volatile uint32_t *)(0x8000400c))) +#define maca_status (*((volatile uint32_t *)(0x80004010))) +#define maca_frmpnd (*((volatile uint32_t *)(0x80004014))) + +#define maca_edvalue (*((volatile uint32_t *)(0x8000401c))) +#define maca_tmren (*((volatile uint32_t *)(0x80004040))) +#define maca_tmrdis (*((volatile uint32_t *)(0x80004044))) +#define maca_clk (*((volatile uint32_t *)(0x80004048))) +#define maca_startclk (*((volatile uint32_t *)(0x8000404c))) +#define maca_cplclk (*((volatile uint32_t *)(0x80004050))) +#define maca_sftclk (*((volatile uint32_t *)(0x80004054))) +#define maca_clkoffset (*((volatile uint32_t *)(0x80004058))) +#define maca_relclk (*((volatile uint32_t *)(0x8000405c))) +#define maca_cpltim (*((volatile uint32_t *)(0x80004060))) +#define maca_slotoffset (*((volatile uint32_t *)(0x80004064))) +#define maca_timestamp (*((volatile uint32_t *)(0x80004068))) +#define maca_dmarx (*((volatile uint32_t *)(0x80004080))) +#define maca_dmatx (*((volatile uint32_t *)(0x80004084))) +#define maca_dmatxpoll (*((volatile uint32_t *)(0x80004088))) +#define maca_txlen (*((volatile uint32_t *)(0x8000408c))) +#define maca_txseqnr (*((volatile uint32_t *)(0x80004090))) +#define maca_setrxlvl (*((volatile uint32_t *)(0x80004094))) +#define maca_getrxlvl (*((volatile uint32_t *)(0x80004098))) +#define maca_irq (*((volatile uint32_t *)(0x800040c0))) +#define maca_clrirq (*((volatile uint32_t *)(0x800040c4))) +#define maca_setirq (*((volatile uint32_t *)(0x800040c8))) +#define maca_maskirq (*((volatile uint32_t *)(0x800040cc))) +#define maca_panid (*((volatile uint32_t *)(0x80004100))) +#define maca_addr16 (*((volatile uint32_t *)(0x80004104))) +#define maca_maca64hi (*((volatile uint32_t *)(0x80004108))) +#define maca_maca64lo (*((volatile uint32_t *)(0x8000410c))) +#define maca_fltrej (*((volatile uint32_t *)(0x80004110))) +#define maca_divider (*((volatile uint32_t *)(0x80004114))) +#define maca_warmup (*((volatile uint32_t *)(0x80004118))) +#define maca_preamble (*((volatile uint32_t *)(0x8000411c))) +#define maca_whiteseed (*((volatile uint32_t *)(0x80004120))) +#define maca_framesync (*((volatile uint32_t *)(0x80004124))) +#define maca_framesync2 (*((volatile uint32_t *)(0x80004128))) +#define maca_txackdelay (*((volatile uint32_t *)(0x80004140))) +#define maca_rxackdelay (*((volatile uint32_t *)(0x80004144))) +#define maca_eofdelay (*((volatile uint32_t *)(0x80004148))) +#define maca_ccadelay (*((volatile uint32_t *)(0x8000414c))) +#define maca_rxend (*((volatile uint32_t *)(0x80004150))) +#define maca_txccadelay (*((volatile uint32_t *)(0x80004154))) +#define maca_key3 (*((volatile uint32_t *)(0x80004158))) +#define maca_key2 (*((volatile uint32_t *)(0x80004158))) +#define maca_key1 (*((volatile uint32_t *)(0x80004158))) +#define maca_key0 (*((volatile uint32_t *)(0x80004158))) + + +typedef union maca_version_reg_tag +{ + struct + { + uint32_t MINOR:8; + uint32_t RESERVED1:8; + uint32_t MAJOR:8; + uint32_t RESERVED2:8; + } Bits; + uint32_t Reg; +} maca_version_reg_t; + +#define maca_version_reg_st ((maca_version_reg_t)(maca_version)) + + +typedef union maca_reset_reg_tag +{ + struct + { + uint32_t RESERVED:30; + uint32_t CLK_ON:1; + uint32_t RST:1; + } Bits; + uint32_t Reg; +} maca_reset_reg_t; + +#define maca_reset_reg_st ((maca_reset_reg_t)(maca_reset)) + + +/* typedef union maca_ctrl_reg_tag */ +/* { */ +/* struct */ +/* { */ +/* uint32_t RESERVED:11; */ +/* uint32_t ISM:1; */ +/* uint32_t PRE_COUNT:4; */ +/* uint32_t RSTO:1; */ +/* uint32_t RSV:1; */ +/* uint32_t ROLE:1; */ +/* uint32_t NOFC:1; */ +/* uint32_t PRM:1; */ +/* uint32_t rel:1; */ +/* uint32_t ASAP:1; */ +/* uint32_t BCN:1; */ +/* uint32_t AUTO:1; */ +/* uint32_t LFSR:1; */ +/* uint32_t TM:1; */ +/* uint32_t MODE:2; */ +/* uint32_t SEQUENCE:3; */ +/* } Bits; */ +/* uint32_t Reg; */ +/* } maca_ctrl_reg_t; */ + +#define maca_control_ism (1<<20) +#define maca_control_zigbee (~maca_control_ism) + +#define maca_ctrl_reg_st ((maca_ctrl_reg_t *)(&maca_reset)) +#define _set_maca_test_mode(x) (maca_ctrl_reg_st->Bits.TM = x) +#define _set_maca_sequence(x) (maca_ctrl_reg_st->Bits.SEQUENCE = x) +#define _set_maca_asap(x) (maca_ctrl_reg_st->Bits.ASAP = x) + + +#define MACA_CTRL_ZIGBEE_MODE (0) +#define MACA_CTRL_ISM_MODE (1) +#define MACA_CTRL_PRM_NORMAL_MODE (0) +#define MACA_CTRL_PRM_PROMISCUOUS_MODE (1) +#define MACA_CTRL_BCN_ALL (0) +#define MACA_CTRL_BCN_BEACON (1) +#define MACA_CTRL_TM_NORMAL (0) +#define MACA_CTRL_TM_TEST (1) +#define MACA_CTRL_MODE_NO_CCA (0) +#define MACA_CTRL_MODE_NON_SLOTTED (1) +#define MACA_CTRL_MODE_SLOTTED (2) + +typedef enum maca_freq_chann_tag +{ + SMAC_CHANN_11 = 0, + SMAC_CHANN_12, + SMAC_CHANN_13, + SMAC_CHANN_14, + SMAC_CHANN_15, + SMAC_CHANN_16, + SMAC_CHANN_17, + SMAC_CHANN_18, + SMAC_CHANN_19, + SMAC_CHANN_20, + SMAC_CHANN_21, + SMAC_CHANN_22, + SMAC_CHANN_23, + SMAC_CHANN_24, + SMAC_CHANN_25, + SMAC_CHANN_26, + MAX_SMAC_CHANNELS +} maca_freq_chann_t; + + +/* Sequence complete codes */ +enum maca_complete_code { + maca_cc_success = 0, + maca_cc_timeout = 1, + maca_cc_channel_busy = 2, + maca_cc_crc_fail = 3, + maca_cc_aborted = 4, + maca_cc_no_ack = 5, + maca_cc_no_data = 6, + maca_cc_late_start = 7, + maca_cc_ext_timeout = 8, + maca_cc_ext_pnd_timeout = 9, + maca_cc_nc1 = 10, + maca_cc_nc2 = 11, + maca_cc_nc3 = 12, + maca_cc_cc_external_abort= 13, + maca_cc_not_completed = 14, + maca_cc_bus_error = 15 +}; + +/* control sequence codes */ +enum maca_ctrl_seq { + maca_ctrl_seq_nop = 0, + maca_ctrl_seq_abort = 1, + maca_ctrl_seq_wait = 2, + maca_ctrl_seq_tx = 3, + maca_ctrl_seq_rx = 4, + maca_ctrl_seq_txpoll = 5, + maca_ctrl_seq_cca = 6, + maca_ctrl_seq_ed = 7 +}; + +/* transmission modes */ +enum maca_ctrl_modes { + maca_ctrl_mode_no_cca = 0, + maca_ctrl_mode_non_slotted_csma_ca = 1, + maca_ctrl_mode_slotted_csma_ca = 2, +}; + +/* MACA_CONTROL bits */ +enum maca_ctrl_bits { + maca_ctrl_seq = 0, /* 3 bits */ + maca_ctrl_mode = 3, /* 2 bits */ + maca_ctrl_tm = 5, + maca_ctrl_lfsr = 6, + maca_ctrl_auto = 7, + maca_ctrl_bcn = 8, + maca_ctrl_asap = 9, + maca_ctrl_rel = 10, + maca_ctrl_prm = 11, + maca_ctrl_nofc = 12, + maca_ctrl_role = 13, + /* 14 reserved */ + maca_ctrl_rsto = 15, + maca_ctrl_pre_count = 16, /* 4 bits */ + maca_ctrl_ism = 20, +}; + +/* MACA_IRQ bits */ +enum maca_irqs { + maca_irq_acpl = 0, + maca_irq_poll = 1, + maca_irq_di = 2, + maca_irq_wu = 3, + maca_irq_rst = 4, + maca_irq_lvl = 9, + maca_irq_sftclk = 10, + maca_irq_flt = 11, + maca_irq_crc = 12, + maca_irq_cm = 13, + maca_irq_sync = 14, + maca_irq_strt = 15, +}; + +/* MACA_RESET bits */ +enum maca_reset_bits { + maca_reset_rst = 0, + maca_reset_clkon = 1, +}; + +/* MACA_TMREN bits */ +enum maca_tmren_bits { + maca_tmren_strt = 0, + maca_tmren_cpl = 1, + maca_tmren_sft = 2, +}; + +enum maca_status_bits { + maca_status_ovr = 12, + maca_status_busy = 13, + maca_status_crc = 14, + maca_status_to = 15, +}; + +#define action_complete_irq() bit_is_set(*MACA_IRQ,maca_irq_acpl) +#define filter_failed_irq() bit_is_set(*MACA_IRQ,maca_irq_flt) +#define checksum_failed_irq() bit_is_set(*MACA_IRQ,maca_irq_crc) +#define data_indication_irq() bit_is_set(*MACA_IRQ,maca_irq_di) +#define softclock_irq() bit_is_set(*MACA_IRQ,maca_irq_sftclk) +#define poll_irq() bit_is_set(*MACA_IRQ,maca_irq_poll) + +#define status_is_not_completed() ((*MACA_STATUS & 0xffff) == maca_cc_not_completed) +#define status_is_success() ((*MACA_STATUS & 0xffff) == maca_cc_success) + +#define SMAC_MACA_CNTL_INIT_STATE ( control_prm | control_nofc | control_mode_non_slotted ) + +#define MACA_WRITE(reg, src) (reg = src) +#define MACA_READ(reg) reg + +#endif // _MACA_H_ diff --git a/cpu/mc1322x/lib/include/mc1322x.h b/cpu/mc1322x/lib/include/mc1322x.h new file mode 100644 index 000000000..2ff34ef75 --- /dev/null +++ b/cpu/mc1322x/lib/include/mc1322x.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: mc1322x.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef MC1322X_H +#define MC1322X_H + +#include "types.h" +#include "isr.h" +#include "gpio.h" +#include "crm.h" +#include "nvm.h" +#include "tmr.h" +#include "kbi.h" +#include "maca.h" +#include "packet.h" +#include "uart1.h" +#include "utils.h" + +#endif diff --git a/cpu/mc1322x/lib/include/nvm.h b/cpu/mc1322x/lib/include/nvm.h new file mode 100644 index 000000000..d9b81065c --- /dev/null +++ b/cpu/mc1322x/lib/include/nvm.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: nvm.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef NVM_H +#define NVM_H + +#include "types.h" + +typedef enum +{ + gNvmType_NoNvm_c, + gNvmType_SST_c, + gNvmType_ST_c, + gNvmType_ATM_c, + gNvmType_Max_c +} nvmType_t; + + +typedef enum +{ + gNvmErrNoError_c = 0, + gNvmErrInvalidInterface_c, + gNvmErrInvalidNvmType_c, + gNvmErrInvalidPointer_c, + gNvmErrWriteProtect_c, + gNvmErrVerifyError_c, + gNvmErrAddressSpaceOverflow_c, + gNvmErrBlankCheckError_c, + gNvmErrRestrictedArea_c, + gNvmErrMaxError_c +} nvmErr_t; + +typedef enum +{ + gNvmInternalInterface_c, + gNvmExternalInterface_c, + gNvmInterfaceMax_c +} nvmInterface_t; + +/* ROM code seems to be THUMB */ +/* need to be in a THUMB block before calling them */ +extern nvmErr_t (*nvm_detect)(nvmInterface_t nvmInterface,nvmType_t* pNvmType); +extern nvmErr_t (*nvm_read)(nvmInterface_t nvmInterface , nvmType_t nvmType , void *pDest, uint32_t address, uint32_t numBytes); +extern nvmErr_t (*nvm_write)(nvmInterface_t nvmInterface, nvmType_t nvmType ,void *pSrc, uint32_t address, uint32_t numBytes); +/* sector bit field selects which sector to erase */ +/* SST flash has 32 sectors 4096 bytes each */ +/* bit 0 is the first sector, bit 31 is the last */ +extern nvmErr_t (*nvm_erase)(nvmInterface_t nvmInterface, nvmType_t nvmType ,uint32_t sectorBitfield); +extern void(*nvm_setsvar)(uint32_t zero_for_awesome); +#endif //NVM_H diff --git a/cpu/mc1322x/lib/include/packet.h b/cpu/mc1322x/lib/include/packet.h new file mode 100644 index 000000000..85fdf6a96 --- /dev/null +++ b/cpu/mc1322x/lib/include/packet.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: packet.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef PACKET_H +#define PACKET_H + +/* does not include 2 byte FCS checksum */ +#ifndef MAX_PAYLOAD_SIZE +#define MAX_PAYLOAD_SIZE 125 +#endif + +#define PACKET_STATS 0 + +struct packet { + uint8_t length; /* does not include FCS checksum */ + volatile struct packet * left; + volatile struct packet * right; + /* offset into data for first byte of the packet payload */ + /* On TX this should be 0 */ + /* On RX this should be 1 since the maca puts the length as the first byte*/ + uint8_t offset; + #if PACKET_STATS + uint8_t seen; + uint8_t post_tx; + uint8_t get_free; + uint8_t rxd; + #endif + uint8_t data[MAX_PAYLOAD_SIZE+2+1]; /* +2 for FCS; + 1 since maca returns the length as the first byte */ +}; +typedef struct packet packet_t; + +#endif diff --git a/cpu/mc1322x/lib/include/tmr.h b/cpu/mc1322x/lib/include/tmr.h new file mode 100644 index 000000000..b6e97800b --- /dev/null +++ b/cpu/mc1322x/lib/include/tmr.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: tmr.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include "utils.h" + +/* Timer registers are all 16-bit wide with 16-bit access only */ +#define TMR_OFFSET (0x20) +#define TMR_BASE (0x80007000) +#define TMR0_BASE (TMR_BASE) +#define TMR1_BASE (TMR_BASE + TMR_OFFSET*1) +#define TMR2_BASE (TMR_BASE + TMR_OFFSET*2) +#define TMR3_BASE (TMR_BASE + TMR_OFFSET*3) + +#define TMR_REGOFF_COMP1 (0x0) +#define TMR_REGOFF_COMP2 (0x2) +#define TMR_REGOFF_CAPT (0x4) +#define TMR_REGOFF_LOAD (0x6) +#define TMR_REGOFF_HOLD (0x8) +#define TMR_REGOFF_CNTR (0xa) +#define TMR_REGOFF_CTRL (0xc) +#define TMR_REGOFF_SCTRL (0xe) +#define TMR_REGOFF_CMPLD1 (0x10) +#define TMR_REGOFF_CMPLD2 (0x12) +#define TMR_REGOFF_CSCTRL (0x14) +#define TMR_REGOFF_ENBL (0x1e) + +/* one enable register to rule them all */ +#define TMR_ENBL ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_ENBL)) + +/* Timer 0 registers */ +#define TMR0_COMP1 ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_COMP1)) +#define TMR0_COMP_UP TMR0_COMP1 +#define TMR0_COMP2 (TMR0_BASE + TMR_REGOFF_COMP2) +#define TMR0_COMP_DOWN TMR0_COMP2 +#define TMR0_CAPT ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CAPT)) +#define TMR0_LOAD ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_LOAD)) +#define TMR0_HOLD ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_HOLD)) +#define TMR0_CNTR ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CTRL)) +#define TMR0_CTRL ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CTRL)) +#define TMR0_SCTRL ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_SCTRL)) +#define TMR0_CMPLD1 ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CMPLD1)) +#define TMR0_CMPLD2 ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CMPLD2)) +#define TMR0_CSCTRL ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CSCTRL)) + +/* Timer 1 registers */ +#define TMR1_COMP1 ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_COMP1)) +#define TMR1_COMP_UP TMR1_COMP1 +#define TMR1_COMP2 ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_COMP2)) +#define TMR1_COMP_DOWN TMR1_COMP2 +#define TMR1_CAPT ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CAPT)) +#define TMR1_LOAD ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_LOAD)) +#define TMR1_HOLD ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_HOLD)) +#define TMR1_CNTR ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CTRL)) +#define TMR1_CTRL ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CTRL)) +#define TMR1_SCTRL ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_SCTRL)) +#define TMR1_CMPLD1 ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CMPLD1)) +#define TMR1_CMPLD2 ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CMPLD2)) +#define TMR1_CSCTRL ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CSCTRL)) + +/* Timer 2 registers */ +#define TMR2_COMP1 ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_COMP1)) +#define TMR2_COMP_UP TMR2_COMP1 +#define TMR2_COMP2 ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_COMP2)) +#define TMR2_COMP_DOWN TMR2_COMP2 +#define TMR2_CAPT ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CAPT)) +#define TMR2_LOAD ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_LOAD)) +#define TMR2_HOLD ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_HOLD)) +#define TMR2_CNTR ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CTRL)) +#define TMR2_CTRL ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CTRL)) +#define TMR2_SCTRL ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_SCTRL)) +#define TMR2_CMPLD1 ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CMPLD1)) +#define TMR2_CMPLD2 ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CMPLD2)) +#define TMR2_CSCTRL ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CSCTRL)) + +/* Timer 3 registers */ +#define TMR3_COMP1 ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_COMP1)) +#define TMR3_COMP_UP TMR3_COMP1 +#define TMR3_COMP2 ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_COMP2)) +#define TMR3_COMP_DOWN TMR3_COMP2 +#define TMR3_CAPT ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CAPT)) +#define TMR3_LOAD ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_LOAD)) +#define TMR3_HOLD ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_HOLD)) +#define TMR3_CNTR ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CTRL)) +#define TMR3_CTRL ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CTRL)) +#define TMR3_SCTRL ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_SCTRL)) +#define TMR3_CMPLD1 ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CMPLD1)) +#define TMR3_CMPLD2 ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CMPLD2)) +#define TMR3_CSCTRL ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CSCTRL)) + +#define TCF 15 +#define TCF1 4 +#define TCF2 5 + +#define TMR(num, reg) CAT2(TMR,num,_##reg) + diff --git a/cpu/mc1322x/lib/include/types.h b/cpu/mc1322x/lib/include/types.h new file mode 100644 index 000000000..2e47389d2 --- /dev/null +++ b/cpu/mc1322x/lib/include/types.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: types.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef _TYPES_H +#define _TYPES_H + +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +typedef signed int int32_t; +typedef unsigned int uint32_t; +typedef signed long long int int64_t; +typedef unsigned long long int uint64_t; + +#endif diff --git a/cpu/mc1322x/lib/include/uart1.h b/cpu/mc1322x/lib/include/uart1.h new file mode 100644 index 000000000..18ad3bbb5 --- /dev/null +++ b/cpu/mc1322x/lib/include/uart1.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: uart1.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef UART1_H +#define UART1_H + +#include + +#define UCON (0) +/* UCON bits */ +#define UCON_SAMP 10 +#define UCON_SAMP_8X 0 +#define UCON_SAMP_16X 1 + +#define USTAT (0x04) +#define UDATA (0x08) +#define URXCON (0x0c) +#define UTXCON (0x10) +#define UCTS (0x14) +#define UBRCNT (0x18) + +#define UART1_BASE (0x80005000) +#define UART2_BASE (0x8000b000) + +#define UART1_UCON ((volatile uint32_t *) ( UART1_BASE + UCON )) +#define UART1_USTAT ((volatile uint32_t *) ( UART1_BASE + USTAT )) +#define UART1_UDATA ((volatile uint32_t *) ( UART1_BASE + UDATA )) +#define UART1_URXCON ((volatile uint32_t *) ( UART1_BASE + URXCON )) +#define UART1_UTXCON ((volatile uint32_t *) ( UART1_BASE + UTXCON )) +#define UART1_UCTS ((volatile uint32_t *) ( UART1_BASE + UCTS )) +#define UART1_UBRCNT ((volatile uint32_t *) ( UART1_BASE + UBRCNT )) + +#define UART2_UCON ((volatile uint32_t *) ( UART2_BASE + UCON )) +#define UART2_USTAT ((volatile uint32_t *) ( UART2_BASE + USTAT )) +#define UART2_UDATA ((volatile uint32_t *) ( UART2_BASE + UDATA )) +#define UART2_URXCON ((volatile uint32_t *) ( UART2_BASE + URXCON )) +#define UART2_UTXCON ((volatile uint32_t *) ( UART2_BASE + UTXCON )) +#define UART2_UCTS ((volatile uint32_t *) ( UART2_BASE + UCTS )) +#define UART2_UBRCNT ((volatile uint32_t *) ( UART2_BASE + UBRCNT )) + +extern volatile uint32_t u1_head, u1_tail; +void uart1_putc(char c); +#define uart1_can_get() (*UART1_URXCON > 0) +uint8_t uart1_getc(void); + + + +#endif diff --git a/cpu/mc1322x/lib/include/utils.h b/cpu/mc1322x/lib/include/utils.h new file mode 100644 index 000000000..b8829d611 --- /dev/null +++ b/cpu/mc1322x/lib/include/utils.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: utils.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef UTILS_H +#define UTILS_H + +#define mem32(x) ((volatile uint32_t *)(x)) +#define mem16(x) ((volatile uint16_t *)(x)) + +#define CAT2(x, y, z) x##y##z + +#define STR(x) #x +#define STR2(x) STR(x) + +#define bit(bit) (1 << bit) +#define bit_is_set(val, bit) (((val & (1 << bit)) >> bit) == 1) +#define clear_bit(val, bit) (val = (val & ~(1 << bit))) +#define set_bit(val, bit) (val = (val | (1 << bit))) + +#define ones(num) ( (1ULL << num) - 1 ) +#define bit_mask(length, shift) (ones(length) << shift) +#define get_field(val, field) ((val & field##_MASK) >> field) +//#define bitfield(name, length, shift) ( #define #name length #define #name##_MASK bit_mask(length, shift)) +#define bitfield(name, length, shift) ( define #name length ) + +#endif /* UTILS_H */ diff --git a/cpu/mc1322x/lib/maca.c b/cpu/mc1322x/lib/maca.c new file mode 100644 index 000000000..a2b38cf15 --- /dev/null +++ b/cpu/mc1322x/lib/maca.c @@ -0,0 +1,1213 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: maca.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#ifndef DEBUG_MACA +#define DEBUG_MACA 0 +#endif +#if (DEBUG_MACA == 0) +#define PRINTF(...) +#else +#define PRINTF(...) printf(__VA_ARGS__) +#endif + +#ifndef MACA_BOUND_CHECK +#define MACA_BOUND_CHECK 0 +#endif +#if (MACA_BOUND_CHECK == 0) +#define BOUND_CHECK(x) +#else +#define BOUND_CHECK(x) bound_check(x) +#endif + +#ifndef NUM_PACKETS +#define NUM_PACKETS 8 +#endif + +/* for 250kHz clock */ +#define MACA_CLOCK_DIV 95 +/* (32 chips/sym) * (sym/4bits) * (8bits/byte) = (64 chips/byte) */ +/* (8 chips/clk) * (byte/64 chips) = byte/8clks */ +#define CLK_PER_BYTE 8 + +#ifndef RECV_SOFTIMEOUT +#define RECV_SOFTIMEOUT (32*128*CLK_PER_BYTE) +#endif + +#ifndef CPL_TIMEOUT +#define CPL_TIMEOUT (2*128*CLK_PER_BYTE) +#endif + +#define reg(x) (*(volatile uint32_t *)(x)) + +int count_packets(void); +void Print_Packets(char *s); + +static volatile packet_t packet_pool[NUM_PACKETS]; +static volatile packet_t *free_head, *rx_end, *tx_end, *dma_tx, *dma_rx; + +/* rx_head and tx_head are visible to the outside */ +/* so you can peek at it and see if there is data */ +/* waiting for you, or data still to be sent */ +volatile packet_t *rx_head, *tx_head; + +/* used for ack recpetion if the packet_pool goes empty */ +/* doesn't go back into the pool when freed */ +static volatile packet_t dummy_ack; + +/* incremented on every maca entry */ +/* you can use this to detect that the receive loop is still running */ +volatile uint32_t maca_entry = 0; + +enum posts { + NO_POST = 0, + TX_POST, + RX_POST, + MAX_POST, +}; +static volatile uint8_t last_post = NO_POST; + +volatile uint8_t fcs_mode = USE_FCS; + +/* call periodically to */ +/* check that maca_entry is changing */ +/* if it is not, it will do a manual call to maca_isr which should */ +/* get the ball rolling again */ +/* also checks that the clock is running --- if it isn't then */ +/* it calls redoes the maca intialization but _DOES NOT_ free all packets */ + +void check_maca(void) { + safe_irq_disable(MACA); + static volatile uint32_t last_time; + static volatile uint32_t last_entry; + volatile uint32_t i; +#if DEBUG_MACA + volatile uint32_t count; +#endif + + + /* if *MACA_CLK == last_time */ + /* try waiting for one clock period */ + /* since maybe check_maca is getting called quickly */ + for(i=0; (i < 1024) && (*MACA_CLK == last_time); i++) { continue; } + + if(*MACA_CLK == last_time) { + PRINTF("check maca: maca_clk stopped, restarting\n"); + /* clock isn't running */ + ResumeMACASync(); + *INTFRC = (1< (*MACA_SFTCLK + RECV_SOFTIMEOUT)) && + (last_time > (*MACA_CPLCLK + CPL_TIMEOUT))) { + PRINTF("check maca: complete clocks expired --- forcing isr\n"); + /* all complete clocks have expired */ + /* check that maca entry is changing */ + /* if not, do call the isr to restart the cycle */ + if(last_entry == maca_entry) { + *INTFRC = (1<left; + printf("->0x%lx", (uint32_t) p); + } + printf("\n\r"); + + p = tx_head; + printf("tx_head: 0x%lx ", (uint32_t) tx_head); + while(p != 0) { + p = p->left; + printf("->0x%lx", (uint32_t) p); + } + printf("\n\r"); + + p = rx_head; + printf("rx_head: 0x%lx ", (uint32_t) rx_head); + while(p != 0) { + p = p->left; + printf("->0x%lx", (uint32_t) p); + } + printf("\n\r"); + + printf("dma_rx: 0x%lx\n", (uint32_t) dma_rx); + printf("dma_tx: 0x%lx\n", (uint32_t) dma_tx); + +} + +inline void bad_packet_bounds(void) { + PRINTF("bad packet bounds! Halting.\n"); + while(1) { continue; } +} + +int count_packets(void) { + volatile int8_t total = -1; + +#if PACKET_STATS + volatile packet_t *pk; + volatile uint8_t tx, rx, free; + volatile int i; + + for(i = 0; i < NUM_PACKETS; i++) { + packet_pool[i].seen = 0; + } + + pk = tx_head; tx = 0; + while( pk != 0 ) { + if(pk->seen == 0) { tx++; } + pk->seen++; + pk = pk->left; + } + pk = rx_head; rx = 0; + while( pk != 0 ) { + if(pk->seen == 0) { rx++; } + pk->seen++; + pk = pk->left; + } + pk = free_head; free = 0; + while( pk != 0 ) { + if(pk->seen == 0) { free++; } + pk->seen++; + pk = pk->left; + } + + total = free + rx + tx; + if(dma_rx && (dma_rx->seen == 0)) { dma_rx->seen++; total++; } + if(dma_tx && (dma_tx->seen == 0)) { dma_tx->seen++; total++; } +#endif /* PACKET_STATS */ + + return total; +} + +void bound_check(volatile packet_t *p) { + volatile int i; + + if((p == 0) || + (p == &dummy_ack)) { return; } + for(i=0; i < NUM_PACKETS; i++) { + if(p == &packet_pool[i]) { return; } + } + + bad_packet_bounds(); +} + + +/* public packet routines */ +/* heads are to the right */ +/* ends are to the left */ +void free_packet(volatile packet_t *p) { + safe_irq_disable(MACA); + + BOUND_CHECK(p); + + if(!p) { PRINTF("free_packet passed packet 0\n\r"); return; } + if(p == &dummy_ack) { return; } + + BOUND_CHECK(free_head); + + p->length = 0; p->offset = 0; + p->left = free_head; p->right = 0; +#if PACKET_STATS + p->seen = 0; + p->post_tx = 0; + p->get_free = 0; + p->rxd = 0; +#endif + + free_head = p; + + BOUND_CHECK(free_head); + + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + + return; +} + +volatile packet_t* get_free_packet(void) { + volatile packet_t *p; + + safe_irq_disable(MACA); + + BOUND_CHECK(free_head); + + p = free_head; + if( p != 0 ) { + free_head = p->left; + free_head->right = 0; + } + + BOUND_CHECK(free_head); + +#if PACKET_STATS + p->get_free++; +#endif + +// print_packets("get_free_packet"); + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + + return p; +} + +void post_receive(void) { + last_post = RX_POST; + /* this sets the rxlen field */ + /* this is undocumented but very important */ + /* you will not receive anything without setting it */ + *MACA_TXLEN = (MAX_PACKET_SIZE << 16); + if(dma_rx == 0) { + dma_rx = get_free_packet(); + if (dma_rx == 0) { + PRINTF("trying to fill MACA_DMARX in post_receieve but out of packet buffers\n\r"); + /* set the sftclock so that we return to the maca_isr */ + *MACA_SFTCLK = *MACA_CLK + RECV_SOFTIMEOUT; /* soft timeout */ + *MACA_TMREN = (1 << maca_tmren_sft); + /* no free buffers, so don't start a reception */ + enable_irq(MACA); + return; + } + } + BOUND_CHECK(dma_rx); + BOUND_CHECK(dma_tx); + *MACA_DMARX = (uint32_t)&(dma_rx->data[0]); + /* with timeout */ + *MACA_SFTCLK = *MACA_CLK + RECV_SOFTIMEOUT; /* soft timeout */ + *MACA_TMREN = (1 << maca_tmren_sft); + /* start the receive sequence */ + *MACA_CONTROL = ( (1 << maca_ctrl_asap) | + ( 4 << PRECOUNT) | + ( fcs_mode << NOFC ) | + (1 << maca_ctrl_auto) | + (1 << maca_ctrl_prm) | + (maca_ctrl_seq_rx)); + /* status bit 10 is set immediately */ + /* then 11, 10, and 9 get set */ + /* they are cleared once we get back to maca_isr */ +} + + +volatile packet_t* rx_packet(void) { + volatile packet_t *p; + safe_irq_disable(MACA); + + BOUND_CHECK(rx_head); + + p = rx_head; + if( p != 0 ) { + rx_head = p->left; + rx_head->right = 0; + } + +#if PACKET_STATS + p->rxd++; +#endif + +// print_packets("rx_packet"); + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + + return p; +} + +void post_tx(void) { + /* set dma tx pointer to the payload */ + /* and set the tx len */ + disable_irq(MACA); + last_post = TX_POST; + dma_tx = tx_head; +#if PACKET_STATS + dma_tx->post_tx++; +#endif + *MACA_TXLEN = (uint32_t)((dma_tx->length) + 2); + *MACA_DMATX = (uint32_t)&(dma_tx->data[ 0 + dma_tx->offset]); + if(dma_rx == 0) { + dma_rx = get_free_packet(); + if (dma_rx == 0) { + dma_rx = &dummy_ack; + PRINTF("trying to fill MACA_DMARX on post_tx but out of packet buffers\n\r"); + } + + } + BOUND_CHECK(dma_rx); + BOUND_CHECK(dma_tx); + *MACA_DMARX = (uint32_t)&(dma_rx->data[0]); + /* disable soft timeout clock */ + /* disable start clock */ + *MACA_TMRDIS = (1 << maca_tmren_sft) | ( 1<< maca_tmren_cpl) | ( 1 << maca_tmren_strt ) ; + + /* set complete clock to long value */ + /* acts like a watchdog in case the MACA locks up */ + *MACA_CPLCLK = *MACA_CLK + CPL_TIMEOUT; + /* enable complete clock */ + *MACA_TMREN = (1 << maca_tmren_cpl); + + enable_irq(MACA); + *MACA_CONTROL = ( (1 << maca_ctrl_prm) | ( 4 << PRECOUNT) | + (maca_ctrl_mode_no_cca << maca_ctrl_mode) | + (1 << maca_ctrl_asap) | + (maca_ctrl_seq_tx)); + /* status bit 10 is set immediately */ + /* then 11, 10, and 9 get set */ + /* they are cleared once we get back to maca_isr */ +} + +void tx_packet(volatile packet_t *p) { + safe_irq_disable(MACA); + + BOUND_CHECK(p); + + if(!p) { PRINTF("tx_packet passed packet 0\n\r"); return; } + if(tx_head == 0) { + /* start a new queue if empty */ + tx_end = p; + tx_end->left = 0; tx_end->right = 0; + tx_head = tx_end; + } else { + /* add p to the end of the queue */ + tx_end->left = p; + p->right = tx_end; + /* move the queue */ + tx_end = p; tx_end->left = 0; + } +// print_packets("tx packet"); + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + + return; +} + +void free_all_packets(void) { + volatile int i; + safe_irq_disable(MACA); + + free_head = 0; + for(i=0; ileft; + if(tx_head == 0) { tx_end = 0; } + free_packet(p); + +// print_packets("free tx head"); + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + + return; +} + +void add_to_rx(volatile packet_t *p) { + safe_irq_disable(MACA); + + BOUND_CHECK(p); + + if(!p) { PRINTF("add_to_rx passed packet 0\n\r"); return; } + p->offset = 1; /* first byte is the length */ + if(rx_head == 0) { + /* start a new queue if empty */ + rx_end = p; + rx_end->left = 0; rx_end->right = 0; + rx_head = rx_end; + } else { + rx_end->left = p; + p->right = rx_end; + rx_end = p; rx_end->left = 0; + } + +// print_packets("add to rx"); + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + + return; +} + +void decode_status(void) { + volatile uint32_t code; + + code = get_field(*MACA_STATUS,CODE); + /* PRINTF("status code 0x%x\n\r",code); */ + + switch(code) + { + case ABORTED: + { +// PRINTF("maca: aborted\n\r"); + ResumeMACASync(); + break; + + } + case NOT_COMPLETED: + { +// PRINTF("maca: not completed\n\r"); + ResumeMACASync(); + break; + + } + case CODE_TIMEOUT: + { +// PRINTF("maca: timeout\n\r"); + ResumeMACASync(); + break; + + } + case NO_ACK: + { + PRINTF("maca: no ack\n\r"); + ResumeMACASync(); + break; + + } + case EXT_TIMEOUT: + { +// PRINTF("maca: ext timeout\n\r"); + ResumeMACASync(); + break; + + } + case EXT_PND_TIMEOUT: + { + PRINTF("maca: ext pnd timeout\n\r"); + ResumeMACASync(); + break; + } + case SUCCESS: + { + //PRINTF("maca: success\n\r"); + ResumeMACASync(); + break; + } + default: + { + PRINTF("status: %x", *MACA_STATUS); + ResumeMACASync(); + + } + } +} + +void maca_isr(void) { + +// print_packets("maca_isr"); + + maca_entry++; + + if (bit_is_set(*MACA_STATUS, maca_status_ovr)) + { PRINTF("maca overrun\n\r"); } + if (bit_is_set(*MACA_STATUS, maca_status_busy)) + { PRINTF("maca busy\n\r"); } + if (bit_is_set(*MACA_STATUS, maca_status_crc)) + { PRINTF("maca crc error\n\r"); } + if (bit_is_set(*MACA_STATUS, maca_status_to)) + { PRINTF("maca timeout\n\r"); } + + if (data_indication_irq()) { + *MACA_CLRIRQ = (1 << maca_irq_di); + dma_rx->length = *MACA_GETRXLVL - 2; /* packet length does not include FCS */ +// PRINTF("maca data ind %x %d\n\r", dma_rx, dma_rx->length); + if(maca_rx_callback != 0) { maca_rx_callback(dma_rx); } + add_to_rx(dma_rx); + dma_rx = 0; + } + if (filter_failed_irq()) { + PRINTF("maca filter failed\n\r"); + ResumeMACASync(); + *MACA_CLRIRQ = (1 << maca_irq_flt); + } + if (checksum_failed_irq()) { + PRINTF("maca checksum failed\n\r"); + ResumeMACASync(); + *MACA_CLRIRQ = (1 << maca_irq_crc); + } + if (softclock_irq()) { + *MACA_CLRIRQ = (1 << maca_irq_sftclk); + } + if (poll_irq()) { + *MACA_CLRIRQ = (1 << maca_irq_poll); + } + if(action_complete_irq()) { + /* PRINTF("maca action complete %d\n\r", get_field(*MACA_CONTROL,SEQUENCE)); */ + if(last_post == TX_POST) { + if(maca_tx_callback != 0) { maca_tx_callback(tx_head); } + dma_tx = 0; + free_tx_head(); + last_post = NO_POST; + } + ResumeMACASync(); + *MACA_CLRIRQ = (1 << maca_irq_acpl); + } + + decode_status(); + + if (*MACA_IRQ != 0) + { PRINTF("*MACA_IRQ %x\n\r", *MACA_IRQ); } + + if(tx_head != 0) { + post_tx(); + } else { + post_receive(); + } +} + + +static uint8_t ram_values[4]; + + +void init_phy(void) +{ +// *MACA_TMREN = (1 << maca_tmren_strt) | (1 << maca_tmren_cpl); + *MACA_CLKDIV = MACA_CLOCK_DIV; + *MACA_WARMUP = 0x00180012; + *MACA_EOFDELAY = 0x00000004; + *MACA_CCADELAY = 0x001a0022; + *MACA_TXCCADELAY = 0x00000025; + *MACA_FRAMESYNC0 = 0x000000A7; + *MACA_CLK = 0x00000008; + *MACA_MASKIRQ = ((1 << maca_irq_rst) | + (1 << maca_irq_acpl) | + (1 << maca_irq_cm) | + (1 << maca_irq_flt) | + (1 << maca_irq_crc) | + (1 << maca_irq_di) | + (1 << maca_irq_sftclk) + ); + *MACA_SLOTOFFSET = 0x00350000; +} + +void reset_maca(void) +{ + volatile uint32_t cnt; + + *MACA_RESET = (1 << maca_reset_rst); + + for(cnt = 0; cnt < 100; cnt++) {}; + + *MACA_RESET = (1 << maca_reset_clkon); + + *MACA_CONTROL = maca_ctrl_seq_nop; + + for(cnt = 0; cnt < 400000; cnt++) {}; + + /* Clear all interrupts. */ + *MACA_CLRIRQ = 0xffff; +} + + +/* + 004030c4 : + 4030c4: 4806 ldr r0, [pc, #24] (4030e0 ) // r0 gets base 0x80009a00 + 4030c6: 6881 ldr r1, [r0, #8] // r1 gets *(0x80009a08) + 4030c8: 4806 ldr r0, [pc, #24] (4030e4 ) // r0 gets 0x0000f7df + 4030ca: 4308 orrs r0, r1 // or them, r0 has it + 4030cc: 4904 ldr r1, [pc, #16] (4030e0 ) // r1 gets base 0x80009a00 + 4030ce: 6088 str r0, [r1, #8] // put r0 into 0x80009a08 + 4030d0: 0008 lsls r0, r1, #0 // r0 gets r1, r0 is the base now + 4030d2: 4905 ldr r1, [pc, #20] (4030e8 ) // r1 gets 0x00ffffff + 4030d4: 60c1 str r1, [r0, #12] // put 0x00ffffff into base+12 + 4030d6: 0b09 lsrs r1, r1, #12 // r1 = 0x00ffffff >> 12 + 4030d8: 6101 str r1, [r0, #16] // put r1 base+16 + 4030da: 2110 movs r1, #16 // r1 gets 16 + 4030dc: 6001 str r1, [r0, #0] // put r1 in the base + 4030de: 4770 bx lr // return + 4030e0: 80009a00 .word 0x80009a00 + 4030e4: 0000f7df .word 0x0000f7df + 4030e8: 00ffffff .word 0x00ffffff +*/ + +/* tested and is good */ +#define RF_BASE 0x80009a00 +void flyback_init(void) { + uint32_t val8, or; + + val8 = *(volatile uint32_t *)(RF_BASE+8); + or = val8 | 0x0000f7df; + *(volatile uint32_t *)(RF_BASE+8) = or; + *(volatile uint32_t *)(RF_BASE+12) = 0x00ffffff; + *(volatile uint32_t *)(RF_BASE+16) = (((uint32_t)0x00ffffff)>>12); + *(volatile uint32_t *)(RF_BASE) = 16; + /* good luck and godspeed */ +} + +#define MAX_SEQ1 2 +const uint32_t addr_seq1[MAX_SEQ1] = { + 0x80003048, + 0x8000304c, +}; + +const uint32_t data_seq1[MAX_SEQ1] = { + 0x00000f78, + 0x00607707, +}; + + +#define MAX_SEQ2 2 +const uint32_t addr_seq2[MAX_SEQ2] = { + 0x8000a050, + 0x8000a054, +}; + +const uint32_t data_seq2[MAX_SEQ2] = { + 0x0000047b, + 0x0000007b, +}; + +#define MAX_CAL3_SEQ1 3 +const uint32_t addr_cal3_seq1[MAX_CAL3_SEQ1] = { 0x80009400,0x80009a04,0x80009a00, }; +const uint32_t data_cal3_seq1[MAX_CAL3_SEQ1] = {0x00020017,0x8185a0a4,0x8c900025, }; + +#define MAX_CAL3_SEQ2 2 +const uint32_t addr_cal3_seq2[MAX_CAL3_SEQ2] = { 0x80009a00,0x80009a00,}; +const uint32_t data_cal3_seq2[MAX_CAL3_SEQ2] = { 0x8c900021,0x8c900027,}; + +#define MAX_CAL3_SEQ3 1 +const uint32_t addr_cal3_seq3[MAX_CAL3_SEQ3] = { 0x80009a00 }; +const uint32_t data_cal3_seq3[MAX_CAL3_SEQ3] = { 0x8c900000 }; + +#define MAX_CAL5 4 +const uint32_t addr_cal5[MAX_CAL5] = { + 0x80009400, + 0x8000a050, + 0x8000a054, + 0x80003048, +}; +const uint32_t data_cal5[MAX_CAL5] = { + 0x00000017, + 0x00000000, + 0x00000000, + 0x00000f00, +}; + +#define MAX_DATA 43 +const uint32_t addr_reg_rep[MAX_DATA] = { 0x80004118,0x80009204,0x80009208,0x8000920c,0x80009210,0x80009300,0x80009304,0x80009308,0x8000930c,0x80009310,0x80009314,0x80009318,0x80009380,0x80009384,0x80009388,0x8000938c,0x80009390,0x80009394,0x8000a008,0x8000a018,0x8000a01c,0x80009424,0x80009434,0x80009438,0x8000943c,0x80009440,0x80009444,0x80009448,0x8000944c,0x80009450,0x80009460,0x80009464,0x8000947c,0x800094e0,0x800094e4,0x800094e8,0x800094ec,0x800094f0,0x800094f4,0x800094f8,0x80009470,0x8000981c,0x80009828 }; + +const uint32_t data_reg_rep[MAX_DATA] = { 0x00180012,0x00000605,0x00000504,0x00001111,0x0fc40000,0x20046000,0x4005580c,0x40075801,0x4005d801,0x5a45d800,0x4a45d800,0x40044000,0x00106000,0x00083806,0x00093807,0x0009b804,0x000db800,0x00093802,0x00000015,0x00000002,0x0000000f,0x0000aaa0,0x01002020,0x016800fe,0x8e578248,0x000000dd,0x00000946,0x0000035a,0x00100010,0x00000515,0x00397feb,0x00180358,0x00000455,0x00000001,0x00020003,0x00040014,0x00240034,0x00440144,0x02440344,0x04440544,0x0ee7fc00,0x00000082,0x0000002a }; + +void maca_off(void) { + disable_irq(MACA); + /* turn off the radio regulators */ + reg(0x80003048) = 0x00000f00; + /* hold the maca in reset */ + maca_reset = maca_reset_rst; +} + +void maca_on(void) { + /* turn the radio regulators back on */ + reg(0x80003048) = 0x00000f78; + /* reinitialize the phy */ + reset_maca(); + init_phy(); + + enable_irq(MACA); + *INTFRC = (1 << INT_NUM_MACA); +} + +/* initialized with 0x4c */ +uint8_t ctov[16] = { + 0x0b, + 0x0b, + 0x0b, + 0x0a, + 0x0d, + 0x0d, + 0x0c, + 0x0c, + 0x0f, + 0x0e, + 0x0e, + 0x0e, + 0x11, + 0x10, + 0x10, + 0x0f, +}; + +/* get_ctov thanks to Umberto */ + +#define _INIT_CTOV_WORD_1 0x00dfbe77 +#define _INIT_CTOV_WORD_2 0x023126e9 +uint8_t get_ctov( uint32_t r0, uint32_t r1 ) +{ + + r0 = r0 * _INIT_CTOV_WORD_1; + r0 += ( r1 << 22 ); + r0 += _INIT_CTOV_WORD_2; + + r0 = (uint32_t)(((int32_t)r0) >> 25); + + return (uint8_t)r0; +} + + +/* radio_init has been tested to be good */ +void radio_init(void) { + volatile uint32_t i; + /* sequence 1 */ + for(i=0; i>17) & 1) !=1) { continue; } /* wait for the bypass to take */ + *(volatile uint32_t *)(0x80003048) = 0x00000fa4; /* start the regulators */ + for(i=0; i<0x161a8; i++) { continue; } /* wait for the bypass to take */ + + init_from_flash(0x1F000); + + PRINTF("ram_values:\n\r"); + for(i=0; i<4; i++) { + PRINTF(" 0x%02x\n\r",ram_values[i]); + } + + PRINTF("radio_init: ctov parameter 0x%02x\n\r",ram_values[3]); + for(i=0; i<16; i++) { + ctov[i] = get_ctov(i,ram_values[3]); + PRINTF("radio_init: ctov[%d] = 0x%02x\n\r",i,ctov[i]); + } + + +} + +const uint32_t PSMVAL[19] = { + 0x0000080f, + 0x0000080f, + 0x0000080f, + 0x0000080f, + 0x0000081f, + 0x0000081f, + 0x0000081f, + 0x0000080f, + 0x0000080f, + 0x0000080f, + 0x0000001f, + 0x0000000f, + 0x0000000f, + 0x00000816, + 0x0000001b, + 0x0000000b, + 0x00000802, + 0x00000817, + 0x00000003, +}; + +const uint32_t PAVAL[19] = { + 0x000022c0, + 0x000022c0, + 0x000022c0, + 0x00002280, + 0x00002303, + 0x000023c0, + 0x00002880, + 0x000029f0, + 0x000029f0, + 0x000029f0, + 0x000029c0, + 0x00002bf0, + 0x000029f0, + 0x000028a0, + 0x00002800, + 0x00002ac0, + 0x00002880, + 0x00002a00, + 0x00002b00, +}; + +const uint32_t AIMVAL[19] = { + 0x000123a0, + 0x000163a0, + 0x0001a3a0, + 0x0001e3a0, + 0x000223a0, + 0x000263a0, + 0x0002a3a0, + 0x0002e3a0, + 0x000323a0, + 0x000363a0, + 0x0003a3a0, + 0x0003a3a0, + 0x0003e3a0, + 0x000423a0, + 0x000523a0, + 0x000423a0, + 0x0004e3a0, + 0x0004e3a0, + 0x0004e3a0, +}; + +#define RF_REG 0x80009400 +void set_demodulator_type(uint8_t demod) { + uint32_t val = reg(RF_REG); + if(demod == DEMOD_NCD) { + val = (val & ~1); + } else { + val = (val | 1); + } + reg(RF_REG) = val; +} + +/* tested and seems to be good */ +#define ADDR_POW1 0x8000a014 +#define ADDR_POW2 ADDR_POW1 + 12 +#define ADDR_POW3 ADDR_POW1 + 64 +void set_power(uint8_t power) { + safe_irq_disable(MACA); + + reg(ADDR_POW1) = PSMVAL[power]; + +/* see http://devl.org/pipermail/mc1322x/2009-October/000065.html */ +/* reg(ADDR_POW2) = (ADDR_POW1>>18) | PAVAL[power]; */ +#ifdef USE_PA + reg(ADDR_POW2) = 0xffffdfff & PAVAL[power]; /* single port */ +#else + reg(ADDR_POW2) = 0x00002000 | PAVAL[power]; /* dual port */ +#endif + + reg(ADDR_POW3) = AIMVAL[power]; + + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } + +} + +const uint8_t VCODivI[16] = { + 0x2f, + 0x2f, + 0x2f, + 0x2f, + 0x2f, + 0x2f, + 0x2f, + 0x2f, + 0x30, + 0x30, + 0x30, + 0x2f, + 0x30, + 0x30, + 0x30, + 0x30, +}; + +const uint32_t VCODivF[16] = { + 0x00355555, + 0x006aaaaa, + 0x00a00000, + 0x00d55555, + 0x010aaaaa, + 0x01400000, + 0x01755555, + 0x01aaaaaa, + 0x01e00000, + 0x00155555, + 0x004aaaaa, + 0x00800000, + 0x00b55555, + 0x00eaaaaa, + 0x01200000, + 0x01555555, +}; + +/* tested good */ +#define ADDR_CHAN1 0x80009800 +#define ADDR_CHAN2 (ADDR_CHAN1+12) +#define ADDR_CHAN3 (ADDR_CHAN1+16) +#define ADDR_CHAN4 (ADDR_CHAN1+48) +void set_channel(uint8_t chan) { + volatile uint32_t tmp; + safe_irq_disable(MACA); + + tmp = reg(ADDR_CHAN1); + tmp = tmp & 0xbfffffff; + reg(ADDR_CHAN1) = tmp; + + reg(ADDR_CHAN2) = VCODivI[chan]; + reg(ADDR_CHAN3) = VCODivF[chan]; + + tmp = reg(ADDR_CHAN4); + tmp = tmp | 2; + reg(ADDR_CHAN4) = tmp; + + tmp = reg(ADDR_CHAN4); + tmp = tmp | 4; + reg(ADDR_CHAN4) = tmp; + + tmp = tmp & 0xffffe0ff; + tmp = tmp | (((ctov[chan])<<8)&0x1F00); + reg(ADDR_CHAN4) = tmp; + /* duh! */ + irq_restore(); + if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } +} + +#define ROM_END 0x0013ffff +#define ENTRY_EOF 0x00000e0f +/* processes up to 4 words of initialization entries */ +/* returns the number of words processed */ +uint32_t exec_init_entry(volatile uint32_t *entries, uint8_t *valbuf) +{ + volatile uint32_t i; + if(entries[0] <= ROM_END) { + if (entries[0] == 0) { + /* do delay command*/ + PRINTF("init_entry: delay 0x%08x\n\r", entries[1]); + for(i=0; i= 16) && + (entries[0] < 0xfff1)) { + /* store bytes in valbuf */ + PRINTF("init_entry: store in valbuf 0x%02x position %d\n\r", entries[1],(entries[0]>>4)-1); + valbuf[(entries[0]>>4)-1] = entries[1]; + return 2; + } else if (entries[0] == ENTRY_EOF) { + PRINTF("init_entry: eof "); + return 0; + } else { + /* invalid command code */ + PRINTF("init_entry: invaild code 0x%08x\n\r",entries[0]); + return 0; + } + } else { /* address isn't in ROM space */ + /* do store value in address command */ + PRINTF("init_entry: address value pair - *0x%08x = 0x%08x\n\r",entries[0],entries[1]); + reg(entries[0]) = entries[1]; + return 2; + } +} + + +#define FLASH_INIT_MAGIC 0x00000abc +uint32_t init_from_flash(uint32_t addr) { + nvmType_t type=0; + nvmErr_t err; + volatile uint32_t buf[8]; + volatile uint32_t len; + volatile uint32_t i=0,j; + + err = nvm_detect(gNvmInternalInterface_c, &type); + PRINTF("nvm_detect returned type 0x%08x err 0x%02x\n\r", type, err); + + nvm_setsvar(0); + err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, addr, 8); + i+=8; + PRINTF("nvm_read returned: 0x%02x\n\r",err); + + for(j=0; j<4; j++) { + PRINTF("0x%08x\n\r",buf[j]); + } + + if(buf[0] == FLASH_INIT_MAGIC) { + len = buf[1] & 0x0000ffff; + while(i < (len-4)) { + err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, addr+i, 32); + i += 4*exec_init_entry(buf, ram_values); + } + return i; + } else { + return 0; + } + +} + +/* + * Do the ABORT-Wait-NOP-Wait sequence in order to prevent MACA malfunctioning. + * This seqeunce is synchronous and no interrupts should be triggered when it is done. + */ +void ResumeMACASync(void) +{ + volatile uint32_t clk, TsmRxSteps, LastWarmupStep, LastWarmupData, LastWarmdownStep, LastWarmdownData; +// bool_t tmpIsrStatus; + volatile uint32_t i; + safe_irq_disable(MACA); + +// ITC_DisableInterrupt(gMacaInt_c); +// AppInterrupts_ProtectFromMACAIrq(tmpIsrStatus); <- Original from MAC code, but not sure how is it implemented + + /* Manual TSM modem shutdown */ + + /* read TSM_RX_STEPS */ + TsmRxSteps = (*((volatile uint32_t *)(0x80009204))); + + /* isolate the RX_WU_STEPS */ + /* shift left to align with 32-bit addressing */ + LastWarmupStep = (TsmRxSteps & 0x1f) << 2; + /* Read "current" TSM step and save this value for later */ + LastWarmupData = (*((volatile uint32_t *)(0x80009300 + LastWarmupStep))); + + /* isolate the RX_WD_STEPS */ + /* right-shift bits down to bit 0 position */ + /* left-shift to align with 32-bit addressing */ + LastWarmdownStep = ((TsmRxSteps & 0x1f00) >> 8) << 2; + /* write "last warmdown data" to current TSM step to shutdown rx */ + LastWarmdownData = (*((volatile uint32_t *)(0x80009300 + LastWarmdownStep))); + (*((volatile uint32_t *)(0x80009300 + LastWarmupStep))) = LastWarmdownData; + + /* Abort */ + MACA_WRITE(maca_control, 1); + + /* Wait ~8us */ + for (clk = maca_clk, i = 0; maca_clk - clk < 3 && i < 300; i++) + ; + + /* NOP */ + MACA_WRITE(maca_control, 0); + + /* Wait ~8us */ + for (clk = maca_clk, i = 0; maca_clk - clk < 3 && i < 300; i++) + ; + + + /* restore original "last warmup step" data to TSM (VERY IMPORTANT!!!) */ + (*((volatile uint32_t *)(0x80009300 + LastWarmupStep))) = LastWarmupData; + + + + /* Clear all MACA interrupts - we should have gotten the ABORT IRQ */ + MACA_WRITE(maca_clrirq, 0xFFFF); + +// AppInterrupts_UnprotectFromMACAIrq(tmpIsrStatus); <- Original from MAC code, but not sure how is it implemented +// ITC_EnableInterrupt(gMacaInt_c); +// enable_irq(MACA); + irq_restore(); + +} diff --git a/cpu/mc1322x/lib/nvm.c b/cpu/mc1322x/lib/nvm.c new file mode 100644 index 000000000..28cb828f4 --- /dev/null +++ b/cpu/mc1322x/lib/nvm.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: nvm.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include "nvm.h" + +nvmErr_t (*nvm_detect) +(nvmInterface_t nvmInterface,nvmType_t* pNvmType) += (void *) 0x00006cb9; + +nvmErr_t (*nvm_read) +(nvmInterface_t nvmInterface , nvmType_t nvmType , void *pDest, uint32_t address, uint32_t numBytes) += (void *) 0x00006d69; + +nvmErr_t (*nvm_write) +(nvmInterface_t nvmInterface, nvmType_t nvmType ,void *pSrc, uint32_t address, uint32_t numBytes) += (void *) 0x00006ec5; + +nvmErr_t (*nvm_erase) +(nvmInterface_t nvmInterface, nvmType_t nvmType ,uint32_t sectorBitfield) += (void*) 0x00006e05; + +void(*nvm_setsvar) +(uint32_t zero_for_awesome) += (void *)0x00007085; + diff --git a/cpu/mc1322x/lib/uart1.c b/cpu/mc1322x/lib/uart1.c new file mode 100644 index 000000000..cc64fa743 --- /dev/null +++ b/cpu/mc1322x/lib/uart1.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: uart1.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +volatile char u1_tx_buf[1024]; +volatile uint32_t u1_head, u1_tail; + +void uart1_isr(void) { + while( *UART1_UTXCON != 0 ) { + if (u1_head == u1_tail) { + disable_irq(UART1); + return; + } + *UART1_UDATA = u1_tx_buf[u1_tail]; + u1_tail++; + if (u1_tail >= sizeof(u1_tx_buf)) + u1_tail = 0; + } +} + +void uart1_putc(char c) { + /* disable UART1 since */ + /* UART1 isr modifies u1_head and u1_tail */ + disable_irq(UART1); + + if( (u1_head == u1_tail) && + (*UART1_UTXCON != 0)) { + *UART1_UDATA = c; + } else { + u1_tx_buf[u1_head] = c; + u1_head += 1; + if (u1_head >= sizeof(u1_tx_buf)) + u1_head = 0; + if (u1_head == u1_tail) /* drop chars when no room */ + return; + enable_irq(UART1); + } +} + +uint8_t uart1_getc(void) { + while(uart1_can_get() == 0) { continue; } + return *UART1_UDATA; +} diff --git a/cpu/mc1322x/tests/Makefile b/cpu/mc1322x/tests/Makefile new file mode 100644 index 000000000..5e8dff878 --- /dev/null +++ b/cpu/mc1322x/tests/Makefile @@ -0,0 +1,44 @@ +MC1322X := .. + +# all off the common objects for each target +# a COBJ is made for EACH board and goes the obj_$(BOARD)_board directory +# board specific code is OK in these files +COBJS := tests.o put.o + +# all of the target programs to build +TARGETS := blink-red blink-green blink-blue blink-white blink-allio \ + uart1-loopback \ + tmr tmr-ints \ + sleep \ + printf + +# these targets are built with space reserved for variables needed by ROM services +# this space is initialized with a rom call to rom_data_init +TARGETS_WITH_ROM_VARS := nvm-read nvm-write romimg flasher \ + rftest-rx rftest-tx \ + per + +################################################## +# you shouldn't need to edit anything below here # +################################################## + +# This Makefile includes the default rule at the top +# it needs to be included first +-include $(MC1322X)/Makefile.include + +# this rule will become the default_goal if +# $(MC1322X)/Makefile.include doesn't exist it will try to update the +# submodule, check if $(MC1322X) exists, and if it does +# try make again +submodule: + git submodule update --init + if [ ! -d $(MC1322X) ] ; then echo "*** cannot find MC1322X directory $(MC1322X)" ; exit 2; fi + $(MAKE) + +.PHONY: submodule + + + + + + diff --git a/cpu/mc1322x/tests/blink-allio.c b/cpu/mc1322x/tests/blink-allio.c new file mode 100644 index 000000000..6c5ba5c1b --- /dev/null +++ b/cpu/mc1322x/tests/blink-allio.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: blink-allio.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#define DELAY 400000 + +void main(void) { + + *GPIO_FUNC_SEL0 = 0xffffffff; + *GPIO_FUNC_SEL1 = 0xffffffff; + *GPIO_FUNC_SEL2 = 0xffffffff; + *GPIO_FUNC_SEL3 = 0xffffffff; + + *GPIO_PAD_DIR0 = 0xffffffff; + *GPIO_PAD_DIR1 = 0xffffffff; + + volatile uint32_t i; + + while(1) { + + *GPIO_DATA0 = 0xffffffff; + *GPIO_DATA1 = 0xffffffff; + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: blink-blue.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#define DELAY 400000 + +#define LED (1ULL << LED_BLUE) + +void main(void) { + volatile uint32_t i; + + gpio_pad_dir(LED); + + while(1) { + + gpio_data(LED); + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: blink-green.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#define DELAY 400000 + +#define LED (1ULL << LED_GREEN) + +void main(void) { + volatile uint32_t i; + + gpio_pad_dir(LED); + + while(1) { + + gpio_data(LED); + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: blink-red.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#define DELAY 400000 + +#define LED (1ULL << LED_RED) + +void main(void) { + volatile uint32_t i; + + gpio_pad_dir(LED); + + while(1) { + + gpio_data(LED); + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: blink-white.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "led.h" + +#define DELAY 400000 + +#define LED LED_WHITE + +void main(void) { + volatile uint32_t i; + + gpio_pad_dir(LED); + + while(1) { + + gpio_data(LED); + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: config.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef CONFIG_H +#define CONFIG_H + +/* Baud rate */ +#define MOD 9999 +/* 230400 bps, INC=767, MOD=9999, 24Mhz 16x samp */ +/* 115200 bps, INC=767, MOD=9999, 24Mhz 8x samp */ +#define INC 767 +/* 921600 bps, MOD=9999, 24Mhz 16x samp */ +//#define INC 3071 +#define SAMP UCON_SAMP_8X +//#define SAMP UCON_SAMP_16X + +/* use uart1 for console */ +#define uart_init uart1_init + +/* nvm-read */ +#define READ_ADDR 0x1E000 +#define READ_NBYTES 8 + +/* nvm-write */ +#define WRITE_NBYTES 8 +#define WRITE_ADDR 0x1e000 +#define WRITEVAL0 0xdeadbeef +#define WRITEVAL1 0xdeadbeef + +/* romimg */ +#define DUMP_BASE 0x00000000 +#define DUMP_LEN 0x00014000 + +/* flasher */ +/* if both BOOT_OK and BOOT_SECURE are 0 then flash image will not be bootable */ +/* if both are 1 then flash image will be secure */ +#define BOOT_OK 1 +#define BOOT_SECURE 0 + +/* sleep */ +#undef USE_32KHZ /* board should have a HAS_32KHZ define */ + +#endif diff --git a/cpu/mc1322x/tests/flasher.c b/cpu/mc1322x/tests/flasher.c new file mode 100644 index 000000000..3846649f6 --- /dev/null +++ b/cpu/mc1322x/tests/flasher.c @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: flasher.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +#define DEBUG 1 +#if DEBUG +#define dbg_putchr(...) putchr(__VA_ARGS__) +#define dbg_putstr(...) putstr(__VA_ARGS__) +#define dbg_put_hex(...) put_hex(__VA_ARGS__) +#define dbg_put_hex16(...) put_hex16(__VA_ARGS__) +#define dbg_put_hex32(...) put_hex32(__VA_ARGS__) +#else +#define dbg_putchr(...) +#define dbg_putstr(...) +#define dbg_put_hex(...) +#define dbg_put_hex16(...) +#define dbg_put_hex32(...) +#endif + +uint8_t getc(void) +{ + volatile uint8_t c; + while(*UART1_URXCON == 0); + + c = *UART1_UDATA; + return c; +} + + +void flushrx(void); +uint32_t to_u32(volatile uint32_t *c); + +enum parse_states { + SCAN_X, + READ_CHARS, + PROCESS, + MAX_STATE, +}; + +void main(void) { + nvmType_t type=0; + nvmErr_t err; + volatile uint8_t c; + volatile uint32_t i; + volatile uint32_t buf[4]; + volatile uint32_t len=0; + volatile uint32_t state = SCAN_X; + volatile uint32_t addr,data; + + + uart_init(INC, MOD, SAMP); + disable_irq(UART1); + + vreg_init(); + + dbg_putstr("Detecting internal nvm\n\r"); + + err = nvm_detect(gNvmInternalInterface_c, &type); + + dbg_putstr("nvm_detect returned: 0x"); + dbg_put_hex(err); + dbg_putstr(" type is: 0x"); + dbg_put_hex32(type); + dbg_putstr("\n\r"); + + /* erase the flash */ + err = nvm_erase(gNvmInternalInterface_c, type, 0x4fffffff); + + dbg_putstr("nvm_erase returned: 0x"); + dbg_put_hex(err); + dbg_putstr("\n\r"); + + dbg_putstr(" type is: 0x"); + dbg_put_hex32(type); + dbg_putstr("\n\r"); + + /* say we are ready */ + len = 0; + putstr("ready"); + flushrx(); + + /* read the length */ + for(i=0; i<4; i++) { + c = uart1_getc(); + /* bail if the first byte of the length is zero */ + len += (c<<(i*8)); + } + + dbg_putstr("len: "); + dbg_put_hex32(len); + dbg_putstr("\n\r"); + + /* write the OKOK magic */ + +#if BOOT_OK + ((uint8_t *)buf)[0] = 'O'; ((uint8_t *)buf)[1] = 'K'; ((uint8_t *)buf)[2] = 'O'; ((uint8_t *)buf)[3] = 'K'; +#elif BOOT_SECURE + ((uint8_t *)buf)[0] = 'S'; ((uint8_t *)buf)[1] = 'E'; ((uint8_t *)buf)[2] = 'C'; ((uint8_t *)buf)[3] = 'U'; +#else + ((uint8_t *)buf)[0] = 'N'; ((uint8_t *)buf)[1] = 'O'; ((uint8_t *)buf)[2] = 'N'; ((uint8_t *)buf)[3] = 'O'; +#endif + + dbg_putstr(" type is: 0x"); + dbg_put_hex32(type); + dbg_putstr("\n\r"); + + err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4); + + dbg_putstr("nvm_write returned: 0x"); + dbg_put_hex(err); + dbg_putstr("\n\r"); + + /* write the length */ + err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)&len, 4, 4); + + /* read a byte, write a byte */ + /* byte at a time will make this work as a contiki process better */ + /* for OTAP */ + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: led.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef LED_H +#define LED_H + +#define LED_YELLOW ((1ULL << LED_RED) | (1ULL << LED_GREEN) ) +#define LED_PURPLE ((1ULL << LED_RED) | (1ULL << LED_BLUE)) +#define LED_CYAN ( (1ULL << LED_GREEN) | (1ULL << LED_BLUE)) +#define LED_WHITE ((1ULL << LED_RED) | (1ULL << LED_GREEN) | (1ULL << LED_BLUE)) + +#endif diff --git a/cpu/mc1322x/tests/nvm-read.c b/cpu/mc1322x/tests/nvm-read.c new file mode 100644 index 000000000..c7011f70e --- /dev/null +++ b/cpu/mc1322x/tests/nvm-read.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: nvm-read.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +void main(void) { + nvmType_t type=0; + nvmErr_t err; + uint32_t buf[READ_NBYTES/4]; + uint32_t i; + + uart_init(INC, MOD, SAMP); + + print_welcome("nvm-read"); + + vreg_init(); + + putstr("Detecting internal nvm\n\r"); + + err = nvm_detect(gNvmInternalInterface_c, &type); + + putstr("nvm_detect returned: 0x"); + put_hex(err); + putstr(" type is: 0x"); + put_hex32(type); + putstr("\n\r"); + + nvm_setsvar(0); + + err = nvm_read(gNvmInternalInterface_c, type, (uint8_t *)buf, READ_ADDR, READ_NBYTES); + putstr("nvm_read returned: 0x"); + put_hex(err); + putstr("\n\r"); + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: nvm-write.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +void main(void) { + nvmType_t type=0; + nvmErr_t err; + uint32_t buf[WRITE_NBYTES/4]; + uint32_t i; + + uart_init(INC, MOD, SAMP); + + print_welcome("nvm-write"); + + vreg_init(); + + putstr("Detecting internal nvm\n\r"); + + err = nvm_detect(gNvmInternalInterface_c, &type); + + putstr("nvm_detect returned: 0x"); + put_hex(err); + putstr(" type is: 0x"); + put_hex32(type); + putstr("\n\r"); + + + buf[0] = WRITEVAL0; + buf[1] = WRITEVAL1; + + err = nvm_erase(gNvmInternalInterface_c, type, 0x40000000); /* erase sector 30 --- sector 31 is the 'secret zone' */ + putstr("nvm_erase returned: 0x"); + put_hex(err); + putstr("\n\r"); + + err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, WRITE_ADDR, WRITE_NBYTES); + putstr("nvm_write returned: 0x"); + put_hex(err); + putstr("\n\r"); + putstr("writing\n\r"); + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: per.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include +#include + +#include "tests.h" +#include "config.h" + +/* This program communicates with itself and determines the packet */ +/* error rate (PER) under a variety of powers and packet sizes */ +/* Each test the packets are sent and received as fast as possible */ + +/* The program first scans on channel 11 and attempts to open a test */ +/* session with a node. After opening a session, the nodes begin the */ +/* test sequence */ + +/* how long to wait between session requests */ +#define SESSION_REQ_TIMEOUT 10000 /* phony seconds */ + +enum STATES { + SCANNING, + MAX_STATE +}; + +typedef uint32_t ptype_t; +enum PACKET_TYPE { + PACKET_SESS_REQ, + MAX_PACKET_TYPE +}; +/* get protocol level packet type */ +/* this is not 802.15.4 packet type */ +ptype_t get_packet_type(packet_t * p __attribute__((unused))) { + return MAX_PACKET_TYPE; +} + +typedef uint32_t session_id_t; + + + +/* phony get_time */ +uint32_t get_time(void) { + static volatile int32_t cur_time = 0; + cur_time++; + return cur_time; +} + + +#define random_short_addr() (*MACA_RANDOM & ones(sizeof(uint16_t)*8)) + +void build_session_req(volatile packet_t *p) { + static uint8_t count = 0; + p->length = 4; p->offset = 0; + p->data[0] = 0xff; + p->data[1] = 0x01; + p->data[2] = 0x02; + p->data[3] = count++; + return; +} + +void session_req(uint16_t addr __attribute__((unused))) { + static volatile int time = 0; + volatile packet_t *p; + + if((get_time() - time) > SESSION_REQ_TIMEOUT) { + time = get_time(); + if((p = get_free_packet())) { + build_session_req(p); + tx_packet(p); + } + } + return; +} + +session_id_t open_session(uint16_t addr __attribute((unused))) { return 0; } + +void main(void) { + uint32_t state; + volatile packet_t *p; + session_id_t sesid; + ptype_t type; + uint16_t addr, my_addr; + + /* trim the reference osc. to 24MHz */ + pack_XTAL_CNTL(CTUNE_4PF, CTUNE, FTUNE, IBIAS); + + uart_init(INC, MOD, SAMP); + + vreg_init(); + + maca_init(); + + set_power(0x0f); /* 0dbm */ + set_channel(0); /* channel 11 */ + + /* generate a random short address */ + my_addr = random_short_addr(); + + /* sets up tx_on, should be a board specific item */ + *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2)); + gpio_pad_dir_set( 1ULL << 44 ); + + print_welcome("Packet error test"); + + state = SCANNING; + while(1) { + + switch(state) { + case SCANNING: + if((p = rx_packet())) { + /* extract what we need and free the packet */ + printf("Recv: "); + print_packet(p); + type = get_packet_type((packet_t *) p); + addr = 0; /* FIXME */ + free_packet(p); + /* pick a new address if someone else is using ours */ + if(addr == my_addr) { + my_addr = random_short_addr(); + printf("DUP addr received, changing to new addr 0x%x02\n\r",my_addr); + } + /* if we have a packet */ + /* check if it's a session request beacon */ + if(type == PACKET_SESS_REQ) { + /* try to start a session */ + sesid = open_session(addr); + } + } else { + session_req(my_addr); + } + break; + default: + break; + } + + + } + +} + diff --git a/cpu/mc1322x/tests/printf.c b/cpu/mc1322x/tests/printf.c new file mode 100644 index 000000000..c54f17d87 --- /dev/null +++ b/cpu/mc1322x/tests/printf.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: printf.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include +#include + +#include "tests.h" +#include "config.h" + +#define print_size(x) do { \ + printf("sizeof("); \ + printf(#x); \ + printf("): %d\n", sizeof(x)); \ + } while(0) + +FILE *stderr; + +void __assert_fail(void) { + return; +} + +int fputs(const char *s, FILE *stream) { + return 0; +} + +size_t fwrite(const void *ptr, size_t size, size_t nmemb, + FILE *stream) { + return 0; +} + +int main(void) +{ + char *ptr = "Hello world!"; + char *np = 0; + int i = 5; + unsigned int bs = sizeof(int)*8; + int mi; +// char buf[80]; + + uart_init(INC, MOD, SAMP); + + print_size(int8_t); + print_size(uint8_t); + print_size(int16_t); + print_size(uint16_t); + print_size(int32_t); + print_size(uint32_t); + print_size(int64_t); + print_size(uint64_t); + + mi = (1 << (bs-1)) + 1; + printf("%s\n", ptr); + printf("printf test\n"); + printf("%s is null pointer\n", np); + printf("%d = 5\n", i); + printf("%d = - max int\n", mi); + printf("char %c = 'a'\n", 'a'); + printf("hex %x = ff\n", 0xff); + printf("hex %02x = 00\n", 0); + printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3); + printf("%d %s(s)", 0, "message"); + printf("\n"); + printf("%d %s(s) with %%\n", 0, "message"); + + printf("sqrt(5) * 100 = %d\n", (int) (sqrt(5)*100)); + +// sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf); +// sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf); +// sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf); +// sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf); +// sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf); +// sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf); +// sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf); +// sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf); + + while(1) { continue; } +} + +/* + * this should display (on 32bit int machine) : + * + * Hello world! + * printf test + * (null) is null pointer + * 5 = 5 + * -2147483647 = - max int + * char a = 'a' + * hex ff = ff + * hex 00 = 00 + * signed -3 = unsigned 4294967293 = hex fffffffd + * 0 message(s) + * 0 message(s) with % + * justif: "left " + * justif: " right" + * 3: 0003 zero padded + * 3: 3 left justif. + * 3: 3 right justif. + * -3: -003 zero padded + * -3: -3 left justif. + * -3: -3 right justif. + */ diff --git a/cpu/mc1322x/tests/put.c b/cpu/mc1322x/tests/put.c new file mode 100644 index 000000000..74eeecc09 --- /dev/null +++ b/cpu/mc1322x/tests/put.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: put.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +const uint8_t hex[16]={'0','1','2','3','4','5','6','7', + '8','9','a','b','c','d','e','f'}; + +void putchr(char c) { + while(*UART1_UTXCON == 31); + /* wait for there to be room in the buffer */ +// while( *UART1_UTXCON == 0 ) { continue; } + *UART1_UDATA = c; +} + +void putstr(char *s) { + while(s && *s!=0) { + putchr(*s++); + } +} + +void put_hex(uint8_t x) +{ + putchr(hex[x >> 4]); + putchr(hex[x & 15]); +} + +void put_hex16(uint16_t x) +{ + put_hex((x >> 8) & 0xFF); + put_hex((x) & 0xFF); +} + +void put_hex32(uint32_t x) +{ + put_hex((x >> 24) & 0xFF); + put_hex((x >> 16) & 0xFF); + put_hex((x >> 8) & 0xFF); + put_hex((x) & 0xFF); +} diff --git a/cpu/mc1322x/tests/put.h b/cpu/mc1322x/tests/put.h new file mode 100644 index 000000000..03f0ffe5d --- /dev/null +++ b/cpu/mc1322x/tests/put.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: put.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef PUT_H +#define PUT_H + +void putchr(char c); +void putstr(char *s); +void put_hex(uint8_t x); +void put_hex16(uint16_t x); +void put_hex32(uint32_t x); + +#endif diff --git a/cpu/mc1322x/tests/rftest-rx.c b/cpu/mc1322x/tests/rftest-rx.c new file mode 100644 index 000000000..bfb65be58 --- /dev/null +++ b/cpu/mc1322x/tests/rftest-rx.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: rftest-rx.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include +#include + +#include "tests.h" +#include "config.h" + +#define LED LED_GREEN + +void maca_rx_callback(volatile packet_t *p) { + (void)p; + gpio_data_set(1ULL<< LED); + gpio_data_reset(1ULL<< LED); +} + +void main(void) { + volatile packet_t *p; + + gpio_data(0); + + gpio_pad_dir_set( 1ULL << LED ); + /* read from the data register instead of the pad */ + /* this is needed because the led clamps the voltage low */ + gpio_data_sel( 1ULL << LED); + + /* trim the reference osc. to 24MHz */ + trim_xtal(); + + uart_init(INC, MOD, SAMP); + + vreg_init(); + + maca_init(); + + /* sets up tx_on, should be a board specific item */ + // *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2)); + gpio_pad_dir_set( 1ULL << 44 ); + + set_power(0x0f); /* 0dbm */ + set_channel(0); /* channel 11 */ + + print_welcome("rftest-rx"); + while(1) { + + /* call check_maca() periodically --- this works around */ + /* a few lockup conditions */ + check_maca(); + + if((p = rx_packet())) { + /* print and free the packet */ + printf("rftest-rx --- "); + print_packet(p); + free_packet(p); + } + } +} diff --git a/cpu/mc1322x/tests/rftest-tx.c b/cpu/mc1322x/tests/rftest-tx.c new file mode 100644 index 000000000..28c46c6ee --- /dev/null +++ b/cpu/mc1322x/tests/rftest-tx.c @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: rftest-tx.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include +#include + +#include "tests.h" +#include "config.h" + +#define LED LED_RED + +/* 802.15.4 PSDU is 127 MAX */ +/* 2 bytes are the FCS */ +/* therefore 125 is the max payload length */ +#define PAYLOAD_LEN 16 +#define DELAY 100000 + +void fill_packet(volatile packet_t *p) { + static volatile uint8_t count=0; + volatile uint8_t i; + p->length = PAYLOAD_LEN; + p->offset = 0; + for(i=0; idata[i] = count++; + } + + /* acks get treated differently, even in promiscuous mode */ + /* setting the third bit makes sure that we never send an ack */ + /* or any valid 802.15.4-2006 packet */ + p->data[0] |= (1 << 3); +} + +void main(void) { + volatile uint32_t i; + volatile packet_t *p; + + /* trim the reference osc. to 24MHz */ + trim_xtal(); + + uart_init(INC, MOD, SAMP); + + vreg_init(); + + maca_init(); + + set_channel(0); /* channel 11 */ +// set_power(0x0f); /* 0xf = -1dbm, see 3-22 */ +// set_power(0x11); /* 0x11 = 3dbm, see 3-22 */ + set_power(0x12); /* 0x12 is the highest, not documented */ + + /* sets up tx_on, should be a board specific item */ + *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2)); + gpio_pad_dir_set( 1ULL << 44 ); + + print_welcome("rftest-tx"); + + while(1) { + + /* call check_maca() periodically --- this works around */ + /* a few lockup conditions */ + check_maca(); + + p = get_free_packet(); + if(p) { + fill_packet(p); + + printf("rftest-tx --- "); + print_packet(p); + + tx_packet(p); + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: romimg.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +void main(void) { + volatile uint8_t *data; + + uart_init(INC, MOD, SAMP); + + for(data = DUMP_BASE; data < ((uint8_t *)(DUMP_BASE+DUMP_LEN)); data++) { + uart1_putc(*data); + } + + while(1); + +} diff --git a/cpu/mc1322x/tests/sleep.c b/cpu/mc1322x/tests/sleep.c new file mode 100644 index 000000000..cbf5abf34 --- /dev/null +++ b/cpu/mc1322x/tests/sleep.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: sleep.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +void main(void) { + + uart_init(INC,MOD,SAMP); + + *mem32(0x00401ffc) = 0x01234567; + *mem32(0x00407ffc) = 0xdeadbeef; + *mem32(0x0040fffc) = 0xface00ff; + *mem32(0x00410000) = 0xabcd0123; + + putstr("sleep test\n\r"); + putstr("0x00401ffc: "); + put_hex32(*mem32(0x00401ffc)); + putstr("\r\n"); + putstr("0x00407ffc: "); + put_hex32(*mem32(0x00407ffc)); + putstr("\r\n"); + putstr("0x0040fffc: "); + put_hex32(*mem32(0x0040fffc)); + putstr("\r\n"); + putstr("0x00410000: "); + put_hex32(*mem32(0x00410000)); + putstr("\r\n"); + + /* radio must be OFF before sleeping */ + /* otherwise MCU will not wake up properly */ + /* this is undocumented behavior */ +// radio_off(); + +#if USE_32KHZ + /* turn on the 32kHz crystal */ + putstr("enabling 32kHz crystal\n\r"); + /* you have to hold it's hand with this on */ + /* once you start the 32xHz crystal it can only be stopped with a reset (hard or soft) */ + /* first, disable the ring osc */ + clear_bit(*CRM_RINGOSC_CNTL,0); + /* enable the 32kHZ crystal */ + set_bit(*CRM_XTAL32_CNTL,0); + + /* set the XTAL32_EXISTS bit */ + /* the datasheet says to do this after you've check that RTC_COUNT is changing */ + /* the datasheet is not correct */ + set_bit(*CRM_SYS_CNTL,5); + { + static volatile uint32_t old; + old = *CRM_RTC_COUNT; + putstr("waiting for xtal\n\r"); + while(*CRM_RTC_COUNT == old) { + continue; + } + /* RTC has started up */ + + set_bit(*CRM_SYS_CNTL,5); + putstr("32kHZ xtal started\n\r"); + + } +#endif + + + /* go to sleep */ + +// *CRM_WU_CNTL = 0; /* don't wake up */ + *CRM_WU_CNTL = 0x1; /* enable wakeup from wakeup timer */ +// *CRM_WU_TIMEOUT = 1875000; /* wake 10 sec later if doze */ +#if USE_32KHZ + *CRM_WU_TIMEOUT = 327680*2; +#else + *CRM_WU_TIMEOUT = 20000; /* wake 10 sec later if hibernate ring osc */ +#endif + + /* hobby board: 2kHz = 11uA; 32kHz = 11uA */ +// *CRM_SLEEP_CNTL = 1; /* hibernate, RAM page 0 only, don't retain state, don't power GPIO */ /* approx. 2kHz = 2.0uA */ + /* hobby board: 2kHz = 18uA; 32kHz = 19uA */ +// *CRM_SLEEP_CNTL = 0x41; /* hibernate, RAM page 0 only, retain state, don't power GPIO */ /* approx. 2kHz = 10.0uA */ + /* hobby board: 2kHz = 20uA; 32kHz = 21uA */ +// *CRM_SLEEP_CNTL = 0x51; /* hibernate, RAM page 0&1 only, retain state, don't power GPIO */ /* approx. 2kHz = 11.7uA */ + /* hobby board: 2kHz = 22uA; 32kHz = 22.5uA */ +// *CRM_SLEEP_CNTL = 0x61; /* hibernate, RAM page 0,1,2 only, retain state, don't power GPIO */ /* approx. 2kHz = 13.9uA */ + /* hobby board: 2kHz = 24uA; 32kHz = 25uA */ + *CRM_SLEEP_CNTL = 0x71; /* hibernate, all RAM pages, retain state, don't power GPIO */ /* approx. 2kHz = 16.1uA */ +// *CRM_SLEEP_CNTL = 0xf1; /* hibernate, all RAM pages, retain state, power GPIO */ /* consumption depends on GPIO hookup */ + +// *CRM_SLEEP_CNTL = 2; /* doze , RAM page 0 only, don't retain state, don't power GPIO */ /* approx. 69.2 uA */ +// *CRM_SLEEP_CNTL = 0x42; /* doze , RAM page 0 only, retain state, don't power GPIO */ /* approx. 77.3uA */ +// *CRM_SLEEP_CNTL = 0x52; /* doze , RAM page 0&1 only, retain state, don't power GPIO */ /* approx. 78.9uA */ +// *CRM_SLEEP_CNTL = 0x62; /* doze , RAM page 0,1,2 only, retain state, don't power GPIO */ /* approx. 81.2uA */ +// *CRM_SLEEP_CNTL = 0x72; /* doze , all RAM pages, retain state, don't power GPIO */ /* approx. 83.4uA - possibly with periodic refresh*/ +// *CRM_SLEEP_CNTL = 0xf2; /* doze , all RAM pages, retain state, power GPIO */ /* consumption depends on GPIO hookup */ + + + /* wait for the sleep cycle to complete */ + while((*CRM_STATUS & 0x1) == 0) { continue; } + /* write 1 to sleep_sync --- this clears the bit (it's a r1wc bit) and powers down */ + *CRM_STATUS = 1; + + /* asleep */ + + /* wait for the awake cycle to complete */ + while((*CRM_STATUS & 0x1) == 0) { continue; } + /* write 1 to sleep_sync --- this clears the bit (it's a r1wc bit) and finishes wakeup */ + *CRM_STATUS = 1; + + putstr("\n\r\n\r\n\r"); + putstr("0x00401ffc: "); + put_hex32(*mem32(0x00401ffc)); + putstr("\r\n"); + putstr("0x00407ffc: "); + put_hex32(*mem32(0x00407ffc)); + putstr("\r\n"); + putstr("0x0040fffc: "); + put_hex32(*mem32(0x0040fffc)); + putstr("\r\n"); + putstr("0x00410000: "); + put_hex32(*mem32(0x00410000)); + putstr("\r\n"); + + *GPIO_PAD_DIR0 = LED_RED; +#define DELAY 400000 + + volatile uint32_t i; + while(1) { + + *GPIO_DATA0 = LED_RED; + + for(i=0; i and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: tests.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "put.h" +#include "tests.h" + +void print_welcome(char* testname) { + printf("mc1322x-test: %s\n\r",testname); + printf("board: %s\n\r", STR2(BOARD)); +} + +void print_packet(volatile packet_t *p) { + volatile uint8_t i,j,k; +#define PER_ROW 16 + if(p) { + printf("len 0x%02x",p->length); + for(j=0, k=0; j <= ((p->length)%PER_ROW); j++) { + printf("\n\r"); + for(i=0; i < PER_ROW; i++, k++) { + if(k >= p->length ) { + printf("\n\r"); + return; + } + printf("%02x ",p->data[j*PER_ROW + i + p->offset]); + } + } + } + printf("\n\r"); + return; +} + +void dump_regs(uint32_t base, uint32_t len) { + volatile uint32_t i; + + printf("base +0 +4 +8 +c +10 +14 +18 +1c \n\r"); + for (i = 0; i < len; i ++) { + if ((i & 7) == 0) { + printf("%02lx",(uint32_t)(4 * i)); + } + printf(" %08lx",(uint32_t)*mem32(base+(4*i))); + if ((i & 7) == 7) + printf(NL); + } + printf(NL); +} diff --git a/cpu/mc1322x/tests/tests.h b/cpu/mc1322x/tests/tests.h new file mode 100644 index 000000000..b1f575fdf --- /dev/null +++ b/cpu/mc1322x/tests/tests.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: tests.h,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#ifndef TESTS_H +#define TESTS_H + +#include "put.h" +#include "led.h" + +#define NL "\033[K\r\n" + +void print_welcome(char* testname); +void dump_regs(uint32_t base, uint32_t len); +void print_packet(volatile packet_t *p); + +#endif diff --git a/cpu/mc1322x/tests/tmr-ints.c b/cpu/mc1322x/tests/tmr-ints.c new file mode 100644 index 000000000..feee95acb --- /dev/null +++ b/cpu/mc1322x/tests/tmr-ints.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: tmr-ints.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +volatile uint8_t led; + +#define LED LED_PURPLE +#define led_init() do { gpio_pad_dir_set(LED_WHITE); gpio_data_reset(LED_WHITE); } while(0); +#define led_on() do { led = 1; gpio_data_set(LED); } while(0); +#define led_off() do { led = 0; gpio_data_reset(LED); } while(0); + +void toggle_led(void) { + if(0 == led) { + led_on(); + led = 1; + + } else { + led_off(); + } +} + +void tmr0_isr(void) { + + toggle_led(); + *TMR0_SCTRL = 0; + *TMR0_CSCTRL = 0x0040; /* clear compare flag */ + +} + + +void main(void) { + + /* pin direction */ + led_init(); + + /* timer setup */ + /* CTRL */ +#define COUNT_MODE 1 /* use rising edge of primary source */ +#define PRIME_SRC 0xf /* Perip. clock with 128 prescale (for 24Mhz = 187500Hz)*/ +#define SEC_SRC 0 /* don't need this */ +#define ONCE 0 /* keep counting */ +#define LEN 1 /* count until compare then reload with value in LOAD */ +#define DIR 0 /* count up */ +#define CO_INIT 0 /* other counters cannot force a re-initialization of this counter */ +#define OUT_MODE 0 /* OFLAG is asserted while counter is active */ + + *TMR_ENBL = 0; /* tmrs reset to enabled */ + *TMR0_SCTRL = 0; + *TMR0_CSCTRL = 0x0040; + *TMR0_LOAD = 0; /* reload to zero */ + *TMR0_COMP_UP = 18750; /* trigger a reload at the end */ + *TMR0_CMPLD1 = 18750; /* compare 1 triggered reload level, 10HZ maybe? */ + *TMR0_CNTR = 0; /* reset count register */ + *TMR0_CTRL = (COUNT_MODE<<13) | (PRIME_SRC<<9) | (SEC_SRC<<7) | (ONCE<<6) | (LEN<<5) | (DIR<<4) | (CO_INIT<<3) | (OUT_MODE); + *TMR_ENBL = 0xf; /* enable all the timers --- why not? */ + + led_on(); + + enable_irq(TMR); + + while(1) { + /* sit here and let the interrupts do the work */ + continue; + }; +} diff --git a/cpu/mc1322x/tests/tmr.c b/cpu/mc1322x/tests/tmr.c new file mode 100644 index 000000000..adf960ed6 --- /dev/null +++ b/cpu/mc1322x/tests/tmr.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: tmr.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +#define LED LED_YELLOW + +void main(void) { + + /* pin direction */ + gpio_pad_dir_set(LED_WHITE); + /* all LEDs off */ + gpio_data_reset(LED_WHITE); + + /* timer setup */ + /* CTRL */ +#define COUNT_MODE 1 /* use rising edge of primary source */ +#define PRIME_SRC 0xf /* Perip. clock with 128 prescale (for 24Mhz = 187500Hz)*/ +#define SEC_SRC 0 /* don't need this */ +#define ONCE 0 /* keep counting */ +#define LEN 1 /* count until compare then reload with value in LOAD */ +#define DIR 0 /* count up */ +#define CO_INIT 0 /* other counters cannot force a re-initialization of this counter */ +#define OUT_MODE 0 /* OFLAG is asserted while counter is active */ + + *TMR_ENBL = 0; /* tmrs reset to enabled */ + *TMR0_SCTRL = 0; + *TMR0_LOAD = 0; /* reload to zero */ + *TMR0_COMP_UP = 18750; /* trigger a reload at the end */ + *TMR0_CMPLD1 = 18750; /* compare 1 triggered reload level, 10HZ maybe? */ + *TMR0_CNTR = 0; /* reset count register */ + *TMR0_CTRL = (COUNT_MODE<<13) | (PRIME_SRC<<9) | (SEC_SRC<<7) | (ONCE<<6) | (LEN<<5) | (DIR<<4) | (CO_INIT<<3) | (OUT_MODE); + *TMR_ENBL = 0xf; /* enable all the timers --- why not? */ + + while(1) { + + /* blink on */ + gpio_data_set(LED); + + while((*TMR0_SCTRL >> 15) == 0) { continue; } + *TMR0_SCTRL = 0; /*clear bit 15, and all the others --- should be ok, but clearly not "the right thing to do" */ + + /* blink off */ + gpio_data_reset(LED); + + while((*TMR0_SCTRL >> 15) == 0) { continue; } + *TMR0_SCTRL = 0; /*clear bit 15, and all the others --- should be ok, but clearly not "the right thing to do" */ + + }; +} diff --git a/cpu/mc1322x/tests/uart1-loopback.c b/cpu/mc1322x/tests/uart1-loopback.c new file mode 100644 index 000000000..fadafd811 --- /dev/null +++ b/cpu/mc1322x/tests/uart1-loopback.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: uart1-loopback.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +void main(void) { + + uart1_init(INC,MOD,SAMP); + + while(1) { + if(uart1_can_get()) { + /* Receive buffer isn't empty */ + /* read a byte and write it to the transmit buffer */ + uart1_putc(uart1_getc()); + } + } + +} diff --git a/cpu/mc1322x/tools/ftditools/Makefile b/cpu/mc1322x/tools/ftditools/Makefile new file mode 100644 index 000000000..0e07771ae --- /dev/null +++ b/cpu/mc1322x/tools/ftditools/Makefile @@ -0,0 +1,10 @@ +LDFLAGS = -lftdi + +TARGETS = bbmc + +CFLAGS = -Wall -Wextra #-Werror + +all: $(TARGETS) + +clean: + -rm $(TARGETS) diff --git a/cpu/mc1322x/tools/ftditools/bbmc.c b/cpu/mc1322x/tools/ftditools/bbmc.c new file mode 100644 index 000000000..519eed81d --- /dev/null +++ b/cpu/mc1322x/tools/ftditools/bbmc.c @@ -0,0 +1,497 @@ +/* + * Copyright (c) 2010, Mariano Alvira and other contributors + * to the MC1322x project (http://mc1322x.devl.org) + * 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. + * + * This file is part of libmc1322x: see http://mc1322x.devl.org + * for details. + * + * $Id: bbmc.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $ + */ + +/* control reset and VREF2 lines */ + +#include +#include +#include +#include + +#define DEBUG 0 + +#define low(x) (1 << x) +#define high(x) (1 << (x + 8)) + +#define REDBEE_ECONOTAG_RESET high(2) +#define REDBEE_ECONOTAG_VREF2L high(7) +#define REDBEE_ECONOTAG_VREF2H high(6) +#define REDBEE_ECONOTAG_INTERFACE INTERFACE_A + +#define REDBEE_USB_RESET high(2) +#define REDBEE_USB_VREF2L low(5) +#define REDBEE_USB_VREF2H low(6) +#define REDBEE_USB_INTERFACE INTERFACE_B + +#define BOARD REDBEE_USB + +#define STR(x) #x +#define STR2(x) STR(x) +#define CAT(x,y) x##y +#define CAT2(x, y, z) x##y##z + +#define dir(x) ( CAT(x,_RESET) | CAT(x,_VREF2L) | CAT(x,_VREF2H)) +#define interface(x) ( CAT(x,_INTERFACE) ) +#define reset_release(x) ( CAT(x,_RESET) ) +#define reset_set(x) ( 0 ) +#define vref2_normal(x) ( CAT(x,_VREF2H) ) +#define vref2_erase(x) ( CAT(x,_VREF2L) ) + +/* fgets input buffer length: for prompts and such */ +#define BUF_LEN 32 + +struct layout { + char *name; + char *desc; + enum ftdi_interface interface; + uint16_t dir; + uint16_t reset_release; + uint16_t reset_set; + uint16_t vref2_normal; + uint16_t vref2_erase; +}; + +int print_and_prompt( struct ftdi_device_list *devlist ); +int bb_mpsee(struct ftdi_context *ftdic, uint16_t dir, uint16_t val); +void reset(struct ftdi_context *ftdic, const struct layout * l); +void erase(struct ftdi_context *ftdic, const struct layout * l); +void usage(void); + +#define std_layout(x) \ + .interface = interface(x), \ + .dir = dir(x), \ + .reset_release = reset_release(x), \ + .reset_set = reset_set(x), \ + .vref2_normal = vref2_normal(x), \ + .vref2_erase = vref2_erase(x), + +static struct layout layouts[] = +{ + { .name = "redbee-econotag", + .desc = "Redbee Econotag", + std_layout(REDBEE_ECONOTAG) + }, + { .name = "redbee-usb", + .desc = "Redbee USB stick", + std_layout(REDBEE_USB) + }, + { .name = NULL, /* end of table */ }, +}; + +struct command { + char *name; + char *desc; + void (*cmd)(struct ftdi_context *ftdic, const struct layout * l); +}; + +static const struct command commands[] = +{ + { + .name = "reset", + .desc = "Toggles reset pin", + .cmd = reset, + }, + { + .name = "erase", + .desc = "Sets VREF2 erase mode; toggles reset; waits 2 sec.; sets normal; toggles reset again", + .cmd = erase, + }, + { .name = NULL, /* end of table */ }, +}; + +struct layout * find_layout(char * str) +{ + uint32_t i = 0; + + while(layouts[i].name != NULL) { + if(strcmp(layouts[i].name, str) == 0) { return &layouts[i]; } + i++; + } + + return NULL; +} + +static uint32_t vendid = 0x0403; uint32_t prodid = 0x6010; + +int main(int argc, char **argv) +{ + struct ftdi_context ftdic; + struct ftdi_device_list *devlist; + int dev_index = -1; int num_devs; + char layout_str[BUF_LEN]; + struct layout layout; + struct layout *l = NULL; + int i, ret; + + /* overrides for layout parameters */ + int interface = -1; + int dir = -1; + int reset_release = -1; + int reset_set = -1; + int vref2_normal = -1; + int vref2_erase = -1; + + layout.name = NULL; + + while (1) { + int c; + int option_index = 0; + static struct option long_options[] = { + {"layout", required_argument, 0, 'l'}, + {"index", required_argument, 0, 'i'}, + {"vendor", required_argument, 0, 'v'}, + {"product", required_argument, 0, 'p'}, + {"dir", required_argument, 0, 0 }, + {"reset_release", required_argument, 0, 0 }, + {"reset_set", required_argument, 0, 0 }, + {"vref2_normal", required_argument, 0, 0 }, + {"vref2_erase", required_argument, 0, 0 }, + {"interface", required_argument, 0, 0 }, + {"help", no_argument, 0, '?'}, + {0, 0, 0, 0} + }; + + c = getopt_long (argc, argv, "i:l:v:p:", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + /* process long opts */ + case 0: + if(strcmp(long_options[option_index].name, "interface") == 0) { + sscanf(optarg, "%i", &interface); + } + if(strcmp(long_options[option_index].name, "dir") == 0) { + sscanf(optarg, "%i", &dir); + } + if (strcmp(long_options[option_index].name, "reset_release") == 0) { + sscanf(optarg, "%i", &reset_release); + } + if (strcmp(long_options[option_index].name, "reset_set") == 0) { + sscanf(optarg, "%i", &reset_set); + } + if (strcmp(long_options[option_index].name, "vref2_normal") == 0) { + sscanf(optarg, "%i", &vref2_normal); + } + if (strcmp(long_options[option_index].name, "vref2_erase") == 0) { + sscanf(optarg, "%i", &vref2_erase); + } + break; + + case 'l': + strncpy(layout_str, optarg, BUF_LEN); + break; + case 'i': + dev_index = atoi(optarg); + break; + case 'v': + sscanf(optarg, "%i", &vendid); + break; + case 'p': + sscanf(optarg, "%i", &prodid); + break; + default: + usage(); + break; + } + } + + if( !(l = find_layout(layout_str)) && + !((interface >= 0) && + (dir >= 0) && + (reset_release >= 0) && + (reset_set >= 0) && + (vref2_normal >= 0) && + (vref2_erase >= 0)) + ) { + + printf("*** You must specify a layout or a complete set of overrides\n"); + return EXIT_FAILURE; + } + + if(l) { + memcpy(&layout, l, sizeof(struct layout)); + } + +#define override(x) if(x > 0) { layout.x = x; } + override(interface); + override(dir); + override(reset_release); override(reset_set); + override(vref2_normal); override(vref2_erase); + + if ((num_devs = ftdi_usb_find_all(&ftdic, &devlist, vendid, prodid)) < 0) + { + fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", + num_devs, + ftdi_get_error_string(&ftdic)); + return EXIT_FAILURE; + } + + if (ftdi_init(&ftdic) < 0) + { + fprintf(stderr, "ftdi_init failed\n"); + return EXIT_FAILURE; + } + + if (ftdi_set_interface(&ftdic, layout.interface) < 0) { + fprintf(stderr, "couldn't set interface %d\n", layout.interface); + return EXIT_FAILURE; + } + + printf("Found %d devices with vendor id 0x%04x product id 0x%04x\n", + num_devs, vendid, prodid); + + if(num_devs == 0) { return EXIT_SUCCESS; } + + if(num_devs == 1) { dev_index = 0; } + while( (dev_index < 0) || (dev_index >= num_devs)){ + dev_index = print_and_prompt(devlist); + } + + if(layout.name != NULL) { + printf("Opening device %d interface %d using layout %s\n", + dev_index, layout.interface, layout.name); + } else { + printf("Opening device %d interface %d without a layout.\n", + dev_index, layout.interface); + } + + if( (ret = ftdi_usb_open_desc_index( + &ftdic, + vendid, + prodid, + NULL, + NULL, + dev_index)) < 0) { + fprintf(stderr, "couldn't open dev_index %d\n", dev_index); + return EXIT_FAILURE; + } + + + for(i = 0; commands[i].name != NULL; i++) { + if( (argv[optind] != NULL) && + (strcmp(commands[i].name, argv[optind]) == 0)) { break; } + } + if(commands[i].name != NULL) { + commands[i].cmd(&ftdic, &layout); + } else { + printf("invalid command\n"); + + ftdi_list_free(&devlist); + ftdi_deinit(&ftdic); + + return EXIT_FAILURE; + } + + printf("done.\n"); + + ftdi_list_free(&devlist); + ftdi_deinit(&ftdic); + + return EXIT_SUCCESS; +} + +void usage(void) +{ + int i; + printf( "Usage: bbmc [options|overrides] -l|--layout layout command \n"); + printf( "Commands:\n"); + for(i = 0; commands[i].name != NULL; i++) { + printf( " %s: %s\n", commands[i].name, commands[i].desc); + } + printf("\n"); + printf( "Required options:\n"); + printf( " -l|--layout\t specifiy which board layout to use\n"); + printf( " \t layout is not necessary with a full\n"); + printf( " \t set of overrides\n"); + printf( "\nLayout overrides:\n"); + printf( " --interface\t\t FTDI interface to use\n"); + printf( " --dir\t\t direction (1 is output)\n"); + printf( " --reset_release\t reset release command\n"); + printf( " --reset_set\t\t reset set command\n"); + printf( " --vref2_normal\t vref2 normal\n"); + printf( " --vref2_erase\t vref2 erase\n"); + printf("\n"); + printf( "Layouts:\n"); + for(i = 0; layouts[i].name != NULL; i++) { + printf( "\t%s: %s\n", layouts[i].name, layouts[i].desc); + printf("\n"); + printf( "\t\tinterface: \t0x%04x\n", layouts[i].interface); + printf( "\t\tdir: \t\t0x%04x\n", layouts[i].dir); + printf( "\t\treset release: \t0x%04x\n", layouts[i].reset_release); + printf( "\t\treset hold: \t0x%04x\n", layouts[i].reset_set); + printf( "\t\tvref2 normal: \t0x%04x\n", layouts[i].vref2_normal); + printf( "\t\tvref2 erase: \t0x%04x\n", layouts[i].vref2_erase); + printf("\n"); + } + printf("\n"); + printf( "Options:\n"); + printf( " -i|--index specifiy which device to use (default 0)\n"); + printf( " -v|--vendor set vendor id (default 0x0403)\n"); + printf( " -p|--product set vendor id (default 0x6010)\n"); +} + +int print_and_prompt( struct ftdi_device_list *devlist ) +{ + int i, ret; + struct ftdi_context ftdic; + struct ftdi_device_list *curdev; + char manufacturer[128], description[128], serial[128]; + char input[BUF_LEN]; char *s; + int sel = -1; + + printf("\n"); + + i = 0; + for (curdev = devlist; curdev != NULL; i++) + { + printf(" [%d] ", i); + if (0 > (ret = ftdi_usb_get_strings(&ftdic, + curdev->dev, + manufacturer, 128, + description, 128, + serial, 128))) + { + fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", + ret, ftdi_get_error_string(&ftdic)); + return EXIT_FAILURE; + } + printf("Manufacturer: %s, Description: %s, Serial %s\n", + manufacturer, description, serial); + curdev = curdev->next; + } + + printf("\nUse which device? "); + + s = fgets(input, BUF_LEN, stdin); + if (s != NULL) { + size_t last = strlen (input) - 1; + if (input[last] == '\n') input[last] = '\0'; + } + + sscanf(s, "%i",&sel); + + return sel; +} + +void reset(struct ftdi_context *ftdic, const struct layout * l) +{ + + /* using MPSSE since it give access to high GPIO*/ + /* set as inputs for now */ + ftdi_set_bitmode(ftdic, 0 , BITMODE_MPSSE); + + printf("toggle reset\n"); + + bb_mpsee(ftdic, l->dir, (l->reset_release | l->vref2_normal)); + bb_mpsee(ftdic, l->dir, (l->reset_set | l->vref2_normal)); + bb_mpsee(ftdic, l->dir, (l->reset_release | l->vref2_normal)); + + return; + +} + + +void erase(struct ftdi_context *ftdic, const struct layout * l) +{ + printf("setting VREF2 erase\n"); + + /* using MPSSE since it give access to high GPIO*/ + /* set as inputs for now */ + ftdi_set_bitmode(ftdic, 0 , BITMODE_MPSSE); + + bb_mpsee(ftdic, l->dir, (l->reset_release | l->vref2_normal)); + bb_mpsee(ftdic, l->dir, (l->reset_release | l->vref2_erase)); + + printf("toggle reset\n"); + + bb_mpsee(ftdic, l->dir, (l->reset_set | l->vref2_erase)); + bb_mpsee(ftdic, l->dir, (l->reset_release | l->vref2_erase)); + + printf("waiting for erase\n"); + + sleep(2); + + printf("setting VREF2 normal\n"); + + bb_mpsee(ftdic, l->dir, (l->reset_release | l->vref2_normal)); + + reset(ftdic, l); + + return; + +} + + +int bb_mpsee(struct ftdi_context *ftdic, uint16_t dir, uint16_t val) +{ + uint8_t buf[3]; + int ret; + + /* command "set data bits low byte" */ + buf[0] = 0x80; + buf[1] = (val & 0xff); + buf[2] = dir & 0xff; +#if DEBUG + fprintf(stderr,"write %x %x %x\n",buf[0],buf[1],buf[2]); +#endif + + if ((ret = (ftdi_write_data(ftdic, buf, 3))) < 0) + { + perror("ft2232_write error"); + fprintf(stderr, "ft2232_write command %x\n", buf[0]); + return EXIT_FAILURE; + } + + + /* command "set data bits high byte" */ + buf[0] = 0x82; + buf[1] = (val >> 8); + buf[2] = dir >> 8; +#if DEBUG + fprintf(stderr,"write %x %x %x\n",buf[0],buf[1],buf[2]); +#endif + + if ((ret = (ftdi_write_data(ftdic, buf, 3))) < 0) + { + perror("ft2232_write error"); + fprintf(stderr, "ft2232_write command %x\n", buf[0]); + return EXIT_FAILURE; + } + + return 0; + +} diff --git a/cpu/mc1322x/tools/map2dot.pl b/cpu/mc1322x/tools/map2dot.pl new file mode 100755 index 000000000..1e834c68f --- /dev/null +++ b/cpu/mc1322x/tools/map2dot.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +# Try: +# gcc -static -o test test.c -Wl,--print-map | perl parse-map.pl | unflatten -l 3 | dot -Tpng > map.png +# +# This won't show all edges! An archive member is only listed the first +# time it needs to get included. But this shows at least one reason why each +# archive member appears in the final object. + +my $flag = 0; +my $line = ""; +print "digraph map {\n"; +print "rankdir = LR;"; +while(<>) +{ + $flag++ if /^Archive member included because of file \(symbol\)$/; + $flag++ if /^$/; + next unless $flag == 2; + + chomp ($line .= $_); + if ($line =~ /^(\S+)\s+(\S+)\s+\(([^)]+)\)$/s) { + $line = ""; + my $archive_member = $1; + my $because_file = $2; + my $because_symbol = $3; + $archive_member =~ s|.*/([^/]+)|$1|; + $because_file =~ s|.*/([^/]+)|$1|; + print "\"$because_file\" -> \"$archive_member\";\n"; + } +} +print "}\n"; diff --git a/cpu/mc1322x/tools/mc1322x-load.pl b/cpu/mc1322x/tools/mc1322x-load.pl new file mode 100755 index 000000000..c56aef892 --- /dev/null +++ b/cpu/mc1322x/tools/mc1322x-load.pl @@ -0,0 +1,134 @@ +#!/usr/bin/perl -w + +use Device::SerialPort; +use Term::ReadKey; +use Getopt::Long; +use Time::HiRes qw(usleep); + +use strict; + +my $filename = ''; +my $second = ''; +my $term = '/dev/ttyUSB0'; +my $baud = '115200'; +my $verbose; +my $rts = 'rts'; + +GetOptions ('file=s' => \$filename, + 'secondfile=s' => \$second, + 'terminal=s' => \$term, + 'verbose' => \$verbose, + 'baud=s' => \$baud, + 'rts=s' => \$rts, + ) or die 'bad options'; + +$| = 1; + +if($filename eq '') { + print "Example usage: mc1322x-load.pl -f foo.bin -t /dev/ttyS0 -b 9600\n"; + print " or : mc1322x-load.pl -f flasher.bin -s flashme.bin 0x1e000,0x11223344,0x55667788\n"; + print " -f required: binary file to load\n"; + print " -s optional: secondary binary file to send\n"; + print " -t default: /dev/ttyUSB0\n"; + print " -b default: 115200\n"; + print " -r [none|rts] flow control default: rts\n"; + print " anything on the command line is sent\n"; + print " after all of the files.\n"; + exit; +} + +if (!(-e $filename)) { die "file $filename not found\n"; } +if (($second ne '') && !(-e $second)) { die "secondary file $second not found\n"; } + +my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; + # next test will die at runtime unless $ob + +$ob->baudrate($baud); +$ob->parity('none'); +$ob->databits(8); +$ob->stopbits(1); +if($rts eq 'rts') { + $ob->handshake('rts'); +} else { + $ob->handshake('none'); +} +$ob->read_const_time(1000); # 1 second per unfulfilled "read" call +$ob->rts_active(1); + +my $s = 0; + +while(1) { + + my $c; my $count; my $ret = ''; my $test=''; + + if($s == 1) { print "secondary send...\n"; } + + $ob->write(pack('C','0')); + + if($s == 1) { + $test = 'ready'; + } else { + $test = 'CONNECT'; + } + + until($ret =~ /$test$/) { + ($count,$c) = $ob->read(1); + if ($count == 0) { + print '.'; + $ob->write(pack('C','0')); + next; + } + $ret .= $c; + } + print $ret . "\n"; + + if (-e $filename) { + + my $size = -s $filename; + + print ("Size: $size bytes\n"); + $ob->write(pack('V',$size)); + + open(FILE, $filename) or die($!); + print "Sending $filename\n"; + + my $i = 1; + while(read(FILE, $c, 1)) { + $i++; + usleep(50); # this is as fast is it can go... + usleep(50) if ($s==1); + $ob->write($c); + } + } + + last if ($s==1); + if((-e $second)) { + $s=1; $filename = $second; + } else { + last; + } + +} + +print "done sending files.\n"; + +if(scalar(@ARGV)!=0) { + print "sending " ; + print @ARGV; + print ",\n"; + + $ob->write(@ARGV); + $ob->write(','); +} + +my $c; my $count; +while(1) { + ($count, $c) = $ob->read(1); + print $c if (defined($count) && ($count != 0)); +} + +$ob -> close or die "Close failed: $!\n"; +ReadMode 0; +undef $ob; # closes port AND frees memory in perl +exit; + diff --git a/cpu/mc1322x/tools/rftestrx2pcap.pl b/cpu/mc1322x/tools/rftestrx2pcap.pl new file mode 100755 index 000000000..2bf58a657 --- /dev/null +++ b/cpu/mc1322x/tools/rftestrx2pcap.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl -w + +use Device::SerialPort; +use Term::ReadKey; +use Getopt::Long; +use Time::HiRes qw(usleep gettimeofday); + +use strict; + +my $filename = ''; +my $second = ''; +my $term = '/dev/ttyUSB0'; +my $baud = '115200'; +my $verbose; + +GetOptions ( + 'terminal=s' => \$term, + 'baud=s' => \$baud, + ); + +$| = 1; + +# TODO: add help argument +# print "Example usage: rftestrx2pcap.pl -t /dev/ttyS0 -b 9600\n"; +# exit; + +my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; + # next test will die at runtime unless $ob + +$ob->baudrate($baud); +$ob->parity('none'); +$ob->databits(8); +$ob->stopbits(1); +$ob->read_const_time(1000); # 1 second per unfulfilled "read" call + +my $str = ''; +my ($sec, $usec, $len); +my @frame; + +my $magic = 0xa1b2c3d4; +my $major = 2; +my $minor = 4; +my $zone = 0; +my $sig = 0; +my $snaplen = 0xffff; +my $network = 195; # 802.15.4 + +print pack('LSSLLLL',($magic,$major,$minor,$zone,$sig,$snaplen,$network)); + +while(1) { + my ($count, $c) = $ob->read(1); + + if (defined($count) && ($count != 0)) { + $str .= $c; + # match if ends in \n or \r and process line + if(($str =~ /\n$/) || + ($str =~ /\r$/)) { + if($str =~ /^rftest/) { + #new packet + ($sec, $usec) = gettimeofday; + print STDERR "rftestline: $sec $usec $str"; + } elsif($str =~ /^\s*data/) { + #packet payload + print STDERR "dataline: "; + print STDERR $str; + $str =~ /data: 0x\d+ (.+)/; + my @data = split(' ',$1); + ($len, @data) = @data; + #write out pcap entry + print pack('LLLL',($sec,$usec,scalar(@data),scalar(@data)+2)); + print STDERR "new packet: $sec $usec " . scalar(@data) . " " . (scalar(@data)+2) . "\n\r"; + @frame = @data[0,1]; + print pack ('CC',(hex($frame[0]),hex($frame[1]))); + print STDERR "$frame[0] $frame[1] "; + foreach my $data (@data[2..scalar(@data)-1]) { + print pack ('C',hex($data)); + print STDERR "$data "; + } + print STDERR "\n\r"; + } + print STDERR "\n\r"; + $str = ''; + } + } +} + +$ob -> close or die "Close failed: $!\n"; +ReadMode 0; +undef $ob; # closes port AND frees memory in perl +exit; + diff --git a/cpu/mc1322x/tools/rimecollect-rrd/collect2rrd.pl b/cpu/mc1322x/tools/rimecollect-rrd/collect2rrd.pl new file mode 100755 index 000000000..197e02e3d --- /dev/null +++ b/cpu/mc1322x/tools/rimecollect-rrd/collect2rrd.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl -w +use strict; +my $verbose = 1; + +#### +# Feed data on stdin from a RIME collect sink +# +# Enters data into rimeaddr.rrd +# +# Creates rimeaddr.rrd from a template if data shows up from a source and +# rimeaddr.rrd doesn't exist +# +# default template is read from default.rrdtmpl +# +# if rimeaddr.rrdtmpl exisits, that is used instead. + +#### +# +# Templates are shell scripts that create the desired rrd +# + +#### +# Data messages are in the form of: +# +# Sink got message from 1.0, seqno 109, hops 0: len 12 'GPIO29-High' +# +# + +my $datapattern = 'Sink got message from ([\d\.]+), seqno \d+, hops \d+: len \d+ \'([\w\d]+-[\w\d]+)\''; + +sub rrdcreate { + my ($newrrd_filename, $tmpl_filename) = @_; + open FILE, "$tmpl_filename" or die $!; + my $tmpl = ; + chomp $tmpl; + print "using template $tmpl found in $tmpl_filename\n" if $verbose; + `rrdtool create $newrrd_filename $tmpl`; +} + +while(<>) { + + next if($_ !~ /$datapattern/); + print("rimeaddr $1 data $2\n") if $verbose; + + my ($ds,$data) = split(/-/,$2); + print("ds: $ds, data: $data\n") if $verbose; + + if(-e "$1.rrd") { + # an rrd already exists for this device + # do an update + `rrdtool update $1.rrd -t $ds N:$data` + } else { + # an rrd for this device doesn't exist yet + # find a template and make it + my $tmpl = "DS:speed:COUNTER:600:U:U RRA:AVERAGE:0.5:6:10"; + print "creating new rrd $1.rrd... " if $verbose; + if(-e "$1.rrdtmpl") { + rrdcreate("$1.rrd","$1.rrdtmpl"); + `rrdtool update $1.rrd -t $ds N:$data` + } elsif(-e "default.rrdtmpl") { + rrdcreate("$1.rrd","default.rrdtmpl"); + `rrdtool update $1.rrd -t $ds N:$data` + } else { + print "can't create rrd for $1: no template found\n"; + } + } +} diff --git a/cpu/mc1322x/tools/rimecollect-rrd/default.rrdtmpl b/cpu/mc1322x/tools/rimecollect-rrd/default.rrdtmpl new file mode 100644 index 000000000..f5cbc5e96 --- /dev/null +++ b/cpu/mc1322x/tools/rimecollect-rrd/default.rrdtmpl @@ -0,0 +1 @@ +-s 1 DS:GPIO29:GAUGE:600:U:U RRA:LAST:0.99:1:3600 diff --git a/cpu/mc1322x/tools/rimecollect-rrd/meshstat.cgi b/cpu/mc1322x/tools/rimecollect-rrd/meshstat.cgi new file mode 100755 index 000000000..498c8a4e9 --- /dev/null +++ b/cpu/mc1322x/tools/rimecollect-rrd/meshstat.cgi @@ -0,0 +1,70 @@ +#!/usr/bin/perl -w + +# CGI script that creates a fill-out form +# and echoes back its values. + +use CGI qw/:standard/; + +# configs + +# paths +my $meshpath = "/home/malvira/work"; +my $wwwpath = "/var/www"; +my $hostname = "hotdog.redwirellc.com"; + +# aliases +my %aliases = ( + "2.0" => { + alias => "Lower Door", + ds=> { + "GPIO29" => "Lock (0 - locked, 1 - unlocked)", + }, + }, + "4.0" => { + alias => "Upper Door", + }, + "1.0" => { + alias => "Hotdog (datasink)", + }, + ); + +opendir(MESHDIR, $meshpath); +my @files = readdir(MESHDIR); + +print header; +print start_html('Collect Mesh'); + + +foreach my $file (@files) { + next if $file !~ /([\d\.]+)\.rrd$/; + my $addr = $1; + print hr; + print h1("$addr: $aliases{$addr}{'alias'}"); + my @info = split(/\n/,qx(rrdtool info $meshpath/$addr.rrd)); + + my %dses; + foreach my $info (@info) { + next if $info !~ /ds\[([\w\d]+)\]\.([\w\d_]+)\s+=\s+([\w\d]+)/; + $dses{$1}{$2} = $3; + } + + my $lastupdate = qx(rrdtool lastupdate $meshpath/$addr.rrd); + $lastupdate =~ /([\w\d]+)\s+(\d+):\s+([\w\d]+)/; + print localtime($2) . " $1 $3
"; + + foreach my $ds (keys(%dses)) { + print h2("$ds: $aliases{$addr}{'ds'}{$ds}"); + qx(rrdtool graph $wwwpath/$addr-$ds.png --start end-60min DEF:$ds=$meshpath/$addr.rrd:$ds:LAST LINE2:$ds#00a000:\"$ds\"); + print img({src=>"http://$hostname/$addr-$ds.png"}); + } + +} + +print hr; + +print end_html; + +#/var/www/demo.png --title="Door" --start end-60min +# --imginfo '' +# DEF:door=/home/malvira/work/2.0.rrd:GPIO29:LAST +# LINE2:door#00a000:"Door lock">