nRF max3102 library

 前回で動作の方が理解できたので、SparkFunからMAX30102のArduinoのライブラリーをnRF用にC言語移植するしてみた。ただし緑色を搭載していないMAX30102向けでLEDの個数に応じた汎用性は持たせていない。

APP timerとTWIが使えるようにsdk_configの設定ができている前提からのスタートになる。

(汎用的でないが自分が使っているテンプレートはここ

【ライブラリーの配置】

\components\drivers_extの下にmax30102をいうフォルダーを作ってあげて、

max30102.c
max30102.h

の二つのファイルを置く。この2つのファイルはここからDLできるようにしておく。

【ライブラリーの読み込み】

embedded srudioでプロジェクトを右クリックして新しいフォルダを作り、名前をmax30102とでもしておく。


Add Existing Fileを選択して、上のファイルのうちmax30102.cを指定する。
User Include Directoriesを開いて、
../../../../../../components/drivers_ext/max30102
を追記する。../../の所は、他のcomponentsフォルダの指定を参考にするとよい。

【インクルードファイルの定義】

#include "max30102.h"

【設定】

setupをmainの中で一度行う。

   //configuration of Max30102
    MAX30102_setup(0x1F,4,2,100,69,4096);
ここの引数は、SparkFunのライブラリーのexampleになされているのと同じ。
C++と違って引数を割愛する事はできない。

【実際の利用】

試しにシンプルな呼び出しをしてみる。

    while (true)
    {
       NRF_LOG_RAW_INFO("Red:%d\n",MAX30102_getRed());
       NRF_LOG_FLUSH();
       nrf_delay_ms(200);
    }


【参考ソース全体】
**
 * Copyright (c) 2009 - 2021, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 */
/** @file
* @brief Example template project.
* @defgroup nrf_templates_example Example Template
*
*/

#include <stdbool.h>
#include <stdint.h>

#include "nrf.h"
#include "nordic_common.h"
#include "boards.h"

// for using nrf_log
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

// for using nrf_delay
#include "nrf_delay.h"

// for using I2C
#include "nrf_drv_twi.h"

// for using OLED display with SSD1306
#include "ssd1306.h"
#include "fun_logo.h"

//To use application timer
#include "app_timer.h"
#include "nrf_drv_clock.h"

//To use MAX30102
#include "max30102.h"

/* ****************/
/*  TWI
/* ****************/
/* TWI instance ID. */
#if TWI0_ENABLED
#define TWI_INSTANCE_ID     0
#elif TWI1_ENABLED
#define TWI_INSTANCE_ID     1
#endif
/* TWI instance. */
const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(0); //do not change instance name "m_twi" which is used ssd1306.c

void twi_init(void)
{
    const nrf_drv_twi_config_t twi_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };
   nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
   nrf_drv_twi_enable(&m_twi);
}


/* ****************/
/*  APP TIMER
/* ****************/
#define LED_INTERVAL1 APP_TIMER_TICKS(100)
#define LED_INTERVAL2 APP_TIMER_TICKS(200)
#define LED1_PIN  17
#define LED2_PIN  18

APP_TIMER_DEF(timer_id1);
APP_TIMER_DEF(timer_id2);

static void lfclk_config(void)
{
  ret_code_t err_code =nrf_drv_clock_init();
  APP_ERROR_CHECK(err_code);
  nrf_drv_clock_lfclk_request(NULL);
}

static void app_timer_handler1(void * p_context)
{
  nrf_gpio_pin_toggle(LED1_PIN);
}
static void app_timer_handler2(void * p_context)
{
  nrf_gpio_pin_toggle(LED2_PIN);
}
static void app_timers_init(void)
{
  ret_code_t  err_code;
  err_code=app_timer_init();
  APP_ERROR_CHECK(err_code);

  err_code=app_timer_create(&timer_id1, APP_TIMER_MODE_REPEATED, app_timer_handler1);
  APP_ERROR_CHECK(err_code);

  err_code=app_timer_create(&timer_id2, APP_TIMER_MODE_REPEATED, app_timer_handler2);
  APP_ERROR_CHECK(err_code);
}


/**
 * @brief Function for application main entry.
 */
int main(void)
{
    //Initialization of TWI
    twi_init();
    //Initialization of SSD1306
    SSD1306_Init();
    SSD1306_DrawBitmap(0,0,fun_logo_mono128x64, 128, 64, 1);
    SSD1306_UpdateScreen();
    nrf_delay_ms(1000);

    //Initialization of NRF_LOG
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    ////configration GPIP pin
    //nrf_gpio_cfg_output(LED1_PIN);
    //nrf_gpio_cfg_output(LED2_PIN);

    //Initilization of APP_timer
    lfclk_config();
    app_timers_init();

    //timer start
    uint32_t err_code;
    err_code =app_timer_start(timer_id1, LED_INTERVAL1,NULL);
    APP_ERROR_CHECK(err_code);
    err_code =app_timer_start(timer_id2, LED_INTERVAL2,NULL);
    APP_ERROR_CHECK(err_code);

    //configration of Max30102
    MAX30102_setup(0x2F,4,2,50,69,4096);

    uint32_t p_ticks;
    while (true)
    {
       NRF_LOG_RAW_INFO("Red:%d,IR:%d\n",MAX30102_getRed());//,MAX30102_getIR());//
       NRF_LOG_FLUSH();
    }
}
/** @} */



コメント

このブログの人気の投稿

Attiny85とAQM0802A(LCD)のI2C接続

ILI9341 240X320 Arduino

Attiny85 FuseRest