]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - embedded/app/usbcan/main.c
Receiving CAN messages with usage of queue system from LinCAN - first version. Attemp...
[lincan.git] / embedded / app / usbcan / main.c
index 4b916312fee5029c84c7e765b3e7b0439382105c..ad7fe36db6caf5891c3f317f9a9c9d195e7097fa 100644 (file)
@@ -106,11 +106,10 @@ LT_TIMER_IMP(lt_2sec)
 
 typedef void (*FNC)(); //function ptr
 
-
 /***********************************************************************
  * global variables
  ***********************************************************************/
-
+volatile uint32_t cnt;
 
 usb_device_t usb_device;
 
@@ -210,8 +209,9 @@ int main(void)
        int chipnr,bd;
        int i,size,m=0;
 
+       cnt=0;
+
        CANMSG("Starting USBCAN module firmware...\n");
-       
 
 //     volatile int i=0;
        bootloader_run=0;
@@ -230,13 +230,8 @@ 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
+// !!! DEBUG - transmitting and receiving CAN messages with usage of queue system from LinCAN - first version! 
 
-/*
 
        //***********************************************************************
        // * CAN device initialization - device side (adapted from LinCAN setup.c)
@@ -244,8 +239,6 @@ int main(void)
 
        // DEBUG
        //can_init(); // only for successful compiling (defined in can_lpcbusemu.c)
-       
-
 
 
        DEBUGMSG("Initiating CAN device initialization\n");
@@ -274,8 +267,13 @@ 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)
+       // register for another board
+       can_lmc1_register(candev->hwspecops);
+
        
 
        bd=baudrate[0];
@@ -283,6 +281,7 @@ int main(void)
                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 +291,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 +305,17 @@ int main(void)
                        if(m>=0) m++;
                }
        }
+
+
        if (candev->hwspecops->request_io(candev))
                sys_err();
        candev->flags|=CANDEV_IO_RESERVED;
+
+
        if (candev->hwspecops->reset(candev))
                sys_err();
+
+
        for(chipnr=0; chipnr<candev->nr_all_chips; chipnr++) {
                if((chip=candev->chip[chipnr])==NULL)
                        continue;
@@ -336,6 +344,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 +363,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 +391,7 @@ int main(void)
        canque_edge_decref(canuser->rx_edge0);
        canque_edge_decref(edge);
 
-*/
+
 
        /***********************************************************************
         * USB Init
@@ -428,10 +438,13 @@ int main(void)
 //             if (!(IO0PIN&P0_SJA1000_INT_PIN)) //INT PIN is inverted
 //                     chip->chipspecops->irq_handler(0,chip);
 
+//             if (CAN1SR & CAN_SR_RBS)
+//                     chip->chipspecops->irq_handler(0,chip);
+
+
                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 +472,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 */
-
-                                       // DEBUG - unused queue system (yet)                            
-                                       //slot->msg=canmsg;
-                                       //canque_put_inslot(qends, qedge, slot);
-                                       CAN_send(&canmsg);
+                       
+                                       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 +507,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");
@@ -524,13 +534,17 @@ int main(void)
                                }
                                usb_udev_write_endpoint(&eps[1],ep1_tx_buff,16);
 
+                               //printf("ID: %d\n", msgid);
+
                                canque_free_outslot(qends, qedge, slot);
                                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 && )