merakian
Created June 15, 2021

Healthy Lungs

Portable lung health sensor

Work in progress34
Healthy Lungs

Things used in this project

Hardware components

nRF5340 Development Kit
Nordic Semiconductor nRF5340 Development Kit
×1

Software apps and online services

VS Code
Microsoft VS Code

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

download_zcxP13q0mQ.jpeg

Schematics

download_BWVYlX77vt.jpeg

Code

Untitled file

C/C++
#define NRFX_I2S_ENABLED	1
//#define NRFX_SPIM0_ENABLED	1

#include <zephyr.h>
#include "buttonLed.h"
#include <nrfx_i2s.h>
#include <nrfx_spim.h>


#define BUFFER_SIZE	200
#define PING_BUFFER_DATA_READY	0
#define PONG_BUFFER_DATA_READY	1

uint32_t rxBufferPing[BUFFER_SIZE] = {0};
uint32_t rxBufferPong[BUFFER_SIZE] = {0};
uint32_t txBufferDummy[BUFFER_SIZE] = {'d', 'e', 'a', 'd', 'b', 'e', 'e', 'f'};

nrfx_i2s_buffers_t rx_buffer_ping = {
	.p_rx_buffer = rxBufferPing,
	.p_tx_buffer = txBufferDummy
};

nrfx_i2s_buffers_t rx_buffer_pong = {
	.p_rx_buffer = rxBufferPong,
	.p_tx_buffer = txBufferDummy
};

volatile int whichBuffer = PING_BUFFER_DATA_READY;

volatile int i = 0;
/*
static void i2s_data_handler(nrfx_i2s_buffers_t const* p_released, uint32_t status){
	
	// data corruption if p_released is null
	i++;

	if(i % 10000 == 0){
		led_toggle(LED0);
		i = 0;
	}
	nrfx_i2s_buffers_t* nextBuffer;
	if(p_released == &rx_buffer_ping){
		nextBuffer = &rx_buffer_pong;
		whichBuffer = PING_BUFFER_DATA_READY;
	}else{
		nextBuffer = &rx_buffer_ping;
		whichBuffer = PONG_BUFFER_DATA_READY;
	}



	nrfx_i2s_next_buffers_set(nextBuffer);
}
*/

#define SPI_INSTANCE  0                                          
static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);  

#define TEST_STRING "Nordic123456789012345678901234567890"
static uint8_t       m_tx_buf[] = TEST_STRING;           
static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];  
static const uint8_t m_length = sizeof(m_tx_buf);        

volatile int spim_counter = 0;
void spim_event_handler(nrfx_spim_evt_t const * p_event,
                       void *                  p_context)
{
    spim_counter++;
	if(spim_counter % 10000 == 0){
		led_toggle(LED0);
	}

}

int main()
{
	// sanity check
	//buttonLed_init();

	led_init();

	/*
	IRQ_CONNECT(DT_IRQN(DT_NODELABEL(i2s0)),
		    DT_IRQ(DT_NODELABEL(i2s0), priority),
		    nrfx_isr, nrfx_i2s_irq_handler, 0);

	// i2s format is unaligned, but alignment param is mandatory
	// NOTE: change priority

	// LRCK = MCK/Ratio
	// SCK = LRCK * 2 * SWIDTH
	// Ratio >= 2 * SWIDTH
	// SWIDTH = 24
	// Ratio = 96
	// MCK = 761904.8
	// LRCK = 7936.5 (audio sampling rate)
	// SCK = 380952.4
	nrfx_i2s_config_t p_config = {
		.sck_pin = 0,
		.lrck_pin = 1,
		.mck_pin = 2,
		.sdout_pin = 3,
		.sdin_pin = 7,             
		.irq_priority = DT_IRQ(DT_NODELABEL(i2s0), priority),
		.mode = 0,
		.format = 0,
		.alignment = 0,
		.sample_width = 2,
		.channels = 2,
		.mck_setup = 16,
		.ratio = 3
	};

	volatile nrfx_err_t i2s_err0 = nrfx_i2s_init(&p_config, i2s_data_handler);
	irq_enable(DT_IRQN(DT_NODELABEL(i2s0)));

	while(1){

		int i_temp = i;
		volatile nrfx_err_t i2s_err1 = nrfx_i2s_start(&rx_buffer_ping, BUFFER_SIZE, 0);
		while(i_temp >= i){

		}
		nrfx_i2s_stop();
		
	}
	*/
	
	IRQ_CONNECT(DT_IRQN(DT_NODELABEL(spi0)),
		    DT_IRQ(DT_NODELABEL(spi0), priority),
		    nrfx_isr, nrfx_spim_0_irq_handler, 0);
			

	nrfx_spim_config_t p_config = {
		.sck_pin = 0,
		.mosi_pin = 1,
		.miso_pin = 2,
		.ss_pin = NRFX_SPIM_PIN_NOT_USED,
		.ss_active_high = false,
		.irq_priority = DT_IRQ(DT_NODELABEL(spi0), priority),
		.orc = '\0',
		.frequency = NRF_SPIM_FREQ_125K,
		.mode = NRF_SPIM_MODE_0,
		.bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST 	
	};

	nrfx_err_t spim_err0 = nrfx_spim_init(&spi, &p_config, spim_event_handler, NULL);
	nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf, m_length);

	//led_init();
	while(1){
		volatile int flag = spim_counter;
		nrfx_spim_xfer(&spi, &xfer_desc, 0);
		while(flag >= spim_counter){

		}

	}
	



	return 0;
}

Credits

merakian
4 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.