cancel
Showing results for 
Search instead for 
Did you mean: 

Alternate function remapping, OPT bytes, and PWM2

pedro23
Senior
Posted on May 30, 2012 at 10:34

Hi, since i dont get a response in discovery forum, i will repaste here.I'm trying to set a pin to an alternate remapping pin, exactly i'm using TIM2 with PWM and i want channels in D2, D3 and D4. I have tried with this:

#define OPT2 0x4803 
FLASH_Unlock (FLASH_MEMTYPE_DATA); 
FLASH_ProgramOptionByte(OPT2, 0x02); 
FLASH_ProgramOptionByte(OPT2+1, 0xFD); 
FLASH_Lock (FLASH_MEMTYPE_DATA);

with no luck, even I started to get problems with SWIM, it said that opt byte complement was different. Anyway using STVP I was able to remove that stuff. I was able to make it work by flashing my code and then chaning the opt byte with STVP....

Another problem is that after it works (0x02 in OPT2 register) if I read it I get the complemented value (0xFD), so i tried to read data memory positions 0x4801, 0x4802, 0x4804 and 0x4805 with no luck (none of them was 0x02).

k=FLASH_ReadOptionByte(OPT2);

I dont know what is wrong.

Btw, after I change OPT2 to 0x02 I start to get SWIM error messages (Error: Swim error [30007]: swim communicator error), but you can start debugging it and go step by step ... I get that error only if i press RUN or CONTINUE.

If you continue you get Error: gdi-error [40127]: flash memory not accessible while core is running.

...... 
1.TIM2_OC3Init(TIM2_OCMODE_PWM2, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_LOW ); 
2.TIM2_OC3PreloadConfig(ENABLE); 
..... 

The problem appears in line 1 .

Checking the function:


void TIM2_OC3Init(TIM2_OCMode_TypeDef TIM2_OCMode, 

TIM2_OutputState_TypeDef TIM2_OutputState, 

u16 TIM2_Pulse, 

TIM2_OCPolarity_TypeDef TIM2_OCPolarity) 

{ 

/* Check the parameters */ 

assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode)); 

assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState)); 

assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity)); 

/* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */ 

TIM2->CCER2 &= (u8)(~( TIM2_CCER2_CC3E | TIM2_CCER2_CC3P)); 

/* Set the Output State & Set the Output Polarity */ 

TIM2->CCER2 |= (u8)((TIM2_OutputState & TIM2_CCER2_CC3E ) | (TIM2_OCPolarity & TIM2_CCER2_CC3P )); 


/* Reset the Output Compare Bits & Set the Output Compare Mode */ 

TIM2->CCMR3 = (u8)((TIM2->CCMR3 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode); 


/* Set the Pulse value */ 

TIM2->CCR3H = (u8)(TIM2_Pulse >> 8); 

TIM2->CCR3L = (u8)(TIM2_Pulse); 


} 

Just on line 13

-> Continue... 
** Application stopped: Breakpoint 4, TIM2_OC3Init (TIM2_OCPolarity=TIM2_OCPOLARITY_LOW, TIM2_Pulse=750, TIM2_OutputState=TIM2_OUTPUTSTATE_ENABLE, 
-> Step over source...Error: gdi-error [40127]: flash memory not accessible while core is running 
** Application stopped 

And this is what I get when trying to read using STVP

PROJECT: 
no project 
CONFIGURATION: 
Hardware: ST-LINK 
Programming mode: SWIM 
Device name: STM8S105x6 
Port: USB 
PROGRAM MEMORY status: 
[0x008000 - 0x00FFFF] 
No File 
Not programmed 
Memory checksum: 0x0 
DATA MEMORY status: 
[0x004000 - 0x0043FF] 
No File 
Not programmed 
Memory checksum: 0x0 
OPTION BYTE status: 
No File 
Not programmed 
Option byte 0: 00 
Option byte 1: 00 
Option byte 2: 00 
Option byte 3: 00 
Option byte 4: 00 
Option byte 5: 00 
Option byte 6: 00 
Option byte 7: 00 
Option byte 8: 00 
Memory checksum: 0x0 
Hardware configuration set to <
ST-LINK
on USB with STM8S105x6 plugged in and SWIM protocol>. 
Blank check before programming (when available) is OFF 
Verify after programming is ON. 
Clear memory before loading a file is ON. 
Erase device memory before programming (when available) is OFF 
Display a popup window when an error occurs is OFF 
Log activity is OFF 
> Reading PROGRAM MEMORY area... 
Error : Error on Option Bytes (complementary bytes). Reprogram Option Bytes of device 
Error : < PROGRAM MEMORY reading failed. 
Error : < Operation aborted. 

I tried with this too, but no luck for now

#define OPT2 0x4803 
FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD); 
FLASH_Unlock (FLASH_MEMTYPE_DATA); 
//FLASH_ProgramOptionByte(OPT2+1, 0xFD); 
FLASH_ProgramOptionByte(OPT2, 0x02); 
FLASH_Lock (FLASH_MEMTYPE_DATA); FLASH. 
k=FLASH_ReadOptionByte(OPT2); 

I still getting 0xFD in k variable. Maybe the read option byte function returns the complemented value, i will check it.

But then when I debug I continue getting the 2 errors that i commented in previous port (communication and flash problems).

------- In last things, read value is sent to a u16 variable where high is normal value and low is complemented value, so thats ok, but i'm still getting errors from STVP and STVD when i program them. I have sent the questions to my stm8 provider #alternate-remapping-swim-opt2
2 REPLIES 2
jdf25252
Associate II
Posted on May 31, 2012 at 17:19

Hi

I will only comment on the first part of this post as I believe that once that initial error is corrected everything else will fall into place.

First,

FLASH_ProgramOptionByte(OPT2, 0x02);   

-----------------

FLASH_ProgramOptionByte(OPT2+1, 0xFD);

This is worse then redundant as this call will screw up the first byte of OPT3.  I don't know what the stm8 will do when the option bytes are incorrect.

-----------------

My own experience has not been good with the option bytes but that was a couple years ago before I knew what I was doing.  I reconfigure the settings a lot but NEVER on the fly.  Unless you are doing some fancy switching it shouldn't be necessary.  It is much easier to change the option bytes with STVP or STVD and if you are going to be manufacturing something it is  REALLY good practice to do the program memory & option bytes to be sure everything is the way you want.  We have actually found a few processors with non virgin option bytes.

If you look at the header file ''stm8s_flash.h'' you'll see

void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data);

Note that the first operand is called ''Address'' not Offset. To progrom OPT->OPT2 the function would expect to see 0x4803

Try:

FLASH_ProgramOptionByte( &(OPT->OPT2), 0x02);

----------------------

hope this helps!  Note that I HAVE NOT actually tried any of this.  I don't use the firmware library as I started out with this processor before it was available and have developed my own stuff.

jdf

pedro23
Senior
Posted on June 04, 2012 at 12:08

I got this form ST to make it at running time.

/* set option bytes */ 
if(FLASH_ReadByte(0x4805)!=2) 
{ 
FLASH_Unlock(FLASH_MemType_Data); 
/* Enable by HW WWDG */ 
FLASH_ProgramOptionByte(0x4805,2); 
FLASH_WaitForLastOperation(FLASH_MemType_Data); 
}