Skip to content

Add memorymap support to RP2 port #8357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ msgid ""
msgstr ""

#: ports/atmel-samd/common-hal/alarm/__init__.c
#: ports/atmel-samd/common-hal/audiobusio/I2SOut.c
#: ports/cxd56/common-hal/analogio/AnalogOut.c ports/cxd56/common-hal/rtc/RTC.c
#: ports/espressif/common-hal/audiobusio/I2SOut.c
#: ports/espressif/common-hal/rtc/RTC.c
#: ports/mimxrt10xx/common-hal/analogio/AnalogOut.c
#: ports/mimxrt10xx/common-hal/rtc/RTC.c ports/nrf/common-hal/alarm/__init__.c
#: ports/nrf/common-hal/analogio/AnalogOut.c ports/nrf/common-hal/rtc/RTC.c
#: ports/nrf/common-hal/analogio/AnalogOut.c
#: ports/nrf/common-hal/audiobusio/I2SOut.c ports/nrf/common-hal/rtc/RTC.c
#: ports/raspberrypi/common-hal/alarm/__init__.c
#: ports/raspberrypi/common-hal/analogio/AnalogOut.c
#: ports/raspberrypi/common-hal/audiobusio/I2SOut.c
#: ports/raspberrypi/common-hal/rtc/RTC.c ports/stm/common-hal/alarm/__init__.c
#: ports/stm/common-hal/canio/Listener.c ports/stm/common-hal/rtc/RTC.c
#: shared-bindings/audiobusio/I2SOut.c shared-bindings/audiobusio/PDMIn.c
Expand Down Expand Up @@ -458,9 +462,14 @@ msgstr ""

#: ports/espressif/common-hal/memorymap/AddressRange.c
#: ports/nrf/common-hal/memorymap/AddressRange.c
#: ports/raspberrypi/common-hal/memorymap/AddressRange.c
msgid "Address range not allowed"
msgstr ""

#: shared-bindings/memorymap/AddressRange.c
msgid "Address range wraps around"
msgstr ""

#: ports/espressif/common-hal/canio/CAN.c
msgid "All CAN peripherals are in use"
msgstr ""
Expand Down Expand Up @@ -2127,6 +2136,10 @@ msgstr ""
msgid "UUID value is not str, int or byte buffer"
msgstr ""

#: ports/raspberrypi/common-hal/memorymap/AddressRange.c
msgid "Unable to access unaligned IO register"
msgstr ""

#: ports/atmel-samd/common-hal/audiobusio/I2SOut.c
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
#: ports/raspberrypi/common-hal/audiobusio/I2SOut.c
Expand Down Expand Up @@ -2180,14 +2193,14 @@ msgstr ""
msgid "Unable to start mDNS query"
msgstr ""

#: shared-bindings/memorymap/AddressRange.c
msgid "Unable to write to address."
msgstr ""

#: shared-bindings/nvm/ByteArray.c
msgid "Unable to write to nvm."
msgstr ""

#: ports/raspberrypi/common-hal/memorymap/AddressRange.c
msgid "Unable to write to read-only memory"
msgstr ""

#: shared-bindings/alarm/SleepMemory.c
msgid "Unable to write to sleep_memory."
msgstr ""
Expand Down Expand Up @@ -2948,7 +2961,7 @@ msgstr ""
msgid "empty file"
msgstr ""

#: extmod/moduasyncio.c extmod/moduheapq.c extmod/modutimeq.c
#: extmod/moduasyncio.c extmod/moduheapq.c
msgid "empty heap"
msgstr ""

Expand Down Expand Up @@ -3899,10 +3912,6 @@ msgstr ""
msgid "pull masks conflict with direction masks"
msgstr ""

#: extmod/modutimeq.c
msgid "queue overflow"
msgstr ""

#: py/parse.c
msgid "raw f-strings are not supported"
msgstr ""
Expand Down
4 changes: 1 addition & 3 deletions ports/espressif/common-hal/memorymap/AddressRange.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ size_t common_hal_memorymap_addressrange_get_length(const memorymap_addressrange
return self->len;
}

bool common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_obj_t *self,
void common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_obj_t *self,
size_t start_index, uint8_t *values, size_t len) {
uint8_t *address = self->start_address + start_index;
#pragma GCC diagnostic push
Expand All @@ -83,8 +83,6 @@ bool common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_ob
memcpy(address, values, len);
}
#pragma GCC diagnostic pop

return true;
}

void common_hal_memorymap_addressrange_get_bytes(const memorymap_addressrange_obj_t *self,
Expand Down
10 changes: 4 additions & 6 deletions ports/nrf/common-hal/memorymap/AddressRange.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ void common_hal_memorymap_addressrange_construct(memorymap_addressrange_obj_t *s
self->len = length;
}

uint32_t common_hal_memorymap_addressrange_get_length(const memorymap_addressrange_obj_t *self) {
size_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) {
void common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_obj_t *self,
size_t start_index, uint8_t *values, size_t len) {
uint8_t *address = self->start_address + start_index;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
Expand All @@ -112,12 +112,10 @@ bool common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_ob
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) {
size_t start_index, size_t len, uint8_t *values) {
uint8_t *address = self->start_address + start_index;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
Expand Down
136 changes: 136 additions & 0 deletions ports/raspberrypi/common-hal/memorymap/AddressRange.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
* Copyright (c) 2023 Bob Abeles
*
* 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 <string.h>

#include "shared-bindings/memorymap/AddressRange.h"

#include "py/runtime.h"

#include "hardware/regs/addressmap.h"

// RP2 address map ranges, must be arranged in order by ascending start address
addressmap_rp2_range_t rp2_ranges[] = {
{(uint8_t *)ROM_BASE, 0x00004000, ROM}, // boot ROM
{(uint8_t *)XIP_BASE, 0x00100000, XIP}, // XIP normal cache operation
{(uint8_t *)XIP_NOALLOC_BASE, 0x00100000, XIP}, // XIP check for hit, no update on miss
{(uint8_t *)XIP_NOCACHE_BASE, 0x00100000, XIP}, // XIP don't check for hit, no update on miss
{(uint8_t *)XIP_NOCACHE_NOALLOC_BASE, 0x00100000, XIP}, // XIP bypass cache completely
{(uint8_t *)XIP_CTRL_BASE, 0x00004000, IO}, // XIP control registers
{(uint8_t *)XIP_SRAM_BASE, 0x00004000, SRAM}, // XIP SRAM 16KB XIP cache
{(uint8_t *)XIP_SSI_BASE, 0x00004000, IO}, // XIP SSI registers
{(uint8_t *)SRAM_BASE, 0x00042000, SRAM}, // SRAM 256KB striped plus 16KB contiguous
{(uint8_t *)SRAM0_BASE, 0x00040000, SRAM}, // SRAM0 to SRAM3 256KB non-striped
{(uint8_t *)SYSINFO_BASE, 0x00070000, IO}, // APB peripherals
{(uint8_t *)DMA_BASE, 0x00004000, IO}, // DMA registers
{(uint8_t *)USBCTRL_DPRAM_BASE, 0x00001000, SRAM}, // USB DPSRAM 4KB
{(uint8_t *)USBCTRL_REGS_BASE, 0x00004000, IO}, // USB registers
{(uint8_t *)PIO0_BASE, 0x00004000, IO}, // PIO0 registers
{(uint8_t *)PIO1_BASE, 0x00004000, IO}, // PIO1 registers
{(uint8_t *)SIO_BASE, 0x00001000, IO}, // SIO registers, no aliases
{(uint8_t *)PPB_BASE, 0x00004000, IO} // PPB registers
};

void common_hal_memorymap_addressrange_construct(memorymap_addressrange_obj_t *self,
uint8_t *start_address, size_t length) {
for (size_t i = 0; i < MP_ARRAY_SIZE(rp2_ranges); i++) {
if (start_address <= rp2_ranges[i].start_address) {
uint8_t *range_end_address = rp2_ranges[i].start_address + rp2_ranges[i].len - 1;
uint8_t *end_address = start_address + length - 1;
if (start_address > range_end_address || end_address > range_end_address) {
break;
}
self->start_address = start_address;
self->len = length;
self->type = rp2_ranges[i].type;
return;
}
}

mp_raise_ValueError(translate("Address range not allowed"));
}

size_t common_hal_memorymap_addressrange_get_length(const memorymap_addressrange_obj_t *self) {
return self->len;
}

void common_hal_memorymap_addressrange_set_bytes(const memorymap_addressrange_obj_t *self,
size_t start_index, uint8_t *values, size_t len) {
uint8_t *dest_addr = self->start_address + start_index;
switch (self->type) {
case SRAM:
// Writes to SRAM may be arbitrary length and alignment. We use memcpy() which
// may optimize aligned writes depending on CIRCUITPY_FULL_BUILD of the CP build.
memcpy(dest_addr, values, len);
break;
case IO:
if ((size_t)dest_addr & 0x03 || len & 0x03) {
// Unaligned access or unaligned length not supported by RP2 for IO registers
mp_raise_RuntimeError(translate("Unable to access unaligned IO register"));
} else {
// Aligned access and length, use 32-bit writes
uint32_t *dest_addr32 = (uint32_t *)dest_addr;
size_t access_count = len >> 2;
for (size_t i = 0; i < access_count; i++) {
*dest_addr32++ = ((uint32_t *)values)[i];
}
}
break;
case XIP:
case ROM:
// XIP and ROM are read-only
mp_raise_RuntimeError(translate("Unable to write to read-only memory"));
break;
}
}

void common_hal_memorymap_addressrange_get_bytes(const memorymap_addressrange_obj_t *self,
size_t start_index, size_t len, uint8_t *values) {
uint8_t *src_addr = self->start_address + start_index;
switch (self->type) {
case SRAM:
case XIP:
case ROM:
// Reads from these sources may be arbitrary length and alignment. We use memcpy()
// which may optimize aligned writes depending on CIRCUITPY_FULL_BUILD of the CP build.
memcpy(values, src_addr, len);
break;
case IO:
if ((size_t)src_addr & 0x03 || len & 0x03) {
// Unaligned access or unaligned length not supported by RP2 for IO registers
mp_raise_RuntimeError(translate("Unable to access unaligned IO register"));
} else {
// Aligned access and length, use 32-bit reads
uint32_t *src_addr32 = (uint32_t *)src_addr;
size_t access_count = len >> 2;
for (size_t i = 0; i < access_count; i++) {
((uint32_t *)values)[i] = *src_addr32++;
}
}
break;
}
}
49 changes: 49 additions & 0 deletions ports/raspberrypi/common-hal/memorymap/AddressRange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
* Copyright (c) 2023 Bob Abeles
*
* 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_RASPBERRYPI_COMMON_HAL_MEMORYMAP_ADDRESSRANGE_H
#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_MEMORYMAP_ADDRESSRANGE_H

#include "py/obj.h"

// depending on the section memory type, different access methods and rules apply
typedef enum { SRAM, ROM, XIP, IO } memorymap_rp2_section_t;

typedef struct {
mp_obj_base_t base;
uint8_t *start_address;
size_t len;
memorymap_rp2_section_t type;
} memorymap_addressrange_obj_t;

typedef struct {
uint8_t *start_address;
size_t len;
memorymap_rp2_section_t type;
} addressmap_rp2_range_t;

#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_MEMORYMAP_ADDRESSRANGE_H
1 change: 1 addition & 0 deletions ports/raspberrypi/common-hal/memorymap/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// No memorymap module functions.
1 change: 1 addition & 0 deletions ports/raspberrypi/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CIRCUITPY_FULL_BUILD ?= 1
CIRCUITPY_AUDIOMP3 ?= 1
CIRCUITPY_BITOPS ?= 1
CIRCUITPY_IMAGECAPTURE ?= 1
CIRCUITPY_MEMORYMAP ?= 1
CIRCUITPY_PWMIO ?= 1
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_DISPLAYIO)
CIRCUITPY_ROTARYIO ?= 1
Expand Down
Loading