SPI and TWI Examples


#1

Could you add the SPI and TWI examples to the MDK? I’m trying to read from a MAX31855 (SPI) and when I get that working I’ll be trying the Adafruit 9-DOF sensor breakour (I2C).


#2

Hi,
Have you get these two sensors work? You can take the SPI & TWI example in the nRF5 SDK to get started first. I will push them soon.


#3

I didn’t get them working yet. I tried to use the examples in the sdk first and I managed to get the SPI example to compile and flash but I’m seeing nothing on the putty terminal, not even “SPI example started.”. The LED is blinking though so it’s made it to the main loop alright.


#4

I got the MAX31855k working, here is the code in case anyone needs it.

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_gpio.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define MAX_cs 13
#define MAX_clk 12
#define MAX_miso 14

#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))

unsigned long Max_Data;
unsigned long Temp_Data;
int bitCount;
double Temp_Ext;
double Temp_Int;

// Initialise the GPIO pins
void gpio_init(void)
{
nrf_gpio_cfg_output(MAX_cs);
nrf_gpio_cfg_output(MAX_clk);
nrf_gpio_cfg_input(MAX_miso, NRF_GPIO_PIN_NOPULL);
}


// Read data from the MAX31855 chip
void MAX31855_read(void)
{

	// Clear data
	Max_Data = 0;

	// Select the MAX31855 chip
	nrf_gpio_pin_clear(MAX_cs);

	// Shift in 32-bit of data
	for (bitCount = 31; bitCount >= 0; bitCount--)
	{
		nrf_gpio_pin_set(MAX_clk);

		// If data bit is high
		if (nrf_gpio_pin_read(MAX_miso))
		{
			// Need to type cast data type to unsigned long, else compiler will
			// truncate to 16-bit
			Max_Data |= ((unsigned long)1 << bitCount);
		}

		nrf_gpio_pin_clear(MAX_clk);
	}

	// Deselect MAX31855 chip
    nrf_gpio_pin_set(MAX_cs);

	if(CHECK_BIT(Max_Data, 0)){NRF_LOG_INFO("Open  Circuit");}
	else if(CHECK_BIT(Max_Data, 1)){NRF_LOG_INFO("Short Circuit to GND");}
	else if(CHECK_BIT(Max_Data, 2)){NRF_LOG_INFO("Short Circuit to VCC");}
	else{

		//*** Extract thermocouple data ***

		// Isolate external temp data
		Temp_Data = Max_Data >> 18;

		// Remove negative temp flag bit
		Temp_Ext = (Temp_Data & 0x00001FFF);

		// Check bit 31 for negative temp flag
		if (CHECK_BIT(Max_Data, 31))
		{
			// 2's complement operation
			// Invert
			Temp_Data = ~Temp_Data;
			// Ensure operation involves lower 13-bit only
			Temp_Ext = Temp_Data & 0x00001FFF;
			// Add 1 to obtain the positive number
			Temp_Ext += 1;
			// Make temperature negative
			Temp_Ext *= -1;
		}

		// Convert to Degree Celsius
        Temp_Ext *= 0.25;

		NRF_LOG_INFO("Thermocouple Temp: %d", Temp_Ext);


		//*** Extract internal temperature data ***

		// Remove the fault data bits & reserved bit
		Temp_Data = Max_Data >> 4;
		// Remove the thermocouple bits and reserved bit
		Temp_Int = (Temp_Data & 0x000007FF);

		// Check for negative temperature
		if (CHECK_BIT(Max_Data, 15))
		{
			// 2's complement operation
			// Invert
			Temp_Data = ~Temp_Data;
			// Ensure operation involves lower 11-bit only
			Temp_Int = Temp_Data & 0x000007FF;
			// Add 1 to obtain the positive number
			Temp_Int += 1;
			// Make temperature negative
			Temp_Int *= -1;
		}

		// Convert to Degree Celsius
		Temp_Int *= 0.0625;

		NRF_LOG_INFO("Internal Temp: %d", Temp_Int);
	}

	// Wait 100ms to ensure stable temperature measurement
	nrf_delay_ms(100);

}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
	gpio_init();

	uint32_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();

	NRF_LOG_INFO("MAX31855 Start.");

    while (true)
    {

		MAX31855_read();

		NRF_LOG_FLUSH();

    }
}

#5

:+1: Good Job~