add rtimers for cxmac
[contiki-2.x.git] / cpu / msp430 / rom.c
blobf9384e7330f922d506ff5ff9399ff093887357a1
1 /*
2 * Copyright (c) 2006, Swedish Institute of Computer Science
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)$Id: rom.c,v 1.2 2006/12/01 15:06:42 bg- Exp $
32 #include <io.h>
34 #include "contiki.h"
36 #include "dev/rom.h"
38 struct ictx {
39 int s;
40 unsigned short ie1, ie2, wdtctl;
43 static void
44 mask_intr(struct ictx *c)
46 /* Disable all interrupts. */
47 c->s = splhigh();
49 #define WDTRPW 0x6900 /* Watchdog key returned by read */
51 /* Disable all NMI-Interrupt sources */
52 c->wdtctl = WDTCTL;
53 c->wdtctl ^= (WDTRPW ^ WDTPW);
54 WDTCTL = WDTPW | WDTHOLD;
55 c->ie1 = IE1;
56 IE1 = 0x00;
57 c->ie2 = IE2;
58 IE2 = 0x00;
60 /* MSP430F1611 257 < f < 476 kHz, 2.4576MHz/(5+1) = 409.6 kHz. */
61 FCTL2 = FWKEY | FSSEL_SMCLK | (FN2 | FN1);
62 FCTL3 = FWKEY; /* Unlock flash. */
65 static void
66 rest_intr(struct ictx *c)
68 FCTL1 = FWKEY; /* Disable erase or write. */
69 FCTL3 = FWKEY | LOCK; /* Lock flash. */
70 /* Restore interrupts. */
71 IE2 = c->ie2;
72 IE1 = c->ie1;
73 WDTCTL = c->wdtctl;
74 splx(c->s);
78 * This helper routine must reside in RAM!
80 static void
81 blkwrt(void *to, const void *from, const void *from_end)
82 // __attribute__ ((section(".data")))
85 int
86 rom_erase(long nbytes, off_t offset)
88 int nb = nbytes;
89 char *to = (char *)(uintptr_t)offset;
90 struct ictx c;
92 if(nbytes % ROM_ERASE_UNIT_SIZE != 0) {
93 return -1;
96 if(offset % ROM_ERASE_UNIT_SIZE != 0) {
97 return -1;
100 while (nbytes > 0) {
101 mask_intr(&c);
103 FCTL1 = FWKEY | ERASE; /* Segment erase. */
104 *to = 0; /* Erase segment containing to. */
105 nbytes -= ROM_ERASE_UNIT_SIZE;
106 to += ROM_ERASE_UNIT_SIZE;
108 rest_intr(&c);
111 return nb;
115 rom_pwrite(const void *buf, int nbytes, off_t offset)
117 const char *from = buf;
118 int nb = nbytes;
119 char *to = (char *)(uintptr_t)offset;
120 struct ictx c;
122 mask_intr(&c);
124 while(nbytes > 0) {
125 int n = (nbytes > 64) ? 64 : nbytes;
126 FCTL1 = FWKEY | BLKWRT | WRT; /* Enable block write. */
127 blkwrt(to, from, from + n);
128 while(FCTL3 & BUSY);
129 to += 64;
130 from += 64;
131 nbytes -= n;
134 rest_intr(&c);
136 return nb;
140 * This helper routine must reside in RAM!
142 asm(".data");
143 asm(".p2align 1,0");
144 asm(".type blkwrt,@function");
145 asm(".section .data");
147 static void
148 blkwrt(void *_to, const void *_from, const void *_from_end)
150 unsigned short *to = _to;
151 const unsigned short *from = _from;
152 const unsigned short *from_end = _from_end;
153 do {
154 *to++ = *from++;
155 while(!(FCTL3 & WAIT));
156 } while(from < from_end);
157 FCTL1 = FWKEY; /* Disable block write. */
158 /* Now ROM is available again! */