This program implements a slave SPI in software for testing the SPI interface of protocoltool.

Dependencies:   mbed

Fork of 8255_emulator by Jacques Pelletier

Committer:
jpelletier
Date:
Mon Sep 07 05:50:57 2015 +0000
Revision:
6:dccae7a269f9
Parent:
5:c79a6e66ed00
revert

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jpelletier0:a5957f25b150 1#include "mbed.h"
jpelletier0:a5957f25b150 2
jpelletier0:a5957f25b150 3//#include <stdarg.h>
jpelletier0:a5957f25b150 4#include <stdio.h>
jpelletier0:a5957f25b150 5#include <stdlib.h>
jpelletier0:a5957f25b150 6#include <string.h>
jpelletier0:a5957f25b150 7
jpelletier1:51bc46468482 8/*
jpelletier0:a5957f25b150 9Instructions for use: connect the mbed to a parallel port using these connexions.
jpelletier1:51bc46468482 10use a terminal program to connect via USB to the mbed side. */
jpelletier0:a5957f25b150 11
jpelletier2:0cc974f03339 12/* This is for testing since it uses the serial port at 9600 bauds to connect to a PC */
jpelletier2:0cc974f03339 13
jpelletier1:51bc46468482 14/*
jpelletier6:dccae7a269f9 15 8255 Parallel Pin Bit
jpelletier6:dccae7a269f9 16 PC7 /OBF -> /ACK 10 6
jpelletier6:dccae7a269f9 17 PC6 /ACK <- /SLCTIN 17 3
jpelletier6:dccae7a269f9 18 PC5 IBF -> BUSY 11 7
jpelletier6:dccae7a269f9 19 PC4 /STB <- /STB 1 0
jpelletier1:51bc46468482 20
jpelletier6:dccae7a269f9 2115 nError -> p9 not used
jpelletier6:dccae7a269f9 2213 Select -> p10 not used
jpelletier6:dccae7a269f9 2312 PE -> p11 not used
jpelletier6:dccae7a269f9 2411 Busy -> p12 MISO
jpelletier6:dccae7a269f9 2510 nAck -> p13 not used
jpelletier6:dccae7a269f9 26
jpelletier6:dccae7a269f9 27 1 nStrobe -> p14 MOSI
jpelletier6:dccae7a269f9 2814 nAutoFeed -> p15 SCLK
jpelletier6:dccae7a269f9 2916 nInit -> p16 not used
jpelletier6:dccae7a269f9 3017 nSelectIn -> p17 not used
jpelletier0:a5957f25b150 31*/
jpelletier0:a5957f25b150 32
jpelletier6:dccae7a269f9 33DigitalOut MISO(p12);
jpelletier6:dccae7a269f9 34DigitalIn MOSI(p14);
jpelletier6:dccae7a269f9 35InterruptIn SCLK(p15);
jpelletier6:dccae7a269f9 36
jpelletier6:dccae7a269f9 37/*
jpelletier6:dccae7a269f9 38CE0 p30 p0.4
jpelletier6:dccae7a269f9 39CE1 p29 p0.5
jpelletier6:dccae7a269f9 40CE2 p8 p0.6
jpelletier6:dccae7a269f9 41CE3 p7 p0.7
jpelletier6:dccae7a269f9 42CE4 p6 p0.8
jpelletier6:dccae7a269f9 43CE5 p5 p0.9
jpelletier6:dccae7a269f9 44CE6 p28 p0.10
jpelletier6:dccae7a269f9 45CE7 p27 p0.11
jpelletier6:dccae7a269f9 46*/
jpelletier6:dccae7a269f9 47BusInOut PtrData(p30,p29,p8,p7,p6,p5,p28,p27);
jpelletier6:dccae7a269f9 48
jpelletier6:dccae7a269f9 49#define __DOUTBUFSIZE 256
jpelletier6:dccae7a269f9 50#define __DINBUFSIZE 256
jpelletier6:dccae7a269f9 51char __outstr[__DOUTBUFSIZE];
jpelletier6:dccae7a269f9 52char __instr[__DINBUFSIZE];
jpelletier6:dccae7a269f9 53
jpelletier0:a5957f25b150 54Serial pc(USBTX, USBRX); // tx, rx
jpelletier0:a5957f25b150 55
jpelletier3:422d80770413 56unsigned char rx_data, tx_data;
jpelletier0:a5957f25b150 57
jpelletier6:dccae7a269f9 58bool msb_first = true;
jpelletier6:dccae7a269f9 59bool edge_falling = false; // false: CPOL = 0, CPHA = 0; true: CPOL = 1, CPHA = 1
jpelletier6:dccae7a269f9 60bool byte_ready;
jpelletier6:dccae7a269f9 61int bit_count;
jpelletier6:dccae7a269f9 62
jpelletier6:dccae7a269f9 63void shift_bit(void)
jpelletier6:dccae7a269f9 64{
jpelletier6:dccae7a269f9 65 if (msb_first)
jpelletier6:dccae7a269f9 66 {
jpelletier6:dccae7a269f9 67 rx_data = (rx_data << 1) | MOSI;
jpelletier6:dccae7a269f9 68 MISO = (tx_data & 0x80) >> 7;
jpelletier6:dccae7a269f9 69 tx_data <<= 1;
jpelletier6:dccae7a269f9 70 }
jpelletier6:dccae7a269f9 71 else
jpelletier6:dccae7a269f9 72 {
jpelletier6:dccae7a269f9 73 rx_data = (rx_data >> 1) | (MOSI << 7);
jpelletier6:dccae7a269f9 74 MISO = tx_data & 1;
jpelletier6:dccae7a269f9 75 tx_data >>= 1;
jpelletier6:dccae7a269f9 76 }
jpelletier6:dccae7a269f9 77 bit_count++;
jpelletier6:dccae7a269f9 78 if (bit_count == 8)
jpelletier6:dccae7a269f9 79 {
jpelletier6:dccae7a269f9 80 byte_ready = true;
jpelletier6:dccae7a269f9 81 bit_count = 0;
jpelletier6:dccae7a269f9 82 }
jpelletier6:dccae7a269f9 83}
jpelletier6:dccae7a269f9 84
jpelletier6:dccae7a269f9 85void sclk_fall(void)
jpelletier6:dccae7a269f9 86{
jpelletier6:dccae7a269f9 87 if (edge_falling && (PtrData == 0xfe))
jpelletier6:dccae7a269f9 88 {
jpelletier6:dccae7a269f9 89 shift_bit();
jpelletier6:dccae7a269f9 90 }
jpelletier6:dccae7a269f9 91}
jpelletier6:dccae7a269f9 92
jpelletier6:dccae7a269f9 93void sclk_rise(void)
jpelletier6:dccae7a269f9 94{
jpelletier6:dccae7a269f9 95 if (!edge_falling && (PtrData == 0xfe))
jpelletier6:dccae7a269f9 96 {
jpelletier6:dccae7a269f9 97 shift_bit();
jpelletier6:dccae7a269f9 98 }
jpelletier6:dccae7a269f9 99}
jpelletier1:51bc46468482 100
jpelletier1:51bc46468482 101int main()
jpelletier1:51bc46468482 102{
jpelletier6:dccae7a269f9 103unsigned char key;
jpelletier6:dccae7a269f9 104bool configure_end = false;
jpelletier6:dccae7a269f9 105
jpelletier6:dccae7a269f9 106 PtrData.input();
jpelletier6:dccae7a269f9 107
jpelletier6:dccae7a269f9 108 /* 9600 baud serial port */
jpelletier6:dccae7a269f9 109 pc.printf("SPI tester on mbed\r\n\n");
jpelletier5:c79a6e66ed00 110
jpelletier6:dccae7a269f9 111 MISO = 0;
jpelletier6:dccae7a269f9 112 SCLK.fall(&sclk_fall);
jpelletier6:dccae7a269f9 113 SCLK.rise(&sclk_rise);
jpelletier6:dccae7a269f9 114 SCLK.mode(PullUp);
jpelletier6:dccae7a269f9 115 SCLK.enable_irq();
jpelletier5:c79a6e66ed00 116
jpelletier6:dccae7a269f9 117 byte_ready = false;
jpelletier6:dccae7a269f9 118 bit_count = 0;
jpelletier6:dccae7a269f9 119
jpelletier6:dccae7a269f9 120 pc.printf("Actual configuration\r\n\n");
jpelletier6:dccae7a269f9 121 pc.printf("MSB first\r\n");
jpelletier6:dccae7a269f9 122 pc.printf("Rising edge clock\r\n\n");
jpelletier6:dccae7a269f9 123
jpelletier5:c79a6e66ed00 124 pc.printf("Configure\r\n\n");
jpelletier6:dccae7a269f9 125 pc.printf("M: MSB first\r\n");
jpelletier6:dccae7a269f9 126 pc.printf("L: LSB first\r\n");
jpelletier6:dccae7a269f9 127 pc.printf("F: Falling edge clock\r\n");
jpelletier6:dccae7a269f9 128 pc.printf("R: Rising edge clock\r\n");
jpelletier6:dccae7a269f9 129 pc.printf("G: Go\r\n\n");
jpelletier3:422d80770413 130
jpelletier6:dccae7a269f9 131 do
jpelletier6:dccae7a269f9 132 {
jpelletier6:dccae7a269f9 133 key = pc.getc();
jpelletier6:dccae7a269f9 134
jpelletier6:dccae7a269f9 135 switch (key)
jpelletier6:dccae7a269f9 136 {
jpelletier6:dccae7a269f9 137 case 'M':
jpelletier6:dccae7a269f9 138 case 'm':
jpelletier6:dccae7a269f9 139 msb_first = true;
jpelletier6:dccae7a269f9 140 pc.printf("MSB first\r\n");
jpelletier6:dccae7a269f9 141 break;
jpelletier1:51bc46468482 142
jpelletier6:dccae7a269f9 143 case 'L':
jpelletier6:dccae7a269f9 144 case 'l':
jpelletier6:dccae7a269f9 145 msb_first = false;
jpelletier6:dccae7a269f9 146 pc.printf("LSB first\r\n");
jpelletier6:dccae7a269f9 147 break;
jpelletier6:dccae7a269f9 148
jpelletier6:dccae7a269f9 149 case 'F':
jpelletier6:dccae7a269f9 150 case 'f':
jpelletier6:dccae7a269f9 151 edge_falling = true;
jpelletier6:dccae7a269f9 152 pc.printf("Falling edge clock\r\n");
jpelletier6:dccae7a269f9 153 break;
jpelletier6:dccae7a269f9 154
jpelletier6:dccae7a269f9 155 case 'R':
jpelletier6:dccae7a269f9 156 case 'r':
jpelletier6:dccae7a269f9 157 edge_falling = true;
jpelletier6:dccae7a269f9 158 pc.printf("Rising edge clock\r\n");
jpelletier6:dccae7a269f9 159 break;
jpelletier6:dccae7a269f9 160
jpelletier6:dccae7a269f9 161 case 'G':
jpelletier6:dccae7a269f9 162 case 'g':
jpelletier6:dccae7a269f9 163 configure_end = true;
jpelletier6:dccae7a269f9 164 break;
jpelletier6:dccae7a269f9 165
jpelletier6:dccae7a269f9 166 default:
jpelletier6:dccae7a269f9 167 ;
jpelletier6:dccae7a269f9 168 }
jpelletier6:dccae7a269f9 169 } while (!configure_end);
jpelletier6:dccae7a269f9 170
jpelletier6:dccae7a269f9 171 pc.printf("Configure end, begin test\r\n\n");
jpelletier6:dccae7a269f9 172
jpelletier6:dccae7a269f9 173 while(1)
jpelletier5:c79a6e66ed00 174 {
jpelletier6:dccae7a269f9 175 if (pc.readable())
jpelletier6:dccae7a269f9 176 {
jpelletier6:dccae7a269f9 177 tx_data = pc.getc();
jpelletier3:422d80770413 178 }
jpelletier6:dccae7a269f9 179 else
jpelletier6:dccae7a269f9 180 {
jpelletier6:dccae7a269f9 181 if (byte_ready)
jpelletier6:dccae7a269f9 182 {
jpelletier6:dccae7a269f9 183 pc.putc(rx_data);
jpelletier6:dccae7a269f9 184 byte_ready = false;
jpelletier6:dccae7a269f9 185 }
jpelletier6:dccae7a269f9 186 }
jpelletier0:a5957f25b150 187 }
jpelletier0:a5957f25b150 188}