8 years, 7 months ago.

How to use MCP2515 for LPC1114FN28?

Hi everyone! I' m not good at English so this question is confusing. I am confronted with a difficult problem. Please help me.

My goal is construct system using CAN. And the system use only mbed LPC1114.

First, I try to use MCP2515 connected to LPC1768. Because mbed LPC1114 have not CAN pin like mbed LPC1768(ex: p29,p30) . It can work good. I use mcp2515.h library (https://developer.mbed.org/users/tecnosys/code/mcp2515/). This is very good library. It is useful function design.

Second, I try to MCP2515 connected to LPC1114. This is not work.

Error is bellow.

Error: Identifier "CANFormat" is undefined in "mcp2515/mcp2515.h", Line: 90, Col: 45

Error: Identifier "CANMessage" is undefined in "mcp2515/mcp2515.h", Line: 91, Col: 52

Error: Identifier "CANMessage" is undefined in "mcp2515/mcp2515.h", Line: 92, Col: 54

Error: Identifier "CANMessage" is undefined in "mcp2515/CAN3.h", Line: 22, Col: 19

Error: Identifier "CANMessage" is undefined in "mcp2515/CAN3.h", Line: 23, Col: 17

Error: Identifier "CANMessage" is undefined in "main.cpp", Line: 16, Col: 6

I think this error came from CANMessage class inCAN.h. LPC1114 is DIP so it has not FPGA like LPC1768.

I use two med platform. mbed LPC1768 (https://developer.mbed.org/platforms/mbed-LPC1768/),

mbed LPC1114 (https://developer.mbed.org/platforms/LPC1114FN28/),

and two IC. MCP2515, MCP2551.

and I change the library in can speed. because I use 20Mhz. I think to add the new header that define CANMessage. What I do this program.

My code is bellow

include the mbed library with this snippet

#include "mbed.h"
#include "CAN3.h"

SPI spi(dp2,dp1,dp6);// mosi, miso, sclk
CAN3 can(spi,dp10,dp11);
DigitalOut led2(LED2);
DigitalOut led1(LED1);
Serial pc(USBTX, USBRX);

int main() {
    char data[1] = {0};
    can.frequency(125000);
    CANMessage msg(2002,data,1);
    msg.data[0] = 253;
    while(1) {
        can.write(&msg);
        pc.printf("Send:%d\r\n",msg.data[0]);
        led1 = !led1;
        wait(0.5);
    }
}

1 Answer

8 years, 7 months ago.

Hi,

This is because the LPC1114FN28 doesn't support CAN in the device.h platform definition file. Therefore, CANFormat and CANMessage definitions are not included in the build.

Following header file example is a small hack to avoid this issue (i.e. to use CAN related helpers with Non-CAN platform).

can_suppor.h

#if (DEVICE_CAN) == 0

enum CANFormat {
    CANStandard = 0,
    CANExtended = 1,
    CANAny = 2
};
typedef enum CANFormat CANFormat;

enum CANType {
    CANData   = 0,
    CANRemote = 1
};
typedef enum CANType CANType;

struct CAN_Message {
    unsigned int   id;                 // 29 bit identifier
    unsigned char  data[8];            // Data field
    unsigned char  len;                // Length of data field in bytes
    CANFormat      format;             // 0 - STANDARD, 1- EXTENDED IDENTIFIER
    CANType        type;               // 0 - DATA FRAME, 1 - REMOTE FRAME
};
typedef struct CAN_Message CAN_Message;

class CANMessage : public CAN_Message {

public:
    /** Creates empty CAN message.
     */
    CANMessage() : CAN_Message() {
        len    = 8;
        type   = CANData;
        format = CANStandard;
        id     = 0;
        memset(data, 0, 8);
    }

    /** Creates CAN message with specific content.
     */
    CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
      len    = _len & 0xF;
      type   = _type;
      format = _format;
      id     = _id;
      memcpy(data, _data, _len);
    }

    /** Creates CAN remote message.
     */
    CANMessage(int _id, CANFormat _format = CANStandard) {
      len    = 0;
      type   = CANRemote;
      format = _format;
      id     = _id;
      memset(data, 0, 8);
    }
};

#endif

And then, you can include this can_support.h from the mcp2515.h file.

I hope this helps. Toyo

Accepted Answer

Thanks for your quick and polite answer. I tried to import your hack code. It works very well.

I want to study programming hard with mbed platform. Thank you Mr.Watari. Son Shaku.

posted by son shaku 01 Apr 2016