1 | /*
|
---|
2 | Test Code to make Arduino Ethernet communicate with PLD of Volker Commichaus
|
---|
3 | Bias Crate Controller of the FACt Bias voltage supply system
|
---|
4 | instead of FT245R interface
|
---|
5 |
|
---|
6 | Interface to PLD or
|
---|
7 | something that behaves similarly:
|
---|
8 |
|
---|
9 | Digital I/O:
|
---|
10 | Arduino Pin : Net Name : Int/Out/Bi : mnemonic
|
---|
11 | -----------------------------------------------------------------------------
|
---|
12 | Pin D2 : WR : I : Write to 'FIFO'
|
---|
13 | Pin D3 : RD# : I : Read from 'FIFO'
|
---|
14 | Pin D8 : TXE# : O : transmit enable
|
---|
15 | Pin D9 : RXF# : O : Read from FIFO possible
|
---|
16 | Pin D4 : D0 : BI
|
---|
17 | Pin D5 : D1 : BI
|
---|
18 | Pin D6 : D2 : BI
|
---|
19 | Pin D7 : D3 : BI
|
---|
20 | Pin A0 : D4 : BI : Data
|
---|
21 | Pin A1 : D5 : BI
|
---|
22 | Pin A2 : D6 : BI
|
---|
23 | Pin A3 : D7 : BI
|
---|
24 | -----------------------------------------------------------------------------
|
---|
25 |
|
---|
26 | port definition:
|
---|
27 | Port D consists of Arduino-Pins 0 - 7 and is used for Data exchange with the PLD
|
---|
28 | Port C consists of Arduino-Pins A0 - A5 and is used for Data exchange with the PLD
|
---|
29 | */
|
---|
30 | //define non transferable variables
|
---|
31 | #define MAXBOARDS 13
|
---|
32 | #define EXISTINGBOARDS 10
|
---|
33 | #define CHPERBOARD 32
|
---|
34 | #define NUMCHANNELS EXISTINGBOARDS*CHPERBOARD
|
---|
35 | #define BLANKBYTE 0x00
|
---|
36 | #define READSTATEBYTE 0x20
|
---|
37 | #define GLOBALSETBYTE 0x40
|
---|
38 | #define CHANNELSETBYTE 0x60
|
---|
39 | #define FREE1 A5
|
---|
40 | #define FREE2 A4
|
---|
41 |
|
---|
42 | //#define MAXVOLTAGE 80
|
---|
43 |
|
---|
44 | typedef enum {
|
---|
45 | set_message,
|
---|
46 | send_message,
|
---|
47 | get_message,
|
---|
48 | pc_serial,
|
---|
49 | parsing
|
---|
50 | } state_contr; //Type to define what microcontroller should do
|
---|
51 |
|
---|
52 | typedef enum {
|
---|
53 | channel_status,
|
---|
54 | sys_reset,
|
---|
55 | global_set,
|
---|
56 | channel_set
|
---|
57 | } function_t; //Type to define which function of PLD shell be build
|
---|
58 |
|
---|
59 | typedef enum {
|
---|
60 | transmit_PLD_answer,
|
---|
61 | get_request,
|
---|
62 | } communication; //Type to define communication with PC
|
---|
63 |
|
---|
64 |
|
---|
65 | const unsigned char wr = 2;
|
---|
66 | const unsigned char rd = 3;
|
---|
67 | const unsigned char txe = 8;
|
---|
68 | const unsigned char rxf = 9;
|
---|
69 | //variables to save Data-Bytes in
|
---|
70 | volatile unsigned char databus = 0x00; //temp. memory for Reading Databus interrupt
|
---|
71 | unsigned char message[3]; //field where the 3 Byte message is saved
|
---|
72 | unsigned char messageindex = 0; //index of field
|
---|
73 | unsigned char incommingByte = 0; //for incomming serial Data
|
---|
74 | unsigned int recent_voltages[NUMCHANNELS]; //The actual Voltage of each channel is saved in this variable
|
---|
75 | unsigned int set_voltage; //The Value to which channel shell be set is saved in the variable
|
---|
76 | boolean stat; //PLD Status-bit
|
---|
77 | unsigned char wrapcounter; //Wrap counter:
|
---|
78 | unsigned char channel; //channelnumber number
|
---|
79 |
|
---|
80 | function_t function;
|
---|
81 | state_contr state_controler;
|
---|
82 | communication pc_com = get_request;
|
---|
83 |
|
---|
84 | //unsigned char call_function; //variable to call a funtion
|
---|
85 | unsigned char error; //Errorcode from PLD
|
---|
86 | unsigned char board; //board number
|
---|
87 | //define Variables that mark edge-detection on RD and WR pin
|
---|
88 | volatile boolean RisingEdge_RD = false;
|
---|
89 | volatile boolean risingEdge_WR = false;
|
---|
90 |
|
---|
91 | void setup() {
|
---|
92 | //delay(1000);
|
---|
93 | //Initiate Controlpin Usage & Stati
|
---|
94 | pinMode( wr, INPUT);
|
---|
95 | pinMode( rd, INPUT);
|
---|
96 | digitalWrite(txe, HIGH);
|
---|
97 | pinMode( txe, OUTPUT);
|
---|
98 | digitalWrite(rxf, HIGH); //set initial state of RXF#
|
---|
99 | pinMode( rxf, OUTPUT);
|
---|
100 | digitalWrite(FREE1, HIGH);
|
---|
101 | pinMode(FREE1, OUTPUT);
|
---|
102 | pinMode(FREE2, INPUT);
|
---|
103 | //define initial I/O of pins of Port D to Input
|
---|
104 |
|
---|
105 | MakeDataBusInput(); // Binary: 00000000, since the bus is Bidirectional .. we better set to input in the idle case
|
---|
106 |
|
---|
107 | //init test_values
|
---|
108 | board = 0x09;
|
---|
109 | channel = 0x0f;
|
---|
110 | function = channel_set;
|
---|
111 | set_voltage = 0xaaa;
|
---|
112 | state_controler = pc_serial; //just for development
|
---|
113 | digitalWrite(FREE1, LOW);
|
---|
114 |
|
---|
115 | //define Interrupts for RD and WR
|
---|
116 | attachInterrupt(1, RisingEdgeOnRD, RISING);
|
---|
117 | attachInterrupt(0, RisingEdgeOnWR, RISING);
|
---|
118 |
|
---|
119 | //Open comunication to PC
|
---|
120 | Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | void loop() {
|
---|
125 | // Communication with the PC, handle PC requests
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | //Here the controller builds the message that will be send to PLD
|
---|
130 | PORTC ^= 1<<PC5;
|
---|
131 | PORTC ^= 1<<PC5;
|
---|
132 | if ( state_controler == set_message){
|
---|
133 | switch( function ){
|
---|
134 |
|
---|
135 | case channel_status:
|
---|
136 | channelStaus(board, channel); //calls the channelstatus-function so values in message[] are set correctly
|
---|
137 | break;
|
---|
138 |
|
---|
139 | case sys_reset:
|
---|
140 | systemReset(); //calls the SystemReset-function so values in message[] are set correctly
|
---|
141 | break;
|
---|
142 |
|
---|
143 | case global_set:
|
---|
144 | globalSet(set_voltage); //calls the GlobalSet-function so values in message[] are set correctly
|
---|
145 | break;
|
---|
146 |
|
---|
147 | case channel_set:
|
---|
148 | channelSet(board, channel, set_voltage); //calls the channelSet-function so values in message[] are set correctly
|
---|
149 | break;
|
---|
150 |
|
---|
151 | }
|
---|
152 | state_controler = send_message; //just for development
|
---|
153 | }
|
---|
154 | if ( state_controler == send_message){
|
---|
155 | //Serial.print ("sending message");
|
---|
156 | //RisingEdge_RD = false;
|
---|
157 | //This is the FIFO Read Cycle, PLD reads from FIFO, ARDUINO writes to FIFO
|
---|
158 | PORTC ^= 1<<PC5;
|
---|
159 | PORTC ^= 1<<PC5;
|
---|
160 | PORTC ^= 1<<PC5;
|
---|
161 | PORTC ^= 1<<PC5;
|
---|
162 | digitalWrite(txe, HIGH); //Forbid PLD to send Datan, needs 4 microsec
|
---|
163 | MakeDataBusOutput(); //set Databus as Output before you tell PLD that you have Data
|
---|
164 | //This is the Statemachine where the 3 bytes will be set at PORTD
|
---|
165 | switch( messageindex ){
|
---|
166 | case 0:
|
---|
167 | PutOnDataBus(message[messageindex]);
|
---|
168 | RisingEdge_RD = false;
|
---|
169 | digitalWrite(rxf, LOW ); //goes to low to tell PLD that Data is availiable, after PORTD to make shure that there is Data
|
---|
170 | while(!RisingEdge_RD){}
|
---|
171 | digitalWrite(rxf, HIGH );
|
---|
172 | messageindex++;
|
---|
173 | break;
|
---|
174 |
|
---|
175 | case 1:
|
---|
176 | PutOnDataBus(message[messageindex]);
|
---|
177 | digitalWrite(rxf, LOW ); //goes to low to tell PLD that Data is availiable
|
---|
178 | while(!RisingEdge_RD){}
|
---|
179 | digitalWrite(rxf, HIGH );
|
---|
180 | messageindex++;
|
---|
181 | break;
|
---|
182 |
|
---|
183 | case 2:
|
---|
184 | PutOnDataBus(message[messageindex]);
|
---|
185 | digitalWrite(rxf, LOW ); //goes to low to tell PLD that Data is availiable
|
---|
186 | while(!RisingEdge_RD ){}
|
---|
187 | digitalWrite(rxf, HIGH ); //goes to high to tell PLD that no Data is availiable
|
---|
188 | digitalWrite(txe, LOW ); //set TXE# to low to permit PLD to send DATA
|
---|
189 | MakeDataBusInput(); //Set PORT D to Input: PLD can write DATA on that Port
|
---|
190 | messageindex = 0;
|
---|
191 | state_controler = get_message; //Listen to PLD for status report
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | RisingEdge_RD = false;
|
---|
195 |
|
---|
196 | }
|
---|
197 |
|
---|
198 | //The FIFO Write Cycle
|
---|
199 | if ( state_controler == get_message){
|
---|
200 | digitalWrite(rxf, HIGH ); //goes to high to tell PLD that no Data is availiable
|
---|
201 | MakeDataBusInput(); //set Port D as Input
|
---|
202 | digitalWrite(txe, LOW ); //goes to low to tell PLD it can send Data
|
---|
203 | if (risingEdge_WR){ //write Data from Byte into FiFo when WR goes from high to low and there was an Rising Edge before
|
---|
204 | switch( messageindex ){
|
---|
205 | case 0:
|
---|
206 | message[messageindex] = databus;
|
---|
207 | messageindex++;
|
---|
208 | break;
|
---|
209 |
|
---|
210 | case 1:
|
---|
211 | message[messageindex] = databus;
|
---|
212 | messageindex++;
|
---|
213 | break;
|
---|
214 |
|
---|
215 | case 2:
|
---|
216 | message[messageindex] = databus;
|
---|
217 | messageindex = 0;
|
---|
218 | state_controler = pc_serial;
|
---|
219 | pc_com = transmit_PLD_answer;
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | risingEdge_WR = false; //reset RisingEdge interrupt variable to false to detect incomming Edge
|
---|
223 | digitalWrite(txe, HIGH); //reset TXE to tell PLD not not to write
|
---|
224 |
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | // delay(1000); //slow down process for test pourpose
|
---|
229 |
|
---|
230 | // Communication with PC via USB
|
---|
231 | if ( state_controler == pc_serial){
|
---|
232 |
|
---|
233 | // Outgoing Commands
|
---|
234 | switch ( pc_com ){
|
---|
235 | //report PLD response to PC
|
---|
236 | case transmit_PLD_answer:
|
---|
237 | Serial.println("PLD answered: ");
|
---|
238 | for (messageindex = 0; messageindex < 3; messageindex++){
|
---|
239 | Serial.print(message[messageindex], BIN);
|
---|
240 | Serial.print( "\t" );
|
---|
241 | }
|
---|
242 | Serial.println(" in BIN ");
|
---|
243 | for (messageindex = 0; messageindex < 3; messageindex++){
|
---|
244 | Serial.print(message[messageindex], HEX);
|
---|
245 | Serial.print( "\t" );
|
---|
246 | }
|
---|
247 | messageindex = 0;
|
---|
248 | Serial.println(" in HEX ");
|
---|
249 | Serial.println( "_______________________ " );
|
---|
250 | pc_com = get_request;
|
---|
251 | break;
|
---|
252 |
|
---|
253 | // Incoming Commands
|
---|
254 | case get_request:
|
---|
255 | while (Serial.available() < 3){};
|
---|
256 | if (Serial.available() > 0){
|
---|
257 | Serial.print("I received: ");
|
---|
258 | messageindex = 0;
|
---|
259 | while (messageindex < 3){
|
---|
260 | message[messageindex] = Serial.read();
|
---|
261 | Serial.print( "0x" );
|
---|
262 | Serial.print(message[messageindex], HEX);
|
---|
263 | messageindex++;
|
---|
264 | Serial.print( " " );
|
---|
265 | }
|
---|
266 | messageindex = 0;
|
---|
267 | //Serial.print(incommingByte, BYTE);
|
---|
268 | Serial.println(",du Nase!");
|
---|
269 | }
|
---|
270 | state_controler = send_message;
|
---|
271 | break;
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /*
|
---|
281 | This are the funtions that can be used with the board:
|
---|
282 | */
|
---|
283 | //Read Data from DATABUS
|
---|
284 | unsigned char ReadFromDataBus(){
|
---|
285 | // The eight bits of the databus are distributed over
|
---|
286 | // the two differen ports of the ATmega. Port C and D
|
---|
287 | // actually they are connected like this:
|
---|
288 | // data0...data3 are connected to Arduino Pins D4..D7
|
---|
289 | // data4...data7 are connected to Arduino Pins C0..D3
|
---|
290 | return ( (PINC&0x0f) << 4 ) | ( (PIND&0xf0) >> 4) ;
|
---|
291 | }
|
---|
292 |
|
---|
293 | //Write Data from DATABUS
|
---|
294 | void PutOnDataBus ( unsigned char data) {
|
---|
295 | // okay .. .again
|
---|
296 | // data0..3 is connected to PD4..7
|
---|
297 | // data4..7 is connected to PC0..3
|
---|
298 | PORTC = (PORTC & 0xf0) | (data&0xf0)>>4 ;
|
---|
299 | PORTD = (PORTD & 0x0f) | (data&0x0f)<<4 ;
|
---|
300 |
|
---|
301 | }
|
---|
302 |
|
---|
303 | void MakeDataBusOutput() {
|
---|
304 | // making the databus out can be done like this:
|
---|
305 | // data0..3 is connected to PD4..7
|
---|
306 | // data4..7 is connected to PC0..3
|
---|
307 |
|
---|
308 | // also auf PORTC die bits 0 bis 3 setzen!
|
---|
309 | DDRC |= 0x0f;
|
---|
310 | // und auf PORTD die bits 4 bis 7 setzen!
|
---|
311 | DDRD |= 0xf0;
|
---|
312 |
|
---|
313 | // Um die Pullups muss man sicht nicht kümmern, da nach dieser Funktion sowieso ein neuer Wert auf den Bus
|
---|
314 | // geschrieben wird.
|
---|
315 | }
|
---|
316 |
|
---|
317 | void MakeDataBusInput() {
|
---|
318 | // see the comments in MakeDataBusOutput()
|
---|
319 |
|
---|
320 | //first we switch the outs to ins
|
---|
321 | // then we switch on the pullups
|
---|
322 |
|
---|
323 | DDRC &= 0xf0;
|
---|
324 | DDRD &= 0x0f;
|
---|
325 | }
|
---|
326 |
|
---|
327 | //System Reset
|
---|
328 | void systemReset(){
|
---|
329 | for (messageindex = 0; messageindex < 2; messageindex++){
|
---|
330 | message[messageindex] = 0x0; // set alle 3byte to 0
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | //Read channel Status & Current
|
---|
335 | void channelStaus(unsigned char brd, unsigned char chan){
|
---|
336 | message[0] = READSTATEBYTE | (brd << 1); //put funtion, board in first byte
|
---|
337 | message[0] = message[0] | (chan >> 4); //add first channelbit to end of first byte
|
---|
338 | message[1] = BLANKBYTE | (chan << 4); //put rest of channelbit in byte2
|
---|
339 | message[2] = BLANKBYTE; // set byte 3 to 0
|
---|
340 | }
|
---|
341 |
|
---|
342 | //Global set
|
---|
343 | void globalSet(unsigned int volt){
|
---|
344 | message[0] = GLOBALSETBYTE;
|
---|
345 | message[1] = BLANKBYTE | (volt >> 8);
|
---|
346 | message[2] = BLANKBYTE | volt;
|
---|
347 | }
|
---|
348 |
|
---|
349 | //Channel set to Voltage
|
---|
350 | void channelSet(unsigned char brd, unsigned char chan, unsigned int volt){
|
---|
351 | message[0] = CHANNELSETBYTE | (brd << 1);
|
---|
352 | message[0] = message[0] | (chan >> 4);
|
---|
353 | message[1] = BLANKBYTE | (chan << 4);
|
---|
354 | message[1] = message[1] | (volt >> 4);
|
---|
355 | message[2] = BLANKBYTE | volt;
|
---|
356 | }
|
---|
357 |
|
---|
358 | //functions for Interrupts on RD and WR
|
---|
359 | void RisingEdgeOnRD(){
|
---|
360 | //digitalWrite(FREE1, HIGH);
|
---|
361 | //digitalWrite(FREE1, LOW);
|
---|
362 |
|
---|
363 | PORTC ^= 1<<PC5;
|
---|
364 | PORTC ^= 1<<PC5;
|
---|
365 | PORTC ^= 1<<PC5;
|
---|
366 | PORTC ^= 1<<PC5;
|
---|
367 | PORTC ^= 1<<PC5;
|
---|
368 | PORTC ^= 1<<PC5;
|
---|
369 |
|
---|
370 | RisingEdge_RD = true;
|
---|
371 | }
|
---|
372 |
|
---|
373 | void RisingEdgeOnWR(){
|
---|
374 | databus = ReadFromDataBus();
|
---|
375 | risingEdge_WR = true;
|
---|
376 | PORTC ^= 1<<PC5;
|
---|
377 | PORTC ^= 1<<PC5;
|
---|
378 | PORTC ^= 1<<PC5;
|
---|
379 | PORTC ^= 1<<PC5;
|
---|
380 | PORTC ^= 1<<PC5;
|
---|
381 | PORTC ^= 1<<PC5;
|
---|
382 | PORTC ^= 1<<PC5;
|
---|
383 | PORTC ^= 1<<PC5;
|
---|
384 |
|
---|
385 | }
|
---|
386 | /*
|
---|
387 | void CommandToPLD(unsigned int txe, unsigned int rxf ){
|
---|
388 | //txe auf D8, rxf auf D9
|
---|
389 | if (txe == HIGH){
|
---|
390 | if (rxf == HIGH){
|
---|
391 | PORTB = B00000011;
|
---|
392 | }
|
---|
393 | else if(rxf == LOW){
|
---|
394 | PORTB = B00000001;
|
---|
395 | }
|
---|
396 | }
|
---|
397 | else if (txe ==
|
---|
398 | PORTB = PORTB;
|
---|
399 | }
|
---|
400 | */
|
---|