STM32 CANFD波特率计算器

C代码定义:

typedef struct _DataRate
{
    uint32_t DataRate;
    uint32_t DataPrescaler; /*!< Specifies the value by which the oscillator frequency is
                                              divided for generating the data bit time quanta.
                                              This parameter must be a number between 1 and 32             */

    uint32_t DataSyncJumpWidth; /*!< Specifies the maximum number of time quanta the FDCAN
                                              hardware is allowed to lengthen or shorten a data bit to
                                              perform resynchronization.
                                              This parameter must be a number between 1 and 16             */

    uint32_t DataTimeSeg1; /*!< Specifies the number of time quanta in Data Bit Segment 1.
                                              This parameter must be a number between 1 and 32             */

    uint32_t DataTimeSeg2; /*!< Specifies the number of time quanta in Data Bit Segment 2.
                                              This parameter must be a number between 1 and 16             */
} DataRate_t;


const DataRate_t DataRateMaps[] =//从上位机那边复制过来
{
            { 125000,12,8,69,10 }, //125KBps:prescaler:12,clk:10Mhz,ts1:69,ts2:10,samplePoint:87.50%
        { 250000,6,8,69,10 }, //250KBps:prescaler:6,clk:20Mhz,ts1:69,ts2:10,samplePoint:87.50%
        { 500000,10,8,20,3 }, //500KBps:prescaler:10,clk:12Mhz,ts1:20,ts2:3,samplePoint:87.50%
        { 1000000,5,8,20,3 }, //1000KBps:prescaler:5,clk:24Mhz,ts1:20,ts2:3,samplePoint:87.50%
        { 2000000,1,8,51,8 }, //2000KBps:prescaler:1,clk:120Mhz,ts1:51,ts2:8,samplePoint:86.67%
        { 3000000,1,8,34,5 }, //3000KBps:prescaler:1,clk:120Mhz,ts1:34,ts2:5,samplePoint:87.50%
        { 4000000,1,8,25,4 }, //4000KBps:prescaler:1,clk:120Mhz,ts1:25,ts2:4,samplePoint:86.67%
        { 5000000,1,8,20,3 }, //5000KBps:prescaler:1,clk:120Mhz,ts1:20,ts2:3,samplePoint:87.50%
        { 6000000,1,8,16,3 }, //6000KBps:prescaler:1,clk:120Mhz,ts1:16,ts2:3,samplePoint:85.00%
        { 8000000,1,8,12,2 }, //8000KBps:prescaler:1,clk:120Mhz,ts1:12,ts2:2,samplePoint:86.67%
};

上位机计算波特率的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanSamplePointCal
{
    class Fuck_Autosar
    {
        static void Main(string[] args)
        {
            int busclk = 120 * 1000 * 1000;//外设总线频率
            int prescaler_min = 1, prescaler_max = 512, prescaler = prescaler_min;

            int sjw_min = 1, sjw_max = 128, sjw = sjw_min;
            int ts1_min = 2, ts1_max = 256, ts1 = ts1_min;
            int ts2_min = 2, ts2_max = 128, ts2 = ts2_min;
            double samplePoint_min = 60.0 / 100.0d, samplePoint_max = 87.5 / 100.0d;//采样点设置
            int NominalSyncJumpWidth = 8;
            int need_can_baudrate = 500* 1000;
            
            for (prescaler = prescaler_min; prescaler <= prescaler_max; prescaler++)
            {
                if ((busclk % prescaler) != 0)
                    continue;
                for (sjw = sjw_min; sjw <= sjw_max; sjw++)
                {
                    for (ts1 = ts1_min; ts1 <= ts1_max; ts1++)
                    {
                        for (ts2 = ts2_min; ts2 <= ts2_max; ts2++)
                        {
                            
                            var clk = busclk / prescaler;

                            if ((clk % (1 + ts1 + ts2)) != 0)
                                continue;
                            var br = clk / (1 + ts1 + ts2);

                            var sp = (1.0d + ts1) / (1.0d + ts1 + ts2);
                            if (
                                (br == need_can_baudrate)
                                && (sp >= samplePoint_min)
                                    && (sp <= samplePoint_max)
                                    )
                            {
                                string clkstr = clk >= (1000 * 1000) ? $"{clk / (1000 * 1000d)}Mhz" : $"{clk / 1000d}Khz";
                                string code = $"{{ {br},{prescaler},{NominalSyncJumpWidth},{ts1},{ts2} }},";
                                Console.WriteLine($"{code} //{need_can_baudrate / 1000}KBps:prescaler:{prescaler},clk:{clkstr},ts1:{ts1},ts2:{ts2},samplePoint:{sp:P}");
                            }
                        }
                    }
                }
            }
            
            Console.WriteLine("End!");
            Console.ReadLine();

        }
    }
}

你可能感兴趣的:(C++,嵌入式linux——QT,stm32,arm,嵌入式硬件)