cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303xC bootloader

ArturasV
Senior
Posted on May 16, 2014 at 16:12

Hi everyone, i'm trying to enter bootloader from my application, so i could use STM flash loader tool to upgrade software. Everything is working fine and im entering bootloader, but after i flash with flash loader tool, nothing is working, even though i get successful program verifying messages. I tried to reset processor., connecting NRST to ground after flashing with the loader, but that doesn't help. So i suppose i did something wrong with configuration, before entering bootloader and stopping all processes. Or maybe it's not a good idea to init my bootloader function in USART IRQ handler? Any advices and help which would allow me to solve this will be highly appreciated. Thanks in advance. 🙂

Code:

#include ''stm32f30x.h''

#include ''main.h''

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

void (*SystemMemoryBootJump) (void);

void BootLoaderInit(uint32_t BootLoaderStatus);

void USART1_Config(void)

{

  USART_InitStructure.USART_BaudRate = 9600; // Baud rate. Bits (or characters) per second.

  USART_InitStructure.USART_WordLength = USART_WordLength_8b; // Word lenght = 8 bits

  USART_InitStructure.USART_StopBits = USART_StopBits_1; // Use one stop bit

  USART_InitStructure.USART_Parity = USART_Parity_No ; // No parity

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Receive and transmit enabletd

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // Hardware flow control disabled (RTS and CTS signals)

  

  USART_Init(USART1, &USART_InitStructure);

  

  USART_Cmd(USART1, ENABLE); // Enable USART1 parameters

  

  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable Rx interrupt

 

  NVIC_EnableIRQ(USART1_IRQn); // Enable USART1 global interrupt

}

void USART1_Rx_Tx_Config(void)

{

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; // use 9 and 10 pins for rx and tx

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);

}

void USART1_IRQHandler(void)

{

  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Check if interrupt has occured or not

    {

        if((char)USART_ReceiveData(USART1) == 't') // If recieved t on rx pin , transmit T from tx pin

        {

          GPIOE->ODR ^= GPIO_Pin_8; // Toggle led state

          USART_SendData(USART1, 'T'); // Transmit T

        }

        

        if((char)USART_ReceiveData(USART1) == 0x7A)

        {

          //USART_SendData(USART1, 0xEE);

          BootLoaderInit(1);

        }

        

        if((char)USART_ReceiveData(USART1) == 'o') // If recieved t on rx pin , transmit T from tx pin

        {

          GPIOE->ODR ^= GPIO_Pin_9; // Toggle led state

          USART_SendData(USART1, 0x3C); // Transmit T

        }

    }

}

int main(void)

{

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable clock for port A

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); // Enable clock for port E

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // USART1 clock enable

  

  USART1_Config();

  USART1_Rx_Tx_Config();

  

  // Port E pin 8 blue led configuration

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // use 8 pin to blink led

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOE, &GPIO_InitStructure);

  

  // NVIC structure for USART1 interrupt

  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

 // USART1_IRQHandler();

   while(1)

   {

  

     // Do nothing, all happens in ISR (Interrupt service routine)  

   }

}

void BootLoaderInit(uint32_t BootLoaderStatus)

{

    SystemMemoryBootJump=(void (*)(void)) (*((u32 *) 0x1FFFD804));// Point PC to System Memory reset vector (+4 the offset)

    

  if(BootLoaderStatus == 1)

  {

    RCC_DeInit(); // Shut down all running tasks. Set all clock configuration to the reset state

    SysTick->CTRL = 0; // Reset the system timer

    SysTick->LOAD = 0; // Reset system reload value register

    SysTick->VAL = 0; // Reset system Current value register.

    

  //  __set_PRIMASK(1); // Disable all interrupts

    __set_MSP(0x20001258); // Set the main stack pointer value

    

    SystemMemoryBootJump(); // Jump to the boot mode

    

 //   while(1);

  }   

}

4 REPLIES 4
Posted on May 16, 2014 at 16:51

Hi everyone, i'm trying to enter bootloader from my application, so i could use STM flash loader tool to upgrade software. Everything is working fine and im entering bootloader, but after i flash with flash loader tool, nothing is working, even though i get successful program verifying messages. I tried to reset processor., connecting NRST to ground after flashing with the loader, but that doesn't help. So i suppose i did something wrong with configuration, before entering bootloader and stopping all processes. Or maybe it's not a good idea to init my bootloader function in USART IRQ handler? Any advices and help which would allow me to solve this will be highly appreciated. Thanks in advance. 🙂

Or, that the code you uploaded is simply non-functional.

Use a debugger, or have some code early in the Reset_Handler to output progress/waypoint indications via GPIO, LED or USART

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ArturasV
Senior
Posted on May 16, 2014 at 17:08

Or, that the code you uploaded is simply non-functional.

 

How can it be, if i download the code for the first time via ST-link and USART interrupt is working, blue or red led turns on when a character is received via USART. Even the entering bootloader command works because then i can use flash loader.

have some code early in the Reset_Handler to output progress/waypoint indications via GPIO, LED or USART

And could you be more specific, what i should look at? It's a first time i'm trying to do something like this.

Thanks in advance.

Posted on May 16, 2014 at 18:02

How can it be, if i download the code for the first time via ST-link and USART interrupt is working, blue or red led turns on when a character is received via USART. Even the entering bootloader command works because then i can use flash loader.

 

Ok, I'm trying to follow the narrative here, but after you enter the System Loader code, and you then erase and download something else, it's this new thing that isn't running, or you're breaking your original loader? The System Loader is not going to come back to your original call, the reset is the way it will restart.

Can you use the ST-Link (Utilities) to read out what you've managed to write in with the Flash Loader Demonstrator? How does this new content materially differ from the loader that you put in initially?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ArturasV
Senior
Posted on May 19, 2014 at 13:47

Your advice helped me to check the contents of the program. And what i found out that look at my code which i posted above. For some reason the line __set_PRIMASK(1) is commented, and it shouldn't be. After that, i found out that is not possible to get program to work this way. When i just set up any random pin to the high level when i get command on usart, and then check the pin state in a while loop if(input_databit ==1){BootloaderInit(1)} everything works fine and i was able to use the flash loader without any problems. So i guess is that because i have to shut down all interrups, i shut them down before doing all tasks which are required to enter bootloader. I believe that is why it wasn't working.