コンテンツにスキップ

01. 始める

クイックスタート

VSCode の Raspberry Pi Pico の拡張機能をインストールする。

New C/C++ Project を選択する。

Exampleボタンを押してhello_usbを選択する。

Raspberry Pi Pico をリセットボタンを押しながら接続したあと、Run Project (USB)を実行する。

シリアルポートから出力を確認する。

L チカをしてみる

hello_usb.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include "pico/stdlib.h"

int main()
{
    stdio_init_all();

    const uint internalLedPin = 25;
    bool value = false;

    gpio_init(internalLedPin);
    gpio_set_dir(internalLedPin, GPIO_OUT);

    while (true)
    {
        value = !value;
        printf("Hello, world!\n");
        gpio_put(internalLedPin, value);
        sleep_ms(1000);
    }
}

パルスの時間軸分解能を確認する

結論から言うと、1us 単位のパルス生成であればbusy_wait_us()で実現できる。100ns あたりはきつい。

sleep_us(1)で 1us

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include "pico/stdlib.h"

int main()
{
    stdio_init_all();

    const uint pin = 2;
    bool value = false;

    gpio_init(pin);
    gpio_set_dir(pin, GPIO_OUT);

    while (true)
    {
        value = !value;
        gpio_put(pin, value);
        sleep_us(1);
    }
}

使い物にならない。

busy_wait_us(1)で 1us

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include "pico/stdlib.h"

int main()
{
    stdio_init_all();

    const uint pin = 2;
    bool value = false;

    gpio_init(pin);
    gpio_set_dir(pin, GPIO_OUT);

    while (true)
    {
        value = !value;
        gpio_put(pin, value);
        busy_wait_us(1);
    }
}

かなり良い。

busy_wait_at_least_cycles()で 1us

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include "pico/stdlib.h"

int main()
{
    stdio_init_all();

    const uint pin = 2;
    bool value = false;

    gpio_init(pin);
    gpio_set_dir(pin, GPIO_OUT);

    while (true)
    {
        value = !value;
        gpio_put(pin, value);
        busy_wait_at_least_cycles(125); // 125MHzだと1サイクル8ns → 125サイクルで1us
    }
}

それほど悪くないが誤差がある。

busy_wait_at_least_cycles()で 100ns

12.5 サイクルで 100ns の計算なので、busy_wait_at_least_cycles(12);とすれば 100us のパルスが生成できるのではと考えた。

苦しい。

ファイル分割する

1
2
3
#pragma once

int sample();
1
2
3
4
5
6
7
8
#include <stdio.h>
#include "sample.h"

int sample()
{
    printf("This is sample\n");
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include "pico/stdlib.h"
#include "sample.h"

int main()
{
    stdio_init_all();

    while (true)
    {
        int ret = sample();
        printf("%d", ret);
        sleep_ms(5000);
    }

    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)

# == DO NEVER EDIT THE NEXT LINES for Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
    set(USERHOME $ENV{USERPROFILE})
else()
    set(USERHOME $ENV{HOME})
endif()
set(sdkVersion 2.0.0)
set(toolchainVersion 13_2_Rel1)
set(picotoolVersion 2.0.0)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
    include(${picoVscode})
endif()
# ====================================================================================
set(PICO_BOARD pico CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project(hello_usb C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

if (TARGET tinyusb_device)
    add_executable(hello_usb
            hello_usb.c
            sample.c
            )

    # pull in common dependencies
    target_link_libraries(hello_usb pico_stdlib)

    # enable usb output, disable uart output
    pico_enable_stdio_usb(hello_usb 1)
    pico_enable_stdio_uart(hello_usb 0)

    # create map/bin/hex/uf2 file etc.
    pico_add_extra_outputs(hello_usb)

    # add url via pico_set_program_url
elseif(PICO_ON_DEVICE)
    message("Skipping hello_usb because TinyUSB submodule is not initialized in the SDK")
endif()