Skip to content

Commit dbfd93b

Browse files
hshosesalkinium
authored andcommitted
[driver] Adding AS5047 driver and example
1 parent 009bb06 commit dbfd93b

File tree

6 files changed

+351
-14
lines changed

6 files changed

+351
-14
lines changed

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -721,100 +721,101 @@ your specific needs.
721721
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ads816x">ADS816x</a></td>
722722
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ams5915">AMS5915</a></td>
723723
<td align="center"><a href="https://modm.io/reference/module/modm-driver-apa102">APA102</a></td>
724+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-as5047">AS5047</a></td>
724725
<td align="center"><a href="https://modm.io/reference/module/modm-driver-at24mac402">AT24MAC402</a></td>
725-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-block-device-spi-flash">SPI Flash</a></td>
726726
</tr><tr>
727+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-block-device-spi-flash">SPI Flash</a></td>
727728
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bme280">BME280</a></td>
728729
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmi088">BMI088</a></td>
729730
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmp085">BMP085</a></td>
730731
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bno055">BNO055</a></td>
731732
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cat24aa">CAT24AA</a></td>
732-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cycle_counter">CYCLE-COUNTER</a></td>
733733
</tr><tr>
734+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cycle_counter">CYCLE-COUNTER</a></td>
734735
<td align="center"><a href="https://modm.io/reference/module/modm-driver-drv832x_spi">DRV832X</a></td>
735736
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1302">DS1302</a></td>
736737
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1631">DS1631</a></td>
737738
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds18b20">DS18B20</a></td>
738739
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ea_dog">EA-DOG</a></td>
739-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input">Encoder Input</a></td>
740740
</tr><tr>
741+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input">Encoder Input</a></td>
741742
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input-bitbang">Encoder Input BitBang</a></td>
742743
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_output-bitbang">Encoder Output BitBang</a></td>
743744
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ft245">FT245</a></td>
744745
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ft6x06">FT6x06</a></td>
745746
<td align="center"><a href="https://modm.io/reference/module/modm-driver-gpio_sampler">Gpio Sampler</a></td>
746-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hclax">HCLAx</a></td>
747747
</tr><tr>
748+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hclax">HCLAx</a></td>
748749
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hd44780">HD44780</a></td>
749750
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc58x">HMC58x</a></td>
750751
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc6343">HMC6343</a></td>
751752
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hx711">HX711</a></td>
752753
<td align="center"><a href="https://modm.io/reference/module/modm-driver-i2c-eeprom">I2C-EEPROM</a></td>
753-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ili9341">ILI9341</a></td>
754754
</tr><tr>
755+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ili9341">ILI9341</a></td>
755756
<td align="center"><a href="https://modm.io/reference/module/modm-driver-is31fl3733">IS31FL3733</a></td>
756757
<td align="center"><a href="https://modm.io/reference/module/modm-driver-itg3200">ITG3200</a></td>
757758
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ixm42xxx">IXM42XXX</a></td>
758759
<td align="center"><a href="https://modm.io/reference/module/modm-driver-l3gd20">L3GD20</a></td>
759760
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lan8720a">LAN8720A</a></td>
760-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lawicel">LAWICEL</a></td>
761761
</tr><tr>
762+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lawicel">LAWICEL</a></td>
762763
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis302dl">LIS302DL</a></td>
763764
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3dsh">LIS3DSH</a></td>
764765
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3mdl">LIS3MDL</a></td>
765766
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lm75">LM75</a></td>
766767
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lp503x">LP503x</a></td>
767-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm303a">LSM303A</a></td>
768768
</tr><tr>
769+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm303a">LSM303A</a></td>
769770
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6ds33">LSM6DS33</a></td>
770771
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6dso">LSM6DSO</a></td>
771772
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
772773
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31855">MAX31855</a></td>
773774
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31865">MAX31865</a></td>
774-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
775775
</tr><tr>
776+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
776777
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max7219">MAX7219</a></td>
777778
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
778779
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
779780
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp3008">MCP3008</a></td>
780781
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
781-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
782782
</tr><tr>
783+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
783784
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
784785
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
785786
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5837">MS5837</a></td>
786787
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
787788
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
788-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
789789
</tr><tr>
790+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
790791
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
791792
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
792793
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
793794
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
794795
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
795-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-qmc5883l">QMC5883L</a></td>
796796
</tr><tr>
797+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-qmc5883l">QMC5883L</a></td>
797798
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
798799
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
799800
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
800801
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
801802
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
802-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
803803
</tr><tr>
804+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
804805
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
805806
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
806807
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
807808
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
808809
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
809-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
810810
</tr><tr>
811+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
811812
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
812813
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
813814
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
814815
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
815816
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
816-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
817817
</tr><tr>
818+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
818819
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
819820
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
820821
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// coding: utf-8
2+
/*
3+
* Copyright (c) 2024, Henrik Hose
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
12+
#include <modm/board.hpp>
13+
#include <modm/driver/encoder/as5047.hpp>
14+
#include <modm/processing.hpp>
15+
16+
using SpiMaster = SpiMaster1;
17+
18+
using Cs = modm::platform::GpioB10;
19+
using Mosi = modm::platform::GpioB5;
20+
using Miso = modm::platform::GpioB4;
21+
using Sck = modm::platform::GpioB3;
22+
23+
using namespace Board;
24+
using namespace modm::literals;
25+
26+
class EncoderThread : public modm::pt::Protothread
27+
{
28+
public:
29+
EncoderThread() : encoder(data) {}
30+
31+
bool
32+
run()
33+
{
34+
PT_BEGIN();
35+
36+
while (true)
37+
{
38+
PT_CALL(encoder.readout());
39+
40+
MODM_LOG_INFO << "\nNew readout:" << modm::endl;
41+
MODM_LOG_INFO << " angle degree: " << data.getAngleDeg() << " degrees" << modm::endl;
42+
MODM_LOG_INFO << " angle rad: " << data.getAngleRad() << " radians" << modm::endl;
43+
MODM_LOG_INFO << " angle raw: " << data.getAngleRaw() << modm::endl;
44+
45+
timeout.restart(std::chrono::milliseconds(500));
46+
PT_WAIT_UNTIL(timeout.isExpired());
47+
}
48+
49+
PT_END();
50+
}
51+
52+
private:
53+
modm::as5047::Data data;
54+
modm::As5047<SpiMaster, Cs> encoder;
55+
56+
modm::ShortTimeout timeout;
57+
} encoderThread;
58+
59+
int
60+
main()
61+
{
62+
Board::initialize();
63+
64+
Cs::setOutput(modm::Gpio::High);
65+
66+
SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>();
67+
SpiMaster::initialize<Board::SystemClock, 1.3_MHz>();
68+
69+
MODM_LOG_INFO << "==========AS5047 Test==========" << modm::endl;
70+
MODM_LOG_DEBUG << "Debug logging here" << modm::endl;
71+
MODM_LOG_INFO << "Info logging here" << modm::endl;
72+
MODM_LOG_WARNING << "Warning logging here" << modm::endl;
73+
MODM_LOG_ERROR << "Error logging here" << modm::endl;
74+
MODM_LOG_INFO << "===============================" << modm::endl;
75+
76+
while (true) { encoderThread.run(); }
77+
78+
return 0;
79+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:nucleo-g474re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g474re/as5047</option>
5+
</options>
6+
<modules>
7+
<module>modm:driver:as5047</module>
8+
<module>modm:platform:gpio</module>
9+
<module>modm:platform:spi:1</module>
10+
<module>modm:processing:protothread</module>
11+
<module>modm:processing:timer</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>

src/modm/driver/encoder/as5047.hpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// coding: utf-8
2+
// ----------------------------------------------------------------------------
3+
/*
4+
* Copyright (c) 2024, Henrik Hose
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
// ----------------------------------------------------------------------------
13+
14+
#ifndef MODM_AS5047_HPP
15+
#define MODM_AS5047_HPP
16+
17+
#include <array>
18+
#include <modm/architecture/interface/register.hpp>
19+
#include <modm/architecture/interface/spi_device.hpp>
20+
#include <modm/processing/resumable.hpp>
21+
#include <modm/processing/timer.hpp>
22+
#include <numbers>
23+
24+
namespace modm
25+
{
26+
27+
/// @cond
28+
namespace detail
29+
{
30+
constexpr uint16_t
31+
as5047_setMsbToEvenParity(const uint16_t num)
32+
{
33+
uint16_t par = 0x7fff & num;
34+
par ^= par >> 8;
35+
par ^= par >> 4;
36+
par ^= par >> 2;
37+
par ^= par >> 1;
38+
return ((par & 1) << 15) | (0x7fff & num);
39+
}
40+
} // namespace detail
41+
/// @endcond
42+
43+
/// @ingroup modm_driver_as5047
44+
struct as5047
45+
{
46+
enum class Errorfl : uint16_t
47+
{
48+
Parerr = Bit2,
49+
Invcomm = Bit1,
50+
Frerr = Bit0,
51+
};
52+
MODM_FLAGS16(Errorfl)
53+
54+
enum class Prog : uint16_t
55+
{
56+
Progver = Bit6,
57+
Progotp = Bit3,
58+
Otpref = Bit2,
59+
Progen = Bit0,
60+
};
61+
MODM_FLAGS16(Prog)
62+
63+
enum class Diaagc : uint16_t
64+
{
65+
Magl = Bit11,
66+
Magh = Bit10,
67+
Cof = Bit9,
68+
Lf = Bit8,
69+
};
70+
MODM_FLAGS16(Diaagc)
71+
72+
enum class Register : uint16_t
73+
{
74+
ReadNop = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x0000)),
75+
ReadErrfl = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x0001)),
76+
ReadProg = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x0003)),
77+
ReadDiaagc = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFC)),
78+
ReadMag = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFD)),
79+
ReadAngleunc = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFE)),
80+
ReadAnglecom = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFF)),
81+
82+
};
83+
84+
struct modm_packed Data
85+
{
86+
/// @return
87+
constexpr float
88+
getAngleRad() const
89+
{
90+
const uint16_t angle = data & 0x3fff;
91+
return static_cast<float>(angle) / 16383.f * 2.f * std::numbers::pi_v<float>;
92+
}
93+
94+
/// @return
95+
constexpr float
96+
getAngleDeg() const
97+
{
98+
const uint16_t angle = data & 0x3fff;
99+
return static_cast<float>(angle) / 16383.f * 360.f;
100+
}
101+
102+
/// @return
103+
constexpr uint16_t
104+
getAngleRaw() const
105+
{
106+
const uint16_t angle = data & 0x3fff;
107+
return angle;
108+
}
109+
110+
uint16_t data;
111+
};
112+
}; // struct as5047
113+
114+
/**
115+
* @tparam SpiMaster
116+
* @tparam Cs
117+
*
118+
* @author Henrik Hose
119+
* @ingroup modm_driver_as5047
120+
*/
121+
template<typename SpiMaster, typename Cs>
122+
class As5047 : public as5047, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<5>
123+
{
124+
public:
125+
using Data = as5047::Data;
126+
127+
/**
128+
* @param data pointer to buffer of the internal data of type Data
129+
*/
130+
As5047(Data &data);
131+
132+
/// Call this function once before using the device
133+
modm::ResumableResult<void>
134+
initialize();
135+
136+
/// Read the raw data from the sensor
137+
modm::ResumableResult<void>
138+
readout();
139+
140+
/// Get the data object for this sensor
141+
inline Data &
142+
getData()
143+
{
144+
return data;
145+
}
146+
147+
private:
148+
Data &data;
149+
uint8_t inBuffer[2];
150+
uint8_t outBuffer[2];
151+
};
152+
153+
} // namespace modm
154+
155+
#include "as5047_impl.hpp"
156+
157+
#endif // MODM_AS5047_HPP

0 commit comments

Comments
 (0)