Tsungta Wu
/
mbed_SPIS_multiByte_example_SOG
fork from Sog
Fork of mbed_SPIS_multiByte_example_SOG by
main.cpp@5:6a1155885fc9, 2017-06-13 (annotated)
- Committer:
- sog_yang
- Date:
- Tue Jun 13 04:34:01 2017 +0000
- Revision:
- 5:6a1155885fc9
- Parent:
- 4:8e796b751cd3
- Child:
- 6:bce37a45b9cb
Sog update version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsungta | 4:8e796b751cd3 | 1 | //#include "mbed.h" |
tsungta | 4:8e796b751cd3 | 2 | // |
tsungta | 4:8e796b751cd3 | 3 | //SPI spi(A1, A2, A3); // mosi, miso, sclk |
tsungta | 4:8e796b751cd3 | 4 | //DigitalOut cs(A0); |
tsungta | 4:8e796b751cd3 | 5 | // |
tsungta | 4:8e796b751cd3 | 6 | //uint8_t tx_buf[32] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'}; |
tsungta | 4:8e796b751cd3 | 7 | // |
tsungta | 4:8e796b751cd3 | 8 | //int main() { |
tsungta | 4:8e796b751cd3 | 9 | // // Chip must be deselected |
tsungta | 4:8e796b751cd3 | 10 | // cs = 1; |
tsungta | 4:8e796b751cd3 | 11 | // |
tsungta | 4:8e796b751cd3 | 12 | // // Setup the spi for 8 bit data, high steady state clock, |
tsungta | 4:8e796b751cd3 | 13 | // // second edge capture, with a 1MHz clock rate |
tsungta | 4:8e796b751cd3 | 14 | // spi.format(8,1); |
tsungta | 4:8e796b751cd3 | 15 | // spi.frequency(8000000); |
tsungta | 4:8e796b751cd3 | 16 | // |
tsungta | 4:8e796b751cd3 | 17 | // // Select the device by seting chip select low |
tsungta | 4:8e796b751cd3 | 18 | // cs = 0; |
tsungta | 4:8e796b751cd3 | 19 | // |
tsungta | 4:8e796b751cd3 | 20 | // for(int i=0; i<32; i++) |
tsungta | 4:8e796b751cd3 | 21 | // spi.write(tx_buf[i]); |
tsungta | 4:8e796b751cd3 | 22 | // printf("End of transmission \r\n"); |
tsungta | 4:8e796b751cd3 | 23 | // |
tsungta | 4:8e796b751cd3 | 24 | // // Deselect the device |
tsungta | 4:8e796b751cd3 | 25 | // cs = 1; |
tsungta | 4:8e796b751cd3 | 26 | //} |
tsungta | 4:8e796b751cd3 | 27 | |
tsungta | 0:b7415ae44dac | 28 | #include "mbed.h" |
sog_yang | 5:6a1155885fc9 | 29 | #include "SPISlave_multiByte.h" |
tsungta | 0:b7415ae44dac | 30 | |
sog_yang | 5:6a1155885fc9 | 31 | //SPISlave_multiByte device(A1, A2, A3, p3); // mosi, miso, sclk, ssel |
sog_yang | 5:6a1155885fc9 | 32 | SPISlave_multiByte device(p15, p9, p11, p29); // mosi, miso, sclk, ssel |
tsungta | 0:b7415ae44dac | 33 | Serial uart(USBTX, USBRX); |
sog_yang | 5:6a1155885fc9 | 34 | DigitalOut myled(LED1); |
tsungta | 0:b7415ae44dac | 35 | |
sog_yang | 5:6a1155885fc9 | 36 | |
sog_yang | 5:6a1155885fc9 | 37 | inline void hexview(const char *buffer, unsigned int size) { |
sog_yang | 5:6a1155885fc9 | 38 | char byte[50]; |
sog_yang | 5:6a1155885fc9 | 39 | char text[20]; |
sog_yang | 5:6a1155885fc9 | 40 | bool big = false; |
sog_yang | 5:6a1155885fc9 | 41 | int i; |
sog_yang | 5:6a1155885fc9 | 42 | for(i = 0; i < size; ++i) { |
sog_yang | 5:6a1155885fc9 | 43 | if((i&0xF) == 0x0) { |
sog_yang | 5:6a1155885fc9 | 44 | if(big) |
sog_yang | 5:6a1155885fc9 | 45 | printf("%04X: %-49s: %-20s\n", (i&~0xF), byte, text); |
sog_yang | 5:6a1155885fc9 | 46 | big = false; |
sog_yang | 5:6a1155885fc9 | 47 | byte[0] = '\0'; |
sog_yang | 5:6a1155885fc9 | 48 | text[0] = '\0'; |
sog_yang | 5:6a1155885fc9 | 49 | } else if((i&0xF) == 0x8) { |
sog_yang | 5:6a1155885fc9 | 50 | big = true; |
sog_yang | 5:6a1155885fc9 | 51 | byte[(i&0xF) * 3] = ' '; |
sog_yang | 5:6a1155885fc9 | 52 | text[(i&0xF)] = ' '; |
sog_yang | 5:6a1155885fc9 | 53 | } |
sog_yang | 5:6a1155885fc9 | 54 | unsigned char value = buffer[i]; |
sog_yang | 5:6a1155885fc9 | 55 | text[(i&0xF) + 0 + big] = (value < 0x20 || value > 0x7F)? '.': value; |
sog_yang | 5:6a1155885fc9 | 56 | text[(i&0xF) + 1 + big] = '\0'; |
sog_yang | 5:6a1155885fc9 | 57 | value = (buffer[i] &0xF0) >> 4; |
sog_yang | 5:6a1155885fc9 | 58 | byte[(i&0xF) * 3 + 0 + big] = (value < 0xA)? (value + 0x30): (value + 0x37); |
sog_yang | 5:6a1155885fc9 | 59 | value = (buffer[i] &0x0F); |
sog_yang | 5:6a1155885fc9 | 60 | byte[(i&0xF) * 3 + 1 + big] = (value < 0xA)? (value + 0x30): (value + 0x37); |
sog_yang | 5:6a1155885fc9 | 61 | byte[(i&0xF) * 3 + 2 + big] = ' '; |
sog_yang | 5:6a1155885fc9 | 62 | byte[(i&0xF) * 3 + 3 + big] = '\0'; |
sog_yang | 5:6a1155885fc9 | 63 | } |
sog_yang | 5:6a1155885fc9 | 64 | if(byte[0]) { |
sog_yang | 5:6a1155885fc9 | 65 | uart.printf("%04X: %-49s: %-20s\n", (i&~0xF), byte, text); |
sog_yang | 5:6a1155885fc9 | 66 | } |
sog_yang | 5:6a1155885fc9 | 67 | uart.printf("\n"); |
sog_yang | 5:6a1155885fc9 | 68 | } |
sog_yang | 5:6a1155885fc9 | 69 | |
tsungta | 0:b7415ae44dac | 70 | int main() { |
tsungta | 0:b7415ae44dac | 71 | uart.printf("START!"); |
sog_yang | 5:6a1155885fc9 | 72 | device.format(8,0); |
sog_yang | 5:6a1155885fc9 | 73 | myled = 1; |
tsungta | 2:c520d7c7739d | 74 | |
tsungta | 0:b7415ae44dac | 75 | while(1) { |
sog_yang | 5:6a1155885fc9 | 76 | |
sog_yang | 5:6a1155885fc9 | 77 | uart.printf("Add: %p \r\n", device.read()); |
sog_yang | 5:6a1155885fc9 | 78 | uart.printf("%c \r\n", device.read()); |
sog_yang | 5:6a1155885fc9 | 79 | |
sog_yang | 5:6a1155885fc9 | 80 | while (!device.readable()) { |
sog_yang | 5:6a1155885fc9 | 81 | __WFE(); |
sog_yang | 5:6a1155885fc9 | 82 | } |
sog_yang | 5:6a1155885fc9 | 83 | hexview((const char *)0x200032D0, 224); |
sog_yang | 5:6a1155885fc9 | 84 | //hexview((const char *)ptr, 224); |
sog_yang | 5:6a1155885fc9 | 85 | //uart.printf("%c\r\n",m_rx_buf[3]); |
sog_yang | 5:6a1155885fc9 | 86 | |
sog_yang | 5:6a1155885fc9 | 87 | myled = !myled; |
sog_yang | 5:6a1155885fc9 | 88 | wait_ms(200); |
tsungta | 0:b7415ae44dac | 89 | } |
tsungta | 0:b7415ae44dac | 90 | } |