forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
66b3ecc
8334: RP2040 lacks raw memory map access
ab59e36
Correct problems found by CI
1648a3d
Fix IO register stores of multiple words
1e17831
review update, add RP2040 example
65fc79e
backout remaining changes to py
79edcbe
second instance of misspelled 'unaligned'
0e0941d
Review changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// No memorymap module functions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.