The driver for the ESP32 WiFi module
The ESP32 WiFi driver for Mbed OS
The Mbed OS driver for the ESP32 WiFi module.
Firmware version
How to write mbed-os compatible firmware : https://github.com/d-kato/GR-Boards_ESP32_Serial_Bridge
Restrictions
- Setting up an UDP server is not possible
- The serial port does not have hardware flow control enabled. The AT command set does not either have a way to limit the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable. An application should be able to read fast enough to stay ahead of the network. This affects mostly the TCP protocol where data would be lost with no notification. On UDP, this would lead to only packet losses which the higher layer protocol should recover from.
Note
ESP32Stack.h@0:92d12d355ba9, 2018-06-29 (annotated)
- Committer:
- dkato
- Date:
- Fri Jun 29 06:17:38 2018 +0000
- Revision:
- 0:92d12d355ba9
- Child:
- 1:5d78eedef723
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:92d12d355ba9 | 1 | /* ESP32 implementation of NetworkInterfaceAPI |
dkato | 0:92d12d355ba9 | 2 | * Copyright (c) 2015 ARM Limited |
dkato | 0:92d12d355ba9 | 3 | * Copyright (c) 2017 Renesas Electronics Corporation |
dkato | 0:92d12d355ba9 | 4 | * |
dkato | 0:92d12d355ba9 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:92d12d355ba9 | 6 | * you may not use this file except in compliance with the License. |
dkato | 0:92d12d355ba9 | 7 | * You may obtain a copy of the License at |
dkato | 0:92d12d355ba9 | 8 | * |
dkato | 0:92d12d355ba9 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:92d12d355ba9 | 10 | * |
dkato | 0:92d12d355ba9 | 11 | * Unless required by applicable law or agreed to in writing, software |
dkato | 0:92d12d355ba9 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:92d12d355ba9 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:92d12d355ba9 | 14 | * See the License for the specific language governing permissions and |
dkato | 0:92d12d355ba9 | 15 | * limitations under the License. |
dkato | 0:92d12d355ba9 | 16 | */ |
dkato | 0:92d12d355ba9 | 17 | |
dkato | 0:92d12d355ba9 | 18 | #ifndef ESP32_STACK_H |
dkato | 0:92d12d355ba9 | 19 | #define ESP32_STACK_H |
dkato | 0:92d12d355ba9 | 20 | |
dkato | 0:92d12d355ba9 | 21 | #include "mbed.h" |
dkato | 0:92d12d355ba9 | 22 | #include "ESP32.h" |
dkato | 0:92d12d355ba9 | 23 | |
dkato | 0:92d12d355ba9 | 24 | /** ESP32Stack class |
dkato | 0:92d12d355ba9 | 25 | * Implementation of the NetworkStack for the ESP32 |
dkato | 0:92d12d355ba9 | 26 | */ |
dkato | 0:92d12d355ba9 | 27 | class ESP32Stack : public NetworkStack |
dkato | 0:92d12d355ba9 | 28 | { |
dkato | 0:92d12d355ba9 | 29 | protected: |
dkato | 0:92d12d355ba9 | 30 | /** ESP32Stack lifetime |
dkato | 0:92d12d355ba9 | 31 | * @param en EN pin |
dkato | 0:92d12d355ba9 | 32 | * @param io0 IO0 pin |
dkato | 0:92d12d355ba9 | 33 | * @param tx TX pin |
dkato | 0:92d12d355ba9 | 34 | * @param rx RX pin |
dkato | 0:92d12d355ba9 | 35 | * @param debug Enable debugging |
dkato | 0:92d12d355ba9 | 36 | * @param baudrate The baudrate of the serial port. |
dkato | 0:92d12d355ba9 | 37 | */ |
dkato | 0:92d12d355ba9 | 38 | ESP32Stack(PinName en, PinName io0, PinName tx, PinName rx, bool debug, int baudrate); |
dkato | 0:92d12d355ba9 | 39 | |
dkato | 0:92d12d355ba9 | 40 | protected: |
dkato | 0:92d12d355ba9 | 41 | /** Open a socket |
dkato | 0:92d12d355ba9 | 42 | * @param handle Handle in which to store new socket |
dkato | 0:92d12d355ba9 | 43 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
dkato | 0:92d12d355ba9 | 44 | * @return 0 on success, negative on failure |
dkato | 0:92d12d355ba9 | 45 | */ |
dkato | 0:92d12d355ba9 | 46 | virtual int socket_open(void **handle, nsapi_protocol_t proto); |
dkato | 0:92d12d355ba9 | 47 | |
dkato | 0:92d12d355ba9 | 48 | /** Close the socket |
dkato | 0:92d12d355ba9 | 49 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 50 | * @return 0 on success, negative on failure |
dkato | 0:92d12d355ba9 | 51 | * @note On failure, any memory associated with the socket must still |
dkato | 0:92d12d355ba9 | 52 | * be cleaned up |
dkato | 0:92d12d355ba9 | 53 | */ |
dkato | 0:92d12d355ba9 | 54 | virtual int socket_close(void *handle); |
dkato | 0:92d12d355ba9 | 55 | |
dkato | 0:92d12d355ba9 | 56 | /** Bind a server socket to a specific port |
dkato | 0:92d12d355ba9 | 57 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 58 | * @param address Local address to listen for incoming connections on |
dkato | 0:92d12d355ba9 | 59 | * @return 0 on success, negative on failure. |
dkato | 0:92d12d355ba9 | 60 | */ |
dkato | 0:92d12d355ba9 | 61 | virtual int socket_bind(void *handle, const SocketAddress &address); |
dkato | 0:92d12d355ba9 | 62 | |
dkato | 0:92d12d355ba9 | 63 | /** Start listening for incoming connections |
dkato | 0:92d12d355ba9 | 64 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 65 | * @param backlog Number of pending connections that can be queued up at any |
dkato | 0:92d12d355ba9 | 66 | * one time [Default: 1] |
dkato | 0:92d12d355ba9 | 67 | * @return 0 on success, negative on failure |
dkato | 0:92d12d355ba9 | 68 | */ |
dkato | 0:92d12d355ba9 | 69 | virtual int socket_listen(void *handle, int backlog); |
dkato | 0:92d12d355ba9 | 70 | |
dkato | 0:92d12d355ba9 | 71 | /** Connects this TCP socket to the server |
dkato | 0:92d12d355ba9 | 72 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 73 | * @param address SocketAddress to connect to |
dkato | 0:92d12d355ba9 | 74 | * @return 0 on success, negative on failure |
dkato | 0:92d12d355ba9 | 75 | */ |
dkato | 0:92d12d355ba9 | 76 | virtual int socket_connect(void *handle, const SocketAddress &address); |
dkato | 0:92d12d355ba9 | 77 | |
dkato | 0:92d12d355ba9 | 78 | /** Accept a new connection. |
dkato | 0:92d12d355ba9 | 79 | * @param handle Handle in which to store new socket |
dkato | 0:92d12d355ba9 | 80 | * @param server Socket handle to server to accept from |
dkato | 0:92d12d355ba9 | 81 | * @return 0 on success, negative on failure |
dkato | 0:92d12d355ba9 | 82 | * @note This call is not-blocking, if this call would block, must |
dkato | 0:92d12d355ba9 | 83 | * immediately return NSAPI_ERROR_WOULD_WAIT |
dkato | 0:92d12d355ba9 | 84 | */ |
dkato | 0:92d12d355ba9 | 85 | virtual int socket_accept(void *handle, void **socket, SocketAddress *address); |
dkato | 0:92d12d355ba9 | 86 | |
dkato | 0:92d12d355ba9 | 87 | /** Send data to the remote host |
dkato | 0:92d12d355ba9 | 88 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 89 | * @param data The buffer to send to the host |
dkato | 0:92d12d355ba9 | 90 | * @param size The length of the buffer to send |
dkato | 0:92d12d355ba9 | 91 | * @return Number of written bytes on success, negative on failure |
dkato | 0:92d12d355ba9 | 92 | * @note This call is not-blocking, if this call would block, must |
dkato | 0:92d12d355ba9 | 93 | * immediately return NSAPI_ERROR_WOULD_WAIT |
dkato | 0:92d12d355ba9 | 94 | */ |
dkato | 0:92d12d355ba9 | 95 | virtual int socket_send(void *handle, const void *data, unsigned size); |
dkato | 0:92d12d355ba9 | 96 | |
dkato | 0:92d12d355ba9 | 97 | /** Receive data from the remote host |
dkato | 0:92d12d355ba9 | 98 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 99 | * @param data The buffer in which to store the data received from the host |
dkato | 0:92d12d355ba9 | 100 | * @param size The maximum length of the buffer |
dkato | 0:92d12d355ba9 | 101 | * @return Number of received bytes on success, negative on failure |
dkato | 0:92d12d355ba9 | 102 | * @note This call is not-blocking, if this call would block, must |
dkato | 0:92d12d355ba9 | 103 | * immediately return NSAPI_ERROR_WOULD_WAIT |
dkato | 0:92d12d355ba9 | 104 | */ |
dkato | 0:92d12d355ba9 | 105 | virtual int socket_recv(void *handle, void *data, unsigned size); |
dkato | 0:92d12d355ba9 | 106 | |
dkato | 0:92d12d355ba9 | 107 | /** Send a packet to a remote endpoint |
dkato | 0:92d12d355ba9 | 108 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 109 | * @param address The remote SocketAddress |
dkato | 0:92d12d355ba9 | 110 | * @param data The packet to be sent |
dkato | 0:92d12d355ba9 | 111 | * @param size The length of the packet to be sent |
dkato | 0:92d12d355ba9 | 112 | * @return The number of written bytes on success, negative on failure |
dkato | 0:92d12d355ba9 | 113 | * @note This call is not-blocking, if this call would block, must |
dkato | 0:92d12d355ba9 | 114 | * immediately return NSAPI_ERROR_WOULD_WAIT |
dkato | 0:92d12d355ba9 | 115 | */ |
dkato | 0:92d12d355ba9 | 116 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
dkato | 0:92d12d355ba9 | 117 | |
dkato | 0:92d12d355ba9 | 118 | /** Receive a packet from a remote endpoint |
dkato | 0:92d12d355ba9 | 119 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 120 | * @param address Destination for the remote SocketAddress or null |
dkato | 0:92d12d355ba9 | 121 | * @param buffer The buffer for storing the incoming packet data |
dkato | 0:92d12d355ba9 | 122 | * If a packet is too long to fit in the supplied buffer, |
dkato | 0:92d12d355ba9 | 123 | * excess bytes are discarded |
dkato | 0:92d12d355ba9 | 124 | * @param size The length of the buffer |
dkato | 0:92d12d355ba9 | 125 | * @return The number of received bytes on success, negative on failure |
dkato | 0:92d12d355ba9 | 126 | * @note This call is not-blocking, if this call would block, must |
dkato | 0:92d12d355ba9 | 127 | * immediately return NSAPI_ERROR_WOULD_WAIT |
dkato | 0:92d12d355ba9 | 128 | */ |
dkato | 0:92d12d355ba9 | 129 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
dkato | 0:92d12d355ba9 | 130 | |
dkato | 0:92d12d355ba9 | 131 | /** Register a callback on state change of the socket |
dkato | 0:92d12d355ba9 | 132 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 133 | * @param callback Function to call on state change |
dkato | 0:92d12d355ba9 | 134 | * @param data Argument to pass to callback |
dkato | 0:92d12d355ba9 | 135 | * @note Callback may be called in an interrupt context. |
dkato | 0:92d12d355ba9 | 136 | */ |
dkato | 0:92d12d355ba9 | 137 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
dkato | 0:92d12d355ba9 | 138 | |
dkato | 0:92d12d355ba9 | 139 | /** Set stack-specific socket options |
dkato | 0:92d12d355ba9 | 140 | * The setsockopt allow an application to pass stack-specific hints |
dkato | 0:92d12d355ba9 | 141 | * to the underlying stack. For unsupported options, |
dkato | 0:92d12d355ba9 | 142 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. |
dkato | 0:92d12d355ba9 | 143 | * |
dkato | 0:92d12d355ba9 | 144 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 145 | * @param level Stack-specific protocol level |
dkato | 0:92d12d355ba9 | 146 | * @param optname Stack-specific option identifier |
dkato | 0:92d12d355ba9 | 147 | * @param optval Option value |
dkato | 0:92d12d355ba9 | 148 | * @param optlen Length of the option value |
dkato | 0:92d12d355ba9 | 149 | * @return 0 on success, negative error code on failure |
dkato | 0:92d12d355ba9 | 150 | */ |
dkato | 0:92d12d355ba9 | 151 | virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, |
dkato | 0:92d12d355ba9 | 152 | int optname, const void *optval, unsigned optlen); |
dkato | 0:92d12d355ba9 | 153 | |
dkato | 0:92d12d355ba9 | 154 | /** Get stack-specific socket options |
dkato | 0:92d12d355ba9 | 155 | * The getstackopt allow an application to retrieve stack-specific hints |
dkato | 0:92d12d355ba9 | 156 | * from the underlying stack. For unsupported options, |
dkato | 0:92d12d355ba9 | 157 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. |
dkato | 0:92d12d355ba9 | 158 | * |
dkato | 0:92d12d355ba9 | 159 | * @param handle Socket handle |
dkato | 0:92d12d355ba9 | 160 | * @param level Stack-specific protocol level |
dkato | 0:92d12d355ba9 | 161 | * @param optname Stack-specific option identifier |
dkato | 0:92d12d355ba9 | 162 | * @param optval Destination for option value |
dkato | 0:92d12d355ba9 | 163 | * @param optlen Length of the option value |
dkato | 0:92d12d355ba9 | 164 | * @return 0 on success, negative error code on failure |
dkato | 0:92d12d355ba9 | 165 | */ |
dkato | 0:92d12d355ba9 | 166 | virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, |
dkato | 0:92d12d355ba9 | 167 | int optname, void *optval, unsigned *optlen); |
dkato | 0:92d12d355ba9 | 168 | |
dkato | 0:92d12d355ba9 | 169 | protected: |
dkato | 0:92d12d355ba9 | 170 | ESP32 *_esp; |
dkato | 0:92d12d355ba9 | 171 | uint16_t _local_ports[ESP32::SOCKET_COUNT]; |
dkato | 0:92d12d355ba9 | 172 | }; |
dkato | 0:92d12d355ba9 | 173 | |
dkato | 0:92d12d355ba9 | 174 | #endif |