Shujima Blog

Apple製品,技術系の話をするブログ

STM32 NUCLEO-F746ZG Zephyr PWM出してみた(自分用メモ2)

自分用memo

やってること

STM32 Nucleo-F746ZGボードでPWM出力可能な全ピン32ヶ所からPWMを出す。

ただし、

www.shujima.work

こちらの記事とは違って、ボードファイルから作るパターンを試してみた。 実行結果は変わらない。

作成

構成は以下の通り

  • prj_f746
    • boards
      • arm
        • prj_f746
          • prj_f746_defconfig
          • prj_f746.dts
          • prj_f746.yaml
          • Kconfig.board
          • Kconfig.defconfig
          • board.cmake
          • support/
            • openocd.cfg
    • src
      • main.c
    • CMakeLists.txt
    • prj.conf

ただし

  • src
    • main.c
  • CMakeLists.txt
  • prj.conf

は前と変わらないため省略。

前回作成していたapp.overlayは、今回ボードのdtsファイルを自由に記述できることから、内容をその中に組み込むこととした。

boards/arm/prj_f746/prj_f746.defconfig

ボード固有のコンフィグを設定。 プロジェクト内で必要がある場合は、prj.confに記載することによって追記できる。

zephyr/nucleo_f746zg_defconfig at main · zephyrproject-rtos/zephyr · GitHub

boards/arm/prj_f746/prj_f746.dts

ボード内のデバイスツリーを記入。

大半はf746の公式デバイスツリーをそのまま使っている。

zephyr/nucleo_f746zg.dts at main · zephyrproject-rtos/zephyr · GitHub

ただし、前回app.overlayに追記した内容を今回はあらかじめ組み込んでいる。

/dts-v1/;
#include <st/f7/stm32f746Xg.dtsi>
#include <st/f7/stm32f746zgtx-pinctrl.dtsi>

/ {
    model = "STMicroelectronics STM32F746ZG-NUCLEO board";
    compatible = "st,stm32f746zg-nucleo";

    chosen {
        zephyr,console = &usart3;
        zephyr,shell-uart = &usart3;
        zephyr,sram = &sram0;
        zephyr,flash = &flash0;
        zephyr,dtcm = &dtcm;
        zephyr,canbus = &can1;
    };

    leds {
        compatible = "gpio-leds";
        green_led: led_0 {
            gpios = <&gpiob 0 GPIO_ACTIVE_HIGH>;
            label = "User LD1";
        };
        blue_led: led_1 {
            gpios = <&gpiob 7 GPIO_ACTIVE_HIGH>;
            label = "User LD2";
        };
        red_led: led_2 {
            gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>;
            label = "User LD3";
        };
    };

    gpio_keys {
        compatible = "gpio-keys";
        user_button: button_0 {
            label = "User";
            gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
        };
    };

    aliases {
        led0 = &green_led;
        led1 = &blue_led;
        led2 = &red_led;
        sw0 = &user_button;
    };
};

&clk_hse {
    hse-bypass;
    clock-frequency = <DT_FREQ_M(8)>; /* STLink 8MHz clock */
    status = "okay";
};

&pll {
    div-m = <4>;
    mul-n = <72>;
    div-p = <2>;
    div-q = <3>;
    clocks = <&clk_hse>;
    status = "okay";
};

&rcc {
    clocks = <&pll>;
    clock-frequency = <DT_FREQ_M(72)>;
    ahb-prescaler = <1>;
    apb1-prescaler = <2>;
    apb2-prescaler = <1>;
};

&usart2 {
    pinctrl-0 = <&usart2_tx_pd5 &usart2_rx_pd6
             &usart2_rts_pd4 &usart2_cts_pd3>;
    pinctrl-names = "default";
    current-speed = <115200>;
};

&usart3 {
    pinctrl-0 = <&usart3_tx_pd8 &usart3_rx_pd9>;
    pinctrl-names = "default";
    current-speed = <115200>;
    status = "okay";
};

&usart6 {
    pinctrl-0 = <&usart6_tx_pg14 &usart6_rx_pg9>;
    pinctrl-names = "default";
    current-speed = <115200>;
    status = "okay";
};

zephyr_udc0: &usbotg_fs {
    pinctrl-0 = <&usb_otg_fs_dm_pa11 &usb_otg_fs_dp_pa12>;
    pinctrl-names = "default";
    status = "okay";
};

&i2c1 {
    pinctrl-0 = <&i2c1_scl_pb8 &i2c1_sda_pb9>;
    pinctrl-names = "default";
    status = "okay";
    clock-frequency = <I2C_BITRATE_FAST>;
};

&timers1 {
    st,prescaler = <0>;
    status = "okay";

    pwm1: pwm {
        status = "okay";
        pinctrl-0 = <&tim1_ch1_pe9 &tim1_ch2_pe11 &tim1_ch3_pe13 &tim1_ch4_pe14>;
        pinctrl-names = "default";
    };
};


&timers2 {
    st,prescaler = <0>;
    status = "okay";

    pwm2: pwm {
        status = "okay";
        pinctrl-0 = <&tim2_ch1_pa15 &tim2_ch2_pb3 &tim2_ch3_pb10 &tim2_ch4_pb11>;
        pinctrl-names = "default";
    };
};

&timers3 {
    st,prescaler = <0>;
    status = "okay";

    pwm3: pwm {
        status = "okay";
        pinctrl-0 = <&tim3_ch1_pb4 &tim3_ch2_pb5 &tim3_ch3_pb0 &tim3_ch4_pb1>;
        pinctrl-names = "default";
    };
};

&timers4 {
    st,prescaler = <0>;
    status = "okay";

    pwm4: pwm {
        status = "okay";
        pinctrl-0 = <&tim4_ch1_pb6 &tim4_ch2_pd13 &tim4_ch3_pb8 &tim4_ch4_pb9>;
        pinctrl-names = "default";
    };
};

&timers5 {
    st,prescaler = <0>;
    status = "okay";

    pwm5: pwm {
        status = "okay";
        pinctrl-0 = <&tim5_ch1_pa0 &tim5_ch2_pa1 &tim5_ch3_pa2 &tim5_ch4_pa3>;
        pinctrl-names = "default";
    };
};

&timers8 {
    st,prescaler = <0>;
    status = "okay";

    pwm8: pwm {
        status = "okay";
        pinctrl-0 = <&tim8_ch1_pc6 &tim8_ch2_pc7 &tim8_ch3_pc8 &tim8_ch4_pc9>;
        pinctrl-names = "default";
    };
};

&timers9 {
    st,prescaler = <0>;
    status = "okay";

    pwm9: pwm {
        status = "okay";
        pinctrl-0 = <&tim9_ch1_pe5 &tim9_ch2_pe6>;
        pinctrl-names = "default";
    };
};

&timers10 {
    st,prescaler = <0>;
    status = "okay";

    pwm10: pwm {
        status = "okay";
        pinctrl-0 = <&tim10_ch1_pf6>;
        pinctrl-names = "default";
    };
};

&timers11 {
    st,prescaler = <0>;
    status = "okay";

    pwm11: pwm {
        status = "okay";
        pinctrl-0 = <&tim11_ch1_pf7>;
        pinctrl-names = "default";
    };
};

&timers12 {
    st,prescaler = <0>;
    status = "okay";

    pwm12: pwm {
        status = "okay";
        pinctrl-0 = <&tim12_ch1_pb14 &tim12_ch2_pb15>;
        pinctrl-names = "default";
    };
};

&timers13 {
    st,prescaler = <0>;
    status = "okay";

    pwm13: pwm {
        status = "okay";
        pinctrl-0 = <&tim13_ch1_pf8>;
        pinctrl-names = "default";
    };
};

&timers14 {
    st,prescaler = <0>;
    status = "okay";

    pwm14: pwm {
        status = "okay";
        pinctrl-0 = <&tim14_ch1_pf9>;
        pinctrl-names = "default";
    };
};




&spi1 {
    pinctrl-0 = <&spi1_sck_pa5 &spi1_miso_pa6 &spi1_mosi_pa7>;
    pinctrl-names = "default";
    cs-gpios = <&gpiod 14 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
    status = "okay";
};

&iwdg {
    status = "okay";
};

&rtc {
    status = "okay";
};

&can1 {
    pinctrl-0 = <&can1_rx_pd0 &can1_tx_pd1>;
    pinctrl-names = "default";
    bus-speed = <125000>;
    sjw = <1>;
    prop-seg = <0>;
    phase-seg1 = <6>;
    phase-seg2 = <5>;
    status = "okay";
};

&dma2 {
    status = "okay";
};

&adc1 {
    pinctrl-0 = <&adc1_in0_pa0>;
    pinctrl-names = "default";
    status = "okay";
};

&rng {
    status = "okay";
};

&mac {
    status = "okay";
    pinctrl-0 = <&eth_mdc_pc1
             &eth_rxd0_pc4
             &eth_rxd1_pc5
             &eth_ref_clk_pa1
             &eth_mdio_pa2
             &eth_crs_dv_pa7
             &eth_tx_en_pg11
             &eth_txd0_pg13
             &eth_txd1_pb13>;
    pinctrl-names = "default";
};

&backup_sram {
    status = "okay";
};

boards/arm/prj_f746/prj_f746.yaml

なくても動く?

zephyr/nucleo_f746zg.yaml at main · zephyrproject-rtos/zephyr · GitHub

boards/arm/prj_f746/Kconfig.board

Kconfig — Zephyr Project Documentation

zephyr/Kconfig.board at main · zephyrproject-rtos/zephyr · GitHub

boards/arm/prj_f746/Kconfig.defconfig

zephyr/Kconfig.defconfig at main · zephyrproject-rtos/zephyr · GitHub

boards/arm/prj_f746/board.cmake

west flashするのに必要

board_runner_args(jlink "--device=STM32F746ZG" "--speed=4000")

include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)

boards/arm/prj_f746/support/openocd.cfg

openocdの設定ファイルwest flashするのに必要

source [find board/st_nucleo_f7.cfg]

$_TARGETNAME configure -event gdb-attach {
        echo "Debugger attaching: halting execution"
        reset halt
        gdb_breakpoint_override hard
}

$_TARGETNAME configure -event gdb-detach {
        echo "Debugger detaching: resuming execution"
        resume
}

ビルドコマンド

west build -b f746_Actuator -- -DBOARD_ROOT=(ディレクトリ)/prj_f746

実行コマンド

west flash
当ブログをご利用いただく際には免責事項をお読みください。