Hello
Nordic Team.
I controling nrf spi.
testing for example nrfSDK14.0.0\example\peripheral\spi\pca10040\blank\arm5_no_packs\spi_pca10040
/**
* Copyright (c) 2015 - 2017, 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:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define SPI_INSTANCE 0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
//#define TEST_STRING "Nordic"
//static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
//static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
//static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
#define SW_RST 0x08
#define WREG 0x00
#define NO_OP 0x00
#define STATUS 0x01
#define EN_INT 0x02
#define EN_INT2 0x03
#define MNGR_INT 0x04
#define MNGR_DYN 0x05
#define SW_RST 0x08
#define SYNCH 0x09
#define FIFO_RST 0x0A
#define INFO 0x0F
#define CNFG_GEN 0x10
#define CNFG_CAL 0x12
#define CNFG_EMUX 0x14
#define CNFG_ECG 0x15
#define CNFG_RTOR1 0x1D
#define CNFG_RTOR2 0x1E
#define ECG_FIFO_BURST 0x20
#define ECG_FIFO 0x21
#define RTOR 0x25
static uint8_t m_tx_buf[4]; /**< TX buffer. */
static uint8_t m_rx_buf[10]; /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
/**
* @brief SPI user event handler.
* @param event
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
void * p_context)
{
spi_xfer_done = true;
NRF_LOG_INFO("Transfer completed.");
if (m_rx_buf[0] != 0)
{
NRF_LOG_INFO(" Received:");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
}
}
void MAX30003_Reg_Write(uint8_t WRITE_ADDRESS, unsigned long data)
{
// now combine the register address and the command into one byte:
uint8_t wRegName = (WRITE_ADDRESS<<1) | WREG;
// take the chip select low to select the device:
nrf_delay_ms(10);
m_tx_buf[0] = wRegName;
m_tx_buf[1] = (data>>16);
m_tx_buf[2] = (data>>8);
m_tx_buf[3] = (data);
nrf_delay_ms(10);
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, 10));
// take the chip select high to de-select:
}
int main(void)
{
bsp_board_leds_init();
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("SPI example.");
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
/*spi_config.ss_pin = SPI_SS_PIN;
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;*/
spi_config.ss_pin = 2;//SPI_SS_PIN;
spi_config.miso_pin = 3;//SPI_MISO_PIN;
spi_config.mosi_pin = 4;//SPI_MOSI_PIN;
spi_config.sck_pin = 7;//SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
nrf_delay_ms(100);
MAX30003_Reg_Write(SW_RST,0x000000);
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_GEN, 0x080004);
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_CAL, 0x720000); // 0x700000
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_EMUX,0x0B0000);
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_ECG, 0x005000); // d23 - d22 : 10 for 250sps , 00:500 sps
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_RTOR1,0x3fc600);
nrf_delay_ms(100);
MAX30003_Reg_Write(MNGR_INT, 0x000004);
//nrf_delay_ms(100);
while (1)
{
NRF_LOG_INFO("while");
// Reset rx buffer and transfer done flag
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
//APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
while (!spi_xfer_done)
{
__WFE();
}
NRF_LOG_FLUSH();
bsp_board_led_invert(BSP_BOARD_LED_0);
nrf_delay_ms(200);
}
}
But i have problem. difference project spi is not working.
drv_max30003.c => max30003_initchip => MAX30003_Reg_Write
Problem here ==> MAX30003_Reg_Write(SW_RST,0x000000);
but source change board is working ==>//MAX30003_Reg_Write(SW_RST,0x000000);
And i have 1 more another question. cccd_handle number difference... not solved question. plz help me.
Here my main source.
main.c
/**@brief Smart patch Board Init.
*/
static void smartpatch_board_init(void)
{
uint32_t err_code;
m_ble_init_t ble_params;
/**@brief Initialize button led Init*/
err_code = buttons_leds_init();
smartpatch_battery_meas_init(&m_ble_service_handles[SMARTPATCH_SERVICE_BATTERY]);
smartpatch_heartrate_init(&m_ble_service_handles[SMARTPATCH_SERVICE_HEARTRATE]);
smartpatch_activity_init(&m_ble_service_handles[SMARTPATCH_SERVICE_ACTIVITY]);
smartpatch_ecg_init(&m_ble_service_handles[SMARTPATCH_SERVICE_ECG]);
smartpatch_holter_init(&m_ble_service_handles[SMARTPATCH_SERVICE_HOLTER]);
smartpatch_filetransfer_init(&m_ble_service_handles[SMARTPATCH_SERVICE_FILETRANSFER]);
/**@brief Initialize BLE handling module. */
ble_params.evt_handler = smart_patch_ble_evt_handler;
ble_params.p_service_handles = m_ble_service_handles;
ble_params.service_num = SMARTPATCH_SERVICES_MAX;
smartpatch_ble_init(&ble_params);
APP_ERROR_CHECK(err_code);
}
int main(void){
// Initialize.
log_init();
timers_init();
power_management_init();
NRF_LOG_INFO("Application started\n");
smartpatch_board_init();
// Start execution.
application_timers_start();
// Enter main loop.
for (;;)
{
if (NRF_LOG_PROCESS() == false)
{
nrf_pwr_mgmt_run();
}
}}
smart_patch_ecg.c
uint32_t smartpatch_ecg_init(m_ble_service_handle_t * p_handle)
{
uint32_t err_code;
// drv_motion_twi_init_t motion_params_mpu9250;
err_code = max30003_initchip();
if (err_code != NRF_SUCCESS)
{
NRF_LOG_ERROR("[%s]FAILED - %d",__func__, err_code);
return err_code;
}
NULL_PARAM_CHECK(p_handle);
NRF_LOG_INFO("[%s]Init",__func__);
p_handle->ble_evt_cb = ecg_on_ble_evt;
p_handle->init_cb = ecg_services_init;
return NRF_SUCCESS;
}
drv_max3003.c##
#include "nrf_log.h"
#include "nrf_spi.h"
#include "nrf_delay.h"
#include "drv_max30003.h"
#define SPI_INSTANCE 0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
static uint8_t m_tx_buf[SPI_TX_SIZE]; /**< TX buffer. */
static uint8_t m_rx_buf[SPI_RX_SIZE]; /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
/**
* @brief SPI user event handler.
* @param event
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
void * p_context)
{
spi_xfer_done = true;
NRF_LOG_INFO("Transfer completed.");
if (m_rx_buf[0] != 0)
{
NRF_LOG_INFO(" Received:");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
}
}
void MAX30003_Reg_Write(uint8_t WRITE_ADDRESS, unsigned long data)
{
// now combine the register address and the command into one byte:
uint8_t wRegName = (WRITE_ADDRESS<<1) | WREG;
// take the chip select low to select the device:
nrf_delay_ms(10);
m_tx_buf[0] = wRegName;
m_tx_buf[1] = (data>>16);
m_tx_buf[2] = (data>>8);
m_tx_buf[3] = (data);
nrf_delay_ms(10);
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, 10));
// take the chip select high to de-select:
}
static void max30003_sw_reset(void)
{
MAX30003_Reg_Write(SW_RST,0x000000);
nrf_delay_ms(100);
}
static void max30003_synch(void)
{
MAX30003_Reg_Write(SYNCH,0x000000);
}
uint32_t max30003_initchip(void)
{
uint32_t err_code;
nrf_drv_spi_config_t spi_config_MAX30003 = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config_MAX30003.sck_pin = SPIM0_SCK_PIN;//2
spi_config_MAX30003.mosi_pin = SPIM0_MOSI_PIN;//3
spi_config_MAX30003.miso_pin = SPIM0_MISO_PIN;//4
spi_config_MAX30003.ss_pin = SPIM0_SS_PIN;//7
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config_MAX30003, spi_event_handler, NULL));
nrf_delay_ms(100);
//APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
MAX30003_Reg_Write(SW_RST,0x000000);
//max30003_sw_reset();
//nrf_delay_ms(10);
//MAX30003_Reg_Write(CNFG_GEN, 0x080004);
/*nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_CAL, 0x720000); // 0x700000
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_EMUX,0x0B0000);
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_ECG, 0x005000); // d23 - d22 : 10 for 250sps , 00:500 sps
nrf_delay_ms(100);
MAX30003_Reg_Write(CNFG_RTOR1,0x3fc600);
nrf_delay_ms(100);
MAX30003_Reg_Write(MNGR_INT, 0x000004);*/
//nrf_delay_ms(100);
spi_xfer_done = false;
return NRF_SUCCESS;
}
Thanks,
Bestregards,
kevin.ko