Skip to content

Commit

Permalink
Added calibrate-dco from tinyos-msp430
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Vagnetoft committed Feb 14, 2012
1 parent 485235e commit ac7f30b
Show file tree
Hide file tree
Showing 11 changed files with 887 additions and 0 deletions.
Binary file added contrib/calibrate-dco-0.7.tar.gz
Binary file not shown.
77 changes: 77 additions & 0 deletions contrib/calibrate-dco-0.7/Makefile
Original file line number Diff line number Diff line change
@@ -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=<frequency_in_hz>
# if you want to use the XTAL for LFXT1 other than 32.768kHz
#
# FREQn_KHZ=<nth_calibration_frequency_in_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:
102 changes: 102 additions & 0 deletions contrib/calibrate-dco-0.7/calibrate_dco.c
Original file line number Diff line number Diff line change
@@ -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:
*/
96 changes: 96 additions & 0 deletions contrib/calibrate-dco-0.7/check_freq.h
Original file line number Diff line number Diff line change
@@ -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 <msp430.h>

#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:
*/
48 changes: 48 additions & 0 deletions contrib/calibrate-dco-0.7/copy.c
Original file line number Diff line number Diff line change
@@ -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:
*/
46 changes: 46 additions & 0 deletions contrib/calibrate-dco-0.7/copy.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
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:
*/
Loading

0 comments on commit ac7f30b

Please sign in to comment.