This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

not getting activity and inactivity interrupt status in status register of ADXL362.

Hi I am working on activity detection using ADXL362 and nrf52. When I am moving the accelerometer ideally I should get activity interrupt in status register and when ADXL362 is stationary it should generate inactivity interrupt and it is updated in status register.When I am moving ADXL362 i am getting 0x40 or 0x41 in status register(not at all the time). please suggest me why I am not getting inactivity interrupt when it is stationary.Following is my code: Hoping for positive response!!

	#include "nrf_delay.h"
	#include "app_error.h"
	#include "app_util_platform.h"
	#include "nrf_drv_spi.h"
	#include "bsp.h"
	#include "app_timer.h"
	#include "nordic_common.h"

	#include <stdbool.h>
	#include <stdint.h>
	#include <stdio.h>
	#include "app_uart.h"
	#include "app_error.h"
	#include "nrf_delay.h"
	#include "nrf.h"
	#include "nrf_temp.h"

	#include "boards.h"
	#include "nrf_drv_gpiote.h"




	#define APP_TIMER_PRESCALER      0                      ///< Value of the RTC1 PRESCALER register.
	#define APP_TIMER_MAX_TIMERS     BSP_APP_TIMERS_NUMBER  ///< Maximum number of simultaneously created timers.
	#define APP_TIMER_OP_QUEUE_SIZE  5                     ///< Size of timer operation queues.

	#define DELAY_MS                 1                   ///< Timer Delay in milli-seconds.
	#define TX_RX_MSG_LENGTH         100
	static uint8_t m_tx_data_spi[TX_RX_MSG_LENGTH]; ///< SPI master TX buffer.
	static uint8_t m_rx_data_spi[TX_RX_MSG_LENGTH]; ///< SPI master RX buffer.

	void read(nrf_drv_spi_t const * p_instance, uint8_t reg, uint8_t rx_data_spi[6]);
	uint8_t read_one_byte(nrf_drv_spi_t const * p_instance, uint8_t reg);
	void write(nrf_drv_spi_t const * p_instance, uint8_t reg, uint8_t value);
	void write_one_byte(nrf_drv_spi_t const * p_instance, uint8_t reg, uint8_t value);
	void temperature(void);
	void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);

	//=================================================GPIO and Interrupt routine===========================
	#ifdef test_pin
			#define PIN_IN test_pin
	#endif

	#ifndef PIN_IN
			#error "Please indicate input pin"
	#endif

	#ifdef BSP_LED_0
			#define PIN_OUT BSP_LED_0
	#endif
	#ifndef PIN_OUT
			#error "Please indicate output pin"
	#endif

	static void gpio_init(void)
	{
			ret_code_t err_code;
			
			err_code = nrf_drv_gpiote_init();
			APP_ERROR_CHECK(err_code);
			
			nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
			
			err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config);
			APP_ERROR_CHECK(err_code);
			
			nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
			in_config.pull = NRF_GPIO_PIN_PULLUP;
			
			err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
			APP_ERROR_CHECK(err_code);
			
			nrf_drv_gpiote_in_event_enable(PIN_IN, true);
	}


	//==========================================================================================
	//==========================================================================================
	/** @def  TX_RX_MSG_LENGTH
	 * number of bytes to transmit and receive. This amount of bytes will also be tested to see that
	 * the received bytes from slave are the same as the transmitted bytes from the master */
	#define TX_RX_MSG_LENGTH         100
	//*************************************************************************************
	#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
	#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
	#define UART_RX_BUF_SIZE 1                           /**< UART RX buffer size. */

	void uart_error_handle(app_uart_evt_t * p_event)
	{
			if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
			{
					APP_ERROR_HANDLER(p_event->data.error_communication);
			}
			else if (p_event->evt_type == APP_UART_FIFO_ERROR)
			{
					APP_ERROR_HANDLER(p_event->data.error_code);
			}
	}

	//*************************************************************************
	#ifdef ENABLE_LOOPBACK_TEST

	static void uart_loopback_test()
	{
			uint8_t * tx_data = (uint8_t *)("\n\rLOOPBACK_TEST\n\r");
			uint8_t   rx_data;

			// Start sending one byte and see if you get the same
			for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
			{
					uint32_t err_code;
					while(app_uart_put(tx_data[i]) != NRF_SUCCESS);

					nrf_delay_ms(10);
					err_code = app_uart_get(&rx_data);

					if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
					{
							show_error();
					}
			}
			return;
	}
	#endif
	//*************************************************************************************



	#if (SPI0_ENABLED == 1) 

	typedef enum
	{
			#if (SPI0_ENABLED == 1)
			TEST_STATE_SPI0_LSB,    ///< Test SPI0, bits order LSB
			TEST_STATE_SPI0_MSB,    ///< Test SPI0, bits order MSB
			#endif
			END_OF_TEST_SEQUENCE
	} spi_master_ex_state_t;

	static volatile bool m_transfer_completed = true;
	static spi_master_ex_state_t m_spi_master_ex_state = (spi_master_ex_state_t)1;

	#if (SPI0_ENABLED == 1)
	static const nrf_drv_spi_t m_spi_master_0 = NRF_DRV_SPI_INSTANCE(0);
	#endif


	void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
	{
			UNUSED_VARIABLE(bsp_indication_set(BSP_INDICATE_FATAL_ERROR));

			/*for (;;)
			{
					// No implementation needed.
			}*/
	}
	void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
	{
		uint8_t tempvar1;
		nrf_drv_spi_t const * p_instance;	
		p_instance = &m_spi_master_0;
		
		printf("Done!!!\r\n");

		nrf_drv_gpiote_out_clear(PIN_OUT);
		nrf_delay_ms(20);
		tempvar1=read_one_byte(p_instance,0x0B);
		//while(((tempvar1) & 0x20)==0x00);
		printf("Status_Register_ISR= %x\r\n",tempvar1);
	}


	#if (SPI0_ENABLED == 1)
	/**@brief Handler for SPI0 master events.
	 *
	 * @param[in] event SPI master event.
	 */
	void spi_master_0_event_handler(nrf_drv_spi_event_t event)
	{
			switch (event)
			{
					case NRF_DRV_SPI_EVENT_DONE:
							// Check if received data is correct.
							//result = check_buf_equal(m_tx_data_spi, m_rx_data_spi, TX_RX_MSG_LENGTH);
							//APP_ERROR_CHECK_BOOL(result);

							//nrf_drv_spi_uninit(&m_spi_master_0);

							//err_code = bsp_indication_set(BSP_INDICATE_RCV_OK);
							//APP_ERROR_CHECK(err_code);

							m_transfer_completed = true;
							break;

					default:
							// No implementation needed.
							break;
			}
	}
	#endif // (SPI0_ENABLED == 1)


	/**@brief Function for initializing a SPI master driver.
	 *
	 * @param[in] p_instance    Pointer to SPI master driver instance.
	 * @param[in] lsb           Bits order LSB if true, MSB if false.
	 */
	static void spi_master_init(nrf_drv_spi_t const * p_instance, bool lsb)
	{
			uint32_t err_code = NRF_SUCCESS;

			nrf_drv_spi_config_t config =
			{
					.ss_pin       = SPIM0_SS_PIN,
					.irq_priority = APP_IRQ_PRIORITY_LOW,
					.orc          = 0x00,
					.frequency    = NRF_DRV_SPI_FREQ_2M,
					.mode         = NRF_DRV_SPI_MODE_0,
					.bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
					//.bit_order    = (lsb ?
					//    NRF_DRV_SPI_BIT_ORDER_LSB_FIRST : NRF_DRV_SPI_BIT_ORDER_MSB_FIRST),
			};

			#if (SPI0_ENABLED == 1)
			if (p_instance == &m_spi_master_0)
			{
					config.sck_pin  = SPIM0_SCK_PIN;
					config.mosi_pin = SPIM0_MOSI_PIN;
					config.miso_pin = SPIM0_MISO_PIN;
					err_code = nrf_drv_spi_init(p_instance, &config,
							spi_master_0_event_handler);
			}
			else
			#endif // (SPI0_ENABLED == 1)

			{}

			APP_ERROR_CHECK(err_code);
	}
	void act_inact_init()
	{
			nrf_drv_spi_t const * p_instance;	
			p_instance = &m_spi_master_0;	

		
			write(p_instance,0x20,0x5C);                //activity threshold_L dec 250=0xFA
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x21,0x00);								//activity threshold_H=0x00
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x23,0x50);								//inactivity threshold_L dec 150=0x96
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x24,0x00);								//inactivity threshold_H dec 0=0x00
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			/*write(p_instance,0x22,0x01);							//activity timer dec 30=0x1E here ODR = 100 Hence Time in sec = 30/100 = 0.3 sec
			while(m_transfer_completed == false)
			m_transfer_completed = false;*/
			write(p_instance,0x25,0x01);								//inactivity timer dec 30=0x1E here ODR = 100 Hence Time in sec = 30/100 = 0.3 sec
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x27,0x3F);					//loop mode,referenced mode.	
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x2B,0x70);					//awake,Activity,Inactivity interrupts and Active low INT2 pin					
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x2C,0x83);
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x2D,0x0A);					//begin measurement
			while(m_transfer_completed == false)
			m_transfer_completed = false;

	}


	/**@brief Function for executing and switching state.
	 *
	 */
	static void switch_state(void)
	{
			nrf_drv_spi_t const * p_instance;

			switch (m_spi_master_ex_state)
			{
					#if (SPI0_ENABLED == 1)
					case TEST_STATE_SPI0_LSB:
							p_instance = &m_spi_master_0;
							spi_master_init(p_instance, true);
							break;

					case TEST_STATE_SPI0_MSB:
							p_instance = &m_spi_master_0;
							spi_master_init(p_instance, false);
							break;
					#endif // (SPI0_ENABLED == 1)

					default:
							return;
			}
		//	adxl_int();
			
	}
			


	#endif // (SPI0_ENABLED == 1) 


	/**@brief Function for initializing bsp module.
	 */
	void bsp_configuration()
	{
			uint32_t err_code = NRF_SUCCESS;

			NRF_CLOCK->LFCLKSRC            = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
			NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
			NRF_CLOCK->TASKS_LFCLKSTART    = 1;

			while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
			{
					// Do nothing.
			}

			APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL);

			err_code = bsp_init(BSP_INIT_LED, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);
			APP_ERROR_CHECK(err_code);
	}


	/** @brief Function for main application entry.
	 */
	int main(void)
	{
			// Setup bsp module.
			bsp_configuration();
			gpio_init();
			LEDS_CONFIGURE(LEDS_MASK);
			LEDS_OFF(LEDS_MASK);
			switch_state();
			act_inact_init();
			//nrf_temp_init();
			uint8_t tempvar[8] = {0},tempvar1;
			short temp1;
			uint32_t err_code;
			int32_t volatile temp;
			
		
			const app_uart_comm_params_t comm_params =
				{
						RX_PIN_NUMBER,
						TX_PIN_NUMBER,
						RTS_PIN_NUMBER,
						CTS_PIN_NUMBER,
						APP_UART_FLOW_CONTROL_ENABLED,
						false,
						UART_BAUDRATE_BAUDRATE_Baud115200
				};

				APP_UART_FIFO_INIT(&comm_params,
													 UART_RX_BUF_SIZE,
													 UART_TX_BUF_SIZE,
													 uart_error_handle,
													 APP_IRQ_PRIORITY_LOW,
													 err_code);

			APP_ERROR_CHECK(err_code);

			nrf_drv_spi_t const * p_instance;	
			p_instance = &m_spi_master_0;	
				 
			#if (SPI0_ENABLED == 1) 
			if (m_transfer_completed)
			{
					m_transfer_completed = false;	//it equivalent to TI flag in 8051 checking TI==0;
					
					while(true)
					{
							act_inact_init();
							write_one_byte(p_instance,0x0B,0x00);		
							while(m_transfer_completed == false);
							m_transfer_completed = false;
							nrf_drv_gpiote_out_set(PIN_OUT);
							//printf("Interrupt not generated\r\n");
							nrf_delay_ms(100*DELAY_MS);
							read(p_instance,0x0E, tempvar);
							tempvar1=read_one_byte(p_instance,0x0B);
							printf("Status Register = %x\r\n",tempvar1);
							write_one_byte(p_instance,0x0B,0x00);		
							while(m_transfer_completed == false);
							m_transfer_completed = false;
										

						/*	if((tempvar[3] & 0x08) == 0x08)
							{
								temp1=(short)((tempvar[3] & 0x07)<<8 | tempvar[2] | 0xf800);
								printf("%d\t", temp1);
							}
							else
							{
								temp1=(short)((tempvar[3] & 0x07)<<8 | tempvar[2] | 0x0000);
								printf(" %d\t", temp1);
							}
							if((tempvar[5] & 0x08) == 0x08)
							{
								temp1=((tempvar[5] & 0x07)<<8 | tempvar[4] | 0xf800);
								printf("%d\t", temp1);
							}
							else
							{
								temp1=(short)((tempvar[5] & 0x07)<<8 | tempvar[4] | 0x0000);
								printf(" %d\t", temp1);
							}
							if((tempvar[7] & 0x08)== 0x08)
							{
								temp1=((tempvar[7] & 0x07)<<8 | tempvar[6] | 0xf800);
								printf("%d\n\r", temp1);
							}
							else
							{
								temp1=(short)((tempvar[7] & 0x07)<<8 | tempvar[6] | 0x0000);
								printf("%d\n\r", temp1);
							}*/
								
								//printf("%d\t%d\t%d\n\r", tempvar[2], tempvar[4], tempvar[6]);
							//printf("%d\t%d\t%d\t%d\t%d\t%d\t\n\r", tempvar[2], tempvar[3], tempvar[4],tempvar[5],tempvar[6],tempvar[7]);
							
					}
			}
			#endif // (SPI0_ENABLED == 1) 		
	}

	uint8_t read_one_byte(nrf_drv_spi_t const * p_instance, uint8_t reg)
	{
			m_tx_data_spi[0] = 0x0B;
			m_tx_data_spi[1] = reg;
			m_tx_data_spi[2] = 0x00;
			uint32_t err_code = nrf_drv_spi_transfer(p_instance,
					m_tx_data_spi, 2, m_rx_data_spi, 3);
			
			while(m_transfer_completed == false);
			m_transfer_completed = false;
			
			
		return m_rx_data_spi[2];
	}

	void write_one_byte(nrf_drv_spi_t const * p_instance, uint8_t reg, uint8_t value)
	{	
			m_tx_data_spi[0] = 0x0A;
			m_tx_data_spi[1] = reg;
			m_tx_data_spi[2] = value;
			m_tx_data_spi[3] = 0x00;
		
			uint32_t err_code = nrf_drv_spi_transfer(p_instance,m_tx_data_spi, 3, m_rx_data_spi, 3); //istead of 3 length of m_rx_data_spi 0 was written?
			
	}
Related