summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Hellstrom <daniel@gaisler.com>2019-04-12 11:01:38 +0200
committerDaniel Hellstrom <daniel@gaisler.com>2021-03-07 16:08:23 +0100
commitda7cb87b4df3047795f0674775131b34469d5fa5 (patch)
tree04df0983ed8ea1a1b231fe71a32ac2e85e78cc46
parentgrlib,ambapp: added new IP core IDs (diff)
downloadrtems-da7cb87b4df3047795f0674775131b34469d5fa5.tar.bz2
leon,can: introduce common CAN baud-rate calculation function
Reimplemented the baud-rate algorithm from scratch to cope with GRCAN, GRCANFD and OC_CAN devices. Update #4306.
-rw-r--r--bsps/headers.am1
-rw-r--r--bsps/include/grlib/canbtrs.h94
-rw-r--r--bsps/include/grlib/grcan.h3
-rw-r--r--bsps/include/grlib/occan.h2
-rw-r--r--bsps/shared/grlib-sources.am1
-rw-r--r--bsps/shared/grlib/can/canbtrs.c144
6 files changed, 244 insertions, 1 deletions
diff --git a/bsps/headers.am b/bsps/headers.am
index 5af7e43b4a..2c9b31df2b 100644
--- a/bsps/headers.am
+++ b/bsps/headers.am
@@ -34,6 +34,7 @@ include_grlib_HEADERS += ../../bsps/include/grlib/apbuart_termios.h
include_grlib_HEADERS += ../../bsps/include/grlib/b1553brm.h
include_grlib_HEADERS += ../../bsps/include/grlib/b1553rt.h
include_grlib_HEADERS += ../../bsps/include/grlib/bspcommon.h
+include_grlib_HEADERS += ../../bsps/include/grlib/canbtrs.h
include_grlib_HEADERS += ../../bsps/include/grlib/canmux.h
include_grlib_HEADERS += ../../bsps/include/grlib/cons.h
include_grlib_HEADERS += ../../bsps/include/grlib/debug_defs.h
diff --git a/bsps/include/grlib/canbtrs.h b/bsps/include/grlib/canbtrs.h
new file mode 100644
index 0000000000..7369136931
--- /dev/null
+++ b/bsps/include/grlib/canbtrs.h
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ * @ingroup can
+ * @brief Common CAN baud-rate routines for OCCAN/GRCAN/GRCANFD controllers
+ */
+
+/*
+ * Copyright (C) 2019, 2020 Cobham Gaisler AB
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __GRLIB_CANBTRS_H__
+#define __GRLIB_CANBTRS_H__
+
+/**
+ * @defgroup can CAN
+ *
+ * @ingroup RTEMSBSPsSharedGRLIB
+ *
+ * @brief CAN routines shared between OCCAN, GRCAN and GRCANFD controllers
+ *
+ * @{
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* CAN Baud-rate parameters, range of valid parameter values */
+struct grlib_canbtrs_ranges {
+ unsigned int max_scaler;
+ char has_bpr;
+ unsigned char divfactor;
+ unsigned char min_tseg1;
+ unsigned char max_tseg1;
+ unsigned char min_tseg2;
+ unsigned char max_tseg2;
+};
+
+struct grlib_canbtrs_timing {
+ unsigned char scaler;
+ unsigned char ps1;
+ unsigned char ps2;
+ unsigned char rsj;
+ unsigned char bpr;
+};
+
+/* @brief Calculate CAN baud-rate generation parameters from requested baud-rate
+ *
+ * @param baud The CAN baud rate requested
+ * @param core_hz Input clock [Hz] to CAN core
+ * @param sampl_pt CAN sample point in %, 80 means 80%
+ * @param br CAN Baud-rate parameters limitations
+ * @param[out] timing result is placed here
+ *
+ * @retval 0 Baud-rate parameters sucessfully calculated
+ * @retval negative Failure to generate parameters with less than 5% error
+ * margin from requested baud-rate
+ */
+int grlib_canbtrs_calc_timing(
+ unsigned int baud,
+ unsigned int core_hz,
+ unsigned int sampl_pt,
+ struct grlib_canbtrs_ranges *br,
+ struct grlib_canbtrs_timing *timing
+ );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/bsps/include/grlib/grcan.h b/bsps/include/grlib/grcan.h
index b1bc2ba758..3dc83bedde 100644
--- a/bsps/include/grlib/grcan.h
+++ b/bsps/include/grlib/grcan.h
@@ -1,6 +1,7 @@
/**
* @file
* @ingroup can
+ * @brief Driver API for the GRLIB GRCAN and GRCANFD controllers
*/
/*
@@ -25,6 +26,8 @@
* @{
*/
+#include <stdint.h>
+
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/bsps/include/grlib/occan.h b/bsps/include/grlib/occan.h
index 0bf34dee48..1112a3e8dc 100644
--- a/bsps/include/grlib/occan.h
+++ b/bsps/include/grlib/occan.h
@@ -1,7 +1,7 @@
/**
* @file
* @ingroup can
- * @brief Gaisler wrapper to OpenCores CAN - driver interface
+ * @brief Driver API for GRLIB wrapper to OpenCores CAN
*/
/*
diff --git a/bsps/shared/grlib-sources.am b/bsps/shared/grlib-sources.am
index 512a48c7f7..f2ad1bf827 100644
--- a/bsps/shared/grlib-sources.am
+++ b/bsps/shared/grlib-sources.am
@@ -20,6 +20,7 @@ librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/ascs/grascs.c
librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/btimer/gptimer.c
librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/btimer/tlib.c
librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/btimer/tlib_ckinit.c
+librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/can/canbtrs.c
librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/can/canmux.c
librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/can/grcan.c
librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/grlib/can/occan.c
diff --git a/bsps/shared/grlib/can/canbtrs.c b/bsps/shared/grlib/can/canbtrs.c
new file mode 100644
index 0000000000..307547475c
--- /dev/null
+++ b/bsps/shared/grlib/can/canbtrs.c
@@ -0,0 +1,144 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup can
+ *
+ * @brief Common CAN baud-rate routines for OCCAN/GRCAN/GRCANFD controllers
+ *
+ * Implements common routines for calculating CAN baud-rate parameters from
+ * a user provided baud-rate speed.
+ */
+
+/*
+ * Copyright (C) 2019, 2020 Cobham Gailer AB
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <grlib/canbtrs.h>
+
+/*#define GRLIB_CANBTRS_DEBUG*/
+
+/* Calculate CAN baud-rate generation parameters from requested baud-rate */
+int grlib_canbtrs_calc_timing(
+ unsigned int baud,
+ unsigned int core_hz,
+ unsigned int sampl_pt,
+ struct grlib_canbtrs_ranges *br,
+ struct grlib_canbtrs_timing *timing
+ )
+{
+ int best_error = 2000000000, best_tseg=0, best_scaler=0;
+ int tseg=0, tseg1=0, tseg2=0, sc, tmp, error;
+
+ /* Default to 80% sample point */
+ if ((sampl_pt < 50) || (sampl_pt > 99))
+ sampl_pt = 80;
+
+ /* step though all TSEG1+TSEG2 values possible */
+ for (tseg = (br->min_tseg1 + br->min_tseg2);
+ tseg <= (br->max_tseg1 + br->max_tseg2);
+ tseg++) {
+ /* calculate scaler */
+ tmp = ((br->divfactor + tseg) * baud);
+ sc = (core_hz * 2)/ tmp - core_hz / tmp;
+ if (sc <= 0 || sc > br->max_scaler)
+ continue;
+ if (br->has_bpr &&
+ (((sc > 256 * 1) && (sc <= 256 * 2) && (sc & 0x1)) ||
+ ((sc > 256 * 2) && (sc <= 256 * 4) && (sc & 0x3)) ||
+ ((sc > 256 * 4) && (sc <= 256 * 8) && (sc & 0x7))))
+ continue;
+
+ error = baud - core_hz / (sc * (br->divfactor + tseg));
+#ifdef GRLIB_CANBTRS_DEBUG
+ printf(" baud=%d, tseg=%d, sc=%d, error=%d\n",
+ baud, tseg, sc, error);
+#endif
+ if (error < 0)
+ error = -error;
+
+ /* tseg is increasing, so we accept higher tseg with the same
+ * baudrate to get better sampling point.
+ */
+ if (error <= best_error) {
+ best_error = error;
+ best_tseg = tseg;
+ best_scaler = sc;
+#ifdef GRLIB_CANBTRS_DEBUG
+ printf(" ! best baud=%d\n",
+ core_hz/(sc * (br->divfactor + tseg)));
+#endif
+ }
+ }
+
+ /* return an error if 5% off baud-rate */
+ if (best_error && (baud / best_error <= 5)) {
+ return -2;
+ } else if (!timing) {
+ return 0; /* nothing to store result in, but a valid bitrate can be calculated */
+ }
+
+ tseg2 = (best_tseg + br->divfactor) -
+ ((sampl_pt * (best_tseg + br->divfactor)) / 100);
+ if (tseg2 < br->min_tseg2) {
+ tseg2 = br->min_tseg2;
+ } else if (tseg2 > br->max_tseg2) {
+ tseg2 = br->max_tseg2;
+ }
+
+ tseg1 = best_tseg - tseg2;
+ if (tseg1 > br->max_tseg1) {
+ tseg1 = br->max_tseg1;
+ tseg2 = best_tseg - tseg1;
+ } else if (tseg1 < br->min_tseg1) {
+ tseg1 = br->min_tseg1;
+ tseg2 = best_tseg - tseg1;
+ }
+
+ /* Get scaler and BPR from pseudo SCALER clock */
+ if (best_scaler <= 256) {
+ timing->scaler = best_scaler - 1;
+ timing->bpr = 0;
+ } else if (best_scaler <= 256 * 2) {
+ timing->scaler = ((best_scaler + 1) >> 1) - 1;
+ timing->bpr = 1;
+ } else if (best_scaler <= 256 * 4) {
+ timing->scaler = ((best_scaler + 1) >> 2) - 1;
+ timing->bpr = 2;
+ } else {
+ timing->scaler = ((best_scaler + 1) >> 3) - 1;
+ timing->bpr = 3;
+ }
+
+ timing->ps1 = tseg1;
+ timing->ps2 = tseg2;
+ timing->rsj = 1;
+
+#ifdef GRLIB_CANBTRS_DEBUG
+ printf(" ! result: sc=%d,bpr=%d,ps1=%d,ps2=%d\n", timing->scaler, timing->bpr, timing->ps1, timing->ps2);
+#endif
+
+ return 0;
+}