--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include <cpu_def.h>
+#include <system_def.h>
+#include <lt_timer.h>
+#include <local_config.h>
+#include <usb/usbdebug.h>
+#include <usb/usb.h>
+#include <usb/lpc.h>
+#include <usb/usb_srq.h>
+#include <mem_loc.h>
+#include <keyval_id.h>
+#include <hal_machperiph.h>
+#include <keyval_loc.h>
+#include <lpciap.h>
+#include <lpciap_kvpb.h>
+
+#include "can/can.h"
+#include "can/sja1000p.h"
+
+#define MASK_EP1RX 0x01
+#define MASK_EP1TX 0x02
+
+#ifdef USB_MAX_PACKET
+ #undef USB_MAX_PACKET
+ #define USB_MAX_PACKET 8
+#endif
+
+#define CAN_OP_MASK 0x80
+#define CAN_OP_READ 0x80
+#define CAN_OP_WRITE 0x00
+
+
+LT_TIMER_DEC(lt_10msec)
+LT_TIMER_IMP(lt_10msec)
+LT_TIMER_DEC(lt_100msec)
+LT_TIMER_IMP(lt_100msec)
+LT_TIMER_DEC(lt_2sec)
+LT_TIMER_IMP(lt_2sec)
+
+typedef void (*FNC)(); //function ptr
+
+/***********************************/
+// global variables
+
+usb_device_t usb_device;
+
+usb_ep_t eps[2];
+unsigned char ep1_rx_buff[USB_MAX_PACKET];
+unsigned char ep1_tx_buff[USB_MAX_PACKET];
+uint8_t timer_str,timer_rx_off,timer_tx_off,timer_configured,usb_can_send;
+volatile uint8_t bootloader_run;
+
+int processlocal;
+
+/**
+ SOMETHING BAD HAPPENED
+*/
+int sys_err(){
+ unsigned char i=0;
+
+ while(1) {
+ if (lt_100msec_expired(100)) {
+ i++;
+ if (i&1) {
+ SET_OUT_PIN(LED_PORT,LED_ERR);
+ } else {
+ CLR_OUT_PIN(LED_PORT,LED_ERR);
+ }
+ #ifdef WATCHDOG_ENABLED
+ watchdog_feed();
+ #endif /* WATCHDOG_ENABLED */
+ }
+ }
+}
+
+/**
+ Routine for visible LED blinking
+*/
+void timer_10ms(void)
+{
+ if (timer_tx_off!=0) timer_tx_off--;
+ else SET_OUT_PIN(LED_PORT,LED1_BIT);
+ if (timer_rx_off!=0) timer_rx_off--;
+ else SET_OUT_PIN(LED_PORT,LED2_BIT);
+
+/* if (timer_configured!=0) timer_configured--;
+ else {
+ timer_configured=20;
+ CLR_OUT_PIN(LED_PORT,LED1_BIT);
+ CLR_OUT_PIN(LED_PORT,LED2_BIT);
+ timer_rx_off=timer_tx_off=5;
+ }*/
+}
+
+/***********************************/
+int main(void)
+{
+// volatile int i=0;
+ bootloader_run=0;
+ /***********************************/
+ lt_10msec_init();
+ lt_100msec_init();
+ lt_2sec_init();
+
+ SET_OUT_PIN(LED_PORT,LED_ERR);
+ CLR_OUT_PIN(LED_PORT,LED_GP);
+
+ //********************
+ // USB init
+ memset( &usb_device, 0, sizeof( usb_device));
+ usb_device.id = 1;
+ usb_device.init = usb_lpc_init;
+ usb_debug_set_level(DEBUG_LEVEL_NONE);
+ usb_device.cntep = 3;
+ usb_device.ep = eps;
+
+ eps[0].max_packet_size = USB_MAX_PACKET;
+ eps[1].max_packet_size = USB_MAX_PACKET;
+ eps[0].epnum = 0x01;
+ eps[1].epnum = 0x81;
+ eps[0].event_mask = 0x04;
+ eps[1].event_mask = 0x08;
+ eps[0].udev = &usb_device;
+ eps[1].udev = &usb_device;
+
+// usb_device.vendor_fnc=usb_loader;
+
+ usb_init(&usb_device);
+ usb_connect(&usb_device);
+
+ can_init();
+ usb_can_send=1;
+
+ /********************/
+ // start
+ timer_rx_off=timer_tx_off=timer_str=timer_configured=0;
+ while (1) {
+
+ usb_check_events(&usb_device);
+ usb_control_response(&usb_device);
+
+ if ((usb_device.ep_events & MASK_EP1RX)&&(usb_can_send)) { //EP1RX
+ int size;
+ uint8_t *data;
+ uint8_t val;
+ size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,USB_MAX_PACKET);
+ data=(uint8_t *)ep1_rx_buff;
+ if (size==2){
+ if (((*data)&CAN_OP_MASK)==CAN_OP_READ){ // Get data from CAN device and return to caller
+ can_read((*data) & 0x7F,&val);
+ *(data+1)=val;
+ usb_udev_write_endpoint(&eps[1],(unsigned char *)data,size);
+ timer_rx_off=50; //rosviceni diody pri prijmu
+ CLR_OUT_PIN(LED_PORT,LED2_BIT);
+ usb_can_send=0;
+ }
+ if (((*data)&CAN_OP_MASK)==CAN_OP_WRITE){ // Save data to CAN device
+ can_write((*data)&(~CAN_OP_MASK),data+1);
+ timer_tx_off=50; //rozsviceni diod pri vysilani
+ CLR_OUT_PIN(LED_PORT,LED1_BIT);
+ }
+ }
+ usb_device.ep_events &= ~MASK_EP1RX;
+ }
+
+ if(usb_device.ep_events & MASK_EP1TX){
+ usb_can_send=1;
+ usb_device.ep_events &= ~MASK_EP1TX;
+ }
+
+#ifdef WATCHDOG_ENABLED
+ watchdog_feed();
+#endif /* WATCHDOG_ENABLED */
+
+ /* 10ms timer */
+ if (lt_10msec_expired(10))
+ timer_10ms();
+ }
+
+ SET_OUT_PIN(LED_PORT,LED_GP);
+ SET_OUT_PIN(LED_PORT,LED_ERR);
+
+ /* unreachable code */
+#ifdef SDCC
+ vec_jmp(0x0); /* need to call a function from misc to correct linking */
+#endif
+ return 0;
+}