Código:
// PRUEBA del interface de flaspi
// Te va sacando alternativamente las señales y lee el bit de entrada
// Si cambias algo en la definición del cable, recuerda cambiarlo en el otro.
//
// Default is Compile for Linux (both #define's below should be commented out)
// #define WINDOWS_VERSION // descomentar esta linea para compilar en Windows
// #define __FreeBSD__ // uncomment only this for FreeBSD
#ifdef WINDOWS_VERSION
#include <windows.h> // Only for Windows Compile
#define strcasecmp stricmp
#define strncasecmp strnicmp
#define OUT_PORT _outp(p_base, data)
#define IN_PORT data_read = (unsigned char)_inp(p_base + 1)
#endif
#ifndef WINDOWS_VERSION
#include <unistd.h>
#include <sys/ioctl.h>
#define OUT_PORT ioctl(pfd, PPWDATA, &data)
#define IN_PORT ioctl(pfd, PPRSTATUS, &data_read)
#ifdef __FreeBSD__
#include <dev/ppbus/ppi.h>
#include <dev/ppbus/ppbconf.h>
#define PPWDATA PPISDATA
#define PPRSTATUS PPIGSTATUS
#else
#include <linux/ppdev.h>
#endif
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
/*
* The followint table shows the pin assignment of 25-pin Parallel Printer Port.
* please refer to IEEE 1284 standard for detailed description.
* data port (Out) (0x378) status port (In) (0x379)
* bit[7] -- pin9 (Out) bit[7] -- pin11 (In), busy (Hardware Inverted)
* bit[6] -- pin8 (Out) bit[6] -- pin10 (In), Ack
* bit[5] -- pin7 (Out) bit[5] -- pin12 (In), Paper out
* bit[4] -- pin6 (Out) bit[4] -- pin13 (In), Select
* bit[3] -- pin5 (Out) bit[3] -- pin15 (In), Error
* bit[2] -- pin4 (Out) bit[2] -- IRQ(Not)
* bit[1] -- pin3 (Out) bit[1] -- Reserved
* bit[0] -- pin2 (Out) bit[0] -- Reserved
*/
// --- Cable ---
#define SI 2 // Pin4
#define SCK 1 // Pin3
#define CS 0 // Pin2
#define SO 4 // Pin13
int pfd;
int p_base = 0x378;
unsigned int start = 0x00;
unsigned int length = 0x10000;
char filename[] = "FILE";
int bit = 0;
int block_total = 32;
// -----------------------------------------
// ---- Start of Compiler Specific Code ----
// -----------------------------------------
void lpt_openport(void)
{
#ifdef WINDOWS_VERSION // ---- Compiler Specific Code ----
HANDLE h;
h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(h == INVALID_HANDLE_VALUE) { printf("Couldn't access giveio device\n"); CloseHandle(h); exit(0); }
CloseHandle(h);
#else // ---- Compiler Specific Code ----
#ifdef __FreeBSD__ // ---- Compiler Specific Code ----
pfd = open("/dev/ppi0", O_RDWR);
if (pfd < 0) { perror("Failed to open /dev/ppi0"); exit(0); }
if ((ioctl(pfd, PPEXCL) < 0) || (ioctl(pfd, PPCLAIM) < 0)) { perror("Failed to lock /dev/ppi0"); close(pfd); exit(0); }
#else // ---- Compiler Specific Code ----
pfd = open("/dev/parport0", O_RDWR);
if (pfd < 0) { perror("Failed to open /dev/parport0"); exit(0); }
if ((ioctl(pfd, PPEXCL) < 0) || (ioctl(pfd, PPCLAIM) < 0)) { perror("Failed to lock /dev/parport0"); close(pfd); exit(0); }
#endif
#endif
}
void lpt_closeport(void)
{
#ifndef WINDOWS_VERSION // ---- Compiler Specific Code ----
#ifndef __FreeBSD__ // ---- Compiler Specific Code ----
if (ioctl(pfd, PPRELEASE) < 0) { perror("Failed to release /dev/parport0"); close(pfd); exit(0); }
#endif
close(pfd);
#endif
}
// ---------------------------------------
// ---- End of Compiler Specific Code ----
// ---------------------------------------
int main()
{
unsigned char data, data_read;
lpt_openport();
while (1)
{ data = (0 << SCK) | (1 << CS) | (0 << SI); OUT_PORT;
IN_PORT; data_read >>=SO; data_read &= 1;
printf("CS => 1 * SCK => 0 * SI => 0 * SO <= %01x\n",data_read);
usleep(1500000);
data = (1 << SCK) | (0 << CS) | (0 << SI); OUT_PORT;
IN_PORT; data_read >>=SO; data_read &= 1;
printf("CS => 0 * SCK => 1 * SI => 0 * SO <= %01x\n",data_read);
usleep(1500000);
data = (0 << SCK) | (0 << CS) | (1 << SI); OUT_PORT;
IN_PORT; data_read >>=SO; data_read &= 1;
printf("CS => 0 * SCK => 0 * SI => 1 * SO <= %01x\n",data_read);
usleep(1500000);
}
}