Changeset - 573011597aec
[Not reviewed]
default
0 8 0
Ethan Zonca - 8 years ago 2017-07-25 07:14:34
ez@ethanzonca.com
Attempt to get temp working
8 files changed with 34 insertions and 19 deletions:
0 comments (0 inline, 0 general)
.settings/language.settings.xml
Show inline comments
 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 
<project>
 
	<configuration id="ilg.gnuarmeclipse.managedbuild.cross.config.elf.debug.1538442532" name="Debug">
 
		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 
			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 
			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 
			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
 
			<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-796171634606229038" id="ilg.gnuarmeclipse.managedbuild.cross.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings Cross ARM" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 
			<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1073395970633912068" id="ilg.gnuarmeclipse.managedbuild.cross.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings Cross ARM" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 
				<language-scope id="org.eclipse.cdt.core.gcc"/>
 
				<language-scope id="org.eclipse.cdt.core.g++"/>
 
			</provider>
 
		</extension>
 
	</configuration>
 
	<configuration id="ilg.gnuarmeclipse.managedbuild.cross.config.elf.release.1229152259" name="Release">
 
		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 
			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 
			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 
			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
 
			<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-786907217906447192" id="ilg.gnuarmeclipse.managedbuild.cross.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings Cross ARM" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 
			<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1105339482864355034" id="ilg.gnuarmeclipse.managedbuild.cross.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings Cross ARM" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 
				<language-scope id="org.eclipse.cdt.core.gcc"/>
 
				<language-scope id="org.eclipse.cdt.core.g++"/>
 
			</provider>
 
		</extension>
 
	</configuration>
 
</project>
inc/tempsense.h
Show inline comments
 
#ifndef TEMPSENSE_H
 
#define TEMPSENSE_H
 

	
 

	
 
#define TEMPSENSE_MAX_CS_PIN GPIO_PIN_15
 
#define TEMPSENSE_MAX_CS_PORT GPIOA
 

	
 

	
 
void tempsense_init(void);
 
float tempsense_readtemp(void);
 
void tempsense_readtemp(void);
 
float tempsense_gettemp(void);
 

	
 
#endif
lib/max31856/max31856.c
Show inline comments
 
//
 
// MAX31856: Driver to configure and read temperature from the MAX31856 thermoouple-to-Digital IC
 
//
 

	
 
#include "max31856.h"
 
#include "states.h"
 
#include "error.h"
 

	
 
// Private variables
 
static float temp_latest = 0.0;
 
static float temp_avg = 0.0;
 

	
 
static SPI_HandleTypeDef* spiport;
 
SPI_HandleTypeDef* spiport;
 
static GPIO_TypeDef* csport;
 
static uint32_t cspin;
 

	
 

	
 
// Private prototypes
 
static void __cs_assert(void);
 
static void __cs_deassert(void);
 
static void __write_reg(uint8_t reg, uint8_t data);
 
static void __read_reg(uint8_t reg, uint8_t* rxbuf, uint8_t len);
 

	
 

	
 
// Initialize the MAX31856 driver
 
void max31856_init(SPI_HandleTypeDef* spi_port, GPIO_TypeDef* cs_port, uint32_t cs_pin, uint32_t sensor_type)
 
{
 
	// Set CS pin references
 
	csport = cs_port;
 
	cspin = cs_pin;
 

	
 
	// Set SPI port reference
 
	spiport = spi_port;
 

	
 
	// Configure the CS pin for output
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
	GPIO_InitStruct.Pin = cs_pin;
 
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
	HAL_GPIO_Init(cs_port, &GPIO_InitStruct);
 

	
 
	// MAX31856
 
	// - set to continuous conversion mode
 
	// - probably no filtering, we'll do that on this side of things
 
	// - set up to read the typical open/short faults but not the high/low alarms
 

	
 

	
 
	// TODO: Enable open/short detection
 
	// Enables auto conv
 
	__write_reg(MAX31856_CR0_REG, MAX31856_CR0_OCFAULT0 | MAX31856_CR0_AUTOCONVERT);
 
	__write_reg(MAX31856_CR0_REG, MAX31856_CR0_AUTOCONVERT); //  MAX31856_CR0_OCFAULT0
 

	
 
	// Averaging set to 1 sample, TC type set to K
 
	__write_reg(MAX31856_CR1_REG, MAX31856_TCTYPE_K);
 

	
 
	// sensor type - could we just mask bits off? maybe optimize the enum for this
 
}
 

	
 

	
 
// Pull reading from the MAX31856 IC
 
void max31856_process(void)
 
float max31856_process(void)
 
{
 
	uint8_t tempbuf[4];
 
	uint8_t tempbuf[3];
 
	__read_reg(MAX31856_LTCBH_REG, tempbuf, 3);
 

	
 

	
 
	int32_t temp24 = tempbuf[0] << 16 | tempbuf[1] << 8 | tempbuf[0];
 
	if (temp24 & 0x800000) {
 
		temp24 |= 0xFF000000;  // fix sign
 
	}
 

	
 
	temp24 >>= 5;  // bottom 5 bits are unused
 

	
 
	float tempfloat = temp24;
 
	tempfloat *= 0.0078125;
 

	
 
	temp_latest = tempfloat;
 
	return tempfloat;
 

	
 

	
 

	
 

	
 
	// Read temperature from the MAX31856 (approx 10hz optimally)
 
	uint8_t data[] = {0,0,0,0};
 
	HAL_SPI_Transmit(spiport, data, 1, 100);
 
//	uint8_t data[] = {0,0,0,0};
 
//	HAL_SPI_Transmit(spiport, data, 1, 100);
 

	
 
}
 

	
 

	
 
// Return latest temperature reading (unaveraged, deg C)
 
float max31856_latest_temp(void)
 
{
 
	return temp_latest;
 
}
 

	
 

	
 
// Return average temperature reading (deg C)
 
float max31856_avg_temp(void)
 
{
 
	return temp_latest;
 
}
 

	
 

	
 
static void __write_reg(uint8_t reg, uint8_t data)
 
{
 
	// Set write bit
 
	reg |= MAX31856_WRITE_BIT;
 

	
 
	uint8_t outarr[2] = {reg, data};
 

	
 
	// Assert the bus
 
	__cs_assert();
 

	
 
	HAL_Delay(1);
 

	
 
	// Write data
 
	HAL_SPI_Transmit(spiport, outarr, 2, 100);
 
	volatile HAL_StatusTypeDef res = HAL_SPI_Transmit(spiport, outarr, 2, 100);
 

	
 
	HAL_Delay(1);
 

	
 
	// Release the bus
 
	__cs_deassert();
 
}
 

	
 
static void __read_reg(uint8_t reg, uint8_t* rxbuf, uint8_t len)
 
{
 
	// Transmit buffer only uses first item for reg addr
 
	uint8_t regarr[1] = {reg};
 

	
 

	
 
	HAL_Delay(1);
 
	// Assert the bus
 
	__cs_assert();
 
	HAL_Delay(1);
 

	
 
	// Send address
 
	HAL_SPI_Transmit(spiport, regarr, 1, 100);
 
	volatile HAL_StatusTypeDef res = HAL_SPI_Transmit(spiport, regarr, 1, 100);
 
	HAL_Delay(1);
 

	
 
	// Receive data
 
	HAL_SPI_Receive(spiport, rxbuf, len, 100);
 
	volatile HAL_StatusTypeDef res2 = HAL_SPI_Receive(spiport, rxbuf, len, 100);
 
	HAL_Delay(1);
 

	
 
	// Release bus
 
	__cs_deassert();
 
	HAL_Delay(1);
 

	
 
}
 

	
 
static void __cs_assert(void)
 
{
 
	HAL_GPIO_WritePin(csport, cspin, 0);
 
}
 

	
 
static void __cs_deassert(void)
 
{
 
	HAL_GPIO_WritePin(csport, cspin, 1);
 
}
lib/max31856/max31856.h
Show inline comments
 
#ifndef MAX31856_H
 
#define MAX31856_H
 

	
 
#include "stm32f3xx_hal.h"
 

	
 
void max31856_init(SPI_HandleTypeDef* spi_port, GPIO_TypeDef* cs_port, uint32_t cs_pin, uint32_t sensor_type);
 
void max31856_process(void);
 
float max31856_process(void);
 
float max31856_latest_temp(void);
 
float max31856_avg_temp(void);
 

	
 
// Thanks to Adafruit:
 
#define MAX31856_CR0_REG           0x00
 
#define MAX31856_CR0_AUTOCONVERT   0x80
 
#define MAX31856_CR0_1SHOT         0x40
 
#define MAX31856_CR0_OCFAULT1      0x20
 
#define MAX31856_CR0_OCFAULT0      0x10
 
#define MAX31856_CR0_CJ            0x08
 
#define MAX31856_CR0_FAULT         0x04
 
#define MAX31856_CR0_FAULTCLR      0x02
 

	
 
#define MAX31856_CR1_REG           0x01
 
#define MAX31856_MASK_REG          0x02
 
#define MAX31856_CJHF_REG          0x03
 
#define MAX31856_CJLF_REG          0x04
 
#define MAX31856_LTHFTH_REG        0x05
 
#define MAX31856_LTHFTL_REG        0x06
 
#define MAX31856_LTLFTH_REG        0x07
 
#define MAX31856_LTLFTL_REG        0x08
 
#define MAX31856_CJTO_REG          0x09
 
#define MAX31856_CJTH_REG          0x0A
 
#define MAX31856_CJTL_REG          0x0B
lib/ssd1306/ssd1306.c
Show inline comments
 
@@ -42,53 +42,53 @@ void ssd1306_init(void)
 
	GPIO_InitStruct.Pin = OLED_CS_Pin|OLED_RESET_Pin|OLED_DC_Pin;
 
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
	HAL_GPIO_Init(OLED_DC_GPIO_Port, &GPIO_InitStruct);
 
 
 
	// Set up MOSI/MISO/SCK
 
	GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
 
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
	GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
 
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 
 
	// Set up SPI port for OLED
 
	hspi1.Instance = SPI3;
 
	hspi1.Init.Mode = SPI_MODE_MASTER;
 
	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 
	hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 
	hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 
	hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 
	hspi1.Init.NSS = SPI_NSS_SOFT;
 
	hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
 
	hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; //16;
 
	hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 
	hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
 
	hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
 
	hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLED;
 
	hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLED;
 
	HAL_SPI_Init(&hspi1);
 
 
 
	// Generate a reset
 
	SSD_Reset_Low();
 
	uint32_t i;
 
	for(i=5000; i>1; i--)
 
	SSD_Reset_High();
 
 
	WriteCommand(0xAE);
 
	WriteCommand(0xD5);
 
	WriteCommand(0x80);
 
	WriteCommand(0xA8);
 
	WriteCommand(0x1F);
 
	WriteCommand(0xD3);
 
	WriteCommand(0x00);
 
	WriteCommand(0x40 | 0x00); // line #0
 
	WriteCommand(0x8D);
 
	WriteCommand(0x14); //10 or 14 if not externalvcc
 
	WriteCommand(0x20);
 
	WriteCommand(0x00);
 
	//  WriteCommand(0xA0 | 0x1); // segremap (normal)
 
	WriteCommand(0xA0); // segremap (flip)
 
	//  WriteCommand(0xC8); // comscandec (normal)
src/main.c
Show inline comments
 
@@ -44,49 +44,49 @@ int main(void)
 
    pwmout_init();
 
    flash_init();
 
	watchdog_init();
 
	tempsense_init();
 
 
	// Soft timers
 
    uint32_t last_pid = 0;
 
    uint32_t last_thermostat = 0;
 
    uint32_t last_1hz = 0;
 
    uint32_t last_5hz = 0;
 
 
	while (1)
 
	{
 
		float duty = 0.0;
 
 
		if(HAL_GetTick() - last_1hz > 750)
 
		{
 
			display_1hz();
 
			last_1hz = HAL_GetTick();
 
		}
 
 
		if(HAL_GetTick() - last_5hz > 200)
 
		{
 
			tempsense_readtemp();
 
//			runtime_status = tempsense_gettemp();
 
			runtime_status()->temp = tempsense_gettemp();
 
			last_5hz = HAL_GetTick();
 
		}
 
 
        if(flash_getsettings()->val.control_mode == MODE_PID && (HAL_GetTick() - last_pid > PID_PERIOD))
 
        {
 
//        	runtime_status()->temp = tempsense_readtemp();
 
        	duty = pid_process();
 
            last_pid = HAL_GetTick();
 
        }
 
 
        // Thermostatic control
 
        if(flash_getsettings()->val.control_mode == MODE_THERMOSTAT && HAL_GetTick() - last_thermostat > SSR_PERIOD)
 
        {
 
//        	runtime_status()->temp = tempsense_readtemp();
 
        	duty = thermostat_process();
 
            last_thermostat = HAL_GetTick();
 
        }
 
 
        pwmout_process(duty);
 
        display_process();
 
        watchdog_feed();
 
 
 
//        // Transmit temperature over USB-CDC on a regular basis
src/system/flash.c
Show inline comments
 
@@ -52,40 +52,41 @@ void flash_savesettings()
 
	HAL_Delay(2);
 
}
 
 
 
// Restore configuration from flash memory, if any was previously saved
 
void flash_restoresettings(void)
 
{
 
	// Check for magic flash value
 
	if(eeprom_emulation[FLASH_MAGIC_LOC] == FLASH_MAGIC_VALUE)
 
	{
 
		// Read page of flash into settings structure
 
		uint16_t readctr = 0;
 
		for(readctr = 0; readctr < 128; readctr++)
 
		{
 
			settings.data[readctr] = eeprom_emulation[readctr];
 
		}
 
	}
 
	// No data in flash! Set defaults here
 
	else
 
	{
 
		settings.val.k_p = 10;
 
		settings.val.k_i = 0;
 
		settings.val.k_d = 0;
 
		settings.val.windup_guard = 300;
 
		settings.val.sensor_type = 1;
 
		//torestore.values.can_id = 22;
 
	}
 
}
 
 
 
// Accessor to retrieve settings structure
 
inline therm_settings_t* flash_getsettings(void)
 
{
 
	return &settings;
 
}
 
 
inline therm_status_t* runtime_status(void)
 
{
 
	return &status;
 
}
 
src/tempsense.c
Show inline comments
 
//
 
// TempSense: read temperature from TC, RTD, or NTC
 
//
 

	
 
#include "tempsense.h"
 
#include "flash.h"
 
#include "ssd1306/ssd1306.h"
 
#include "max31856/max31856.h"
 

	
 

	
 
void tempsense_init(void)
 
{
 
	// TODO: Rework SPI stuff... init the port in a sharedlib then pass ref to display and tempsense
 

	
 
	max31856_init(spi_get(), TEMPSENSE_MAX_CS_PORT, TEMPSENSE_MAX_CS_PIN, 0);
 

	
 
	// Maybe don't perform temp sensor setup in here, but in readtemp?
 
	// what happens if the user changes the tempsense type while running?
 
	// we need to re-init...
 

	
 

	
 
}
 

	
 
// Returns the latest reading from the configured temperature sensor
 
float tempsense_readtemp(void)
 
void tempsense_readtemp(void)
 
{
 

	
 
	switch(flash_getsettings()->val.sensor_type)
 
	{
 
		case SENSOR_TC_K:
 
		case SENSOR_TC_E:
 
		case SENSOR_TC_N:
 
		case SENSOR_TC_R:
 
		case SENSOR_TC_S:
 
		case SENSOR_TC_T:
 
		{
 
			// Read MAX31856
 
			max31856_process();
 
		} break;
 

	
 
		case SENSOR_NTC:
 
		{
 
			// Read analog value from internal ADC, linearize the reading, etc
 
		} break;
 

	
 
	}
 

	
 
	// either return latest reading from DMA loop (NTC, etc)
 
	// or initiate a blocking read and return it.
 
	// Need to gracefully handle the timeout...
 
}
 

	
 
float tempsense_gettemp(void)
 
{
 
	return max31856_latest_temp();
 
}
 

	
 

	
0 comments (0 inline, 0 general)