STM8S---IO multiplexing of option byte write operation

Publisher:数据梦想Latest update time:2018-04-13 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Functional realization goal

The PWM output IO multiplexing of the CH3 channel of TIM2 can   be realized by writing the option byte, and can be set to PA3 or PD2 output. 

Option Byte

  The option bytes include the configuration of the chip hardware features and the protection information of the memory. These bytes are stored in a dedicated block in the memory. In addition to the ROP (read-out protection) byte, each option byte must be saved twice, one in the usual format (OPTx) and one in the backup complementary format (NOPTx). The option bytes can be modified by the application in IAP mode, but the ROP option can only be modified in ICP mode (through SWIM). For the content of the SWIM programming process, please refer to the STM8S Flash Programming Manual (PM0051) and the STM8 SWIM Communication Protocol and Debug Module User Manual (UM0470). 
  The option bytes of different chips are different in size. For details, please refer to the chip data sheet. Taking STM8S103F3 as an example, the option bytes are as follows:

Option Byte

  STM8S103F redefines the multiplexing function bits for 20-pin package products:

STM8S103F multiplexing function redefine bit

  From this we can know that we need to write the AFR1 bit in the OPT2 byte. By writing 0, port A3 multiplexes the function TIM2_CH3, and by writing 1, port D2 multiplexes the function TIM2_CH3. Next, implement this function through the program. You can modify the value of AFR1 to see if the PWM output switches the pin. If it can, the write operation is implemented. 
  The option bytes are stored in the EEPROM, so the option bytes can be modified in the same way as reading and writing EEPROM. The application can write directly to the target address. We already know the address from the figure above: 0x4803, 0x4804. The configuration of the register can be found in the reference manual (RM0016). 
  Related register operations:

FLASH_CR2

FLASH_NCR2

Test program implementation

  Note: When implementing the program to erase Option Bytes, the application cannot be run, otherwise an error will occur! But I still think this is a bit troublesome. It would be better to use STVP to erase and write. It would be great if it can be put in the application to erase and write. Is it so troublesome to use the program to implement multiplexing? I hope to explore and find a good method. In the end, I only found an unreliable one, which is to add a delay after erasing, but this sometimes works and sometimes doesn't. Or should I check the information to see what's going on?

ST Visual Develop

  But when using STVP to erase, I encountered this error:

Error : Error on Option Bytes (complementary bytes). Reprogram Option Bytes of device
Error: < OPTION BYTE verifying failed.123

  Use STVP to erase Option Bytes. First set ROP to ON, then erase Option Bytes. Two prompt boxes will appear. Select Yes (Y). Then set ROP to OFF again and erase Option Bytes again. Then you can use STVD to burn the program and simulate through stlink.

STVP program option bytes

T1

T2

test program:

/* MAIN.C file

Functons: Operate the option byte, set IO multiplexing, and modify the PWM output pin PA3 or PD2 of CH3 channel of TIM2

Date : July 22, 2015

Author : yicm

Notes:   

 */


#include


void CLK_init(void)

{

    CLK_ICKR |= 0X01; //Enable internal high-speed clock HSI

    CLK_CKDIVR = 0x08; //16M internal RC is divided by 2 and the system clock is 8M

    while(!(CLK_ICKR&0x02)); //HSI is ready 

    CLK_SWR=0xe1; //HSI is the main clock source 

}


void Init_GPIO(void)

{

    /*Set to push-pull output, PD2 is connected to LED light*/

    PD_DDR |= 0X04; //Set PD2 port to output mode

    PD_CR1 |= 0X04; //Set PD2 port to push-pull output mode

    PD_CR2 &= 0XFD;


    PA_DDR |= 0X08; //Set PA3 port to output mode

    PA_CR1 |= 0X08; //Set PA3 port to push-pull output mode

    PA_CR2 |= 0XF7;

}


void Init_Tim2(void)

{

    TIM2_CCMR3 |= 0X70; //Set the output comparison mode of timer 2 three channels (PD2)

    TIM2_CCMR3 |= 0X04; //Output compare 3 preload enable


    TIM2_CCER2 |= 0x03; //Channel 3 is enabled, low level is valid, and it is configured as output


    // Initialize the clock divider to 1, that is, the clock frequency of the counter is Fmaster=8M/64=0.125MHZ

    TIM2_PSCR = 0X07;   

    // Initialize the auto-load register to determine the frequency of the PWM square wave, Fpwm=0.125M/62500=2HZ

    TIM2_ARRH = 62500/256;

    TIM2_ARRL = 62500%256;

    // Initialize the comparison register to determine the duty cycle of the PWM square wave: 5000/10000 = 50%

    TIM2_CCR3H = 31250/256;

    TIM2_CCR3L = 31250%256;



    //Start counting; Update interrupt disable

    TIM2_CR1 |= 0x81;

    //TIM2_IER |= 0x00;        

}


void Write_Option_Byte(void)

{   

    unsigned char opt[6] = {0,0,0x00,0,0,0};


    /*Unlock Flash*/

    do

    {

        FLASH_DUKR = 0xAE;

        FLASH_DUKR = 0x56;      

    }

    while(!(FLASH_IAPSR & 0X08));


    /*Enable write operation to option bytes*/

    FLASH_CR2 = 0X80;

    /*Complementary control register*/

    FLASH_NCR2 = 0X7F;

    /*Write operation, 0x02: PD2. 0x00: PA3*/

    *((unsigned char *)0x4800) = opt[0];


    *((unsigned char *)0x4801) = opt[1];

    *((unsigned char *)0x4802) = ~opt[1];


    *((unsigned char *)0x4803) = opt[2];

    *((unsigned char *)0x4804) = ~opt[2];


    *((unsigned char *)0x4805) = opt[3];

    *((unsigned char *)0x4806) = ~opt[3];


    *((unsigned char *)0x4807) = opt[4];

    *((unsigned char *)0x4808) = ~opt[0];


    *((unsigned char *)0x4809) = opt[5];

    *((unsigned char *)0x480A) = ~opt[5];  


    /*Wait for writing to complete*/

    while(!(FLASH_IAPSR & 0x04));

}


main()

{

    int i;


    Write_Option_Byte(); //When running the program, shield  

    for(i=0;i<10000;++i); //Delay effect. Sometimes adding a delay can make the erase and application program work without blocking at the same time.


    CLK_init(); //Shield during erase, otherwise an error will occur during the next stlink simulation

    Init_GPIO(); //Shield during erase, otherwise an error will occur during the next stlink simulation

    Init_Tim2(); //Shield during erase, otherwise an error will occur during the next stlink simulation

    while (1);

}



Keywords:STM8S Reference address:STM8S---IO multiplexing of option byte write operation

Previous article:The difference between PLC control and single chip microcomputer control
Next article:STM8S---Power consumption management halt mode (halt) implementation

Recommended ReadingLatest update time:2024-11-16 03:52

STM8S_ 006_AWU automatic wake-up
Preface Ⅰ In some low-power devices, the device needs to enter low power consumption and wake up the MCU at a certain interval, so a timed "AWU automatic wake-up" function is needed. To implement the above functions in STM32, the common operation is to use RTC. However, RTC needs to be configured before entering l
[Microcontroller]
STM8S ADC self-study notes 1
Because the chip is stm8s105c6t6, the internal ADC is ADC1. I looked at some registers of ADC1 a few days ago and wrote a simple single conversion program. Using 1602 display, the last 2 digits of the displayed voltage value jump a lot. The 10-bit AD accuracy to mV level should be no problem. I don't know if it is rel
[Microcontroller]
STM8S ADC initialization settings and applications
//ADC channel number definition #define ADC_Chanel0 (unsigned char)0x00 #define ADC_Chanel1 (unsigned char)0x01 #define ADC_Chanel2 (unsigned char)0x02 #define ADC_Chanel3 (unsigned char)0x03 #define ADC_Chanel4 (unsigned char)0x04 #define ADC_Chanel5 (unsigned char)0x05 #define ADC_Chanel6 (unsi
[Microcontroller]
STM8S assembly code analysis
  br data-filtered="filtered" Among them, .asm file is the source file of assembly code, and .inc file is the include file, which is similar to .c file and .h file in a class="replace_word" title="C language knowledge base" href="http://lib.csdn.net/base/c" target="_blank" C language /a . Next, let's analyze these th
[Microcontroller]
STM8S assembly code analysis
STM8S three clock source configuration HSE\HSI\LSI configuration
1. About the HSE clock configuration as the main clock static void CLK_Config(void) {     CLK_DeInit(); // Initialization     CLK_HSECmd(ENABLE); //Enable HSE    CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE,                                 CLK_CURRENTCLOCKSTATE_DISABLE); //Switch HSE and turn o
[Microcontroller]
STM8S three clock source configuration HSE\HSI\LSI configuration
STM8S operates internal EEPROM
For the STM8S103F3 chip, there is 640 bytes of EEPROM. Of course, if there is an external EEPROM, the internal EEPROM will not be used. But small things also have big uses. For example, when doing wireless applications, while reducing costs, some specific settings can be saved, such as: sleep time, operating frequency
[Microcontroller]
STM8S Learning 01——SPI&IIC
1. Review the IIC bus protocol 1) Some characteristics of the I2C bus 1 Only two bus lines are required: one serial data line SDA and one serial clock line SCL 2 Each device connected to the bus can be addressed by software with a unique address and a simple master-slave relationship that always exists. The master can
[Microcontroller]
STM8S Learning 05——EEPROM read and write operation C language program
/* During the project development in the past two days, the EEPROM function of STM8 was used. Several data needed to be saved after power failure. I also checked the EEPROM operation on the Internet and found that there were two main problems on the Internet: 1. EEPROM reading and writing were unsuccessful; 2. The EEP
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号