?? main.c
字號:
if(rq->bRequest == GET_LINE_CODING || rq->bRequest == SET_LINE_CODING){ requestType = REQUEST_TYPE_LINE_CODING; return 0xff; }# if USE_DCD_REPORTING if(rq->bRequest == SET_CONTROL_LINE_STATE){ /* Report serial state (carrier detect). On several Unix platforms, * tty devices can only be opened when carrier detect is set. */ intr3Status = 2; }# endif#endif } }#if ENABLE_DEBUG_INTERFACE else if(rqType == USBRQ_TYPE_VENDOR){ /* vendor requests */ if(rq->bRequest == 1){ /* transmit data */ serialPutc(rq->wValue.bytes[0]); }else if(rq->bRequest == 2){ requestType = REQUEST_TYPE_VENDOR; return 0xff; /* handle in usbFunctionRead() */ } }#endif return 0;}uchar usbFunctionRead(uchar *data, uchar len){ if(requestType == REQUEST_TYPE_LINE_CODING){#if ENABLE_CDC_INTERFACE /* return the "virtual" configuration */ memcpy(data, modeBuffer, 7); return 7;#endif }#if ENABLE_DEBUG_INTERFACE else if(requestType == REQUEST_TYPE_VENDOR){ uchar cnt; for(cnt = 0; cnt < len && ringBufferHasData(&serialRingBuffer); cnt++){ *data++ = ringBufferRead(&serialRingBuffer); } return cnt;#if ENABLE_HID_INTERFACE }else if(requestType == REQUEST_TYPE_HID_DEBUGDATA){ uchar *p = data, remaining; if(hidStatus == 5){ /* first call */ *p++ = hidStatus; /* report ID */ *p++ = ringBufferCount(&serialRingBuffer); remaining = len - 2; hidStatus = 1; /* continue with subsequent call */ }else{ remaining = len; } if(hidStatus){ do{ if(!ringBufferHasData(&serialRingBuffer)){ hidStatus = 0; break; } *p++ = ringBufferRead(&serialRingBuffer); }while(--remaining); } return len;#endif }#endif#if ENABLE_HID_INTERFACE else if(requestType == REQUEST_TYPE_HID_FIRST || requestType == REQUEST_TYPE_HID_SUBSEQUENT){ uchar *p = data, remaining; if(requestType == REQUEST_TYPE_HID_FIRST){ int cnt; *p++ = hidStatus; /* report ID */ cnt = stkGetTxCount(); if(utilHi8(cnt)){ *p = 255; }else{ *p = cnt; /* second byte is number of remaining bytes buffered */ } p++; remaining = len - 2; requestType = REQUEST_TYPE_HID_SUBSEQUENT; }else{ remaining = len; } if(hidStatus){ do{ int c = stkGetTxByte(); if(c < 0){ hidStatus = 0; break; } *p++ = c; }while(--remaining); } return len; }#endif return 0; /* error -> terminate transfer */}uchar usbFunctionWrite(uchar *data, uchar len){ if(requestType == REQUEST_TYPE_LINE_CODING){#if ENABLE_CDC_INTERFACE /* Don't know why data toggling is reset when line coding is changed, but it is... */ usbTxPacketCnt1 = 1; /* enforce DATA0 token for next transfer */ /* store the line configuration so that we can return it on request */ memcpy(modeBuffer, data, 7); return 1;#endif }#if ENABLE_HID_INTERFACE#if ENABLE_DEBUG_INTERFACE else if(requestType == REQUEST_TYPE_HID_DEBUGDATA){ uchar *p = data, rval = len != 8; if(hidStatus == 5){ /* first call */ hidStatus = -p[1]; /* second byte is data length */ p += 2; len -= 2; } do{ if(!hidStatus) break; serialPutc(*p++); hidStatus++; }while(--len); return rval; /* the last packet must have 7 bytes insted of 8 */ }#endif else if(requestType == REQUEST_TYPE_HID_FIRST || requestType == REQUEST_TYPE_HID_SUBSEQUENT){ uchar *p = data, rval = len != 8; if(requestType == REQUEST_TYPE_HID_FIRST){ hidStatus = p[1]; /* second byte is data length */ p += 2; len -= 2; requestType = REQUEST_TYPE_HID_SUBSEQUENT; } do{ if(!hidStatus) break; stkSetRxChar(*p++); hidStatus--; }while(--len); return rval; /* the last packet must have 7 bytes insted of 8 */ }#endif return 1; /* error -> accept everything until end */}void usbFunctionWriteOut(uchar *data, uchar len){#if ENABLE_CDC_INTERFACE do{ /* len must be at least 1 character, the driver discards zero sized packets */ stkSetRxChar(*data++); }while(--len);#endif}/* ------------------------------------------------------------------------- *//* ------------------------------------------------------------------------- *//* ------------------------------------------------------------------------- */static void readInterfaceType(void){#if ENABLE_HID_INTERFACE && ENABLE_CDC_INTERFACE PORT_DDR_SET(HWPIN_ISP_MOSI); PORT_PIN_SET(HWPIN_ISP_MOSI);// _delay_us(10); PORT_DDR_CLR(HWPIN_ISP_MOSI); PORT_PIN_CLR(HWPIN_ISP_MOSI); /* deactivate pullup */ useHIDInterface = PORT_PIN_VALUE(HWPIN_ISP_MOSI) == 0;#elif ENABLE_HID_INTERFACE useHIDInterface = 1;#elif !ENABLE_CDC_INTERFACE#error "You must set either ENABLE_HID_INTERFACE or ENABLE_CDC_INTERFACE in hardware.h!"#endif}/*20 pin HVSP / PP connector: 1 .... GND 2 .... Vtarget 3 .... HVSP SCI 4 .... RESET 16 ... HVSP SDO 18 ... HVSP SII 20 ... HVSP SDI * Timer usage: * Timer 0 [8 bit]: * 1/64 prescaler for timer interrupt * Timer 1 [16 bit]: * PWM for voltage supply -> fastPWM mode * f = 23.4 kHz -> prescaler = 1, 9 bit * Timer 2 [8 bit]: * Clock generation for target device */static void hardwareInit(void){uchar i, j;uchar portB = 0, portC = 0, portD = 0, ddrB = 0, ddrC = 0, ddrD = 0; UTIL_PBIT_SET(port, HWPIN_HVSP_SUPPLY); UTIL_PBIT_SET(ddr, HWPIN_HVSP_SUPPLY); UTIL_PBIT_CLR(port, HWPIN_SMPS_OUT); UTIL_PBIT_SET(ddr, HWPIN_SMPS_OUT); UTIL_PBIT_CLR(port, HWPIN_HVSP_HVRESET); UTIL_PBIT_SET(ddr, HWPIN_HVSP_HVRESET); UTIL_PBIT_CLR(port, HWPIN_HVSP_SCI); UTIL_PBIT_SET(ddr, HWPIN_HVSP_SCI); UTIL_PBIT_CLR(port, HWPIN_ISP_CLK); UTIL_PBIT_SET(ddr, HWPIN_ISP_CLK); UTIL_PBIT_CLR(port, HWPIN_ISP_DRIVER); UTIL_PBIT_SET(ddr, HWPIN_ISP_DRIVER); UTIL_PBIT_CLR(port, HWPIN_LED); UTIL_PBIT_SET(ddr, HWPIN_LED); UTIL_PBIT_CLR(port, HWPIN_ADC_SMPS); UTIL_PBIT_CLR(ddr, HWPIN_ADC_SMPS); UTIL_PBIT_CLR(port, HWPIN_ADC_VTARGET); UTIL_PBIT_CLR(ddr, HWPIN_ADC_VTARGET); UTIL_PBIT_CLR(port, HWPIN_ISP_SCK); UTIL_PBIT_SET(ddr, HWPIN_ISP_SCK); UTIL_PBIT_SET(port, HWPIN_ISP_MISO); UTIL_PBIT_CLR(ddr, HWPIN_ISP_MISO); UTIL_PBIT_CLR(port, HWPIN_ISP_MOSI); UTIL_PBIT_SET(ddr, HWPIN_ISP_MOSI); UTIL_PBIT_CLR(port, HWPIN_ISP_RESET); UTIL_PBIT_SET(ddr, HWPIN_ISP_RESET); UTIL_PBIT_SET(port, HWPIN_ISP_TXD); UTIL_PBIT_SET(ddr, HWPIN_ISP_TXD); UTIL_PBIT_SET(port, HWPIN_ISP_RXD); UTIL_PBIT_CLR(ddr, HWPIN_ISP_RXD); UTIL_PBIT_CLR(port, HWPIN_USB_DPLUS); UTIL_PBIT_SET(ddr, HWPIN_USB_DPLUS); UTIL_PBIT_CLR(port, HWPIN_USB_DMINUS); UTIL_PBIT_SET(ddr, HWPIN_USB_DMINUS); UTIL_PBIT_SET(port, HWPIN_JUMPER); UTIL_PBIT_CLR(ddr, HWPIN_JUMPER); UTIL_PBIT_CLR(port, HWPIN_HVSP_SII); UTIL_PBIT_CLR(port, HWPIN_HVSP_SDI); UTIL_PBIT_SET(ddr, HWPIN_HVSP_SDI); UTIL_PBIT_CLR(port, HWPIN_HVSP_SDO); UTIL_PBIT_CLR(ddr, HWPIN_HVSP_SDO); PORTB = portB; DDRB = ddrB; PORTC = portC; DDRC = ddrC; PORTD = portD; DDRD = ddrD; j = 0; while(--j){ /* USB Reset by device only required on Watchdog Reset */ i = 0; while(--i); /* delay >10ms for USB reset */ } UTIL_PBIT_CLR(ddr, HWPIN_USB_DPLUS); UTIL_PBIT_CLR(ddr, HWPIN_USB_DMINUS); DDRD = ddrD; /* timer 0 configuration: ~ 1.365 ms interrupt */ TCCR0 = 3; /* 1/64 prescaler */ TIMSK = (1 << TOIE0); /* enable timer0 overflow interrupt */ /* timer 1 configuration: ~23.4 kHz PWM (9 bit) */ TCCR1A = UTIL_BIN8(1000, 0010); /* OC1A = PWM, OC1B disconnected, 9 bit */ TCCR1B = UTIL_BIN8(0000, 1001); /* 9 bit, prescaler=1 */ OCR1A = 1; /* set duty cycle to minimum */ /* timer 2 configuration */ TCCR2 = UTIL_BIN8(0000, 1001); /* OC2 disconnected, prescaler=1 */ OCR2 = 2; /* should give 3 MHz clock */}int main(void){ wdt_enable(WDTO_1S); odDebugInit();#if ENABLE_DEBUG_INTERFACE serialInit();#endif readInterfaceType(); hardwareInit(); vregInit(); usbInit(); sei(); for(;;){ /* main event loop */ wdt_reset(); usbPoll();#if ENABLE_CDC_INTERFACE if(!useHIDInterface && usbInterruptIsReady()){ static uchar sendEmptyFrame = 1, buffer[8]; /* start with empty frame because the host eats the first packet -- don't know why... */ int c; uchar i = 0; while(i < 8 && (c = stkGetTxByte()) >= 0){ buffer[i++] = c; } if(i > 0 || sendEmptyFrame){ sendEmptyFrame = i; /* send an empty block after last data block to indicate transfer end */ usbSetInterrupt(buffer, i); } }#endif#if USE_DCD_REPORTING && ENABLE_CDC_INTERFACE /* We need to report rx and tx carrier after open attempt */ if(usbInterruptIsReady3() && intr3Status != 0){ static uchar serialStateNotification[8] = {0xa1, 0x20, 0, 0, 0, 0, 2, 0}; static uchar serialStateData[2] = {3, 0}; if(intr3Status == 2){ usbSetInterrupt3(serialStateNotification, 8); }else{ usbSetInterrupt3(serialStateData, 2); } intr3Status--; }#endif } return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -