]> rtime.felk.cvut.cz Git - lincan.git/commitdiff
lpc17xx embedded: new function for obtaining chip bittiming constant parameters.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Fri, 25 May 2012 16:10:27 +0000 (18:10 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Fri, 25 May 2012 16:10:27 +0000 (18:10 +0200)
This allows to use same CAN-USB converter code for other chips as well.

Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
embedded/app/usbcan/Makefile.omk
embedded/app/usbcan/can/can_bittiming.h [new file with mode: 0644]
embedded/app/usbcan/can/lpc17xx_can.h
embedded/app/usbcan/can/main.h
embedded/app/usbcan/lpc17xx_can.c
embedded/app/usbcan/main.c
embedded/app/usbcan/usb_vend.c

index 723e15730f386322f2aa623cfdf5d50d23d37142..c4c7cbae4468a3f9ac69cf9e68effe3b570f7a6b 100644 (file)
@@ -21,8 +21,11 @@ INCLUDES += -I.
 bin_PROGRAMS = usbcan
 usbcan_SOURCES = main.c can_queue.c sja1000p.c  devcommon.c setup.c finish.c
 usbcan_SOURCES += usb_vend.c can_quesysless.c sysdep_sysless.c
 bin_PROGRAMS = usbcan
 usbcan_SOURCES = main.c can_queue.c sja1000p.c  devcommon.c setup.c finish.c
 usbcan_SOURCES += usb_vend.c can_quesysless.c sysdep_sysless.c
+ifeq ($(MACH),lpc17xx)
 usbcan_SOURCES += lpc17xx_can.c
 usbcan_SOURCES += lpc17xx_can.c
+else #MACH
 #usbcan_SOURCES += can_lpcbusemu.c ul_usb1.c
 #usbcan_SOURCES += can_lpcbusemu.c ul_usb1.c
+endif #MACH
 
 #usbtest_SOURCES +=  ul_idstr.c
 #lib_LOADLIBES =  bspbase ul_drv lpciap keyval lpciap_kvpb mach_hal uldy
 
 #usbtest_SOURCES +=  ul_idstr.c
 #lib_LOADLIBES =  bspbase ul_drv lpciap keyval lpciap_kvpb mach_hal uldy
diff --git a/embedded/app/usbcan/can/can_bittiming.h b/embedded/app/usbcan/can/can_bittiming.h
new file mode 100644 (file)
index 0000000..32f79c9
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef _CAN_BITTIMING_H
+#define _CAN_BITTIMING_H
+
+#include "./can_sysdep.h"
+
+/*
+ * CAN harware-dependent bit-timing constant
+ *
+ * Used for calculating and checking bit-timing parameters
+ */
+
+struct can_bittiming_const {
+       char name[16];          /* Name of the CAN controller hardware */
+       uint32_t tseg1_min;        /* Time segement 1 = prop_seg + phase_seg1 */
+       uint32_t tseg1_max;
+       uint32_t tseg2_min;        /* Time segement 2 = phase_seg2 */
+       uint32_t tseg2_max;
+       uint32_t sjw_max;          /* Synchronisation jump width */
+       uint32_t brp_min;          /* Bit-rate prescaler */
+       uint32_t brp_max;
+       uint32_t brp_inc;
+};
+
+#endif /*_CAN_BITTIMING_H*/
index 5f9f4c0a533206d1cb8b2751ae2710c3c63c35b9..9a8b25977de117fbd489eddc6e48f2765e967bec 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "can/can.h"
 #include "can/canmsg.h"
 
 #include "can/can.h"
 #include "can/canmsg.h"
+#include "can/can_bittiming.h"
 
 #ifdef __cplusplus
 extern "C"
 
 #ifdef __cplusplus
 extern "C"
@@ -155,29 +156,9 @@ void CAN_init(struct canchip_t *chip);
 void CAN_send(struct canchip_t *chip, canmsg_t* msg);
 void CAN_recv(struct canchip_t *chip, canmsg_t* msg);
 
 void CAN_send(struct canchip_t *chip, canmsg_t* msg);
 void CAN_recv(struct canchip_t *chip, canmsg_t* msg);
 
-
-/*
- * CAN harware-dependent bit-timing constant
- *
- * Used for calculating and checking bit-timing parameters
- */
-
-struct can_bittiming_const {
-       char name[16];          /* Name of the CAN controller hardware */
-       uint32_t tseg1_min;        /* Time segement 1 = prop_seg + phase_seg1 */
-       uint32_t tseg1_max;
-       uint32_t tseg2_min;        /* Time segement 2 = phase_seg2 */
-       uint32_t tseg2_max;
-       uint32_t sjw_max;          /* Synchronisation jump width */
-       uint32_t brp_min;          /* Bit-rate prescaler */
-       uint32_t brp_max;
-       uint32_t brp_inc;
-};
-
 struct can_lmc1_chip_data
 {
        int flags;
 struct can_lmc1_chip_data
 {
        int flags;
-       struct can_bittiming_const btc;
 };
 
 
 };
 
 
index b9fae2655b7b963b94f1992c3c60bf24c9c1c928..cd5fd51769c7635fa9301fc1d7d550098df77402 100644 (file)
@@ -40,6 +40,7 @@
 #include "./ul_listbase.h"
 #include "./can_sysdep.h"
 #include "./can_queue.h"
 #include "./ul_listbase.h"
 #include "./can_sysdep.h"
 #include "./can_queue.h"
+#include "./can_bittiming.h"
 #include "./errno.h"
 
 #ifdef CAN_DEBUG
 #include "./errno.h"
 
 #ifdef CAN_DEBUG
@@ -361,6 +362,7 @@ struct hwspecops_t {
  * @irq_accept: optional fast irq accept routine responsible for blocking further interrupts
  *
  * @set_bittiming: set bittiming parameters
  * @irq_accept: optional fast irq accept routine responsible for blocking further interrupts
  *
  * @set_bittiming: set bittiming parameters
+ * @get_bittiming_const: get chip specific constants for bittiming computation
  */
 struct chipspecops_t {
        int (*chip_config)(struct canchip_t *chip);
  */
 struct chipspecops_t {
        int (*chip_config)(struct canchip_t *chip);
@@ -395,6 +397,7 @@ struct chipspecops_t {
        int (*irq_accept)(int irq, struct canchip_t *chip);
 
        int (*set_bittiming)(struct canchip_t *chip, int brp, int sjw, int tseg1, int tseg2);
        int (*irq_accept)(int irq, struct canchip_t *chip);
 
        int (*set_bittiming)(struct canchip_t *chip, int brp, int sjw, int tseg1, int tseg2);
+       int (*get_bittiming_const)(struct canchip_t *chip, struct can_bittiming_const *btc);
 };
 
 struct mem_addr {
 };
 
 struct mem_addr {
index 0d42658c16414049463e6df5265d64934ea25103..f9fd5ac5c2e634e5cf53c14083babdcecad98099 100644 (file)
@@ -32,7 +32,6 @@ int can_lmc1_init_hw_data(struct candevice_t *candev){
 
 int can_lmc1_init_chip_data(struct candevice_t *candev, int chipnr){
 
 
 int can_lmc1_init_chip_data(struct candevice_t *candev, int chipnr){
 
-       struct can_bittiming_const *btc;
        struct can_lmc1_chip_data *chip_data;
 
        // used CAN1 peripherial -> CAN1 registers base 
        struct can_lmc1_chip_data *chip_data;
 
        // used CAN1 peripherial -> CAN1 registers base 
@@ -48,18 +47,7 @@ int can_lmc1_init_chip_data(struct candevice_t *candev, int chipnr){
 
 
        chip_data = (struct can_lmc1_chip_data*) candev->chip[chipnr]->chip_data;
 
 
        chip_data = (struct can_lmc1_chip_data*) candev->chip[chipnr]->chip_data;
-
-       btc = &chip_data->btc;
-       
-       // set bittiming constants
-       btc->tseg1_min = 1;
-       btc->tseg1_max = 16;
-       btc->tseg2_min = 1;
-       btc->tseg2_max = 8;
-       btc->sjw_max = 4;
-       btc->brp_min = 1;
-       btc->brp_max = 1024;
-       btc->brp_inc = 1;
+       chip_data->flags = 0;
 
        return 0;
 }
 
        return 0;
 }
@@ -113,9 +101,10 @@ int lpc17xx_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
        int best_tseg=0, best_brp=0, best_rate=0, brp=0;
        int tseg=0, tseg1=0, tseg2=0;
 
        int best_tseg=0, best_brp=0, best_rate=0, brp=0;
        int tseg=0, tseg1=0, tseg2=0;
 
-       struct can_lmc1_chip_data *chip_data = (struct can_lmc1_chip_data*) chip->chip_data;
+       struct can_bittiming_const btc_buff;
+       struct can_bittiming_const *btc = &btc_buff;
 
 
-       struct can_bittiming_const *btc = &chip_data->btc;
+       chip->chipspecops->get_bittiming_const(chip, btc);              
 
        /* tseg even = round down, odd = round up */
        for (tseg=(0+0+2)*2; tseg<=(btc->tseg2_max+btc->tseg1_max+2)*2+1; tseg++) {
 
        /* tseg even = round down, odd = round up */
        for (tseg=(0+0+2)*2; tseg<=(btc->tseg2_max+btc->tseg1_max+2)*2+1; tseg++) {
@@ -385,6 +374,20 @@ int lpc17xx_set_bittiming(struct canchip_t *chip, int brp, int sjw, int tseg1, i
        return 0;
 }
 
        return 0;
 }
 
+int lpc17xx_get_bittiming_const(struct canchip_t *chip, struct can_bittiming_const *btc) {
+       btc->tseg1_min = 1;
+       btc->tseg1_max = 16;
+       btc->tseg2_min = 1;
+       btc->tseg2_max = 8;
+       btc->sjw_max = 4;
+       btc->brp_min = 1;
+       btc->brp_max = 1024;
+       btc->brp_inc = 1;
+
+       return 0;
+}
+
+
 int lpc17xx_fill_chipspecops(struct canchip_t *chip){
 
        chip->max_objects=1;
 int lpc17xx_fill_chipspecops(struct canchip_t *chip){
 
        chip->max_objects=1;
@@ -408,6 +411,7 @@ int lpc17xx_register(struct chipspecops_t *chipspecops){
        chipspecops->irq_handler = lpc17xx_irq_handler;
 
        chipspecops->set_bittiming = lpc17xx_set_bittiming;
        chipspecops->irq_handler = lpc17xx_irq_handler;
 
        chipspecops->set_bittiming = lpc17xx_set_bittiming;
+       chipspecops->get_bittiming_const = lpc17xx_get_bittiming_const;
 
        return 0;       
 
 
        return 0;       
 
index 6a5b3d4aa494dd2c1f818350979e9361c57dc17a..6a7b8be8fcd5732d0d18736950d6d4f33ac13016 100644 (file)
@@ -67,8 +67,7 @@
 #include "./usb/usb_defs.h"
 #include "./usb/usb_vend.h"
 
 #include "./usb/usb_defs.h"
 #include "./usb/usb_vend.h"
 
-
-#include "can/lpc17xx_can.h"
+extern int can_lmc1_register(struct hwspecops_t *hwspecops);
 
 #define MASK_EP1RX  0x01
 #define MASK_EP1TX  0x02
 
 #define MASK_EP1RX  0x01
 #define MASK_EP1TX  0x02
@@ -227,7 +226,7 @@ int main(void)
        }
 
 
        }
 
 
-       //***********************************************************************
+       // ***********************************************************************
        // * CAN device initialization - device side (adapted from LinCAN setup.c)
        // ***********************************************************************
 
        // * CAN device initialization - device side (adapted from LinCAN setup.c)
        // ***********************************************************************
 
index fbe5be97bd402760ecd644cecb5c9ec4247c1a72..460c85d7573fac82bc5dfa5fcff80e3067cc063e 100644 (file)
@@ -14,7 +14,7 @@
   #include <byteswap.h>
 #endif
 
   #include <byteswap.h>
 #endif
 
-#include "./can/lpc17xx_can.h"
+#include "./can/can_bittiming.h"
 
 extern struct canuser_t *canuser;
 extern uint8_t vendor_ret;
 
 extern struct canuser_t *canuser;
 extern uint8_t vendor_ret;
@@ -140,12 +140,12 @@ error:
        return -1;
 }
 
        return -1;
 }
 
-int get_bittiming_const_complete_fnc(usb_device_t *udev){
-
+int usbcan_get_bittiming_const_reply(usb_device_t *udev){
        int dest_chip;
        int dest_chip;
+       struct canchip_t *chip;
 
 
-       struct can_lmc1_chip_data *chip_data=NULL;
-       struct can_bittiming_const *btc;
+       struct can_bittiming_const btc_buff;
+       struct can_bittiming_const *btc = &btc_buff;
 
        uint8_t buffer[USBCAN_BITTIMING_CONST_SIZE];
        uint32_t *ptr;
 
        uint8_t buffer[USBCAN_BITTIMING_CONST_SIZE];
        uint32_t *ptr;
@@ -155,14 +155,13 @@ int get_bittiming_const_complete_fnc(usb_device_t *udev){
                dest_chip=(udev->request.wIndex);
                if ((dest_chip>=MAX_TOT_CHIPS)||(dest_chip<0))
                        goto error;
                dest_chip=(udev->request.wIndex);
                if ((dest_chip>=MAX_TOT_CHIPS)||(dest_chip<0))
                        goto error;
-               if (!chips_p[dest_chip])
+               if ((chip = chips_p[dest_chip]) == NULL)
                        goto error;
                        goto error;
-               if ((chip_data=((struct can_lmc1_chip_data*)(chips_p[dest_chip]->chip_data)))==NULL){
+               if (chip->chipspecops->get_bittiming_const == NULL){
                        goto error;
                }
 
                        goto error;
                }
 
-               btc = &chip_data->btc;
-               
+               chip->chipspecops->get_bittiming_const(chip, btc);              
                ptr = (uint32_t*) buffer;
 
                #if __BYTE_ORDER == __BIG_ENDIAN
                ptr = (uint32_t*) buffer;
 
                #if __BYTE_ORDER == __BIG_ENDIAN
@@ -217,7 +216,7 @@ int usbcan_vendor(usb_device_t *udev)
                        return 1;
 
                case USBCAN_VENDOR_GET_BITTIMING_CONST:
                        return 1;
 
                case USBCAN_VENDOR_GET_BITTIMING_CONST:
-                       get_bittiming_const_complete_fnc(udev);
+                       usbcan_get_bittiming_const_reply(udev);
                        return 1;
                //lpc17xx - end
 
                        return 1;
                //lpc17xx - end