Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SEEED_CAN by
seeed_can_spi.cpp
00001 /* seeed_can_spi.cpp 00002 * Copyright (c) 2013 Sophie Dexter 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "seeed_can_spi.h" 00018 00019 /** reset the MCP2515 00020 */ 00021 void mcpReset(mcp_can_t *obj) 00022 { 00023 obj->ncs = 0; 00024 obj->spi.write(MCP_RESET); 00025 obj->ncs = 1; 00026 wait_ms(10); 00027 } 00028 00029 /** read from a single MCP2515 register 00030 */ 00031 uint8_t mcpRead(mcp_can_t *obj, const uint8_t address) 00032 { 00033 uint8_t result; 00034 00035 obj->ncs = 0; 00036 obj->spi.write(MCP_READ); 00037 obj->spi.write(address); 00038 result = obj->spi.write(0x00); 00039 obj->ncs = 1; 00040 return result; 00041 } 00042 00043 /** read multiple MCP2515 registers sequentially into an array (relies on address auto-increment) 00044 */ 00045 void mcpReadMultiple(mcp_can_t *obj, const uint8_t address, uint8_t values[], const uint8_t n) 00046 { 00047 obj->ncs = 0; 00048 obj->spi.write(MCP_READ); 00049 obj->spi.write(address); 00050 for (uint32_t i=0; i<n; i++) { 00051 values[i] = obj->spi.write(0x00); 00052 } 00053 obj->ncs = 1; 00054 } 00055 00056 /** read the specified MCP2515 receive buffer into an array (needs one fewer SPI transfer than mcpReadMultiple) 00057 */ 00058 void mcpReadBuffer(mcp_can_t *obj, const uint8_t command, uint8_t values[], const uint8_t n) 00059 { 00060 obj->ncs = 0; 00061 obj->spi.write(command); 00062 for (uint32_t i=0; i<n; i++) { 00063 values[i] = obj->spi.write(0x00); 00064 } 00065 obj->ncs = 1; 00066 } 00067 00068 /** write to a single MCP2515 register 00069 */ 00070 void mcpWrite(mcp_can_t *obj, const uint8_t address, const uint8_t value) 00071 { 00072 obj->ncs = 0; 00073 obj->spi.write(MCP_WRITE); 00074 obj->spi.write(address); 00075 obj->spi.write(value); 00076 obj->ncs = 1; 00077 } 00078 00079 /** write to multiple MCP2515 registers consecutively from an array 00080 */ 00081 void mcpWriteMultiple(mcp_can_t *obj, const uint8_t address, const uint8_t values[], const uint8_t n) 00082 { 00083 obj->ncs = 0; 00084 obj->spi.write(MCP_WRITE); 00085 obj->spi.write(address); 00086 for (uint32_t i=0; i<n; i++) { 00087 obj->spi.write(values[i]); 00088 } 00089 obj->ncs = 1; 00090 } 00091 00092 /** write to the specified MCP2515 transmit buffer from an array (needs one fewer SPI transfer than mcpWriteMultiple) 00093 */ 00094 void mcpWriteBuffer(mcp_can_t *obj, const uint8_t command, uint8_t values[], const uint8_t n) 00095 { 00096 obj->ncs = 0; 00097 obj->spi.write(command); 00098 for (uint32_t i=0; i<n; i++) { 00099 obj->spi.write(values[i]); 00100 } 00101 obj->ncs = 1; 00102 } 00103 00104 /** initiate transmission of the specified MCP2515 transmit buffer 00105 */ 00106 void mcpBufferRTS(mcp_can_t *obj, const uint8_t command) 00107 { 00108 obj->ncs = 0; 00109 obj->spi.write(command); 00110 obj->ncs = 1; 00111 } 00112 00113 /** read mcp2515's status register 00114 */ 00115 uint8_t mcpStatus(mcp_can_t *obj) 00116 { 00117 uint8_t status; 00118 00119 obj->ncs = 0; 00120 obj->spi.write(MCP_READ_STATUS); 00121 status = obj->spi.write(0x00); 00122 obj->ncs = 1; 00123 return status; 00124 } 00125 00126 /** read mcp2515's receive status register 00127 */ 00128 uint8_t mcpReceiveStatus(mcp_can_t *obj) 00129 { 00130 uint8_t status; 00131 00132 obj->ncs = 0; 00133 obj->spi.write(MCP_RX_STATUS); 00134 status = obj->spi.write(0x00); 00135 obj->ncs = 1; 00136 return status; 00137 } 00138 00139 /** modify bits of a register specified by a mask 00140 */ 00141 void mcpBitModify(mcp_can_t *obj, const uint8_t address, const uint8_t mask, const uint8_t data) 00142 { 00143 obj->ncs = 0; 00144 obj->spi.write(MCP_BITMOD); 00145 obj->spi.write(address); 00146 obj->spi.write(mask); 00147 obj->spi.write(data); 00148 obj->ncs = 1; 00149 }
Generated on Tue Jul 12 2022 19:07:56 by
1.7.2
