00001
00050 #include <sys/timer.h>
00051 #include <dev/avrtarget.h>
00052
00057
00058 #ifndef AVRTARGET_CHIPERASE_TIMEOUT
00059
00062 #define AVRTARGET_CHIPERASE_TIMEOUT 200
00063 #endif
00064
00065 #ifndef AVRTARGET_PAGEWRITE_TIMEOUT
00066
00069 #define AVRTARGET_PAGEWRITE_TIMEOUT 10
00070 #endif
00071
00089 #define AVR_FUSES_NEVER_PROG 0x00F88062UL
00090
00108 #define AVR_FUSES_ALWAYS_PROG 0x0000201DUL
00109
00110 #ifndef AVRTARGET_SPI_MODE
00111 #define AVRTARGET_SPI_MODE 0
00112 #endif
00113
00114 #ifndef AVRTARGET_SPI_RATE
00115 #define AVRTARGET_SPI_RATE 500000
00116 #endif
00117
00118 #if defined(AVRTARGET_SPI0_DEVICE)
00119
00120 #include <dev/sppif0.h>
00121 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00122 #define TargetReset(act) Sppi0ChipReset(AVRTARGET_SPI0_DEVICE, act)
00123 #else
00124 #define TargetReset(act) Sppi0ChipReset(AVRTARGET_SPI0_DEVICE, !act)
00125 #endif
00126 #define SpiSetMode() Sppi0SetMode(AVRTARGET_SPI0_DEVICE, AVRTARGET_SPI_MODE)
00127 #define SpiSetSpeed() Sppi0SetSpeed(AVRTARGET_SPI0_DEVICE, AVRTARGET_SPI_RATE)
00128 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00129 #define SpiSelect() Sppi0SelectDevice(AVRTARGET_SPI0_DEVICE)
00130 #define SpiDeselect() Sppi0DeselectDevice(AVRTARGET_SPI0_DEVICE)
00131 #else
00132 #define SpiSelect() Sppi0NegSelectDevice(AVRTARGET_SPI0_DEVICE)
00133 #define SpiDeselect() Sppi0NegDeselectDevice(AVRTARGET_SPI0_DEVICE)
00134 #endif
00135 #define SpiByte Sppi0Byte
00136
00137 #elif defined(AVRTARGET_SBBI0_DEVICE)
00138
00139 #include <dev/sbbif0.h>
00140 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00141 #define TargetReset(act) Sbbi0ChipReset(AVRTARGET_SBBI0_DEVICE, act)
00142 #else
00143 #define TargetReset(act) Sbbi0ChipReset(AVRTARGET_SBBI0_DEVICE, !act)
00144 #endif
00145 #define SpiSetMode() Sbbi0SetMode(AVRTARGET_SBBI0_DEVICE, AVRTARGET_SPI_MODE)
00146 #define SpiSetSpeed() Sbbi0SetSpeed(AVRTARGET_SBBI0_DEVICE, AVRTARGET_SPI_RATE)
00147 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00148 #define SpiSelect() Sbbi0SelectDevice(AVRTARGET_SBBI0_DEVICE)
00149 #define SpiDeselect() Sbbi0DeselectDevice(AVRTARGET_SBBI0_DEVICE)
00150 #else
00151 #define SpiSelect() Sbbi0NegSelectDevice(AVRTARGET_SBBI0_DEVICE)
00152 #define SpiDeselect() Sbbi0NegDeselectDevice(AVRTARGET_SBBI0_DEVICE)
00153 #endif
00154 #define SpiByte Sbbi0Byte
00155
00156 #elif defined(AVRTARGET_SBBI1_DEVICE)
00157
00158 #include <dev/sbbif1.h>
00159 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00160 #define TargetReset(act) Sbbi1ChipReset(AVRTARGET_SBBI1_DEVICE, act)
00161 #else
00162 #define TargetReset(act) Sbbi1ChipReset(AVRTARGET_SBBI1_DEVICE, !act)
00163 #endif
00164 #define SpiSetMode() Sbbi1SetMode(AVRTARGET_SBBI1_DEVICE, AVRTARGET_SPI_MODE)
00165 #define SpiSetSpeed() Sbbi1SetSpeed(AVRTARGET_SBBI1_DEVICE, AVRTARGET_SPI_RATE)
00166 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00167 #define SpiSelect() Sbbi1SelectDevice(AVRTARGET_SBBI1_DEVICE)
00168 #define SpiDeselect() Sbbi1DeselectDevice(AVRTARGET_SBBI1_DEVICE)
00169 #else
00170 #define SpiSelect() Sbbi1NegSelectDevice(AVRTARGET_SBBI1_DEVICE)
00171 #define SpiDeselect() Sbbi1NegDeselectDevice(AVRTARGET_SBBI1_DEVICE)
00172 #endif
00173 #define SpiByte Sbbi1Byte
00174
00175 #elif defined(AVRTARGET_SBBI2_DEVICE)
00176
00177 #include <dev/sbbif2.h>
00178 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00179 #define TargetReset(act) Sbbi2ChipReset(AVRTARGET_SBBI2_DEVICE, act)
00180 #else
00181 #define TargetReset(act) Sbbi2ChipReset(AVRTARGET_SBBI2_DEVICE, !act)
00182 #endif
00183 #define SpiSetMode() Sbbi2SetMode(AVRTARGET_SBBI2_DEVICE, AVRTARGET_SPI_MODE)
00184 #define SpiSetSpeed() Sbbi2SetSpeed(AVRTARGET_SBBI2_DEVICE, AVRTARGET_SPI_RATE)
00185 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00186 #define SpiSelect() Sbbi2SelectDevice(AVRTARGET_SBBI2_DEVICE)
00187 #define SpiDeselect() Sbbi2DeselectDevice(AVRTARGET_SBBI2_DEVICE)
00188 #else
00189 #define SpiSelect() Sbbi2NegSelectDevice(AVRTARGET_SBBI2_DEVICE)
00190 #define SpiDeselect() Sbbi2NegDeselectDevice(AVRTARGET_SBBI2_DEVICE)
00191 #endif
00192 #define SpiByte Sbbi2Byte
00193
00194 #elif defined(AVRTARGET_SBBI3_DEVICE)
00195
00196 #include <dev/sbbif3.h>
00197 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00198 #define TargetReset(act) Sbbi3ChipReset(AVRTARGET_SBBI3_DEVICE, act)
00199 #else
00200 #define TargetReset(act) Sbbi3ChipReset(AVRTARGET_SBBI3_DEVICE, !act)
00201 #endif
00202 #define SpiSetMode() Sbbi3SetMode(AVRTARGET_SBBI3_DEVICE, AVRTARGET_SPI_MODE)
00203 #define SpiSetSpeed() Sbbi3SetSpeed(AVRTARGET_SBBI3_DEVICE, AVRTARGET_SPI_RATE)
00204 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00205 #define SpiSelect() Sbbi3SelectDevice(AVRTARGET_SBBI3_DEVICE)
00206 #define SpiDeselect() Sbbi3DeselectDevice(AVRTARGET_SBBI3_DEVICE)
00207 #else
00208 #define SpiSelect() Sbbi3NegSelectDevice(AVRTARGET_SBBI3_DEVICE)
00209 #define SpiDeselect() Sbbi3NegDeselectDevice(AVRTARGET_SBBI3_DEVICE)
00210 #endif
00211 #define SpiByte Sbbi3Byte
00212
00213 #else
00214
00215 #define AVRTARGET_NO_SPI_DEVICE
00216
00217 #endif
00218
00227 int AvrTargetInit(void)
00228 {
00229 #if defined(AVRTARGET_NO_SPI_DEVICE)
00230 return -1;
00231 #else
00232 if (SpiSetMode()) {
00233 return -1;
00234 }
00235 SpiSetSpeed();
00236 SpiDeselect();
00237
00238 return 0;
00239 #endif
00240 }
00241
00254 void AvrTargetSelect(ureg_t act)
00255 {
00256 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00257 if (act) {
00258 SpiSelect();
00259 } else {
00260 SpiDeselect();
00261 }
00262 #endif
00263 }
00264
00272 void AvrTargetReset(ureg_t act)
00273 {
00274 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00275 TargetReset(act);
00276 #endif
00277 }
00278
00286 uint32_t AvrTargetCmd(uint32_t cmd)
00287 {
00288 uint32_t rc = 0;
00289 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00290 ureg_t i;
00291
00292 for (i = 0; i < 4; i++) {
00293 rc <<= 8;
00294 rc |= SpiByte((uint8_t) (cmd >> 24));
00295 cmd <<= 8;
00296 }
00297 #endif
00298 return rc;
00299 }
00300
00306 int AvrTargetProgEnable(void)
00307 {
00308 #if defined(AVRTARGET_NO_SPI_DEVICE)
00309 return -1;
00310 #else
00311 if ((uint8_t) (AvrTargetCmd(AVRCMD_PROG_ENABLE) >> 8) != 0x53) {
00312 return -1;
00313 }
00314 return 0;
00315 #endif
00316 }
00317
00323 uint32_t AvrTargetSignature(void)
00324 {
00325 uint32_t rc = 0;
00326 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00327 ureg_t i;
00328
00329 for (i = 0; i < 3; i++) {
00330 rc <<= 8;
00331 rc |= (uint8_t) AvrTargetCmd(AVRCMD_READ_SIGNATURE_BYTE | ((uint32_t) i << 8));
00332 }
00333 #endif
00334 return rc;
00335 }
00336
00342 uint32_t AvrTargetFusesRead(void)
00343 {
00344 uint32_t rc = 0;
00345
00346 rc = (uint8_t) AvrTargetCmd(AVRCMD_READ_FUSE_EXT_BITS);
00347 rc <<= 8;
00348 rc |= (uint8_t) AvrTargetCmd(AVRCMD_READ_FUSE_HI_BITS);
00349 rc <<= 8;
00350 rc |= (uint8_t) AvrTargetCmd(AVRCMD_READ_FUSE_BITS);
00351
00352 return rc;
00353 }
00354
00366 uint32_t AvrTargetFusesWriteSafe(uint32_t fuses)
00367 {
00368 uint32_t rc;
00369
00370 fuses |= AVR_FUSES_NEVER_PROG;
00371 fuses &= ~AVR_FUSES_ALWAYS_PROG;
00372 rc = fuses;
00373
00374 AvrTargetCmd(AVRCMD_WRITE_FUSE_BITS | (uint8_t)fuses);
00375 fuses >>= 8;
00376 AvrTargetCmd(AVRCMD_WRITE_FUSE_HI_BITS | (uint8_t)fuses);
00377 fuses >>= 8;
00378 AvrTargetCmd(AVRCMD_WRITE_FUSE_EXT_BITS | (uint8_t)fuses);
00379 fuses >>= 8;
00380
00381 return rc;
00382 }
00383
00394 int AvrTargetPollReady(u_int tmo)
00395 {
00396 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00397 while (AvrTargetCmd(AVRCMD_POLL_READY) & 1UL) {
00398 if (tmo-- == 0) {
00399 return -1;
00400 }
00401 NutSleep(1);
00402
00403 SpiSelect();
00404 }
00405 #endif
00406 return 0;
00407 }
00408
00417 int AvrTargetChipErase(void)
00418 {
00419 AvrTargetCmd(AVRCMD_CHIP_ERASE);
00420
00421 return AvrTargetPollReady(AVRTARGET_CHIPERASE_TIMEOUT);
00422 }
00423
00431 void AvrTargetPageLoad(uint32_t page, CONST uint8_t * data)
00432 {
00433 ureg_t bc;
00434
00435 page <<= 6;
00436 for (bc = 0; bc < (AVRTARGET_PAGESIZE / 2); bc++) {
00437 AvrTargetCmd(AVRCMD_LOAD_PROG_MEM_PAGE_LO | (page << 8) | *data);
00438 data++;
00439 AvrTargetCmd(AVRCMD_LOAD_PROG_MEM_PAGE_HI | (page << 8) | *data);
00440 data++;
00441 page++;
00442 }
00443 }
00444
00455 int AvrTargetPageWrite(uint32_t page)
00456 {
00457 AvrTargetCmd(AVRCMD_WRITE_PROG_MEM_PAGE | (page << 14));
00458
00459 return AvrTargetPollReady(AVRTARGET_PAGEWRITE_TIMEOUT);
00460 }
00461
00471 int AvrTargetPageVerify(uint32_t page, CONST uint8_t * data)
00472 {
00473 uint32_t waddr = page << 6;
00474 ureg_t bc;
00475
00476 for (bc = 0; bc < (AVRTARGET_PAGESIZE / 2); bc++) {
00477 if ((uint8_t) AvrTargetCmd(AVRCMD_READ_PROG_MEM_LO | (waddr << 8)) != *data++) {
00478 return -1;
00479 }
00480 if ((uint8_t) AvrTargetCmd(AVRCMD_READ_PROG_MEM_HI | (waddr << 8)) != *data++) {
00481 return -1;
00482 }
00483 waddr++;
00484 }
00485 return 0;
00486 }
00487