TM STM32F4xx Libraries  v1.0.0
Libraries for STM32F4xx devices from Tilen Majerle
tm_stm32f4_ethernet.h
1 
31 #ifndef TM_ETHERNET_H
32 #define TM_ETHERNET_H 110
33 
34 /* C++ detection */
35 #ifdef __cplusplus
36 extern C {
37 #endif
38 
188 #include "stm32f4xx.h"
189 #include "stm32f4xx_rcc.h"
190 #include "stm32f4xx_gpio.h"
191 #include "defines.h"
192 #include "attributes.h"
193 #include "tm_stm32f4_delay.h"
194 #include "tm_stm32f4_gpio.h"
195 
196 /* Ethernet based includes */
197 #include "stm32f4x7_eth.h"
198 #include "stm32f4x7_eth_bsp.h"
199 #include "netconf.h"
200 #include "tcp_echoclient.h"
201 #include "lwip/ip_addr.h"
202 #include "lwip/dns.h"
203 #include "lwip/tcp.h"
204 
205 /* C based includes */
206 #include "string.h"
207 #include "stdio.h"
208 
219 #ifndef ETHERNET_PHY
220 #define ETHERNET_PHY 0
221 #endif
222 
226 #ifndef LINK_TIMER_INTERVAL
227 #define LINK_TIMER_INTERVAL 1000
228 #endif
229 
233 #ifndef ETHERNET_SERVER_PORT
234 #define ETHERNET_SERVER_PORT 80
235 #endif
236 
237 /* Include server based options */
238 #include "httpd.h"
239 #include "fs.h"
240 
241 /* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
242 /* In case you want to use custom MAC, use parameter in init function */
247 #ifndef MAC_ADDR0
248 #define MAC_ADDR0 0x06
249 #define MAC_ADDR1 0x05
250 #define MAC_ADDR2 0x04
251 #define MAC_ADDR3 0x03
252 #define MAC_ADDR4 0x02
253 #define MAC_ADDR5 0x01
254 #endif
255 
256 /* Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
257 /* Used in case DHCP is not used or response failed */
258 /* In case you want to use custom IP, use parameter in init function */
264 #ifndef IP_ADDR0
265 #define IP_ADDR0 192
266 #define IP_ADDR1 168
267 #define IP_ADDR2 0
268 #define IP_ADDR3 120
269 #endif
270 
275 #ifndef NETMASK_ADDR0
276 #define NETMASK_ADDR0 255
277 #define NETMASK_ADDR1 255
278 #define NETMASK_ADDR2 255
279 #define NETMASK_ADDR3 0
280 #endif
281 
286 #ifndef GW_ADDR0
287 #define GW_ADDR0 192
288 #define GW_ADDR1 168
289 #define GW_ADDR2 0
290 #define GW_ADDR3 1
291 #endif
292 
296 #ifndef ETHERNET_HOSTNAME_DHCP
297 #define ETHERNET_HOSTNAME_DHCP "TM-ETH-Device"
298 #endif
299 
300 /* RMII mode by default */
301 #if !defined(ETHERNET_MII_MODE) && !defined(ETHERNET_RMII_MODE)
302  #define ETHERNET_RMII_MODE
303 #endif
304 
305 /* Buffer size for ethernet headers when requesting as client */
306 /* Used for headers after you are connected to server, HTTP headers */
307 /* Ex: GET / HTTP/1.1\r\n...etc */
313 #ifndef ETHERNET_MAX_HEADER_SIZE
314 #define ETHERNET_MAX_HEADER_SIZE 1024
315 #endif
316 
321 #ifndef ETHERNET_MAX_CONNECTION_NAME
322 #define ETHERNET_MAX_CONNECTION_NAME 100
323 #endif
324 
328 #ifndef ETHERNET_MAX_OPEN_FILES
329 #define ETHERNET_MAX_OPEN_FILES 10
330 #endif
331 
335 #ifndef ETHERNET_SSI_MAX_TAG_LEN
336 #define ETHERNET_SSI_MAX_TAG_LEN 32
337 #endif
338 
343 #ifndef ETHERNET_SSI_MAX_TAG_INSERT_LEN
344 #define ETHERNET_SSI_MAX_TAG_INSERT_LEN 512
345 #endif
346 
363 /*
364  * @brief Internal structure which should not be used by user
365  * @note For private use
366  */
367 typedef struct {
368  uint8_t ip_addr[4];
369  uint8_t mac_addr[6];
370  uint8_t netmask[4];
371  uint8_t gateway[4];
372  uint8_t staticip;
373  uint8_t ip_is_set;
374  uint8_t link_is_up;
375  uint32_t ClientSuccessfullConnections, ClientConnections;
376  uint64_t Client_TX_Bytes, Client_RX_Bytes;
377  uint32_t Server_Connections;
378  uint64_t Server_TX_Bytes, Server_RX_Bytes;
379  uint8_t server_active;
380  uint16_t server_port;
381  uint8_t speed_100m;
382  uint8_t full_duplex;
383  uint8_t timeout_detected;
384 } TM_ETHERNET_t;
385 extern TM_ETHERNET_t TM_ETHERNET;
386 
391 typedef enum {
392  CLIENT_NOT_CONNECTED = 0,
393  CLIENT_CONNECTED,
394  CLIENT_RECEIVED,
395  CLIENT_CLOSING,
396 } client_states;
397 
405 typedef struct {
407  uint8_t active;
408  uint8_t ip_addr[4];
409  uint16_t port;
410  uint8_t headers_done;
419  /* Private use */
420  struct tcp_pcb* pcb;
422  struct pbuf *p_tx;
424 
430 typedef struct {
431  void* connection;
432  const char* uri;
433  const char* http_request;
434  uint16_t http_request_len;
436  char* response_uri;
437  uint16_t response_uri_len;
439 
443 typedef enum {
451 
455 typedef const char * TM_ETHERNET_SSI_t;
456 
466 typedef tCGI TM_ETHERNET_CGI_t;
467 
507 TM_ETHERNET_Result_t TM_ETHERNET_Init(uint8_t* mac_addr, uint8_t* ip_addr, uint8_t* gateway, uint8_t* netmask);
508 
515 
523 
532 
540 #define TM_ETHERNET_IsIPStatic() (TM_ETHERNET.staticip)
541 
549 #define TM_ETHERNET_Is100M() (TM_ETHERNET.speed_100m)
550 
558 #define TM_ETHERNET_IsFullDuplex() (TM_ETHERNET.full_duplex)
559 
565 #define TM_ETHERNET_GetLocalIP(x) (((x) >= 0 && (x) < 4) ? TM_ETHERNET.ip_addr[(x)] : 0)
566 
572 #define TM_ETHERNET_GetMACAddr(x) (((x) >= 0 && (x) < 6) ? TM_ETHERNET.mac_addr[(x)] : 0)
573 
579 #define TM_ETHERNET_GetGateway(x) (((x) >= 0 && (x) < 4) ? TM_ETHERNET.gateway[(x)] : 0)
580 
586 #define TM_ETHERNET_GetNetmask(x) (((x) >= 0 && (x) < 4) ? TM_ETHERNET.netmask[(x)] : 0)
587 
596 #define TM_ETHERNET_SetTimeoutDetected() (TM_ETHERNET.timeout_detected = 1)
597 
616 TM_ETHERNET_Result_t TM_ETHERNETSERVER_Enable(uint16_t server_port);
617 
624 
632 #define TM_ETHERNETSERVER_Enabled() (TM_ETHERNET.server_active)
633 
639 #define TM_ETHERNETSERVER_GetPortNumber() (TM_ETHERNET.server_port)
640 
646 #define TM_ETHERNETSERVER_GetTXBytes() (TM_ETHERNET.Server_TX_Bytes)
647 
653 #define TM_ETHERNETSERVER_GetRXBytes() (TM_ETHERNET.Server_RX_Bytes)
654 
660 #define TM_ETHERNETSERVER_GetConnectionsCount() (TM_ETHERNET.Server_Connections)
661 
675 TM_ETHERNET_Result_t TM_ETHERNETSERVER_SetSSITags(TM_ETHERNET_SSI_t* tags, uint16_t number_of_tags);
676 
691 TM_ETHERNET_Result_t TM_ETHERNETSERVER_SetCGIHandlers(const TM_ETHERNET_CGI_t* cgis, uint16_t number_of_handlers);
692 
717 TM_ETHERNET_Result_t TM_ETHERNETCLIENT_Connect(char* conn_name, uint8_t ip1, uint8_t ip2, uint8_t ip3, uint8_t ip4, uint16_t port, void* user_parameters);
718 
724 #define TM_ETHERNETCLIENT_GetTXBytes() (TM_ETHERNET.Client_TX_Bytes)
725 
731 #define TM_ETHERNETCLIENT_GetRXBytes() (TM_ETHERNET.Client_RX_Bytes)
732 
738 #define TM_ETHERNETCLIENT_GetConnectionsCount() (TM_ETHERNET.ClientConnections)
739 
745 #define TM_ETHERNETCLIENT_GetSuccessfullConnectionsCount() (TM_ETHERNET.ClientSuccessfullConnections)
746 
765 
788 void TM_ETHERNET_IPIsSetCallback(uint8_t ip_addr1, uint8_t ip_addr2, uint8_t ip_addr3, uint8_t ip_addr4, uint8_t dhcp);
789 
797 
806 
815 
828 
854 uint16_t TM_ETHERNETCLIENT_CreateHeadersCallback(TM_TCPCLIENT_t* connection, char* buffer, uint16_t buffer_length);
855 
869 void TM_ETHERNETCLIENT_ReceiveDataCallback(TM_TCPCLIENT_t* connection, uint8_t* buffer, uint16_t buffer_length, uint16_t total_length);
870 
880 
892 void TM_ETHERNETCLIENT_ConnectionClosedCallback(TM_TCPCLIENT_t* connection, uint8_t success);
893 
905 
915 
930 void TM_ETHERNETDNS_FoundCallback(char* host_name, uint8_t ip_addr1, uint8_t ip_addr2, uint8_t ip_addr3, uint8_t ip_addr4);
931 
945 void TM_ETHERNETDNS_ErrorCallback(char* host_name);
946 
957 uint16_t TM_ETHERNETSERVER_SSICallback(int iIndex, char *pcInsert, int iInsertLen);
958 
977 uint8_t TM_ETHERNETSERVER_ClientConnectedCallback(struct tcp_pcb *pcb);
978 
987 
1007 int TM_ETHERNETSERVER_OpenFileCallback(struct fs_file* file, const char *name);
1008 
1019 void TM_ETHERNETSERVER_CloseFileCallback(struct fs_file* file);
1020 
1033 int TM_ETHERNETSERVER_ReadFileCallback(struct fs_file* file, char* buffer, int count);
1034 
1057 
1078 
1092 void TM_ETHERNETSERVER_PostRequestEndCallback(void* connection, char* response_uri, u16_t response_uri_len);
1093 
1108 void TM_ETHERNETPHY_CustomOptions(uint32_t PHYAddress);
1109 
1120 uint8_t TM_ETHERNET_InitPinsCallback(void);
1121 
1138 /* Private functions which should not be called from user */
1139 void TM_ETHERNET_INT_CustomOptions(uint32_t PHYAddress);
1140 void TM_ETHERNET_INT_SetIPAddress(uint8_t ip_addr1, uint8_t ip_addr2, uint8_t ip_addr3, uint8_t ip_addr4, uint8_t dhcp);
1141 void TM_ETHERNET_INT_LinkIsDownCallback(void);
1142 void TM_ETHERNET_INT_LinkIsUpCallback(void);
1143 
1144 /* C++ detection */
1145 #ifdef __cplusplus
1146 }
1147 #endif
1148 
1149 #endif
1150 
TM_ETHERNET_Result_t TM_ETHERNETDNS_GetHostByName(char *host_name)
Definition: tm_stm32f4_ethernet.h:444
int content_len
Definition: tm_stm32f4_ethernet.h:435
char * response_uri
Definition: tm_stm32f4_ethernet.h:436
Definition: tm_stm32f4_ethernet.h:446
void TM_ETHERNETDNS_ErrorCallback(char *host_name)
DNS error callback.
const char * http_request
Definition: tm_stm32f4_ethernet.h:433
Client structure, passed as first parameter in all client based callback functions.
Definition: tm_stm32f4_ethernet.h:405
void TM_ETHERNET_LinkIsDownCallback(void)
Link is down callback.
const char * TM_ETHERNET_SSI_t
User friendly typedef for SSI tags.
Definition: tm_stm32f4_ethernet.h:455
POST request structure.
Definition: tm_stm32f4_ethernet.h:430
uint16_t http_request_len
Definition: tm_stm32f4_ethernet.h:434
TM_ETHERNET_Result_t TM_ETHERNETSERVER_Disable(void)
Disables and stops LwIP stack from server functionality.
client_states state
Definition: tm_stm32f4_ethernet.h:421
struct pbuf * p_tx
Definition: tm_stm32f4_ethernet.h:422
TM_ETHERNET_Result_t
Result enumeration used in ethernet library.
Definition: tm_stm32f4_ethernet.h:443
uint16_t response_uri_len
Definition: tm_stm32f4_ethernet.h:437
int TM_ETHERNETSERVER_OpenFileCallback(struct fs_file *file, const char *name)
Open file callback.
void TM_ETHERNET_DHCPStartCallback(void)
DHCP started callback.
struct tcp_pcb * pcb
Definition: tm_stm32f4_ethernet.h:420
void * connection
Definition: tm_stm32f4_ethernet.h:431
uint8_t TM_ETHERNETSERVER_PostRequestBeginCallback(TM_ETHERNETPOST_t *params)
Post request begin callback.
void TM_ETHERNETCLIENT_ConnectionClosedCallback(TM_TCPCLIENT_t *connection, uint8_t success)
Client Connection closed callback.
TM_ETHERNET_Result_t TM_ETHERNETSERVER_SetSSITags(TM_ETHERNET_SSI_t *tags, uint16_t number_of_tags)
Set SSI tags, which will be used in your HTML files for displaying variables on website.
TM_ETHERNET_Result_t TM_ETHERNET_Init(uint8_t *mac_addr, uint8_t *ip_addr, uint8_t *gateway, uint8_t *netmask)
Initializes ethernet device and prepare device to work.
void TM_ETHERNETCLIENT_ConnectedCallback(TM_TCPCLIENT_t *connection)
Connected callback.
uint8_t TM_ETHERNETSERVER_ClientConnectedCallback(struct tcp_pcb *pcb)
Client connected callback Called when new client is connected to server.
TM_ETHERNET_Result_t TM_ETHERNET_TimeUpdate(uint16_t millis)
This function should be called in specific time period for LwIP stack using interrupts.
uint8_t active
Definition: tm_stm32f4_ethernet.h:407
void * user_parameters
Definition: tm_stm32f4_ethernet.h:416
void TM_ETHERNETSERVER_ClientDisconnectedCallback(void)
Client disconnected from server.
uint16_t TM_ETHERNETSERVER_SSICallback(int iIndex, char *pcInsert, int iInsertLen)
SSI callback for ethernet server.
uint8_t headers_done
Definition: tm_stm32f4_ethernet.h:410
uint8_t * active_connections_count
Definition: tm_stm32f4_ethernet.h:415
#define ETHERNET_MAX_CONNECTION_NAME
Max connection string name when connection as client.
Definition: tm_stm32f4_ethernet.h:322
void TM_ETHERNETCLIENT_ReceiveDataCallback(TM_TCPCLIENT_t *connection, uint8_t *buffer, uint16_t buffer_length, uint16_t total_length)
Receive data callback.
void TM_ETHERNET_LinkIsUpCallback(void)
Link is up callback.
TM_ETHERNET_Result_t TM_ETHERNETCLIENT_Connect(char *conn_name, uint8_t ip1, uint8_t ip2, uint8_t ip3, uint8_t ip4, uint16_t port, void *user_parameters)
Makes TCP CLIENT Connection to external server.
void TM_ETHERNETSERVER_CloseFileCallback(struct fs_file *file)
Close file callback.
void TM_ETHERNETSERVER_PostRequestEndCallback(void *connection, char *response_uri, u16_t response_uri_len)
Post request end callback.
void TM_ETHERNET_SystemResetCallback(void)
System reset callback.
Definition: tm_stm32f4_ethernet.h:448
uint8_t TM_ETHERNETSERVER_PostRequestReceiveDataCallback(TM_ETHERNETPOST_t *params, struct pbuf *p)
Post request receive data available callback.
void TM_ETHERNETCLIENT_ErrorCallback(TM_TCPCLIENT_t *connection)
Client Error callback.
const char * uri
Definition: tm_stm32f4_ethernet.h:432
void TM_ETHERNET_IPIsSetCallback(uint8_t ip_addr1, uint8_t ip_addr2, uint8_t ip_addr3, uint8_t ip_addr4, uint8_t dhcp)
IP is set callback. This function is called when IP is assigned to device.
client_states
Client protocol states.
Definition: tm_stm32f4_ethernet.h:391
Definition: tm_stm32f4_ethernet.h:445
void TM_ETHERNETCLIENT_ConnectionStartedCallback(TM_TCPCLIENT_t *connection)
Connection started callback.
int TM_ETHERNETSERVER_ReadFileCallback(struct fs_file *file, char *buffer, int count)
Read file callback.
TM_ETHERNET_Result_t TM_ETHERNET_TestReady(void)
Tests if ethernet library is prepared to work as client or server.
TM_ETHERNET_Result_t TM_ETHERNETSERVER_SetCGIHandlers(const TM_ETHERNET_CGI_t *cgis, uint16_t number_of_handlers)
Set GCI handlers which will be used when you want to control something over ethernet configured as se...
tCGI TM_ETHERNET_CGI_t
Structure for CGI tags.
Definition: tm_stm32f4_ethernet.h:466
TM_ETHERNET_Result_t TM_ETHERNETSERVER_Enable(uint16_t server_port)
Enables and prepares LwIP stack for server functionality.
Definition: tm_stm32f4_ethernet.h:367
void TM_ETHERNETDNS_FoundCallback(char *host_name, uint8_t ip_addr1, uint8_t ip_addr2, uint8_t ip_addr3, uint8_t ip_addr4)
DNS Found callback.
TM_ETHERNET_Result_t TM_ETHERNET_Update(void)
Updates ethernet LwIP stack.
Definition: tm_stm32f4_ethernet.h:447
uint8_t TM_ETHERNET_InitPinsCallback(void)
Called on ethernet GPIO initialization In this function, user can initialize custom pins...
uint16_t TM_ETHERNETCLIENT_CreateHeadersCallback(TM_TCPCLIENT_t *connection, char *buffer, uint16_t buffer_length)
Create headers callback.
uint16_t port
Definition: tm_stm32f4_ethernet.h:409
void TM_ETHERNETPHY_CustomOptions(uint32_t PHYAddress)
PHY low layer custom options callback.
Definition: tm_stm32f4_ethernet.h:449