mcp_can.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. mcp_can.h
  3. 2012 Copyright (c) Seeed Technology Inc. All right reserved.
  4. 2017 Copyright (c) Cory J. Fowler All Rights Reserved.
  5. Author:Loovee
  6. Contributor: Cory J. Fowler
  7. 2017-09-25
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-
  19. 1301 USA
  20. */
  21. #ifndef _MCP2515_H_
  22. #define _MCP2515_H_
  23. #include "mcp_can_dfs.h"
  24. #define MAX_CHAR_IN_MESSAGE 8
  25. class MCP_CAN
  26. {
  27. private:
  28. INT8U m_nExtFlg; // Identifier Type
  29. // Extended (29 bit) or Standard (11 bit)
  30. INT32U m_nID; // CAN ID
  31. INT8U m_nDlc; // Data Length Code
  32. INT8U m_nDta[MAX_CHAR_IN_MESSAGE]; // Data array
  33. INT8U m_nRtr; // Remote request flag
  34. INT8U m_nfilhit; // The number of the filter that matched the message
  35. INT8U MCPCS; // Chip Select pin number
  36. INT8U mcpMode; // Mode to return to after configurations are performed.
  37. /*********************************************************************************************************
  38. * mcp2515 driver function
  39. *********************************************************************************************************/
  40. // private:
  41. private:
  42. void mcp2515_reset(void); // Soft Reset MCP2515
  43. INT8U mcp2515_readRegister(const INT8U address); // Read MCP2515 register
  44. void mcp2515_readRegisterS(const INT8U address, // Read MCP2515 successive registers
  45. INT8U values[],
  46. const INT8U n);
  47. void mcp2515_setRegister(const INT8U address, // Set MCP2515 register
  48. const INT8U value);
  49. void mcp2515_setRegisterS(const INT8U address, // Set MCP2515 successive registers
  50. const INT8U values[],
  51. const INT8U n);
  52. void mcp2515_initCANBuffers(void);
  53. void mcp2515_modifyRegister(const INT8U address, // Set specific bit(s) of a register
  54. const INT8U mask,
  55. const INT8U data);
  56. INT8U mcp2515_readStatus(void); // Read MCP2515 Status
  57. INT8U mcp2515_setCANCTRL_Mode(const INT8U newmode); // Set mode
  58. INT8U mcp2515_configRate(const INT8U canSpeed, // Set baud rate
  59. const INT8U canClock);
  60. INT8U mcp2515_init(const INT8U canIDMode, // Initialize Controller
  61. const INT8U canSpeed,
  62. const INT8U canClock);
  63. void mcp2515_write_mf( const INT8U mcp_addr, // Write CAN Mask or Filter
  64. const INT8U ext,
  65. const INT32U id );
  66. void mcp2515_write_id( const INT8U mcp_addr, // Write CAN ID
  67. const INT8U ext,
  68. const INT32U id );
  69. void mcp2515_read_id( const INT8U mcp_addr, // Read CAN ID
  70. INT8U* ext,
  71. INT32U* id );
  72. void mcp2515_write_canMsg( const INT8U buffer_sidh_addr ); // Write CAN message
  73. void mcp2515_read_canMsg( const INT8U buffer_sidh_addr); // Read CAN message
  74. INT8U mcp2515_getNextFreeTXBuf(INT8U *txbuf_n); // Find empty transmit buffer
  75. inline void initSS() { pinMode(MCPCS, OUTPUT); }
  76. inline void setSS() { digitalWrite(MCPCS, LOW); Serial.print(" SET CS["); Serial.print(MCPCS); Serial.print("] "); }
  77. inline void resetSS() { digitalWrite(MCPCS, HIGH); }
  78. /*********************************************************************************************************
  79. * CAN operator function
  80. *********************************************************************************************************/
  81. INT8U setMsg(INT32U id, INT8U rtr, INT8U ext, INT8U len, INT8U *pData); // Set message
  82. INT8U clearMsg(); // Clear all message to zero
  83. INT8U readMsg(); // Read message
  84. INT8U sendMsg(); // Send message
  85. public:
  86. MCP_CAN(INT8U _CS);
  87. INT8U begin(INT8U idmodeset, INT8U speedset, INT8U clockset); // Initialize controller parameters
  88. INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); // Initialize Mask(s)
  89. INT8U init_Mask(INT8U num, INT32U ulData); // Initialize Mask(s)
  90. INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); // Initialize Filter(s)
  91. INT8U init_Filt(INT8U num, INT32U ulData); // Initialize Filter(s)
  92. INT8U setMode(INT8U opMode); // Set operational mode
  93. INT8U sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf); // Send message to transmit buffer
  94. INT8U sendMsgBuf(INT32U id, INT8U len, INT8U *buf); // Send message to transmit buffer
  95. INT8U readMsgBuf(INT32U *id, INT8U *ext, INT8U *len, INT8U *buf); // Read message from receive buffer
  96. INT8U readMsgBuf(INT32U *id, INT8U *len, INT8U *buf); // Read message from receive buffer
  97. INT8U checkReceive(void); // Check for received data
  98. INT8U checkError(void); // Check for errors
  99. INT8U getError(void); // Check for errors
  100. INT8U errorCountRX(void); // Get error count
  101. INT8U errorCountTX(void); // Get error count
  102. INT8U enOneShotTX(void); // Enable one-shot transmission
  103. INT8U disOneShotTX(void); // Disable one-shot transmission
  104. INT8U abortTX(void); // Abort queued transmission(s)
  105. INT8U setGPO(INT8U data); // Sets GPO
  106. INT8U getGPI(void); // Reads GPI
  107. };
  108. #endif
  109. /*********************************************************************************************************
  110. * END FILE
  111. *********************************************************************************************************/