/** * Copyright (c) 2015 - 2018, 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 #include "nrf_log.h" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" #include "MAX30003.h" #include #include #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 "patil" // 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. */ //FOr ECG Tx Buffer uint8_t SPI_RX_Buff[6]; /**< RX buffer. */ uint8_t SPI_TX_Buff[6]; uint8_t ECG_length = sizeof(SPI_TX_Buff); //volatile char SPI_RX_Buff[5] ; volatile char *SPI_RX_Buff_Ptr; int i=0; unsigned long uintECGraw = 0; signed long intECGraw=0; uint8_t DataPacketHeader[20]; uint8_t data_len = 8; signed long ecgdata; unsigned long data; char SPI_temp_32b[4]; char SPI_temp_Burst[100]; static const uint8_t m_board_led_list[LEDS_NUMBER] = SPI_LIST; /* As Reference from Aurdino Code */ void bsp_board_toggle_pin(uint32_t pin_idx); void timerIsr(); void MAX30003_Reg_Write (unsigned char WRITE_ADDRESS, unsigned long data); void max30003_sw_reset(void); void max30003_synch(void); void MAX30003_Reg_Read(uint8_t Reg_address); void MAX30003_Read_Data(int num_samples); void MAX30003_begin(); void max30003_sw_reset(void) { nrf_gpio_pin_set(SPI_SS_PIN); //Slave select Set nrf_gpio_pin_clear(SPI_SS_PIN); //Slave select High MAX30003_Reg_Write(SW_RST,0x00); //As per Arduino Code } void max30003_synch(void) { MAX30003_Reg_Write(SYNCH,0x000000); //As per Arduino Code } void timerIsr() { nrf_gpio_pin_toggle(NRF_FCLK_PIN); //As per Arduino Code } void MAX30003_Reg_Write (unsigned char WRITE_ADDRESS, unsigned long data) //Its Completed //for main.oerror { uint8_t dataToSend[1] = (WRITE_ADDRESS<<1) | WREG; nrf_gpio_pin_clear(SPI_SS_PIN); nrf_delay_ms(200); (nrf_drv_spi_transfer(&spi, (const uint8_t *)dataToSend[1], ECG_length, SPI_RX_Buff, ECG_length)); (nrf_drv_spi_transfer(&spi, (const uint8_t *)(data>>16), ECG_length, SPI_RX_Buff, ECG_length)); (nrf_drv_spi_transfer(&spi, (const uint8_t *)(data>>8), ECG_length, SPI_RX_Buff, ECG_length)); (nrf_drv_spi_transfer(&spi, (const uint8_t *)(data), ECG_length, SPI_RX_Buff, ECG_length)); nrf_delay_ms(200); nrf_gpio_pin_set(SPI_SS_PIN); } void MAX30003_Reg_Read(uint8_t Reg_address) //Its Completed { uint8_t A[1]=0xFF; nrf_gpio_pin_clear(SPI_SS_PIN); // as per arduino code SPI_TX_Buff[20] = (Reg_address<<1 ) | RREG; (nrf_drv_spi_transfer(&spi, (const uint8_t *)SPI_TX_Buff[20], ECG_length, SPI_RX_Buff, ECG_length)); for ( i = 0; i < 3; i++) { SPI_temp_32b[i]=nrf_drv_spi_transfer(&spi, (const uint8_t *)A[1], ECG_length, SPI_RX_Buff, ECG_length); } nrf_gpio_pin_set(SPI_SS_PIN); //it make High } void MAX30003_Read_Data(int num_samples) //Its Completed { uint8_t A[1]=0x00; nrf_gpio_pin_clear(SPI_SS_PIN); // as per arduino code SPI_TX_Buff[20] = (ECG_FIFO_BURST<<1 ) | RREG; (nrf_drv_spi_transfer(&spi, (const uint8_t *)SPI_TX_Buff[20], ECG_length, SPI_RX_Buff, ECG_length)); for ( i = 0; i < num_samples*3; ++i) { SPI_temp_Burst[i] = (nrf_drv_spi_transfer(&spi, (const uint8_t *)A[1], ECG_length, SPI_RX_Buff, ECG_length)); } nrf_gpio_pin_set(SPI_SS_PIN); //it make High } void MAX30003_begin() { max30003_sw_reset(); nrf_delay_ms(100); MAX30003_Reg_Write(CNFG_GEN, 0x081007); 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); max30003_synch(); nrf_delay_ms(100); } void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void * p_context) { nrf_gpio_pin_set(SPI_SS_PIN); spi_xfer_done = true; NRF_LOG_INFO("Transfer completed."); if (SPI_RX_Buff[0] != 0) { NRF_LOG_INFO(" Received:"); NRF_LOG_HEXDUMP_INFO(SPI_RX_Buff, strlen((const char *)SPI_RX_Buff)); } } int main(void) { char ECG_CHAR[30]; /*From Procentral upto line 222 */ MAX30003_Reg_Read(ECG_FIFO); unsigned long data0 = (unsigned long) (SPI_temp_32b[0]); data0 = data0 <<24; unsigned long data1 = (unsigned long) (SPI_temp_32b[1]); data1 = data1 <<16; unsigned long data2 = (unsigned long) (SPI_temp_32b[2]); data2 = data2 >>6; data2 = data2 & 0x03; data = (unsigned long) (data0 | data1 | data2); ecgdata = (signed long) (data); MAX30003_Reg_Read(RTOR); unsigned long RTOR_msb = (unsigned long) (SPI_temp_32b[0]); unsigned char RTOR_lsb = (unsigned char) (SPI_temp_32b[1]); unsigned long rtor = (RTOR_msb<<8 | RTOR_lsb); rtor = ((rtor >>2) & 0x3fff); float hr = 60 /((float)rtor*0.008); unsigned int HR = (unsigned int)hr; // type cast to int unsigned int RR = (unsigned int)rtor*8 ; //8ms DataPacketHeader[0] = 0x0A; DataPacketHeader[1] = 0xFA; DataPacketHeader[2] = 0x0C; DataPacketHeader[3] = 0; DataPacketHeader[4] = 0x02; DataPacketHeader[5] = ecgdata; DataPacketHeader[6] = ecgdata>>8; DataPacketHeader[7] = ecgdata>>16; DataPacketHeader[8] = ecgdata>>24; DataPacketHeader[9] = RR ; DataPacketHeader[10] = RR >>8; DataPacketHeader[11] = 0x00; DataPacketHeader[12] = 0x00; DataPacketHeader[13] = HR ; DataPacketHeader[14] = HR >>8; DataPacketHeader[15] = 0x00; DataPacketHeader[16] = 0x00; DataPacketHeader[17] = 0x00; DataPacketHeader[18] = 0x0b; bsp_board_init(BSP_INIT_LEDS); APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); NRF_LOG_DEFAULT_BACKENDS_INIT(); nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; spi_config.ss_pin = SPI_SS_PIN; //29 spi_config.miso_pin = SPI_MISO_PIN; //28 spi_config.mosi_pin = SPI_MOSI_PIN; //4 spi_config.sck_pin = SPI_SCK_PIN; //3 APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL)); NRF_LOG_INFO("SPI example started."); while (1) { // Reset rx buffer and transfer done flag memset(SPI_RX_Buff, 0, ECG_length); spi_xfer_done = false; for(i=0; i<19; i++) // transmit the data { APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, (const uint8_t *) DataPacketHeader[i], ECG_length, SPI_RX_Buff, ECG_length)); } while (!spi_xfer_done) { __WFE(); } NRF_LOG_FLUSH(); bsp_board_led_invert(BSP_BOARD_LED_0); nrf_delay_ms(200); } }