1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lognan Nanoその2(gd32vでシリアル通信)

Posted at

Lognan Nano gd32v Framework

「Longan NanoをPlatoform IOで完結」(その1)では、「Arduino」Frameworkを使った。ここでは、同じPlatform IOを用いて、「gd32v」Frameworkを用いて、シリアル通信を行う。

環境

「Longan NanoをPlatoform IOで完結」同様、シリアル接続されたPCにLognan Nanoボードからの出力を実施。

platformio.ini

platformio.ini
[env:sipeed-longan-nano-lite]
platform = gd32v
board = sipeed-longan-nano-lite
framework = gd32vf103-sdk
upload_protocol = dfu

frameworkが「gd32vf103-sdk」となっている。

ソースコード(C++)

あえてC++を用いて、てこずった。(下記serial.cppの記載参照。)

serial.cpp

シリアル初期設定など。こちらのソースコードをほぼそのまま利用。

serial.cpp
extern "C" {
# include "gd32vf103.h"
}

int _put_char(int c) {
    usart_data_transmit(USART0, (uint8_t) c);
    while(usart_flag_get(USART0, USART_FLAG_TBE) == RESET);
    return c;
}

// initialize USART0
void serial_init(void) {
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_USART0);
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

    usart_deinit(USART0);
    usart_baudrate_set(USART0, 9600);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
    usart_enable(USART0);
}

「extern "C"」により、Cライブラリを使えるようにしている。ただし、Framework「gd32v」のC++環境ではBuildエラーになる。調べたところ、この記事にあるように「gd32vf103.h」に手を加える必要がある。

gd32vf103.h
// 追記部分のみ

# ifndef __cplusplus
typedef enum {FALSE = 0, TRUE = !FALSE} bool;
# endif

main.cpp

メイン部分。Delay(delay_1ms())については、こちらのソースコードをそのまま利用。

main.cpp
extern "C" {
# include "gd32vf103.h"
}

# include <stdio.h>
extern void serial_init();

void delay_1ms(uint32_t count)
{
    uint64_t start_mtime, delta_mtime;

    // Don't start measuruing until we see an mtime tick
    uint64_t tmp = get_timer_value();
    do {
    start_mtime = get_timer_value();
    } while (start_mtime == tmp);

    do {
    delta_mtime = get_timer_value() - start_mtime;
    } while(delta_mtime <(SystemCoreClock/4000.0 *count ));
}

main () {
    int i = 0;

    serial_init();

    while (1) {
        printf("N = %i\n", i);
        i++;
        delay_1ms(2000);
    }
}

2秒毎にPCに出力する。

プログラムを書き込むためのボードのDFUモードへの入り方や、Platform IOから書き込む方法についても、「Longan NanoをPlatoform IOで完結」参照。

出力の様子

スクリーンショット 2021-06-26 16.27.14.png

成功。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?