diff --git a/examples/OBDII_PIDs/OBDII_PIDs.ino b/examples/OBDII_PIDs/OBDII_PIDs.ino index 0c857b6..216f2a2 100644 --- a/examples/OBDII_PIDs/OBDII_PIDs.ino +++ b/examples/OBDII_PIDs/OBDII_PIDs.ino @@ -36,7 +36,7 @@ mcp2518fd* controller; // Set CS pin #define CAN_ID_PID 0x7DF unsigned char PID_INPUT; -unsigned char getPid = 0; +unsigned char getPid = 1; void set_mask_filt() { /* @@ -61,7 +61,7 @@ void sendPid(unsigned char __pid) { unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; SERIAL.print("SEND PID: 0x"); SERIAL.println(__pid, HEX); - CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); + controller->sendMsgBuf(CAN_ID_PID, 0, 8, tmp); } void setup() { @@ -94,10 +94,11 @@ void taskCanRecv() { unsigned char len = 0; unsigned char buf[8]; - if (CAN_MSGAVAIL == controller->checkReceive()) { // check if get data + if (1 == controller->checkReceive()) { // check if get data controller->readMsgBuf(&len, buf); // read data, len: data length, buf: data buf SERIAL.println("\r\n------------------------------------------------------------------"); + SERIAL.printf("len = %d\n\r",len); SERIAL.print("Get Data From id: 0x"); SERIAL.println(controller->getCanId(), HEX); for (int i = 0; i < len; i++) { // print the data diff --git a/mcp2518fd_can.cpp b/mcp2518fd_can.cpp index d36bc9c..f743ddd 100644 --- a/mcp2518fd_can.cpp +++ b/mcp2518fd_can.cpp @@ -1,7 +1,7 @@ #include "mcp2518fd_can.h" -uint8_t SPICS = 0; -SPIClass* pSPI; +uint8_t SPICS = 0; +SPIClass *pSPI; CAN_CONFIG config; @@ -13,7 +13,6 @@ CAN_RX_FIFO_EVENT rxFlags; CAN_RX_MSGOBJ rxObj; uint8_t rxd[MAX_DATA_BYTES]; - // Transmit objects CAN_TX_FIFO_CONFIG txConfig; CAN_TX_FIFO_EVENT txFlags; @@ -23,11 +22,11 @@ uint8_t txd[MAX_DATA_BYTES]; CAN_TX_QUEUE_CONFIG txqueConfig; // Message IDs -#define TX_REQUEST_ID 0x300 -#define TX_RESPONSE_ID 0x301 -#define BUTTON_STATUS_ID 0x201 -#define LED_STATUS_ID 0x200 -#define PAYLOAD_ID 0x101 +#define TX_REQUEST_ID 0x300 +#define TX_RESPONSE_ID 0x301 +#define BUTTON_STATUS_ID 0x201 +#define LED_STATUS_ID 0x200 +#define PAYLOAD_ID 0x101 #define MAX_TXQUEUE_ATTEMPTS 50 @@ -40,7 +39,6 @@ CAN_TX_QUEUE_CONFIG txqueConfig; // Maximum number of data bytes in message #define MAX_DATA_BYTES 64 - CAN_BUS_DIAGNOSTIC busDiagnostics; uint8_t tec; uint8_t rec; @@ -51,45 +49,44 @@ CAN_ERROR_STATE errorFlags; // Section: Variables //! SPI Transmit buffer -uint8_t spiTransmitBuffer[SPI_DEFAULT_BUFFER_LENGTH+2]; +uint8_t spiTransmitBuffer[SPI_DEFAULT_BUFFER_LENGTH + 2]; //! SPI Receive buffer uint8_t spiReceiveBuffer[SPI_DEFAULT_BUFFER_LENGTH]; - -uint16_t DRV_CANFDSPI_CalculateCRC16(uint8_t* data, uint16_t size) +uint16_t DRV_CANFDSPI_CalculateCRC16(uint8_t *data, uint16_t size) { uint16_t init = CRCBASE; uint8_t index; - while (size-- != 0) { - index = ((uint8_t*) & init)[CRCUPPER] ^ *data++; + while (size-- != 0) + { + index = ((uint8_t *)&init)[CRCUPPER] ^ *data++; init = (init << 8) ^ crc16_table[index]; } return init; } - - /********************************************************************************************************* ** Function name: MCP_CAN ** Descriptions: Constructor *********************************************************************************************************/ -void mcp2518fd::mcp_canbus(uint8_t _CS) { +void mcp2518fd::mcp_canbus(uint8_t _CS) +{ nReservedTx = 0; - pSPI = &SPI; + pSPI = &SPI; init_CS(_CS); } - - /********************************************************************************************************* ** Function name: init_CS ** Descriptions: init CS pin and set UNSELECTED *********************************************************************************************************/ -void mcp2518fd::init_CS(byte _CS) { - if (_CS == 0) { +void mcp2518fd::init_CS(byte _CS) +{ + if (_CS == 0) + { return; } SPICS = _CS; @@ -97,105 +94,101 @@ void mcp2518fd::init_CS(byte _CS) { MCP2518fd_UNSELECT(); } - /********************************************************************************************************* ** Function name: begin ** Descriptions: init can and set speed *********************************************************************************************************/ -byte mcp2518fd::begin(byte speedset) { +byte mcp2518fd::begin(byte speedset) +{ SPI.begin(); byte res = mcp2518fd_init(speedset); return ((res == MCP2518fd_OK) ? CAN_OK : CAN_FAILINIT); } - /********************************************************************************************************* ** Function name: mcp2518fd_reset ** Descriptions: reset the device *********************************************************************************************************/ -int8_t mcp2518fd::mcp2518fd_reset(void) { +int8_t mcp2518fd::mcp2518fd_reset(void) +{ uint16_t spiTransferSize = 2; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) (cINSTRUCTION_RESET << 4); + spiTransmitBuffer[0] = (uint8_t)(cINSTRUCTION_RESET << 4); spiTransmitBuffer[1] = 0; - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReadByte(uint16_t address, uint8_t *rxd) { uint16_t spiTransferSize = 3; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); spiTransmitBuffer[2] = 0; - - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); spiReceiveBuffer[2] = spi_readwrite(0x00); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); - Serial.printf("DRV_CANFDSPI_ReadByte = %d\n\r",spiReceiveBuffer[2]); + Serial.printf("DRV_CANFDSPI_ReadByte = %d\n\r", spiReceiveBuffer[2]); // Update data *rxd = spiReceiveBuffer[2]; return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_WriteByte(uint16_t address, uint8_t txd) { uint16_t spiTransferSize = 3; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); spiTransmitBuffer[2] = txd; - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); spi_readwrite(spiTransmitBuffer[2]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReadWord(uint16_t address, uint32_t *rxd) { uint8_t i; @@ -204,53 +197,55 @@ int8_t mcp2518fd::mcp2518fd_ReadWord(uint16_t address, uint32_t *rxd) int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); - for (i = 2; i < 6; i++) { + for (i = 2; i < 6; i++) + { spiReceiveBuffer[i] = spi_readwrite(0x00); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); // Update data *rxd = 0; - for (i = 2; i < 6; i++) { - x = (uint32_t) spiReceiveBuffer[i]; - *rxd += x << ((i - 2)*8); + for (i = 2; i < 6; i++) + { + x = (uint32_t)spiReceiveBuffer[i]; + *rxd += x << ((i - 2) * 8); } return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_WriteWord(uint16_t address,uint32_t txd) +int8_t mcp2518fd::mcp2518fd_WriteWord(uint16_t address, uint32_t txd) { uint8_t i; uint16_t spiTransferSize = 6; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); // Split word into 4 bytes and add them to buffer - for (i = 0; i < 4; i++) { - spiTransmitBuffer[i + 2] = (uint8_t) ((txd >> (i * 8)) & 0xFF); + for (i = 0; i < 4; i++) + { + spiTransmitBuffer[i + 2] = (uint8_t)((txd >> (i * 8)) & 0xFF); } - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); @@ -259,15 +254,14 @@ int8_t mcp2518fd::mcp2518fd_WriteWord(uint16_t address,uint32_t txd) spi_readwrite(spiTransmitBuffer[4]); spi_readwrite(spiTransmitBuffer[5]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReadHalfWord(uint16_t address, uint16_t *rxd) { uint8_t i; @@ -276,149 +270,153 @@ int8_t mcp2518fd::mcp2518fd_ReadHalfWord(uint16_t address, uint16_t *rxd) int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); - for (i = 2; i < 4; i++) { + for (i = 2; i < 4; i++) + { spiReceiveBuffer[i] = spi_readwrite(0x00); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); // Update data *rxd = 0; - for (i = 2; i < 4; i++) { - x = (uint32_t) spiReceiveBuffer[i]; - *rxd += x << ((i - 2)*8); + for (i = 2; i < 4; i++) + { + x = (uint32_t)spiReceiveBuffer[i]; + *rxd += x << ((i - 2) * 8); } return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_WriteHalfWord(uint16_t address,uint16_t txd) +int8_t mcp2518fd::mcp2518fd_WriteHalfWord(uint16_t address, uint16_t txd) { uint8_t i; uint16_t spiTransferSize = 4; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); // Split word into 2 bytes and add them to buffer - for (i = 0; i < 2; i++) { - spiTransmitBuffer[i + 2] = (uint8_t) ((txd >> (i * 8)) & 0xFF); + for (i = 0; i < 2; i++) + { + spiTransmitBuffer[i + 2] = (uint8_t)((txd >> (i * 8)) & 0xFF); } - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); spi_readwrite(spiTransmitBuffer[2]); - spi_readwrite(spiTransmitBuffer[3]); + spi_readwrite(spiTransmitBuffer[3]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_ReadByteArray(uint16_t address,uint8_t *rxd, uint16_t nBytes) +int8_t mcp2518fd::mcp2518fd_ReadByteArray(uint16_t address, uint8_t *rxd, uint16_t nBytes) { uint16_t i; uint16_t spiTransferSize = nBytes + 2; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_READ << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); // Clear data - for (i = 2; i < spiTransferSize; i++) { + for (i = 2; i < spiTransferSize; i++) + { spiTransmitBuffer[i] = 0; } - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { spiReceiveBuffer[i + 2] = spi_readwrite(0x00); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); // Update data - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { rxd[i] = spiReceiveBuffer[i + 2]; } return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_WriteByteArray(uint16_t address,uint8_t *txd, uint16_t nBytes) +int8_t mcp2518fd::mcp2518fd_WriteByteArray(uint16_t address, uint8_t *txd, uint16_t nBytes) { uint16_t i; uint16_t spiTransferSize = nBytes + 2; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); // Add data - for (i = 2; i < spiTransferSize; i++) { + for (i = 2; i < spiTransferSize; i++) + { spiTransmitBuffer[i] = txd[i - 2]; } - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); - for (i = 2; i < spiTransferSize; i++) { + for (i = 2; i < spiTransferSize; i++) + { spi_readwrite(spiTransmitBuffer[i]); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_WriteByteSafe(uint16_t address,uint8_t txd) +int8_t mcp2518fd::mcp2518fd_WriteByteSafe(uint16_t address, uint8_t txd) { uint16_t crcResult = 0; uint16_t spiTransferSize = 5; int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE_SAFE << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE_SAFE << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); spiTransmitBuffer[2] = txd; // Add CRC @@ -426,10 +424,9 @@ int8_t mcp2518fd::mcp2518fd_WriteByteSafe(uint16_t address,uint8_t txd) spiTransmitBuffer[3] = (crcResult >> 8) & 0xFF; spiTransmitBuffer[4] = crcResult & 0xFF; - - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); @@ -437,16 +434,15 @@ int8_t mcp2518fd::mcp2518fd_WriteByteSafe(uint16_t address,uint8_t txd) spi_readwrite(spiTransmitBuffer[3]); spi_readwrite(spiTransmitBuffer[4]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_WriteWordSafe(uint16_t address,uint32_t txd) +int8_t mcp2518fd::mcp2518fd_WriteWordSafe(uint16_t address, uint32_t txd) { uint8_t i; uint16_t crcResult = 0; @@ -454,12 +450,13 @@ int8_t mcp2518fd::mcp2518fd_WriteWordSafe(uint16_t address,uint32_t txd) int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE_SAFE << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE_SAFE << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); // Split word into 4 bytes and add them to buffer - for (i = 0; i < 4; i++) { - spiTransmitBuffer[i + 2] = (uint8_t) ((txd >> (i * 8)) & 0xFF); + for (i = 0; i < 4; i++) + { + spiTransmitBuffer[i + 2] = (uint8_t)((txd >> (i * 8)) & 0xFF); } // Add CRC @@ -467,9 +464,9 @@ int8_t mcp2518fd::mcp2518fd_WriteWordSafe(uint16_t address,uint32_t txd) spiTransmitBuffer[6] = (crcResult >> 8) & 0xFF; spiTransmitBuffer[7] = crcResult & 0xFF; - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); @@ -480,17 +477,16 @@ int8_t mcp2518fd::mcp2518fd_WriteWordSafe(uint16_t address,uint32_t txd) spi_readwrite(spiTransmitBuffer[6]); spi_readwrite(spiTransmitBuffer[7]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReadByteArrayWithCRC(uint16_t address, - uint8_t *rxd, uint16_t nBytes, bool fromRam, bool* crcIsCorrect) + uint8_t *rxd, uint16_t nBytes, bool fromRam, bool *crcIsCorrect) { uint8_t i; uint16_t crcFromSpiSlave = 0; @@ -499,38 +495,42 @@ int8_t mcp2518fd::mcp2518fd_ReadByteArrayWithCRC(uint16_t address, int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_READ_CRC << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); - if (fromRam) { + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_READ_CRC << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); + if (fromRam) + { spiTransmitBuffer[2] = nBytes >> 2; - } else { + } + else + { spiTransmitBuffer[2] = nBytes; } // Clear data - for (i = 3; i < spiTransferSize; i++) { + for (i = 3; i < spiTransferSize; i++) + { spiTransmitBuffer[i] = 0; } - - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); spi_readwrite(spiTransmitBuffer[2]); - for (i = 3; i < spiTransferSize; i++) { + for (i = 3; i < spiTransferSize; i++) + { spiReceiveBuffer[i] = spi_readwrite(0x00); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); // Get CRC from controller - crcFromSpiSlave = (uint16_t) (spiReceiveBuffer[spiTransferSize - 2] << 8) + (uint16_t) (spiReceiveBuffer[spiTransferSize - 1]); + crcFromSpiSlave = (uint16_t)(spiReceiveBuffer[spiTransferSize - 2] << 8) + (uint16_t)(spiReceiveBuffer[spiTransferSize - 1]); // Use the receive buffer to calculate CRC // First three bytes need to be command @@ -540,23 +540,26 @@ int8_t mcp2518fd::mcp2518fd_ReadByteArrayWithCRC(uint16_t address, crcAtController = DRV_CANFDSPI_CalculateCRC16(spiReceiveBuffer, nBytes + 3); // Compare CRC readings - if (crcFromSpiSlave == crcAtController) { + if (crcFromSpiSlave == crcAtController) + { *crcIsCorrect = true; - } else { + } + else + { *crcIsCorrect = false; } // Update data - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { rxd[i] = spiReceiveBuffer[i + 3]; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_WriteByteArrayWithCRC(uint16_t address, - uint8_t *txd, uint16_t nBytes, bool fromRam) + uint8_t *txd, uint16_t nBytes, bool fromRam) { uint16_t i; uint16_t crcResult = 0; @@ -564,49 +567,52 @@ int8_t mcp2518fd::mcp2518fd_WriteByteArrayWithCRC(uint16_t address, int8_t spiTransferError = 0; // Compose command - spiTransmitBuffer[0] = (uint8_t) ((cINSTRUCTION_WRITE_CRC << 4) + ((address >> 8) & 0xF)); - spiTransmitBuffer[1] = (uint8_t) (address & 0xFF); - if (fromRam) { + spiTransmitBuffer[0] = (uint8_t)((cINSTRUCTION_WRITE_CRC << 4) + ((address >> 8) & 0xF)); + spiTransmitBuffer[1] = (uint8_t)(address & 0xFF); + if (fromRam) + { spiTransmitBuffer[2] = nBytes >> 2; - } else { + } + else + { spiTransmitBuffer[2] = nBytes; } // Add data - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { spiTransmitBuffer[i + 3] = txd[i]; } // Add CRC crcResult = DRV_CANFDSPI_CalculateCRC16(spiTransmitBuffer, spiTransferSize - 2); - spiTransmitBuffer[spiTransferSize - 2] = (uint8_t) ((crcResult >> 8) & 0xFF); - spiTransmitBuffer[spiTransferSize - 1] = (uint8_t) (crcResult & 0xFF); + spiTransmitBuffer[spiTransferSize - 2] = (uint8_t)((crcResult >> 8) & 0xFF); + spiTransmitBuffer[spiTransferSize - 1] = (uint8_t)(crcResult & 0xFF); - - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); spi_readwrite(spiTransmitBuffer[2]); - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { spi_readwrite(spiTransmitBuffer[i + 3]); } spi_readwrite(spiTransmitBuffer[spiTransferSize - 2]); spi_readwrite(spiTransmitBuffer[spiTransferSize - 1]); MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReadWordArray(uint16_t address, - uint32_t *rxd, uint16_t nWords) + uint32_t *rxd, uint16_t nWords) { uint16_t i, j, n; REG_t w; @@ -618,30 +624,34 @@ int8_t mcp2518fd::mcp2518fd_ReadWordArray(uint16_t address, spiTransmitBuffer[1] = address & 0xFF; // Clear data - for (i = 2; i < spiTransferSize; i++) { + for (i = 2; i < spiTransferSize; i++) + { spiTransmitBuffer[i] = 0; } - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); - for (i = 2; i < spiTransferSize; i++) { - //for (i = 2; i < 6; i++) { + for (i = 2; i < spiTransferSize; i++) + { + //for (i = 2; i < 6; i++) { spiReceiveBuffer[i] = spi_readwrite(0x00); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); // Convert Byte array to Word array n = 2; - for (i = 0; i < nWords; i++) { + for (i = 0; i < nWords; i++) + { w.word = 0; - for (j = 0; j < 4; j++, n++) { + for (j = 0; j < 4; j++, n++) + { w.byte[j] = spiReceiveBuffer[n]; } rxd[i] = w.word; @@ -649,9 +659,8 @@ int8_t mcp2518fd::mcp2518fd_ReadWordArray(uint16_t address, return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_WriteWordArray(uint16_t address, - uint32_t *txd, uint16_t nWords) + uint32_t *txd, uint16_t nWords) { uint16_t i, j, n; REG_t w; @@ -664,32 +673,34 @@ int8_t mcp2518fd::mcp2518fd_WriteWordArray(uint16_t address, // Convert ByteArray to word array n = 2; - for (i = 0; i < nWords; i++) { + for (i = 0; i < nWords; i++) + { w.word = txd[i]; - for (j = 0; j < 4; j++, n++) { + for (j = 0; j < 4; j++, n++) + { spiTransmitBuffer[n] = w.byte[j]; } } - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_BEGIN(); - #endif +#endif MCP2518fd_SELECT(); spi_readwrite(spiTransmitBuffer[0]); spi_readwrite(spiTransmitBuffer[1]); - for (i = 2; i < spiTransferSize; i++) { + for (i = 2; i < spiTransferSize; i++) + { spi_readwrite(spiTransmitBuffer[i]); } MCP2518fd_UNSELECT(); - #ifdef SPI_HAS_TRANSACTION +#ifdef SPI_HAS_TRANSACTION SPI_END(); - #endif +#endif delay(10); return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_EccEnable() { int8_t spiTransferError = 0; @@ -697,7 +708,8 @@ int8_t mcp2518fd::mcp2518fd_EccEnable() // Read spiTransferError = mcp2518fd_ReadByte(cREGADDR_ECCCON, &d); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Modify @@ -705,14 +717,14 @@ int8_t mcp2518fd::mcp2518fd_EccEnable() // Write spiTransferError = mcp2518fd_WriteByte(cREGADDR_ECCCON, d); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return 0; } - int8_t mcp2518fd::mcp2518fd_RamInit(uint8_t d) { uint8_t txd[SPI_DEFAULT_BUFFER_LENGTH]; @@ -720,15 +732,18 @@ int8_t mcp2518fd::mcp2518fd_RamInit(uint8_t d) int8_t spiTransferError = 0; // Prepare data - for (k = 0; k < SPI_DEFAULT_BUFFER_LENGTH; k++) { + for (k = 0; k < SPI_DEFAULT_BUFFER_LENGTH; k++) + { txd[k] = d; } uint16_t a = cRAMADDR_START; - for (k = 0; k < (cRAM_SIZE / SPI_DEFAULT_BUFFER_LENGTH); k++) { + for (k = 0; k < (cRAM_SIZE / SPI_DEFAULT_BUFFER_LENGTH); k++) + { spiTransferError = mcp2518fd_WriteByteArray(a, txd, SPI_DEFAULT_BUFFER_LENGTH); - if (spiTransferError) { + if (spiTransferError) + { return -1; } a += SPI_DEFAULT_BUFFER_LENGTH; @@ -737,7 +752,7 @@ int8_t mcp2518fd::mcp2518fd_RamInit(uint8_t d) return spiTransferError; } -int8_t mcp2518fd::mcp2518fd_ConfigureObjectReset(CAN_CONFIG* config) +int8_t mcp2518fd::mcp2518fd_ConfigureObjectReset(CAN_CONFIG *config) { REG_CiCON ciCon; ciCon.word = canControlResetValues[cREGADDR_CiCON / 4]; @@ -758,7 +773,7 @@ int8_t mcp2518fd::mcp2518fd_ConfigureObjectReset(CAN_CONFIG* config) return 0; } -int8_t mcp2518fd::mcp2518fd_Configure(CAN_CONFIG* config) +int8_t mcp2518fd::mcp2518fd_Configure(CAN_CONFIG *config) { REG_CiCON ciCon; int8_t spiTransferError = 0; @@ -779,15 +794,15 @@ int8_t mcp2518fd::mcp2518fd_Configure(CAN_CONFIG* config) ciCon.bF.TxBandWidthSharing = config->TxBandWidthSharing; spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiCON, ciCon.word); - if (spiTransferError) { + if (spiTransferError) + { return -1; } return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_TransmitChannelConfigureObjectReset(CAN_TX_FIFO_CONFIG* config) +int8_t mcp2518fd::mcp2518fd_TransmitChannelConfigureObjectReset(CAN_TX_FIFO_CONFIG *config) { REG_CiFIFOCON ciFifoCon; ciFifoCon.word = canFifoResetValues[0]; //10010010100101010000 @@ -801,9 +816,8 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelConfigureObjectReset(CAN_TX_FIFO_CONF return 0; } - int8_t mcp2518fd::mcp2518fd_TransmitChannelConfigure( - CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_CONFIG* config) + CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_CONFIG *config) { int8_t spiTransferError = 0; uint16_t a = 0; @@ -820,12 +834,11 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelConfigure( a = cREGADDR_CiFIFOCON + (channel * CiFIFO_OFFSET); spiTransferError = mcp2518fd_WriteWord(a, ciFifoCon.word); - + return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_ReceiveChannelConfigureObjectReset(CAN_RX_FIFO_CONFIG* config) +int8_t mcp2518fd::mcp2518fd_ReceiveChannelConfigureObjectReset(CAN_RX_FIFO_CONFIG *config) { REG_CiFIFOCON ciFifoCon; ciFifoCon.word = canFifoResetValues[0]; @@ -837,14 +850,14 @@ int8_t mcp2518fd::mcp2518fd_ReceiveChannelConfigureObjectReset(CAN_RX_FIFO_CONFI return 0; } - int8_t mcp2518fd::mcp2518fd_ReceiveChannelConfigure( - CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_CONFIG* config) + CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_CONFIG *config) { int8_t spiTransferError = 0; uint16_t a = 0; - if (channel == CAN_TXQUEUE_CH0) { + if (channel == CAN_TXQUEUE_CH0) + { return -100; } @@ -864,9 +877,8 @@ int8_t mcp2518fd::mcp2518fd_ReceiveChannelConfigure( return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_FilterObjectConfigure( - CAN_FILTER filter, CAN_FILTEROBJ_ID* id) + CAN_FILTER filter, CAN_FILTEROBJ_ID *id) { uint16_t a; REG_CiFLTOBJ fObj; @@ -882,9 +894,8 @@ int8_t mcp2518fd::mcp2518fd_FilterObjectConfigure( return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_FilterMaskConfigure( - CAN_FILTER filter, CAN_MASKOBJ_ID* mask) + CAN_FILTER filter, CAN_MASKOBJ_ID *mask) { uint16_t a; REG_CiMASK mObj; @@ -900,18 +911,20 @@ int8_t mcp2518fd::mcp2518fd_FilterMaskConfigure( return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_FilterToFifoLink( - CAN_FILTER filter, CAN_FIFO_CHANNEL channel, bool enable) + CAN_FILTER filter, CAN_FIFO_CHANNEL channel, bool enable) { uint16_t a; REG_CiFLTCON_BYTE fCtrl; int8_t spiTransferError = 0; // Enable - if (enable) { + if (enable) + { fCtrl.bF.Enable = 1; - } else { + } + else + { fCtrl.bF.Enable = 0; } @@ -924,7 +937,6 @@ int8_t mcp2518fd::mcp2518fd_FilterToFifoLink( return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal40MHz(CAN_BITTIME_SETUP bitTime) { int8_t spiTransferError = 0; @@ -933,54 +945,55 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal40MHz(CAN_BITTIME_SETUP bitTi ciNbtcfg.word = canControlResetValues[cREGADDR_CiNBTCFG / 4]; // Arbitration Bit rate - switch (bitTime) { - // All 500K - case CAN_500K_1M: - case CAN_500K_2M: - case CAN_500K_3M: - case CAN_500K_4M: - case CAN_500K_5M: - case CAN_500K_6M7: - case CAN_500K_8M: - case CAN_500K_10M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 62; - ciNbtcfg.bF.TSEG2 = 15; - ciNbtcfg.bF.SJW = 15; - break; + switch (bitTime) + { + // All 500K + case CAN_500K_1M: + case CAN_500K_2M: + case CAN_500K_3M: + case CAN_500K_4M: + case CAN_500K_5M: + case CAN_500K_6M7: + case CAN_500K_8M: + case CAN_500K_10M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 62; + ciNbtcfg.bF.TSEG2 = 15; + ciNbtcfg.bF.SJW = 15; + break; - // All 250K - case CAN_250K_500K: - case CAN_250K_833K: - case CAN_250K_1M: - case CAN_250K_1M5: - case CAN_250K_2M: - case CAN_250K_3M: - case CAN_250K_4M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 126; - ciNbtcfg.bF.TSEG2 = 31; - ciNbtcfg.bF.SJW = 31; - break; + // All 250K + case CAN_250K_500K: + case CAN_250K_833K: + case CAN_250K_1M: + case CAN_250K_1M5: + case CAN_250K_2M: + case CAN_250K_3M: + case CAN_250K_4M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 126; + ciNbtcfg.bF.TSEG2 = 31; + ciNbtcfg.bF.SJW = 31; + break; - case CAN_1000K_4M: - case CAN_1000K_8M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 30; - ciNbtcfg.bF.TSEG2 = 7; - ciNbtcfg.bF.SJW = 7; - break; + case CAN_1000K_4M: + case CAN_1000K_8M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 30; + ciNbtcfg.bF.TSEG2 = 7; + ciNbtcfg.bF.SJW = 7; + break; - case CAN_125K_500K: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 254; - ciNbtcfg.bF.TSEG2 = 63; - ciNbtcfg.bF.SJW = 63; - break; + case CAN_125K_500K: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 254; + ciNbtcfg.bF.TSEG2 = 63; + ciNbtcfg.bF.SJW = 63; + break; - default: - return -1; - break; + default: + return -1; + break; } // Write Bit time registers @@ -989,7 +1002,6 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal40MHz(CAN_BITTIME_SETUP bitTi return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData40MHz(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode) { int8_t spiTransferError = 0; @@ -1005,165 +1017,167 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData40MHz(CAN_BITTIME_SETUP bitTime, uint32_t tdcValue = 0; // Data Bit rate and SSP - switch (bitTime) { - case CAN_500K_1M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 30; - ciDbtcfg.bF.TSEG2 = 7; - ciDbtcfg.bF.SJW = 7; - // SSP - ciTdc.bF.TDCOffset = 31; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_2M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 14; - ciDbtcfg.bF.TSEG2 = 3; - ciDbtcfg.bF.SJW = 3; - // SSP - ciTdc.bF.TDCOffset = 15; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_3M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 8; - ciDbtcfg.bF.TSEG2 = 2; - ciDbtcfg.bF.SJW = 2; - // SSP - ciTdc.bF.TDCOffset = 9; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_4M: - case CAN_1000K_4M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 6; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 7; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_5M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 4; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 5; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_6M7: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 3; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 4; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_8M: - case CAN_1000K_8M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 2; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 3; - ciTdc.bF.TDCValue = 1; - break; - case CAN_500K_10M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 1; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 2; - ciTdc.bF.TDCValue = 0; - break; + switch (bitTime) + { + case CAN_500K_1M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 30; + ciDbtcfg.bF.TSEG2 = 7; + ciDbtcfg.bF.SJW = 7; + // SSP + ciTdc.bF.TDCOffset = 31; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_2M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 14; + ciDbtcfg.bF.TSEG2 = 3; + ciDbtcfg.bF.SJW = 3; + // SSP + ciTdc.bF.TDCOffset = 15; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_3M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 8; + ciDbtcfg.bF.TSEG2 = 2; + ciDbtcfg.bF.SJW = 2; + // SSP + ciTdc.bF.TDCOffset = 9; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_4M: + case CAN_1000K_4M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 6; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 7; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_5M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 4; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 5; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_6M7: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 3; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 4; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_8M: + case CAN_1000K_8M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 2; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 3; + ciTdc.bF.TDCValue = 1; + break; + case CAN_500K_10M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 1; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 2; + ciTdc.bF.TDCValue = 0; + break; - case CAN_250K_500K: - case CAN_125K_500K: - ciDbtcfg.bF.BRP = 1; - ciDbtcfg.bF.TSEG1 = 30; - ciDbtcfg.bF.TSEG2 = 7; - ciDbtcfg.bF.SJW = 7; - // SSP - ciTdc.bF.TDCOffset = 31; - ciTdc.bF.TDCValue = tdcValue; - ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; - break; - case CAN_250K_833K: - ciDbtcfg.bF.BRP = 1; - ciDbtcfg.bF.TSEG1 = 17; - ciDbtcfg.bF.TSEG2 = 4; - ciDbtcfg.bF.SJW = 4; - // SSP - ciTdc.bF.TDCOffset = 18; - ciTdc.bF.TDCValue = tdcValue; - ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; - break; - case CAN_250K_1M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 30; - ciDbtcfg.bF.TSEG2 = 7; - ciDbtcfg.bF.SJW = 7; - // SSP - ciTdc.bF.TDCOffset = 31; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_1M5: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 18; - ciDbtcfg.bF.TSEG2 = 5; - ciDbtcfg.bF.SJW = 5; - // SSP - ciTdc.bF.TDCOffset = 19; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_2M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 14; - ciDbtcfg.bF.TSEG2 = 3; - ciDbtcfg.bF.SJW = 3; - // SSP - ciTdc.bF.TDCOffset = 15; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_3M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 8; - ciDbtcfg.bF.TSEG2 = 2; - ciDbtcfg.bF.SJW = 2; - // SSP - ciTdc.bF.TDCOffset = 9; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_4M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 6; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 7; - ciTdc.bF.TDCValue = tdcValue; - break; + case CAN_250K_500K: + case CAN_125K_500K: + ciDbtcfg.bF.BRP = 1; + ciDbtcfg.bF.TSEG1 = 30; + ciDbtcfg.bF.TSEG2 = 7; + ciDbtcfg.bF.SJW = 7; + // SSP + ciTdc.bF.TDCOffset = 31; + ciTdc.bF.TDCValue = tdcValue; + ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; + break; + case CAN_250K_833K: + ciDbtcfg.bF.BRP = 1; + ciDbtcfg.bF.TSEG1 = 17; + ciDbtcfg.bF.TSEG2 = 4; + ciDbtcfg.bF.SJW = 4; + // SSP + ciTdc.bF.TDCOffset = 18; + ciTdc.bF.TDCValue = tdcValue; + ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; + break; + case CAN_250K_1M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 30; + ciDbtcfg.bF.TSEG2 = 7; + ciDbtcfg.bF.SJW = 7; + // SSP + ciTdc.bF.TDCOffset = 31; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_1M5: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 18; + ciDbtcfg.bF.TSEG2 = 5; + ciDbtcfg.bF.SJW = 5; + // SSP + ciTdc.bF.TDCOffset = 19; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_2M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 14; + ciDbtcfg.bF.TSEG2 = 3; + ciDbtcfg.bF.SJW = 3; + // SSP + ciTdc.bF.TDCOffset = 15; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_3M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 8; + ciDbtcfg.bF.TSEG2 = 2; + ciDbtcfg.bF.SJW = 2; + // SSP + ciTdc.bF.TDCOffset = 9; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_4M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 6; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 7; + ciTdc.bF.TDCValue = tdcValue; + break; - default: - return -1; - break; + default: + return -1; + break; } // Write Bit time registers spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiDBTCFG, ciDbtcfg.word); - if (spiTransferError) { + if (spiTransferError) + { return -2; } @@ -1174,14 +1188,14 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData40MHz(CAN_BITTIME_SETUP bitTime, #endif spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiTDC, ciTdc.word); - if (spiTransferError) { + if (spiTransferError) + { return -3; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal20MHz(CAN_BITTIME_SETUP bitTime) { int8_t spiTransferError = 0; @@ -1190,65 +1204,66 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal20MHz(CAN_BITTIME_SETUP bitTi ciNbtcfg.word = canControlResetValues[cREGADDR_CiNBTCFG / 4]; // Arbitration Bit rate - switch (bitTime) { - // All 500K - case CAN_500K_1M: - case CAN_500K_2M: - case CAN_500K_4M: - case CAN_500K_5M: - case CAN_500K_6M7: - case CAN_500K_8M: - case CAN_500K_10M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 30; - ciNbtcfg.bF.TSEG2 = 7; - ciNbtcfg.bF.SJW = 7; - break; + switch (bitTime) + { + // All 500K + case CAN_500K_1M: + case CAN_500K_2M: + case CAN_500K_4M: + case CAN_500K_5M: + case CAN_500K_6M7: + case CAN_500K_8M: + case CAN_500K_10M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 30; + ciNbtcfg.bF.TSEG2 = 7; + ciNbtcfg.bF.SJW = 7; + break; - // All 250K - case CAN_250K_500K: - case CAN_250K_833K: - case CAN_250K_1M: - case CAN_250K_1M5: - case CAN_250K_2M: - case CAN_250K_3M: - case CAN_250K_4M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 62; - ciNbtcfg.bF.TSEG2 = 15; - ciNbtcfg.bF.SJW = 15; - break; + // All 250K + case CAN_250K_500K: + case CAN_250K_833K: + case CAN_250K_1M: + case CAN_250K_1M5: + case CAN_250K_2M: + case CAN_250K_3M: + case CAN_250K_4M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 62; + ciNbtcfg.bF.TSEG2 = 15; + ciNbtcfg.bF.SJW = 15; + break; - case CAN_1000K_4M: - case CAN_1000K_8M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 14; - ciNbtcfg.bF.TSEG2 = 3; - ciNbtcfg.bF.SJW = 3; - break; + case CAN_1000K_4M: + case CAN_1000K_8M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 14; + ciNbtcfg.bF.TSEG2 = 3; + ciNbtcfg.bF.SJW = 3; + break; - case CAN_125K_500K: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 126; - ciNbtcfg.bF.TSEG2 = 31; - ciNbtcfg.bF.SJW = 31; - break; + case CAN_125K_500K: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 126; + ciNbtcfg.bF.TSEG2 = 31; + ciNbtcfg.bF.SJW = 31; + break; - default: - return -1; - break; + default: + return -1; + break; } // Write Bit time registers spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiNBTCFG, ciNbtcfg.word); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData20MHz(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode) { int8_t spiTransferError = 0; @@ -1264,126 +1279,128 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData20MHz(CAN_BITTIME_SETUP bitTime, uint32_t tdcValue = 0; // Data Bit rate and SSP - switch (bitTime) { - case CAN_500K_1M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 14; - ciDbtcfg.bF.TSEG2 = 3; - ciDbtcfg.bF.SJW = 3; - // SSP - ciTdc.bF.TDCOffset = 15; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_2M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 6; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 7; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_4M: - case CAN_1000K_4M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 2; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 3; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_5M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 1; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 2; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_6M7: - case CAN_500K_8M: - case CAN_500K_10M: - case CAN_1000K_8M: - //qDebug("Data Bitrate not feasible with this clock!"); - return -1; - break; + switch (bitTime) + { + case CAN_500K_1M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 14; + ciDbtcfg.bF.TSEG2 = 3; + ciDbtcfg.bF.SJW = 3; + // SSP + ciTdc.bF.TDCOffset = 15; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_2M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 6; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 7; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_4M: + case CAN_1000K_4M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 2; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 3; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_5M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 1; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 2; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_6M7: + case CAN_500K_8M: + case CAN_500K_10M: + case CAN_1000K_8M: + //qDebug("Data Bitrate not feasible with this clock!"); + return -1; + break; - case CAN_250K_500K: - case CAN_125K_500K: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 30; - ciDbtcfg.bF.TSEG2 = 7; - ciDbtcfg.bF.SJW = 7; - // SSP - ciTdc.bF.TDCOffset = 31; - ciTdc.bF.TDCValue = tdcValue; - ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; - break; - case CAN_250K_833K: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 17; - ciDbtcfg.bF.TSEG2 = 4; - ciDbtcfg.bF.SJW = 4; - // SSP - ciTdc.bF.TDCOffset = 18; - ciTdc.bF.TDCValue = tdcValue; - ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; - break; - case CAN_250K_1M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 14; - ciDbtcfg.bF.TSEG2 = 3; - ciDbtcfg.bF.SJW = 3; - // SSP - ciTdc.bF.TDCOffset = 15; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_1M5: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 8; - ciDbtcfg.bF.TSEG2 = 2; - ciDbtcfg.bF.SJW = 2; - // SSP - ciTdc.bF.TDCOffset = 9; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_2M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 6; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 7; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_3M: - //qDebug("Data Bitrate not feasible with this clock!"); - return -1; - break; - case CAN_250K_4M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 2; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 3; - ciTdc.bF.TDCValue = tdcValue; - break; + case CAN_250K_500K: + case CAN_125K_500K: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 30; + ciDbtcfg.bF.TSEG2 = 7; + ciDbtcfg.bF.SJW = 7; + // SSP + ciTdc.bF.TDCOffset = 31; + ciTdc.bF.TDCValue = tdcValue; + ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; + break; + case CAN_250K_833K: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 17; + ciDbtcfg.bF.TSEG2 = 4; + ciDbtcfg.bF.SJW = 4; + // SSP + ciTdc.bF.TDCOffset = 18; + ciTdc.bF.TDCValue = tdcValue; + ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; + break; + case CAN_250K_1M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 14; + ciDbtcfg.bF.TSEG2 = 3; + ciDbtcfg.bF.SJW = 3; + // SSP + ciTdc.bF.TDCOffset = 15; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_1M5: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 8; + ciDbtcfg.bF.TSEG2 = 2; + ciDbtcfg.bF.SJW = 2; + // SSP + ciTdc.bF.TDCOffset = 9; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_2M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 6; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 7; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_3M: + //qDebug("Data Bitrate not feasible with this clock!"); + return -1; + break; + case CAN_250K_4M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 2; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 3; + ciTdc.bF.TDCValue = tdcValue; + break; - default: - return -1; - break; + default: + return -1; + break; } // Write Bit time registers spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiDBTCFG, ciDbtcfg.word); - if (spiTransferError) { + if (spiTransferError) + { return -2; } @@ -1394,14 +1411,14 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData20MHz(CAN_BITTIME_SETUP bitTime, #endif spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiTDC, ciTdc.word); - if (spiTransferError) { + if (spiTransferError) + { return -3; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal10MHz(CAN_BITTIME_SETUP bitTime) { int8_t spiTransferError = 0; @@ -1410,65 +1427,66 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureNominal10MHz(CAN_BITTIME_SETUP bitTi ciNbtcfg.word = canControlResetValues[cREGADDR_CiNBTCFG / 4]; // Arbitration Bit rate - switch (bitTime) { - // All 500K - case CAN_500K_1M: - case CAN_500K_2M: - case CAN_500K_4M: - case CAN_500K_5M: - case CAN_500K_6M7: - case CAN_500K_8M: - case CAN_500K_10M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 14; - ciNbtcfg.bF.TSEG2 = 3; - ciNbtcfg.bF.SJW = 3; - break; + switch (bitTime) + { + // All 500K + case CAN_500K_1M: + case CAN_500K_2M: + case CAN_500K_4M: + case CAN_500K_5M: + case CAN_500K_6M7: + case CAN_500K_8M: + case CAN_500K_10M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 14; + ciNbtcfg.bF.TSEG2 = 3; + ciNbtcfg.bF.SJW = 3; + break; - // All 250K - case CAN_250K_500K: - case CAN_250K_833K: - case CAN_250K_1M: - case CAN_250K_1M5: - case CAN_250K_2M: - case CAN_250K_3M: - case CAN_250K_4M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 30; - ciNbtcfg.bF.TSEG2 = 7; - ciNbtcfg.bF.SJW = 7; - break; + // All 250K + case CAN_250K_500K: + case CAN_250K_833K: + case CAN_250K_1M: + case CAN_250K_1M5: + case CAN_250K_2M: + case CAN_250K_3M: + case CAN_250K_4M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 30; + ciNbtcfg.bF.TSEG2 = 7; + ciNbtcfg.bF.SJW = 7; + break; - case CAN_1000K_4M: - case CAN_1000K_8M: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 7; - ciNbtcfg.bF.TSEG2 = 2; - ciNbtcfg.bF.SJW = 2; - break; + case CAN_1000K_4M: + case CAN_1000K_8M: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 7; + ciNbtcfg.bF.TSEG2 = 2; + ciNbtcfg.bF.SJW = 2; + break; - case CAN_125K_500K: - ciNbtcfg.bF.BRP = 0; - ciNbtcfg.bF.TSEG1 = 62; - ciNbtcfg.bF.TSEG2 = 15; - ciNbtcfg.bF.SJW = 15; - break; + case CAN_125K_500K: + ciNbtcfg.bF.BRP = 0; + ciNbtcfg.bF.TSEG1 = 62; + ciNbtcfg.bF.TSEG2 = 15; + ciNbtcfg.bF.SJW = 15; + break; - default: - return -1; - break; + default: + return -1; + break; } // Write Bit time registers spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiNBTCFG, ciNbtcfg.word); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData10MHz(CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode) { int8_t spiTransferError = 0; @@ -1484,94 +1502,96 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData10MHz(CAN_BITTIME_SETUP bitTime, uint32_t tdcValue = 0; // Data Bit rate and SSP - switch (bitTime) { - case CAN_500K_1M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 6; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 7; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_2M: - // Data BR - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 2; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 3; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_500K_4M: - case CAN_500K_5M: - case CAN_500K_6M7: - case CAN_500K_8M: - case CAN_500K_10M: - case CAN_1000K_4M: - case CAN_1000K_8M: - //qDebug("Data Bitrate not feasible with this clock!"); - return -1; - break; + switch (bitTime) + { + case CAN_500K_1M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 6; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 7; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_2M: + // Data BR + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 2; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 3; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_500K_4M: + case CAN_500K_5M: + case CAN_500K_6M7: + case CAN_500K_8M: + case CAN_500K_10M: + case CAN_1000K_4M: + case CAN_1000K_8M: + //qDebug("Data Bitrate not feasible with this clock!"); + return -1; + break; - case CAN_250K_500K: - case CAN_125K_500K: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 14; - ciDbtcfg.bF.TSEG2 = 3; - ciDbtcfg.bF.SJW = 3; - // SSP - ciTdc.bF.TDCOffset = 15; - ciTdc.bF.TDCValue = tdcValue; - ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; - break; - case CAN_250K_833K: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 7; - ciDbtcfg.bF.TSEG2 = 2; - ciDbtcfg.bF.SJW = 2; - // SSP - ciTdc.bF.TDCOffset = 8; - ciTdc.bF.TDCValue = tdcValue; - ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; - break; - case CAN_250K_1M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 6; - ciDbtcfg.bF.TSEG2 = 1; - ciDbtcfg.bF.SJW = 1; - // SSP - ciTdc.bF.TDCOffset = 7; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_1M5: - //qDebug("Data Bitrate not feasible with this clock!"); - return -1; - break; - case CAN_250K_2M: - ciDbtcfg.bF.BRP = 0; - ciDbtcfg.bF.TSEG1 = 2; - ciDbtcfg.bF.TSEG2 = 0; - ciDbtcfg.bF.SJW = 0; - // SSP - ciTdc.bF.TDCOffset = 3; - ciTdc.bF.TDCValue = tdcValue; - break; - case CAN_250K_3M: - case CAN_250K_4M: - //qDebug("Data Bitrate not feasible with this clock!"); - return -1; - break; + case CAN_250K_500K: + case CAN_125K_500K: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 14; + ciDbtcfg.bF.TSEG2 = 3; + ciDbtcfg.bF.SJW = 3; + // SSP + ciTdc.bF.TDCOffset = 15; + ciTdc.bF.TDCValue = tdcValue; + ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; + break; + case CAN_250K_833K: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 7; + ciDbtcfg.bF.TSEG2 = 2; + ciDbtcfg.bF.SJW = 2; + // SSP + ciTdc.bF.TDCOffset = 8; + ciTdc.bF.TDCValue = tdcValue; + ciTdc.bF.TDCMode = CAN_SSP_MODE_OFF; + break; + case CAN_250K_1M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 6; + ciDbtcfg.bF.TSEG2 = 1; + ciDbtcfg.bF.SJW = 1; + // SSP + ciTdc.bF.TDCOffset = 7; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_1M5: + //qDebug("Data Bitrate not feasible with this clock!"); + return -1; + break; + case CAN_250K_2M: + ciDbtcfg.bF.BRP = 0; + ciDbtcfg.bF.TSEG1 = 2; + ciDbtcfg.bF.TSEG2 = 0; + ciDbtcfg.bF.SJW = 0; + // SSP + ciTdc.bF.TDCOffset = 3; + ciTdc.bF.TDCValue = tdcValue; + break; + case CAN_250K_3M: + case CAN_250K_4M: + //qDebug("Data Bitrate not feasible with this clock!"); + return -1; + break; - default: - return -1; - break; + default: + return -1; + break; } // Write Bit time registers spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiDBTCFG, ciDbtcfg.word); - if (spiTransferError) { + if (spiTransferError) + { return -2; } @@ -1582,52 +1602,54 @@ int8_t mcp2518fd::mcp2518fd_BitTimeConfigureData10MHz(CAN_BITTIME_SETUP bitTime, #endif spiTransferError = mcp2518fd_WriteWord(cREGADDR_CiTDC, ciTdc.word); - if (spiTransferError) { + if (spiTransferError) + { return -3; } return spiTransferError; } - - int8_t mcp2518fd::mcp2518fd_BitTimeConfigure( - CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode, - CAN_SYSCLK_SPEED clk) + CAN_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode, + CAN_SYSCLK_SPEED clk) { int8_t spiTransferError = 0; // Decode clk - switch (clk) { - case CAN_SYSCLK_40M: - spiTransferError = mcp2518fd_BitTimeConfigureNominal40MHz(bitTime); - if (spiTransferError) return spiTransferError; + switch (clk) + { + case CAN_SYSCLK_40M: + spiTransferError = mcp2518fd_BitTimeConfigureNominal40MHz(bitTime); + if (spiTransferError) + return spiTransferError; - spiTransferError = mcp2518fd_BitTimeConfigureData40MHz(bitTime, sspMode); - break; - case CAN_SYSCLK_20M: - spiTransferError = mcp2518fd_BitTimeConfigureNominal20MHz(bitTime); - if (spiTransferError) return spiTransferError; + spiTransferError = mcp2518fd_BitTimeConfigureData40MHz(bitTime, sspMode); + break; + case CAN_SYSCLK_20M: + spiTransferError = mcp2518fd_BitTimeConfigureNominal20MHz(bitTime); + if (spiTransferError) + return spiTransferError; - spiTransferError = mcp2518fd_BitTimeConfigureData20MHz(bitTime, sspMode); - break; - case CAN_SYSCLK_10M: - spiTransferError = mcp2518fd_BitTimeConfigureNominal10MHz(bitTime); - if (spiTransferError) return spiTransferError; + spiTransferError = mcp2518fd_BitTimeConfigureData20MHz(bitTime, sspMode); + break; + case CAN_SYSCLK_10M: + spiTransferError = mcp2518fd_BitTimeConfigureNominal10MHz(bitTime); + if (spiTransferError) + return spiTransferError; - spiTransferError = mcp2518fd_BitTimeConfigureData10MHz(bitTime, sspMode); - break; - default: - spiTransferError = -1; - break; + spiTransferError = mcp2518fd_BitTimeConfigureData10MHz(bitTime, sspMode); + break; + default: + spiTransferError = -1; + break; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_GpioModeConfigure( - GPIO_PIN_MODE gpio0, GPIO_PIN_MODE gpio1) + GPIO_PIN_MODE gpio0, GPIO_PIN_MODE gpio1) { int8_t spiTransferError = 0; uint16_t a = 0; @@ -1638,7 +1660,8 @@ int8_t mcp2518fd::mcp2518fd_GpioModeConfigure( iocon.word = 0; spiTransferError = mcp2518fd_ReadByte(a, &iocon.byte[3]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -1648,16 +1671,16 @@ int8_t mcp2518fd::mcp2518fd_GpioModeConfigure( // Write spiTransferError = mcp2518fd_WriteByte(a, iocon.byte[3]); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_TransmitChannelEventEnable( - CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_EVENT flags) + CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_EVENT flags) { int8_t spiTransferError = 0; uint16_t a = 0; @@ -1668,7 +1691,8 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelEventEnable( ciFifoCon.word = 0; spiTransferError = mcp2518fd_ReadByte(a, &ciFifoCon.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -1677,21 +1701,22 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelEventEnable( // Write spiTransferError = mcp2518fd_WriteByte(a, ciFifoCon.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReceiveChannelEventEnable( - CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_EVENT flags) + CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_EVENT flags) { int8_t spiTransferError = 0; uint16_t a = 0; - if (channel == CAN_TXQUEUE_CH0) return -100; + if (channel == CAN_TXQUEUE_CH0) + return -100; // Read Interrupt Enables a = cREGADDR_CiFIFOCON + (channel * CiFIFO_OFFSET); @@ -1699,7 +1724,8 @@ int8_t mcp2518fd::mcp2518fd_ReceiveChannelEventEnable( ciFifoCon.word = 0; spiTransferError = mcp2518fd_ReadByte(a, &ciFifoCon.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -1708,14 +1734,14 @@ int8_t mcp2518fd::mcp2518fd_ReceiveChannelEventEnable( // Write spiTransferError = mcp2518fd_WriteByte(a, ciFifoCon.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ModuleEventEnable(CAN_MODULE_EVENT flags) { int8_t spiTransferError = 0; @@ -1727,7 +1753,8 @@ int8_t mcp2518fd::mcp2518fd_ModuleEventEnable(CAN_MODULE_EVENT flags) intEnables.word = 0; spiTransferError = mcp2518fd_ReadHalfWord(a, &intEnables.word); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -1736,14 +1763,14 @@ int8_t mcp2518fd::mcp2518fd_ModuleEventEnable(CAN_MODULE_EVENT flags) // Write spiTransferError = mcp2518fd_WriteHalfWord(a, intEnables.word); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_OperationModeSelect(CAN_OPERATION_MODE opMode) { uint8_t d = 0; @@ -1751,7 +1778,8 @@ int8_t mcp2518fd::mcp2518fd_OperationModeSelect(CAN_OPERATION_MODE opMode) // Read spiTransferError = mcp2518fd_ReadByte(cREGADDR_CiCON + 3, &d); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -1761,14 +1789,14 @@ int8_t mcp2518fd::mcp2518fd_OperationModeSelect(CAN_OPERATION_MODE opMode) // Write spiTransferError = mcp2518fd_WriteByte(cREGADDR_CiCON + 3, d); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; } - CAN_OPERATION_MODE mcp2518fd::mcp2518fd_OperationModeGet() { uint8_t d = 0; @@ -1777,7 +1805,8 @@ CAN_OPERATION_MODE mcp2518fd::mcp2518fd_OperationModeGet() // Read Opmode spiTransferError = mcp2518fd_ReadByte(cREGADDR_CiCON + 2, &d); - if (spiTransferError) { + if (spiTransferError) + { return CAN_INVALID_MODE; } @@ -1785,41 +1814,41 @@ CAN_OPERATION_MODE mcp2518fd::mcp2518fd_OperationModeGet() d = (d >> 5) & 0x7; // Decode Opmode - switch (d) { - case CAN_NORMAL_MODE: - mode = CAN_NORMAL_MODE; - break; - case CAN_SLEEP_MODE: - mode = CAN_SLEEP_MODE; - break; - case CAN_INTERNAL_LOOPBACK_MODE: - mode = CAN_INTERNAL_LOOPBACK_MODE; - break; - case CAN_EXTERNAL_LOOPBACK_MODE: - mode = CAN_EXTERNAL_LOOPBACK_MODE; - break; - case CAN_LISTEN_ONLY_MODE: - mode = CAN_LISTEN_ONLY_MODE; - break; - case CAN_CONFIGURATION_MODE: - mode = CAN_CONFIGURATION_MODE; - break; - case CAN_CLASSIC_MODE: - mode = CAN_CLASSIC_MODE; - break; - case CAN_RESTRICTED_MODE: - mode = CAN_RESTRICTED_MODE; - break; - default: - mode = CAN_INVALID_MODE; - break; + switch (d) + { + case CAN_NORMAL_MODE: + mode = CAN_NORMAL_MODE; + break; + case CAN_SLEEP_MODE: + mode = CAN_SLEEP_MODE; + break; + case CAN_INTERNAL_LOOPBACK_MODE: + mode = CAN_INTERNAL_LOOPBACK_MODE; + break; + case CAN_EXTERNAL_LOOPBACK_MODE: + mode = CAN_EXTERNAL_LOOPBACK_MODE; + break; + case CAN_LISTEN_ONLY_MODE: + mode = CAN_LISTEN_ONLY_MODE; + break; + case CAN_CONFIGURATION_MODE: + mode = CAN_CONFIGURATION_MODE; + break; + case CAN_CLASSIC_MODE: + mode = CAN_CLASSIC_MODE; + break; + case CAN_RESTRICTED_MODE: + mode = CAN_RESTRICTED_MODE; + break; + default: + mode = CAN_INVALID_MODE; + break; } return mode; } - -int8_t mcp2518fd::mcp2518fd_TransmitChannelEventGet(CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_EVENT* flags) +int8_t mcp2518fd::mcp2518fd_TransmitChannelEventGet(CAN_FIFO_CHANNEL channel, CAN_TX_FIFO_EVENT *flags) { int8_t spiTransferError = 0; uint16_t a = 0; @@ -1830,18 +1859,18 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelEventGet(CAN_FIFO_CHANNEL channel, CA a = cREGADDR_CiFIFOSTA + (channel * CiFIFO_OFFSET); spiTransferError = mcp2518fd_ReadByte(a, &ciFifoSta.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Update data - *flags = (CAN_TX_FIFO_EVENT) (ciFifoSta.byte[0] & CAN_TX_FIFO_ALL_EVENTS); + *flags = (CAN_TX_FIFO_EVENT)(ciFifoSta.byte[0] & CAN_TX_FIFO_ALL_EVENTS); return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_ErrorCountStateGet(uint8_t* tec, uint8_t* rec, CAN_ERROR_STATE* flags) +int8_t mcp2518fd::mcp2518fd_ErrorCountStateGet(uint8_t *tec, uint8_t *rec, CAN_ERROR_STATE *flags) { int8_t spiTransferError = 0; uint16_t a = 0; @@ -1852,14 +1881,15 @@ int8_t mcp2518fd::mcp2518fd_ErrorCountStateGet(uint8_t* tec, uint8_t* rec, CAN_E ciTrec.word = 0; spiTransferError = mcp2518fd_ReadWord(a, &ciTrec.word); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Update data *tec = ciTrec.byte[1]; *rec = ciTrec.byte[0]; - *flags = (CAN_ERROR_STATE) (ciTrec.byte[2] & CAN_ERROR_ALL); + *flags = (CAN_ERROR_STATE)(ciTrec.byte[2] & CAN_ERROR_ALL); return spiTransferError; } @@ -1874,45 +1904,48 @@ uint32_t DRV_CANFDSPI_DlcToDataBytes(CAN_DLC dlc) Nop(); Nop(); - if (dlc < CAN_DLC_12) { + if (dlc < CAN_DLC_12) + { dataBytesInObject = dlc; - } else { - switch (dlc) { - case CAN_DLC_12: - dataBytesInObject = 12; - break; - case CAN_DLC_16: - dataBytesInObject = 16; - break; - case CAN_DLC_20: - dataBytesInObject = 20; - break; - case CAN_DLC_24: - dataBytesInObject = 24; - break; - case CAN_DLC_32: - dataBytesInObject = 32; - break; - case CAN_DLC_48: - dataBytesInObject = 48; - break; - case CAN_DLC_64: - dataBytesInObject = 64; - break; - default: - break; + } + else + { + switch (dlc) + { + case CAN_DLC_12: + dataBytesInObject = 12; + break; + case CAN_DLC_16: + dataBytesInObject = 16; + break; + case CAN_DLC_20: + dataBytesInObject = 20; + break; + case CAN_DLC_24: + dataBytesInObject = 24; + break; + case CAN_DLC_32: + dataBytesInObject = 32; + break; + case CAN_DLC_48: + dataBytesInObject = 48; + break; + case CAN_DLC_64: + dataBytesInObject = 64; + break; + default: + break; } } return dataBytesInObject; } - int8_t mcp2518fd::mcp2518fd_TransmitChannelLoad( - CAN_FIFO_CHANNEL channel, CAN_TX_MSGOBJ* txObj, - uint8_t *txd, uint32_t txdNumBytes, bool flush) + CAN_FIFO_CHANNEL channel, CAN_TX_MSGOBJ *txObj, + uint8_t *txd, uint32_t txdNumBytes, bool flush) { - Serial.printf("into DRV_CANFDSPI_TransmitChannelLoad\n\r"); + Serial.printf("into DRV_CANFDSPI_TransmitChannelLoad\n\r"); uint16_t a; uint32_t fifoReg[3]; uint32_t dataBytesInObject; @@ -1921,26 +1954,26 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelLoad( REG_CiFIFOUA ciFifoUa; int8_t spiTransferError = 0; - - // Get FIFO registers a = cREGADDR_CiFIFOCON + (channel * CiFIFO_OFFSET); - + spiTransferError = mcp2518fd_ReadWordArray(a, fifoReg, 3); - if (spiTransferError) { + if (spiTransferError) + { return -1; } - - // Check that it is a transmit buffer + + // Check that it is a transmit buffer ciFifoCon.word = fifoReg[0]; - if (!ciFifoCon.txBF.TxEnable) { + if (!ciFifoCon.txBF.TxEnable) + { return -2; } - // Check that DLC is big enough for data - dataBytesInObject = DRV_CANFDSPI_DlcToDataBytes((CAN_DLC) txObj->bF.ctrl.DLC); - if (dataBytesInObject < txdNumBytes) { + dataBytesInObject = DRV_CANFDSPI_DlcToDataBytes((CAN_DLC)txObj->bF.ctrl.DLC); + if (dataBytesInObject < txdNumBytes) + { return -3; } @@ -1968,7 +2001,8 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelLoad( txBuffer[7] = txObj->byte[7]; uint8_t i; - for (i = 0; i < txdNumBytes; i++) { + for (i = 0; i < txdNumBytes; i++) + { txBuffer[i + 8] = txd[i]; } @@ -1976,49 +2010,52 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelLoad( uint16_t n = 0; uint8_t j = 0; - if (txdNumBytes % 4) { + if (txdNumBytes % 4) + { // Need to add bytes n = 4 - (txdNumBytes % 4); i = txdNumBytes + 8; - for (j = 0; j < n; j++) { + for (j = 0; j < n; j++) + { txBuffer[i + 8 + j] = 0; } } - Serial.printf("txBuffer[8] = %d\n\r",txBuffer[8]); - Serial.printf("txBuffer[9] = %d\n\r",txBuffer[9]); - Serial.printf("txBuffer[10] = %d\n\r",txBuffer[10]); - Serial.printf("txBuffer[11] = %d\n\r",txBuffer[11]); - Serial.printf("txBuffer[12] = %d\n\r",txBuffer[12]); - Serial.printf("txBuffer[13] = %d\n\r",txBuffer[13]); - Serial.printf("txBuffer[14] = %d\n\r",txBuffer[14]); - Serial.printf("txBuffer[15] = %d\n\r",txBuffer[15]); - + Serial.printf("txBuffer[8] = %d\n\r", txBuffer[8]); + Serial.printf("txBuffer[9] = %d\n\r", txBuffer[9]); + Serial.printf("txBuffer[10] = %d\n\r", txBuffer[10]); + Serial.printf("txBuffer[11] = %d\n\r", txBuffer[11]); + Serial.printf("txBuffer[12] = %d\n\r", txBuffer[12]); + Serial.printf("txBuffer[13] = %d\n\r", txBuffer[13]); + Serial.printf("txBuffer[14] = %d\n\r", txBuffer[14]); + Serial.printf("txBuffer[15] = %d\n\r", txBuffer[15]); spiTransferError = mcp2518fd_WriteByteArray(a, txBuffer, txdNumBytes + 8 + n); - if (spiTransferError) { + if (spiTransferError) + { return -4; } // Set UINC and TXREQ spiTransferError = mcp2518fd_TransmitChannelUpdate(channel, flush); - if (spiTransferError) { + if (spiTransferError) + { return -5; } - Serial.printf("end DRV_CANFDSPI_TransmitChannelLoad\n\r"); + Serial.printf("end DRV_CANFDSPI_TransmitChannelLoad\n\r"); return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_ReceiveChannelEventGet(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_EVENT* flags) +int8_t mcp2518fd::mcp2518fd_ReceiveChannelEventGet(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_EVENT *flags) { - Serial.println("DRV_CANFDSPI_ReceiveChannelEventGet\n\r"); + Serial.println("DRV_CANFDSPI_ReceiveChannelEventGet\n\r"); int8_t spiTransferError = 0; uint16_t a = 0; - if (channel == CAN_TXQUEUE_CH0) return -100; + if (channel == CAN_TXQUEUE_CH0) + return -100; // Read Interrupt flags REG_CiFIFOSTA ciFifoSta; @@ -2026,24 +2063,22 @@ int8_t mcp2518fd::mcp2518fd_ReceiveChannelEventGet(CAN_FIFO_CHANNEL channel, CAN a = cREGADDR_CiFIFOSTA + (channel * CiFIFO_OFFSET); spiTransferError = mcp2518fd_ReadByte(a, &ciFifoSta.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Update data - *flags = (CAN_RX_FIFO_EVENT) (ciFifoSta.byte[0] & CAN_RX_FIFO_ALL_EVENTS); + *flags = (CAN_RX_FIFO_EVENT)(ciFifoSta.byte[0] & CAN_RX_FIFO_ALL_EVENTS); - Serial.printf("DRV_CANFDSPI_ReceiveChannelEventGet flags = %x\n\r",(*flags)); + Serial.printf("DRV_CANFDSPI_ReceiveChannelEventGet flags = %x\n\r", (*flags)); return spiTransferError; } - - - int8_t mcp2518fd::mcp2518fd_ReceiveMessageGet( - CAN_FIFO_CHANNEL channel, CAN_RX_MSGOBJ* rxObj, - uint8_t *rxd, uint8_t nBytes) + CAN_FIFO_CHANNEL channel, CAN_RX_MSGOBJ *rxObj, + uint8_t *rxd, uint8_t nBytes) { uint8_t n = 0; uint8_t i = 0; @@ -2056,18 +2091,20 @@ int8_t mcp2518fd::mcp2518fd_ReceiveMessageGet( // Get FIFO registers a = cREGADDR_CiFIFOCON + (channel * CiFIFO_OFFSET); - + spiTransferError = mcp2518fd_ReadWordArray(a, fifoReg, 3); - if (spiTransferError) { + if (spiTransferError) + { return -1; } - Serial.printf("ciFifoCon.txBF.TxEnable2 = %d\n\r",ciFifoCon.txBF.TxEnable); + Serial.printf("ciFifoCon.txBF.TxEnable2 = %d\n\r", ciFifoCon.txBF.TxEnable); // Check that it is a receive buffer ciFifoCon.word = fifoReg[0]; - Serial.printf("ciFifoCon.txBF.TxEnable3 = %d\n\r",fifoReg[0]); + Serial.printf("ciFifoCon.txBF.TxEnable3 = %d\n\r", fifoReg[0]); ciFifoCon.txBF.TxEnable = 0; - if (ciFifoCon.txBF.TxEnable) { + if (ciFifoCon.txBF.TxEnable) + { return -2; } @@ -2085,24 +2122,28 @@ int8_t mcp2518fd::mcp2518fd_ReceiveMessageGet( // Number of bytes to read n = nBytes + 8; // Add 8 header bytes - if (ciFifoCon.rxBF.RxTimeStampEnable) { + if (ciFifoCon.rxBF.RxTimeStampEnable) + { n += 4; // Add 4 time stamp bytes } // Make sure we read a multiple of 4 bytes from RAM - if (n % 4) { + if (n % 4) + { n = n + 4 - (n % 4); } // Read rxObj using one access uint8_t ba[MAX_MSG_SIZE]; - if (n > MAX_MSG_SIZE) { + if (n > MAX_MSG_SIZE) + { n = MAX_MSG_SIZE; } spiTransferError = mcp2518fd_ReadByteArray(a, ba, n); - if (spiTransferError) { + if (spiTransferError) + { return -3; } @@ -2121,7 +2162,8 @@ int8_t mcp2518fd::mcp2518fd_ReceiveMessageGet( myReg.byte[3] = ba[7]; rxObj->word[1] = myReg.word; - if (ciFifoCon.rxBF.RxTimeStampEnable) { + if (ciFifoCon.rxBF.RxTimeStampEnable) + { myReg.byte[0] = ba[8]; myReg.byte[1] = ba[9]; myReg.byte[2] = ba[10]; @@ -2129,21 +2171,26 @@ int8_t mcp2518fd::mcp2518fd_ReceiveMessageGet( rxObj->word[2] = myReg.word; // Assign message data - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { rxd[i] = ba[i + 12]; } - } else { + } + else + { rxObj->word[2] = 0; // Assign message data - for (i = 0; i < nBytes; i++) { + for (i = 0; i < nBytes; i++) + { rxd[i] = ba[i + 8]; } } // UINC channel spiTransferError = mcp2518fd_ReceiveChannelUpdate(channel); - if (spiTransferError) { + if (spiTransferError) + { return -4; } @@ -2151,7 +2198,6 @@ int8_t mcp2518fd::mcp2518fd_ReceiveMessageGet( return spiTransferError; } - int8_t mcp2518fd::mcp2518fd_ReceiveChannelUpdate(CAN_FIFO_CHANNEL channel) { uint16_t a = 0; @@ -2181,12 +2227,14 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelUpdate(CAN_FIFO_CHANNEL channel, bool ciFifoCon.txBF.UINC = 1; // Set TXREQ - if (flush) { + if (flush) + { ciFifoCon.txBF.TxRequest = 1; } spiTransferError = mcp2518fd_WriteByte(a, ciFifoCon.byte[1]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -2194,7 +2242,7 @@ int8_t mcp2518fd::mcp2518fd_TransmitChannelUpdate(CAN_FIFO_CHANNEL channel, bool } int8_t mcp2518fd::mcp2518fd_ReceiveChannelStatusGet( - CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_STATUS* status) + CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_STATUS *status) { uint16_t a; REG_CiFIFOSTA ciFifoSta; @@ -2205,18 +2253,18 @@ int8_t mcp2518fd::mcp2518fd_ReceiveChannelStatusGet( a = cREGADDR_CiFIFOSTA + (channel * CiFIFO_OFFSET); spiTransferError = mcp2518fd_ReadByte(a, &ciFifoSta.byte[0]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Update data - *status = (CAN_RX_FIFO_STATUS) (ciFifoSta.byte[0] & 0x0F); + *status = (CAN_RX_FIFO_STATUS)(ciFifoSta.byte[0] & 0x0F); return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_ErrorStateGet(CAN_ERROR_STATE* flags) +int8_t mcp2518fd::mcp2518fd_ErrorStateGet(CAN_ERROR_STATE *flags) { int8_t spiTransferError = 0; uint16_t a = 0; @@ -2226,16 +2274,74 @@ int8_t mcp2518fd::mcp2518fd_ErrorStateGet(CAN_ERROR_STATE* flags) uint8_t f = 0; spiTransferError = mcp2518fd_ReadByte(a, &f); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Update data - *flags = (CAN_ERROR_STATE) (f & CAN_ERROR_ALL); + *flags = (CAN_ERROR_STATE)(f & CAN_ERROR_ALL); return spiTransferError; } +int8_t mcp2518fd::mcp2518fd_ModuleEventRxCodeGet(CAN_RXCODE *rxCode) +{ + int8_t spiTransferError = 0; + uint16_t a = 0; + uint8_t rxCodeByte = 0; + + // Read + a = cREGADDR_CiVEC + 3; + + spiTransferError = mcp2518fd_ReadByte(a, &rxCodeByte); + if (spiTransferError) + { + return -1; + } + + // Decode data + // 0x40 = "no interrupt" (CAN_FIFO_CIVEC_NOINTERRUPT) + if ((rxCodeByte < CAN_RXCODE_TOTAL_CHANNELS) || (rxCodeByte == CAN_RXCODE_NO_INT)) + { + *rxCode = (CAN_RXCODE)rxCodeByte; + } + else + { + *rxCode = CAN_RXCODE_RESERVED; // shouldn't get here + } + + return spiTransferError; +} + +int8_t mcp2518fd::mcp2518fd_ModuleEventTxCodeGet(CAN_TXCODE *txCode) +{ + int8_t spiTransferError = 0; + uint16_t a = 0; + uint8_t txCodeByte = 0; + + // Read + a = cREGADDR_CiVEC + 2; + + spiTransferError = mcp2518fd_ReadByte(a, &txCodeByte); + if (spiTransferError) + { + return -1; + } + + // Decode data + // 0x40 = "no interrupt" (CAN_FIFO_CIVEC_NOINTERRUPT) + if ((txCodeByte < CAN_TXCODE_TOTAL_CHANNELS) || (txCodeByte == CAN_TXCODE_NO_INT)) + { + *txCode = (CAN_TXCODE)txCodeByte; + } + else + { + *txCode = CAN_TXCODE_RESERVED; // shouldn't get here + } + + return spiTransferError; +} int8_t mcp2518fd::mcp2518fd_LowPowerModeEnable() { @@ -2248,7 +2354,8 @@ int8_t mcp2518fd::mcp2518fd_LowPowerModeEnable() #else // Read spiTransferError = mcp2518fd_ReadByte(cREGADDR_OSC, &d); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -2257,14 +2364,14 @@ int8_t mcp2518fd::mcp2518fd_LowPowerModeEnable() // Write spiTransferError = mcp2518fd_WriteByte(cREGADDR_OSC, d); - if (spiTransferError) { + if (spiTransferError) + { return -2; } #endif - - return spiTransferError; -} + return spiTransferError; +} int8_t mcp2518fd::mcp2518fd_LowPowerModeDisable() { @@ -2277,7 +2384,8 @@ int8_t mcp2518fd::mcp2518fd_LowPowerModeDisable() #else // Read spiTransferError = mcp2518fd_ReadByte(index, cREGADDR_OSC, &d); - if (spiTransferError) { + if (spiTransferError) + { return -1; } @@ -2286,131 +2394,121 @@ int8_t mcp2518fd::mcp2518fd_LowPowerModeDisable() // Write spiTransferError = mcp2518fd_WriteByte(index, cREGADDR_OSC, d); - if (spiTransferError) { + if (spiTransferError) + { return -2; } #endif - - return spiTransferError; -} + return spiTransferError; +} void mcp2518fd::mcp2518fd_TransmitMessageQueue(void) { uint8_t attempts = MAX_TXQUEUE_ATTEMPTS; - + // Check if FIFO is not full - do { + do + { mcp2518fd_TransmitChannelEventGet(APP_TX_FIFO, &txFlags); - if (attempts == 0) { + if (attempts == 0) + { Nop(); Nop(); mcp2518fd_ErrorCountStateGet(&tec, &rec, &errorFlags); return; } attempts--; - } - while (!(txFlags & CAN_TX_FIFO_NOT_FULL_EVENT)); + } while (!(txFlags & CAN_TX_FIFO_NOT_FULL_EVENT)); // Load message and transmit uint8_t n = DRV_CANFDSPI_DlcToDataBytes((CAN_DLC)txObj.bF.ctrl.DLC); - - mcp2518fd_TransmitChannelLoad(APP_TX_FIFO, &txObj, txd, n, true); + mcp2518fd_TransmitChannelLoad(APP_TX_FIFO, &txObj, txd, n, true); } /********************************************************************************************************* ** Function name: sendMsgBuf ** Descriptions: send buf *********************************************************************************************************/ -void mcp2518fd::mcp2518fd_sendMsgBuf(const byte* buf,byte len) { - return mcp2518fd_sendMsg(buf,len); +byte mcp2518fd::mcp2518fd_sendMsgBuf(const byte *buf, byte len, unsigned long id, byte ext,bool wait_sent) +{ + return mcp2518fd_sendMsg(buf, len, id, ext,wait_sent); } /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message *********************************************************************************************************/ -void mcp2518fd::mcp2518fd_sendMsg(const byte* buf, byte len) { +byte mcp2518fd::mcp2518fd_sendMsg(const byte *buf, byte len, unsigned long id, byte ext,bool wait_sent) +{ uint8_t n; int i; - - // // Configure message data - // txObj.word[0] = 0; - // txObj.word[1] = 0; - // txObj.bF.id.SID = id; - // txObj.bF.ctrl.DLC = CAN_DLC_64; - // if (ext == 1) - // { - // txObj.bF.ctrl.IDE = 1; - // txObj.bF.ctrl.FDF = 1; - // } - // if (ext == 0) - // { - // txObj.bF.ctrl.IDE = 0; - // txObj.bF.ctrl.FDF = 0; - // } - - // txObj.bF.ctrl.BRS = true; - - + byte spiTransferError = 0; // Configure message data txObj.word[0] = 0; txObj.word[1] = 0; - txObj.bF.id.SID = TX_REQUEST_ID; - txObj.bF.ctrl.DLC = CAN_DLC_64; - txObj.bF.ctrl.IDE = 0; + txObj.bF.id.SID = id; + txObj.bF.ctrl.DLC = len; + if (ext == 1) + { + txObj.bF.ctrl.IDE = 1; + txObj.bF.ctrl.FDF = 1; + } + if (ext == 0) + { + txObj.bF.ctrl.IDE = 0; + txObj.bF.ctrl.FDF = 0; + } txObj.bF.ctrl.BRS = true; - txObj.bF.ctrl.FDF = 0; //Prepare data - for(i = 0; i< len; i++) + for (i = 0; i < len; i++) { txd[i] = buf[i]; } mcp2518fd_TransmitMessageQueue(); - + return spiTransferError; } - -int8_t mcp2518fd::mcp2518fd_receiveMsg() { +int8_t mcp2518fd::mcp2518fd_receiveMsg() +{ mcp2518fd_ReceiveChannelEventGet(APP_RX_FIFO, &rxFlags); - if (rxFlags & CAN_RX_FIFO_NOT_EMPTY_EVENT) { + if (rxFlags & CAN_RX_FIFO_NOT_EMPTY_EVENT) + { mcp2518fd_ReceiveMessageGet(APP_RX_FIFO, &rxObj, rxd, 8); - for (int i = 0; i < 8; i++) { - Serial.println(rxd[i]); Serial.println("\t"); + for (int i = 0; i < 8; i++) + { + Serial.println(rxd[i]); + Serial.println("\t"); } Serial.println(); - } return 0; - } /********************************************************************************************************* ** Function name: mcp2515_init ** Descriptions: init the device *********************************************************************************************************/ -uint8_t mcp2518fd::mcp2518fd_init(byte speedset) { +uint8_t mcp2518fd::mcp2518fd_init(byte speedset) +{ // Reset device mcp2518fd_reset(); // Enable ECC and initialize RAM mcp2518fd_EccEnable(); - mcp2518fd_RamInit(0xff); - // Configure device mcp2518fd_ConfigureObjectReset(&config); - config.IsoCrcEnable = 1; + config.IsoCrcEnable = 1; config.StoreInTEF = 0; mcp2518fd_Configure(&config); - // Setup TX FIFO mcp2518fd_TransmitChannelConfigureObjectReset(&txConfig); @@ -2419,13 +2517,11 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) { txConfig.TxPriority = 1; mcp2518fd_TransmitChannelConfigure(APP_TX_FIFO, &txConfig); - // Setup RX FIFO mcp2518fd_ReceiveChannelConfigureObjectReset(&rxConfig); rxConfig.FifoSize = 15; rxConfig.PayLoadSize = CAN_PLSIZE_64; mcp2518fd_ReceiveChannelConfigure(APP_RX_FIFO, &rxConfig); - // Setup RX Filter fObj.word = 0; @@ -2450,24 +2546,23 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) { // Setup Transmit and Receive Interrupts mcp2518fd_GpioModeConfigure(GPIO_MODE_INT, GPIO_MODE_INT); - #ifdef APP_USE_TX_INT +#ifdef APP_USE_TX_INT mcp2518fd_TransmitChannelEventEnable(APP_TX_FIFO, CAN_TX_FIFO_NOT_FULL_EVENT); - #endif +#endif mcp2518fd_ReceiveChannelEventEnable(APP_RX_FIFO, CAN_RX_FIFO_NOT_EMPTY_EVENT); mcp2518fd_ModuleEventEnable((CAN_MODULE_EVENT)(CAN_TX_EVENT | CAN_RX_EVENT)); // Select Normal Mode - mcp2518fd_OperationModeSelect(CAN_CLASSIC_MODE); + // mcp2518fd_OperationModeSelect(CAN_CLASSIC_MODE); + setMode(CAN_CLASSIC_MODE); // CAN_OPERATION_MODE abc; // abc = DRV_CANFDSPI_OperationModeGet(0); - // Serial.printf("DRV_CANFDSPI_OperationModeGet = %d\n\r",abc); + // Serial.printf("DRV_CANFDSPI_OperationModeGet = %d\n\r",abc); return 0; - } - // /********************************************************************************************************* // ** Function name: enableTxInterrupt // ** Descriptions: enable interrupt for all tx buffers @@ -2493,56 +2588,55 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) { // { // ciFifoCon.byte[0] |= (flags & CAN_RX_FIFO_NO_EVENT); // } - + // spiTransferError = mcp2518fd_WriteByte(index, a, ciFifoCon.byte[0]); // return; // } - -byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) { +byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) +{ int8_t err; mcp2518fd_OperationModeSelect(CAN_CONFIGURATION_MODE); - - // Setup RX Mask + // Setup RX Mask mObj.word = 0; mObj.bF.MSID = ulData; mObj.bF.MIDE = ext; // Only allow standard IDs mObj.bF.MEID = 0x0; err = mcp2518fd_FilterMaskConfigure((CAN_FILTER)num, &mObj.bF); - mcp2518fd_OperationModeSelect(mcpMode); + mcp2518fd_OperationModeSelect(mcpMode); - return err; + return err; } +/********************************************************************************************************* +** Function name: init_Filt +** Descriptions: init canid filters +*********************************************************************************************************/ +byte mcp2518fd::init_Filt(byte num, byte ext, unsigned long ulData) +{ + int8_t err; + err = mcp2518fd_OperationModeSelect(CAN_CONFIGURATION_MODE); -// /********************************************************************************************************* -// ** Function name: init_Filt -// ** Descriptions: init canid filters -// *********************************************************************************************************/ -// byte mcp2518fd::init_Filt(byte num, byte ext, unsigned long ulData) { -// int8_t err; -// err = mcp2518fd_OperationModeSelect(CAN_CONFIGURATION_MODE); - -// // Setup RX Filter -// fObj.word = 0; -// if (ext == 0) -// { -// fObj.bF.SID = ulData; -// fObj.bF.EXIDE = 0; //standard identifier -// fObj.bF.EID = 0x00; -// }else -// if (ext == 1) -// fObj.bF.SID = 0; -// fObj.bF.EXIDE = 1; //extended identifier -// fObj.bF.EID = ulData; - -// mcp2518fd_FilterObjectConfigure(num, &fObj.bF); -// mcp2518fd_OperationModeSelect(mcpMode); -// return res; -// } - + // Setup RX Filter + fObj.word = 0; + if (ext == 0) + { + fObj.bF.SID = ulData; + fObj.bF.EXIDE = 0; //standard identifier + fObj.bF.EID = 0x00; + } + else if (ext == 1) + { + fObj.bF.SID = 0; + fObj.bF.EXIDE = 1; //extended identifier + fObj.bF.EID = ulData; + } + mcp2518fd_FilterObjectConfigure((CAN_FILTER)num, &fObj.bF); + mcp2518fd_OperationModeSelect(mcpMode); + return err; +} // /********************************************************************************************************* // ** Function name: setSleepWakeup @@ -2557,9 +2651,8 @@ byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) { // { // mcp2518fd_LowPowerModeDisable(); // } - -// } +// } // /********************************************************************************************************* // ** Function name: sleep @@ -2586,7 +2679,6 @@ byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) { // } // } - // /********************************************************************************************************* // ** Function name: getMode // ** Descriptions: Returns current control mode @@ -2598,46 +2690,54 @@ byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) { // ret = (byte)mode; // } +/********************************************************************************************************* +** Function name: setMode +** Descriptions: Sets control mode +*********************************************************************************************************/ +byte mcp2518fd::setMode(CAN_OPERATION_MODE opMode) +{ + if (opMode != + CAN_SLEEP_MODE) + { // if going to sleep, the value stored in opMode is not changed so that we can return to it later + mcpMode = opMode; + } + return mcp2518fd_OperationModeSelect(mcpMode); +} -// /********************************************************************************************************* -// ** Function name: setMode -// ** Descriptions: Sets control mode -// *********************************************************************************************************/ -// byte mcp2518fd::setMode(const byte opMode) { -// if (opMode != -// CAN_SLEEP_MODE) { // if going to sleep, the value stored in opMode is not changed so that we can return to it later -// mcpMode = opMode; -// } -// return mcp2518fd_OperationModeSelect(mcpMode); -// } - +/********************************************************************************************************* +** Function name: getCanId +** Descriptions: when receive something, you can get the can id!! +*********************************************************************************************************/ +unsigned long mcp2518fd::getCanId(void) +{ + return can_id; +} /********************************************************************************************************* ** Function name: readMsgBuf ** Descriptions: read message buf *********************************************************************************************************/ -byte MCP_CAN::readMsgBuf(byte* len, byte buf[]) { - return readMsgBufID(readRxTxStatus(), &can_id, &ext_flg, &rtr, len, buf); +byte mcp2518fd::readMsgBuf(byte *len, byte buf[]) +{ + return readMsgBufID(len, buf); } - /********************************************************************************************************* ** Function name: checkReceive ** Descriptions: check if got something *********************************************************************************************************/ -byte mcp2518fd::checkReceive(void) { +byte mcp2518fd::checkReceive(void) +{ CAN_RX_FIFO_STATUS status; // byte res; // res = mcp2518_readStatus(); // RXnIF in Bit 1 and 0 // return ((res & MCP_STAT_RXIF_MASK) ? CAN_MSGAVAIL : CAN_NOMSG); - mcp2518fd_ReceiveChannelStatusGet(APP_RX_FIFO,status); - + mcp2518fd_ReceiveChannelStatusGet(APP_RX_FIFO, &status); + byte res = (byte)(status & CAN_RX_FIFO_NOT_EMPTY_EVENT); return res; } - - // /********************************************************************************************************* // ** Function name: checkError // ** Descriptions: if something error @@ -2652,61 +2752,48 @@ byte mcp2518fd::checkReceive(void) { // return eflg; // } - // /********************************************************************************************************* // ** Function name: readMsgBufID // ** Descriptions: Read message buf and can bus source ID according to status. // ** Status has to be read with readRxTxStatus. // *********************************************************************************************************/ -byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtrBit, - volatile byte* len, volatile byte* buf) { - // byte rc = CAN_NOMSG; +byte mcp2518fd::readMsgBufID(volatile byte *len, volatile byte *buf) +{ - // if (status & MCP_RX0IF) { // Msg in Buffer 0 - // mcp2515_read_canMsg(MCP_READ_RX0, id, ext, rtrBit, len, buf); - // rc = CAN_OK; - // } else if (status & MCP_RX1IF) { // Msg in Buffer 1 - // mcp2515_read_canMsg(MCP_READ_RX1, id, ext, rtrBit, len, buf); - // rc = CAN_OK; - // } + + mcp2518fd_ReceiveMessageGet(APP_RX_FIFO, &rxObj, rxd, MAX_DATA_BYTES); + - // if (rc == CAN_OK) { - // rtr = *rtrBit; - // // dta_len=*len; // not used on any interface function - // ext_flg = *ext; - // can_id = *id; - // } else { - // *len = 0; - // } - if (status & CAN_RX_FIFO_NOT_EMPTY_EVENT) { - mcp2518fd_ReceiveMessageGet(APP_RX_FIFO, &rxObj, rxd, 8); + can_id = (unsigned long)rxObj.bF.id.SID; + Serial.printf("readMsgBufID can_id= %lu\n\r",can_id); + uint8_t n = DRV_CANFDSPI_DlcToDataBytes((CAN_DLC)rxObj.bF.ctrl.DLC); + Serial.printf("readMsgBufID n= %d\n\r",n); + *len = n; + + for (int i = 0; i < n; i++) + { + buf[i] = rxd[i]; } - can_id = rxObj->bF.id; - - return rc; + return 0; } - - // /********************************************************************************************************* // ** Function name: trySendMsgBuf // ** Descriptions: Try to send message. There is no delays for waiting free buffer. // *********************************************************************************************************/ // byte mcp2518fd::trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, byte iTxBuf) { - + // return CAN_OK; // } - - - // /********************************************************************************************************* // ** Function name: sendMsgBuf // ** Descriptions: Send message by using buffer read as free from CANINTF status // ** Status has to be read with readRxTxStatus and filtered with checkClearTxStatus // *********************************************************************************************************/ -// byte mcp2518fd::sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte* buf) { +// byte mcp2518fd::sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte *buf) +// { // // byte txbuf_n = statusToTxSidh(status); // // if (txbuf_n == 0) { @@ -2715,28 +2802,29 @@ byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long* id, volatile b // // mcp2515_modifyRegister(MCP_CANINTF, status, 0); // Clear interrupt flag // // mcp2515_write_canMsg(txbuf_n, id, ext, rtrBit, len, buf); - -// mcp2518fd_sendMsg(buf,len,id,ext); + +// mcp2518fd_sendMsg(buf, len, id, ext); // return CAN_OK; // } -// /********************************************************************************************************* -// ** Function name: sendMsgBuf -// ** Descriptions: send buf -// *********************************************************************************************************/ -// byte mcp2518fd::sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, bool wait_sent) { -// return mcp2518fd_sendMsg(buf,len,id,ext); -// } - -// /********************************************************************************************************* -// ** Function name: sendMsgBuf -// ** Descriptions: send buf -// *********************************************************************************************************/ -// byte mcp2518fd::sendMsgBuf(unsigned long id, byte ext, byte len, const byte* buf, bool wait_sent) { -// return mcp2518fd_sendMsg(buf,len,id,ext); -// } +/********************************************************************************************************* +** Function name: sendMsgBuf +** Descriptions: send buf +*********************************************************************************************************/ +byte mcp2518fd::sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent) +{ + return mcp2518fd_sendMsg(buf, len, id, ext,wait_sent); +} +/********************************************************************************************************* +** Function name: sendMsgBuf +** Descriptions: send buf +*********************************************************************************************************/ +byte mcp2518fd::sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf, bool wait_sent) +{ + return mcp2518fd_sendMsg(buf, len, id, ext, wait_sent); +} /********************************************************************************************************* ** Function name: clearBufferTransmitIfFlags @@ -2752,7 +2840,6 @@ byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long* id, volatile b // // mcp2515_modifyRegister(MCP_CANINTF, flags, 0); // } - /********************************************************************************************************* ** Function name: readRxTxStatus ** Descriptions: Read RX and TX interrupt bits. Function uses status reading, but translates. @@ -2760,15 +2847,33 @@ byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long* id, volatile b ** with one single call to save SPI calls. Then use checkClearRxStatus and ** checkClearTxStatus for testing. *********************************************************************************************************/ -byte mcp2518fd::readRxTxStatus(void) { +byte mcp2518fd::readRxTxStatus(void) +{ // byte ret = (mcp2515_readStatus() & (MCP_STAT_TXIF_MASK | MCP_STAT_RXIF_MASK)); // ret = (ret & MCP_STAT_TX0IF ? MCP_TX0IF : 0) | // (ret & MCP_STAT_TX1IF ? MCP_TX1IF : 0) | // (ret & MCP_STAT_TX2IF ? MCP_TX2IF : 0) | // (ret & MCP_STAT_RXIF_MASK); // Rx bits happend to be same on status and MCP_CANINTF // return ret; -} + // byte ret; + // CAN_RXCODE rxCode; + // CAN_TXCODE txCode; + // mcp2518fd_ModuleEventRxCodeGet(rxCode); + // mcp2518fd_ModuleEventTxCodeGet(txCode); + // if (rxCode != CAN_RXCODE_RESERVED) + // { + // ret = (byte)rxCode; + // } + // if (txCode != CAN_RXCODE_RESERVED) + // { + // ret = (byte)txCode; + // } + byte ret; + mcp2518fd_ReceiveChannelEventGet(APP_RX_FIFO, &rxFlags); + ret = (byte)rxFlags; + return ret; +} // /********************************************************************************************************* // ** Function name: checkClearRxStatus @@ -2789,7 +2894,6 @@ byte mcp2518fd::readRxTxStatus(void) { // // return ret; // } - // /********************************************************************************************************* // ** Function name: checkClearTxStatus // ** Descriptions: Return specified buffer of first found tx CANINTF status and clears it from parameter. @@ -2819,7 +2923,8 @@ byte mcp2518fd::readRxTxStatus(void) { ** Function name: mcpPinMode ** Descriptions: switch supported pins between HiZ, interrupt, output or input *********************************************************************************************************/ -bool mcp2518fd::mcpPinMode(const byte pin, const byte mode) { +bool mcp2518fd::mcpPinMode(const byte pin, const byte mode) +{ int8_t spiTransferError = 1; uint16_t a = 0; @@ -2829,30 +2934,29 @@ bool mcp2518fd::mcpPinMode(const byte pin, const byte mode) { iocon.word = 0; mcp2518fd_ReadByte(a, &iocon.byte[3]); - - + if (pin == GPIO_PIN_0) { - // Modify - iocon.bF.PinMode0 =(GPIO_PIN_MODE)mode; + // Modify + iocon.bF.PinMode0 = (GPIO_PIN_MODE)mode; } if (pin == GPIO_PIN_1) { - // Modify - iocon.bF.PinMode1 =(GPIO_PIN_MODE)mode; + // Modify + iocon.bF.PinMode1 = (GPIO_PIN_MODE)mode; } // Write mcp2518fd_WriteByte(a, iocon.byte[3]); - + return spiTransferError; } - /********************************************************************************************************* ** Function name: mcpDigitalWrite ** Descriptions: write HIGH or LOW to RX0BF/RX1BF *********************************************************************************************************/ -bool mcp2518fd::mcpDigitalWrite(const byte pin, const byte mode) { +bool mcp2518fd::mcpDigitalWrite(const byte pin, const byte mode) +{ int8_t spiTransferError = 0; uint16_t a = 0; @@ -2862,41 +2966,42 @@ bool mcp2518fd::mcpDigitalWrite(const byte pin, const byte mode) { iocon.word = 0; spiTransferError = mcp2518fd_ReadByte(a, &iocon.byte[1]); - if (spiTransferError) { + if (spiTransferError) + { return -1; } // Modify - switch (pin) { - case GPIO_PIN_0: - iocon.bF.LAT0 = (GPIO_PIN_STATE)mode; - break; - case GPIO_PIN_1: - iocon.bF.LAT1 = (GPIO_PIN_STATE)mode; - break; - default: - return -1; - break; + switch (pin) + { + case GPIO_PIN_0: + iocon.bF.LAT0 = (GPIO_PIN_STATE)mode; + break; + case GPIO_PIN_1: + iocon.bF.LAT1 = (GPIO_PIN_STATE)mode; + break; + default: + return -1; + break; } // Write spiTransferError = mcp2518fd_WriteByte(a, iocon.byte[1]); - if (spiTransferError) { + if (spiTransferError) + { return -2; } return spiTransferError; - } - - /********************************************************************************************************* ** Function name: mcpDigitalRead ** Descriptions: read HIGH or LOW from supported pins *********************************************************************************************************/ -byte mcp2518fd::mcpDigitalRead(const byte pin) { - +byte mcp2518fd::mcpDigitalRead(const byte pin) +{ + GPIO_PIN_STATE state; uint16_t a = 0; @@ -2908,23 +3013,20 @@ byte mcp2518fd::mcpDigitalRead(const byte pin) { mcp2518fd_ReadByte(a, &iocon.byte[2]); // Update data - switch (pin) { - case GPIO_PIN_0: - state = (GPIO_PIN_STATE) iocon.bF.GPIO0; - break; - case GPIO_PIN_1: - state = (GPIO_PIN_STATE) iocon.bF.GPIO1; - break; - default: - return -1; - break; + switch (pin) + { + case GPIO_PIN_0: + state = (GPIO_PIN_STATE)iocon.bF.GPIO0; + break; + case GPIO_PIN_1: + state = (GPIO_PIN_STATE)iocon.bF.GPIO1; + break; + default: + return -1; + break; } - byte ret = (byte)state; - - return ret; - + + return ret; } - - diff --git a/mcp2518fd_can.h b/mcp2518fd_can.h index 3c721cf..983b7a1 100644 --- a/mcp2518fd_can.h +++ b/mcp2518fd_can.h @@ -105,27 +105,27 @@ public: void mcp_canbus(uint8_t _CS); void init_CS(byte _CS); byte begin(byte speedset); // init can - void mcp2518fd_sendMsgBuf(const byte* buf,byte len); // send buf - void mcp2518fd_sendMsg(const byte* buf,byte len); + byte mcp2518fd_sendMsgBuf(const byte* buf,byte len,unsigned long id, byte ext,bool wait_sent); // send buf + byte mcp2518fd_sendMsg(const byte* buf,byte len,unsigned long id, byte ext,bool wait_sent); int8_t mcp2518fd_receiveMsg(); // void enableTxInterrupt(bool enable = true); // enable transmit interrupt byte init_Mask(byte num, byte ext, unsigned long ulData); - // byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters + byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters // void setSleepWakeup(const byte enable); // byte sleep(); // byte wake(); - // byte setMode(byte opMode); + byte setMode(CAN_OPERATION_MODE opMode); // byte getMode(); + unsigned long getCanId(void); byte readMsgBuf(byte* len, byte* buf); byte checkReceive(void); // byte checkError(void); - byte readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtr, volatile byte* len, - volatile byte* buf); + byte readMsgBufID( volatile byte* len,volatile byte* buf); // byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, byte iTxBuf); // byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte* buf); - // byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, bool wait_sent = true); // send buf - // byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte* buf, bool wait_sent = true); // send buf + byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, bool wait_sent = true); // send buf + byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte* buf, bool wait_sent = true); // send buf // void clearBufferTransmitIfFlags(byte flags); byte readRxTxStatus(void); // byte checkClearRxStatus(byte* status); @@ -169,6 +169,10 @@ public: int8_t mcp2518fd_TransmitChannelUpdate(CAN_FIFO_CHANNEL channel, bool flush); int8_t mcp2518fd_ReceiveChannelStatusGet(CAN_FIFO_CHANNEL channel, CAN_RX_FIFO_STATUS* status); int8_t mcp2518fd_ErrorStateGet(CAN_ERROR_STATE* flags); + int8_t mcp2518fd_ModuleEventRxCodeGet(CAN_RXCODE* rxCode); + int8_t mcp2518fd_ModuleEventTxCodeGet(CAN_TXCODE* txCode); + + int8_t mcp2518fd_LowPowerModeEnable(); @@ -203,7 +207,7 @@ private: unsigned long can_id; // can id byte rtr; // rtr byte nReservedTx; // Count of tx buffers for reserved send - byte mcpMode; // Current controller mode + CAN_OPERATION_MODE mcpMode; // Current controller mode }; #endif \ No newline at end of file