embedded: reintroduce missing emulated bus initialization for UL_USB1 board.
[lincan.git] / embedded / app / usbcan / main.c
index 4b91631..14fbe28 100644 (file)
 
 //#include "./can/ul_usb1.h"
 
-//#include "./can/setup.h"
+#include "./can/setup.h"
 
 #include "./usb/usb_defs.h"
 #include "./usb/usb_vend.h"
 
-// DEBUG CAN
-#include "can/lpc17xx_can.h"
-// DEBUG CAN - end
+extern int can_lmc1_register(struct hwspecops_t *hwspecops);
+extern int ul_usb1_register(struct hwspecops_t *hwspecops);
 
 #define MASK_EP1RX  0x01
 #define MASK_EP1TX  0x02
@@ -106,12 +105,10 @@ LT_TIMER_IMP(lt_2sec)
 
 typedef void (*FNC)(); //function ptr
 
-
 /***********************************************************************
  * global variables
  ***********************************************************************/
 
-
 usb_device_t usb_device;
 
 usb_ep_t eps[NUM_ENDPOINTS];
@@ -159,19 +156,6 @@ int sys_err(){
   }
 }
 
-/***********************************************************************
- * Microsecond delay routine
- ***********************************************************************/
-/*
-void udelay(long time)
-{
-  volatile long ticks=(time * CCLK) / 2000000;
-  do{
-    ticks--;
-  } while(ticks>0);
-}
-*/
-
 /***********************************************************************
  * Routine for visible LED blinking (on USB transmission)
  ***********************************************************************/
@@ -211,7 +195,6 @@ int main(void)
        int i,size,m=0;
 
        CANMSG("Starting USBCAN module firmware...\n");
-       
 
 //     volatile int i=0;
        bootloader_run=0;
@@ -230,24 +213,11 @@ int main(void)
                sys_err();
        }
 
-// !!! DEBUG - first version, only transmitting CAN messages withnout usage of queue system from LinCAN
-
-       // CAN
-       CAN_init(1000000);
-       // CAN end
 
-/*
-
-       //***********************************************************************
+       // ***********************************************************************
        // * CAN device initialization - device side (adapted from LinCAN setup.c)
        // ***********************************************************************
 
-       // DEBUG
-       //can_init(); // only for successful compiling (defined in can_lpcbusemu.c)
-       
-
-
-
        DEBUGMSG("Initiating CAN device initialization\n");
        baudrate[0]=1000;
 
@@ -274,15 +244,21 @@ int main(void)
        }
        memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
 
-       // DEBUG
-       //ul_usb1_register(candev->hwspecops);  // only for successful compiling (defined in ul_usb1.c)
        
 
+#ifdef CONFIG_OC_LINCAN_CARD_ul_usb1
+       ul_usb1_register(candev->hwspecops);
+#endif
+#ifdef CONFIG_OC_LINCAN_CARD_can_lmc1
+       can_lmc1_register(candev->hwspecops);
+#endif
+
        bd=baudrate[0];
        if (candev->hwspecops->init_hw_data(candev)){
                CANMSG("HW data could not be initialized\n");
                sys_err();
        }
+
        // Alocate and initialize the chip structures
        for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
 //             if(chipnr<irqnum)
@@ -292,6 +268,9 @@ int main(void)
                        sys_err();
                }
        }
+
+
+
        for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
                struct canchip_t *chip=candev->chip[chipnr];
                int objnr;
@@ -303,11 +282,21 @@ int main(void)
                        if(m>=0) m++;
                }
        }
-       if (candev->hwspecops->request_io(candev))
+
+
+       if (candev->hwspecops->request_io(candev)) {
+               CANMSG("Error to request IO\n");
                sys_err();
+       }
        candev->flags|=CANDEV_IO_RESERVED;
-       if (candev->hwspecops->reset(candev))
+
+
+       if (candev->hwspecops->reset(candev)) {
+               CANMSG("Error to reset chip\n");
                sys_err();
+       }
+
+
        for(chipnr=0; chipnr<candev->nr_all_chips; chipnr++) {
                if((chip=candev->chip[chipnr])==NULL)
                        continue;
@@ -319,11 +308,12 @@ int main(void)
 
                chip->flags |= CHIP_ATTACHED;
 
-// Interrupts from chip are served in main cycle
-//             if(can_chip_setup_irq(chip)<0) {
-//                     CANMSG("Error to setup chip IRQ\n");
-//                     sys_err();
-//             }
+               // used with lpc17xx:
+               if(can_chip_setup_irq(chip)<0) {
+                       CANMSG("Error to setup chip IRQ\n");
+                       sys_err();
+               }
+
        }
 
        if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
@@ -336,6 +326,7 @@ int main(void)
        // * CAN device initialization - client side (adapted from LinCAN open.c)
        // ***********************************************************************
 
+
        chip=candev->chip[0];
        obj=chip->msgobj[0];
        atomic_inc(&obj->obj_used);
@@ -354,6 +345,7 @@ int main(void)
 
        } // End of chip configuration
 
+
        canuser = (struct canuser_t *)malloc(sizeof(struct canuser_t));
        if(canuser == NULL) sys_err();
        canuser->flags=0;
@@ -381,7 +373,7 @@ int main(void)
        canque_edge_decref(canuser->rx_edge0);
        canque_edge_decref(edge);
 
-*/
+
 
        /***********************************************************************
         * USB Init
@@ -424,14 +416,15 @@ int main(void)
                usb_check_events(&usb_device);
                usb_control_response(&usb_device);
 
-// DEBUG
-//             if (!(IO0PIN&P0_SJA1000_INT_PIN)) //INT PIN is inverted
-//                     chip->chipspecops->irq_handler(0,chip);
+#ifdef CONFIG_OC_LINCAN_CARD_ul_usb1
+               /* polled IRQ mode for ul_usb1 board*/
+               if (!(IO0PIN&P0_SJA1000_INT_PIN)) /* INT PIN is inverted */
+                       chip->chipspecops->irq_handler(0,chip);
+#endif
 
                if (usb_device.ep_events & MASK_EP1RX) {  //EP1RX - data waiting to receive
                        
-                       // DEBUG - unused queue system (yet)
-                       //if (canque_get_inslot(qends, &qedge, &slot, 0)>=0){ //Free slot obtained
+                       if (canque_get_inslot(qends, &qedge, &slot, 0)>=0){ //Free slot obtained
 
                                size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,16);
                                if (size==16){
@@ -459,24 +452,22 @@ int main(void)
                                        /* Automatic selection of extended format if ID>2047 */
                                        if (canmsg.id & ~0x7ffl & MSG_ID_MASK ) canmsg.flags |= MSG_EXT;
                                        /* has been dependent on "extended" option */
+                       
+                                       slot->msg=canmsg;
+                                       canque_put_inslot(qends, qedge, slot);
 
-                                       // DEBUG - unused queue system (yet)                            
-                                       //slot->msg=canmsg;
-                                       //canque_put_inslot(qends, qedge, slot);
-                                       CAN_send(&canmsg);
                                }
-
-                               // DEBUG - unused queue system (yet)
-                               //else
-                               //      canque_abort_inslot(qends,qedge,slot);
+                               else
+                                       canque_abort_inslot(qends,qedge,slot);
+                                       
 
                                timer_rx_off=50;        //rosviceni diody pri prijmu
                                CLR_OUT_PIN(LED_PORT,LED2_BIT);
                                usb_device.ep_events &= ~MASK_EP1RX;
                        
-                       // DEBUG                        
-                       //}
-
+                                       
+                       }
+                       
 
 /*                     if (size==2){
                                uint8_t val;
@@ -496,8 +487,7 @@ int main(void)
                        }*/
                }
 
-               // DEBUG - only transmitting CAN messages, no reading (yet)
-               /*
+               
                if(usb_device.ep_events & MASK_EP1TX){ //EP1TX - data transmitted
                        if(canque_test_outslot(qends, &qedge, &slot)>=0){
                                DEBUGMSG("CAN message ready to send over usb\n");
@@ -528,9 +518,12 @@ int main(void)
                                timer_tx_off=50;                //rozsviceni diod pri vysilani
                                CLR_OUT_PIN(LED_PORT,LED1_BIT);
                                usb_device.ep_events &= ~MASK_EP1TX;
+
                        }
+                       
+
                }
-               */
+               
 
                //if (usb_can_send && )