BLEDev

シリアル通信

関連レジスタ

番地 定数名 概要
0x50000004 CLK_PER_REG Peripheral divider register
0x5000100C UART_LCR_REG Line Control Register
0x50001004 UART_IER_DLH_REG Interrupt Enable Register
0x50001000 UART_RBR_THR_DLL_REG Receive Buffer Register
0x5000100C UART_LCR_REG Line Control Register
0x50001010 UART_MCR_REG Modem Control Register
0x50001008 UART_IIR_FCR_REG Interrupt Identification Register/FIFO Control Register
0x50001004 UART_IER_DLH_REG Interrupt Enable Register
    SetBits16(CLK_PER_REG, UART1_ENABLE, 1);      // enable  clock for UART 1

    SetWord16(UART_LCR_REG, 0x80); // set bit to access DLH and DLL register
    SetWord16(UART_IER_DLH_REG,(UART_BAUDRATE_115K2&0xFF>>8));//set high byte
    SetWord16(UART_RBR_THR_DLL_REG,UART_BAUDRATE_115K2&0xFF);//set low byte
    SetWord16(UART_LCR_REG,UART_DATALENGTH|UART_PARITY|UART_STOPBITS);
    SetBits16(UART_MCR_REG, UART_SIRE, 0);  // mode 0 for normal , 1 for IRDA
    SetWord16(UART_IIR_FCR_REG,1);  // enable fifo  
    SetBits16(UART_IER_DLH_REG,ERBFI_dlh0,0);  // IER access, disable interrupt for available data

    char uart_receive_byte(void){
    #ifndef UART_ENABLED
        return 0;
#else
    do{
    }while((GetWord16(UART_LSR_REG)&0x01)==0);    // wait until serial data is ready 
    return 0xFF&GetWord16(UART_RBR_THR_DLL_REG);    // read from receive data buffer
#endif
}

void uart_send_byte(char ch){
    #ifndef UART_ENABLED
        return ;
#else
    while((GetWord16(UART_LSR_REG)&0x20)==0);    // read status reg to check if THR is empty
    SetWord16(UART_RBR_THR_DLL_REG,(0xFF&ch)); // write to THR register
    return;
    #endif
}

新規作成

main.c, periph_setup.c, periph_setup.hは新規作成する。

Keil μVision上では、

src/main.c src/periph_setup.c include/periph_setup.h

Windows上では、

src/main.c src/periph_setup.c include/periph_setup.h

main.c

nclude <stdio.h>
#include <periph_setup.h>
#include <uart.h>

int main (void)
{
    perif_init();

    // Init LED
    GPIO_ConfigurePin(GPIO_PORT_0, GPIO_PIN_1, OUTPUT, PID_GPIO, false);

    // Init UART
    GPIO_ConfigurePin(UART_GPIO_PORT, UART_TX_PIN, OUTPUT, PID_UART1_TX, false);
    GPIO_ConfigurePin(UART_GPIO_PORT, UART_RX_PIN, INPUT, PID_UART1_RX, false);

    uart_initialization();

    printf_string("TEST");
}

periph_setup.c

#include <periph_setup.h>
#include <uart.h>

void perif_init() {

    // system init
    SetWord16(CLK_AMBA_REG, 0x00);                 // set clocks (hclk and pclk ) 16MHz
    SetWord16(SET_FREEZE_REG,FRZ_WDOG);            // stop watch dog    
    SetBits16(SYS_CTRL_REG,PAD_LATCH_EN,1);        // open pads
    SetBits16(SYS_CTRL_REG,DEBUGGER_ENABLE,1);     // open debugger
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP,0);       // exit peripheral power down

    // Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP));

}

periph_setup.h

#include <datasheet.h>
#include <gpio.h>
#include <uart.h>

// UART Setting
#define UART_BAUDRATE     UART_BAUDRATE_115K2       // Baudrate in bits/s: {9K6, 14K4, 19K2, 28K8, 38K4, 57K6, 115K2}
#define UART_DATALENGTH  UART_DATALENGTH_8        // Datalength in bits: {5, 6, 7, 8}
#define UART_PARITY       UART_PARITY_NONE          // Parity: {UART_PARITY_NONE, UART_PARITY_EVEN, UART_PARITY_ODD}
#define UART_STOPBITS     UART_STOPBITS_1           // Stop bits: {UART_STOPBITS_1, UART_STOPBITS_2} 
#define UART_FLOWCONTROL  UART_FLOWCONTROL_DISABLED // Flow control: {UART_FLOWCONTROL_DISABLED, UART_FLOWCONTROL_ENABLED}
// UART Pin
#define UART_GPIO_PORT    GPIO_PORT_0
#define UART_TX_PIN       GPIO_PIN_7
#define UART_RX_PIN       GPIO_PIN_4
#define UART_ENABLED

void perif_init(void);
void uart_initilize(void);

コピーして取り込み

コピー元

DA14580_581_583_SDK_3.0.10.1/peripheral_examples/startup/startup_CMSDK_CM0.s DA14580_581_583_SDK_3.0.10.1/peripheral_examples/startup/system_CMSDK.c DA14580_581_583_SDK_3.0.10.1/dk_apps/src/plf/refip/src/arch/main/ble/hardfault_handler.c DA14580_581_583_SDK_3.0.10.1/peripheral_examples/include/uart.h DA14580_581_583_SDK_3.0.10.1/peripheral_examples/src/uart.c

をWindows上では、

startup/startup_CMSDK_CM0.s startup/system_CMSDK.c startup/hardfault_handler.c include/common_uart.h utilities/common_uart.c

Keil μVision上では、

boot/startup_CMSDK_CM0.s boot/system_CMSDK.c boot/hardfault_handler.c include/common_uart.h utilities/common_uart.c

参照で取り込み

参照元

DA14580_581_583_SDK_3.0.10.1/dk_apps/src/plf/refip/src/driver/gpio/gpio.c

Keil μVision上では、

boot/gpio.c

設定

Include path

..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\arch\boot\rvds; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\arch\compiler\rvds; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\arch\ll\rvds; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\dialog\include; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\arch; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\driver\reg; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\driver\uart; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\driver\gpio; .\include; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\driver\timer; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\arch\main\ble; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\ip\ble\ll\src\hcic; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\modules\rwip\api; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\ip\ble\ll\src\rwble; ..\DA14580_581_583_SDK_3.0.10.1\dk_apps\src\plf\refip\src\driver\uart; .\utilities