Skip to content

Commit

Permalink
[stm32] Enable extended I2C on F0/F3/F7/L0 targets
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Nov 6, 2018
2 parents 771e3eb + 804516a commit e46e7df
Show file tree
Hide file tree
Showing 19 changed files with 813 additions and 92 deletions.
125 changes: 125 additions & 0 deletions examples/stm32f072_discovery/tmp102/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2014, Sascha Schade
* Copyright (c) 2014-2017, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>

#include <modm/processing/timer.hpp>
#include <modm/processing/protothread.hpp>
#include <modm/driver/temperature/tmp102.hpp>

#include <modm/io/iostream.hpp>

modm::IODeviceWrapper< Usart1, modm::IOBuffer::BlockIfFull > device;
modm::IOStream stream(device);

typedef I2cMaster1 MyI2cMaster;


class ThreadOne : public modm::pt::Protothread
{
public:
ThreadOne()
: temp(temperatureData, 0x48)
{
}

bool
update()
{
temp.update();

PT_BEGIN();

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


PT_CALL(temp.setUpdateRate(200));
PT_CALL(temp.enableExtendedMode());

PT_CALL(temp.configureAlertMode(
modm::tmp102::ThermostatMode::Comparator,
modm::tmp102::AlertPolarity::ActiveLow,
modm::tmp102::FaultQueue::Faults6));
PT_CALL(temp.setLowerLimit(28.f));
PT_CALL(temp.setUpperLimit(30.f));

while (true)
{
{
PT_CALL(temp.readComparatorMode(result));
float temperature = temperatureData.getTemperature();
uint8_t tI = (int) temperature;
uint16_t tP = (temperature - tI) * 10000;
stream << "T= " << tI << ".";
if (tP == 0)
{
stream << "0000 C";
}
else if (tP == 625)
{
stream << "0" << tP << " C";
}
else
{
stream << tP << " C";
}
stream << modm::endl;
if (result) stream << "Heat me up!" << modm::endl;
}
this->timeout.restart(200);
PT_WAIT_UNTIL(this->timeout.isExpired());
Board::LedDown::toggle();
}

PT_END();
}

private:
bool result;
modm::ShortTimeout timeout;
modm::tmp102::Data temperatureData;
modm::Tmp102<MyI2cMaster> temp;
};

ThreadOne one;

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

Usart1::connect<GpioA9::Tx>();
Usart1::initialize<Board::systemClock, 115'200>();

MyI2cMaster::connect<GpioB7::Sda, GpioB8::Scl>();
MyI2cMaster::initialize<Board::systemClock, 400'000>();

stream << "\n\nRESTART\n\n";

while (1)
{
one.update();
Board::LedUp::toggle();
}

return 0;
}
15 changes: 15 additions & 0 deletions examples/stm32f072_discovery/tmp102/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<library>
<extends>modm:board:disco-f072rb</extends>
<options>
<option name=":build:build.path">../../../build/stm32f072_discovery/tmp102</option>
</options>
<modules>
<module>:driver:tmp102</module>
<module>:io</module>
<module>:platform:gpio</module>
<module>:platform:i2c:1</module>
<module>:platform:uart:1</module>
<module>:processing:protothread</module>
<module>:build:scons</module>
</modules>
</library>
117 changes: 117 additions & 0 deletions examples/stm32f746g_discovery/tmp102/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2014, Sascha Schade
* Copyright (c) 2014-2017, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>

#include <modm/processing/timer.hpp>
#include <modm/processing/protothread.hpp>
#include <modm/driver/temperature/tmp102.hpp>

typedef I2cMaster1 MyI2cMaster;


class ThreadOne : public modm::pt::Protothread
{
public:
ThreadOne()
: temp(temperatureData, 0x48)
{
}

bool
update()
{
temp.update();

PT_BEGIN();

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


PT_CALL(temp.setUpdateRate(200));
PT_CALL(temp.enableExtendedMode());

PT_CALL(temp.configureAlertMode(
modm::tmp102::ThermostatMode::Comparator,
modm::tmp102::AlertPolarity::ActiveLow,
modm::tmp102::FaultQueue::Faults6));
PT_CALL(temp.setLowerLimit(28.f));
PT_CALL(temp.setUpperLimit(30.f));

while (true)
{
{
PT_CALL(temp.readComparatorMode(result));
float temperature = temperatureData.getTemperature();
uint8_t tI = (int) temperature;
uint16_t tP = (temperature - tI) * 10000;
MODM_LOG_INFO << "T= " << tI << ".";
if (tP == 0)
{
MODM_LOG_INFO << "0000 C";
}
else if (tP == 625)
{
MODM_LOG_INFO << "0" << tP << " C";
}
else
{
MODM_LOG_INFO << tP << " C";
}
if (result) { MODM_LOG_INFO << " Heat me up!"; }
MODM_LOG_INFO << modm::endl;
}
this->timeout.restart(200);
PT_WAIT_UNTIL(this->timeout.isExpired());
Board::LedD13::toggle();
}

PT_END();
}

private:
bool result;
modm::ShortTimeout timeout;
modm::tmp102::Data temperatureData;
modm::Tmp102<MyI2cMaster> temp;
};

ThreadOne one;

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
Board::LedD13::setOutput(modm::Gpio::Low);

MyI2cMaster::connect<Board::D14::Sda, Board::D15::Scl>();
MyI2cMaster::initialize<Board::systemClock, 400'000>();

MODM_LOG_INFO << "\n\nRESTART\n\n";

while (1)
{
one.update();
}

return 0;
}
13 changes: 13 additions & 0 deletions examples/stm32f746g_discovery/tmp102/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library>
<extends>modm:board:disco-f746ng</extends>
<options>
<option name=":build:build.path">../../../build/stm32f746g_discovery/tmp102</option>
</options>
<modules>
<module>:driver:tmp102</module>
<module>:io</module>
<module>:platform:i2c:1</module>
<module>:processing:protothread</module>
<module>:build:scons</module>
</modules>
</library>
2 changes: 1 addition & 1 deletion ext/modm-devices
Submodule modm-devices updated 66 files
+11 −0 .travis.yml
+4 −1 README.md
+2 −0 devices/stm32/stm32f0-30-8.xml
+2 −0 devices/stm32/stm32f0-30_70-4_6.xml
+3 −0 devices/stm32/stm32f0-30_70-b_c.xml
+2 −0 devices/stm32/stm32f0-31_51-4_6.xml
+2 −0 devices/stm32/stm32f0-38_48-6.xml
+2 −0 devices/stm32/stm32f0-42-4_6.xml
+2 −0 devices/stm32/stm32f0-51_71-8.xml
+2 −0 devices/stm32/stm32f0-58-8.xml
+2 −0 devices/stm32/stm32f0-71_91-b_c.xml
+2 −0 devices/stm32/stm32f0-72-8_b.xml
+2 −0 devices/stm32/stm32f0-78_98-b_c.xml
+2 −0 devices/stm32/stm32f3-01.xml
+2 −0 devices/stm32/stm32f3-02-6_8.xml
+2 −0 devices/stm32/stm32f3-02-b_c_d_e.xml
+2 −0 devices/stm32/stm32f3-03-6_8.xml
+2 −0 devices/stm32/stm32f3-03-b_c_d_e.xml
+2 −0 devices/stm32/stm32f3-18_28.xml
+2 −0 devices/stm32/stm32f3-34.xml
+2 −0 devices/stm32/stm32f3-58_98.xml
+2 −0 devices/stm32/stm32f3-73_78.xml
+2 −2 devices/stm32/stm32f4-01_11.xml
+2 −0 devices/stm32/stm32f7-22_23_32_33.xml
+2 −0 devices/stm32/stm32f7-30_50.xml
+2 −0 devices/stm32/stm32f7-45_46_56.xml
+2 −0 devices/stm32/stm32f7-65_67_68_69_77_78_79.xml
+3 −1 devices/stm32/stm32h7-43_53.xml
+3 −1 devices/stm32/stm32h7-50.xml
+3 −1 devices/stm32/stm32l0-11_21.xml
+3 −1 devices/stm32/stm32l0-31_41.xml
+3 −1 devices/stm32/stm32l0-51_52_53_62_63.xml
+3 −1 devices/stm32/stm32l0-71_72_73_81_82_83.xml
+524 −0 devices/stm32/stm32l4-12_22.xml
+2 −1 devices/stm32/stm32l4-31_33_43.xml
+2 −1 devices/stm32/stm32l4-32_42.xml
+2 −1 devices/stm32/stm32l4-51_71.xml
+2 −1 devices/stm32/stm32l4-52_62.xml
+2 −1 devices/stm32/stm32l4-75_85.xml
+2 −1 devices/stm32/stm32l4-76_86.xml
+2 −1 devices/stm32/stm32l4-96_a6.xml
+2 −1 devices/stm32/stm32l4-r5_r7_r9.xml
+2 −1 devices/stm32/stm32l4-s5_s7_s9.xml
+2 −5 tools/device/Makefile
+4 −4 tools/device/modm/__init__.py
+2 −2 tools/device/modm/device.py
+5 −5 tools/device/modm/device_file.py
+80 −50 tools/device/modm/device_identifier.py
+0 −0 tools/device/modm/exception.py
+1 −1 tools/device/modm/parser.py
+101 −31 tools/device/test/device_test.py
+0 −46 tools/device/test/name_test.py
+23 −6 tools/generator/Makefile
+1 −1 tools/generator/cmsis-header-stm32
+0 −1 tools/generator/dfg/avr/avr_device_tree.py
+18 −19 tools/generator/dfg/avr/avr_identifier.py
+2 −2 tools/generator/dfg/avr/avr_mcu.py
+38 −19 tools/generator/dfg/device_tree.py
+9 −20 tools/generator/dfg/input/xml.py
+12 −9 tools/generator/dfg/stm32/stm.py
+8 −13 tools/generator/dfg/stm32/stm_device_tree.py
+3 −0 tools/generator/dfg/stm32/stm_groups.py
+4 −4 tools/generator/dfg/stm32/stm_header.py
+7 −8 tools/generator/dfg/stm32/stm_identifier.py
+65 −28 tools/generator/dfg/stm32/stm_peripherals.py
+3 −3 tools/generator/raw-data-extractor/extract-stm32.sh
29 changes: 26 additions & 3 deletions src/modm/board/disco_f072rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,32 @@ namespace Board
struct systemClock
{
static constexpr int Frequency = MHz48;
static constexpr int Usart1 = Frequency;
static constexpr int Can = Frequency;
static constexpr int Spi2 = Frequency;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;

static constexpr uint32_t Adc = Apb;
static constexpr uint32_t Can = Apb;

static constexpr uint32_t Spi1 = Apb;
static constexpr uint32_t Spi2 = Apb;

static constexpr uint32_t Usart1 = Apb;
static constexpr uint32_t Usart2 = Apb;
static constexpr uint32_t Usart3 = Apb;
static constexpr uint32_t Usart4 = Apb;

static constexpr uint32_t I2c1 = Apb;
static constexpr uint32_t I2c2 = Apb;

static constexpr uint32_t Timer1 = Apb;
static constexpr uint32_t Timer2 = Apb;
static constexpr uint32_t Timer3 = Apb;
static constexpr uint32_t Timer6 = Apb;
static constexpr uint32_t Timer7 = Apb;
static constexpr uint32_t Timer14 = Apb;
static constexpr uint32_t Timer15 = Apb;
static constexpr uint32_t Timer16 = Apb;
static constexpr uint32_t Timer17 = Apb;

static bool inline
enable()
Expand Down
6 changes: 2 additions & 4 deletions src/modm/board/disco_f303vc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ using Int2 = GpioInputE5; // MEMS_INT4 [LSM303DLHC_INT2]: GPXTI5
using Scl = GpioB6; // I2C1_SCL [LSM303DLHC_SCL]: I2C1_SCL
using Sda = GpioB7; // I2C1_SDA [LSM303DLHC_SDA]: I2C1_SDA

// Hardware I2C not yet implemented for F3!
//using I2cMaster = I2cMaster1;
using I2cMaster = BitBangI2cMaster<Scl, Sda>;
using I2cMaster = I2cMaster1;
using Accelerometer = modm::Lsm303a< I2cMaster >;
}

Expand Down Expand Up @@ -225,7 +223,7 @@ initializeLsm3()
lsm3::Drdy::enableExternalInterrupt();
// lsm3::Drdy::enableExternalInterruptVector(12);

lsm3::I2cMaster::connect<lsm3::Scl::BitBang, lsm3::Sda::BitBang>();
lsm3::I2cMaster::connect<lsm3::Scl::Scl, lsm3::Sda::Sda>();
lsm3::I2cMaster::initialize<systemClock, 400000>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/modm/board/disco_f303vc/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def prepare(module, options):
":platform:clock",
":platform:core",
":platform:gpio",
":platform:i2c.bitbang",
":platform:i2c:1",
":platform:spi:1")
return True

Expand Down
25 changes: 25 additions & 0 deletions src/modm/board/disco_f746ng/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ using LedD13 = GpioOutputI1; // User LED 1 (Arduino D13)

using Leds = SoftwareGpioPort< LedD13 >;

// Arduino footprint
using A0 = GpioA0;
using A1 = GpioF10;
using A2 = GpioF9;
using A3 = GpioF8;
using A4 = GpioF7;
using A5 = GpioF6;

using D0 = GpioC7;
using D1 = GpioC6;
using D2 = GpioG6;
using D3 = GpioB4;
using D4 = GpioG7;
using D5 = GpioA8;
using D6 = GpioH6;
using D7 = GpioI3;
using D8 = GpioI2;
using D9 = GpioA15;
using D10 = GpioI0;
using D11 = GpioB15;
using D12 = GpioB14;
using D13 = GpioI1;
using D14 = GpioB9;
using D15 = GpioB8;


namespace stlink
{
Expand Down
4 changes: 2 additions & 2 deletions src/modm/platform/adc/stm32f3/adc.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public:
SynchronousPrescaler4 = {{ adc_ccr }}_CKMODE_1 | {{ adc_ccr }}_CKMODE_0,
};

%% if target["family"] in ["l4"]
%% if clock_mux
// ADCs clock source selection
enum class ClockSource : uint32_t
{
Expand Down Expand Up @@ -235,7 +235,7 @@ public:
*/
static inline void
initialize( const ClockMode clk = ClockMode::DoNotChange,
%% if target["family"] in ["l4"]
%% if clock_mux
const ClockSource clk_src = ClockSource::SystemClock,
%% endif
const Prescaler pre = Prescaler::Disabled,
Expand Down
Loading

0 comments on commit e46e7df

Please sign in to comment.