Test the SPI slave at pins p5-8

Dependencies:   mbed

Fork of spi_tester by Jacques Pelletier

Revision:
1:51bc46468482
Parent:
0:a5957f25b150
Child:
2:0cc974f03339
--- a/main.cpp	Fri Jul 26 04:51:00 2013 +0000
+++ b/main.cpp	Sun Oct 27 04:32:14 2013 +0000
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+/*
 #define PAR_NEGOTIATE_EXTENSIBILITY_LINK        0x80
 #define PAR_NEGOTIATE_REQ_EPP_MODE              0x40
 #define PAR_NEGOTIATE_REQ_ECP_MODE              0x10
@@ -15,36 +16,41 @@
 #define PAR_NEGOTIATE_REQ_DEV_ID_ECP_RLE_MODE   0x34
 #define PAR_NEGOTIATE_NIBBLE_MODE               0x00
 #define PAR_NEGOTIATE_BYTE_MODE                 0x01
+*/
 
 /*
 Instructions for use: connect the mbed to a parallel port using these connexions.
-use a terminal program to connect via USB to the mbed side.
+use a terminal program to connect via USB to the mbed side. */
 
-This program uses CR to rewrite over the status previously printed. Setup the terminal
-program so that CR = CR without line feed.
+#define PAR_INVERT_OBF_SIGNAL
+
+#define PAR_8255_SEND_MASK      0x80
+#define PAR_8255_RECV_MASK      0x40
 
-15 nError       -> p9
-13 Select       -> p10
-12 PE           -> p11
-11 Busy         -> p12
-10 nAck         -> p13
+/*
+  8255          Parallel    Pin     Bit
+  PC7   /OBF    ->  /ACK        10      6
+  PC6   /ACK    <-  /SLCTIN     17      3
+  PC5   IBF     ->  BUSY        11      7
+  PC4   /STB    <-  /STB        1       0
 
- 1 nStrobe      -> p14
-14 nAutoFeed    -> p15
-16 nInit        -> p16
-17 nSelectIn    -> p17
+15 nError       -> p9       not used
+13 Select       -> p10      not used
+12 PE           -> p11      not used
+11 Busy         -> p12      IBF
+10 nAck         -> p13      /OBF
+
+ 1 nStrobe      -> p14      /STB
+14 nAutoFeed    -> p15      not used
+16 nInit        -> p16      not used
+17 nSelectIn    -> p17      /ACK
 */
 
-DigitalOut nError(p9);
-DigitalOut Select(p10);
-DigitalOut PaperOut(p11);
-DigitalOut Busy(p12);
-DigitalOut nAck(p13);
+DigitalOut IBF(p12); // IBF
+DigitalOut nOBF(p13); // /OBF
 
-DigitalIn nStrobe(p14);
-DigitalIn nAutoFeed(p15);
-DigitalIn nInit(p16);
-DigitalIn nSelectIn(p17);
+InterruptIn nSTB(p14);
+InterruptIn nACK(p17);
 
 /*
 D0 p30  p0.4
@@ -65,98 +71,70 @@
 
 Serial pc(USBTX, USBRX); // tx, rx
 
-int main() {
+unsigned char rx_data;
+
+// Peripheral should check that there is no output pending from the 8255 to the peripheral before writing to the 8255
+
+// When /STB is falling
+void perif2mbed(void)
+{
+    // read byte from peripheral
+    rx_data = PtrData;
+    IBF = 1;
+}
+
+// When /ACK is rising
+void mbed2perif(void)
+{
+    nOBF = 1;
+    PtrData.input();
+}
+
+void write_byte(unsigned char out)
+{
+    // wait for /OBF = 1 and no read cycle in progress for politeness
+    while ((nOBF == 0) || (IBF == 1));
 
-char key;
-bool PortIsInput = false;
+    PtrData = out;
+    PtrData.output();
+    nOBF = 0;
+}
+
+unsigned char read_byte(void)
+{
+    while (IBF == 0);
+    IBF = 0;
+    return rx_data;
+}
+
+int main()
+{
+    PtrData.input();
+
+    /* 9600 baud serial port */
+    pc.printf("8255 emulator on mbed\r\n\n");
+    
+    IBF = 0;
+    nOBF = 1;
+    nSTB.fall(&perif2mbed);
+    nACK.rise(&mbed2perif);
 
     PtrData.input();
 
-    pc.printf("Parallel port tester on mbed\r\n\n");
-    pc.printf("Press keys 0-7,G,H,J,K and L to toggle output bits (inputs on PC)\r\n");
-    pc.printf("Press keys I and O to toggle data direction for bits 0-7\r\n");
-    pc.printf(" (be sure your PC and mbed don't output at the same time)\r\n\n");
-    pc.printf("PINS 9-2     1    14    16    17     ------ 15    13    12    11    10\r\n");
-    pc.printf("DATA 7-0   /STB  /AF  /INIT  /SEL_IN ------/ERR   SEL   PO    BUSY  /ACK\r\n");
-    pc.printf("Keys                                 ------  G     H     J     K     L\r\n");
-             //  XX         B     B     B     B     ------  B     B     B     B     B
-    while(1) {
-        pc.printf("  %02X         %c     %c     %c     %c             %c     %c     %c     %c     %c \r",
-            PtrData & 0xff,
-            '0'| nStrobe,
-            '0'| nAutoFeed,
-            '0'| nInit,
-            '0'| nSelectIn,            
-            '0'| nError,
-            '0'| Select,
-            '0'| PaperOut,
-            '0'| Busy,
-            '0'| nAck                                                
-            );
-
-        if (pc.readable())
+    while(1)
+    {
+        // bytes from peripherals to 8255
+        if (IBF == 1)
         {
-            key = pc.getc();
-            switch(key)
+            IBF = 0;
+            pc.putc(rx_data);
+        }
+        else
+        {
+            if (pc.readable())
             {
-                case 'I':
-                    PtrData.input();
-                    PortIsInput = true;
-                    break;
-                case 'O':
-                    PtrData.output();
-                    PortIsInput = false;
-                    break;
-                case '0':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x01;
-                    break;
-                case '1':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x02;
-                    break;
-                case '2':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x04;
-                    break;
-                case '3':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x08;
-                    break;
-                case '4':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x10;
-                    break;
-                case '5':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x20;
-                    break;
-                case '6':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x40;
-                    break;
-                case '7':
-                    if (!PortIsInput)
-                    PtrData = PtrData ^ 0x80;
-                    break;
-                case 'G':
-                    nError = !nError;
-                    break;
-                case 'H':
-                    Select = !Select;
-                    break;
-                case 'J':
-                    PaperOut = !PaperOut;
-                    break;
-                case 'K':
-                    Busy = !Busy;
-                    break;
-                case 'L':
-                    nAck = !nAck;
-                    break;
-                default:
-                    ; 
+                write_byte(pc.getc());
             }
-        }
+        }  
     }
 }