source: avrstuff/EPRoxygen/device.h@ a6ae6ac

main
Last change on this file since a6ae6ac was a6ae6ac, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

Still not working...
Added very agressive timing, fixed some potential glitches, but still not enough to even read the device ID !

git-svn-id: svn://pulkomandy.tk/avrstuff@60 c6672c3c-f6b6-47f9-9001-1fd6b12fecbe

  • Property mode set to 100644
File size: 2.9 KB
Line 
1#ifndef _DEVICE_H_
2#define _DEVICE_H_
3
4#pragma once
5
6#include <errno.h>
7#include <stdio.h>
8#include <stdint.h>
9#include <unistd.h>
10
11// FreeBSD
12#include <sys/types.h>
13#include <machine/cpufunc.h>
14#include <machine/sysarch.h>
15
16
17class Device {
18 public:
19 Device()
20 {};
21
22 void shutdown();
23 virtual void power() = 0;
24
25 virtual void read(const char* file) = 0;
26 virtual void write(const char* file) = 0;
27 virtual void erase(void) = 0;
28
29 int setup(const int port_base) {
30 port = port_base;
31 if (i386_set_ioperm(port_base, 3, true) != 0)
32 {
33 perror("Unable to get access to parallel port");
34 return errno;
35 }
36 shutdown();
37
38 return 0;
39 }
40
41 virtual ~Device() {};
42
43 protected:
44 typedef enum {
45 NONE = 0b0011, /* Nothing selected */
46 NYBLE = 0b0100, /* Read low nyble */
47 /* When 0, status reg shows D0.3
48 * When 1, status reg shows D4.7
49 */
50 CTRL = 0b0101, /* Control reg. */
51 /* b0: A16
52 * b1: /A17 (A17 is reversed !)
53 * b2: WE
54 * b3: A18
55 * b4: OE
56 * b5: VPP on pin 25 (0 = enable)
57 * b6: VPP on pin 24 (0 = enable)
58 * b7: CE
59 */
60 VOLT = 0b0110, /* Power config reg. */
61 /* b0: VPP on pin 1 (0 = enable)
62 * b1: VPP on pin 3 (0 = enable)
63 * b2: VCC on pin 32 (0 = enable)
64 * b3: Data reg. output enable (and green led)
65 * b4: VPP config
66 * b5: VPP config
67 * 00 = 24V
68 * 01 = ??
69 * 10 = ??
70 * 11 = 12V
71 * b6: Address reg. output enable
72 * b7: VCC enable (1 = power ON)
73 */
74 ADR0 = 0b1101, /* Low address byte A0.7 */
75 DATA = 0b1110, /* Data byte */
76 ADR1 = 0b1111, /* High address byte A8.15 */
77 /* A13 is reversed ! (also used as VCC pin in smaller packages)
78 */
79 } Register;
80
81 static const uint8_t A13 = 1 << 5;
82
83 typedef enum {
84 A16 = 1,
85 A17 = 2,
86 WE = 4,
87 A18 = 8,
88 OE = 16,
89 VPP25 = 32,
90 VPP24 = 64,
91 CE = 128
92 } CTRLbits;
93
94 typedef enum {
95 VPP1 = 1,
96 VPP3 = 2,
97 VCC32 = 4,
98 DOE = 8,
99
100 V24 = 0,
101 V12 = 48,
102
103 AOE = 64,
104 VCC30 = 128
105 } VOLTbits;
106
107 static inline void write(const Register reg, const uint8_t val)
108 {
109 // To avoid glitches, first set the bits that select the register,
110 // then enable the output. And disable the output when we are done
111 // while not touching the selected reg. This is the only way to
112 // avoid glitches (another reg may be selected long enough for
113 // losing some bits)
114 outb(port, val);
115 usleep(1000);
116 outb(port + 2, reg ^ 4);
117 usleep(1000);
118 outb(port + 2, reg);
119 usleep(1000);
120 outb(port + 2, reg ^ 4);
121 usleep(1000);
122 }
123
124 static inline uint8_t read()
125 {
126 outb(port+2, NYBLE ^ 4);
127 usleep(1000);
128 uint8_t byte = inb(port + 1) & 0xF0;
129
130 outb(port+2, NYBLE);
131 usleep(1000);
132 byte |= inb(port + 1) >> 4;
133printf("R %X", byte ^ 0x88);
134getchar();
135 outb(port+2, NYBLE ^ 4);
136 usleep(1000);
137 return byte ^ 0x88;
138 }
139
140 static int port;
141 private:
142};
143
144#endif
Note: See TracBrowser for help on using the repository browser.