1 | #define TXE (PORTF & (1 << 6)) // Transmitt-enable pin
|
2 | #define WR_dis PORTF &= ~(1 << 4) // Write disable
|
3 | #define WR_en PORTF |= (1 << 4) // Write enable
|
4 | #define RX (PORTD & (1 << 11)) // Receive data Pin
|
5 | #define RD_dis PORTF |= (1 << 3) // Read disable
|
6 | #define RD_en PORTF &= ~(1 << 3) // Read enable
|
7 | #define PWREN (PORTF & (1 << 5)) // configurations-status
|
8 | #define DATA_bus PORTB
|
9 | #define DDR_in TRISB = 0xFFFF
|
10 | #define DDR_out TRISB = 0
|
11 |
|
12 | static unsigned char index_rxBuf_R = 0; // Index of last write RX-Buffer
|
13 | static unsigned char index_rxBuf_W = 0; // Index of last read RX-Buffer
|
14 | static unsigned char rxBuf = 0; // RX-Buffer
|
15 | static unsigned char index_txBuf_R = 0; // Index to write TX-Buffer
|
16 | static unsigned char index_txBuf_W = 0; // Index to read TX-Buffer
|
17 | static unsigned char txBuf[txBuf_max + 1]; // TX-Buffer
|
18 |
|
19 |
|
20 | void ft245_init(void){
|
21 | char i = 0;
|
22 | unsigned char blink_cnt = 0;
|
23 |
|
24 | RD_dis; // set control-pins
|
25 | WR_dis;
|
26 |
|
27 | for(i=0;i<=txBuf_max;i++){ // clear buffer
|
28 | txBuf[i] = 0;
|
29 | }
|
30 | while(PWREN != 0){ // wait until USB FTDI-configuration
|
31 | if(blink_cnt >= 250){ // Blaue LED mit 2Hz blinken
|
32 | PORTC ^= (1 << 1);
|
33 | blink_cnt = 0;
|
34 | }
|
35 | blink_cnt ++;
|
36 | __delay_ms(1);
|
37 | }
|
38 | }
|
39 | void ft245_tx(void){ // send-function
|
40 | if(TXE == 0){ // if sending possible
|
41 |
|
42 | if(index_txBuf_R == index_txBuf_W){ // no new data => return
|
43 | return;
|
44 | }
|
45 | if(index_txBuf_R == txBuf_max){ // ringbuffer
|
46 | index_txBuf_R = 0;
|
47 | }
|
48 | else{
|
49 | index_txBuf_R ++;
|
50 | }
|
51 |
|
52 | DATA_bus = txBuf[index_txBuf_R]; // write datas to the ftdi
|
53 | WR_en; // new data available
|
54 | WR_dis;
|
55 | }
|
56 | }
|
57 | void txBuf_Write(char parameter){ // TX buffer handling
|
58 | if(index_txBuf_W == txBuf_max){ // ringbuffer
|
59 | index_txBuf_W = 0;
|
60 | }
|
61 | else{
|
62 | index_txBuf_W ++;
|
63 | }
|
64 | txBuf[index_txBuf_W] = parameter;
|
65 | }
|
66 |
|
67 | void ft245_rx(void){ // read function (polling)
|
68 | if(RX == 0){ // if new data in the FTDI buffer
|
69 | index_rxBuf_W = ~index_rxBuf_W;
|
70 | DDR_in;
|
71 | ADPCFG = 0xFFFF; // Digital IO
|
72 | RD_en; // read data enable
|
73 | rxBuf = (DATA_bus & 0xFF); // store data
|
74 | RD_dis; // read finished
|
75 | DDR_out;
|
76 | }
|
77 | }
|
78 |
|
79 | void USB(void){
|
80 | ft245_tx();
|
81 | ft245_rx();
|
82 | if(index_rxBuf_W == index_rxBuf_R){ // Buffer Error abgleichen
|
83 | return; // Wenn keine neuen Daten => aus Funktion heraus springen
|
84 | }
|
85 | index_rxBuf_R = ~index_rxBuf_R;
|
86 |
|
87 | txBuf_Write(rxBuf);
|
88 | }
|
89 |
|
90 | int main(void){
|
91 | TRISB = 0x0000; // Data-Bus
|
92 | ADPCFG = 0xFFFF; // Digital IO bei PortB
|
93 | TRISD = 0b0000111111111000; // RXF, CON inputs
|
94 | TRISF = 0b1111111111100111; // TEX, RD, WR, PWREN
|
95 | while(1){
|
96 | USB();
|
97 | }
|
98 | return 0;
|
99 | }
|