Project based on platform nucleo F401RE, WIZnet5500 shield and stepper motor driver.
Dependencies: WIZnet_Library mbed
main.cpp@0:379817511db0, 2016-01-06 (annotated)
- Committer:
- Whisper_84
- Date:
- Wed Jan 06 09:11:35 2016 +0000
- Revision:
- 0:379817511db0
Create project for test Ethernet sield and stepper motor driver.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Whisper_84 | 0:379817511db0 | 1 | #include "mbed.h" |
Whisper_84 | 0:379817511db0 | 2 | #include "WIZnetInterface.h" |
Whisper_84 | 0:379817511db0 | 3 | |
Whisper_84 | 0:379817511db0 | 4 | Serial uart(USBTX, USBRX); // UART2 Debug PA2, PA3 |
Whisper_84 | 0:379817511db0 | 5 | SPI spi(PA_7, PA_6, PA_5); // MOSI, MISO, SCLK |
Whisper_84 | 0:379817511db0 | 6 | WIZnetInterface eth(&spi, PB_6, PA_9); // SPI, CS, reset |
Whisper_84 | 0:379817511db0 | 7 | |
Whisper_84 | 0:379817511db0 | 8 | unsigned char MAC_Addr[6] = {0x00,0x01,0x00,0x00,0x00,0x00}; |
Whisper_84 | 0:379817511db0 | 9 | UDPSocket socket; |
Whisper_84 | 0:379817511db0 | 10 | Endpoint client; |
Whisper_84 | 0:379817511db0 | 11 | |
Whisper_84 | 0:379817511db0 | 12 | |
Whisper_84 | 0:379817511db0 | 13 | PwmOut stepper(PB_4); // Тактовый сигнал на ШД |
Whisper_84 | 0:379817511db0 | 14 | DigitalOut enable(PB_3); // Включение ШД |
Whisper_84 | 0:379817511db0 | 15 | DigitalOut direction(PB_5); // Направление вращения ШД |
Whisper_84 | 0:379817511db0 | 16 | |
Whisper_84 | 0:379817511db0 | 17 | |
Whisper_84 | 0:379817511db0 | 18 | #define MSG_BUF_LEN 50 |
Whisper_84 | 0:379817511db0 | 19 | char msg[MSG_BUF_LEN]; |
Whisper_84 | 0:379817511db0 | 20 | char *msgPtr = msg; |
Whisper_84 | 0:379817511db0 | 21 | |
Whisper_84 | 0:379817511db0 | 22 | int main() { |
Whisper_84 | 0:379817511db0 | 23 | |
Whisper_84 | 0:379817511db0 | 24 | int returnCode = 0; |
Whisper_84 | 0:379817511db0 | 25 | char *ip; |
Whisper_84 | 0:379817511db0 | 26 | int udpPackLength = 0; |
Whisper_84 | 0:379817511db0 | 27 | |
Whisper_84 | 0:379817511db0 | 28 | /* Используем UART для вывода отладочной информации */ |
Whisper_84 | 0:379817511db0 | 29 | uart.baud(115200); |
Whisper_84 | 0:379817511db0 | 30 | |
Whisper_84 | 0:379817511db0 | 31 | /* Настройки SPI для Ethernet shield */ |
Whisper_84 | 0:379817511db0 | 32 | spi.format(8,0); |
Whisper_84 | 0:379817511db0 | 33 | spi.frequency(8000000); |
Whisper_84 | 0:379817511db0 | 34 | wait(1); |
Whisper_84 | 0:379817511db0 | 35 | |
Whisper_84 | 0:379817511db0 | 36 | /* Инициализация Ethernet, попытка получить IP по DHCP */ |
Whisper_84 | 0:379817511db0 | 37 | printf("Initializing Ethernet\r\n"); |
Whisper_84 | 0:379817511db0 | 38 | returnCode = eth.init(MAC_Addr); |
Whisper_84 | 0:379817511db0 | 39 | if ( returnCode == 0 ) |
Whisper_84 | 0:379817511db0 | 40 | { |
Whisper_84 | 0:379817511db0 | 41 | printf(" - Ethernet ready\r\n"); |
Whisper_84 | 0:379817511db0 | 42 | printf("Ethernet.connecting...\r\n"); |
Whisper_84 | 0:379817511db0 | 43 | returnCode = eth.connect(); |
Whisper_84 | 0:379817511db0 | 44 | printf(" - connecting returned %d \r\n", returnCode); |
Whisper_84 | 0:379817511db0 | 45 | printf("Trying to get IP address..\r\n"); |
Whisper_84 | 0:379817511db0 | 46 | ip = eth.getIPAddress(); |
Whisper_84 | 0:379817511db0 | 47 | printf(" - IP address: %s\r\n", ip); |
Whisper_84 | 0:379817511db0 | 48 | } |
Whisper_84 | 0:379817511db0 | 49 | else |
Whisper_84 | 0:379817511db0 | 50 | printf(" - Could not initialize Ethernet\r\n"); |
Whisper_84 | 0:379817511db0 | 51 | |
Whisper_84 | 0:379817511db0 | 52 | /* В модуле Socket.cpp в конструкторе можно настроить режзим работы сокета (блокирующий/неблокирующий) |
Whisper_84 | 0:379817511db0 | 53 | и настроить timeout ожидания. Сейчас настроил неблокирующий режим, timeout = 300 ms */ |
Whisper_84 | 0:379817511db0 | 54 | socket.init(); |
Whisper_84 | 0:379817511db0 | 55 | socket.bind(80); |
Whisper_84 | 0:379817511db0 | 56 | |
Whisper_84 | 0:379817511db0 | 57 | |
Whisper_84 | 0:379817511db0 | 58 | enable = 1; |
Whisper_84 | 0:379817511db0 | 59 | stepper.period(0.005f); |
Whisper_84 | 0:379817511db0 | 60 | stepper.write(0.50f); |
Whisper_84 | 0:379817511db0 | 61 | |
Whisper_84 | 0:379817511db0 | 62 | |
Whisper_84 | 0:379817511db0 | 63 | while(1) |
Whisper_84 | 0:379817511db0 | 64 | { |
Whisper_84 | 0:379817511db0 | 65 | udpPackLength = socket.receiveFrom(client, msg, MSG_BUF_LEN); |
Whisper_84 | 0:379817511db0 | 66 | |
Whisper_84 | 0:379817511db0 | 67 | if (udpPackLength != -1) |
Whisper_84 | 0:379817511db0 | 68 | { |
Whisper_84 | 0:379817511db0 | 69 | printf("UDP received data: "); |
Whisper_84 | 0:379817511db0 | 70 | printf(msg); |
Whisper_84 | 0:379817511db0 | 71 | printf("\r\n"); |
Whisper_84 | 0:379817511db0 | 72 | socket.sendTo(client, msg, udpPackLength); // Эхо |
Whisper_84 | 0:379817511db0 | 73 | memset(msg, 0, MSG_BUF_LEN); |
Whisper_84 | 0:379817511db0 | 74 | } |
Whisper_84 | 0:379817511db0 | 75 | |
Whisper_84 | 0:379817511db0 | 76 | wait_ms(1000); |
Whisper_84 | 0:379817511db0 | 77 | } |
Whisper_84 | 0:379817511db0 | 78 | } |
Whisper_84 | 0:379817511db0 | 79 | |
Whisper_84 | 0:379817511db0 | 80 |