Implemented Low Level Compunications

This commit is contained in:
AlexanderHD27
2024-10-14 09:19:00 +02:00
parent 7eebf619ae
commit b150a905a3
15 changed files with 645 additions and 60 deletions

View File

@@ -1,5 +1,5 @@
idf_component_register(SRCS "hello_world_main.cpp"
REQUIRES driver
REQUIRES mcp2125
REQUIRES mcp2521
REQUIRES spi_flash
INCLUDE_DIRS "")

View File

@@ -5,6 +5,7 @@
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "sdkconfig.h"
@@ -20,12 +21,14 @@
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "mcp2125.hpp"
#include "mcp2521.hpp"
#include "reg.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 CAN_INT_PIN GPIO_NUM_21
#define EXTERNAL_TRIGGER GPIO_NUM_26
@@ -42,61 +45,35 @@ void app_main() {
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);
spi_bus_config_t spi_bus;
MCP2521_SPI_Interface mcp2521_spi(
VSPI_HOST,
&spi_bus,
SPI_PIN_MOSI,
SPI_PIN_MISO,
SPI_PIN_SCLK,
SPI_PIN_CS0,
CAN_INT_PIN
);
mcp2521_spi.reset();
printf("%x\n", mcp2521_spi.read_reg(MCP2521_CANCTRL));
uint8_t data[16];
mcp2521_spi.read_reg(MCP2521_CANSTAT, data, 16);
for(int i=0; i<0x10; i++) {
printf("%x ", i);
for(int j=0; j<8; j++) {
printf("%u", (data[i] >> (7-j)) & 1);
}
printf("\n");
}
gpio_set_level(EXTERNAL_TRIGGER, true);
bool flag = true;