| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				| Write calibration data to HEF |  
				|  Posted: Thu Nov 04, 2021 4:57 am |   |  
				| 
 |  
				| Dear all, 
 PCW compiler IDE/PCB/PCM version 5.105        <16F15224.H>
 
 I write some calibration data (fairly accurate and good enough for the first start-up) to the HEF when I program the µcontroller for the first time.
 Then I execute a calibration routine to save the exact values to the HEF. At the same time a variable is changed so I can see the calibration routine is executed.
 
 Problem:
 Later I want to update (reprogram) the µcontroller but now it's not allowed to overwrite the accurate calibration data of the HEF. Does anybody has a good idea how to get it done?
 I just need to prevent to overwrite the HEF values but at the same time I want to be able to program a new (empty) µcontroller.
 
 Working with the HEF is no problem. Programming, reading and rewriting works well. Underneath a brief overview of the used code.
 
  	  | Code: |  	  | // command to define and load data into the HEF// #define  HEF 0X0F80 // 0x0FFF is last adress for 16F15224
 #ROM     int16 HEF = {1,84,0b11001100} // calibration data
 
 struct twobytes
 {
 int8 l;
 int8 h;
 };
 
 union  prog_mem
 {
 int16 word;
 struct twobytes b;
 };
 //
 struct   {
 union prog_mem OTP_ref_hi_hef;
 union prog_mem OTP_ref_lo_hef;
 union prog_mem version_bits_hef;
 }
 values;
 //
 read_program_memory(HEF,&values,6);                                             // 6 geeft aan dat er 3 x 2 = 6 bytes zijn gebruikt.
 OTP_ref      = make16(values.OTP_ref_hi_hef.b.l,values.OTP_ref_lo_hef.b.l);
 version_bits = values.version_bits_hef.b.l;
 //
 values.OTP_ref_hi_hef.b.l = make8(OTP_ref,1);         // MSB
 values.OTP_ref_lo_hef.b.l = make8(OTP_ref,0);         // LSB
 values.version_bits_hef.b.l = version_bits;
 write_program_memory(HEF, &values, 6);
 | 
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 6:34 am |   |  
				| 
 |  
				| You can't. What you have to do, is go into the data window in your programmer,
 read the chip, then record the values that are in the HEF area, and then
 either add these to the code that is being programmed in, or load/type
 them back into this area on the new chip.
 The chip does not allow an option to protect the HEF area when being
 erased for programming.
 |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 6:53 am |   |  
				| 
 |  
				| re:  	  | Quote: |  	  | The chip does not allow an option to protect the HEF area when being erased for programming.
 | 
 hmm, seems kinda 'silly' to me.....NOT being able to protect the HEF from being erased.... I know other PICs, you can  protect certain banks of memory from being erased.
 
 Wondering if he can put the calibration data in the area that can be used for a serial number, and be protected from erasing ?
 |  | 
	
		|  | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 7:50 am |   |  
				| 
 |  
				| Dear Ttelmah, 
 That sounds logical. But what if the chip is read-protected?
 I will investigate if it's possible to NOT apply a read-protect to the last memory locations where the HEF values are stored so you can read them. But I think read, modify the code and program the chip is too difficult for most people (customers).
 
 At page 102 of the datasheet I also found another memory type.
 "NVM - Nonvolatile Memory Control".
 What is it and is it supported by CCS?
 
 PS.
 The code I use to write, read and modify the HEF is it still common used or is a more easy way available?
 
 Thank you,
 Bas
 |  | 
	
		|  | 
	
		| gaugeguy 
 
 
 Joined: 05 Apr 2011
 Posts: 350
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 8:05 am |   |  
				| 
 |  
				| If your product has some type of external interface (UART, I2C, SPI) then add code to send out calibration data as well as to set the calibration data. If you add a bootloader to your product then the bootloader could be written to always preserve the calibration data when updating the operational code.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 8:09 am |   |  
				| 
 |  
				| I'm now totally puzzled. Are you sure you have posted the right chip number?.
 The 16F15224, does not have HEF.....
 It does have the SAF area. This can be write protected.
 You would have to set your programmer to not do a bulk erase. This is
 normally an option.
 |  | 
	
		|  | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 8:30 am |   |  
				| 
 |  
				| Dear Ttelmah, 
 Yes it's a 16F15224.
 
 I think I must apologize.
 I use the term "HEF" but apparently that's not the right name for it.
 Anyway I'm able to write, read and modify some Program Flash memory locations. I hope this is a better description.
 
 Sorry but I'm a hardware designer and like to use the intelligence of a small µcontroller.
 
 Thank you all for your suggestions.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 04, 2021 9:07 am |   |  
				| 
 |  
				| OK. Makes sense.
 HEF is 'high endurance flash', usually one page of flash, built to have much
 better durability than the standard flash.
 
 You chip does not have this just normal flash. However yours does allow an
 area of 128bytes at the top of the memory to be designates as 'SAF', which
 is a special area for user settings. Now this (brilliantly for you), does support
 being protected. You need to set the SAF fuse. With this enabled, the
 compiler will not put code into the top 128bytes of the ROM.
 
 Now in the programmer, if you set it not to do a full erase before
 programming, but to only erase the needed blocks, it should leave this
 unchanged.
 After writing your configuration data, you could then update the configuration
 in the code, and turn on the NOWRTSAF fuse. This then makes this area
 write protected.
 A full erase from the programmer would clear it if needed.
 |  | 
	
		|  | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Fri Nov 05, 2021 4:11 am |   |  
				| 
 |  
				| Thanks Ttelmah, 
 I will investigate and post the fuse settings and code when ready.
 So other people can use it too.
 
 Best regards,
 Bas
 |  | 
	
		|  | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Tue Nov 23, 2021 9:44 am |   |  
				| 
 |  
				| Maybe a stupid question. According the datasheet of the 16F15224:
 
 9.1.2.3 Storage Area Flash
 Storage Area Flash (SAF) is enabled by clearing the SAFEN bit. If enabled, the SAF block is placed at the end of memory and spans 128 words.
 
 1)
 Memory of the 16F15224 ends at 0X0FFF.
 Does it mean the SAF is located from 0x0F80 to 0x0FFF?
 
 Thanks in advance,
 |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Nov 23, 2021 10:35 am |   |  
				| 
 |  
				| curious, I downloaded the datasheet.. yes 0F80x-0FFFx
 |  | 
	
		|  | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Wed Nov 24, 2021 4:17 am |   |  
				| 
 |  
				| Step by step 
 Ok the SAF is in the range of 0xF80-0xFFF.
 
 When I enable the SAF     #Fuses SAF, WRTSAF in the C-code
 I can check the configuration settings by use of the CCS Device Programmer (CCSload program).
 - The Storage Area Flash is enabled.
 - Storage Area Flash is write protected.
 So far so good.
 
 But what I can't see in the memory map is the "reserved" SAF memory.
 I expected to see a "reserved" range from  0xF80-0xFFF but that's not the case. The memory overview just stops at the last row of code.
 
 
 Question:
 How can I place a few values in the SAF?  I assume the same way as in the Flash.
 #define  FLASH 0X0F80            // 0X0FFF is last adress for 16F15224
 #ROM     int16 FLASH = {1,84,101}
 Now I can see at the memory map the "reserved" 3 memory addresses (0xF80,0xF81, 0xF82)
 
 Now I will perform some tests to see if I can change the 3 values in the SAF.
 More will come soon.
 |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Nov 24, 2021 6:10 am |   |  
				| 
 |  
				| While I don't have that PIC or use CCSLoad.... According to the datasheet, BB? has to be '1', SAF has to be '0'  to enable SAF.
 you say SAF is write protected ! THAT has to be changed before your can access the SAF, at least to write data into that area of flash...
 
 You should post your test program, there may be something obvious to others that you don't see.
 |  | 
	
		|  | 
	
		| bschriek 
 
 
 Joined: 18 Dec 2007
 Posts: 82
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Thu Nov 25, 2021 8:31 am |   |  
				| 
 |  
				| Can I change the #Fuse settings when running the program? 
 See my first post:
 I write some calibration data (fairly accurate and good enough for the first start-up) to the SAF when I program the µcontroller for the first time.
 Then I execute a calibration routine to save the exact values to the SAF. At the same time a variable is changed so I can see the calibration routine is executed. Now I need to change the Fuse settings so the SAF becomes write-protected.
 
 Any idea?
 
 PS  Fuse settings underneath works great.
 #Fuses SAF,WRTSAF        // Prevents against writing to SAF memory block
 #Fuses SAF,NOWRTSAF    // Writing to SAF memory block
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 25, 2021 12:20 pm |   |  
				| 
 |  
				| Yes, but.... You don't change the fuses. You directly write to the configuration area.
 You can only change bits from the erased to the set state.
 You would need to work out what needs to be in this area to disable
 the write, and write this.
 |  | 
	
		|  | 
	
		|  |