From ac7f30bca5baa7cf92b8ff5fa3fcf4487ebfe879 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Tue, 14 Feb 2012 15:21:14 +0100 Subject: [PATCH] Added calibrate-dco from tinyos-msp430 --- contrib/calibrate-dco-0.7.tar.gz | Bin 0 -> 5721 bytes contrib/calibrate-dco-0.7/Makefile | 77 ++++++++++ contrib/calibrate-dco-0.7/calibrate_dco.c | 102 +++++++++++++ contrib/calibrate-dco-0.7/check_freq.h | 96 ++++++++++++ contrib/calibrate-dco-0.7/copy.c | 48 ++++++ contrib/calibrate-dco-0.7/copy.h | 46 ++++++ contrib/calibrate-dco-0.7/dco.c | 161 ++++++++++++++++++++ contrib/calibrate-dco-0.7/dco.h | 67 ++++++++ contrib/calibrate-dco-0.7/delay.h | 63 ++++++++ contrib/calibrate-dco-0.7/flash.c | 177 ++++++++++++++++++++++ contrib/calibrate-dco-0.7/flash.h | 50 ++++++ 11 files changed, 887 insertions(+) create mode 100644 contrib/calibrate-dco-0.7.tar.gz create mode 100644 contrib/calibrate-dco-0.7/Makefile create mode 100644 contrib/calibrate-dco-0.7/calibrate_dco.c create mode 100644 contrib/calibrate-dco-0.7/check_freq.h create mode 100644 contrib/calibrate-dco-0.7/copy.c create mode 100644 contrib/calibrate-dco-0.7/copy.h create mode 100644 contrib/calibrate-dco-0.7/dco.c create mode 100644 contrib/calibrate-dco-0.7/dco.h create mode 100644 contrib/calibrate-dco-0.7/delay.h create mode 100644 contrib/calibrate-dco-0.7/flash.c create mode 100644 contrib/calibrate-dco-0.7/flash.h diff --git a/contrib/calibrate-dco-0.7.tar.gz b/contrib/calibrate-dco-0.7.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..a6249ea0e3e29beb8546fbf6a62c717659a9d365 GIT binary patch literal 5721 zcmZwIRa6szqsQ?vIz&1oN65$_rK3hlNK1?kDJf~CM>D#l1tg`r1Ox%;?vRj1K)T0v z?>+aN|9!bHpYwhCz5ZBY2mnHyKn(!)vDBgOvU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4>EZDyHF`L*Gf}$gJ*5EdE*x-t{5R{M6h-Xt#X zE2jYNqr%v~VD4oVdMUb;$N- zUyMZo;8??xV}IPPq%=Qe^_XjDPTjh#{nC}zG6yG1PA7B?1}_3mai0azCsFg5)6SQ< z%v1l{2+|44E}Xk(@O&Y6=OdtR#?jO&VpR2u(>&%~jk3eps+SQer3PfUR?b&7|82wb z8BT3+Pi4-ajDepTf{w}7$4f*LxVFfS5KSD)i3uArGHu8VzSeXY<%4%4GD z%ct9#1n0jui%l~%w=4g7Rm`3T$eenj9nkYW%K}@aqAajQsSG;~1gVrVl z%;*wMxRoU$6kSM(M8fs;kO}CHB|VDd%GmYKHXBL{+?m23NKdjXN4xsz`@ZrW5V47% za+q|Jb(}gY(pO8b7Kkh6G0Ywqmc?H~S1_mq=7R3ekHes)bMt0YIzPr{lomHTAsrjyvvy-~vNtRZcjcHHg@7P>u#&dm6+z`Qi|e4*>iZM6E${ literal 0 HcmV?d00001 diff --git a/contrib/calibrate-dco-0.7/Makefile b/contrib/calibrate-dco-0.7/Makefile new file mode 100644 index 00000000..90351608 --- /dev/null +++ b/contrib/calibrate-dco-0.7/Makefile @@ -0,0 +1,77 @@ +# -*- mode: makefile-gmake; mode: flyspell; -*- + +# Makefile for calibrate DCO oscillator + +TARGET=calibrate_dco + +# Options may be specified by OPTION variable in command line. +# +# WRITE_TO_FLASH +# if you want to erase the Segment A flash and write the calibrated +# value into Segment A. +# +# USE_ROSC +# if you want to use external Rosc. +# +# LFXT1_HZ= +# if you want to use the XTAL for LFXT1 other than 32.768kHz +# +# FREQn_KHZ= +# if you want to alter the calibration frequency. n=2..4 + +# For example +# +# make msp430f2132 OPTION='WRITE_TO_FLASH LFXT1=32768' ERRATA='BCL12 XOSC8' +# +# will make calibrate_dco which writes calibrated values of DCO, using +# 32768kHz LFXT1 xtal, onto Segment A flash for msp430f2131, which has +# errata BCL12 and XOSC8. + +default: msp430f2013 + +msp430%: +ifneq ($(ERRATA),) + $(MAKE) clean $(TARGET).hex MCU=$@ ERRATA="$(ERRATA)" +else + $(MAKE) clean $(TARGET).hex MCU=$@ ERRATA="$(ERRATA_$@)" +endif + +# Default silicon errata table +ERRATA_msp430f1121=BCL5 +ERRATA_msp430f1132=BCL5 +ERRATA_msp430f1232=BCL5 +ERRATA_msp430f1611=BCL5 +ERRATA_msp430f1612=BCL5 +ERRATA_msp430f2012=BCL12 XOSC8 +ERRATA_msp430f2013=BCL12 +ERRATA_msp430f2131=BCL12 +ERRATA_msp430f2132=BCL12 XOSC8 +ERRATA_msp430f2274=BCL12 +ERRATA_msp430g2211=BCL12 XOSC8 +ERRATA_msp430g2231=BCL12 XOSC8 +ERRATA_msp430f2618=BCL12 + +CC=msp430-gcc +CFLAGS+=-Os -Wall -mmcu=$(MCU) +LDFLAGS+=-mmcu=$(MCU) -mdisable-watchdog +OBJCOPY=msp430-objcopy + +ifneq ($(OPTION),) +CFLAGS+=$(foreach _opt,$(OPTION),$(patsubst %,-D%,$(_opt))) +endif + +ifneq ($(ERRATA),) +CFLAGS+=$(foreach _errata,$(ERRATA),$(patsubst %,-DERRATA_%,$(_errata))) +endif + +calibrate_dco: calibrate_dco.o flash.o dco.o copy.o + +%.hex : % + $(OBJCOPY) --output-target=ihex $< $@ + +clean: + -rm -f *~ *.o $(TARGET) $(TARGET).hex + +.PHONY: clean + +# vim: set noet ts=8 sw=8: diff --git a/contrib/calibrate-dco-0.7/calibrate_dco.c b/contrib/calibrate-dco-0.7/calibrate_dco.c new file mode 100644 index 00000000..82966146 --- /dev/null +++ b/contrib/calibrate-dco-0.7/calibrate_dco.c @@ -0,0 +1,102 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include "dco.h" +#include "flash.h" +#include "delay.h" +#include "copy.h" + +#include "check_freq.h" + +#if defined(__MSP430_HAS_FLASH2__) +#define SEGMENT_A 0x10c0 +uint16_t backup_segment_a[32]; /* 64 bytes */ +#define INDEX_FREQ4 28 +#define INDEX_FREQ3 29 +#define INDEX_FREQ2 30 +#define INDEX_1MHZ 31 +#else /* !defined(__MSP430_HAS_FLASH2__) */ +#define SEGMENT_A 0x1080 +uint16_t backup_segment_a[64]; /* 128 byte */ +#define INDEX_FREQ4 60 +#define INDEX_FREQ3 61 +#define INDEX_FREQ2 62 +#define INDEX_1MHZ 63 +#endif + +static void blink_led(void) { + dco_set(backup_segment_a[INDEX_1MHZ]); + uint16_t notify_loop = 10; + while (--notify_loop != 0) { + /* turn on LED */ + P1OUT = 0x01; + delay_1000n(100); + + /* turn off LED */ + P1OUT = 0x00; + delay_1000n(900); + } +} + +int main() __attribute__ ((noreturn)); +int main() { + /* Configure P1.0 as output */ + P1DIR = 0x01; + + copy_word(backup_segment_a, (const uint16_t *)SEGMENT_A, ARRAY_SIZE(backup_segment_a)); + + dco_setup_calibrate(); + backup_segment_a[INDEX_1MHZ] = dco_calibrate(1000); /* 1MHz */ + backup_segment_a[INDEX_FREQ2] = dco_calibrate(FREQ2_KHZ); + backup_segment_a[INDEX_FREQ3] = dco_calibrate(FREQ3_KHZ); + backup_segment_a[INDEX_FREQ4] = dco_calibrate(FREQ4_KHZ); + +#if defined(WRITE_TO_FLASH) + flash_setup(backup_segment_a[INDEX_1MHZ]); + flash_erase_segment_a(); + flash_write_block(backup_segment_a, (uint16_t *)SEGMENT_A, ARRAY_SIZE(backup_segment_a)); + blink_led(); + flash_erase_mass(); /* erase main flash, then turn on LED */ +#else + for (;;) + blink_led(); +#endif +} + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/check_freq.h b/contrib/calibrate-dco-0.7/check_freq.h new file mode 100644 index 00000000..b864d605 --- /dev/null +++ b/contrib/calibrate-dco-0.7/check_freq.h @@ -0,0 +1,96 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include + +#if defined(__MSP430_HAS_BC2__) + +#if !defined(FREQ2_KHZ) +#define FREQ2_KHZ 8000 /* 8MHz */ +#elif FREQ2_KHZ < 100 +#error "FREQ2_KHZ is too low" +#elif FREQ2_KHZ > 16000 +#error "FREQ2_KHZ is too high" +#endif + +#if !defined(FREQ3_KHZ) +#define FREQ3_KHZ 12000 /* 12MHz */ +#elif FREQ3_KHZ < 100 +#error "FREQ3_KHZ is too low" +#elif FREQ3_KHZ > 16000 +#error "FREQ3_KHZ is too high" +#endif + +#if !defined(FREQ4_KHZ) +#define FREQ4_KHZ 16000 /* 16MHz */ +#elif FREQ4_KHZ < 100 +#error "FREQ4_KHZ is too low" +#elif FREQ4_KHZ > 16000 +#error "FREQ4_KHZ is too high" +#endif + +#else + +#if !defined(FREQ2_KHZ) +#define FREQ2_KHZ 8000 /* 8MHz */ +#elif FREQ2_KHZ < 100 +#error "FREQ2_KHZ is too low" +#elif FREQ2_KHZ > 8000 +#error "FREQ2_KHZ is too high" +#endif + +#if !defined(FREQ3_KHZ) +#define FREQ3_KHZ 2000 /* 2MHz */ +#elif FREQ3_KHZ < 100 +#error "FREQ3_KHZ is too low" +#elif FREQ3_KHZ > 8000 +#error "FREQ3_KHZ is too high" +#endif + +#if !defined(FREQ4_KHZ) +#define FREQ4_KHZ 4000 /* 4MHz */ +#elif FREQ4_KHZ < 100 +#error "FREQ4_KHZ is too low" +#elif FREQ4_KHZ > 8000 +#error "FREQ4_KHZ is too high" +#endif + +#endif + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/copy.c b/contrib/calibrate-dco-0.7/copy.c new file mode 100644 index 00000000..58a7c2aa --- /dev/null +++ b/contrib/calibrate-dco-0.7/copy.c @@ -0,0 +1,48 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include "copy.h" + +void copy_word(uint16_t *to, const uint16_t *from, uint16_t words) { + while (words-- > 0) { + *to++ = *from++; + } +} + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/copy.h b/contrib/calibrate-dco-0.7/copy.h new file mode 100644 index 00000000..43002609 --- /dev/null +++ b/contrib/calibrate-dco-0.7/copy.h @@ -0,0 +1,46 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include +void copy_word(uint16_t *to, const uint16_t *from, uint16_t words); +#if !defined(ARRAY_SIZE) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#endif + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/dco.c b/contrib/calibrate-dco-0.7/dco.c new file mode 100644 index 00000000..20d926c5 --- /dev/null +++ b/contrib/calibrate-dco-0.7/dco.c @@ -0,0 +1,161 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include "dco.h" +#include "delay.h" + +#if !defined(LFXT1_HZ) +#define LFXT1_HZ 32768 /* 32.768kHz */ +#elif LFXT1_HZ < 1000 +#error "LFXT1_HZ is too low" +#elif LFXT1_HZ > 40000 +#error "LFXT1_HZ is too high" +#endif + +void dco_set(uint16_t calib) { +#if defined(ERRATA_BCL12) + /* Errata BCL12 - Switching RSEL can cause DCO dead time + * RSEL=14..15 -> RSEL=0..12 : switch to 13 first, then to target RSEL + * RSEL=0..12 -> RSEL=13..15: set DCO to 0 first, then set target RSEL, and set DCO. */ + uint8_t old_bc1 = BCSCTL1; + uint8_t cur_rsel = old_bc1 & RSEL_MASK; + uint8_t new_rsel = calib2rsel(calib); + old_bc1 &= ~RSEL_MASK; + if (cur_rsel >= 14 && new_rsel <= 12) { + BCSCTL1 = old_bc1 | 13; + BCSCTL1 = old_bc1 | new_rsel; + DCOCTL = calib2dco(calib); + } else { + DCOCTL &= ~(DCO2|DCO1|DCO0); /* DCO=0 */ + BCSCTL1 = old_bc1 | new_rsel; + DCOCTL = calib2dco(calib); + } +#elif defined(ERRATA_BCL5) + /* Errata BCL5 - RSEL bit modification can generate high frequency spikes on MCLK + * DIVM=0 or 1 cause this issue. + * Setting DIVM=2 or 3 and modify RSEL and restore DIVM will work. */ + uint8_t old_bc2 = BCSCTL2; + BCSCTL2 |= DIVM1; + BCSCTL1 = (BCSCTL1 & ~RSEL_MASK) | calib2rsel(calib); + DCOCTL = calib2dco(calib); + BCSCTL2 = old_bc2; +#else + BCSCTL1 = (BCSCTL1 & ~RSEL_MASK) | calib2rsel(calib); + DCOCTL = calib2dco(calib); +#endif +} + +static uint16_t dco_fine(uint16_t calib, uint16_t dco_khz) { + dco_set(calib); + P1OUT ^= 1; + TACTL |= TACLR; /* clear TAR */ + delay_1000n(dco_khz); + return TAR; +} + +static uint16_t dco_coarse(uint16_t calib, uint16_t dco_khz) { + dco_set(calib); + P1OUT ^= 1; + TACTL |= TACLR; /* clear TAR */ + delay_10n(dco_khz); + return TAR; +} + +uint16_t dco_calibrate(uint16_t dco_khz) { + uint16_t calib = 0; + uint16_t step; + for (step = rsel2calib(RSEL_MAXBIT); step >= DCO0; step >>= 1) { + if (dco_coarse(calib | step, dco_khz) >= LFXT1_HZ / 100) + calib |= step; + } + + for (step = rsel2calib(RSEL_MAXBIT); step != 0; step >>= 1) { + if (dco_fine(calib | step, dco_khz) >= LFXT1_HZ) { + calib |= step; + } else { + calib &= ~step; + } + } + + if ((calib2dco(calib) & (DCO2 | DCO1 | DCO0)) == (DCO2 | DCO1 | DCO0)) + calib &= ~(MOD4 | MOD3 | MOD2 | MOD1 | MOD0); /* clear MODx */ + + return calib | (XT2OFF << 8); +} + +void dco_setup_calibrate() { + /* set LFXT1/32.768kHz XTAL oscillator */ + BCSCTL1 &= ~(XTS | DIVA1 | DIVA0); /* LF mode, ACLK=LFXT1CLK/1 */ +#if defined(__MSP430_HAS_BC2__) +#if defined(ERRATA_XOSC8) + BCSCTL3 = LFXT1S_0 | XCAP_3; /* LFXT1=32.768kHz, CAP=12.5pF */ +#else + BCSCTL3 = LFXT1S_0 | XCAP_2; /* LFXT1=32.768kHz, CAP=10pF */ +#endif + P2SEL |= 0xc0; /* enable P2.6/XIN and P2.7/XOUT */ + P2OUT |= 0x80; /* some chips need P2.7 set as OUT */ +#endif + + /* turn on DCO and LFXT1 */ + _BIC_SR(SCG0 | OSCOFF); + +#if defined(__MSP430_HAS_BC2__) + /* wait for oscillating */ + do { + IFG1 &= ~OFIFG; + delay_995(); + } while ((IFG1 & OFIFG) != 0); +#else + /* In MSP430x1xx, OFIFG works only for LFXT1/HF mode or XT2 */ + delay_1000n(100); +#endif + +#if defined(USE_ROSC) + /* MCLK=DCOCLK/1, SMCLK=DCOCLK/1, external Rosc */ + BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0 | DCOR; +#else + /* MCLK=DCOCLK/1, SMCLK=DCOCLK/1, internal Rosc */ + BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0; +#endif + + /* TimerA=ACLK/1, continuous mode, reset */ + TACTL = TASSEL_1 | ID_0 | MC_2 | TACLR; +} + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/dco.h b/contrib/calibrate-dco-0.7/dco.h new file mode 100644 index 00000000..8d49b97c --- /dev/null +++ b/contrib/calibrate-dco-0.7/dco.h @@ -0,0 +1,67 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include +#include + +#if defined(__MSP430_HAS_BC2__) && !defined(USE_ROSC) +static const uint8_t RSEL_MASK = RSEL3|RSEL2|RSEL1|RSEL0; +static const uint8_t RSEL_MAXBIT = RSEL3; +#else +static const uint8_t RSEL_MASK = RSEL2|RSEL1|RSEL0; +static const uint8_t RSEL_MAXBIT = RSEL2; +#endif + +static inline uint8_t calib2dco(uint16_t calib) { + return calib & 0xff; +} + +static inline uint8_t calib2rsel(uint16_t calib) { + return (calib >> 8) & RSEL_MASK; +} + +static inline uint16_t rsel2calib(uint8_t rsel) { + return rsel << 8; +} + +void dco_set(uint16_t calib); +void dco_setup_calibrate(void); +uint16_t dco_calibrate(uint16_t dco_khz); + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/delay.h b/contrib/calibrate-dco-0.7/delay.h new file mode 100644 index 00000000..f1009851 --- /dev/null +++ b/contrib/calibrate-dco-0.7/delay.h @@ -0,0 +1,63 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +static inline void delay_10n(uint16_t n) { + do { + __asm__ __volatile__ ("nop"); /* 1 cycle */ + __asm__ __volatile__ ("jmp $+2"); /* 2 cycles */ + __asm__ __volatile__ ("jmp $+2"); /* 2 cycles */ + __asm__ __volatile__ ("jmp $+2"); /* 2 cycles */ + } while (--n != 0); /* add, jnz: (1+2)=3 cycles */ +} + +static inline void delay_995(void) { + uint16_t m = 331; /* mov: 2 cycles */ + do { + __asm__ __volatile__ (""); + } while (--m != 0); /* add, jnz: (1+2)*331=993 cycles */ +} + +static inline void delay_1000n(uint16_t n) { + do { + delay_995(); + __asm__ __volatile__ ("nop"); /* 1 cycles */ + } while (--n != 0); /* add, cmp, jnz: (1+1+2)=4 cycles */ +} + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/flash.c b/contrib/calibrate-dco-0.7/flash.c new file mode 100644 index 00000000..32acfe16 --- /dev/null +++ b/contrib/calibrate-dco-0.7/flash.c @@ -0,0 +1,177 @@ +/* -*- mode: c; mode: flyspell-prog; -*- */ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include + +#include "flash.h" +#include "dco.h" +#include "copy.h" + +uint16_t code_in_ram[18]; +#define CODE_IN_FLASH 0 /* must be 0, debug purpose */ + +static void flash_lock_segment(void) __attribute__ ((noinline)); +static void flash_lock_segment(void) { +#if defined(__MSP430_HAS_FLASH2__) + uint16_t lock_bits = (FCTL3 & LOCKA) ^ LOCKA; + FCTL3 = FWKEY | LOCK | lock_bits; +#else + FCTL3 = FWKEY | LOCK; +#endif +} + +static void flash_unlock_segment(void) __attribute__ ((noinline)); +static void flash_unlock_segment(void) { +#if defined(__MSP430_HAS_FLASH2__) + uint16_t lock_bits = FCTL3 & LOCKA; + FCTL3 = FWKEY | lock_bits; +#else + FCTL3 = FWKEY; +#endif +} + +void flash_setup(uint16_t calibrated_1mhz_dco) { + dco_set(calibrated_1mhz_dco); + FCTL2 = FWKEY | FSSEL_1 | (3 - 1); /* fFTG=MCLK/3=333kHz */ +} + +void flash_erase_segment_a() { + flash_unlock_segment(); + FCTL1 = FWKEY | ERASE; + *(uint16_t *)0x10c0 = 0; /* trigger erase segment A */ + FCTL1 = FWKEY; + flash_lock_segment(); +} + +#if CODE_IN_FLASH +static void do_mass_erase(uint16_t *etext) __attribute__ ((noreturn,noinline)); +static void do_mass_erase(uint16_t *etext) { + FCTL3 = FWKEY; /* unlock main flash */ + FCTL1 = FWKEY | MERAS; + *etext = 0; /* trigger mass erase main flash */ + while (FCTL3 & BUSY) /* wait for erasing */ + ; + FCTL3 = FWKEY | LOCK; /* lock main flash */ + P1OUT |= 1; /* turn LED on */ + for (;;) + ; +} +#else +typedef void (*do_mass_erase_t)(uint16_t *etext) __attribute__ ((noreturn)); +#define do_mass_erase(etext) \ + do { \ + copy_word(code_in_ram, do_mass_erase_body, ARRAY_SIZE(do_mass_erase_body)); \ + ((do_mass_erase_t)code_in_ram)(etext); \ + } while (0) +static const uint16_t do_mass_erase_body[] = { + 0x40b2, 0xa500, 0x012c, /* mov #FWKEY,&FCTL3 */ + 0x40b2, 0xa504, 0x0128, /* mov #FWKEY|MERAS,&FCTL1 */ + 0x438f, 0x0000, /* mov #0,0(r15) */ + 0xb392, 0x012c, /* 1: bit #BUSY,&FCTL3 */ + 0x23fd, /* jnz 1b */ + 0x40b2, 0xa510, 0x012c, /* mov #FWKEY|LOCK,&FCTL3 */ + 0xd3d2, 0x0021, /* bis.b #1,&P1OUT */ + 0x3fff, /* 2: jmp 2b */ +}; +#endif + +void flash_erase_mass() { + extern uint16_t _etext[]; /* end of .text section */ + do_mass_erase(_etext); +} + +#if CODE_IN_FLASH +static void do_block_write(uint16_t *to, const uint16_t *from, uint16_t *end) + __attribute__ ((noinline)); +static void do_block_write(uint16_t *to, const uint16_t *from, uint16_t *end) { + FCTL1 = FWKEY | BLKWRT | WRT; /* enable block write */ + while (to < end) { + *to++ = *from++; + while ((FCTL3 & WAIT) == 0) /* wait for buffer ready */ + ; + } + FCTL1 = FWKEY; + while (FCTL3 & BUSY) /* wait for writing completion */ + ; +} +#else +typedef void (*do_block_write_t)(uint16_t *to, const uint16_t *from, uint16_t *end); +#define do_block_write(to, from, end) \ + do { \ + copy_word(code_in_ram, do_block_write_body, ARRAY_SIZE(do_block_write_body)); \ + ((do_block_write_t)code_in_ram)((to), (from), (end)); \ + } while (0) +static const uint16_t do_block_write_body[] = { + 0x40b2, 0xa5c0, 0x0128, /* mov #FWKEY|BLKWRT|WRT,&FCTL1 */ + 0x4ebf, 0x0000, /* 1: mov @r14+,0(r15) */ + 0xb2b2, 0x012c, /* 2: bit #WAIT,&FCTL3 */ + 0x27fd, /* jz 2b */ + 0x532f, /* incd r15 */ + 0x9d0f, /* cmp r13,r15 */ + 0x2bf8, /* jnc 1b */ + 0x40b2, 0xa500, 0x0128, /* mov #FWKEY,&FCTL1 */ + 0xb392, 0x012c, /* 3: bit #BUSY,&FCTL3 */ + 0x23fd, /* jnz 3b */ + 0x4130, /* ret */ +}; +#endif + +void flash_write_block(const uint16_t *from, uint16_t *to, uint16_t words) { + flash_unlock_segment(); + while (words > 0) { + uint16_t size = (words < 32) ? words : 32; + do_block_write(to, from, to + size); + to += size; + from += size; + words -= size; + } + flash_lock_segment(); +} + +#if 0 +void flash_write_word(uint16_t data, uintptr_t addr) { + flash_unlock_segment(); + FCTL1 = FWKEY | WRT; + *(uint16_t *)addr = data; + FCTL1 = FWKEY; + flash_lock_segment(); +} +#endif + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */ diff --git a/contrib/calibrate-dco-0.7/flash.h b/contrib/calibrate-dco-0.7/flash.h new file mode 100644 index 00000000..8f9c1f96 --- /dev/null +++ b/contrib/calibrate-dco-0.7/flash.h @@ -0,0 +1,50 @@ +/* -*- mode: c; mode: flyspell-prog; -*-*/ +/* Copyright (c) 2010, Tadashi G Takaoka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of Tadashi G. Takaoka 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ + +#include + +void flash_setup(uint16_t calibrated_1mhz_dco); +void flash_erase_segment_a(); +void flash_erase_mass() __attribute__ ((noreturn)); +void flash_write_block(const uint16_t *from, uint16_t *to, uint16_t words); +#if 0 +void flash_write_word(uint16_t data, uintptr_t addr); +#endif + +/* + * Local Variables: + * c-file-style: "bsd" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: set et ts=4 sw=4: + */