Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
[examples] add example for STM32F4 discovery with AMSYS5915 pressure …
Browse files Browse the repository at this point in the history
…sensor
  • Loading branch information
rleh authored and salkinium committed Aug 12, 2017
1 parent 21d7231 commit fc59fc0
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/stm32f4_discovery/pressure_amsys5915/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# path to the xpcc root directory
xpccpath = '../../..'
# execute the common SConstruct file
execfile(xpccpath + '/scons/SConstruct')
120 changes: 120 additions & 0 deletions examples/stm32f4_discovery/pressure_amsys5915/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// coding: utf-8
/* Copyright (c) 2017, Raphael Lehmann
* All Rights Reserved.
*
* The file is part of the xpcc library and is released under the 3-clause BSD
* license. See the file `LICENSE` for the full license governing this code.
*/
// ----------------------------------------------------------------------------

#include <xpcc/architecture/platform.hpp>

#include <xpcc/processing.hpp>
#include <xpcc/driver/pressure/amsys5915.hpp>
#include <xpcc/io/iostream.hpp>
#include <xpcc/debug/logger.hpp>

xpcc::IODeviceWrapper< Usart2, xpcc::IOBuffer::BlockIfFull > device;

// Set all four logger streams to use the UART
xpcc::log::Logger xpcc::log::debug(device);
xpcc::log::Logger xpcc::log::info(device);
xpcc::log::Logger xpcc::log::warning(device);
xpcc::log::Logger xpcc::log::error(device);

// Set the log level
#undef XPCC_LOG_LEVEL
#define XPCC_LOG_LEVEL xpcc::log::DEBUG

/**
* Example to demonstrate a XPCC driver for pressure sensor AMSYS 5915
*
* This example uses I2cMaster2 of STM32F407
*
* SDA PB11
* SCL PB10
*
* GND and +3V are connected to the sensor.
*/

typedef I2cMaster2 MyI2cMaster;
// typedef xpcc::SoftwareI2cMaster<GpioB10, GpioB11> MyI2cMaster;

xpcc::amsys5915::Data data;
xpcc::Amsys5915<MyI2cMaster> pressureSensor(data);

class ThreadOne : public xpcc::pt::Protothread
{
public:
bool
update()
{
PT_BEGIN();

XPCC_LOG_DEBUG << "Ping the device from ThreadOne" << xpcc::endl;

// ping the device until it responds
while (true)
{
// we wait until the task started
if (PT_CALL(pressureSensor.ping())) {
break;
}
// otherwise, try again in 10ms
this->timeout.restart(10);
PT_WAIT_UNTIL(this->timeout.isExpired());
}

XPCC_LOG_DEBUG << "Device responded" << xpcc::endl;

while (true)
{
while (! PT_CALL(pressureSensor.readPressure()))
{
PT_YIELD();
}

XPCC_LOG_INFO << "Pressure [0..1]: " << data.getPressure() << xpcc::endl;
XPCC_LOG_INFO << "Temperature [degree centigrade]: " << data.getTemperature() << xpcc::endl;

// read next pressure measurement in 1ms
this->timeout.restart(1);
PT_WAIT_UNTIL(this->timeout.isExpired());
}

PT_END();
}
private:
xpcc::ShortTimeout timeout;
};

ThreadOne one;

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();

GpioOutputA2::connect(Usart2::Tx);
Usart2::initialize<Board::systemClock, xpcc::Uart::B115200>(10);

GpioB11::connect(I2cMaster2::Sda);
GpioB10::connect(I2cMaster2::Scl);

MyI2cMaster::initialize<Board::systemClock, 400000>();

XPCC_LOG_INFO << "\n\nWelcome to AMSYS 5915 pressure sensor demo!\n\n";

xpcc::ShortPeriodicTimer tmr(500);

while (1)
{
one.update();
if (tmr.execute()) {
Board::LedOrange::toggle();
}
}

return 0;
}
3 changes: 3 additions & 0 deletions examples/stm32f4_discovery/pressure_amsys5915/project.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
board = stm32f4_discovery
buildpath = ${xpccpath}/build/stm32f4_discovery/${name}

0 comments on commit fc59fc0

Please sign in to comment.