00001
00047 #include <sys/timer.h>
00048 #include <dev/avrtarget.h>
00049
00054
00055 #ifndef AVRTARGET_CHIPERASE_TIMEOUT
00056
00059 #define AVRTARGET_CHIPERASE_TIMEOUT 200
00060 #endif
00061
00062 #ifndef AVRTARGET_PAGEWRITE_TIMEOUT
00063
00066 #define AVRTARGET_PAGEWRITE_TIMEOUT 10
00067 #endif
00068
00086 #define AVR_FUSES_NEVER_PROG 0x00F88062UL
00087
00105 #define AVR_FUSES_ALWAYS_PROG 0x0000201DUL
00106
00107 #ifndef AVRTARGET_SPI_MODE
00108 #define AVRTARGET_SPI_MODE 0
00109 #endif
00110
00111 #ifndef AVRTARGET_SPI_RATE
00112 #define AVRTARGET_SPI_RATE 500000
00113 #endif
00114
00115 #if defined(AVRTARGET_SPI0_DEVICE)
00116
00117 #include <dev/sppif0.h>
00118 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00119 #define TargetReset(act) Sppi0ChipReset(AVRTARGET_SPI0_DEVICE, act)
00120 #else
00121 #define TargetReset(act) Sppi0ChipReset(AVRTARGET_SPI0_DEVICE, !act)
00122 #endif
00123 #define SpiSetMode() Sppi0SetMode(AVRTARGET_SPI0_DEVICE, AVRTARGET_SPI_MODE)
00124 #define SpiSetSpeed() Sppi0SetSpeed(AVRTARGET_SPI0_DEVICE, AVRTARGET_SPI_RATE)
00125 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00126 #define SpiSelect() Sppi0SelectDevice(AVRTARGET_SPI0_DEVICE)
00127 #define SpiDeselect() Sppi0DeselectDevice(AVRTARGET_SPI0_DEVICE)
00128 #else
00129 #define SpiSelect() Sppi0NegSelectDevice(AVRTARGET_SPI0_DEVICE)
00130 #define SpiDeselect() Sppi0NegDeselectDevice(AVRTARGET_SPI0_DEVICE)
00131 #endif
00132 #define SpiByte Sppi0Byte
00133
00134 #elif defined(AVRTARGET_SBBI0_DEVICE)
00135
00136 #include <dev/sbbif0.h>
00137 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00138 #define TargetReset(act) Sbbi0ChipReset(AVRTARGET_SBBI0_DEVICE, act)
00139 #else
00140 #define TargetReset(act) Sbbi0ChipReset(AVRTARGET_SBBI0_DEVICE, !act)
00141 #endif
00142 #define SpiSetMode() Sbbi0SetMode(AVRTARGET_SBBI0_DEVICE, AVRTARGET_SPI_MODE)
00143 #define SpiSetSpeed() Sbbi0SetSpeed(AVRTARGET_SBBI0_DEVICE, AVRTARGET_SPI_RATE)
00144 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00145 #define SpiSelect() Sbbi0SelectDevice(AVRTARGET_SBBI0_DEVICE)
00146 #define SpiDeselect() Sbbi0DeselectDevice(AVRTARGET_SBBI0_DEVICE)
00147 #else
00148 #define SpiSelect() Sbbi0NegSelectDevice(AVRTARGET_SBBI0_DEVICE)
00149 #define SpiDeselect() Sbbi0NegDeselectDevice(AVRTARGET_SBBI0_DEVICE)
00150 #endif
00151 #define SpiByte Sbbi0Byte
00152
00153 #elif defined(AVRTARGET_SBBI1_DEVICE)
00154
00155 #include <dev/sbbif1.h>
00156 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00157 #define TargetReset(act) Sbbi1ChipReset(AVRTARGET_SBBI1_DEVICE, act)
00158 #else
00159 #define TargetReset(act) Sbbi1ChipReset(AVRTARGET_SBBI1_DEVICE, !act)
00160 #endif
00161 #define SpiSetMode() Sbbi1SetMode(AVRTARGET_SBBI1_DEVICE, AVRTARGET_SPI_MODE)
00162 #define SpiSetSpeed() Sbbi1SetSpeed(AVRTARGET_SBBI1_DEVICE, AVRTARGET_SPI_RATE)
00163 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00164 #define SpiSelect() Sbbi1SelectDevice(AVRTARGET_SBBI1_DEVICE)
00165 #define SpiDeselect() Sbbi1DeselectDevice(AVRTARGET_SBBI1_DEVICE)
00166 #else
00167 #define SpiSelect() Sbbi1NegSelectDevice(AVRTARGET_SBBI1_DEVICE)
00168 #define SpiDeselect() Sbbi1NegDeselectDevice(AVRTARGET_SBBI1_DEVICE)
00169 #endif
00170 #define SpiByte Sbbi1Byte
00171
00172 #elif defined(AVRTARGET_SBBI2_DEVICE)
00173
00174 #include <dev/sbbif2.h>
00175 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00176 #define TargetReset(act) Sbbi2ChipReset(AVRTARGET_SBBI2_DEVICE, act)
00177 #else
00178 #define TargetReset(act) Sbbi2ChipReset(AVRTARGET_SBBI2_DEVICE, !act)
00179 #endif
00180 #define SpiSetMode() Sbbi2SetMode(AVRTARGET_SBBI2_DEVICE, AVRTARGET_SPI_MODE)
00181 #define SpiSetSpeed() Sbbi2SetSpeed(AVRTARGET_SBBI2_DEVICE, AVRTARGET_SPI_RATE)
00182 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00183 #define SpiSelect() Sbbi2SelectDevice(AVRTARGET_SBBI2_DEVICE)
00184 #define SpiDeselect() Sbbi2DeselectDevice(AVRTARGET_SBBI2_DEVICE)
00185 #else
00186 #define SpiSelect() Sbbi2NegSelectDevice(AVRTARGET_SBBI2_DEVICE)
00187 #define SpiDeselect() Sbbi2NegDeselectDevice(AVRTARGET_SBBI2_DEVICE)
00188 #endif
00189 #define SpiByte Sbbi2Byte
00190
00191 #elif defined(AVRTARGET_SBBI3_DEVICE)
00192
00193 #include <dev/sbbif3.h>
00194 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00195 #define TargetReset(act) Sbbi3ChipReset(AVRTARGET_SBBI3_DEVICE, act)
00196 #else
00197 #define TargetReset(act) Sbbi3ChipReset(AVRTARGET_SBBI3_DEVICE, !act)
00198 #endif
00199 #define SpiSetMode() Sbbi3SetMode(AVRTARGET_SBBI3_DEVICE, AVRTARGET_SPI_MODE)
00200 #define SpiSetSpeed() Sbbi3SetSpeed(AVRTARGET_SBBI3_DEVICE, AVRTARGET_SPI_RATE)
00201 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00202 #define SpiSelect() Sbbi3SelectDevice(AVRTARGET_SBBI3_DEVICE)
00203 #define SpiDeselect() Sbbi3DeselectDevice(AVRTARGET_SBBI3_DEVICE)
00204 #else
00205 #define SpiSelect() Sbbi3NegSelectDevice(AVRTARGET_SBBI3_DEVICE)
00206 #define SpiDeselect() Sbbi3NegDeselectDevice(AVRTARGET_SBBI3_DEVICE)
00207 #endif
00208 #define SpiByte Sbbi3Byte
00209
00210 #else
00211
00212 #define AVRTARGET_NO_SPI_DEVICE
00213
00214 #endif
00215
00224 int AvrTargetInit(void)
00225 {
00226 #if defined(AVRTARGET_NO_SPI_DEVICE)
00227 return -1;
00228 #else
00229 if (SpiSetMode()) {
00230 return -1;
00231 }
00232 SpiSetSpeed();
00233 SpiDeselect();
00234
00235 return 0;
00236 #endif
00237 }
00238
00251 void AvrTargetSelect(ureg_t act)
00252 {
00253 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00254 if (act) {
00255 SpiSelect();
00256 } else {
00257 SpiDeselect();
00258 }
00259 #endif
00260 }
00261
00269 void AvrTargetReset(ureg_t act)
00270 {
00271 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00272 TargetReset(act);
00273 #endif
00274 }
00275
00283 u_long AvrTargetCmd(u_long cmd)
00284 {
00285 u_long rc = 0;
00286 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00287 ureg_t i;
00288
00289 for (i = 0; i < 4; i++) {
00290 rc <<= 8;
00291 rc |= SpiByte((u_char) (cmd >> 24));
00292 cmd <<= 8;
00293 }
00294 #endif
00295 return rc;
00296 }
00297
00303 int AvrTargetProgEnable(void)
00304 {
00305 #if defined(AVRTARGET_NO_SPI_DEVICE)
00306 return -1;
00307 #else
00308 if ((u_char) (AvrTargetCmd(AVRCMD_PROG_ENABLE) >> 8) != 0x53) {
00309 return -1;
00310 }
00311 return 0;
00312 #endif
00313 }
00314
00320 u_long AvrTargetSignature(void)
00321 {
00322 u_long rc = 0;
00323 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00324 ureg_t i;
00325
00326 for (i = 0; i < 3; i++) {
00327 rc <<= 8;
00328 rc |= (u_char) AvrTargetCmd(AVRCMD_READ_SIGNATURE_BYTE | ((u_long) i << 8));
00329 }
00330 #endif
00331 return rc;
00332 }
00333
00339 u_long AvrTargetFusesRead(void)
00340 {
00341 u_long rc = 0;
00342
00343 rc = (u_char) AvrTargetCmd(AVRCMD_READ_FUSE_EXT_BITS);
00344 rc <<= 8;
00345 rc |= (u_char) AvrTargetCmd(AVRCMD_READ_FUSE_HI_BITS);
00346 rc <<= 8;
00347 rc |= (u_char) AvrTargetCmd(AVRCMD_READ_FUSE_BITS);
00348
00349 return rc;
00350 }
00351
00363 u_long AvrTargetFusesWriteSafe(u_long fuses)
00364 {
00365 u_long rc;
00366
00367 fuses |= AVR_FUSES_NEVER_PROG;
00368 fuses &= ~AVR_FUSES_ALWAYS_PROG;
00369 rc = fuses;
00370
00371 AvrTargetCmd(AVRCMD_WRITE_FUSE_BITS | (u_char)fuses);
00372 fuses >>= 8;
00373 AvrTargetCmd(AVRCMD_WRITE_FUSE_HI_BITS | (u_char)fuses);
00374 fuses >>= 8;
00375 AvrTargetCmd(AVRCMD_WRITE_FUSE_EXT_BITS | (u_char)fuses);
00376 fuses >>= 8;
00377
00378 return rc;
00379 }
00380
00391 int AvrTargetPollReady(u_int tmo)
00392 {
00393 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00394 while (AvrTargetCmd(AVRCMD_POLL_READY) & 1UL) {
00395 if (tmo-- == 0) {
00396 return -1;
00397 }
00398 NutSleep(1);
00399
00400 SpiSelect();
00401 }
00402 #endif
00403 return 0;
00404 }
00405
00414 int AvrTargetChipErase(void)
00415 {
00416 AvrTargetCmd(AVRCMD_CHIP_ERASE);
00417
00418 return AvrTargetPollReady(AVRTARGET_CHIPERASE_TIMEOUT);
00419 }
00420
00428 void AvrTargetPageLoad(u_long page, CONST u_char * data)
00429 {
00430 ureg_t bc;
00431
00432 page <<= 6;
00433 for (bc = 0; bc < (AVRTARGET_PAGESIZE / 2); bc++) {
00434 AvrTargetCmd(AVRCMD_LOAD_PROG_MEM_PAGE_LO | (page << 8) | *data);
00435 data++;
00436 AvrTargetCmd(AVRCMD_LOAD_PROG_MEM_PAGE_HI | (page << 8) | *data);
00437 data++;
00438 page++;
00439 }
00440 }
00441
00452 int AvrTargetPageWrite(u_long page)
00453 {
00454 AvrTargetCmd(AVRCMD_WRITE_PROG_MEM_PAGE | (page << 14));
00455
00456 return AvrTargetPollReady(AVRTARGET_PAGEWRITE_TIMEOUT);
00457 }
00458
00468 int AvrTargetPageVerify(u_long page, CONST u_char * data)
00469 {
00470 u_long waddr = page << 6;
00471 ureg_t bc;
00472
00473 for (bc = 0; bc < (AVRTARGET_PAGESIZE / 2); bc++) {
00474 if ((u_char) AvrTargetCmd(AVRCMD_READ_PROG_MEM_LO | (waddr << 8)) != *data++) {
00475 return -1;
00476 }
00477 if ((u_char) AvrTargetCmd(AVRCMD_READ_PROG_MEM_HI | (waddr << 8)) != *data++) {
00478 return -1;
00479 }
00480 waddr++;
00481 }
00482 return 0;
00483 }
00484