void main(void) { char x=10; char y=10; CLK->CKDIVR = 0; disableInterrupts(); Init_GPIO(); Init_TIM1(); Init_Clock(); usb_init(); enableInterrupts(); while(usb_ready == 0) { usb_process(); } while(1) { delay(100); if(get_random_byte()>127) { x=-x; y=-y; } data_buffer[0] = 0x00; data_buffer[1] = x; data_buffer[2] = y; data_buffer[3] = 0x00; usb_send_data(&data_buffer[0], 4, 0); } }
uint8_t uart_isdata(void) { if (usb_rxpacket_leftb) { Endpoint_SelectEndpoint(CDC_RX_EPNUM); return usb_rxpacket_leftb; } usb_process(); usb_rxpacket_leftb = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); return usb_rxpacket_leftb; }
uint8_t uart_send_getfree(void) { if (usb_txpacket_leftb) { Endpoint_SelectEndpoint(CDC_TX_EPNUM); return usb_txpacket_leftb-1; } do { usb_process(); if (CDC_Device_SendByte_Prep(&VirtualSerial_CDC_Interface) == 0) { usb_txpacket_leftb = CDC_IN_EPSIZE; return usb_txpacket_leftb-1; } } while (1); }
void uart_send(uint8_t d) { do { if (usb_txpacket_leftb) { Endpoint_SelectEndpoint(CDC_TX_EPNUM); Endpoint_Write_Byte(d); usb_txpacket_leftb--; if (usb_txpacket_leftb == 1) { Endpoint_ClearIN(); /* Go data, GO. */ usb_txpacket_leftb = 0; } return; } usb_process(); if (CDC_Device_SendByte_Prep(&VirtualSerial_CDC_Interface) == 0) usb_txpacket_leftb = CDC_IN_EPSIZE; } while (1); }
uint8_t uart_recv(void) { do { if (usb_rxpacket_leftb) { uint8_t d; Endpoint_SelectEndpoint(CDC_RX_EPNUM); d = Endpoint_Read_Byte(); usb_rxpacket_leftb--; if (!usb_rxpacket_leftb) Endpoint_ClearOUT(); return d; } usb_process(); usb_rxpacket_leftb = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); if ((!usb_rxpacket_leftb)&&(usb_txpacket_leftb)) { Endpoint_SelectEndpoint(CDC_TX_EPNUM); Endpoint_ClearIN(); /* Go data, GO. */ usb_txpacket_leftb = 0; } } while (1); }
int main(int argc, char **argv) { signal(SIGHUP, sighandler); signal(SIGINT, sighandler); signal(SIGTERM, sighandler); signal(SIGPIPE, SIG_IGN); // Setting default file permissions to o+rw umask(~(S_IRUSR | S_IWUSR)); // Turning off buffering setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); processcommandline(argc, argv); if (!config_init(configfilename)) return 1; daemon_poll_init(); if (!usb_init()) return 2; while (!nai_flags.sigexit) { if (!usb_process()) break; if (!daemon_poll_process()) break; if (!nai_process()) break; } usb_deinit(); daemon_poll_deinit(); config_deinit(); printf("main: exiting.\n"); return 0; }
static int main_loop(int listenfd) { int to, cnt, i, dto; struct fdlist pollfds; struct timespec tspec; sigset_t empty_sigset; sigemptyset(&empty_sigset); // unmask all signals fdlist_create(&pollfds); while(!should_exit) { usbmuxd_log(LL_FLOOD, "main_loop iteration"); to = usb_get_timeout(); usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); dto = device_get_timeout(); usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", dto); if(dto < to) to = dto; fdlist_reset(&pollfds); fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); usb_get_fds(&pollfds); client_get_fds(&pollfds); usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); tspec.tv_sec = to / 1000; tspec.tv_nsec = (to % 1000) * 1000000; cnt = ppoll(pollfds.fds, pollfds.count, &tspec, &empty_sigset); usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); if(cnt == -1) { if(errno == EINTR) { if(should_exit) { usbmuxd_log(LL_INFO, "Event processing interrupted"); break; } if(should_discover) { should_discover = 0; usbmuxd_log(LL_INFO, "Device discovery triggered"); usb_discover(); } } } else if(cnt == 0) { if(usb_process() < 0) { usbmuxd_log(LL_FATAL, "usb_process() failed"); fdlist_free(&pollfds); return -1; } device_check_timeouts(); } else { int done_usb = 0; for(i=0; i<pollfds.count; i++) { if(pollfds.fds[i].revents) { if(!done_usb && pollfds.owners[i] == FD_USB) { if(usb_process() < 0) { usbmuxd_log(LL_FATAL, "usb_process() failed"); fdlist_free(&pollfds); return -1; } done_usb = 1; } if(pollfds.owners[i] == FD_LISTEN) { if(client_accept(listenfd) < 0) { usbmuxd_log(LL_FATAL, "client_accept() failed"); fdlist_free(&pollfds); return -1; } } if(pollfds.owners[i] == FD_CLIENT) { client_process(pollfds.fds[i].fd, pollfds.fds[i].revents); } } } } } fdlist_free(&pollfds); return 0; }
/** * Main function. Entry point for USBtin application. * Handles initialization and the the main processing loop. */ void main(void) { // initialize MCP2515 (reset and clock setup) mcp2515_init(); // switch (back) to external clock (if fail-safe-monitor switched to internal) OSCCON = 0x30; // disable all analog pin functions, set led pin to output ANSEL = 0; ANSELH = 0; TRISBbits.TRISB5 = 0; hardware_setLED(0); // initialize modules clock_init(); usb_init(); char line[LINE_MAXLEN]; unsigned char linepos = 0; unsigned short lastclock = 0; while (1) { // do module processing usb_process(); clock_process(); // receive characters from UART and collect the data until end of line is indicated if (usb_chReceived()) { unsigned char ch = usb_getch(); if (ch == CR) { line[linepos] = 0; parseLine(line); linepos = 0; } else if (ch != LR) { line[linepos] = ch; if (linepos < LINE_MAXLEN - 1) linepos++; } } // handles interrupt requests of MCP2515 controller: receive message and print it out. if ((state != STATE_CONFIG) && (hardware_getMCP2515Int())) { canmsg_t canmsg; mcp2515_receive_message(&canmsg); char type; unsigned char idlen; unsigned short timestamp = clock_getMS(); if (canmsg.flags.rtr) type = 'r'; else type = 't'; if (canmsg.flags.extended) { type -= 'a' - 'A'; idlen = 8; } else { idlen = 3; } usb_putch(type); sendHex(canmsg.id, idlen); sendHex(canmsg.length, 1); if (!canmsg.flags.rtr) { unsigned char i; for (i = 0; i < canmsg.length; i++) { sendHex(canmsg.data[i], 2); } } if (timestamping) { sendHex(timestamp, 4); } usb_putch(CR); } // led signaling hardware_setLED(state != STATE_CONFIG); // jump into bootloader, if jumper is closed if (hardware_getBLSwitch()) { UCON = 0; _delay(1000); RESET(); } } }