Created ESP-IDF for can-interface
This commit is contained in:
113
can-interface/main/hello_world_main.cpp
Executable file
113
can-interface/main/hello_world_main.cpp
Executable file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "esp_chip_info.h"
|
||||
#include "esp_flash.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
#include "mcp2125.hpp"
|
||||
|
||||
#define SPI_PIN_CS0 GPIO_NUM_5
|
||||
#define SPI_PIN_SCLK GPIO_NUM_18
|
||||
#define SPI_PIN_MISO GPIO_NUM_19
|
||||
#define SPI_PIN_MOSI GPIO_NUM_23
|
||||
|
||||
#define EXTERNAL_TRIGGER GPIO_NUM_26
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void app_main() {
|
||||
printf("Hello world!\n");
|
||||
|
||||
const gpio_num_t LED_PIN = GPIO_NUM_2;
|
||||
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
|
||||
|
||||
gpio_set_direction(EXTERNAL_TRIGGER, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(EXTERNAL_TRIGGER, true);
|
||||
|
||||
spi_bus_config_t spi_config = {
|
||||
.mosi_io_num = SPI_PIN_MOSI,
|
||||
.miso_io_num = SPI_PIN_MISO,
|
||||
.sclk_io_num = SPI_PIN_SCLK,
|
||||
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
|
||||
.flags = SPICOMMON_BUSFLAG_MASTER
|
||||
};
|
||||
|
||||
spi_device_interface_config_t mp2125_devcfg = {
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
|
||||
.mode = 0,
|
||||
.duty_cycle_pos = 128,
|
||||
.cs_ena_pretrans = 1,
|
||||
.cs_ena_posttrans = 1,
|
||||
.clock_speed_hz = 10000,
|
||||
|
||||
.spics_io_num = SPI_PIN_CS0,
|
||||
.flags = SPI_DEVICE_HALFDUPLEX,
|
||||
.queue_size = 5,
|
||||
};
|
||||
|
||||
spi_device_handle_t mp2125_handle;
|
||||
|
||||
spi_bus_initialize(VSPI_HOST, &spi_config, SPI_DMA_CH_AUTO);
|
||||
spi_bus_add_device(VSPI_HOST, &mp2125_devcfg, &mp2125_handle);
|
||||
|
||||
char rxBuffer[32] = {0};
|
||||
|
||||
spi_transaction_t transaction0 = { // RESET
|
||||
.cmd = 0b11000000,
|
||||
.length = 0,
|
||||
.rxlength = 2 * 8,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = rxBuffer
|
||||
};
|
||||
|
||||
spi_transaction_t transaction1 = { // READ STAT/S
|
||||
.cmd = 0b10110000,
|
||||
.length = 0,
|
||||
.rxlength = 2 * 8,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = rxBuffer
|
||||
};
|
||||
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
gpio_set_level(EXTERNAL_TRIGGER, false);
|
||||
spi_device_transmit(mp2125_handle, &transaction0);
|
||||
spi_device_transmit(mp2125_handle, &transaction1);
|
||||
gpio_set_level(EXTERNAL_TRIGGER, true);
|
||||
|
||||
bool flag = true;
|
||||
while (true) {
|
||||
gpio_set_level(LED_PIN, flag);
|
||||
flag = dummy_function(flag);
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user