SPIDevice.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "SPIDevice.h"
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <linux/spi/spidev.h>
  22. #include <stdio.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <vector>
  28. #include <AP_HAL/AP_HAL.h>
  29. #include <AP_HAL/utility/OwnPtr.h>
  30. #include "GPIO.h"
  31. #include "PollerThread.h"
  32. #include "Scheduler.h"
  33. #include "Semaphores.h"
  34. #include "Thread.h"
  35. #include "Util.h"
  36. #define DEBUG 0
  37. namespace Linux {
  38. static const AP_HAL::HAL &hal = AP_HAL::get_HAL();
  39. #define MHZ (1000U*1000U)
  40. #define KHZ (1000U)
  41. #define SPI_CS_KERNEL -1
  42. struct SPIDesc {
  43. SPIDesc(const char *name_, uint16_t bus_, uint16_t subdev_, uint8_t mode_,
  44. uint8_t bits_per_word_, int16_t cs_pin_, uint32_t lowspeed_,
  45. uint32_t highspeed_)
  46. : name(name_), bus(bus_), subdev(subdev_), mode(mode_)
  47. , bits_per_word(bits_per_word_), cs_pin(cs_pin_), lowspeed(lowspeed_)
  48. , highspeed(highspeed_)
  49. {
  50. }
  51. const char *name;
  52. uint16_t bus;
  53. uint16_t subdev;
  54. uint8_t mode;
  55. uint8_t bits_per_word;
  56. int16_t cs_pin;
  57. uint32_t lowspeed;
  58. uint32_t highspeed;
  59. };
  60. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD
  61. SPIDesc SPIDeviceManager::_device[] = {
  62. // different SPI tables per board subtype
  63. SPIDesc("lsm9ds0_am", 1, 0, SPI_MODE_3, 8, BBB_P9_17, 10*MHZ,10*MHZ),
  64. SPIDesc("lsm9ds0_g", 1, 0, SPI_MODE_3, 8, BBB_P8_9, 10*MHZ,10*MHZ),
  65. SPIDesc("ms5611", 2, 0, SPI_MODE_3, 8, BBB_P9_42, 10*MHZ,10*MHZ),
  66. SPIDesc("mpu6000", 2, 0, SPI_MODE_3, 8, BBB_P9_28, 500*1000, 20*MHZ),
  67. SPIDesc("mpu9250", 2, 0, SPI_MODE_3, 8, BBB_P9_23, 1*MHZ, 11*MHZ),
  68. };
  69. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIGATOR
  70. SPIDesc SPIDeviceManager::_device[] = {
  71. SPIDesc("lsm9ds1_ag", 1, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  72. SPIDesc("lsm9ds1_m", 1, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  73. SPIDesc("bmp280", 1, 2, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ,10*MHZ),
  74. };
  75. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
  76. SPIDesc SPIDeviceManager::_device[] = {
  77. SPIDesc("mpu9250", 0, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  78. SPIDesc("ublox", 0, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 5*MHZ, 5*MHZ),
  79. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
  80. SPIDesc("lsm9ds1_m", 0, 2, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  81. #endif
  82. };
  83. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
  84. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI
  85. SPIDesc SPIDeviceManager::_device[] = {
  86. SPIDesc("mpu9250", 0, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  87. SPIDesc("ms5611", 0, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*KHZ, 10*MHZ),
  88. };
  89. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
  90. SPIDesc SPIDeviceManager::_device[] = {
  91. SPIDesc("mpu9250", 2, 0, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  92. SPIDesc("mpu9250ext", 1, 0, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  93. SPIDesc("ms5611", 2, 1, SPI_MODE_3, 8, SPI_CS_KERNEL, 10*MHZ,10*MHZ),
  94. };
  95. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
  96. SPIDesc SPIDeviceManager::_device[] = {
  97. SPIDesc("mpu9250", 2, 1, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  98. SPIDesc("bmp280", 2, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 10*MHZ,10*MHZ),
  99. };
  100. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_OCPOC_ZYNQ
  101. SPIDesc SPIDeviceManager::_device[] = {
  102. /* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
  103. SPIDesc("mpu9250", 1, 0, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  104. SPIDesc("ms5611", 1, 1, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  105. };
  106. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH
  107. SPIDesc SPIDeviceManager::_device[] = {
  108. SPIDesc("mpu9250", 0, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  109. SPIDesc("ublox", 0, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 5*MHZ, 5*MHZ),
  110. };
  111. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DARK
  112. SPIDesc SPIDeviceManager::_device[] = {
  113. SPIDesc("mpu9250", 0, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  114. };
  115. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
  116. SPIDesc SPIDeviceManager::_device[] = {
  117. SPIDesc("bebop", 1, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 320*KHZ, 320*KHZ),
  118. };
  119. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_AERO
  120. SPIDesc SPIDeviceManager::_device[] = {
  121. SPIDesc("aeroio", 1, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 10*MHZ, 10*MHZ),
  122. SPIDesc("bmi160", 3, 0, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  123. };
  124. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE
  125. SPIDesc SPIDeviceManager::_device[] = {
  126. SPIDesc("mpu60x0", 0, 1, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  127. SPIDesc("mpu60x0ext", 0, 2, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 11*MHZ),
  128. SPIDesc("ms5611", 0, 0, SPI_MODE_0, 8, SPI_CS_KERNEL, 10*MHZ,10*MHZ),
  129. };
  130. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RST_ZYNQ
  131. SPIDesc SPIDeviceManager::_device[] = {
  132. SPIDesc("rst_g", 0, 0, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  133. SPIDesc("lis3mdl", 0, 1, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  134. SPIDesc("rst_a", 0, 2, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  135. SPIDesc("ms5611", 0, 3, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
  136. };
  137. #else
  138. // empty device table
  139. SPIDesc SPIDeviceManager::_device[] = {
  140. SPIDesc("**dummy**", 0, 0, SPI_MODE_3, 0, 0, 0 * MHZ, 0 * MHZ),
  141. };
  142. #define LINUX_SPI_DEVICE_NUM_DEVICES 1
  143. #endif
  144. #ifndef LINUX_SPI_DEVICE_NUM_DEVICES
  145. #define LINUX_SPI_DEVICE_NUM_DEVICES ARRAY_SIZE(SPIDeviceManager::_device)
  146. #endif
  147. #define MAX_SUBDEVS 6
  148. const uint8_t SPIDeviceManager::_n_device_desc = LINUX_SPI_DEVICE_NUM_DEVICES;
  149. /* Private struct to maintain for each bus */
  150. class SPIBus : public TimerPollable::WrapperCb {
  151. public:
  152. SPIBus(uint16_t bus_);
  153. ~SPIBus();
  154. /*
  155. * TimerPollable::WrapperCb methods to take
  156. * and release semaphore while calling the callback
  157. */
  158. void start_cb() override;
  159. void end_cb() override;
  160. void open(uint16_t subdev);
  161. PollerThread thread;
  162. Semaphore sem;
  163. int fd[MAX_SUBDEVS];
  164. uint16_t bus;
  165. int16_t last_mode = -1;
  166. uint8_t ref;
  167. };
  168. SPIBus::SPIBus(uint16_t bus_)
  169. : bus(bus_)
  170. {
  171. memset(fd, -1, sizeof(fd));
  172. }
  173. SPIBus::~SPIBus()
  174. {
  175. for (unsigned i = 0; i < MAX_SUBDEVS; i++) {
  176. ::close(fd[i]);
  177. }
  178. }
  179. void SPIBus::start_cb()
  180. {
  181. sem.take(HAL_SEMAPHORE_BLOCK_FOREVER);
  182. }
  183. void SPIBus::end_cb()
  184. {
  185. sem.give();
  186. }
  187. void SPIBus::open(uint16_t subdev)
  188. {
  189. char path[sizeof("/dev/spidevXXXXX.XXXXX")];
  190. /* Already open by another device */
  191. if (fd[subdev] >= 0) {
  192. return;
  193. }
  194. snprintf(path, sizeof(path), "/dev/spidev%u.%u", bus, subdev);
  195. fd[subdev] = ::open(path, O_RDWR | O_CLOEXEC);
  196. if (fd[subdev] < 0) {
  197. AP_HAL::panic("SPI: unable to open SPI bus %s: %s",
  198. path, strerror(errno));
  199. }
  200. }
  201. SPIDevice::SPIDevice(SPIBus &bus, SPIDesc &device_desc)
  202. : _bus(bus)
  203. , _desc(device_desc)
  204. {
  205. set_device_bus(_bus.bus);
  206. set_device_address(_desc.subdev);
  207. _speed = _desc.highspeed;
  208. if (_desc.cs_pin != SPI_CS_KERNEL) {
  209. _cs = hal.gpio->channel(_desc.cs_pin);
  210. if (!_cs) {
  211. AP_HAL::panic("Unable to instantiate cs pin");
  212. }
  213. _cs->mode(HAL_GPIO_OUTPUT);
  214. // do not hold the SPI bus initially
  215. _cs_release();
  216. }
  217. }
  218. SPIDevice::~SPIDevice()
  219. {
  220. // Unregister itself from the SPIDeviceManager
  221. SPIDeviceManager::from(hal.spi)->_unregister(_bus);
  222. }
  223. bool SPIDevice::set_speed(AP_HAL::Device::Speed speed)
  224. {
  225. switch (speed) {
  226. case AP_HAL::Device::SPEED_HIGH:
  227. _speed = _desc.highspeed;
  228. break;
  229. case AP_HAL::Device::SPEED_LOW:
  230. _speed = _desc.lowspeed;
  231. break;
  232. }
  233. return true;
  234. }
  235. bool SPIDevice::transfer(const uint8_t *send, uint32_t send_len,
  236. uint8_t *recv, uint32_t recv_len)
  237. {
  238. struct spi_ioc_transfer msgs[2] = { };
  239. unsigned nmsgs = 0;
  240. int fd = _bus.fd[_desc.subdev];
  241. assert(fd >= 0);
  242. if (send && send_len != 0) {
  243. msgs[nmsgs].tx_buf = (uint64_t) send;
  244. msgs[nmsgs].rx_buf = 0;
  245. msgs[nmsgs].len = send_len;
  246. msgs[nmsgs].speed_hz = _speed;
  247. msgs[nmsgs].delay_usecs = 0;
  248. msgs[nmsgs].bits_per_word = _desc.bits_per_word;
  249. msgs[nmsgs].cs_change = 0;
  250. nmsgs++;
  251. }
  252. if (recv && recv_len != 0) {
  253. msgs[nmsgs].tx_buf = 0;
  254. msgs[nmsgs].rx_buf = (uint64_t) recv;
  255. msgs[nmsgs].len = recv_len;
  256. msgs[nmsgs].speed_hz = _speed;
  257. msgs[nmsgs].delay_usecs = 0;
  258. msgs[nmsgs].bits_per_word = _desc.bits_per_word;
  259. msgs[nmsgs].cs_change = 0;
  260. nmsgs++;
  261. }
  262. if (!nmsgs) {
  263. return false;
  264. }
  265. #if DEBUG
  266. if (_desc.mode == _bus.last_mode) {
  267. /*
  268. the mode in the kernel is not tied to the file descriptor,
  269. so there is a chance some other process has changed it since
  270. we last used the bus. We want to report when this happens so
  271. the user has a chance of figuring out when there is
  272. conflicted use of the SPI bus. Unfortunately this costs us
  273. an extra syscall per transfer.
  274. */
  275. uint8_t current_mode;
  276. if (ioctl(fd, SPI_IOC_RD_MODE, &current_mode) < 0) {
  277. hal.console->printf("SPIDevice: error on getting mode fd=%d (%s)\n",
  278. fd, strerror(errno));
  279. _bus.last_mode = -1;
  280. } else if (current_mode != _bus.last_mode) {
  281. hal.console->printf("SPIDevice: bus mode conflict fd=%d mode=%u/%u\n",
  282. fd, (unsigned)_bus.last_mode, (unsigned)current_mode);
  283. _bus.last_mode = -1;
  284. }
  285. }
  286. #endif
  287. int r;
  288. if (_desc.mode != _bus.last_mode) {
  289. r = ioctl(fd, SPI_IOC_WR_MODE, &_desc.mode);
  290. if (r < 0) {
  291. hal.console->printf("SPIDevice: error on setting mode fd=%d (%s)\n",
  292. fd, strerror(errno));
  293. return false;
  294. }
  295. _bus.last_mode = _desc.mode;
  296. }
  297. _cs_assert();
  298. r = ioctl(fd, SPI_IOC_MESSAGE(nmsgs), &msgs);
  299. _cs_release();
  300. if (r == -1) {
  301. hal.console->printf("SPIDevice: error transferring data fd=%d (%s)\n",
  302. fd, strerror(errno));
  303. return false;
  304. }
  305. return true;
  306. }
  307. bool SPIDevice::transfer_fullduplex(const uint8_t *send, uint8_t *recv,
  308. uint32_t len)
  309. {
  310. struct spi_ioc_transfer msgs[1] = { };
  311. int fd = _bus.fd[_desc.subdev];
  312. assert(fd >= 0);
  313. if (!send || !recv || len == 0) {
  314. return false;
  315. }
  316. msgs[0].tx_buf = (uint64_t) send;
  317. msgs[0].rx_buf = (uint64_t) recv;
  318. msgs[0].len = len;
  319. msgs[0].speed_hz = _speed;
  320. msgs[0].delay_usecs = 0;
  321. msgs[0].bits_per_word = _desc.bits_per_word;
  322. msgs[0].cs_change = 0;
  323. int r = ioctl(fd, SPI_IOC_WR_MODE, &_desc.mode);
  324. if (r < 0) {
  325. hal.console->printf("SPIDevice: error on setting mode fd=%d (%s)\n",
  326. fd, strerror(errno));
  327. return false;
  328. }
  329. _cs_assert();
  330. r = ioctl(fd, SPI_IOC_MESSAGE(1), &msgs);
  331. _cs_release();
  332. if (r == -1) {
  333. hal.console->printf("SPIDevice: error transferring data fd=%d (%s)\n",
  334. fd, strerror(errno));
  335. return false;
  336. }
  337. return true;
  338. }
  339. void SPIDevice::_cs_assert()
  340. {
  341. if (_desc.cs_pin == SPI_CS_KERNEL) {
  342. return;
  343. }
  344. _cs->write(0);
  345. }
  346. void SPIDevice::_cs_release()
  347. {
  348. if (_desc.cs_pin == SPI_CS_KERNEL) {
  349. return;
  350. }
  351. _cs->write(1);
  352. }
  353. AP_HAL::Semaphore *SPIDevice::get_semaphore()
  354. {
  355. return &_bus.sem;
  356. }
  357. AP_HAL::Device::PeriodicHandle SPIDevice::register_periodic_callback(
  358. uint32_t period_usec, AP_HAL::Device::PeriodicCb cb)
  359. {
  360. TimerPollable *p = _bus.thread.add_timer(cb, &_bus, period_usec);
  361. if (!p) {
  362. AP_HAL::panic("Could not create periodic callback");
  363. }
  364. if (!_bus.thread.is_started()) {
  365. char name[16];
  366. snprintf(name, sizeof(name), "ap-spi-%u", _bus.bus);
  367. _bus.thread.set_stack_size(AP_LINUX_SENSORS_STACK_SIZE);
  368. _bus.thread.start(name, AP_LINUX_SENSORS_SCHED_POLICY,
  369. AP_LINUX_SENSORS_SCHED_PRIO);
  370. }
  371. return static_cast<AP_HAL::Device::PeriodicHandle>(p);
  372. }
  373. bool SPIDevice::adjust_periodic_callback(
  374. AP_HAL::Device::PeriodicHandle h, uint32_t period_usec)
  375. {
  376. return _bus.thread.adjust_timer(static_cast<TimerPollable*>(h), period_usec);
  377. }
  378. AP_HAL::OwnPtr<AP_HAL::SPIDevice>
  379. SPIDeviceManager::get_device(const char *name)
  380. {
  381. SPIDesc *desc = nullptr;
  382. /* Find the bus description in the table */
  383. for (uint8_t i = 0; i < _n_device_desc; i++) {
  384. if (!strcmp(_device[i].name, name)) {
  385. desc = &_device[i];
  386. break;
  387. }
  388. }
  389. if (!desc) {
  390. return AP_HAL::OwnPtr<AP_HAL::SPIDevice>(nullptr);
  391. }
  392. /* Find if bus already exists */
  393. for (uint8_t i = 0, n = _buses.size(); i < n; i++) {
  394. if (_buses[i]->bus == desc->bus) {
  395. return _create_device(*_buses[i], *desc);
  396. }
  397. }
  398. /* Bus not found for this device, create a new one */
  399. AP_HAL::OwnPtr<SPIBus> b{new SPIBus(desc->bus)};
  400. if (!b) {
  401. return nullptr;
  402. }
  403. auto dev = _create_device(*b, *desc);
  404. if (!dev) {
  405. return nullptr;
  406. }
  407. _buses.push_back(b.leak());
  408. return dev;
  409. }
  410. uint8_t SPIDeviceManager::get_count()
  411. {
  412. return _n_device_desc;
  413. }
  414. const char* SPIDeviceManager::get_device_name(uint8_t idx)
  415. {
  416. return _device[idx].name;
  417. }
  418. /* Create a new device increasing the bus reference */
  419. AP_HAL::OwnPtr<AP_HAL::SPIDevice>
  420. SPIDeviceManager::_create_device(SPIBus &b, SPIDesc &desc) const
  421. {
  422. // Ensure bus is open
  423. b.open(desc.subdev);
  424. auto dev = AP_HAL::OwnPtr<AP_HAL::SPIDevice>(new SPIDevice(b, desc));
  425. if (!dev) {
  426. return nullptr;
  427. }
  428. b.ref++;
  429. return dev;
  430. }
  431. void SPIDeviceManager::_unregister(SPIBus &b)
  432. {
  433. if (b.ref == 0 || --b.ref > 0) {
  434. return;
  435. }
  436. for (auto it = _buses.begin(); it != _buses.end(); it++) {
  437. if ((*it)->bus == b.bus) {
  438. _buses.erase(it);
  439. delete &b;
  440. break;
  441. }
  442. }
  443. }
  444. void SPIDeviceManager::teardown()
  445. {
  446. for (auto it = _buses.begin(); it != _buses.end(); it++) {
  447. /* Try to stop thread - it may not even be started yet */
  448. (*it)->thread.stop();
  449. }
  450. for (auto it = _buses.begin(); it != _buses.end(); it++) {
  451. /* Try to join thread - failing is normal if thread was not started */
  452. (*it)->thread.join();
  453. }
  454. }
  455. }