Working on AVR support.
[goodfet] / firmware / apps / avr / avr.c
1 /*! \file avr.c
2   \author Travis Goodspeed
3   \brief AVR SPI Programmer
4 */
5
6 #include "platform.h"
7 #include "command.h"
8
9 #include <signal.h>
10 #include <io.h>
11 #include <iomacros.h>
12
13 #include "avr.h"
14
15 //! Setup the AVR pins.
16 void avrsetup(){
17   spisetup();
18 }
19
20 //! Initialized an attached AVR.
21 void avrconnect(){
22   register int i;
23   avrsetup();//set I/O pins
24   
25   //Pulse !RST (SS) at least twice while CLK is low.
26   CLRSS;
27   CLRCLK;
28   
29   for(i=0;i<5;i++){
30     SETSS;
31     CLRSS;
32   }
33   
34   //Enable programming
35   avr_prgen();
36 }
37
38 //! Perform a 4-byte exchange.
39 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
40   spitrans8(a);
41   spitrans8(b);
42   if(spitrans8(c)!=b){
43     debugstr("AVR sync error, b not returned as c.");
44   }
45   spitrans8(c);
46   return spitrans8(d);
47 }
48
49 //! Enable AVR programming mode.
50 void avr_prgen(){
51   avrexchange(0xac, 0x53, 0, 0);
52 }
53
54 //! Read AVR device code.
55 u8 avr_devicecode(){
56   return avrexchange(0x30, //Read signature byte
57               0x00,
58               0x00, //&0x03 is sig adr
59               0x00 //don't care.
60               );
61 }
62
63 //! Handles an AVR command.
64 void avrhandle(unsigned char app,
65                unsigned char verb,
66                unsigned long len){
67   unsigned long i;
68   
69   
70   switch(verb){
71   case READ:
72   case WRITE:
73     for(i=0;i<len;i++)
74       cmddata[i]=spitrans8(cmddata[i]);
75     txdata(app,verb,len);
76     break;
77   case SETUP:
78     avrsetup();
79     txdata(app,verb,0);
80     break;
81   case START:
82     avrconnect();
83     txdata(app,verb,0);
84     break;
85   case PEEK:
86   case POKE:
87   default:
88     debugstr("Verb unimplemented in AVR application.");
89     txdata(app,NOK,0);
90     break;
91   }
92 }
93