Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:52

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup m
0007  *
0008  * @brief MSCAN register definitions and support functions.
0009  */
0010 
0011 /*
0012  * Copyright (c) 2008 embedded brains GmbH & Co. KG
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifndef LIBBSP_MSCAN_BASE_H
0036 #define LIBBSP_MSCAN_BASE_H
0037 
0038 #include <stdbool.h>
0039 
0040 #include <bsp/mpc5200.h>
0041 
0042 /**
0043  * @defgroup RTEMSBSPsPowerPCGen5200MSCAN MSCAN
0044  *
0045  * @ingroup RTEMSBSPsPowerPCGen5200
0046  *
0047  * @{
0048  */
0049 
0050 #define MSCAN_BIT_RATE_MIN 10000
0051 
0052 #define MSCAN_BIT_RATE_MAX 1000000
0053 
0054 #define MSCAN_BIT_RATE_DEFAULT 125000
0055 
0056 #define MSCAN_FILTER_NUMBER_MIN 0
0057 
0058 #define MSCAN_FILTER_NUMBER_2 2
0059 
0060 #define MSCAN_FILTER_NUMBER_4 4
0061 
0062 #define MSCAN_FILTER_NUMBER_MAX 8
0063 
0064 #define MSCAN_FILTER_ID_DEFAULT 0U
0065 
0066 #define MSCAN_FILTER_MASK_DEFAULT 0xffffffffU
0067 
0068 #define MSCAN_TRANSMIT_BUFFER_NUMBER 3
0069 
0070 /**
0071  * @name MSCAN Control Register 0 (CANCTL0)
0072  *
0073  * @{
0074  */
0075 
0076 #define CTL0_RXFRM               (1 << 7)
0077 #define CTL0_RXACT               (1 << 6)
0078 #define CTL0_CSWAI               (1 << 5)
0079 #define CTL0_SYNCH               (1 << 4)
0080 #define CTL0_TIME                (1 << 3)
0081 #define CTL0_WUPE                (1 << 2)
0082 #define CTL0_SLPRQ               (1 << 1)
0083 #define CTL0_INITRQ              (1 << 0)
0084 
0085 /** @} */
0086 
0087 /**
0088  * @name MSCAN Control Register 1 (CANCTL1)
0089  *
0090  * @{
0091  */
0092 
0093 #define CTL1_CANE                (1 << 7)
0094 #define CTL1_CLKSRC              (1 << 6)
0095 #define CTL1_LOOPB               (1 << 5)
0096 #define CTL1_LISTEN              (1 << 4)
0097 #define CTL1_WUPM                (1 << 2)
0098 #define CTL1_SLPAK               (1 << 1)
0099 #define CTL1_INITAK              (1 << 0)
0100 
0101 /** @} */
0102 
0103 /**
0104  * @name MSCAN Bus Timing Register 0 (CANBTR0)
0105  *
0106  * @{
0107  */
0108 
0109 #define BTR0_SJW_MASK            0xc0
0110 #define BTR0_BRP_MASK            0x3f
0111 
0112 #define BTR0_SJW( btr0)          ((btr0) << 6)
0113 #define BTR0_BRP( btr0)          ((btr0) << 0)
0114 
0115 #define BTR0_GET_SJW( btr0)      (((btr0) & BTR0_SJW_MASK) >> 6)
0116 #define BTR0_GET_BRP( btr0)      (((btr0) & BTR0_BRP_MASK) >> 0)
0117 
0118 /** @} */
0119 
0120 /**
0121  * @name MSCAN Bus Timing Register 1 (CANBTR1)
0122  *
0123  * @{
0124  */
0125 
0126 #define BTR1_SAMP_MASK           0x80
0127 #define BTR1_TSEG1_MASK          0x0f
0128 #define BTR1_TSEG2_MASK          0x70
0129 
0130 #define BTR1_SAMP                (1 << 7)
0131 #define BTR1_TSEG1( btr1)        ((btr1) << 0)
0132 #define BTR1_TSEG2( btr1)        ((btr1) << 4)
0133 
0134 #define BTR1_GET_TSEG1( btr0)    (((btr0) & BTR1_TSEG1_MASK) >> 0)
0135 #define BTR1_GET_TSEG2( btr0)    (((btr0) & BTR1_TSEG2_MASK) >> 4)
0136 
0137 /** @} */
0138 
0139 /**
0140  * @name MSCAN Receiver Flag Register (CANRFLG)
0141  *
0142  * @{
0143  */
0144 
0145 #define RFLG_WUPIF               (1 << 7)
0146 #define RFLG_CSCIF               (1 << 6)
0147 #define RFLG_RSTAT_MASK          (3 << 4)
0148 #define RFLG_RSTAT_OK            (0 << 4)
0149 #define RFLG_RSTAT_WRN           (1 << 4)
0150 #define RFLG_RSTAT_ERR           (2 << 4)
0151 #define RFLG_RSTAT_OFF           (3 << 4)
0152 #define RFLG_TSTAT_MASK          (3 << 2)
0153 #define RFLG_TSTAT_OK            (0 << 2)
0154 #define RFLG_TSTAT_WRN           (1 << 2)
0155 #define RFLG_TSTAT_ERR           (2 << 2)
0156 #define RFLG_TSTAT_OFF           (3 << 2)
0157 #define RFLG_OVRIF               (1 << 1)
0158 #define RFLG_RXF                 (1 << 0)
0159 #define RFLG_GET_RX_STATE(rflg)  (((rflg) >> 4) & 0x03)
0160 #define RFLG_GET_TX_STATE(rflg)  (((rflg) >> 2) & 0x03)
0161 
0162 /** @} */
0163 
0164 /**
0165  * @name MSCAN Receiver Interrupt Enable Register (CANRIER)
0166  *
0167  * @{
0168  */
0169 
0170 #define RIER_WUPIE               (1 << 7)
0171 #define RIER_CSCIE               (1 << 6)
0172 #define RIER_RSTAT(rier)         ((rier) << 4)
0173 #define RIER_TSTAT(rier)         ((rier) << 2)
0174 #define RIER_OVRIE               (1 << 1)
0175 #define RIER_RXFIE               (1 << 0)
0176 
0177 /** @} */
0178 
0179 /**
0180  * @name MSCAN Transmitter Flag Register (CANTFLG)
0181  *
0182  * @{
0183  */
0184 
0185 #define TFLG_TXE2                (1 << 2)
0186 #define TFLG_TXE1                (1 << 1)
0187 #define TFLG_TXE0                (1 << 0)
0188 
0189 /** @} */
0190 
0191 /**
0192  * @name MSCAN Transmitter Interrupt Enable Register (CANTIER)
0193  *
0194  * @{
0195  */
0196 
0197 #define TIER_TXEI2               (1 << 2)
0198 #define TIER_TXEI1               (1 << 1)
0199 #define TIER_TXEI0               (1 << 0)
0200 
0201 /** @} */
0202 
0203 /**
0204  * @name MSCAN Transmitter Message Abort Request (CANTARQ)
0205  *
0206  * @{
0207  */
0208 
0209 #define TARQ_ABTRQ2              (1 << 2)
0210 #define TARQ_ABTRQ1              (1 << 1)
0211 #define TARQ_ABTRQ0              (1 << 0)
0212 
0213 /** @} */
0214 
0215 /**
0216  * @name MSCAN Transmitter Message Abort Acknoledge (CANTAAK)
0217  *
0218  * @{
0219  */
0220 
0221 #define TAAK_ABTRQ2              (1 << 2)
0222 #define TAAK_ABTRQ1              (1 << 1)
0223 #define TAAK_ABTRQ0              (1 << 0)
0224 
0225 /** @} */
0226 
0227 /**
0228  * @name MSCAN Transmit Buffer Selection (CANBSEL)
0229  *
0230  * @{
0231  */
0232 
0233 #define BSEL_TX2                 (1 << 2)
0234 #define BSEL_TX1                 (1 << 1)
0235 #define BSEL_TX0                 (1 << 0)
0236 
0237 /** @} */
0238 
0239 /**
0240  * @name MSCAN ID Acceptance Control Register (CANIDAC)
0241  *
0242  * @{
0243  */
0244 
0245 #define IDAC_IDAM1               (1 << 5)
0246 #define IDAC_IDAM0               (1 << 4)
0247 #define IDAC_IDAM                (IDAC_IDAM1 | IDAC_IDAM0)
0248 #define IDAC_IDHIT( idac)        ((idac) & 0x7)
0249 
0250 /** @} */
0251 
0252 /**
0253  * @brief MSCAN registers.
0254  */
0255 typedef struct mpc5200_mscan mscan;
0256 
0257 /**
0258  * @brief MSCAN context that has to be saved throughout the initialization
0259  * mode.
0260  */
0261 typedef struct {
0262   uint8_t ctl0;
0263   uint8_t rier;
0264   uint8_t tier;
0265 } mscan_context;
0266 
0267 bool mscan_enable( volatile mscan *m, unsigned bit_rate);
0268 
0269 void mscan_disable( volatile mscan *m);
0270 
0271 void mscan_interrupts_disable( volatile mscan *m);
0272 
0273 bool mscan_set_bit_rate( volatile mscan *m, unsigned bit_rate);
0274 
0275 void mscan_initialization_mode_enter( volatile mscan *m, mscan_context *context);
0276 
0277 void mscan_initialization_mode_leave( volatile mscan *m, const mscan_context *context);
0278 
0279 void mscan_sleep_mode_enter( volatile mscan *m);
0280 
0281 void mscan_sleep_mode_leave( volatile mscan *m);
0282 
0283 volatile uint8_t *mscan_id_acceptance_register( volatile mscan *m, unsigned i);
0284 
0285 volatile uint8_t *mscan_id_mask_register( volatile mscan *m, unsigned i);
0286 
0287 unsigned mscan_filter_number( volatile mscan *m);
0288 
0289 bool mscan_set_filter_number( volatile mscan *m, unsigned number);
0290 
0291 bool mscan_filter_operation( volatile mscan *m, bool set, unsigned index, uint32_t *id, uint32_t *mask);
0292 
0293 void mscan_filter_clear( volatile mscan *m);
0294 
0295 void mscan_get_error_counters( volatile mscan *m, unsigned *rec, unsigned *tec);
0296 
0297 /** @} */
0298 
0299 #endif /* LIBBSP_MSCAN_BASE_H */