Changeset - a02651c5dd9b
[Not reviewed]
default
0 3 0
Ethan Zonca - 17 months ago 2024-01-15 14:16:47
ez@ethanzonca.com
Add missing includes and update readme
3 files changed with 15 insertions and 3 deletions:
0 comments (0 inline, 0 general)
README.md
Show inline comments
 
# Protofusion ESP32S3 Template
 

	
 
## Configure
 
Set target with `idf.y set-target esp32s3`
 

	
 
Run menuconfig with `idf.py menuconfig` and choose the Protofusion menu option.
 

	
 
Set target with `idf.y set-target esp32s3`
 

	
 
Rename the project by editing CMakeLists.txt and change `project(protofusion_template)` to reflect your project name
 

	
 
Note that clean doesn't always clean everything... do `idf.py fullclean` to make sure all generated files are removed (especially when switching processors).
 

	
 
## Build and Flash
 

	
 
Build the project and flash it to the board, then run monitor tool to view serial output:
 
Build the project
 

	
 
```bash
 
idf.py build
 
```
 

	
 
Flash to board and run monitor tool to view serial output:
 

	
 
```bash
 
idf.py flash monitor
 
```
 

	
 
(To exit the serial monitor, type ``Ctrl-]``.)
 

	
main/can.c
Show inline comments
 
//
 
// can
 
//
 

	
 
#include "can.h"
 
#include "esp_log.h"
 
#include "driver/twai.h"
 
#include "freertos/FreeRTOS.h"
 
#include "freertos/task.h"
 
#include "freertos/event_groups.h"
 

	
 
// EMZ FIXME
 
#define TX_GPIO_NUM 5
 
#define RX_GPIO_NUM 6
 
#define RX_TASK_PRIO 3
 

	
 

	
 
// Private variables
 
static const char *TAG = "canbus";
 

	
 

	
 
// static QueueHandle_t tx_task_queue;
 
// static QueueHandle_t rx_task_queue;
 

	
 

	
 
// Task for receiving CAN messages
 
static void twai_receive_task(void *arg)
 
{
 
    while (1) {
 
        twai_message_t rx_msg;
 
        twai_receive(&rx_msg, portMAX_DELAY);
 
        if (rx_msg.identifier == 0xdead) {
 
            ESP_LOGI(TAG, "Received data ");
 
        }
 

	
 
    }
 
    vTaskDelete(NULL);
 
}
 

	
 

	
 
// Initialize the CAN bus
 
void can_init(void)
 
{
 
    // CAN bus configuration
 
    twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS();
 
    twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
 
    twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL);
 
	
 
    // Move canbus irq to free up the default level 1 IRQ it will take up (processor panics if we don't do this)
 
    g_config.intr_flags = ESP_INTR_FLAG_LOWMED;
 

	
 
    // Install TWAI driver
 
    ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
 
    ESP_LOGI(TAG, "Driver installed");
 
    ESP_ERROR_CHECK(twai_start());
 
    ESP_LOGI(TAG, "Driver started");
 

	
 
    // rx_task_queue = xQueueCreate(1, sizeof(rx_task_action_t));
 
    // tx_task_queue = xQueueCreate(1, sizeof(tx_task_action_t));
 
    xTaskCreatePinnedToCore(twai_receive_task, "TWAI_rx", 4096, NULL, RX_TASK_PRIO, NULL, tskNO_AFFINITY);
 
}
 

	
 

	
 
// Send message on the CAN bus
 
void can_send(uint32_t id, uint8_t* data, uint8_t len, uint32_t timeout)
 
{
 
    // Gross
 
    twai_message_t msg = { .identifier = id, .data_length_code = len, .ss = 1, .data = {data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]} };
 
    twai_transmit(&msg, timeout);
 
}
 
\ No newline at end of file
main/wifi.c
Show inline comments
 
//
 
// wifi
 
//
 

	
 
#include "wifi.h"
 
#include "esp_wifi.h"
 
#include "esp_event.h"
 
#include "esp_log.h"
 
#include "lwip/err.h"
 
#include "lwip/sys.h"
 
#include "display.h"
 

	
 
#include "freertos/FreeRTOS.h"
 
#include "freertos/task.h"
 
#include "freertos/event_groups.h"
 

	
 
// Private variables
 

	
 
// FreeRTOS event group to signal when we are connected
 
static EventGroupHandle_t s_wifi_event_group;
 
static const char *TAG = "wifi station";
 
static int s_retry_num = 0;
 

	
 

	
 
// Handler for wifi events
 
static void __event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
 
{
 
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
 
        esp_wifi_connect();
 
    }
 
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) 
 
    {
 
        if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) 
 
        {
 
            esp_wifi_connect();
 
            s_retry_num++;
 
            ESP_LOGI(TAG, "retry to connect to the AP");
 
            display_update_text("AP Connect Retry");
 

	
 
        } 
 
        else 
 
        {
 
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
 
        }
 
        ESP_LOGI(TAG,"connect to the AP fail");
 
        display_update_text("AP Connect Fail");
 
        
 
    } 
 
    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) 
 
    {
 
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
 
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
 
        char out[128] = {0};
 
        snprintf(out, 128, "Got IP " IPSTR, IP2STR(&event->ip_info.ip));
 
        display_update_text(out);
 
        s_retry_num = 0;
 
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
 
    }
 
}
 

	
 

	
 
// Initialize wifi and connect to network
 
void wifi_init(void)
 
{
 
    s_wifi_event_group = xEventGroupCreate();
 

	
 
    ESP_ERROR_CHECK(esp_netif_init());
 

	
 
    ESP_ERROR_CHECK(esp_event_loop_create_default());
 
    esp_netif_create_default_wifi_sta();
 

	
 
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
 
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
 

	
 
    esp_event_handler_instance_t instance_any_id;
 
    esp_event_handler_instance_t instance_got_ip;
 
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
 
                                                        ESP_EVENT_ANY_ID,
 
                                                        &__event_handler,
 
                                                        NULL,
 
                                                        &instance_any_id));
 
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
 
                                                        IP_EVENT_STA_GOT_IP,
 
                                                        &__event_handler,
 
                                                        NULL,
 
                                                        &instance_got_ip));
 

	
 
    wifi_config_t wifi_config = {
 
        .sta = {
 
            .ssid = EXAMPLE_ESP_WIFI_SSID,
 
            .password = EXAMPLE_ESP_WIFI_PASS,
 
            /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
 
             * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
 
             * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
 
             * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
 
             */
 
            .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
 
            .sae_pwe_h2e = ESP_WIFI_SAE_MODE,
 
            .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER,
 
        },
 
    };
 
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
 
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
 
    ESP_ERROR_CHECK(esp_wifi_start() );
 

	
 
    ESP_LOGI(TAG, "wifi_init_sta finished.");
 

	
 
    /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
 
     * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
 
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
 
            WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
0 comments (0 inline, 0 general)