Seed can implementation
Fork of SEEED_CAN by
Diff: seeed_can_api.cpp
- Revision:
- 1:ad71faa09868
- Parent:
- 0:f5d099885d3d
- Child:
- 2:fd026fcfde94
diff -r f5d099885d3d -r ad71faa09868 seeed_can_api.cpp --- a/seeed_can_api.cpp Tue Nov 05 22:37:35 2013 +0000 +++ b/seeed_can_api.cpp Wed Nov 06 20:16:11 2013 +0000 @@ -42,20 +42,16 @@ for (uint32_t i = 0; i < 3; i++) { // Clear all CAN TX buffers mcpWriteMultiple(obj, canBuffer[i], y, sizeof(x) ); // using empty CAN message (as an array) } - mcpWrite(obj, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); // RX buffers can generate a interrupt -#if (DEBUG_RXANY==1) - // enable both receive-buffers to receive any message and enable rollover - mcpBitModify(obj, MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); - mcpBitModify(obj, MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_ANY); -#else - // enable both receive-buffers to receive messages with std. and ext. identifiers and enable rollover + // enable both receive-buffers, using filters to receive messages with std. and ext. identifiers that meet the filter criteria and enable rollover from RXB0 to RXB1 if RXB0 is full mcpBitModify(obj, MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); mcpBitModify(obj, MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_STDEXT); -#endif #ifdef DEBUG printf("Setting bit rate\r\n"); #endif - return (mcpSetBitRate(obj, bitRate)) ? 1 : 0; // set baudrate and return + if (!mcpSetBitRate(obj, bitRate)) { // set baudrate + return 0; + } + return mcpSetMode(obj, MODE_NORMAL) ? 1 : 0; // set Normal mode and return } /** set MCP2515 operation mode @@ -130,14 +126,15 @@ uint32_t bestCanRate = 0; uint32_t minBRP = (MCP_CLOCK_FREQ / (2 * MCP_MAX_TIME_QUANTA * bitRate)); uint32_t maxBRP = (MCP_CLOCK_FREQ / (2 * MCP_MIN_TIME_QUANTA * bitRate)); - - #ifdef DEBUG - printf("Setting configuration mode\r\n"); - #endif - if(!mcpSetMode(obj, MODE_CONFIG)) { // Go into configuration mode - return 0; - } - + +#ifdef DEBUG + printf("Setting configuration mode\r\n"); +#endif + uint8_t initialMode = mcpRead(obj, MCP_CANCTRL) & MODE_MASK; // Store the current operation mode + if(!mcpSetMode(obj, MODE_CONFIG)) { // Go into configuration mode + return 0; + } + for (uint32_t i = 0; i < sizeof(x); i++) y[i] = NULL; // Initialise CANtiming (btlmode, sjw and sam all = 0) if ((bitRate < CAN_MIN_RATE) || (bitRate > CAN_MAX_RATE)) { #ifdef DEBUG @@ -171,7 +168,7 @@ printf("Syncseg: 1\tPropSeg: %d\tPhaseSeg1: %d\tPhaseSeg2: %d\r\n", (x.prseg+1), (x.phseg1+1), (x.phseg1+1)); printf("Setting normal mode\r\n"); #endif - return (mcpSetMode(obj, MODE_NORMAL)) ? 1 : 0; // desired bit rate set enter normal mode and return + return (mcpSetMode(obj, initialMode)) ? 1 : 0; // desired bit rate set enter normal mode and return } /** write a CAN id to a mask, filter or transmit buffer @@ -322,11 +319,12 @@ #ifdef DEBUG printf("Begin to set Mask!!\r\n"); #endif + uint8_t initialMode = mcpRead(obj, MCP_CANCTRL) & MODE_MASK; // Store the current operation mode if(!mcpSetMode(obj, MODE_CONFIG)) { return 0; } mcpWriteId(obj, mask[num], ext, ulData); - if(!mcpSetMode(obj, MODE_NORMAL)) { + if(!mcpSetMode(obj, initialMode)) { return 0; } #ifdef DEBUG @@ -350,11 +348,12 @@ #ifdef DEBUG printf("Begin to set Filter!!\r\n"); #endif + uint8_t initialMode = mcpRead(obj, MCP_CANCTRL) & MODE_MASK; // Store the current operation mode if(!mcpSetMode(obj, MODE_CONFIG)) { return 0; } mcpWriteId(obj, filter[num], ext, ulData); - if(!mcpSetMode(obj, MODE_NORMAL)) { + if(!mcpSetMode(obj, initialMode)) { return 0; } #ifdef DEBUG