Skip to content

Commit 2490c0f

Browse files
author
fpr
committed
Fix issue #3. Scheduler called by timer and close client modified
Signed-off-by: fpr <[email protected]>
1 parent 1ce51b4 commit 2490c0f

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

src/EthernetClient.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,10 @@ void EthernetClient::stop() {
158158
return;
159159
}
160160

161-
// Close tcp connection. If failed, force to close connection.
162-
if(ERR_OK != tcp_close(_tcp_client->pcb)) {
163-
tcp_abort(_tcp_client->pcb);
161+
// close tcp connection if not closed yet
162+
if(status() != TCP_CLOSING) {
163+
tcp_connection_close(_tcp_client->pcb, _tcp_client);
164164
}
165-
166-
_tcp_client->pcb = NULL;
167-
168-
mem_free(_tcp_client);
169-
_tcp_client = NULL;
170165
}
171166

172167
uint8_t EthernetClient::connected() {

src/utility/stm32_eth.c

+42-15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
******************************************************************************
3737
*/
3838

39+
#include "Arduino.h"
3940
#include "stm32_eth.h"
4041
#include "lwip/init.h"
4142
#include "lwip/netif.h"
@@ -46,16 +47,6 @@
4647
#include "lwip/prot/dhcp.h"
4748
#include "lwip/dns.h"
4849

49-
//Keeps compatibilty with older version of the STM32 core
50-
#if __has_include("core_callback.h")
51-
#include "core_callback.h"
52-
#else
53-
void registerCoreCallback(void (*func)(void)) {
54-
UNUSED(func);
55-
}
56-
#endif
57-
58-
5950
#ifdef __cplusplus
6051
extern "C" {
6152
#endif
@@ -69,6 +60,11 @@ void registerCoreCallback(void (*func)(void)) {
6960
/* Maximum number of retries for DHCP request */
7061
#define MAX_DHCP_TRIES 4
7162

63+
/* Timer used to call the scheduler */
64+
#ifndef DEFAULT_ETHERNET_TIMER
65+
#define DEFAULT_ETHERNET_TIMER TIM14
66+
#endif
67+
7268
/* Ethernet configuration: user parameters */
7369
struct stm32_eth_config {
7470
ip_addr_t ipaddr;
@@ -94,13 +90,16 @@ static uint8_t DHCP_Started_by_user = 0;
9490
/* Ethernet link status periodic timer */
9591
static uint32_t gEhtLinkTickStart = 0;
9692

93+
/* Handler for stimer */
94+
static stimer_t TimHandle;
95+
9796
/*************************** Function prototype *******************************/
9897
static void Netif_Config(void);
99-
static void tcp_connection_close(struct tcp_pcb *tpcb, struct tcp_struct *tcp);
10098
static err_t tcp_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
10199
static err_t tcp_sent_callback(void *arg, struct tcp_pcb *tpcb, u16_t len);
102100
static void tcp_err_callback(void *arg, err_t err);
103-
101+
static void scheduler_callback(stimer_t *htim);
102+
static void TIM_scheduler_Config(void);
104103

105104
/**
106105
* @brief Configurates the network interface
@@ -132,6 +131,34 @@ static void Netif_Config(void)
132131
#endif /* LWIP_NETIF_LINK_CALLBACK */
133132
}
134133

134+
/**
135+
* @brief Scheduler callback. Call by a timer interrupt.
136+
* @param htim: pointer to stimer_t
137+
* @retval None
138+
*/
139+
static void scheduler_callback(stimer_t *htim)
140+
{
141+
UNUSED(htim);
142+
stm32_eth_scheduler();
143+
}
144+
145+
/**
146+
* @brief Enable the timer used to call ethernet scheduler function at regular
147+
* interval.
148+
* @param None
149+
* @retval None
150+
*/
151+
static void TIM_scheduler_Config(void)
152+
{
153+
/* Set TIMx instance. */
154+
TimHandle.timer = DEFAULT_ETHERNET_TIMER;
155+
156+
/* Timer set to 1ms */
157+
TimerHandleInit(&TimHandle, (uint16_t)(1000 - 1), ((uint32_t)(getTimerClkFreq(DEFAULT_ETHERNET_TIMER) / (1000000)) - 1));
158+
159+
attachIntHandle(&TimHandle, scheduler_callback);
160+
}
161+
135162
void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, const uint8_t *netmask)
136163
{
137164
/* Initialize the LwIP stack */
@@ -178,8 +205,8 @@ void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, co
178205

179206
stm32_eth_scheduler();
180207

181-
// stm32_eth_scheduler() will be called directly inside the loop of the main() function.
182-
registerCoreCallback(stm32_eth_scheduler);
208+
// stm32_eth_scheduler() will be called every 1ms.
209+
TIM_scheduler_Config();
183210
}
184211

185212
/**
@@ -954,7 +981,7 @@ static void tcp_err_callback(void *arg, err_t err)
954981
* @param es: pointer on echoclient structure
955982
* @retval None
956983
*/
957-
static void tcp_connection_close(struct tcp_pcb *tpcb, struct tcp_struct *tcp)
984+
void tcp_connection_close(struct tcp_pcb *tpcb, struct tcp_struct *tcp)
958985
{
959986
/* remove callbacks */
960987
tcp_recv(tpcb, NULL);

src/utility/stm32_eth.h

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ uint32_t ip_addr_to_u32(ip_addr_t *ipaddr);
167167
#if LWIP_TCP
168168
err_t tcp_connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err);
169169
err_t tcp_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err);
170+
void tcp_connection_close(struct tcp_pcb *tpcb, struct tcp_struct *tcp);
170171
#else
171172
#error "LWIP_TCP must be enabled in lwipopts.h"
172173
#endif

0 commit comments

Comments
 (0)