From 0d1cc63b04a887fad86861a34b7ccbdd47fe156c Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Tue, 17 May 2011 16:26:03 -0400 Subject: [PATCH 1/6] add xtal trimming program --- tests/Makefile | 3 +- tests/xtal-trim.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/xtal-trim.c diff --git a/tests/Makefile b/tests/Makefile index ef4b6c287..09e928385 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -14,7 +14,8 @@ TARGETS := blink-red blink-green blink-blue blink-white blink-allio \ asm \ adc \ pwm \ - wdt + wdt \ + xtal-trim # 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 diff --git a/tests/xtal-trim.c b/tests/xtal-trim.c new file mode 100644 index 000000000..02e1d944e --- /dev/null +++ b/tests/xtal-trim.c @@ -0,0 +1,71 @@ +/* + * 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. + * + * + */ + +#include +#include +#include + +#include "config.h" +#include "tests.h" + +int main(void) +{ + uint8_t ctune_4pf, ctune, ftune; + + ctune_4pf = 0; + ctune = 0; + ftune = 0; + + uart1_init(INC,MOD,SAMP); + + print_welcome("pwm test\r\n"); + pack_XTAL_CNTL(ctune_4pf, ctune, ftune, IBIAS); + + for(;;) { + switch(uart1_getc()) { + case '[': if(ctune > 0) ctune -= 1; break; + case ']': if(ctune < 15) ctune += 1; break; + case '-': if(ftune > 0) ftune -= 1; break; + case '=': if(ftune < 31) ftune += 1; break; + case ' ': if(ctune_4pf) { ctune_4pf = 0; } else { ctune_4pf = 1;} break; + } + + printf("ctune_4pf %d; ctune %d; ftune %d\n\r", ctune_4pf, ctune, ftune); + pack_XTAL_CNTL(ctune_4pf, ctune, ftune, IBIAS); + + } +} + + From be190969376c80886143dbda2a80eff383632a1c Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Mon, 23 May 2011 13:46:41 -0400 Subject: [PATCH 2/6] add index option to burn-mac and changes burn-macs to use burn-mac. --- tools/burn-mac.pl | 4 +++- tools/test-grid/burn-macs.pl | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/burn-mac.pl b/tools/burn-mac.pl index 6ea201309..69bcd9dc6 100755 --- a/tools/burn-mac.pl +++ b/tools/burn-mac.pl @@ -6,10 +6,12 @@ my $oui; my $addr = "0x1e000"; my $iab; my $term = "/dev/ttyUSB1"; +my $index = 0; GetOptions ('iab=s' => \$iab, 'oui=s' => \$oui, 'term=s' => \$term, + 'index=s' => \$index, ) or die 'bad options'; my $bin = shift; @@ -59,7 +61,7 @@ reverse @words; my $word1 = sprintf("%02X%02X%02X%02X",$words[4],$words[5],$words[6],$words[7]); my $word2 = sprintf("%02X%02X%02X%02X",$words[0],$words[1],$words[2],$words[3]); -my $cmd = "mc1322x-load.pl -e -f $bin -z -t $term -c 'bbmc -l redbee-econotag reset' $addr,0x$word1,0x$word2 &"; +my $cmd = "mc1322x-load.pl -e -f $bin -z -t $term -c 'bbmc -i $index -l redbee-econotag reset' $addr,0x$word1,0x$word2 &"; print "$cmd\n"; system($cmd); diff --git a/tools/test-grid/burn-macs.pl b/tools/test-grid/burn-macs.pl index 311bac768..8dd9ac543 100755 --- a/tools/test-grid/burn-macs.pl +++ b/tools/test-grid/burn-macs.pl @@ -44,7 +44,8 @@ for (my $t=0; $t<$terms; $t++) { my $word2 = sprintf("%02X%02X%02X%02X",$words[0],$words[1],$words[2],$words[3]); my $ftdi_num = $terms - $t - 1; - my $cmd = "mc1322x-load.pl -e -f $bin -z -t /dev/ttyUSB$dev_num -c 'bbmc -l redbee-econotag -i $ftdi_num reset' $addr,0x$word1,0x$word2 &"; + + my $cmd = "../burn-mac.pl --iab=a8c --term=/dev/ttyUSB$dev_num --index=$ftdi_num $bin $dev_num"; print "$cmd\n"; system($cmd); } From 0becce7f00ad1781ce2db00c4abeb6ce1e0d507a Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Wed, 25 May 2011 10:50:59 -0400 Subject: [PATCH 3/6] adc data. Documents about 2.7% gain error and 8 code offset. Also documents flattening out at the region near VrefH. --- doc/adc.gnumeric | Bin 0 -> 3414 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 doc/adc.gnumeric diff --git a/doc/adc.gnumeric b/doc/adc.gnumeric new file mode 100644 index 0000000000000000000000000000000000000000..152c5074f391f1adb580cf8e7416fa9985c3a288 GIT binary patch literal 3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eEy>J- zrt9nCYT@Bj@klZ8{o>=QOeU=#S6{y}a>riF8SxM^6?u3!fv8KKH741Jsj+}^7Id(n zL4S5pmXI5mL$!dg6GMdjFu(h63p1!>Cd5xI^wp9+FZ~->P%9l_{et>kr01SHY+

!wvp0{mbpaQ;S zHOXT;3m+*h@uTWlm*3y$HwhoKW!6RQEC?4+TNkS5Y3_pEh<;G@6wO^QtR^vpP|gLl ztqb)tv<4i~#zKV)mf0kaP;O#IZD(dwKQ(Kr-l*KFpN%yesckF>7uwFqsD4V;v}R#Y zKPiI?O|lu~re)N2Ue>Dv$y}eVU!DBlZxi-PBx0`(z~V~PfB%hdfUAmbDMBC*FL40t zLn)f$kiB#s&oKMxm+;#%j*X=7;v8EdT=5+nX+#&|we`cyVokK7{v+7GrY`XXUj91{ zbmEhFdVYQI=QhIoFHdDlO) sieZAxsrXeRcKYFx?NLkO1w7)8@FX!HTCaa?Y52|m0TrUMmvT)209m}H;{X5v literal 0 HcmV?d00001 From 14e06a0f42ed4c951c32d82b8ff8a0376ca1fde9 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Thu, 2 Jun 2011 07:27:21 -0400 Subject: [PATCH 4/6] Added some code to avoid the generation of pcap files starting directly with a payload --- tools/rftestrx2pcap.pl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/tools/rftestrx2pcap.pl b/tools/rftestrx2pcap.pl index df568cccb..d079e2475 100755 --- a/tools/rftestrx2pcap.pl +++ b/tools/rftestrx2pcap.pl @@ -47,6 +47,7 @@ my $network = 230; # 802.15.4 no FCS my $newpacket = 0; my $len = 0; +my $file_empty = 1; print pack('LSSLLLL',($magic,$major,$minor,$zone,$sig,$snaplen,$network)); @@ -75,15 +76,21 @@ while(1) { $newpacket = 0; print pack('LLLL',($sec,$usec,$len,$len)); print STDERR "new packet: $sec $usec $len " . ($len) . "\n\r"; + # This header starts the file + if ($file_empty == 1) { + $file_empty = 0; + } + } + # packet payload (don't start the file with a payload) + if ($file_empty == 0) { + print STDERR "dataline: "; + print STDERR $str . "\n\r"; + + foreach my $data (@data) { + print pack ('C',hex($data)); + } } - # packet payload - print STDERR "dataline: "; - print STDERR $str . "\n\r"; - - foreach my $data (@data) { - print pack ('C',hex($data)); - } } $str = ''; } From 6624ee04a12f7c3b8c28dc5a2fea466ac62f417d Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Mon, 4 Jul 2011 11:35:11 -0400 Subject: [PATCH 5/6] fix asy syntax for new versions of asy --- doc/lqi-pdr/plot.asy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/lqi-pdr/plot.asy b/doc/lqi-pdr/plot.asy index 93c97c3b3..d109288d6 100644 --- a/doc/lqi-pdr/plot.asy +++ b/doc/lqi-pdr/plot.asy @@ -2,7 +2,8 @@ import graph; size(350,250,IgnoreAspect); file fin=input("./1000pkt-64len.csv"); -real[][] A=dimension(csv(fin),0,2); +fin.csv(); +real[][] A=fin.dimension(0,2); real[][] pdr=transpose(A); int[] lqi = sequence(100); From 6b45e353e1ff9ec0b93749e19e843bc2a1109dc7 Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Mon, 4 Jul 2011 14:07:12 -0400 Subject: [PATCH 6/6] add uart1, uart2 and a common uart.h include --- lib/include/isr.h | 1 + lib/include/mc1322x.h | 2 +- lib/include/{uart1.h => uart.h} | 97 +++++++++++++++++++++++++++++++-- lib/uart2.c | 78 ++++++++++++++++++++++++++ src/default_lowlevel.c | 50 ++++++++++++++--- src/default_lowlevel.h | 1 + src/isr.c | 3 + tests/Makefile | 1 + tests/u1u2-loopback.c | 58 ++++++++++++++++++++ 9 files changed, 276 insertions(+), 15 deletions(-) rename lib/include/{uart1.h => uart.h} (63%) create mode 100644 lib/uart2.c create mode 100644 tests/u1u2-loopback.c diff --git a/lib/include/isr.h b/lib/include/isr.h index e8f5fa867..3377d8d85 100644 --- a/lib/include/isr.h +++ b/lib/include/isr.h @@ -163,6 +163,7 @@ extern void kbi7_isr(void) __attribute__((weak)); extern void cal_isr(void) __attribute__((weak)); extern void uart1_isr(void) __attribute__((weak)); +extern void uart2_isr(void) __attribute__((weak)); extern void maca_isr(void) __attribute__((weak)); diff --git a/lib/include/mc1322x.h b/lib/include/mc1322x.h index e6437e0de..3cec5af32 100644 --- a/lib/include/mc1322x.h +++ b/lib/include/mc1322x.h @@ -44,7 +44,7 @@ #include "kbi.h" #include "maca.h" #include "packet.h" -#include "uart1.h" +#include "uart.h" #include "utils.h" #include "asm.h" diff --git a/lib/include/uart1.h b/lib/include/uart.h similarity index 63% rename from lib/include/uart1.h rename to lib/include/uart.h index d80eac65d..ad738649f 100644 --- a/lib/include/uart1.h +++ b/lib/include/uart.h @@ -33,11 +33,94 @@ * */ -#ifndef UART1_H -#define UART1_H +#ifndef UART_H +#define UART_H #include +/* Timer registers are all 16-bit wide with 16-bit access only */ +#define UART1_BASE (0x80005000) +#define UART2_BASE (0x8000B000) + +struct UART_struct { + union { + uint32_t CON; + struct UART_CON { + uint32_t :16; + uint32_t TST:1; + uint32_t MRXR:1; + uint32_t MTXR:1; + uint32_t FCE:1; + uint32_t FCP:1; + uint32_t XTIM:1; + uint32_t :2; + uint32_t TXOENB:1; + uint32_t CONTX:1; + uint32_t SB:1; + uint32_t ST2:1; + uint32_t EP:1; + uint32_t PEN:1; + uint32_t RXE:1; + uint32_t TXE:1; + } CONbits; + }; + union { + uint32_t STAT; + struct UART_STAT { + uint32_t :24; + uint32_t TXRDY:1; + uint32_t RXRDY:1; + uint32_t RUE:1; + uint32_t ROE:1; + uint32_t TOE:1; + uint32_t FE:1; + uint32_t PE:1; + uint32_t SE:1; + } USTATbits; + }; + union { + uint32_t DATA; + struct UART_DATA { + uint32_t :24; + uint32_t DATA:8; + } DATAbits; + }; + union { + uint32_t RXCON; + struct UART_URXCON { + uint32_t :26; + uint32_t LVL:6; + } RXCONbits; + }; + union { + uint32_t TXCON; + struct UART_TXCON { + uint32_t :26; + uint32_t LVL:6; + } TXCONbits; + }; + union { + uint32_t CTS; + struct UART_CTS { + uint32_t :27; + uint32_t LVL:5; + } CTSbits; + }; + union { + uint32_t BR; + struct UART_BR { + uint32_t INC:16; + uint32_t MOD:16; + } BRbits; + }; +}; + +static volatile struct UART_struct * const UART1 = (void *) (UART1_BASE); +static volatile struct UART_struct * const UART2 = (void *) (UART2_BASE); + +/* Old uart definitions, for compatibility */ +#ifndef REG_NO_COMPAT + #define UCON (0) /* UCON bits */ #define UCON_SAMP 10 @@ -51,9 +134,6 @@ #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 )) @@ -70,11 +150,16 @@ #define UART2_UCTS ((volatile uint32_t *) ( UART2_BASE + UCTS )) #define UART2_UBRCNT ((volatile uint32_t *) ( UART2_BASE + UBRCNT )) +#endif /* REG_NO_COMPAT */ + 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); - +extern volatile uint32_t u2_head, u2_tail; +void uart2_putc(char c); +#define uart2_can_get() (*UART2_URXCON > 0) +uint8_t uart2_getc(void); #endif diff --git a/lib/uart2.c b/lib/uart2.c new file mode 100644 index 000000000..3b9f310f0 --- /dev/null +++ b/lib/uart2.c @@ -0,0 +1,78 @@ +/* + * 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. + * + * + */ + +#include +#include + +volatile char u2_tx_buf[64]; +volatile uint32_t u2_head, u2_tail; + +void uart2_isr(void) { + while( *UART1_UTXCON != 0 ) { + if (u2_head == u2_tail) { + disable_irq(UART2); + return; + } + *UART2_UDATA = u2_tx_buf[u2_tail]; + u2_tail++; + if (u2_tail >= sizeof(u2_tx_buf)) + u2_tail = 0; + } +} + +void uart2_putc(char c) { + /* disable UART2 since */ + /* UART2 isr modifies u2_head and u2_tail */ + disable_irq(UART2); + + if( (u2_head == u2_tail) && + (*UART2_UTXCON != 0)) { + *UART2_UDATA = c; + } else { + u2_tx_buf[u2_head] = c; + u2_head += 1; + if (u2_head >= sizeof(u2_tx_buf)) + u2_head = 0; + if (u2_head == u2_tail) { /* drop chars when no room */ + if (u2_head) { u2_head -=1; } else { u2_head = sizeof(u2_tx_buf); } + } + enable_irq(UART1); + } +} + +uint8_t uart2_getc(void) { + while(uart2_can_get() == 0) { continue; } + return *UART2_UDATA; +} diff --git a/src/default_lowlevel.c b/src/default_lowlevel.c index 35750314a..ca87ec7c4 100644 --- a/src/default_lowlevel.c +++ b/src/default_lowlevel.c @@ -34,6 +34,7 @@ */ #include +#include void default_vreg_init(void) { volatile uint32_t i; @@ -44,16 +45,17 @@ void default_vreg_init(void) { *CRM_VREG_CNTL = 0x00000ff8; /* start the regulators */ } -void uart1_init(uint16_t inc, uint16_t mod, uint8_t samp) { - +void uart1_init(volatile uint16_t inc, volatile uint16_t mod, volatile uint8_t samp) { + /* UART must be disabled to set the baudrate */ - *UART1_UCON = 0; - *UART1_UBRCNT = ( inc << 16 ) | mod; + UART1->CON = 0; + + UART1->BR = ( inc << 16 ) | mod; /* TX and CTS as outputs */ GPIO->PAD_DIR_SET.GPIO_14 = 1; GPIO->PAD_DIR_SET.GPIO_16 = 1; - + /* RX and RTS as inputs */ GPIO->PAD_DIR_RESET.GPIO_15 = 1; GPIO->PAD_DIR_RESET.GPIO_17 = 1; @@ -62,16 +64,17 @@ void uart1_init(uint16_t inc, uint16_t mod, uint8_t samp) { /* you must enable the peripheral first BEFORE setting the function in GPIO_FUNC_SEL */ /* From the datasheet: "The peripheral function will control operation of the pad IF */ /* THE PERIPHERAL IS ENABLED. */ - *UART1_UCON = (1 << 0) | (1 << 1); /* enable receive, transmit */ + UART1->CON = (1 << 0) | (1 << 1); /* enable receive, transmit */ if(samp == UCON_SAMP_16X) set_bit(*UART1_UCON,UCON_SAMP); - /* set GPIO15-14 to UART (UART1 TX and RX)*/ + /* set GPIO15-14 to UART (UART1 TX and RX)*/ GPIO->FUNC_SEL.GPIO_14 = 1; GPIO->FUNC_SEL.GPIO_15 = 1; /* interrupt when there are this number or more bytes free in the TX buffer*/ - *UART1_UTXCON = 16; + UART1->TXCON = 16; + UART1->RXCON = 16; u1_head = 0; u1_tail = 0; @@ -80,3 +83,34 @@ void uart1_init(uint16_t inc, uint16_t mod, uint8_t samp) { /* enable UART1 interrupts in the interrupt controller */ enable_irq(UART1); } + +void uart2_init(volatile uint16_t inc, volatile uint16_t mod, volatile uint8_t samp) { + + /* UART must be disabled to set the baudrate */ + UART2->CON = 0; + UART2->BR = ( inc << 16 ) | mod; + + /* see Section 11.5.1.2 Alternate Modes */ + /* you must enable the peripheral first BEFORE setting the function in GPIO_FUNC_SEL */ + /* From the datasheet: "The peripheral function will control operation of the pad IF */ + /* THE PERIPHERAL IS ENABLED. Can override with U2_ENABLE_DEFAULT. */ + UART2->CON = (1 << 0) | (1 << 1); /* enable receive, transmit */ + + if(samp == UCON_SAMP_16X) + set_bit(*UART2_UCON, samp); + + /* set GPIO18-19 to UART (UART2 TX and RX)*/ + GPIO->FUNC_SEL.GPIO_18 = 1; + GPIO->FUNC_SEL.GPIO_19 = 1; + + /* interrupt when there are this number or more bytes free in the TX buffer*/ + UART2->TXCON = 16; + UART2->RXCON = 16; + + u2_head = 0; u2_tail = 0; + + /* tx and rx interrupts are enabled in the UART by default */ + /* see status register bits 13 and 14 */ + /* enable UART2 interrupts in the interrupt controller */ + enable_irq(UART2); +} diff --git a/src/default_lowlevel.h b/src/default_lowlevel.h index e91ec949b..20fc43e66 100644 --- a/src/default_lowlevel.h +++ b/src/default_lowlevel.h @@ -42,6 +42,7 @@ void default_vreg_init(void); void uart1_init(uint16_t inc, uint16_t mod, uint8_t samp); +void uart2_init(uint16_t inc, uint16_t mod, uint8_t samp); void irq_register_timer_handler(int timer, void (*isr)(void)); diff --git a/src/isr.c b/src/isr.c index fc17659ed..ccc2f1091 100644 --- a/src/isr.c +++ b/src/isr.c @@ -73,6 +73,9 @@ void irq(void) if(bit_is_set(pending, INT_NUM_UART1)) { if(uart1_isr != 0) { uart1_isr(); } } + if(bit_is_set(pending, INT_NUM_UART2)) { + if(uart2_isr != 0) { uart2_isr(); } + } if(bit_is_set(pending, INT_NUM_CRM)) { if(rtc_wu_evt() && (rtc_isr != 0)) { rtc_isr(); } if(kbi_evnt(4) && (kbi4_isr != 0)) { kbi4_isr(); } diff --git a/tests/Makefile b/tests/Makefile index 09e928385..1611c0640 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -8,6 +8,7 @@ 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 \ + u1u2-loopback \ tmr tmr-ints \ sleep \ printf \ diff --git a/tests/u1u2-loopback.c b/tests/u1u2-loopback.c new file mode 100644 index 000000000..4f7262e17 --- /dev/null +++ b/tests/u1u2-loopback.c @@ -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. + * + * + */ + +#include +#include + +#include "tests.h" +#include "config.h" + +void main(void) { + + uart1_init(INC,MOD,SAMP); + uart2_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 */ + uart2_putc(uart1_getc()); + } + if(uart2_can_get()) { + uart1_putc(uart2_getc()); + } + } + +}