Skip to content

Commit edbfd2b

Browse files
author
fpr
committed
Add callback methods to remove core dependency with STM32Ethernet library
Signed-off-by: fpr <[email protected]>
1 parent 1ae377b commit edbfd2b

File tree

4 files changed

+130
-23
lines changed

4 files changed

+130
-23
lines changed

cores/arduino/board.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77
#include "analog.h"
88
#include "clock.h"
9+
#include "core_callback.h"
910
#include "digital_io.h"
10-
#include "ethernet.h"
1111
#include "hal_uart_emul.h"
1212
#include "hw_config.h"
1313
#include "interrupt.h"

cores/arduino/main.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ int main( void )
4747

4848
for (;;)
4949
{
50-
// Define by Ethernet library. It is defined as __weak.
51-
stm32_eth_scheduler();
52-
50+
CoreCallback();
5351
loop();
5452
if (serialEventRun) serialEventRun();
5553
}

cores/arduino/stm32/core_callback.c

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
******************************************************************************
3+
* @file core_callback.c
4+
* @author WI6LABS
5+
* @version V1.0.0
6+
* @date 8-September-2017
7+
* @brief Provides methods to add callback to call inside the main loop.
8+
******************************************************************************
9+
* @attention
10+
*
11+
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
12+
*
13+
* Redistribution and use in source and binary forms, with or without modification,
14+
* are permitted provided that the following conditions are met:
15+
* 1. Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
21+
* may be used to endorse or promote products derived from this software
22+
* without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
******************************************************************************
36+
*/
37+
38+
/*
39+
* @NOTE
40+
* This file provides methods to add callback in the main() function loop.
41+
* If you need to call as often as possible a function to update your system and
42+
* you want to be sure this function to be called, you can add it to the callback
43+
* list. Otherwise, your function should be called inside the loop() function of
44+
* the sketch.
45+
*/
46+
47+
#ifdef __cplusplus
48+
extern "C" {
49+
#endif
50+
51+
/* Includes ------------------------------------------------------------------*/
52+
#include "core_callback.h"
53+
54+
// List of callback to call
55+
static void (*callbackList[CALLBACK_LIST_SIZE])(void);
56+
57+
/**
58+
* @brief Adds a callback pointer
59+
* @param func: callback pointer
60+
* @retval None
61+
*/
62+
void registerCoreCallback(void (*func)(void))
63+
{
64+
if(func == NULL)
65+
return;
66+
67+
for(uint8_t i = 0; i < CALLBACK_LIST_SIZE; i++) {
68+
if(callbackList[i] == NULL) {
69+
callbackList[i] = func;
70+
break;
71+
}
72+
}
73+
}
74+
75+
/**
76+
* @brief Removes a callback pointer
77+
* @param func: callback pointer
78+
* @retval None
79+
*/
80+
void unregisterCoreCallback(void (*func)(void))
81+
{
82+
if(func == NULL)
83+
return;
84+
85+
for(uint8_t i = 0; i < CALLBACK_LIST_SIZE; i++) {
86+
if(callbackList[i] == func) {
87+
callbackList[i] = NULL;
88+
break;
89+
}
90+
}
91+
}
92+
93+
/**
94+
* @brief Calls callback of the list. There is no priority. First added first
95+
* called. This function must be called only in main() function loop.
96+
* @param None
97+
* @retval None
98+
*/
99+
void CoreCallback(void)
100+
{
101+
for(uint8_t i = 0; i < CALLBACK_LIST_SIZE; i++) {
102+
if(callbackList[i] != NULL)
103+
callbackList[i]();
104+
}
105+
}
106+
107+
#ifdef __cplusplus
108+
}
109+
#endif
110+
111+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

cores/arduino/stm32/ethernet.h renamed to cores/arduino/stm32/core_callback.h

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
22
******************************************************************************
3-
* @file ethernet.h
3+
* @file core_callback.h
44
* @author WI6LABS
55
* @version V1.0.0
6-
* @date 14-June-2017
7-
* @brief Header for ethernet background task for LwIP stack.
6+
* @date 8-September-2017
7+
* @brief Header for callback methods.
88
******************************************************************************
99
* @attention
1010
*
11-
* <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
11+
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
1212
*
1313
* Redistribution and use in source and binary forms, with or without modification,
1414
* are permitted provided that the following conditions are met:
@@ -36,34 +36,32 @@
3636
*/
3737

3838
/* Define to prevent recursive inclusion -------------------------------------*/
39-
#ifndef __ETHERNET_H
40-
#define __ETHERNET_H
39+
#ifndef __CALLBACK_H
40+
#define __CALLBACK_H
41+
42+
#include "Arduino.h"
4143

42-
/* Includes ------------------------------------------------------------------*/
4344
#ifdef __cplusplus
4445
extern "C" {
4546
#endif
4647

48+
/* Includes ------------------------------------------------------------------*/
4749
/* Exported types ------------------------------------------------------------*/
4850
/* Exported constants --------------------------------------------------------*/
4951
/* Exported macro ------------------------------------------------------------*/
50-
/* Exported functions ------------------------------------------------------- */
51-
52-
/* This function is defined by the NativeEthernet library and it is used as
53-
background task inside the main loop. */
54-
__weak void stm32_eth_scheduler(void)
55-
{
56-
/* NOTE : This function should not be modified. It is defined in the Ethernet
57-
library.
58-
*/
59-
}
52+
#ifndef CALLBACK_LIST_SIZE
53+
#define CALLBACK_LIST_SIZE 4
54+
#endif
6055

61-
void stm32_eth_scheduler(void);
56+
/* Exported functions ------------------------------------------------------- */
57+
void registerCoreCallback(void (*func)(void));
58+
void unregisterCoreCallback(void (*func)(void));
59+
void CoreCallback(void);
6260

6361
#ifdef __cplusplus
6462
}
6563
#endif
6664

67-
#endif /* __ETHERNET_H */
65+
#endif /* __CALLBACK_H */
6866

6967
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)