You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2024/09/10 07:21:58 UTC

(mynewt-core) 01/04: mcu/lpc55: Enable MPU for USB_SRAM access

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit ae4edad7d03267b97791ca3f40188f30568eb3e1
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Thu Aug 22 10:30:06 2024 +0200

    mcu/lpc55: Enable MPU for USB_SRAM access
    
    USB_SRAM by default requires aligned access as
    it is mapped same way peripherals are accessed.
    
    To overcome this limitation (needed by TinyUSB)
    MPU has to be enabled that configures USB_SRAM
    so there is no alignment limitations.
    
    Without this TinyUSB implementation will not work.
    
    Signed-off-by: Jerzy Kasenberg <je...@codecoup.pl>
---
 hw/mcu/nxp/lpc55xx/src/hal_system_init.c | 37 ++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/hw/mcu/nxp/lpc55xx/src/hal_system_init.c b/hw/mcu/nxp/lpc55xx/src/hal_system_init.c
index 78a3c5b4c..4d7622d74 100644
--- a/hw/mcu/nxp/lpc55xx/src/hal_system_init.c
+++ b/hw/mcu/nxp/lpc55xx/src/hal_system_init.c
@@ -19,6 +19,40 @@
 
 #include "os/mynewt.h"
 #include <hal/hal_system.h>
+#include <mpu_armv8.h>
+
+static int
+mpu_add_region(uint32_t rnr, uint32_t start, size_t size, uint8_t attr_ix, uint8_t ro, uint8_t xn)
+{
+    if (rnr < 8) {
+        ARM_MPU_SetRegion(rnr, ARM_MPU_RBAR(start, 1, ro, 1, xn), ARM_MPU_RLAR((start + size - 1), attr_ix));
+        rnr++;
+    }
+    return rnr;
+}
+
+static void
+mpu_init(void)
+{
+    int nrn = 0;
+
+    ARM_MPU_Disable();
+    /* Set Attr 0 - normal memory */
+    ARM_MPU_SetMemAttr(0, ARM_MPU_ATTR(
+        ARM_MPU_ATTR_MEMORY_(0, 1, 1, 1),   /* Outer Write-Back transient with read and write allocate */
+        ARM_MPU_ATTR_MEMORY_(0, 0, 1, 1))); /* Inner Write-Through transient with read and write allocate */
+
+    /*
+     * Add USB RAM, this is required to allow unaligned access to this RAM that is used by
+     * USB1 (High-speed) for storing data
+     */
+    mpu_add_region(nrn, FSL_FEATURE_USB_USB_RAM_BASE_ADDRESS, FSL_FEATURE_USB_USB_RAM, 0, 0, 1);
+
+    /* Enable MPU with default map */
+    ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
+    __DSB();
+    __ISB();
+}
 
 void
 hal_system_init(void)
@@ -27,5 +61,8 @@ hal_system_init(void)
 
     /* Relocate the vector table */
     NVIC_Relocate();
+
+    /* MPU required to access USB SRAM needed by TinyUSB */
+    mpu_init();
 }