Calculate CAN bit timing parameters

Calculate CAN bit timing parameters

  1. typedef struct
  2. {
  3. //char name[ 16 ]; // Name of the CAN controller hardware
  4. //uint32_t ref_clk; // CAN system clock frequency in Hz
  5. //uint32_t sjw_max; // Synchronisation jump width
  6. uint32_t brp_min; // Bit-rate prescaler
  7. uint32_t brp_max;
  8. uint32_t brp_inc;
  9. uint32_t tseg1_min; // Time segement 1 = prop_seg + phase_seg1
  10. uint32_t tseg1_max;
  11. uint32_t tseg2_min; // Time segement 2 = phase_seg2
  12. uint32_t tseg2_max;
  13. } CAN_BitTimingConst_TypeDef;
  14.  
  15. typedef struct
  16. {
  17. uint32_t ref_clk; // CAN system clock frequency in Hz
  18. uint32_t bitrate; // Bit-rate in bits/second
  19. uint32_t sample_point; // Sample point in one-tenth of a percent
  20. uint32_t brp; // Bit-rate prescaler
  21. uint32_t tq; // Time quanta (TQ) in nanoseconds
  22. uint32_t tseg1; // Time segement 1 = prop_seg + phase_seg1
  23. uint32_t tseg2; // Time segement 2 = phase_seg2
  24. uint32_t sjw; // Synchronisation jump width in TQs
  25. //uint32_t prop_seg; // Propagation segment in TQs
  26. //uint32_t phase_seg1; // Phase buffer segment 1 in TQs
  27. //uint32_t phase_seg2; // Phase buffer segment 2 in TQs
  28. } CAN_BitTiming_TypeDef;
  1. #define CAN_CALC_MAX_ERROR 50 // in one-tenth of a percent
  2.  
  3. int32_t CAN_UpdateSamplePoint( CAN_BitTimingConst_TypeDef *btc,
  4. int32_t sampl_pt, int32_t tseg, int32_t *tseg1, int32_t *tseg2 )
  5. {
  6. *tseg2 = tseg + - ( sampl_pt * ( tseg + ) ) / ;
  7.  
  8. if ( *tseg2 < btc->tseg2_min )
  9. *tseg2 = btc->tseg2_min;
  10.  
  11. if ( *tseg2 > btc->tseg2_max )
  12. *tseg2 = btc->tseg2_max;
  13.  
  14. *tseg1 = tseg - *tseg2;
  15.  
  16. if ( *tseg1 > btc->tseg1_max )
  17. {
  18. *tseg1 = btc->tseg1_max;
  19. *tseg2 = tseg - *tseg1;
  20. }
  21.  
  22. return * ( tseg + - *tseg2 ) / ( tseg + );
  23. }
  24.  
  25. // CIA Sample Point : 75.0% : Speed > 800000
  26. // CIA Sample Point : 80.0% : Speed > 500000
  27. // CIA Sample Point : 87.5% : Speed <= 500000
  28. uint32_t CAN_CIA_SamplePoint( uint32_t bitrate )
  29. {
  30. uint32_t sampl_pt;
  31.  
  32. if ( bitrate > )
  33. sampl_pt = ;
  34. else if ( bitrate > )
  35. sampl_pt = ;
  36. else
  37. sampl_pt = ;
  38.  
  39. return sampl_pt;
  40. }
  41.  
  42. int32_t CAN_CalcBitTiming( CAN_BitTimingConst_TypeDef *btc,
  43. CAN_BitTiming_TypeDef *bt )
  44. {
  45. uint64_t v64;
  46. int32_t rate = ;
  47. int32_t best_error = , error = ;
  48. int32_t best_tseg = , best_brp = , brp = ;
  49. int32_t tsegall, tseg = , tseg1 = , tseg2 = ;
  50. int32_t spt_error = , spt = , sampl_pt;
  51.  
  52. // Use gived sample points
  53. if ( bt->sample_point )
  54. sampl_pt = bt->sample_point;
  55. else
  56. // Use CIA recommended sample points
  57. sampl_pt = CAN_CIA_SamplePoint( bt->bitrate );
  58.  
  59. // tseg even = round down, odd = round up
  60. for ( tseg = ( btc->tseg1_max + btc->tseg2_max ) * + ;
  61. tseg >= ( btc->tseg1_min + btc->tseg2_min ) * ; tseg-- )
  62. {
  63. tsegall = + tseg / ;
  64.  
  65. // Compute all possible tseg choices (tseg=tseg1+tseg2)
  66. brp = bt->ref_clk / ( tsegall * bt->bitrate ) + tseg % ;
  67.  
  68. // chose brp step which is possible in system
  69. brp = ( brp / btc->brp_inc ) * btc->brp_inc;
  70. if ( ( brp < btc->brp_min ) || ( brp > btc->brp_max ) )
  71. continue;
  72.  
  73. rate = bt->ref_clk / ( brp * tsegall );
  74. error = bt->bitrate - rate;
  75.  
  76. // tseg brp biterror
  77. if ( error < )
  78. error = -error;
  79.  
  80. if ( error > best_error )
  81. continue;
  82.  
  83. best_error = error;
  84. if ( error == )
  85. {
  86. spt = CAN_UpdateSamplePoint( btc, sampl_pt, tseg / , &tseg1, &tseg2 );
  87. error = sampl_pt - spt;
  88. if ( error < )
  89. error = -error;
  90. if ( error > spt_error )
  91. continue;
  92.  
  93. spt_error = error;
  94. }
  95.  
  96. best_tseg = tseg / ;
  97. best_brp = brp;
  98. if ( error == )
  99. break;
  100. }
  101.  
  102. if ( best_error )
  103. {
  104. /* Error in one-tenth of a percent */
  105. error = ( best_error * ) / bt->bitrate;
  106. if ( error > CAN_CALC_MAX_ERROR )
  107. {
  108. // error ( "bitrate error %ld.%ld%% too high\n", error / 10, error % 10 );
  109. return DRIVER_ERROR_PARAMETER;
  110. }
  111. else
  112. {
  113. // warn( "bitrate error %ld.%ld%%\n", error / 10, error % 10 );
  114. }
  115. }
  116.  
  117. v64 = ( (uint64_t) best_brp * 1000000000UL ) / bt->ref_clk;
  118.  
  119. bt->tq = (uint32_t) v64;
  120. bt->brp = best_brp;
  121. bt->tseg2 = tseg2;
  122. bt->tseg1 = tseg1;
  123. bt->sjw = ;
  124. // bt->prop_seg = tseg1 / 2;
  125. // bt->phase_seg1 = tseg1 - bt->prop_seg;
  126. // bt->phase_seg2 = tseg2;
  127.  
  128. // real bit-rate
  129. bt->bitrate = bt->ref_clk / ( bt->brp * ( tseg1 + tseg2 + ) );
  130. // real sample point
  131.  
  132. bt->sample_point = CAN_UpdateSamplePoint( btc, sampl_pt, best_tseg, &tseg1,
  133. &tseg2 );
  134.  
  135. return DRIVER_OK;
  136. }

SJW[1:0]: Resynchronization jump width
These bits define the maximum number of time quanta the CAN hardware 
is allowed to lengthen or shorten a bit to perform the resynchronization.
tRJW = tq x (SJW[1:0] + 1)

TS2[2:0]: Time segment 2
These bits define the number of time quanta in Time Segment 2.
tBS2 = tq x (TS2[2:0] + 1)

TS1[3:0]: Time segment 1
These bits define the number of time quanta in Time Segment 1
tBS1 = tq x (TS1[3:0] + 1)

BRP[9:0]: Baud rate prescaler
These bits define the length of a time quanta.
tq = (BRP[9:0]+1) x tPCLK

  1. const CAN_BitTimingConst_TypeDef CAN_BitTimingConst =
  2. { , // Bit-rate prescaler Min
  3. , // Bit-rate prescaler Max
  4. , // Bit-rate prescaler Inc
  5. , // Time segement 1 = prop_seg + phase_seg1 Min
  6. , // Time segement 1 = prop_seg + phase_seg1 Max
  7. , // Time segement 2 = phase_seg2 Min
  8. , // Time segement 2 = phase_seg2 Max
  9. };
  10.  
  11. static int32_t CAN_SetSpeed( CAN_Controller_TypeDef *can, uint32_t speed )
  12. {
  13. int32_t RetValue = CAN_EnterInit( can );
  14. if ( RetValue != DRIVER_OK )
  15. return RetValue;
  16.  
  17. uint32_t Freq = can->freq( );
  18. CAN_BitTiming_TypeDef CAN_BitTiming;
  19. CAN_BitTiming.ref_clk = Freq;
  20. CAN_BitTiming.bitrate = speed; // be updated to real speed
    CAN_BitTiming.sample_point = 0; // be updated to real spt
  21.  
  22. RetValue = CAN_CalcBitTiming( &CAN_BitTimingConst, &CAN_BitTiming );
  23. if ( RetValue == DRIVER_OK )
  24. {
  25. can->info->speed = CAN_BitTiming.bitrate; // updated
  26. uint32_t BTR = can->reg->BTR & 0xC0000000; // SILM|LBKM
  27. BTR |= ( ( CAN_BitTiming.brp - ) << ) // BRP
  28. | ( ( CAN_BitTiming.tseg1 ) << ) // TS1
  29. | ( ( CAN_BitTiming.tseg2 - ) << ) // TS2
  30. | ( ( CAN_BitTiming.sjw - ) << ); // SJW
  31. can->reg->BTR = BTR;
  32. }
  33.  
  34. return CAN_LeaveInit( can );
  1. /* BPR TSEG1 TSEG2 */
  2. /* 36 MHz 1 Mbps */ { , , }, // 75%
  3. /* 36 MHz 800 Kbps */ { , , }, // 80%
  4. /* 36 MHz 500 Kbps */ { , , }, // 83.3%
  5. /* 36 MHz 250 Kbps */ { , , }, // 87.5%
  6. /* 36 MHz 125 Kbps */ {, , }, // 87.5%
  7. /* 36 MHz 100 Kbps */ {, , }, // 86.6%
  8. /* 36 MHz 83.3 Kbps */ {, , }, // 83.3%
  9. /* 36 MHz 62.5 Kbps */ {, , }, // 87.5%
  10. /* 36 MHz 50 Kbps */ {, , }, // 87.5%
  11. /* 36 MHz 20 Kbps */ {,, }, // 86.6%
  12. /* 36 MHz 10 Kbps */ {,, }, // 87.5%
  13. /* 36 MHz 500 Kbps */ { , , } // 83.3%

Calculate CAN bit timing parameters -- STM32的更多相关文章

  1. Calculate CAN bit timing parameters

    Calculate CAN bit timing parameters TSYNC_SEG === 1 TSEG1 = Prop_Seg + Phase_Seg1 TSEG2 = Phase_Seg2 ...

  2. 0xWS2812 STM32 driver for WS2812(B) RGB LEDs

    0xWS2812 STM32 driver for WS2812(B) RGB LEDs 0xWS2812 pronounced "hex-WS2812" This code ai ...

  3. CRT/LCD/VGA Information and Timing

    彩色阴极射线管的剖面图: 1. 电子QIANG Three Electron guns (for red, green, and blue phosphor dots)2. 电子束 Electron ...

  4. CRT/LCD/VGA Information and Timing【转】

    转自:http://www.cnblogs.com/shangdawei/p/4760933.html 彩色阴极射线管的剖面图: 1. 电子QIANG Three Electron guns (for ...

  5. CALayer之 customizing timing of an animation

    customizing timing of an animation Timing is an important part of animations, and with Core Animatio ...

  6. RFID 读写器 Reader Writer Cloner

    RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...

  7. RFID Reader 线路图收集

    This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...

  8. BlackArch-Tools

    BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...

  9. Timequest静态时序分析(STA)基础

    Setup Slack Hold Slack Recovery&Removal Recovery: The minimum time an asynchronous signal must b ...

随机推荐

  1. 第11月第11天 avplayer循环播放

    1. /* Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very sim ...

  2. 常用的C#编译命令

    使用 csc.exe 实现命令行生成 作为一个半路出家的非计算机专业出身的前端码农,最近对C#很感兴趣,原因如下: 1.希望通过学习C#能熟悉一下windows系统和一些概念,例如:windows服务 ...

  3. Bug Bounty Reference

    https://github.com/ngalongc/bug-bounty-reference/blob/master/README.md#remote-code-execution Bug Bou ...

  4. BAT获取FTP指定文件

    以下两个文件放在同一目录下 getfile.bat文件内容如下: @echo offftp.exe -i -s:getfile.txt 192.168.1.2(更换成你的ip,参数之间有空格)paus ...

  5. MAC系统下Sublime Text3 配置Python3详细教程

    MAC系统下Sublime Text3 配置Python3详细教程(亲测有效) https://blog.csdn.net/weixin_41768008/article/details/798590 ...

  6. php的递归函数示例

    递归函数太难理解了,写了一个示例放在这里方便没事的时候看一下. <?php /** *php递归函数示例 *(从1到100的累加和计算) * */ function summation($num ...

  7. 使用netperf测试网络性能

    1.安装netperf 1)获取netperf安装包 netperf-2.7.0.tar.bz2 2)解压到本地目录 3)进入netperf-2.7.0,执行:./configure 4)编译执行:m ...

  8. CentOS下配置MySQL允许root用户远程登录

    1.常用命令: 安装上传下载文件命令yum install lrzsz安装webget工具yum -y install wget ----------------------------------- ...

  9. 交换机NTP的MD5配置

    1.ntp-service authentication enable  开启NTP身份验证功能 2.ntp-service source-interfer LoopBack0 指定本机发生NTP的端 ...

  10. IntelliJ IDEA 设置Output 窗口字体大小

    Settings——>Editor——>Colors&Fonts——>Console Font 如图: 字体调好了以后使用起来眼睛就轻松多了