Skip to content

Commit 9da16ee

Browse files
committed
pbio/include/pbdrv/cache.h: Rename EV3-specific macros
This makes it much more clear that this functionality is specific to how Pybricks sets up the EV3.
1 parent 39c1f48 commit 9da16ee

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

lib/pbio/drv/block_device/block_device_ev3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,8 @@ pbio_error_t pbdrv_adc_get_ch(uint8_t ch, uint16_t *value) {
755755
uint16_t a, b;
756756
do {
757757
// Values for the requested channel are received several samples later.
758-
a = PBDRV_UNCACHED(channel_data[ch + PBDRV_ADC_EV3_NUM_DELAY_SAMPLES]);
759-
b = PBDRV_UNCACHED(channel_data[ch + PBDRV_ADC_EV3_NUM_DELAY_SAMPLES]);
758+
a = PBDRV_EV3_UNCACHED(channel_data[ch + PBDRV_ADC_EV3_NUM_DELAY_SAMPLES]);
759+
b = PBDRV_EV3_UNCACHED(channel_data[ch + PBDRV_ADC_EV3_NUM_DELAY_SAMPLES]);
760760
} while (a != b);
761761

762762
// Mask the data to 10 bits

lib/pbio/drv/usb/usb_ev3.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static uint32_t cppi_linking_ram[CPPI_DESC_COUNT];
284284

285285
// Fill in the CPPI DMA descriptor to receive a packet
286286
static void usb_setup_rx_dma_desc(void) {
287-
PBDRV_UNCACHED(cppi_descriptors[CPPI_DESC_RX]) = (usb_cppi_hpd_t) {
287+
PBDRV_EV3_UNCACHED(cppi_descriptors[CPPI_DESC_RX]) = (usb_cppi_hpd_t) {
288288
.word0 = {
289289
.hostPktType = CPPI_HOST_PACKET_DESCRIPTOR_TYPE,
290290
},
@@ -308,7 +308,7 @@ static void usb_setup_rx_dma_desc(void) {
308308

309309
// Fill in the CPPI DMA descriptor to send a packet
310310
static void usb_setup_tx_dma_desc(int tx_type, void *buf, uint32_t buf_len) {
311-
PBDRV_UNCACHED(cppi_descriptors[tx_type]) = (usb_cppi_hpd_t) {
311+
PBDRV_EV3_UNCACHED(cppi_descriptors[tx_type]) = (usb_cppi_hpd_t) {
312312
.word0 = {
313313
.hostPktType = CPPI_HOST_PACKET_DESCRIPTOR_TYPE,
314314
.pktLength = buf_len,
@@ -905,7 +905,7 @@ static pbio_error_t pbdrv_usb_ev3_process_thread(pbio_os_state_t *state, void *c
905905
// (which is technically allowed by the as-if rule).
906906
pbdrv_compiler_memory_barrier();
907907

908-
uint32_t usb_rx_sz = PBDRV_UNCACHED(cppi_descriptors[CPPI_DESC_RX].word0).pktLength;
908+
uint32_t usb_rx_sz = PBDRV_EV3_UNCACHED(cppi_descriptors[CPPI_DESC_RX].word0).pktLength;
909909
pbio_pybricks_error_t result;
910910
bool usb_send_response = false;
911911
// Skip empty commands, or commands sent when previous response is still busy

lib/pbio/include/pbdrv/cache.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ void pbdrv_cache_prepare_before_dma(const void *buf, size_t sz);
1414
// by DMA peripherals. This invalidates the relevant cache lines.
1515
void pbdrv_cache_prepare_after_dma(const void *buf, size_t sz);
1616

17+
// The EV3 MMU is configured to additionally map
18+
// VA 0xDxxxxxxx onto PA 0xCxxxxxxx, covering the DDR RAM region.
19+
// The 0xD alias is mapped as non-cacheable and non-bufferable
20+
// and can be used when data needs to be shared with bus-mastering
21+
// hardware peripherals. The 0xD region is unused in the physical
22+
// address map and lies at a convenient offset from the 0xC region.
23+
// The 0xC region, which is the region typically used to access RAM,
24+
// is cacheable and bufferable.
25+
#define PBDRV_EV3_UNCACHED_OFFSET 0x10000000
26+
1727
// Accesses a variable via the uncached memory alias
18-
#define PBDRV_UNCACHED(x) (*(volatile __typeof__(x) *)((uint32_t)(&(x)) + 0x10000000))
28+
#define PBDRV_EV3_UNCACHED(x) (*(volatile __typeof__(x) *)((uintptr_t)(&(x)) + PBDRV_EV3_UNCACHED_OFFSET))
1929

2030
// Gets the address of the uncached memory alias of a variable
21-
#define PBDRV_UNCACHED_ADDR(x) ((__typeof__(x) *)((uint32_t)(&(x)) + 0x10000000))
31+
#define PBDRV_EV3_UNCACHED_ADDR(x) ((__typeof__(x) *)((uintptr_t)(&(x)) + PBDRV_EV3_UNCACHED_OFFSET))
2232

2333
// Cache line size
2434
#define PBDRV_CACHE_LINE_SZ 32

lib/pbio/platform/ev3/platform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static void mmu_init(void) {
727727
// Off-chip main DDR RAM, uncacheable mirror @ 0xD0000000
728728
for (unsigned int i = 0; i < SYSTEM_RAM_SZ_MB; i++) {
729729
uint32_t addr_phys = 0xC0000000 + i * MMU_SECTION_SZ;
730-
uint32_t addr_virt = 0xD0000000 + i * MMU_SECTION_SZ;
730+
uint32_t addr_virt = addr_phys + PBDRV_EV3_UNCACHED_OFFSET;
731731
l1_page_table[addr_virt >> MMU_SECTION_SHIFT] = MMU_L1_SECTION(addr_phys, 0, 1, 0, 0);
732732
}
733733

0 commit comments

Comments
 (0)