Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active January 24, 2023 20:52
Show Gist options
  • Save f-steff/6dda073ccaf1da6b9ede3718adc0407a to your computer and use it in GitHub Desktop.
Save f-steff/6dda073ccaf1da6b9ede3718adc0407a to your computer and use it in GitHub Desktop.
Batch script to switch USB drivers for the Microchip ICD3 In Circuit Debugger. Unlike the "MPLAB Drive Switcher" utility, this script supports all installation points of MPLAB8 and MPLABX, both 32 and 64 bit, and it uses only windows functionality to perform the switch, which is virtually instantaneous. Tested on Win10. Updated to support multip…
@echo off
:: ICD3 switcher by Flemming Steffensen, Febuary 2021.
:: Switch USB drivers for all Microchip ICD3 In Circuit Debugger connected to the computer.
:: Unlike the "MPLAB Drive Switcher" utility, this script supports all installation points of MPLAB8 and MPLABX,
:: both 32 and 64 bit, and it uses only windows functionality to perform the switch, which is virtually instantaneous.
:: This script is parameterized in run in command prompt and interactive when doubleclicked.
:: Tested on Win10.
::
:: Note, A known ICD3 bug appear to happen much more frequently when switching between drives. The ICD3 firmware also
:: need to be programmed, and while it can happen automatic, I recommend to not do so. If after installing the firmware,
:: the ICD3 still can not read the Device ID of your target, then change to another device family of target, perform the manual
:: firmware update and then switch back to the correct device, ie. Pic32->Pic16->Pic32 will trigger the correct update.
::
::Known device info, extracted with wmic:
:: DeviceID, where VID and PID are the only certains (the last part is the device serial number), looks like this: USB\VID_04D8&PID_9009\JIT162110173
:: For MPLAB8 driver, Name looks like this: Microchip Custom USB Device
:: For MPLABX driver, Name looks like this: Microchip WinUSB Device
:: For no driver, Name looks like this: MPLAB® ICD3 tm (www.microchip.com)
:Intro
echo:ICD3_switcher
:GetParameters
set choice=%1
:DetectDoubleClicked
set DoubleClicked=0
setlocal enabledelayedexpansion
set testl=%cmdcmdline:"=%
set testr=!testl:%~nx0=!
if not "%testl%" == "%testr%" set DoubleClicked=1
:CheckAdminRights
net session >nul 2>&1
if not %errorLevel% == 0 (
echo:Error: This script must be run with administrator rights.
goto error
)
:FindUSBDrivers
:Find8
set "MPLAB8=c:\Program Files\Microchip\MPLAB IDE\Drivers64\"
if not exist "%MPLAB8%" (
set "MPLAB8=c:\Program Files (x86)\Microchip\MPLAB IDE\Drivers64\"
)
if not exist "%MPLAB8%" (
echo:Warning: MPLAB8 drivers not found at any of the expected locations.
)
For /R "%MPLAB8%" %%G IN (mchpusb.inf) do (
set "Driver8=%%G"
goto :Got8
)
echo:Warning: Didn't find ICD3 driver for MPLAB8
set Driver8=
goto :Done8
:Got8
echo:Located MPLAB 8 ICD3 driver at
echo: %Driver8%
:Done8
:FindX
set "MPLABX=c:\Program Files\Microchip\MPLABX\"
if not exist "%MPLABX%" (
set "MPLABX=c:\Program Files (x86)\Microchip\MPLABX\"
)
if not exist "%MPLABX%" (
echo:Warning: MPLABX drivers not found at any of the expected locations.
)
::Find files in path with wildscards:
for /d %%D in ("%MPLABX%*") do for %%F in ("%%~fD\Switcher\64Bit\winusb\amd64\MCHPWinUSBDevice.inf") do (
set "DriverX=%%~fF"
goto :GotX
)
echo:Warning: Didn't find ICD3 driver for MPLABX
set DriverX=
goto :DoneX
:GotX
echo:Located MPLAB X ICD3 driver at
echo: %DriverX%
:DoneX
:FindMicrochipUSBDevices
set ICD3VidPid=%%VID_04D8%%PID_9009%%
set /a FoundCounter=0
for /F "tokens=* skip=2 usebackq" %%F IN (`wmic path CIM_LogicalDevice where "DeviceID like '%ICD3VidPid%'" get DeviceID^,Name /format:csv 2^>nul`) DO (
set /a FoundCounter=!FoundCounter!+1
for /F "tokens=2,3 delims=," %%G in ("%%F") do (
set DeviceID[!FoundCounter!]=%%G
set Name[!FoundCounter!]=%%H
)
)
echo:Found %FoundCounter% ICD3 device(s) connected to this computer.
if %FoundCounter%==0 goto :ok
:IdentifyCurrentICD3DriverMode
set /a LoopCounter=LoopCounter+1
for /L %%a in (1,1,%FoundCounter%) do (
call set Name=%%Name[%%a]%%
if "!Name!"=="Microchip Custom USB Device" (
echo: ICD3 device #%%a is in MPLAB 8 mode: !Name!
) else (
if "!Name!"=="Microchip WinUSB Device" (
echo: ICD3 device #%%a is in MPLAB X mode: !Name!
) else (
echo: ICD3 device #%%a is in unitialized Mode: !Name!
)
)
)
:WhatToDo
:: If started by double click, display usage, then return to :CheckInput
if %DoubleClicked%==1 (
echo:Select action:
echo: 0 : Remove all ICD3 drivers from system
echo: 8 : Switch to MPLAB 8 ICD3 Driver
echo: x : Switch to MPLAB X ICD3 Driver
echo: z : Exit without changing anything.
set /p choice=What to do?
)
:Usage
if /i "%choice%"=="" (
echo:Usage:
echo: ICD3_Switcher 0 : Remove all ICD3 drivers from system
echo: ICD3_Switcher 8 : Switch to MPLAB 8 ICD3 Driver
echo: ICD3_Switcher x : Switch to MPLAB X ICD3 Driver
echo: ICD3_Switcher z : Exit without changing anything.
goto ok
)
:Leave
if /i "%choice%"=="z" (
goto :ok
)
:FixDrivers
for /L %%a in (1,1,%FoundCounter%) do (
call set DeviceID=%%DeviceID[%%a]%%
::Uninstall
if /i "%choice%"=="0" (
if exist "%Driver8%" pnputil /delete-driver "%Driver8%" /uninstall /force 2>&1>nul
if exist "%DriverX%" pnputil /delete-driver "%DriverX%" /uninstall /force 2>&1>nul
pnputil /remove-device "%DeviceID%" /force 2>&1>nul
echo:Microchip ICD3 device #%%a and drivers have been removed.
if %FoundCounter%==%%a goto :ok
)
::SwitchTo8
if /i "%choice%"=="8" (
if "%Driver8%"=="" (
echo:Error: Can not switch to MPLAB8 ICD3 driver, as it appear not to be installed.
goto error
)
if exist "%DriverX%" pnputil /delete-driver "%DriverX%" /uninstall /force 2>&1>nul
pnputil /remove-device "%DeviceID%" /force 2>&1>nul
if exist "%Driver8%" pnputil /add-driver "%Driver8%" /install /force 2>&1>nul
echo:Microchip ICD3 device #%%a have had MPLAB 8 device drivers installed and enabled.
if %FoundCounter%==%%a goto :ok
)
::SwitchToX
if /i "%choice%"=="x" (
if "%DriverX%"=="" (
echo:Error: Can not switch to MPLABX ICD3 driver, as it appear not to be installed.
goto error
)
if exist "%Driver8%" pnputil /delete-driver "%Driver8%" /uninstall /force 2>&1>nul
pnputil /remove-device "%DeviceID%" /force 2>&1>nul
if exist "%DriverX%" pnputil /add-driver "%DriverX%" /install /force 2>&1>nul
echo:Microchip ICD3 device #%%a have had MPLAB X device drivers installed and enabled.
if %FoundCounter%==%%a goto :ok
)
)
goto :ok
:ok
echo:Done
set ErrorLevel=0
goto done
:error
set ErrorLevel=1
goto done
:done
if %DoubleClicked%==1 pause
exit /b %ErrorLevel%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment