본문으로 바로가기

[nRF52] ble_app_hids_keyboard 에 NUS 추가하기

category Nordic_nRF52 2023. 3. 23. 14:15

목차

     

    1> 프로젝트위치

       : <InstallFolder>\examples\ble_peripheral\ble_app_uart

     

     

    2> 수정사항

        ▶ sdk_config.h  (NUS 추가 및 Vendor Specific UUID  갯수 추가)

    ~~ 중략 ~~
    #define BLE_NUS_ENABLED 1
    
    ~~ 중략 ~~
    #define NRF_SDH_BLE_VS_UUID_COUNT 1

     

        ▶ .emProject  (NUS 컴포넌트 추가)

        <folder Name="nRF_BLE_Services">
          ~~ 중략 ~~
          <file file_name="../../../../../../components/ble/ble_services/ble_nus/ble_nus.c" />
        </folder>

     

        ▶ main.c

    #include "ble_nus.h"
    
    ~~ 중략 ~~
    BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); 
    
    ~~ 중략 ~~
    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            uint32_t err_code;
    
            NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
            NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
    
            for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
            {
                do
                {
                    err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
                    if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                    {
                        NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                        APP_ERROR_CHECK(err_code);
                    }
                } while (err_code == NRF_ERROR_BUSY);
            }
            if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
            {
                while (app_uart_put('\n') == NRF_ERROR_BUSY);
            }
        }
    }
    
     static void services_init(void)
    {
    	uint32_t		   err_code;
    	ble_nus_init_t     nus_init;
        
        ~~ 중략 ~~
        // Initialize NUS.
        memset(&nus_init, 0, sizeof(nus_init));
    
        nus_init.data_handler = nus_data_handler;
    
        err_code = ble_nus_init(&m_nus, &nus_init);
        APP_ERROR_CHECK(err_code);

     

     

     

    3>  적용후  nRF Connect 을 통한 디스크립터 내용

      ==> NUS 아래의 Characteristics

     

     

    4> NUS 동작 관련 자세한 사항은 아래 링크 참조하세요.

    https://leevisual.tistory.com/182

     

    [nRF52] ble_app_uart 프로젝트

    : 이 프로젝트는 BLE 위에서 Serial 포트를 구현한 예제로 NUS (Nordic Uart Service) 어플리케이션이다. 목차 0> 개요 :이 프로젝트는 BLE 를 통해 들어온 데이타를 시리얼로 데이터 전송및 Serial 로 들어온

    leevisual.tistory.com

     

     

    <기타 >

        ▶ 코드추가후 아래처럼 메모리 부족 에러가 발생한다면 아래 링크 참조하세요.

    <warning> nrf_sdh_ble: Insufficient RAM allocated for the SoftDevice.
    28C8 to 0x200028D8.
    <warning> nrf_sdh_ble: Maximum RAM size for application is 0xD728.
    <error> nrf_sdh_ble: sd_ble_enable() returned NRF_ERROR_NO_MEM.
    <error> app: ERROR 4 [NRF_ERROR_NO_MEM] at D:\Project\SES\nRF5SD7
    PC at: 0x00033CCB
    <error> app: End of error report

    https://leevisual.tistory.com/104

     

    [nRF52] NRF_ERROR_NO_MEM 에러 디버깅

    : ble_peripheral ==> keyboard 프로젝트에 Central 기능을 추가중 아래와 같은 에러 발생시 디버깅 해봤습니다. >> Debug Terminal 로그를 보니 RAM_START 위치를 0x200028C8 로 수정 하라고 적혀 있네요. RAM_START 는 .e

    leevisual.tistory.com

     

        ▶  Serial TX/RX 통신을 위해서는  uart_event_handle() 도  아래처럼 수정해야 합니다.

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t data_array[UART_RX_BUF_SIZE];
        static uint8_t index = 0;
    	uint32_t	   err_code;
    
        switch (p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
                if ((data_array[index - 1] == '\n') ||
                    (data_array[index - 1] == '\r') ||
                    (index >= 64))
                {
                    if (index > 1)
                    {
                        NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                        NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                        do
                        {
                            uint16_t length = (uint16_t)index;
                            err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                (err_code != NRF_ERROR_RESOURCES) &&
                                (err_code != NRF_ERROR_NOT_FOUND))
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_RESOURCES);
                    }
    
                    index = 0;
                }
    
                break;
            ~~ 중략 ~~
        }
    }

     

    그럼 수고하세요.

    반응형