Automatic switching form PIC mode to EDBG (AVR) mode, for MPLAB Snap and PICkit 4 tools.
This commit is contained in:
@@ -27,9 +27,21 @@ ACTION=="add", ATTR{idVendor}=="03eb", ATTR{idProduct}=="2175", MODE="0666"
|
|||||||
# MPLAB PICkit 4 In-Circuit Debugger
|
# MPLAB PICkit 4 In-Circuit Debugger
|
||||||
ACTION=="add", ATTR{idVendor}=="03eb", ATTR{idProduct}=="2177", MODE="0666"
|
ACTION=="add", ATTR{idVendor}=="03eb", ATTR{idProduct}=="2177", MODE="0666"
|
||||||
|
|
||||||
|
# MPLAB PICkit 4 (PIC mode)
|
||||||
|
ACTION=="add", ATTR{idVendor}=="04d8", ATTR{idProduct}=="9012", MODE="0666"
|
||||||
|
|
||||||
|
# MPLAB PICkit 4 (Bootloader mode)
|
||||||
|
ACTION=="add", ATTR{idVendor}=="04d8", ATTR{idProduct}=="9017", MODE="0666"
|
||||||
|
|
||||||
# MPLAB Snap In-Circuit Debugger
|
# MPLAB Snap In-Circuit Debugger
|
||||||
ACTION=="add", ATTR{idVendor}=="03eb", ATTR{idProduct}=="2180", MODE="0666"
|
ACTION=="add", ATTR{idVendor}=="03eb", ATTR{idProduct}=="2180", MODE="0666"
|
||||||
|
|
||||||
|
# MPLAB Snap (PIC mode)
|
||||||
|
ACTION=="add", ATTR{idVendor}=="04d8", ATTR{idProduct}=="9018", MODE="0666"
|
||||||
|
|
||||||
|
# MPLAB Snap (Bootloader mode)
|
||||||
|
ACTION=="add", ATTR{idVendor}=="04d8", ATTR{idProduct}=="9019", MODE="0666"
|
||||||
|
|
||||||
# WCH-LinkE
|
# WCH-LinkE
|
||||||
ACTION=="add", ATTR{idVendor}=="1a86", ATTR{idProduct}=="8010", MODE="0666"
|
ACTION=="add", ATTR{idVendor}=="1a86", ATTR{idProduct}=="8010", MODE="0666"
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
#include "EdbgDevice.hpp"
|
#include "EdbgDevice.hpp"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include "src/DebugToolDrivers/USB/HID/HidInterface.hpp"
|
#include "src/DebugToolDrivers/USB/HID/HidInterface.hpp"
|
||||||
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/CmsisDapInterface.hpp"
|
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/CmsisDapInterface.hpp"
|
||||||
#include "src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/CommandFrames/AvrCommandFrames.hpp"
|
#include "src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/CommandFrames/AvrCommandFrames.hpp"
|
||||||
|
#include "src/DebugToolDrivers/USB/UsbInterface.hpp"
|
||||||
|
|
||||||
#include "src/Exceptions/InvalidConfig.hpp"
|
#include "src/Exceptions/InvalidConfig.hpp"
|
||||||
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
||||||
@@ -194,4 +199,31 @@ namespace DebugToolDrivers::Microchip
|
|||||||
|
|
||||||
this->sessionStarted = false;
|
this->sessionStarted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EdbgDevice::exitBootloaderMode(UsbDevice& device) const {
|
||||||
|
static constexpr auto INTERFACE_NUMBER = std::uint8_t{0};
|
||||||
|
static constexpr auto COMMAND_ENDPOINT_ADDRESS = std::uint8_t{0x02};
|
||||||
|
|
||||||
|
auto interface = Usb::UsbInterface{INTERFACE_NUMBER, device.libusbDeviceHandle.get()};
|
||||||
|
interface.init();
|
||||||
|
|
||||||
|
static constexpr auto EXIT_BL_MODE_COMMAND = std::to_array<unsigned char>({0xE6});
|
||||||
|
|
||||||
|
interface.writeBulk(COMMAND_ENDPOINT_ADDRESS, EXIT_BL_MODE_COMMAND, 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EdbgDevice::enableEdbgMode(UsbDevice& device) const {
|
||||||
|
static constexpr auto INTERFACE_NUMBER = std::uint8_t{0};
|
||||||
|
static constexpr auto COMMAND_ENDPOINT_ADDRESS = std::uint8_t{0x02};
|
||||||
|
|
||||||
|
auto interface = Usb::UsbInterface{INTERFACE_NUMBER, device.libusbDeviceHandle.get()};
|
||||||
|
interface.init();
|
||||||
|
|
||||||
|
static constexpr auto AVR_MODE_COMMAND = std::to_array<unsigned char>({0xF0, 0x01});
|
||||||
|
static constexpr auto RESET_COMMAND = std::to_array<unsigned char>({0xED});
|
||||||
|
|
||||||
|
interface.writeBulk(COMMAND_ENDPOINT_ADDRESS, AVR_MODE_COMMAND, 64);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds{250});
|
||||||
|
interface.writeBulk(COMMAND_ENDPOINT_ADDRESS, RESET_COMMAND, 64);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,5 +158,8 @@ namespace DebugToolDrivers::Microchip
|
|||||||
virtual void configureAvr8Interface() {
|
virtual void configureAvr8Interface() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void exitBootloaderMode(Usb::UsbDevice& device) const;
|
||||||
|
void enableEdbgMode(Usb::UsbDevice& device) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ namespace DebugToolDrivers::Microchip
|
|||||||
{
|
{
|
||||||
const auto& toolNode = toolConfig.toolNode;
|
const auto& toolNode = toolConfig.toolNode;
|
||||||
|
|
||||||
|
if (toolNode["exit_bootloader_mode"]) {
|
||||||
|
this->exitBootloaderMode = toolNode["exit_bootloader_mode"].as<bool>(this->exitBootloaderMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toolNode["enable_edbg_mode"]) {
|
||||||
|
this->enableEdbgMode = toolNode["enable_edbg_mode"].as<bool>(this->enableEdbgMode);
|
||||||
|
}
|
||||||
|
|
||||||
const auto edbgDriverNode = toolNode["edbg_driver"];
|
const auto edbgDriverNode = toolNode["edbg_driver"];
|
||||||
if (edbgDriverNode) {
|
if (edbgDriverNode) {
|
||||||
if (edbgDriverNode["cmsis_command_delay"]) {
|
if (edbgDriverNode["cmsis_command_delay"]) {
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ namespace DebugToolDrivers::Microchip
|
|||||||
struct EdbgToolConfig: public DebugToolConfig
|
struct EdbgToolConfig: public DebugToolConfig
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
bool exitBootloaderMode = true;
|
||||||
|
bool enableEdbgMode = true;
|
||||||
std::optional<std::chrono::milliseconds> cmsisCommandDelay = std::nullopt;
|
std::optional<std::chrono::milliseconds> cmsisCommandDelay = std::nullopt;
|
||||||
|
|
||||||
explicit EdbgToolConfig(const DebugToolConfig& toolConfig);
|
explicit EdbgToolConfig(const DebugToolConfig& toolConfig);
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "MplabPickit4.hpp"
|
#include "MplabPickit4.hpp"
|
||||||
|
|
||||||
#include "src/TargetController/Exceptions/DeviceNotFound.hpp"
|
#include "src/Logger/Logger.hpp"
|
||||||
#include "src/Services/PathService.hpp"
|
|
||||||
|
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
||||||
|
|
||||||
namespace DebugToolDrivers::Microchip
|
namespace DebugToolDrivers::Microchip
|
||||||
{
|
{
|
||||||
@@ -15,30 +16,59 @@ namespace DebugToolDrivers::Microchip
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
void MplabPickit4::init() {
|
void MplabPickit4::init() {
|
||||||
using Exceptions::DeviceNotFound;
|
using Exceptions::DeviceInitializationFailure;
|
||||||
|
|
||||||
try {
|
if (!UsbDevice::devicePresent(this->vendorId, this->productId)) {
|
||||||
EdbgDevice::init();
|
auto blDevice = Usb::UsbDevice::tryDevice(
|
||||||
|
MplabPickit4::PIC_MODE_USB_VENDOR_ID,
|
||||||
} catch (const DeviceNotFound& exception) {
|
MplabPickit4::BL_MODE_USB_PRODUCT_ID
|
||||||
/*
|
|
||||||
* The MPLAB PICkit 4 could be connected but not in AVR mode - if this is the case, inform the user and
|
|
||||||
* direct them to the AVR mode article.
|
|
||||||
*/
|
|
||||||
const auto nonEdbgDevices = this->findMatchingDevices(
|
|
||||||
MplabPickit4::NON_EDBG_USB_VENDOR_ID,
|
|
||||||
MplabPickit4::NON_EDBG_USB_PRODUCT_ID
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!nonEdbgDevices.empty()) {
|
if (blDevice.has_value()) {
|
||||||
throw DeviceNotFound{
|
if (!this->toolConfig.exitBootloaderMode) {
|
||||||
"The connected MPLAB PICkit 4 device is not in \"AVR mode\". Please follow the instructions at "
|
throw DeviceInitializationFailure{"Device is currently in bootloader mode"};
|
||||||
+ Services::PathService::homeDomainName() + "/docs/avr-mode"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw exception;
|
Logger::warning("Found device in bootloader mode - attempting exit operation");
|
||||||
|
this->exitBootloaderMode(*blDevice);
|
||||||
|
|
||||||
|
Logger::info("Waiting for device to re-enumerate...");
|
||||||
|
if (
|
||||||
|
!Usb::UsbDevice::waitForDevice(
|
||||||
|
MplabPickit4::PIC_MODE_USB_VENDOR_ID,
|
||||||
|
MplabPickit4::PIC_MODE_USB_PRODUCT_ID,
|
||||||
|
std::chrono::seconds{8}
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw DeviceInitializationFailure{"Timeout exceeded whilst waiting for device to re-enumerate"};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logger::info("Re-enumerated device found - exit operation was successful");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto picDevice = Usb::UsbDevice::tryDevice(
|
||||||
|
MplabPickit4::PIC_MODE_USB_VENDOR_ID,
|
||||||
|
MplabPickit4::PIC_MODE_USB_PRODUCT_ID
|
||||||
|
);
|
||||||
|
|
||||||
|
if (picDevice.has_value()) {
|
||||||
|
if (!this->toolConfig.enableEdbgMode) {
|
||||||
|
throw DeviceInitializationFailure{"Device is currently in PIC mode"};
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::warning("Found device in PIC mode - attempting to switch to EDBG (AVR) mode");
|
||||||
|
this->enableEdbgMode(*picDevice);
|
||||||
|
|
||||||
|
Logger::info("Waiting for device to re-enumerate...");
|
||||||
|
if (!Usb::UsbDevice::waitForDevice(this->vendorId, this->productId, std::chrono::seconds{8})) {
|
||||||
|
throw DeviceInitializationFailure{"Timeout exceeded whilst waiting for device to re-enumerate"};
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::info("Re-enumerated device found - mode switch operation was successful");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EdbgDevice::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MplabPickit4::configureAvr8Interface() {
|
void MplabPickit4::configureAvr8Interface() {
|
||||||
|
|||||||
@@ -26,8 +26,9 @@ namespace DebugToolDrivers::Microchip
|
|||||||
static const inline std::uint16_t USB_PRODUCT_ID = 0x2177;
|
static const inline std::uint16_t USB_PRODUCT_ID = 0x2177;
|
||||||
static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0;
|
static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0;
|
||||||
|
|
||||||
static const inline std::uint16_t NON_EDBG_USB_VENDOR_ID = 0x04d8;
|
static const inline std::uint16_t PIC_MODE_USB_VENDOR_ID = 0x04d8;
|
||||||
static const inline std::uint16_t NON_EDBG_USB_PRODUCT_ID = 0x9012;
|
static const inline std::uint16_t PIC_MODE_USB_PRODUCT_ID = 0x9012;
|
||||||
|
static const inline std::uint16_t BL_MODE_USB_PRODUCT_ID = 0x9017;
|
||||||
|
|
||||||
MplabPickit4(const DebugToolConfig& debugToolConfig);
|
MplabPickit4(const DebugToolConfig& debugToolConfig);
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "MplabSnap.hpp"
|
#include "MplabSnap.hpp"
|
||||||
|
|
||||||
#include "src/TargetController/Exceptions/DeviceNotFound.hpp"
|
#include "src/Logger/Logger.hpp"
|
||||||
#include "src/Services/PathService.hpp"
|
|
||||||
|
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
||||||
|
|
||||||
namespace DebugToolDrivers::Microchip
|
namespace DebugToolDrivers::Microchip
|
||||||
{
|
{
|
||||||
@@ -15,38 +16,59 @@ namespace DebugToolDrivers::Microchip
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
void MplabSnap::init() {
|
void MplabSnap::init() {
|
||||||
using Exceptions::DeviceNotFound;
|
using Exceptions::DeviceInitializationFailure;
|
||||||
|
|
||||||
|
if (!UsbDevice::devicePresent(this->vendorId, this->productId)) {
|
||||||
|
auto blDevice = Usb::UsbDevice::tryDevice(
|
||||||
|
MplabSnap::PIC_MODE_USB_VENDOR_ID,
|
||||||
|
MplabSnap::BL_MODE_USB_PRODUCT_ID
|
||||||
|
);
|
||||||
|
|
||||||
|
if (blDevice.has_value()) {
|
||||||
|
if (!this->toolConfig.exitBootloaderMode) {
|
||||||
|
throw DeviceInitializationFailure{"Device is currently in bootloader mode"};
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::warning("Found device in bootloader mode - attempting exit operation");
|
||||||
|
this->exitBootloaderMode(*blDevice);
|
||||||
|
|
||||||
|
Logger::info("Waiting for device to re-enumerate...");
|
||||||
|
if (
|
||||||
|
!Usb::UsbDevice::waitForDevice(
|
||||||
|
MplabSnap::PIC_MODE_USB_VENDOR_ID,
|
||||||
|
MplabSnap::PIC_MODE_USB_PRODUCT_ID,
|
||||||
|
std::chrono::seconds{8}
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw DeviceInitializationFailure{"Timeout exceeded whilst waiting for device to re-enumerate"};
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::info("Re-enumerated device found - exit operation was successful");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto picDevice = Usb::UsbDevice::tryDevice(
|
||||||
|
MplabSnap::PIC_MODE_USB_VENDOR_ID,
|
||||||
|
MplabSnap::PIC_MODE_USB_PRODUCT_ID
|
||||||
|
);
|
||||||
|
|
||||||
|
if (picDevice.has_value()) {
|
||||||
|
if (!this->toolConfig.enableEdbgMode) {
|
||||||
|
throw DeviceInitializationFailure{"Device is currently in PIC mode"};
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::warning("Found device in PIC mode - attempting to switch to EDBG (AVR) mode");
|
||||||
|
this->enableEdbgMode(*picDevice);
|
||||||
|
|
||||||
|
Logger::info("Waiting for device to re-enumerate...");
|
||||||
|
if (!Usb::UsbDevice::waitForDevice(this->vendorId, this->productId, std::chrono::seconds{8})) {
|
||||||
|
throw DeviceInitializationFailure{"Timeout exceeded whilst waiting for device to re-enumerate"};
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::info("Re-enumerated device found - mode switch operation was successful");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
EdbgDevice::init();
|
EdbgDevice::init();
|
||||||
|
|
||||||
} catch (const DeviceNotFound& exception) {
|
|
||||||
/*
|
|
||||||
* The MPLAB Snap could be connected but not in AVR mode - if this is the case, inform the user and direct
|
|
||||||
* them to the AVR mode article.
|
|
||||||
*/
|
|
||||||
auto nonEdbgDevices = this->findMatchingDevices(
|
|
||||||
MplabSnap::NON_EDBG_USB_VENDOR_ID,
|
|
||||||
MplabSnap::NON_EDBG_USB_PRODUCT_ID
|
|
||||||
);
|
|
||||||
|
|
||||||
if (nonEdbgDevices.empty()) {
|
|
||||||
// The MPLAB Snap sometimes uses another product ID when not in AVR mode.
|
|
||||||
nonEdbgDevices = this->findMatchingDevices(
|
|
||||||
MplabSnap::NON_EDBG_USB_VENDOR_ID,
|
|
||||||
MplabSnap::NON_EDBG_USB_PRODUCT_ID_ALTERNATIVE
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!nonEdbgDevices.empty()) {
|
|
||||||
throw DeviceNotFound{
|
|
||||||
"The connected MPLAB Snap device is not in \"AVR mode\". Please follow the instructions at "
|
|
||||||
+ Services::PathService::homeDomainName() + "/docs/avr-mode"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
throw exception;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MplabSnap::configureAvr8Interface() {
|
void MplabSnap::configureAvr8Interface() {
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ namespace DebugToolDrivers::Microchip
|
|||||||
static const inline std::uint16_t USB_PRODUCT_ID = 0x2180;
|
static const inline std::uint16_t USB_PRODUCT_ID = 0x2180;
|
||||||
static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0;
|
static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0;
|
||||||
|
|
||||||
static const inline std::uint16_t NON_EDBG_USB_VENDOR_ID = 0x04d8;
|
static const inline std::uint16_t PIC_MODE_USB_VENDOR_ID = 0x04d8;
|
||||||
static const inline std::uint16_t NON_EDBG_USB_PRODUCT_ID = 0x9018;
|
static const inline std::uint16_t PIC_MODE_USB_PRODUCT_ID = 0x9018;
|
||||||
static const inline std::uint16_t NON_EDBG_USB_PRODUCT_ID_ALTERNATIVE = 0x9017;
|
static const inline std::uint16_t BL_MODE_USB_PRODUCT_ID = 0x9019;
|
||||||
|
|
||||||
MplabSnap(const DebugToolConfig& debugToolConfig);
|
MplabSnap(const DebugToolConfig& debugToolConfig);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user