Go to the documentation of this file.00001
00054 #include <sys/timer.h>
00055 #include <dev/avrtarget.h>
00056
00061
00062 #ifndef AVRTARGET_CHIPERASE_TIMEOUT
00063
00066 #define AVRTARGET_CHIPERASE_TIMEOUT 200
00067 #endif
00068
00069 #ifndef AVRTARGET_PAGEWRITE_TIMEOUT
00070
00073 #define AVRTARGET_PAGEWRITE_TIMEOUT 10
00074 #endif
00075
00093 #define AVR_FUSES_NEVER_PROG 0x00F88062UL
00094
00112 #define AVR_FUSES_ALWAYS_PROG 0x0000201DUL
00113
00114 #ifndef AVRTARGET_SPI_MODE
00115 #define AVRTARGET_SPI_MODE 0
00116 #endif
00117
00118 #ifndef AVRTARGET_SPI_RATE
00119 #define AVRTARGET_SPI_RATE 500000
00120 #endif
00121
00122 #if defined(AVRTARGET_SPI0_DEVICE)
00123
00124 #include <dev/sppif0.h>
00125 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00126 #define TargetReset(act) Sppi0ChipReset(AVRTARGET_SPI0_DEVICE, act)
00127 #else
00128 #define TargetReset(act) Sppi0ChipReset(AVRTARGET_SPI0_DEVICE, !act)
00129 #endif
00130 #define SpiSetMode() Sppi0SetMode(AVRTARGET_SPI0_DEVICE, AVRTARGET_SPI_MODE)
00131 #define SpiSetSpeed() Sppi0SetSpeed(AVRTARGET_SPI0_DEVICE, AVRTARGET_SPI_RATE)
00132 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00133 #define SpiSelect() Sppi0SelectDevice(AVRTARGET_SPI0_DEVICE)
00134 #define SpiDeselect() Sppi0DeselectDevice(AVRTARGET_SPI0_DEVICE)
00135 #else
00136 #define SpiSelect() Sppi0NegSelectDevice(AVRTARGET_SPI0_DEVICE)
00137 #define SpiDeselect() Sppi0NegDeselectDevice(AVRTARGET_SPI0_DEVICE)
00138 #endif
00139 #define SpiByte Sppi0Byte
00140
00141 #elif defined(AVRTARGET_SBBI0_DEVICE)
00142
00143 #include <dev/sbbif0.h>
00144 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00145 #define TargetReset(act) Sbbi0ChipReset(AVRTARGET_SBBI0_DEVICE, act)
00146 #else
00147 #define TargetReset(act) Sbbi0ChipReset(AVRTARGET_SBBI0_DEVICE, !act)
00148 #endif
00149 #define SpiSetMode() Sbbi0SetMode(AVRTARGET_SBBI0_DEVICE, AVRTARGET_SPI_MODE)
00150 #define SpiSetSpeed() Sbbi0SetSpeed(AVRTARGET_SBBI0_DEVICE, AVRTARGET_SPI_RATE)
00151 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00152 #define SpiSelect() Sbbi0SelectDevice(AVRTARGET_SBBI0_DEVICE)
00153 #define SpiDeselect() Sbbi0DeselectDevice(AVRTARGET_SBBI0_DEVICE)
00154 #else
00155 #define SpiSelect() Sbbi0NegSelectDevice(AVRTARGET_SBBI0_DEVICE)
00156 #define SpiDeselect() Sbbi0NegDeselectDevice(AVRTARGET_SBBI0_DEVICE)
00157 #endif
00158 #define SpiByte Sbbi0Byte
00159
00160 #elif defined(AVRTARGET_SBBI1_DEVICE)
00161
00162 #include <dev/sbbif1.h>
00163 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00164 #define TargetReset(act) Sbbi1ChipReset(AVRTARGET_SBBI1_DEVICE, act)
00165 #else
00166 #define TargetReset(act) Sbbi1ChipReset(AVRTARGET_SBBI1_DEVICE, !act)
00167 #endif
00168 #define SpiSetMode() Sbbi1SetMode(AVRTARGET_SBBI1_DEVICE, AVRTARGET_SPI_MODE)
00169 #define SpiSetSpeed() Sbbi1SetSpeed(AVRTARGET_SBBI1_DEVICE, AVRTARGET_SPI_RATE)
00170 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00171 #define SpiSelect() Sbbi1SelectDevice(AVRTARGET_SBBI1_DEVICE)
00172 #define SpiDeselect() Sbbi1DeselectDevice(AVRTARGET_SBBI1_DEVICE)
00173 #else
00174 #define SpiSelect() Sbbi1NegSelectDevice(AVRTARGET_SBBI1_DEVICE)
00175 #define SpiDeselect() Sbbi1NegDeselectDevice(AVRTARGET_SBBI1_DEVICE)
00176 #endif
00177 #define SpiByte Sbbi1Byte
00178
00179 #elif defined(AVRTARGET_SBBI2_DEVICE)
00180
00181 #include <dev/sbbif2.h>
00182 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00183 #define TargetReset(act) Sbbi2ChipReset(AVRTARGET_SBBI2_DEVICE, act)
00184 #else
00185 #define TargetReset(act) Sbbi2ChipReset(AVRTARGET_SBBI2_DEVICE, !act)
00186 #endif
00187 #define SpiSetMode() Sbbi2SetMode(AVRTARGET_SBBI2_DEVICE, AVRTARGET_SPI_MODE)
00188 #define SpiSetSpeed() Sbbi2SetSpeed(AVRTARGET_SBBI2_DEVICE, AVRTARGET_SPI_RATE)
00189 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00190 #define SpiSelect() Sbbi2SelectDevice(AVRTARGET_SBBI2_DEVICE)
00191 #define SpiDeselect() Sbbi2DeselectDevice(AVRTARGET_SBBI2_DEVICE)
00192 #else
00193 #define SpiSelect() Sbbi2NegSelectDevice(AVRTARGET_SBBI2_DEVICE)
00194 #define SpiDeselect() Sbbi2NegDeselectDevice(AVRTARGET_SBBI2_DEVICE)
00195 #endif
00196 #define SpiByte Sbbi2Byte
00197
00198 #elif defined(AVRTARGET_SBBI3_DEVICE)
00199
00200 #include <dev/sbbif3.h>
00201 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00202 #define TargetReset(act) Sbbi3ChipReset(AVRTARGET_SBBI3_DEVICE, act)
00203 #else
00204 #define TargetReset(act) Sbbi3ChipReset(AVRTARGET_SBBI3_DEVICE, !act)
00205 #endif
00206 #define SpiSetMode() Sbbi3SetMode(AVRTARGET_SBBI3_DEVICE, AVRTARGET_SPI_MODE)
00207 #define SpiSetSpeed() Sbbi3SetSpeed(AVRTARGET_SBBI3_DEVICE, AVRTARGET_SPI_RATE)
00208 #if defined(AVRTARGET_SELECT_ACTIVE_HIGH)
00209 #define SpiSelect() Sbbi3SelectDevice(AVRTARGET_SBBI3_DEVICE)
00210 #define SpiDeselect() Sbbi3DeselectDevice(AVRTARGET_SBBI3_DEVICE)
00211 #else
00212 #define SpiSelect() Sbbi3NegSelectDevice(AVRTARGET_SBBI3_DEVICE)
00213 #define SpiDeselect() Sbbi3NegDeselectDevice(AVRTARGET_SBBI3_DEVICE)
00214 #endif
00215 #define SpiByte Sbbi3Byte
00216
00217 #else
00218
00219 #define AVRTARGET_NO_SPI_DEVICE
00220
00221 #endif
00222
00231 int AvrTargetInit(void)
00232 {
00233 #if defined(AVRTARGET_NO_SPI_DEVICE)
00234 return -1;
00235 #else
00236 if (SpiSetMode()) {
00237 return -1;
00238 }
00239 SpiSetSpeed();
00240 SpiDeselect();
00241
00242 return 0;
00243 #endif
00244 }
00245
00258 void AvrTargetSelect(ureg_t act)
00259 {
00260 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00261 if (act) {
00262 SpiSelect();
00263 } else {
00264 SpiDeselect();
00265 }
00266 #endif
00267 }
00268
00276 void AvrTargetReset(ureg_t act)
00277 {
00278 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00279 TargetReset(act);
00280 #endif
00281 }
00282
00290 uint32_t AvrTargetCmd(uint32_t cmd)
00291 {
00292 uint32_t rc = 0;
00293 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00294 ureg_t i;
00295
00296 for (i = 0; i < 4; i++) {
00297 rc <<= 8;
00298 rc |= SpiByte((uint8_t) (cmd >> 24));
00299 cmd <<= 8;
00300 }
00301 #endif
00302 return rc;
00303 }
00304
00310 int AvrTargetProgEnable(void)
00311 {
00312 #if defined(AVRTARGET_NO_SPI_DEVICE)
00313 return -1;
00314 #else
00315 if ((uint8_t) (AvrTargetCmd(AVRCMD_PROG_ENABLE) >> 8) != 0x53) {
00316 return -1;
00317 }
00318 return 0;
00319 #endif
00320 }
00321
00327 uint32_t AvrTargetSignature(void)
00328 {
00329 uint32_t rc = 0;
00330 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00331 ureg_t i;
00332
00333 for (i = 0; i < 3; i++) {
00334 rc <<= 8;
00335 rc |= (uint8_t) AvrTargetCmd(AVRCMD_READ_SIGNATURE_BYTE | ((uint32_t) i << 8));
00336 }
00337 #endif
00338 return rc;
00339 }
00340
00346 uint32_t AvrTargetFusesRead(void)
00347 {
00348 uint32_t rc = 0;
00349
00350 rc = (uint8_t) AvrTargetCmd(AVRCMD_READ_FUSE_EXT_BITS);
00351 rc <<= 8;
00352 rc |= (uint8_t) AvrTargetCmd(AVRCMD_READ_FUSE_HI_BITS);
00353 rc <<= 8;
00354 rc |= (uint8_t) AvrTargetCmd(AVRCMD_READ_FUSE_BITS);
00355
00356 return rc;
00357 }
00358
00370 uint32_t AvrTargetFusesWriteSafe(uint32_t fuses)
00371 {
00372 uint32_t rc;
00373
00374 fuses |= AVR_FUSES_NEVER_PROG;
00375 fuses &= ~AVR_FUSES_ALWAYS_PROG;
00376 rc = fuses;
00377
00378 AvrTargetCmd(AVRCMD_WRITE_FUSE_BITS | (uint8_t)fuses);
00379 fuses >>= 8;
00380 AvrTargetCmd(AVRCMD_WRITE_FUSE_HI_BITS | (uint8_t)fuses);
00381 fuses >>= 8;
00382 AvrTargetCmd(AVRCMD_WRITE_FUSE_EXT_BITS | (uint8_t)fuses);
00383 fuses >>= 8;
00384
00385 return rc;
00386 }
00387
00398 int AvrTargetPollReady(unsigned int tmo)
00399 {
00400 #if !defined(AVRTARGET_NO_SPI_DEVICE)
00401 while (AvrTargetCmd(AVRCMD_POLL_READY) & 1UL) {
00402 if (tmo-- == 0) {
00403 return -1;
00404 }
00405 NutSleep(1);
00406
00407 SpiSelect();
00408 }
00409 #endif
00410 return 0;
00411 }
00412
00421 int AvrTargetChipErase(void)
00422 {
00423 AvrTargetCmd(AVRCMD_CHIP_ERASE);
00424
00425 return AvrTargetPollReady(AVRTARGET_CHIPERASE_TIMEOUT);
00426 }
00427
00435 void AvrTargetPageLoad(uint32_t page, CONST uint8_t * data)
00436 {
00437 ureg_t bc;
00438
00439 page <<= 6;
00440 for (bc = 0; bc < (AVRTARGET_PAGESIZE / 2); bc++) {
00441 AvrTargetCmd(AVRCMD_LOAD_PROG_MEM_PAGE_LO | (page << 8) | *data);
00442 data++;
00443 AvrTargetCmd(AVRCMD_LOAD_PROG_MEM_PAGE_HI | (page << 8) | *data);
00444 data++;
00445 page++;
00446 }
00447 }
00448
00459 int AvrTargetPageWrite(uint32_t page)
00460 {
00461 AvrTargetCmd(AVRCMD_WRITE_PROG_MEM_PAGE | (page << 14));
00462
00463 return AvrTargetPollReady(AVRTARGET_PAGEWRITE_TIMEOUT);
00464 }
00465
00475 int AvrTargetPageVerify(uint32_t page, CONST uint8_t * data)
00476 {
00477 uint32_t waddr = page << 6;
00478 ureg_t bc;
00479
00480 for (bc = 0; bc < (AVRTARGET_PAGESIZE / 2); bc++) {
00481 if ((uint8_t) AvrTargetCmd(AVRCMD_READ_PROG_MEM_LO | (waddr << 8)) != *data++) {
00482 return -1;
00483 }
00484 if ((uint8_t) AvrTargetCmd(AVRCMD_READ_PROG_MEM_HI | (waddr << 8)) != *data++) {
00485 return -1;
00486 }
00487 waddr++;
00488 }
00489 return 0;
00490 }
00491