]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - embedded/app/usbcan/sja1000p.c
embedded ul_usb1: provide support for external bittiming parameters calculation.
[lincan.git] / embedded / app / usbcan / sja1000p.c
index bbee1cd19b3f40b17aca43250a6f570824480206..76ad469a80a3622542664fcf694bf01fe335bcb2 100644 (file)
@@ -348,6 +348,70 @@ int sja1000p_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
        return 0;
 }
 
+
+/**
+ * sja1000p_set_bittiming: - set bittiming according to already computed parameters.
+ * @chip: pointer to chip state structure
+ * @bpr: baud rate prescaler
+ * @sjw: synchronization jump width in bittime quanta
+ * @tseg1: length of the segment 1 in bittime quanta
+ * @tseg2: length of the segment 2 in bittime quanta
+ *
+ * Return Value: negative value reports error.
+ * File: src/sja1000p.c
+ */
+int sja1000p_set_bittiming(struct canchip_t *chip, int brp, int sjw, int tseg1, int tseg2)
+{
+       uint8_t sam3times = 0; /* 0 = the bus is sampled once */
+
+       if((--brp)<0)
+               return -EINVAL;
+
+       if((--sjw)<0)
+               return -EINVAL;
+
+       if((--tseg1)<0)
+               return -EINVAL;
+
+       if((--tseg2)<0)
+               return -EINVAL;
+       
+
+
+       if (sja1000p_enable_configuration(chip))
+               return -ENODEV;
+
+       can_write_reg(chip, sjw<<6 | brp, SJABTR0);
+       can_write_reg(chip, (sam3times<<7) | (tseg2<<4) | tseg1, SJABTR1);
+
+       sja1000p_disable_configuration(chip);
+       
+       return 0;
+}
+
+/**
+ * sja1000p_get_bittiming_const: - obtain description of chip bittiming calculation.
+ * @chip: pointer to chip state structure
+ * @btc: pointer to the structure filled by data
+ *
+ * Return Value: negative value reports error.
+ * File: src/sja1000p.c
+ */
+int sja1000p_get_bittiming_const(struct canchip_t *chip, struct can_bittiming_const *btc)
+{
+       btc->tseg1_min = 1;
+       btc->tseg1_max = 16; /* sjaMAX_TSEG1+1 */
+       btc->tseg2_min = 1;
+       btc->tseg2_max = 8; /* sjaMAX_TSEG2+1 */
+       btc->sjw_max = 4;
+       btc->brp_min = 1;
+       btc->brp_max = 64;
+       btc->brp_inc = 1;
+
+       return 0;
+}
+
+
 /**
  * sja1000p_read: - reads and distributes one or more received messages
  * @chip: pointer to chip state structure
@@ -915,6 +979,8 @@ int sja1000p_register(struct chipspecops_t *chipspecops)
        chipspecops->stop_chip=sja1000p_stop_chip;
        chipspecops->irq_handler=sja1000p_irq_handler;
        chipspecops->irq_accept=NULL;
+       chipspecops->set_bittiming=sja1000p_set_bittiming;
+       chipspecops->get_bittiming_const=sja1000p_get_bittiming_const;
        return 0;
 }