简体   繁体   中英

How to flash to STM32f103c8 using codeblocks?

I am trying to program my STM32f103c8 using STM32Cubemx, code::blocks and CubeMXMakefile.py. The compilation result shows 0 errors and 0 warnings and that flashing was successful but the code (blinking PC13) isn't running on the board.

I installed Stlink by cloning " https://github.com/texane/stlink " repo, and running make on it and checked if it worked by running lsusb and confirmed that "STMicroelectronics ST-LINK/V2" was detected.

i also had already installed the arm compilers for codeblocks and they seem to work as well.

My issue was mainly at first that the code wouldn't flash to the board and when running " CubeMXMakefile.py . " i would get a warning that st-flash is not installed although it was built from running Make on the clone, and when trying to "make flash" i would get an error that st-flash can't be found in "/usr/local/bin". I solved this issue (i think) by manually copying the the st-flash file from the build/release directory in the clone repo to the path mentioned. Now the code compiles with no errors in compiling or flashing but the code doesn't work still.

this is the code, all ofcourse generated by cubemx except for the while block:

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2019 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  *
  * @retval None
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
  HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
  HAL_Delay(500);

  }
  /* USER CODE END 3 */

}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  file: The file name as string.
  * @param  line: The line in file as a number.
  * @retval None
  */
void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

this is the compilation output:

CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
-------------- Clean: Release in testagain (compiler: GNU GCC Compiler for ARM)---------------
Executing clean command: make -f Makefile cleanRelease
rm -fR .dep build
Cleaned "testagain - Release"
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
-------------- Build: Release in testagain (compiler: GNU GCC Compiler for ARM)---------------
Scanned 0 files for #includes, cache used 0, cache updated 0
Checking if target is up-to-date: make -q -f Makefile Release
Running command: make -f Makefile Release
mkdir -p build      
C. Compiling build/system_stm32f1xx.o...
C. Compiling build/stm32f1xx_hal.o...
C. Compiling build/stm32f1xx_hal_cortex.o...
C. Compiling build/stm32f1xx_hal_dma.o...
C. Compiling build/stm32f1xx_hal_flash.o...
C. Compiling build/stm32f1xx_hal_flash_ex.o...
C. Compiling build/stm32f1xx_hal_gpio.o...
C. Compiling build/stm32f1xx_hal_gpio_ex.o...
C. Compiling build/stm32f1xx_hal_pwr.o...
C. Compiling build/stm32f1xx_hal_rcc.o...
C. Compiling build/stm32f1xx_hal_rcc_ex.o...
C. Compiling build/stm32f1xx_hal_tim.o...
C. Compiling build/stm32f1xx_hal_tim_ex.o...
C. Compiling build/main.o...
C. Compiling build/stm32f1xx_hal_msp.o...
C. Compiling build/stm32f1xx_it.o...
S. Compiling build/startup_stm32f103xb.o...
2019-01-27T23:17:52 INFO common.c: Loading device parameters....
2019-01-27T23:17:52 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
2019-01-27T23:17:52 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
C. Linking build/testagain.elf...
/usr/bin/arm-none-eabi-size build/testagain.elf
   text    data     bss     dec     hex filename
   3560      12    1572    5144    1418 build/testagain.elf
H. Linking build/testagain.hex...
B. Building build/testagain.bin...
Used gcc: 6.3.1
/usr/local/bin/st-flash erase
st-flash 1.5.1-12-g30de1b3
Mass erasing
/usr/local/bin/st-flash --reset write build/testagain.bin 0x8000000
2019-01-27T23:17:52 INFO common.c: Loading device parameters....
2019-01-27T23:17:52 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
2019-01-27T23:17:52 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
2019-01-27T23:17:52 INFO common.c: Attempting to write 3572 (0xdf4) bytes to stm32 address: 134217728 (0x8000000)
st-flash 1.5.1-12-g30de1b3
Flash page at addr: 0x08000000 erased
Flash page at addr: 0x08000400 erased
Flash page at addr: 0x08000800 erased
2019-01-27T23:17:52 INFO common.c: Finished erasing 4 pages of 1024 (0x400) bytes
2019-01-27T23:17:52 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id
2019-01-27T23:17:52 INFO flash_loader.c: Successfully loaded flash loader in sram
Flash page at addr: 0x08000c00 erased
  1/4 pages written
  2/4 pages written
2019-01-27T23:17:52 INFO common.c: Starting verification of write complete
2019-01-27T23:17:52 INFO common.c: Flash written and verified! jolly good!
  3/4 pages written
  4/4 pages written
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /home/ubunzer/Documents/CBARM/testagain.
CWD for depslib is: /home/ubunzer/Documents/CBARM/testagain.
Scanned 0 files for #includes, cache used 0, cache updated 0
Process terminated with status 0 (0 minute(s), 2 second(s))
0 error(s), 0 warning(s) (0 minute(s), 2 second(s))

try

st-flash reset

I found my MCU not running code after flashing. It worked after resetting manually.

If it does not work for you, try this with openOCD.

Tried with OpenOCD, didn't re-flash the device, just "reset run" - and device started working.

https://github.com/texane/stlink/issues/532#issuecomment-286882627

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM