gombeのブログ

マイコンの電子工作系PIC32/KiCad/C/C++/3D/

uncrustifyを使ってコードを整形

はい。整形しました。ラフガキなのに綺麗です。

・長いコメント強制改行 ・強制スペース挿入 ・長い関数(行数が40を超えるもの)の末尾に関数名を入れる ・コードスタイルはK&Rで ・適当に改行突っ込む。(w

こんなかんじです。ではサンプルソースをどうぞ。

[c] / * A skeleton main.c * Add your own code! / / Load CMSIS and peripheral library and configuration /

include "stm32f1xx_hal.h"

include "stm32f1xx.h"

include <stdint.h>

include <stdio.h>

include "xprintf.h"

/ Peripheral configuration functions / void GPIO_Config();

/ A simple busy wait loop / void Delay(volatile unsigned long delay);

uint32_t SystemCoreClock = 72000000;

UART_HandleTypeDef U2handle = { .Instance = USART2, .Init = { .BaudRate = 115200, .WordLength = UART_WORDLENGTH_8B, .StopBits = UART_STOPBITS_2, .Parity = UART_PARITY_NONE, .Mode = UART_MODE_TX_RX, .HwFlowCtl = UART_HWCONTROL_NONE, .OverSampling = UART_OVERSAMPLING_16, } };

void SystemInit(void) { volatile int i[1000] = { 0 }; i[1000 - 1] = 1 << 16; i[2] = i[1];

/ Set HSEBYP bit / / RCC->CR &= (uint32_t)0x1<<18; /

/ Set HSEON bit / RCC->CR |= (uint32_t)0x1 << 16;

/set 9x8MHz/ RCC->CFGR |= RCC_CFGR_PLLMULL9;

/use HSE/ RCC->CFGR |= RCC_CFGR_PLLSRC;

/set AHB prescaler 1:2/ RCC->CFGR |= 0x10 << 10;

FLASH->ACR = 0; /flash prefetch buffer/ FLASH->ACR |= FLASH_ACR_PRFTBE; /flash latency/ FLASH->ACR |= FLASH_ACR_LATENCY_2;

/wait get ready/ while( !( RCC->CR & ( 0x01 << 17 ))){ }

Delay(10); /PLL enable/ RCC->CR |= 0x01 << 24; Delay(10);

/while PLL is unlocked/ while( !( RCC->CR & ( 0x01 << 25 ))){ }

RCC->CFGR |= 0x02; /select PLL clock/

RCC->CR &= ~(uint32_t)( 0x01 << 16 ); SCB->VTOR = FLASH_BASE; } / SystemInit /

int __io_putchar(int ch) { HAL_UART_Transmit(&U2handle, (uint8_t*)&ch, 1, 0xFFFF); return ch; }

int main(void) { SystemInit(); / Setup the GPIOs / GPIO_Config();

HAL_Init(); { if( HAL_UART_Init(&U2handle) != HAL_OK ){ while( 1 ){ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);

    for( uint32_t i = 0; i &lt; 200000; i++ ){
    }
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);

    for( uint32_t i = 0; i &lt; 500000; i++ ){
    }
  }
} else {
  xdev_out(__io_putchar);

  while( 1 ){
    xprintf(&quot;HelloWorld\n&quot;);
    xprintf(&quot;HelloWorld%d\n&quot;, 114514);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);

    for( int i = 0; i &lt; 500000; i++ ){
    }

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);

    for( int i = 0; i &lt; 500000; i++ ){
    }
  }
}

} } / main /

void Delay(volatile unsigned long delay) { for(; delay; --delay ){ } }

void GPIO_Config() { RCC->APB2ENR |= ( 0x01 << 4 ) | ( 0x01 << 5 );

GPIOD->CRL = 0x42444444; GPIOD->CRH = 0x44244444;

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_USART2_CLK_ENABLE();

/ GPIO_InitStruct.Pin = GPIO_PIN_2; / / GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; / / GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; / { GPIO_InitTypeDef gpio = { GPIO_PIN_2, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH, }; HAL_GPIO_Init(GPIOA, &gpio); }

{ GPIO_InitTypeDef gpio = { GPIO_PIN_3, GPIO_MODE_INPUT, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH }; HAL_GPIO_Init(GPIOA, &gpio); } } / GPIO_Config / [/c]

設定ファイルはこんなかんじです。 [shell] #

K&R (sort-of)

#

indent_with_tabs = 0 # 1=indent to level only, 2=indent with tabs input_tab_size = 8 # original tab size output_tab_size = 2 # new tab size indent_columns = output_tab_size indent_label = 2 # pos: absolute col, neg: relative column indent_align_assign = true

indent_align_string = False # align broken strings

indent_brace = 0

nl_enum_brace = remove # "enum {" vs "enum \n {" nl_union_brace = remove # "union {" vs "union \n {" nl_struct_brace = remove # "struct {" vs "struct \n {" nl_do_brace = remove # "do {" vs "do \n {" nl_if_brace = remove # "if () {" vs "if () \n {" nl_for_brace = remove # "for () {" vs "for () \n {" nl_else_brace = remove # "else {" vs "else \n {" nl_while_brace = remove # "while () {" vs "while () \n {" nl_switch_brace = remove # "switch () {" vs "switch () \n {"

nl_func_var_def_blk = 1

nl_before_case = 1

nl_fcall_brace = remove # "foo() {" vs "foo()\n{" nl_fdef_brace = remove # "int foo() {" vs "int foo()\n{"

nl_after_return = TRUE

nl_brace_while = remove nl_brace_else = remove nl_squeeze_ifdef = TRUE

nl_max = 3 # max of new line nl_after_func_proto = 1 nl_after_func_proto_group = 2 nl_after_func_body = 2 nl_after_func_body_one_liner = 2 nl_after_multiline_comment = true nl_after_struct = 2

cmt_star_cont = true cmt_sp_before_star_cont = 0 cmt_sp_after_star_cont = 5 cmt_width = 80 cmt_indent_multi = true cmt_c_nl_end = true cmt_cpp_to_c = true cmt_star_cont = true

mod_paren_on_return = add # "return 1;" vs "return (1);"

mod_full_brace_if = add # "if (a) a--;" vs "if (a) { a--; }" mod_full_brace_for = add # "for () a--;" vs "for () { a--; }" mod_full_brace_do = add # "do a--; while ();" vs "do { a--; } while ();" mod_full_brace_while = add # "while (a) a--;" vs "while (a) { a--; }" mod_add_long_function_closebrace_comment = 30 mod_add_long_switch_closebrace_comment = 30 mod_add_long_ifdef_else_comment = 30 mod_remove_empty_return = remove

sp_before_semi = remove sp_paren_paren = remove # space between *1 sp_return_paren = remove # "return (1);" vs "return(1);" sp_sizeof_paren = remove # "sizeof (int)" vs "sizeof(int)" sp_before_sparen = remove # "if (" vs "if(" sp_after_sparen = remove # "if () {" vs "if (){" sp_after_cast = remove # "(int) a" vs "(int)a" sp_inside_braces = force # "{ 1 }" vs "{1}" sp_inside_braces_struct = force # "{ 1 }" vs "{1}" sp_inside_braces_enum = force # "{ 1 }" vs "{1}" sp_inside_paren = force # "( 1 )" vs "(1)" sp_inside_fparen = remove # "( 1 )" vs "(1)" - functions sp_inside_sparen = force # "( 1 )" vs "(1)" - if/for/etc

sp_type_func = ignore

sp_assign = force sp_arith = force sp_bool = force sp_compare = force sp_after_comma = force sp_func_def_paren = remove # "int foo (){" vs "int foo(){" sp_func_call_paren = remove # "foo (" vs "foo(" sp_func_proto_paren = remove # "int foo ();" vs "int foo();" mod_remove_extra_semicolon = true

align_with_tabs = FALSE # use tabs to align

align_on_tabstop = FALSE # align on tabstops

align_enum_equ_span = 4

align_nl_cont = TRUE

align_var_def_span = 2

align_var_def_inline = TRUE

align_var_def_star = TRUE

align_var_def_colon = TRUE

align_assign_span = 1

align_struct_init_span = 3

align_var_struct_span = 3

align_right_cmt_span = 3

align_pp_define_span = 3

align_pp_define_gap = 4

align_number_left = TRUE

align_typedef_span = 5

align_typedef_gap = 3

cmt_star_cont = TRUE

eat_blanks_before_close_brace = TRUE eat_blanks_after_open_brace = TRUE [/shell]  

*1: and