mirror of
https://github.com/micropython/micropython.git
synced 2025-12-27 23:30:11 +01:00
Compare commits
361 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
032b0e9369 | ||
|
|
0d5d16074f | ||
|
|
7027fd5343 | ||
|
|
7da2fdc3cd | ||
|
|
2673374d18 | ||
|
|
651c870d77 | ||
|
|
aa3569cd57 | ||
|
|
8cd9fedf58 | ||
|
|
e23ae63970 | ||
|
|
31f6a6fa70 | ||
|
|
e54a4f1f48 | ||
|
|
d43019163d | ||
|
|
00c4d6562e | ||
|
|
e86b47175f | ||
|
|
20f40c3229 | ||
|
|
dbfba6a20e | ||
|
|
7ede3ec4b1 | ||
|
|
872f9af6ae | ||
|
|
8f59bacb92 | ||
|
|
be66a9ecf6 | ||
|
|
4836bcc957 | ||
|
|
89852d38ef | ||
|
|
526dd54252 | ||
|
|
8a1a5c236d | ||
|
|
5e11d2b349 | ||
|
|
34fe5a30c6 | ||
|
|
26664dd180 | ||
|
|
4434e43fa1 | ||
|
|
7a55c1a884 | ||
|
|
28e51c9eb1 | ||
|
|
af9d885f8c | ||
|
|
9e8eba797e | ||
|
|
8f1eced69d | ||
|
|
751485fe6b | ||
|
|
a632037866 | ||
|
|
c0e39864c6 | ||
|
|
6e552e15fa | ||
|
|
0851751615 | ||
|
|
6f1c00869c | ||
|
|
1934dca6de | ||
|
|
7a26e4f484 | ||
|
|
9a2913ed1c | ||
|
|
c6926c374d | ||
|
|
3a2fb201a5 | ||
|
|
a95b06fc6b | ||
|
|
92d4b51ad5 | ||
|
|
7e7fb0b7a3 | ||
|
|
ea8bf81058 | ||
|
|
f243851ccd | ||
|
|
7c934ae501 | ||
|
|
aa58c7ec74 | ||
|
|
b56634e691 | ||
|
|
fb3f9cff33 | ||
|
|
da33b31c7d | ||
|
|
9a348fc840 | ||
|
|
007878781c | ||
|
|
cfc4c33801 | ||
|
|
28596edf07 | ||
|
|
7ccdf8be77 | ||
|
|
f5d04750db | ||
|
|
92e9a5e0a7 | ||
|
|
84d59c2873 | ||
|
|
d8a7f8bff2 | ||
|
|
3dd0b69e46 | ||
|
|
7d588b0c7c | ||
|
|
cd14188bc8 | ||
|
|
db109ca0fc | ||
|
|
f1236734bb | ||
|
|
9de53bf788 | ||
|
|
3900fed849 | ||
|
|
96f0dd3cbc | ||
|
|
fa7c61dfab | ||
|
|
d241c2a592 | ||
|
|
f17e663493 | ||
|
|
e45c1dbd6f | ||
|
|
7693ef3bd6 | ||
|
|
99a21dc05d | ||
|
|
abc24c1876 | ||
|
|
6f5e0fe955 | ||
|
|
6a388aaa7c | ||
|
|
2af846e711 | ||
|
|
4915c2b871 | ||
|
|
cceff157dd | ||
|
|
32aba40830 | ||
|
|
f576057274 | ||
|
|
0621eca05e | ||
|
|
a39df51d8a | ||
|
|
e4d43401eb | ||
|
|
d136737872 | ||
|
|
75b1d881ec | ||
|
|
c3bd9415cc | ||
|
|
1e8ca3a3cf | ||
|
|
ab14c30493 | ||
|
|
f22b35e4e5 | ||
|
|
a243d6b057 | ||
|
|
f738424403 | ||
|
|
753a8e8bc4 | ||
|
|
e955089da0 | ||
|
|
9309e609cd | ||
|
|
ade9a05236 | ||
|
|
c48740e20b | ||
|
|
b178dccb9c | ||
|
|
3b83aeb403 | ||
|
|
115afdb07d | ||
|
|
cf814b2d34 | ||
|
|
ecb7f9fe58 | ||
|
|
fa47bebfbc | ||
|
|
8eb802a55b | ||
|
|
af33ebb13b | ||
|
|
9220dc466a | ||
|
|
d18ced9cdd | ||
|
|
7463442e58 | ||
|
|
354d17523f | ||
|
|
a0a3de60be | ||
|
|
5685b565c3 | ||
|
|
76e52b5daf | ||
|
|
fa655ce196 | ||
|
|
194c8c761e | ||
|
|
11115e4d23 | ||
|
|
c91727b75a | ||
|
|
9f76dcd682 | ||
|
|
ef7dd8db2d | ||
|
|
6ab8b63bdd | ||
|
|
68e8b595de | ||
|
|
722d4842df | ||
|
|
de575c80b9 | ||
|
|
3a2e9f20f6 | ||
|
|
7e66b859b2 | ||
|
|
aaf7c5b35e | ||
|
|
c030e77861 | ||
|
|
219a74c014 | ||
|
|
7370fd5560 | ||
|
|
e284a95cc3 | ||
|
|
616986a5f3 | ||
|
|
b19d273beb | ||
|
|
31b40eebe8 | ||
|
|
035deae1c6 | ||
|
|
5161239c9f | ||
|
|
9a65fa304c | ||
|
|
7c1c9af5d4 | ||
|
|
4f8eeaedef | ||
|
|
d680e28a11 | ||
|
|
7fd538c1b6 | ||
|
|
f522849a4d | ||
|
|
077812b2ab | ||
|
|
bdf958df30 | ||
|
|
70fc42cb28 | ||
|
|
5ebf39784a | ||
|
|
9780e55274 | ||
|
|
4f5b896a0b | ||
|
|
813b581127 | ||
|
|
778413168b | ||
|
|
0807139c1d | ||
|
|
fa1cdb09fc | ||
|
|
8cc8f280eb | ||
|
|
9896314f5b | ||
|
|
c4045f57e3 | ||
|
|
6557a096d6 | ||
|
|
f44cc517a2 | ||
|
|
abfd4da287 | ||
|
|
59fba2d6ea | ||
|
|
ed570e4b2a | ||
|
|
484adac0bb | ||
|
|
f64e080d9a | ||
|
|
e44c1d3ace | ||
|
|
186b355b28 | ||
|
|
0d3e309ebc | ||
|
|
2110dc5a6d | ||
|
|
aef3846c13 | ||
|
|
4af5424242 | ||
|
|
c5175526dd | ||
|
|
3299f687f5 | ||
|
|
9c7d183a94 | ||
|
|
d02f671737 | ||
|
|
d709622bd2 | ||
|
|
e64afde073 | ||
|
|
2a1090a637 | ||
|
|
228c68a9cd | ||
|
|
6e1dfb0d1a | ||
|
|
de8b585ab7 | ||
|
|
c4b592d379 | ||
|
|
fe99ea9aab | ||
|
|
7f19a39a3b | ||
|
|
a06c38b486 | ||
|
|
63b9e598a3 | ||
|
|
60ccb41fac | ||
|
|
06593fb0f2 | ||
|
|
a193ced7fa | ||
|
|
c8870b7c69 | ||
|
|
b6a08208e1 | ||
|
|
fb4b800820 | ||
|
|
0cba23716d | ||
|
|
b85bf25e97 | ||
|
|
2474c2ae94 | ||
|
|
92d95cc9d3 | ||
|
|
7c61249ae6 | ||
|
|
686516f90a | ||
|
|
ea2cc2b907 | ||
|
|
cdfa11f550 | ||
|
|
e78a8c94b6 | ||
|
|
f6518a7bd5 | ||
|
|
d59ca4efdb | ||
|
|
3ce212e0a6 | ||
|
|
e9ce00d874 | ||
|
|
c5029bcbf3 | ||
|
|
6f49520042 | ||
|
|
05c6fbcae6 | ||
|
|
dd0e24f4b0 | ||
|
|
32eb4b9055 | ||
|
|
ee3fec3167 | ||
|
|
431603ad69 | ||
|
|
a19ba5fea0 | ||
|
|
f28ed55aeb | ||
|
|
0a903be7d0 | ||
|
|
cfcf47c064 | ||
|
|
b630de1103 | ||
|
|
1811c6bccf | ||
|
|
e2dc7ac2a9 | ||
|
|
95cc1ff542 | ||
|
|
330e21c986 | ||
|
|
f960d753e4 | ||
|
|
3319780e96 | ||
|
|
8a6d93aeed | ||
|
|
640d00ea9d | ||
|
|
071d47fa3b | ||
|
|
6148f8b7d2 | ||
|
|
76285469d3 | ||
|
|
3d30d605f5 | ||
|
|
7ed58cb663 | ||
|
|
6e56bb623c | ||
|
|
371f4ba6b3 | ||
|
|
0aa5e75000 | ||
|
|
491c321720 | ||
|
|
b4a41a8f70 | ||
|
|
a3acaa000c | ||
|
|
098f5ae221 | ||
|
|
1406d9ccde | ||
|
|
06e85ecfa6 | ||
|
|
1679696612 | ||
|
|
07408cbd1f | ||
|
|
d7192fe68c | ||
|
|
3eece29807 | ||
|
|
601cfea6a3 | ||
|
|
7ca1bd314b | ||
|
|
031278f661 | ||
|
|
9724a0538b | ||
|
|
87ad80edf9 | ||
|
|
567b349c2b | ||
|
|
4d9cad180d | ||
|
|
a3cf4ea2f6 | ||
|
|
71f85cc330 | ||
|
|
53a8aeb6e7 | ||
|
|
80f638fe19 | ||
|
|
953c23b1bc | ||
|
|
181adc6a4e | ||
|
|
c36635c112 | ||
|
|
f8a39e3bb1 | ||
|
|
0e6c89a5c8 | ||
|
|
cc20482aa9 | ||
|
|
a546acda8c | ||
|
|
a32538bb66 | ||
|
|
2ffb6e1b15 | ||
|
|
32ce72cb9e | ||
|
|
f8bce131c0 | ||
|
|
0df20da4dd | ||
|
|
9456732b86 | ||
|
|
2fc1e64319 | ||
|
|
3ac2d06bd1 | ||
|
|
18fda7b42f | ||
|
|
db52fd8e4d | ||
|
|
0ec8cf8e80 | ||
|
|
4e4772bb5b | ||
|
|
26b512ea1b | ||
|
|
7d8edeff4e | ||
|
|
78ccb44a90 | ||
|
|
278d22ce8f | ||
|
|
9a522dda6e | ||
|
|
100004eeaf | ||
|
|
6a664cb114 | ||
|
|
51726b4d43 | ||
|
|
c02dc8b2c9 | ||
|
|
a8e7c03171 | ||
|
|
f5ae384d4f | ||
|
|
84d11b5e53 | ||
|
|
417205623a | ||
|
|
2f2c0a13fb | ||
|
|
622f241317 | ||
|
|
286ced4c2f | ||
|
|
25a8a42447 | ||
|
|
492fd5cb6b | ||
|
|
a16715ac62 | ||
|
|
9ede4dcfbb | ||
|
|
79474c6b16 | ||
|
|
2cae0f6290 | ||
|
|
78744c4f7a | ||
|
|
2abb58d758 | ||
|
|
1580e331ef | ||
|
|
6d1ff7e966 | ||
|
|
17d9b5006d | ||
|
|
5a57447a8c | ||
|
|
669dbca959 | ||
|
|
70446f46c2 | ||
|
|
0e6f5e08e1 | ||
|
|
1775b65e07 | ||
|
|
9ae3fc6523 | ||
|
|
4a10214be2 | ||
|
|
2acfb7c002 | ||
|
|
c754d8011d | ||
|
|
95104b0fbd | ||
|
|
d07de2d307 | ||
|
|
9414f92fa9 | ||
|
|
8096be089e | ||
|
|
fca3493442 | ||
|
|
5a0c5f8fea | ||
|
|
ec1f0e7551 | ||
|
|
967f3230f5 | ||
|
|
59610c4004 | ||
|
|
fabe79f7af | ||
|
|
124aa000af | ||
|
|
2dd47239de | ||
|
|
6545336206 | ||
|
|
5cd34aca27 | ||
|
|
95f19b4542 | ||
|
|
90d7c4ef3d | ||
|
|
1a97f6721f | ||
|
|
3c4b5d4281 | ||
|
|
968b7dd173 | ||
|
|
3cb804de26 | ||
|
|
9f8c5456be | ||
|
|
0d31bbc7fa | ||
|
|
ed56b0baba | ||
|
|
18030bd85d | ||
|
|
7bd273b818 | ||
|
|
a3a14b9db7 | ||
|
|
6f1cffeb28 | ||
|
|
7506db4ccb | ||
|
|
2fddc68fdf | ||
|
|
0f716aced7 | ||
|
|
a3c96c9252 | ||
|
|
e800db562f | ||
|
|
5e38b48dd6 | ||
|
|
56053c37cf | ||
|
|
514ba15d6a | ||
|
|
afaa66b657 | ||
|
|
3ad94d6072 | ||
|
|
97ce883217 | ||
|
|
861670ba2a | ||
|
|
f5dd6f7f37 | ||
|
|
b5cef5c7ea | ||
|
|
cf5112b26f | ||
|
|
44e7cbf019 | ||
|
|
6f848b6dba | ||
|
|
95a4f61499 | ||
|
|
c27dc38e85 | ||
|
|
cd3f2523f1 | ||
|
|
a379b6ed11 | ||
|
|
fb9e4cf463 | ||
|
|
8e611e8414 | ||
|
|
9466e154b4 | ||
|
|
2b62707051 | ||
|
|
ebf8dc6b03 |
@@ -261,6 +261,7 @@ today. The names appear in order of pledging.
|
||||
503 skakz
|
||||
504 Chad Cooper
|
||||
505 Makhan Virdi, mlvirdi.com, InfinityXLabs.com, USA
|
||||
506 Glenn Ruben Bakke, Norway
|
||||
507 Alasdair Allan
|
||||
509 dlbrandon
|
||||
511 Dr J Garcia, Sweden
|
||||
|
||||
@@ -33,16 +33,17 @@ Braces:
|
||||
closing brace.
|
||||
|
||||
Header files:
|
||||
- Try to stick to the Plan 9 header style, where header files do not
|
||||
include other header files.
|
||||
- Don't protect a header file from multiple inclusion with #if directives.
|
||||
- Header files should be protected from multiple inclusion with #if
|
||||
directives. See an existing header for naming convention.
|
||||
|
||||
Type names and declarations:
|
||||
- When defining a type, put '_t' after it.
|
||||
Names:
|
||||
- Use underscore_case, not camelCase for all names.
|
||||
- Use CAPS_WITH_UNDERSCORE for enums and macros.
|
||||
- When defining a type use underscore_case and put '_t' after it.
|
||||
|
||||
Integer types: Micro Python runs on 32 and 64 bit machines (and one day
|
||||
maybe 16 bit), so it's important to use the correctly-sized (and signed)
|
||||
integer types. The general guidelines are:
|
||||
Integer types: Micro Python runs on 16, 32, and 64 bit machines, so it's
|
||||
important to use the correctly-sized (and signed) integer types. The
|
||||
general guidelines are:
|
||||
- For most cases use mp_int_t for signed and mp_uint_t for unsigned
|
||||
integer values. These are guaranteed to be machine-word sized and
|
||||
therefore big enough to hold the value from a Micro Python small-int
|
||||
@@ -51,19 +52,31 @@ integer types. The general guidelines are:
|
||||
- You can use int/uint, but remember that they may be 16-bits wide.
|
||||
- If in doubt, use mp_int_t/mp_uint_t.
|
||||
|
||||
Comments:
|
||||
- Be concise and only write comments for things that are not obvious.
|
||||
- Use `// ` prefix, NOT `/* ... */`. No extra fluff.
|
||||
|
||||
Memory allocation:
|
||||
- Use m_new, m_renew, m_del (and friends) to allocate and free heap memory.
|
||||
These macros are defined in py/misc.h.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Braces and spaces:
|
||||
Braces, spaces, names and comments:
|
||||
|
||||
int foo(int x, int y) {
|
||||
if (x < y) {
|
||||
foo(y, x);
|
||||
#define TO_ADD (123)
|
||||
|
||||
// This function will always recurse indefinitely and is only used to show
|
||||
// coding style
|
||||
int foo_function(int x, int some_value) {
|
||||
if (x < some_value) {
|
||||
foo(some_value, x);
|
||||
} else {
|
||||
foo(x + 1, y - 1);
|
||||
foo(x + TO_ADD, some_value - 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < x; i++) {
|
||||
for (int my_counter = 0; my_counter < x; my_counter++) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
README.md
26
README.md
@@ -46,6 +46,7 @@ Additional components:
|
||||
(preliminary but functional).
|
||||
- pic16bit/ -- a version of Micro Python for 16-bit PIC microcontrollers.
|
||||
- cc3200/ -- a version of Micro Python that runs on the CC3200 from TI.
|
||||
- esp8266/ -- an experimental port for ESP8266 WiFi modules.
|
||||
- unix-cpy/ -- a version of Micro Python that outputs bytecode (for testing).
|
||||
- tests/ -- test framework and test scripts.
|
||||
- tools/ -- various tools, including the pyboard.py module.
|
||||
@@ -64,7 +65,7 @@ as ARM and MIPS. Making full-featured port to another architecture requires
|
||||
writing some assembly code for the exception handling and garbage collection.
|
||||
Alternatively, fallback implementation based on setjmp/longjmp can be used.
|
||||
|
||||
To build:
|
||||
To build (*):
|
||||
|
||||
$ cd unix
|
||||
$ make
|
||||
@@ -78,10 +79,19 @@ Run complete testsuite:
|
||||
|
||||
$ make test
|
||||
|
||||
Debian/Ubuntu/Mint derivative Linux distros will require build-essentials and
|
||||
libreadline-dev packages installed. To build FFI (Foreign Function Interface)
|
||||
module, libffi-dev and pkg-config packages are required. If you have problems
|
||||
with some dependencies, they can be disabled in unix/mpconfigport.mk .
|
||||
Unix version comes with a builtin package manager called upip, e.g.:
|
||||
|
||||
$ ./micropython -m upip install micropython-pystone
|
||||
$ ./micropython -m pystone
|
||||
|
||||
Browse available modules on
|
||||
[PyPI](https://pypi.python.org/pypi?%3Aaction=search&term=micropython).
|
||||
Standard library modules come from
|
||||
[micropython-lib](https://github.com/micropython/micropython-lib) project.
|
||||
|
||||
(*) Debian/Ubuntu/Mint derivative Linux distros will require build-essentials,
|
||||
libffi-dev and pkg-config packages installed. If you have problems with some
|
||||
dependencies, they can be disabled in unix/mpconfigport.mk .
|
||||
|
||||
The STM version
|
||||
---------------
|
||||
@@ -121,6 +131,8 @@ preferably in a virtualenv:
|
||||
|
||||
In `micropython/docs`, build the docs:
|
||||
|
||||
make html
|
||||
make MICROPY_PORT=<port_name> BUILDDIR=<port_name>/build html
|
||||
|
||||
You'll find the index page at `micropython/docs/build/html/index.html`.
|
||||
Where `<port_name>` can be `unix`, `pyboard`, `wipy` or `esp8266`.
|
||||
|
||||
You'll find the index page at `micropython/docs/<port_name>/build/html/index.html`.
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "py/runtime.h"
|
||||
#include "py/repl.h"
|
||||
|
||||
void do_str(const char *src) {
|
||||
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
|
||||
if (lex == NULL) {
|
||||
return;
|
||||
@@ -16,7 +16,7 @@ void do_str(const char *src) {
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
qstr source_name = lex->source_name;
|
||||
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
|
||||
mp_parse_node_t pn = mp_parse(lex, input_kind);
|
||||
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
|
||||
mp_call_function_0(module_fun);
|
||||
nlr_pop();
|
||||
@@ -28,7 +28,8 @@ void do_str(const char *src) {
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
mp_init();
|
||||
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')");
|
||||
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
|
||||
do_str("for i in range(10):\n print(i)", MP_PARSE_FILE_INPUT);
|
||||
mp_deinit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
// options to control how Micro Python is built
|
||||
|
||||
#define MICROPY_QSTR_BYTES_IN_HASH (1)
|
||||
#define MICROPY_ALLOC_PATH_MAX (512)
|
||||
#define MICROPY_EMIT_X64 (0)
|
||||
#define MICROPY_EMIT_THUMB (0)
|
||||
@@ -18,9 +19,12 @@
|
||||
#define MICROPY_ENABLE_SOURCE_LINE (0)
|
||||
#define MICROPY_ENABLE_DOC_STRING (0)
|
||||
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
|
||||
#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0)
|
||||
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
|
||||
#define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
|
||||
#define MICROPY_PY_BUILTINS_ENUMERATE (0)
|
||||
#define MICROPY_PY_BUILTINS_FROZENSET (0)
|
||||
#define MICROPY_PY_BUILTINS_REVERSED (0)
|
||||
#define MICROPY_PY_BUILTINS_SET (0)
|
||||
#define MICROPY_PY_BUILTINS_SLICE (0)
|
||||
#define MICROPY_PY_BUILTINS_PROPERTY (0)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
# Select the board to build for: if not given on the command line,
|
||||
# then default to LAUNCHXL
|
||||
BOARD ?= LAUNCHXL
|
||||
# then default to WIPY
|
||||
BOARD ?= WIPY
|
||||
ifeq ($(wildcard boards/$(BOARD)/.),)
|
||||
$(error Invalid BOARD specified)
|
||||
endif
|
||||
|
||||
# Make 'release' the default build type
|
||||
BTYPE ?= release
|
||||
|
||||
# If the build directory is not given, make it reflect the board name.
|
||||
@@ -20,7 +21,7 @@ CFLAGS = -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTE
|
||||
CFLAGS += -g -ffunction-sections -fdata-sections -fno-common -fsigned-char -mno-unaligned-access
|
||||
CFLAGS += -Iboards/$(BOARD)
|
||||
|
||||
LDFLAGS = -Wl,-nostdlib -Wl,--gc-sections -Wl,-Map=$@.map --specs=nano.specs
|
||||
LDFLAGS = -Wl,-nostdlib -Wl,--gc-sections -Wl,-Map=$@.map
|
||||
|
||||
ifeq ($(BTARGET), application)
|
||||
# qstr definitions (must come before including py.mk)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Build Instructions for the CC3200
|
||||
|
||||
Currently the CC3200 port of Micro Python builds under Linux and OSX and not under Windows.
|
||||
Currently the CC3200 port of Micro Python builds under Linux and OSX **but not under Windows**.
|
||||
|
||||
The tool chain required for the build can be found at <https://launchpad.net/gcc-arm-embedded>.
|
||||
|
||||
@@ -13,29 +13,42 @@ In order to debug the port specific code, optimizations need to be disabled on t
|
||||
port file (check the Makefile for specific details). You can use CCS from TI.
|
||||
Use the CC3200.ccxml file supplied with this distribution for the debuuger configuration.
|
||||
```bash
|
||||
make BTARGET=application BTYPE=debug
|
||||
make BTARGET=application BTYPE=debug BOARD=LAUNCHXL
|
||||
```
|
||||
## To build an image suitable to be flashed to the device:
|
||||
```bash
|
||||
make BTARGET=application BTYPE=release
|
||||
make BTARGET=application BTYPE=release BOARD=LAUNCHXL
|
||||
```
|
||||
## Building the bootloader
|
||||
```bash
|
||||
make BTARGET=bootloader BTYPE=release
|
||||
make BTARGET=bootloader BTYPE=release BOARD=LAUNCHXL
|
||||
```
|
||||
|
||||
## Regarding old revisions of the CC3200-LAUNCHXL
|
||||
First silicon (pre-release) revisions of the CC3200 had issues with the ram blocks, and Micro Python cannot run
|
||||
there. Make sure to use a **v4.1 (or higer) LAUNCHXL board** when trying this port, otherwise it won't work.
|
||||
|
||||
## Flashing the CC3200
|
||||
- Make sure that you have built both the *bootloader* and the *application* in **release** mode.
|
||||
- Make sure the SOP2 jumper is in position.
|
||||
- Open CCS_Uniflash and connect to the board (by default on port 22).
|
||||
- Format the serial flash (select 1MB size in case of the CC3200-LAUNCHXL, 2MB in case of the WiPy, leave the rest unchecked).
|
||||
- Mark the following files for erasing: `/cert/ca.pem`, `/cert/client.pm`, `/cert/private.key` and `/tmp/pac.bin`.
|
||||
- Mark the following files for erasing: `/cert/ca.pem`, `/cert/client.pem`, `/cert/private.key` and `/tmp/pac.bin`.
|
||||
- Add a new file with the name of /sys/mcuimg.bin, and select the URL to point to cc3200\bootmgr\build\<BOARD_NAME>\bootloader.bin.
|
||||
- Add another file with the name of /sys/factimg.bin, and select the URL to point to cc3200\build\<BOARD_NAME>\mcuimg.bin.
|
||||
- Click "Program" to apply all changes.
|
||||
- Flash the latest service pack (servicepack_1.0.0.10.0.bin) using the "Service Pack Update" button.
|
||||
- Close CCS_Uniflash, remove the SOP2 jumper and reset the board.
|
||||
|
||||
## Updating the board to with new software version
|
||||
- Make sure the board is running and connected to the same network as the computer.
|
||||
|
||||
```bash
|
||||
make BTARGET=application BTYPE=release BOARD=LAUNCHXL WIPY_IP=192.168.1.1 WIPY_USER=micro WIPY_PWD=python deploy
|
||||
```
|
||||
|
||||
If `WIPY_IP`, `WIPY_USER` or `WIPY_PWD` are omitted the default values (the ones shown above) will be used.
|
||||
|
||||
## Playing with MicroPython and the CC3200:
|
||||
|
||||
Once the software is running, you have two options to access the MicroPython REPL:
|
||||
|
||||
@@ -24,9 +24,8 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
__stack_size__ = 2K; /* interrupts are handled within this stack */
|
||||
__stack_size__ = 3K; /* interrupts are handled within this stack */
|
||||
__min_heap_size__ = 8K;
|
||||
__rtos_heap_size = 16K;
|
||||
|
||||
MEMORY
|
||||
{
|
||||
|
||||
@@ -75,6 +75,7 @@ APP_HAL_SRC_C = $(addprefix hal/,\
|
||||
)
|
||||
|
||||
APP_MISC_SRC_C = $(addprefix misc/,\
|
||||
antenna.c \
|
||||
FreeRTOSHooks.c \
|
||||
pin_named_pins.c \
|
||||
help.c \
|
||||
@@ -86,9 +87,12 @@ APP_MISC_SRC_C = $(addprefix misc/,\
|
||||
|
||||
APP_MODS_SRC_C = $(addprefix mods/,\
|
||||
modnetwork.c \
|
||||
moduhashlib.c \
|
||||
modubinascii.c \
|
||||
modpyb.c \
|
||||
moduos.c \
|
||||
modusocket.c \
|
||||
modussl.c \
|
||||
modutime.c \
|
||||
modwlan.c \
|
||||
pybadc.c \
|
||||
@@ -98,6 +102,7 @@ APP_MODS_SRC_C = $(addprefix mods/,\
|
||||
pybsd.c \
|
||||
pybsleep.c \
|
||||
pybspi.c \
|
||||
pybtimer.c \
|
||||
pybuart.c \
|
||||
pybwdt.c \
|
||||
)
|
||||
@@ -123,6 +128,7 @@ APP_TELNET_SRC_C = $(addprefix telnet/,\
|
||||
)
|
||||
|
||||
APP_UTIL_SRC_C = $(addprefix util/,\
|
||||
cryptohash.c \
|
||||
fifo.c \
|
||||
gccollect.c \
|
||||
random.c \
|
||||
@@ -192,6 +198,7 @@ $(BUILD)/FreeRTOS/Source/%.o: CFLAGS += -Os
|
||||
$(BUILD)/ftp/%.o: CFLAGS += -Os
|
||||
$(BUILD)/hal/%.o: CFLAGS += -Os
|
||||
$(BUILD)/misc/%.o: CFLAGS += -Os
|
||||
$(BUILD)/mods/%.o: CFLAGS += -Os
|
||||
$(BUILD)/py/%.o: CFLAGS += -Os
|
||||
$(BUILD)/simplelink/%.o: CFLAGS += -Os
|
||||
$(BUILD)/drivers/cc3100/%.o: CFLAGS += -Os
|
||||
@@ -209,9 +216,19 @@ endif
|
||||
|
||||
SHELL = bash
|
||||
APP_SIGN = appsign.sh
|
||||
UPDATE_WIPY ?= tools/update-wipy.py
|
||||
WIPY_IP ?= '192.168.1.1'
|
||||
WIPY_USER ?= 'micro'
|
||||
WIPY_PWD ?= 'python'
|
||||
|
||||
all: $(BUILD)/mcuimg.bin
|
||||
|
||||
.PHONY: deploy
|
||||
|
||||
deploy: $(BUILD)/mcuimg.bin
|
||||
$(ECHO) "Writing $< to the board"
|
||||
$(Q)$(PYTHON) $(UPDATE_WIPY) --verify --ip $(WIPY_IP) --user $(WIPY_USER) --password $(WIPY_PWD) --file $<
|
||||
|
||||
$(BUILD)/application.axf: $(OBJ) $(LINKER_SCRIPT)
|
||||
$(ECHO) "LINK $@"
|
||||
$(Q)$(CC) -o $@ $(LDFLAGS) $(OBJ) $(LIBS)
|
||||
|
||||
@@ -33,18 +33,23 @@
|
||||
#define MICROPY_HW_HAS_SDCARD (0)
|
||||
#define MICROPY_HW_ENABLE_RNG (1)
|
||||
#define MICROPY_HW_ENABLE_RTC (1)
|
||||
#define MICROPY_HW_ANTENNA_DIVERSITY (0)
|
||||
|
||||
#define MICROPY_STDIO_UART PYB_UART_0
|
||||
#define MICROPY_STDIO_UART 1
|
||||
#define MICROPY_STDIO_UART_BAUD 115200
|
||||
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
|
||||
#define MICROPY_STDIO_UART_TX_PIN (pin_GP1)
|
||||
#define MICROPY_STDIO_UART_RX_PIN (pin_GP2)
|
||||
#define MICROPY_STDIO_UART_TX_PIN_AF PIN_MODE_3
|
||||
#define MICROPY_STDIO_UART_RX_PIN_AF PIN_MODE_3
|
||||
|
||||
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA1
|
||||
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA2
|
||||
#define MICROPY_SYS_LED_PORT GPIOA1_BASE
|
||||
#define MICROPY_SAFE_BOOT_PORT GPIOA2_BASE
|
||||
#define MICROPY_SYS_LED_GPIO pin_GPIO9
|
||||
#define MICROPY_SYS_LED_PIN_NUM PIN_64 // GPIO9
|
||||
#define MICROPY_SAFE_BOOT_PIN_NUM PIN_15 // GPIO22
|
||||
#define MICROPY_SYS_LED_GPIO pin_GP9
|
||||
#define MICROPY_SYS_LED_PIN_NUM PIN_64 // GP9
|
||||
#define MICROPY_SAFE_BOOT_PIN_NUM PIN_15 // GP22
|
||||
#define MICROPY_SYS_LED_PORT_PIN GPIO_PIN_1
|
||||
#define MICROPY_SAFE_BOOT_PORT_PIN GPIO_PIN_6
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
L2,GPIO2
|
||||
L3,GPIO1
|
||||
L4,GPIO23
|
||||
L5,GPIO24
|
||||
L6,GPIO11
|
||||
L7,GPIO12
|
||||
L8,GPIO13
|
||||
L9,GPIO14
|
||||
L10,GPIO15
|
||||
L11,GPIO16
|
||||
L12,GPIO17
|
||||
L13,GPIO22
|
||||
L14,GPIO28
|
||||
R4,GPIO10
|
||||
R5,GPIO9
|
||||
R6,GPIO8
|
||||
R7,GPIO7
|
||||
R8,GPIO6
|
||||
R9,GPIO30
|
||||
R10,GPIO31
|
||||
R11,GPIO3
|
||||
R12,GPIO0
|
||||
R13,GPIO4
|
||||
R14,GPIO5
|
||||
HBL,GPIO25
|
||||
|
@@ -30,21 +30,18 @@
|
||||
#define MICROPY_HW_BOARD_NAME "WiPy"
|
||||
#define MICROPY_HW_MCU_NAME "CC3200"
|
||||
|
||||
#define MICROPY_HW_HAS_SDCARD (0)
|
||||
#define MICROPY_HW_HAS_SDCARD (1)
|
||||
#define MICROPY_HW_ENABLE_RNG (1)
|
||||
#define MICROPY_HW_ENABLE_RTC (1)
|
||||
|
||||
#define MICROPY_STDIO_UART PYB_UART_0
|
||||
#define MICROPY_STDIO_UART_BAUD 115200
|
||||
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
|
||||
#define MICROPY_HW_ANTENNA_DIVERSITY (1)
|
||||
|
||||
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA3
|
||||
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3
|
||||
#define MICROPY_SYS_LED_PORT GPIOA3_BASE
|
||||
#define MICROPY_SAFE_BOOT_PORT GPIOA3_BASE
|
||||
#define MICROPY_SYS_LED_GPIO pin_GPIO25
|
||||
#define MICROPY_SYS_LED_PIN_NUM PIN_21 // GPIO25 (SOP2)
|
||||
#define MICROPY_SAFE_BOOT_PIN_NUM PIN_18 // GPIO28
|
||||
#define MICROPY_SYS_LED_GPIO pin_GP25
|
||||
#define MICROPY_SYS_LED_PIN_NUM PIN_21 // GP25 (SOP2)
|
||||
#define MICROPY_SAFE_BOOT_PIN_NUM PIN_18 // GP28
|
||||
#define MICROPY_SYS_LED_PORT_PIN GPIO_PIN_1
|
||||
#define MICROPY_SAFE_BOOT_PORT_PIN GPIO_PIN_4
|
||||
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
L2,GPIO2
|
||||
L3,GPIO1
|
||||
L4,GPIO23
|
||||
L5,GPIO24
|
||||
L6,GPIO11
|
||||
L7,GPIO12
|
||||
L8,GPIO13
|
||||
L9,GPIO14
|
||||
L10,GPIO15
|
||||
L11,GPIO16
|
||||
L12,GPIO17
|
||||
L13,GPIO22
|
||||
L14,GPIO28
|
||||
R4,GPIO10
|
||||
R5,GPIO9
|
||||
R6,GPIO8
|
||||
R7,GPIO7
|
||||
R8,GPIO6
|
||||
R9,GPIO30
|
||||
R10,GPIO31
|
||||
R11,GPIO3
|
||||
R12,GPIO0
|
||||
R13,GPIO4
|
||||
R14,GPIO5
|
||||
HBL,GPIO25
|
||||
L2,GP2
|
||||
L3,GP1
|
||||
L4,GP23
|
||||
L5,GP24
|
||||
L6,GP11
|
||||
L7,GP12
|
||||
L8,GP13
|
||||
L9,GP14
|
||||
L10,GP15
|
||||
L11,GP16
|
||||
L12,GP17
|
||||
L13,GP22
|
||||
L14,GP28
|
||||
R4,GP10
|
||||
R5,GP9
|
||||
R6,GP8
|
||||
R7,GP7
|
||||
R8,GP6
|
||||
R9,GP30
|
||||
R10,GP31
|
||||
R11,GP3
|
||||
R12,GP0
|
||||
R13,GP4
|
||||
R14,GP5
|
||||
HBL,GP25
|
||||
|
||||
|
@@ -1,25 +1,25 @@
|
||||
Pin,Name,Default,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,AF8,AF9,AF10,AF11,AF12,AF13,AF14,AF15,ADC
|
||||
1,GPIO10,GPIO10,GPIO10,I2C_SCL,,GT_PWM06,,,SDCARD_CLK,UART1_TX,,,,,GT_CCP01,,,,
|
||||
2,GPIO11,GPIO11,GPIO11,I2C_SDA,,GT_PWM07,pXCLK(XVCLK),,SDCARD_CMD,UART1_RX,,,,,GT_CCP02,McAFSX,,,
|
||||
3,GPIO12,GPIO12,GPIO12,,,McACLK,pVS(VSYNC),I2C_SCL,,UART0_TX,,,,,GT_CCP03,,,,
|
||||
4,GPIO13,GPIO13,GPIO13,,,,pHS(HSYNC),I2C_SDA,,UART0_RX,,,,,GT_CCP04,,,,
|
||||
5,GPIO14,GPIO14,GPIO14,,,,pDATA8(CAM_D4),2C_SCL,,GSPI_CLK,,,,,GT_CCP05,,,,
|
||||
6,GPIO15,GPIO15,GPIO15,,,,pDATA9(CAM_D5),I2C_SDA,,GSPI_MISO,,,,,,GT_CCP06,,,
|
||||
7,GPIO16,GPIO16,GPIO16,,,,pDATA10(CAM_D6),UART1_TX,,GSPI_MOSI,,,,,,GT_CCP07,,,
|
||||
8,GPIO17,GPIO17,GPIO17,,,,pDATA11(CAM_D7),UART1_RX,,GSPI_CS,,,,,,,,,
|
||||
1,GP10,GP10,GP10,I2C_SCL,,GT_PWM06,,,SDCARD_CLK,UART1_TX,,,,,GT_CCP01,,,,
|
||||
2,GP11,GP11,GP11,I2C_SDA,,GT_PWM07,pXCLK(XVCLK),,SDCARD_CMD,UART1_RX,,,,,GT_CCP02,McAFSX,,,
|
||||
3,GP12,GP12,GP12,,,McACLK,pVS(VSYNC),I2C_SCL,,UART0_TX,,,,,GT_CCP03,,,,
|
||||
4,GP13,GP13,GP13,,,,pHS(HSYNC),I2C_SDA,,UART0_RX,,,,,GT_CCP04,,,,
|
||||
5,GP14,GP14,GP14,,,,pDATA8(CAM_D4),2C_SCL,,GSPI_CLK,,,,,GT_CCP05,,,,
|
||||
6,GP15,GP15,GP15,,,,pDATA9(CAM_D5),I2C_SDA,,GSPI_MISO,,,,,,GT_CCP06,,,
|
||||
7,GP16,GP16,GP16,,,,pDATA10(CAM_D6),UART1_TX,,GSPI_MOSI,,,,,,GT_CCP07,,,
|
||||
8,GP17,GP17,GP17,,,,pDATA11(CAM_D7),UART1_RX,,GSPI_CS,,,,,,,,,
|
||||
9,VDD_DIG1,VDD_DIG1,VDD_DIG1,,,,,,,,,,,,,,,,
|
||||
10,VIN_IO1,VIN_IO1,VIN_IO1,,,,,,,,,,,,,,,,
|
||||
11,FLASH_SPI_CLK,FLASH_SPI_CLK,FLASH_SPI_CLK,,,,,,,,,,,,,,,,
|
||||
12,FLASH_SPI_DOUT,FLASH_SPI_DOUT,FLASH_SPI_DOUT,,,,,,,,,,,,,,,,
|
||||
13,FLASH_SPI_DIN,FLASH_SPI_DIN,FLASH_SPI_DIN,,,,,,,,,,,,,,,,
|
||||
14,FLASH_SPI_CS,FLASH_SPI_CS,FLASH_SPI_CS,,,,,,,,,,,,,,,,
|
||||
15,GPIO22,GPIO22,GPIO22,,,,,GT_CCP04,,McAFSX,,,,,,,,,
|
||||
16,GPIO23,TDI,GPIO23,TDI,UART1_TX,,,,,,,2C_SCL,,,,,,,
|
||||
17,GPIO24,TDO,GPIO24,TDO,UART1_RX,,GT_CCP06,PWM0,McAFSX,,,I2C_SDA,,,,,,,
|
||||
18,GPIO28,GPIO28,GPIO28,,,,,,,,,,,,,,,,
|
||||
15,GP22,GP22,GP22,,,,,GT_CCP04,,McAFSX,,,,,,,,,
|
||||
16,GP23,TDI,GP23,TDI,UART1_TX,,,,,,,2C_SCL,,,,,,,
|
||||
17,GP24,TDO,GP24,TDO,UART1_RX,,GT_CCP06,PWM0,McAFSX,,,I2C_SDA,,,,,,,
|
||||
18,GP28,GP28,GP28,,,,,,,,,,,,,,,,
|
||||
19,TCK,TCK,,TCK,,,,,,,GT_PWM03,,,,,,,,
|
||||
20,GPIO29,TMS,GPIO29,TMS,,,,,,,,,,,,,,,
|
||||
21,GPIO25,SOP2,GPIO25,,McAFSX,,,,,,,GT_PWM02,,,,,,,
|
||||
20,GP29,TMS,GP29,TMS,,,,,,,,,,,,,,,
|
||||
21,GP25,SOP2,GP25,,McAFSX,,,,,,,GT_PWM02,,,,,,,
|
||||
22,WLAN_XTAL_N,WLAN_XTAL_N,WLAN_XTAL_N,,,,,,,,,,,,,,,,
|
||||
23,WLAN_XTAL_P,WLAN_XTAL_P,WLAN_XTAL_P,,,,,,,,,,,,,,,,
|
||||
24,VDD_PLL,VDD_PLL,VDD_PLL,,,,,,,,,,,,,,,,
|
||||
@@ -43,24 +43,24 @@ Pin,Name,Default,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,AF8,AF9,AF10,AF11,AF12,AF13,AF1
|
||||
42,DCDC_PA_OUT,DCDC_PA_O UT,DCDC_PA_O UT,,,,,,,,,,,,,,,,
|
||||
43,DCDC_DIG_SW,DCDC_DIG_ SW,DCDC_DIG_ SW,,,,,,,,,,,,,,,,
|
||||
44,VIN_DCDC_DIG,VIN_DCDC_ DIG,VIN_DCDC_ DIG,,,,,,,,,,,,,,,,
|
||||
45,GPIO31,DCDC_ANA2_SW_P,GPIO31,,UART1_RX,,,,McAXR0,GSPI_CLK,,UART0_RX,,,McAFSX,,,,
|
||||
45,GP31,DCDC_ANA2_SW_P,GP31,,UART1_RX,,,,McAXR0,GSPI_CLK,,UART0_RX,,,McAFSX,,,,
|
||||
46,DCDC_ANA2_SW_N,DCDC_ANA2_SW_N,DCDC_ANA2_SW_N,,,,,,,,,,,,,,,,
|
||||
47,VDD_ANA2,VDD_ANA2,VDD_ANA2,,,,,,,,,,,,,,,,
|
||||
48,VDD_ANA1,VDD_ANA1,VDD_ANA1,,,,,,,,,,,,,,,,
|
||||
49,VDD_RAM,VDD_RAM,VDD_RAM,,,,,,,,,,,,,,,,
|
||||
50,GPIO0,GPIO0,GPIO0,,,UART0_RTS,McAXR0,,McAXR1,GT_CCP00,,GSPI_CS,UART1_RTS,,UART0_CTS,,,,
|
||||
50,GP0,GP0,GP0,,,UART0_RTS,McAXR0,,McAXR1,GT_CCP00,,GSPI_CS,UART1_RTS,,UART0_CTS,,,,
|
||||
51,RTC_XTAL_P,RTC_XTAL_P,RTC_XTAL_P,,,,,,,,,,,,,,,,
|
||||
52,RTC_XTAL_N,RTC_XTAL_N,GPIO32,,McACLK,,McAXR0,,UART0_RTS,,GSPI_MOSI,,,,,,,,
|
||||
53,GPIO30,GPIO30,GPIO30,,McACLK,McAFSX,GT_CCP05,,,GSPI_MISO,,UART0_TX,,,,,,,
|
||||
52,RTC_XTAL_N,RTC_XTAL_N,GP32,,McACLK,,McAXR0,,UART0_RTS,,GSPI_MOSI,,,,,,,,
|
||||
53,GP30,GP30,GP30,,McACLK,McAFSX,GT_CCP05,,,GSPI_MISO,,UART0_TX,,,,,,,
|
||||
54,VIN_IO2,VIN_IO2,VIN_IO2,,,,,,,,,,,,,,,,
|
||||
55,GPIO1,GPIO1,GPIO1,,,GSPI_MISO,pCLK (PIXCLK),,UART1_TX,GT_CCP01,,,,,,,,,
|
||||
55,GP1,GP1,GP1,,,GSPI_MISO,pCLK (PIXCLK),,UART1_TX,GT_CCP01,,,,,,,,,
|
||||
56,VDD_DIG2,VDD_DIG2,VDD_DIG2,,,,,,,,,,,,,,,,
|
||||
57,GPIO2,GPIO2,GPIO2,,,UART0_RX,,,UART1_RX,GT_CCP02,,,,,,,,,ADC_CH0
|
||||
58,GPIO3,GPIO3,GPIO3,,,,pDATA7(CAM_D3),,UART1_TX,,,,,,,,,,ADC_CH1
|
||||
59,GPIO4,GPIO4,GPIO4,,,,pDATA6(CAM_D2),,UART1_RX,,,,,,,,,,ADC_CH2
|
||||
60,GPIO5,GPIO5,GPIO5,,,,pDATA5(CAM_D1),,McAXR1,GT_CCP05,,,,,,,,,ADC_CH3
|
||||
61,GPIO6,GPIO6,GPIO6,,,UART1_CTS,pDATA4(CAM_D0),UART0_RTS,UART0_CTS,GT_CCP06,,,,,,,,,
|
||||
62,GPIO7,GPIO7,GPIO7,,,UART1_RTS,,,,,,,UART0_RTS,UART0_TX,,McACLKX,,,
|
||||
63,GPIO8,GPIO8,GPIO8,,,,,,SDCARD_IRQ,McAFSX,,,,,GT_CCP06,,,,
|
||||
64,GPIO9,GPIO9,GPIO9,,,GT_PWM05,,,SDCARD_DATA,McAXR0,,,,,GT_CCP00,,,,
|
||||
57,GP2,GP2,GP2,,,UART0_RX,,,UART1_RX,GT_CCP02,,,,,,,,,ADC_CH0
|
||||
58,GP3,GP3,GP3,,,,pDATA7(CAM_D3),,UART1_TX,,,,,,,,,,ADC_CH1
|
||||
59,GP4,GP4,GP4,,,,pDATA6(CAM_D2),,UART1_RX,,,,,,,,,,ADC_CH2
|
||||
60,GP5,GP5,GP5,,,,pDATA5(CAM_D1),,McAXR1,GT_CCP05,,,,,,,,,ADC_CH3
|
||||
61,GP6,GP6,GP6,,,UART1_CTS,pDATA4(CAM_D0),UART0_RTS,UART0_CTS,GT_CCP06,,,,,,,,,
|
||||
62,GP7,GP7,GP7,,,UART1_RTS,,,,,,,UART0_RTS,UART0_TX,,McACLKX,,,
|
||||
63,GP8,GP8,GP8,,,,,,SDCARD_IRQ,McAFSX,,,,,GT_CCP06,,,,
|
||||
64,GP9,GP9,GP9,,,GT_PWM05,,,SDCARD_DATA,McAXR0,,,,,GT_CCP00,,,,
|
||||
65,GND_TAB,GND_TAB,GND_TAB,,,,,,,,,,,,,,,,
|
||||
|
||||
|
@@ -10,14 +10,14 @@ import csv
|
||||
|
||||
def parse_port_pin(name_str):
|
||||
"""Parses a string and returns a (port, gpio_bit) tuple."""
|
||||
if len(name_str) < 5:
|
||||
raise ValueError("Expecting pin name to be at least 5 characters")
|
||||
if name_str[:4] != 'GPIO':
|
||||
raise ValueError("Expecting pin name to start with GPIO")
|
||||
if not name_str[4:].isdigit():
|
||||
if len(name_str) < 3:
|
||||
raise ValueError("Expecting pin name to be at least 3 characters")
|
||||
if name_str[:2] != 'GP':
|
||||
raise ValueError("Expecting pin name to start with GP")
|
||||
if not name_str[2:].isdigit():
|
||||
raise ValueError("Expecting numeric GPIO number")
|
||||
port = int(int(name_str[4:]) / 8)
|
||||
gpio_bit = 1 << int(int(name_str[4:]) % 8)
|
||||
port = int(int(name_str[2:]) / 8)
|
||||
gpio_bit = 1 << int(int(name_str[2:]) % 8)
|
||||
return (port, gpio_bit)
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ BOOT_CPPDEFINES = -Dgcc -DBOOTLOADER -DTARGET_IS_CC3200 -DSL_TINY
|
||||
BOOT_HAL_SRC_C = $(addprefix hal/,\
|
||||
cpu.c \
|
||||
interrupt.c \
|
||||
gpio.c \
|
||||
pin.c \
|
||||
prcm.c \
|
||||
shamd5.c \
|
||||
@@ -42,6 +43,7 @@ BOOT_CC3100_SRC_C = $(addprefix drivers/cc3100/,\
|
||||
)
|
||||
|
||||
BOOT_MISC_SRC_C = $(addprefix misc/,\
|
||||
antenna.c \
|
||||
mperror.c \
|
||||
)
|
||||
|
||||
@@ -50,7 +52,7 @@ BOOT_SL_SRC_C = $(addprefix simplelink/,\
|
||||
)
|
||||
|
||||
BOOT_UTIL_SRC_C = $(addprefix util/,\
|
||||
hash.c \
|
||||
cryptohash.c \
|
||||
)
|
||||
|
||||
BOOT_MAIN_SRC_C = \
|
||||
|
||||
@@ -39,6 +39,7 @@ SECTIONS
|
||||
{
|
||||
_text = .;
|
||||
KEEP(*(.intvecs))
|
||||
*(.boot*)
|
||||
*(.text*)
|
||||
*(.rodata*)
|
||||
*(.ARM.extab* .gnu.linkonce.armextab.*)
|
||||
|
||||
@@ -43,23 +43,31 @@ extern "C"
|
||||
*******************************************************************************/
|
||||
#define IMG_BOOT_INFO "/sys/bootinfo.bin"
|
||||
#define IMG_FACTORY "/sys/factimg.bin"
|
||||
#define IMG_UPDATE "/sys/updtimg.bin"
|
||||
#define IMG_UPDATE1 "/sys/updtimg1.bin"
|
||||
#define IMG_UPDATE2 "/sys/updtimg2.bin"
|
||||
#define IMG_PREFIX "/sys/updtimg"
|
||||
|
||||
#define IMG_SRVPACK "/sys/servicepack.ucf"
|
||||
#define SRVPACK_SIGN "/sys/servicepack.sig"
|
||||
|
||||
#define CA_FILE "/cert/ca.pem"
|
||||
#define CERT_FILE "/cert/cert.pem"
|
||||
#define KEY_FILE "/cert/private.key"
|
||||
|
||||
/******************************************************************************
|
||||
Image file sizes
|
||||
Special file sizes
|
||||
*******************************************************************************/
|
||||
#define IMG_SIZE (232 * 1024) /* 16KB are reserved for the bootloader and at least 8KB for the heap*/
|
||||
#define IMG_SIZE (192 * 1024) /* 16KB are reserved for the bootloader and at least 48KB for the heap*/
|
||||
#define SRVPACK_SIZE (16 * 1024)
|
||||
#define SIGN_SIZE (2 * 1024)
|
||||
#define CA_KEY_SIZE (4 * 1024)
|
||||
|
||||
/******************************************************************************
|
||||
Active Image
|
||||
*******************************************************************************/
|
||||
#define IMG_ACT_FACTORY 0
|
||||
#define IMG_ACT_UPDATE 1
|
||||
#define IMG_ACT_UPDATE1 1
|
||||
#define IMG_ACT_UPDATE2 2
|
||||
|
||||
#define IMG_STATUS_CHECK 0
|
||||
#define IMG_STATUS_READY 1
|
||||
@@ -67,13 +75,13 @@ extern "C"
|
||||
/******************************************************************************
|
||||
Boot Info structure
|
||||
*******************************************************************************/
|
||||
typedef struct sBootInfo
|
||||
typedef struct _sBootInfo_t
|
||||
{
|
||||
_u8 ActiveImg;
|
||||
_u8 Status;
|
||||
_u8 PrevImg;
|
||||
_u8 : 8;
|
||||
_u8 : 8;
|
||||
}sBootInfo_t;
|
||||
} sBootInfo_t;
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "std.h"
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
@@ -47,11 +48,12 @@
|
||||
#include "flc.h"
|
||||
#include "bootmgr.h"
|
||||
#include "shamd5.h"
|
||||
#include "hash.h"
|
||||
#include "cryptohash.h"
|
||||
#include "utils.h"
|
||||
#include "cc3200_hal.h"
|
||||
#include "debug.h"
|
||||
#include "mperror.h"
|
||||
#include "antenna.h"
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
@@ -62,11 +64,16 @@
|
||||
#define BOOTMGR_HASH_SIZE 32
|
||||
#define BOOTMGR_BUFF_SIZE 512
|
||||
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_MS 1200
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS 200
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_0_MS 500
|
||||
|
||||
#define BOOTMGR_SAFE_MODE_ENTER_MS 800
|
||||
#define BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS 80
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_1_MS 3000
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_1_BLINK_MS 250
|
||||
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_2_MS 3000
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_2_BLINK_MS 250
|
||||
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_3_MS 1500
|
||||
#define BOOTMGR_WAIT_SAFE_MODE_3_BLINK_MS 100
|
||||
|
||||
//*****************************************************************************
|
||||
// Exported functions declarations
|
||||
@@ -77,9 +84,11 @@ extern void bootmgr_run_app (_u32 base);
|
||||
// Local functions declarations
|
||||
//*****************************************************************************
|
||||
static void bootmgr_board_init (void);
|
||||
static bool bootmgr_verify (void);
|
||||
static bool bootmgr_verify (_u8 *image);
|
||||
static void bootmgr_load_and_execute (_u8 *image);
|
||||
static bool safe_mode_boot (void);
|
||||
static bool wait_while_blinking (uint32_t wait_time, uint32_t period, bool force_wait);
|
||||
static bool safe_boot_request_start (uint32_t wait_time);
|
||||
static void wait_for_safe_boot (sBootInfo_t *psBootInfo);
|
||||
static void bootmgr_image_loader (sBootInfo_t *psBootInfo);
|
||||
|
||||
//*****************************************************************************
|
||||
@@ -138,44 +147,52 @@ void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
|
||||
//! Board Initialization & Configuration
|
||||
//*****************************************************************************
|
||||
static void bootmgr_board_init(void) {
|
||||
// Set vector table base
|
||||
// set the vector table base
|
||||
MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
|
||||
|
||||
// Enable Processor Interrupts
|
||||
// enable processor interrupts
|
||||
MAP_IntMasterEnable();
|
||||
MAP_IntEnable(FAULT_SYSTICK);
|
||||
|
||||
// Mandatory MCU Initialization
|
||||
// mandatory MCU initialization
|
||||
PRCMCC3200MCUInit();
|
||||
|
||||
// clear all the special bits, since we can't trust their content after reset
|
||||
PRCMClearSpecialBit(PRCM_SAFE_BOOT_BIT);
|
||||
PRCMClearSpecialBit(PRCM_WDT_RESET_BIT);
|
||||
PRCMClearSpecialBit(PRCM_FIRST_BOOT_BIT);
|
||||
|
||||
// check the reset after clearing the special bits
|
||||
mperror_bootloader_check_reset_cause();
|
||||
|
||||
// Enable the Data Hashing Engine
|
||||
HASH_Init();
|
||||
#if MICROPY_HW_ANTENNA_DIVERSITY
|
||||
// configure the antenna selection pins
|
||||
antenna_init0();
|
||||
#endif
|
||||
|
||||
// Init the system led and the system switch
|
||||
// enable the data hashing engine
|
||||
CRYPTOHASH_Init();
|
||||
|
||||
// init the system led and the system switch
|
||||
mperror_init0();
|
||||
|
||||
// clear the safe boot flag, since we can't trust its content after reset
|
||||
PRCMClearSafeBootRequest();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//! Verifies the integrity of the new application binary
|
||||
//*****************************************************************************
|
||||
static bool bootmgr_verify (void) {
|
||||
static bool bootmgr_verify (_u8 *image) {
|
||||
SlFsFileInfo_t FsFileInfo;
|
||||
_u32 reqlen, offset = 0;
|
||||
_i32 fHandle;
|
||||
|
||||
// open the file for reading
|
||||
if (0 == sl_FsOpen((_u8 *)IMG_UPDATE, FS_MODE_OPEN_READ, NULL, &fHandle)) {
|
||||
if (0 == sl_FsOpen(image, FS_MODE_OPEN_READ, NULL, &fHandle)) {
|
||||
// get the file size
|
||||
sl_FsGetInfo((_u8 *)IMG_UPDATE, 0, &FsFileInfo);
|
||||
sl_FsGetInfo(image, 0, &FsFileInfo);
|
||||
|
||||
if (FsFileInfo.FileLen > BOOTMGR_HASH_SIZE) {
|
||||
FsFileInfo.FileLen -= BOOTMGR_HASH_SIZE;
|
||||
HASH_SHAMD5Start(BOOTMGR_HASH_ALGO, FsFileInfo.FileLen);
|
||||
CRYPTOHASH_SHAMD5Start(BOOTMGR_HASH_ALGO, FsFileInfo.FileLen);
|
||||
do {
|
||||
if ((FsFileInfo.FileLen - offset) > BOOTMGR_BUFF_SIZE) {
|
||||
reqlen = BOOTMGR_BUFF_SIZE;
|
||||
@@ -185,10 +202,10 @@ static bool bootmgr_verify (void) {
|
||||
}
|
||||
|
||||
offset += sl_FsRead(fHandle, offset, bootmgr_file_buf, reqlen);
|
||||
HASH_SHAMD5Update(bootmgr_file_buf, reqlen);
|
||||
CRYPTOHASH_SHAMD5Update(bootmgr_file_buf, reqlen);
|
||||
} while (offset < FsFileInfo.FileLen);
|
||||
|
||||
HASH_SHAMD5Read (bootmgr_file_buf);
|
||||
CRYPTOHASH_SHAMD5Read (bootmgr_file_buf);
|
||||
|
||||
// convert the resulting hash to hex
|
||||
for (_u32 i = 0; i < (BOOTMGR_HASH_SIZE / 2); i++) {
|
||||
@@ -235,47 +252,81 @@ static void bootmgr_load_and_execute (_u8 *image) {
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//! Check for the safe mode pin
|
||||
//! Wait while the safe mode pin is being held high and blink the system led
|
||||
//! with the specified period
|
||||
//*****************************************************************************
|
||||
static bool safe_mode_boot (void) {
|
||||
_u32 count = 0;
|
||||
while (MAP_GPIOPinRead(MICROPY_SAFE_BOOT_PORT, MICROPY_SAFE_BOOT_PORT_PIN) &&
|
||||
((BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS * count++) < BOOTMGR_WAIT_SAFE_MODE_MS)) {
|
||||
static bool wait_while_blinking (uint32_t wait_time, uint32_t period, bool force_wait) {
|
||||
_u32 count;
|
||||
for (count = 0; (force_wait || MAP_GPIOPinRead(MICROPY_SAFE_BOOT_PORT, MICROPY_SAFE_BOOT_PORT_PIN)) &&
|
||||
((period * count) < wait_time); count++) {
|
||||
// toogle the led
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, ~MAP_GPIOPinRead(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN));
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS * 1000));
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(period * 1000));
|
||||
}
|
||||
return MAP_GPIOPinRead(MICROPY_SAFE_BOOT_PORT, MICROPY_SAFE_BOOT_PORT_PIN) ? true : false;
|
||||
}
|
||||
|
||||
static bool safe_boot_request_start (uint32_t wait_time) {
|
||||
if (MAP_GPIOPinRead(MICROPY_SAFE_BOOT_PORT, MICROPY_SAFE_BOOT_PORT_PIN)) {
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(wait_time * 1000));
|
||||
}
|
||||
mperror_deinit_sfe_pin();
|
||||
return MAP_GPIOPinRead(MICROPY_SAFE_BOOT_PORT, MICROPY_SAFE_BOOT_PORT_PIN) ? true : false;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//! Load the proper image based on information from boot info and executes it.
|
||||
//! Check for the safe mode pin
|
||||
//*****************************************************************************
|
||||
static void wait_for_safe_boot (sBootInfo_t *psBootInfo) {
|
||||
if (safe_boot_request_start(BOOTMGR_WAIT_SAFE_MODE_0_MS)) {
|
||||
if (wait_while_blinking(BOOTMGR_WAIT_SAFE_MODE_1_MS, BOOTMGR_WAIT_SAFE_MODE_1_BLINK_MS, false)) {
|
||||
// go back one step in time
|
||||
psBootInfo->ActiveImg = psBootInfo->PrevImg;
|
||||
if (wait_while_blinking(BOOTMGR_WAIT_SAFE_MODE_2_MS, BOOTMGR_WAIT_SAFE_MODE_2_BLINK_MS, false)) {
|
||||
// go back directly to the factory image
|
||||
psBootInfo->ActiveImg = IMG_ACT_FACTORY;
|
||||
wait_while_blinking(BOOTMGR_WAIT_SAFE_MODE_3_MS, BOOTMGR_WAIT_SAFE_MODE_3_BLINK_MS, true);
|
||||
}
|
||||
}
|
||||
// turn off the system led
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0);
|
||||
// request a safe boot to the application
|
||||
PRCMSetSpecialBit(PRCM_SAFE_BOOT_BIT);
|
||||
}
|
||||
// deinit the safe boot pin
|
||||
mperror_deinit_sfe_pin();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//! Load the proper image based on the information from the boot info
|
||||
//! and launch it.
|
||||
//*****************************************************************************
|
||||
static void bootmgr_image_loader(sBootInfo_t *psBootInfo) {
|
||||
_i32 fhandle;
|
||||
if (safe_mode_boot()) {
|
||||
_u32 count = 0;
|
||||
while ((BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * count++) < BOOTMGR_SAFE_MODE_ENTER_MS) {
|
||||
// toogle the led
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, ~MAP_GPIOPinRead(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN));
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * 1000));
|
||||
}
|
||||
psBootInfo->ActiveImg = IMG_ACT_FACTORY;
|
||||
// turn the led off
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0);
|
||||
// request a safe boot to the application
|
||||
PRCMRequestSafeBoot();
|
||||
_u8 *image;
|
||||
|
||||
// search for the active image
|
||||
switch (psBootInfo->ActiveImg) {
|
||||
case IMG_ACT_UPDATE1:
|
||||
image = (unsigned char *)IMG_UPDATE1;
|
||||
break;
|
||||
case IMG_ACT_UPDATE2:
|
||||
image = (unsigned char *)IMG_UPDATE2;
|
||||
break;
|
||||
default:
|
||||
image = (unsigned char *)IMG_FACTORY;
|
||||
break;
|
||||
}
|
||||
// do we have a new update image that needs to be verified?
|
||||
else if ((psBootInfo->ActiveImg == IMG_ACT_UPDATE) && (psBootInfo->Status == IMG_STATUS_CHECK)) {
|
||||
if (!bootmgr_verify()) {
|
||||
// delete the corrupted file
|
||||
sl_FsDel((_u8 *)IMG_UPDATE, 0);
|
||||
// switch to the factory image
|
||||
psBootInfo->ActiveImg = IMG_ACT_FACTORY;
|
||||
|
||||
// do we have a new image that needs to be verified?
|
||||
if ((psBootInfo->ActiveImg != IMG_ACT_FACTORY) && (psBootInfo->Status == IMG_STATUS_CHECK)) {
|
||||
if (!bootmgr_verify(image)) {
|
||||
// verification failed, delete the broken file
|
||||
sl_FsDel(image, 0);
|
||||
// switch to the previous image
|
||||
psBootInfo->ActiveImg = psBootInfo->PrevImg;
|
||||
psBootInfo->PrevImg = IMG_ACT_FACTORY;
|
||||
}
|
||||
// in any case, set the status as "READY"
|
||||
// in any case, change the status to "READY"
|
||||
psBootInfo->Status = IMG_STATUS_READY;
|
||||
// write the new boot info
|
||||
if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle)) {
|
||||
@@ -285,24 +336,34 @@ static void bootmgr_image_loader(sBootInfo_t *psBootInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
// now boot the active image
|
||||
if (IMG_ACT_UPDATE == psBootInfo->ActiveImg) {
|
||||
bootmgr_load_and_execute((unsigned char *)IMG_UPDATE);
|
||||
}
|
||||
else {
|
||||
bootmgr_load_and_execute((unsigned char *)IMG_FACTORY);
|
||||
// this one might modify the boot info hence it MUST be called after
|
||||
// bootmgr_verify! (so that the changes are not saved to flash)
|
||||
wait_for_safe_boot(psBootInfo);
|
||||
|
||||
// select the active image again, since it might have changed
|
||||
switch (psBootInfo->ActiveImg) {
|
||||
case IMG_ACT_UPDATE1:
|
||||
image = (unsigned char *)IMG_UPDATE1;
|
||||
break;
|
||||
case IMG_ACT_UPDATE2:
|
||||
image = (unsigned char *)IMG_UPDATE2;
|
||||
break;
|
||||
default:
|
||||
image = (unsigned char *)IMG_FACTORY;
|
||||
break;
|
||||
}
|
||||
bootmgr_load_and_execute(image);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//! Main function
|
||||
//*****************************************************************************
|
||||
int main (void) {
|
||||
sBootInfo_t sBootInfo = { .ActiveImg = IMG_ACT_FACTORY, .Status = IMG_STATUS_READY };
|
||||
sBootInfo_t sBootInfo = { .ActiveImg = IMG_ACT_FACTORY, .Status = IMG_STATUS_READY, .PrevImg = IMG_ACT_FACTORY };
|
||||
bool bootapp = false;
|
||||
_i32 fhandle;
|
||||
|
||||
// Board Initialization
|
||||
// board setup
|
||||
bootmgr_board_init();
|
||||
|
||||
// start simplelink since we need it to access the sflash
|
||||
@@ -315,17 +376,20 @@ int main (void) {
|
||||
}
|
||||
sl_FsClose(fhandle, 0, 0, 0);
|
||||
}
|
||||
// boot info file not present, it means that this is the first boot after being programmed
|
||||
if (!bootapp) {
|
||||
// create a new boot info file
|
||||
_u32 BootInfoCreateFlag = _FS_FILE_OPEN_FLAG_COMMIT | _FS_FILE_PUBLIC_WRITE | _FS_FILE_PUBLIC_READ;
|
||||
if (!sl_FsOpen ((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_CREATE((2 * sizeof(sBootInfo_t)),
|
||||
BootInfoCreateFlag), NULL, &fhandle)) {
|
||||
// Write the default boot info.
|
||||
// write the default boot info.
|
||||
if (sizeof(sBootInfo_t) == sl_FsWrite(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t))) {
|
||||
bootapp = true;
|
||||
}
|
||||
sl_FsClose(fhandle, 0, 0, 0);
|
||||
}
|
||||
// signal the first boot to the application
|
||||
PRCMSetSpecialBit(PRCM_FIRST_BOOT_BIT);
|
||||
}
|
||||
|
||||
if (bootapp) {
|
||||
@@ -336,14 +400,14 @@ int main (void) {
|
||||
// stop simplelink
|
||||
sl_Stop(SL_STOP_TIMEOUT);
|
||||
|
||||
// if we've reached this point, then it means a fatal error occurred and the application
|
||||
// could not be loaded, so, loop forever and signal the crash to the user
|
||||
// if we've reached this point, then it means that a fatal error has occurred and the
|
||||
// application could not be loaded, so, loop forever and signal the crash to the user
|
||||
while (true) {
|
||||
// keep the bld on
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, MICROPY_SYS_LED_PORT_PIN);
|
||||
__asm volatile(" dsb \n"
|
||||
" isb \n"
|
||||
" wfi \n");
|
||||
__asm volatile(" dsb \n"
|
||||
" isb \n"
|
||||
" wfi \n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include "py/obj.h"
|
||||
#include "diskio.h" /* FatFs lower layer API */
|
||||
#include "sflash_diskio.h" /* Serial flash disk IO API */
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
@@ -20,6 +21,7 @@
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "rom_map.h"
|
||||
#include "prcm.h"
|
||||
#include "pybrtc.h"
|
||||
#include "timeutils.h"
|
||||
|
||||
/* Definitions of physical drive number for each drive */
|
||||
@@ -193,12 +195,7 @@ DWORD get_fattime (
|
||||
)
|
||||
{
|
||||
timeutils_struct_time_t tm;
|
||||
uint32_t seconds;
|
||||
uint16_t mseconds;
|
||||
|
||||
// Get the time from the on-chip RTC and convert it to struct_time
|
||||
MAP_PRCMRTCGet(&seconds, &mseconds);
|
||||
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
|
||||
timeutils_seconds_since_2000_to_struct_time(pybrtc_get_seconds(), &tm);
|
||||
|
||||
return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) |
|
||||
((tm.tm_mday) << 16) | ((tm.tm_hour) << 11) |
|
||||
|
||||
@@ -4,10 +4,12 @@
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "simplelink.h"
|
||||
#include "diskio.h"
|
||||
#include "sflash_diskio.h"
|
||||
#include "debug.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
|
||||
#define SFLASH_TIMEOUT_MAX_MS 5500
|
||||
@@ -58,7 +60,7 @@ DRESULT sflash_disk_init (void) {
|
||||
print_block_name (i);
|
||||
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
||||
// Create the block file if it doesn't exist
|
||||
if (sl_FsGetInfo(sflash_block_name, 0, &FsFileInfo) < 0) {
|
||||
if (sl_FsGetInfo(sflash_block_name, 0, &FsFileInfo) != 0) {
|
||||
if (!sl_FsOpen(sflash_block_name, FS_MODE_OPEN_CREATE(SFLASH_BLOCK_SIZE, 0), NULL, &fileHandle)) {
|
||||
sl_FsClose(fileHandle, NULL, NULL, 0);
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
@@ -72,6 +74,10 @@ DRESULT sflash_disk_init (void) {
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
return RES_ERROR;
|
||||
}
|
||||
} else {
|
||||
// file system exists, break here to speed up booting
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
break;
|
||||
}
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ void ff_get_volname(BYTE vol, TCHAR **dest) {
|
||||
#endif
|
||||
{
|
||||
memcpy(*dest, "/flash", 6);
|
||||
*dest += 7;
|
||||
*dest += 6;
|
||||
}
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
else
|
||||
|
||||
@@ -39,7 +39,9 @@
|
||||
#include "pybrtc.h"
|
||||
#include "ftp.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "modusocket.h"
|
||||
#include "debug.h"
|
||||
#include "serverstask.h"
|
||||
#include "ff.h"
|
||||
@@ -50,7 +52,6 @@
|
||||
#include "updater.h"
|
||||
#include "timeutils.h"
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE CONSTANTS
|
||||
******************************************************************************/
|
||||
@@ -67,7 +68,6 @@
|
||||
#define FTP_UNIX_TIME_20150101 1420070400
|
||||
#define FTP_UNIX_SECONDS_180_DAYS 15552000
|
||||
#define FTP_DATA_TIMEOUT_MS 5000 // 5 seconds
|
||||
#define FTP_CMD_TIMEOUT_MS 120000 // 2 minutes
|
||||
#define FTP_SOCKETFIFO_ELEMENTS_MAX 4
|
||||
#define FTP_CYCLE_TIME_MS (SERVERS_CYCLE_TIME_MS * 2)
|
||||
|
||||
@@ -137,17 +137,16 @@ typedef struct {
|
||||
uint8_t e_open;
|
||||
bool closechild;
|
||||
bool enabled;
|
||||
bool swupdating;
|
||||
|
||||
bool special_file;
|
||||
} ftp_data_t;
|
||||
|
||||
typedef struct {
|
||||
char * cmd;
|
||||
}ftp_cmd_t;
|
||||
} ftp_cmd_t;
|
||||
|
||||
typedef struct {
|
||||
char * month;
|
||||
}ftp_month_t;
|
||||
} ftp_month_t;
|
||||
|
||||
typedef enum {
|
||||
E_FTP_CMD_NOT_SUPPORTED = -1,
|
||||
@@ -174,7 +173,7 @@ typedef enum {
|
||||
E_FTP_CMD_NOOP,
|
||||
E_FTP_CMD_QUIT,
|
||||
E_FTP_NUM_FTP_CMDS
|
||||
}ftp_cmd_index_t;
|
||||
} ftp_cmd_index_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
@@ -224,7 +223,6 @@ static ftp_result_t ftp_list_dir (char *list, uint32_t maxlistsize, uint32_t *li
|
||||
static void ftp_open_child (char *pwd, char *dir);
|
||||
static void ftp_close_child (char *pwd);
|
||||
static void ftp_return_to_previous_path (char *pwd, char *dir);
|
||||
static void ftp_reset (void);
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
@@ -244,7 +242,7 @@ void ftp_init (void) {
|
||||
ftp_data.e_open = E_FTP_NOTHING_OPEN;
|
||||
ftp_data.state = E_FTP_STE_DISABLED;
|
||||
ftp_data.substate.data = E_FTP_STE_SUB_DISCONNECTED;
|
||||
ftp_data.swupdating = false;
|
||||
ftp_data.special_file = false;
|
||||
}
|
||||
|
||||
void ftp_run (void) {
|
||||
@@ -253,7 +251,7 @@ void ftp_run (void) {
|
||||
ftp_wait_for_enabled();
|
||||
break;
|
||||
case E_FTP_STE_START:
|
||||
if (ftp_create_listening_socket(&ftp_data.lc_sd, FTP_CMD_PORT, FTP_CMD_CLIENTS_MAX )) {
|
||||
if (wlan_is_connected() && ftp_create_listening_socket(&ftp_data.lc_sd, FTP_CMD_PORT, FTP_CMD_CLIENTS_MAX)) {
|
||||
ftp_data.state = E_FTP_STE_READY;
|
||||
}
|
||||
break;
|
||||
@@ -326,7 +324,7 @@ void ftp_run (void) {
|
||||
ftp_data.dtimeout = 0;
|
||||
ftp_data.ctimeout = 0;
|
||||
// its a software update
|
||||
if (ftp_data.swupdating) {
|
||||
if (ftp_data.special_file) {
|
||||
if (updater_write(ftp_data.dBuffer, len)) {
|
||||
break;
|
||||
}
|
||||
@@ -346,8 +344,8 @@ void ftp_run (void) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (ftp_data.swupdating) {
|
||||
ftp_data.swupdating = false;
|
||||
if (ftp_data.special_file) {
|
||||
ftp_data.special_file = false;
|
||||
updater_finnish();
|
||||
}
|
||||
ftp_close_files();
|
||||
@@ -408,6 +406,16 @@ void ftp_disable (void) {
|
||||
ftp_data.state = E_FTP_STE_DISABLED;
|
||||
}
|
||||
|
||||
void ftp_reset (void) {
|
||||
// close all connections and start all over again
|
||||
servers_close_socket(&ftp_data.lc_sd);
|
||||
servers_close_socket(&ftp_data.ld_sd);
|
||||
ftp_close_cmd_data();
|
||||
ftp_data.state = E_FTP_STE_START;
|
||||
ftp_data.substate.data = E_FTP_STE_SUB_DISCONNECTED;
|
||||
SOCKETFIFO_Flush();
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
@@ -429,21 +437,27 @@ static bool ftp_create_listening_socket (_i16 *sd, _u16 port, _u8 backlog) {
|
||||
_sd = *sd;
|
||||
|
||||
if (_sd > 0) {
|
||||
// add the new socket to the network administration
|
||||
modusocket_socket_add(_sd, false);
|
||||
|
||||
// Enable non-blocking mode
|
||||
nonBlockingOption.NonblockingEnabled = 1;
|
||||
ASSERT (sl_SetSockOpt(_sd, SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlockingOption, sizeof(nonBlockingOption)) == SL_SOC_OK);
|
||||
ASSERT ((result = sl_SetSockOpt(_sd, SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlockingOption, sizeof(nonBlockingOption))) == SL_SOC_OK);
|
||||
|
||||
// Bind the socket to a port number
|
||||
sServerAddress.sin_family = AF_INET;
|
||||
sServerAddress.sin_addr.s_addr = INADDR_ANY;
|
||||
sServerAddress.sin_port = htons(port);
|
||||
|
||||
ASSERT (sl_Bind(_sd, (const SlSockAddr_t *)&sServerAddress, sizeof(sServerAddress)) == SL_SOC_OK);
|
||||
ASSERT ((result |= sl_Bind(_sd, (const SlSockAddr_t *)&sServerAddress, sizeof(sServerAddress))) == SL_SOC_OK);
|
||||
|
||||
// Start listening
|
||||
ASSERT ((result = sl_Listen (_sd, backlog)) == SL_SOC_OK);
|
||||
ASSERT ((result |= sl_Listen (_sd, backlog)) == SL_SOC_OK);
|
||||
|
||||
return (result == SL_SOC_OK) ? true : false;
|
||||
if (result == SL_SOC_OK) {
|
||||
return true;
|
||||
}
|
||||
servers_close_socket(sd);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -464,6 +478,9 @@ static ftp_result_t ftp_wait_for_connection (_i16 l_sd, _i16 *n_sd) {
|
||||
return E_FTP_RESULT_FAILED;
|
||||
}
|
||||
|
||||
// add the new socket to the network administration
|
||||
modusocket_socket_add(_sd, false);
|
||||
|
||||
// client connected, so go on
|
||||
return E_FTP_RESULT_OK;
|
||||
}
|
||||
@@ -560,8 +577,8 @@ static void ftp_send_from_fifo (void) {
|
||||
// close the listening and the data sockets
|
||||
servers_close_socket(&ftp_data.ld_sd);
|
||||
servers_close_socket(&ftp_data.d_sd);
|
||||
if (ftp_data.swupdating) {
|
||||
ftp_data.swupdating = false;
|
||||
if (ftp_data.special_file) {
|
||||
ftp_data.special_file = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -741,15 +758,14 @@ static void ftp_process_cmd (void) {
|
||||
ftp_get_param_and_open_child (&bufptr);
|
||||
// first check if a software update is being requested
|
||||
if (updater_check_path (ftp_path)) {
|
||||
// start by erasing the previous status file
|
||||
// must be done before starting the updater
|
||||
f_unlink(ftp_path);
|
||||
if (updater_start()) {
|
||||
ftp_data.swupdating = true;
|
||||
ftp_data.special_file = true;
|
||||
ftp_data.state = E_FTP_STE_CONTINUE_FILE_RX;
|
||||
ftp_send_reply(150, NULL);
|
||||
}
|
||||
else {
|
||||
// to unlock the updater
|
||||
updater_finnish();
|
||||
ftp_data.state = E_FTP_STE_END_TRANSFER;
|
||||
ftp_send_reply(550, NULL);
|
||||
}
|
||||
@@ -822,7 +838,7 @@ static void ftp_process_cmd (void) {
|
||||
}
|
||||
}
|
||||
else if (result == E_FTP_RESULT_CONTINUE) {
|
||||
if (ftp_data.ctimeout++ > (FTP_CMD_TIMEOUT_MS / FTP_CYCLE_TIME_MS)) {
|
||||
if (ftp_data.ctimeout++ > (servers_get_timeout() / FTP_CYCLE_TIME_MS)) {
|
||||
ftp_send_reply(221, NULL);
|
||||
}
|
||||
}
|
||||
@@ -843,9 +859,9 @@ static void ftp_close_files (void) {
|
||||
|
||||
static void ftp_close_filesystem_on_error (void) {
|
||||
ftp_close_files();
|
||||
if (ftp_data.swupdating) {
|
||||
if (ftp_data.special_file) {
|
||||
updater_finnish ();
|
||||
ftp_data.swupdating = false;
|
||||
ftp_data.special_file = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -881,7 +897,6 @@ static int ftp_print_eplf_item (char *dest, uint32_t destsize, FILINFO *fno) {
|
||||
|
||||
char *type = (fno->fattrib & AM_DIR) ? "d" : "-";
|
||||
uint32_t tseconds;
|
||||
uint16_t mseconds;
|
||||
uint mindex = (((fno->fdate >> 5) & 0x0f) > 0) ? (((fno->fdate >> 5) & 0x0f) - 1) : 0;
|
||||
uint day = ((fno->fdate & 0x1f) > 0) ? (fno->fdate & 0x1f) : 1;
|
||||
uint fseconds = timeutils_seconds_since_2000(1980 + ((fno->fdate >> 9) & 0x7f),
|
||||
@@ -890,7 +905,7 @@ static int ftp_print_eplf_item (char *dest, uint32_t destsize, FILINFO *fno) {
|
||||
(fno->ftime >> 11) & 0x1f,
|
||||
(fno->ftime >> 5) & 0x3f,
|
||||
2 * (fno->ftime & 0x1f));
|
||||
MAP_PRCMRTCGet(&tseconds, &mseconds);
|
||||
tseconds = pybrtc_get_seconds();
|
||||
if (FTP_UNIX_SECONDS_180_DAYS < tseconds - fseconds) {
|
||||
return snprintf(dest, destsize, "%srw-rw-r-- 1 root root %9u %s %2u %5u %s\r\n",
|
||||
type, (_u32)fno->fsize, ftp_month[mindex].month, day,
|
||||
@@ -914,12 +929,11 @@ static int ftp_print_eplf_item (char *dest, uint32_t destsize, FILINFO *fno) {
|
||||
static int ftp_print_eplf_drive (char *dest, uint32_t destsize, char *name) {
|
||||
timeutils_struct_time_t tm;
|
||||
uint32_t tseconds;
|
||||
uint16_t mseconds;
|
||||
char *type = "d";
|
||||
|
||||
timeutils_seconds_since_2000_to_struct_time((FTP_UNIX_TIME_20150101 - FTP_UNIX_TIME_20000101), &tm);
|
||||
|
||||
MAP_PRCMRTCGet(&tseconds, &mseconds);
|
||||
tseconds = pybrtc_get_seconds();
|
||||
if (FTP_UNIX_SECONDS_180_DAYS < tseconds - (FTP_UNIX_TIME_20150101 - FTP_UNIX_TIME_20000101)) {
|
||||
return snprintf(dest, destsize, "%srw-rw-r-- 1 root root %9u %s %2u %5u %s\r\n",
|
||||
type, 0, ftp_month[(tm.tm_mon - 1)].month, tm.tm_mday, tm.tm_year, name);
|
||||
@@ -1073,12 +1087,3 @@ static void ftp_return_to_previous_path (char *pwd, char *dir) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ftp_reset (void) {
|
||||
// close all connections and start all over again
|
||||
servers_close_socket(&ftp_data.lc_sd);
|
||||
servers_close_socket(&ftp_data.ld_sd);
|
||||
ftp_close_cmd_data();
|
||||
ftp_data.state = E_FTP_STE_START;
|
||||
ftp_data.substate.data = E_FTP_STE_SUB_DISCONNECTED;
|
||||
SOCKETFIFO_Flush();
|
||||
}
|
||||
|
||||
@@ -34,5 +34,6 @@ extern void ftp_init (void);
|
||||
extern void ftp_run (void);
|
||||
extern void ftp_enable (void);
|
||||
extern void ftp_disable (void);
|
||||
extern void ftp_reset (void);
|
||||
|
||||
#endif /* FTP_H_ */
|
||||
|
||||
@@ -1,14 +1,43 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "simplelink.h"
|
||||
#include "flc.h"
|
||||
#include "updater.h"
|
||||
#include "shamd5.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "debug.h"
|
||||
#include "osi.h"
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE CONSTANTS
|
||||
@@ -16,6 +45,9 @@
|
||||
#define UPDATER_IMG_PATH "/flash/sys/mcuimg.bin"
|
||||
#define UPDATER_SRVPACK_PATH "/flash/sys/servicepack.ucf"
|
||||
#define UPDATER_SIGN_PATH "/flash/sys/servicepack.sig"
|
||||
#define UPDATER_CA_PATH "/flash/cert/ca.pem"
|
||||
#define UPDATER_CERT_PATH "/flash/cert/cert.pem"
|
||||
#define UPDATER_KEY_PATH "/flash/cert/private.key"
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE TYPES
|
||||
@@ -30,33 +62,64 @@ typedef struct {
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
static updater_data_t updater_data;
|
||||
static updater_data_t updater_data = { .path = NULL, .fhandle = -1, .fsize = 0, .foffset = 0 };
|
||||
static OsiLockObj_t updater_LockObj;
|
||||
static sBootInfo_t sBootInfo;
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
__attribute__ ((section (".boot")))
|
||||
void updater_pre_init (void) {
|
||||
// create the updater lock
|
||||
ASSERT(OSI_OK == sl_LockObjCreate(&updater_LockObj, "UpdaterLock"));
|
||||
}
|
||||
|
||||
bool updater_check_path (void *path) {
|
||||
sl_LockObjLock (&updater_LockObj, SL_OS_WAIT_FOREVER);
|
||||
if (!strcmp(UPDATER_IMG_PATH, path)) {
|
||||
updater_data.path = IMG_UPDATE;
|
||||
updater_data.fsize = IMG_SIZE;
|
||||
return true;
|
||||
}
|
||||
else if (!strcmp(UPDATER_SRVPACK_PATH, path)) {
|
||||
updater_data.path = IMG_UPDATE1;
|
||||
// the launchxl doesn't have enough flash space for 2 user update images
|
||||
#ifdef WIPY
|
||||
// check which one should be the next active image
|
||||
_i32 fhandle;
|
||||
if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_READ, NULL, &fhandle)) {
|
||||
ASSERT (sizeof(sBootInfo_t) == sl_FsRead(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t)));
|
||||
sl_FsClose(fhandle, 0, 0, 0);
|
||||
if ((sBootInfo.Status == IMG_STATUS_CHECK && sBootInfo.ActiveImg == IMG_ACT_UPDATE2) ||
|
||||
sBootInfo.ActiveImg == IMG_ACT_UPDATE1) {
|
||||
updater_data.path = IMG_UPDATE2;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else if (!strcmp(UPDATER_SRVPACK_PATH, path)) {
|
||||
updater_data.path = IMG_SRVPACK;
|
||||
updater_data.fsize = SRVPACK_SIZE;
|
||||
return true;
|
||||
}
|
||||
else if (!strcmp(UPDATER_SIGN_PATH, path)) {
|
||||
} else if (!strcmp(UPDATER_SIGN_PATH, path)) {
|
||||
updater_data.path = SRVPACK_SIGN;
|
||||
updater_data.fsize = SIGN_SIZE;
|
||||
return true;
|
||||
} else if (!strcmp(UPDATER_CA_PATH, path)) {
|
||||
updater_data.path = CA_FILE;
|
||||
updater_data.fsize = CA_KEY_SIZE;
|
||||
} else if (!strcmp(UPDATER_CERT_PATH, path)) {
|
||||
updater_data.path = CERT_FILE;
|
||||
updater_data.fsize = CA_KEY_SIZE;
|
||||
} else if (!strcmp(UPDATER_KEY_PATH, path)) {
|
||||
updater_data.path = KEY_FILE;
|
||||
updater_data.fsize = CA_KEY_SIZE;
|
||||
} else {
|
||||
sl_LockObjUnlock (&updater_LockObj);
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool updater_start (void) {
|
||||
_u32 AccessModeAndMaxSize = FS_MODE_OPEN_WRITE;
|
||||
SlFsFileInfo_t FsFileInfo;
|
||||
bool result = false;
|
||||
|
||||
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
||||
if (0 != sl_FsGetInfo((_u8 *)updater_data.path, 0, &FsFileInfo)) {
|
||||
// file doesn't exist, create it
|
||||
@@ -64,49 +127,76 @@ bool updater_start (void) {
|
||||
}
|
||||
if (!sl_FsOpen((_u8 *)updater_data.path, AccessModeAndMaxSize, NULL, &updater_data.fhandle)) {
|
||||
updater_data.foffset = 0;
|
||||
return true;
|
||||
result = true;
|
||||
}
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
return false;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool updater_write (uint8_t *buf, uint32_t len) {
|
||||
bool result = false;
|
||||
|
||||
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
||||
if (len == sl_FsWrite(updater_data.fhandle, updater_data.foffset, buf, len)) {
|
||||
updater_data.foffset += len;
|
||||
return true;
|
||||
result = true;
|
||||
}
|
||||
return false;
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void updater_finnish (void) {
|
||||
sBootInfo_t sBootInfo;
|
||||
_i32 fhandle;
|
||||
|
||||
if (updater_data.fhandle > 0) {
|
||||
sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
|
||||
// close the file being updated
|
||||
sl_FsClose(updater_data.fhandle, NULL, NULL, 0);
|
||||
|
||||
if (!strcmp (IMG_UPDATE, updater_data.path)) {
|
||||
// open the boot info file for reading
|
||||
#ifdef WIPY
|
||||
// if we still have an image pending for verification, leave the boot info as it is
|
||||
if (!strncmp(IMG_PREFIX, updater_data.path, strlen(IMG_PREFIX)) && sBootInfo.Status != IMG_STATUS_CHECK) {
|
||||
#else
|
||||
if (!strncmp(IMG_PREFIX, updater_data.path, strlen(IMG_PREFIX))) {
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_READ, NULL, &fhandle)) {
|
||||
|
||||
ASSERT (sizeof(sBootInfo_t) == sl_FsRead(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t)));
|
||||
sl_FsClose(fhandle, 0, 0, 0);
|
||||
// open the file for writing
|
||||
#endif
|
||||
// open the boot info file for writing
|
||||
ASSERT (sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle) == 0);
|
||||
#ifdef DEBUG
|
||||
}
|
||||
else {
|
||||
// the boot info file doesn't exist yet
|
||||
_u32 BootInfoCreateFlag = _FS_FILE_OPEN_FLAG_COMMIT | _FS_FILE_PUBLIC_WRITE | _FS_FILE_PUBLIC_READ;
|
||||
ASSERT (sl_FsOpen ((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_CREATE((2 * sizeof(sBootInfo_t)),
|
||||
BootInfoCreateFlag), NULL, &fhandle) == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
// write the new boot info
|
||||
sBootInfo.ActiveImg = IMG_ACT_UPDATE;
|
||||
// save the new boot info
|
||||
#ifdef WIPY
|
||||
sBootInfo.PrevImg = sBootInfo.ActiveImg;
|
||||
if (sBootInfo.ActiveImg == IMG_ACT_UPDATE1) {
|
||||
sBootInfo.ActiveImg = IMG_ACT_UPDATE2;
|
||||
} else {
|
||||
sBootInfo.ActiveImg = IMG_ACT_UPDATE1;
|
||||
}
|
||||
// the launchxl doesn't have enough flash space for 2 user updates
|
||||
#else
|
||||
sBootInfo.PrevImg = IMG_ACT_FACTORY;
|
||||
sBootInfo.ActiveImg = IMG_ACT_UPDATE1;
|
||||
#endif
|
||||
sBootInfo.Status = IMG_STATUS_CHECK;
|
||||
ASSERT (sizeof(sBootInfo_t) == sl_FsWrite(fhandle, 0, (unsigned char *)&sBootInfo, sizeof(sBootInfo_t)));
|
||||
sl_FsClose(fhandle, 0, 0, 0);
|
||||
}
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
updater_data.fhandle = -1;
|
||||
}
|
||||
updater_data.fhandle = -1;
|
||||
sl_LockObjUnlock (&wlan_LockObj);
|
||||
sl_LockObjUnlock (&updater_LockObj);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,38 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UPDATER_H_
|
||||
#define UPDATER_H_
|
||||
|
||||
bool updater_check_path (void *path);
|
||||
bool updater_start (void);
|
||||
bool updater_write (uint8_t *buf, uint32_t len);
|
||||
void updater_finnish (void);
|
||||
bool updater_verify (uint8_t *rbuff, uint8_t *hasbuff);
|
||||
extern void updater_pre_init (void);
|
||||
extern bool updater_check_path (void *path);
|
||||
extern bool updater_start (void);
|
||||
extern bool updater_write (uint8_t *buf, uint32_t len);
|
||||
extern void updater_finnish (void);
|
||||
extern bool updater_verify (uint8_t *rbuff, uint8_t *hasbuff);
|
||||
|
||||
#endif /* UPDATER_H_ */
|
||||
|
||||
@@ -45,6 +45,8 @@
|
||||
#include "mpexception.h"
|
||||
#include "telnet.h"
|
||||
#include "pybuart.h"
|
||||
#include "utils.h"
|
||||
#include "irq.h"
|
||||
|
||||
#ifdef USE_FREERTOS
|
||||
#include "FreeRTOS.h"
|
||||
@@ -107,16 +109,23 @@ uint32_t HAL_GetTick(void) {
|
||||
}
|
||||
|
||||
void HAL_Delay(uint32_t delay) {
|
||||
#ifdef USE_FREERTOS
|
||||
vTaskDelay (delay / portTICK_PERIOD_MS);
|
||||
#else
|
||||
uint32_t start = HAL_tickCount;
|
||||
// Wraparound of tick is taken care of by 2's complement arithmetic.
|
||||
while (HAL_tickCount - start < delay) {
|
||||
// Enter sleep mode, waiting for (at least) the SysTick interrupt.
|
||||
__WFI();
|
||||
// only if we are not within interrupt context and interrupts are enabled
|
||||
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
|
||||
#ifdef USE_FREERTOS
|
||||
vTaskDelay (delay / portTICK_PERIOD_MS);
|
||||
#else
|
||||
uint32_t start = HAL_tickCount;
|
||||
// wraparound of tick is taken care of by 2's complement arithmetic.
|
||||
while (HAL_tickCount - start < delay) {
|
||||
// enter sleep mode, waiting for (at least) the SysTick interrupt.
|
||||
__WFI();
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
for (int ms = 0; ms < delay; ms++) {
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(1000));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void mp_hal_set_interrupt_char (int c) {
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
#define HAL_FCPU_MHZ 80U
|
||||
#define HAL_FCPU_HZ (1000000U * HAL_FCPU_MHZ)
|
||||
#define HAL_SYSTICK_PERIOD_US 1000U
|
||||
#define UTILS_DELAY_US_TO_COUNT(us) (((us) * HAL_FCPU_MHZ) / 3)
|
||||
#define UTILS_DELAY_US_TO_COUNT(us) (((us) * HAL_FCPU_MHZ) / 6)
|
||||
|
||||
#define HAL_NVIC_INT_CTRL_REG (*((volatile uint32_t *) 0xE000ED04 ) )
|
||||
#define HAL_VECTACTIVE_MASK (0x1FUL)
|
||||
|
||||
@@ -49,15 +49,6 @@
|
||||
#include "inc/hw_ocp_shared.h"
|
||||
#include "pin.h"
|
||||
|
||||
//*****************************************************************************
|
||||
// Macros
|
||||
//*****************************************************************************
|
||||
#define PAD_MODE_MASK 0x0000000F
|
||||
#define PAD_STRENGTH_MASK 0x000000E0
|
||||
#define PAD_TYPE_MASK 0x00000310
|
||||
#define PAD_CONFIG_BASE ((OCP_SHARED_BASE + \
|
||||
OCP_SHARED_O_GPIO_PAD_CONFIG_0))
|
||||
|
||||
//*****************************************************************************
|
||||
// PIN to PAD matrix
|
||||
//*****************************************************************************
|
||||
|
||||
@@ -143,6 +143,13 @@ extern "C"
|
||||
#define PIN_TYPE_OD_PD 0x00000210
|
||||
#define PIN_TYPE_ANALOG 0x10000000
|
||||
|
||||
//*****************************************************************************
|
||||
// Macros for mode and type
|
||||
//*****************************************************************************
|
||||
#define PAD_MODE_MASK 0x0000000F
|
||||
#define PAD_STRENGTH_MASK 0x000000E0
|
||||
#define PAD_TYPE_MASK 0x00000310
|
||||
#define PAD_CONFIG_BASE ((OCP_SHARED_BASE + OCP_SHARED_O_GPIO_PAD_CONFIG_0))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
|
||||
@@ -129,11 +129,12 @@
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Bit: 31 is used to indicate use of RTC. If set as '1', RTC feature is used.
|
||||
// Bit: 30 is used to indicate that a safe boot should be performed
|
||||
// bit: 29 is used to indicate that the last reset was caused by the WDT
|
||||
// Bits: 28 to 26 are unused
|
||||
// Bit: 30 is used to indicate that a safe boot should be performed.
|
||||
// bit: 29 is used to indicate that the last reset was caused by the WDT.
|
||||
// bit: 28 is used to indicate that the board is booting for the first time after being programmed in factory.
|
||||
// Bits: 27 and 26 are unused.
|
||||
// Bits: 25 to 16 are used to save millisecond part of RTC reference.
|
||||
// Bits: 15 to 0 are being used for HW Changes / ECO
|
||||
// Bits: 15 to 0 are being used for HW Changes / ECO.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
@@ -254,94 +255,49 @@ static const PRCM_PeriphRegs_t PRCM_PeriphRegsList[] =
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Requests a safe boot
|
||||
//! Set a special bit
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void PRCMRequestSafeBoot(void)
|
||||
void PRCMSetSpecialBit(unsigned char bit)
|
||||
{
|
||||
unsigned int uiRegValue;
|
||||
|
||||
uiRegValue = MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) | (1 << 30);
|
||||
uiRegValue = MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) | (1 << bit);
|
||||
|
||||
PRCMHIBRegWrite(RTC_MSEC_U32_REG_ADDR, uiRegValue);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Clear the safe boot request
|
||||
//! Clear a special bit
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void PRCMClearSafeBootRequest(void)
|
||||
void PRCMClearSpecialBit(unsigned char bit)
|
||||
{
|
||||
unsigned int uiRegValue;
|
||||
|
||||
uiRegValue = MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) & (~(1 << 30));
|
||||
uiRegValue = MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) & (~(1 << bit));
|
||||
|
||||
PRCMHIBRegWrite(RTC_MSEC_U32_REG_ADDR, uiRegValue);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Read the safe boot request bit. This bit is cleared after reading.
|
||||
//! Read a special bit
|
||||
//!
|
||||
//! \return Value of the safe boot bit
|
||||
//! \return Value of the bit
|
||||
//
|
||||
//*****************************************************************************
|
||||
tBoolean PRCMIsSafeBootRequested(void)
|
||||
tBoolean PRCMGetSpecialBit(unsigned char bit)
|
||||
{
|
||||
tBoolean safeboot = (MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) & (1 << 30)) ? true : false;
|
||||
|
||||
PRCMClearSafeBootRequest();
|
||||
|
||||
return safeboot;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Signals that a WDT reset has occurred
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void PRCMSignalWDTReset(void)
|
||||
{
|
||||
unsigned int uiRegValue;
|
||||
|
||||
uiRegValue = MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) | (1 << 29);
|
||||
|
||||
PRCMHIBRegWrite(RTC_MSEC_U32_REG_ADDR, uiRegValue);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Clear the WDT reset signal
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void PRCMClearWDTResetSignal(void)
|
||||
{
|
||||
unsigned int uiRegValue;
|
||||
|
||||
uiRegValue = MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) & (~(1 << 29));
|
||||
|
||||
PRCMHIBRegWrite(RTC_MSEC_U32_REG_ADDR, uiRegValue);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Read the WDT reset signal bit
|
||||
//!
|
||||
//! \return Value of the WDT reset signal bit
|
||||
//
|
||||
//*****************************************************************************
|
||||
tBoolean PRCMWasResetBecauseOfWDT(void)
|
||||
{
|
||||
return (MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) & (1 << 29)) ? true : false;
|
||||
tBoolean value = (MAP_PRCMHIBRegRead(RTC_MSEC_U32_REG_ADDR) & (1 << bit)) ? true : false;
|
||||
// special bits must be cleared immediatelly after reading
|
||||
PRCMClearSpecialBit(bit);
|
||||
return value;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
@@ -192,17 +192,21 @@ unsigned char ulRstReg;
|
||||
// PRCM_ADC should never be used in any user code.
|
||||
#define PRCM_ADC 0x000000FF
|
||||
|
||||
//*****************************************************************************
|
||||
// User bits in the PRCM persistent registers
|
||||
//*****************************************************************************
|
||||
#define PRCM_SAFE_BOOT_BIT 30
|
||||
#define PRCM_WDT_RESET_BIT 29
|
||||
#define PRCM_FIRST_BOOT_BIT 28
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// API Function prototypes
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void PRCMRequestSafeBoot(void);
|
||||
extern void PRCMClearSafeBootRequest(void);
|
||||
extern tBoolean PRCMIsSafeBootRequested(void);
|
||||
extern void PRCMSignalWDTReset(void);
|
||||
extern void PRCMClearWDTResetSignal(void);
|
||||
extern tBoolean PRCMWasResetBecauseOfWDT(void);
|
||||
extern void PRCMSetSpecialBit(unsigned char bit);
|
||||
extern void PRCMClearSpecialBit(unsigned char bit);
|
||||
extern tBoolean PRCMGetSpecialBit(unsigned char bit);
|
||||
extern void PRCMSOCReset(void);
|
||||
extern void PRCMMCUReset(tBoolean bIncludeSubsystem);
|
||||
extern unsigned long PRCMSysResetCauseGet(void);
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "simplelink.h"
|
||||
#include "pybwdt.h"
|
||||
#include "debug.h"
|
||||
#include "antenna.h"
|
||||
#include "mperror.h"
|
||||
|
||||
/******************************************************************************
|
||||
@@ -65,6 +66,11 @@ int main (void) {
|
||||
// Initialize the clocks and the interrupt system
|
||||
HAL_SystemInit();
|
||||
|
||||
#if MICROPY_HW_ANTENNA_DIVERSITY
|
||||
// configure the antenna selection pins
|
||||
antenna_init0();
|
||||
#endif
|
||||
|
||||
// Init the watchdog
|
||||
pybwdt_init0();
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "pybuart.h"
|
||||
#include "osi.h"
|
||||
#include "pybwdt.h"
|
||||
#include "mperror.h"
|
||||
|
||||
|
||||
@@ -49,8 +48,6 @@
|
||||
//*****************************************************************************
|
||||
void vApplicationIdleHook (void)
|
||||
{
|
||||
// kick the watchdog
|
||||
pybwdt_kick();
|
||||
// signal that we are alive and kicking
|
||||
mperror_heartbeat_signal();
|
||||
// gate the processor's clock to save power
|
||||
|
||||
97
cc3200/misc/antenna.c
Normal file
97
cc3200/misc/antenna.c
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mpconfigboard.h"
|
||||
#include "inc/hw_types.h"
|
||||
#include "inc/hw_gpio.h"
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "rom_map.h"
|
||||
#include "pin.h"
|
||||
#include "prcm.h"
|
||||
#include "gpio.h"
|
||||
#include "antenna.h"
|
||||
|
||||
|
||||
#if MICROPY_HW_ANTENNA_DIVERSITY
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define REG_PAD_CONFIG_26 (0x4402E108)
|
||||
#define REG_PAD_CONFIG_27 (0x4402E10C)
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
static antenna_type_t antenna_type_selected = ANTENNA_TYPE_INTERNAL;
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
void antenna_init0(void) {
|
||||
// enable the peripheral clock and set the gpio direction for
|
||||
// both antenna 1 and antenna 2 pins
|
||||
MAP_PRCMPeripheralClkEnable(PRCM_GPIOA3, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
MAP_GPIODirModeSet(GPIOA3_BASE, 0x0C, GPIO_DIR_MODE_OUT);
|
||||
|
||||
// configure antenna 1 pin type and strength
|
||||
HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) | (0x00000020 | 0x00000000));
|
||||
// set the mode
|
||||
HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~PAD_MODE_MASK) | 0x00000000) & ~(3 << 10);
|
||||
// set the direction
|
||||
HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~0xC00) | 0x00000800);
|
||||
|
||||
// configure antenna 2 pin type and strength
|
||||
HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) | (0x00000020 | 0x00000000));
|
||||
// set the mode
|
||||
HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~PAD_MODE_MASK) | 0x00000000) & ~(3 << 10);
|
||||
// set the direction
|
||||
HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~0xC00) | 0x00000800);
|
||||
|
||||
// select the currently active antenna
|
||||
antenna_select(antenna_type_selected);
|
||||
}
|
||||
|
||||
void antenna_select (antenna_type_t _antenna) {
|
||||
if (_antenna == ANTENNA_TYPE_INTERNAL) {
|
||||
MAP_GPIOPinWrite(GPIOA3_BASE, 0x0C, 0x04);
|
||||
// also configure the pull-up and pull-down accordingly
|
||||
HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PU;
|
||||
HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PD;
|
||||
} else {
|
||||
MAP_GPIOPinWrite(GPIOA3_BASE, 0x0C, 0x08);
|
||||
// also configure the pull-up and pull-down accordingly
|
||||
HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PD;
|
||||
HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PU;
|
||||
}
|
||||
antenna_type_selected = _antenna;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
38
cc3200/misc/antenna.h
Normal file
38
cc3200/misc/antenna.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _ANTENNA_H_
|
||||
#define _ANTENNA_H_
|
||||
|
||||
typedef enum {
|
||||
ANTENNA_TYPE_INTERNAL = 0,
|
||||
ANTENNA_TYPE_EXTERNAL
|
||||
} antenna_type_t;
|
||||
|
||||
extern void antenna_init0 (void);
|
||||
extern void antenna_select (antenna_type_t antenna_type);
|
||||
|
||||
#endif /* _ANTENNA_H_ */
|
||||
@@ -24,6 +24,8 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "std.h"
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
@@ -41,7 +43,7 @@
|
||||
DEFINE PUBLIC DATA
|
||||
******************************************************************************/
|
||||
const mp_arg_t mpcallback_init_args[] = {
|
||||
{ MP_QSTR_intmode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
{ MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
|
||||
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
@@ -71,7 +73,6 @@ mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_
|
||||
|
||||
mpcallback_obj_t *mpcallback_find (mp_obj_t parent) {
|
||||
for (mp_uint_t i = 0; i < MP_STATE_PORT(mpcallback_obj_list).len; i++) {
|
||||
// search for the object and then remove it
|
||||
mpcallback_obj_t *callback_obj = ((mpcallback_obj_t *)(MP_STATE_PORT(mpcallback_obj_list).items[i]));
|
||||
if (callback_obj->parent == parent) {
|
||||
return callback_obj;
|
||||
@@ -125,10 +126,8 @@ uint mpcallback_translate_priority (uint priority) {
|
||||
void mpcallback_handler (mp_obj_t self_in) {
|
||||
mpcallback_obj_t *self = self_in;
|
||||
if (self && self->handler != mp_const_none) {
|
||||
// disable interrupts to avoid nesting
|
||||
uint primsk = disable_irq();
|
||||
// when executing code within a handler we must lock the GC to prevent
|
||||
// any memory allocations. We must also catch any exceptions.
|
||||
// any memory allocations.
|
||||
gc_lock();
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
@@ -139,13 +138,13 @@ void mpcallback_handler (mp_obj_t self_in) {
|
||||
// uncaught exception; disable the callback so that it doesn't run again
|
||||
self->methods->disable (self->parent);
|
||||
self->handler = mp_const_none;
|
||||
// printing an exception here will cause a stack overflow that will end up in
|
||||
// a hard fault, so is better to signal the uncaught (probably non-recoverable)
|
||||
// exception by blinking the system led instead.
|
||||
// signal the error using the heart beat led and print an
|
||||
// exception message as well
|
||||
mperror_signal_error();
|
||||
printf("Uncaught exception in callback handler\n");
|
||||
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
|
||||
}
|
||||
gc_unlock();
|
||||
enable_irq(primsk);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
#include "hw_ints.h"
|
||||
#include "hw_types.h"
|
||||
#include "hw_gpio.h"
|
||||
@@ -63,12 +64,17 @@
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
#ifndef BOOTLOADER
|
||||
STATIC const mp_obj_base_t pyb_heartbeat_obj = {&pyb_heartbeat_type};
|
||||
#endif
|
||||
|
||||
struct mperror_heart_beat {
|
||||
uint32_t off_time;
|
||||
uint32_t on_time;
|
||||
bool beating;
|
||||
bool enabled;
|
||||
} mperror_heart_beat = {.off_time = 0, .on_time = 0, .beating = false, .enabled = false};
|
||||
bool do_disable;
|
||||
} mperror_heart_beat = {.off_time = 0, .on_time = 0, .beating = false, .enabled = false, .do_disable = false};
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
@@ -90,6 +96,7 @@ void mperror_init0 (void) {
|
||||
// configure the system led
|
||||
pin_config ((pin_obj_t *)&MICROPY_SYS_LED_GPIO, PIN_MODE_0, GPIO_DIR_MODE_OUT, PIN_TYPE_STD, PIN_STRENGTH_6MA);
|
||||
#endif
|
||||
mperror_heart_beat.enabled = true;
|
||||
mperror_heartbeat_switch_off();
|
||||
}
|
||||
|
||||
@@ -108,7 +115,7 @@ void mperror_bootloader_check_reset_cause (void) {
|
||||
|
||||
// since the reset cause will be changed, we must store the right reason
|
||||
// so that the application knows it when booting for the next time
|
||||
PRCMSignalWDTReset();
|
||||
PRCMSetSpecialBit(PRCM_WDT_RESET_BIT);
|
||||
|
||||
MAP_PRCMHibernateWakeupSourceEnable(PRCM_HIB_SLOW_CLK_CTR);
|
||||
// set the sleep interval to 10ms
|
||||
@@ -131,23 +138,21 @@ void mperror_signal_error (void) {
|
||||
}
|
||||
}
|
||||
|
||||
void mperror_enable_heartbeat (void) {
|
||||
mperror_heart_beat.enabled = true;
|
||||
}
|
||||
|
||||
void mperror_heartbeat_switch_off (void) {
|
||||
mperror_heart_beat.on_time = 0;
|
||||
mperror_heart_beat.off_time = 0;
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0);
|
||||
}
|
||||
|
||||
void mperror_disable_heartbeat (void) {
|
||||
mperror_heart_beat.enabled = false;
|
||||
mperror_heartbeat_switch_off();
|
||||
if (mperror_heart_beat.enabled) {
|
||||
mperror_heart_beat.on_time = 0;
|
||||
mperror_heart_beat.off_time = 0;
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void mperror_heartbeat_signal (void) {
|
||||
if (mperror_heart_beat.enabled) {
|
||||
if (mperror_heart_beat.do_disable) {
|
||||
mperror_heart_beat.do_disable = false;
|
||||
mperror_heartbeat_switch_off();
|
||||
mperror_heart_beat.enabled = false;
|
||||
}
|
||||
else if (mperror_heart_beat.enabled) {
|
||||
if (!mperror_heart_beat.beating) {
|
||||
if ((mperror_heart_beat.on_time = HAL_GetTick()) - mperror_heart_beat.off_time > MPERROR_HEARTBEAT_OFF_MS) {
|
||||
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, MICROPY_SYS_LED_PORT_PIN);
|
||||
@@ -198,10 +203,21 @@ void nlr_jump_fail(void *val) {
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings
|
||||
|
||||
/// \classmethod \constructor()
|
||||
///
|
||||
/// Return the heart beat object
|
||||
STATIC mp_obj_t pyb_heartbeat_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
// check arguments
|
||||
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
||||
|
||||
// return constant object
|
||||
return (mp_obj_t)&pyb_heartbeat_obj;
|
||||
}
|
||||
|
||||
/// \function enable()
|
||||
/// Enables the heartbeat signal
|
||||
STATIC mp_obj_t pyb_enable_heartbeat(mp_obj_t self) {
|
||||
mperror_enable_heartbeat ();
|
||||
mperror_heart_beat.enabled = true;
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_enable_heartbeat_obj, pyb_enable_heartbeat);
|
||||
@@ -209,7 +225,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_enable_heartbeat_obj, pyb_enable_heartbeat)
|
||||
/// \function disable()
|
||||
/// Disables the heartbeat signal
|
||||
STATIC mp_obj_t pyb_disable_heartbeat(mp_obj_t self) {
|
||||
mperror_disable_heartbeat ();
|
||||
mperror_heart_beat.do_disable = true;
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_disable_heartbeat_obj, pyb_disable_heartbeat);
|
||||
@@ -220,11 +236,11 @@ STATIC const mp_map_elem_t pyb_heartbeat_locals_dict_table[] = {
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(pyb_heartbeat_locals_dict, pyb_heartbeat_locals_dict_table);
|
||||
|
||||
static const mp_obj_type_t pyb_heartbeat_type = {
|
||||
const mp_obj_type_t pyb_heartbeat_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_HeartBeat,
|
||||
.make_new = pyb_heartbeat_make_new,
|
||||
.locals_dict = (mp_obj_t)&pyb_heartbeat_locals_dict,
|
||||
};
|
||||
|
||||
const mp_obj_base_t pyb_heartbeat_obj = {&pyb_heartbeat_type};
|
||||
#endif
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#define MPERROR_H_
|
||||
|
||||
#ifndef BOOTLOADER
|
||||
extern const mp_obj_base_t pyb_heartbeat_obj;
|
||||
extern const mp_obj_type_t pyb_heartbeat_type;
|
||||
#endif
|
||||
|
||||
extern void NORETURN __fatal_error(const char *msg);
|
||||
@@ -38,9 +38,7 @@ void mperror_init0 (void);
|
||||
void mperror_bootloader_check_reset_cause (void);
|
||||
void mperror_deinit_sfe_pin (void);
|
||||
void mperror_signal_error (void);
|
||||
void mperror_enable_heartbeat (void);
|
||||
void mperror_heartbeat_switch_off (void);
|
||||
void mperror_disable_heartbeat (void);
|
||||
void mperror_heartbeat_signal (void);
|
||||
|
||||
#endif // MPERROR_H_
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "irq.h"
|
||||
#include "mpsystick.h"
|
||||
#include "systick.h"
|
||||
#include "inc/hw_types.h"
|
||||
|
||||
@@ -30,14 +30,26 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "inc/hw_types.h"
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "pybpin.h"
|
||||
#include MICROPY_HAL_H
|
||||
|
||||
|
||||
STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pin_named_pins_obj_t *self = self_in;
|
||||
mp_printf(print, "<Pin.%q>", self->name);
|
||||
}
|
||||
|
||||
const mp_obj_type_t pin_cpu_pins_obj_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_cpu,
|
||||
.print = pin_named_pins_obj_print,
|
||||
.locals_dict = (mp_obj_t)&pin_cpu_pins_locals_dict,
|
||||
};
|
||||
|
||||
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
|
||||
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
|
||||
mp_map_elem_t *named_elem = mp_map_lookup(named_map, name, MP_MAP_LOOKUP);
|
||||
|
||||
@@ -25,85 +25,76 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <std.h>
|
||||
|
||||
#include "py/mpstate.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "modnetwork.h"
|
||||
#include "mpexception.h"
|
||||
#include "serverstask.h"
|
||||
#include "simplelink.h"
|
||||
|
||||
|
||||
/// \module network - network configuration
|
||||
///
|
||||
/// This module provides network drivers and routing configuration.
|
||||
/// This module provides network drivers and server configuration.
|
||||
|
||||
void mod_network_init0(void) {
|
||||
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
|
||||
}
|
||||
|
||||
void mod_network_register_nic(mp_obj_t nic) {
|
||||
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
|
||||
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
|
||||
// nic already registered
|
||||
return;
|
||||
}
|
||||
}
|
||||
// nic not registered so add to list
|
||||
mp_obj_list_append(&MP_STATE_PORT(mod_network_nic_list), nic);
|
||||
}
|
||||
|
||||
mp_obj_t mod_network_find_nic(void) {
|
||||
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
|
||||
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
|
||||
return nic;
|
||||
}
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
|
||||
}
|
||||
|
||||
STATIC mp_obj_t network_route(void) {
|
||||
return &MP_STATE_PORT(mod_network_nic_list);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
|
||||
|
||||
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
|
||||
STATIC mp_obj_t network_server_start(void) {
|
||||
servers_start();
|
||||
return mp_const_none;
|
||||
STATIC mp_obj_t network_server_running(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
if (n_args > 0) {
|
||||
// set
|
||||
if (mp_obj_is_true(args[0])) {
|
||||
servers_start();
|
||||
} else {
|
||||
servers_stop();
|
||||
}
|
||||
return mp_const_none;
|
||||
} else {
|
||||
// get
|
||||
return MP_BOOL(servers_are_enabled());
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_server_start_obj, network_server_start);
|
||||
|
||||
STATIC mp_obj_t network_server_stop(void) {
|
||||
servers_stop();
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_server_stop_obj, network_server_stop);
|
||||
|
||||
STATIC mp_obj_t network_server_enabled(void) {
|
||||
return MP_BOOL(servers_are_enabled());
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_server_enabled_obj, network_server_enabled);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_server_running_obj, 0, 1, network_server_running);
|
||||
|
||||
STATIC mp_obj_t network_server_login(mp_obj_t user, mp_obj_t pass) {
|
||||
const char *_user = mp_obj_str_get_str(user);
|
||||
const char *_pass = mp_obj_str_get_str(pass);
|
||||
if (strlen(user) > SERVERS_USER_PASS_LEN_MAX || strlen(pass) > SERVERS_USER_PASS_LEN_MAX) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
servers_set_login ((char *)_user, (char *)_pass);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(network_server_login_obj, network_server_login);
|
||||
|
||||
// timeout value given in seconds
|
||||
STATIC mp_obj_t network_server_timeout(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
if (n_args > 0) {
|
||||
uint32_t _timeout = mp_obj_get_int(args[0]);
|
||||
if (!servers_set_timeout(_timeout * 1000)) {
|
||||
// timeout is too low
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
return mp_const_none;
|
||||
} else {
|
||||
// get
|
||||
return mp_obj_new_int(servers_get_timeout() / 1000);
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_server_timeout_obj, 0, 1, network_server_timeout);
|
||||
#endif
|
||||
|
||||
STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&mod_network_nic_type_wlan },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_route), (mp_obj_t)&network_route_obj },
|
||||
|
||||
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_start_server), (mp_obj_t)&network_server_start_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_stop_server), (mp_obj_t)&network_server_stop_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_server_enabled), (mp_obj_t)&network_server_enabled_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_server_running), (mp_obj_t)&network_server_running_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_server_login), (mp_obj_t)&network_server_login_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_server_timeout), (mp_obj_t)&network_server_timeout_obj },
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -28,53 +28,47 @@
|
||||
#ifndef MODNETWORK_H_
|
||||
#define MODNETWORK_H_
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define MOD_NETWORK_IPV4ADDR_BUF_SIZE (4)
|
||||
|
||||
// Forward declaration
|
||||
struct _mod_network_socket_obj_t;
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE TYPES
|
||||
******************************************************************************/
|
||||
typedef struct _mod_network_nic_type_t {
|
||||
mp_obj_type_t base;
|
||||
|
||||
// API for non-socket operations
|
||||
int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out, uint8_t family);
|
||||
|
||||
// API for socket operations; return -1 on error
|
||||
int (*socket)(struct _mod_network_socket_obj_t *s, int *_errno);
|
||||
void (*close)(struct _mod_network_socket_obj_t *socket);
|
||||
int (*bind)(struct _mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno);
|
||||
int (*listen)(struct _mod_network_socket_obj_t *s, mp_int_t backlog, int *_errno);
|
||||
int (*accept)(struct _mod_network_socket_obj_t *s, struct _mod_network_socket_obj_t *s2, byte *ip, mp_uint_t *port, int *_errno);
|
||||
int (*connect)(struct _mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno);
|
||||
int (*send)(struct _mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno);
|
||||
int (*recv)(struct _mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno);
|
||||
int (*sendto)(struct _mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno);
|
||||
int (*recvfrom)(struct _mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno);
|
||||
int (*setsockopt)(struct _mod_network_socket_obj_t *s, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
|
||||
int (*settimeout)(struct _mod_network_socket_obj_t *s, mp_uint_t timeout_ms, int *_errno);
|
||||
int (*ioctl)(struct _mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno);
|
||||
} mod_network_nic_type_t;
|
||||
|
||||
typedef struct _mod_network_socket_obj_t {
|
||||
mp_obj_base_t base;
|
||||
mp_obj_t nic;
|
||||
mod_network_nic_type_t *nic_type;
|
||||
typedef struct _mod_network_socket_base_t {
|
||||
union {
|
||||
struct {
|
||||
// this order is important so that fileno gets > 0 once
|
||||
// the socket descriptor is assigned after being created.
|
||||
uint8_t domain;
|
||||
int8_t fileno;
|
||||
uint8_t type;
|
||||
uint8_t proto;
|
||||
int8_t fileno;
|
||||
} u_param;
|
||||
int16_t sd;
|
||||
};
|
||||
bool closed;
|
||||
bool has_timeout;
|
||||
bool cert_req;
|
||||
} mod_network_socket_base_t;
|
||||
|
||||
typedef struct _mod_network_socket_obj_t {
|
||||
mp_obj_base_t base;
|
||||
mod_network_socket_base_t sock_base;
|
||||
} mod_network_socket_obj_t;
|
||||
|
||||
/******************************************************************************
|
||||
EXPORTED DATA
|
||||
******************************************************************************/
|
||||
extern const mod_network_nic_type_t mod_network_nic_type_wlan;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE FUNCTIONS
|
||||
******************************************************************************/
|
||||
void mod_network_init0(void);
|
||||
void mod_network_register_nic(mp_obj_t nic);
|
||||
mp_obj_t mod_network_find_nic(void);
|
||||
|
||||
#endif // MODNETWORK_H_
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "inc/hw_uart.h"
|
||||
#include "rom_map.h"
|
||||
#include "prcm.h"
|
||||
#include "pyexec.h"
|
||||
#include "pybuart.h"
|
||||
@@ -44,6 +45,7 @@
|
||||
#include "pybrtc.h"
|
||||
#include "mpsystick.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "moduos.h"
|
||||
#include "telnet.h"
|
||||
@@ -62,6 +64,7 @@
|
||||
#include "pybwdt.h"
|
||||
#include "pybsleep.h"
|
||||
#include "pybspi.h"
|
||||
#include "pybtimer.h"
|
||||
#include "utils.h"
|
||||
#include "gccollect.h"
|
||||
#include "mperror.h"
|
||||
@@ -79,17 +82,17 @@ extern OsiTaskHandle xSimpleLinkSpawnTaskHndl;
|
||||
///
|
||||
/// The `pyb` module contains specific functions related to the pyboard.
|
||||
|
||||
/// \function hard_reset()
|
||||
/// Resets the pyboard in a manner similar to pushing the external RESET
|
||||
/// button.
|
||||
STATIC mp_obj_t pyb_hard_reset(void) {
|
||||
/// \function reset()
|
||||
/// Resets the pyboard in a manner similar to pushing the external
|
||||
/// reset button.
|
||||
STATIC mp_obj_t pyb_reset(void) {
|
||||
// disable wlan
|
||||
wlan_stop(SL_STOP_TIMEOUT_LONG);
|
||||
// perform a SoC reset
|
||||
PRCMSOCReset();
|
||||
// reset the cpu and it's peripherals
|
||||
MAP_PRCMMCUReset(true);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_hard_reset_obj, pyb_hard_reset);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_reset_obj, pyb_reset);
|
||||
|
||||
#ifdef DEBUG
|
||||
/// \function info([dump_alloc_table])
|
||||
@@ -123,10 +126,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
|
||||
/// \function freq()
|
||||
/// Returns the CPU frequency: (F_CPU).
|
||||
STATIC mp_obj_t pyb_freq(void) {
|
||||
return mp_obj_new_int(HAL_FCPU_HZ);
|
||||
mp_obj_t tuple[1] = {
|
||||
mp_obj_new_int(HAL_FCPU_HZ),
|
||||
};
|
||||
return mp_obj_new_tuple(1, tuple);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_freq_obj, pyb_freq);
|
||||
|
||||
/// \function unique_id()
|
||||
/// Returns a string of 6 bytes (48 bits), which is the unique ID for the MCU.
|
||||
STATIC mp_obj_t pyb_unique_id(void) {
|
||||
uint8_t mac[SL_BSSID_LENGTH];
|
||||
wlan_get_mac (mac);
|
||||
return mp_obj_new_bytes(mac, SL_BSSID_LENGTH);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
|
||||
|
||||
/// \function millis()
|
||||
/// Returns the number of milliseconds since the board was last reset.
|
||||
///
|
||||
@@ -238,11 +253,12 @@ MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
|
||||
STATIC const mp_map_elem_t pyb_module_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
|
||||
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&pyb_hard_reset_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&pyb_reset_obj },
|
||||
#ifdef DEBUG
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj },
|
||||
#endif
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_unique_id), (mp_obj_t)&pyb_unique_id_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_info), (mp_obj_t)&pyb_set_repl_info_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&pyb_repl_uart_obj },
|
||||
|
||||
@@ -263,7 +279,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
|
||||
#endif
|
||||
|
||||
#if MICROPY_HW_ENABLE_RTC
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type },
|
||||
#endif
|
||||
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_type },
|
||||
@@ -271,9 +287,10 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&pyb_spi_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&pyb_wdt_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&pyb_wdt_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Sleep), (mp_obj_t)&pyb_sleep_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_HeartBeat), (mp_obj_t)&pyb_heartbeat_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_HeartBeat), (mp_obj_t)&pyb_heartbeat_type },
|
||||
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sd_type },
|
||||
|
||||
64
cc3200/mods/modubinascii.c
Normal file
64
cc3200/mods/modubinascii.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Paul Sokolovsky
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/nlr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/binary.h"
|
||||
#include "extmod/modubinascii.h"
|
||||
#include "modubinascii.h"
|
||||
#include "inc/hw_types.h"
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_nvic.h"
|
||||
#include "inc/hw_dthe.h"
|
||||
#include "hw_memmap.h"
|
||||
#include "rom_map.h"
|
||||
#include "prcm.h"
|
||||
#include "crc.h"
|
||||
#include "cryptohash.h"
|
||||
#include "mpexception.h"
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings
|
||||
|
||||
STATIC const mp_map_elem_t mp_module_binascii_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ubinascii) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_hexlify), (mp_obj_t)&mod_binascii_hexlify_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_unhexlify), (mp_obj_t)&mod_binascii_unhexlify_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_a2b_base64), (mp_obj_t)&mod_binascii_a2b_base64_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_b2a_base64), (mp_obj_t)&mod_binascii_b2a_base64_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);
|
||||
|
||||
const mp_obj_module_t mp_module_ubinascii = {
|
||||
.base = { &mp_type_module },
|
||||
.name = MP_QSTR_ubinascii,
|
||||
.globals = (mp_obj_dict_t*)&mp_module_binascii_globals,
|
||||
};
|
||||
31
cc3200/mods/modubinascii.h
Normal file
31
cc3200/mods/modubinascii.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Paul Sokolovsky
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MODUBINASCII_H_
|
||||
#define MODUBINASCII_H_
|
||||
|
||||
|
||||
#endif /* MODUBINASCII_H_ */
|
||||
210
cc3200/mods/moduhashlib.c
Normal file
210
cc3200/mods/moduhashlib.c
Normal file
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Paul Sokolovsky
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/nlr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "inc/hw_types.h"
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_nvic.h"
|
||||
#include "inc/hw_shamd5.h"
|
||||
#include "inc/hw_dthe.h"
|
||||
#include "hw_memmap.h"
|
||||
#include "rom_map.h"
|
||||
#include "prcm.h"
|
||||
#include "shamd5.h"
|
||||
#include "cryptohash.h"
|
||||
#include "mpexception.h"
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE TYPES
|
||||
******************************************************************************/
|
||||
typedef struct _mp_obj_hash_t {
|
||||
mp_obj_base_t base;
|
||||
uint8_t *buffer;
|
||||
uint32_t b_size;
|
||||
uint32_t c_size;
|
||||
uint8_t algo;
|
||||
uint8_t h_size;
|
||||
bool fixedlen;
|
||||
bool digested;
|
||||
uint8_t hash[32];
|
||||
} mp_obj_hash_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest);
|
||||
STATIC mp_obj_t hash_read (mp_obj_t self_in);
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest) {
|
||||
mp_obj_hash_t *self = self_in;
|
||||
mp_buffer_info_t bufinfo;
|
||||
|
||||
if (data) {
|
||||
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
|
||||
}
|
||||
|
||||
if (digest) {
|
||||
CRYPTOHASH_SHAMD5Start (self->algo, self->b_size);
|
||||
}
|
||||
|
||||
if (self->c_size < self->b_size || !data || !self->fixedlen) {
|
||||
if (digest || self->fixedlen) {
|
||||
// no data means we want to process our internal buffer
|
||||
CRYPTOHASH_SHAMD5Update (data ? bufinfo.buf : self->buffer, data ? bufinfo.len : self->b_size);
|
||||
self->c_size += data ? bufinfo.len : 0;
|
||||
} else {
|
||||
self->buffer = m_renew(byte, self->buffer, self->b_size, self->b_size + bufinfo.len);
|
||||
mp_seq_copy((byte*)self->buffer + self->b_size, bufinfo.buf, bufinfo.len, byte);
|
||||
self->b_size += bufinfo.len;
|
||||
self->digested = false;
|
||||
}
|
||||
} else {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t hash_read (mp_obj_t self_in) {
|
||||
mp_obj_hash_t *self = self_in;
|
||||
|
||||
if (!self->fixedlen) {
|
||||
if (!self->digested) {
|
||||
hash_update_internal(self, MP_OBJ_NULL, true);
|
||||
}
|
||||
} else if (self->c_size < self->b_size) {
|
||||
// it's a fixed len block which is still incomplete
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
|
||||
}
|
||||
|
||||
if (!self->digested) {
|
||||
CRYPTOHASH_SHAMD5Read ((uint8_t *)self->hash);
|
||||
self->digested = true;
|
||||
}
|
||||
return mp_obj_new_bytes(self->hash, self->h_size);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings
|
||||
|
||||
/// \classmethod \constructor([data[, block_size]])
|
||||
/// initial data must be given if block_size wants to be passed
|
||||
STATIC mp_obj_t hash_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 2, false);
|
||||
mp_obj_hash_t *self = m_new0(mp_obj_hash_t, 1);
|
||||
self->base.type = type_in;
|
||||
if (self->base.type->name == MP_QSTR_sha1) {
|
||||
self->algo = SHAMD5_ALGO_SHA1;
|
||||
self->h_size = 20;
|
||||
} else /* if (self->base.type->name == MP_QSTR_sha256) */ {
|
||||
self->algo = SHAMD5_ALGO_SHA256;
|
||||
self->h_size = 32;
|
||||
} /* else {
|
||||
self->algo = SHAMD5_ALGO_MD5;
|
||||
self->h_size = 32;
|
||||
} */
|
||||
|
||||
if (n_args) {
|
||||
// CPython extension to avoid buffering the data before digesting it
|
||||
// Note: care must be taken to provide all intermediate blocks as multiple
|
||||
// of four bytes, otherwise the resulting hash will be incorrect.
|
||||
// the final block can be of any length
|
||||
if (n_args > 1) {
|
||||
// block size given, we will feed the data directly into the hash engine
|
||||
self->fixedlen = true;
|
||||
self->b_size = mp_obj_get_int(args[1]);
|
||||
hash_update_internal(self, args[0], true);
|
||||
} else {
|
||||
hash_update_internal(self, args[0], false);
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
|
||||
mp_obj_hash_t *self = self_in;
|
||||
hash_update_internal(self, arg, false);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
|
||||
|
||||
STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
|
||||
return hash_read(self_in);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
|
||||
|
||||
STATIC const mp_map_elem_t hash_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t) &hash_update_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_digest), (mp_obj_t) &hash_digest_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(hash_locals_dict, hash_locals_dict_table);
|
||||
|
||||
//STATIC const mp_obj_type_t md5_type = {
|
||||
// { &mp_type_type },
|
||||
// .name = MP_QSTR_md5,
|
||||
// .make_new = hash_make_new,
|
||||
// .locals_dict = (mp_obj_t)&hash_locals_dict,
|
||||
//};
|
||||
|
||||
STATIC const mp_obj_type_t sha1_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_sha1,
|
||||
.make_new = hash_make_new,
|
||||
.locals_dict = (mp_obj_t)&hash_locals_dict,
|
||||
};
|
||||
|
||||
STATIC const mp_obj_type_t sha256_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_sha256,
|
||||
.make_new = hash_make_new,
|
||||
.locals_dict = (mp_obj_t)&hash_locals_dict,
|
||||
};
|
||||
|
||||
STATIC const mp_map_elem_t mp_module_hashlib_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib) },
|
||||
// { MP_OBJ_NEW_QSTR(MP_QSTR_md5), (mp_obj_t)&md5_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sha1), (mp_obj_t)&sha1_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sha256), (mp_obj_t)&sha256_type },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_hashlib_globals, mp_module_hashlib_globals_table);
|
||||
|
||||
const mp_obj_module_t mp_module_uhashlib = {
|
||||
.base = { &mp_type_module },
|
||||
.name = MP_QSTR_uhashlib,
|
||||
.globals = (mp_obj_dict_t*)&mp_module_hashlib_globals,
|
||||
};
|
||||
|
||||
@@ -150,9 +150,11 @@ STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
if (path[0] == '/' && path[1] == '\0') {
|
||||
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
|
||||
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_flash));
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
if (sd_in_root()) {
|
||||
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_sd));
|
||||
}
|
||||
#endif
|
||||
return dir_list;
|
||||
}
|
||||
|
||||
@@ -219,6 +221,22 @@ STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
|
||||
|
||||
/// \function rename(old_path, new_path)
|
||||
/// Rename a file
|
||||
STATIC mp_obj_t os_rename(mp_obj_t path_in, mp_obj_t path_out) {
|
||||
const char *old_path = mp_obj_str_get_str(path_in);
|
||||
const char *new_path = mp_obj_str_get_str(path_out);
|
||||
FRESULT res = f_rename(old_path, new_path);
|
||||
switch (res) {
|
||||
case FR_OK:
|
||||
return mp_const_none;
|
||||
default:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
||||
}
|
||||
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename);
|
||||
|
||||
/// \function remove(path)
|
||||
/// Remove a file or a directory
|
||||
STATIC mp_obj_t os_remove(mp_obj_t path_o) {
|
||||
@@ -335,16 +353,16 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
|
||||
#endif
|
||||
|
||||
/// \function mkdisk('path')
|
||||
/// \function mkfs('path')
|
||||
/// Formats the selected drive, useful when the filesystem has been damaged beyond repair
|
||||
STATIC mp_obj_t os_mkdisk(mp_obj_t path_o) {
|
||||
STATIC mp_obj_t os_mkfs(mp_obj_t path_o) {
|
||||
const char *path = mp_obj_str_get_str(path_o);
|
||||
if (FR_OK != f_mkfs(path, 1, 0)) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdisk_obj, os_mkdisk);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkfs_obj, os_mkfs);
|
||||
|
||||
STATIC const mp_map_elem_t os_module_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },
|
||||
@@ -354,6 +372,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&os_getcwd_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&os_listdir_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename), (mp_obj_t)&os_rename_obj},
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_remove_obj }, // rmdir aliases to remove
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
|
||||
@@ -362,7 +381,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
|
||||
#if MICROPY_HW_ENABLE_RNG
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
|
||||
#endif
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdisk), (mp_obj_t)&os_mkdisk_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkfs), (mp_obj_t)&os_mkfs_obj },
|
||||
|
||||
/// \constant sep - separation character used in paths
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },
|
||||
|
||||
@@ -29,67 +29,141 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "simplelink.h"
|
||||
#include "py/mpstate.h"
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/stream.h"
|
||||
#include "netutils.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "modusocket.h"
|
||||
#include "mpexception.h"
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define MOD_NETWORK_MAX_SOCKETS 10
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE TYPES
|
||||
******************************************************************************/
|
||||
typedef struct {
|
||||
int16_t sd;
|
||||
bool user;
|
||||
} modusocket_sock_t;
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
STATIC const mp_obj_type_t socket_type;
|
||||
STATIC OsiLockObj_t modusocket_LockObj;
|
||||
STATIC modusocket_sock_t modusocket_sockets[MOD_NETWORK_MAX_SOCKETS] = {{.sd = -1}, {.sd = -1}, {.sd = -1}, {.sd = -1}, {.sd = -1},
|
||||
{.sd = -1}, {.sd = -1}, {.sd = -1}, {.sd = -1}, {.sd = -1}};
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
__attribute__ ((section (".boot")))
|
||||
void modusocket_pre_init (void) {
|
||||
// create the wlan lock
|
||||
ASSERT(OSI_OK == sl_LockObjCreate(&modusocket_LockObj, "SockLock"));
|
||||
sl_LockObjUnlock (&modusocket_LockObj);
|
||||
}
|
||||
|
||||
void modusocket_socket_add (int16_t sd, bool user) {
|
||||
sl_LockObjLock (&modusocket_LockObj, SL_OS_WAIT_FOREVER);
|
||||
for (int i = 0; i < MOD_NETWORK_MAX_SOCKETS; i++) {
|
||||
if (modusocket_sockets[i].sd < 0) {
|
||||
modusocket_sockets[i].sd = sd;
|
||||
modusocket_sockets[i].user = user;
|
||||
break;
|
||||
}
|
||||
}
|
||||
sl_LockObjUnlock (&modusocket_LockObj);
|
||||
}
|
||||
|
||||
void modusocket_socket_delete (int16_t sd) {
|
||||
sl_LockObjLock (&modusocket_LockObj, SL_OS_WAIT_FOREVER);
|
||||
for (int i = 0; i < MOD_NETWORK_MAX_SOCKETS; i++) {
|
||||
if (modusocket_sockets[i].sd == sd) {
|
||||
modusocket_sockets[i].sd = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
sl_LockObjUnlock (&modusocket_LockObj);
|
||||
}
|
||||
|
||||
void modusocket_enter_sleep (void) {
|
||||
fd_set socketset;
|
||||
int16_t maxfd = 0;
|
||||
|
||||
for (int i = 0; i < MOD_NETWORK_MAX_SOCKETS; i++) {
|
||||
int16_t sd;
|
||||
if ((sd = modusocket_sockets[i].sd) >= 0) {
|
||||
FD_SET(sd, &socketset);
|
||||
maxfd = (maxfd > sd) ? maxfd : sd;
|
||||
}
|
||||
}
|
||||
|
||||
// wait for any of the sockets to become ready...
|
||||
sl_Select(maxfd + 1, &socketset, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
void modusocket_close_all_user_sockets (void) {
|
||||
sl_LockObjLock (&modusocket_LockObj, SL_OS_WAIT_FOREVER);
|
||||
for (int i = 0; i < MOD_NETWORK_MAX_SOCKETS; i++) {
|
||||
if (modusocket_sockets[i].sd >= 0 && modusocket_sockets[i].user) {
|
||||
sl_Close(modusocket_sockets[i].sd);
|
||||
modusocket_sockets[i].sd = -1;
|
||||
}
|
||||
}
|
||||
sl_LockObjUnlock (&modusocket_LockObj);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// socket class
|
||||
|
||||
STATIC const mp_obj_type_t socket_type;
|
||||
|
||||
STATIC void socket_select_nic(mod_network_socket_obj_t *self) {
|
||||
// select a nic
|
||||
self->nic = mod_network_find_nic();
|
||||
self->nic_type = (mod_network_nic_type_t*)mp_obj_get_type(self->nic);
|
||||
|
||||
// call the nic to open the socket
|
||||
int _errno;
|
||||
if (self->nic_type->socket(self, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
}
|
||||
}
|
||||
|
||||
// constructor socket(family=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, fileno=None)
|
||||
STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 4, false);
|
||||
|
||||
// create socket object (not bound to any NIC yet)
|
||||
// create socket object
|
||||
mod_network_socket_obj_t *s = m_new_obj_with_finaliser(mod_network_socket_obj_t);
|
||||
s->base.type = (mp_obj_t)&socket_type;
|
||||
s->nic = MP_OBJ_NULL;
|
||||
s->nic_type = NULL;
|
||||
s->u_param.domain = AF_INET;
|
||||
s->u_param.type = SOCK_STREAM;
|
||||
s->u_param.proto = IPPROTO_TCP;
|
||||
s->u_param.fileno = -1;
|
||||
if (n_args >= 1) {
|
||||
s->u_param.domain = mp_obj_get_int(args[0]);
|
||||
if (n_args >= 2) {
|
||||
s->u_param.type = mp_obj_get_int(args[1]);
|
||||
if (n_args >= 3) {
|
||||
s->u_param.proto = mp_obj_get_int(args[2]);
|
||||
if (n_args == 4) {
|
||||
s->u_param.fileno = mp_obj_get_int(args[3]);
|
||||
s->sock_base.u_param.domain = AF_INET;
|
||||
s->sock_base.u_param.type = SOCK_STREAM;
|
||||
s->sock_base.u_param.proto = IPPROTO_TCP;
|
||||
s->sock_base.u_param.fileno = -1;
|
||||
s->sock_base.has_timeout = false;
|
||||
s->sock_base.cert_req = false;
|
||||
|
||||
if (n_args > 0) {
|
||||
s->sock_base.u_param.domain = mp_obj_get_int(args[0]);
|
||||
if (n_args > 1) {
|
||||
s->sock_base.u_param.type = mp_obj_get_int(args[1]);
|
||||
if (n_args > 2) {
|
||||
s->sock_base.u_param.proto = mp_obj_get_int(args[2]);
|
||||
if (n_args > 3) {
|
||||
s->sock_base.u_param.fileno = mp_obj_get_int(args[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
socket_select_nic(s);
|
||||
|
||||
// create the socket
|
||||
int _errno;
|
||||
if (wlan_socket_socket(s, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// method socket.close()
|
||||
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
if (self->nic != MP_OBJ_NULL) {
|
||||
self->nic_type->close(self);
|
||||
}
|
||||
wlan_socket_close(self);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
|
||||
@@ -104,10 +178,9 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
||||
|
||||
// call the NIC to bind the socket
|
||||
int _errno;
|
||||
if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
if (wlan_socket_bind(self, ip, port, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
|
||||
@@ -116,16 +189,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
|
||||
STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
|
||||
if (self->nic == MP_OBJ_NULL) {
|
||||
// not connected
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN)));
|
||||
}
|
||||
|
||||
int _errno;
|
||||
if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
if (wlan_socket_listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
|
||||
@@ -135,29 +202,25 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
|
||||
// create new socket object
|
||||
// starts with empty NIC so that finaliser doesn't run close() method if accept() fails
|
||||
mod_network_socket_obj_t *socket2 = m_new_obj_with_finaliser(mod_network_socket_obj_t);
|
||||
socket2->base.type = (mp_obj_t)&socket_type;
|
||||
socket2->nic = MP_OBJ_NULL;
|
||||
socket2->nic_type = NULL;
|
||||
// the new socket inherits all properties from its parent
|
||||
memcpy (socket2, self, sizeof(mod_network_socket_obj_t));
|
||||
|
||||
// accept incoming connection
|
||||
// accept the incoming connection
|
||||
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||
mp_uint_t port;
|
||||
int _errno;
|
||||
if (self->nic_type->accept(self, socket2, ip, &port, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
if (wlan_socket_accept(self, socket2, ip, &port, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
|
||||
// new socket has valid state, so set the NIC to the same as parent
|
||||
socket2->nic = self->nic;
|
||||
socket2->nic_type = self->nic_type;
|
||||
// add the socket to the list
|
||||
modusocket_socket_add(socket2->sock_base.sd, true);
|
||||
|
||||
// make the return value
|
||||
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
|
||||
client->items[0] = socket2;
|
||||
client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_LITTLE);
|
||||
|
||||
return client;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
|
||||
@@ -170,12 +233,14 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
||||
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
|
||||
|
||||
// call the NIC to connect the socket
|
||||
// connect the socket
|
||||
int _errno;
|
||||
if (self->nic_type->connect(self, ip, port, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
if (wlan_socket_connect(self, ip, port, &_errno) != 0) {
|
||||
if (!self->sock_base.cert_req && _errno == SL_ESECSNOVERIFY) {
|
||||
return mp_const_none;
|
||||
}
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
||||
@@ -183,16 +248,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
||||
// method socket.send(bytes)
|
||||
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
if (self->nic == MP_OBJ_NULL) {
|
||||
// not connected
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(EBADF)));
|
||||
}
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
|
||||
int _errno;
|
||||
mp_uint_t ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno);
|
||||
if (ret == -1) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
mp_int_t ret = wlan_socket_send(self, bufinfo.buf, bufinfo.len, &_errno);
|
||||
if (ret < 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
return mp_obj_new_int_from_uint(ret);
|
||||
}
|
||||
@@ -201,17 +262,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
|
||||
// method socket.recv(bufsize)
|
||||
STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
if (self->nic == MP_OBJ_NULL) {
|
||||
// not connected
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN)));
|
||||
}
|
||||
mp_int_t len = mp_obj_get_int(len_in);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, len);
|
||||
int _errno;
|
||||
mp_uint_t ret = self->nic_type->recv(self, (byte*)vstr.buf, len, &_errno);
|
||||
if (ret == -1) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
mp_int_t ret = wlan_socket_recv(self, (byte*)vstr.buf, len, &_errno);
|
||||
if (ret < 0) {
|
||||
if (_errno == EAGAIN && self->sock_base.has_timeout) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
|
||||
}
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
if (ret == 0) {
|
||||
return mp_const_empty_bytes;
|
||||
@@ -234,13 +294,12 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_
|
||||
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
|
||||
|
||||
// call the NIC to sendto
|
||||
// call the nic to sendto
|
||||
int _errno;
|
||||
mp_int_t ret = self->nic_type->sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
|
||||
if (ret == -1) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
mp_int_t ret = wlan_socket_sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
|
||||
if (ret < 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
|
||||
return mp_obj_new_int(ret);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(socket_sendto_obj, socket_sendto);
|
||||
@@ -248,18 +307,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(socket_sendto_obj, socket_sendto);
|
||||
// method socket.recvfrom(bufsize)
|
||||
STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
if (self->nic == MP_OBJ_NULL) {
|
||||
// not connected
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN)));
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, mp_obj_get_int(len_in));
|
||||
byte ip[4];
|
||||
mp_uint_t port;
|
||||
int _errno;
|
||||
mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
|
||||
if (ret == -1) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
mp_int_t ret = wlan_socket_recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
|
||||
if (ret < 0) {
|
||||
if (_errno == EAGAIN && self->sock_base.has_timeout) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
|
||||
}
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
mp_obj_t tuple[2];
|
||||
if (ret == 0) {
|
||||
@@ -296,10 +354,9 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
|
||||
int _errno;
|
||||
if (self->nic_type->setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
if (wlan_socket_setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
|
||||
@@ -310,19 +367,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_s
|
||||
// otherwise, timeout is in seconds
|
||||
STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
if (self->nic == MP_OBJ_NULL) {
|
||||
// not connected
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN)));
|
||||
}
|
||||
mp_uint_t timeout;
|
||||
if (timeout_in == mp_const_none) {
|
||||
timeout = -1;
|
||||
} else {
|
||||
timeout = 1000 * mp_obj_get_int(timeout_in);
|
||||
timeout = mp_obj_get_int(timeout_in);
|
||||
}
|
||||
int _errno;
|
||||
if (self->nic_type->settimeout(self, timeout, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
if (wlan_socket_settimeout(self, timeout, &_errno) != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
@@ -339,29 +392,68 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
|
||||
|
||||
STATIC const mp_map_elem_t socket_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sendto), (mp_obj_t)&socket_sendto_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_recvfrom), (mp_obj_t)&socket_recvfrom_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_settimeout), (mp_obj_t)&socket_settimeout_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_setblocking), (mp_obj_t)&socket_setblocking_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sendall), (mp_obj_t)&socket_send_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sendto), (mp_obj_t)&socket_sendto_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_recvfrom), (mp_obj_t)&socket_recvfrom_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_settimeout), (mp_obj_t)&socket_settimeout_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_setblocking), (mp_obj_t)&socket_setblocking_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&mp_identity_obj },
|
||||
|
||||
// stream methods
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
|
||||
MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
|
||||
|
||||
mp_uint_t socket_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
|
||||
STATIC mp_uint_t socket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
return self->nic_type->ioctl(self, request, arg, errcode);
|
||||
mp_int_t ret = wlan_socket_recv(self, buf, size, errcode);
|
||||
if (ret < 0) {
|
||||
// we need to ignore the socket closed error here because a readall() or read() without params
|
||||
// only returns when the socket is closed by the other end
|
||||
if (*errcode != SL_ESECCLOSED) {
|
||||
ret = MP_STREAM_ERROR;
|
||||
// needed to convert simplelink's negative error codes to POSIX
|
||||
(*errcode) *= -1;
|
||||
} else {
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
STATIC const mp_stream_p_t socket_stream_p = {
|
||||
STATIC mp_uint_t socket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
mp_int_t ret = wlan_socket_send(self, buf, size, errcode);
|
||||
if (ret < 0) {
|
||||
ret = MP_STREAM_ERROR;
|
||||
// needed to convert simplelink's negative error codes to POSIX
|
||||
(*errcode) *= -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
STATIC mp_uint_t socket_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
|
||||
mod_network_socket_obj_t *self = self_in;
|
||||
return wlan_socket_ioctl(self, request, arg, errcode);
|
||||
}
|
||||
|
||||
const mp_stream_p_t socket_stream_p = {
|
||||
.read = socket_read,
|
||||
.write = socket_write,
|
||||
.ioctl = socket_ioctl,
|
||||
.is_text = false,
|
||||
};
|
||||
@@ -384,28 +476,19 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
|
||||
const char *host = mp_obj_str_get_data(host_in, &hlen);
|
||||
mp_int_t port = mp_obj_get_int(port_in);
|
||||
|
||||
// find a NIC that can do a name lookup
|
||||
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
|
||||
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
|
||||
mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
|
||||
if (nic_type->gethostbyname != NULL) {
|
||||
// Only IPv4 is supported
|
||||
uint8_t out_ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||
int32_t result = nic_type->gethostbyname(nic, host, hlen, out_ip, AF_INET);
|
||||
if (result != 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(result)));
|
||||
}
|
||||
mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL);
|
||||
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(AF_INET);
|
||||
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM);
|
||||
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
|
||||
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
||||
tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_LITTLE);
|
||||
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
|
||||
}
|
||||
// ipv4 only
|
||||
uint8_t out_ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||
int32_t result = wlan_gethostbyname(host, hlen, out_ip, AF_INET);
|
||||
if (result < 0) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-result)));
|
||||
}
|
||||
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
|
||||
mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL);
|
||||
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(AF_INET);
|
||||
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM);
|
||||
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
|
||||
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
||||
tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_LITTLE);
|
||||
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_usocket_getaddrinfo_obj, mod_usocket_getaddrinfo);
|
||||
|
||||
@@ -415,6 +498,10 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_usocket_getaddrinfo_obj },
|
||||
|
||||
// class exceptions
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_error), (mp_obj_t)&mp_type_OSError },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), (mp_obj_t)&mp_type_TimeoutError },
|
||||
|
||||
// class constants
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) },
|
||||
|
||||
39
cc3200/mods/modusocket.h
Normal file
39
cc3200/mods/modusocket.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MODUSOCKET_H_
|
||||
#define MODUSOCKET_H_
|
||||
|
||||
extern const mp_obj_dict_t socket_locals_dict;
|
||||
extern const mp_stream_p_t socket_stream_p;
|
||||
|
||||
extern void modusocket_pre_init (void);
|
||||
extern void modusocket_socket_add (int16_t sd, bool user);
|
||||
extern void modusocket_socket_delete (int16_t sd);
|
||||
extern void modusocket_enter_sleep (void);
|
||||
extern void modusocket_close_all_user_sockets (void);
|
||||
|
||||
#endif /* MODUSOCKET_H_ */
|
||||
149
cc3200/mods/modussl.c
Normal file
149
cc3200/mods/modussl.c
Normal file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <std.h>
|
||||
|
||||
#include "simplelink.h"
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modusocket.h"
|
||||
#include "mpexception.h"
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define SSL_CERT_NONE (0)
|
||||
#define SSL_CERT_OPTIONAL (1)
|
||||
#define SSL_CERT_REQUIRED (2)
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE TYPES
|
||||
******************************************************************************/
|
||||
typedef struct _mp_obj_ssl_socket_t {
|
||||
mp_obj_base_t base;
|
||||
mod_network_socket_base_t sock_base;
|
||||
mp_obj_t o_sock;
|
||||
} mp_obj_ssl_socket_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
STATIC const mp_obj_type_t ssl_socket_type;
|
||||
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings; SSL class
|
||||
|
||||
// ssl socket inherits from normal socket, so we take its
|
||||
// locals and stream methods
|
||||
STATIC const mp_obj_type_t ssl_socket_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_ussl,
|
||||
.getiter = NULL,
|
||||
.iternext = NULL,
|
||||
.stream_p = &socket_stream_p,
|
||||
.locals_dict = (mp_obj_t)&socket_locals_dict,
|
||||
};
|
||||
|
||||
STATIC mp_obj_t mod_ssl_wrap_socket(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
STATIC const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_sock, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_keyfile, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
{ MP_QSTR_certfile, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} },
|
||||
{ MP_QSTR_cert_reqs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SSL_CERT_NONE} },
|
||||
{ MP_QSTR_ca_certs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
};
|
||||
|
||||
// parse arguments
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// chech if ca validation is required
|
||||
if (args[4].u_int != SSL_CERT_NONE && args[5].u_obj == mp_const_none) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
// server side param is irrelevant for us (at least for the moment)
|
||||
|
||||
// retrieve the file paths (with an 6 byte offset because to strip the '/flash' prefix)
|
||||
const char *keyfile = (args[1].u_obj == mp_const_none) ? NULL : &(mp_obj_str_get_str(args[1].u_obj)[6]);
|
||||
const char *certfile = (args[2].u_obj == mp_const_none) ? NULL : &(mp_obj_str_get_str(args[2].u_obj)[6]);
|
||||
const char *cafile = (args[5].u_obj == mp_const_none || args[4].u_int != SSL_CERT_REQUIRED) ?
|
||||
NULL : &(mp_obj_str_get_str(args[5].u_obj)[6]);
|
||||
|
||||
_i16 sd = ((mod_network_socket_obj_t *)args[0].u_obj)->sock_base.sd;
|
||||
_i16 _errno;
|
||||
if (keyfile && (_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, keyfile, strlen(keyfile))) < 0) {
|
||||
goto socket_error;
|
||||
}
|
||||
if (certfile && (_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, certfile, strlen(certfile))) < 0) {
|
||||
goto socket_error;
|
||||
}
|
||||
if (cafile && (_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME, cafile, strlen(cafile))) < 0) {
|
||||
goto socket_error;
|
||||
}
|
||||
|
||||
// create the ssl socket
|
||||
mp_obj_ssl_socket_t *ssl_sock = m_new_obj(mp_obj_ssl_socket_t);
|
||||
// ssl socket inherits all properties from the original socket
|
||||
memcpy (&ssl_sock->sock_base, &((mod_network_socket_obj_t *)args[0].u_obj)->sock_base, sizeof(mod_network_socket_base_t));
|
||||
ssl_sock->base.type = &ssl_socket_type;
|
||||
ssl_sock->sock_base.cert_req = (args[4].u_int == SSL_CERT_REQUIRED) ? true : false;
|
||||
ssl_sock->o_sock = args[0].u_obj;
|
||||
|
||||
return ssl_sock;
|
||||
|
||||
socket_error:
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 1, mod_ssl_wrap_socket);
|
||||
|
||||
STATIC const mp_map_elem_t mp_module_ussl_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ussl) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_wrap_socket), (mp_obj_t)&mod_ssl_wrap_socket_obj },
|
||||
|
||||
// class exceptions
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_SSLError), (mp_obj_t)&mp_type_OSError },
|
||||
|
||||
// class constants
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_CERT_NONE), MP_OBJ_NEW_SMALL_INT(SSL_CERT_NONE) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_CERT_OPTIONAL), MP_OBJ_NEW_SMALL_INT(SSL_CERT_OPTIONAL) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_CERT_REQUIRED), MP_OBJ_NEW_SMALL_INT(SSL_CERT_REQUIRED) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ussl_globals, mp_module_ussl_globals_table);
|
||||
|
||||
const mp_obj_module_t mp_module_ussl = {
|
||||
.base = { &mp_type_module },
|
||||
.name = MP_QSTR_ussl,
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ussl_globals,
|
||||
};
|
||||
|
||||
@@ -73,11 +73,11 @@ STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_new_int(tm.tm_year),
|
||||
mp_obj_new_int(tm.tm_mon),
|
||||
mp_obj_new_int(tm.tm_mday),
|
||||
mp_obj_new_int(tm.tm_wday),
|
||||
mp_obj_new_int(tm.tm_hour),
|
||||
mp_obj_new_int(tm.tm_min),
|
||||
mp_obj_new_int(tm.tm_sec),
|
||||
mp_obj_new_int(mseconds)
|
||||
mp_obj_new_int(tm.tm_wday),
|
||||
mp_obj_new_int(tm.tm_yday)
|
||||
};
|
||||
return mp_obj_new_tuple(8, tuple);
|
||||
} else {
|
||||
@@ -105,7 +105,6 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
|
||||
/// which expresses a time as per localtime. It returns an integer which is
|
||||
/// the number of seconds since Jan 1, 2000.
|
||||
STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
|
||||
|
||||
mp_uint_t len;
|
||||
mp_obj_t *elem;
|
||||
|
||||
@@ -119,15 +118,17 @@ STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
|
||||
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
|
||||
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
|
||||
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
|
||||
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
|
||||
|
||||
|
||||
/// \function sleep(milliseconds)
|
||||
/// Sleep for the given number of milliseconds.
|
||||
STATIC mp_obj_t time_sleep(mp_obj_t milliseconds_o) {
|
||||
HAL_Delay(mp_obj_get_int(milliseconds_o));
|
||||
/// \function sleep(seconds)
|
||||
/// Sleep for the given number of seconds.
|
||||
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
|
||||
int32_t sleep_s = mp_obj_get_int(seconds_o);
|
||||
if (sleep_s > 0) {
|
||||
HAL_Delay(sleep_s * 1000);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
|
||||
@@ -135,12 +136,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
|
||||
/// \function time()
|
||||
/// Returns the number of seconds, as an integer, since 1/1/2000.
|
||||
STATIC mp_obj_t time_time(void) {
|
||||
uint32_t seconds;
|
||||
uint16_t mseconds;
|
||||
|
||||
// get the seconds and the milliseconds from the RTC
|
||||
MAP_PRCMRTCGet(&seconds, &mseconds);
|
||||
return mp_obj_new_int(seconds);
|
||||
return mp_obj_new_int(pybrtc_get_seconds());
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -43,7 +43,7 @@ typedef enum {
|
||||
MODWLAN_ERROR_INVALID_PARAMS = -1,
|
||||
MODWLAN_ERROR_TIMEOUT = -2,
|
||||
MODWLAN_ERROR_UNKNOWN = -3,
|
||||
}modwlan_Status_t;
|
||||
} modwlan_Status_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PUBLIC DATA
|
||||
@@ -54,13 +54,30 @@ extern _SlLockObj_t wlan_LockObj;
|
||||
DECLARE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
extern void wlan_pre_init (void);
|
||||
extern modwlan_Status_t wlan_sl_enable (SlWlanMode_t mode, const char *ssid, uint8_t ssid_len, uint8_t sec,
|
||||
const char *key, uint8_t key_len, uint8_t channel);
|
||||
extern void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t sec,
|
||||
const char *key, uint8_t key_len, uint8_t channel, bool append_mac);
|
||||
extern void wlan_first_start (void);
|
||||
extern void wlan_update(void);
|
||||
extern void wlan_stop (uint32_t timeout);
|
||||
extern void wlan_start (void);
|
||||
extern void wlan_get_mac (uint8_t *macAddress);
|
||||
extern void wlan_get_ip (uint32_t *ip);
|
||||
extern bool wlan_is_connected (void);
|
||||
extern void wlan_set_current_time (uint32_t seconds_since_2000);
|
||||
|
||||
extern int wlan_gethostbyname(const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family);
|
||||
extern int wlan_socket_socket(mod_network_socket_obj_t *s, int *_errno);
|
||||
extern void wlan_socket_close(mod_network_socket_obj_t *s);
|
||||
extern int wlan_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno);
|
||||
extern int wlan_socket_listen(mod_network_socket_obj_t *s, mp_int_t backlog, int *_errno);
|
||||
extern int wlan_socket_accept(mod_network_socket_obj_t *s, mod_network_socket_obj_t *s2, byte *ip, mp_uint_t *port, int *_errno);
|
||||
extern int wlan_socket_connect(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno);
|
||||
extern int wlan_socket_send(mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno);
|
||||
extern int wlan_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno);
|
||||
extern int wlan_socket_sendto( mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno);
|
||||
extern int wlan_socket_recvfrom(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno);
|
||||
extern int wlan_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
|
||||
extern int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int *_errno);
|
||||
extern int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno);
|
||||
|
||||
#endif /* MODWLAN_H_ */
|
||||
|
||||
@@ -57,10 +57,8 @@
|
||||
///
|
||||
/// Usage:
|
||||
///
|
||||
/// adc = pyb.ADC(channel) # create an adc object on the given channel (0 to 3)
|
||||
/// this automatically configures the pin associated to
|
||||
/// that analog channel.
|
||||
/// adc.read() # read channel value
|
||||
/// adc = pyb.ADC('GP5') # create an adc object on the given pin (GP2, GP3, GP4 o GP5)
|
||||
/// adc.read() # read channel value
|
||||
///
|
||||
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
|
||||
|
||||
@@ -75,14 +73,23 @@
|
||||
******************************************************************************/
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
pin_obj_t *pin;
|
||||
byte channel;
|
||||
byte num;
|
||||
byte id;
|
||||
} pyb_adc_obj_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS] = { {.pin = &pin_GP2, .channel = ADC_CH_0, .id = 1}, {.pin = &pin_GP3, .channel = ADC_CH_1, .id = 2},
|
||||
{.pin = &pin_GP4, .channel = ADC_CH_2, .id = 2}, {.pin = &pin_GP5, .channel = ADC_CH_3, .id = 4} };
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
STATIC void pybadc_init (pyb_adc_obj_t *self) {
|
||||
// configure the pin in analog mode
|
||||
pin_config (self->pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PYBPIN_ANALOG_TYPE, PIN_STRENGTH_2MA);
|
||||
// enable the ADC channel
|
||||
MAP_ADCChannelEnable(ADC_BASE, self->channel);
|
||||
// enable and configure the timer
|
||||
@@ -92,68 +99,34 @@ STATIC void pybadc_init (pyb_adc_obj_t *self) {
|
||||
MAP_ADCEnable(ADC_BASE);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
|
||||
|
||||
/******************************************************************************/
|
||||
/* Micro Python bindings : adc object */
|
||||
|
||||
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pyb_adc_obj_t *self = self_in;
|
||||
mp_printf(print, "<ADC, channel=%u>", self->num);
|
||||
mp_printf(print, "<ADC1 channel=%u on %q>", self->id, self->pin->name);
|
||||
}
|
||||
|
||||
/// \classmethod \constructor(channel)
|
||||
/// Create an ADC object associated with the given channel.
|
||||
/// \classmethod \constructor(pin)
|
||||
/// Create an ADC object associated with the given pin.
|
||||
/// This allows you to then read analog values on that pin.
|
||||
STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
// check number of arguments
|
||||
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
||||
|
||||
// the first argument is the channel number
|
||||
uint num = mp_obj_get_int(args[0]);
|
||||
const pin_obj_t *pin;
|
||||
uint channel;
|
||||
switch (num) {
|
||||
case 0:
|
||||
channel = ADC_CH_0;
|
||||
pin = &pin_GPIO2;
|
||||
break;
|
||||
case 1:
|
||||
channel = ADC_CH_1;
|
||||
pin = &pin_GPIO3;
|
||||
break;
|
||||
case 2:
|
||||
channel = ADC_CH_2;
|
||||
pin = &pin_GPIO4;
|
||||
break;
|
||||
case 3:
|
||||
channel = ADC_CH_3;
|
||||
pin = &pin_GPIO5;
|
||||
break;
|
||||
default:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
break;
|
||||
// the argument passed is the pin
|
||||
const pin_obj_t *pin = (pin_obj_t *)pin_find(args[0]);
|
||||
for (int32_t idx = 0; idx < PYB_ADC_NUM_CHANNELS; idx++) {
|
||||
if (pin == pyb_adc_obj[idx].pin) {
|
||||
pyb_adc_obj_t *self = &pyb_adc_obj[idx];
|
||||
self->base.type = &pyb_adc_type;
|
||||
pybadc_init (self);
|
||||
// register it with the sleep module
|
||||
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybadc_init);
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
// disable the callback before re-configuring
|
||||
pyb_adc_obj_t *self = &pyb_adc_obj[channel];
|
||||
self->base.type = &pyb_adc_type;
|
||||
self->channel = channel;
|
||||
self->num = num;
|
||||
|
||||
// configure the pin in analog mode
|
||||
pin_config ((pin_obj_t *)pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PYBPIN_ANALOG_TYPE, PIN_STRENGTH_2MA);
|
||||
|
||||
// initialize it
|
||||
pybadc_init (self);
|
||||
|
||||
// register it with the sleep module
|
||||
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybadc_init);
|
||||
|
||||
return self;
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
/// \method read()
|
||||
@@ -172,19 +145,19 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
|
||||
|
||||
/// \method enable()
|
||||
/// \method init()
|
||||
/// Enable the adc channel
|
||||
STATIC mp_obj_t adc_enable(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t adc_init(mp_obj_t self_in) {
|
||||
pyb_adc_obj_t *self = self_in;
|
||||
|
||||
pybadc_init(self);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_init_obj, adc_init);
|
||||
|
||||
/// \method disable()
|
||||
/// \method deinit()
|
||||
/// Disable the adc channel
|
||||
STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t adc_deinit(mp_obj_t self_in) {
|
||||
pyb_adc_obj_t *self = self_in;
|
||||
|
||||
MAP_ADCChannelDisable(ADC_BASE, self->channel);
|
||||
@@ -192,12 +165,12 @@ STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
|
||||
pybsleep_remove ((const mp_obj_t)self);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_disable_obj, adc_disable);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_deinit_obj, adc_deinit);
|
||||
|
||||
STATIC const mp_map_elem_t adc_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&adc_enable_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&adc_disable_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&adc_init_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&adc_deinit_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
|
||||
|
||||
@@ -56,10 +56,10 @@
|
||||
///
|
||||
/// from pyb import I2C
|
||||
///
|
||||
/// i2c = I2C() # create
|
||||
/// i2c = I2C(50000) # create and init with a 50KHz baudrate
|
||||
/// i2c.init(100000) # init with a 100KHz baudrate
|
||||
/// i2c.deinit() # turn off the peripheral
|
||||
/// i2c = I2C(1) # create
|
||||
/// i2c = I2C(1, I2C.MASTER, baudrate=50000) # create and init with a 50KHz baudrate
|
||||
/// i2c.init(I2C.MASTER, baudrate=100000) # init with a 100KHz baudrate
|
||||
/// i2c.deinit() # turn off the peripheral
|
||||
///
|
||||
/// Printing the i2c object gives you information about its configuration.
|
||||
///
|
||||
@@ -76,7 +76,6 @@
|
||||
///
|
||||
/// A master must specify the recipient's address:
|
||||
///
|
||||
/// i2c.init(100000)
|
||||
/// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
|
||||
/// i2c.send(b'456', addr=0x42) # keyword for address
|
||||
///
|
||||
@@ -98,6 +97,8 @@ typedef struct _pyb_i2c_obj_t {
|
||||
/******************************************************************************
|
||||
DEFINE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define PYBI2C_MASTER (0)
|
||||
|
||||
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
|
||||
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
|
||||
|
||||
@@ -251,16 +252,40 @@ STATIC bool pyb_i2c_scan_device(byte devAddr) {
|
||||
/******************************************************************************/
|
||||
/* Micro Python bindings */
|
||||
/******************************************************************************/
|
||||
|
||||
/// \method init(100000)
|
||||
///
|
||||
/// Initialise the I2C bus as a master with the given baudrate.
|
||||
///
|
||||
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
|
||||
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pyb_i2c_obj_t *self = self_in;
|
||||
if (self->baudrate > 0) {
|
||||
mp_printf(print, "<I2C1, I2C.MASTER, baudrate=%u>)", self->baudrate);
|
||||
}
|
||||
else {
|
||||
mp_print_str(print, "<I2C1>");
|
||||
}
|
||||
}
|
||||
|
||||
/// \method init(mode, *, baudrate=100000)
|
||||
///
|
||||
/// Initialise the I2C bus with the given parameters:
|
||||
///
|
||||
/// - `mode` must be either `I2C.MASTER` or `I2C.SLAVE`
|
||||
/// - `baudrate` is the SCL clock rate (only sensible for a master)
|
||||
STATIC const mp_arg_t pyb_i2c_init_args[] = {
|
||||
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, },
|
||||
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
|
||||
};
|
||||
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
|
||||
|
||||
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
// parse args
|
||||
mp_arg_val_t vals[PYB_I2C_INIT_NUM_ARGS];
|
||||
mp_arg_parse_all(n_args, args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, vals);
|
||||
|
||||
// verify that mode is master
|
||||
if (vals[0].u_int != PYBI2C_MASTER) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
// make sure the baudrate is between the valid range
|
||||
self->baudrate = MIN(MAX(mp_obj_get_int(baudrate), PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
|
||||
self->baudrate = MIN(MAX(vals[1].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
|
||||
|
||||
// init the I2C bus
|
||||
i2c_init(self);
|
||||
@@ -273,7 +298,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
|
||||
|
||||
/// \classmethod \constructor(bus, ...)
|
||||
///
|
||||
/// Construct an I2C object on the given bus. `bus` can only be 0.
|
||||
/// Construct an I2C object on the given bus. `bus` can only be 1.
|
||||
/// With no additional parameters, the I2C object is created but not
|
||||
/// initialised (it has the settings from the last initialisation of
|
||||
/// the bus, if any). If extra arguments are given, the bus is initialised.
|
||||
@@ -286,28 +311,20 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
|
||||
pyb_i2c_obj_t *self = &pyb_i2c_obj;
|
||||
self->base.type = &pyb_i2c_type;
|
||||
|
||||
if (n_args > 0) {
|
||||
if (n_args > 1 || n_kw > 0) {
|
||||
// start the peripheral
|
||||
pyb_i2c_init_helper(self, *args);
|
||||
mp_map_t kw_args;
|
||||
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
||||
pyb_i2c_init_helper(self, n_args - 1, args + 1, &kw_args);
|
||||
}
|
||||
|
||||
return (mp_obj_t)self;
|
||||
}
|
||||
|
||||
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pyb_i2c_obj_t *self = self_in;
|
||||
if (self->baudrate > 0) {
|
||||
mp_printf(print, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
|
||||
}
|
||||
else {
|
||||
mp_print_str(print, "<I2C0>");
|
||||
}
|
||||
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, kw_args);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t pyb_i2c_init(mp_obj_t self_in, mp_obj_t baudrate) {
|
||||
return pyb_i2c_init_helper(self_in, baudrate);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_init_obj, pyb_i2c_init);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
|
||||
|
||||
/// \method deinit()
|
||||
/// Turn off the I2C bus.
|
||||
@@ -361,8 +378,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
|
||||
/// - `addr` is the address to send to (only required in master mode)
|
||||
/// Return value: `None`.
|
||||
STATIC const mp_arg_t pyb_i2c_send_args[] = {
|
||||
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
||||
};
|
||||
#define PYB_I2C_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_send_args)
|
||||
|
||||
@@ -396,8 +414,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send);
|
||||
/// Return value: if `recv` is an integer then a new buffer of the bytes received,
|
||||
/// otherwise the same buffer that was passed in to `recv`.
|
||||
STATIC const mp_arg_t pyb_i2c_recv_args[] = {
|
||||
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
||||
};
|
||||
#define PYB_I2C_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_recv_args)
|
||||
|
||||
@@ -437,9 +456,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
|
||||
/// Returns the read data.
|
||||
/// This is only valid in master mode.
|
||||
STATIC const mp_arg_t pyb_i2c_mem_read_args[] = {
|
||||
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
||||
{ MP_QSTR_addr_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
||||
};
|
||||
#define PYB_I2C_MEM_READ_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_mem_read_args)
|
||||
@@ -529,6 +549,10 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_i2c_recv_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_read), (mp_obj_t)&pyb_i2c_mem_read_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
|
||||
|
||||
// class constants
|
||||
/// \constant MASTER - for initialising the bus to master mode
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(PYBI2C_MASTER) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);
|
||||
|
||||
@@ -69,10 +69,10 @@
|
||||
/// Example callback:
|
||||
///
|
||||
/// def pincb(pin):
|
||||
/// print(pin.get_config().name)
|
||||
/// print(pin.name())
|
||||
///
|
||||
/// extint = pyb.Pin('GPIO10', 0, pyb.Pin.INT_RISING, pyb.GPIO.STD_PD, pyb.S2MA)
|
||||
/// extint.callback (intmode=pyb.Pin.INT_RISING, handler=pincb)
|
||||
/// extint.callback (mode=pyb.Pin.INT_RISING, handler=pincb)
|
||||
/// # the callback can be triggered manually
|
||||
/// extint.callback()()
|
||||
/// # to disable the callback
|
||||
@@ -92,15 +92,17 @@
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
STATIC void pin_obj_configure (const pin_obj_t *self);
|
||||
STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *wake_pin, uint *idx);
|
||||
STATIC void pin_extint_enable (mp_obj_t self_in);
|
||||
STATIC void pin_extint_disable (mp_obj_t self_in);
|
||||
STATIC void pin_verify_af (uint af);
|
||||
STATIC void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority);
|
||||
STATIC void GPIOA0IntHandler (void);
|
||||
STATIC void GPIOA1IntHandler (void);
|
||||
STATIC void GPIOA2IntHandler (void);
|
||||
STATIC void GPIOA3IntHandler (void);
|
||||
STATIC void EXTI_Handler(uint port);
|
||||
STATIC void pin_obj_configure (const pin_obj_t *self);
|
||||
STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *wake_pin, uint *idx);
|
||||
STATIC void pin_extint_enable (mp_obj_t self_in);
|
||||
STATIC void pin_extint_disable (mp_obj_t self_in);
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE CONSTANTS
|
||||
@@ -133,11 +135,11 @@ STATIC pybpin_wake_pin_t pybpin_wake_pin[PYBPIN_NUM_WAKE_PINS] =
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
void pin_init0(void) {
|
||||
// assign GPIO10 and GPIO11 to the GPIO peripheral (the default is I2C), so that the I2C bus can
|
||||
// assign GP10 and GP11 to the GPIO peripheral (the default is I2C), so that the I2C bus can
|
||||
// be assigned safely to any other pins (as recomended by the SDK release notes). Make them
|
||||
// inputs with pull-downs enabled to ensure they are not floating during LDPS and hibernate.
|
||||
pin_config ((pin_obj_t *)&pin_GPIO10, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD_PD, PIN_STRENGTH_2MA);
|
||||
pin_config ((pin_obj_t *)&pin_GPIO11, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD_PD, PIN_STRENGTH_2MA);
|
||||
pin_config ((pin_obj_t *)&pin_GP10, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD_PD, PIN_STRENGTH_2MA);
|
||||
pin_config ((pin_obj_t *)&pin_GP11, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD_PD, PIN_STRENGTH_2MA);
|
||||
}
|
||||
|
||||
// C API used to convert a user-supplied pin name into an ordinal pin number.
|
||||
@@ -159,12 +161,6 @@ pin_obj_t *pin_find(mp_obj_t user_obj) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
void pin_verify_af (uint af) {
|
||||
if (af > PIN_MODE_15) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
}
|
||||
|
||||
void pin_config (pin_obj_t *self, uint af, uint mode, uint type, uint strength) {
|
||||
// configure the pin in analog mode
|
||||
self->af = af, self->mode = mode, self->type = type, self->strength = strength;
|
||||
@@ -175,37 +171,6 @@ void pin_config (pin_obj_t *self, uint af, uint mode, uint type, uint strength)
|
||||
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pin_obj_configure);
|
||||
}
|
||||
|
||||
void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority) {
|
||||
void *handler;
|
||||
uint32_t intnum;
|
||||
|
||||
// configure the interrupt type
|
||||
MAP_GPIOIntTypeSet(self->port, self->bit, intmode);
|
||||
switch (self->port) {
|
||||
case GPIOA0_BASE:
|
||||
handler = GPIOA0IntHandler;
|
||||
intnum = INT_GPIOA0;
|
||||
break;
|
||||
case GPIOA1_BASE:
|
||||
handler = GPIOA1IntHandler;
|
||||
intnum = INT_GPIOA1;
|
||||
break;
|
||||
case GPIOA2_BASE:
|
||||
handler = GPIOA2IntHandler;
|
||||
intnum = INT_GPIOA2;
|
||||
break;
|
||||
case GPIOA3_BASE:
|
||||
default:
|
||||
handler = GPIOA3IntHandler;
|
||||
intnum = INT_GPIOA3;
|
||||
break;
|
||||
}
|
||||
MAP_GPIOIntRegister(self->port, handler);
|
||||
// set the interrupt to the lowest priority, to make sure that
|
||||
// no other ISRs will be preemted by this one
|
||||
MAP_IntPrioritySet(intnum, priority);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
@@ -245,27 +210,27 @@ STATIC void pin_obj_configure (const pin_obj_t *self) {
|
||||
STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *hib_pin, uint *idx) {
|
||||
// pin_num is actually : (package_pin - 1)
|
||||
switch (self->pin_num) {
|
||||
case 56: // GPIO2
|
||||
case 56: // GP2
|
||||
*hib_pin = PRCM_HIB_GPIO2;
|
||||
*idx = 0;
|
||||
break;
|
||||
case 58: // GPIO4
|
||||
case 58: // GP4
|
||||
*hib_pin = PRCM_HIB_GPIO4;
|
||||
*idx = 1;
|
||||
break;
|
||||
case 3: // GPIO13
|
||||
case 3: // GP13
|
||||
*hib_pin = PRCM_HIB_GPIO13;
|
||||
*idx = 2;
|
||||
break;
|
||||
case 7: // GPIO17
|
||||
case 7: // GP17
|
||||
*hib_pin = PRCM_HIB_GPIO17;
|
||||
*idx = 3;
|
||||
break;
|
||||
case 1: // GPIO11
|
||||
case 1: // GP11
|
||||
*hib_pin = PRCM_HIB_GPIO11;
|
||||
*idx = 4;
|
||||
break;
|
||||
case 16: // GPIO24
|
||||
case 16: // GP24
|
||||
*hib_pin = PRCM_HIB_GPIO24;
|
||||
*idx = 5;
|
||||
break;
|
||||
@@ -326,6 +291,77 @@ STATIC void pin_extint_disable (mp_obj_t self_in) {
|
||||
MAP_GPIOIntDisable(self->port, self->bit);
|
||||
}
|
||||
|
||||
STATIC void pin_verify_af (uint af) {
|
||||
if (af > PIN_MODE_15) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority) {
|
||||
void *handler;
|
||||
uint32_t intnum;
|
||||
|
||||
// configure the interrupt type
|
||||
MAP_GPIOIntTypeSet(self->port, self->bit, intmode);
|
||||
switch (self->port) {
|
||||
case GPIOA0_BASE:
|
||||
handler = GPIOA0IntHandler;
|
||||
intnum = INT_GPIOA0;
|
||||
break;
|
||||
case GPIOA1_BASE:
|
||||
handler = GPIOA1IntHandler;
|
||||
intnum = INT_GPIOA1;
|
||||
break;
|
||||
case GPIOA2_BASE:
|
||||
handler = GPIOA2IntHandler;
|
||||
intnum = INT_GPIOA2;
|
||||
break;
|
||||
case GPIOA3_BASE:
|
||||
default:
|
||||
handler = GPIOA3IntHandler;
|
||||
intnum = INT_GPIOA3;
|
||||
break;
|
||||
}
|
||||
MAP_GPIOIntRegister(self->port, handler);
|
||||
// set the interrupt to the lowest priority, to make sure that
|
||||
// no other ISRs will be preemted by this one
|
||||
MAP_IntPrioritySet(intnum, priority);
|
||||
}
|
||||
|
||||
STATIC void GPIOA0IntHandler (void) {
|
||||
EXTI_Handler(GPIOA0_BASE);
|
||||
}
|
||||
|
||||
STATIC void GPIOA1IntHandler (void) {
|
||||
EXTI_Handler(GPIOA1_BASE);
|
||||
}
|
||||
|
||||
STATIC void GPIOA2IntHandler (void) {
|
||||
EXTI_Handler(GPIOA2_BASE);
|
||||
}
|
||||
|
||||
STATIC void GPIOA3IntHandler (void) {
|
||||
EXTI_Handler(GPIOA3_BASE);
|
||||
}
|
||||
|
||||
// common interrupt handler
|
||||
STATIC void EXTI_Handler(uint port) {
|
||||
uint32_t bits = MAP_GPIOIntStatus(port, true);
|
||||
MAP_GPIOIntClear(port, bits);
|
||||
|
||||
// might be that we have more than one Pin interrupt pending
|
||||
// therefore we must loop through all of the 8 possible bits
|
||||
for (int i = 0; i < 8; i++) {
|
||||
uint32_t bit = (1 << i);
|
||||
if (bit & bits) {
|
||||
pin_obj_t *self = (pin_obj_t *)pin_find_pin_by_port_bit(&pin_cpu_pins_locals_dict, port, bit);
|
||||
mp_obj_t _callback = mpcallback_find(self);
|
||||
mpcallback_handler(_callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings
|
||||
|
||||
@@ -333,7 +369,7 @@ STATIC void pin_extint_disable (mp_obj_t self_in) {
|
||||
/// Initialise the pin:
|
||||
///
|
||||
/// - `af` can be in range 0-15, please check the CC3200 datasheet
|
||||
/// for the details on the AFs availables on each pin (af=0, keeps it as a gpio pin).
|
||||
/// for the details on the AFs availables on each pin (af=0 keeps it as a gpio pin).
|
||||
/// - `mode` can be one of:
|
||||
/// - `Pin.IN` - configure the pin for input;
|
||||
/// - `Pin.OUT` - configure the pin for output;
|
||||
@@ -344,7 +380,6 @@ STATIC void pin_extint_disable (mp_obj_t self_in) {
|
||||
/// - `Pin.OD` - standard without pull up or pull down;
|
||||
/// - `Pin.OD_PU` - open drain with pull-up resistor;
|
||||
/// - `Pin.OD_PD` - open drain with pull-down resistor.
|
||||
/// - `Pin.ANALOG` - configured in analog (adc) mode
|
||||
/// - `strength` can be one of:
|
||||
/// - `Pin.S2MA` - 2ma drive strength;
|
||||
/// - `Pin.S4MA` - 4ma drive strength;
|
||||
@@ -367,32 +402,35 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
|
||||
// get the af
|
||||
uint af = args[0].u_int;
|
||||
if (af < PIN_MODE_0 || af > PIN_MODE_15) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto invalid_args;
|
||||
}
|
||||
// get the io mode
|
||||
uint mode = args[1].u_int;
|
||||
// checking the mode only makes sense if af == GPIO
|
||||
if (af == PIN_MODE_0) {
|
||||
if (mode != GPIO_DIR_MODE_IN && mode != GPIO_DIR_MODE_OUT) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto invalid_args;
|
||||
}
|
||||
}
|
||||
// get the type
|
||||
uint type = args[2].u_int;
|
||||
if (type != PIN_TYPE_STD && type != PIN_TYPE_STD_PU && type != PIN_TYPE_STD_PD &&
|
||||
type != PIN_TYPE_OD && type != PIN_TYPE_OD_PU && type != PIN_TYPE_OD_PD) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto invalid_args;
|
||||
}
|
||||
// get the strenght
|
||||
uint strength = args[3].u_int;
|
||||
if (strength != PIN_STRENGTH_2MA && strength != PIN_STRENGTH_4MA && strength != PIN_STRENGTH_6MA) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto invalid_args;
|
||||
}
|
||||
|
||||
// configure the pin as requested
|
||||
pin_config (self, af, mode, type, strength);
|
||||
|
||||
return mp_const_none;
|
||||
|
||||
invalid_args:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
/// \method print()
|
||||
@@ -523,10 +561,18 @@ STATIC mp_obj_t pin_toggle(mp_obj_t self_in) {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_toggle_obj, pin_toggle);
|
||||
|
||||
/// \method get_config()
|
||||
/// \method name()
|
||||
/// Returns the qstr name of the pin
|
||||
STATIC mp_obj_t pin_name(mp_obj_t self_in) {
|
||||
pin_obj_t *self = self_in;
|
||||
return MP_OBJ_NEW_QSTR(self->name);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_name_obj, pin_name);
|
||||
|
||||
/// \method info()
|
||||
/// Returns a named tupple with the current configuration of the gpio pin
|
||||
STATIC mp_obj_t pin_get_config(mp_obj_t self_in) {
|
||||
STATIC const qstr pin_config_fields[] = {
|
||||
STATIC mp_obj_t pin_info(mp_obj_t self_in) {
|
||||
STATIC const qstr pin_info_fields[] = {
|
||||
MP_QSTR_name, MP_QSTR_af, MP_QSTR_mode,
|
||||
MP_QSTR_type, MP_QSTR_strength
|
||||
};
|
||||
@@ -539,13 +585,13 @@ STATIC mp_obj_t pin_get_config(mp_obj_t self_in) {
|
||||
pin_config[3] = mp_obj_new_int(self->type);
|
||||
pin_config[4] = mp_obj_new_int(self->strength);
|
||||
|
||||
return mp_obj_new_attrtuple(pin_config_fields, 5, pin_config);
|
||||
return mp_obj_new_attrtuple(pin_info_fields, 5, pin_config);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_get_config_obj, pin_get_config);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_info_obj, pin_info);
|
||||
|
||||
/// \method callback(method, intmode, priority, pwrmode)
|
||||
/// \method callback(method, mode, priority, pwrmode)
|
||||
/// Creates a callback object associated to a pin
|
||||
/// min num of arguments is 1 (intmode)
|
||||
/// min num of arguments is 1 (mode)
|
||||
STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
mp_arg_val_t args[mpcallback_INIT_NUM_ARGS];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mpcallback_INIT_NUM_ARGS, mpcallback_init_args, args);
|
||||
@@ -560,7 +606,7 @@ STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
|
||||
uint intmode = args[0].u_int;
|
||||
if (intmode != GPIO_FALLING_EDGE && intmode != GPIO_RISING_EDGE && intmode != GPIO_BOTH_EDGES &&
|
||||
intmode != GPIO_LOW_LEVEL && intmode != GPIO_HIGH_LEVEL) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto invalid_args;
|
||||
}
|
||||
|
||||
uint pwrmode = args[4].u_int;
|
||||
@@ -679,9 +725,13 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pin_low_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pin_high_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&pin_toggle_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_config), (mp_obj_t)&pin_get_config_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)&pin_name_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pin_info_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pin_callback_obj },
|
||||
|
||||
// class attributes
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_cpu), (mp_obj_t)&pin_cpu_pins_obj_type },
|
||||
|
||||
// class constants
|
||||
/// \constant IN - set the pin to input mode
|
||||
/// \constant OUT - set the pin to output mode
|
||||
@@ -712,7 +762,6 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT_RISING_FALLING), MP_OBJ_NEW_SMALL_INT(GPIO_BOTH_EDGES) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT_LOW_LEVEL), MP_OBJ_NEW_SMALL_INT(GPIO_LOW_LEVEL) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT_HIGH_LEVEL), MP_OBJ_NEW_SMALL_INT(GPIO_HIGH_LEVEL) },
|
||||
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_S2MA), MP_OBJ_NEW_SMALL_INT(PIN_STRENGTH_2MA) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_S4MA), MP_OBJ_NEW_SMALL_INT(PIN_STRENGTH_4MA) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_S6MA), MP_OBJ_NEW_SMALL_INT(PIN_STRENGTH_6MA) },
|
||||
@@ -734,29 +783,3 @@ STATIC const mp_cb_methods_t pin_cb_methods = {
|
||||
.disable = pin_extint_disable,
|
||||
};
|
||||
|
||||
STATIC void GPIOA0IntHandler (void) {
|
||||
EXTI_Handler(GPIOA0_BASE);
|
||||
}
|
||||
|
||||
STATIC void GPIOA1IntHandler (void) {
|
||||
EXTI_Handler(GPIOA1_BASE);
|
||||
}
|
||||
|
||||
STATIC void GPIOA2IntHandler (void) {
|
||||
EXTI_Handler(GPIOA2_BASE);
|
||||
}
|
||||
|
||||
STATIC void GPIOA3IntHandler (void) {
|
||||
EXTI_Handler(GPIOA3_BASE);
|
||||
}
|
||||
|
||||
// common interrupt handler
|
||||
STATIC void EXTI_Handler(uint port) {
|
||||
uint32_t bit = MAP_GPIOIntStatus(port, true);
|
||||
MAP_GPIOIntClear(port, bit);
|
||||
|
||||
pin_obj_t *self = (pin_obj_t *)pin_find_pin_by_port_bit(&pin_cpu_pins_locals_dict, port, bit);
|
||||
mp_obj_t _callback = mpcallback_find(self);
|
||||
mpcallback_handler(_callback);
|
||||
}
|
||||
|
||||
|
||||
@@ -67,14 +67,9 @@ extern const mp_obj_type_t pin_cpu_pins_obj_type;
|
||||
extern const mp_obj_dict_t pin_cpu_pins_locals_dict;
|
||||
|
||||
void pin_init0(void);
|
||||
void pin_verify_af (uint af);
|
||||
void pin_config(pin_obj_t *self, uint af, uint mode, uint type, uint strength);
|
||||
void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority);
|
||||
pin_obj_t *pin_find(mp_obj_t user_obj);
|
||||
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name);
|
||||
pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit);
|
||||
uint32_t pin_get_mode(const pin_obj_t *self);
|
||||
uint32_t pin_get_type(const pin_obj_t *self);
|
||||
uint32_t pin_get_strenght(const pin_obj_t *self);
|
||||
|
||||
#endif // PYBPIN_H_
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <std.h>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
@@ -40,6 +40,9 @@
|
||||
#include "pybsleep.h"
|
||||
#include "mpcallback.h"
|
||||
#include "timeutils.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
|
||||
/// \moduleref pyb
|
||||
/// \class RTC - real time clock
|
||||
@@ -71,6 +74,7 @@ typedef struct {
|
||||
******************************************************************************/
|
||||
STATIC pybrtc_data_t pybrtc_data;
|
||||
STATIC const mp_cb_methods_t pybrtc_cb_methods;
|
||||
STATIC const mp_obj_base_t pyb_rtc_obj = {&pyb_rtc_type};
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PUBLIC FUNCTIONS
|
||||
@@ -92,6 +96,26 @@ void pybrtc_init(void) {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t pybrtc_get_seconds (void) {
|
||||
uint32_t seconds;
|
||||
uint16_t mseconds;
|
||||
|
||||
MAP_PRCMRTCGet(&seconds, &mseconds);
|
||||
return seconds;
|
||||
}
|
||||
|
||||
void pyb_rtc_callback_disable (mp_obj_t self_in) {
|
||||
// check the wake from param
|
||||
if (pybrtc_data.prwmode & PYB_PWR_MODE_ACTIVE) {
|
||||
// disable the slow clock interrupt
|
||||
MAP_PRCMIntDisable(PRCM_INT_SLOW_CLK_CTR);
|
||||
}
|
||||
// disable wake from ldps and hibernate
|
||||
pybsleep_configure_timer_wakeup (PYB_PWR_MODE_ACTIVE);
|
||||
// read the interrupt status to clear any pending interrupt
|
||||
(void)MAP_PRCMIntStatus();
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
@@ -108,21 +132,19 @@ STATIC void pyb_rtc_callback_enable (mp_obj_t self_in) {
|
||||
pybsleep_configure_timer_wakeup (pybrtc_data.prwmode);
|
||||
}
|
||||
|
||||
STATIC void pyb_rtc_callback_disable (mp_obj_t self_in) {
|
||||
// check the wake from param
|
||||
if (pybrtc_data.prwmode & PYB_PWR_MODE_ACTIVE) {
|
||||
// disable the slow clock interrupt
|
||||
MAP_PRCMIntDisable(PRCM_INT_SLOW_CLK_CTR);
|
||||
}
|
||||
// disable wake from ldps and hibernate
|
||||
pybsleep_configure_timer_wakeup (PYB_PWR_MODE_ACTIVE);
|
||||
// read the interrupt status to clear any pending interrupt
|
||||
(void)MAP_PRCMIntStatus();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings
|
||||
|
||||
/// \classmethod \constructor()
|
||||
/// Create an RTC object.
|
||||
STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
// check arguments
|
||||
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
||||
|
||||
// return constant object
|
||||
return (mp_obj_t)&pyb_rtc_obj;
|
||||
}
|
||||
|
||||
/// \method datetime([datetimetuple])
|
||||
/// Get or set the date and time of the RTC.
|
||||
///
|
||||
@@ -166,7 +188,7 @@ mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
tm.tm_year = mp_obj_get_int(items[0]);
|
||||
tm.tm_mon = mp_obj_get_int(items[1]);
|
||||
tm.tm_mday = mp_obj_get_int(items[2]);
|
||||
// Skip the weekday
|
||||
// skip the weekday
|
||||
tm.tm_hour = mp_obj_get_int(items[4]);
|
||||
tm.tm_min = mp_obj_get_int(items[5]);
|
||||
tm.tm_sec = mp_obj_get_int(items[6]);
|
||||
@@ -176,6 +198,8 @@ mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
mseconds = RTC_U16MS_CYCLES(mseconds);
|
||||
MAP_PRCMRTCSet(seconds, mseconds);
|
||||
|
||||
// set WLAN time and date, this is needed to verify certificates
|
||||
wlan_set_current_time(seconds);
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
@@ -192,7 +216,7 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
|
||||
// check if any parameters were passed
|
||||
mp_obj_t _callback = mpcallback_find((mp_obj_t)&pyb_rtc_obj);
|
||||
if (kw_args->used > 0 || !_callback) {
|
||||
uint32_t f_mseconds = args[3].u_int;
|
||||
uint32_t f_mseconds = MAX(1, args[3].u_int);
|
||||
uint32_t seconds;
|
||||
uint16_t mseconds;
|
||||
// get the seconds and the milliseconds from the RTC
|
||||
@@ -210,7 +234,7 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
|
||||
// set the match value
|
||||
MAP_PRCMRTCMatchSet(seconds, mseconds);
|
||||
|
||||
// save the match data for later
|
||||
// save the power mode data for later
|
||||
pybrtc_data.prwmode = args[4].u_int;
|
||||
|
||||
// create the callback
|
||||
@@ -235,9 +259,10 @@ STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
|
||||
|
||||
STATIC const mp_obj_type_t pyb_rtc_type = {
|
||||
const mp_obj_type_t pyb_rtc_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_RTC,
|
||||
.make_new = pyb_rtc_make_new,
|
||||
.locals_dict = (mp_obj_t)&pyb_rtc_locals_dict,
|
||||
};
|
||||
|
||||
@@ -246,5 +271,3 @@ STATIC const mp_cb_methods_t pybrtc_cb_methods = {
|
||||
.enable = pyb_rtc_callback_enable,
|
||||
.disable = pyb_rtc_callback_disable,
|
||||
};
|
||||
|
||||
const mp_obj_base_t pyb_rtc_obj = {&pyb_rtc_type};
|
||||
|
||||
@@ -31,8 +31,10 @@
|
||||
#define RTC_U16MS_CYCLES(msec) ((msec * 1024) / 1000)
|
||||
#define RTC_CYCLES_U16MS(cycles) ((cycles * 1000) / 1024)
|
||||
|
||||
extern const mp_obj_base_t pyb_rtc_obj;
|
||||
extern const mp_obj_type_t pyb_rtc_type;
|
||||
|
||||
void pybrtc_init(void);
|
||||
extern void pybrtc_init(void);
|
||||
extern void pyb_rtc_callback_disable (mp_obj_t self_in);
|
||||
extern uint32_t pybrtc_get_seconds (void);
|
||||
|
||||
#endif // PYBRTC_H_
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#include "pybsleep.h"
|
||||
#include "pybpin.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "osi.h"
|
||||
#include "debug.h"
|
||||
@@ -52,6 +53,8 @@
|
||||
#include "mpcallback.h"
|
||||
#include "mperror.h"
|
||||
#include "sleeprestore.h"
|
||||
#include "serverstask.h"
|
||||
#include "antenna.h"
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE CONSTANTS
|
||||
@@ -123,7 +126,7 @@ STATIC nvic_reg_store_t *nvic_reg_store;
|
||||
STATIC pybsleep_data_t pybsleep_data = {NULL, NULL, NULL, 0};
|
||||
volatile arm_cm4_core_regs_t vault_arm_registers;
|
||||
STATIC pybsleep_reset_cause_t pybsleep_reset_cause = PYB_SLP_PWRON_RESET;
|
||||
STATIC pybsleep_reset_cause_t pybsleep_wake_reason = PYB_SLP_WAKED_PWRON;
|
||||
STATIC pybsleep_wake_reason_t pybsleep_wake_reason = PYB_SLP_WAKED_PWRON;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
@@ -134,7 +137,7 @@ STATIC NORETURN void pybsleep_suspend_enter (void);
|
||||
void pybsleep_suspend_exit (void);
|
||||
STATIC void pybsleep_obj_wakeup (void);
|
||||
STATIC void PRCMInterruptHandler (void);
|
||||
STATIC void pybsleep_iopark (void);
|
||||
STATIC void pybsleep_iopark (bool hibernate);
|
||||
STATIC bool setup_timer_lpds_wake (void);
|
||||
STATIC bool setup_timer_hibernate_wake (void);
|
||||
|
||||
@@ -175,7 +178,7 @@ void pybsleep_init0 (void) {
|
||||
pybsleep_reset_cause = PYB_SLP_WDT_RESET;
|
||||
break;
|
||||
case PRCM_HIB_EXIT:
|
||||
if (PRCMWasResetBecauseOfWDT()) {
|
||||
if (PRCMGetSpecialBit(PRCM_WDT_RESET_BIT)) {
|
||||
pybsleep_reset_cause = PYB_SLP_WDT_RESET;
|
||||
}
|
||||
else {
|
||||
@@ -337,7 +340,7 @@ STATIC NORETURN void pybsleep_suspend_enter (void) {
|
||||
mperror_heartbeat_switch_off();
|
||||
|
||||
// park the gpio pins
|
||||
pybsleep_iopark();
|
||||
pybsleep_iopark(false);
|
||||
|
||||
// store the cpu registers
|
||||
sleep_store();
|
||||
@@ -395,6 +398,11 @@ void pybsleep_suspend_exit (void) {
|
||||
// ungate the clock to the shared spi bus
|
||||
MAP_PRCMPeripheralClkEnable(PRCM_SSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
|
||||
#if MICROPY_HW_ANTENNA_DIVERSITY
|
||||
// re-configure the antenna selection pins
|
||||
antenna_init0();
|
||||
#endif
|
||||
|
||||
// reinitialize simplelink's interface
|
||||
sl_IfOpen (NULL, 0);
|
||||
|
||||
@@ -429,7 +437,7 @@ STATIC void PRCMInterruptHandler (void) {
|
||||
pybsleep_wake_reason = PYB_SLP_WAKED_BY_GPIO;
|
||||
break;
|
||||
case PRCM_LPDS_TIMER:
|
||||
// disable the timer as a wake-up source
|
||||
// disable the timer as wake-up source
|
||||
pybsleep_data.timer_wake_pwrmode &= ~PYB_PWR_MODE_LPDS;
|
||||
MAP_PRCMLPDSWakeupSourceDisable(PRCM_LPDS_TIMER);
|
||||
mpcallback_handler(pybsleep_data.timer_lpds_wake_cb);
|
||||
@@ -448,31 +456,29 @@ STATIC void pybsleep_obj_wakeup (void) {
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void pybsleep_iopark (void) {
|
||||
STATIC void pybsleep_iopark (bool hibernate) {
|
||||
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)&pin_cpu_pins_locals_dict);
|
||||
for (uint i = 0; i < named_map->used; i++) {
|
||||
pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value;
|
||||
// skip the sflash pins since these are shared with the network processor
|
||||
switch (pin->pin_num) {
|
||||
case PIN_11:
|
||||
case PIN_12:
|
||||
case PIN_13:
|
||||
case PIN_14:
|
||||
#ifdef DEBUG
|
||||
// also skip the JTAG pins
|
||||
// skip the JTAG pins
|
||||
case PIN_16:
|
||||
case PIN_17:
|
||||
case PIN_19:
|
||||
case PIN_20:
|
||||
#endif
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
// enable a weak pull-down if the pin is unused
|
||||
if (!pin->isused) {
|
||||
MAP_PinConfigSet(pin->pin_num, pin->strength, PIN_TYPE_STD_PD);
|
||||
}
|
||||
// make it an input
|
||||
MAP_PinDirModeSet(pin->pin_num, PIN_DIR_MODE_IN);
|
||||
if (hibernate) {
|
||||
// make it an input
|
||||
MAP_PinDirModeSet(pin->pin_num, PIN_DIR_MODE_IN);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -487,9 +493,25 @@ STATIC void pybsleep_iopark (void) {
|
||||
HWREG(0x4402E0F4) &= ~(0x3 << 8);
|
||||
HWREG(0x4402E0F4) |= (0x1 << 8);
|
||||
|
||||
// park the antenna selection pins
|
||||
HWREG(0x4402E108) = 0x00000E61;
|
||||
HWREG(0x4402E10C) = 0x00000E61;
|
||||
// if the board has antenna diversity, only park the antenna
|
||||
// selection pins when going into hibernation
|
||||
#if MICROPY_HW_ANTENNA_DIVERSITY
|
||||
if (hibernate) {
|
||||
#endif
|
||||
// park the antenna selection pins
|
||||
// (tri-stated with pull down enabled)
|
||||
HWREG(0x4402E108) = 0x00000E61;
|
||||
HWREG(0x4402E10C) = 0x00000E61;
|
||||
#if MICROPY_HW_ANTENNA_DIVERSITY
|
||||
} else {
|
||||
// park the antenna selection pins
|
||||
// (tri-stated without changing the pull up/down resistors)
|
||||
HWREG(0x4402E108) &= ~0x000000FF;
|
||||
HWREG(0x4402E108) |= 0x00000C61;
|
||||
HWREG(0x4402E10C) &= ~0x000000FF;
|
||||
HWREG(0x4402E10C) |= 0x00000C61;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
STATIC bool setup_timer_lpds_wake (void) {
|
||||
@@ -593,6 +615,7 @@ STATIC mp_obj_t pyb_sleep_suspend (mp_obj_t self_in) {
|
||||
// do we need network wake-up?
|
||||
if (pybsleep_data.wlan_lpds_wake_cb) {
|
||||
MAP_PRCMLPDSWakeupSourceEnable (PRCM_LPDS_HOST_IRQ);
|
||||
server_sleep_sockets();
|
||||
}
|
||||
else {
|
||||
MAP_PRCMLPDSWakeupSourceDisable (PRCM_LPDS_HOST_IRQ);
|
||||
@@ -629,7 +652,7 @@ STATIC mp_obj_t pyb_sleep_hibernate (mp_obj_t self_in) {
|
||||
wlan_stop(SL_STOP_TIMEOUT);
|
||||
pybsleep_flash_powerdown();
|
||||
// must be done just before entering hibernate mode
|
||||
pybsleep_iopark();
|
||||
pybsleep_iopark(true);
|
||||
MAP_PRCMHibernateEnter();
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
@@ -54,9 +54,9 @@
|
||||
/// parameters to init the SPI bus:
|
||||
///
|
||||
/// from pyb import SPI
|
||||
/// spi = SPI(2000000, bits=8, submode=0, cs=SPI.ACTIVE_LOW)
|
||||
/// spi = SPI(1, SPI.MASTER, baudrate=2000000, bits=8, polarity=0, phase=0, nss=SPI.ACTIVE_LOW)
|
||||
///
|
||||
/// Only required parameter is the baudrate, in Hz. Submode may be 0-3.
|
||||
/// Only required parameter is the baudrate, in Hz. polarity and phase may be 0 or 1.
|
||||
/// Bit accepts 8, 16, 32. Chip select values are ACTIVE_LOW and ACTIVE_HIGH
|
||||
///
|
||||
/// Additional method for SPI:
|
||||
@@ -77,6 +77,8 @@ typedef struct _pyb_spi_obj_t {
|
||||
vstr_t rx_vstr;
|
||||
uint tx_index;
|
||||
uint rx_index;
|
||||
byte polarity;
|
||||
byte phase;
|
||||
byte submode;
|
||||
byte wlen;
|
||||
} pyb_spi_obj_t;
|
||||
@@ -85,7 +87,6 @@ typedef struct _pyb_spi_obj_t {
|
||||
DEFINE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define PYBSPI_DEF_BAUDRATE 1000000 // 1MHz
|
||||
#define PYBSPI_CS_NONE 0xFF // spi cs is controlled by the user
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
@@ -167,27 +168,31 @@ STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
|
||||
pyb_spi_obj_t *self = self_in;
|
||||
|
||||
if (self->baudrate > 0) {
|
||||
mp_printf(print, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>",
|
||||
self->baudrate, self->config, self->submode, (self->wlen * 8));
|
||||
}
|
||||
else {
|
||||
mp_print_str(print, "<SPI0>");
|
||||
mp_printf(print, "<SPI1, SPI.MASTER, baudrate=%u, bits=%u, polarity=%u, phase=%u, nss=%q>",
|
||||
self->baudrate, (self->wlen * 8), self->polarity, self->phase,
|
||||
(self->config & SPI_CS_ACTIVELOW) ? MP_QSTR_ACTIVE_LOW : MP_QSTR_ACTIVE_HIGH);
|
||||
} else {
|
||||
mp_print_str(print, "<SPI1>");
|
||||
}
|
||||
}
|
||||
|
||||
/// \method init(2000000, *, bits=8, submode=0, cs=SPI.ACTIVELOW)
|
||||
/// \method init(mode, *, baudrate=1000000, bits=8, polarity=0, phase=0, nss=SPI.ACTIVE_LOW)
|
||||
///
|
||||
/// Initialise the SPI bus with the given parameters:
|
||||
///
|
||||
/// - `mode` must be MASTER.
|
||||
/// - `baudrate` is the SCK clock rate.
|
||||
/// - `bits` is the transfer width size (8, 16, 32).
|
||||
/// - `submode` is the spi mode (0, 1, 2, 3).
|
||||
/// - `cs` can be ACTIVELOW, ACTIVEHIGH, or NONE
|
||||
/// - `polarity` (0, 1).
|
||||
/// - `phase` (0, 1).
|
||||
/// - `nss` can be ACTIVE_LOW or ACTIVE_HIGH.
|
||||
static const mp_arg_t pybspi_init_args[] = {
|
||||
{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, },
|
||||
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, },
|
||||
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBSPI_DEF_BAUDRATE} },
|
||||
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
||||
{ MP_QSTR_submode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SPI_CS_ACTIVELOW} },
|
||||
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_nss, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SPI_CS_ACTIVELOW} },
|
||||
};
|
||||
|
||||
STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
@@ -195,13 +200,13 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(pybspi_init_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pybspi_init_args), pybspi_init_args, args);
|
||||
|
||||
uint submode = args[2].u_int;
|
||||
uint cs = args[3].u_int;
|
||||
uint bits;
|
||||
// verify that mode is master
|
||||
if (args[0].u_int != SPI_MODE_MASTER) {
|
||||
goto invalid_args;
|
||||
}
|
||||
|
||||
// save the word length for later use
|
||||
self->wlen = args[1].u_int / 8;
|
||||
switch (args[1].u_int) {
|
||||
uint bits;
|
||||
switch (args[2].u_int) {
|
||||
case 8:
|
||||
bits = SPI_WL_8;
|
||||
break;
|
||||
@@ -212,22 +217,28 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
|
||||
bits = SPI_WL_32;
|
||||
break;
|
||||
default:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto invalid_args;
|
||||
break;
|
||||
}
|
||||
|
||||
if (submode < SPI_SUB_MODE_0 || submode > SPI_SUB_MODE_3) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
uint polarity = args[3].u_int;
|
||||
uint phase = args[4].u_int;
|
||||
if (polarity > 1 || phase > 1) {
|
||||
goto invalid_args;
|
||||
}
|
||||
|
||||
if (cs != SPI_CS_ACTIVELOW && cs != SPI_CS_ACTIVEHIGH) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
uint nss = args[5].u_int;
|
||||
if (nss != SPI_CS_ACTIVELOW && nss != SPI_CS_ACTIVEHIGH) {
|
||||
goto invalid_args;
|
||||
}
|
||||
|
||||
// build the configuration
|
||||
self->baudrate = args[0].u_int;
|
||||
self->config = bits | cs | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF;
|
||||
self->submode = submode;
|
||||
self->baudrate = args[1].u_int;
|
||||
self->wlen = args[2].u_int >> 3;
|
||||
self->config = bits | nss | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF;
|
||||
self->polarity = polarity;
|
||||
self->phase = phase;
|
||||
self->submode = (polarity << 1) | phase;
|
||||
|
||||
// init the bus
|
||||
pybspi_init((const pyb_spi_obj_t *)self);
|
||||
@@ -236,16 +247,18 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
|
||||
pybsleep_add((const mp_obj_t)self, (WakeUpCB_t)pybspi_init);
|
||||
|
||||
return mp_const_none;
|
||||
|
||||
invalid_args:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
/// \classmethod \constructor(bus, ...)
|
||||
///
|
||||
/// Construct an SPI object with the given baudrate.
|
||||
/// With no parameters, the SPI object is created but not
|
||||
/// Construct an SPI object with the given baudrate. Bus can only be 1.
|
||||
/// With no extra parameters, the SPI object is created but not
|
||||
/// initialised (it has the settings from the last initialisation of
|
||||
/// the bus, if any). If extra arguments are given, the bus is initialised.
|
||||
/// See `init` for parameters of initialisation.
|
||||
///
|
||||
STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
// check arguments
|
||||
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
||||
@@ -253,11 +266,11 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
|
||||
pyb_spi_obj_t *self = &pyb_spi_obj;
|
||||
self->base.type = &pyb_spi_type;
|
||||
|
||||
if (n_args > 0 || n_kw > 0) {
|
||||
if (n_args > 1 || n_kw > 0) {
|
||||
// start the peripheral
|
||||
mp_map_t kw_args;
|
||||
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
||||
pyb_spi_init_helper(self, n_args, args, &kw_args);
|
||||
pyb_spi_init_helper(self, n_args - 1, args + 1, &kw_args);
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -282,39 +295,59 @@ STATIC mp_obj_t pyb_spi_deinit(mp_obj_t self_in) {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_spi_deinit_obj, pyb_spi_deinit);
|
||||
|
||||
/// \method send(send)
|
||||
/// \method send(send, *, timeout=5000)
|
||||
/// Send data on the bus:
|
||||
///
|
||||
/// - `send` is the data to send (a byte to send, or a buffer object).
|
||||
/// - `timeout` is the timeout in milliseconds to wait for the send.
|
||||
///
|
||||
STATIC mp_obj_t pyb_spi_send (mp_obj_t self_in, mp_obj_t send_o) {
|
||||
pyb_spi_obj_t *self = self_in;
|
||||
STATIC mp_obj_t pyb_spi_send (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
||||
};
|
||||
|
||||
// parse args
|
||||
pyb_spi_obj_t *self = pos_args[0];
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// get the buffer to send from
|
||||
mp_buffer_info_t bufinfo;
|
||||
uint8_t data[1];
|
||||
pyb_buf_get_for_send(send_o, &bufinfo, data);
|
||||
pyb_buf_get_for_send(args[0].u_obj, &bufinfo, data);
|
||||
|
||||
// just send
|
||||
pybspi_transfer(self, (const char *)bufinfo.buf, NULL, bufinfo.len);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_spi_send_obj, pyb_spi_send);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_send_obj, 1, pyb_spi_send);
|
||||
|
||||
/// \method recv(recv)
|
||||
/// \method recv(recv, *, timeout=5000)
|
||||
///
|
||||
/// Receive data on the bus:
|
||||
///
|
||||
/// - `recv` can be an integer, which is the number of bytes to receive,
|
||||
/// or a mutable buffer, which will be filled with received bytes.
|
||||
/// - `timeout` is the timeout in milliseconds to wait for the receive.
|
||||
///
|
||||
/// Return: if `recv` is an integer then a new buffer of the bytes received,
|
||||
/// otherwise the same buffer that was passed in to `recv`.
|
||||
STATIC mp_obj_t pyb_spi_recv(mp_obj_t self_in, mp_obj_t recv_o) {
|
||||
pyb_spi_obj_t *self = self_in;
|
||||
STATIC mp_obj_t pyb_spi_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
||||
};
|
||||
|
||||
// parse args
|
||||
pyb_spi_obj_t *self = pos_args[0];
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// get the buffer to receive into
|
||||
vstr_t vstr;
|
||||
mp_obj_t o_ret = pyb_buf_get_for_recv(recv_o, &vstr);
|
||||
mp_obj_t o_ret = pyb_buf_get_for_recv(args[0].u_obj, &vstr);
|
||||
|
||||
// just receive
|
||||
pybspi_transfer(self, NULL, vstr.buf, vstr.len);
|
||||
@@ -326,9 +359,9 @@ STATIC mp_obj_t pyb_spi_recv(mp_obj_t self_in, mp_obj_t recv_o) {
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_spi_recv_obj, pyb_spi_recv);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_recv_obj, 1, pyb_spi_recv);
|
||||
|
||||
/// \method send_recv(send, recv)
|
||||
/// \method send_recv(send, recv=None, *, timeout=5000)
|
||||
///
|
||||
/// Send and receive data on the bus at the same time:
|
||||
///
|
||||
@@ -336,10 +369,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_spi_recv_obj, pyb_spi_recv);
|
||||
/// - `recv` is a mutable buffer which will be filled with received bytes.
|
||||
/// It can be the same as `send`, or omitted. If omitted, a new buffer will
|
||||
/// be created.
|
||||
/// - `timeout` is the timeout in milliseconds to wait for the transaction to complete.
|
||||
///
|
||||
/// Return: the buffer with the received bytes.
|
||||
STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args, const mp_obj_t *args) {
|
||||
pyb_spi_obj_t *self = args[0];
|
||||
STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
||||
{ MP_QSTR_recv, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
||||
};
|
||||
|
||||
// parse args
|
||||
pyb_spi_obj_t *self = pos_args[0];
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// get buffers to send from/receive to
|
||||
mp_buffer_info_t bufinfo_send;
|
||||
@@ -348,35 +391,34 @@ STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args, const mp_obj_t *args) {
|
||||
vstr_t vstr_recv;
|
||||
mp_obj_t o_ret;
|
||||
|
||||
if (args[1] == args[2]) {
|
||||
if (args[0].u_obj == args[1].u_obj) {
|
||||
// same object for sending and receiving, it must be a r/w buffer
|
||||
mp_get_buffer_raise(args[1], &bufinfo_send, MP_BUFFER_RW);
|
||||
mp_get_buffer_raise(args[0].u_obj, &bufinfo_send, MP_BUFFER_RW);
|
||||
bufinfo_recv = bufinfo_send;
|
||||
o_ret = args[1];
|
||||
o_ret = args[0].u_obj;
|
||||
} else {
|
||||
// get the buffer to send from
|
||||
pyb_buf_get_for_send(args[1], &bufinfo_send, data_send);
|
||||
pyb_buf_get_for_send(args[0].u_obj, &bufinfo_send, data_send);
|
||||
|
||||
// get the buffer to receive into
|
||||
if (n_args == 2) {
|
||||
if (args[1].u_obj == mp_const_none) {
|
||||
// only the send was argument given, so create a fresh buffer of the send length
|
||||
vstr_init_len(&vstr_recv, bufinfo_send.len);
|
||||
bufinfo_recv.len = vstr_recv.len;
|
||||
bufinfo_recv.buf = vstr_recv.buf;
|
||||
o_ret = MP_OBJ_NULL;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// recv argument given
|
||||
mp_get_buffer_raise(args[2], &bufinfo_recv, MP_BUFFER_WRITE);
|
||||
mp_get_buffer_raise(args[1].u_obj, &bufinfo_recv, MP_BUFFER_WRITE);
|
||||
if (bufinfo_recv.len != bufinfo_send.len) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
o_ret = args[2];
|
||||
o_ret = args[1].u_obj;
|
||||
}
|
||||
}
|
||||
|
||||
// send and receive
|
||||
pybspi_transfer(self, (const char *)bufinfo_send.buf, vstr_recv.buf, bufinfo_send.len);
|
||||
pybspi_transfer(self, (const char *)bufinfo_send.buf, bufinfo_recv.buf, bufinfo_send.len);
|
||||
|
||||
// return the received data
|
||||
if (o_ret != MP_OBJ_NULL) {
|
||||
@@ -385,7 +427,7 @@ STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args, const mp_obj_t *args) {
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr_recv);
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_spi_send_recv_obj, 2, 3, pyb_spi_send_recv);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_send_recv_obj, 1, pyb_spi_send_recv);
|
||||
|
||||
STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = {
|
||||
// instance methods
|
||||
@@ -396,6 +438,7 @@ STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_send_recv), (mp_obj_t)&pyb_spi_send_recv_obj },
|
||||
|
||||
// class constants
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(SPI_MODE_MASTER) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ACTIVE_LOW), MP_OBJ_NEW_SMALL_INT(SPI_CS_ACTIVELOW) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ACTIVE_HIGH), MP_OBJ_NEW_SMALL_INT(SPI_CS_ACTIVEHIGH) },
|
||||
};
|
||||
|
||||
880
cc3200/mods/pybtimer.c
Normal file
880
cc3200/mods/pybtimer.c
Normal file
@@ -0,0 +1,880 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013, 2014 Damien P. George
|
||||
* Copyright (c) 2015 Daniel Campora
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include MICROPY_HAL_H
|
||||
#include "py/obj.h"
|
||||
#include "py/nlr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/gc.h"
|
||||
#include "inc/hw_types.h"
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "inc/hw_timer.h"
|
||||
#include "rom_map.h"
|
||||
#include "interrupt.h"
|
||||
#include "prcm.h"
|
||||
#include "timer.h"
|
||||
#include "pybtimer.h"
|
||||
#include "pybsleep.h"
|
||||
#include "mpcallback.h"
|
||||
#include "mpexception.h"
|
||||
|
||||
|
||||
/// \moduleref pyb
|
||||
/// \class Timer - generate periodic events, count events, and create PWM signals.
|
||||
///
|
||||
/// Each timer consists of a counter that counts up at a certain rate. The rate
|
||||
/// at which it counts is the peripheral clock frequency (in Hz) divided by the
|
||||
/// timer prescaler. When the counter reaches the timer period it triggers an
|
||||
/// event, and the counter resets back to zero. By using the callback method,
|
||||
/// the timer event can call a Python function.
|
||||
///
|
||||
/// Example usage to toggle an LED at a fixed frequency:
|
||||
///
|
||||
/// tim = pyb.Timer(4) # create a timer object using timer 4
|
||||
/// tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode
|
||||
/// tim_ch = tim.channel(Timer.A, freq=2) # configure channel A at a frequency of 2Hz
|
||||
/// tim_ch.callback(handler=lambda t:led.toggle()) # toggle a LED on every cycle of the timer
|
||||
///
|
||||
/// Further examples:
|
||||
///
|
||||
/// tim1 = pyb.Timer(2, mode=Timer.EVENT_COUNT) # initialize it capture mode
|
||||
/// tim2 = pyb.Timer(1, mode=Timer.PWM) # initialize it in PWM mode
|
||||
/// tim_ch = tim1.channel(Timer.A, freq=1, polarity=Timer.POSITIVE) # start the event counter with a frequency of 1Hz and triggered by positive edges
|
||||
/// tim_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=50) # start the PWM on channel B with a 50% duty cycle
|
||||
/// tim_ch.time() # get the current time in usec (can also be set)
|
||||
/// tim_ch.freq(20) # set the frequency (can also get)
|
||||
/// tim_ch.duty_cycle(30) # set the duty cycle to 30% (can also get)
|
||||
/// tim_ch.duty_cycle(30, Timer.NEGATIVE) # set the duty cycle to 30% and change the polarity to negative
|
||||
/// tim_ch.event_count() # get the number of captured events
|
||||
/// tim_ch.event_time() # get the the time of the last captured event
|
||||
/// tim_ch.period(2000000) # change the period to 2 seconds
|
||||
///
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define PYBTIMER_NUM_TIMERS (4)
|
||||
#define PYBTIMER_POLARITY_POS (0x01)
|
||||
#define PYBTIMER_POLARITY_NEG (0x02)
|
||||
|
||||
#define PYBTIMER_SRC_FREQ_HZ HAL_FCPU_HZ
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE TYPES
|
||||
******************************************************************************/
|
||||
typedef struct _pyb_timer_obj_t {
|
||||
mp_obj_base_t base;
|
||||
uint32_t timer;
|
||||
uint32_t config;
|
||||
uint16_t intflags;
|
||||
uint8_t peripheral;
|
||||
uint8_t id;
|
||||
} pyb_timer_obj_t;
|
||||
|
||||
typedef struct _pyb_timer_channel_obj_t {
|
||||
mp_obj_base_t base;
|
||||
struct _pyb_timer_obj_t *timer;
|
||||
uint32_t frequency;
|
||||
uint32_t period;
|
||||
uint16_t channel;
|
||||
uint8_t polarity;
|
||||
uint8_t duty_cycle;
|
||||
} pyb_timer_channel_obj_t;
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
STATIC const mp_cb_methods_t pyb_timer_channel_cb_methods;
|
||||
STATIC pyb_timer_obj_t pyb_timer_obj[PYBTIMER_NUM_TIMERS] = {{.timer = TIMERA0_BASE, .peripheral = PRCM_TIMERA0},
|
||||
{.timer = TIMERA1_BASE, .peripheral = PRCM_TIMERA1},
|
||||
{.timer = TIMERA2_BASE, .peripheral = PRCM_TIMERA2},
|
||||
{.timer = TIMERA3_BASE, .peripheral = PRCM_TIMERA3}};
|
||||
STATIC const mp_obj_type_t pyb_timer_channel_type;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
STATIC void timer_disable (pyb_timer_obj_t *tim);
|
||||
STATIC void TIMER0AIntHandler(void);
|
||||
STATIC void TIMER0BIntHandler(void);
|
||||
STATIC void TIMER1AIntHandler(void);
|
||||
STATIC void TIMER1BIntHandler(void);
|
||||
STATIC void TIMER2AIntHandler(void);
|
||||
STATIC void TIMER2BIntHandler(void);
|
||||
STATIC void TIMER3AIntHandler(void);
|
||||
STATIC void TIMER3BIntHandler(void);
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
void timer_init0 (void) {
|
||||
mp_obj_list_init(&MP_STATE_PORT(pyb_timer_channel_obj_list), 0);
|
||||
}
|
||||
|
||||
void timer_disable_all (void) {
|
||||
pyb_timer_obj_t timer = {
|
||||
.timer = TIMERA0_BASE,
|
||||
.intflags = TIMER_CAPB_EVENT | TIMER_CAPB_MATCH |
|
||||
TIMER_TIMB_TIMEOUT | TIMER_CAPA_EVENT |
|
||||
TIMER_CAPA_MATCH | TIMER_TIMA_TIMEOUT,
|
||||
.peripheral = PRCM_TIMERA0
|
||||
};
|
||||
|
||||
for (uint32_t i = 0; i < PYBTIMER_NUM_TIMERS; i++) {
|
||||
// in case it's not clocked
|
||||
MAP_PRCMPeripheralClkEnable(timer.peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
timer_disable(&timer);
|
||||
// timer base offset according to hw_memmap.h
|
||||
timer.timer += 0x1000;
|
||||
// peripheral offset according to prcm.h
|
||||
timer.peripheral++;
|
||||
}
|
||||
}
|
||||
|
||||
void pyb_timer_channel_callback_enable (mp_obj_t self_in) {
|
||||
pyb_timer_channel_obj_t *self = self_in;
|
||||
MAP_TimerIntClear(self->timer->timer, self->timer->intflags & self->channel);
|
||||
MAP_TimerIntEnable(self->timer->timer, self->timer->intflags & self->channel);
|
||||
}
|
||||
|
||||
void pyb_timer_channel_callback_disable (mp_obj_t self_in) {
|
||||
pyb_timer_channel_obj_t *self = self_in;
|
||||
MAP_TimerIntDisable(self->timer->timer, self->timer->intflags & self->channel);
|
||||
}
|
||||
|
||||
pyb_timer_channel_obj_t *pyb_timer_channel_find (uint32_t timer, uint16_t channel_n) {
|
||||
for (mp_uint_t i = 0; i < MP_STATE_PORT(pyb_timer_channel_obj_list).len; i++) {
|
||||
pyb_timer_channel_obj_t *ch = ((pyb_timer_channel_obj_t *)(MP_STATE_PORT(pyb_timer_channel_obj_list).items[i]));
|
||||
// any 32-bit timer must be matched by any of its 16-bit versions
|
||||
if (ch->timer->timer == timer && ((ch->channel & TIMER_A) == channel_n || (ch->channel & TIMER_B) == channel_n)) {
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
return MP_OBJ_NULL;
|
||||
}
|
||||
|
||||
void pyb_timer_channel_remove (pyb_timer_channel_obj_t *ch) {
|
||||
pyb_timer_channel_obj_t *channel;
|
||||
if ((channel = pyb_timer_channel_find(ch->timer->timer, ch->channel))) {
|
||||
mp_obj_list_remove(&MP_STATE_PORT(pyb_timer_channel_obj_list), channel);
|
||||
}
|
||||
}
|
||||
|
||||
void pyb_timer_channel_add (pyb_timer_channel_obj_t *ch) {
|
||||
// remove it in case it already exists
|
||||
pyb_timer_channel_remove(ch);
|
||||
mp_obj_list_append(&MP_STATE_PORT(pyb_timer_channel_obj_list), ch);
|
||||
}
|
||||
|
||||
STATIC void timer_disable (pyb_timer_obj_t *tim) {
|
||||
// disable all timers and it's interrupts
|
||||
MAP_TimerDisable(tim->timer, TIMER_A | TIMER_B);
|
||||
MAP_TimerIntDisable(tim->timer, tim->intflags);
|
||||
MAP_TimerIntClear(tim->timer, tim->intflags);
|
||||
MAP_PRCMPeripheralClkDisable(tim->peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
memset(&pyb_timer_obj[tim->id], 0, sizeof(pyb_timer_obj_t));
|
||||
}
|
||||
|
||||
// computes prescaler period and match value so timer triggers at freq-Hz
|
||||
STATIC uint32_t compute_prescaler_period_and_match_value(pyb_timer_channel_obj_t *ch, uint32_t *period_out, uint32_t *match_out) {
|
||||
uint32_t maxcount = (ch->channel == (TIMER_A | TIMER_B)) ? 0xFFFFFFFF : 0xFFFF;
|
||||
uint32_t prescaler;
|
||||
uint32_t period_c = (ch->frequency > 0) ? PYBTIMER_SRC_FREQ_HZ / ch->frequency : ((PYBTIMER_SRC_FREQ_HZ / 1000000) * ch->period);
|
||||
|
||||
period_c = MAX(1, period_c) - 1;
|
||||
if (period_c == 0) {
|
||||
goto error;
|
||||
}
|
||||
prescaler = period_c >> 16;
|
||||
*period_out = period_c;
|
||||
if (prescaler > 0xFF && maxcount == 0xFFFF) {
|
||||
goto error;
|
||||
}
|
||||
// check limit values for the duty cycle
|
||||
if (ch->duty_cycle == 0) {
|
||||
*match_out = period_c - 1;
|
||||
}
|
||||
else {
|
||||
*match_out = period_c - ((period_c * ch->duty_cycle) / 100);
|
||||
}
|
||||
if ((ch->timer->config & 0x0F) == TIMER_CFG_A_PWM && (*match_out > 0xFFFF)) {
|
||||
goto error;
|
||||
}
|
||||
return prescaler;
|
||||
|
||||
error:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
STATIC void timer_init (pyb_timer_obj_t *tim) {
|
||||
MAP_PRCMPeripheralClkEnable(tim->peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
MAP_PRCMPeripheralReset(tim->peripheral);
|
||||
MAP_TimerConfigure(tim->timer, tim->config);
|
||||
}
|
||||
|
||||
STATIC void timer_channel_init (pyb_timer_channel_obj_t *ch) {
|
||||
// calculate the period, the prescaler and the match value
|
||||
uint32_t period_c;
|
||||
uint32_t match;
|
||||
uint32_t prescaler = compute_prescaler_period_and_match_value(ch, &period_c, &match);
|
||||
|
||||
// set the prescaler
|
||||
MAP_TimerPrescaleSet(ch->timer->timer, ch->channel, (prescaler < 0xFF) ? prescaler : 0);
|
||||
|
||||
// set the load value
|
||||
MAP_TimerLoadSet(ch->timer->timer, ch->channel, period_c);
|
||||
|
||||
// configure the pwm if we are in such mode
|
||||
if ((ch->timer->config & 0x0F) == TIMER_CFG_A_PWM) {
|
||||
// invert the timer output if required
|
||||
MAP_TimerControlLevel(ch->timer->timer, ch->channel, (ch->polarity == PYBTIMER_POLARITY_NEG) ? true : false);
|
||||
// set the match value (which is simply the duty cycle translated to ticks)
|
||||
MAP_TimerMatchSet(ch->timer->timer, ch->channel, match);
|
||||
}
|
||||
// configure the event edge type if we are in such mode
|
||||
else if ((ch->timer->config & 0x0F) == TIMER_CFG_A_CAP_COUNT || (ch->timer->config & 0x0F) == TIMER_CFG_A_CAP_TIME) {
|
||||
uint32_t polarity = TIMER_EVENT_BOTH_EDGES;
|
||||
if (ch->polarity == PYBTIMER_POLARITY_POS) {
|
||||
polarity = TIMER_EVENT_POS_EDGE;
|
||||
}
|
||||
else if (ch->polarity == PYBTIMER_POLARITY_NEG) {
|
||||
polarity = TIMER_EVENT_NEG_EDGE;
|
||||
}
|
||||
MAP_TimerControlEvent(ch->timer->timer, ch->channel, polarity);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
// stall the timer when the processor is halted while debugging
|
||||
MAP_TimerControlStall(ch->timer->timer, ch->channel, true);
|
||||
#endif
|
||||
|
||||
// now enable the timer channel
|
||||
MAP_TimerEnable(ch->timer->timer, ch->channel);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* Micro Python bindings */
|
||||
|
||||
STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pyb_timer_obj_t *tim = self_in;
|
||||
uint32_t mode = tim->config & 0xFF;
|
||||
|
||||
// timer mode
|
||||
qstr mode_qst = MP_QSTR_PWM;
|
||||
switch(mode) {
|
||||
case TIMER_CFG_A_ONE_SHOT:
|
||||
mode_qst = MP_QSTR_ONE_SHOT;
|
||||
break;
|
||||
case TIMER_CFG_A_PERIODIC:
|
||||
mode_qst = MP_QSTR_PERIODIC;
|
||||
break;
|
||||
case TIMER_CFG_A_CAP_COUNT:
|
||||
mode_qst = MP_QSTR_EDGE_COUNT;
|
||||
break;
|
||||
case TIMER_CFG_A_CAP_TIME:
|
||||
mode_qst = MP_QSTR_EDGE_TIME;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mp_printf(print, "<Timer%u, mode=Timer.%q>", (tim->id + 1), mode_qst);
|
||||
}
|
||||
|
||||
/// \method init(mode, *, width)
|
||||
/// Initialise the timer. Initialisation must give the desired mode
|
||||
/// and an optional timer width
|
||||
///
|
||||
/// tim.init(mode=Timer.ONE_SHOT, width=32) # one shot mode
|
||||
/// tim.init(mode=Timer.PERIODIC) # configure in free running periodic mode
|
||||
/// split into two 16-bit independent timers
|
||||
///
|
||||
/// Keyword arguments:
|
||||
///
|
||||
/// - `width` - specifies the width of the timer. Default is 32 bit mode. When in 16 bit mode
|
||||
/// the timer is splitted into 2 independent channels.
|
||||
///
|
||||
STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *tim, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, },
|
||||
{ MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
|
||||
};
|
||||
|
||||
// parse args
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// check the mode
|
||||
uint32_t _mode = args[0].u_int;
|
||||
if (_mode != TIMER_CFG_A_ONE_SHOT && _mode != TIMER_CFG_A_PERIODIC && _mode != TIMER_CFG_A_CAP_COUNT &&
|
||||
_mode != TIMER_CFG_A_CAP_TIME && _mode != TIMER_CFG_A_PWM) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// check the width
|
||||
if (args[1].u_int != 16 && args[1].u_int != 32) {
|
||||
goto error;
|
||||
}
|
||||
bool is16bit = (args[1].u_int == 16);
|
||||
|
||||
if (!is16bit && (_mode != TIMER_CFG_A_ONE_SHOT && _mode != TIMER_CFG_A_PERIODIC)) {
|
||||
// 32-bit mode is only available when in free running modes
|
||||
goto error;
|
||||
}
|
||||
tim->config = is16bit ? ((_mode | (_mode << 8)) | TIMER_CFG_SPLIT_PAIR) : _mode;
|
||||
|
||||
timer_init(tim);
|
||||
// register it with the sleep module
|
||||
pybsleep_add ((const mp_obj_t)tim, (WakeUpCB_t)timer_init);
|
||||
|
||||
return mp_const_none;
|
||||
|
||||
error:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
/// \classmethod \constructor(id, ...)
|
||||
/// Construct a new timer object of the given id. If additional
|
||||
/// arguments are given, then the timer is initialised by `init(...)`.
|
||||
/// `id` can be 1 to 4
|
||||
STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
// check arguments
|
||||
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
||||
|
||||
// create a new Timer object
|
||||
int32_t timer_idx = mp_obj_get_int(args[0]) - 1;
|
||||
if (timer_idx < 0 || timer_idx > (PYBTIMER_NUM_TIMERS - 1)) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
|
||||
}
|
||||
|
||||
pyb_timer_obj_t *tim = &pyb_timer_obj[timer_idx];
|
||||
tim->base.type = &pyb_timer_type;
|
||||
tim->id = timer_idx;
|
||||
|
||||
if (n_args > 1 || n_kw > 0) {
|
||||
// start the peripheral
|
||||
mp_map_t kw_args;
|
||||
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
||||
pyb_timer_init_helper(tim, n_args - 1, args + 1, &kw_args);
|
||||
}
|
||||
return (mp_obj_t)tim;
|
||||
}
|
||||
|
||||
// \method init()
|
||||
/// initializes the timer
|
||||
STATIC mp_obj_t pyb_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
return pyb_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init);
|
||||
|
||||
// \method deinit()
|
||||
/// disables the timer
|
||||
STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
|
||||
pyb_timer_obj_t *self = self_in;
|
||||
timer_disable(self);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
|
||||
|
||||
/// \method channel(channel, *, freq, period, polarity, duty_cycle)
|
||||
/// Initialise the timer channel. Initialization requires at least a frequency param. With no
|
||||
/// extra params given besides the channel id, the channel is returned with the previous configuration
|
||||
/// os 'None', if it hasn't been initialized before.
|
||||
///
|
||||
/// tim1.channel(Timer.A, freq=1000) # set channel A frequency to 1KHz
|
||||
/// tim2.channel(Timer.AB, freq=10) # both channels (because it's a 32 bit timer) combined to create a 10Hz timer
|
||||
///
|
||||
/// when initialiazing the channel of a 32-bit timer, channel ID MUST be = Timer.AB
|
||||
///
|
||||
/// Keyword arguments:
|
||||
///
|
||||
/// - `freq` - specifies the frequency in Hz.
|
||||
/// - `period` - specifies the period in microseconds.
|
||||
/// - `polarity` - in PWM specifies the polarity of the pulse. In capture mode specifies the edge to capture.
|
||||
/// in order to capture on both negative and positive edges, make it = Timer.POSITIVE | Timer.NEGATIVE.
|
||||
/// - `duty_cycle` - sets the duty cycle value
|
||||
///
|
||||
STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBTIMER_POLARITY_POS} },
|
||||
{ MP_QSTR_duty_cycle, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
};
|
||||
|
||||
pyb_timer_obj_t *tim = pos_args[0];
|
||||
mp_int_t channel_n = mp_obj_get_int(pos_args[1]);
|
||||
|
||||
// verify that the timer has been already initialized
|
||||
if (!tim->config) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
|
||||
}
|
||||
if (channel_n != TIMER_A && channel_n != TIMER_B && channel_n != (TIMER_A | TIMER_B)) {
|
||||
// invalid channel
|
||||
goto error;
|
||||
}
|
||||
if (channel_n == (TIMER_A | TIMER_B) && (tim->config & TIMER_CFG_SPLIT_PAIR)) {
|
||||
// 32-bit channel selected when the timer is in 16-bit mode
|
||||
goto error;
|
||||
}
|
||||
|
||||
// if only the channel number is given return the previously
|
||||
// allocated channel (or None if no previous channel)
|
||||
if (n_args == 2 && kw_args->used == 0) {
|
||||
pyb_timer_channel_obj_t *ch;
|
||||
if ((ch = pyb_timer_channel_find(tim->timer, channel_n))) {
|
||||
return ch;
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
// parse the arguments
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// throw an exception if both frequency and period are given
|
||||
if (args[0].u_int != 0 && args[1].u_int != 0) {
|
||||
goto error;
|
||||
}
|
||||
// check that at least one of them has a valid value
|
||||
if (args[0].u_int <= 0 && args[1].u_int <= 0) {
|
||||
goto error;
|
||||
}
|
||||
// check that the polarity is not 'both' in pwm mode
|
||||
if ((tim->config & TIMER_A) == TIMER_CFG_A_PWM && args[2].u_int == (PYBTIMER_POLARITY_POS | PYBTIMER_POLARITY_NEG)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// allocate a new timer channel
|
||||
pyb_timer_channel_obj_t *ch = m_new_obj(pyb_timer_channel_obj_t);
|
||||
ch->base.type = &pyb_timer_channel_type;
|
||||
ch->timer = tim;
|
||||
ch->channel = channel_n;
|
||||
|
||||
// get the frequency the polarity and the duty cycle
|
||||
ch->frequency = args[0].u_int;
|
||||
ch->period = args[1].u_int;
|
||||
ch->polarity = args[2].u_int;
|
||||
ch->duty_cycle = MIN(100, MAX(0, args[3].u_int));
|
||||
|
||||
timer_channel_init(ch);
|
||||
|
||||
// register it with the sleep module
|
||||
pybsleep_add ((const mp_obj_t)ch, (WakeUpCB_t)timer_channel_init);
|
||||
|
||||
// add the timer to the list
|
||||
pyb_timer_channel_add(ch);
|
||||
|
||||
return ch;
|
||||
|
||||
error:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_channel_obj, 2, pyb_timer_channel);
|
||||
|
||||
STATIC const mp_map_elem_t pyb_timer_locals_dict_table[] = {
|
||||
// instance methods
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_timer_init_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_timer_deinit_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_channel), (mp_obj_t)&pyb_timer_channel_obj },
|
||||
|
||||
// class constants
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_A), MP_OBJ_NEW_SMALL_INT(TIMER_A) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_B), MP_OBJ_NEW_SMALL_INT(TIMER_B) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ONE_SHOT), MP_OBJ_NEW_SMALL_INT(TIMER_CFG_A_ONE_SHOT) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERIODIC), MP_OBJ_NEW_SMALL_INT(TIMER_CFG_A_PERIODIC) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_EDGE_COUNT), MP_OBJ_NEW_SMALL_INT(TIMER_CFG_A_CAP_COUNT) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_EDGE_TIME), MP_OBJ_NEW_SMALL_INT(TIMER_CFG_A_CAP_TIME) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_PWM), MP_OBJ_NEW_SMALL_INT(TIMER_CFG_A_PWM) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_POSITIVE), MP_OBJ_NEW_SMALL_INT(PYBTIMER_POLARITY_POS) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_NEGATIVE), MP_OBJ_NEW_SMALL_INT(PYBTIMER_POLARITY_NEG) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(pyb_timer_locals_dict, pyb_timer_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t pyb_timer_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Timer,
|
||||
.print = pyb_timer_print,
|
||||
.make_new = pyb_timer_make_new,
|
||||
.locals_dict = (mp_obj_t)&pyb_timer_locals_dict,
|
||||
};
|
||||
|
||||
STATIC const mp_cb_methods_t pyb_timer_channel_cb_methods = {
|
||||
.init = pyb_timer_channel_callback,
|
||||
.enable = pyb_timer_channel_callback_enable,
|
||||
.disable = pyb_timer_channel_callback_disable,
|
||||
};
|
||||
|
||||
STATIC void TIMERGenericIntHandler(uint32_t timer, uint16_t channel) {
|
||||
pyb_timer_channel_obj_t *self;
|
||||
uint32_t status;
|
||||
|
||||
if ((self = pyb_timer_channel_find(timer, channel))) {
|
||||
status = MAP_TimerIntStatus(self->timer->timer, true) & self->channel;
|
||||
MAP_TimerIntClear(self->timer->timer, status);
|
||||
mp_obj_t _callback = mpcallback_find(self);
|
||||
mpcallback_handler(_callback);
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void TIMER0AIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA0_BASE, TIMER_A);
|
||||
}
|
||||
|
||||
STATIC void TIMER0BIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA0_BASE, TIMER_B);
|
||||
}
|
||||
|
||||
STATIC void TIMER1AIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA1_BASE, TIMER_A);
|
||||
}
|
||||
|
||||
STATIC void TIMER1BIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA1_BASE, TIMER_B);
|
||||
}
|
||||
|
||||
STATIC void TIMER2AIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA2_BASE, TIMER_A);
|
||||
}
|
||||
|
||||
STATIC void TIMER2BIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA2_BASE, TIMER_B);
|
||||
}
|
||||
|
||||
STATIC void TIMER3AIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA3_BASE, TIMER_A);
|
||||
}
|
||||
|
||||
STATIC void TIMER3BIntHandler(void) {
|
||||
TIMERGenericIntHandler(TIMERA3_BASE, TIMER_B);
|
||||
}
|
||||
|
||||
STATIC void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pyb_timer_channel_obj_t *ch = self_in;
|
||||
char *ch_id = "AB";
|
||||
// timer channel
|
||||
if (ch->channel == TIMER_A) {
|
||||
ch_id = "A";
|
||||
}
|
||||
else if (ch->channel == TIMER_B) {
|
||||
ch_id = "B";
|
||||
}
|
||||
|
||||
mp_printf(print, "<%q %s, timer=%u, %q=%u", MP_QSTR_TimerChannel,
|
||||
ch_id, (ch->timer->id + 1), MP_QSTR_freq, ch->frequency);
|
||||
|
||||
uint32_t mode = ch->timer->config & 0xFF;
|
||||
if (mode == TIMER_CFG_A_CAP_COUNT || mode == TIMER_CFG_A_CAP_TIME || mode == TIMER_CFG_A_PWM) {
|
||||
mp_printf(print, ", %q=Timer.", MP_QSTR_polarity);
|
||||
switch (ch->polarity) {
|
||||
case PYBTIMER_POLARITY_POS:
|
||||
mp_printf(print, "POSITIVE");
|
||||
break;
|
||||
case PYBTIMER_POLARITY_NEG:
|
||||
mp_printf(print, "NEGATIVE");
|
||||
break;
|
||||
default:
|
||||
mp_printf(print, "BOTH");
|
||||
break;
|
||||
}
|
||||
if (mode == TIMER_CFG_A_PWM) {
|
||||
mp_printf(print, ", %q=%u", MP_QSTR_duty_cycle, ch->duty_cycle);
|
||||
}
|
||||
}
|
||||
mp_printf(print, ">");
|
||||
}
|
||||
|
||||
/// \method freq([value])
|
||||
/// get or set the frequency of the timer channel
|
||||
STATIC mp_obj_t pyb_timer_channel_freq(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
pyb_timer_channel_obj_t *ch = args[0];
|
||||
if (n_args == 1) {
|
||||
// get
|
||||
return mp_obj_new_int(ch->frequency);
|
||||
} else {
|
||||
// set
|
||||
int32_t _frequency = mp_obj_get_int(args[1]);
|
||||
if (_frequency <= 0) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
ch->frequency = _frequency;
|
||||
ch->period = 1000000 / _frequency;
|
||||
timer_channel_init(ch);
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_freq_obj, 1, 2, pyb_timer_channel_freq);
|
||||
|
||||
/// \method period([value])
|
||||
/// get or set the period of the timer channel in microseconds
|
||||
STATIC mp_obj_t pyb_timer_channel_period(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
pyb_timer_channel_obj_t *ch = args[0];
|
||||
if (n_args == 1) {
|
||||
// get
|
||||
return mp_obj_new_int(ch->period);
|
||||
} else {
|
||||
// set
|
||||
int32_t _period = mp_obj_get_int(args[1]);
|
||||
if (_period <= 0) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
ch->period = _period;
|
||||
ch->frequency = 1000000 / _period;
|
||||
timer_channel_init(ch);
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_period_obj, 1, 2, pyb_timer_channel_period);
|
||||
|
||||
/// \method time([value])
|
||||
/// get or set the value of the timer channel in microseconds
|
||||
STATIC mp_obj_t pyb_timer_channel_time(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
pyb_timer_channel_obj_t *ch = args[0];
|
||||
uint32_t value;
|
||||
// calculate the period, the prescaler and the match value
|
||||
uint32_t period_c;
|
||||
uint32_t match;
|
||||
(void)compute_prescaler_period_and_match_value(ch, &period_c, &match);
|
||||
if (n_args == 1) {
|
||||
// get
|
||||
value = (ch->channel == TIMER_B) ? HWREG(ch->timer->timer + TIMER_O_TBV) : HWREG(ch->timer->timer + TIMER_O_TAV);
|
||||
// return the current timer value in microseconds
|
||||
// substract value to period since we are always operating in count-down mode
|
||||
uint32_t time_t = (1000 * (period_c - value)) / period_c;
|
||||
return mp_obj_new_int((time_t * 1000) / ch->frequency);
|
||||
}
|
||||
else {
|
||||
// set
|
||||
value = (mp_obj_get_int(args[1]) * ((ch->frequency * period_c) / 1000)) / 1000;
|
||||
if ((value > 0xFFFF) && (ch->timer->config & TIMER_CFG_SPLIT_PAIR)) {
|
||||
// this exceeds the maximum value of a 16-bit timer
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
// write period minus value since we are always operating in count-down mode
|
||||
TimerValueSet (ch->timer->timer, ch->channel, (period_c - value));
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_time_obj, 1, 2, pyb_timer_channel_time);
|
||||
|
||||
/// \method event_count()
|
||||
/// get the number of events triggered by the configured edge
|
||||
STATIC mp_obj_t pyb_timer_channel_event_count(mp_obj_t self_in) {
|
||||
pyb_timer_channel_obj_t *ch = self_in;
|
||||
return mp_obj_new_int(MAP_TimerValueGet(ch->timer->timer, ch->channel == (TIMER_A | TIMER_B) ? TIMER_A : ch->channel));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_channel_event_count_obj, pyb_timer_channel_event_count);
|
||||
|
||||
/// \method event_time()
|
||||
/// get the time at which the last event was triggered
|
||||
STATIC mp_obj_t pyb_timer_channel_event_time(mp_obj_t self_in) {
|
||||
pyb_timer_channel_obj_t *ch = self_in;
|
||||
// calculate the period, the prescaler and the match value
|
||||
uint32_t period_c;
|
||||
uint32_t match;
|
||||
(void)compute_prescaler_period_and_match_value(ch, &period_c, &match);
|
||||
uint32_t value = MAP_TimerValueGet(ch->timer->timer, ch->channel == (TIMER_A | TIMER_B) ? TIMER_A : ch->channel);
|
||||
// substract value to period since we are always operating in count-down mode
|
||||
uint32_t time_t = (1000 * (period_c - value)) / period_c;
|
||||
return mp_obj_new_int((time_t * 1000) / ch->frequency);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_channel_event_time_obj, pyb_timer_channel_event_time);
|
||||
|
||||
/// \method duty_cycle()
|
||||
/// get or set the duty cycle when in PWM mode
|
||||
STATIC mp_obj_t pyb_timer_channel_duty_cycle(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
pyb_timer_channel_obj_t *ch = args[0];
|
||||
if (n_args == 1) {
|
||||
// get
|
||||
return mp_obj_new_int(ch->duty_cycle);
|
||||
}
|
||||
else {
|
||||
// duty cycle must be converted from percentage to ticks
|
||||
// calculate the period, the prescaler and the match value
|
||||
uint32_t period_c;
|
||||
uint32_t match;
|
||||
ch->duty_cycle = MIN(100, MAX(0, mp_obj_get_int(args[1])));
|
||||
compute_prescaler_period_and_match_value(ch, &period_c, &match);
|
||||
if (n_args == 3) {
|
||||
// set the new polarity if requested
|
||||
ch->polarity = mp_obj_get_int(args[2]);
|
||||
MAP_TimerControlLevel(ch->timer->timer, ch->channel, (ch->polarity == PYBTIMER_POLARITY_NEG) ? true : false);
|
||||
}
|
||||
MAP_TimerMatchSet(ch->timer->timer, ch->channel, match);
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_duty_cycle_obj, 1, 3, pyb_timer_channel_duty_cycle);
|
||||
|
||||
/// \method callback(handler, priority, value)
|
||||
/// create a callback object associated with the timer channel
|
||||
STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
mp_arg_val_t args[mpcallback_INIT_NUM_ARGS];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mpcallback_INIT_NUM_ARGS, mpcallback_init_args, args);
|
||||
|
||||
pyb_timer_channel_obj_t *ch = pos_args[0];
|
||||
mp_obj_t _callback = mpcallback_find(ch);
|
||||
if (kw_args->used > 0 || !_callback) {
|
||||
// convert the priority to the correct value
|
||||
uint priority = mpcallback_translate_priority (args[2].u_int);
|
||||
|
||||
// validate the power mode
|
||||
uint pwrmode = args[4].u_int;
|
||||
if (pwrmode != PYB_PWR_MODE_ACTIVE) {
|
||||
goto invalid_args;
|
||||
}
|
||||
|
||||
uint32_t _config = (ch->channel == TIMER_B) ? ((ch->timer->config & TIMER_B) >> 8) : (ch->timer->config & TIMER_A);
|
||||
|
||||
// validate and set the value if we are in edge count mode
|
||||
if (_config == TIMER_CFG_A_CAP_COUNT) {
|
||||
uint32_t c_value = args[3].u_int;
|
||||
if (!c_value || c_value > 0xFFFF) {
|
||||
// zero or exceeds the maximum value of a 16-bit timer
|
||||
goto invalid_args;
|
||||
}
|
||||
MAP_TimerMatchSet(ch->timer->timer, ch->channel, c_value);
|
||||
}
|
||||
|
||||
// disable the callback first
|
||||
pyb_timer_channel_callback_disable(ch);
|
||||
|
||||
uint8_t shift = (ch->channel == TIMER_B) ? 8 : 0;
|
||||
switch (_config) {
|
||||
case TIMER_CFG_A_ONE_SHOT:
|
||||
case TIMER_CFG_A_PERIODIC:
|
||||
ch->timer->intflags |= TIMER_TIMA_TIMEOUT << shift;
|
||||
break;
|
||||
case TIMER_CFG_A_CAP_COUNT:
|
||||
ch->timer->intflags |= TIMER_CAPA_MATCH << shift;
|
||||
// set the match value and make 1 the minimum
|
||||
MAP_TimerMatchSet(ch->timer->timer, ch->channel, MAX(1, args[3].u_int));
|
||||
break;
|
||||
case TIMER_CFG_A_CAP_TIME:
|
||||
ch->timer->intflags |= TIMER_CAPA_EVENT << shift;
|
||||
break;
|
||||
case TIMER_CFG_A_PWM:
|
||||
// special case for the PWM match interrupt
|
||||
ch->timer->intflags |= ((ch->channel & TIMER_A) == TIMER_A) ? TIMER_TIMA_MATCH : TIMER_TIMB_MATCH;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// special case for a 32-bit timer
|
||||
if (ch->channel == (TIMER_A | TIMER_B)) {
|
||||
ch->timer->intflags |= (ch->timer->intflags << 8);
|
||||
}
|
||||
|
||||
void (*pfnHandler)(void);
|
||||
uint32_t intregister;
|
||||
switch (ch->timer->timer) {
|
||||
case TIMERA0_BASE:
|
||||
if (ch->channel == TIMER_B) {
|
||||
pfnHandler = &TIMER0BIntHandler;
|
||||
intregister = INT_TIMERA0B;
|
||||
} else {
|
||||
pfnHandler = &TIMER0AIntHandler;
|
||||
intregister = INT_TIMERA0A;
|
||||
}
|
||||
break;
|
||||
case TIMERA1_BASE:
|
||||
if (ch->channel == TIMER_B) {
|
||||
pfnHandler = &TIMER1BIntHandler;
|
||||
intregister = INT_TIMERA1B;
|
||||
} else {
|
||||
pfnHandler = &TIMER1AIntHandler;
|
||||
intregister = INT_TIMERA1A;
|
||||
}
|
||||
break;
|
||||
case TIMERA2_BASE:
|
||||
if (ch->channel == TIMER_B) {
|
||||
pfnHandler = &TIMER2BIntHandler;
|
||||
intregister = INT_TIMERA2B;
|
||||
} else {
|
||||
pfnHandler = &TIMER2AIntHandler;
|
||||
intregister = INT_TIMERA2A;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (ch->channel == TIMER_B) {
|
||||
pfnHandler = &TIMER3BIntHandler;
|
||||
intregister = INT_TIMERA3B;
|
||||
} else {
|
||||
pfnHandler = &TIMER3AIntHandler;
|
||||
intregister = INT_TIMERA3A;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// register the interrupt and configure the priority
|
||||
MAP_IntPrioritySet(intregister, priority);
|
||||
MAP_TimerIntRegister(ch->timer->timer, ch->channel, pfnHandler);
|
||||
|
||||
// create the callback
|
||||
_callback = mpcallback_new (ch, args[1].u_obj, &pyb_timer_channel_cb_methods);
|
||||
|
||||
// reload the timer
|
||||
uint32_t period_c;
|
||||
uint32_t match;
|
||||
compute_prescaler_period_and_match_value(ch, &period_c, &match);
|
||||
MAP_TimerLoadSet(ch->timer->timer, ch->channel, period_c);
|
||||
|
||||
// enable the callback before returning
|
||||
pyb_timer_channel_callback_enable(ch);
|
||||
}
|
||||
return _callback;
|
||||
|
||||
invalid_args:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_channel_callback_obj, 1, pyb_timer_channel_callback);
|
||||
|
||||
STATIC const mp_map_elem_t pyb_timer_channel_locals_dict_table[] = {
|
||||
// instance methods
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_timer_channel_freq_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_period), (mp_obj_t)&pyb_timer_channel_period_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&pyb_timer_channel_time_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_event_count), (mp_obj_t)&pyb_timer_channel_event_count_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_event_time), (mp_obj_t)&pyb_timer_channel_event_time_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_duty_cycle), (mp_obj_t)&pyb_timer_channel_duty_cycle_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_timer_channel_callback_obj },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(pyb_timer_channel_locals_dict, pyb_timer_channel_locals_dict_table);
|
||||
|
||||
STATIC const mp_obj_type_t pyb_timer_channel_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_TimerChannel,
|
||||
.print = pyb_timer_channel_print,
|
||||
.locals_dict = (mp_obj_t)&pyb_timer_channel_locals_dict,
|
||||
};
|
||||
|
||||
@@ -25,27 +25,14 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define WIPY_SD
|
||||
/******************************************************************************
|
||||
DECLARE EXPORTED DATA
|
||||
******************************************************************************/
|
||||
extern const mp_obj_type_t pyb_timer_type;
|
||||
|
||||
#define MICROPY_HW_BOARD_NAME "WiPy-SD"
|
||||
#define MICROPY_HW_MCU_NAME "CC3200"
|
||||
/******************************************************************************
|
||||
DECLARE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
void timer_init0 (void);
|
||||
void timer_disable_all (void);
|
||||
|
||||
#define MICROPY_HW_HAS_SDCARD (1)
|
||||
#define MICROPY_HW_ENABLE_RNG (1)
|
||||
#define MICROPY_HW_ENABLE_RTC (1)
|
||||
|
||||
#define MICROPY_STDIO_UART PYB_UART_0
|
||||
#define MICROPY_STDIO_UART_BAUD 115200
|
||||
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
|
||||
|
||||
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA3
|
||||
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3
|
||||
#define MICROPY_SYS_LED_PORT GPIOA3_BASE
|
||||
#define MICROPY_SAFE_BOOT_PORT GPIOA3_BASE
|
||||
#define MICROPY_SYS_LED_GPIO pin_GPIO25
|
||||
#define MICROPY_SYS_LED_PIN_NUM PIN_21 // GPIO25 (SOP2)
|
||||
#define MICROPY_SAFE_BOOT_PIN_NUM PIN_18 // GPIO28
|
||||
#define MICROPY_SYS_LED_PORT_PIN GPIO_PIN_1
|
||||
#define MICROPY_SAFE_BOOT_PORT_PIN GPIO_PIN_4
|
||||
|
||||
#define MICROPY_PORT_SFLASH_BLOCK_COUNT 96
|
||||
@@ -51,6 +51,7 @@
|
||||
#include "mpexception.h"
|
||||
#include "py/mpstate.h"
|
||||
#include "osi.h"
|
||||
#include "utils.h"
|
||||
|
||||
/// \moduleref pyb
|
||||
/// \class UART - duplex serial communication bus
|
||||
@@ -62,7 +63,7 @@
|
||||
///
|
||||
/// from pyb import UART
|
||||
///
|
||||
/// uart = UART(0, 9600) # init with given baudrate
|
||||
/// uart = UART(1, 9600) # init with given baudrate
|
||||
/// uart.init(9600, bits=8, stop=1, parity=None) # init with given parameters
|
||||
///
|
||||
/// Bits can be 5, 6, 7, 8, parity can be None, 0 (even), 1 (odd). Stop can be 1 or 2.
|
||||
@@ -88,8 +89,8 @@
|
||||
/******************************************************************************
|
||||
DEFINE CONSTANTS
|
||||
******************************************************************************/
|
||||
#define PYBUART_TX_WAIT_MS 1
|
||||
#define PYBUART_TX_MAX_TIMEOUT_MS 5
|
||||
#define PYBUART_TX_WAIT_US (50)
|
||||
#define PYBUART_TX_MAX_TIMEOUT_MS (5)
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
@@ -101,6 +102,7 @@ STATIC void UART0IntHandler(void);
|
||||
STATIC void UART1IntHandler(void);
|
||||
STATIC void uart_callback_enable (mp_obj_t self_in);
|
||||
STATIC void uart_callback_disable (mp_obj_t self_in);
|
||||
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE TYPES
|
||||
@@ -124,7 +126,8 @@ struct _pyb_uart_obj_t {
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS];
|
||||
STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS] = {{.reg = UARTA0_BASE, .baudrate = 0, .peripheral = PRCM_UARTA0},
|
||||
{.reg = UARTA1_BASE, .baudrate = 0, .peripheral = PRCM_UARTA1}};
|
||||
STATIC const mp_cb_methods_t uart_cb_methods;
|
||||
|
||||
/******************************************************************************
|
||||
@@ -153,10 +156,10 @@ bool uart_tx_char(pyb_uart_obj_t *self, int c) {
|
||||
uint32_t timeout = 0;
|
||||
|
||||
while (!MAP_UARTCharPutNonBlocking(self->reg, c)) {
|
||||
if (timeout++ > (PYBUART_TX_MAX_TIMEOUT_MS / PYBUART_TX_WAIT_MS)) {
|
||||
if (timeout++ > ((PYBUART_TX_MAX_TIMEOUT_MS * 1000) / PYBUART_TX_WAIT_US)) {
|
||||
return false;
|
||||
}
|
||||
HAL_Delay (PYBUART_TX_WAIT_MS);
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBUART_TX_WAIT_US));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -208,20 +211,19 @@ mp_obj_t uart_callback_new (pyb_uart_obj_t *self, mp_obj_t handler, uint rxbuffe
|
||||
return _callback;
|
||||
}
|
||||
|
||||
void uart_disable_all (void) {
|
||||
for (int i = 0; i < PYB_NUM_UARTS; i++) {
|
||||
// in case it's not clocked
|
||||
MAP_PRCMPeripheralClkEnable(pyb_uart_obj[i].peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
pyb_uart_deinit(&pyb_uart_obj[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
// assumes init parameters have been set up correctly
|
||||
STATIC void uart_init (pyb_uart_obj_t *self) {
|
||||
if (self->uart_id == PYB_UART_0) {
|
||||
self->reg = UARTA0_BASE;
|
||||
self->peripheral = PRCM_UARTA0;
|
||||
}
|
||||
else {
|
||||
self->reg = UARTA1_BASE;
|
||||
self->peripheral = PRCM_UARTA1;
|
||||
}
|
||||
|
||||
// Enable the peripheral clock
|
||||
MAP_PRCMPeripheralClkEnable(self->peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
|
||||
@@ -315,7 +317,7 @@ STATIC void uart_callback_disable (mp_obj_t self_in) {
|
||||
STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
pyb_uart_obj_t *self = self_in;
|
||||
if (self->baudrate > 0) {
|
||||
mp_printf(print, "<UART%u, baudrate=%u, bits=", self->uart_id, self->baudrate);
|
||||
mp_printf(print, "<UART%u, baudrate=%u, bits=", (self->uart_id + 1), self->baudrate);
|
||||
switch (self->config & UART_CONFIG_WLEN_MASK) {
|
||||
case UART_CONFIG_WLEN_5:
|
||||
mp_print_str(print, "5");
|
||||
@@ -342,7 +344,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
|
||||
self->timeout, self->timeout_char, self->read_buf_len);
|
||||
}
|
||||
else {
|
||||
mp_printf(print, "<UART%u>", self->uart_id);
|
||||
mp_printf(print, "<UART%u>", (self->uart_id + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,11 +353,11 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
|
||||
/// Initialise the UART bus with the given parameters:
|
||||
///
|
||||
/// - `baudrate` is the clock rate.
|
||||
/// - `bits` is the number of bits per byte, 7, 8 or 9.
|
||||
/// - `bits` is the number of bits per byte, 5, 6, 7, 8
|
||||
/// - `parity` is the parity, `None`, 0 (even) or 1 (odd).
|
||||
/// - `stop` is the number of stop bits, 1 or 2.
|
||||
/// - `flowcontrol` is the flow control mode, `None`, `UART.FLOW_TX`,
|
||||
/// `UART.FLOW_RX', 'UART.FLOW_TXRX`.
|
||||
/// - `flow` is the flow control mode, `None`, `UART.RTS`,
|
||||
/// `UART.CTS', or `UART.CTS | UART.RTS`
|
||||
/// - `timeout` is the timeout (in milliseconds) when waiting for the first character.
|
||||
/// - `timeout_char` is the timeout (in milliseconds) between characters.
|
||||
STATIC const mp_arg_t pyb_uart_init_args[] = {
|
||||
@@ -402,7 +404,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
|
||||
self->config = UART_CONFIG_WLEN_8;
|
||||
break;
|
||||
default:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
goto error;
|
||||
break;
|
||||
}
|
||||
// Parity
|
||||
@@ -414,6 +416,10 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
|
||||
// Stop bits
|
||||
self->config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO);
|
||||
// Flow control
|
||||
if (args[4].u_int != UART_FLOWCONTROL_NONE || args[4].u_int != UART_FLOWCONTROL_TX ||
|
||||
args[4].u_int != UART_FLOWCONTROL_RX || args[4].u_int != (UART_FLOWCONTROL_TX | UART_FLOWCONTROL_RX)) {
|
||||
goto error;
|
||||
}
|
||||
self->flowcontrol = args[4].u_int;
|
||||
}
|
||||
else {
|
||||
@@ -427,11 +433,14 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
|
||||
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)uart_init);
|
||||
|
||||
return mp_const_none;
|
||||
|
||||
error:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
/// \classmethod \constructor(bus, ...)
|
||||
///
|
||||
/// Construct a UART object on the given bus id. `bus id` can be 0 or 1
|
||||
/// Construct a UART object on the given bus id. `bus id` can be 1 or 2
|
||||
/// With no additional parameters, the UART object is created but not
|
||||
/// initialised (it has the settings from the last initialisation of
|
||||
/// the bus, if any).
|
||||
@@ -447,7 +456,7 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
|
||||
mp_arg_check_num(n_args, n_kw, 1, MP_ARRAY_SIZE(pyb_uart_init_args), true);
|
||||
|
||||
// work out the uart id
|
||||
pyb_uart_id_t uart_id = mp_obj_get_int(args[0]);
|
||||
int32_t uart_id = mp_obj_get_int(args[0]) - 1;
|
||||
|
||||
if (uart_id < PYB_UART_0 || uart_id > PYB_UART_1) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
|
||||
@@ -457,6 +466,7 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
|
||||
pyb_uart_obj_t *self = &pyb_uart_obj[uart_id];
|
||||
self->base.type = &pyb_uart_type;
|
||||
self->uart_id = uart_id;
|
||||
|
||||
if (n_args > 1 || n_kw > 0) {
|
||||
// start the peripheral
|
||||
mp_map_t kw_args;
|
||||
@@ -474,7 +484,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
|
||||
|
||||
/// \method deinit()
|
||||
/// Turn off the UART bus.
|
||||
mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
|
||||
pyb_uart_obj_t *self = self_in;
|
||||
|
||||
// unregister it with the sleep module
|
||||
@@ -516,7 +526,7 @@ STATIC mp_obj_t pyb_uart_callback (mp_uint_t n_args, const mp_obj_t *pos_args, m
|
||||
uint priority = mpcallback_translate_priority (args[2].u_int);
|
||||
|
||||
// check the power mode
|
||||
if (PYB_PWR_MODE_ACTIVE != args[3].u_int) {
|
||||
if (PYB_PWR_MODE_ACTIVE != args[4].u_int) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
|
||||
@@ -538,7 +548,7 @@ STATIC mp_obj_t pyb_uart_writechar(mp_obj_t self_in, mp_obj_t char_in) {
|
||||
|
||||
// send the character
|
||||
if (!uart_tx_char(self, data)) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
@@ -559,6 +569,18 @@ STATIC mp_obj_t pyb_uart_readchar(mp_obj_t self_in) {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_readchar_obj, pyb_uart_readchar);
|
||||
|
||||
/// \method sendbreak()
|
||||
STATIC mp_obj_t pyb_uart_sendbreak(mp_obj_t self_in) {
|
||||
pyb_uart_obj_t *self = self_in;
|
||||
// send a break signal for at least 2 complete frames
|
||||
MAP_UARTBreakCtl(self->reg, true);
|
||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT((22 * 1000000) / self->baudrate));
|
||||
MAP_UARTBreakCtl(self->reg, false);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_sendbreak_obj, pyb_uart_sendbreak);
|
||||
|
||||
|
||||
STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = {
|
||||
// instance methods
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_uart_init_obj },
|
||||
@@ -579,12 +601,11 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = {
|
||||
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_writechar), (mp_obj_t)&pyb_uart_writechar_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readchar), (mp_obj_t)&pyb_uart_readchar_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sendbreak), (mp_obj_t)&pyb_uart_sendbreak_obj },
|
||||
|
||||
// class constants
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_FLOW_NONE), MP_OBJ_NEW_SMALL_INT(UART_FLOWCONTROL_NONE) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_FLOW_TX), MP_OBJ_NEW_SMALL_INT(UART_FLOWCONTROL_TX) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_FLOW_RX), MP_OBJ_NEW_SMALL_INT(UART_FLOWCONTROL_RX) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_FLOW_TXRX), MP_OBJ_NEW_SMALL_INT(UART_FLOWCONTROL_TX | UART_FLOWCONTROL_RX) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_CTS), MP_OBJ_NEW_SMALL_INT(UART_FLOWCONTROL_TX) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTS), MP_OBJ_NEW_SMALL_INT(UART_FLOWCONTROL_RX) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table);
|
||||
@@ -622,7 +643,7 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
|
||||
|
||||
// write the data
|
||||
if (!uart_tx_strn(self, buf, size)) {
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -38,12 +38,12 @@ typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
|
||||
extern const mp_obj_type_t pyb_uart_type;
|
||||
|
||||
void uart_init0(void);
|
||||
mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
|
||||
bool uart_rx_any(pyb_uart_obj_t *uart_obj);
|
||||
int uart_rx_char(pyb_uart_obj_t *uart_obj);
|
||||
bool uart_tx_char(pyb_uart_obj_t *self, int c);
|
||||
bool uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
|
||||
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
|
||||
mp_obj_t uart_callback_new (pyb_uart_obj_t *self, mp_obj_t handler, uint rxbuffer_size, mp_int_t priority);
|
||||
void uart_disable_all (void);
|
||||
|
||||
#endif // PYBUART_H_
|
||||
|
||||
@@ -54,14 +54,16 @@
|
||||
******************************************************************************/
|
||||
typedef struct {
|
||||
bool servers;
|
||||
bool servers_sleeping;
|
||||
bool simplelink;
|
||||
bool running;
|
||||
}pybwdt_data_t;
|
||||
} pybwdt_data_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
static pybwdt_data_t pybwdt_data;
|
||||
STATIC pybwdt_data_t pybwdt_data = {.servers = false, .servers_sleeping = false, .simplelink = false, .running = false};
|
||||
STATIC const mp_obj_base_t pyb_wdt_obj = {&pyb_wdt_type};
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
@@ -69,22 +71,16 @@ static pybwdt_data_t pybwdt_data;
|
||||
// must be called in main.c just after initializing the hal
|
||||
__attribute__ ((section (".boot")))
|
||||
void pybwdt_init0 (void) {
|
||||
pybwdt_data.running = false;
|
||||
}
|
||||
|
||||
void pybwdt_kick (void) {
|
||||
// check that the servers and simplelink are running fine
|
||||
if (pybwdt_data.servers && pybwdt_data.simplelink && pybwdt_data.running) {
|
||||
pybwdt_data.servers = false;
|
||||
pybwdt_data.simplelink = false;
|
||||
MAP_WatchdogIntClear(WDT_BASE);
|
||||
}
|
||||
}
|
||||
|
||||
void pybwdt_srv_alive (void) {
|
||||
pybwdt_data.servers = true;
|
||||
}
|
||||
|
||||
void pybwdt_srv_sleeping (bool state) {
|
||||
pybwdt_data.servers_sleeping = state;
|
||||
}
|
||||
|
||||
void pybwdt_sl_alive (void) {
|
||||
pybwdt_data.simplelink = true;
|
||||
}
|
||||
@@ -92,58 +88,66 @@ void pybwdt_sl_alive (void) {
|
||||
/******************************************************************************/
|
||||
// Micro Python bindings
|
||||
|
||||
/// \function wdt_enable('msec')
|
||||
/// Enabled the watchdog timer with msec timeout value
|
||||
STATIC mp_obj_t pyb_enable_wdt(mp_obj_t self, mp_obj_t msec_in) {
|
||||
mp_int_t msec = mp_obj_get_int(msec_in);
|
||||
/// \function constructor('msec')
|
||||
/// Enables the watchdog timer with msec timeout value
|
||||
STATIC mp_obj_t pyb_wdt_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
// check the arguments
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
|
||||
if (msec < PYBWDT_MIN_TIMEOUT_MS) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
if (pybwdt_data.running) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
|
||||
if (n_args > 0) {
|
||||
mp_int_t msec = mp_obj_get_int(args[0]);
|
||||
if (msec < PYBWDT_MIN_TIMEOUT_MS) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
||||
}
|
||||
if (pybwdt_data.running) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
|
||||
}
|
||||
|
||||
// Enable the WDT peripheral clock
|
||||
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
|
||||
// Unlock to be able to configure the registers
|
||||
MAP_WatchdogUnlock(WDT_BASE);
|
||||
|
||||
#ifdef DEBUG
|
||||
// make the WDT stall when the debugger stops on a breakpoint
|
||||
MAP_WatchdogStallEnable (WDT_BASE);
|
||||
#endif
|
||||
|
||||
// set the watchdog timer reload value
|
||||
// the WDT trigger a system reset after the second timeout
|
||||
// so, divide by 2 the timeout value received
|
||||
MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(msec / 2));
|
||||
|
||||
// start the timer. Once it's started, it cannot be disabled.
|
||||
MAP_WatchdogEnable(WDT_BASE);
|
||||
pybwdt_data.running = true;
|
||||
}
|
||||
|
||||
// Enable the WDT peripheral clock
|
||||
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
|
||||
// Unlock to be able to configure the registers
|
||||
MAP_WatchdogUnlock(WDT_BASE);
|
||||
|
||||
// make the WDT stall when the debugger stops on a breakpoint
|
||||
MAP_WatchdogStallEnable (WDT_BASE);
|
||||
|
||||
// set the watchdog timer reload value
|
||||
// the WDT trigger a system reset after the second timeout
|
||||
// so, divide by the 2 timeout value received
|
||||
MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(msec / 2));
|
||||
|
||||
// start the timer. Once wdt is started, it cannot be disabled.
|
||||
MAP_WatchdogEnable(WDT_BASE);
|
||||
pybwdt_data.running = true;
|
||||
|
||||
return mp_const_none;
|
||||
return (mp_obj_t)&pyb_wdt_obj;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_enable_wdt_obj, pyb_enable_wdt);
|
||||
|
||||
/// \function wdt_kick()
|
||||
/// Kicks the watchdog timer
|
||||
STATIC mp_obj_t pyb_kick_wdt(mp_obj_t self) {
|
||||
pybwdt_kick ();
|
||||
if ((pybwdt_data.servers || pybwdt_data.servers_sleeping) && pybwdt_data.simplelink && pybwdt_data.running) {
|
||||
pybwdt_data.servers = false;
|
||||
pybwdt_data.simplelink = false;
|
||||
MAP_WatchdogIntClear(WDT_BASE);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_kick_wdt_obj, pyb_kick_wdt);
|
||||
|
||||
STATIC const mp_map_elem_t pybwdt_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&pyb_enable_wdt_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_kick), (mp_obj_t)&pyb_kick_wdt_obj },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(pybwdt_locals_dict, pybwdt_locals_dict_table);
|
||||
|
||||
static const mp_obj_type_t pybwdt_type = {
|
||||
const mp_obj_type_t pyb_wdt_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_WDT,
|
||||
.make_new = pyb_wdt_make_new,
|
||||
.locals_dict = (mp_obj_t)&pybwdt_locals_dict,
|
||||
};
|
||||
|
||||
const mp_obj_base_t pyb_wdt_obj = {&pybwdt_type};
|
||||
|
||||
@@ -29,11 +29,11 @@
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
extern const mp_obj_base_t pyb_wdt_obj;
|
||||
extern const mp_obj_type_t pyb_wdt_type;
|
||||
|
||||
void pybwdt_init0 (void);
|
||||
void pybwdt_kick (void);
|
||||
void pybwdt_srv_alive (void);
|
||||
void pybwdt_srv_sleeping (bool state);
|
||||
void pybwdt_sl_alive (void);
|
||||
|
||||
#endif /* PYBWDT_H_ */
|
||||
|
||||
@@ -48,7 +48,8 @@
|
||||
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
|
||||
#define MICROPY_OPT_COMPUTED_GOTO (0)
|
||||
#define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0)
|
||||
#define MICROPY_CPYTHON_COMPAT (0)
|
||||
#define MICROPY_CPYTHON_COMPAT (1)
|
||||
#define MICROPY_QSTR_BYTES_IN_HASH (1)
|
||||
|
||||
/* Enable FatFS LFNs
|
||||
0: Disable LFN feature.
|
||||
@@ -62,21 +63,22 @@
|
||||
#define MICROPY_MODULE_WEAK_LINKS (1)
|
||||
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
|
||||
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
||||
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (0)
|
||||
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
|
||||
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
|
||||
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
||||
#define MICROPY_PY_BUILTINS_EXECFILE (1)
|
||||
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (1)
|
||||
#define MICROPY_PY_MICROPYTHON_MEM_INFO (0)
|
||||
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (0)
|
||||
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (0)
|
||||
#define MICROPY_PY_SYS_MAXSIZE (0)
|
||||
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
|
||||
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
|
||||
#define MICROPY_PY_SYS_MAXSIZE (1)
|
||||
#define MICROPY_PY_SYS_EXIT (1)
|
||||
#define MICROPY_PY_SYS_STDFILES (1)
|
||||
#define MICROPY_PY_CMATH (0)
|
||||
#define MICROPY_PY_IO (1)
|
||||
#define MICROPY_PY_IO_FILEIO (1)
|
||||
#define MICROPY_PY_UBINASCII (1)
|
||||
#define MICROPY_PY_UCTYPES (1)
|
||||
#define MICROPY_PY_UBINASCII (0)
|
||||
#define MICROPY_PY_UCTYPES (0)
|
||||
#define MICROPY_PY_UZLIB (0)
|
||||
#define MICROPY_PY_UJSON (1)
|
||||
#define MICROPY_PY_URE (1)
|
||||
@@ -97,7 +99,6 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
|
||||
|
||||
// extra built in modules to add to the list of known ones
|
||||
extern const struct _mp_obj_module_t pyb_module;
|
||||
extern const struct _mp_obj_module_t mp_module_ubinascii;
|
||||
extern const struct _mp_obj_module_t mp_module_ure;
|
||||
extern const struct _mp_obj_module_t mp_module_ujson;
|
||||
extern const struct _mp_obj_module_t mp_module_uheapq;
|
||||
@@ -106,17 +107,23 @@ extern const struct _mp_obj_module_t mp_module_utime;
|
||||
extern const struct _mp_obj_module_t mp_module_uselect;
|
||||
extern const struct _mp_obj_module_t mp_module_usocket;
|
||||
extern const struct _mp_obj_module_t mp_module_network;
|
||||
extern const struct _mp_obj_module_t mp_module_uhashlib;
|
||||
extern const struct _mp_obj_module_t mp_module_ubinascii;
|
||||
extern const struct _mp_obj_module_t mp_module_ussl;
|
||||
|
||||
#define MICROPY_PORT_BUILTIN_MODULES \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ubinascii), (mp_obj_t)&mp_module_ubinascii }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ussl), (mp_obj_t)&mp_module_ussl }, \
|
||||
|
||||
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_binascii), (mp_obj_t)&mp_module_ubinascii }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&mp_module_ustruct }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_re), (mp_obj_t)&mp_module_ure }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_json), (mp_obj_t)&mp_module_ujson }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_heapq), (mp_obj_t)&mp_module_uheapq }, \
|
||||
@@ -124,7 +131,9 @@ extern const struct _mp_obj_module_t mp_module_network;
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_utime }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_module_uselect }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&mp_module_usocket }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&mp_module_ustruct }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_hashlib), (mp_obj_t)&mp_module_uhashlib }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_binascii), (mp_obj_t)&mp_module_ubinascii }, \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ssl), (mp_obj_t)&mp_module_ussl }, \
|
||||
|
||||
// extra constants
|
||||
#define MICROPY_PORT_CONSTANTS \
|
||||
@@ -136,9 +145,9 @@ extern const struct _mp_obj_module_t mp_module_network;
|
||||
const char *readline_hist[8]; \
|
||||
mp_obj_t mp_const_user_interrupt; \
|
||||
mp_obj_t pyb_config_main; \
|
||||
mp_obj_list_t mod_network_nic_list; \
|
||||
mp_obj_list_t pybsleep_obj_list; \
|
||||
mp_obj_list_t mpcallback_obj_list; \
|
||||
mp_obj_list_t pyb_timer_channel_obj_list; \
|
||||
|
||||
|
||||
// type definitions for the specific machine
|
||||
@@ -182,6 +191,7 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len);
|
||||
#define MICROPY_HAL_H "cc3200_hal.h"
|
||||
#define MICROPY_PORT_HAS_TELNET (1)
|
||||
#define MICROPY_PORT_HAS_FTP (1)
|
||||
#define MICROPY_PORT_WLAN_URN (0)
|
||||
#define MICROPY_PY_SYS_PLATFORM "WiPy"
|
||||
|
||||
#define MICROPY_PORT_WLAN_AP_SSID "wipy-wlan"
|
||||
|
||||
105
cc3200/mptask.c
105
cc3200/mptask.c
@@ -50,6 +50,7 @@
|
||||
#include "mperror.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modusocket.h"
|
||||
#include "modwlan.h"
|
||||
#include "serverstask.h"
|
||||
#include "telnet.h"
|
||||
@@ -63,7 +64,10 @@
|
||||
#include "pybsd.h"
|
||||
#include "pins.h"
|
||||
#include "pybsleep.h"
|
||||
#include "pybtimer.h"
|
||||
#include "mpcallback.h"
|
||||
#include "cryptohash.h"
|
||||
#include "updater.h"
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE CONSTANTS
|
||||
@@ -101,11 +105,14 @@ void TASK_Micropython (void *pvParameters) {
|
||||
// initialize the garbage collector with the top of our stack
|
||||
uint32_t sp = gc_helper_get_sp();
|
||||
gc_collect_init (sp);
|
||||
bool safeboot = false;
|
||||
FRESULT res;
|
||||
|
||||
bool safeboot = false;
|
||||
mptask_pre_init();
|
||||
|
||||
#ifndef DEBUG
|
||||
safeboot = PRCMGetSpecialBit(PRCM_SAFE_BOOT_BIT);
|
||||
#endif
|
||||
|
||||
soft_reset:
|
||||
|
||||
// GC init
|
||||
@@ -124,20 +131,18 @@ soft_reset:
|
||||
mperror_init0();
|
||||
uart_init0();
|
||||
pin_init0();
|
||||
timer_init0();
|
||||
readline_init0();
|
||||
mod_network_init0();
|
||||
#if MICROPY_HW_ENABLE_RNG
|
||||
rng_init0();
|
||||
#endif
|
||||
|
||||
// we are alive, so let the world know it
|
||||
mperror_enable_heartbeat();
|
||||
|
||||
#ifdef LAUNCHXL
|
||||
// configure the stdio uart pins with the correct alternate functions
|
||||
// param 3 ("mode") is DON'T CARE" for AFs others than GPIO
|
||||
pin_config ((pin_obj_t *)&pin_GPIO1, PIN_MODE_3, 0, PIN_TYPE_STD_PU, PIN_STRENGTH_2MA);
|
||||
pin_config ((pin_obj_t *)&pin_GPIO2, PIN_MODE_3, 0, PIN_TYPE_STD_PU, PIN_STRENGTH_2MA);
|
||||
pin_config ((pin_obj_t *)&MICROPY_STDIO_UART_TX_PIN, MICROPY_STDIO_UART_TX_PIN_AF, 0, PIN_TYPE_STD_PU, PIN_STRENGTH_2MA);
|
||||
pin_config ((pin_obj_t *)&MICROPY_STDIO_UART_RX_PIN, MICROPY_STDIO_UART_RX_PIN_AF, 0, PIN_TYPE_STD_PU, PIN_STRENGTH_2MA);
|
||||
// instantiate the stdio uart
|
||||
mp_obj_t args[2] = {
|
||||
mp_obj_new_int(MICROPY_STDIO_UART),
|
||||
@@ -160,9 +165,6 @@ soft_reset:
|
||||
else {
|
||||
// only if not comming out of hibernate or a soft reset
|
||||
mptask_enter_ap_mode();
|
||||
#ifndef DEBUG
|
||||
safeboot = PRCMIsSafeBootRequested();
|
||||
#endif
|
||||
}
|
||||
|
||||
// enable telnet and ftp
|
||||
@@ -180,18 +182,14 @@ soft_reset:
|
||||
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
|
||||
|
||||
if (!safeboot) {
|
||||
// run boot.py, if it exists
|
||||
const char *boot_py = "boot.py";
|
||||
res = f_stat(boot_py, NULL);
|
||||
if (res == FR_OK) {
|
||||
int ret = pyexec_file(boot_py);
|
||||
if (ret & PYEXEC_FORCED_EXIT) {
|
||||
goto soft_reset_exit;
|
||||
}
|
||||
if (!ret) {
|
||||
// flash the system led
|
||||
mperror_signal_error();
|
||||
}
|
||||
// run boot.py
|
||||
int ret = pyexec_file("boot.py");
|
||||
if (ret & PYEXEC_FORCED_EXIT) {
|
||||
goto soft_reset_exit;
|
||||
}
|
||||
if (!ret) {
|
||||
// flash the system led
|
||||
mperror_signal_error();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,16 +208,13 @@ soft_reset:
|
||||
} else {
|
||||
main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
|
||||
}
|
||||
res = f_stat(main_py, NULL);
|
||||
if (res == FR_OK) {
|
||||
int ret = pyexec_file(main_py);
|
||||
if (ret & PYEXEC_FORCED_EXIT) {
|
||||
goto soft_reset_exit;
|
||||
}
|
||||
if (!ret) {
|
||||
// flash the system led
|
||||
mperror_signal_error();
|
||||
}
|
||||
int ret = pyexec_file(main_py);
|
||||
if (ret & PYEXEC_FORCED_EXIT) {
|
||||
goto soft_reset_exit;
|
||||
}
|
||||
if (!ret) {
|
||||
// flash the system led
|
||||
mperror_signal_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -242,15 +237,26 @@ soft_reset_exit:
|
||||
|
||||
// soft reset
|
||||
pybsleep_signal_soft_reset();
|
||||
|
||||
mp_printf(&mp_plat_print, "PYB: soft reboot\n");
|
||||
|
||||
// disable all peripherals that could trigger a callback
|
||||
pyb_rtc_callback_disable(NULL);
|
||||
timer_disable_all();
|
||||
uart_disable_all();
|
||||
|
||||
// flush the serial flash buffer
|
||||
sflash_disk_flush();
|
||||
|
||||
// clean-up the user socket space
|
||||
modusocket_close_all_user_sockets();
|
||||
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
pybsd_deinit();
|
||||
#endif
|
||||
|
||||
// wait for pending transactions to complete
|
||||
HAL_Delay(20);
|
||||
|
||||
goto soft_reset;
|
||||
}
|
||||
|
||||
@@ -272,13 +278,21 @@ STATIC void mptask_pre_init (void) {
|
||||
// this one allocates memory for the nvic vault
|
||||
pybsleep_pre_init();
|
||||
|
||||
// this one allocates mameory for the WLAN semaphore
|
||||
// this one allocates memory for the WLAN semaphore
|
||||
wlan_pre_init();
|
||||
|
||||
// this one allocates memory for the updater semaphore
|
||||
updater_pre_init();
|
||||
|
||||
// this one allocates memory for the Socket semaphore
|
||||
modusocket_pre_init();
|
||||
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
pybsd_init0();
|
||||
#endif
|
||||
|
||||
CRYPTOHASH_Init();
|
||||
|
||||
#ifdef DEBUG
|
||||
ASSERT (OSI_OK == osi_TaskCreate(TASK_Servers,
|
||||
(const signed char *)"Servers",
|
||||
@@ -299,7 +313,6 @@ STATIC void mptask_init_sflash_filesystem (void) {
|
||||
|
||||
// Initialise the local flash filesystem.
|
||||
// Create it if needed, and mount in on /flash.
|
||||
// try to mount the flash
|
||||
FRESULT res = f_mount(sflash_fatfs, "/flash", 1);
|
||||
if (res == FR_NO_FILESYSTEM) {
|
||||
// no filesystem, so create a fresh one
|
||||
@@ -325,7 +338,20 @@ STATIC void mptask_init_sflash_filesystem (void) {
|
||||
// It is set to the internal flash filesystem by default.
|
||||
f_chdrive("/flash");
|
||||
|
||||
// Make sure we have a /flash/boot.py. Create it if needed.
|
||||
// create /flash/sys, /flash/lib and /flash/cert if they don't exist
|
||||
if (FR_OK != f_chdir ("/flash/sys")) {
|
||||
f_mkdir("/flash/sys");
|
||||
}
|
||||
if (FR_OK != f_chdir ("/flash/lib")) {
|
||||
f_mkdir("/flash/lib");
|
||||
}
|
||||
if (FR_OK != f_chdir ("/flash/cert")) {
|
||||
f_mkdir("/flash/cert");
|
||||
}
|
||||
|
||||
f_chdir ("/flash");
|
||||
|
||||
// make sure we have a /flash/boot.py. Create it if needed.
|
||||
res = f_stat("/flash/boot.py", &fno);
|
||||
if (res == FR_OK) {
|
||||
if (fno.fattrib & AM_DIR) {
|
||||
@@ -347,9 +373,12 @@ STATIC void mptask_init_sflash_filesystem (void) {
|
||||
}
|
||||
|
||||
STATIC void mptask_enter_ap_mode (void) {
|
||||
// enable simplelink in low power mode
|
||||
// append the mac only if it's not the first boot
|
||||
bool append_mac = !PRCMGetSpecialBit(PRCM_FIRST_BOOT_BIT);
|
||||
|
||||
// enable simplelink in ap mode (use the MAC address to make the ssid unique)
|
||||
wlan_sl_enable (ROLE_AP, MICROPY_PORT_WLAN_AP_SSID, strlen(MICROPY_PORT_WLAN_AP_SSID), MICROPY_PORT_WLAN_AP_SECURITY,
|
||||
MICROPY_PORT_WLAN_AP_KEY, strlen(MICROPY_PORT_WLAN_AP_KEY), MICROPY_PORT_WLAN_AP_CHANNEL);
|
||||
MICROPY_PORT_WLAN_AP_KEY, strlen(MICROPY_PORT_WLAN_AP_KEY), MICROPY_PORT_WLAN_AP_CHANNEL, append_mac);
|
||||
}
|
||||
|
||||
STATIC void mptask_create_main_py (void) {
|
||||
|
||||
@@ -48,6 +48,7 @@ Q(readline)
|
||||
Q(input)
|
||||
Q(os)
|
||||
Q(freq)
|
||||
Q(unique_id)
|
||||
Q(repl_info)
|
||||
Q(disable_irq)
|
||||
Q(enable_irq)
|
||||
@@ -58,7 +59,6 @@ Q(elapsed_micros)
|
||||
Q(udelay)
|
||||
Q(flush)
|
||||
Q(FileIO)
|
||||
Q(mkdisk)
|
||||
Q(enable)
|
||||
Q(disable)
|
||||
// Entries for sys.path
|
||||
@@ -70,10 +70,12 @@ Q(/sd/lib)
|
||||
#endif
|
||||
|
||||
// for module weak links
|
||||
Q(struct)
|
||||
Q(binascii)
|
||||
Q(re)
|
||||
Q(json)
|
||||
Q(heapq)
|
||||
Q(hashlib)
|
||||
|
||||
// for os module
|
||||
Q(uos)
|
||||
@@ -93,12 +95,14 @@ Q(chdir)
|
||||
Q(getcwd)
|
||||
Q(listdir)
|
||||
Q(mkdir)
|
||||
Q(rename)
|
||||
Q(remove)
|
||||
Q(rmdir)
|
||||
Q(unlink)
|
||||
Q(sep)
|
||||
Q(stat)
|
||||
Q(urandom)
|
||||
Q(mkfs)
|
||||
|
||||
// for file class
|
||||
Q(seek)
|
||||
@@ -106,12 +110,13 @@ Q(tell)
|
||||
|
||||
// for Pin class
|
||||
Q(Pin)
|
||||
Q(cpu)
|
||||
Q(init)
|
||||
Q(value)
|
||||
Q(low)
|
||||
Q(high)
|
||||
Q(toggle)
|
||||
Q(get_config)
|
||||
Q(info)
|
||||
Q(name)
|
||||
Q(af)
|
||||
Q(mode)
|
||||
@@ -145,21 +150,23 @@ Q(deinit)
|
||||
Q(all)
|
||||
Q(writechar)
|
||||
Q(readchar)
|
||||
Q(sendbreak)
|
||||
Q(readinto)
|
||||
Q(read_buf_len)
|
||||
Q(timeout)
|
||||
Q(timeout_char)
|
||||
Q(repl_uart)
|
||||
Q(flow)
|
||||
Q(FLOW_NONE)
|
||||
Q(FLOW_TX)
|
||||
Q(FLOW_RX)
|
||||
Q(FLOW_TXRX)
|
||||
Q(RTS)
|
||||
Q(CTS)
|
||||
|
||||
// for I2C class
|
||||
Q(I2C)
|
||||
Q(mode)
|
||||
Q(baudrate)
|
||||
Q(addr)
|
||||
Q(data)
|
||||
Q(timeout)
|
||||
Q(memaddr)
|
||||
Q(addr_size)
|
||||
Q(init)
|
||||
@@ -170,10 +177,13 @@ Q(send)
|
||||
Q(recv)
|
||||
Q(mem_read)
|
||||
Q(mem_write)
|
||||
Q(MASTER)
|
||||
|
||||
// for ADC class
|
||||
Q(ADC)
|
||||
Q(read)
|
||||
Q(init)
|
||||
Q(deinit)
|
||||
|
||||
#if MICROPY_HW_HAS_SDCARD
|
||||
// for SD class
|
||||
@@ -208,6 +218,7 @@ Q(getaddrinfo)
|
||||
Q(family)
|
||||
Q(type)
|
||||
Q(send)
|
||||
Q(sendall)
|
||||
Q(sendto)
|
||||
Q(recv)
|
||||
Q(recvfrom)
|
||||
@@ -218,7 +229,10 @@ Q(settimeout)
|
||||
Q(setblocking)
|
||||
Q(setsockopt)
|
||||
Q(close)
|
||||
Q(makefile)
|
||||
Q(protocol)
|
||||
Q(error)
|
||||
Q(timeout)
|
||||
Q(AF_INET)
|
||||
Q(AF_INET6)
|
||||
Q(SOCK_STREAM)
|
||||
@@ -229,16 +243,30 @@ Q(IPPROTO_TCP)
|
||||
Q(IPPROTO_UDP)
|
||||
Q(IPPROTO_RAW)
|
||||
|
||||
// for ssl class
|
||||
Q(ssl)
|
||||
Q(ussl)
|
||||
Q(wrap_socket)
|
||||
Q(sock)
|
||||
Q(keyfile)
|
||||
Q(certfile)
|
||||
Q(server_side)
|
||||
Q(cert_reqs)
|
||||
Q(ca_certs)
|
||||
Q(SSLError)
|
||||
Q(CERT_NONE)
|
||||
Q(CERT_OPTIONAL)
|
||||
Q(CERT_REQUIRED)
|
||||
|
||||
// for network class
|
||||
Q(network)
|
||||
Q(route)
|
||||
Q(start_server)
|
||||
Q(stop_server)
|
||||
Q(server_enabled)
|
||||
Q(server_running)
|
||||
Q(server_login)
|
||||
Q(server_timeout)
|
||||
|
||||
// for WLAN class
|
||||
Q(WLAN)
|
||||
Q(iwconfig)
|
||||
Q(key)
|
||||
Q(security)
|
||||
Q(ssid)
|
||||
@@ -250,29 +278,29 @@ Q(disconnect)
|
||||
Q(channel)
|
||||
Q(rssi)
|
||||
Q(ifconfig)
|
||||
Q(info)
|
||||
Q(connections)
|
||||
#if MICROPY_PORT_WLAN_URN
|
||||
Q(urn)
|
||||
#endif
|
||||
Q(mode)
|
||||
Q(config_ip)
|
||||
Q(ip)
|
||||
Q(subnet)
|
||||
Q(gateway)
|
||||
Q(dns)
|
||||
Q(mac)
|
||||
Q(antenna)
|
||||
Q(STA)
|
||||
Q(AP)
|
||||
Q(P2P)
|
||||
Q(OPEN)
|
||||
Q(WEP)
|
||||
Q(WPA_WPA2)
|
||||
Q(WPA_ENT)
|
||||
Q(WPS_PBC)
|
||||
Q(WPS_PIN)
|
||||
Q(DYNAMIC)
|
||||
Q(STATIC)
|
||||
Q(WPA)
|
||||
Q(WPA2)
|
||||
Q(INTERNAL)
|
||||
Q(EXTERNAL)
|
||||
|
||||
// for WDT class
|
||||
Q(WDT)
|
||||
Q(enable)
|
||||
Q(kick)
|
||||
|
||||
// for HeartBeat class
|
||||
@@ -286,7 +314,7 @@ Q(enable)
|
||||
Q(disable)
|
||||
Q(callback)
|
||||
Q(handler)
|
||||
Q(intmode)
|
||||
Q(mode)
|
||||
Q(value)
|
||||
Q(priority)
|
||||
Q(wakes)
|
||||
@@ -312,15 +340,59 @@ Q(RTC_WAKE)
|
||||
|
||||
// for SPI class
|
||||
Q(SPI)
|
||||
Q(mode)
|
||||
Q(baudrate)
|
||||
Q(bits)
|
||||
Q(submode)
|
||||
Q(cs)
|
||||
Q(polarity)
|
||||
Q(phase)
|
||||
Q(nss)
|
||||
Q(init)
|
||||
Q(deinit)
|
||||
Q(send)
|
||||
Q(recv)
|
||||
Q(send_recv)
|
||||
Q(timeout)
|
||||
Q(MASTER)
|
||||
Q(ACTIVE_LOW)
|
||||
Q(ACTIVE_HIGH)
|
||||
|
||||
// for Timer class
|
||||
Q(Timer)
|
||||
Q(TimerChannel)
|
||||
Q(init)
|
||||
Q(deinit)
|
||||
Q(freq)
|
||||
Q(period)
|
||||
Q(mode)
|
||||
Q(width)
|
||||
Q(channel)
|
||||
Q(polarity)
|
||||
Q(duty_cycle)
|
||||
Q(time)
|
||||
Q(event_count)
|
||||
Q(event_time)
|
||||
Q(A)
|
||||
Q(B)
|
||||
Q(ONE_SHOT)
|
||||
Q(PERIODIC)
|
||||
Q(EDGE_COUNT)
|
||||
Q(EDGE_TIME)
|
||||
Q(PWM)
|
||||
Q(POSITIVE)
|
||||
Q(NEGATIVE)
|
||||
|
||||
// for uhashlib module
|
||||
Q(uhashlib)
|
||||
Q(update)
|
||||
Q(digest)
|
||||
//Q(md5)
|
||||
Q(sha1)
|
||||
Q(sha256)
|
||||
|
||||
// for ubinascii module
|
||||
Q(ubinascii)
|
||||
Q(hexlify)
|
||||
Q(unhexlify)
|
||||
Q(a2b_base64)
|
||||
Q(b2a_base64)
|
||||
|
||||
|
||||
@@ -36,28 +36,35 @@
|
||||
#include "telnet.h"
|
||||
#include "ftp.h"
|
||||
#include "pybwdt.h"
|
||||
#include "modusocket.h"
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DEFINITIONS
|
||||
******************************************************************************/
|
||||
|
||||
#define SERVERS_DEF_USER "micro"
|
||||
#define SERVERS_DEF_PASS "python"
|
||||
#define SERVERS_DEF_USER "micro"
|
||||
#define SERVERS_DEF_PASS "python"
|
||||
#define SERVERS_DEF_TIMEOUT_MS 300000 // 5 minutes
|
||||
#define SERVERS_MIN_TIMEOUT_MS 5000 // 5 seconds
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE TYPES
|
||||
******************************************************************************/
|
||||
typedef struct {
|
||||
volatile bool enabled;
|
||||
volatile bool do_disable;
|
||||
volatile bool do_enable;
|
||||
}servers_Data_t;
|
||||
uint32_t timeout;
|
||||
bool enabled;
|
||||
bool do_disable;
|
||||
bool do_enable;
|
||||
bool do_reset;
|
||||
} servers_data_t;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE DATA
|
||||
******************************************************************************/
|
||||
static servers_Data_t servers_data = {.enabled = false, .do_disable = false, .do_enable = false};
|
||||
static servers_data_t servers_data = {.timeout = SERVERS_DEF_TIMEOUT_MS, .enabled = false, .do_disable = false,
|
||||
.do_enable = false, .do_reset = false};
|
||||
static volatile bool sleep_sockets = false;
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PRIVATE FUNCTIONS
|
||||
@@ -100,6 +107,17 @@ void TASK_Servers (void *pvParameters) {
|
||||
servers_data.do_disable = false;
|
||||
servers_data.enabled = false;
|
||||
}
|
||||
else if (servers_data.do_reset) {
|
||||
// resetting the servers is needed to prevent half-open sockets
|
||||
servers_data.do_reset = false;
|
||||
if (servers_data.enabled) {
|
||||
telnet_reset();
|
||||
ftp_reset();
|
||||
}
|
||||
// and we should also close all user sockets. We do it here
|
||||
// for convinience and to save on code size.
|
||||
modusocket_close_all_user_sockets();
|
||||
}
|
||||
|
||||
if (cycle) {
|
||||
telnet_run();
|
||||
@@ -108,11 +126,19 @@ void TASK_Servers (void *pvParameters) {
|
||||
ftp_run();
|
||||
}
|
||||
|
||||
if (sleep_sockets) {
|
||||
sleep_sockets = false;
|
||||
pybwdt_srv_sleeping(true);
|
||||
modusocket_enter_sleep();
|
||||
pybwdt_srv_sleeping(false);
|
||||
}
|
||||
|
||||
// set the alive flag for the wdt
|
||||
pybwdt_srv_alive();
|
||||
|
||||
// move to the next cycle
|
||||
cycle = cycle ? false : true;
|
||||
HAL_Delay(SERVERS_CYCLE_TIME_MS);
|
||||
// set the alive flag for the wdt
|
||||
pybwdt_srv_alive();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,12 +155,22 @@ void servers_stop (void) {
|
||||
HAL_Delay (SERVERS_CYCLE_TIME_MS * 5);
|
||||
}
|
||||
|
||||
void servers_reset (void) {
|
||||
servers_data.do_reset = true;
|
||||
}
|
||||
|
||||
bool servers_are_enabled (void) {
|
||||
return servers_data.enabled;
|
||||
}
|
||||
|
||||
void server_sleep_sockets (void) {
|
||||
sleep_sockets = true;
|
||||
HAL_Delay (SERVERS_CYCLE_TIME_MS + 1);
|
||||
}
|
||||
|
||||
void servers_close_socket (int16_t *sd) {
|
||||
if (*sd > 0) {
|
||||
modusocket_socket_delete(*sd);
|
||||
sl_Close(*sd);
|
||||
*sd = -1;
|
||||
}
|
||||
@@ -145,6 +181,18 @@ void servers_set_login (char *user, char *pass) {
|
||||
memcpy(servers_pass, pass, SERVERS_USER_PASS_LEN_MAX);
|
||||
}
|
||||
|
||||
bool servers_set_timeout (uint32_t timeout) {
|
||||
if (timeout < SERVERS_MIN_TIMEOUT_MS) {
|
||||
return false;
|
||||
}
|
||||
servers_data.timeout = timeout;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t servers_get_timeout (void) {
|
||||
return servers_data.timeout;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE FUNCTIONS
|
||||
******************************************************************************/
|
||||
|
||||
@@ -56,8 +56,12 @@ extern char servers_pass[];
|
||||
extern void TASK_Servers (void *pvParameters);
|
||||
extern void servers_start (void);
|
||||
extern void servers_stop (void);
|
||||
extern void servers_reset (void);
|
||||
extern bool servers_are_enabled (void);
|
||||
extern void servers_close_socket (int16_t *sd);
|
||||
extern void servers_set_login (char *user, char *pass);
|
||||
extern void server_sleep_sockets (void);
|
||||
extern bool servers_set_timeout (uint32_t timeout);
|
||||
extern uint32_t servers_get_timeout (void);
|
||||
|
||||
#endif /* SERVERSTASK_H_ */
|
||||
|
||||
@@ -31,11 +31,14 @@
|
||||
#include "py/obj.h"
|
||||
#include "telnet.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "modusocket.h"
|
||||
#include "debug.h"
|
||||
#include "mpexception.h"
|
||||
#include "serverstask.h"
|
||||
#include "genhdr/mpversion.h"
|
||||
#include "irq.h"
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PRIVATE CONSTANTS
|
||||
@@ -44,10 +47,9 @@
|
||||
// rxRindex and rxWindex must be uint8_t and TELNET_RX_BUFFER_SIZE == 256
|
||||
#define TELNET_RX_BUFFER_SIZE 256
|
||||
#define TELNET_MAX_CLIENTS 1
|
||||
#define TELNET_TX_RETRIES_MAX 25
|
||||
#define TELNET_TX_RETRIES_MAX 50
|
||||
#define TELNET_WAIT_TIME_MS 5
|
||||
#define TELNET_LOGIN_RETRIES_MAX 3
|
||||
#define TELNET_TIMEOUT_MS 300000 // 5 minutes
|
||||
#define TELNET_CYCLE_TIME_MS (SERVERS_CYCLE_TIME_MS * 2)
|
||||
|
||||
/******************************************************************************
|
||||
@@ -107,8 +109,8 @@ typedef struct {
|
||||
******************************************************************************/
|
||||
static telnet_data_t telnet_data;
|
||||
static const char* telnet_welcome_msg = "Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n";
|
||||
static const char* telnet_request_user = "Login as:";
|
||||
static const char* telnet_request_password = "Password:";
|
||||
static const char* telnet_request_user = "Login as: ";
|
||||
static const char* telnet_request_password = "Password: ";
|
||||
static const char* telnet_invalid_loggin = "\r\nInvalid credentials, try again.\r\n";
|
||||
static const char* telnet_loggin_success = "\r\nLogin succeeded!\r\nType \"help()\" for more information.\r\n";
|
||||
static const uint8_t telnet_options_user[] = // IAC WONT ECHO IAC WONT SUPPRESS_GO_AHEAD IAC WILL LINEMODE
|
||||
@@ -131,7 +133,6 @@ static void telnet_process (void);
|
||||
static int telnet_process_credential (char *credential, _i16 rxLen);
|
||||
static void telnet_parse_input (uint8_t *str, int16_t *len);
|
||||
static bool telnet_send_with_retries (int16_t sd, const void *pBuf, int16_t len);
|
||||
static void telnet_reset (void);
|
||||
static void telnet_reset_buffer (void);
|
||||
|
||||
/******************************************************************************
|
||||
@@ -150,7 +151,7 @@ void telnet_run (void) {
|
||||
telnet_wait_for_enabled();
|
||||
break;
|
||||
case E_TELNET_STE_START:
|
||||
if (telnet_create_socket()) {
|
||||
if (wlan_is_connected() && telnet_create_socket()) {
|
||||
telnet_data.state = E_TELNET_STE_LISTEN;
|
||||
}
|
||||
break;
|
||||
@@ -236,7 +237,7 @@ void telnet_run (void) {
|
||||
}
|
||||
|
||||
if (telnet_data.state >= E_TELNET_STE_CONNECTED) {
|
||||
if (telnet_data.timeout++ > (TELNET_TIMEOUT_MS / TELNET_CYCLE_TIME_MS)) {
|
||||
if (telnet_data.timeout++ > (servers_get_timeout() / TELNET_CYCLE_TIME_MS)) {
|
||||
telnet_reset();
|
||||
}
|
||||
}
|
||||
@@ -292,6 +293,13 @@ void telnet_disable (void) {
|
||||
telnet_data.state = E_TELNET_STE_DISABLED;
|
||||
}
|
||||
|
||||
void telnet_reset (void) {
|
||||
// close the connection and start all over again
|
||||
servers_close_socket(&telnet_data.n_sd);
|
||||
servers_close_socket(&telnet_data.sd);
|
||||
telnet_data.state = E_TELNET_STE_START;
|
||||
}
|
||||
|
||||
bool telnet_is_enabled (void) {
|
||||
return telnet_data.enabled;
|
||||
}
|
||||
@@ -322,21 +330,27 @@ static bool telnet_create_socket (void) {
|
||||
// Open a socket for telnet
|
||||
ASSERT ((telnet_data.sd = sl_Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) > 0);
|
||||
if (telnet_data.sd > 0) {
|
||||
// add the socket to the network administration
|
||||
modusocket_socket_add(telnet_data.sd, false);
|
||||
|
||||
// Enable non-blocking mode
|
||||
nonBlockingOption.NonblockingEnabled = 1;
|
||||
ASSERT (sl_SetSockOpt(telnet_data.sd, SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlockingOption, sizeof(nonBlockingOption)) == SL_SOC_OK);
|
||||
ASSERT ((result = sl_SetSockOpt(telnet_data.sd, SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlockingOption, sizeof(nonBlockingOption))) == SL_SOC_OK);
|
||||
|
||||
// Bind the socket to a port number
|
||||
sServerAddress.sin_family = AF_INET;
|
||||
sServerAddress.sin_addr.s_addr = INADDR_ANY;
|
||||
sServerAddress.sin_port = htons(TELNET_PORT);
|
||||
|
||||
ASSERT (sl_Bind(telnet_data.sd, (const SlSockAddr_t *)&sServerAddress, sizeof(sServerAddress)) == SL_SOC_OK);
|
||||
ASSERT ((result |= sl_Bind(telnet_data.sd, (const SlSockAddr_t *)&sServerAddress, sizeof(sServerAddress))) == SL_SOC_OK);
|
||||
|
||||
// Start listening
|
||||
ASSERT ((result = sl_Listen (telnet_data.sd, TELNET_MAX_CLIENTS)) == SL_SOC_OK);
|
||||
ASSERT ((result |= sl_Listen (telnet_data.sd, TELNET_MAX_CLIENTS)) == SL_SOC_OK);
|
||||
|
||||
return (result == SL_SOC_OK) ? true : false;
|
||||
if (result == SL_SOC_OK) {
|
||||
return true;
|
||||
}
|
||||
servers_close_socket(&telnet_data.sd);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -352,15 +366,18 @@ static void telnet_wait_for_connection (void) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// close the listening socket, we don't need it anymore
|
||||
sl_Close(telnet_data.sd);
|
||||
|
||||
if (telnet_data.n_sd <= 0) {
|
||||
// error
|
||||
telnet_reset();
|
||||
return;
|
||||
}
|
||||
|
||||
// close the listening socket, we don't need it anymore
|
||||
servers_close_socket(&telnet_data.sd);
|
||||
|
||||
// add the new socket to the network administration
|
||||
modusocket_socket_add(telnet_data.n_sd, false);
|
||||
|
||||
// client connected, so go on
|
||||
telnet_data.rxWindex = 0;
|
||||
telnet_data.rxRindex = 0;
|
||||
@@ -476,8 +493,9 @@ static void telnet_parse_input (uint8_t *str, int16_t *len) {
|
||||
|
||||
static bool telnet_send_with_retries (int16_t sd, const void *pBuf, int16_t len) {
|
||||
int32_t retries = 0;
|
||||
// abort sending if we happen to be within interrupt context
|
||||
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0) {
|
||||
uint32_t delay = TELNET_WAIT_TIME_MS;
|
||||
// only if we are not within interrupt context and interrupts are enabled
|
||||
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
|
||||
do {
|
||||
_i16 result = sl_Send(sd, pBuf, len, 0);
|
||||
if (result > 0) {
|
||||
@@ -486,19 +504,13 @@ static bool telnet_send_with_retries (int16_t sd, const void *pBuf, int16_t len)
|
||||
else if (SL_EAGAIN != result) {
|
||||
return false;
|
||||
}
|
||||
HAL_Delay (TELNET_WAIT_TIME_MS);
|
||||
// start with the default delay and increment it on each retry
|
||||
HAL_Delay (delay++);
|
||||
} while (++retries <= TELNET_TX_RETRIES_MAX);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void telnet_reset (void) {
|
||||
// close the connection and start all over again
|
||||
servers_close_socket(&telnet_data.n_sd);
|
||||
servers_close_socket(&telnet_data.sd);
|
||||
telnet_data.state = E_TELNET_STE_START;
|
||||
}
|
||||
|
||||
static void telnet_reset_buffer (void) {
|
||||
// erase any characters present in the current line
|
||||
memset (telnet_data.rxBuffer, '\b', TELNET_RX_BUFFER_SIZE / 2);
|
||||
|
||||
@@ -38,6 +38,7 @@ extern bool telnet_rx_any (void);
|
||||
extern int telnet_rx_char (void);
|
||||
extern void telnet_enable (void);
|
||||
extern void telnet_disable (void);
|
||||
extern void telnet_reset (void);
|
||||
extern bool telnet_is_enabled (void);
|
||||
extern bool telnet_is_active (void);
|
||||
|
||||
|
||||
63
cc3200/tools/smoke.py
Normal file
63
cc3200/tools/smoke.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import pyb
|
||||
import os
|
||||
|
||||
"""
|
||||
Execute it like this:
|
||||
|
||||
python3 run-tests --target wipy --device 192.168.1.1 ../cc3200/tools/smoke.py
|
||||
"""
|
||||
|
||||
pin_map = [2, 1, 23, 24, 11, 12, 13, 14, 15, 16, 17, 22, 28, 10, 9, 8, 7, 6, 30, 31, 3, 0, 4, 5]
|
||||
test_bytes = os.urandom(2048)
|
||||
|
||||
def test_pin_read (type):
|
||||
# enable the pull resistor on all pins, then read the value
|
||||
for p in pin_map:
|
||||
pin = pyb.Pin('GP' + str(p), af= 0, mode=pyb.Pin.IN, type=type)
|
||||
# read the pin value
|
||||
print (pin.value())
|
||||
|
||||
def test_pin_shorts (type):
|
||||
if type == pyb.Pin.STD_PU:
|
||||
type_inverted = pyb.Pin.STD_PD
|
||||
else:
|
||||
type_inverted = pyb.Pin.STD_PU
|
||||
# enable all pulls of the specified type
|
||||
for p in pin_map:
|
||||
pin = pyb.Pin('GP' + str(p), af= 0, mode=pyb.Pin.IN, type=type_inverted)
|
||||
# then change the pull one pin at a time and read its value
|
||||
i = 0
|
||||
while i < len(pin_map):
|
||||
pin = pyb.Pin('GP' + str(pin_map[i]), af= 0, mode=pyb.Pin.IN, type=type)
|
||||
pyb.Pin('GP' + str(pin_map[i - 1]), af= 0, mode=pyb.Pin.IN, type=type_inverted)
|
||||
i += 1
|
||||
# read the pin value
|
||||
print (pin.value())
|
||||
|
||||
test_pin_read(pyb.Pin.STD_PU)
|
||||
test_pin_read(pyb.Pin.STD_PD)
|
||||
test_pin_shorts(pyb.Pin.STD_PU)
|
||||
test_pin_shorts(pyb.Pin.STD_PD)
|
||||
|
||||
# create a test directory
|
||||
os.mkdir('/flash/test')
|
||||
os.chdir('/flash/test')
|
||||
print(os.getcwd())
|
||||
# create a new file
|
||||
f = open ('test.txt', 'w')
|
||||
n_w = f.write (test_bytes)
|
||||
print (n_w == len(test_bytes))
|
||||
f.close()
|
||||
f = open('test.txt', 'r')
|
||||
r = bytes(f.readall(), 'ascii')
|
||||
# check that we can write and read it correctly
|
||||
print (r == test_bytes)
|
||||
f.close()
|
||||
os.remove('test.txt')
|
||||
os.chdir('..')
|
||||
os.rmdir('test')
|
||||
|
||||
ls = os.listdir()
|
||||
print('test' not in ls)
|
||||
print(ls)
|
||||
|
||||
101
cc3200/tools/smoke.py.exp
Normal file
101
cc3200/tools/smoke.py.exp
Normal file
@@ -0,0 +1,101 @@
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
/flash
|
||||
True
|
||||
True
|
||||
True
|
||||
['main.py', 'sys', 'lib', 'cert', 'boot.py']
|
||||
56
cc3200/tools/uniflash.py
Normal file
56
cc3200/tools/uniflash.py
Normal file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Flash the WiPy (format, update service pack and program).
|
||||
|
||||
Example:
|
||||
|
||||
> python uniflash.py -u "C:\ti\uniflash_3.2\uniflashCLI.bat" -c "C:\VirtualBoxShared\GitHub\wipy_uniflash.usf" -p 8 -s "C:\ti\CC31xx_CC32xx_ServicePack_1.0.0.10.0\servicepack_1.0.0.10.0.bin"
|
||||
|
||||
or:
|
||||
|
||||
> python uniflash.py -u "C:\ti\uniflash_3.2\uniflashCLI.bat" -c "C:\VirtualBoxShared\GitHub\launchxl_uniflash.usf" -p 8 -s "C:\ti\CC31xx_CC32xx_ServicePack_1.0.0.10.0\servicepack_1.0.0.10.0.bin"
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import subprocess
|
||||
|
||||
|
||||
def print_exception(e):
|
||||
print ('Exception: {}, on line {}'.format(e, sys.exc_info()[-1].tb_lineno))
|
||||
|
||||
|
||||
def main():
|
||||
cmd_parser = argparse.ArgumentParser(description='Flash the WiPy and optionally run a small test on it.')
|
||||
cmd_parser.add_argument('-u', '--uniflash', default=None, help='the path to the uniflash cli executable')
|
||||
cmd_parser.add_argument('-c', '--config', default=None, help='the path to the uniflash config file')
|
||||
cmd_parser.add_argument('-p', '--port', default=8, help='the com serial port')
|
||||
cmd_parser.add_argument('-s', '--servicepack', default=None, help='the path to the servicepack file')
|
||||
args = cmd_parser.parse_args()
|
||||
|
||||
result = 1
|
||||
com_port = 'com=' + str(args.port)
|
||||
servicepack_path = 'spPath=' + args.servicepack
|
||||
|
||||
try:
|
||||
if args.uniflash == None or args.config == None:
|
||||
raise ValueError('uniflash path and config path are mandatory')
|
||||
if args.servicepack == None:
|
||||
subprocess.check_call([args.uniflash, '-config', args.config, '-setOptions', com_port, '-operations', 'format', 'program'], stderr=subprocess.STDOUT)
|
||||
else:
|
||||
subprocess.check_call([args.uniflash, '-config', args.config, '-setOptions', com_port, servicepack_path, '-operations', 'format', 'servicePackUpdate', 'program'], stderr=subprocess.STDOUT)
|
||||
result = 0
|
||||
except Exception as e:
|
||||
print_exception(e)
|
||||
finally:
|
||||
if result:
|
||||
print ("ERROR: Programming failed!")
|
||||
else:
|
||||
print ("Board programmed OK")
|
||||
sys.exit(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
189
cc3200/tools/update-wipy.py
Normal file
189
cc3200/tools/update-wipy.py
Normal file
@@ -0,0 +1,189 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
The WiPy firmware update script. Transmits the specified firmware file
|
||||
over FTP, and then resets the WiPy and optionally verifies that software
|
||||
was correctly updated.
|
||||
|
||||
Usage:
|
||||
|
||||
./update-wipy.py --file "path_to_mcuimg.bin" --verify
|
||||
|
||||
Or:
|
||||
|
||||
python update-wipy.py --file "path_to_mcuimg.bin"
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import time
|
||||
from ftplib import FTP
|
||||
from telnetlib import Telnet
|
||||
|
||||
|
||||
def print_exception(e):
|
||||
print ('Exception: {}, on line {}'.format(e, sys.exc_info()[-1].tb_lineno))
|
||||
|
||||
|
||||
def ftp_directory_exists(ftpobj, directory_name):
|
||||
filelist = []
|
||||
ftpobj.retrlines('LIST',filelist.append)
|
||||
for f in filelist:
|
||||
if f.split()[-1] == directory_name:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def transfer_file(args):
|
||||
with FTP(args.ip, timeout=20) as ftp:
|
||||
print ('FTP connection established')
|
||||
|
||||
if '230' in ftp.login(args.user, args.password):
|
||||
print ('Login successful')
|
||||
|
||||
if '250' in ftp.cwd('/flash'):
|
||||
if not ftp_directory_exists(ftp, 'sys'):
|
||||
print ('/flash/sys directory does not exist')
|
||||
if not '550' in ftp.mkd('sys'):
|
||||
print ('/flash/sys directory created')
|
||||
else:
|
||||
print ('Error: cannot create /flash/sys directory')
|
||||
return False
|
||||
if '250' in ftp.cwd('sys'):
|
||||
print ("Entered '/flash/sys' directory")
|
||||
with open(args.file, "rb") as fwfile:
|
||||
print ('Firmware image found, initiating transfer...')
|
||||
if '226' in ftp.storbinary("STOR " + 'mcuimg.bin', fwfile, 512):
|
||||
print ('File transfer complete')
|
||||
return True
|
||||
else:
|
||||
print ('Error: file transfer failed')
|
||||
else:
|
||||
print ('Error: cannot enter /flash/sys directory')
|
||||
else:
|
||||
print ('Error: cannot enter /flash directory')
|
||||
else:
|
||||
print ('Error: ftp login failed')
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def reset_board(args):
|
||||
success = False
|
||||
|
||||
try:
|
||||
tn = Telnet(args.ip, timeout=5)
|
||||
print("Connected via Telnet, trying to login now")
|
||||
|
||||
if b'Login as:' in tn.read_until(b"Login as:", timeout=5):
|
||||
tn.write(bytes(args.user, 'ascii') + b"\r\n")
|
||||
|
||||
if b'Password:' in tn.read_until(b"Password:", timeout=5):
|
||||
# needed because of internal implementation details of the WiPy's telnet server
|
||||
time.sleep(0.2)
|
||||
tn.write(bytes(args.password, 'ascii') + b"\r\n")
|
||||
|
||||
if b'Type "help()" for more information.' in tn.read_until(b'Type "help()" for more information.', timeout=5):
|
||||
print("Telnet login succeeded")
|
||||
tn.write(b'\r\x03\x03') # ctrl-C twice: interrupt any running program
|
||||
time.sleep(1)
|
||||
tn.write(b'\r\x02') # ctrl-B: enter friendly REPL
|
||||
if b'Type "help()" for more information.' in tn.read_until(b'Type "help()" for more information.', timeout=5):
|
||||
tn.write(b"import pyb\r\n")
|
||||
tn.write(b"pyb.reset()\r\n")
|
||||
time.sleep(1)
|
||||
print("Reset performed")
|
||||
success = True
|
||||
else:
|
||||
print("Error: cannot enter friendly REPL")
|
||||
else:
|
||||
print("Error: telnet login failed")
|
||||
|
||||
except Exception as e:
|
||||
print_exception(e)
|
||||
finally:
|
||||
try:
|
||||
tn.close()
|
||||
except Exception as e:
|
||||
pass
|
||||
return success
|
||||
|
||||
|
||||
def verify_update(args):
|
||||
success = False
|
||||
firmware_tag = ''
|
||||
|
||||
def find_tag (tag):
|
||||
if tag in firmware_tag:
|
||||
print("Verification passed")
|
||||
return True
|
||||
else:
|
||||
print("Error: verification failed, the git tag doesn't match")
|
||||
return False
|
||||
|
||||
try:
|
||||
# Specify a longer time out value here because the board has just been
|
||||
# reset and the wireless connection might not be fully established yet
|
||||
tn = Telnet(args.ip, timeout=15)
|
||||
print("Connected via telnet again, lets check the git tag")
|
||||
|
||||
firmware_tag = tn.read_until (b'with CC3200')
|
||||
tag_file_path = args.file.rstrip('mcuimg.bin') + 'genhdr/mpversion.h'
|
||||
|
||||
if args.tag is not None:
|
||||
success = find_tag(bytes(args.tag, 'ascii'))
|
||||
else:
|
||||
with open(tag_file_path) as tag_file:
|
||||
for line in tag_file:
|
||||
bline = bytes(line, 'ascii')
|
||||
if b'MICROPY_GIT_HASH' in bline:
|
||||
bline = bline.lstrip(b'#define MICROPY_GIT_HASH ').replace(b'"', b'').replace(b'\r', b'').replace(b'\n', b'')
|
||||
success = find_tag(bline)
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
print_exception(e)
|
||||
finally:
|
||||
try:
|
||||
tn.close()
|
||||
except Exception as e:
|
||||
pass
|
||||
return success
|
||||
|
||||
|
||||
def main():
|
||||
cmd_parser = argparse.ArgumentParser(description='Update the WiPy firmware with the specified image file')
|
||||
cmd_parser.add_argument('-f', '--file', default=None, help='the path of the firmware file')
|
||||
cmd_parser.add_argument('-u', '--user', default='micro', help='the username')
|
||||
cmd_parser.add_argument('-p', '--password', default='python', help='the login password')
|
||||
cmd_parser.add_argument('--ip', default='192.168.1.1', help='the ip address of the WiPy')
|
||||
cmd_parser.add_argument('--verify', action='store_true', help='verify that the update succeeded')
|
||||
cmd_parser.add_argument('-t', '--tag', default=None, help='git tag of the firmware image')
|
||||
args = cmd_parser.parse_args()
|
||||
|
||||
result = 1
|
||||
|
||||
try:
|
||||
if args.file is None:
|
||||
raise ValueError('the image file path must be specified')
|
||||
if transfer_file(args):
|
||||
if reset_board(args):
|
||||
if args.verify:
|
||||
print ('Waiting for the WiFi connection to come up again...')
|
||||
# this time is to allow the system's wireless network card to connect to the
|
||||
# WiPy again. Sometimes it might only take a couple of seconds, but let's
|
||||
# leave 15s to be on the safe side
|
||||
time.sleep(15)
|
||||
if verify_update(args):
|
||||
result = 0
|
||||
else:
|
||||
result = 0
|
||||
|
||||
except Exception as e:
|
||||
print_exception(e)
|
||||
finally:
|
||||
sys.exit(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -35,20 +35,20 @@
|
||||
#include "rom_map.h"
|
||||
#include "prcm.h"
|
||||
#include "shamd5.h"
|
||||
#include "hash.h"
|
||||
#include "simplelink.h"
|
||||
#include "cryptohash.h"
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
DEFINE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
void HASH_Init (void) {
|
||||
__attribute__ ((section (".boot")))
|
||||
void CRYPTOHASH_Init (void) {
|
||||
// Enable the Data Hashing and Transform Engine
|
||||
MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
||||
MAP_PRCMPeripheralReset(PRCM_DTHE);
|
||||
}
|
||||
|
||||
void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
|
||||
void CRYPTOHASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
|
||||
// wait until the context is ready
|
||||
while ((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_CONTEXT_READY) == 0);
|
||||
|
||||
@@ -64,12 +64,12 @@ void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
|
||||
HWREG(SHAMD5_BASE + SHAMD5_O_LENGTH) = blocklen;
|
||||
}
|
||||
|
||||
void HASH_SHAMD5Update (uint8_t *data, uint32_t datalen) {
|
||||
void CRYPTOHASH_SHAMD5Update (uint8_t *data, uint32_t datalen) {
|
||||
// write the data
|
||||
SHAMD5DataWriteMultiple(data, datalen);
|
||||
}
|
||||
|
||||
void HASH_SHAMD5Read (uint8_t *hash) {
|
||||
void CRYPTOHASH_SHAMD5Read (uint8_t *hash) {
|
||||
// wait for the output to be ready
|
||||
while((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_OUTPUT_READY) == 0);
|
||||
// read the result
|
||||
@@ -24,15 +24,15 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef HASH_H_
|
||||
#define HASH_H_
|
||||
#ifndef CRYPTOHASH_H_
|
||||
#define CRYPTOHASH_H_
|
||||
|
||||
/******************************************************************************
|
||||
DECLARE PUBLIC FUNCTIONS
|
||||
******************************************************************************/
|
||||
extern void HASH_Init (void);
|
||||
extern void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen);
|
||||
extern void HASH_SHAMD5Update (uint8_t *data, uint32_t datalen);
|
||||
extern void HASH_SHAMD5Read (uint8_t *hash);
|
||||
extern void CRYPTOHASH_Init (void);
|
||||
extern void CRYPTOHASH_SHAMD5Start (uint32_t algo, uint32_t blocklen);
|
||||
extern void CRYPTOHASH_SHAMD5Update (uint8_t *data, uint32_t datalen);
|
||||
extern void CRYPTOHASH_SHAMD5Read (uint8_t *hash);
|
||||
|
||||
#endif /* HASH_H_ */
|
||||
#endif /* CRYPTOHASH_H_ */
|
||||
@@ -27,14 +27,15 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "inc/hw_types.h"
|
||||
#include "inc/hw_ints.h"
|
||||
#include "inc/hw_memmap.h"
|
||||
#include "rom_map.h"
|
||||
#include "prcm.h"
|
||||
#include "simplelink.h"
|
||||
#include "modnetwork.h"
|
||||
#include "modwlan.h"
|
||||
#include "py/obj.h"
|
||||
#include "random.h"
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
42
docs/conf.py
42
docs/conf.py
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Micro Python documentation build configuration file, created by
|
||||
# MicroPython documentation build configuration file, created by
|
||||
# sphinx-quickstart on Sun Sep 21 11:42:03 2014.
|
||||
#
|
||||
# This file is execfile()d with the current directory set to its
|
||||
@@ -47,10 +47,10 @@ source_suffix = '.rst'
|
||||
#source_encoding = 'utf-8-sig'
|
||||
|
||||
# The master toctree document.
|
||||
master_doc = 'index'
|
||||
#master_doc = 'index'
|
||||
|
||||
# General information about the project.
|
||||
project = 'Micro Python'
|
||||
project = 'MicroPython'
|
||||
copyright = '2014, Damien P. George'
|
||||
|
||||
# The version info for the project you're documenting, acts as replacement for
|
||||
@@ -60,7 +60,7 @@ copyright = '2014, Damien P. George'
|
||||
# The short X.Y version.
|
||||
version = '1.4'
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = '1.4.3'
|
||||
release = '1.4.5'
|
||||
|
||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||
# for a list of supported languages.
|
||||
@@ -134,7 +134,7 @@ else:
|
||||
|
||||
# The name of an image file (relative to this directory) to place at the top
|
||||
# of the sidebar.
|
||||
#html_logo = '../logo/trans-logo.png'
|
||||
#html_logo = '../../logo/trans-logo.png'
|
||||
|
||||
# The name of an image file (within the static path) to use as favicon of the
|
||||
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
|
||||
@@ -213,7 +213,7 @@ latex_elements = {
|
||||
# (source start file, target name, title,
|
||||
# author, documentclass [howto, manual, or own class]).
|
||||
latex_documents = [
|
||||
('index', 'MicroPython.tex', 'Micro Python Documentation',
|
||||
('index', 'MicroPython.tex', 'MicroPython Documentation',
|
||||
'Damien P. George', 'manual'),
|
||||
]
|
||||
|
||||
@@ -243,7 +243,7 @@ latex_documents = [
|
||||
# One entry per manual page. List of tuples
|
||||
# (source start file, name, description, authors, manual section).
|
||||
man_pages = [
|
||||
('index', 'micropython', 'Micro Python Documentation',
|
||||
('index', 'micropython', 'MicroPython Documentation',
|
||||
['Damien P. George'], 1),
|
||||
]
|
||||
|
||||
@@ -257,7 +257,7 @@ man_pages = [
|
||||
# (source start file, target name, title, author,
|
||||
# dir menu entry, description, category)
|
||||
texinfo_documents = [
|
||||
('index', 'MicroPython', 'Micro Python Documentation',
|
||||
('index', 'MicroPython', 'MicroPython Documentation',
|
||||
'Damien P. George', 'MicroPython', 'One line description of project.',
|
||||
'Miscellaneous'),
|
||||
]
|
||||
@@ -277,3 +277,29 @@ texinfo_documents = [
|
||||
|
||||
# Example configuration for intersphinx: refer to the Python standard library.
|
||||
intersphinx_mapping = {'http://docs.python.org/': None}
|
||||
|
||||
|
||||
# Work out the port to generate the docs for
|
||||
from collections import OrderedDict
|
||||
micropy_port = os.getenv('MICROPY_PORT') or 'pyboard'
|
||||
tags.add('port_' + micropy_port)
|
||||
ports = OrderedDict((
|
||||
("unix", "unix"),
|
||||
("pyboard", "the pyboard"),
|
||||
("wipy", "the WiPy"),
|
||||
("esp8266", "esp8266"),
|
||||
))
|
||||
|
||||
# The members of the html_context dict are available inside topindex.html
|
||||
url_prefix = os.getenv('MICROPY_URL_PREFIX') or '/'
|
||||
html_context = {
|
||||
'port':micropy_port,
|
||||
'port_name':ports[micropy_port],
|
||||
'all_ports':[(n, url_prefix + p) for p, n in ports.items()],
|
||||
}
|
||||
|
||||
# Append the other ports' specific folders/files to the exclude pattern
|
||||
exclude_patterns.extend([port + '*' for port in ports if port != micropy_port])
|
||||
|
||||
# Specify a custom master document based on the port name
|
||||
master_doc = micropy_port + '_' + 'index'
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
Micro Python documentation contents
|
||||
===================================
|
||||
|
||||
.. toctree::
|
||||
|
||||
quickref.rst
|
||||
general.rst
|
||||
tutorial/index.rst
|
||||
library/index.rst
|
||||
hardware/index.rst
|
||||
license.rst
|
||||
8
docs/esp8266_contents.rst
Normal file
8
docs/esp8266_contents.rst
Normal file
@@ -0,0 +1,8 @@
|
||||
MicroPython documentation contents
|
||||
==================================
|
||||
|
||||
.. toctree::
|
||||
|
||||
library/index.rst
|
||||
reference/index.rst
|
||||
license.rst
|
||||
15
docs/esp8266_index.rst
Normal file
15
docs/esp8266_index.rst
Normal file
@@ -0,0 +1,15 @@
|
||||
MicroPython documentation and references
|
||||
========================================
|
||||
|
||||
.. toctree::
|
||||
|
||||
library/index.rst
|
||||
license.rst
|
||||
esp8266_contents.rst
|
||||
|
||||
Indices and tables
|
||||
==================
|
||||
|
||||
* :ref:`genindex`
|
||||
* :ref:`modindex`
|
||||
* :ref:`search`
|
||||
@@ -1,19 +0,0 @@
|
||||
Micro Python documentation and references
|
||||
=========================================
|
||||
|
||||
.. toctree::
|
||||
|
||||
quickref.rst
|
||||
general.rst
|
||||
tutorial/index.rst
|
||||
library/index.rst
|
||||
hardware/index.rst
|
||||
license.rst
|
||||
contents.rst
|
||||
|
||||
Indices and tables
|
||||
==================
|
||||
|
||||
* :ref:`genindex`
|
||||
* :ref:`modindex`
|
||||
* :ref:`search`
|
||||
40
docs/library/esp.rst
Normal file
40
docs/library/esp.rst
Normal file
@@ -0,0 +1,40 @@
|
||||
:mod:`esp` --- functions related to the ESP8266
|
||||
===============================================
|
||||
|
||||
.. module:: esp
|
||||
:synopsis: functions related to the ESP8266
|
||||
|
||||
The ``esp`` module contains specific functions related to the ESP8266 module.
|
||||
|
||||
|
||||
Functions
|
||||
---------
|
||||
|
||||
.. function:: status()
|
||||
|
||||
Return the current status of the wireless connection.
|
||||
|
||||
The possible statuses are defined as constants:
|
||||
|
||||
* ``STAT_IDLE`` -- no connection and no activity,
|
||||
* ``STAT_CONNECTING`` -- connecting in progress,
|
||||
* ``STAT_WRONG_PASSWORD`` -- failed due to incorrect password,
|
||||
* ``STAT_NO_AP_FOUND`` -- failed because no access point replied,
|
||||
* ``STAT_CONNECT_FAIL`` -- failed due to other problems,
|
||||
* ``STAT_GOT_IP`` -- connection susccessful.
|
||||
|
||||
.. function:: getaddrinfo((hostname, port, lambda))
|
||||
|
||||
Initiate resolving of the given hostname.
|
||||
|
||||
When the hostname is resolved, the provided ``lambda`` callback will be
|
||||
called with two arguments, first being the hostname being resolved,
|
||||
second a tuple with information about that hostname.
|
||||
|
||||
Classes
|
||||
-------
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
esp.socket.rst
|
||||
82
docs/library/esp.socket.rst
Normal file
82
docs/library/esp.socket.rst
Normal file
@@ -0,0 +1,82 @@
|
||||
class socket -- network socket
|
||||
==============================
|
||||
|
||||
``socket`` is an object that represents a network socket. Example usage::
|
||||
|
||||
socket = esp.socket()
|
||||
socket.onrecv(print)
|
||||
socket.connect(('207.58.139.247', 80))
|
||||
socket.send('GET /testwifi/index.html HTTP/1.0\r\n\r\n')
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: esp.socket()
|
||||
|
||||
Create and return a socket object.
|
||||
|
||||
|
||||
TCP Methods
|
||||
-----------
|
||||
|
||||
.. method:: socket.connect(addr)
|
||||
|
||||
Connect to the adress and port specified in the ``addr`` tuple.
|
||||
|
||||
.. method:: socket.close()
|
||||
|
||||
Close the connection.
|
||||
|
||||
.. method:: socket.accept()
|
||||
|
||||
Accept a single connection from the connection queue.
|
||||
|
||||
.. method:: socket.listen(backlog)
|
||||
|
||||
Start listening for incoming connections.
|
||||
|
||||
Note: Only one socket can be listening for connections at a time.
|
||||
|
||||
.. method:: socket.bind(addr)
|
||||
|
||||
Bind the socket to the address and port specified by the ``addr`` tuple.
|
||||
|
||||
.. method:: socket.send(buf)
|
||||
|
||||
Send the bytes from ``buf``.
|
||||
|
||||
.. method:: socket.recv()
|
||||
|
||||
Receive and return bytes from the socket.
|
||||
|
||||
|
||||
UDP Methods
|
||||
-----------
|
||||
|
||||
.. method:: socket.sendto(data, addr)
|
||||
|
||||
Placeholder for UDP support, not implemented yet.
|
||||
|
||||
.. method:: socket.recvfrom(addr)
|
||||
|
||||
Placeholder for UDP support, not implemented yet.
|
||||
|
||||
|
||||
Callback Setter Methods
|
||||
-----------------------
|
||||
|
||||
.. method:: onconnect(lambda)::
|
||||
|
||||
When connection is established, call the callback ``lambda``.
|
||||
|
||||
.. method:: onrecv(lambda)::
|
||||
|
||||
When data is received, call the callback ``lambda``.
|
||||
|
||||
.. method:: onsent(lamda)::
|
||||
|
||||
What data is finished sending, call the callback ``lambda``.
|
||||
|
||||
.. method:: ondisconnect(lambda)::
|
||||
|
||||
Call the callback ``lambda`` when the connection is closed.
|
||||
@@ -1,7 +1,7 @@
|
||||
Micro Python libraries
|
||||
======================
|
||||
MicroPython libraries
|
||||
=====================
|
||||
|
||||
Functionality specific to the Micro Python implementation is available in
|
||||
Functionality specific to the MicroPython implementation is available in
|
||||
the following library.
|
||||
|
||||
.. toctree::
|
||||
@@ -12,28 +12,41 @@ the following library.
|
||||
Python standard libraries
|
||||
-------------------------
|
||||
|
||||
The following standard Python libraries are built in to Micro Python.
|
||||
The following standard Python libraries are built in to MicroPython.
|
||||
|
||||
For additional libraries, please download them from the `micropython-lib repository
|
||||
<https://github.com/micropython/micropython-lib>`_.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
.. only:: port_pyboard
|
||||
|
||||
cmath.rst
|
||||
gc.rst
|
||||
math.rst
|
||||
os.rst
|
||||
select.rst
|
||||
struct.rst
|
||||
sys.rst
|
||||
time.rst
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
cmath.rst
|
||||
gc.rst
|
||||
math.rst
|
||||
os.rst
|
||||
select.rst
|
||||
struct.rst
|
||||
sys.rst
|
||||
time.rst
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
gc.rst
|
||||
os.rst
|
||||
select.rst
|
||||
sys.rst
|
||||
time.rst
|
||||
|
||||
Python micro-libraries
|
||||
----------------------
|
||||
|
||||
The following standard Python libraries have been "micro-ified" to fit in with
|
||||
the philosophy of Micro Python. They provide the core functionality of that
|
||||
the philosophy of MicroPython. They provide the core functionality of that
|
||||
module and are intended to be a drop-in replacement for the standard Python
|
||||
library.
|
||||
|
||||
@@ -43,25 +56,69 @@ For example, ``import json`` will first search for a file ``json.py`` or
|
||||
directory ``json`` and load that package if it is found. If nothing is found,
|
||||
it will fallback to loading the built-in ``ujson`` module.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
.. only:: port_pyboard
|
||||
|
||||
ubinascii.rst
|
||||
uctypes.rst
|
||||
uhashlib.rst
|
||||
uheapq.rst
|
||||
ujson.rst
|
||||
ure.rst
|
||||
usocket.rst
|
||||
uzlib.rst
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
ubinascii.rst
|
||||
uctypes.rst
|
||||
uhashlib.rst
|
||||
uheapq.rst
|
||||
ujson.rst
|
||||
ure.rst
|
||||
usocket.rst
|
||||
uzlib.rst
|
||||
|
||||
Libraries specific to the pyboard
|
||||
---------------------------------
|
||||
.. only:: port_pyboard
|
||||
|
||||
The following libraries are specific to the pyboard.
|
||||
Libraries specific to the pyboard
|
||||
---------------------------------
|
||||
|
||||
The following libraries are specific to the pyboard.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
pyb.rst
|
||||
network.rst
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
.. only:: port_wipy
|
||||
|
||||
pyb.rst
|
||||
network.rst
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
ubinascii.rst
|
||||
uhashlib.rst
|
||||
uheapq.rst
|
||||
ujson.rst
|
||||
ure.rst
|
||||
usocket.rst
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
Libraries specific to the WiPy
|
||||
---------------------------------
|
||||
|
||||
The following libraries are specific to the WiPy.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
pyb.rst
|
||||
network.rst
|
||||
|
||||
|
||||
.. only:: port_esp8266
|
||||
|
||||
Libraries specific to the ESP8266
|
||||
---------------------------------
|
||||
|
||||
The following libraries are specific to the ESP8266.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
pyb.rst
|
||||
esp.rst
|
||||
network.rst
|
||||
|
||||
@@ -1,29 +1,31 @@
|
||||
:mod:`micropython` -- access and control Micro Python internals
|
||||
===============================================================
|
||||
:mod:`micropython` -- access and control MicroPython internals
|
||||
==============================================================
|
||||
|
||||
.. module:: micropython
|
||||
:synopsis: access and control Micro Python internals
|
||||
:synopsis: access and control MicroPython internals
|
||||
|
||||
Functions
|
||||
---------
|
||||
|
||||
.. function:: mem_info([verbose])
|
||||
.. only:: port_pyboard or port_unix
|
||||
|
||||
Print information about currently used memory. If the ``verbose`` argument
|
||||
is given then extra information is printed.
|
||||
|
||||
The information that is printed is implementation dependent, but currently
|
||||
includes the amount of stack and heap used. In verbose mode it prints out
|
||||
the entire heap indicating which blocks are used and which are free.
|
||||
|
||||
.. function:: qstr_info([verbose])
|
||||
|
||||
Print information about currently interned strings. If the ``verbose``
|
||||
argument is given then extra information is printed.
|
||||
|
||||
The information that is printed is implementation dependent, but currently
|
||||
includes the number of interned strings and the amount of RAM they use. In
|
||||
verbose mode it prints out the names of all RAM-interned strings.
|
||||
.. function:: mem_info([verbose])
|
||||
|
||||
Print information about currently used memory. If the ``verbose`` argument
|
||||
is given then extra information is printed.
|
||||
|
||||
The information that is printed is implementation dependent, but currently
|
||||
includes the amount of stack and heap used. In verbose mode it prints out
|
||||
the entire heap indicating which blocks are used and which are free.
|
||||
|
||||
.. function:: qstr_info([verbose])
|
||||
|
||||
Print information about currently interned strings. If the ``verbose``
|
||||
argument is given then extra information is printed.
|
||||
|
||||
The information that is printed is implementation dependent, but currently
|
||||
includes the number of interned strings and the amount of RAM they use. In
|
||||
verbose mode it prints out the names of all RAM-interned strings.
|
||||
|
||||
.. function:: alloc_emergency_exception_buf(size)
|
||||
|
||||
|
||||
@@ -27,151 +27,343 @@ For example::
|
||||
data = s.recv(1000)
|
||||
s.close()
|
||||
|
||||
class CC3K
|
||||
.. only:: port_pyboard
|
||||
|
||||
class CC3K
|
||||
==========
|
||||
|
||||
This class provides a driver for CC3000 wifi modules. Example usage::
|
||||
|
||||
import network
|
||||
nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3)
|
||||
nic.connect('your-ssid', 'your-password')
|
||||
while not nic.isconnected():
|
||||
pyb.delay(50)
|
||||
print(nic.ifconfig())
|
||||
|
||||
# now use socket as usual
|
||||
...
|
||||
|
||||
For this example to work the CC3000 module must have the following connections:
|
||||
|
||||
- MOSI connected to Y8
|
||||
- MISO connected to Y7
|
||||
- CLK connected to Y6
|
||||
- CS connected to Y5
|
||||
- VBEN connected to Y4
|
||||
- IRQ connected to Y3
|
||||
|
||||
It is possible to use other SPI busses and other pins for CS, VBEN and IRQ.
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: CC3K(spi, pin_cs, pin_en, pin_irq)
|
||||
|
||||
Create a CC3K driver object, initialise the CC3000 module using the given SPI bus
|
||||
and pins, and return the CC3K object.
|
||||
|
||||
Arguments are:
|
||||
|
||||
- ``spi`` is an :ref:`SPI object <pyb.SPI>` which is the SPI bus that the CC3000 is
|
||||
connected to (the MOSI, MISO and CLK pins).
|
||||
- ``pin_cs`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 CS pin.
|
||||
- ``pin_en`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 VBEN pin.
|
||||
- ``pin_irq`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 IRQ pin.
|
||||
|
||||
All of these objects will be initialised by the driver, so there is no need to
|
||||
initialise them yourself. For example, you can use::
|
||||
|
||||
nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3)
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: cc3k.connect(ssid, key=None, \*, security=WPA2, bssid=None)
|
||||
|
||||
Connect to a wifi access point using the given SSID, and other security
|
||||
parameters.
|
||||
|
||||
.. method:: cc3k.disconnect()
|
||||
|
||||
Disconnect from the wifi access point.
|
||||
|
||||
.. method:: cc3k.isconnected()
|
||||
|
||||
Returns True if connected to a wifi access point and has a valid IP address,
|
||||
False otherwise.
|
||||
|
||||
.. method:: cc3k.ifconfig()
|
||||
|
||||
Returns a 7-tuple with (ip, subnet mask, gateway, DNS server, DHCP server,
|
||||
MAC address, SSID).
|
||||
|
||||
.. method:: cc3k.patch_version()
|
||||
|
||||
Return the version of the patch program (firmware) on the CC3000.
|
||||
|
||||
.. method:: cc3k.patch_program('pgm')
|
||||
|
||||
Upload the current firmware to the CC3000. You must pass 'pgm' as the first
|
||||
argument in order for the upload to proceed.
|
||||
|
||||
Constants
|
||||
---------
|
||||
|
||||
.. data:: CC3K.WEP
|
||||
.. data:: CC3K.WPA
|
||||
.. data:: CC3K.WPA2
|
||||
|
||||
security type to use
|
||||
|
||||
class WIZNET5K
|
||||
==============
|
||||
|
||||
This class allows you to control WIZnet5x00 Ethernet adaptors based on
|
||||
the W5200 and W5500 chipsets (only W5200 tested).
|
||||
|
||||
Example usage::
|
||||
|
||||
import network
|
||||
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
|
||||
print(nic.ifconfig())
|
||||
|
||||
# now use socket as usual
|
||||
...
|
||||
|
||||
For this example to work the WIZnet5x00 module must have the following connections:
|
||||
|
||||
- MOSI connected to X8
|
||||
- MISO connected to X7
|
||||
- SCLK connected to X6
|
||||
- nSS connected to X5
|
||||
- nRESET connected to X4
|
||||
|
||||
It is possible to use other SPI busses and other pins for nSS and nRESET.
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: WIZNET5K(spi, pin_cs, pin_rst)
|
||||
|
||||
Create a WIZNET5K driver object, initialise the WIZnet5x00 module using the given
|
||||
SPI bus and pins, and return the WIZNET5K object.
|
||||
|
||||
Arguments are:
|
||||
|
||||
- ``spi`` is an :ref:`SPI object <pyb.SPI>` which is the SPI bus that the WIZnet5x00 is
|
||||
connected to (the MOSI, MISO and SCLK pins).
|
||||
- ``pin_cs`` is a :ref:`Pin object <pyb.Pin>` which is connected to the WIZnet5x00 nSS pin.
|
||||
- ``pin_rst`` is a :ref:`Pin object <pyb.Pin>` which is connected to the WIZnet5x00 nRESET pin.
|
||||
|
||||
All of these objects will be initialised by the driver, so there is no need to
|
||||
initialise them yourself. For example, you can use::
|
||||
|
||||
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: wiznet5k.ifconfig([(ip, subnet, gateway, dns)])
|
||||
|
||||
Get/set IP address, subnet mask, gateway and DNS.
|
||||
|
||||
When called with no arguments, this method returns a 4-tuple with the above information.
|
||||
|
||||
To set the above values, pass a 4-tuple with the required information. For example::
|
||||
|
||||
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
|
||||
|
||||
.. method:: wiznet5k.regs()
|
||||
|
||||
Dump the WIZnet5x00 registers. Useful for debugging.
|
||||
|
||||
class WLAN
|
||||
==========
|
||||
|
||||
This class provides a driver for CC3000 wifi modules. Example usage::
|
||||
.. _network.WLAN:
|
||||
|
||||
import network
|
||||
nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3)
|
||||
nic.connect('your-ssid', 'your-password')
|
||||
while not nic.isconnected():
|
||||
pyb.delay(50)
|
||||
print(nic.ifconfig())
|
||||
.. only:: port_esp8266
|
||||
|
||||
# now use socket as usual
|
||||
...
|
||||
This class provides a driver for WiFi network processor in the ESP8266. Example usage::
|
||||
|
||||
For this example to work the CC3000 module must have the following connections:
|
||||
import network
|
||||
# setup as a station
|
||||
nic = network.WLAN()
|
||||
nic.connect('your-ssid', 'your-password')
|
||||
# now use socket as usual
|
||||
|
||||
- MOSI connected to Y8
|
||||
- MISO connected to Y7
|
||||
- CLK connected to Y6
|
||||
- CS connected to Y5
|
||||
- VBEN connected to Y4
|
||||
- IRQ connected to Y3
|
||||
Constructors
|
||||
------------
|
||||
.. class:: WLAN()
|
||||
|
||||
It is possible to use other SPI busses and other pins for CS, VBEN and IRQ.
|
||||
Create a WLAN driver object.
|
||||
|
||||
Constructors
|
||||
------------
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. class:: CC3K(spi, pin_cs, pin_en, pin_irq)
|
||||
.. method:: wlan.connect(ssid, password)
|
||||
|
||||
Create a CC3K driver object, initialise the CC3000 module using the given SPI bus
|
||||
and pins, and return the CC3K object.
|
||||
Connect to the specified wireless network, using the specified password.
|
||||
|
||||
Arguments are:
|
||||
.. method:: wlan.disconnect()
|
||||
|
||||
- ``spi`` is an :ref:`SPI object <pyb.SPI>` which is the SPI bus that the CC3000 is
|
||||
connected to (the MOSI, MISO and CLK pins).
|
||||
- ``pin_cs`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 CS pin.
|
||||
- ``pin_en`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 VBEN pin.
|
||||
- ``pin_irq`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 IRQ pin.
|
||||
Disconnect from the currently connected wireless network.
|
||||
|
||||
All of these objects will be initialised by the driver, so there is no need to
|
||||
initialise them yourself. For example, you can use::
|
||||
.. method:: wlan.scan(cb)
|
||||
|
||||
nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3)
|
||||
Initiate scanning for the available wireless networks.
|
||||
|
||||
Methods
|
||||
-------
|
||||
Scanning is only possible if the radio is in station or station+AP mode; if
|
||||
called while in AP only mode, an OSError exception will be raised.
|
||||
|
||||
.. method:: cc3k.connect(ssid, key=None, \*, security=WPA2, bssid=None)
|
||||
Once the scanning is complete, the provided callback function ``cb`` will
|
||||
be called once for each network found, and passed a tuple with information
|
||||
about that network:
|
||||
|
||||
Connect to a wifi access point using the given SSID, and other security
|
||||
parameters.
|
||||
(ssid, bssid, channel, RSSI, authmode, hidden)
|
||||
|
||||
.. method:: cc3k.disconnect()
|
||||
There are five values for authmode:
|
||||
|
||||
Disconnect from the wifi access point.
|
||||
* 0 -- open
|
||||
* 1 -- WEP
|
||||
* 2 -- WPA-PSK
|
||||
* 3 -- WPA2-PSK
|
||||
* 4 -- WPA/WPA2-PSK
|
||||
|
||||
.. method:: cc3k.isconnected()
|
||||
and two for hidden:
|
||||
|
||||
Returns True if connected to a wifi access point and has a valid IP address,
|
||||
False otherwise.
|
||||
* 0 -- visible
|
||||
* 1 -- hidden
|
||||
|
||||
.. method:: cc3k.ifconfig()
|
||||
.. only:: port_wipy
|
||||
|
||||
Returns a 7-tuple with (ip, subnet mask, gateway, DNS server, DHCP server,
|
||||
MAC address, SSID).
|
||||
This class provides a driver for WiFi network processor in the WiPy. Example usage::
|
||||
|
||||
import network
|
||||
# setup as a station
|
||||
nic = network.WLAN(mode=WLAN.STA)
|
||||
nic.connect('your-ssid', security=WLAN.WPA2, key='your-key')
|
||||
while not nic.isconnected():
|
||||
pyb.delay(50)
|
||||
print(nic.ifconfig())
|
||||
|
||||
.. method:: cc3k.patch_version()
|
||||
# now use socket as usual
|
||||
...
|
||||
|
||||
Return the version of the patch program (firmware) on the CC3000.
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: WLAN(..)
|
||||
|
||||
.. method:: cc3k.patch_program('pgm')
|
||||
Create a WLAN object, and optionally configure it. See ``iwconfig`` for params of configuration.
|
||||
|
||||
Upload the current firmware to the CC3000. You must pass 'pgm' as the first
|
||||
argument in order for the upload to proceed.
|
||||
Methods
|
||||
-------
|
||||
|
||||
Constants
|
||||
---------
|
||||
.. method:: iwconfig(\*, mode, ssid, security, key, channel, antenna)
|
||||
|
||||
Set or get the WiFi network processor configuration.
|
||||
|
||||
Arguments are:
|
||||
|
||||
- ``mode`` can be either ``WLAN.STA`` or ``WLAN.AP``.
|
||||
- ``ssid`` is a string with the ssid name. Only needed when mode is ``WLAN.AP``.
|
||||
- ``security`` can be ``WLAN.OPEN``, ``WLAN.WEP``, ``WLAN.WPA`` or ``WLAN.WPA2``.
|
||||
Only needed when mode is ``WLAN.AP``.
|
||||
- ``key`` is a string with the network password. Not needed when mode is ``WLAN.STA``
|
||||
or security is ``WLAN.OPEN``. If ``security`` is ``WLAN.WEP`` the key must be a
|
||||
string representing hexadecimal values (e.g. 'ABC1DE45BF').
|
||||
- ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``.
|
||||
- ``antenna`` selects between the internal and the external antenna. Can be either
|
||||
``WLAN.INTERNAL`` or ``WLAN.EXTERNAL``.
|
||||
|
||||
For example, you can do::
|
||||
|
||||
.. data:: CC3K.WEP
|
||||
.. data:: CC3K.WPA
|
||||
.. data:: CC3K.WPA2
|
||||
# create and configure as an access point
|
||||
nic.iwconfig(mode=WLAN.AP, ssid='wipy-wlan', security=WLAN.WPA2, key='www.wipy.io', channel=7, antenna=WLAN.INTERNAL)
|
||||
|
||||
security type to use
|
||||
or::
|
||||
|
||||
class WIZNET5K
|
||||
==============
|
||||
# configure as an station
|
||||
nic.iwconfig(mode=WLAN.STA)
|
||||
|
||||
This class allows you to control WIZnet5x00 Ethernet adaptors based on
|
||||
the W5200 and W5500 chipsets (only W5200 tested).
|
||||
With no arguments given, the current configuration is returned as a namedtuple that looks like this:
|
||||
``(mode=2, ssid='wipy-wlan', security=2, key='www.wipy.io', channel=5, antenna=0)``
|
||||
|
||||
Example usage::
|
||||
.. method:: wlan.connect(ssid, \*, security=WLAN.OPEN, key=None, bssid=None, timeout=5000)
|
||||
|
||||
Connect to a wifi access point using the given SSID, and other security
|
||||
parameters.
|
||||
|
||||
- ``key`` is always a string, but if ``security`` is ``WLAN.WEP`` the key must be a string
|
||||
representing hexadecimal values (e.g. 'ABC1DE45BF').
|
||||
- ``bssid`` is the MAC address of the AP to connect to. Useful when there are several APs
|
||||
with the same ssid.
|
||||
- ``timeout`` is the maximum time in milliseconds to wait for the connection to succeed.
|
||||
|
||||
.. method:: wlan.scan()
|
||||
|
||||
Performs a network scan and returns a list of named tuples with (ssid, bssid, security, channel, rssi).
|
||||
Note that channel is always ``None`` since this info is not provided by the WiPy.
|
||||
|
||||
.. method:: wlan.disconnect()
|
||||
|
||||
Disconnect from the wifi access point.
|
||||
|
||||
.. method:: wlan.isconnected()
|
||||
|
||||
In case of STA mode, returns ``True`` if connected to a wifi access point and has a valid IP address.
|
||||
In AP mode returns ``True`` when a station is connected. Returns ``False`` otherwise.
|
||||
|
||||
.. method:: wlan.ifconfig(['dhcp' or configtuple])
|
||||
|
||||
With no parameters given eturns a 4-tuple of ``(ip, subnet mask, gateway, DNS server)``.
|
||||
|
||||
if ``'dhcp'`` is passed as a parameter then the DHCP client is enabled and the IP params
|
||||
are negotiated with the AP.
|
||||
|
||||
if the 4-tuple config is given then a static IP is configured. For example::
|
||||
|
||||
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
|
||||
|
||||
.. method:: wlan.mac()
|
||||
|
||||
Returns a 6-byte long bytes object with the MAC address.
|
||||
|
||||
import network
|
||||
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
|
||||
print(nic.ifconfig())
|
||||
.. method:: wlan.connections()
|
||||
|
||||
Returns a list of the devices currently connected. Each item in the list is a
|
||||
tuple of ``(ssid, mac)``.
|
||||
|
||||
# now use socket as usual
|
||||
...
|
||||
.. method:: wlan.callback(wakes)
|
||||
|
||||
For this example to work the WIZnet5x00 module must have the following connections:
|
||||
Create a callback to be triggered when a WLAN event occurs during ``pyb.Sleep.SUSPENDED``
|
||||
mode. Events are triggered by socket activity or by WLAN connection/disconnection.
|
||||
|
||||
- MOSI connected to X8
|
||||
- MISO connected to X7
|
||||
- SCLK connected to X6
|
||||
- nSS connected to X5
|
||||
- nRESET connected to X4
|
||||
- ``wakes`` can only be ``pyb.Sleep.SUSPENDED``.
|
||||
|
||||
It is possible to use other SPI busses and other pins for nSS and nRESET.
|
||||
Returns a callback object.
|
||||
|
||||
Constructors
|
||||
------------
|
||||
Constants
|
||||
---------
|
||||
|
||||
.. data:: WLAN.STA
|
||||
|
||||
.. class:: WIZNET5K(spi, pin_cs, pin_rst)
|
||||
WiFi station mode
|
||||
|
||||
Create a WIZNET5K driver object, initialise the WIZnet5x00 module using the given
|
||||
SPI bus and pins, and return the WIZNET5K object.
|
||||
.. data:: WLAN.AP
|
||||
|
||||
Arguments are:
|
||||
WiFi access point mode
|
||||
|
||||
- ``spi`` is an :ref:`SPI object <pyb.SPI>` which is the SPI bus that the WIZnet5x00 is
|
||||
connected to (the MOSI, MISO and SCLK pins).
|
||||
- ``pin_cs`` is a :ref:`Pin object <pyb.Pin>` which is connected to the WIZnet5x00 nSS pin.
|
||||
- ``pin_rst`` is a :ref:`Pin object <pyb.Pin>` which is connected to the WIZnet5x00 nRESET pin.
|
||||
.. data:: WLAN.OPEN
|
||||
.. data:: WLAN.WEP
|
||||
.. data:: WLAN.WPA
|
||||
.. data:: WLAN.WPA2
|
||||
|
||||
All of these objects will be initialised by the driver, so there is no need to
|
||||
initialise them yourself. For example, you can use::
|
||||
selects the network security
|
||||
|
||||
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
|
||||
.. data:: WLAN.INTERNAL
|
||||
.. data:: WLAN.EXTERNAL
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: wiznet5k.ifconfig([(ip, subnet, gateway, dns)])
|
||||
|
||||
Get/set IP address, subnet mask, gateway and DNS.
|
||||
|
||||
When called with no arguments, this method returns a 4-tuple with the above information.
|
||||
|
||||
To set the above values, pass a 4-tuple with the required information. For example::
|
||||
|
||||
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
|
||||
|
||||
.. method:: wiznet5k.regs()
|
||||
|
||||
Dump the WIZnet5x00 registers. Useful for debugging.
|
||||
selects the antenna type
|
||||
|
||||
@@ -16,8 +16,14 @@ available physical drives are accessible from here. They are currently:
|
||||
|
||||
``/sd`` -- the SD card (if it exists)
|
||||
|
||||
On boot up, the current directory is ``/flash`` if no SD card is inserted,
|
||||
otherwise it is ``/sd``.
|
||||
.. only:: port_pyboard
|
||||
|
||||
On boot up, the current directory is ``/flash`` if no SD card is inserted,
|
||||
otherwise it is ``/sd``.
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
On boot up, the current directory is ``/flash``.
|
||||
|
||||
Functions
|
||||
---------
|
||||
@@ -63,6 +69,12 @@ Functions
|
||||
Return a bytes object with n random bytes, generated by the hardware
|
||||
random number generator.
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. function:: mkfs(drive)
|
||||
|
||||
Formats the specified drive, must be either ``/flash`` or ``/sd``.
|
||||
|
||||
Constants
|
||||
---------
|
||||
|
||||
|
||||
@@ -3,28 +3,57 @@
|
||||
class ADC -- analog to digital conversion: read analog values on a pin
|
||||
======================================================================
|
||||
|
||||
Usage::
|
||||
.. only:: port_pyboard
|
||||
|
||||
import pyb
|
||||
Usage::
|
||||
|
||||
import pyb
|
||||
|
||||
adc = pyb.ADC(pin) # create an analog object from a pin
|
||||
val = adc.read() # read an analog value
|
||||
|
||||
adc = pyb.ADCAll(resolution) # creale an ADCAll object
|
||||
val = adc.read_channel(channel) # read the given channel
|
||||
val = adc.read_core_temp() # read MCU temperature
|
||||
val = adc.read_core_vbat() # read MCU VBAT
|
||||
val = adc.read_core_vref() # read MCU VREF
|
||||
|
||||
adc = pyb.ADC(pin) # create an analog object from a pin
|
||||
val = adc.read() # read an analog value
|
||||
.. only:: port_wipy
|
||||
|
||||
adc = pyb.ADCAll(resolution) # creale an ADCAll object
|
||||
val = adc.read_channel(channel) # read the given channel
|
||||
val = adc.read_core_temp() # read MCU temperature
|
||||
val = adc.read_core_vbat() # read MCU VBAT
|
||||
val = adc.read_core_vref() # read MCU VREF
|
||||
Usage::
|
||||
|
||||
import pyb
|
||||
|
||||
adc = pyb.ADC(pin) # create an analog object on one of the 4 ADC channels
|
||||
val = adc.read() # read an analog value
|
||||
adc.deinit() # disable the adc channel
|
||||
adc.init() # enable the adc channel
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: pyb.ADC(pin)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Create an ADC object associated with the given pin.
|
||||
This allows you to then read analog values on that pin.
|
||||
.. class:: pyb.ADC(pin)
|
||||
|
||||
Create an ADC object associated with the given pin.
|
||||
This allows you to then read analog values on that pin.
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. class:: pyb.ADC(pin)
|
||||
|
||||
Create an ADC object associated with the given pin.
|
||||
This allows you to then read analog values on that pin.
|
||||
For more info check the `pinout and alternate functions
|
||||
table. <https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png>`_
|
||||
|
||||
.. warning::
|
||||
|
||||
ADC pin input range is 0-1.4V (being 1.8V the absolute maximum that it
|
||||
can withstand). When GP2, GP3, GP4 or GP5 are remapped to the
|
||||
ADC block, 1.8 V is the maximum. If these pins are used in digital mode,
|
||||
then the maximum allowed input is 3.6V.
|
||||
|
||||
Methods
|
||||
-------
|
||||
@@ -34,19 +63,49 @@ Methods
|
||||
Read the value on the analog pin and return it. The returned value
|
||||
will be between 0 and 4095.
|
||||
|
||||
.. method:: adc.read_timed(buf, freq)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Read analog values into the given buffer at the given frequency. Buffer
|
||||
can be bytearray or array.array for example. If a buffer with 8-bit elements
|
||||
is used, sample resolution will be reduced to 8 bits.
|
||||
|
||||
Example::
|
||||
|
||||
adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
|
||||
buf = bytearray(100) # create a buffer of 100 bytes
|
||||
adc.read_timed(buf, 10) # read analog values into buf at 10Hz
|
||||
# this will take 10 seconds to finish
|
||||
for val in buf: # loop over all values
|
||||
print(val) # print the value out
|
||||
|
||||
This function does not allocate any memory.
|
||||
.. method:: adc.read_timed(buf, timer)
|
||||
|
||||
Read analog values into ``buf`` at a rate set by the ``timer`` object.
|
||||
|
||||
``buf`` can be bytearray or array.array for example. The ADC values have
|
||||
12-bit resolution and are stored directly into ``buf`` if its element size is
|
||||
16 bits or greater. If ``buf`` has only 8-bit elements (eg a bytearray) then
|
||||
the sample resolution will be reduced to 8 bits.
|
||||
|
||||
``timer`` should be a Timer object, and a sample is read each time the timer
|
||||
triggers. The timer must already be initialised and running at the desired
|
||||
sampling frequency.
|
||||
|
||||
To support previous behaviour of this function, ``timer`` can also be an
|
||||
integer which specifies the frequency (in Hz) to sample at. In this case
|
||||
Timer(6) will be automatically configured to run at the given frequency.
|
||||
|
||||
Example using a Timer object (preferred way)::
|
||||
|
||||
adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
|
||||
tim = pyb.Timer(6, freq=10) # create a timer running at 10Hz
|
||||
buf = bytearray(100) # creat a buffer to store the samples
|
||||
adc.read_timed(buf, tim) # sample 100 values, taking 10s
|
||||
|
||||
Example using an integer for the frequency::
|
||||
|
||||
adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
|
||||
buf = bytearray(100) # create a buffer of 100 bytes
|
||||
adc.read_timed(buf, 10) # read analog values into buf at 10Hz
|
||||
# this will take 10 seconds to finish
|
||||
for val in buf: # loop over all values
|
||||
print(val) # print the value out
|
||||
|
||||
This function does not allocate any memory.
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. method:: adc.init()
|
||||
|
||||
Enable the ADC channel.
|
||||
|
||||
.. method:: adc.deinit()
|
||||
|
||||
Disable the ADC channel.
|
||||
|
||||
@@ -17,16 +17,6 @@ Constructors
|
||||
|
||||
Create and return an accelerometer object.
|
||||
|
||||
Note: if you read accelerometer values immediately after creating this object
|
||||
you will get 0. It takes around 20ms for the first sample to be ready, so,
|
||||
unless you have some other code between creating this object and reading its
|
||||
values, you should put a ``pyb.delay(20)`` after creating it. For example::
|
||||
|
||||
accel = pyb.Accel()
|
||||
pyb.delay(20)
|
||||
print(accel.x())
|
||||
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
@@ -34,6 +24,11 @@ Methods
|
||||
|
||||
Get a 3-tuple of filtered x, y and z values.
|
||||
|
||||
Implementation note: this method is currently implemented as taking the
|
||||
sum of 4 samples, sampled from the 3 previous calls to this function along
|
||||
with the sample from the current call. Returned values are therefore 4
|
||||
times the size of what they would be from the raw x(), y() and z() calls.
|
||||
|
||||
.. method:: accel.tilt()
|
||||
|
||||
Get the tilt register.
|
||||
|
||||
@@ -64,6 +64,16 @@ Methods
|
||||
Initiates a burst of RAM to DAC using a DMA transfer.
|
||||
The input data is treated as an array of bytes (8 bit data).
|
||||
|
||||
``freq`` can be an integer specifying the frequency to write the DAC
|
||||
samples at, using Timer(6). Or it can be an already-initialised
|
||||
Timer object which is used to trigger the DAC sample. Valid timers
|
||||
are 2, 4, 5, 6, 7 and 8.
|
||||
|
||||
``mode`` can be ``DAC.NORMAL`` or ``DAC.CIRCULAR``.
|
||||
|
||||
TIM6 is used to control the frequency of the transfer.
|
||||
|
||||
Example using both DACs at the same time::
|
||||
|
||||
dac1 = DAC(1)
|
||||
dac2 = DAC(2)
|
||||
dac1.write_timed(buf1, pyb.Timer(6, freq=100), mode=DAC.CIRCULAR)
|
||||
dac2.write_timed(buf2, pyb.Timer(7, freq=200), mode=DAC.CIRCULAR)
|
||||
|
||||
46
docs/library/pyb.HeartBeat.rst
Normal file
46
docs/library/pyb.HeartBeat.rst
Normal file
@@ -0,0 +1,46 @@
|
||||
.. _pyb.HeartBeat:
|
||||
|
||||
class HeartBeat -- heart beat LED
|
||||
=================================
|
||||
|
||||
The HeartBeat class controls the heart beat led which by default
|
||||
flashes once every 5s. The user can disable the HeartBeat and then
|
||||
is free to control this LED manually through GP25 using the Pin
|
||||
class. The GP25 can also be remapped as a PWM output, an this
|
||||
can be used to control the light intesity of the heart beat LED.
|
||||
|
||||
Example usage::
|
||||
|
||||
hb = pyb.HeartBeat()
|
||||
hb.disable() # disable the heart beat
|
||||
hb.enable() # enable the heart beat
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: pyb.HeartBeat()
|
||||
|
||||
Create a HeartBeat object.
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: heartbeat.enable()
|
||||
|
||||
Enable the heart beat. The LED will flash once every 5 seconds.
|
||||
|
||||
.. method:: heartbeat.disable()
|
||||
|
||||
Disable the heart beat. The LED can then be controlled manually.
|
||||
|
||||
Example::
|
||||
|
||||
import pyb
|
||||
|
||||
# disable the heart beat
|
||||
pyb.HeartBeat().disable()
|
||||
# get the GP25 pin object
|
||||
hbl = pyb.Pin('GP25')
|
||||
# toggle the led
|
||||
hbl.toggle()
|
||||
...
|
||||
@@ -7,19 +7,34 @@ I2C is a two-wire protocol for communicating between devices. At the physical
|
||||
level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
|
||||
|
||||
I2C objects are created attached to a specific bus. They can be initialised
|
||||
when created, or initialised later on::
|
||||
when created, or initialised later on.
|
||||
|
||||
from pyb import I2C
|
||||
.. only:: port_pyboard
|
||||
|
||||
i2c = I2C(1) # create on bus 1
|
||||
i2c = I2C(1, I2C.MASTER) # create and init as a master
|
||||
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
|
||||
i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
|
||||
i2c.deinit() # turn off the peripheral
|
||||
Example::
|
||||
|
||||
from pyb import I2C
|
||||
|
||||
i2c = I2C(1) # create on bus 1
|
||||
i2c = I2C(1, I2C.MASTER) # create and init as a master
|
||||
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
|
||||
i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
|
||||
i2c.deinit() # turn off the peripheral
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
Example::
|
||||
|
||||
from pyb import I2C
|
||||
|
||||
i2c = I2C(1) # create on bus 1
|
||||
i2c = I2C(1, I2C.MASTER) # create and init as a master
|
||||
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
|
||||
i2c.deinit() # turn off the peripheral
|
||||
|
||||
Printing the i2c object gives you information about its configuration.
|
||||
|
||||
Basic methods for slave are send and recv::
|
||||
The basic methods are send and recv::
|
||||
|
||||
i2c.send('abc') # send 3 bytes
|
||||
i2c.send(0x42) # send a single byte, given by the number
|
||||
@@ -47,24 +62,36 @@ Master also has other methods::
|
||||
# a list of valid addresses
|
||||
i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
|
||||
# starting at address 2 in the slave
|
||||
i2c.mem_write('abc', 0x42, 2, timeout=1000)
|
||||
|
||||
i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42
|
||||
# starting at address 2 in the slave, timeout after 1 second
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: pyb.I2C(bus, ...)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Construct an I2C object on the given bus. ``bus`` can be 1 or 2.
|
||||
With no additional parameters, the I2C object is created but not
|
||||
initialised (it has the settings from the last initialisation of
|
||||
the bus, if any). If extra arguments are given, the bus is initialised.
|
||||
See ``init`` for parameters of initialisation.
|
||||
|
||||
The physical pins of the I2C busses are:
|
||||
|
||||
- ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)``
|
||||
- ``I2C(2)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PB10, PB11)``
|
||||
.. class:: pyb.I2C(bus, ...)
|
||||
|
||||
Construct an I2C object on the given bus. ``bus`` can be 1 or 2.
|
||||
With no additional parameters, the I2C object is created but not
|
||||
initialised (it has the settings from the last initialisation of
|
||||
the bus, if any). If extra arguments are given, the bus is initialised.
|
||||
See ``init`` for parameters of initialisation.
|
||||
|
||||
The physical pins of the I2C busses are:
|
||||
|
||||
- ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)``
|
||||
- ``I2C(2)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PB10, PB11)``
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. class:: pyb.I2C(bus, ...)
|
||||
|
||||
Construct an I2C object on the given bus. `bus` can only be 1.
|
||||
With no additional parameters, the I2C object is created but not
|
||||
initialised (it has the settings from the last initialisation of
|
||||
the bus, if any). If extra arguments are given, the bus is initialised.
|
||||
See `init` for parameters of initialisation.
|
||||
|
||||
|
||||
Methods
|
||||
@@ -74,54 +101,65 @@ Methods
|
||||
|
||||
Turn off the I2C bus.
|
||||
|
||||
.. method:: i2c.init(mode, \*, addr=0x12, baudrate=400000, gencall=False)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Initialise the I2C bus with the given parameters:
|
||||
|
||||
- ``mode`` must be either ``I2C.MASTER`` or ``I2C.SLAVE``
|
||||
- ``addr`` is the 7-bit address (only sensible for a slave)
|
||||
- ``baudrate`` is the SCL clock rate (only sensible for a master)
|
||||
- ``gencall`` is whether to support general call mode
|
||||
.. method:: i2c.init(mode, \*, addr=0x12, baudrate=400000, gencall=False)
|
||||
|
||||
Initialise the I2C bus with the given parameters:
|
||||
|
||||
- ``mode`` must be either ``I2C.MASTER`` or ``I2C.SLAVE``
|
||||
- ``addr`` is the 7-bit address (only sensible for a slave)
|
||||
- ``baudrate`` is the SCL clock rate (only sensible for a master)
|
||||
- ``gencall`` is whether to support general call mode
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. method:: i2c.init(mode, \*, baudrate=100000)
|
||||
|
||||
Initialise the I2C bus with the given parameters:
|
||||
|
||||
- ``mode`` must be ``I2C.MASTER``
|
||||
- ``baudrate`` is the SCL clock rate
|
||||
|
||||
.. method:: i2c.is_ready(addr)
|
||||
|
||||
Check if an I2C device responds to the given address. Only valid when in master mode.
|
||||
|
||||
.. method:: i2c.mem_read(data, addr, memaddr, timeout=5000, addr_size=8)
|
||||
.. method:: i2c.mem_read(data, addr, memaddr, \*, timeout=5000, addr_size=8)
|
||||
|
||||
Read from the memory of an I2C device:
|
||||
|
||||
|
||||
- ``data`` can be an integer (number of bytes to read) or a buffer to read into
|
||||
- ``addr`` is the I2C device address
|
||||
- ``memaddr`` is the memory location within the I2C device
|
||||
- ``timeout`` is the timeout in milliseconds to wait for the read
|
||||
- ``addr_size`` selects width of memaddr: 8 or 16 bits
|
||||
|
||||
|
||||
Returns the read data.
|
||||
This is only valid in master mode.
|
||||
|
||||
.. method:: i2c.mem_write(data, addr, memaddr, timeout=5000, addr_size=8)
|
||||
.. method:: i2c.mem_write(data, addr, memaddr, \*, timeout=5000, addr_size=8)
|
||||
|
||||
Write to the memory of an I2C device:
|
||||
|
||||
|
||||
- ``data`` can be an integer or a buffer to write from
|
||||
- ``addr`` is the I2C device address
|
||||
- ``memaddr`` is the memory location within the I2C device
|
||||
- ``timeout`` is the timeout in milliseconds to wait for the write
|
||||
- ``addr_size`` selects width of memaddr: 8 or 16 bits
|
||||
|
||||
|
||||
Returns ``None``.
|
||||
This is only valid in master mode.
|
||||
|
||||
.. method:: i2c.recv(recv, addr=0x00, timeout=5000)
|
||||
.. method:: i2c.recv(recv, addr=0x00, \*, timeout=5000)
|
||||
|
||||
Receive data on the bus:
|
||||
|
||||
|
||||
- ``recv`` can be an integer, which is the number of bytes to receive,
|
||||
or a mutable buffer, which will be filled with received bytes
|
||||
- ``addr`` is the address to receive from (only required in master mode)
|
||||
- ``timeout`` is the timeout in milliseconds to wait for the receive
|
||||
|
||||
|
||||
Return value: if ``recv`` is an integer then a new buffer of the bytes received,
|
||||
otherwise the same buffer that was passed in to ``recv``.
|
||||
|
||||
@@ -130,16 +168,15 @@ Methods
|
||||
Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
|
||||
Only valid when in master mode.
|
||||
|
||||
.. method:: i2c.send(send, addr=0x00, timeout=5000)
|
||||
.. method:: i2c.send(send, addr=0x00, \*, timeout=5000)
|
||||
|
||||
Send data on the bus:
|
||||
|
||||
|
||||
- ``send`` is the data to send (an integer to send, or a buffer object)
|
||||
- ``addr`` is the address to send to (only required in master mode)
|
||||
- ``timeout`` is the timeout in milliseconds to wait for the send
|
||||
|
||||
Return value: ``None``.
|
||||
|
||||
Return value: ``None``.
|
||||
|
||||
Constants
|
||||
---------
|
||||
@@ -148,6 +185,8 @@ Constants
|
||||
|
||||
for initialising the bus to master mode
|
||||
|
||||
.. data:: I2C.SLAVE
|
||||
.. only:: port_pyboard
|
||||
|
||||
for initialising the bus to slave mode
|
||||
.. data:: I2C.SLAVE
|
||||
|
||||
for initialising the bus to slave mode
|
||||
|
||||
@@ -5,113 +5,190 @@ class Pin -- control I/O pins
|
||||
|
||||
A pin is the basic object to control I/O pins. It has methods to set
|
||||
the mode of the pin (input, output, etc) and methods to get and set the
|
||||
digital logic level. For analog control of a pin, see the ADC class.
|
||||
digital logic level. For analog control of a pin, see the ADC class.
|
||||
|
||||
Usage Model:
|
||||
|
||||
All Board Pins are predefined as pyb.Pin.board.Name ::
|
||||
.. only:: port_pyboard
|
||||
|
||||
x1_pin = pyb.Pin.board.X1
|
||||
All Board Pins are predefined as pyb.Pin.board.Name::
|
||||
|
||||
x1_pin = pyb.Pin.board.X1
|
||||
|
||||
g = pyb.Pin(pyb.Pin.board.X1, pyb.Pin.IN)
|
||||
|
||||
CPU pins which correspond to the board pins are available
|
||||
as ``pyb.cpu.Name``. For the CPU pins, the names are the port letter
|
||||
followed by the pin number. On the PYBv1.0, ``pyb.Pin.board.X1`` and
|
||||
``pyb.Pin.cpu.B6`` are the same pin.
|
||||
|
||||
You can also use strings::
|
||||
|
||||
g = pyb.Pin('X1', pyb.Pin.OUT_PP)
|
||||
|
||||
Users can add their own names::
|
||||
|
||||
MyMapperDict = { 'LeftMotorDir' : pyb.Pin.cpu.C12 }
|
||||
pyb.Pin.dict(MyMapperDict)
|
||||
g = pyb.Pin("LeftMotorDir", pyb.Pin.OUT_OD)
|
||||
|
||||
and can query mappings::
|
||||
|
||||
pin = pyb.Pin("LeftMotorDir")
|
||||
|
||||
Users can also add their own mapping function::
|
||||
|
||||
def MyMapper(pin_name):
|
||||
if pin_name == "LeftMotorDir":
|
||||
return pyb.Pin.cpu.A0
|
||||
|
||||
pyb.Pin.mapper(MyMapper)
|
||||
|
||||
So, if you were to call: ``pyb.Pin("LeftMotorDir", pyb.Pin.OUT_PP)``
|
||||
then ``"LeftMotorDir"`` is passed directly to the mapper function.
|
||||
|
||||
To summarise, the following order determines how things get mapped into
|
||||
an ordinal pin number:
|
||||
|
||||
1. Directly specify a pin object
|
||||
2. User supplied mapping function
|
||||
3. User supplied mapping (object must be usable as a dictionary key)
|
||||
4. Supply a string which matches a board pin
|
||||
5. Supply a string which matches a CPU port/pin
|
||||
|
||||
You can set ``pyb.Pin.debug(True)`` to get some debug information about
|
||||
how a particular object gets mapped to a pin.
|
||||
|
||||
When a pin has the ``Pin.PULL_UP`` or ``Pin.PULL_DOWN`` pull-mode enabled,
|
||||
that pin has an effective 40k Ohm resistor pulling it to 3V3 or GND
|
||||
respectively (except pin Y5 which has 11k Ohm resistors).
|
||||
|
||||
g = pyb.Pin(pyb.Pin.board.X1, pyb.Pin.IN)
|
||||
.. only:: port_wipy
|
||||
|
||||
CPU pins which correspond to the board pins are available
|
||||
as ``pyb.cpu.Name``. For the CPU pins, the names are the port letter
|
||||
followed by the pin number. On the PYBv1.0, ``pyb.Pin.board.X1`` and
|
||||
``pyb.Pin.cpu.B6`` are the same pin.
|
||||
Board pins are identified by their string name::
|
||||
|
||||
You can also use strings::
|
||||
g = pyb.Pin('GP9', af=0, mode=pyb.Pin.IN, type=pyb.Pin.STD, strength=pyb.Pin.S2MA)
|
||||
|
||||
g = pyb.Pin('X1', pyb.Pin.OUT_PP)
|
||||
You can also configure the Pin to generate interrupts. For instance::
|
||||
|
||||
Users can add their own names::
|
||||
def pincb(pin):
|
||||
print(pin.name())
|
||||
|
||||
MyMapperDict = { 'LeftMotorDir' : pyb.Pin.cpu.C12 }
|
||||
pyb.Pin.dict(MyMapperDict)
|
||||
g = pyb.Pin("LeftMotorDir", pyb.Pin.OUT_OD)
|
||||
pin_int = pyb.Pin('GP10', af=0, mode=Pin.IN, type=pyb.Pin.STD_PD, strength=pyb.Pin.S2MA)
|
||||
pin_int.callback (mode=pyb.Pin.INT_RISING, handler=pincb)
|
||||
# the callback can be triggered manually
|
||||
pin_int.callback()()
|
||||
# to disable the callback
|
||||
pin_int.callback().disable()
|
||||
|
||||
and can query mappings ::
|
||||
Now every time a falling edge is seen on the gpio pin, the callback will be
|
||||
executed. Caution: mechanical push buttons have "bounce" and pushing or
|
||||
releasing a switch will often generate multiple edges.
|
||||
See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed
|
||||
explanation, along with various techniques for debouncing.
|
||||
|
||||
pin = pyb.Pin("LeftMotorDir")
|
||||
|
||||
Users can also add their own mapping function::
|
||||
|
||||
def MyMapper(pin_name):
|
||||
if pin_name == "LeftMotorDir":
|
||||
return pyb.Pin.cpu.A0
|
||||
|
||||
pyb.Pin.mapper(MyMapper)
|
||||
|
||||
So, if you were to call: ``pyb.Pin("LeftMotorDir", pyb.Pin.OUT_PP)``
|
||||
then ``"LeftMotorDir"`` is passed directly to the mapper function.
|
||||
|
||||
To summarise, the following order determines how things get mapped into
|
||||
an ordinal pin number:
|
||||
|
||||
1. Directly specify a pin object
|
||||
2. User supplied mapping function
|
||||
3. User supplied mapping (object must be usable as a dictionary key)
|
||||
4. Supply a string which matches a board pin
|
||||
5. Supply a string which matches a CPU port/pin
|
||||
|
||||
You can set ``pyb.Pin.debug(True)`` to get some debug information about
|
||||
how a particular object gets mapped to a pin.
|
||||
|
||||
When a pin has the ``Pin.PULL_UP`` or ``Pin.PULL_DOWN`` pull-mode enabled,
|
||||
that pin has an effective 40k Ohm resistor pulling it to 3V3 or GND
|
||||
respectively (except pin Y5 which has 11k Ohm resistors).
|
||||
All pin objects go through the pin mapper to come up with one of the
|
||||
gpio pins.
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. class:: pyb.Pin(id, ...)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Create a new Pin object associated with the id. If additional arguments are given,
|
||||
they are used to initialise the pin. See :meth:`pin.init`.
|
||||
.. class:: pyb.Pin(id, ...)
|
||||
|
||||
Create a new Pin object associated with the id. If additional arguments are given,
|
||||
they are used to initialise the pin. See :meth:`pin.init`.
|
||||
|
||||
Class methods
|
||||
-------------
|
||||
.. only:: port_wipy
|
||||
|
||||
.. method:: Pin.af_list()
|
||||
.. class:: pyb.Pin(name, ...)
|
||||
|
||||
Returns an array of alternate functions available for this pin.
|
||||
Create a new Pin object associated with the name. If additional arguments are given,
|
||||
they are used to initialise the pin. See :meth:`pin.init`.
|
||||
|
||||
.. method:: Pin.debug([state])
|
||||
.. only:: port_pyboard
|
||||
|
||||
Get or set the debugging state (``True`` or ``False`` for on or off).
|
||||
|
||||
.. method:: Pin.dict([dict])
|
||||
|
||||
Get or set the pin mapper dictionary.
|
||||
|
||||
.. method:: Pin.mapper([fun])
|
||||
|
||||
Get or set the pin mapper function.
|
||||
Class methods
|
||||
-------------
|
||||
|
||||
.. method:: Pin.af_list()
|
||||
|
||||
Returns an array of alternate functions available for this pin.
|
||||
|
||||
.. method:: Pin.debug([state])
|
||||
|
||||
Get or set the debugging state (``True`` or ``False`` for on or off).
|
||||
|
||||
.. method:: Pin.dict([dict])
|
||||
|
||||
Get or set the pin mapper dictionary.
|
||||
|
||||
.. method:: Pin.mapper([fun])
|
||||
|
||||
Get or set the pin mapper function.
|
||||
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: pin.init(mode, pull=Pin.PULL_NONE, af=-1)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Initialise the pin:
|
||||
|
||||
- ``mode`` can be one of:
|
||||
- ``Pin.IN`` - configure the pin for input;
|
||||
- ``Pin.OUT_PP`` - configure the pin for output, with push-pull control;
|
||||
- ``Pin.OUT_OD`` - configure the pin for output, with open-drain control;
|
||||
- ``Pin.AF_PP`` - configure the pin for alternate function, pull-pull;
|
||||
- ``Pin.AF_OD`` - configure the pin for alternate function, open-drain;
|
||||
- ``Pin.ANALOG`` - configure the pin for analog.
|
||||
- ``pull`` can be one of:
|
||||
- ``Pin.PULL_NONE`` - no pull up or down resistors;
|
||||
- ``Pin.PULL_UP`` - enable the pull-up resistor;
|
||||
- ``Pin.PULL_DOWN`` - enable the pull-down resistor.
|
||||
- when mode is Pin.AF_PP or Pin.AF_OD, then af can be the index or name
|
||||
of one of the alternate functions associated with a pin.
|
||||
|
||||
Returns: ``None``.
|
||||
.. method:: pin.init(mode, pull=Pin.PULL_NONE, af=-1)
|
||||
|
||||
Initialise the pin:
|
||||
|
||||
- ``mode`` can be one of:
|
||||
|
||||
- ``Pin.IN`` - configure the pin for input;
|
||||
- ``Pin.OUT_PP`` - configure the pin for output, with push-pull control;
|
||||
- ``Pin.OUT_OD`` - configure the pin for output, with open-drain control;
|
||||
- ``Pin.AF_PP`` - configure the pin for alternate function, pull-pull;
|
||||
- ``Pin.AF_OD`` - configure the pin for alternate function, open-drain;
|
||||
- ``Pin.ANALOG`` - configure the pin for analog.
|
||||
|
||||
- ``pull`` can be one of:
|
||||
|
||||
- ``Pin.PULL_NONE`` - no pull up or down resistors;
|
||||
- ``Pin.PULL_UP`` - enable the pull-up resistor;
|
||||
- ``Pin.PULL_DOWN`` - enable the pull-down resistor.
|
||||
|
||||
- when mode is ``Pin.AF_PP`` or ``Pin.AF_OD``, then af can be the index or name
|
||||
of one of the alternate functions associated with a pin.
|
||||
|
||||
Returns: ``None``.
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
.. method:: pin.init(af, mode, type, strength)
|
||||
|
||||
Initialise the pin:
|
||||
|
||||
- ``af`` is the number of the alternate function. Please refer to the
|
||||
`pinout and alternate functions table. <https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png>`_
|
||||
for the specific alternate functions that each pin supports.
|
||||
|
||||
- ``mode`` can be one of:
|
||||
|
||||
- ``Pin.OUT`` - no pull up or down resistors.
|
||||
- ``Pin.IN`` - enable the pull-up resistor.
|
||||
|
||||
- ``type`` can be one of:
|
||||
|
||||
- ``Pin.STD`` - push-pull pin.
|
||||
- ``Pin.STD_PU`` - push-pull pin with pull-up resistor.
|
||||
- ``Pin.STD_PD`` - push-pull pin with pull-down resistor.
|
||||
- ``Pin.OD`` - open drain pin.
|
||||
- ``Pin.OD_PU`` - open drain pin with pull-up resistor.
|
||||
- ``Pin.OD_PD`` - open drain pin with pull-down resistor.
|
||||
|
||||
- ``strength`` can be one of:
|
||||
|
||||
- ``Pin.S2MA`` - 2mA drive capability.
|
||||
- ``Pin.S4MA`` - 4mA drive capability.
|
||||
- ``Pin.S6MA`` - 6mA drive capability.
|
||||
|
||||
Returns: ``None``.
|
||||
|
||||
.. method:: pin.high()
|
||||
|
||||
@@ -130,137 +207,256 @@ Methods
|
||||
anything that converts to a boolean. If it converts to ``True``, the pin
|
||||
is set high, otherwise it is set low.
|
||||
|
||||
.. method:: pin.__str__()
|
||||
|
||||
Return a string describing the pin object.
|
||||
|
||||
.. method:: pin.af()
|
||||
|
||||
Returns the currently configured alternate-function of the pin. The
|
||||
integer returned will match one of the allowed constants for the af
|
||||
argument to the init function.
|
||||
|
||||
.. method:: pin.gpio()
|
||||
|
||||
Returns the base address of the GPIO block associated with this pin.
|
||||
|
||||
.. method:: pin.mode()
|
||||
|
||||
Returns the currently configured mode of the pin. The integer returned
|
||||
will match one of the allowed constants for the mode argument to the init
|
||||
function.
|
||||
.. only:: port_pyboard
|
||||
|
||||
.. method:: pin.__str__()
|
||||
|
||||
Return a string describing the pin object.
|
||||
|
||||
.. method:: pin.af()
|
||||
|
||||
Returns the currently configured alternate-function of the pin. The
|
||||
integer returned will match one of the allowed constants for the af
|
||||
argument to the init function.
|
||||
|
||||
.. method:: pin.gpio()
|
||||
|
||||
Returns the base address of the GPIO block associated with this pin.
|
||||
|
||||
.. method:: pin.mode()
|
||||
|
||||
Returns the currently configured mode of the pin. The integer returned
|
||||
will match one of the allowed constants for the mode argument to the init
|
||||
function.
|
||||
|
||||
.. method:: pin.name()
|
||||
|
||||
Get the pin name.
|
||||
|
||||
.. method:: pin.names()
|
||||
.. only:: port_pyboard
|
||||
|
||||
Returns the cpu and board names for this pin.
|
||||
.. method:: pin.names()
|
||||
|
||||
Returns the cpu and board names for this pin.
|
||||
|
||||
.. method:: pin.pin()
|
||||
|
||||
Get the pin number.
|
||||
|
||||
.. method:: pin.port()
|
||||
|
||||
Get the pin port.
|
||||
|
||||
.. method:: pin.pull()
|
||||
|
||||
Returns the currently configured pull of the pin. The integer returned
|
||||
will match one of the allowed constants for the pull argument to the init
|
||||
function.
|
||||
|
||||
.. method:: pin.pin()
|
||||
.. only:: port_wipy
|
||||
|
||||
Get the pin number.
|
||||
.. method:: pin.toggle()
|
||||
|
||||
.. method:: pin.port()
|
||||
Toggle the value of the pin.
|
||||
|
||||
Get the pin port.
|
||||
.. method:: pin.info()
|
||||
|
||||
.. method:: pin.pull()
|
||||
Return a 5-tuple with the configuration of the pin:
|
||||
``(name, alternate-function, mode, type, strength)``
|
||||
|
||||
Returns the currently configured pull of the pin. The integer returned
|
||||
will match one of the allowed constants for the pull argument to the init
|
||||
function.
|
||||
.. warning::
|
||||
This method cannot be called within a callback (interrupt-context)
|
||||
because it needs to allocate memory to return the tuple and memory
|
||||
allocations are disabled while interrupts are being serviced.
|
||||
|
||||
.. method:: pin.callback(\*, mode, priority=1, handler=None, wakes=pyb.Sleep.ACTIVE)
|
||||
|
||||
Create a callback to be triggered when the input level at the pin changes.
|
||||
|
||||
- ``mode`` configures the pin level which can generate an interrupt. Possible values are:
|
||||
|
||||
- ``Pin.INT_FALLING`` interrupt on falling edge.
|
||||
- ``Pin.INT_RISING`` interrupt on rising edge.
|
||||
- ``Pin.INT_RISING_FALLING`` interrupt on rising and falling edge.
|
||||
- ``Pin.INT_LOW_LEVEL`` interrupt on low level.
|
||||
- ``Pin.INT_HIGH_LEVEL`` interrupt on high level.
|
||||
|
||||
- ``priority`` level of the interrupt. Can take values in the range 1-7.
|
||||
Higher values represent higher priorities.
|
||||
- ``handler`` is an optional function to be called when new characters arrive.
|
||||
- ``wakes`` selects the power mode in which this interrupt can wake up the
|
||||
board. Please note:
|
||||
|
||||
- If ``wakes=pyb.Sleep.ACTIVE`` any pin can wake the board.
|
||||
- If ``wakes=pyb.Sleep.SUSPENDED`` pins ``GP2``, ``GP4``, ``GP10``,
|
||||
``GP11``, GP17`` or ``GP24`` can wake the board. Note that only 1
|
||||
of this pins can be enabled as a wake source at the same time, so, only
|
||||
the last enabled pin as a ``pyb.Sleep.SUSPENDED`` wake source will have effect.
|
||||
- If ``wakes=pyb.Sleep.SUSPENDED`` pins ``GP2``, ``GP4``, ``GP10``,
|
||||
``GP11``, ``GP17`` and ``GP24`` can wake the board. In this case all of the
|
||||
6 pins can be enabled as a ``pyb.Sleep.HIBERNATE`` wake source at the same time.
|
||||
- Values can be ORed to make a pin generate interrupts in more than one power
|
||||
mode.
|
||||
|
||||
Returns a callback object.
|
||||
|
||||
|
||||
Constants
|
||||
---------
|
||||
|
||||
.. data:: Pin.AF_OD
|
||||
.. only:: port_pyboard
|
||||
|
||||
initialise the pin to alternate-function mode with an open-drain drive
|
||||
.. data:: Pin.AF_OD
|
||||
|
||||
initialise the pin to alternate-function mode with an open-drain drive
|
||||
|
||||
.. data:: Pin.AF_PP
|
||||
|
||||
initialise the pin to alternate-function mode with a push-pull drive
|
||||
|
||||
.. data:: Pin.ANALOG
|
||||
|
||||
initialise the pin to analog mode
|
||||
|
||||
.. data:: Pin.IN
|
||||
|
||||
initialise the pin to input mode
|
||||
|
||||
.. data:: Pin.OUT_OD
|
||||
|
||||
initialise the pin to output mode with an open-drain drive
|
||||
|
||||
.. data:: Pin.OUT_PP
|
||||
|
||||
initialise the pin to output mode with a push-pull drive
|
||||
|
||||
.. data:: Pin.PULL_DOWN
|
||||
|
||||
enable the pull-down resistor on the pin
|
||||
|
||||
.. data:: Pin.PULL_NONE
|
||||
|
||||
don't enable any pull up or down resistors on the pin
|
||||
|
||||
.. data:: Pin.PULL_UP
|
||||
|
||||
enable the pull-up resistor on the pin
|
||||
|
||||
.. data:: Pin.AF_PP
|
||||
.. only:: port_wipy
|
||||
|
||||
initialise the pin to alternate-function mode with a push-pull drive
|
||||
.. data:: Pin.IN
|
||||
|
||||
input pin mode
|
||||
|
||||
.. data:: Pin.OUT
|
||||
|
||||
output pin mode
|
||||
|
||||
.. data:: Pin.STD
|
||||
|
||||
push-pull pin type
|
||||
|
||||
.. data:: Pin.STD_PU
|
||||
|
||||
push-pull pin with internall pull-up resistor
|
||||
|
||||
.. data:: Pin.STD_PD
|
||||
|
||||
push-pull pin with internall pull-down resistor
|
||||
|
||||
.. data:: Pin.OD
|
||||
|
||||
open-drain pin
|
||||
|
||||
.. data:: Pin.OD_PU
|
||||
|
||||
open-drain pin with pull-up resistor
|
||||
|
||||
.. data:: Pin.OD_PD
|
||||
|
||||
open-drain pin with pull-down resistor
|
||||
|
||||
.. data:: Pin.INT_FALLING
|
||||
|
||||
interrupt on falling edge
|
||||
|
||||
.. data:: Pin.INT_RISING
|
||||
|
||||
interrupt on rising edge
|
||||
|
||||
.. data:: Pin.INT_RISING_FALLING
|
||||
|
||||
interrupt on rising and falling edge
|
||||
|
||||
.. data:: Pin.INT_LOW_LEVEL
|
||||
|
||||
interrupt on low level
|
||||
|
||||
.. data:: Pin.INT_HIGH_LEVEL
|
||||
|
||||
interrupt on high level
|
||||
|
||||
.. data:: Pin.S2MA
|
||||
|
||||
2mA drive strength
|
||||
|
||||
.. data:: Pin.S4MA
|
||||
|
||||
4mA drive strength
|
||||
|
||||
.. data:: Pin.S6MA
|
||||
|
||||
6mA drive strength
|
||||
|
||||
.. data:: Pin.ANALOG
|
||||
.. only:: port_pyboard
|
||||
|
||||
initialise the pin to analog mode
|
||||
class PinAF -- Pin Alternate Functions
|
||||
======================================
|
||||
|
||||
A Pin represents a physical pin on the microcprocessor. Each pin
|
||||
can have a variety of functions (GPIO, I2C SDA, etc). Each PinAF
|
||||
object represents a particular function for a pin.
|
||||
|
||||
Usage Model::
|
||||
|
||||
x3 = pyb.Pin.board.X3
|
||||
x3_af = x3.af_list()
|
||||
|
||||
x3_af will now contain an array of PinAF objects which are availble on
|
||||
pin X3.
|
||||
|
||||
For the pyboard, x3_af would contain:
|
||||
[Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM9, Pin.AF7_USART2]
|
||||
|
||||
Normally, each peripheral would configure the af automatically, but sometimes
|
||||
the same function is available on multiple pins, and having more control
|
||||
is desired.
|
||||
|
||||
To configure X3 to expose TIM2_CH3, you could use::
|
||||
|
||||
pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=pyb.Pin.AF1_TIM2)
|
||||
|
||||
or::
|
||||
|
||||
pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=1)
|
||||
|
||||
.. data:: Pin.IN
|
||||
|
||||
initialise the pin to input mode
|
||||
|
||||
.. data:: Pin.OUT_OD
|
||||
|
||||
initialise the pin to output mode with an open-drain drive
|
||||
|
||||
.. data:: Pin.OUT_PP
|
||||
|
||||
initialise the pin to output mode with a push-pull drive
|
||||
|
||||
.. data:: Pin.PULL_DOWN
|
||||
|
||||
enable the pull-down resistor on the pin
|
||||
|
||||
.. data:: Pin.PULL_NONE
|
||||
|
||||
don't enable any pull up or down resistors on the pin
|
||||
|
||||
.. data:: Pin.PULL_UP
|
||||
|
||||
enable the pull-up resistor on the pin
|
||||
|
||||
|
||||
class PinAF -- Pin Alternate Functions
|
||||
======================================
|
||||
|
||||
A Pin represents a physical pin on the microcprocessor. Each pin
|
||||
can have a variety of functions (GPIO, I2C SDA, etc). Each PinAF
|
||||
object represents a particular function for a pin.
|
||||
|
||||
Usage Model::
|
||||
|
||||
x3 = pyb.Pin.board.X3
|
||||
x3_af = x3.af_list()
|
||||
|
||||
x3_af will now contain an array of PinAF objects which are availble on
|
||||
pin X3.
|
||||
|
||||
For the pyboard, x3_af would contain:
|
||||
[Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM9, Pin.AF7_USART2]
|
||||
|
||||
Normally, each peripheral would configure the af automatically, but sometimes
|
||||
the same function is available on multiple pins, and having more control
|
||||
is desired.
|
||||
|
||||
To configure X3 to expose TIM2_CH3, you could use::
|
||||
|
||||
pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=pyb.Pin.AF1_TIM2)
|
||||
|
||||
or::
|
||||
|
||||
pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=1)
|
||||
|
||||
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: pinaf.__str__()
|
||||
|
||||
Return a string describing the alternate function.
|
||||
|
||||
.. method:: pinaf.index()
|
||||
|
||||
Return the alternate function index.
|
||||
|
||||
.. method:: pinaf.name()
|
||||
|
||||
Return the name of the alternate function.
|
||||
|
||||
.. method:: pinaf.reg()
|
||||
|
||||
Return the base register associated with the peripheral assigned to this
|
||||
alternate function. For example, if the alternate function were TIM2_CH3
|
||||
this would return stm.TIM2
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: pinaf.__str__()
|
||||
|
||||
Return a string describing the alternate function.
|
||||
|
||||
.. method:: pinaf.index()
|
||||
|
||||
Return the alternate function index.
|
||||
|
||||
.. method:: pinaf.name()
|
||||
|
||||
Return the name of the alternate function.
|
||||
|
||||
.. method:: pinaf.reg()
|
||||
|
||||
Return the base register associated with the peripheral assigned to this
|
||||
alternate function. For example, if the alternate function were TIM2_CH3
|
||||
this would return stm.TIM2
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
.. _pyb.RTC:
|
||||
|
||||
class RTC -- real time clock
|
||||
============================
|
||||
|
||||
@@ -30,46 +32,70 @@ Methods
|
||||
date and time. With 1 argument (being an 8-tuple) it sets the date
|
||||
and time.
|
||||
|
||||
The 8-tuple has the following format:
|
||||
.. only:: port_pyboard
|
||||
|
||||
(year, month, day, weekday, hours, minutes, seconds, subseconds)
|
||||
The 8-tuple has the following format:
|
||||
|
||||
(year, month, day, weekday, hours, minutes, seconds, subseconds)
|
||||
|
||||
``weekday`` is 1-7 for Monday through Sunday.
|
||||
|
||||
``subseconds`` counts down from 255 to 0
|
||||
|
||||
.. only:: port_wipy
|
||||
|
||||
``weekday`` is 1-7 for Monday through Sunday.
|
||||
|
||||
``subseconds`` counts down from 255 to 0
|
||||
The 8-tuple has the following format:
|
||||
|
||||
``(year, month, day, weekday, hours, minutes, seconds, milliseconds)``
|
||||
|
||||
``weekday`` is 0-6 for Monday through Sunday.
|
||||
|
||||
.. method:: rtc.wakeup(timeout, callback=None)
|
||||
.. only:: port_pyboard
|
||||
|
||||
Set the RTC wakeup timer to trigger repeatedly at every ``timeout``
|
||||
milliseconds. This trigger can wake the pyboard from both the sleep
|
||||
states: :meth:`pyb.stop` and :meth:`pyb.standby`.
|
||||
.. method:: rtc.wakeup(timeout, callback=None)
|
||||
|
||||
Set the RTC wakeup timer to trigger repeatedly at every ``timeout``
|
||||
milliseconds. This trigger can wake the pyboard from both the sleep
|
||||
states: :meth:`pyb.stop` and :meth:`pyb.standby`.
|
||||
|
||||
If ``timeout`` is ``None`` then the wakeup timer is disabled.
|
||||
|
||||
If ``callback`` is given then it is executed at every trigger of the
|
||||
wakeup timer. ``callback`` must take exactly one argument.
|
||||
|
||||
.. method:: rtc.info()
|
||||
|
||||
Get information about the startup time and reset source.
|
||||
|
||||
- The lower 0xffff are the number of milliseconds the RTC took to
|
||||
start up.
|
||||
- Bit 0x10000 is set if a power-on reset occurred.
|
||||
- Bit 0x20000 is set if an external reset occurred
|
||||
|
||||
.. method:: rtc.calibration(cal)
|
||||
|
||||
Get or set RTC calibration.
|
||||
|
||||
With no arguments, ``calibration()`` returns the current calibration
|
||||
value, which is an integer in the range [-511 : 512]. With one
|
||||
argument it sets the RTC calibration.
|
||||
|
||||
The RTC Smooth Calibration mechanism addjusts the RTC clock rate by
|
||||
adding or subtracting the given number of ticks from the 32768 Hz
|
||||
clock over a 32 second period (corresponding to 2^20 clock ticks.)
|
||||
Each tick added will speed up the clock by 1 part in 2^20, or 0.954
|
||||
ppm; likewise the RTC clock it slowed by negative values. The
|
||||
usable calibration range is:
|
||||
(-511 * 0.954) ~= -487.5 ppm up to (512 * 0.954) ~= 488.5 ppm
|
||||
|
||||
If ``timeout`` is ``None`` then the wakeup timer is disabled.
|
||||
.. only:: port_wipy
|
||||
|
||||
If ``callback`` is given then it is executed at every trigger of the
|
||||
wakeup timer. ``callback`` must take exactly one argument.
|
||||
.. method:: rtc.callback(\*, value, handler=None, wakes=pyb.Sleep.ACTIVE)
|
||||
|
||||
Create a callback object triggered by a real time clock alarm.
|
||||
|
||||
- ``value`` is the alarm timeout in milliseconds. This parameter is required.
|
||||
- ``handler`` is the function to be called when the callback is triggered.
|
||||
- ``wakes`` specifies the power mode from where this interrupt can wake
|
||||
up the system.
|
||||
|
||||
.. method:: rtc.info()
|
||||
|
||||
Get information about the startup time and reset source.
|
||||
|
||||
- The lower 0xffff are the number of milliseconds the RTC took to
|
||||
start up.
|
||||
- Bit 0x10000 is set if a power-on reset occurred.
|
||||
- Bit 0x20000 is set if an external reset occurred
|
||||
|
||||
.. method:: rtc.calibration(cal)
|
||||
|
||||
Get or set RTC calibration.
|
||||
|
||||
With no arguments, ``calibration()`` returns the current calibration
|
||||
value, which is an integer in the range [-511 : 512]. With one
|
||||
argument it sets the RTC calibration.
|
||||
|
||||
The RTC Smooth Calibration mechanism addjusts the RTC clock rate by
|
||||
adding or subtracting the given number of ticks from the 32768 Hz
|
||||
clock over a 32 second period (corresponding to 2^20 clock ticks.)
|
||||
Each tick added will speed up the clock by 1 part in 2^20, or 0.954
|
||||
ppm; likewise the RTC clock it slowed by negative values. The
|
||||
usable calibration range is:
|
||||
(-511 * 0.954) ~= -487.5 ppm up to (512 * 0.954) ~= 488.5 ppm
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user