Seed can implementation
Fork of SEEED_CAN by
seeed_can_spi.cpp@0:f5d099885d3d, 2013-11-05 (annotated)
- Committer:
- Just4pLeisure
- Date:
- Tue Nov 05 22:37:35 2013 +0000
- Revision:
- 0:f5d099885d3d
- Child:
- 2:fd026fcfde94
Beta release of a CAN-BUS library for Seeed Studios' CAN BUS Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Just4pLeisure | 0:f5d099885d3d | 1 | /* seeed_can_spi.cpp |
Just4pLeisure | 0:f5d099885d3d | 2 | * Copyright (c) 2013 Sophie Dexter |
Just4pLeisure | 0:f5d099885d3d | 3 | * |
Just4pLeisure | 0:f5d099885d3d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Just4pLeisure | 0:f5d099885d3d | 5 | * you may not use this file except in compliance with the License. |
Just4pLeisure | 0:f5d099885d3d | 6 | * You may obtain a copy of the License at |
Just4pLeisure | 0:f5d099885d3d | 7 | * |
Just4pLeisure | 0:f5d099885d3d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Just4pLeisure | 0:f5d099885d3d | 9 | * |
Just4pLeisure | 0:f5d099885d3d | 10 | * Unless required by applicable law or agreed to in writing, software |
Just4pLeisure | 0:f5d099885d3d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Just4pLeisure | 0:f5d099885d3d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Just4pLeisure | 0:f5d099885d3d | 13 | * See the License for the specific language governing permissions and |
Just4pLeisure | 0:f5d099885d3d | 14 | * limitations under the License. |
Just4pLeisure | 0:f5d099885d3d | 15 | */ |
Just4pLeisure | 0:f5d099885d3d | 16 | |
Just4pLeisure | 0:f5d099885d3d | 17 | #include "seeed_can_spi.h" |
Just4pLeisure | 0:f5d099885d3d | 18 | |
Just4pLeisure | 0:f5d099885d3d | 19 | /** reset the MCP2515 |
Just4pLeisure | 0:f5d099885d3d | 20 | */ |
Just4pLeisure | 0:f5d099885d3d | 21 | void mcpReset(can_t *obj) |
Just4pLeisure | 0:f5d099885d3d | 22 | { |
Just4pLeisure | 0:f5d099885d3d | 23 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 24 | obj->spi.write(MCP_RESET); |
Just4pLeisure | 0:f5d099885d3d | 25 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 26 | wait_ms(10); |
Just4pLeisure | 0:f5d099885d3d | 27 | } |
Just4pLeisure | 0:f5d099885d3d | 28 | |
Just4pLeisure | 0:f5d099885d3d | 29 | /** read from a single MCP2515 register |
Just4pLeisure | 0:f5d099885d3d | 30 | */ |
Just4pLeisure | 0:f5d099885d3d | 31 | uint8_t mcpRead(can_t *obj, const uint8_t address) |
Just4pLeisure | 0:f5d099885d3d | 32 | { |
Just4pLeisure | 0:f5d099885d3d | 33 | uint8_t result; |
Just4pLeisure | 0:f5d099885d3d | 34 | |
Just4pLeisure | 0:f5d099885d3d | 35 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 36 | obj->spi.write(MCP_READ); |
Just4pLeisure | 0:f5d099885d3d | 37 | obj->spi.write(address); |
Just4pLeisure | 0:f5d099885d3d | 38 | result = obj->spi.write(0x00); |
Just4pLeisure | 0:f5d099885d3d | 39 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 40 | return result; |
Just4pLeisure | 0:f5d099885d3d | 41 | } |
Just4pLeisure | 0:f5d099885d3d | 42 | |
Just4pLeisure | 0:f5d099885d3d | 43 | /** read multiple MCP2515 registers sequentially into an array (relies on address auto-increment) |
Just4pLeisure | 0:f5d099885d3d | 44 | */ |
Just4pLeisure | 0:f5d099885d3d | 45 | void mcpReadMultiple(can_t *obj, const uint8_t address, uint8_t values[], const uint8_t n) |
Just4pLeisure | 0:f5d099885d3d | 46 | { |
Just4pLeisure | 0:f5d099885d3d | 47 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 48 | obj->spi.write(MCP_READ); |
Just4pLeisure | 0:f5d099885d3d | 49 | obj->spi.write(address); |
Just4pLeisure | 0:f5d099885d3d | 50 | for (uint32_t i=0; i<n; i++) { |
Just4pLeisure | 0:f5d099885d3d | 51 | values[i] = obj->spi.write(0x00); |
Just4pLeisure | 0:f5d099885d3d | 52 | } |
Just4pLeisure | 0:f5d099885d3d | 53 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 54 | } |
Just4pLeisure | 0:f5d099885d3d | 55 | |
Just4pLeisure | 0:f5d099885d3d | 56 | /** read the specified MCP2515 receive buffer into an array (needs one fewer SPI transfer than mcpReadMultiple) |
Just4pLeisure | 0:f5d099885d3d | 57 | */ |
Just4pLeisure | 0:f5d099885d3d | 58 | void mcpReadBuffer(can_t *obj, const uint8_t command, uint8_t values[], const uint8_t n) |
Just4pLeisure | 0:f5d099885d3d | 59 | { |
Just4pLeisure | 0:f5d099885d3d | 60 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 61 | obj->spi.write(command); |
Just4pLeisure | 0:f5d099885d3d | 62 | for (uint32_t i=0; i<n; i++) { |
Just4pLeisure | 0:f5d099885d3d | 63 | values[i] = obj->spi.write(0x00); |
Just4pLeisure | 0:f5d099885d3d | 64 | } |
Just4pLeisure | 0:f5d099885d3d | 65 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 66 | } |
Just4pLeisure | 0:f5d099885d3d | 67 | |
Just4pLeisure | 0:f5d099885d3d | 68 | /** write to a single MCP2515 register |
Just4pLeisure | 0:f5d099885d3d | 69 | */ |
Just4pLeisure | 0:f5d099885d3d | 70 | void mcpWrite(can_t *obj, const uint8_t address, const uint8_t value) |
Just4pLeisure | 0:f5d099885d3d | 71 | { |
Just4pLeisure | 0:f5d099885d3d | 72 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 73 | obj->spi.write(MCP_WRITE); |
Just4pLeisure | 0:f5d099885d3d | 74 | obj->spi.write(address); |
Just4pLeisure | 0:f5d099885d3d | 75 | obj->spi.write(value); |
Just4pLeisure | 0:f5d099885d3d | 76 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 77 | } |
Just4pLeisure | 0:f5d099885d3d | 78 | |
Just4pLeisure | 0:f5d099885d3d | 79 | /** write to multiple MCP2515 registers consecutively from an array |
Just4pLeisure | 0:f5d099885d3d | 80 | */ |
Just4pLeisure | 0:f5d099885d3d | 81 | void mcpWriteMultiple(can_t *obj, const uint8_t address, const uint8_t values[], const uint8_t n) |
Just4pLeisure | 0:f5d099885d3d | 82 | { |
Just4pLeisure | 0:f5d099885d3d | 83 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 84 | obj->spi.write(MCP_WRITE); |
Just4pLeisure | 0:f5d099885d3d | 85 | obj->spi.write(address); |
Just4pLeisure | 0:f5d099885d3d | 86 | for (uint32_t i=0; i<n; i++) { |
Just4pLeisure | 0:f5d099885d3d | 87 | obj->spi.write(values[i]); |
Just4pLeisure | 0:f5d099885d3d | 88 | } |
Just4pLeisure | 0:f5d099885d3d | 89 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 90 | } |
Just4pLeisure | 0:f5d099885d3d | 91 | |
Just4pLeisure | 0:f5d099885d3d | 92 | /** write to the specified MCP2515 transmit buffer from an array (needs one fewer SPI transfer than mcpWriteMultiple) |
Just4pLeisure | 0:f5d099885d3d | 93 | */ |
Just4pLeisure | 0:f5d099885d3d | 94 | void mcpWriteBuffer(can_t *obj, const uint8_t command, uint8_t values[], const uint8_t n) |
Just4pLeisure | 0:f5d099885d3d | 95 | { |
Just4pLeisure | 0:f5d099885d3d | 96 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 97 | obj->spi.write(command); |
Just4pLeisure | 0:f5d099885d3d | 98 | for (uint32_t i=0; i<n; i++) { |
Just4pLeisure | 0:f5d099885d3d | 99 | obj->spi.write(values[i]); |
Just4pLeisure | 0:f5d099885d3d | 100 | } |
Just4pLeisure | 0:f5d099885d3d | 101 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 102 | } |
Just4pLeisure | 0:f5d099885d3d | 103 | |
Just4pLeisure | 0:f5d099885d3d | 104 | /** initiate transmission of the specified MCP2515 transmit buffer |
Just4pLeisure | 0:f5d099885d3d | 105 | */ |
Just4pLeisure | 0:f5d099885d3d | 106 | void mcpBufferRTS(can_t *obj, const uint8_t command) |
Just4pLeisure | 0:f5d099885d3d | 107 | { |
Just4pLeisure | 0:f5d099885d3d | 108 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 109 | obj->spi.write(command); |
Just4pLeisure | 0:f5d099885d3d | 110 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 111 | } |
Just4pLeisure | 0:f5d099885d3d | 112 | |
Just4pLeisure | 0:f5d099885d3d | 113 | /** read mcp2515's status register |
Just4pLeisure | 0:f5d099885d3d | 114 | */ |
Just4pLeisure | 0:f5d099885d3d | 115 | uint8_t mcpStatus(can_t *obj) |
Just4pLeisure | 0:f5d099885d3d | 116 | { |
Just4pLeisure | 0:f5d099885d3d | 117 | uint8_t status; |
Just4pLeisure | 0:f5d099885d3d | 118 | |
Just4pLeisure | 0:f5d099885d3d | 119 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 120 | obj->spi.write(MCP_READ_STATUS); |
Just4pLeisure | 0:f5d099885d3d | 121 | status = obj->spi.write(0x00); |
Just4pLeisure | 0:f5d099885d3d | 122 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 123 | return status; |
Just4pLeisure | 0:f5d099885d3d | 124 | } |
Just4pLeisure | 0:f5d099885d3d | 125 | |
Just4pLeisure | 0:f5d099885d3d | 126 | /** read mcp2515's receive status register |
Just4pLeisure | 0:f5d099885d3d | 127 | */ |
Just4pLeisure | 0:f5d099885d3d | 128 | uint8_t mcpReceiveStatus(can_t *obj) |
Just4pLeisure | 0:f5d099885d3d | 129 | { |
Just4pLeisure | 0:f5d099885d3d | 130 | uint8_t status; |
Just4pLeisure | 0:f5d099885d3d | 131 | |
Just4pLeisure | 0:f5d099885d3d | 132 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 133 | obj->spi.write(MCP_RX_STATUS); |
Just4pLeisure | 0:f5d099885d3d | 134 | status = obj->spi.write(0x00); |
Just4pLeisure | 0:f5d099885d3d | 135 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 136 | return status; |
Just4pLeisure | 0:f5d099885d3d | 137 | } |
Just4pLeisure | 0:f5d099885d3d | 138 | |
Just4pLeisure | 0:f5d099885d3d | 139 | /** modify bits of a register specified by a mask |
Just4pLeisure | 0:f5d099885d3d | 140 | */ |
Just4pLeisure | 0:f5d099885d3d | 141 | void mcpBitModify(can_t *obj, const uint8_t address, const uint8_t mask, const uint8_t data) |
Just4pLeisure | 0:f5d099885d3d | 142 | { |
Just4pLeisure | 0:f5d099885d3d | 143 | obj->ncs = 0; |
Just4pLeisure | 0:f5d099885d3d | 144 | obj->spi.write(MCP_BITMOD); |
Just4pLeisure | 0:f5d099885d3d | 145 | obj->spi.write(address); |
Just4pLeisure | 0:f5d099885d3d | 146 | obj->spi.write(mask); |
Just4pLeisure | 0:f5d099885d3d | 147 | obj->spi.write(data); |
Just4pLeisure | 0:f5d099885d3d | 148 | obj->ncs = 1; |
Just4pLeisure | 0:f5d099885d3d | 149 | } |