Functional USB reset
parent
2b9c73dbd5
commit
4f6ea16515
|
|
@ -2,9 +2,17 @@
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include "../watchdog.h"
|
#include "../watchdog.h"
|
||||||
|
|
||||||
|
#include "lufa_config.h"
|
||||||
|
#include "Descriptors.h"
|
||||||
|
#include "lufa/LUFA/Drivers/USB/USB.h"
|
||||||
|
|
||||||
|
#include "../usart.h"
|
||||||
|
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace cpu {
|
namespace cpu {
|
||||||
|
|
||||||
|
bool resetPending = false;
|
||||||
|
|
||||||
void Init() {
|
void Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -15,5 +23,14 @@ void Reset() {
|
||||||
; //endless loop while waiting for the watchdog to reset
|
; //endless loop while waiting for the watchdog to reset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Step() {
|
||||||
|
if (resetPending) {
|
||||||
|
hal::usart::usart1.puts("resetPending\n");
|
||||||
|
USB_Detach();
|
||||||
|
for (;;)
|
||||||
|
; //endless loop while waiting for the watchdog to reset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace CPU
|
} // namespace CPU
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,12 @@ namespace cpu {
|
||||||
#define F_CPU (16000000ul)
|
#define F_CPU (16000000ul)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
extern bool resetPending;
|
||||||
|
|
||||||
/// CPU init routines (not really necessary for the AVR)
|
/// CPU init routines (not really necessary for the AVR)
|
||||||
void Init();
|
void Init();
|
||||||
void Reset();
|
void Reset();
|
||||||
|
void Step();
|
||||||
|
|
||||||
} // namespace cpu
|
} // namespace cpu
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,12 @@ public:
|
||||||
uint8_t prescalerBits = 0;
|
uint8_t prescalerBits = 0;
|
||||||
uint32_t ticks = 1;
|
uint32_t ticks = 1;
|
||||||
switch (timeout) {
|
switch (timeout) {
|
||||||
|
case 250:
|
||||||
|
prescalerBits = 4;
|
||||||
|
break;
|
||||||
case 8000:
|
case 8000:
|
||||||
prescalerBits = 9;
|
prescalerBits = 9;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration config = { prescalerBits, static_cast<uint16_t>(ticks - 1) };
|
configuration config = { prescalerBits, static_cast<uint16_t>(ticks - 1) };
|
||||||
|
|
|
||||||
47
src/main.cpp
47
src/main.cpp
|
|
@ -100,10 +100,10 @@ void EVENT_USB_Device_ConfigurationChanged(void) {
|
||||||
ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
|
ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
|
||||||
|
|
||||||
// LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
|
// LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
|
||||||
char str1[] = "ready\n";
|
// char str1[] = "ready\n";
|
||||||
char str0[] = "error\n";
|
// char str0[] = "error\n";
|
||||||
hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:");
|
// hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:");
|
||||||
hal::usart::usart1.puts(ConfigSuccess ? str1 : str0);
|
// hal::usart::usart1.puts(ConfigSuccess ? str1 : str0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Event handler for the library USB Control Request reception event. */
|
/** Event handler for the library USB Control Request reception event. */
|
||||||
|
|
@ -118,27 +118,28 @@ void EVENT_USB_Device_ControlRequest(void) {
|
||||||
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
|
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
|
||||||
*/
|
*/
|
||||||
void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) {
|
void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) {
|
||||||
/* You can get changes to the virtual CDC lines in this callback; a common
|
// Printing to serial from here will make Windows commit suicide when opening the port
|
||||||
use-case is to use the Data Terminal Ready (DTR) flag to enable and
|
|
||||||
disable CDC communications in your application when set to avoid the
|
// hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged ");
|
||||||
application blocking while waiting for a host to become ready and read
|
// bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0;
|
||||||
in the pending data from the USB endpoints.
|
// char str[50];
|
||||||
*/
|
// sprintf_P(str, PSTR("DTR:%hu\n"), HostReady);
|
||||||
hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged ");
|
// hal::usart::usart1.puts(str);
|
||||||
bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0;
|
|
||||||
char str[50];
|
|
||||||
sprintf_P(str, PSTR("DTR:%hu\n"), HostReady);
|
|
||||||
hal::usart::usart1.puts(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) {
|
void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) {
|
||||||
hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged ");
|
// Printing to serial from here will make Windows commit suicide when opening the port
|
||||||
char str[50];
|
|
||||||
sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
|
// hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged ");
|
||||||
hal::usart::usart1.puts(str);
|
// char str[50];
|
||||||
|
// sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
|
||||||
|
// hal::usart::usart1.puts(str);
|
||||||
|
|
||||||
if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200) {
|
if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200) {
|
||||||
*(uint16_t *)0x0800U = 0x7777;
|
// *(uint16_t *)0x0800U = 0x7777; //old bootloader?
|
||||||
hal::cpu::Reset();
|
*(uint16_t *)(RAMEND-1) = 0x7777;
|
||||||
|
hal::cpu::resetPending = true;
|
||||||
|
hal::watchdog::Enable(hal::watchdog::configuration::compute(250));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -251,6 +252,8 @@ void setup() {
|
||||||
|
|
||||||
USB_Init();
|
USB_Init();
|
||||||
|
|
||||||
|
_delay_ms(100);
|
||||||
|
|
||||||
/// Turn off all leds
|
/// Turn off all leds
|
||||||
for (uint8_t i = 0; i < config::toolCount; i++) {
|
for (uint8_t i = 0; i < config::toolCount; i++) {
|
||||||
ml::leds.SetMode(i, ml::Color::green, ml::Mode::off);
|
ml::leds.SetMode(i, ml::Color::green, ml::Mode::off);
|
||||||
|
|
@ -477,7 +480,9 @@ void loop() {
|
||||||
ms::selector.Step();
|
ms::selector.Step();
|
||||||
mui::userInput.Step();
|
mui::userInput.Step();
|
||||||
currentCommand->Step();
|
currentCommand->Step();
|
||||||
|
hal::cpu::Step();
|
||||||
|
|
||||||
|
CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
|
||||||
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
||||||
USB_USBTask();
|
USB_USBTask();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue