rom.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <io.h>
00033
00034 #include "contiki.h"
00035
00036 #include "dev/rom.h"
00037
00038 struct ictx {
00039 int s;
00040 unsigned short ie1, ie2, wdtctl;
00041 };
00042
00043 static void
00044 mask_intr(struct ictx *c)
00045 {
00046
00047 c->s = splhigh();
00048
00049 #define WDTRPW 0x6900
00050
00051
00052 c->wdtctl = WDTCTL;
00053 c->wdtctl ^= (WDTRPW ^ WDTPW);
00054 WDTCTL = WDTPW | WDTHOLD;
00055 c->ie1 = IE1;
00056 IE1 = 0x00;
00057 c->ie2 = IE2;
00058 IE2 = 0x00;
00059
00060
00061 FCTL2 = FWKEY | FSSEL_SMCLK | (FN2 | FN1);
00062 FCTL3 = FWKEY;
00063 }
00064
00065 static void
00066 rest_intr(struct ictx *c)
00067 {
00068 FCTL1 = FWKEY;
00069 FCTL3 = FWKEY | LOCK;
00070
00071 IE2 = c->ie2;
00072 IE1 = c->ie1;
00073 WDTCTL = c->wdtctl;
00074 splx(c->s);
00075 }
00076
00077
00078
00079
00080 static void
00081 blkwrt(void *to, const void *from, const void *from_end)
00082
00083 ;
00084
00085 int
00086 rom_erase(long nbytes, off_t offset)
00087 {
00088 int nb = nbytes;
00089 char *to = (char *)(uintptr_t)offset;
00090 struct ictx c;
00091
00092 if(nbytes % ROM_ERASE_UNIT_SIZE != 0) {
00093 return -1;
00094 }
00095
00096 if(offset % ROM_ERASE_UNIT_SIZE != 0) {
00097 return -1;
00098 }
00099
00100 while (nbytes > 0) {
00101 mask_intr(&c);
00102
00103 FCTL1 = FWKEY | ERASE;
00104 *to = 0;
00105 nbytes -= ROM_ERASE_UNIT_SIZE;
00106 to += ROM_ERASE_UNIT_SIZE;
00107
00108 rest_intr(&c);
00109 }
00110
00111 return nb;
00112 }
00113
00114 int
00115 rom_pwrite(const void *buf, int nbytes, off_t offset)
00116 {
00117 const char *from = buf;
00118 int nb = nbytes;
00119 char *to = (char *)(uintptr_t)offset;
00120 struct ictx c;
00121
00122 mask_intr(&c);
00123
00124 while(nbytes > 0) {
00125 int n = (nbytes > 64) ? 64 : nbytes;
00126 FCTL1 = FWKEY | BLKWRT | WRT;
00127 blkwrt(to, from, from + n);
00128 while(FCTL3 & BUSY);
00129 to += 64;
00130 from += 64;
00131 nbytes -= n;
00132 }
00133
00134 rest_intr(&c);
00135
00136 return nb;
00137 }
00138
00139
00140
00141
00142 asm(".data");
00143 asm(".p2align 1,0");
00144 asm(".type blkwrt,@function");
00145 asm(".section .data");
00146
00147 static void
00148 blkwrt(void *_to, const void *_from, const void *_from_end)
00149 {
00150 unsigned short *to = _to;
00151 const unsigned short *from = _from;
00152 const unsigned short *from_end = _from_end;
00153 do {
00154 *to++ = *from++;
00155 while(!(FCTL3 & WAIT));
00156 } while(from < from_end);
00157 FCTL1 = FWKEY;
00158
00159 }