Browse Source

extmod/modplatform: Expose CPU features/extensions.

This adds the ability to expose CPU-specific features/extensions to
scripts when the `platform` module is compiled in, by implementing
`platform.processor()`.   Right now this is only available on
bare-metal RV32 and RV64, on other CPUs it will return an empty string.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
pull/15268/head
Alessandro Gatti 5 months ago
parent
commit
2e24bbae21
  1. 6
      docs/library/platform.rst
  2. 34
      extmod/modplatform.c

6
docs/library/platform.rst

@ -36,3 +36,9 @@ Functions
Returns a tuple of strings *(lib, version)*, where *lib* is the name of the
libc that MicroPython is linked to, and *version* the corresponding version
of this libc.
.. function:: processor()
Returns a string with a detailed name of the processor, if one is available.
If no name for the processor is known, it will return an empty string
instead.

34
extmod/modplatform.c

@ -61,11 +61,45 @@ static mp_obj_t platform_libc_ver(size_t n_args, const mp_obj_t *pos_args, mp_ma
}
static MP_DEFINE_CONST_FUN_OBJ_KW(platform_libc_ver_obj, 0, platform_libc_ver);
static mp_obj_t platform_processor(void) {
#if defined(__riscv) && (__riscv_xlen <= 64) && !defined(__linux__)
uintptr_t misa_csr = 0;
// Load the MISA CSR directly.
__asm volatile (
"csrr %0, misa \n"
: "+r" (misa_csr)
:
:
);
char processor_buffer[31] = { // "RV32"/"RV64" + up to 26 chars.
#if (__riscv_xlen < 64)
"RV32"
#else
"RV64"
#endif
};
mp_uint_t offset = 4;
for (mp_uint_t bit = 0; bit < 26; bit++) {
if (misa_csr & (1U << bit)) {
processor_buffer[offset++] = 'A' + bit;
}
}
return mp_obj_new_str(processor_buffer, offset);
#else
return MP_OBJ_NEW_QSTR(MP_QSTR_);
#endif
}
static MP_DEFINE_CONST_FUN_OBJ_0(platform_processor_obj, platform_processor);
static const mp_rom_map_elem_t modplatform_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_platform) },
{ MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_platform_obj) },
{ MP_ROM_QSTR(MP_QSTR_python_compiler), MP_ROM_PTR(&platform_python_compiler_obj) },
{ MP_ROM_QSTR(MP_QSTR_libc_ver), MP_ROM_PTR(&platform_libc_ver_obj) },
{ MP_ROM_QSTR(MP_QSTR_processor), MP_ROM_PTR(&platform_processor_obj) },
};
static MP_DEFINE_CONST_DICT(modplatform_globals, modplatform_globals_table);

Loading…
Cancel
Save