8000 Simple examples showing how to make esp_clk_cpu_freq() available to a… · micropython/micropython@fc111c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc111c1

Browse files
jimmotve
authored andcommitted
Simple examples showing how to make esp_clk_cpu_freq() available to a native module on ESP32.
1 parent e4d4ea9 commit fc111c1

File tree

6 files changed

+98
-14
lines changed

6 files changed

+98
-14
lines changed

examples/natmod/features0/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ MOD = features0
88
SRC = features0.c
99

1010
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11-
ARCH = x64
11+
ARCH = xtensawin
12+
13+
PORT = esp32
1214

1315
# Include to get the rules for compiling and linking the module
1416
include $(MPY_DIR)/py/dynruntime.mk

examples/natmod/features0/features0.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77
// Include the header file to get access to the MicroPython API
88
#include "py/dynruntime.h"
99

10-
// Helper function to compute factorial
11-
STATIC mp_int_t factorial_helper(mp_int_t x) {
12-
if (x == 0) {
13-
return 1;
14-
}
15-
return x * factorial_helper(x - 1);
16-
}
10+
// // Helper function to compute factorial
11+
// STATIC mp_int_t factorial_helper(mp_int_t x) {
12+
// if (x == 0) {
13+
// return 1;
14+
// }
15+
// return x * factorial_helper(x - 1);
16+
// }
1717

1818
// This is the function which will be called from Python, as factorial(x)
1919
STATIC mp_obj_t factorial(mp_obj_t x_obj) {
20-
// Extract the integer from the MicroPython input object
21-
mp_int_t x = mp_obj_get_int(x_obj);
22-
// Calculate the factorial
23-
mp_int_t result = factorial_helper(x);
24-
// Convert the result to a MicroPython integer object and return it
25-
return mp_obj_new_int(result);
20+
// // Extract the integer from the MicroPython input object
21+
// mp_int_t x = mp_obj_get_int(x_obj);
22+
// // Calculate the factorial
23+
// mp_int_t result = factorial_helper(x);
24+
// // Convert the result to a MicroPython integer object and return it
25+
// return mp_obj_new_int(result);
26+
27+
return mp_obj_new_int(mp_port_fun_table.esp_clk_cpu_freq());
2628
}
2729
// Define a Python reference to the function above
2830
STATIC MP_DEFINE_CONST_FUN_OBJ_1(factorial_obj, factorial);

ports/esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ SRC_C = \
351351
mpthreadport.c \
352352
machine_rtc.c \
353353
machine_sdcard.c \
354+
portnativeglue.c \
354355
$(wildcard $(BOARD_DIR)/*.c) \
355356
$(SRC_MOD)
356357

ports/esp32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define MICROPY_ENABLE_SCHEDULER (1)
6060
#define MICROPY_SCHEDULER_DEPTH (8)
6161
#define MICROPY_VFS (1)
62+
#define MICROPY_PORT_FUN_TABLE (1)
6263

6364
// control over Python builtins
6465
#define MICROPY_PY_FUNCTION_ATTRS (1)

ports/esp32/portnativeglue.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jim Mussared
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#if MICROPY_ESP_IDF_4
28+
#include "esp32/clk.h"
29+
#else
30+
#include "esp_clk.h"
31+
#endif
32+
33+
#include "py/runtime.h"
34+
#include "py/nativeglue.h"
35+
36+
#if MICROPY_PORT_FUN_TABLE && MICROPY_EMIT_NATIVE
37+
38+
const mp_port_fun_table_t mp_port_fun_table = {
39+
esp_clk_cpu_freq,
40+
};
41+
42+
#endif // MICROPY_PORT_FUN_TABLE && MICROPY_EMIT_NATIVE

ports/esp32/portnativeglue.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jim Mussared
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32_PORTNATIVEGLUE_H
28+
#define MICROPY_INCLUDED_ESP32_PORTNATIVEGLUE_H
29+
30+
typedef struct _mp_port_fun_table_t {
31+
int (*esp_clk_cpu_freq)(void);
32+
} mp_port_fun_table_t;
33+
34+
extern const mp_port_fun_table_t mp_port_fun_table;
35+
36+
#endif // MICROPY_INCLUDED_ESP32_PORTNATIVEGLUE_H

0 commit comments

Comments
 (0)
0