Skip to content

Commit 0116218

Browse files
committed
modmachine: Add new module to access hardware, starting with physical memory.
Refactored from "stm" module, provides mem8, mem16, mem32 objects with array subscript syntax.
1 parent c4dc1b5 commit 0116218

7 files changed

Lines changed: 136 additions & 0 deletions

File tree

extmod/modmachine.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdint.h>
29+
30+
#include "py/nlr.h"
31+
#include "py/obj.h"
32+
33+
#if MICROPY_PY_MACHINE
34+
35+
STATIC mp_uint_t get_read_addr(mp_obj_t addr_o, uint align) {
36+
mp_uint_t addr = mp_obj_int_get_truncated(addr_o);
37+
if ((addr & (align - 1)) != 0) {
38+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
39+
}
40+
return addr;
41+
}
42+
43+
STATIC mp_uint_t get_write_addr(mp_obj_t addr_o, uint align) {
44+
mp_uint_t addr = mp_obj_int_get_truncated(addr_o);
45+
if ((addr & (align - 1)) != 0) {
46+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
47+
}
48+
return addr;
49+
}
50+
51+
typedef struct _machine_mem_obj_t {
52+
mp_obj_base_t base;
53+
unsigned elem_size; // in bytes
54+
} machine_mem_obj_t;
55+
56+
STATIC void machine_mem_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
57+
(void)kind;
58+
machine_mem_obj_t *self = self_in;
59+
mp_printf(print, "<%u-bit memory>", 8 * self->elem_size);
60+
}
61+
62+
STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
63+
// TODO support slice index to read/write multiple values at once
64+
machine_mem_obj_t *self = self_in;
65+
if (value == MP_OBJ_NULL) {
66+
// delete
67+
return MP_OBJ_NULL; // op not supported
68+
} else if (value == MP_OBJ_SENTINEL) {
69+
// load
70+
mp_uint_t addr = get_read_addr(index, self->elem_size);
71+
uint32_t val;
72+
switch (self->elem_size) {
73+
case 1: val = (*(uint8_t*)addr); break;
74+
case 2: val = (*(uint16_t*)addr); break;
75+
default: val = (*(uint32_t*)addr); break;
76+
}
77+
return mp_obj_new_int(val);
78+
} else {
79+
// store
80+
mp_uint_t addr = get_write_addr(index, self->elem_size);
81+
uint32_t val = mp_obj_get_int(value);
82+
switch (self->elem_size) {
83+
case 1: (*(uint8_t*)addr) = val; break;
84+
case 2: (*(uint16_t*)addr) = val; break;
85+
default: (*(uint32_t*)addr) = val; break;
86+
}
87+
return mp_const_none;
88+
}
89+
}
90+
91+
STATIC const mp_obj_type_t machine_mem_type = {
92+
{ &mp_type_type },
93+
.name = MP_QSTR_mem,
94+
.print = machine_mem_print,
95+
.subscr = machine_mem_subscr,
96+
};
97+
98+
STATIC const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1};
99+
STATIC const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2};
100+
STATIC const machine_mem_obj_t machine_mem32_obj = {{&machine_mem_type}, 4};
101+
102+
STATIC const mp_map_elem_t machine_module_globals_table[] = {
103+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_machine) },
104+
105+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem8), (mp_obj_t)&machine_mem8_obj },
106+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem16), (mp_obj_t)&machine_mem16_obj },
107+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem32), (mp_obj_t)&machine_mem32_obj },
108+
};
109+
110+
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
111+
112+
const mp_obj_module_t mp_module_machine = {
113+
.base = { &mp_type_module },
114+
.name = MP_QSTR_machine,
115+
.globals = (mp_obj_dict_t*)&machine_module_globals,
116+
};
117+
118+
#endif // MICROPY_PY_MACHINE

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,6 @@ extern const mp_obj_module_t mp_module_ure;
101101
extern const mp_obj_module_t mp_module_uheapq;
102102
extern const mp_obj_module_t mp_module_uhashlib;
103103
extern const mp_obj_module_t mp_module_ubinascii;
104+
extern const mp_obj_module_t mp_module_machine;
104105

105106
#endif // __MICROPY_INCLUDED_PY_BUILTIN_H__

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ typedef double mp_float_t;
620620
#define MICROPY_PY_UBINASCII (0)
621621
#endif
622622

623+
#ifndef MICROPY_PY_MACHINE
624+
#define MICROPY_PY_MACHINE (0)
625+
#endif
626+
623627
/*****************************************************************************/
624628
/* Hooks for a port to add builtins */
625629

py/objmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
183183
#if MICROPY_PY_UBINASCII
184184
{ MP_OBJ_NEW_QSTR(MP_QSTR_ubinascii), (mp_obj_t)&mp_module_ubinascii },
185185
#endif
186+
#if MICROPY_PY_MACHINE
187+
{ MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine },
188+
#endif
186189

187190
// extra builtin modules as defined by a port
188191
MICROPY_PORT_BUILTIN_MODULES

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ PY_O_BASENAME = \
118118
../extmod/moduheapq.o \
119119
../extmod/moduhashlib.o \
120120
../extmod/modubinascii.o \
121+
../extmod/modmachine.o \
121122

122123
# prepend the build destination prefix to the py object files
123124
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))

py/qstrdefs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,11 @@ Q(sha256)
583583
Q(ubinascii)
584584
Q(hexlify)
585585
#endif
586+
587+
#if MICROPY_PY_MACHINE
588+
Q(machine)
589+
Q(mem)
590+
Q(mem8)
591+
Q(mem16)
592+
Q(mem32)
593+
#endif

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
#define MICROPY_PY_UHEAPQ (1)
8989
#define MICROPY_PY_UHASHLIB (1)
9090
#define MICROPY_PY_UBINASCII (1)
91+
#define MICROPY_PY_MACHINE (1)
9192

9293
// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
9394
// names in exception messages (may require more RAM).

0 commit comments

Comments
 (0)