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