From 9e43e8187ee56ce923857b149c9e89ad16925506 Mon Sep 17 00:00:00 2001 From: Dragon-Knight Date: Wed, 30 Oct 2024 04:44:34 +0300 Subject: [PATCH] use new `SPIManager` class --- include/CAN_SPI.h | 7 +- include/SPI.h | 2 + lib/MCP2515Lib/src/MCP2515_HAL.cpp | 471 ----------------------------- lib/MCP2515Lib/src/MCP2515_HAL.h | 115 ------- 4 files changed, 6 insertions(+), 589 deletions(-) delete mode 100644 lib/MCP2515Lib/src/MCP2515_HAL.cpp delete mode 100644 lib/MCP2515Lib/src/MCP2515_HAL.h diff --git a/include/CAN_SPI.h b/include/CAN_SPI.h index 361fbf2..6c5ec69 100644 --- a/include/CAN_SPI.h +++ b/include/CAN_SPI.h @@ -1,7 +1,8 @@ #pragma once #include #include -#include +#include +#include extern SPI_HandleTypeDef hspi1; @@ -164,8 +165,8 @@ class SteeringAngleSensor SPIManager<2> manager(SPI_Config, SPI_Write, SPI_Read, SPI_WriteRead); - MCP2515Class obj1(SPI_CS_CAN1, CAN1_RX_INT, SPI_BAUDRATEPRESCALER_8); - MCP2515Class obj2(SPI_CS_CAN2, CAN2_RX_INT, SPI_BAUDRATEPRESCALER_8); + SPI_MCP2515 obj1(SPI_CS_CAN1, CAN1_RX_INT, SPI_BAUDRATEPRESCALER_8); + SPI_MCP2515 obj2(SPI_CS_CAN2, CAN2_RX_INT, SPI_BAUDRATEPRESCALER_8); EasyPinD pin_rs_1(CAN1_RS.Port, {CAN1_RS.Pin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH}, GPIO_PIN_SET); EasyPinD pin_sens_1(CAN1_SENS_PWR.Port, {CAN1_SENS_PWR.Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH}, GPIO_PIN_RESET); EasyPinD pin_rs_2(CAN2_RS.Port, {CAN2_RS.Pin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH}, GPIO_PIN_SET); diff --git a/include/SPI.h b/include/SPI.h index 70208be..379ac2c 100644 --- a/include/SPI.h +++ b/include/SPI.h @@ -1,6 +1,8 @@ #pragma once #include #include +#include +#include #include "SPIFast.h" extern SPI_HandleTypeDef hspi2; diff --git a/lib/MCP2515Lib/src/MCP2515_HAL.cpp b/lib/MCP2515Lib/src/MCP2515_HAL.cpp deleted file mode 100644 index b6ae5ed..0000000 --- a/lib/MCP2515Lib/src/MCP2515_HAL.cpp +++ /dev/null @@ -1,471 +0,0 @@ -// Copyright (c) Sandeep Mistry. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -// https://github.com/sandeepmistry/arduino-CAN -// Porting for StarPixel project: Dragon_Knight, https://github.com/Dragon-Knight - -#include "MCP2515_HAL.h" - -#define REG_BFPCTRL 0x0C -#define REG_TXRTSCTRL 0x0D - -#define REG_CANCTRL 0x0F - -#define REG_CNF3 0x28 -#define REG_CNF2 0x29 -#define REG_CNF1 0x2A - -#define REG_CANINTE 0x2B -#define REG_CANINTF 0x2C - -#define FLAG_RXnIE(n) (0x01 << n) -#define FLAG_RXnIF(n) (0x01 << n) -#define FLAG_TXnIF(n) (0x04 << n) - -#define REG_RXFnSIDH(n) (0x00 + (n * 4)) -#define REG_RXFnSIDL(n) (0x01 + (n * 4)) -#define REG_RXFnEID8(n) (0x02 + (n * 4)) -#define REG_RXFnEID0(n) (0x03 + (n * 4)) - -#define REG_RXMnSIDH(n) (0x20 + (n * 0x04)) -#define REG_RXMnSIDL(n) (0x21 + (n * 0x04)) -#define REG_RXMnEID8(n) (0x22 + (n * 0x04)) -#define REG_RXMnEID0(n) (0x23 + (n * 0x04)) - -#define REG_TXBnCTRL(n) (0x30 + (n * 0x10)) -#define REG_TXBnSIDH(n) (0x31 + (n * 0x10)) -#define REG_TXBnSIDL(n) (0x32 + (n * 0x10)) -#define REG_TXBnEID8(n) (0x33 + (n * 0x10)) -#define REG_TXBnEID0(n) (0x34 + (n * 0x10)) -#define REG_TXBnDLC(n) (0x35 + (n * 0x10)) -#define REG_TXBnD0(n) (0x36 + (n * 0x10)) - -#define REG_RXBnCTRL(n) (0x60 + (n * 0x10)) -#define REG_RXBnSIDH(n) (0x61 + (n * 0x10)) -#define REG_RXBnSIDL(n) (0x62 + (n * 0x10)) -#define REG_RXBnEID8(n) (0x63 + (n * 0x10)) -#define REG_RXBnEID0(n) (0x64 + (n * 0x10)) -#define REG_RXBnDLC(n) (0x65 + (n * 0x10)) -#define REG_RXBnD0(n) (0x66 + (n * 0x10)) - -#define FLAG_IDE 0x08 -#define FLAG_SRR 0x10 -#define FLAG_RTR 0x40 -#define FLAG_EXIDE 0x08 - -#define FLAG_RXM0 0x20 -#define FLAG_RXM1 0x40 - - - - - -bool MCP2515Class::begin(uint32_t clock_frequency, uint32_t baud_rate, func_rx_t callback) -{ - // Нет необходимости чистить всё полностью. Этого достаточно - _rx.flag = false; - _rx.id = NO_CAN_ID; - _tx.flag = false; - _tx.id = NO_CAN_ID; - - _onReceive = callback; - - reset(); - - if( writeReadRegister(REG_CANCTRL, 0x80) == false ) return false; - - const uint8_t *cnf = nullptr; - for(uint8_t i = 0; i < (sizeof(_cnf_map) / sizeof(_cnf_map[0])); ++i) - { - if(_cnf_map[i].clockFrequency == clock_frequency && _cnf_map[i].baudRate == baud_rate) - { - cnf = _cnf_map[i].cnf; - break; - } - } - - if(cnf == nullptr) return false; - - writeRegister(REG_CNF1, cnf[0]); - writeRegister(REG_CNF2, cnf[1]); - writeRegister(REG_CNF3, cnf[2]); - - writeRegister(REG_CANINTE, FLAG_RXnIE(1) | FLAG_RXnIE(0)); - writeRegister(REG_BFPCTRL, 0x00); - writeRegister(REG_TXRTSCTRL, 0x00); - writeRegister(REG_RXBnCTRL(0), FLAG_RXM1 | FLAG_RXM0); - writeRegister(REG_RXBnCTRL(1), FLAG_RXM1 | FLAG_RXM0); - - if( writeReadRegister(REG_CANCTRL, 0x00) == false ) return false; - - return true; -} - -void MCP2515Class::end() -{ - return; -} - - - - - -void MCP2515Class::Init() -{ - _int_pin.Init(); - - return; -} - -void MCP2515Class::Tick(uint32_t &time) -{ - if(time - _last_tick == 0) return; - _last_tick = time; - - if(_int_pin.Read() == GPIO_PIN_RESET) - { - if(readRegister(REG_CANINTF) == 0) return; - - while(parsePacket() || _rx.id != NO_CAN_ID) - { - _onReceive(_rx.id, _rx.data, _rx.length); - } - } - - return; -} - - - - - -bool MCP2515Class::beginPacket(uint16_t id, uint8_t dlc, bool rtr) -{ - if(id > 0x7FF) return false; - if(dlc > 8) return false; - - _tx.flag = true; - _tx.id = id; - _tx.extended = false; - _tx.rtr = rtr; - _tx.dlc = dlc; - _tx.length = 0; - memset(_tx.data, 0x00, sizeof(_tx.data)); - - return true; -} - -bool MCP2515Class::beginExtendedPacket(uint32_t id, uint8_t dlc, bool rtr) -{ - if(id > 0x1FFFFFFF) return false; - if(dlc > 8) return false; - - _tx.flag = true; - _tx.id = id; - _tx.extended = true; - _tx.rtr = rtr; - _tx.dlc = dlc; - _tx.length = 0; - memset(_tx.data, 0x00, sizeof(_tx.data)); - - return true; -} - -uint8_t MCP2515Class::write(const uint8_t *buffer, uint8_t size) -{ - if(_tx.flag == false) return 0; - - if(size > (sizeof(_tx.data) - _tx.length)) - { - size = sizeof(_tx.data) - _tx.length; - } - memcpy(&_tx.data[_tx.length], buffer, size); - _tx.length += size; - - return size; -} - -bool MCP2515Class::endPacket() -{ - if(_tx.flag == false) return false; - _tx.flag = false; - - if(_tx.dlc >= 0) - _tx.length = _tx.dlc; - - uint8_t n = 0; - if(_tx.extended == true) - { - writeRegister(REG_TXBnSIDH(n), _tx.id >> 21); - writeRegister(REG_TXBnSIDL(n), (((_tx.id >> 18) & 0x07) << 5) | FLAG_EXIDE | ((_tx.id >> 16) & 0x03)); - writeRegister(REG_TXBnEID8(n), (_tx.id >> 8) & 0xff); - writeRegister(REG_TXBnEID0(n), _tx.id & 0xff); - } else { - writeRegister(REG_TXBnSIDH(n), _tx.id >> 3); - writeRegister(REG_TXBnSIDL(n), _tx.id << 5); - writeRegister(REG_TXBnEID8(n), 0x00); - writeRegister(REG_TXBnEID0(n), 0x00); - } - - if(_tx.rtr == true) - { - writeRegister(REG_TXBnDLC(n), 0x40 | _tx.length); - } else { - writeRegister(REG_TXBnDLC(n), _tx.length); - - for(uint8_t i = 0; i < _tx.length; ++i) - { - writeRegister(REG_TXBnD0(n) + i, _tx.data[i]); - } - } - - writeRegister(REG_TXBnCTRL(n), 0x08); - - bool aborted = false; - while(readRegister(REG_TXBnCTRL(n)) & 0x08) - { - if(readRegister(REG_TXBnCTRL(n)) & 0x10) - { - aborted = true; - - modifyRegister(REG_CANCTRL, 0x10, 0x10); - } - } - if(aborted) - { - // clear abort command - modifyRegister(REG_CANCTRL, 0x10, 0x00); - } - - modifyRegister(REG_CANINTF, FLAG_TXnIF(n), 0x00); - - return ((readRegister(REG_TXBnCTRL(n)) & 0x70) ? false : true); -} - - - - - -uint8_t MCP2515Class::parsePacket() -{ - uint8_t n; - uint8_t intf = readRegister(REG_CANINTF); - if(intf & FLAG_RXnIF(0)) - n = 0; - else if(intf & FLAG_RXnIF(1)) - n = 1; - else - { - _rx.id = NO_CAN_ID; - - return 0; - } - - _rx.extended = (readRegister(REG_RXBnSIDL(n)) & FLAG_IDE) ? true : false; - - uint32_t idA = ((readRegister(REG_RXBnSIDH(n)) << 3) & 0x07f8) | ((readRegister(REG_RXBnSIDL(n)) >> 5) & 0x07); - if(_rx.extended == true) - { - uint32_t idB = (((uint32_t)(readRegister(REG_RXBnSIDL(n)) & 0x03) << 16) & 0x30000) | ((readRegister(REG_RXBnEID8(n)) << 8) & 0xff00) | readRegister(REG_RXBnEID0(n)); - - _rx.id = (idA << 18) | idB; - _rx.rtr = (readRegister(REG_RXBnDLC(n)) & FLAG_RTR) ? true : false; - } - else - { - _rx.id = idA; - _rx.rtr = (readRegister(REG_RXBnSIDL(n)) & FLAG_SRR) ? true : false; - } - - _rx.dlc = readRegister(REG_RXBnDLC(n)) & 0x0f; - - if(_rx.rtr == true) - _rx.length = 0; - else - { - _rx.length = _rx.dlc; - - for(uint8_t i = 0; i < _rx.length; ++i) - { - _rx.data[i] = readRegister(REG_RXBnD0(n) + i); - } - } - - modifyRegister(REG_CANINTF, FLAG_RXnIF(n), 0x00); - - return _rx.dlc; -} - - - - - -bool MCP2515Class::filter(uint16_t id, uint16_t mask) -{ - id &= 0x7ff; - mask &= 0x7ff; - - if( writeReadRegister(REG_CANCTRL, 0x80) == false ) return false; - - uint8_t n; - for(n = 0; n < 2; ++n) - { - // standard only - writeRegister(REG_RXBnCTRL(n), FLAG_RXM0); - writeRegister(REG_RXBnCTRL(n), FLAG_RXM0); - - writeRegister(REG_RXMnSIDH(n), mask >> 3); - writeRegister(REG_RXMnSIDL(n), mask << 5); - writeRegister(REG_RXMnEID8(n), 0); - writeRegister(REG_RXMnEID0(n), 0); - } - - for(n = 0; n < 6; ++n) - { - writeRegister(REG_RXFnSIDH(n), id >> 3); - writeRegister(REG_RXFnSIDL(n), id << 5); - writeRegister(REG_RXFnEID8(n), 0); - writeRegister(REG_RXFnEID0(n), 0); - } - - if( writeReadRegister(REG_CANCTRL, 0x00) == false ) return false; - - return true; -} - -bool MCP2515Class::filterExtended(uint32_t id, uint32_t mask) -{ - id &= 0x1FFFFFFF; - mask &= 0x1FFFFFFF; - - if( writeReadRegister(REG_CANCTRL, 0x80) == false ) return false; - - uint8_t n; - for(n = 0; n < 2; ++n) - { - // extended only - writeRegister(REG_RXBnCTRL(n), FLAG_RXM1); - writeRegister(REG_RXBnCTRL(n), FLAG_RXM1); - - writeRegister(REG_RXMnSIDH(n), mask >> 21); - writeRegister(REG_RXMnSIDL(n), (((mask >> 18) & 0x03) << 5) | FLAG_EXIDE | ((mask >> 16) & 0x03)); - writeRegister(REG_RXMnEID8(n), (mask >> 8) & 0xff); - writeRegister(REG_RXMnEID0(n), mask & 0xff); - } - - for(n = 0; n < 6; ++n) - { - writeRegister(REG_RXFnSIDH(n), id >> 21); - writeRegister(REG_RXFnSIDL(n), (((id >> 18) & 0x03) << 5) | FLAG_EXIDE | ((id >> 16) & 0x03)); - writeRegister(REG_RXFnEID8(n), (id >> 8) & 0xff); - writeRegister(REG_RXFnEID0(n), id & 0xff); - } - - if( writeReadRegister(REG_CANCTRL, 0x00) == false ) return false; - - return true; -} - - - - - -bool MCP2515Class::cmd_observe() -{ - return writeReadRegister(REG_CANCTRL, 0x60); -} - -bool MCP2515Class::cmd_loopback() -{ - return writeReadRegister(REG_CANCTRL, 0x40); -} - -bool MCP2515Class::cmd_sleep() -{ - return writeReadRegister(REG_CANCTRL, 0x01); -} - -bool MCP2515Class::cmd_wakeup() -{ - return writeReadRegister(REG_CANCTRL, 0x00); -} - - - - - - -/* -void MCP2515Class::dumpRegisters(Stream& out) -{ - for (int i = 0; i < 128; i++) { - byte b = readRegister(i); - - out.print("0x"); - if (i < 16) { - out.print('0'); - } - out.print(i, HEX); - out.print(": 0x"); - if (b < 16) { - out.print('0'); - } - out.println(b, HEX); - } -} -*/ - - - - - -void MCP2515Class::reset() -{ - DeviceActivate(); - uint8_t spi_data[] = {0xC0}; - _spi_interface->TransmitData(this, spi_data, sizeof(spi_data)); - DeviceDeactivate(); - - //delayMicroseconds(10); - HAL_Delay(5); - - return; -} - -uint8_t MCP2515Class::readRegister(uint8_t address) -{ - DeviceActivate(); - uint8_t spi_data[] = {0x03, address}; - _spi_interface->TransmitData(this, spi_data, sizeof(spi_data)); - _spi_interface->ReceiveData(this, spi_data, 1); - DeviceDeactivate(); - - return spi_data[0]; -} - -void MCP2515Class::modifyRegister(uint8_t address, uint8_t mask, uint8_t value) -{ - DeviceActivate(); - uint8_t spi_data[] = {0x05, address, mask, value}; - _spi_interface->TransmitData(this, spi_data, sizeof(spi_data)); - DeviceDeactivate(); - - return; -} - -void MCP2515Class::writeRegister(uint8_t address, uint8_t value) -{ - DeviceActivate(); - uint8_t spi_data[] = {0x02, address, value}; - _spi_interface->TransmitData(this, spi_data, sizeof(spi_data)); - DeviceDeactivate(); - - return; -} - -bool MCP2515Class::writeReadRegister(uint8_t address, uint8_t value) -{ - writeRegister(address, value); - if(readRegister(address) != value) return false; - - return true; -} diff --git a/lib/MCP2515Lib/src/MCP2515_HAL.h b/lib/MCP2515Lib/src/MCP2515_HAL.h deleted file mode 100644 index 335587a..0000000 --- a/lib/MCP2515Lib/src/MCP2515_HAL.h +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) Sandeep Mistry. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -// https://github.com/sandeepmistry/arduino-CAN -// Porting for StarPixel project: Dragon_Knight, https://github.com/Dragon-Knight - -#pragma once -#include -#include -#include - -class MCP2515Class : public SPIBaseDevice -{ - static constexpr uint32_t NO_CAN_ID = 0xFFFFFFFF; - - using func_rx_t = void (*)(uint32_t address, uint8_t *data, uint8_t length); - - struct packet_t - { - bool flag = false; // Некий флаг для работы, для tx это инициализация пакета, для rx это готовность пакета - bool extended = false; // Флаг exID - bool rtr = false; // Флаг RTR - uint32_t id = NO_CAN_ID; // Идентификатор пакета CAN - uint8_t dlc = 0; // DLC ?? - uint8_t length = 0; // Длина пакета CAN - uint8_t data[8] = {0x00}; // Данные пакета CAN - }; - - public: - - MCP2515Class(EasyPinD::d_pin_t cs_pin, EasyPinD::d_pin_t int_pin, uint32_t spi_prescaler) : - SPIBaseDevice(cs_pin, spi_prescaler), - _int_pin(int_pin.Port, {int_pin.Pin, GPIO_MODE_EVT_FALLING, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH}), - _rx{false, false, false, NO_CAN_ID, 0, 0, {0x00}}, - _tx{false, false, false, NO_CAN_ID, 0, 0, {0x00}}, - _onReceive(nullptr), - _last_tick(0) - {} - - bool begin(uint32_t clock_frequency, uint32_t baud_rate, func_rx_t callback); - void end(); - - virtual void Init() override; - virtual void Tick(uint32_t &time) override; - - bool beginPacket(uint16_t id, uint8_t dlc, bool rtr); - bool beginExtendedPacket(uint32_t id, uint8_t dlc, bool rtr); - uint8_t write(uint8_t byte){ return write(&byte, 1); } - uint8_t write(const uint8_t *buffer, uint8_t size); - bool endPacket(); - - uint8_t parsePacket(); - - bool filter(uint16_t id) { return filter(id, 0x7ff); } - bool filter(uint16_t id, uint16_t mask); - bool filterExtended(uint32_t id) { return filterExtended(id, 0x1fffffff); } - bool filterExtended(uint32_t id, uint32_t mask); - - bool cmd_observe(); - bool cmd_loopback(); - bool cmd_sleep(); - bool cmd_wakeup(); - - //void dumpRegisters(Stream& out); - - private: - - void reset(); - uint8_t readRegister(uint8_t address); - void modifyRegister(uint8_t address, uint8_t mask, uint8_t value); - void writeRegister(uint8_t address, uint8_t value); - bool writeReadRegister(uint8_t address, uint8_t value); - - EasyPinD _int_pin; - packet_t _rx; - packet_t _tx; - func_rx_t _onReceive; - uint32_t _last_tick; - - - struct cnf_f - { - uint32_t clockFrequency; - uint32_t baudRate; - uint8_t cnf[3]; - } _cnf_map[26] = - { - { 8000000, 1000000, {0x00, 0x80, 0x00} }, - { 8000000, 666666, {0xC0, 0xB8, 0x01} }, - { 8000000, 500000, {0x00, 0x90, 0x02} }, - { 8000000, 250000, {0x00, 0xB1, 0x05} }, - { 8000000, 200000, {0x00, 0xB4, 0x06} }, - { 8000000, 125000, {0x01, 0xB1, 0x05} }, - { 8000000, 100000, {0x01, 0xB4, 0x06} }, - { 8000000, 80000, {0x01, 0xBF, 0x07} }, - { 8000000, 50000, {0x03, 0xB4, 0x06} }, - { 8000000, 40000, {0x03, 0xBF, 0x07} }, - { 8000000, 20000, {0x07, 0xBF, 0x07} }, - { 8000000, 10000, {0x0F, 0xBF, 0x07} }, - { 8000000, 5000, {0x1F, 0xBF, 0x07} }, - - { 16000000, 1000000, {0x00, 0xD0, 0x82} }, - { 16000000, 666666, {0xC0, 0xF8, 0x81} }, - { 16000000, 500000, {0x00, 0xF0, 0x86} }, - { 16000000, 250000, {0x41, 0xF1, 0x85} }, - { 16000000, 200000, {0x01, 0xFA, 0x87} }, - { 16000000, 125000, {0x03, 0xF0, 0x86} }, - { 16000000, 100000, {0x03, 0xFA, 0x87} }, - { 16000000, 80000, {0x03, 0xFF, 0x87} }, - { 16000000, 50000, {0x07, 0xFA, 0x87} }, - { 16000000, 40000, {0x07, 0xFF, 0x87} }, - { 16000000, 20000, {0x0F, 0xFF, 0x87} }, - { 16000000, 10000, {0x1F, 0xFF, 0x87} }, - { 16000000, 5000, {0x3F, 0xFF, 0x87} }, - }; -};