/***************************************************************************** * * * Application Control Interface Example for Phyton ChipProgUSB Programmer * * * * Description: * * 1. Waits until a device is in socket * * 2. Reads device and algorithm parameters from the device * * * 3. Prints device and algorithm parameters * * 4. Sets "Vpp" algorithm parameter to 10.5 Volts, "Brown-out Reset" * * to "Disabled" * * 5. Programs device and algorithm parameters to the device * * * * Code to modify: See main() function at the end of this file. Change * * the path to the programmer executable file. This example uses * * device and algorithm parameter and programming function names specific * * for Microchip PIC18Fxx devices. * * * * (C) Phyton * * * ******************************************************************************/ #define _CRT_SECURE_NO_WARNINGS #define WIN32_LEAN_AND_MEAN #include #include #include #include #include "..\aciprog.h" void PrintError(UINT error_code); UINT WaitDevice(void); BOOL Attach(const char * exe, const char * command_line, BOOL debug); BOOL SetDevice(const char * manufacturer, const char * device_name); BOOL ExecFunction(const char * function_name, UINT buffer_num, BOOL silent); void PrintParameter(const char * par_name, UINT mode); ACI_PStatus_Params Status = { sizeof(ACI_PStatus_Params) }; /*+ PrintError ° 01.07.09 15:07:30*/ void PrintError(UINT error_code) { const char * msg; char text[48]; switch (error_code) { case ACI_ERR_SUCCESS: return; case ACI_ERR_INVALID_PARAMS_SIZE: msg = "ACI_ERR_INVALID_PARAMS_SIZE: Invalid structure size in ACI function"; break; case ACI_ERR_INVALID_EXE: msg = "ACI_ERR_INVALID_EXE: Invalid executable in ACI_Launch()"; break; case ACI_ERR_EXE_FAILED: msg = "ACI_ERR_EXE_FAILED: Programmer executable failed to launch"; break; case ACI_ERR_FILE_ERROR: msg = "ACI_ERR_FILE_ERROR: File error"; break; case ACI_ERR_ATTACH_FAILED: msg = "ACI_ERR_ATTACH_FAILED: ACI_Launch() failed"; break; case ACI_ERR_INVALID_PARAMETER: msg = "ACI_ERR_INVALID_PARAMETER: Invalid parameter in function structure"; break; case ACI_ERR_FUNCTION_FAILED: msg = "ACI_ERR_FUNCTION_FAILED: Function execution failed in ACI_ExecFunction()"; break; case ACI_ERR_NOT_CONNECTED: msg = "ACI_ERR_NOT_CONNECTED: No connection to the target hardware"; break; case ACI_ERR_NOT_AVAILABLE_NOW: msg = "ACI_ERR_NOT_AVAILABLE_NOW: Requested operation is not available at the moment"; break; case ACI_ERR_INVALID_LAYER: msg = "ACI_ERR_INVALID_LAYER: Invalid buffer or layer number"; break; case ACI_ERR_INVALID_ADDR: msg = "ACI_ERR_INVALID_ADDR: Invalid address"; break; case ACI_ERR_OUT_OF_RANGE: msg = "ACI_ERR_OUT_OF_RANGE: Value is out of range"; break; default: sprintf(text, "%u: Unknown error code", error_code); msg = text; break; } printf("Error: %s\n", msg); } /*+ Attach ° 01.07.09 15:04:27*/ BOOL Attach(const char * exe, const char * command_line, BOOL debug) { UINT r; static ACI_Launch_Params params; params.Size = sizeof(ACI_Launch_Params); // UINT Size; // (in) Size of structure, in bytes params.ProgrammerExe = exe; // LPCSTR ProgrammerExe; // (in) Programmer executable file name params.CommandLine = command_line; // LPCSTR CommandLine; // (in) Optional programmer command-line parameters params.DebugMode = debug; // BOOL DebugMode; // (in) Debug mode. Programmer window is not hidden r = ACI_Launch(¶ms); PrintError(r); return r == ACI_ERR_SUCCESS; } /*+ SetDevice ° 01.07.09 15:15:01*/ BOOL SetDevice(const char * manufacturer, const char * device_name) { UINT r; ACI_Device_Params params = { sizeof(ACI_Device_Params) }; strcpy(params.Manufacturer, manufacturer); strcpy(params.Name, device_name); r = ACI_SetDevice(¶ms); PrintError(r); return r == ACI_ERR_SUCCESS; } /*+ ExecFunction ° 01.07.09 18:40:21*/ BOOL ExecFunction(const char * function_name, UINT buffer_num, BOOL silent) { UINT r; ACI_Function_Params params = { sizeof(ACI_Function_Params) }; params.FunctionName = function_name; params.BufferNumber = buffer_num; params.Silent = silent; printf("Executing %s...\n", function_name); r = ACI_ExecFunction(¶ms); PrintError(r); if (r == ACI_ERR_SUCCESS) printf("%s complete.\n", function_name); else printf("%s error: %s.\n", function_name, params.ErrorMessage); return r == ACI_ERR_SUCCESS; } /*+ WaitDevice ° 13.07.09 13:11:24*/ UINT WaitDevice(void) { UINT r; printf("Waiting until a new device is in socket. Please insert a device...\n"); do { r = ACI_GetStatus(&Status); PrintError(r); if (r != ACI_ERR_SUCCESS) return r; Sleep(0); } while (!Status.NewDevice || Status.DeviceStatus != ACI_DS_OK); return ACI_ERR_SUCCESS; } /*+ PrintParameter ° 13.07.09 13:55:50*/ void PrintParameter(const char * par_name, UINT mode) { UINT r, i; ACI_ProgOption_Params params = { sizeof(ACI_ProgOption_Params) }; params.OptionName = par_name; params.Mode = mode; // Setting Value.String to NULL will return in VSize buffer size needed to // store the string for string options params.Value.String = NULL; r = ACI_GetProgOption(¶ms); PrintError(r); if (r != ACI_ERR_SUCCESS) return; printf("\n%s [%s]%s\n", par_name, params.OptionDescription, params.ReadOnly? " Read-Only" : ""); switch (params.OptionType) { case ACI_PO_LONG: printf("%d %s (long)", params.Value.LongValue, params.Units); break; case ACI_PO_FLOAT: printf("%g %s (float)", params.Value.FloatValue, params.Units); break; case ACI_PO_STRING: // Get option value. Required buffer size is in params.VSize. params.Value.String = (LPSTR)malloc(params.VSize); r = ACI_GetProgOption(¶ms); PrintError(r); if (r != ACI_ERR_SUCCESS) return; printf("\"%s\" (string)", params.Value.String); free(params.Value.String); break; case ACI_PO_CHECKBOXES: printf("CheckBoxes:\n" "Bit #: 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1\n" " 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0\n" " "); for (i = 32; i != 0; i--) printf(" %c", (params.Value.CheckBoxesValue & (1 << (i - 1))) == 0? '0' : '1'); break; case ACI_PO_LIST: printf("StateIndex = %u: '%s'", params.Value.StateIndex, params.ListString); break; default: printf("", params.OptionType); break; } printf("\n"); } // Device and algorithm parameter names const char * DevParNameTable[] = { "Memory protection", "Table write protection", "Table read protection", "CONFIG1^Oscillator", "CONFIG1^Oscillator switching", "CONFIG2^PWRT", "CONFIG2^Brown-out Reset", "CONFIG2^Brown-out Reset Voltage", "CONFIG2^WDT", "CONFIG2^WDT Postscaler", "CONFIG3^CCP2", "CONFIG4^Stack Full/Underflow Reset", "CONFIG4^Low voltage programming", "CONFIG4^Background Debugger", "DEVID", "Vpp", "Vcc", }; /*+ main ° 01.07.09 17:37:24*/ int main(void /*int argc, char ** argv*/) { UINT i, r; ACI_ProgOption_Params params = { sizeof(ACI_ProgOption_Params) }; // Launch the programmer executable if (!Attach("C:\\Program Files\\ChipProgUSB\\4_73_00\\UProgNT2.exe", "", FALSE)) return -1; // Select device to operate on if (!SetDevice("Microchip", "PIC18F242")) return -1; // Wait until device is in socket if (WaitDevice() != ACI_ERR_SUCCESS) return -1; // Read device and algorithm parameters from the device if (!ExecFunction("Device Parameters & ID^Read", 0, TRUE)) return -1; // Print device and algorithm parameter values. The PrintParameter() // function can be used to print current, default, mimimal or maximal // value according to its "mode" parameter printf("------ Current device and algorithm parameter values ------\n"); for (i = 0; i < sizeof(DevParNameTable) / sizeof(DevParNameTable[0]); i++) PrintParameter(DevParNameTable[i], ACI_PP_MODE_VALUE); // Set Vpp to 10.5 Volts params.OptionName = "Vpp"; params.Value.FloatValue = 10.5; r = ACI_SetProgOption(¶ms); PrintError(r); if (r != ACI_ERR_SUCCESS) return -1; // Set Brown-out Reset in the CONFIG2 list to Disabled params.OptionName = "CONFIG2^Brown-out Reset"; params.Value.StateIndex = 0; r = ACI_SetProgOption(¶ms); PrintError(r); if (r != ACI_ERR_SUCCESS) return -1; // Write device and algorithm parameters to the device if (!ExecFunction("Erase", 0, TRUE) || // only the whole device can be erased !ExecFunction("Device Parameters & ID^Blank Check", 0, TRUE) || !ExecFunction("Device Parameters & ID^Program", 0, TRUE) || !ExecFunction("Device Parameters & ID^Verify", 0, TRUE) ) return -1; return 0; }