3636 ******************************************************************************
3737 */
3838
39+ #include "Arduino.h"
3940#include "stm32_eth.h"
4041#include "lwip/init.h"
4142#include "lwip/netif.h"
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 */
7369struct 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 */
9591static uint32_t gEhtLinkTickStart = 0 ;
9692
93+ /* Handler for stimer */
94+ static stimer_t TimHandle ;
95+
9796/*************************** Function prototype *******************************/
9897static void Netif_Config (void );
99- static void tcp_connection_close (struct tcp_pcb * tpcb , struct tcp_struct * tcp );
10098static err_t tcp_recv_callback (void * arg , struct tcp_pcb * tpcb , struct pbuf * p , err_t err );
10199static err_t tcp_sent_callback (void * arg , struct tcp_pcb * tpcb , u16_t len );
102100static 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+
135162void 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 );
0 commit comments