Files
gobot/can-interface/main/hello_world_main.cpp
2024-10-16 21:28:36 +02:00

87 lines
1.9 KiB
C++
Executable File

/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include <string.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 "mcp2521.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
extern "C" 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);
vTaskDelay(100 / portTICK_PERIOD_MS);
spi_bus_config_t spi_bus;
MCP2521_Hardware_Handle_ESP hardware_mcp2521(
VSPI_HOST,
&spi_bus,
SPI_PIN_MOSI,
SPI_PIN_MISO,
SPI_PIN_SCLK,
SPI_PIN_CS0,
CAN_INT_PIN
);
MCP2521_Command_Interface mcp2521(&hardware_mcp2521);
gpio_set_level(EXTERNAL_TRIGGER, false);
printf("CANCTRL: %x\n", mcp2521.read_reg(MCP2521_CANCTRL));
printf("RX_STATUS: %x\n", mcp2521.read_rx_status());
printf("STATUS: %x\n", mcp2521.read_status());
uint8_t data[16];
hardware_mcp2521.read(MCP2521_OP_READ, data, 16, MCP2521_RXB0CTRL);
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;
while (true) {
gpio_set_level(LED_PIN, flag);
flag = !flag;
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}