Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed_SPIS_multiByte_example_SOG by
main.cpp@7:0ea5460e0de3, 2017-06-21 (annotated)
- Committer:
- tsungta
- Date:
- Wed Jun 21 08:54:26 2017 +0000
- Revision:
- 7:0ea5460e0de3
- Parent:
- 6:bce37a45b9cb
Add TCP send function
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 | |
| tsungta | 7:0ea5460e0de3 | 31 | #include "EthernetInterface.h" |
| tsungta | 7:0ea5460e0de3 | 32 | #include "WIFIDevice.h" |
| tsungta | 7:0ea5460e0de3 | 33 | |
| tsungta | 7:0ea5460e0de3 | 34 | SPISlave_multiByte device(A1, A2, A3, p3); // mosi, miso, sclk, ssel |
| sog_yang | 6:bce37a45b9cb | 35 | //SPISlave_multiByte device(p15, p9, p11, p29); // mosi, miso, sclk, ssel |
| tsungta | 7:0ea5460e0de3 | 36 | //SPISlave_multiByte device(A1, A0, A3, A2); // mosi, miso, sclk, ssel |
| tsungta | 0:b7415ae44dac | 37 | Serial uart(USBTX, USBRX); |
| sog_yang | 5:6a1155885fc9 | 38 | DigitalOut myled(LED1); |
| tsungta | 0:b7415ae44dac | 39 | |
| tsungta | 7:0ea5460e0de3 | 40 | char* TCP_SERVER_ADDRESS = "192.168.2.7"; |
| tsungta | 7:0ea5460e0de3 | 41 | int TCP_SERVER_PORT = 1030; |
| sog_yang | 5:6a1155885fc9 | 42 | |
| sog_yang | 5:6a1155885fc9 | 43 | inline void hexview(const char *buffer, unsigned int size) { |
| sog_yang | 5:6a1155885fc9 | 44 | char byte[50]; |
| sog_yang | 5:6a1155885fc9 | 45 | char text[20]; |
| sog_yang | 5:6a1155885fc9 | 46 | bool big = false; |
| sog_yang | 5:6a1155885fc9 | 47 | int i; |
| sog_yang | 5:6a1155885fc9 | 48 | for(i = 0; i < size; ++i) { |
| sog_yang | 5:6a1155885fc9 | 49 | if((i&0xF) == 0x0) { |
| sog_yang | 5:6a1155885fc9 | 50 | if(big) |
| sog_yang | 5:6a1155885fc9 | 51 | printf("%04X: %-49s: %-20s\n", (i&~0xF), byte, text); |
| sog_yang | 5:6a1155885fc9 | 52 | big = false; |
| sog_yang | 5:6a1155885fc9 | 53 | byte[0] = '\0'; |
| sog_yang | 5:6a1155885fc9 | 54 | text[0] = '\0'; |
| sog_yang | 5:6a1155885fc9 | 55 | } else if((i&0xF) == 0x8) { |
| sog_yang | 5:6a1155885fc9 | 56 | big = true; |
| sog_yang | 5:6a1155885fc9 | 57 | byte[(i&0xF) * 3] = ' '; |
| sog_yang | 5:6a1155885fc9 | 58 | text[(i&0xF)] = ' '; |
| sog_yang | 5:6a1155885fc9 | 59 | } |
| sog_yang | 5:6a1155885fc9 | 60 | unsigned char value = buffer[i]; |
| sog_yang | 5:6a1155885fc9 | 61 | text[(i&0xF) + 0 + big] = (value < 0x20 || value > 0x7F)? '.': value; |
| sog_yang | 5:6a1155885fc9 | 62 | text[(i&0xF) + 1 + big] = '\0'; |
| sog_yang | 5:6a1155885fc9 | 63 | value = (buffer[i] &0xF0) >> 4; |
| sog_yang | 5:6a1155885fc9 | 64 | byte[(i&0xF) * 3 + 0 + big] = (value < 0xA)? (value + 0x30): (value + 0x37); |
| sog_yang | 5:6a1155885fc9 | 65 | value = (buffer[i] &0x0F); |
| sog_yang | 5:6a1155885fc9 | 66 | byte[(i&0xF) * 3 + 1 + big] = (value < 0xA)? (value + 0x30): (value + 0x37); |
| sog_yang | 5:6a1155885fc9 | 67 | byte[(i&0xF) * 3 + 2 + big] = ' '; |
| sog_yang | 5:6a1155885fc9 | 68 | byte[(i&0xF) * 3 + 3 + big] = '\0'; |
| sog_yang | 5:6a1155885fc9 | 69 | } |
| sog_yang | 5:6a1155885fc9 | 70 | if(byte[0]) { |
| sog_yang | 5:6a1155885fc9 | 71 | uart.printf("%04X: %-49s: %-20s\n", (i&~0xF), byte, text); |
| sog_yang | 5:6a1155885fc9 | 72 | } |
| sog_yang | 5:6a1155885fc9 | 73 | uart.printf("\n"); |
| sog_yang | 5:6a1155885fc9 | 74 | } |
| sog_yang | 5:6a1155885fc9 | 75 | |
| tsungta | 7:0ea5460e0de3 | 76 | void scanCallback(tstrM2mWifiscanResult result) |
| tsungta | 7:0ea5460e0de3 | 77 | { |
| tsungta | 7:0ea5460e0de3 | 78 | printf("SSID: %s \n", result.au8SSID); |
| tsungta | 7:0ea5460e0de3 | 79 | printf("RSSI: %i \n", result.s8rssi); |
| tsungta | 7:0ea5460e0de3 | 80 | } |
| tsungta | 7:0ea5460e0de3 | 81 | |
| tsungta | 0:b7415ae44dac | 82 | int main() { |
| sog_yang | 6:bce37a45b9cb | 83 | uart.baud(115200); |
| tsungta | 0:b7415ae44dac | 84 | uart.printf("START!"); |
| sog_yang | 5:6a1155885fc9 | 85 | device.format(8,0); |
| sog_yang | 5:6a1155885fc9 | 86 | myled = 1; |
| tsungta | 7:0ea5460e0de3 | 87 | |
| tsungta | 7:0ea5460e0de3 | 88 | EthernetInterface eth; |
| tsungta | 7:0ea5460e0de3 | 89 | WIFIDevice wifi; |
| tsungta | 7:0ea5460e0de3 | 90 | |
| tsungta | 7:0ea5460e0de3 | 91 | eth.init(); |
| tsungta | 7:0ea5460e0de3 | 92 | |
| tsungta | 7:0ea5460e0de3 | 93 | wifi.apScan(scanCallback); |
| tsungta | 7:0ea5460e0de3 | 94 | |
| tsungta | 7:0ea5460e0de3 | 95 | wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TWCYNPC0209_Mac mini", "mayday55555"); |
| tsungta | 2:c520d7c7739d | 96 | |
| tsungta | 7:0ea5460e0de3 | 97 | eth.connect(); |
| tsungta | 7:0ea5460e0de3 | 98 | |
| tsungta | 7:0ea5460e0de3 | 99 | if(wifi.is_AP_connected()) |
| tsungta | 7:0ea5460e0de3 | 100 | uart.printf("Connect Success! \n"); |
| tsungta | 7:0ea5460e0de3 | 101 | else |
| tsungta | 7:0ea5460e0de3 | 102 | uart.printf("Connect Fail! \n"); |
| tsungta | 7:0ea5460e0de3 | 103 | |
| tsungta | 7:0ea5460e0de3 | 104 | TCPSocketConnection tcp; |
| tsungta | 7:0ea5460e0de3 | 105 | tcp.set_blocking(false, 1500); |
| tsungta | 7:0ea5460e0de3 | 106 | tcp.connect(TCP_SERVER_ADDRESS, TCP_SERVER_PORT); |
| tsungta | 7:0ea5460e0de3 | 107 | |
| tsungta | 7:0ea5460e0de3 | 108 | char out_buffer[] = "Hello World"; |
| tsungta | 7:0ea5460e0de3 | 109 | |
| tsungta | 0:b7415ae44dac | 110 | while(1) { |
| sog_yang | 5:6a1155885fc9 | 111 | |
| sog_yang | 5:6a1155885fc9 | 112 | uart.printf("Add: %p \r\n", device.read()); |
| sog_yang | 6:bce37a45b9cb | 113 | //uart.printf("%c \r\n", device.read()); |
| sog_yang | 5:6a1155885fc9 | 114 | |
| sog_yang | 5:6a1155885fc9 | 115 | while (!device.readable()) { |
| sog_yang | 5:6a1155885fc9 | 116 | __WFE(); |
| sog_yang | 5:6a1155885fc9 | 117 | } |
| tsungta | 7:0ea5460e0de3 | 118 | hexview((const char *)0x20003384, 224); |
| sog_yang | 5:6a1155885fc9 | 119 | //hexview((const char *)ptr, 224); |
| sog_yang | 5:6a1155885fc9 | 120 | //uart.printf("%c\r\n",m_rx_buf[3]); |
| sog_yang | 5:6a1155885fc9 | 121 | |
| tsungta | 7:0ea5460e0de3 | 122 | tcp.send(out_buffer, strlen(out_buffer)); |
| tsungta | 7:0ea5460e0de3 | 123 | |
| sog_yang | 5:6a1155885fc9 | 124 | myled = !myled; |
| sog_yang | 5:6a1155885fc9 | 125 | wait_ms(200); |
| tsungta | 0:b7415ae44dac | 126 | } |
| tsungta | 0:b7415ae44dac | 127 | } |
