Main Page | Modules | Related Pages | Examples

bootloader.c

Example how to use bootloader. Blinking LED #0. See the Makefile as well.
/*
 * Copyright (C) 2004 BLADOX, s.r.o.  All rights reserved.
 * 
 * This file is part of an example program for Turbo. This example
 * program may be used, distributed and modified without limitation.
 *
 */

#include <avr/io.h>
#include <avr/interrupt.h>

// structure for storing actual values written to external I/O regs
// which are mapped to the external data memory
// PORT0 - USB
// PORT1 - LED diodes
// PORT2 - ENABLES0 (Enable signals)
// PORT3 - ENABLES1 (Enable signals)

unsigned char ext_regs[4];

#define EXTERNAL_ADDR_START 0x1100
#define RAMn_PORT_PORT PORTF
#define RAMn_PORT_PORT_PIN PORT3

unsigned char ext_inb (unsigned char port)
{
  unsigned char *p;
  unsigned char old_ramn_port;
  unsigned char retVal;

  old_ramn_port = (inb (RAMn_PORT_PORT) & (1 << RAMn_PORT_PORT_PIN));
  sbi (RAMn_PORT_PORT, RAMn_PORT_PORT_PIN);
  p = (unsigned char *) (EXTERNAL_ADDR_START + port);
  retVal = *p;
  if (old_ramn_port == 0)
    cbi (RAMn_PORT_PORT, RAMn_PORT_PORT_PIN);
}

void ext_outb (unsigned char port, unsigned char value)
{
  unsigned char *p;
  unsigned char interrupts;
  unsigned char old_ramn_port;

  old_ramn_port = (inb (RAMn_PORT_PORT) & (1 << RAMn_PORT_PORT_PIN));
  sbi (RAMn_PORT_PORT, RAMn_PORT_PORT_PIN);
  p = (unsigned char *) (EXTERNAL_ADDR_START + port);
  *p = value;
  ext_regs[port] = value;
  if (old_ramn_port == 0)
    cbi (RAMn_PORT_PORT, RAMn_PORT_PORT_PIN);
}

void ext_sbi (unsigned char port, unsigned char line)
{
  unsigned char newVal;

  newVal = ext_regs[port] | (1 << line);
  ext_outb (port, newVal);
}

void ext_cbi (unsigned char port, unsigned char line)
{
  unsigned char newVal;

  newVal = ext_regs[port] & (~(1 << line));
  ext_outb (port, newVal);
}

void delayOneUs (void)
{
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
  __asm__ __volatile__ ("nop");
}

void delayUs (unsigned short us)
{
  unsigned short counter;

  for (counter = 0; counter < us; counter++)
    delayOneUs ();
}

void delayOneMs (void)
{
  unsigned short counter;

  for (counter = 0; counter < 1000; counter++)
    delayOneUs ();
}

void delayMs (unsigned short ms)
{
  unsigned short counter;

  for (counter = 0; counter < ms; counter++)
    delayOneMs ();
}

int main ()
{
#define LED_WRITE_PORT 1

  while (1)
  {
    ext_outb (LED_WRITE_PORT, 0xFF);    //off
    delayMs (100);
    ext_cbi (LED_WRITE_PORT, 0);
    delayMs (100);
  }
}

void __attribute__ ((section ("tprog_reset_pos")))
  __attribute__ ((naked)) tprog_reset ()
{
  __asm__ __volatile__ ("jmp 0x00");
}


Copyright © 2004 BLADOX
Turbo Programmer version 2.0