int main(void) { SetupHardware(); GlobalInterruptEnable(); sei(); for (;;) { if (tx_ticks > 0) { tx_ticks--; } else if (tx_ticks == 0) { LEDs_TurnOnLEDs(LEDS_LED2); // on = off? } if (rx_ticks > 0) { rx_ticks--; } else if (rx_ticks == 0) { LEDs_TurnOnLEDs(LEDS_LED1); // on = off? } MIDI_To_Arduino(); MIDI_To_Host(); USB_USBTask(); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer); RingBuffer_InitBuffer(&USARTtoUSB_Buffer); sei(); for (;;) { /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */ RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if ((TIFR0 & (1 << TOV0)) || (BufferCount > BUFFER_NEARLY_FULL)) { TIFR0 |= (1 << TOV0); if (USARTtoUSB_Buffer.Count) { LEDs_TurnOnLEDs(LEDMASK_TX); PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; } /* Read bytes from the USART receive buffer into the USB IN endpoint */ while (BufferCount--) CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer)); /* Turn off TX LED(s) once the TX pulse period has elapsed */ if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); /* Turn off RX LED(s) once the RX pulse period has elapsed */ if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); } /* Load the next byte from the USART transmit buffer into the USART */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); LEDs_TurnOnLEDs(LEDMASK_RX); PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS; } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
void sendCDCbyte(uint8_t b){ //TODO improve this // try to send until sucess while (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, b) != ENDPOINT_READYWAIT_NoError){ CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); while (1){ // TODO remove this freezing loop! LEDs_TurnOnLEDs(LEDS_ERR); _delay_ms(100); LEDs_TurnOnLEDs(LEDS_ERR); _delay_ms(100); } } }
int main(void) { uint_reg_t Dummy; /* ============================= * Buttons Compile Check * ============================= */ // cppcheck-suppress redundantAssignment Dummy = BUTTONS_BUTTON1; Buttons_Init(); // cppcheck-suppress redundantAssignment Dummy = Buttons_GetStatus(); Buttons_Disable(); /* ============================= * Dataflash Compile Check * ============================= */ // cppcheck-suppress redundantAssignment Dummy = DATAFLASH_TOTALCHIPS + DATAFLASH_NO_CHIP + DATAFLASH_CHIP1 + DATAFLASH_PAGE_SIZE + DATAFLASH_PAGES; Dataflash_Init(); Dataflash_TransferByte(0); Dataflash_SendByte(0); // cppcheck-suppress redundantAssignment Dummy = Dataflash_ReceiveByte(); // cppcheck-suppress redundantAssignment Dummy = Dataflash_GetSelectedChip(); Dataflash_SelectChip(DATAFLASH_CHIP1); Dataflash_DeselectChip(); Dataflash_SelectChipFromPage(0); Dataflash_ToggleSelectedChipCS(); Dataflash_WaitWhileBusy(); Dataflash_SendAddressBytes(0, 0); /* ============================= * LEDs Compile Check * ============================= */ // cppcheck-suppress redundantAssignment Dummy = LEDS_LED1 + LEDS_LED2 + LEDS_LED3 + LEDS_LED4; LEDs_Init(); LEDs_TurnOnLEDs(LEDS_ALL_LEDS); LEDs_TurnOffLEDs(LEDS_ALL_LEDS); LEDs_SetAllLEDs(LEDS_ALL_LEDS); LEDs_ChangeLEDs(LEDS_ALL_LEDS, LEDS_NO_LEDS); LEDs_ToggleLEDs(LEDS_ALL_LEDS); // cppcheck-suppress redundantAssignment Dummy = LEDs_GetLEDs(); LEDs_Disable(); /* ============================= * Joystick Compile Check * ============================= */ // cppcheck-suppress redundantAssignment Dummy = JOY_LEFT + JOY_RIGHT + JOY_UP + JOY_DOWN + JOY_PRESS; Joystick_Init(); // cppcheck-suppress redundantAssignment Dummy = Joystick_GetStatus(); Joystick_Disable(); (void)Dummy; }
void MIDI_OUT(void) { if (complete == 1) { complete = 0; LEDs_TurnOnLEDs(LEDMASK_TX); uint8_t Channel = 0; MIDI_EventPacket_t MIDIEvent = (MIDI_EventPacket_t) { .CableNumber = 0, .Command = MIDI_FROM_ARDUINO.Data1 >> 4, .Data1 = MIDI_FROM_ARDUINO.Data1 | Channel, .Data2 = MIDI_FROM_ARDUINO.Data2, .Data3 = MIDI_FROM_ARDUINO.Data3, }; MIDI_Device_SendEventPacket(&MIDI_Interface, &MIDIEvent); MIDI_Device_Flush(&MIDI_Interface); } else {
int main(void) { /* Disable watchdog */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); LEDs_Init(); usb_init(); sei(); char c; for (;;) { c = getchar(); switch (c) { case '1': printf("ok\r\n"); LEDs_TurnOnLEDs(LEDS_LED1); break; case '0': printf("ok\r\n"); LEDs_TurnOffLEDs(LEDS_LED1); break; default: printf("error\r\n"); } } }
/** HID class driver callback function for the creation of HID reports to the host. * * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID * \param[in] ReportType Type of the report to create, either REPORT_ITEM_TYPE_In or REPORT_ITEM_TYPE_Feature * \param[out] ReportData Pointer to a buffer where the created report should be stored * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent * * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent */ bool CALLBACK_HID_Device_CreateHIDReport( USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize) { USB_JoystickReport_Data_t *reportp = (USB_JoystickReport_Data_t*)ReportData; RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); /* If there's a new report from the Arduino, copy it in and send that. * If not then the last report is sent again. */ if (BufferCount >= sizeof(joyReport)) { uint8_t ind; for (ind=0; ind<sizeof(joyReport); ind++) { ((uint8_t *)&joyReport)[ind] = RingBuffer_Remove(&USARTtoUSB_Buffer); } LEDs_TurnOnLEDs(LEDS_LED1); led1_ticks = LED_ON_TICKS; } *reportp = joyReport; *ReportSize = sizeof(joyReport); return false; }
void forceHardReset(void) { LEDs_TurnOnLEDs(LEDS_ALL_LEDS); cli(); // disable interrupts wdt_enable(WDTO_15MS); // enable watchdog while(1); // wait for watchdog to reset processor }
int main(void) { uint_reg_t Dummy; /* ============================= * Buttons Compile Check * ============================= */ Buttons_Init(); // cppcheck-suppress redundantAssignment Dummy = Buttons_GetStatus(); Buttons_Disable(); /* ============================= * Dataflash Compile Check * ============================= */ Dataflash_Init(); Dataflash_TransferByte(0); Dataflash_SendByte(0); // cppcheck-suppress redundantAssignment Dummy = Dataflash_ReceiveByte(); // cppcheck-suppress redundantAssignment Dummy = Dataflash_GetSelectedChip(); Dataflash_SelectChip(0); Dataflash_DeselectChip(); Dataflash_SelectChipFromPage(0); Dataflash_ToggleSelectedChipCS(); Dataflash_WaitWhileBusy(); Dataflash_SendAddressBytes(0, 0); /* ============================= * LEDs Compile Check * ============================= */ LEDs_Init(); LEDs_TurnOnLEDs(LEDS_ALL_LEDS); LEDs_TurnOffLEDs(LEDS_ALL_LEDS); LEDs_SetAllLEDs(LEDS_ALL_LEDS); LEDs_ChangeLEDs(LEDS_ALL_LEDS, LEDS_NO_LEDS); LEDs_ToggleLEDs(LEDS_ALL_LEDS); // cppcheck-suppress redundantAssignment Dummy = LEDs_GetLEDs(); LEDs_Disable(); /* ============================= * Joystick Compile Check * ============================= */ Joystick_Init(); // cppcheck-suppress redundantAssignment Dummy = Joystick_GetStatus(); Joystick_Disable(); (void)Dummy; }
// write (length) bytes, (start) is a byte address uint8_t write_eeprom_chunk(int start, int length) { // this writes byte-by-byte, // page writing may be faster (4 bytes at a time) fill(length); LEDs_TurnOffLEDs(LEDS_PMODE); for (int x = 0; x < length; x++) { //TODO check here if we can use intern address int addr = start + x; accessData(0xC0, addr, ram.RingBuffer_Data[x]); _delay_ms(45); } LEDs_TurnOnLEDs(LEDS_PMODE); return STK_OK; }
void MIDI_IN(void) { MIDI_EventPacket_t ReceivedMIDIEvent; if (MIDI_Device_ReceiveEventPacket(&MIDI_Interface, &ReceivedMIDIEvent)) { LEDs_TurnOnLEDs(LEDMASK_RX); Serial_TxByte(ReceivedMIDIEvent.Data1); Serial_TxByte(ReceivedMIDIEvent.Data2); Serial_TxByte(ReceivedMIDIEvent.Data3); } else { LEDs_TurnOffLEDs(LEDMASK_RX); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { MIDI_EventPacket_t midiEvent; struct { uint8_t command; uint8_t channel; uint8_t data2; uint8_t data3; } midiMsg; int ind; int led1_ticks = 0; int led2_ticks = 0; SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer); RingBuffer_InitBuffer(&USARTtoUSB_Buffer); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); sei(); for (;;) { RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); /* See if we have a message yet */ if (BufferCount >= 4) { /* Read in the message from the serial buffer */ for (ind=0; ind<4; ind++) { ((uint8_t *)&midiMsg)[ind] = RingBuffer_Remove(&USARTtoUSB_Buffer); } /* Build a midi event to send via USB */ midiEvent.CableNumber = 0; midiEvent.Command = midiMsg.command >> 4; midiEvent.Data1 = (midiMsg.command & 0xF0) | ((midiMsg.channel-1) & 0x0F); midiEvent.Data2 = midiMsg.data2; midiEvent.Data3 = midiMsg.data3; MIDI_Device_SendEventPacket(&Keyboard_MIDI_Interface, &midiEvent); MIDI_Device_Flush(&Keyboard_MIDI_Interface); /* Turn on the TX led and starts its timer */ LEDs_TurnOnLEDs(LEDS_LED1); led1_ticks = LED_ON_TICKS; } /* Turn off the Tx LED when the tick count reaches zero */ if (led1_ticks) { led1_ticks--; if (led1_ticks == 0) { LEDs_TurnOffLEDs(LEDS_LED1); } } if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &midiEvent)) { RingBuff_Count_t count = RingBuffer_GetCount(&USBtoUSART_Buffer); /* Room to send a message? */ if ((BUFFER_SIZE - count) >= sizeof(midiMsg)) { midiMsg.command = midiEvent.Command << 4; midiMsg.channel = (midiEvent.Data1 & 0x0F) + 1; midiMsg.data2 = midiEvent.Data2; midiMsg.data3 = midiEvent.Data3; for (ind=0; ind<sizeof(midiMsg); ind++) { RingBuffer_Insert(&USBtoUSART_Buffer, ((uint8_t *)&midiMsg)[ind]); } /* Turn on the RX led and start its timer */ LEDs_TurnOnLEDs(LEDS_LED2); led2_ticks = LED_ON_TICKS; } else { /* Turn on the RX led and leave it on to indicate the * buffer is full and the sketch is not reading it * fast enough. */ LEDs_TurnOnLEDs(LEDS_LED2); } /* if there's no room in the serial buffer the message gets dropped */ } /* Turn off the RX LED when the tick count reaches zero */ if (led2_ticks) { led2_ticks--; if (led2_ticks == 0) { LEDs_TurnOffLEDs(LEDS_LED2); } } /* any data to send to main processor? */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); } MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); sei(); for (;;) { /* Echo bytes from the host to the target via the hardware USART */ if ((UCSR1A & (1 << UDRE1)) && CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)) { UDR1 = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); LEDs_TurnOnLEDs(LEDMASK_TX); PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; } /* Check if the millisecond timer has elapsed */ if (TIFR0 & (1 << OCF0A)) { /* Clear flush timer expiry flag */ TIFR0 |= (1 << TOV0); /* Check if the reset pulse period has elapsed, if so tristate the target reset line */ if (PulseMSRemaining.ResetPulse && !(--PulseMSRemaining.ResetPulse)) { LEDs_TurnOffLEDs(LEDMASK_BUSY); AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK; } /* Check if the LEDs should be ping-ponging (during enumeration) */ if (PulseMSRemaining.PingPongLEDPulse && !(--PulseMSRemaining.PingPongLEDPulse)) { LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX); PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS; } /* Turn off TX LED(s) once the TX pulse period has elapsed */ if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); /* Turn off RX LED(s) once the RX pulse period has elapsed */ if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); /* Check if the receive buffer flush period has expired */ uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if (!(--FlushPeriodRemaining) || (BufferCount > 200)) { FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS; /* Start RX LED indicator pulse */ if (BufferCount) { LEDs_TurnOnLEDs(LEDMASK_RX); PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS; } /* Echo bytes from the target to the host via the virtual serial port */ while (BufferCount--) { /* Try to send the next byte of data to the host, abort if there is an error without dequeuing */ if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { break; } /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ RingBuffer_Remove(&USARTtoUSB_Buffer); } } } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
void avrisp(int ReceivedByte){ // is pmode active? if (ram.isp.pmode) LEDs_TurnOnLEDs(LEDS_PMODE); else LEDs_TurnOffLEDs(LEDS_PMODE); // is there an error? if (ram.isp.error) LEDs_TurnOnLEDs(LEDS_ERR); else LEDs_TurnOffLEDs(LEDS_ERR); // read in bytes from the CDC interface if (!(ReceivedByte < 0)){ switch (ReceivedByte) { case STK_GET_SYNC: ram.isp.error = 0; replyOK(); break; case STK_GET_SIGNON: if (getch() == CRC_EOP) { sendCDCbyte(STK_INSYNC); sendCDCbyte('A'); sendCDCbyte('V'); sendCDCbyte('R'); sendCDCbyte(' '); sendCDCbyte('I'); sendCDCbyte('S'); sendCDCbyte('P'); sendCDCbyte(STK_OK); } break; case STK_GET_PARM: get_parameters(getch()); break; case STK_SET_PARM: set_parameters(); replyOK(); break; case STK_SET_PARM_EXT: // extended parameters - ignore for now fill(5); replyOK(); break; case STK_PMODE_START: start_pmode(); replyOK(); break; case STK_SET_ADDR: ram.isp._addr = getch(); ram.isp._addr += 256 * getch(); replyOK(); break; case STK_PROG_FLASH: //uint8_t low = getch(); getch(); //uint8_t high = getch(); getch(); replyOK(); break; case STK_PROG_DATA: //uint8_t data = getch(); getch(); replyOK(); break; case STK_PROG_PAGE: program_page(); break; case STK_READ_PAGE: read_page(); break; case STK_UNIVERSAL: universal(); break; case STK_PMODE_END: ram.isp.error = 0; end_pmode(); replyOK(); break; case STK_READ_SIGN: read_signature(); break; // expecting a command, not CRC_EOP // this is how we can get back in sync case CRC_EOP: ram.isp.error++; sendCDCbyte(STK_NOSYNC); break; // anything else we will return STK_UNKNOWN default: ram.isp.error++; if (CRC_EOP == getch()) sendCDCbyte(STK_UNKNOWN); else sendCDCbyte(STK_NOSYNC); } } }
// Main int main(void) { setupHardware(); LEDs_TurnOnLEDs(LEDMASK_USB_NOTREADY); sei(); while(1) { // Perform the HID specific tasks hidTask(); // Perform the USB library tasks USB_USBTask(); // Show command processing activity on led 1 (Rx) if (activityIndicatorRx > 0) { // See if this is the first loop if (activityIndicatorRx == 1) LEDs_TurnOnLEDs(LEDS_LED1); activityIndicatorRx++; if (activityIndicatorRx > 5000) // Flash length is in loops (16 bit maximum) { // Turn the indicator off activityIndicatorRx = 0; LEDs_TurnOffLEDs(LEDS_LED1); } } // Show command processing activity on led 2 (Tx) if (activityIndicatorTx > 0) { // See if this is the first loop if (activityIndicatorTx == 1) LEDs_TurnOnLEDs(LEDS_LED2); activityIndicatorTx++; if (activityIndicatorTx > 5000) // Flash length is in loops (16 bit maximum) { // Turn the indicator off activityIndicatorTx = 0; LEDs_TurnOffLEDs(LEDS_LED2); } } // Show flashing for system/blink command on led 3 (L) if (activityIndicatorL > 0) { // See if this is the first loop if (activityIndicatorL == 1) { LEDs_TurnOnLEDs(LEDS_LED3); activityIndicatorLblink = 0; } // Increment the activity counters activityIndicatorL++; activityIndicatorLblink++; // Control the flashing of the LED if (activityIndicatorLblink == 5000) LEDs_TurnOffLEDs(LEDS_LED3); if (activityIndicatorLblink == 10000) { LEDs_TurnOnLEDs(LEDS_LED3); activityIndicatorLblink = 0; } if (activityIndicatorL > 200000) // Flash length is in loops (32 bit maximum) { // Turn the indicator off activityIndicatorL = 0; // Default state of the L LED is on LEDs_TurnOnLEDs(LEDS_LED3); } } } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); GlobalInterruptEnable(); for (;;) { /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); /* Store received byte into the USART transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if (BufferCount) { Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address); /* Check if a packet is already enqueued to the host - if so, we shouldn't try to send more data * until it completes as there is a chance nothing is listening and a lengthy timeout could occur */ if (Endpoint_IsINReady()) { LEDs_TurnOnLEDs(LEDMASK_TX); PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; /* Never send more than one bank size less one byte to the host at a time, so that we don't block * while a Zero Length Packet (ZLP) to terminate the transfer is sent if the host isn't listening */ uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1)); /* Read bytes from the USART receive buffer into the USB IN endpoint */ while (BytesToSend--) { /* Try to send the next byte of data to the host, abort if there is an error without dequeuing */ if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { break; } /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ RingBuffer_Remove(&USARTtoUSB_Buffer); } } } /* Load the next byte from the USART transmit buffer into the USART if transmit buffer space is available */ if (Serial_IsSendReady() && !(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer)); LEDs_TurnOnLEDs(LEDMASK_RX); PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS; } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); /* Turn off TX LED(s) once the TX pulse period has elapsed */ if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); /* Turn off RX LED(s) once the RX pulse period has elapsed */ if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); } }
int main(void) { unsigned char tmp; wdt_disable(); /* Disable watchdog if enabled by bootloader/fuses */ // Buttons_Init(); LEDs_Init(); clock_prescale_set(clock_div_1); // Timer 0 setup for simple count mode, used as timebase for IR-Signal capture TCCR0A = 0; // simple count more TCCR0B = 5; // system Clock 16Mhz/1024 = 64us per counter tick TIMSK0 = 0x1; // Enable overflow interrupt enable TIFR0 = 0; // clear Overflow interrupt flag before enabling interrupts EIFR =0 ; // clear external interrupt register DDRD = 0; // all inputs DDRC = 0; // all inputs PORTD = 0x80; // pull-up on PD0 MCUSR =0; // Timer 1 setup for fast PWM mode for servo pulse generation 1 to 2ms DDRB = 0x81; // PB7 = output OC1_C, PB0 =LED TIMSK1 = 0; // timer 1 running with no interrupts TCCR1A = 0x0F; // use OC1_C pin for servo pulse TCCR1B = 0x0C; // Fast PWM 10bit, clock/256 OCR1C = SERVO_POS_RIGHT; EIFR = 0 ; // clear external interrupt flag EICRA = 0x3; // set interrupt to raising edge trigger EIMSK = 0x01; // enable INT0 (pin pb0 alternate function) sei(); //enable external interrupts // set initial state Signal_captured = 1; IR_code = VIZIO_GREEN_BTN; while(1) { /*// for manual toggling via HWB button if (IsButtonPressed(BUTTONS_BUTTON1) ) { // de-bounce -- 5 consecutive reads of switch open for (tmp =0; tmp<=5; tmp++) { _delay_ms(1); if (IsButtonPressed(BUTTONS_BUTTON1)) tmp = 0; } if (IR_code == VIZIO_GREEN_BTN ) IR_code = VIZIO_RED_BTN; else IR_code = VIZIO_GREEN_BTN; Signal_captured = 1; } */ if ( Signal_captured ) { switch (IR_code) { case VIZIO_GREEN_BTN: // Vizio Code Green button LEDs_TurnOnLEDs(LEDS_LED1); OCR1C = SERVO_POS_LEFT; break; case VIZIO_RED_BTN: // Vizio Code Red button LEDs_TurnOffLEDs(LEDS_LED1); OCR1C = SERVO_POS_RIGHT; break; } Signal_captured = 0; } } }
void commit(int addr) { LEDs_TurnOffLEDs(LEDS_PMODE); accessData(0x4C, addr, 0x00); _delay_ms(30); LEDs_TurnOnLEDs(LEDS_PMODE); }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer); RingBuffer_InitBuffer(&USARTtoUSB_Buffer); setupEeprom(); sei(); for (;;) { // Only try to read in bytes from the CDC interface if the transmit buffer is not full if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); // Read bytes from the USB OUT endpoint into the USART transmit buffer if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } // Check if the UART receive buffer flush timer has expired or the buffer is nearly full RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if ((TIFR0 & (1 << TOV0)) || (BufferCount > BUFFER_NEARLY_FULL)) { TIFR0 |= (1 << TOV0); if (USARTtoUSB_Buffer.Count) { LEDs_TurnOnLEDs(LEDMASK_TX); PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; } // Read bytes from the USART receive buffer into the USB IN endpoint while (BufferCount--) CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer)); // Turn off TX LED(s) once the TX pulse period has elapsed if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); // Turn off RX LED(s) once the RX pulse period has elapsed if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); if (ResetTimer > 0) { // SAM3X RESET/ERASE Sequence // -------------------------- if (ResetTimer == 95) { eepromInterruptDisable(); setErasePin(true); setResetPin(false); } if (ResetTimer == 35) { eepromInterruptDisable(); setErasePin(false); setResetPin(false); } if (ResetTimer == 25) { eepromInterruptDisable(); setErasePin(false); setResetPin(true); } if (ResetTimer == 1) { eepromInterruptDisable(); setErasePin(false); setResetPin(false); } ResetTimer--; } else { setErasePin(false); setResetPin(false); eepromInterruptEnable(); } } // Load the next byte from the USART transmit buffer into the USART if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); LEDs_TurnOnLEDs(LEDMASK_RX); PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS; } // CDC_Device_USBTask(&VirtualSerial_CDC_Interface); // USB_USBTask(); serviceEepromRequest(); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&Tx_Buffer); sei(); for (;;) { /* Echo bytes from the host to the target via the hardware USART */ int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); if (!(ReceivedByte < 0) && (UCSR1A & (1 << UDRE1))) { UDR1 = ReceivedByte; LEDs_TurnOnLEDs(LEDMASK_TX); PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; } /* Check if the millisecond timer has elapsed */ if (TIFR0 & (1 << OCF0A)) { /* Check if the reset pulse period has elapsed, if so tristate the target reset line */ if (PulseMSRemaining.ResetPulse && !(--PulseMSRemaining.ResetPulse)) { LEDs_TurnOffLEDs(LEDMASK_BUSY); AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK; } /* Check if the LEDs should be ping-ponging (during enumeration) */ if (PulseMSRemaining.PingPongLEDPulse && !(--PulseMSRemaining.PingPongLEDPulse)) { LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX); PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS; } /* Turn off TX LED(s) once the TX pulse period has elapsed */ if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); /* Turn off RX LED(s) once the RX pulse period has elapsed */ if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); /* Check if the receive buffer flush period has expired */ RingBuff_Count_t BufferCount = RingBuffer_GetCount(&Tx_Buffer); if (!(--FlushPeriodRemaining) || (BufferCount > 200)) { /* Echo bytes from the target to the host via the virtual serial port */ if (BufferCount) { while (BufferCount--) CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&Tx_Buffer)); LEDs_TurnOnLEDs(LEDMASK_RX); PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS; } FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS; } /* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */ TIFR0 |= (1 << OCF0A); } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
/** HID class driver callback function for the creation of HID reports to the host. * * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID * \param[in] ReportType Type of the report to create, either REPORT_ITEM_TYPE_In or REPORT_ITEM_TYPE_Feature * \param[out] ReportData Pointer to a buffer where the created report should be stored * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent * * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent */ bool CALLBACK_HID_Device_CreateHIDReport( USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize) { if (ReportData != NULL && ReportSize != NULL) { if (ReportType == HID_REPORT_ITEM_Feature && *ReportID == 0x44) { uint8_t *reportp = ReportData; uint8_t id = *ReportID; *reportp++ = id; memcpy(reportp, TouchBlob, sizeof(TouchBlob)); *ReportSize = sizeof(TouchBlob) + 1; return true; } else if (ReportType == HID_REPORT_ITEM_Feature) { USB_SurfaceReport_Feature_t *reportp = (USB_SurfaceReport_Feature_t*)ReportData; uint8_t maximumContactCount; maximumContactCount = 10; uint8_t id = *ReportID; reportp->ReportId = id; reportp->MaximumContactCount = maximumContactCount; *ReportSize = sizeof(USB_SurfaceReport_Feature_t); return true; } else { USB_SurfaceReport_Data_t *reportp = (USB_SurfaceReport_Data_t*)ReportData; RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); uint8_t reportId = 1; if (BufferCount >= SURFACE_REPORT_SIZE) { uint8_t *ptr = ReportData; uint8_t TipSwitch = RingBuffer_Remove(&USARTtoUSB_Buffer); uint8_t ContactId = RingBuffer_Remove(&USARTtoUSB_Buffer); uint16_t X = RingBuffer_Remove(&USARTtoUSB_Buffer) | (RingBuffer_Remove(&USARTtoUSB_Buffer) << 8); uint16_t Y = RingBuffer_Remove(&USARTtoUSB_Buffer) | (RingBuffer_Remove(&USARTtoUSB_Buffer) << 8); uint16_t ScanTime = RingBuffer_Remove(&USARTtoUSB_Buffer) | (RingBuffer_Remove(&USARTtoUSB_Buffer) << 8); uint8_t ContactCount = RingBuffer_Remove(&USARTtoUSB_Buffer); /* *ptr++ = reportId; *ptr++ = TipSwitch; *ptr++ = ContactId; *ptr++ = (uint8_t)(X & 0xff); *ptr++ = (uint8_t)((X >> 8) & 0xff); *ptr++ = (uint8_t)(Y & 0xff); *ptr++ = (uint8_t)((Y >> 8) & 0xff); *ptr++ = (uint8_t)(ScanTime & 0xff); *ptr++ = (uint8_t)((ScanTime >> 8) & 0xff); *ptr++ = ContactCount; */ reportp->ReportId = reportId; reportp->TipSwitch = (TipSwitch > 0) ? 0xff : 0 ; reportp->ContactId = ContactId; reportp->ScanTime = ScanTime; reportp->X = X; reportp->Y = Y; reportp->ContactCount = ContactCount; LEDs_TurnOnLEDs(LEDS_LED1); led1_ticks = LED_ON_TICKS; } else { reportp->ReportId = reportId; reportp->TipSwitch = 0; reportp->ContactId = 0; reportp->ScanTime = 0; reportp->X = 0; reportp->Y = 0; reportp->ContactCount = 0; *ReportSize = 0; return false; } *ReportSize = sizeof(USB_SurfaceReport_Data_t); return true; } *ReportSize = 0; } return false; }