Seed can implementation
Fork of SEEED_CAN by
Diff: seeed_can_api.cpp
- Revision:
- 2:fd026fcfde94
- Parent:
- 1:ad71faa09868
--- a/seeed_can_api.cpp Wed Nov 06 20:16:11 2013 +0000 +++ b/seeed_can_api.cpp Tue Nov 12 20:54:16 2013 +0000 @@ -18,7 +18,7 @@ /** Initialise the MCP2515 and set the bit rate */ -uint8_t mcpInit(can_t *obj, const uint32_t bitRate) +uint8_t mcpInit(mcp_can_t *obj, const uint32_t bitRate, const CANMode mode) { union { // Access CANMsg as: CANMsg x; // the organised struct @@ -51,14 +51,15 @@ if (!mcpSetBitRate(obj, bitRate)) { // set baudrate return 0; } - return mcpSetMode(obj, MODE_NORMAL) ? 1 : 0; // set Normal mode and return +// return mcpSetMode(obj, MODE_NORMAL) ? 1 : 0; // set Normal mode and return + return mcpSetMode(obj, mode) ? 1 : 0; // set Normal mode and return } /** set MCP2515 operation mode * * Configuration, Normal, Sleep, Listen-only or Loopback */ -uint8_t mcpSetMode(can_t *obj, const uint8_t newmode) +uint8_t mcpSetMode(mcp_can_t *obj, const uint8_t newmode) { mcpBitModify(obj, MCP_CANCTRL, MODE_MASK, newmode); for (uint32_t i = 0; i<10; i++) { @@ -115,7 +116,7 @@ {0x7, 0x7}, // 25, 68.0% }; -uint8_t mcpSetBitRate(can_t *obj, const uint32_t bitRate) +uint8_t mcpSetBitRate(mcp_can_t *obj, const uint32_t bitRate) { union { // Access CANtiming as: CANtiming x; // the organised struct @@ -173,7 +174,7 @@ /** write a CAN id to a mask, filter or transmit buffer */ -void mcpWriteId(can_t *obj, const uint8_t mcp_addr, const uint8_t ext, const uint32_t id ) +void mcpWriteId(mcp_can_t *obj, const uint8_t mcp_addr, const uint8_t ext, const uint32_t id ) { union { // Access CANid as: CANid x; // the organised struct @@ -207,7 +208,7 @@ /** write a CAN message to the MCP2515 */ -uint8_t mcpCanWrite(can_t *obj, CAN_Message msg) +uint8_t mcpCanWrite(mcp_can_t *obj, CAN_Message msg) { union { // Access CANMsg as: CANMsg x; // the organised struct @@ -251,7 +252,7 @@ /** read a CAN message from the MCP2515 */ -uint8_t mcpCanRead(can_t *obj, CAN_Message *msg) +uint8_t mcpCanRead(mcp_can_t *obj, CAN_Message *msg) { union { // Access CANMsg as: CANMsg x; // the organised struct @@ -306,7 +307,7 @@ /** initialise an Acceptance Mask */ -uint8_t mcpInitMask(can_t *obj, uint8_t num, uint32_t ulData, bool ext) +uint8_t mcpInitMask(mcp_can_t *obj, uint8_t num, uint32_t ulData, bool ext) { uint8_t mask[2] = { MCP_RXM0SIDH, MCP_RXM1SIDH }; @@ -335,7 +336,7 @@ /** initialise an Acceptance Filter */ -uint8_t mcpInitFilter(can_t *obj, uint8_t num, uint32_t ulData, bool ext) +uint8_t mcpInitFilter(mcp_can_t *obj, uint8_t num, uint32_t ulData, bool ext) { uint8_t filter[6] = { MCP_RXF0SIDH, MCP_RXF1SIDH, MCP_RXF2SIDH, MCP_RXF3SIDH, MCP_RXF4SIDH, MCP_RXF5SIDH }; @@ -362,34 +363,67 @@ return 1; } +/* Report on the specified errors and warnings + */ +uint8_t mcpErrorType(mcp_can_t *obj, const CANFlags type) +{ + uint8_t which[] = { MCP_EFLG_ALLMASK, + MCP_EFLG_ERRORMASK, + MCP_EFLG_WARNMASK, + MCP_EFLG_RX1OVR, + MCP_EFLG_RX0OVR, + MCP_EFLG_TXBO, + MCP_EFLG_TXEP, + MCP_EFLG_RXEP, + MCP_EFLG_TXWAR, + MCP_EFLG_RXWAR, + MCP_EFLG_EWARN + }; + + return (mcpRead(obj, MCP_EFLG) & which[type]) ? 1 : 0; +} + +/* Return contents of the error and warning flags register + */ +uint8_t mcpErrorFlags(mcp_can_t *obj) +{ + return (mcpRead(obj, MCP_EFLG)); +} + /* Number of message reception errors */ -uint8_t mcpReceptionErrorCount(can_t *obj) +uint8_t mcpReceptionErrorCount(mcp_can_t *obj) { return (mcpRead(obj, MCP_REC)); } /* Number of message transmission errors */ -uint8_t mcpTransmissionErrorCount(can_t *obj) +uint8_t mcpTransmissionErrorCount(mcp_can_t *obj) { return (mcpRead(obj, MCP_TEC)); } /* Select between monitor (silent = 1) and normal (silent = 0) modes */ -void mcpMonitor(can_t *obj, const bool silent) +void mcpMonitor(mcp_can_t *obj, const bool silent) { silent ? mcpSetMode(obj, MODE_LISTENONLY) : mcpSetMode(obj, MODE_NORMAL); } /* Change CAN operation to the specified mode */ -uint8_t mcpMode(can_t *obj, const CANMode mode) +uint8_t mcpMode(mcp_can_t *obj, const CANMode mode) { - uint8_t which[] = { MODE_NORMAL, MODE_SLEEP, MODE_LOOPBACK, MODE_LISTENONLY, MODE_CONFIG, MODE_CONFIG}; + uint8_t which[] = { MODE_NORMAL, + MODE_SLEEP, + MODE_LOOPBACK, + MODE_LISTENONLY, + MODE_CONFIG, + MODE_CONFIG + }; - if (mode == _RESET) { + if (mode == _M_RESET) { mcpReset(obj); } if (mcpSetMode(obj, which[mode])) { @@ -397,3 +431,52 @@ } return 0; } + +/* Configure interrupt sources + */ +void mcpSetInterrupts(mcp_can_t *obj, const CANIrqs irqSet) +{ + uint8_t which[] = { MCP_NO_INTS, + MCP_ALL_INTS, + MCP_RX_INTS, + MCP_TX_INTS, + MCP_RX0IF, + MCP_RX1IF, + MCP_TX0IF, + MCP_TX1IF, + MCP_TX2IF, + MCP_ERRIF, + MCP_WAKIF, + MCP_MERRF + }; + + mcpWrite(obj, MCP_CANINTE, which[irqSet]); +} + +/* Report on the specified interrupt causes + */ +uint8_t mcpInterruptType(mcp_can_t *obj, const CANIrqs irqFlag) +{ + uint8_t which[] = { MCP_NO_INTS, + MCP_ALL_INTS, + MCP_RX_INTS, + MCP_TX_INTS, + MCP_RX0IF, + MCP_RX1IF, + MCP_TX0IF, + MCP_TX1IF, + MCP_TX2IF, + MCP_ERRIF, + MCP_WAKIF, + MCP_MERRF + }; + + return (mcpRead(obj, MCP_EFLG) & which[irqFlag]) ? 1 : 0; +} + +/* Return contents of the interrupt flags register + */ +uint8_t mcpInterruptFlags(mcp_can_t *obj) +{ + return (mcpRead(obj, MCP_EFLG)); +}