Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max31855: stop using SPI from ISR #891

Merged
merged 4 commits into from Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/modules/tools/temperaturecontrol/TempSensor.h
Expand Up @@ -26,6 +26,7 @@ class TempSensor
virtual bool set_optional(const sensor_options_t& options) { return false; }
virtual bool get_optional(sensor_options_t& options) { return false; }
virtual void get_raw() {}
virtual void on_idle() {}
};

#endif
7 changes: 7 additions & 0 deletions src/modules/tools/temperaturecontrol/TemperatureControl.cpp
Expand Up @@ -103,6 +103,7 @@ void TemperatureControl::on_module_loaded()
this->register_for_event(ON_MAIN_LOOP);
this->register_for_event(ON_SET_PUBLIC_DATA);
this->register_for_event(ON_HALT);
this->register_for_event(ON_IDLE);
}
}

Expand All @@ -116,6 +117,12 @@ void TemperatureControl::on_halt(void *arg)
}
}

void TemperatureControl::on_idle(void *arg)
{
sensor->on_idle();
}


void TemperatureControl::on_main_loop(void *argument)
{
if (this->temp_violated) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/tools/temperaturecontrol/TemperatureControl.h
Expand Up @@ -26,6 +26,7 @@ class TemperatureControl : public Module {
void on_get_public_data(void* argument);
void on_set_public_data(void* argument);
void on_halt(void* argument);
void on_idle(void* argument);

void set_desired_temperature(float desired_temperature);

Expand Down
47 changes: 28 additions & 19 deletions src/modules/tools/temperaturecontrol/max31855.cpp
Expand Up @@ -22,6 +22,7 @@
Max31855::Max31855() :
spi(nullptr)
{
this->read_flag=true;
}

Max31855::~Max31855()
Expand Down Expand Up @@ -57,40 +58,36 @@ void Max31855::UpdateConfig(uint16_t module_checksum, uint16_t name_checksum)
spi->format(16);
}

// returns an average of the last few temperature values we've read
float Max31855::get_temperature()
{
// Return an average of the last readings
if (readings.size() >= readings.capacity()) {
readings.delete_tail();
}

float temp = read_temp();
// allow read from hardware via SPI on next call to on_idle()
this->read_flag=true;

// Discard occasional errors...
if(!isinf(temp))
{
readings.push_back(temp);
}
// Return an average of the last readings
if(readings.size()==0) return infinityf();

if(readings.size()==0) return infinityf();

float sum = 0;
float sum = 0;
for (int i=0; i<readings.size(); i++) {
sum += *readings.get_ref(i);
}

return sum / readings.size();
return sum / readings.size();
}

float Max31855::read_temp()
// ask the temperature sensor hardware for a value, store it in a buffer
void Max31855::on_idle()
{
// this rate limits SPI access
if(!this->read_flag) return;

this->spi_cs_pin.set(false);
wait_us(1); // Must wait for first bit valid

// Read 16 bits (writing something as well is required by the api)
uint16_t data = spi->write(0);
// Read next 16 bits (diagnostics)
// uint16_t data2 = spi->write(0);
// Read next 16 bits (diagnostics)
// uint16_t data2 = spi->write(0);

this->spi_cs_pin.set(true);

Expand All @@ -114,5 +111,17 @@ float Max31855::read_temp()
temperature = ((data & 0x1FFF) + 1) / -4.f;
}
}
return temperature;

if (readings.size() >= readings.capacity()) {
readings.delete_tail();
}

// Discard occasional errors...
if(!isinf(temperature))
{
readings.push_back(temperature);
}

// don't read again until get_temperature() is called
this->read_flag=false;
}
3 changes: 2 additions & 1 deletion src/modules/tools/temperaturecontrol/max31855.h
Expand Up @@ -21,9 +21,10 @@ class Max31855 : public TempSensor
~Max31855();
void UpdateConfig(uint16_t module_checksum, uint16_t name_checksum);
float get_temperature();
void on_idle();

private:
float read_temp();
struct { bool read_flag:1; } ; //when true, the next call to on_idle will read a new temperature value
Pin spi_cs_pin;
mbed::SPI *spi;
RingBuffer<float,16> readings;
Expand Down