Schubert, Thorsten wrote:
> I am struggling with the CAN bit-timing. I implemented an SJA1000 driver
> on top of the SJA1000 kernel module which comes with Linux 2.6.31. So
> far it
> seems to work except that the bitrate is off by factor two, i.e. a

To be clear here, for the SJA1000 you have to set "priv->can.clock.freq"
to half of the oscillator frequency, e.g. to 8000000 for 16MHz. This is
actually the frequency before the pre-scaler, which is used by the
bit-timing calculation algorithm. This is special to the SJA1000.

> # ip link set can0 type can bitrate 250000
>  
> makes my CAN run at 125kBit/s. While it is easy to work around this
> problem
> by simply providing half the real clock frequency to the struct
> sja1000_priv, 
> I was trying to figure out where and why things go wrong. After reading
> through
> the kernel/driver sources, the CAN sources (Verilog), the SJA1000 spec
> as
> well as the Bosch CAN spec, I got stuck at following point:
> 
> In linux/can/netlink.h I have:
> 
>> /*
>>  * CAN bit-timing parameters
>>  *
>>  * For futher information, please read chapter "8 BIT TIMING
>>  * REQUIREMENTS" of the "Bosch CAN Specification version 2.0"
>>  * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf.
>>  */
>> struct can_bittiming {
>>      __u32 bitrate;          /* Bit-rate in bits/second */
>>      __u32 sample_point;     /* Sample point in one-tenth of a
> percent */
>>      __u32 tq;               /* Time quanta (TQ) in nanoseconds */
>>      __u32 prop_seg;         /* Propagation segment in TQs */
>>      __u32 phase_seg1;       /* Phase buffer segment 1 in TQs */
>>      __u32 phase_seg2;       /* Phase buffer segment 2 in TQs */
>>      __u32 sjw;              /* Synchronisation jump width in TQs */
>>      __u32 brp;              /* Bit-rate prescaler */
>> }; 
> 
> What is the exact meaning of the brp field? Looking at
> drivers/net/can/dev.c,
> 
>> static int can_calc_bittiming(struct net_device *dev, struct
> can_bittiming *bt)
>> {
>>      u64 v64;
>> [...]
>>      v64 = (u64)best_brp * 1000000000UL;
>>      do_div(v64, priv->clock.freq);
>>      bt->tq = (u32)v64;
>> [...]
>>      bt->brp = best_brp;
>>      /* real bit-rate */
>>      bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 +
> 1));
> 
> it seems the meaning of brp=n is "one time quanta is n clock cycles".
> However, looking at drivers/net/can/sja1000/sja1000.c:
> 
>> static int sja1000_set_bittiming(struct net_device *dev)
>> {
>>      struct sja1000_priv *priv = netdev_priv(dev);
>>      struct can_bittiming *bt = &priv->can.bittiming;
>>      u8 btr0, btr1;
>>
>>      btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
>>
>> [...]
>>      
>>      priv->write_reg(priv, REG_BTR0, btr0);
> 
> the brp value (minus one) is written into the SJA's Baud Rate Prescaler
> (BTR0[5:0]). According my SJA1000 spec
> 
> t_scl = 2 * t_clk * (BRP + 1)

Here is the factor of *two* I mentioned above. The frequency before the
pre-scaler is half the frequency of the external oscillator clock.

> Where t_scl is the time quanta and t_clk the clock period. So BRP=n
> means
> "one time quanta is 2*(n+1) clock cycles". So wouldn't
> 
>       btr0 = ((bt->brp/2 - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
> 
> and
> 
> static struct can_bittiming_const sja1000_bittiming_const = {
> [...]
>       .brp_min = 2,
>       .brp_inc = 2,
> [...]
> 
> be more correct?

No, ".brp_inc = 2" would mean that the brp register does not support odd
values. To avoid further confusion, we might think of adding a field
.brp_div, which we would set to "2" for the SJA1000. All other (or
most?) CAN controllers would use "1".

Hope it's clean now.

Wolfgang.
_______________________________________________
Socketcan-core mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/socketcan-core

Reply via email to