Controller Area Network library for NUCLEO boards equipped with CAN peripheral.

Dependents:   Nucleo-Courtois CANBLE CANnucleo_Hello3 Nucleo_Serialprintf ... more

Controller Area Network library for the NUCLEO and DISCOVERY boards equipped with CAN peripheral


Information

Because CAN support has been finally implemented into the mbed library also for the ST boards there is no need to use the CANnucleo library anymore (however you may if you want). The CAN_Hello example is trying to demonstrate the mbed built-in CAN API with NUCLEO boards.


Provides CAN support for the following boards:

with the following features:

  • Easy to use. Delete the mbed library from your project and import the latest mbed-dev and CANnucleo libraries. In the mbed-dev library open the device.h file associated with the selected target board and add #undef DEVICE_CAN as follows:

device.h

#ifndef MBED_DEVICE_H
#define MBED_DEVICE_H

//=======================================
#define DEVICE_ID_LENGTH       24

#undef DEVICE_CAN

#include "objects.h"

#endif

See the CANnucleo_Hello demo for more details.

  • Automatic recovery from bus-off state can be enabled/disabled in the constructor (defaults to ENABLE).
  • Up to 14 filters (0 - 13) are available for the application to set up for message filtering performed by hardware.
    For more details see below or have a look at the comments in CANnucleo.cpp.
  • One CAN channel per NUCLEO board is supported. The CAN peripheral can be connected either to pins PA_11, PA_12 (Receiver, Transmitter) or to pins PB_8, PB_9 (Receiver, Transmitter). This is configured when creating a CAN instance.
  • Simplifies adding/getting data to/from a CAN message by using the << (append) and the >> (extract) operators.

Import programCANnucleo_Hello

Using CAN bus with NUCLEO boards (Demo for the CANnucleo library).



Filtering performed by the built-in CAN controller without disturbing the CPU

CANnucleo supports only mask mode and 32-bit filter scale. Identifier list mode filtering and 16-bit filter scale are not supported. There are 14 filters available (0 - 13) for the application to set up. Each filter is a 32-bit filter defined by a filter ID and a filter mask. If no filter is set up then no CAN message is accepted! That's why filter #0 is set up in the constructor to accept all CAN messages by default. On reception of a message it is compared with filter #0. If there is a match, the message is accepted and stored. If there is no match, the incoming identifier is then compared with the next filter. If the received identifier does not match any of the identifiers configured in the filters, the message is discarded by hardware without disturbing the software.

CAN filter function - designed to setup a CAN filter

int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle)

Parameters

id - 'Filter ID' defines the bit values to be compared with the corresponding received bits.

Mapping of 32-bits (4-bytes) :

STID[10:3]STID[2:0] EXID[17:13]EXID[12:5]EXID[4:0] IDE RTR 0
  • STID - Stardard Identifier bits
  • EXID - Extended Identifier bits
  • [x:y]- bit range
  • IDE - Identifier Extension bit (0 -> Standard Identifier, 1 -> Extended Identifier)
  • RTR - Remote Transmission Request bit (0 -> Remote Transmission Request, 1 -> Standard message)

mask - 'Filter mask' defines which bits of the 'Filter ID' are compared with the received bits and which are disregarded.
Mapping of 32-bits (4-bytes) :

STID[10:3]STID[2:0] EXID[17:13]EXID[12:5]EXID[4:0] IDE RTR 0
  • STID - Stardard Identifier bits
  • EXID - Extended Identifier bits
  • [x:y]- bit range
  • IDE - Identifier Extension bit
  • RTR - Remote Transmission Request bit
  • 1 -> bit is considered
  • 0 -> bit is disregarded

format - This parameter must be CANAny
handle - Selects the filter. This parameter must be a number between 0 and 13.
retval - 0 - successful, 1 - error, 2 - busy, 3 - time out

Example of filter set up and filtering

Let's assume we would like to accept only messages with standard identifier 0x207:

STID[15:0] = 0x207 = 00000010 00000111


We map the STID to filter ID by shifting the bits adequately:

Filter ID = STID << (16 + (15 - 10)) = STID << 21 = 01000000 11100000 00000000 00000000


To compare only the bits representing STID we set the filter mask appropriately:

Filter mask = 11111111 11100000 00000000 00000100 = 0xFFE00004
              |||||||| |||                    |
              -------- ---                    |
                  |     |                     |
           STID[10:3]  STID[2:0]             IDE


Recall that filter #0 has been set up in the constructor to accept all CAN messages by default. So we have to reconfigure it. If we were set up filter #1 here then filter #0 would accept all the messages and no message would reach filter #1!
To reconfigure (set up) filter #0 we call:

can.filter(0x207 << 21, 0xFFE00004, CANAny, 0);


            Only these bits of 'Filter id' (set to 1 here in 'Filter mask') are compared 
            with the corresponding bits of received message (the others are disregarded)
                                |
                 ---------------------------------
                 |||||||| |||                    |
   Filter mask = 11111111 11100000 00000000 00000100 (= 0xFFE00004)
   Filter id   = 01000000 11100000 00000000 00000000 (= 0x40E00000)
                 |||||||| |||                    |
                 ---------------------------------
                                |
            To accept the message the values of these bits must match.
            Otherwise the message is passed to the next filter or
            discarded if this was the last active filter.
                                |
                 ---------------------------------
                 |||||||| |||                    |
   Received id = 01000000 11100000 00000000 00000010 (= 0x40E00002)
                             ||||| |||||||| ||||| ||
                             -----------------------
                                         |
                          These bits (set to 0 in 'Filter mask') are disregarded (masked).
                          They can have arbitrary values.


NOTE: For the meaning of individual bits see the mapping of 32-bits explained above.

We can use the filter function to setup more (up to 14) CAN filters for example as follows:

can.filter(0x207 << 21, 0xFFE00004, CANAny, 0);    // filter #0
can.filter(0x251 << 21, 0xFFE00004, CANAny, 1);    // filter #1
can.filter(0x304 << 21, 0xFFE00004, CANAny, 2);    // filter #2
...
Committer:
hudakz
Date:
Sun Jul 19 14:04:31 2015 +0000
Revision:
1:eb04f7f0478d
Parent:
0:e29bc8e0dddd
Child:
2:09a0d2838572
rev 01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:e29bc8e0dddd 1 /*
hudakz 0:e29bc8e0dddd 2 can_api.c for STMicroelectronics mbed boards equipped with Controller Area Network interface:
hudakz 0:e29bc8e0dddd 3
hudakz 0:e29bc8e0dddd 4 NUCLEO-F072RB
hudakz 1:eb04f7f0478d 5 NUCLEO-F091RC
hudakz 0:e29bc8e0dddd 6 NUCLEO-F103RB
hudakz 0:e29bc8e0dddd 7 NUCLEO-F302R8
hudakz 1:eb04f7f0478d 8 NUCLEO-F303RE
hudakz 0:e29bc8e0dddd 9 NUCLEO-F334R8
hudakz 0:e29bc8e0dddd 10 DISCO-F334C8
hudakz 0:e29bc8e0dddd 11
hudakz 0:e29bc8e0dddd 12 Copyright (c) 2015 Zoltan Hudak <hudakz@inbox.com>
hudakz 0:e29bc8e0dddd 13 All rights reserved.
hudakz 0:e29bc8e0dddd 14
hudakz 0:e29bc8e0dddd 15 This program is free software: you can redistribute it and/or modify
hudakz 0:e29bc8e0dddd 16 it under the terms of the GNU General Public License as published by
hudakz 0:e29bc8e0dddd 17 the Free Software Foundation, either version 3 of the License, or
hudakz 0:e29bc8e0dddd 18 (at your option) any later version.
hudakz 0:e29bc8e0dddd 19
hudakz 0:e29bc8e0dddd 20 This program is distributed in the hope that it will be useful,
hudakz 0:e29bc8e0dddd 21 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:e29bc8e0dddd 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:e29bc8e0dddd 23 GNU General Public License for more details.
hudakz 0:e29bc8e0dddd 24
hudakz 0:e29bc8e0dddd 25 You should have received a copy of the GNU General Public License
hudakz 0:e29bc8e0dddd 26 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:e29bc8e0dddd 27 */
hudakz 0:e29bc8e0dddd 28 #include "stm32f1xx_hal.h"
hudakz 0:e29bc8e0dddd 29 #include "can_api.h"
hudakz 0:e29bc8e0dddd 30 #include "can_helper.h"
hudakz 0:e29bc8e0dddd 31 #include "pinmap.h"
hudakz 0:e29bc8e0dddd 32
hudakz 0:e29bc8e0dddd 33 extern void (*rxCompleteCallback) (void);
hudakz 0:e29bc8e0dddd 34 extern CAN_HandleTypeDef _canHandle;
hudakz 0:e29bc8e0dddd 35
hudakz 0:e29bc8e0dddd 36 /**
hudakz 0:e29bc8e0dddd 37 * @brief
hudakz 0:e29bc8e0dddd 38 * @note
hudakz 0:e29bc8e0dddd 39 * @param
hudakz 0:e29bc8e0dddd 40 * @retval
hudakz 0:e29bc8e0dddd 41 */
hudakz 0:e29bc8e0dddd 42 void can_init(can_t* obj, PinName rd, PinName td) {
hudakz 0:e29bc8e0dddd 43 initCAN(obj, rd, td);
hudakz 0:e29bc8e0dddd 44 can_filter(obj, 0, 0, CANAny, 0);
hudakz 0:e29bc8e0dddd 45 }
hudakz 0:e29bc8e0dddd 46
hudakz 0:e29bc8e0dddd 47 /**
hudakz 0:e29bc8e0dddd 48 * @brief
hudakz 0:e29bc8e0dddd 49 * @note
hudakz 0:e29bc8e0dddd 50 * @param
hudakz 0:e29bc8e0dddd 51 * @retval
hudakz 0:e29bc8e0dddd 52 */
hudakz 0:e29bc8e0dddd 53 void can_free(can_t* obj) {
hudakz 0:e29bc8e0dddd 54 HAL_CAN_MspDeInit(obj);
hudakz 0:e29bc8e0dddd 55 }
hudakz 0:e29bc8e0dddd 56
hudakz 0:e29bc8e0dddd 57 /**
hudakz 0:e29bc8e0dddd 58 * @brief
hudakz 0:e29bc8e0dddd 59 * @note
hudakz 0:e29bc8e0dddd 60 * @param
hudakz 0:e29bc8e0dddd 61 * @retval
hudakz 0:e29bc8e0dddd 62 */
hudakz 0:e29bc8e0dddd 63 int can_frequency(can_t* obj, int hz) {
hudakz 0:e29bc8e0dddd 64 HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
hudakz 0:e29bc8e0dddd 65
hudakz 0:e29bc8e0dddd 66 #if defined(TARGET_NUCLEO_F103RB) || \
hudakz 0:e29bc8e0dddd 67 defined(TARGET_NUCLEO_F302R8) || \
hudakz 1:eb04f7f0478d 68 defined(TARGET_NUCLEO_F303RE) || \
hudakz 0:e29bc8e0dddd 69 defined(TARGET_NUCLEO_F334R8) || \
hudakz 0:e29bc8e0dddd 70 defined(TARGET_DISCO_F334C8)
hudakz 0:e29bc8e0dddd 71 // APB1 pheripheral clock = 36000000Hz
hudakz 0:e29bc8e0dddd 72
hudakz 0:e29bc8e0dddd 73 switch(hz) {
hudakz 0:e29bc8e0dddd 74 case 1000000:
hudakz 0:e29bc8e0dddd 75 // 1000kbps bit rate
hudakz 0:e29bc8e0dddd 76 _canHandle.Init.Prescaler = 3; // number of time quanta = 36000000/3/1000000 = 12
hudakz 0:e29bc8e0dddd 77 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 78 _canHandle.Init.BS1 = CAN_BS1_8TQ; // sample point at: (1 + 8) / 12 * 100 = 75%
hudakz 0:e29bc8e0dddd 79 _canHandle.Init.BS2 = CAN_BS2_3TQ;
hudakz 0:e29bc8e0dddd 80 break;
hudakz 0:e29bc8e0dddd 81
hudakz 0:e29bc8e0dddd 82 case 500000:
hudakz 0:e29bc8e0dddd 83 // 500kbps bit rate
hudakz 0:e29bc8e0dddd 84 _canHandle.Init.Prescaler = 6; // number of time quanta = 36000000/6/500000 = 12
hudakz 0:e29bc8e0dddd 85 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 86 _canHandle.Init.BS1 = CAN_BS1_8TQ; // sample point at: (1 + 8) / 12 * 100 = 75%
hudakz 0:e29bc8e0dddd 87 _canHandle.Init.BS2 = CAN_BS2_3TQ;
hudakz 0:e29bc8e0dddd 88 break;
hudakz 0:e29bc8e0dddd 89
hudakz 0:e29bc8e0dddd 90 case 250000:
hudakz 0:e29bc8e0dddd 91 // 250kbps
hudakz 0:e29bc8e0dddd 92 _canHandle.Init.Prescaler = 9; // number of time quanta = 36000000/9/250000 = 16
hudakz 0:e29bc8e0dddd 93 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 94 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 0:e29bc8e0dddd 95 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 0:e29bc8e0dddd 96 break;
hudakz 0:e29bc8e0dddd 97
hudakz 0:e29bc8e0dddd 98 case 125000:
hudakz 0:e29bc8e0dddd 99 // 125kbps
hudakz 0:e29bc8e0dddd 100 _canHandle.Init.Prescaler = 18; // number of time quanta = 36000000/18/125000 = 16
hudakz 0:e29bc8e0dddd 101 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 102 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 0:e29bc8e0dddd 103 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 0:e29bc8e0dddd 104 break;
hudakz 0:e29bc8e0dddd 105
hudakz 0:e29bc8e0dddd 106 default:
hudakz 0:e29bc8e0dddd 107 // 125kbps (default)
hudakz 0:e29bc8e0dddd 108 printf("Unknown frequency specified!\r\n");
hudakz 0:e29bc8e0dddd 109 printf("Using default 125kbps\r\n");
hudakz 0:e29bc8e0dddd 110 _canHandle.Init.Prescaler = 18; // number of time quanta = 36000000/18/125000 = 16
hudakz 0:e29bc8e0dddd 111 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 112 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 0:e29bc8e0dddd 113 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 0:e29bc8e0dddd 114 }
hudakz 0:e29bc8e0dddd 115
hudakz 1:eb04f7f0478d 116 #elif defined(TARGET_NUCLEO_F072RB) || \
hudakz 0:e29bc8e0dddd 117 defined(TARGET_NUCLEO_F091RC)
hudakz 0:e29bc8e0dddd 118 // APB1 pheripheral clock = 48000000Hz
hudakz 0:e29bc8e0dddd 119
hudakz 0:e29bc8e0dddd 120 switch(hz) {
hudakz 0:e29bc8e0dddd 121 case 1000000:
hudakz 0:e29bc8e0dddd 122 // 1000kbps bit rate
hudakz 0:e29bc8e0dddd 123 _canHandle.Init.Prescaler = 4; // number of time quanta = 48000000/4/1000000 = 12
hudakz 0:e29bc8e0dddd 124 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 125 _canHandle.Init.BS1 = CAN_BS1_8TQ; // sample point at: (1 + 8) / 12 * 100 = 75%
hudakz 0:e29bc8e0dddd 126 _canHandle.Init.BS2 = CAN_BS2_3TQ;
hudakz 0:e29bc8e0dddd 127 break;
hudakz 0:e29bc8e0dddd 128
hudakz 0:e29bc8e0dddd 129 case 500000:
hudakz 0:e29bc8e0dddd 130 // 500kbps bit rate
hudakz 0:e29bc8e0dddd 131 _canHandle.Init.Prescaler = 8; // number of time quanta = 48000000/8/500000 = 12
hudakz 0:e29bc8e0dddd 132 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 133 _canHandle.Init.BS1 = CAN_BS1_8TQ; // sample point at: (1 + 8) / 12 * 100 = 75%
hudakz 0:e29bc8e0dddd 134 _canHandle.Init.BS2 = CAN_BS2_3TQ;
hudakz 0:e29bc8e0dddd 135 break;
hudakz 0:e29bc8e0dddd 136
hudakz 0:e29bc8e0dddd 137 case 250000:
hudakz 0:e29bc8e0dddd 138 // 250kbps
hudakz 0:e29bc8e0dddd 139 _canHandle.Init.Prescaler = 12; // number of time quanta = 48000000/12/250000 = 16
hudakz 0:e29bc8e0dddd 140 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 141 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 0:e29bc8e0dddd 142 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 0:e29bc8e0dddd 143 break;
hudakz 0:e29bc8e0dddd 144
hudakz 0:e29bc8e0dddd 145 case 125000:
hudakz 0:e29bc8e0dddd 146 // 125kbps
hudakz 0:e29bc8e0dddd 147 _canHandle.Init.Prescaler = 24; // number of time quanta = 48000000/24/125000 = 16
hudakz 0:e29bc8e0dddd 148 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 149 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 0:e29bc8e0dddd 150 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 0:e29bc8e0dddd 151 break;
hudakz 0:e29bc8e0dddd 152
hudakz 0:e29bc8e0dddd 153 default:
hudakz 0:e29bc8e0dddd 154 // 125kbps (default)
hudakz 0:e29bc8e0dddd 155 printf("Unknown frequency specified!\r\n");
hudakz 0:e29bc8e0dddd 156 printf("Using default 125kbps\r\n");
hudakz 0:e29bc8e0dddd 157 _canHandle.Init.Prescaler = 24; // number of time quanta = 48000000/24/125000 = 16
hudakz 0:e29bc8e0dddd 158 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 0:e29bc8e0dddd 159 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 0:e29bc8e0dddd 160 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 0:e29bc8e0dddd 161 }
hudakz 0:e29bc8e0dddd 162 #endif
hudakz 0:e29bc8e0dddd 163 HAL_CAN_Init(&_canHandle);
hudakz 0:e29bc8e0dddd 164 HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
hudakz 0:e29bc8e0dddd 165 }
hudakz 0:e29bc8e0dddd 166
hudakz 0:e29bc8e0dddd 167 /**
hudakz 0:e29bc8e0dddd 168 * @brief
hudakz 0:e29bc8e0dddd 169 * @note
hudakz 0:e29bc8e0dddd 170 * @param
hudakz 0:e29bc8e0dddd 171 * @retval
hudakz 0:e29bc8e0dddd 172 */
hudakz 0:e29bc8e0dddd 173 void can_irq_init(can_t* obj, can_irq_handler handler, uint32_t id) {
hudakz 0:e29bc8e0dddd 174 if(HAL_CAN_Receive_IT(&_canHandle, CAN_FIFO0) != HAL_OK) {
hudakz 0:e29bc8e0dddd 175 printf("CAN reception initialization error\r\n");
hudakz 0:e29bc8e0dddd 176 }
hudakz 0:e29bc8e0dddd 177 }
hudakz 0:e29bc8e0dddd 178
hudakz 0:e29bc8e0dddd 179 /**
hudakz 0:e29bc8e0dddd 180 * @brief
hudakz 0:e29bc8e0dddd 181 * @note
hudakz 0:e29bc8e0dddd 182 * @param
hudakz 0:e29bc8e0dddd 183 * @retval
hudakz 0:e29bc8e0dddd 184 */
hudakz 0:e29bc8e0dddd 185 void can_irq_free(can_t* obj) {
hudakz 0:e29bc8e0dddd 186 rxCompleteCallback = NULL;
hudakz 0:e29bc8e0dddd 187 }
hudakz 0:e29bc8e0dddd 188
hudakz 0:e29bc8e0dddd 189 /**
hudakz 0:e29bc8e0dddd 190 * @brief
hudakz 0:e29bc8e0dddd 191 * @note
hudakz 0:e29bc8e0dddd 192 * @param
hudakz 0:e29bc8e0dddd 193 * @retval
hudakz 0:e29bc8e0dddd 194 */
hudakz 0:e29bc8e0dddd 195 void can_irq_set(void (*fptr) (void)) {
hudakz 0:e29bc8e0dddd 196 rxCompleteCallback = fptr;
hudakz 0:e29bc8e0dddd 197 }
hudakz 0:e29bc8e0dddd 198
hudakz 0:e29bc8e0dddd 199 /**
hudakz 0:e29bc8e0dddd 200 * @brief
hudakz 0:e29bc8e0dddd 201 * @note
hudakz 0:e29bc8e0dddd 202 * @param
hudakz 0:e29bc8e0dddd 203 * @retval
hudakz 0:e29bc8e0dddd 204 */
hudakz 0:e29bc8e0dddd 205 int can_write(can_t* obj, CAN_Message msg, int cc) {
hudakz 0:e29bc8e0dddd 206 int i = 0;
hudakz 0:e29bc8e0dddd 207
hudakz 0:e29bc8e0dddd 208 if(msg.format == CANStandard) {
hudakz 0:e29bc8e0dddd 209 _canHandle.pTxMsg->StdId = msg.id;
hudakz 0:e29bc8e0dddd 210 _canHandle.pTxMsg->ExtId = 0x00;
hudakz 0:e29bc8e0dddd 211 }
hudakz 0:e29bc8e0dddd 212 else {
hudakz 0:e29bc8e0dddd 213 _canHandle.pTxMsg->StdId = 0x00;
hudakz 0:e29bc8e0dddd 214 _canHandle.pTxMsg->ExtId = msg.id;
hudakz 0:e29bc8e0dddd 215 }
hudakz 0:e29bc8e0dddd 216
hudakz 0:e29bc8e0dddd 217 _canHandle.pTxMsg->RTR = msg.type == CANData ? CAN_RTR_DATA : CAN_RTR_REMOTE;
hudakz 0:e29bc8e0dddd 218 _canHandle.pTxMsg->IDE = msg.format == CANStandard ? CAN_ID_STD : CAN_ID_EXT;
hudakz 0:e29bc8e0dddd 219 _canHandle.pTxMsg->DLC = msg.len;
hudakz 0:e29bc8e0dddd 220
hudakz 0:e29bc8e0dddd 221 for(i = 0; i < msg.len; i++)
hudakz 0:e29bc8e0dddd 222 _canHandle.pTxMsg->Data[i] = msg.data[i];
hudakz 0:e29bc8e0dddd 223
hudakz 0:e29bc8e0dddd 224 if(HAL_CAN_Transmit(&_canHandle, 10) != HAL_OK) {
hudakz 0:e29bc8e0dddd 225 printf("Transmission error\r\n");
hudakz 0:e29bc8e0dddd 226 }
hudakz 0:e29bc8e0dddd 227 }
hudakz 0:e29bc8e0dddd 228
hudakz 0:e29bc8e0dddd 229 /**
hudakz 0:e29bc8e0dddd 230 * @brief
hudakz 0:e29bc8e0dddd 231 * @note
hudakz 0:e29bc8e0dddd 232 * @param
hudakz 0:e29bc8e0dddd 233 * @retval
hudakz 0:e29bc8e0dddd 234 */
hudakz 0:e29bc8e0dddd 235 int can_read(can_t* obj, CAN_Message* msg, int handle) {
hudakz 0:e29bc8e0dddd 236 int i = 0;
hudakz 0:e29bc8e0dddd 237
hudakz 0:e29bc8e0dddd 238 msg->id = _canHandle.pRxMsg->IDE == CAN_ID_STD ? _canHandle.pRxMsg->StdId : _canHandle.pRxMsg->ExtId;
hudakz 0:e29bc8e0dddd 239 msg->type = _canHandle.pRxMsg->RTR == CAN_RTR_DATA ? CANData : CANRemote;
hudakz 0:e29bc8e0dddd 240 msg->format = _canHandle.pRxMsg->IDE == CAN_ID_STD ? CANStandard : CANExtended;
hudakz 0:e29bc8e0dddd 241 msg->len = _canHandle.pRxMsg->DLC;
hudakz 0:e29bc8e0dddd 242 for(i = 0; i < msg->len; i++)
hudakz 0:e29bc8e0dddd 243 msg->data[i] = _canHandle.pRxMsg->Data[i];
hudakz 0:e29bc8e0dddd 244 }
hudakz 0:e29bc8e0dddd 245
hudakz 0:e29bc8e0dddd 246 /**
hudakz 0:e29bc8e0dddd 247 * @brief
hudakz 0:e29bc8e0dddd 248 * @note
hudakz 0:e29bc8e0dddd 249 * @param
hudakz 0:e29bc8e0dddd 250 * @retval
hudakz 0:e29bc8e0dddd 251 */
hudakz 0:e29bc8e0dddd 252 int can_mode(can_t* obj, CanMode mode) {
hudakz 0:e29bc8e0dddd 253 switch(mode) {
hudakz 0:e29bc8e0dddd 254 case MODE_RESET:
hudakz 0:e29bc8e0dddd 255 return HAL_ERROR;
hudakz 0:e29bc8e0dddd 256
hudakz 0:e29bc8e0dddd 257 case MODE_NORMAL:
hudakz 0:e29bc8e0dddd 258 _canHandle.Init.Mode = CAN_MODE_NORMAL;
hudakz 0:e29bc8e0dddd 259 break;
hudakz 0:e29bc8e0dddd 260
hudakz 0:e29bc8e0dddd 261 case MODE_SILENT:
hudakz 0:e29bc8e0dddd 262 _canHandle.Init.Mode = CAN_MODE_SILENT;
hudakz 0:e29bc8e0dddd 263 break;
hudakz 0:e29bc8e0dddd 264
hudakz 0:e29bc8e0dddd 265 case MODE_TEST_GLOBAL:
hudakz 0:e29bc8e0dddd 266 _canHandle.Init.Mode = CAN_MODE_LOOPBACK;
hudakz 0:e29bc8e0dddd 267 break;
hudakz 0:e29bc8e0dddd 268
hudakz 0:e29bc8e0dddd 269 case MODE_TEST_LOCAL:
hudakz 0:e29bc8e0dddd 270 _canHandle.Init.Mode = CAN_MODE_LOOPBACK;
hudakz 0:e29bc8e0dddd 271 break;
hudakz 0:e29bc8e0dddd 272
hudakz 0:e29bc8e0dddd 273 case MODE_TEST_SILENT:
hudakz 0:e29bc8e0dddd 274 _canHandle.Init.Mode = CAN_MODE_SILENT_LOOPBACK;
hudakz 0:e29bc8e0dddd 275 break;
hudakz 0:e29bc8e0dddd 276 }
hudakz 0:e29bc8e0dddd 277
hudakz 0:e29bc8e0dddd 278 _canHandle.Init.Mode = CAN_MODE_NORMAL;
hudakz 0:e29bc8e0dddd 279 return HAL_CAN_Init(&_canHandle);
hudakz 0:e29bc8e0dddd 280 }
hudakz 0:e29bc8e0dddd 281
hudakz 0:e29bc8e0dddd 282 /**
hudakz 0:e29bc8e0dddd 283 * @brief
hudakz 0:e29bc8e0dddd 284 * @note
hudakz 0:e29bc8e0dddd 285 * @param
hudakz 0:e29bc8e0dddd 286 * @retval
hudakz 0:e29bc8e0dddd 287 */
hudakz 0:e29bc8e0dddd 288 int can_filter(can_t* obj, uint32_t id, uint32_t mask, CANFormat format /*=CANAny*/, int32_t handle /*=0*/ ) {
hudakz 0:e29bc8e0dddd 289 CAN_FilterConfTypeDef sFilterConfig;
hudakz 0:e29bc8e0dddd 290
hudakz 0:e29bc8e0dddd 291 sFilterConfig.FilterNumber = handle;
hudakz 0:e29bc8e0dddd 292 sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
hudakz 0:e29bc8e0dddd 293 sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
hudakz 0:e29bc8e0dddd 294 sFilterConfig.FilterIdHigh = (((id) >> 16) & 0xFFFF);
hudakz 0:e29bc8e0dddd 295 sFilterConfig.FilterIdLow = ((id) & 0xFFFF);
hudakz 0:e29bc8e0dddd 296 sFilterConfig.FilterMaskIdHigh = (((mask) >> 16) & 0xFFFF);
hudakz 0:e29bc8e0dddd 297 sFilterConfig.FilterMaskIdLow = ((mask) & 0xFFFF);
hudakz 0:e29bc8e0dddd 298 sFilterConfig.FilterFIFOAssignment = 0;
hudakz 0:e29bc8e0dddd 299 sFilterConfig.FilterActivation = ENABLE;
hudakz 0:e29bc8e0dddd 300 sFilterConfig.BankNumber = 14;
hudakz 0:e29bc8e0dddd 301 HAL_CAN_ConfigFilter(&_canHandle, &sFilterConfig);
hudakz 0:e29bc8e0dddd 302 }
hudakz 0:e29bc8e0dddd 303
hudakz 0:e29bc8e0dddd 304 /**
hudakz 0:e29bc8e0dddd 305 * @brief
hudakz 0:e29bc8e0dddd 306 * @note
hudakz 0:e29bc8e0dddd 307 * @param
hudakz 0:e29bc8e0dddd 308 * @retval
hudakz 0:e29bc8e0dddd 309 */
hudakz 0:e29bc8e0dddd 310 void can_reset(can_t* obj) {
hudakz 0:e29bc8e0dddd 311 __HAL_CAN_RESET_HANDLE_STATE(&_canHandle);
hudakz 0:e29bc8e0dddd 312 }
hudakz 0:e29bc8e0dddd 313
hudakz 0:e29bc8e0dddd 314 /**
hudakz 0:e29bc8e0dddd 315 * @brief
hudakz 0:e29bc8e0dddd 316 * @note
hudakz 0:e29bc8e0dddd 317 * @param
hudakz 0:e29bc8e0dddd 318 * @retval
hudakz 0:e29bc8e0dddd 319 */
hudakz 0:e29bc8e0dddd 320 unsigned char can_rderror(can_t* obj) {
hudakz 0:e29bc8e0dddd 321 return HAL_CAN_GetError(&_canHandle);
hudakz 0:e29bc8e0dddd 322 }
hudakz 0:e29bc8e0dddd 323
hudakz 0:e29bc8e0dddd 324 /**
hudakz 0:e29bc8e0dddd 325 * @brief
hudakz 0:e29bc8e0dddd 326 * @note
hudakz 0:e29bc8e0dddd 327 * @param
hudakz 0:e29bc8e0dddd 328 * @retval
hudakz 0:e29bc8e0dddd 329 */
hudakz 0:e29bc8e0dddd 330 unsigned char can_tderror(can_t* obj) {
hudakz 0:e29bc8e0dddd 331 return HAL_CAN_GetError(&_canHandle);
hudakz 0:e29bc8e0dddd 332 }
hudakz 0:e29bc8e0dddd 333
hudakz 0:e29bc8e0dddd 334 /**
hudakz 0:e29bc8e0dddd 335 * @brief
hudakz 0:e29bc8e0dddd 336 * @note
hudakz 0:e29bc8e0dddd 337 * @param
hudakz 0:e29bc8e0dddd 338 * @retval
hudakz 0:e29bc8e0dddd 339 */
hudakz 0:e29bc8e0dddd 340 void can_monitor(can_t* obj, int silent) {
hudakz 0:e29bc8e0dddd 341
hudakz 0:e29bc8e0dddd 342 // not implemented
hudakz 0:e29bc8e0dddd 343 }
hudakz 1:eb04f7f0478d 344