News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

18F25K22 not running after code protect

Started by evoortman, Jan 02, 2024, 10:33 AM

Previous topic - Next topic

evoortman

Hi,

Happy new year to all!

Strange problem:
The code below runs and manipulates porta outputs.
But if I turn ON one or more code protect blocks (CP0, CP1, CP2, CP3, CPB) the code no longer runs.

Anyone have an idea?


;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F25K22

Config_Start
  FOSC = INTIO67   ;Internal oscillator block
  PLLCFG = OFF   ;Oscillator used directly
  PRICLKEN = OFF   ;Primary clock can be disabled by software
  FCMEN = OFF   ;Fail-Safe Clock Monitor disabled
  IESO = OFF   ;Oscillator Switchover mode disabled
  PWRTEN = On   ;Power up timer enabled
  BOREN = OFF   ;Brown-out Reset disabled in hardware and software
  BORV = 285   ;VBOR set to 2.85 V nominal
  WDTEN = OFF   ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 32768   ;1:32768
  CCP2MX = PORTC1   ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF   ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5   ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = OFF   ;HFINTOSC output and ready status are delayed by the oscillator stable status
  T3CMX = PORTC0   ;T3CKI is on RC0
  P2BMX = PORTB5   ;P2B is on RB5
  MCLRE = EXTMCLR   ;MCLR pin enabled, RE3 input pin disabled
  STVREN = OFF   ;Stack full/underflow will not cause Reset
  LVP = OFF   ;Single-Supply ICSP disabled
  XINST = OFF   ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF   ;Disabled
  Cp0 = OFF   ;Block 0 (000800-001FFFh) not code-protected
  CP1 = OFF   ;Block 1 (002000-003FFFh) not code-protected
  CP2 = OFF   ;Block 2 (004000-005FFFh) not code-protected
  CP3 = OFF   ;Block 3 (006000-007FFFh) not code-protected
  CPB = OFF   ;Boot block (000000-0007FFh) not code-protected

  CPD = OFF   ;Data EEPROM not code-protected
  WRT0 = OFF   ;Block 0 (000800-001FFFh) not write-protected
  WRT1 = OFF   ;Block 1 (002000-003FFFh) not write-protected
  WRT2 = OFF   ;Block 2 (004000-005FFFh) not write-protected
  WRT3 = OFF   ;Block 3 (006000-007FFFh) not write-protected
  WRTC = OFF   ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF   ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF   ;Data EEPROM not write-protected
  EBTR0 = OFF   ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF   ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF   ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF   ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
  EBTRB = OFF   ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Xtal = 16
All_Digital = TRUE

ANSELA = 0
ANSELB = 0
ANSELC = 0
CM1CON0 = 0
CM2CON0 = 0

OSCCON = %01110000                                                                  ' Internal RC Oscillator Frequency HFINTOSC – (16 MHz)
OSCCON2 = %00000000                                                                 ' Primary Oscillator Drive Circuit off
     
Dim X As Byte
Dim Y As Byte

DelayMS 100

PORTA = 1
Output PORTA

MAIN:
            If Y < 128 And X = 0 Then
               Y = Y * 2
               PORTA = ~Y
            Else   
               X = 1
            EndIf
           
            If Y > 1 And X = 1 Then
               Y = Y / 2
               PORTA = ~Y
            Else   
               X = 0
            EndIf
               
            DelayMS 50
           
           
            GoTo MAIN

End

trastikata

Quote from: evoortman on Jan 02, 2024, 10:33 AMBut if I turn ON one or more code protect blocks (CP0, CP1, CP2, CP3, CPB) the code no longer runs.

Hello evoortman,

did you run the code in Proteus sim or in an actual application?

evoortman


Pepe

proteus simulation and program modified

Stephen Moss

Quote from: evoortman on Jan 02, 2024, 10:33 AMHi,

Happy new year to all!

Strange problem:
The code below runs and manipulates porta outputs.
But if I turn ON one or more code protect blocks (CP0, CP1, CP2, CP3, CPB) the code no longer runs.

Anyone have an idea?

That seems rather odd, does it happen when any protection block is enable or only when one particular memory block is protected?
As the only thing I can think of that it may be is that the compiler is using a memory location within the protected block to store the variable, but with it being in a protected memory block the code is then prevented from updating the variable value from its initial setting as it cannot write the new value to the protected block.

It looks like you are just setting one bit of Y high at a time, and then inverting it to make it one bit low instead, so do you get the same problem if you bitwise shift or Rotate the value, i.e.
While Y < 128
ROL Y,clear
PortA = ~Y
Wend

or

While Y < 128
Y = Y << 1
PortA = Y
Wend
instead of using multiple and divide?
Theoretically they should all produce the same results but it is worth checking whether or not that is the case because if there is a difference depending on the method used that may then help someone to identify what the issue may be.

RGV250

Hi,
I never turn those on so cannot comment but, is it specific just to that code. Have you tried setting them with just a flashing LED.

Bob

evoortman

#6
Quote from: Pepe on Jan 02, 2024, 09:45 PMproteus simulation and program modified

Thanks All! I supidly forgot to set Y=1 so the multiply always stayed 0
But what i still don't understand is why it did work while the copy protection was all off?