4 #include <system_def.h>
6 #include <local_config.h>
7 #include <usb/usbdebug.h>
10 #include <usb/usb_srq.h>
12 #include <keyval_id.h>
13 #include <hal_machperiph.h>
14 #include <keyval_loc.h>
16 #include <lpciap_kvpb.h>
18 #include "./can/can.h"
19 #include "./can/sja1000p.h"
20 #include "./can/main.h"
22 #include "./can/can_sysdep.h"
23 #include "./can/modparms.h"
24 #include "./can/devcommon.h"
26 #include "./can/ul_usb1.h"
27 //#include "./can/setup.h"
29 #define MASK_EP1RX 0x01
30 #define MASK_EP1TX 0x02
34 #define USB_MAX_PACKET 8
37 #define CAN_OP_MASK 0x80
38 #define CAN_OP_READ 0x80
39 #define CAN_OP_WRITE 0x00
42 LT_TIMER_DEC(lt_10msec)
43 LT_TIMER_IMP(lt_10msec)
44 LT_TIMER_DEC(lt_100msec)
45 LT_TIMER_IMP(lt_100msec)
49 typedef void (*FNC)(); //function ptr
51 /***********************************/
54 usb_device_t usb_device;
57 unsigned char ep1_rx_buff[USB_MAX_PACKET];
58 unsigned char ep1_tx_buff[USB_MAX_PACKET];
59 uint8_t timer_str,timer_rx_off,timer_tx_off,timer_configured,usb_can_send;
60 volatile uint8_t bootloader_run;
64 int baudrate[MAX_TOT_CHIPS];
65 struct canhardware_t canhardware;
66 struct canhardware_t *hardware_p=&canhardware;
67 struct canchip_t *chips_p[MAX_TOT_CHIPS];
68 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
72 SOMETHING BAD HAPPENED
78 if (lt_100msec_expired(100)) {
81 SET_OUT_PIN(LED_PORT,LED_ERR);
83 CLR_OUT_PIN(LED_PORT,LED_ERR);
85 #ifdef WATCHDOG_ENABLED
87 #endif /* WATCHDOG_ENABLED */
93 Routine for visible LED blinking
97 if (timer_tx_off!=0) timer_tx_off--;
98 else SET_OUT_PIN(LED_PORT,LED1_BIT);
99 if (timer_rx_off!=0) timer_rx_off--;
100 else SET_OUT_PIN(LED_PORT,LED2_BIT);
102 /* if (timer_configured!=0) timer_configured--;
105 CLR_OUT_PIN(LED_PORT,LED1_BIT);
106 CLR_OUT_PIN(LED_PORT,LED2_BIT);
107 timer_rx_off=timer_tx_off=5;
111 /***********************************/
114 struct candevice_t *candev;
115 struct canchip_t *chip;
120 /***********************************/
125 SET_OUT_PIN(LED_PORT,LED_ERR);
126 CLR_OUT_PIN(LED_PORT,LED_GP);
128 /// *************************
129 /// CAN device initialization
133 canqueue_kern_initialize();
135 hardware_p->nr_boards=1;
137 candev=(struct candevice_t *)malloc(sizeof(struct candevice_t));
138 if (candev==NULL) sys_err();
139 memset(candev, 0, sizeof(struct candevice_t));
141 hardware_p->candevice[0]=candev;
142 candev->candev_idx=0;
144 candev->dev_base_addr=0;
146 candev->hwspecops=(struct hwspecops_t *)malloc(sizeof(struct hwspecops_t));
147 if (candev->hwspecops==NULL) sys_err();
148 memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
150 ul_usb1_register(candev->hwspecops);
153 if (candev->hwspecops->init_hw_data(candev)) sys_err();
154 /* Alocate and initialize the chip structures */
155 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
157 irqsig=irq[*irq_param_idx_p+chipnr];*/
158 if (init_chip_struct(candev, chipnr, 0, bd*1000)) sys_err();
160 if (candev->hwspecops->request_io(candev))
162 candev->flags|=CANDEV_IO_RESERVED;
163 if (candev->hwspecops->reset(candev))
165 for(chipnr=0; chipnr<candev->nr_all_chips; chipnr++) {
166 if((chip=candev->chip[chipnr])==NULL)
169 if(chip->chipspecops->attach_to_chip(chip)<0) {
170 // CANMSG("Initial attach to the chip HW failed\n");
174 chip->flags |= CHIP_ATTACHED;
176 // Interrupts from chip are served in main cycle
177 /* if(can_chip_setup_irq(chip)<0) {
178 // CANMSG("Error to setup chip IRQ\n");
183 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
184 if (candev->hwspecops->program_irq(candev)){
185 // CANMSG("Error to program board interrupt\n");
191 /// ***************************************
192 /// CAN device initialization - client side
197 memset( &usb_device, 0, sizeof( usb_device));
199 usb_device.init = usb_lpc_init;
200 usb_debug_set_level(DEBUG_LEVEL_NONE);
201 usb_device.cntep = 3;
204 eps[0].max_packet_size = USB_MAX_PACKET;
205 eps[1].max_packet_size = USB_MAX_PACKET;
208 eps[0].event_mask = 0x04;
209 eps[1].event_mask = 0x08;
210 eps[0].udev = &usb_device;
211 eps[1].udev = &usb_device;
213 // usb_device.vendor_fnc=usb_loader;
215 usb_init(&usb_device);
216 usb_connect(&usb_device);
221 /********************/
223 timer_rx_off=timer_tx_off=timer_str=timer_configured=0;
226 usb_check_events(&usb_device);
227 usb_control_response(&usb_device);
229 if ((usb_device.ep_events & MASK_EP1RX)&&(usb_can_send)) { //EP1RX
233 size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,USB_MAX_PACKET);
234 data=(uint8_t *)ep1_rx_buff;
236 if (((*data)&CAN_OP_MASK)==CAN_OP_READ){ // Get data from CAN device and return to caller
237 can_read((*data) & 0x7F,&val);
239 usb_udev_write_endpoint(&eps[1],(unsigned char *)data,size);
240 timer_rx_off=50; //rosviceni diody pri prijmu
241 CLR_OUT_PIN(LED_PORT,LED2_BIT);
244 if (((*data)&CAN_OP_MASK)==CAN_OP_WRITE){ // Save data to CAN device
245 can_write((*data)&(~CAN_OP_MASK),data+1);
246 timer_tx_off=50; //rozsviceni diod pri vysilani
247 CLR_OUT_PIN(LED_PORT,LED1_BIT);
250 usb_device.ep_events &= ~MASK_EP1RX;
253 if(usb_device.ep_events & MASK_EP1TX){
255 usb_device.ep_events &= ~MASK_EP1TX;
258 #ifdef WATCHDOG_ENABLED
260 #endif /* WATCHDOG_ENABLED */
263 if (lt_10msec_expired(10))
267 SET_OUT_PIN(LED_PORT,LED_GP);
268 SET_OUT_PIN(LED_PORT,LED_ERR);
270 /* unreachable code */
272 vec_jmp(0x0); /* need to call a function from misc to correct linking */