amiro-blt / Target / Source / ARMCM4_STM32 / can.c @ 7737db33
History | View | Annotate | Download (28.418 KB)
| 1 | 69661903 | Thomas Schöpping | /************************************************************************************//** |
|---|---|---|---|
| 2 | * \file Source\ARMCM4_STM32\can.c
|
||
| 3 | * \brief Bootloader CAN communication interface source file.
|
||
| 4 | * \ingroup Target_ARMCM4_STM32
|
||
| 5 | * \internal
|
||
| 6 | *----------------------------------------------------------------------------------------
|
||
| 7 | * C O P Y R I G H T
|
||
| 8 | *----------------------------------------------------------------------------------------
|
||
| 9 | * Copyright (c) 2013 by Feaser http://www.feaser.com All rights reserved
|
||
| 10 | *
|
||
| 11 | *----------------------------------------------------------------------------------------
|
||
| 12 | * L I C E N S E
|
||
| 13 | *----------------------------------------------------------------------------------------
|
||
| 14 | * This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
||
| 15 | * modify it under the terms of the GNU General Public License as published by the Free
|
||
| 16 | * Software Foundation, either version 3 of the License, or (at your option) any later
|
||
| 17 | * version.
|
||
| 18 | *
|
||
| 19 | * OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||
| 20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||
| 21 | * PURPOSE. See the GNU General Public License for more details.
|
||
| 22 | *
|
||
| 23 | * You should have received a copy of the GNU General Public License along with OpenBLT.
|
||
| 24 | * If not, see <http://www.gnu.org/licenses/>.
|
||
| 25 | *
|
||
| 26 | d54d2f07 | Thomas Schöpping | * A special exception to the GPL is included to allow you to distribute a combined work
|
| 27 | * that includes OpenBLT without being obliged to provide the source code for any
|
||
| 28 | 69661903 | Thomas Schöpping | * proprietary components. The exception text is included at the bottom of the license
|
| 29 | * file <license.html>.
|
||
| 30 | d54d2f07 | Thomas Schöpping | *
|
| 31 | 69661903 | Thomas Schöpping | * \endinternal
|
| 32 | ****************************************************************************************/
|
||
| 33 | |||
| 34 | d54d2f07 | Thomas Schöpping | #define CAN_DEBUG (0) |
| 35 | 69661903 | Thomas Schöpping | |
| 36 | /****************************************************************************************
|
||
| 37 | * Include files
|
||
| 38 | ****************************************************************************************/
|
||
| 39 | #include "boot.h" /* bootloader generic header */ |
||
| 40 | |||
| 41 | d54d2f07 | Thomas Schöpping | #if (CAN_DEBUG > 0) |
| 42 | #include <helper.h> |
||
| 43 | #endif
|
||
| 44 | |||
| 45 | 69661903 | Thomas Schöpping | |
| 46 | #if (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
||
| 47 | /****************************************************************************************
|
||
| 48 | * Type definitions
|
||
| 49 | ****************************************************************************************/
|
||
| 50 | /** \brief CAN transmission mailbox layout. */
|
||
| 51 | typedef struct |
||
| 52 | {
|
||
| 53 | volatile blt_int32u TIR;
|
||
| 54 | volatile blt_int32u TDTR;
|
||
| 55 | volatile blt_int32u TDLR;
|
||
| 56 | volatile blt_int32u TDHR;
|
||
| 57 | } tCanTxMailBox; |
||
| 58 | |||
| 59 | /** \brief CAN reception FIFO mailbox layout. */
|
||
| 60 | typedef struct |
||
| 61 | {
|
||
| 62 | volatile blt_int32u RIR;
|
||
| 63 | volatile blt_int32u RDTR;
|
||
| 64 | volatile blt_int32u RDLR;
|
||
| 65 | volatile blt_int32u RDHR;
|
||
| 66 | d54d2f07 | Thomas Schöpping | } tCanRxFIFOMailBox; |
| 67 | 69661903 | Thomas Schöpping | |
| 68 | /** \brief CAN filter register layout. */
|
||
| 69 | typedef struct |
||
| 70 | {
|
||
| 71 | volatile blt_int32u FR1;
|
||
| 72 | volatile blt_int32u FR2;
|
||
| 73 | } tCanFilter; |
||
| 74 | |||
| 75 | /** \brief CAN controller register layout. */
|
||
| 76 | typedef struct |
||
| 77 | {
|
||
| 78 | volatile blt_int32u MCR;
|
||
| 79 | volatile blt_int32u MSR;
|
||
| 80 | volatile blt_int32u TSR;
|
||
| 81 | volatile blt_int32u RF0R;
|
||
| 82 | volatile blt_int32u RF1R;
|
||
| 83 | volatile blt_int32u IER;
|
||
| 84 | volatile blt_int32u ESR;
|
||
| 85 | volatile blt_int32u BTR;
|
||
| 86 | blt_int32u RESERVED0[88];
|
||
| 87 | tCanTxMailBox sTxMailBox[3];
|
||
| 88 | tCanRxFIFOMailBox sFIFOMailBox[2];
|
||
| 89 | blt_int32u RESERVED1[12];
|
||
| 90 | volatile blt_int32u FMR;
|
||
| 91 | volatile blt_int32u FM1R;
|
||
| 92 | blt_int32u RESERVED2; |
||
| 93 | volatile blt_int32u FS1R;
|
||
| 94 | blt_int32u RESERVED3; |
||
| 95 | volatile blt_int32u FFA1R;
|
||
| 96 | blt_int32u RESERVED4; |
||
| 97 | volatile blt_int32u FA1R;
|
||
| 98 | blt_int32u RESERVED5[8];
|
||
| 99 | tCanFilter sFilterRegister[28];
|
||
| 100 | d54d2f07 | Thomas Schöpping | } tCanRegs; |
| 101 | 69661903 | Thomas Schöpping | |
| 102 | |||
| 103 | /****************************************************************************************
|
||
| 104 | * Macro definitions
|
||
| 105 | ****************************************************************************************/
|
||
| 106 | /** \brief Reset request bit. */
|
||
| 107 | #define CAN_BIT_RESET ((blt_int32u)0x00008000) |
||
| 108 | /** \brief Initialization request bit. */
|
||
| 109 | #define CAN_BIT_INRQ ((blt_int32u)0x00000001) |
||
| 110 | /** \brief Initialization acknowledge bit. */
|
||
| 111 | #define CAN_BIT_INAK ((blt_int32u)0x00000001) |
||
| 112 | /** \brief Sleep mode request bit. */
|
||
| 113 | #define CAN_BIT_SLEEP ((blt_int32u)0x00000002) |
||
| 114 | /** \brief Filter 0 selection bit. */
|
||
| 115 | #define CAN_BIT_FILTER0 ((blt_int32u)0x00000001) |
||
| 116 | /** \brief Filter 14 selection bit. */
|
||
| 117 | #define CAN_BIT_FILTER14 ((blt_int32u)0x00004000) |
||
| 118 | /** \brief Filter init mode bit. */
|
||
| 119 | #define CAN_BIT_FINIT ((blt_int32u)0x00000001) |
||
| 120 | /** \brief Transmit mailbox 0 empty bit. */
|
||
| 121 | #define CAN_BIT_TME0 ((blt_int32u)0x04000000) |
||
| 122 | /** \brief Transmit mailbox request bit. */
|
||
| 123 | #define CAN_BIT_TXRQ ((blt_int32u)0x00000001) |
||
| 124 | /** \brief Release FIFO 0 mailbox bit. */
|
||
| 125 | #define CAN_BIT_RFOM0 ((blt_int32u)0x00000020) |
||
| 126 | |||
| 127 | #if (BOOT_GATE_CAN_ENABLE > 0) |
||
| 128 | blt_bool commandSend; |
||
| 129 | #endif /* BOOT_GATE_CAN_ENABLE > 0 */ |
||
| 130 | |||
| 131 | |||
| 132 | /****************************************************************************************
|
||
| 133 | * Register definitions
|
||
| 134 | ****************************************************************************************/
|
||
| 135 | #if (BOOT_COM_CAN_CHANNEL_INDEX == 0) |
||
| 136 | /** \brief Macro for accessing CAN1 controller registers. */
|
||
| 137 | #define CANx ((tCanRegs *) (blt_int32u)0x40006400) |
||
| 138 | #else
|
||
| 139 | /** \brief Macro for accessing CAN2 controller registers. */
|
||
| 140 | #define CANx ((tCanRegs *) (blt_int32u)0x40006800) |
||
| 141 | #endif
|
||
| 142 | /** \brief Macro for accessing CAN1 controller registers. */
|
||
| 143 | #define CAN1 ((tCanRegs *) (blt_int32u)0x40006400) |
||
| 144 | |||
| 145 | |||
| 146 | /****************************************************************************************
|
||
| 147 | * Type definitions
|
||
| 148 | ****************************************************************************************/
|
||
| 149 | /** \brief Structure type for grouping CAN bus timing related information. */
|
||
| 150 | typedef struct t_can_bus_timing |
||
| 151 | {
|
||
| 152 | blt_int8u tseg1; /**< CAN time segment 1 */
|
||
| 153 | blt_int8u tseg2; /**< CAN time segment 2 */
|
||
| 154 | } tCanBusTiming; |
||
| 155 | |||
| 156 | |||
| 157 | /****************************************************************************************
|
||
| 158 | * Local constant declarations
|
||
| 159 | ****************************************************************************************/
|
||
| 160 | /** \brief CAN bittiming table for dynamically calculating the bittiming settings.
|
||
| 161 | d54d2f07 | Thomas Schöpping | * \details According to the CAN protocol 1 bit-time can be made up of between 8..25
|
| 162 | * time quanta (TQ). The total TQ in a bit is SYNC + TSEG1 + TSEG2 with SYNC
|
||
| 163 | * always being 1. The sample point is (SYNC + TSEG1) / (SYNC + TSEG1 + SEG2) *
|
||
| 164 | 69661903 | Thomas Schöpping | * 100%. This array contains possible and valid time quanta configurations with
|
| 165 | * a sample point between 68..78%.
|
||
| 166 | */
|
||
| 167 | static const tCanBusTiming canTiming[] = |
||
| 168 | { /* TQ | TSEG1 | TSEG2 | SP */
|
||
| 169 | /* ------------------------- */
|
||
| 170 | { 5, 2 }, /* 8 | 5 | 2 | 75% */
|
||
| 171 | { 6, 2 }, /* 9 | 6 | 2 | 78% */
|
||
| 172 | { 6, 3 }, /* 10 | 6 | 3 | 70% */
|
||
| 173 | { 7, 3 }, /* 11 | 7 | 3 | 73% */
|
||
| 174 | { 8, 3 }, /* 12 | 8 | 3 | 75% */
|
||
| 175 | { 9, 3 }, /* 13 | 9 | 3 | 77% */
|
||
| 176 | { 9, 4 }, /* 14 | 9 | 4 | 71% */
|
||
| 177 | { 10, 4 }, /* 15 | 10 | 4 | 73% */
|
||
| 178 | { 11, 4 }, /* 16 | 11 | 4 | 75% */
|
||
| 179 | { 12, 4 }, /* 17 | 12 | 4 | 76% */
|
||
| 180 | { 12, 5 }, /* 18 | 12 | 5 | 72% */
|
||
| 181 | { 13, 5 }, /* 19 | 13 | 5 | 74% */
|
||
| 182 | { 14, 5 }, /* 20 | 14 | 5 | 75% */
|
||
| 183 | { 15, 5 }, /* 21 | 15 | 5 | 76% */
|
||
| 184 | { 15, 6 }, /* 22 | 15 | 6 | 73% */
|
||
| 185 | { 16, 6 }, /* 23 | 16 | 6 | 74% */
|
||
| 186 | { 16, 7 }, /* 24 | 16 | 7 | 71% */
|
||
| 187 | { 16, 8 } /* 25 | 16 | 8 | 68% */
|
||
| 188 | }; |
||
| 189 | |||
| 190 | int counter = 0; |
||
| 191 | /************************************************************************************//** |
||
| 192 | d54d2f07 | Thomas Schöpping | ** \brief Search algorithm to match the desired baudrate to a possible bus
|
| 193 | 69661903 | Thomas Schöpping | ** timing configuration.
|
| 194 | ** \param baud The desired baudrate in kbps. Valid values are 10..1000.
|
||
| 195 | ** \param prescaler Pointer to where the value for the prescaler will be stored.
|
||
| 196 | ** \param tseg1 Pointer to where the value for TSEG2 will be stored.
|
||
| 197 | ** \param tseg2 Pointer to where the value for TSEG2 will be stored.
|
||
| 198 | d54d2f07 | Thomas Schöpping | ** \return BLT_TRUE if the CAN bustiming register values were found, BLT_FALSE
|
| 199 | 69661903 | Thomas Schöpping | ** otherwise.
|
| 200 | **
|
||
| 201 | ****************************************************************************************/
|
||
| 202 | d54d2f07 | Thomas Schöpping | static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler,
|
| 203 | 69661903 | Thomas Schöpping | blt_int8u *tseg1, blt_int8u *tseg2) |
| 204 | {
|
||
| 205 | blt_int8u cnt; |
||
| 206 | |||
| 207 | /* loop through all possible time quanta configurations to find a match */
|
||
| 208 | for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) |
||
| 209 | {
|
||
| 210 | if (((BOOT_CPU_SYSTEM_SPEED_KHZ/4) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) |
||
| 211 | {
|
||
| 212 | /* compute the prescaler that goes with this TQ configuration */
|
||
| 213 | *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/4)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); |
||
| 214 | |||
| 215 | /* make sure the prescaler is valid */
|
||
| 216 | if ( (*prescaler > 0) && (*prescaler <= 1024) ) |
||
| 217 | {
|
||
| 218 | /* store the bustiming configuration */
|
||
| 219 | *tseg1 = canTiming[cnt].tseg1; |
||
| 220 | *tseg2 = canTiming[cnt].tseg2; |
||
| 221 | /* found a good bus timing configuration */
|
||
| 222 | return BLT_TRUE;
|
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | /* could not find a good bus timing configuration */
|
||
| 227 |