Explorar o código

Split the initialization code into the porting file, and add thread initialization supports

Evlers %!s(int64=2) %!d(string=hai) anos
pai
achega
043dd6f218
Modificáronse 6 ficheiros con 203 adicións e 65 borrados
  1. 39 13
      Kconfig
  2. 77 8
      README.md
  3. 3 0
      SConscript
  4. 18 21
      host/driver/transport/spi/spi_drv.c
  5. 57 0
      sample/esp-hosted_port.c
  6. 9 23
      wlan/esp_wlan.c

+ 39 - 13
Kconfig

@@ -8,22 +8,22 @@ menuconfig RT_USING_ESP_HOSTED
     if RT_USING_ESP_HOSTED
 
         config ESP_HOSTED_THREAD_PRIORITY
-            int "The priority level value of esp-hosted thread"
+            int "The priority of the esp-hosted thread"
             range 0 32
             default 8
 
         config ESP_HOSTED_THREAD_STACK_SIZE
-            int "The stack size for esp-hosted thread"
+            int "The stack size of the esp-hosted thread"
             range 0 8192
             default 4096
 
         config ESP_HOSTED_SPI_THREAD_PRIORITY
-            int "The priority level value of esp-hosted SPI thread"
+            int "The priority of the esp-hosted SPI thread"
             range 0 32
             default 20
 
         config ESP_HOSTED_SPI_THREAD_STACK_SIZE
-            int "The stack size for esp-hosted SPI thread"
+            int "The stack size of the esp-hosted SPI thread"
             range 0 4096
             default 512
 
@@ -32,18 +32,28 @@ menuconfig RT_USING_ESP_HOSTED
             range 0 32
             default 2
 
-        config ESP_HOSTED_SPI_BUS_NAME
-            string "Set the spi bus name"
-            default "spi2"
+        config ESP_HOSTED_USING_SAMPLE
+            bool "Initialize WiFi using the sample"
+            default y
+
+        if ESP_HOSTED_USING_SAMPLE
+            config ESP_HOSTED_SPI_BUS_NAME
+                string "Set the spi bus name"
+                default "spi1"
+
+            config ESP_HOSTED_SPI_CS_PIN
+                int "Set the SPI CS pin"
+                default 15
+        endif
+
+        config ESP_HOSTED_SPI_DEVICE_NAME
+            string "Set the spi device name"
+            default "esp-hosted"
 
         config ESP_HOSTED_SPI_MAX_HZ
             int "Set the maximum spi frequency(Hz)"
             default 25000000
 
-        config ESP_HOSTED_SPI_CS_PIN
-            int "Set the SPI CS pin"
-            default 15
-
         config ESP_HOSTED_DATA_READY_PIN
             int "Set the data ready pin"
             default 39
@@ -53,7 +63,23 @@ menuconfig RT_USING_ESP_HOSTED
             default 40
 
         config ESP_HOSTED_RESET_PIN
-            int "Set the reset pin"
-            default 22
+                int "Set the reset pin"
+                default 22
+
+        config ESP_HOSTED_THREAD_INIT
+            bool "Use thread initialization"
+            default n
+
+        if ESP_HOSTED_THREAD_INIT
+            config ESP_HOSTED_INIT_THREAD_STACK_SIZE
+                int "The stack size of the init thread"
+                range 0 4096
+                default 2048
+
+            config ESP_HOSTED_INIT_THREAD_PRIORITY
+                int "The priority of the init thread"
+                range 0 32
+                default 20
+        endif
 
     endif

+ 77 - 8
README.md

@@ -37,17 +37,86 @@ endmenu
 - Enter `Using esp-hosted form espressif` menu to configure the pin and SPI bus:
 ```
 --- Using esp-hosted form espressif
-(8)   The priority level value of esp-hosted thread
-(4096) The stack size for esp-hosted thread
-(20)  The priority level value of esp-hosted SPI thread
-(512) The stack size for esp-hosted SPI thread
-(10)  The size for esp-hosted SPI queue
-(spi2) Set the spi bus name
+(8)   The priority of the esp-hosted thread
+(4096) The stack size of the esp-hosted thread
+(20)  The priority of the esp-hosted SPI thread
+(512) The stack size of the esp-hosted SPI thread
+(2)   The size for esp-hosted SPI queue
+[*]   Initialize WiFi using the sample
+(spi1)  Set the spi bus name (NEW)
+(28)    Set the SPI CS pin
+(esp-hosted) Set the spi device name
 (25000000) Set the maximum spi frequency(Hz)
-(15)  Set the SPI CS pin
 (39)  Set the data ready pin
 (40)  Set the handshake pin
-(22)  Set the reset pin
+(38)  Set the reset pin
+[*]   Use thread initialization
+(2048)  The stack size of the init thread
+(20)    The priority of the init thread
+```
+- If you do not use `sample` to initialize WiFi, copy `esp-hosted_port.c` in the `smaple` folder to the `board` folder for modification and add it to the project
+- Here is an example of ART-PI(STM32H750):
+```
+/*
+ * Copyright (c) 2006-2024, Evlers Developers
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date         Author      Notes
+ * 2024-01-11   Evlers      first implementation
+ */
+
+#include <rtthread.h>
+
+#if defined(RT_USING_ESP_HOSTED) && !defined(ESP_HOSTED_USING_SAMPLE)
+#include <rtdevice.h>
+#include <drv_spi.h>
+#include <board.h>
+
+#define ESP_HOSTED_SPI_BUS_NAME             "spi2"
+#define ESP_HOSTED_SPI_CS_PORT              GPIOI
+#define ESP_HOSTED_SPI_CS_PIN               GPIO_PIN_0
+
+extern int rt_hw_esp_wlan_init (void);
+
+static void esp_hosted_init (void *parameter)
+{
+    /* reset esp32 chip */
+    rt_pin_mode(ESP_HOSTED_RESET_PIN, PIN_MODE_OUTPUT);
+    rt_pin_write(ESP_HOSTED_RESET_PIN, PIN_LOW);
+    rt_thread_mdelay(50);
+    rt_pin_write(ESP_HOSTED_RESET_PIN, PIN_HIGH);
+    
+    /* stop spi transactions short time to avoid slave sync issues */
+	rt_thread_mdelay(50);
+
+    /* attach spi device */
+    rt_hw_spi_device_attach(ESP_HOSTED_SPI_BUS_NAME, ESP_HOSTED_SPI_DEVICE_NAME, ESP_HOSTED_SPI_CS_PORT, ESP_HOSTED_SPI_CS_PIN);
+
+    /* Initialize the esp-hosted */
+    rt_hw_esp_wlan_init();
+}
+
+int esp_spi_device_init (void)
+{
+#ifdef ESP_HOSTED_THREAD_INIT
+    /* Use thread initialization */
+    rt_thread_t init_thread = rt_thread_create("esp_init", esp_hosted_init, NULL, 
+                                                ESP_HOSTED_INIT_THREAD_STACK_SIZE, ESP_HOSTED_INIT_THREAD_PRIORITY, 20);
+    RT_ASSERT(init_thread != NULL);
+    rt_thread_startup(init_thread);
+#else
+    /* Thread initialization is not used */
+    esp_hosted_init(NULL);
+#endif
+
+    return RT_EOK;
+}
+INIT_APP_EXPORT(esp_spi_device_init);
+
+
+#endif /* defined(RT_USING_ESP_HOSTED) && !defined(ESP_HOSTED_USING_SAMPLE) */
 ```
 
 #### Hardware connections for ESP32

+ 3 - 0
SConscript

@@ -48,6 +48,9 @@ path += [cwd + '/host/port/include']
 src += Glob('wlan/*.c')
 path += [cwd + '/wlan']
 
+# add sample source files
+if GetDepend(['ESP_HOSTED_USING_SAMPLE']):
+    src += Glob('sample/*.c')
 
 CPPDEFINES = ['']
 

+ 18 - 21
host/driver/transport/spi/spi_drv.c

@@ -14,9 +14,10 @@
 // limitations under the License.
 
 /** Includes **/
+#include <stdint.h>
+#include <string.h>
 #include "rtthread.h"
 #include "rtdevice.h"
-#include "drv_spi.h"
 #include "string.h"
 #include "common/trace.h"
 #include "spi_drv.h"
@@ -205,6 +206,22 @@ void transport_init(void(*transport_evt_handler_fp)(uint8_t))
 	assert(process_rx_task_id);
 	rt_thread_startup(process_rx_task_id);
 
+	/* Initializes the spi bus */
+	if ((spi_dev = (struct rt_spi_device *)rt_device_find(ESP_HOSTED_SPI_DEVICE_NAME)) != NULL)
+	{
+		/* Configure SPI bus */
+		struct rt_spi_configuration cfg;
+        cfg.data_width = 8;
+        cfg.mode = RT_SPI_MODE_2 | RT_SPI_MSB;
+        cfg.max_hz = ESP_HOSTED_SPI_MAX_HZ;
+        rt_spi_configure(spi_dev, &cfg);
+	}
+	else
+	{
+		LOG_E("No spi device (%s) is found. Please attach the spi bus!", ESP_HOSTED_SPI_DEVICE_NAME);
+		return ;
+	}
+
 	/* Initializes an external interrupt */
 	rt_pin_mode(ESP_HOSTED_HANDSHAKE_PIN, PIN_MODE_INPUT_PULLUP);
 	rt_pin_mode(ESP_HOSTED_DATA_READY_PIN, PIN_MODE_INPUT_PULLUP);
@@ -212,26 +229,6 @@ void transport_init(void(*transport_evt_handler_fp)(uint8_t))
 	rt_pin_attach_irq(ESP_HOSTED_DATA_READY_PIN, PIN_IRQ_MODE_RISING, gpio_interrupt, NULL);
 	rt_pin_irq_enable(ESP_HOSTED_HANDSHAKE_PIN, RT_TRUE);
 	rt_pin_irq_enable(ESP_HOSTED_DATA_READY_PIN, RT_TRUE);
-
-	/* Initializes the spi bus */
-	const char *dev_name = "esp-hosted";
-	rt_hw_spi_device_attach(ESP_HOSTED_SPI_BUS_NAME, dev_name, ESP_HOSTED_SPI_CS_PIN);
-	spi_dev = (struct rt_spi_device *)rt_device_find(dev_name);
-
-	if (spi_dev == RT_NULL)
-    {
-        LOG_E("spi device %s not found!\r", dev_name);
-        return ;
-    }
-
-    /* Configure SPI bus */
-    {
-        struct rt_spi_configuration cfg;
-        cfg.data_width = 8;
-        cfg.mode = RT_SPI_MODE_2 | RT_SPI_MSB; /* SPI Compatible: Mode 0. */
-        cfg.max_hz = ESP_HOSTED_SPI_MAX_HZ;
-        rt_spi_configure(spi_dev, &cfg);
-    }
 }
 
 /**

+ 57 - 0
sample/esp-hosted_port.c

@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2006-2024, Evlers Developers
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date         Author      Notes
+ * 2024-01-11   Evlers      first implementation
+ */
+
+#include <rtthread.h>
+
+#if defined(RT_USING_ESP_HOSTED) && defined(ESP_HOSTED_USING_SAMPLE)
+#include <rtdevice.h>
+#include <drv_spi.h>
+#include <board.h>
+
+
+extern int rt_hw_esp_wlan_init (void);
+
+static void esp_hosted_init (void *parameter)
+{
+    /* reset esp32 chip */
+    rt_pin_mode(ESP_HOSTED_RESET_PIN, PIN_MODE_OUTPUT);
+    rt_pin_write(ESP_HOSTED_RESET_PIN, PIN_LOW);
+    rt_thread_mdelay(50);
+    rt_pin_write(ESP_HOSTED_RESET_PIN, PIN_HIGH);
+    
+    /* stop spi transactions short time to avoid slave sync issues */
+	rt_thread_mdelay(50);
+
+    /* attach spi device */
+    rt_hw_spi_device_attach(ESP_HOSTED_SPI_BUS_NAME, ESP_HOSTED_SPI_DEVICE_NAME, ESP_HOSTED_SPI_CS_PIN);
+
+    /* Initialize the esp-hosted */
+    rt_hw_esp_wlan_init();
+}
+
+int esp_spi_device_init (void)
+{
+#ifdef ESP_HOSTED_THREAD_INIT
+    /* Use thread initialization */
+    rt_thread_t init_thread = rt_thread_create("esp_init", esp_hosted_init, NULL, 
+                                                ESP_HOSTED_INIT_THREAD_STACK_SIZE, ESP_HOSTED_INIT_THREAD_PRIORITY, 20);
+    RT_ASSERT(init_thread != NULL);
+    rt_thread_startup(init_thread);
+#else
+    /* Thread initialization is not used */
+    esp_hosted_init(NULL);
+#endif
+
+    return RT_EOK;
+}
+INIT_APP_EXPORT(esp_spi_device_init);
+
+
+#endif /* defined(RT_USING_ESP_HOSTED) && defined(ESP_HOSTED_USING_SAMPLE) */

+ 9 - 23
wlan/esp_wlan.c

@@ -6,8 +6,11 @@
  * Change Logs:
  * Date         Author      Notes
  * 2024-01-04   Evlers      first implementation
+ * 2024-01-11	Evlers		removed the hardware initialization code
  */
 
+#include <stdint.h>
+#include <string.h>
 #include "rtthread.h"
 #include "rtdevice.h"
 #include "ctrl_api.h"
@@ -59,19 +62,6 @@ static struct drv_wifi wifi_sta, wifi_ap;
 
 static void network_data_rx_callback(struct network_handle *net_handle);
 
-
-static int esp_init (void)
-{
-    /* Reset esp wifi */
-    rt_pin_mode(ESP_HOSTED_RESET_PIN, PIN_MODE_OUTPUT);
-    rt_pin_write(ESP_HOSTED_RESET_PIN, PIN_LOW);
-    rt_thread_mdelay(50);
-    rt_pin_write(ESP_HOSTED_RESET_PIN, PIN_HIGH);
-
-    return RT_EOK;
-}
-INIT_PREV_EXPORT(esp_init);
-
 static int get_response_result(ctrl_cmd_t * resp)
 {
 	int ret = SUCCESS;
@@ -125,8 +115,8 @@ static int esp_ctrl_event_callback (ctrl_cmd_t * event)
 		goto fail_parsing;
 	}
 
-	switch(event->msg_id) {
-
+	switch(event->msg_id)
+	{
 		case CTRL_EVENT_ESP_INIT:
 			LOG_D("App EVENT: ESP INIT");
 			rt_sem_release(sem_esp_init);
@@ -503,10 +493,10 @@ static int drv_wlan_send(struct rt_wlan_device *wlan, void *buff, int len)
 	}
 
 	tx_buffer = malloc(sizeof(struct pbuf));
-	assert(tx_buffer);
+	RT_ASSERT(tx_buffer);
 
 	tx_buffer->payload = malloc(len);
-	assert(tx_buffer->payload);
+	RT_ASSERT(tx_buffer->payload);
 
 	memcpy(tx_buffer->payload, buff, len);
 	tx_buffer->len = len;
@@ -570,15 +560,12 @@ static void network_data_rx_callback(struct network_handle *net_handle)
 	}
 }
 
-int rt_hw_wlan_init (void)
+int rt_hw_esp_wlan_init (void)
 {
 	static struct rt_wlan_device wlan_ap, wlan_sta;
 
 	sem_esp_init = rt_sem_create("esp_init", 0, RT_IPC_FLAG_PRIO);
-	assert(sem_esp_init != NULL);
-
-	/* stop spi transactions short time to avoid slave sync issues */
-	rt_thread_mdelay(50);
+	RT_ASSERT(sem_esp_init != NULL);
 
     /* Init network interface */
 	network_init();
@@ -633,4 +620,3 @@ int rt_hw_wlan_init (void)
 
     return RT_EOK;
 }
-INIT_APP_EXPORT(rt_hw_wlan_init);