You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/12/20 22:35:24 UTC
[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #7940: Add SAMA5D2 MCAN support
pkarashchenko commented on code in PR #7940:
URL: https://github.com/apache/nuttx/pull/7940#discussion_r1053811763
##########
arch/arm/src/sama5/sam_mcan.c:
##########
@@ -0,0 +1,4202 @@
+/****************************************************************************
+ * arch/arm/src/sama5/sam_mcan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* References:
+ * SAMA5D2 Series Data Sheet
+ * Atmel sample code
+ * Based on pre-existing SAMV71 NuttX mcan driver
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+#include <assert.h>
+
+#include <arch/board/board.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/can/can.h>
+
+#include "arm_internal.h"
+
+#include "hardware/sam_sfr.h"
+#include "hardware/sam_pinmap.h"
+#include "sam_periphclks.h"
+#include "sam_pio.h"
+#include "sam_mcan.h"
+
+#if defined(CONFIG_CAN) && (defined(CONFIG_SAMA5_MCAN0) || \
+ defined(CONFIG_SAMA5_MCAN1))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Common definitions */
+
+#ifndef MIN
+# define MIN(a,b) ((a < b) ? a : b)
+#endif
+
+#ifndef MAX
+# define MAX(a,b) ((a > b) ? a : b)
Review Comment:
```suggestion
# define MAX(a,b) ((a) > (b) ? (a) : (b))
```
##########
arch/arm/src/sama5/sam_mcan.c:
##########
@@ -0,0 +1,4202 @@
+/****************************************************************************
+ * arch/arm/src/sama5/sam_mcan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* References:
+ * SAMA5D2 Series Data Sheet
+ * Atmel sample code
+ * Based on pre-existing SAMV71 NuttX mcan driver
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+#include <assert.h>
+
+#include <arch/board/board.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/can/can.h>
+
+#include "arm_internal.h"
+
+#include "hardware/sam_sfr.h"
+#include "hardware/sam_pinmap.h"
+#include "sam_periphclks.h"
+#include "sam_pio.h"
+#include "sam_mcan.h"
+
+#if defined(CONFIG_CAN) && (defined(CONFIG_SAMA5_MCAN0) || \
+ defined(CONFIG_SAMA5_MCAN1))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Common definitions */
+
+#ifndef MIN
+# define MIN(a,b) ((a < b) ? a : b)
+#endif
+
+#ifndef MAX
+# define MAX(a,b) ((a > b) ? a : b)
+#endif
+
+/* dedicated RS and TX buffers are not supported */
+
+#define DEDICATED_BUFFERS_NOT_USED
+
+/* Clock source */
+
+/* PCK5 is the programmable clock source, common to all MCAN controllers */
+
+#if defined(CONFIG_SAMA5_MCAN_CLKSRC_SLOW)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_SLOW
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_SLOWCLK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_PLLA)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_PLLA
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_PLLA_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_UPLL)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_UPLL
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_UPLL_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MCK)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MCK
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MCK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MAIN)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MAIN
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MAINOSC_FREQUENCY
+#else
+# error no mcan clock defined!
+#endif
+
+#ifndef CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER
+# define CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER 1
+#endif
+
+#define SAMA5_MCANCLK_FREQUENCY \
+ (SAMA5_MCAN_CLKSRC_FREQUENCY / CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER)
+
+/* Buffer Alignment.
+ *
+ * The MCAN peripheral does not require any data be aligned. However, if
+ * the data cache is enabled then alignment is required. That is because
+ * the data will need to be invalidated and that cache invalidation will
+ * occur in multiples of full change lines.
+ */
+
+#ifdef CONFIG_ARCH_DCACHE
+# define MCAN_ALIGN ARMV7A_DCACHE_LINESIZE
+# define MCAN_ALIGN_MASK (MCAN_ALIGN-1)
+# define MCAN_ALIGN_UP(n) (((n) + MCAN_ALIGN_MASK) & ~MCAN_ALIGN_MASK)
+# define SAM_MCAN_SFR_SHIFT 16
+# define SAM_MCAN0_SFR_MASK 0xffff0000
+# define SAM_MCAN1_SFR_MASK 0x0000ffff
+# define SAM_MCAN_SFR_MSB_MASK 0xffff0000
+# define SAM_MCAN_SFR_GET_MSB(n) (( (uint32_t)(n) & (SAM_MCAN_SFR_MSB_MASK)) >> SAM_MCAN_SFR_SHIFT)
+#else
+# define MCAN_ALIGN (0)
+# define MCAN_ALIGN_MASK (0)
+# define MCAN_ALIGN_UP(n) (n)
+#endif
+
+/* General Configuration */
+
+#ifndef CONFIG_CAN_TXREADY
+# warning WARNING!!! CONFIG_CAN_TXREADY is required by this driver
+#endif
+
+/* MCAN0 Configuration */
+
+#ifdef CONFIG_SAMA5_MCAN0
+
+/* Bit timing */
+
+# define MCAN0_TSEG1 (CONFIG_SAMA5_MCAN0_PROPSEG + CONFIG_SAMA5_MCAN0_PHASESEG1 - 1)
+# define MCAN0_TSEG2 (CONFIG_SAMA5_MCAN0_PHASESEG2 - 1)
+# define MCAN0_BRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
+ ((float)(MCAN0_TSEG1 + MCAN0_TSEG2 + 3) * \
+ (float)CONFIG_SAMA5_MCAN0_BITRATE)) - 1))
+
+# define MCAN0_SJW (CONFIG_SAMA5_MCAN0_FSJW - 1)
+
+/* NB: errata sheet states TSEG1 must not be programmed as 0 */
+
+# if ((MCAN0_TSEG1 > 256) || (MCAN0_TSEG1 < 2))
+# error Invalid MCAN0 TSEG1
+# endif
+# if MCAN0_TSEG2 > 128
+# error Invalid MCAN0 TSEG2
+# endif
+# if MCAN0_SJW > 128
+# error Invalid MCAN0 SJW
+# endif
+
+# define MCAN0_FTSEG1 (CONFIG_SAMA5_MCAN0_FPROPSEG + CONFIG_SAMA5_MCAN0_FPHASESEG1 - 1)
+# define MCAN0_FTSEG2 (CONFIG_SAMA5_MCAN0_FPHASESEG2 - 1)
+# define MCAN0_FBRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
Review Comment:
```suggestion
# define MCAN0_FBRP ((uint32_t)(((float)SAMA5_MCANCLK_FREQUENCY / \
```
##########
arch/arm/src/sama5/sam_mcan.c:
##########
@@ -0,0 +1,4202 @@
+/****************************************************************************
+ * arch/arm/src/sama5/sam_mcan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* References:
+ * SAMA5D2 Series Data Sheet
+ * Atmel sample code
+ * Based on pre-existing SAMV71 NuttX mcan driver
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+#include <assert.h>
+
+#include <arch/board/board.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/can/can.h>
+
+#include "arm_internal.h"
+
+#include "hardware/sam_sfr.h"
+#include "hardware/sam_pinmap.h"
+#include "sam_periphclks.h"
+#include "sam_pio.h"
+#include "sam_mcan.h"
+
+#if defined(CONFIG_CAN) && (defined(CONFIG_SAMA5_MCAN0) || \
+ defined(CONFIG_SAMA5_MCAN1))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Common definitions */
+
+#ifndef MIN
+# define MIN(a,b) ((a < b) ? a : b)
Review Comment:
```suggestion
# define MIN(a,b) ((a) < (b) ? (a) : (b))
```
##########
arch/arm/src/sama5/sam_mcan.c:
##########
@@ -0,0 +1,4202 @@
+/****************************************************************************
+ * arch/arm/src/sama5/sam_mcan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* References:
+ * SAMA5D2 Series Data Sheet
+ * Atmel sample code
+ * Based on pre-existing SAMV71 NuttX mcan driver
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+#include <assert.h>
+
+#include <arch/board/board.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/can/can.h>
+
+#include "arm_internal.h"
+
+#include "hardware/sam_sfr.h"
+#include "hardware/sam_pinmap.h"
+#include "sam_periphclks.h"
+#include "sam_pio.h"
+#include "sam_mcan.h"
+
+#if defined(CONFIG_CAN) && (defined(CONFIG_SAMA5_MCAN0) || \
+ defined(CONFIG_SAMA5_MCAN1))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Common definitions */
+
+#ifndef MIN
+# define MIN(a,b) ((a < b) ? a : b)
+#endif
+
+#ifndef MAX
+# define MAX(a,b) ((a > b) ? a : b)
+#endif
+
+/* dedicated RS and TX buffers are not supported */
+
+#define DEDICATED_BUFFERS_NOT_USED
+
+/* Clock source */
+
+/* PCK5 is the programmable clock source, common to all MCAN controllers */
+
+#if defined(CONFIG_SAMA5_MCAN_CLKSRC_SLOW)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_SLOW
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_SLOWCLK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_PLLA)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_PLLA
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_PLLA_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_UPLL)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_UPLL
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_UPLL_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MCK)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MCK
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MCK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MAIN)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MAIN
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MAINOSC_FREQUENCY
+#else
+# error no mcan clock defined!
+#endif
+
+#ifndef CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER
+# define CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER 1
+#endif
+
+#define SAMA5_MCANCLK_FREQUENCY \
+ (SAMA5_MCAN_CLKSRC_FREQUENCY / CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER)
+
+/* Buffer Alignment.
+ *
+ * The MCAN peripheral does not require any data be aligned. However, if
+ * the data cache is enabled then alignment is required. That is because
+ * the data will need to be invalidated and that cache invalidation will
+ * occur in multiples of full change lines.
+ */
+
+#ifdef CONFIG_ARCH_DCACHE
+# define MCAN_ALIGN ARMV7A_DCACHE_LINESIZE
+# define MCAN_ALIGN_MASK (MCAN_ALIGN-1)
+# define MCAN_ALIGN_UP(n) (((n) + MCAN_ALIGN_MASK) & ~MCAN_ALIGN_MASK)
+# define SAM_MCAN_SFR_SHIFT 16
+# define SAM_MCAN0_SFR_MASK 0xffff0000
+# define SAM_MCAN1_SFR_MASK 0x0000ffff
+# define SAM_MCAN_SFR_MSB_MASK 0xffff0000
+# define SAM_MCAN_SFR_GET_MSB(n) (( (uint32_t)(n) & (SAM_MCAN_SFR_MSB_MASK)) >> SAM_MCAN_SFR_SHIFT)
+#else
+# define MCAN_ALIGN (0)
+# define MCAN_ALIGN_MASK (0)
+# define MCAN_ALIGN_UP(n) (n)
+#endif
+
+/* General Configuration */
+
+#ifndef CONFIG_CAN_TXREADY
+# warning WARNING!!! CONFIG_CAN_TXREADY is required by this driver
+#endif
+
+/* MCAN0 Configuration */
+
+#ifdef CONFIG_SAMA5_MCAN0
+
+/* Bit timing */
+
+# define MCAN0_TSEG1 (CONFIG_SAMA5_MCAN0_PROPSEG + CONFIG_SAMA5_MCAN0_PHASESEG1 - 1)
+# define MCAN0_TSEG2 (CONFIG_SAMA5_MCAN0_PHASESEG2 - 1)
+# define MCAN0_BRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
Review Comment:
```suggestion
# define MCAN0_BRP ((uint32_t)(((float)SAMA5_MCANCLK_FREQUENCY / \
```
##########
arch/arm/src/sama5/sam_mcan.c:
##########
@@ -0,0 +1,4202 @@
+/****************************************************************************
+ * arch/arm/src/sama5/sam_mcan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* References:
+ * SAMA5D2 Series Data Sheet
+ * Atmel sample code
+ * Based on pre-existing SAMV71 NuttX mcan driver
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+#include <assert.h>
+
+#include <arch/board/board.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/can/can.h>
+
+#include "arm_internal.h"
+
+#include "hardware/sam_sfr.h"
+#include "hardware/sam_pinmap.h"
+#include "sam_periphclks.h"
+#include "sam_pio.h"
+#include "sam_mcan.h"
+
+#if defined(CONFIG_CAN) && (defined(CONFIG_SAMA5_MCAN0) || \
+ defined(CONFIG_SAMA5_MCAN1))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Common definitions */
+
+#ifndef MIN
+# define MIN(a,b) ((a < b) ? a : b)
+#endif
+
+#ifndef MAX
+# define MAX(a,b) ((a > b) ? a : b)
+#endif
+
+/* dedicated RS and TX buffers are not supported */
+
+#define DEDICATED_BUFFERS_NOT_USED
+
+/* Clock source */
+
+/* PCK5 is the programmable clock source, common to all MCAN controllers */
+
+#if defined(CONFIG_SAMA5_MCAN_CLKSRC_SLOW)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_SLOW
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_SLOWCLK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_PLLA)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_PLLA
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_PLLA_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_UPLL)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_UPLL
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_UPLL_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MCK)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MCK
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MCK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MAIN)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MAIN
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MAINOSC_FREQUENCY
+#else
+# error no mcan clock defined!
+#endif
+
+#ifndef CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER
+# define CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER 1
+#endif
+
+#define SAMA5_MCANCLK_FREQUENCY \
+ (SAMA5_MCAN_CLKSRC_FREQUENCY / CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER)
+
+/* Buffer Alignment.
+ *
+ * The MCAN peripheral does not require any data be aligned. However, if
+ * the data cache is enabled then alignment is required. That is because
+ * the data will need to be invalidated and that cache invalidation will
+ * occur in multiples of full change lines.
+ */
+
+#ifdef CONFIG_ARCH_DCACHE
+# define MCAN_ALIGN ARMV7A_DCACHE_LINESIZE
+# define MCAN_ALIGN_MASK (MCAN_ALIGN-1)
+# define MCAN_ALIGN_UP(n) (((n) + MCAN_ALIGN_MASK) & ~MCAN_ALIGN_MASK)
+# define SAM_MCAN_SFR_SHIFT 16
+# define SAM_MCAN0_SFR_MASK 0xffff0000
+# define SAM_MCAN1_SFR_MASK 0x0000ffff
+# define SAM_MCAN_SFR_MSB_MASK 0xffff0000
+# define SAM_MCAN_SFR_GET_MSB(n) (( (uint32_t)(n) & (SAM_MCAN_SFR_MSB_MASK)) >> SAM_MCAN_SFR_SHIFT)
+#else
+# define MCAN_ALIGN (0)
+# define MCAN_ALIGN_MASK (0)
+# define MCAN_ALIGN_UP(n) (n)
+#endif
+
+/* General Configuration */
+
+#ifndef CONFIG_CAN_TXREADY
+# warning WARNING!!! CONFIG_CAN_TXREADY is required by this driver
+#endif
+
+/* MCAN0 Configuration */
+
+#ifdef CONFIG_SAMA5_MCAN0
+
+/* Bit timing */
+
+# define MCAN0_TSEG1 (CONFIG_SAMA5_MCAN0_PROPSEG + CONFIG_SAMA5_MCAN0_PHASESEG1 - 1)
+# define MCAN0_TSEG2 (CONFIG_SAMA5_MCAN0_PHASESEG2 - 1)
+# define MCAN0_BRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
+ ((float)(MCAN0_TSEG1 + MCAN0_TSEG2 + 3) * \
+ (float)CONFIG_SAMA5_MCAN0_BITRATE)) - 1))
+
+# define MCAN0_SJW (CONFIG_SAMA5_MCAN0_FSJW - 1)
+
+/* NB: errata sheet states TSEG1 must not be programmed as 0 */
+
+# if ((MCAN0_TSEG1 > 256) || (MCAN0_TSEG1 < 2))
+# error Invalid MCAN0 TSEG1
+# endif
+# if MCAN0_TSEG2 > 128
+# error Invalid MCAN0 TSEG2
+# endif
+# if MCAN0_SJW > 128
+# error Invalid MCAN0 SJW
+# endif
+
+# define MCAN0_FTSEG1 (CONFIG_SAMA5_MCAN0_FPROPSEG + CONFIG_SAMA5_MCAN0_FPHASESEG1 - 1)
+# define MCAN0_FTSEG2 (CONFIG_SAMA5_MCAN0_FPHASESEG2 - 1)
+# define MCAN0_FBRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
+ ((float)(MCAN0_FTSEG1 + MCAN0_FTSEG2 + 3) * \
+ (float)CONFIG_SAMA5_MCAN0_FBITRATE)) - 1))
+# define MCAN0_FSJW (CONFIG_SAMA5_MCAN0_FFSJW - 1)
+
+/* NB: errata sheet states TSEG1 must not be programmed as 0 */
+
+# if ((MCAN0_FTSEG1 > 32) || (MCAN0_FTSEG1 < 2))
+# error Invalid MCAN0 FTSEG1
+# endif
+# if MCAN0_FTSEG2 > 17
+# error Invalid MCAN0 FTSEG2
+# endif
+# if MCAN0_FSJW > 8
+# error Invalid MCAN0 FSJW
+# endif
+
+/* MCAN0 RX FIFO0 element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_RXFIFO0_8BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 8
+# define MCAN0_RXFIFO0_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_12BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 12
+# define MCAN0_RXFIFO0_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_16BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 16
+# define MCAN0_RXFIFO0_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_20BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 20
+# define MCAN0_RXFIFO0_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_24BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 24
+# define MCAN0_RXFIFO0_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_32BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 32
+# define MCAN0_RXFIFO0_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_48BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 48
+# define MCAN0_RXFIFO0_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_64BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 64
+# define MCAN0_RXFIFO0_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 RX FIFO0 element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE
+# define CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE > 64
+# error Invalid MCAN0 number of RX FIFO0 elements
+# endif
+
+# define MCAN0_RXFIFO0_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE * \
+ MCAN0_RXFIFO0_ELEMENT_SIZE + 8)
+# define MCAN0_RXFIFO0_WORDS (MCAN0_RXFIFO0_BYTES >> 2)
+
+/* MCAN0 RX FIFO1 element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_RXFIFO1_8BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 8
+# define MCAN0_RXFIFO1_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_12BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 12
+# define MCAN0_RXFIFO1_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_16BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 16
+# define MCAN0_RXFIFO1_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_20BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 20
+# define MCAN0_RXFIFO1_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_24BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 24
+# define MCAN0_RXFIFO1_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_32BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 32
+# define MCAN0_RXFIFO1_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_48BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 48
+# define MCAN0_RXFIFO1_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_64BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 64
+# define MCAN0_RXFIFO1_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 RX FIFO1 element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE
+# define CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE > 64
+# error Invalid MCAN0 number of RX FIFO1 elements
+# endif
+
+# define MCAN0_RXFIFO1_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE * \
+ MCAN0_RXFIFO1_ELEMENT_SIZE + 8)
+# define MCAN0_RXFIFO1_WORDS (MCAN0_RXFIFO1_BYTES >> 2)
+
+/* MCAN0 Filters */
+
+# ifndef CONFIG_SAMA5_MCAN0_NSTDFILTERS
+# define CONFIG_SAMA5_MCAN0_NSTDFILTERS 0
+# endif
+
+# if (CONFIG_SAMA5_MCAN0_NSTDFILTERS > 128)
+# error Invalid MCAN0 number of Standard Filters
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_NEXTFILTERS
+# define CONFIG_SAMA5_MCAN0_NEXTFILTERS 0
+# endif
+
+# if (CONFIG_SAMA5_MCAN0_NEXTFILTERS > 64)
+# error Invalid MCAN0 number of Extended Filters
+# endif
+
+# define MCAN0_STDFILTER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_NSTDFILTERS << 2)
+# define MCAN0_STDFILTER_WORDS (MCAN0_STDFILTER_BYTES >> 2)
+
+# define MCAN0_EXTFILTER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_NEXTFILTERS << 3)
+# define MCAN0_EXTFILTER_WORDS (MCAN0_EXTFILTER_BYTES >> 2)
+
+/* MCAN0 RX buffer element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_RXBUFFER_8BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 8
+# define MCAN0_RXBUFFER_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_12BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 12
+# define MCAN0_RXBUFFER_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_16BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 16
+# define MCAN0_RXBUFFER_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_20BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 20
+# define MCAN0_RXBUFFER_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_24BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 24
+# define MCAN0_RXBUFFER_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_32BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 32
+# define MCAN0_RXBUFFER_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_48BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 48
+# define MCAN0_RXBUFFER_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_64BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 64
+# define MCAN0_RXBUFFER_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 RX buffer element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE
+# define CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE > 64
+# error Invalid MCAN0 number of RX BUFFER elements
+# endif
+
+# define MCAN0_DEDICATED_RXBUFFER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE * \
+ MCAN0_RXBUFFER_ELEMENT_SIZE + 8)
+# define MCAN0_DEDICATED_RXBUFFER_WORDS \
+ (MCAN0_DEDICATED_RXBUFFER_BYTES >> 2)
+
+/* MCAN0 TX buffer element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_TXBUFFER_8BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 8
+# define MCAN0_TXBUFFER_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_12BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 12
+# define MCAN0_TXBUFFER_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_16BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 16
+# define MCAN0_TXBUFFER_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_20BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 20
+# define MCAN0_TXBUFFER_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_24BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 24
+# define MCAN0_TXBUFFER_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_32BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 32
+# define MCAN0_TXBUFFER_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_48BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 48
+# define MCAN0_TXBUFFER_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_64BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 64
+# define MCAN0_TXBUFFER_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 TX buffer element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE
+# define CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE 0
+# endif
+
+# define MCAN0_DEDICATED_TXBUFFER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE * \
+ MCAN0_TXBUFFER_ELEMENT_SIZE + 8)
+# define MCAN0_DEDICATED_TXBUFFER_WORDS \
+ (MCAN0_DEDICATED_TXBUFFER_BYTES >> 2)
+
+/* MCAN0 TX FIFOs */
+
+# ifndef CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE
+# define CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE 0
+# endif
+
+# if (CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE + \
+ CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE) > 32
+# error Invalid MCAN0 number of TX BUFFER elements
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE
+# define CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE > 32
+# error Invalid MCAN0 number of TX EVENT FIFO elements
+# endif
+
+# define MCAN0_TXEVENTFIFO_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE << 3)
+# define MCAN0_TXEVENTFIFO_WORDS \
+ (MCAN0_TXEVENTFIFO_BYTES >> 2)
+
+# define MCAN0_TXFIFIOQ_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE * \
+ MCAN0_TXBUFFER_ELEMENT_SIZE + 8)
+# define MCAN0_TXFIFIOQ_WORDS (MCAN0_TXFIFIOQ_BYTES >> 2)
+
+/* MCAN0 Message RAM */
+
+# define MCAN0_STDFILTER_INDEX 0
+# define MCAN0_EXTFILTERS_INDEX (MCAN0_STDFILTER_INDEX + MCAN0_STDFILTER_WORDS)
+# define MCAN0_RXFIFO0_INDEX (MCAN0_EXTFILTERS_INDEX + MCAN0_EXTFILTER_WORDS)
+# define MCAN0_RXFIFO1_INDEX (MCAN0_RXFIFO0_INDEX + MCAN0_RXFIFO0_WORDS)
+# define MCAN0_RXDEDICATED_INDEX (MCAN0_RXFIFO1_INDEX + MCAN0_RXFIFO1_WORDS)
+# define MCAN0_TXEVENTFIFO_INDEX (MCAN0_RXDEDICATED_INDEX + MCAN0_DEDICATED_RXBUFFER_WORDS)
+# define MCAN0_TXDEDICATED_INDEX (MCAN0_TXEVENTFIFO_INDEX + MCAN0_TXEVENTFIFO_WORDS)
+# define MCAN0_TXFIFOQ_INDEX (MCAN0_TXDEDICATED_INDEX + MCAN0_DEDICATED_TXBUFFER_WORDS)
+# define MCAN0_MSGRAM_WORDS (MCAN0_TXFIFOQ_INDEX + MCAN0_TXFIFIOQ_WORDS)
+
+#endif /* CONFIG_SAMA5_MCAN0 */
+
+/* Loopback mode */
+
+#undef SAMA5_MCAN_LOOPBACK
+#if defined(CONFIG_SAMA5_MCAN0_LOOPBACK) || defined(CONFIG_SAMA5_MCAN1_LOOPBACK)
+# define SAMA5_MCAN_LOOPBACK 1
+#endif
+
+/* MCAN1 Configuration */
+
+#ifdef CONFIG_SAMA5_MCAN1
+ /* Bit timing */
+
+# define MCAN1_TSEG1 (CONFIG_SAMA5_MCAN1_PROPSEG + CONFIG_SAMA5_MCAN1_PHASESEG1)
+# define MCAN1_TSEG2 CONFIG_SAMA5_MCAN1_PHASESEG2
+# define MCAN1_BRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
Review Comment:
```suggestion
# define MCAN1_BRP ((uint32_t)(((float)SAMA5_MCANCLK_FREQUENCY / \
```
##########
arch/arm/src/sama5/sam_mcan.c:
##########
@@ -0,0 +1,4202 @@
+/****************************************************************************
+ * arch/arm/src/sama5/sam_mcan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* References:
+ * SAMA5D2 Series Data Sheet
+ * Atmel sample code
+ * Based on pre-existing SAMV71 NuttX mcan driver
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+#include <assert.h>
+
+#include <arch/board/board.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/can/can.h>
+
+#include "arm_internal.h"
+
+#include "hardware/sam_sfr.h"
+#include "hardware/sam_pinmap.h"
+#include "sam_periphclks.h"
+#include "sam_pio.h"
+#include "sam_mcan.h"
+
+#if defined(CONFIG_CAN) && (defined(CONFIG_SAMA5_MCAN0) || \
+ defined(CONFIG_SAMA5_MCAN1))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Common definitions */
+
+#ifndef MIN
+# define MIN(a,b) ((a < b) ? a : b)
+#endif
+
+#ifndef MAX
+# define MAX(a,b) ((a > b) ? a : b)
+#endif
+
+/* dedicated RS and TX buffers are not supported */
+
+#define DEDICATED_BUFFERS_NOT_USED
+
+/* Clock source */
+
+/* PCK5 is the programmable clock source, common to all MCAN controllers */
+
+#if defined(CONFIG_SAMA5_MCAN_CLKSRC_SLOW)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_SLOW
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_SLOWCLK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_PLLA)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_PLLA
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_PLLA_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_UPLL)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_UPLL
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_UPLL_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MCK)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MCK
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MCK_FREQUENCY
+#elif defined(CONFIG_SAMA5_MCAN_CLKSRC_MAIN)
+# define SAMA5_MCAN_CLKSRC PMC_PCR_GCKCSS_MAIN
+# define SAMA5_MCAN_CLKSRC_FREQUENCY BOARD_MAINOSC_FREQUENCY
+#else
+# error no mcan clock defined!
+#endif
+
+#ifndef CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER
+# define CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER 1
+#endif
+
+#define SAMA5_MCANCLK_FREQUENCY \
+ (SAMA5_MCAN_CLKSRC_FREQUENCY / CONFIG_SAMA5_MCAN_CLKSRC_PRESCALER)
+
+/* Buffer Alignment.
+ *
+ * The MCAN peripheral does not require any data be aligned. However, if
+ * the data cache is enabled then alignment is required. That is because
+ * the data will need to be invalidated and that cache invalidation will
+ * occur in multiples of full change lines.
+ */
+
+#ifdef CONFIG_ARCH_DCACHE
+# define MCAN_ALIGN ARMV7A_DCACHE_LINESIZE
+# define MCAN_ALIGN_MASK (MCAN_ALIGN-1)
+# define MCAN_ALIGN_UP(n) (((n) + MCAN_ALIGN_MASK) & ~MCAN_ALIGN_MASK)
+# define SAM_MCAN_SFR_SHIFT 16
+# define SAM_MCAN0_SFR_MASK 0xffff0000
+# define SAM_MCAN1_SFR_MASK 0x0000ffff
+# define SAM_MCAN_SFR_MSB_MASK 0xffff0000
+# define SAM_MCAN_SFR_GET_MSB(n) (( (uint32_t)(n) & (SAM_MCAN_SFR_MSB_MASK)) >> SAM_MCAN_SFR_SHIFT)
+#else
+# define MCAN_ALIGN (0)
+# define MCAN_ALIGN_MASK (0)
+# define MCAN_ALIGN_UP(n) (n)
+#endif
+
+/* General Configuration */
+
+#ifndef CONFIG_CAN_TXREADY
+# warning WARNING!!! CONFIG_CAN_TXREADY is required by this driver
+#endif
+
+/* MCAN0 Configuration */
+
+#ifdef CONFIG_SAMA5_MCAN0
+
+/* Bit timing */
+
+# define MCAN0_TSEG1 (CONFIG_SAMA5_MCAN0_PROPSEG + CONFIG_SAMA5_MCAN0_PHASESEG1 - 1)
+# define MCAN0_TSEG2 (CONFIG_SAMA5_MCAN0_PHASESEG2 - 1)
+# define MCAN0_BRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
+ ((float)(MCAN0_TSEG1 + MCAN0_TSEG2 + 3) * \
+ (float)CONFIG_SAMA5_MCAN0_BITRATE)) - 1))
+
+# define MCAN0_SJW (CONFIG_SAMA5_MCAN0_FSJW - 1)
+
+/* NB: errata sheet states TSEG1 must not be programmed as 0 */
+
+# if ((MCAN0_TSEG1 > 256) || (MCAN0_TSEG1 < 2))
+# error Invalid MCAN0 TSEG1
+# endif
+# if MCAN0_TSEG2 > 128
+# error Invalid MCAN0 TSEG2
+# endif
+# if MCAN0_SJW > 128
+# error Invalid MCAN0 SJW
+# endif
+
+# define MCAN0_FTSEG1 (CONFIG_SAMA5_MCAN0_FPROPSEG + CONFIG_SAMA5_MCAN0_FPHASESEG1 - 1)
+# define MCAN0_FTSEG2 (CONFIG_SAMA5_MCAN0_FPHASESEG2 - 1)
+# define MCAN0_FBRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
+ ((float)(MCAN0_FTSEG1 + MCAN0_FTSEG2 + 3) * \
+ (float)CONFIG_SAMA5_MCAN0_FBITRATE)) - 1))
+# define MCAN0_FSJW (CONFIG_SAMA5_MCAN0_FFSJW - 1)
+
+/* NB: errata sheet states TSEG1 must not be programmed as 0 */
+
+# if ((MCAN0_FTSEG1 > 32) || (MCAN0_FTSEG1 < 2))
+# error Invalid MCAN0 FTSEG1
+# endif
+# if MCAN0_FTSEG2 > 17
+# error Invalid MCAN0 FTSEG2
+# endif
+# if MCAN0_FSJW > 8
+# error Invalid MCAN0 FSJW
+# endif
+
+/* MCAN0 RX FIFO0 element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_RXFIFO0_8BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 8
+# define MCAN0_RXFIFO0_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_12BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 12
+# define MCAN0_RXFIFO0_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_16BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 16
+# define MCAN0_RXFIFO0_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_20BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 20
+# define MCAN0_RXFIFO0_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_24BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 24
+# define MCAN0_RXFIFO0_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_32BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 32
+# define MCAN0_RXFIFO0_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_48BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 48
+# define MCAN0_RXFIFO0_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO0_64BYTES)
+# define MCAN0_RXFIFO0_ELEMENT_SIZE 64
+# define MCAN0_RXFIFO0_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 RX FIFO0 element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE
+# define CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE > 64
+# error Invalid MCAN0 number of RX FIFO0 elements
+# endif
+
+# define MCAN0_RXFIFO0_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_RXFIFO0_SIZE * \
+ MCAN0_RXFIFO0_ELEMENT_SIZE + 8)
+# define MCAN0_RXFIFO0_WORDS (MCAN0_RXFIFO0_BYTES >> 2)
+
+/* MCAN0 RX FIFO1 element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_RXFIFO1_8BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 8
+# define MCAN0_RXFIFO1_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_12BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 12
+# define MCAN0_RXFIFO1_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_16BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 16
+# define MCAN0_RXFIFO1_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_20BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 20
+# define MCAN0_RXFIFO1_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_24BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 24
+# define MCAN0_RXFIFO1_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_32BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 32
+# define MCAN0_RXFIFO1_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_48BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 48
+# define MCAN0_RXFIFO1_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_RXFIFO1_64BYTES)
+# define MCAN0_RXFIFO1_ELEMENT_SIZE 64
+# define MCAN0_RXFIFO1_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 RX FIFO1 element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE
+# define CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE > 64
+# error Invalid MCAN0 number of RX FIFO1 elements
+# endif
+
+# define MCAN0_RXFIFO1_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_RXFIFO1_SIZE * \
+ MCAN0_RXFIFO1_ELEMENT_SIZE + 8)
+# define MCAN0_RXFIFO1_WORDS (MCAN0_RXFIFO1_BYTES >> 2)
+
+/* MCAN0 Filters */
+
+# ifndef CONFIG_SAMA5_MCAN0_NSTDFILTERS
+# define CONFIG_SAMA5_MCAN0_NSTDFILTERS 0
+# endif
+
+# if (CONFIG_SAMA5_MCAN0_NSTDFILTERS > 128)
+# error Invalid MCAN0 number of Standard Filters
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_NEXTFILTERS
+# define CONFIG_SAMA5_MCAN0_NEXTFILTERS 0
+# endif
+
+# if (CONFIG_SAMA5_MCAN0_NEXTFILTERS > 64)
+# error Invalid MCAN0 number of Extended Filters
+# endif
+
+# define MCAN0_STDFILTER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_NSTDFILTERS << 2)
+# define MCAN0_STDFILTER_WORDS (MCAN0_STDFILTER_BYTES >> 2)
+
+# define MCAN0_EXTFILTER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_NEXTFILTERS << 3)
+# define MCAN0_EXTFILTER_WORDS (MCAN0_EXTFILTER_BYTES >> 2)
+
+/* MCAN0 RX buffer element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_RXBUFFER_8BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 8
+# define MCAN0_RXBUFFER_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_12BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 12
+# define MCAN0_RXBUFFER_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_16BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 16
+# define MCAN0_RXBUFFER_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_20BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 20
+# define MCAN0_RXBUFFER_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_24BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 24
+# define MCAN0_RXBUFFER_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_32BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 32
+# define MCAN0_RXBUFFER_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_48BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 48
+# define MCAN0_RXBUFFER_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_RXBUFFER_64BYTES)
+# define MCAN0_RXBUFFER_ELEMENT_SIZE 64
+# define MCAN0_RXBUFFER_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 RX buffer element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE
+# define CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE > 64
+# error Invalid MCAN0 number of RX BUFFER elements
+# endif
+
+# define MCAN0_DEDICATED_RXBUFFER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_DEDICATED_RXBUFFER_SIZE * \
+ MCAN0_RXBUFFER_ELEMENT_SIZE + 8)
+# define MCAN0_DEDICATED_RXBUFFER_WORDS \
+ (MCAN0_DEDICATED_RXBUFFER_BYTES >> 2)
+
+/* MCAN0 TX buffer element size */
+
+# if defined(CONFIG_SAMA5_MCAN0_TXBUFFER_8BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 8
+# define MCAN0_TXBUFFER_ENCODED_SIZE 0
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_12BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 12
+# define MCAN0_TXBUFFER_ENCODED_SIZE 1
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_16BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 16
+# define MCAN0_TXBUFFER_ENCODED_SIZE 2
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_20BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 20
+# define MCAN0_TXBUFFER_ENCODED_SIZE 3
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_24BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 24
+# define MCAN0_TXBUFFER_ENCODED_SIZE 4
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_32BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 32
+# define MCAN0_TXBUFFER_ENCODED_SIZE 5
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_48BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 48
+# define MCAN0_TXBUFFER_ENCODED_SIZE 6
+# elif defined(CONFIG_SAMA5_MCAN0_TXBUFFER_64BYTES)
+# define MCAN0_TXBUFFER_ELEMENT_SIZE 64
+# define MCAN0_TXBUFFER_ENCODED_SIZE 7
+# else
+# error Undefined MCAN0 TX buffer element size
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE
+# define CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE 0
+# endif
+
+# define MCAN0_DEDICATED_TXBUFFER_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE * \
+ MCAN0_TXBUFFER_ELEMENT_SIZE + 8)
+# define MCAN0_DEDICATED_TXBUFFER_WORDS \
+ (MCAN0_DEDICATED_TXBUFFER_BYTES >> 2)
+
+/* MCAN0 TX FIFOs */
+
+# ifndef CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE
+# define CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE 0
+# endif
+
+# if (CONFIG_SAMA5_MCAN0_DEDICATED_TXBUFFER_SIZE + \
+ CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE) > 32
+# error Invalid MCAN0 number of TX BUFFER elements
+# endif
+
+# ifndef CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE
+# define CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE 0
+# endif
+
+# if CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE > 32
+# error Invalid MCAN0 number of TX EVENT FIFO elements
+# endif
+
+# define MCAN0_TXEVENTFIFO_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_TXEVENTFIFO_SIZE << 3)
+# define MCAN0_TXEVENTFIFO_WORDS \
+ (MCAN0_TXEVENTFIFO_BYTES >> 2)
+
+# define MCAN0_TXFIFIOQ_BYTES \
+ MCAN_ALIGN_UP(CONFIG_SAMA5_MCAN0_TXFIFOQ_SIZE * \
+ MCAN0_TXBUFFER_ELEMENT_SIZE + 8)
+# define MCAN0_TXFIFIOQ_WORDS (MCAN0_TXFIFIOQ_BYTES >> 2)
+
+/* MCAN0 Message RAM */
+
+# define MCAN0_STDFILTER_INDEX 0
+# define MCAN0_EXTFILTERS_INDEX (MCAN0_STDFILTER_INDEX + MCAN0_STDFILTER_WORDS)
+# define MCAN0_RXFIFO0_INDEX (MCAN0_EXTFILTERS_INDEX + MCAN0_EXTFILTER_WORDS)
+# define MCAN0_RXFIFO1_INDEX (MCAN0_RXFIFO0_INDEX + MCAN0_RXFIFO0_WORDS)
+# define MCAN0_RXDEDICATED_INDEX (MCAN0_RXFIFO1_INDEX + MCAN0_RXFIFO1_WORDS)
+# define MCAN0_TXEVENTFIFO_INDEX (MCAN0_RXDEDICATED_INDEX + MCAN0_DEDICATED_RXBUFFER_WORDS)
+# define MCAN0_TXDEDICATED_INDEX (MCAN0_TXEVENTFIFO_INDEX + MCAN0_TXEVENTFIFO_WORDS)
+# define MCAN0_TXFIFOQ_INDEX (MCAN0_TXDEDICATED_INDEX + MCAN0_DEDICATED_TXBUFFER_WORDS)
+# define MCAN0_MSGRAM_WORDS (MCAN0_TXFIFOQ_INDEX + MCAN0_TXFIFIOQ_WORDS)
+
+#endif /* CONFIG_SAMA5_MCAN0 */
+
+/* Loopback mode */
+
+#undef SAMA5_MCAN_LOOPBACK
+#if defined(CONFIG_SAMA5_MCAN0_LOOPBACK) || defined(CONFIG_SAMA5_MCAN1_LOOPBACK)
+# define SAMA5_MCAN_LOOPBACK 1
+#endif
+
+/* MCAN1 Configuration */
+
+#ifdef CONFIG_SAMA5_MCAN1
+ /* Bit timing */
+
+# define MCAN1_TSEG1 (CONFIG_SAMA5_MCAN1_PROPSEG + CONFIG_SAMA5_MCAN1_PHASESEG1)
+# define MCAN1_TSEG2 CONFIG_SAMA5_MCAN1_PHASESEG2
+# define MCAN1_BRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
+ ((float)(MCAN1_TSEG1 + MCAN1_TSEG2 + 3) * \
+ (float)CONFIG_SAMA5_MCAN1_BITRATE)) - 1))
+# define MCAN1_SJW (CONFIG_SAMA5_MCAN1_FSJW - 1)
+
+# if MCAN1_TSEG1 > 63
+# error Invalid MCAN1 TSEG1
+# endif
+# if MCAN1_TSEG2 > 15
+# error Invalid MCAN1 TSEG2
+# endif
+# if MCAN1_SJW > 15
+# error Invalid MCAN1 SJW
+# endif
+
+# define MCAN1_FTSEG1 (CONFIG_SAMA5_MCAN1_FPROPSEG + CONFIG_SAMA5_MCAN1_FPHASESEG1)
+# define MCAN1_FTSEG2 (CONFIG_SAMA5_MCAN1_FPHASESEG2)
+# define MCAN1_FBRP ((uint32_t)(((float) SAMA5_MCANCLK_FREQUENCY / \
Review Comment:
```suggestion
# define MCAN1_FBRP ((uint32_t)(((float)SAMA5_MCANCLK_FREQUENCY / \
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org