diff --git a/ports/nrf/common-hal/memorymap/AddressRange.c b/ports/nrf/common-hal/memorymap/AddressRange.c new file mode 100644 index 0000000000..eaef202d0d --- /dev/null +++ b/ports/nrf/common-hal/memorymap/AddressRange.c @@ -0,0 +1,136 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * 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 +#include "nrf.h" + +#include "shared-bindings/memorymap/AddressRange.h" + +#include "py/runtime.h" + + +#ifdef NRF51_SERIES +size_t allow_ranges[][2] = { + // FLASH + {0x00000000, 0x00040000}, + // FICR & UICR ranges + {0x10000000, 0x10002000}, + // RAM + {0x20000000, 0x20010000}, + // PERIPHERALS + {0x40000000, 0x60000000} +}; +#elif defined NRF52_SERIES +size_t allow_ranges[][2] = { + // FLASH + {0x00000000, 0x00100000}, + // FICR & UICR ranges + {0x10000000, 0x10002000}, + // RAM + {0x20000000, 0x20040000}, + // PERIPHERALS + {0x40000000, 0x60000000} +}; +#elif defined NRF53_SERIES +size_t allow_ranges[][2] = { + // FLASH + {0x00000000, 0x00100000}, + // FICR & UICR ranges + {0x00FF0000, 0x01000000}, + // RAM + {0x20000000, 0x20080000}, + // PERIPHERALS + {0x40000000, 0x60000000}, + {0xE0000000, 0xE0100000} +}; +#else +#error "Unsupported nRF variant" +#endif + +void common_hal_memorymap_addressrange_construct(memorymap_addressrange_obj_t *self, uint8_t *start_address, size_t length) { + bool allowed = false; + for (size_t i = 0; i < MP_ARRAY_SIZE(allow_ranges); i++) { + uint8_t *allowed_start = (uint8_t *)allow_ranges[i][0]; + uint8_t *allowed_end = (uint8_t *)allow_ranges[i][1]; + if (allowed_start <= start_address && + (start_address + length) <= allowed_end) { + allowed = true; + break; + } + } + + if (!allowed) { + mp_raise_ValueError(translate("Address range not allowed")); + } + + self->start_address = start_address; + self->len = length; +} + +uint32_t common_hal_memorymap_addressrange_get_length(const memorymap_addressrange_obj_t *self) { + return self->len; +} + + +bool common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_obj_t *self, + uint32_t start_index, uint8_t *values, uint32_t len) { + uint8_t *address = self->start_address + start_index; + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" + if (len == 1) { + *((uint8_t *)address) = values[0]; + } else if (len == sizeof(uint16_t) && (((size_t)address) % sizeof(uint16_t)) == 0) { + *((uint16_t *)address) = ((uint16_t *)values)[0]; + } else if (len == sizeof(uint32_t) && (((size_t)address) % sizeof(uint32_t)) == 0) { + *((uint32_t *)address) = ((uint32_t *)values)[0]; + } else if (len == sizeof(uint64_t) && (((size_t)address) % sizeof(uint64_t)) == 0) { + *((uint64_t *)address) = ((uint64_t *)values)[0]; + } else { + memcpy(address, values, len); + } + #pragma GCC diagnostic pop + + return true; +} + +void common_hal_memorymap_addressrange_get_bytes(const memorymap_addressrange_obj_t *self, + uint32_t start_index, uint32_t len, uint8_t *values) { + uint8_t *address = self->start_address + start_index; + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" + if (len == 1) { + values[0] = *((uint8_t *)address); + } else if (len == sizeof(uint16_t) && (((size_t)address) % sizeof(uint16_t)) == 0) { + ((uint16_t *)values)[0] = *((uint16_t *)address); + } else if (len == sizeof(uint32_t) && (((size_t)address) % sizeof(uint32_t)) == 0) { + ((uint32_t *)values)[0] = *((uint32_t *)address); + } else if (len == sizeof(uint64_t) && (((size_t)address) % sizeof(uint64_t)) == 0) { + ((uint64_t *)values)[0] = *((uint64_t *)address); + } else { + memcpy(values, address, len); + } + #pragma GCC diagnostic pop +} diff --git a/ports/nrf/common-hal/memorymap/AddressRange.h b/ports/nrf/common-hal/memorymap/AddressRange.h new file mode 100644 index 0000000000..20f59f3c73 --- /dev/null +++ b/ports/nrf/common-hal/memorymap/AddressRange.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * 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 MICROPY_INCLUDED_NRF_COMMON_HAL_MEMORYMAP_ADDRESSRANGE_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_MEMORYMAP_ADDRESSRANGE_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t *start_address; + size_t len; +} memorymap_addressrange_obj_t; + +#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_MEMORYMAP_ADDRESSRANGE_H diff --git a/ports/nrf/common-hal/memorymap/__init__.c b/ports/nrf/common-hal/memorymap/__init__.c new file mode 100644 index 0000000000..c15b17f451 --- /dev/null +++ b/ports/nrf/common-hal/memorymap/__init__.c @@ -0,0 +1 @@ +// No memorymap module functions. diff --git a/ports/nrf/mpconfigport.mk b/ports/nrf/mpconfigport.mk index 04c4f3bce6..7973453c2f 100644 --- a/ports/nrf/mpconfigport.mk +++ b/ports/nrf/mpconfigport.mk @@ -47,6 +47,7 @@ CIRCUITPY_SERIAL_BLE ?= 1 CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 1 + # nRF52840-specific ifeq ($(MCU_CHIP),nrf52840) @@ -56,6 +57,7 @@ MCU_SUB_VARIANT = nrf52840 # Fits on nrf52840 but space is tight on nrf52833. CIRCUITPY_AESIO ?= 1 +CIRCUITPY_MEMORYMAP ?= 1 CIRCUITPY_RGBMATRIX ?= 1 CIRCUITPY_FRAMEBUFFERIO ?= 1