From 1c3150fa382689bdce1f4d483dff033328dea0a8 Mon Sep 17 00:00:00 2001
From: Jeremy Ellis <keyfreemusic@gmail.com>
Date: Thu, 23 Jun 2022 13:08:29 -0700
Subject: [PATCH 1/2] update camera.cpp for M4 Core

---
 libraries/Camera/src/camera.cpp | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/libraries/Camera/src/camera.cpp b/libraries/Camera/src/camera.cpp
index e57b4af01..0a96ac35f 100644
--- a/libraries/Camera/src/camera.cpp
+++ b/libraries/Camera/src/camera.cpp
@@ -28,7 +28,7 @@
 #define ALIGN_PTR(p,a)   ((p & (a-1)) ?(((uintptr_t)p + a) & ~(uintptr_t)(a-1)) : p)
 
 // Include all image sensor drivers here.
-#ifdef ARDUINO_PORTENTA_H7_M7
+#if defined (ARDUINO_PORTENTA_H7_M7) || defined (ARDUINO_PORTENTA_H7_M4)
 
 #define DCMI_TIM                    (TIM1)
 #define DCMI_TIM_PIN                (GPIO_PIN_1)
@@ -65,7 +65,7 @@ arduino::MbedI2C CameraWire(I2C_SDA2, I2C_SCL2);
 
 // DCMI GPIO pins struct
 static const struct { GPIO_TypeDef *port; uint16_t pin; } dcmi_pins[] = {
-#ifdef ARDUINO_PORTENTA_H7_M7
+#if defined (ARDUINO_PORTENTA_H7_M7) || defined (ARDUINO_PORTENTA_H7_M4)
     {GPIOA,     GPIO_PIN_4  },
     {GPIOA,     GPIO_PIN_6  },
     {GPIOI,     GPIO_PIN_4  },
@@ -146,7 +146,7 @@ void HAL_DCMI_MspInit(DCMI_HandleTypeDef *hdcmi)
     hgpio.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
     hgpio.Alternate = GPIO_AF13_DCMI;
 
-#ifdef ARDUINO_PORTENTA_H7_M7
+#if defined (ARDUINO_PORTENTA_H7_M7) || defined (ARDUINO_PORTENTA_H7_M4)
     /* Enable GPIO clocks */
     __HAL_RCC_GPIOA_CLK_ENABLE();
     __HAL_RCC_GPIOH_CLK_ENABLE();
@@ -365,7 +365,7 @@ Camera::Camera(ImageSensor &sensor) :
 
 int Camera::reset()
 {
-#ifdef ARDUINO_PORTENTA_H7_M7
+#if defined (ARDUINO_PORTENTA_H7_M7) || defined (ARDUINO_PORTENTA_H7_M4)
     // Reset sensor.
     digitalWrite(PC_13, LOW);
     HAL_Delay(10);
@@ -605,9 +605,10 @@ int Camera::grabFrame(FrameBuffer &fb, uint32_t timeout)
 
     HAL_DCMI_Suspend(&hdcmi);
 
-    // Invalidate buffer after DMA transfer.
-    SCB_InvalidateDCache_by_Addr((uint32_t*) framebuffer, framesize);
-
+    #ifndef ARDUINO_PORTENTA_H7_M4  // do not invalidate if Portenta M4 Core
+       // Invalidate buffer after DMA transfer.
+       SCB_InvalidateDCache_by_Addr((uint32_t*) framebuffer, framesize);
+    #endif
     return 0;
 }
 

From 89f7ab0315e4b071936cc8cad2dee7177c8bcc5a Mon Sep 17 00:00:00 2001
From: Jeremy Ellis <keyfreemusic@gmail.com>
Date: Mon, 27 Jun 2022 08:56:13 -0700
Subject: [PATCH 2/2] Update camera.cpp

---
 libraries/Camera/src/camera.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libraries/Camera/src/camera.cpp b/libraries/Camera/src/camera.cpp
index 0a96ac35f..54dc71ce6 100644
--- a/libraries/Camera/src/camera.cpp
+++ b/libraries/Camera/src/camera.cpp
@@ -605,7 +605,7 @@ int Camera::grabFrame(FrameBuffer &fb, uint32_t timeout)
 
     HAL_DCMI_Suspend(&hdcmi);
 
-    #ifndef ARDUINO_PORTENTA_H7_M4  // do not invalidate if Portenta M4 Core
+    #if defined(__CORTEX_M7)  // only invalidate buffer for Cortex M7
        // Invalidate buffer after DMA transfer.
        SCB_InvalidateDCache_by_Addr((uint32_t*) framebuffer, framesize);
     #endif