Advertisement

Membangun Aplikasi STM32 Dengan STM32CubeMX (Bagian 2)

0 1,920

Pada bagian 1, embeddednesia telah membahas bagaimana mengenerate source code dari CubeMX. Pada tulisan ini, penulis akan mengulas bagaimana caranya mengimpor source code tersebut kedalam eclipse IDE. Pastikan pembaca telah menginstal Eclipse CDT dengan plugin GNU MCU EClipse. Pernah dibahas penulis pada tulisan sebelumnya. Silahkan baca di tautan ini tentang bagaimana cara menginstalnya.

  • Install Compiler GNU ARM

Ada banyak pilihan compiler / toolchain yang bisa digunakan. Namun dalam tutorial ini yang digunakan penulis adalah GNU ARM karena sifatnya yang gratis dan open source. Alternatif lainnya, anda bisa gunakan compiler dari microC ataupun Keil uVision. Klik disini untuk membuka halaman pengunduhan. Dan pilih sesuai sistem operasi yang digunakan.

penulis menggunakan versi exe nya  gcc-arm-none-eabi-5_3-2016q1-20160330-win32.exe. Setelah selesei diunduh, install, dan ikuti petunjuk instalasinya.

Untuk sistem operasi windows 64 bit, secara default, biasanya folder instalasi akan berada di C:\Program Files (x86)\GNU Tools ARM Embedded. Buka folder tersebut dan masuk direktori bin lalu test apakah proses instalasi berhasil. Gunakan command –version

C:\Program Files (x86)\GNU Tools ARM Embedded\5.3 2016q1\arm-none-eabi\bin>arm-none-eabi-addr2line.exe --version
GNU addr2line (GNU Tools for ARM Embedded Processors) 2.26.0.20160310
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.
  • Buat Project Eclipse

Pada Eclipse CDT, buat project baru melalui menu File > New C/C++ Project. Kemudian Pilih template C++ Managed Build pada jendela dialog New C/C++ Project  Lalu klik Next.

Pada kotak dialog berikutnya, isi nama project pada Project Name. Pada contoh, penulis memberi nama Project STM32LoveCubeMx, pada kotak Project Type pilih direktori Executable > Hello World ARM Cortex-M C/C++. Klik Next

Pada dialog kedua, isi field Processor Core, Clock (Hz), Flash Size (kB) dan RAM size (kB) sebagaimana gambar 3. Nilai – nilai ini didapatkan dari datasheet STM32F10C8T6. pembaca dapat menyesuaikan dengan mirkokontroler yang digunakan.

Pada dialog ketiga, biarkan semua field pada kondisi default kecuali Vendor CMSIS name. Field tersebut memiliki pattern <stm32family>xx. Misalnya pada mikrokontroler yang sedang dipakai  tulis stm32f1xx. .

Saat tiba pada jendela dialog GNU ARM Cross ToolChain, biasanya Eclipse akan otomatis mendeteksi toolchain GNU Tools Arm Embedded yang telah diinstall sebelumnya. Jika tidak ada, atur terlebih dahulu pada direktori dimana direktori instalasi gnu arm berada. Setelah itu klik Finish.

 

Setelah project terbentuk, delete project terlebih dahulu, dengan klik kanan direktori project lewat Project Explorer lalu pilih pilih Delete

  • Install Python

Python diperlukan untuk menjalankan script yang menggunakan bahasa pemrograman Python yang diperlukan untuk me-copy project CubeMX yang telah dibuat pada bagian 1 ke Project Eclipse yang baru saja dibuat. Jika pembaca ingin tahu tentang python dan bagaimana menginstalnya, silahkan buka tautan ini untuk mengujungi tulisan penulis tentang python. Penulis menggunakan Python 2.7

Setelah python terinstall, untuk pengguna Windows perlu menginstall library tambahan lxml. Pembaca dapat mengunduhnya di tautan ini

  • CubeMXImporter

Unduh script CubeMXImporter melalui Github. Jika di komputer pembaca telah terinstall Git, jalankan script berikut untuk melakukan clone

git clone https://github.com/cnoviello/CubeMXImporter

Pembaca juga bisa mengunduhnya langsung dengan mengunjungi tautan Github di sini. Lalu memilih button download di pojok kanan atas. Script dapat dijalankan dengan format berikut

$ python cubemximporter.py <path-to-eclipse-project> <path-to-cubemx-project>

Misalnya pada environment komputer milik penulis sebagai berikut

  • Source project di Eclipse : D:\project\workspace-cpp\STM32LoveCubeMX
  • Source code CubeMX: D:\project\cubemx\STM32LoveCubeMX

maka script cubemximporter dapat ditulis sebagai berikut

python cubemximporter.py “D:\project\workspace-cpp\STM32LoveCubeMX” “D:\project\cubemx\STM32LoveCubeMX”

Pada Eclipse IDE import project eclipse tersebut melalui menu File > Import > Existing Project Into workspace lalu pilih direktori project eclipse tadi berada. Setelah project berhasil diimpor, perhatikan, pada System > stm32f1xx kini telah diisi source code yang berasal dari CubeMX. Untuk mencoba  melakukan build, klik kanan project tersebut melalui Project Explorer, lalu pilih Build Project

  • Ubah program Main.c

Berikut adalah code main.c


/**
******************************************************************************
* @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) 2018 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 */
GPIOC->BRR |= 1<<13;
for (int i = 0; i < 1000000; ++i) asm("nop");
GPIOC->BSRR |= 1<<13;
for (int i = 0; i < 500000; ++i) asm("nop");
/* USER CODE BEGIN 3 */
}
/* 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.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****/
view raw

main.c

hosted with ❤ by GitHub

Pada bagian 1, GPIO_PIN_13 telah diatur sebagai GPIO_Output, perhatikan pada baris 184-187, konfigurasi tersebut sekarang telah diimport ke project eclipse. Untuk membuat LED internal board development STM32F103C8T6 berkedip – kedip, tambahkan sintaks berikut pada bagian superloop di fungsi main. Lalu build project

  • Unduh file Hex ke Board Mikrokontroler

Setelah file hex didapatkan, kini jalankan STLINK Utilty. Pembaca dapat mengunduhnya di tautan ini.

Pastikan juga STLINK dan Mikrokontroler target telah terhubung. Klik Connect untuk membuka koneksi antara komputer ke mikrokontroler target. Lalu Pilih File > Open (atau gunakan shortcut Ctrl+O) dan pilih lokasi file Hex berada.

Untuk mulai memprogram gunakan Target > Program & Verifiy (atau gunakan shortcut Ctrl+P). Pada jendela dialog konfirmasi download, klik Start untuk memulai proses download ke mikrokontroler target. Setelah proses download berhasil, jangan lupa untuk memutus sambungan dengan mikrokontroler dengan mengklik disconnect.

Baca juga tulisan tentang #Tutorial Mikrokontroler STM32 atau tulisan menarik lainnya di kategori Mikrokontroler  ARM 32 bit

Tinggalkan Balasan

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.