في عندي هادا السورس فايل
/*
$Id: p2051.c,v 1.1.1.1 2001/04/30 12:56:15 daniele Exp $
Copyright (C) 2001 Daniele Nicolodi <
daniele@grinta.net>
Released under the terms of the GNU General Public License
AT89C2051 Programmer
*/
#include <stdlib.h>
#include <stdio.h>
#include "asm-io.h"
/* Parallel port address */
#define BASE_ADDRESS 0x378
#define DATA (BASE_ADDRESS)
#define STATUS (BASE_ADDRESS + 1)
#define CONTROL (BASE_ADDRESS + 2)
#define MEM_SIZE 2048
/* STATUS port pins */
#define PROG 0x08
#define XTAL 0x10
#define BCLK 0x20
/* CONTROL port pins */
#define READY 0x08
/* P3.5 : P3.4. : P3.3 */
#define WRITE_DATA 0x06
#define READ_DATA 0x04
#define WRITE_LOCK_1 0x07
#define WRITE_LOCK_2 0x03
#define CHIP_ERASE 0x01
#define READ_SIGNATURE 0x00
#define RESET 0x00
#define RST_12 0x00
#define RST_H 0x08
#define RST_L 0x18
void erase();
int program(char * data, int size);
void xread(char * data, int size);
int verify(char * data, int size);
int blank(int size);
void signature(char * data);
void lock(char lbyte);
void set_data(char data);
char get_data();
void set_mode(char mode);
void pulse_xtal(int delay);
void pulse_prog(int delay);
int check_ready();
int load_data(char * file, char * data, int size);
int save_data(char * file, char * data, int size);
/* Show progres into programming process */
void progress(int i)
{
printf("\rProgramming ... %2d%% ", i);
fflush(stdout);
}
/* Print a hex dump of the data array */
void hexdump(unsigned char * data, int size)
{
int i;
int j;
printf("\n 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
for (i=0; i < size; i+=16) {
printf("0x%03X ", i);
for (j=0; (j < 16 && i+j < size); j++)
printf("%02X ", data[i+j]);
printf("\n");
}
printf("\n");
}
int main(int argc, char * argv[])
{
int i;
char witch;
char file[30];
char byte;
int size = MEM_SIZE;
unsigned char data[MEM_SIZE];
if (argc < 2) {
fprintf(stderr,
"Usage: p2051 "
"(erase|blank|program|verify|read|signature|lock) "
"[file] [byte]\n");
exit(1);
}
if ((strcmp(argv[1], "erase")) == 0)
witch = 'e';
else if ((strcmp(argv[1], "blank")) == 0)
witch = 'b';
else if ((strcmp(argv[1], "program")) == 0)
witch = 'p';
else if ((strcmp(argv[1], "verify")) == 0)
witch = 'v';
else if ((strcmp(argv[1], "read")) == 0)
witch = 'r';
else if ((strcmp(argv[1], "signature")) == 0)
witch = 's';
else if ((strcmp(argv[1], "lock")) == 0)
witch = 'l';
else {
fprintf(stderr, "Unknown option %s\n", argv[1]);
exit(1);
}
if ((ioperm(BASE_ADDRESS, 3, 1)) != 0) {
fprintf(stderr,
"Error iperm failed on 0x%03X\n", BASE_ADDRESS);
exit(1);
}
switch (witch) {
case 'e':
printf("Erasing ... ");
erase();
printf("done !!\n");
break;
case 'b':
printf("Checking ... ");
if (! (blank(MEM_SIZE))) {
printf("CHIP IS NOT BLANK\n");
exit(1);
}
printf("done !!\n");
break;
case 'p':
strcpy(file, argv[2]);
if (! (size = load_data(file, data, size))) {
fprintf(stderr, "Error reading file %s\n", file);
exit(1);
}
printf("Loaded %d bytes from %s\n", size, file);
hexdump(data, size);
printf("Programming ... ");
if (! (program(data, size))) {
printf("ERROR\n");
exit(1);
}
printf("\rProgramming ... done !!\n");
break;
case 'v':
strcpy(file, argv[2]);
if (! (size = load_data(file, data, size))) {
fprintf(stderr, "Error reading file %s\n", file);
exit(1);
}
printf("Loaded %d bytes from %s\n", size, file);
hexdump(data, size);
printf("Verifing ... ");
if (! (verify(data, size))) {
printf("ERROR\n");
exit(1);
}
printf("done !!\n");
break;
case 'r':
strcpy(file, argv[2]);
printf("Reading ... ");
read(data, size);
printf("done !!\n");
if (! (size = save_data(file, data, size))) {
fprintf(stderr, "Error writing file \n\n", file);
exit(1);
}
break;
case 's':
printf("Reading signature ... ");
signature(data);
printf("done !!\n");
for (i=0; i < 3; i++)
printf("[%d] 0x%02X\n", i, data[i]);
break;
case 'l':
strcpy(file, argv[2]);
byte =
printf("Setting lock ... ");
lock(byte);
printf("done !!\n");
break;
}
ioperm(BASE_ADDRESS, 3, 0);
exit(0);
}
/* Clear the chip memory to all ones */
void erase()
{
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
set_mode(CHIP_ERASE | RST_H);
usleep(10);
set_mode(CHIP_ERASE | RST_12);
usleep(15000);
pulse_prog(10000);
usleep(10);
set_mode(CHIP_ERASE | RST_L);
usleep(15000);
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
}
/* Program the chip with the contents of the specified data array */
int program(char * data, int size)
{
int addr;
int ii;
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
set_mode(WRITE_DATA | RST_H);
usleep(10);
set_mode(WRITE_DATA | RST_12);
usleep(15000);
for (addr = 0; addr < size; addr++) {
set_data(data[addr]);
usleep(10);
pulse_prog(10);
usleep(1200);
ii = 0;
while (! check_ready) {
usleep(10);
ii++;
if (ii > 100) {
return 0;
}
}
pulse_xtal(10);
progress(addr*100/size);
}
set_mode(WRITE_DATA | RST_L);
usleep(15000);
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
return 1;
}
/* Read the contents of the chip into the specified data array */
void xread(char * data, int size)
{
int addr;
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
set_mode(READ_DATA | RST_H);
usleep(10);
for (addr = 0; addr < size; addr++) {
usleep(10);
data[addr] = get_data();
pulse_xtal(10);
}
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
}
/* Compare the contents of the chip to the specified data array */
int verify(char * data, int size)
{
int addr;
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
set_mode(READ_DATA | RST_H);
usleep(10);
for (addr = 0; addr < size; addr++) {
usleep(10);
if (get_data() != data[addr]) {
return 0;
}
pulse_xtal(10);
}
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
return 1;
}
/* Determine if the chip is erased */
int blank(int size)
{
int addr;
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
set_mode(READ_DATA | RST_H);
usleep(10);
for (addr = 0; addr < size; addr++) {
usleep(10);
if (get_data() != (char)0xFF)
return 0;
pulse_xtal(10);
}
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
return 1;
}
/* Read signature bytes into the specified data array.
* (000H) = 1EH indicates manufactured by Atmel
* (001H) = 21H indicates 89C2051
* (002H) = FFH indicates 12 volt programing
*/
void signature(char * data)
{
int addr;
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
set_mode(READ_SIGNATURE | RST_H);
usleep(10);
for (addr = 0; addr < 3; addr++) {
usleep(10);
data[addr] = get_data();
pulse_xtal(10);
}
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
}
/* Write specified lock bit */
void lock(char lbyte)
{
outb(0x00, STATUS);
set_mode(RESET | RST_L);
usleep(10);
set_mode(RESET | RST_H);
outb(PROG, STATUS);
usleep(10);
if (lbyte & 0x01) {
set_mode(WRITE_LOCK_1 | RST_H);
usleep(10);
set_mode(WRITE_LOCK_1 | RST_12);
usleep(15000);
pulse_prog(10);
usleep(10);
set_mode(WRITE_LOCK_1 | RST_L);
usleep(15000);
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
}
if (lbyte & 0x02) {
set_mode(WRITE_LOCK_2 | RST_H);
usleep(10);
set_mode(WRITE_LOCK_2 | RST_12);
usleep(15000);
pulse_prog(10);
usleep(10);
set_mode(WRITE_LOCK_2 | RST_L);
usleep(15000);
set_mode(RESET | RST_L);
outb(0x00, STATUS);
usleep(1000);
}
}
void set_data(char data)
{
outb(data, DATA);
}
char get_data()
{
char data;
outb(0x20, CONTROL);
data = inb(DATA);
outb(0x00, CONTROL);
return data;
}
void set_mode(char mode)
{
char tmp;
tmp = inb(STATUS);
outb(mode, DATA);
outb((tmp | BCLK), STATUS);
usleep(10);
outb(tmp, STATUS);
outb(0x00, DATA);
}
void pulse_xtal(int delay)
{
char tmp;
tmp = inb(STATUS);
outb((tmp ^ XTAL), STATUS);
usleep(delay);
outb(tmp, STATUS);
}
void pulse_prog(int delay)
{
char tmp;
tmp = inb(STATUS);
outb((tmp ^ PROG), STATUS);
usleep(delay);
outb(tmp, STATUS);
}
int check_ready()
{
if ((inb(CONTROL) & READY) == 1)
return 1;
else
return 0;
}
/* Read file into array */
int load_data(char * file, char * data, int size)
{
FILE * fd;
int readed;
if (! (fd = fopen(file, "r")))
return 0;
readed = fread(data, 1, size, fd);
if (ferror(fd))
return 0;
fclose(fd);
return readed;
}
/* Write array into file */
int save_data(char * file, char * data, int size)
{
FILE * fd;
int writed;
if (! (fd = fopen(file, "w")))
return 0;
writed = fwrite(data, 1, size, fd);
if (size != writed)
return 0;
fclose(fd);
return writed;
}
وهادا الهيدر فايل
#ifndef _ASM_IO_H
#define _ASM_IO_H
/*
* This file contains the definitions for the x86 IO instructions
* inb/inw/inl/outb/outw/outl and the "string versions" of the same
* (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
* versions of the single-IO instructions (inb_p/inw_p/..).
*
* This file is not meant to be obfuscating: it's just complicated
* to (a) handle it all in a way that makes gcc able to optimize it
* as well as possible and (b) trying to avoid writing the same thing
* over and over again with slight variations and possibly making a
* mistake somewhere.
*/
/*
* Thanks to James van Artsdalen for a better timing-fix than
* the two short jumps: using outb's to a nonexistent port seems
* to guarantee better timings even on fast machines.
*
* On the other hand, I'd like to be sure of a non-existent port:
* I feel a bit unsafe about using 0x80 (should be safe, though)
*
* Linus
*/
#ifdef SLOW_IO_BY_JUMPING
#define _SLOW_DOWN_IO _asm_ _volatile_("jmp 1f\n1:\tjmp 1f\n1:")
#else
#define _SLOW_DOWN_IO _asm_ _volatile_("outb %al,$0x80")
#endif
#ifdef REALLY_SLOW_IO
#define SLOW_DOWN_IO { _SLOW_DOWN_IO; _SLOW_DOWN_IO; _SLOW_DOWN_IO; _SLOW_DOWN_IO; }
#else
#define SLOW_DOWN_IO _SLOW_DOWN_IO
#endif
/*
* Change virtual addresses to physical addresses and vv.
* These are trivial on the 1:1 Linux/i386 mapping (but if we ever
* make the kernel segment mapped at 0, we need to do translation
* on the i386 as well)
*/
extern inline unsigned long virt_to_phys(volatile void * address)
{
return (unsigned long) address;
}
extern inline void * phys_to_virt(unsigned long address)
{
return (void *) address;
}
/*
* IO bus memory addresses are also 1:1 with the physical address
*/
#define virt_to_bus virt_to_phys
#define bus_to_virt phys_to_virt
/*
* readX/writeX() are used to access memory mapped devices. On some
* architectures the memory mapped IO stuff needs to be accessed
* differently. On the x86 architecture, we just read/write the
* memory location directly.
*/
#define readb(addr) (*(volatile unsigned char *) (addr))
#define readw(addr) (*(volatile unsigned short *) (addr))
#define readl(addr) (*(volatile unsigned int *) (addr))
#define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
#define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
#define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
#define memset_io(a,b,c) memset((void *)(a),(b),(c))
#define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
#define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
/*
* Again, i386 does not require mem IO specific function.
*/
#define eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(void *)(b),(c),(d))
/*
* Talk about misusing macros..
*/
#define _OUT1(s,x)
extern inline void _out##s(unsigned x value, unsigned short port){
#define _OUT2(s,s1,s2)
_asm_ _volatile_ ("out" #s " %" s1 "0,%" s2 "1");
#define _OUT(s,s1,x)
_OUT1(s,x) _OUT2(s,s1,"w") : : "a" (value), "d" (port)); }
_OUT1(s##c,x) _OUT2(s,s1,"") : : "a" (value), "id" (port)); }
_OUT1(s##_p,x) _OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; }
_OUT1(s##c_p,x) _OUT2(s,s1,"") : : "a" (value), "id" (port)); SLOW_DOWN_IO; }
}
#define _IN1(s)
extern inline RETURN_TYPE _in##s(unsigned short port) { RETURN_TYPE _v;
#define _IN2(s,s1,s2)
_asm_ _volatile_ ("in" #s " %" s2 "1,%" s1 "0"
#define _IN(s,s1,i)
_IN1(s) _IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; }
_IN1(s##c) _IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; }
_IN1(s##_p) _IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; }
_IN1(s##c_p) _IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; }
#define _INS(s)
extern inline void ins##s(unsigned short port, void * addr, unsigned long count)
{ _asm_ _volatile_ ("cld ; rep ; ins" #s
: "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
#define _OUTS(s)
extern inline void outs##s(unsigned short port, const void * addr, unsigned long count)
{ _asm_ _volatile_ ("cld ; rep ; outs" #s
: "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
#define RETURN_TYPE unsigned char
/* _IN(b,"b","0" (0)) */
_IN(b,"")
#undef RETURN_TYPE
#define RETURN_TYPE unsigned short
/* _IN(w,"w","0" (0)) */
_IN(w,"")
#undef RETURN_TYPE
#define RETURN_TYPE unsigned int
_IN(l,"")
#undef RETURN_TYPE
_OUT(b,"b",char)
_OUT(w,"w",short)
_OUT(l,,int)
_INS(b)
_INS(w)
_INS(l)
_OUTS(b)
_OUTS(w)
_OUTS(l)
/*
* Note that due to the way _builtin_constant_p() works, you
* - can't use it inside a inline function (it will never be true)
* - you don't have to worry about side effects within the _builtin..
*/
#define outb(val,port)
((_builtin_constant_p((port)) && (port) < 256) ?
_outbc((val),(port)) :
_outb((val),(port)))
#define inb(port)
((_builtin_constant_p((port)) && (port) < 256) ?
_inbc(port) :
_inb(port))
#define outb_p(val,port)
((_builtin_constant_p((port)) && (port) < 256) ?
_outbc_p((val),(port)) :
_outb_p((val),(port)))
#define inb_p(port)
((_builtin_constant_p((port)) && (port) < 256) ?
_inbc_p(port) :
_inb_p(port))
#define outw(val,port)
((_builtin_constant_p((port)) && (port) < 256) ?
_outwc((val),(port)) :
_outw((val),(port)))
#define inw(port)
((_builtin_constant_p((port)) && (port) < 256) ?
_inwc(port) :
_inw(port))
#define outw_p(val,port)
((_builtin_constant_p((port)) && (port) < 256) ?
_outwc_p((val),(port)) :
_outw_p((val),(port)))
#define inw_p(port)
((_builtin_constant_p((port)) && (port) < 256) ?
_inwc_p(port) :
_inw_p(port))
#define outl(val,port)
((_builtin_constant_p((port)) && (port) < 256) ?
_outlc((val),(port)) :
_outl((val),(port)))
#define inl(port)
((_builtin_constant_p((port)) && (port) < 256) ?
_inlc(port) :
_inl(port))
#define outl_p(val,port)
((_builtin_constant_p((port)) && (port) < 256) ?
_outlc_p((val),(port)) :
_outl_p((val),(port)))
#define inl_p(port)
((_builtin_constant_p((port)) && (port) < 256) ?
_inlc_p(port) :
_inl_p(port))
#endif
وكل ماعم ترجم البرنامج عم يعطيني اخطاء كتير
حليت كتير منها و ما استفدت فاذا حدا بيعرف ارجو انو ينقذني بشي حل
ولكم الشكر