5 Replies Latest reply on Nov 29, 2017 10:34 AM by srds

    SWD PSoC4 c++ COM writeProtection() per the example, dosent remove chip protection and erase chip.

    padams_2836281

      I am trying to write a mass programmer for production.

       

      The PSoC4/SWD/Cpp-EX Example code provided for erase flash  dose not seam to actually be able to clear chip protection.

       

      The code as written in the original example did not erase a protected chip.

       

      I then thinking the chip needed rebooted added a release command before the acquire, No Effect

       

      I then added a erase operation after the writeProtection(), No effect.

       

      I then had it hit the writeProtection() 3 times in a row, Also no effect.

       

       

      The chip is a CYBLE-014008-00.

       

      The PSoC Programmer software is able to program and erase the protected chip with no issues.

       

      The example code before and after moving it to C++ was/is able to program and erase unlocked chips with no issues.  Only the unlocking portion of the example fails.

       

       

      long c_cypCom::PSoC4_EraseAll()
      {
          long hr;
      
          //Check chip level protection here. If PROTECTED then move to OPEN by PSoC4_WriteProtection() API.
          //Otherwise use PSoC4_EraseAll() - in OPEN/VIRGIN modes.
              //TODO  do we need to detect no chip and not run any further if none is there???  ppPSoC4_WriteProtection crashes if there is no chip in  its default state.
          hr = PSoC4_IsChipNotProtected();
          if (SUCCEEDED(hr)) //OPEN mode
          {
              //Erase All - Flash and Protection bits. Still be in OPEN mode.
              hr = ppPSoC4_EraseAll(sErrorMsg);
                       if (!SUCCEEDED(hr))
                      {
                              printf("ERROR:%s\n",sErrorMsg.c_str());
                              return REC_DEVICE_ERASE*-1;
                      }
          }
          else
          {   //Move to OPEN from PROTECTED. It automatically erases Flash and its Protection bits.
              std::vector<byte> flashProt; // do not care in PROTECTED mode
              std::vector<byte> chipProt; //move to OPEN
              chipProt.push_back(CHIP_PROT_OPEN);// Set as 0x01   value in memory is 1 for the first element of chipProt.
      
              hr = ppPSoC4_WriteProtection(flashProt, chipProt, sErrorMsg);//      FAILS TO UNLOCK AND ERASE CHIP
              if (!SUCCEEDED(hr))// No errors present here
                      {
                              printf("ERROR:%s\n",sErrorMsg.c_str());
                              return REC_DEVICE_ERASE*-1;
                      }
      
              //Need to reacquire chip here to boot in OPEN mode.
              //ChipLevelProtection is applied only after Reset.
              hr = ppDAP_Acquire(sErrorMsg);
              if (!SUCCEEDED(hr))
              {
                  printf("ERROR:%s\n",sErrorMsg.c_str());
                  return REC_DEVICE_ERASE*-1;
              }
          }
          if (!SUCCEEDED(hr)) return REC_DEVICE_ERASE*-1;
          return REC_GOOD;
      }
      

       

       

      I am looking for suggestions.

        • 1. Re: SWD PSoC4 c++ COM writeProtection() per the example, dosent remove chip protection and erase chip.
          srds

          Hi,

           

          I have used the C# example (same flow as C++) with CYBLE-022001-00 and PSoC4_WriteProtection() worked correctly. It changed the device protection from PROTECTED to OPEN and programmed the new application HEX file without any issues. That mean PSoC4_WriteProtection() API working correctly.

           

          What error code and message you get when PSoC4_WriteProtection() is executed?

          1 of 1 people found this helpful
          • 2. Re: SWD PSoC4 c++ COM writeProtection() per the example, dosent remove chip protection and erase chip.
            padams_2836281

            Prior to execution of that line these variables have this data.

             

            hr = -2147467259

            flashProt = [0]()

            chipProt = [1](1 ' ')

            sErrorMsg = 'Chip is in PROTECTED mode. Any Access to Flash is suppressed.'

             

             

             

            hr = ppPSoC4_WriteProtection(flashProt, chipProt, sErrorMsg);

             

            Post execution  these variables have this data.

             

            hr = 0

            flashProt = [0]()

            chipProt = [1](1 ' ')

            sErrorMsg = 'Chip is in PROTECTED mode. Any Access to Flash is suppressed.'

             

             

            If i clear the sErrorMsg before  WriteProtection()   it remains cleared and is not set by WriteProtection().

             

            checking with the PSoC Programmer program  the chip still is locked at this point.

             

             

            Also attempting to program further down fails with these values

            hr = -2147467259

             

             

            I am checking on the differences between the C++ and python and c# versions.   ppPSoC4_WriteProtection is written in C++ in the example.  the others have it as a function embedded in the API/COM

             

            It looks like the C++ Version is overwriting the chipProt byte with the flashProt byte  and that the whole issue might be caused by the array not being set to index 2 when inserting the flashProt byte in ppPSoC4_WriteProtection()

             

             

            Thanks

            pat a

            • 3. Re: SWD PSoC4 c++ COM writeProtection() per the example, dosent remove chip protection and erase chip.
              srds

              Please now try modifying the ppPSoC4_WriteProtection() to match C# example and see if that works. You can get details of the API in the PSoC Programmer installation directory in the below path:

              C:\Program Files (x86)\Cypress\Programmer\Documents\PSoC Programmer COM User Guide.pdf

               

              Cheers!

              1 of 1 people found this helpful
              • 4. Re: SWD PSoC4 c++ COM writeProtection() per the example, dosent remove chip protection and erase chip.
                padams_2836281

                SOLUTION

                 

                The C++ Example code has 2 Lines with typos that causes this issue.

                The array index shuld be [2]  not 1 for the 2nd parameter

                 

                A 3rd line i had already changed in every function in the API that has it.  As it can cause a Crash!

                bstrError Must be checked to see if it is 0 or null BEFORE using it.  Sometimes with a poor connection to the target PSoC4 device commands will return a memory address of 0 causing a crash.

                 

                 

                long ppPSoC4_WriteProtection(std::vector<BYTE> flashProtect, std::vector<BYTE> chipProtect, std::string &strError)
                {
                    DISPID dispid=dispID_PSoC4_WriteProtection;
                   
                    // Set up parameters
                    DISPPARAMS dispparams;
                    memset( &dispparams, 0, sizeof( DISPPARAMS ));
                    dispparams.cArgs = 3;   
                    // Allocate memory for parameters
                    VARIANTARG* pArg = new VARIANTARG[dispparams.cArgs];
                    dispparams.rgvarg = pArg;
                    memset( pArg, 0, sizeof(VARIANT) * dispparams.cArgs);   
                    //Convert hexData into SafeArray
                    VARIANT varData1, varData2;
                    ConvertByteVector2SA(chipProtect, &varData1);
                    ConvertByteVector2SA(flashProtect, &varData2);
                
                    //Initialize Parameters
                    BSTR bstrError=0;
                    dispparams.rgvarg[0].vt = VT_BSTR | VT_BYREF;
                    dispparams.rgvarg[0].pbstrVal = &bstrError;
                    dispparams.rgvarg[1].vt = VT_ARRAY | VT_UI1;
                    dispparams.rgvarg[1].parray = varData1.parray;
                    dispparams.rgvarg[2].vt = VT_ARRAY | VT_UI1;//LINE EDITED  ORIGINAL dispparams.rgvarg[1].vt = VT_ARRAY | VT_UI1;
                    dispparams.rgvarg[2].parray = varData2.parray;//LINE EDITED  ORIGINAL dispparams.rgvarg[1].parray = varData2.parray;
                   
                    //Init Result (Return Value)
                    VARIANTARG vaResult;
                    VariantInit( &vaResult );
                
                    HRESULT hr;
                    hr = pIDispatch->Invoke(dispid,    IID_NULL, GetUserDefaultLCID(),    DISPATCH_METHOD,
                                            &dispparams, &vaResult, NULL, NULL);
                
                    USES_CONVERSION;
                    //The following Line has a Potental Crash that has acctualy occured in testing.  This shows the fixed version.
                    if(bstrError != 0) strError = W2A(bstrError);//LINE EDITED  ORIGINAL strError = W2A(bstrError);   
                    //Free allocated resources
                    delete[] pArg;
                    ::SysFreeString(bstrError);
                    VariantClear(&varData1);
                    VariantClear(&varData2);
                   
                    return vaResult.lVal;
                }
                

                 

                 

                With the first 2 fixes the chips now erase when protected as expected.

                 

                Is there a good place to send this bug report to?

                 

                Thanks Srds.

                • 5. Re: SWD PSoC4 c++ COM writeProtection() per the example, dosent remove chip protection and erase chip.
                  srds

                  You can always report such issues by contacting Cypress Support and filing a support case.

                  1 of 1 people found this helpful