Main Page | Modules | Related Pages | Examples

avrprog/module-src/avrprog.c

/*
 * Turbo Programmer Utilities, turbo-prog-utils, www.bladox.com 
 *
 * Copyright (C) 2004 BLADOX, s.r.o.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <config.h>
#include <tprog/tprog.h>
#include <stdlib.h>
#include <avr/interrupt.h>

#include "avrprog.h"
#include "isp.h"

void init_avr_prog_port ()
{
  led_on (LED_PRG);

  // enable SPI, set SPI MASTER mode
  // CLKosc/128 SPI clock (with SPI2X /64)
  outb (SPCR, 0x53);

  // Disable double SPI clock
  cbi (SPSR, SPI2X);

  //_DEBUG_SS_MCU change direction to output 
  sbi (DEBUG_SS_MCU_DDR, DEBUG_SS_MCU_DDR_DD);

  // RESETn pin stays output and set it to log.0
  cbi (PROG_RESETn_PORT, PROG_RESETn_PORT_PIN);
  sbi (PROG_RESETn_DDR, PROG_RESETn_DDR_DD);

  //PROG_DEBUG_RESETn_ENABLE  pin set it to log.1
  //(in PROG mode RESETn is connected)
  ext_sbi (EXT_PROG_DEBUG_RESETn_ENABLE_PORT,
           EXT_PROG_DEBUG_RESETn_ENABLE_PORT_PIN);

  delayMs (100);

  //PROG_DEBUG_VCC_ENABLE pin set it to log.0
  //(in PROG mode we must apply power after RESETn=0)
  ext_cbi (EXT_PROG_DEBUG_VCC_ENABLE_PORT,
           EXT_PROG_DEBUG_VCC_ENABLE_PORT_PIN);

  //PROG_DEBUG_CLK_ENABLE  pin set it to log.1
  //(in PROG mode CLK is connected)
  ext_sbi (EXT_PROG_DEBUG_CLK_ENABLE_PORT,
           EXT_PROG_DEBUG_CLK_ENABLE_PORT_PIN);

  delayMs (10);

  // SCK pin will be output and set it to log.0
  cbi (PROG_SCK_PORT, PROG_SCK_PORT_PIN);
  sbi (PROG_SCK_DDR, PROG_SCK_DDR_DD);

  // MOSI pin is output and set it to log.1
  sbi (DEBUG_MOSI_PORT, DEBUG_MOSI_PORT_PIN);
  sbi (DEBUG_MOSI_DDR, DEBUG_MOSI_DDR_DD);

  // MISO is input
  cbi (DEBUG_MISO_DDR, DEBUG_MISO_DDR_DD);

  //PROG_DEBUG_SCK_ENABLE pin set it to log.1
  //(in PROG mode SCK is connected)
  ext_sbi (EXT_PROG_DEBUG_SCK_ENABLE_PORT,
           EXT_PROG_DEBUG_SCK_ENABLE_PORT_PIN);

  //PROG_DEBUG_PDO_ENABLE pin set it to log.1
  //(in PROG mode PDO is connected)
  ext_sbi (EXT_PROG_DEBUG_PDO_ENABLE_PORT,
           EXT_PROG_DEBUG_PDO_ENABLE_PORT_PIN);

  //PROG_DEBUG_PDI_ENABLE pin set it to log.1
  //(in PROG mode PDI is connected)
  ext_sbi (EXT_PROG_DEBUG_PDI_ENABLE_PORT,
           EXT_PROG_DEBUG_PDI_ENABLE_PORT_PIN);

  delayMs (50);

  ispOutReset (0);
  ispOutSck (0);
  delayMs (100);
  ispPulseReset ();
  delayMs (100);
}

void turbo_handler (u8 action, USB_Data * data)
{
  u8 *buf = buf_A ();
  u16 i;

  switch (action)
  {
    case ACTION_APP_VERSION:
      buf[0] = 2;
      buf[1] = 0;
      buf[2] = 0;
      usb_send (NO_ERROR, 3, buf);
      break;
    case ACTION_AVR_PROG_INIT:
      init_avr_prog_port ();
      ispEnableAvr ();
      usb_send (NO_ERROR, 0, NULL);
      break;

    case ACTION_AVR_GET_PART_INFO:
      for (i = 0; i < 3; i++)
        buf[i] = ispGetPartInfo (i);
      usb_send (NO_ERROR, 3, buf);
      break;

    case ACTION_AVR_READ_FUSE_LOW_BYTE:
      buf[0] = ispReadFuseLowBits ();
      usb_send (NO_ERROR, 1, buf);
      break;

    case ACTION_AVR_WRITE_FUSE_LOW_BYTE:
      ispWriteFuseLowBits (data->buf[0]);
      delayMs (12);
      usb_send (NO_ERROR, 0, NULL);
      break;

    case ACTION_AVR_READ_FUSE_HIGH_BYTE:
      buf[0] = ispReadFuseHighBits ();
      usb_send (NO_ERROR, 1, buf);
      break;

    case ACTION_AVR_WRITE_FUSE_HIGH_BYTE:
      ispWriteFuseHighBits (data->buf[0]);
      delayMs (12);
      usb_send (NO_ERROR, 0, NULL);
      break;

    case ACTION_AVR_READ_FUSE_EXTENDED_BYTE:
      buf[0] = ispReadFuseExtBits ();
      usb_send (NO_ERROR, 1, buf);
      break;

    case ACTION_AVR_WRITE_FUSE_EXTENDED_BYTE:
      ispWriteFuseExtBits (data->buf[0]);
      delayMs (12);
      usb_send (NO_ERROR, 0, NULL);
      break;

    case ACTION_AVR_READ_CALIBRATE_BYTE:
      buf[0] = ispReadCalByte (0);
      usb_send (NO_ERROR, 1, buf);
      break;

    case ACTION_AVR_READ_LOCK_BITS:
      buf[0] = ispReadLockBits ();
      usb_send (NO_ERROR, 1, buf);
      break;

    case ACTION_AVR_WRITE_LOCK_BITS:
      ispWriteLockBits (data->buf[0]);
      delayMs (12);
      usb_send (NO_ERROR, 0, NULL);
      break;

    case ACTION_AVR_CHIP_ERASE:
      delayMs (100);
      ispEnableAvr ();
      delayMs (100);
      ispChipErase ();
      delayMs (100);
      ispEnableAvr ();
      usb_send (NO_ERROR, 0, NULL);
      break;

    case ACTION_AVR_READ_CHUNK_EEPROM:
      {
        u32 addr;

        addr = data->buf[0];
        addr <<= 8;
        addr += data->buf[1];
        addr <<= 8;
        addr += data->buf[2];
        addr <<= 8;
        addr += data->buf[3];
        i = data->buf[4];
        ispReadChunkEeprom (addr, buf, i);
        usb_send (NO_ERROR, i, buf);
      }
      break;

    case ACTION_AVR_WRITE_CHUNK_EEPROM:
      {
        u32 addr;

        addr = data->buf[0];
        addr <<= 8;
        addr += data->buf[1];
        addr <<= 8;
        addr += data->buf[2];
        addr <<= 8;
        addr += data->buf[3];
        i = data->buf[4];
        i = ispWriteChunkEeprom (addr, &data->buf[5], i);
        buf[0] = i;
        usb_send (NO_ERROR, 1, buf);
      }
      break;

    case ACTION_AVR_READ_CHUNK_FLASH:
      {
        u32 addr;

        addr = data->buf[0];
        addr <<= 8;
        addr += data->buf[1];
        addr <<= 8;
        addr += data->buf[2];
        addr <<= 8;
        addr += data->buf[3];
        i = data->buf[4];
        ispReadChunkFlash (addr, buf, i);
        usb_send (NO_ERROR, i, buf);
      }
      break;

    case ACTION_AVR_WRITE_CHUNK_FLASH:
      {
        u32 addr;
        u16 i;

        addr = data->buf[0];
        addr <<= 8;
        addr += data->buf[1];
        addr <<= 8;
        addr += data->buf[2];
        addr <<= 8;
        addr += data->buf[3];
        i = data->buf[4];
        i = ispWriteChunkFlash (addr, &data->buf[5], i);
        buf[0] = i;
        usb_send (NO_ERROR, 1, buf);
      }
      break;

    case ACTION_AVR_READ_BYTE_FLASH:
    case ACTION_AVR_WRITE_BYTE_FLASH:
    case ACTION_AVR_READ_BYTE_EEPROM:
    case ACTION_AVR_WRITE_BYTE_EEPROM:
// TODO
      usb_send (NO_ERROR, 1, buf);
      break;
    default:
      break;
  }
}


Copyright © 2004 BLADOX
Turbo Programmer version 2.0