From 648d87ea98ec39d5745d36a0b5c5078cd9491211 Mon Sep 17 00:00:00 2001
From: Loek Le Blansch <loek@pipeframe.xyz>
Date: Sun, 23 Jun 2024 13:31:49 +0200
Subject: place rp2040 back in pbdrv

---
 lib/pbdrv/drv/arduino/include.cmake |  6 +-----
 lib/pbdrv/drv/arduino/index.dox     | 20 +++++++++++-------
 lib/pbdrv/drv/arduino/mod.cpp       | 42 +++++++++++--------------------------
 3 files changed, 25 insertions(+), 43 deletions(-)

(limited to 'lib/pbdrv/drv/arduino')

diff --git a/lib/pbdrv/drv/arduino/include.cmake b/lib/pbdrv/drv/arduino/include.cmake
index 1e2ff08..520bfc1 100644
--- a/lib/pbdrv/drv/arduino/include.cmake
+++ b/lib/pbdrv/drv/arduino/include.cmake
@@ -3,9 +3,5 @@ if(NOT DEFINED ARDUINO)
 endif()
 
 target_sources(pbdrv-mod PRIVATE "${CMAKE_CURRENT_LIST_DIR}/mod.cpp")
-target_link_arduino_libraries(pbdrv-mod core Wire)
-
-# freertos is used to defer the handling of i2c messages outside the receive
-# interrupt service routine
-include("${CMAKE_CURRENT_LIST_DIR}/../../ext/freertos/include.cmake")
+target_link_arduino_libraries(pbdrv-mod PRIVATE core Wire)
 
diff --git a/lib/pbdrv/drv/arduino/index.dox b/lib/pbdrv/drv/arduino/index.dox
index 4c74222..1856918 100644
--- a/lib/pbdrv/drv/arduino/index.dox
+++ b/lib/pbdrv/drv/arduino/index.dox
@@ -2,18 +2,22 @@
 /**
 \ingroup pb_drv
 \defgroup pb_drv_arduino Arduino
-\brief Arduino (Arduino-CMake-Toolchain) driver
-
-This driver is automatically enabled if the variable \c ARDUINO is defined in
-your CMakeLists.txt (it is by default when using Arduino-CMake-Toolchain).
-
-\note This driver automatically includes the
-\ref pb_ext_freertos "FreeRTOS extension" for deferring calls to \c
-pb_i2c_recv() from the I2C ISR.
+\brief Arduino ATmega (w/ Arduino-CMake-Toolchain) driver
 
 This driver is known to work with the following MCUs:
 - ATmega328P (Arduino Uno)
 - ATmega2560 (Arduino Mega)
 
+\par Usage
+- Link the \c pbdrv-mod library with your main executable
+- Load an \ref pb_ext "extension"
+
+\note This driver is automatically enabled if the variable \c ARDUINO is
+defined in your CMakeLists.txt (it is by default when using
+Arduino-CMake-Toolchain).
+
+A complete example for this driver is available in the \ref puzzle/dummy
+folder.
+
 */
 
diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp
index 2eef8d5..d2a6402 100644
--- a/lib/pbdrv/drv/arduino/mod.cpp
+++ b/lib/pbdrv/drv/arduino/mod.cpp
@@ -6,32 +6,17 @@
 #include <Wire.h>
 #include <avr/delay.h>
 
-#include <FreeRTOS.h>
-#include <timers.h>
-#include <task.h>
-
 #include "../../pb.h"
 #include "../../pb-mod.h"
 #include "../../pb-types.h"
-#include "../../pb-buf.h"
-#include "../../pb-mem.h"
-
-static void async_pb_i2c_recv(void * _msg, uint32_t _) {
-	pb_buf_t * msg = (pb_buf_t *) _msg;
-	pb_i2c_recv((uint8_t *) msg->data, msg->size);
-	pb_buf_free(msg);
-	pb_free(msg);
-}
 
 static void recv_event(int bytes) {
-	pb_buf_t * msg = (pb_buf_t *) pb_malloc(sizeof(pb_buf_t));
-	msg->data = (char *) pb_malloc(bytes);
-	msg->size = 0;
+	uint8_t data[bytes];
+	size_t sz = 0;
 	while (Wire.available())
-		msg->data[msg->size++] = Wire.read();
+		data[sz++] = Wire.read();
 
-	// defer pb_i2c_recv call
-	xTimerPendFunctionCallFromISR(async_pb_i2c_recv, msg, 0, NULL);
+	pb_i2c_recv(data, sz);
 }
 
 static void pb_setup() {
@@ -42,7 +27,6 @@ static void pb_setup() {
 	Wire.onReceive(recv_event);
 }
 
-/// \ingroup pb_drv_arduino
 __weak void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
 	Wire.beginTransmission((int) addr);
 	Wire.write(buf, sz);
@@ -57,18 +41,14 @@ extern void loop(void);
 //! Arduino internal initialization
 void init(void);
 
-//! FreeRTOS loop task
-void loop_task() {
-	for(;;) {
-		loop();
-		if (serialEventRun) serialEventRun();
-	}
-}
-
 /**
  * \ingroup pb_drv_arduino
  * \brief Application entrypoint
  *
+ * This function overrides the default (weak) implementation of the \c main()
+ * function in the Arduino framework. No additional setup is required to use
+ * this driver.
+ *
  * \note I should really be able to use Arduino's initVariant function for
  * this, but I can't seem to get it to link properly using the CMake setup in
  * this repository. Overriding the main() function seems to work, and the
@@ -79,8 +59,10 @@ int main(void) {
 	init(); // call arduino internal setup
 	setup(); // call regular arduino setup
 	pb_setup(); // call pbdrv-mod setup
-	xTaskCreate((TaskFunction_t) loop_task, "loop", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
-	vTaskStartScheduler(); // start freertos scheduler
+	for(;;) {
+		loop();
+		if (serialEventRun) serialEventRun();
+	}
 	return 0;
 }
 
-- 
cgit v1.2.3