This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Activity detection using ADXL362 and nrf52.

Hello, I want to detect the any activity of the ADXL 362 and blink the LED ON and it should also detect the inactivity to turn OFF the LED.I am able to generate the activity interrupt using following code but the code does not wait for inactivity event.it will suddenly turn off LED.Can Any one please suggest me the necessary changes in the code?Please tell me how can I decide threshold value from accelerometer data.This my code:

	#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.


	void read(nrf_drv_spi_t const * p_instance, uint8_t reg, uint8_t rx_data_spi[6]);
	void write(nrf_drv_spi_t const * p_instance, uint8_t reg, uint8_t value);
	void temperature(void);

	//=================================================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
	void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
	{
		printf("Done!!!\r\n");
		//nrf_delay_ms(2000*DELAY_MS);
		nrf_drv_gpiote_out_set(PIN_OUT);
		/*int i=0;
		while(i<5)
		{
			
			
			nrf_drv_gpiote_out_set(PIN_OUT);
			nrf_delay_ms(100);
			nrf_drv_gpiote_out_clear(PIN_OUT);
			nrf_delay_ms(100);
			i++;
		}*/

	}

	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_LOTOHI(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.
		}*/
	}



	#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,0x10);								//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,0x0A);								//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,0x10);							//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,0x20);					//awake,Activity,Inactivity interrupts and Active low INT2 pin					
			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;

	}


	void adxl_int()
	{
			nrf_drv_spi_t const * p_instance;	
			p_instance = &m_spi_master_0;	

		
			write(p_instance,0x23,0x58);
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x25,0x03);
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x27,0x0C);
			while(m_transfer_completed == false)
			m_transfer_completed = false;
			write(p_instance,0x2B,0x20);
			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);
			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);
		uint32_t err_code;
			int32_t volatile temp;
		nrf_temp_init();
			uint8_t tempvar[8] = {0};
			short temp1;
			switch_state();
			adxl_int();
			//act_inact_init();
	  
			
		
			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();
							nrf_drv_gpiote_out_set(PIN_OUT);
							//printf("Interrupt not generated\r\n");
							nrf_delay_ms(DELAY_MS);
							read(p_instance,0x0E, tempvar);
							//read(p_instance,0x0B, tempvar);
							//printf("Status Register=%x\r\n");
							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[3], tempvar[5], tempvar[7]);
							//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]);
						
							//temperature();
					}
			}
			#endif // (SPI0_ENABLED == 1) 		
	}


	/*void temperature()
	{
			int32_t volatile temp;
		
			NRF_TEMP->TASKS_START = 1;
			while (NRF_TEMP->EVENTS_DATARDY == 0)
									{
										// Do nothing.
									}
			NRF_TEMP->EVENTS_DATARDY = 0;
			temp = (nrf_temp_read() / 4);
			NRF_TEMP->TASKS_STOP = 1;
			//str_print("Actual temperature: ");
			printf("Actual temperature: %d\n\r", (int)temp);
			//print(temp);
	}
	*/
Parents Reply Children
No Data
Related