-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from spark/m-mcgowan/spibus_arbiter
Use Mat McGowan's SPI bus arbitration code
- Loading branch information
Showing
13 changed files
with
149 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* File: spi_bus.h | ||
* Author: mat | ||
* | ||
* Created on 30 June 2014, 14:25 | ||
*/ | ||
|
||
#ifndef SPI_BUS_H | ||
#define SPI_BUS_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
void reset_bus(); | ||
|
||
int try_acquire_spi_bus(int owner); | ||
|
||
void acquire_spi_bus(int owner); | ||
|
||
void release_spi_bus(int owner); | ||
|
||
int current_bus_owner(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* SPI_BUS_H */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdint.h> | ||
#include <stdatomic.h> | ||
#include "spi_bus.h" | ||
#include "debug.h" | ||
#include "hw_config.h" | ||
|
||
#ifndef SPI_BUS_ARBITER | ||
#define SPI_BUS_ARBITER 1 | ||
#endif | ||
|
||
volatile int spi_bus_lock = 0; | ||
|
||
void reset_bus() { spi_bus_lock = 0; } | ||
|
||
int try_acquire_spi_bus(int owner) { | ||
#if SPI_BUS_ARBITER | ||
__sync_synchronize(); | ||
return spi_bus_lock==owner || __sync_bool_compare_and_swap(&spi_bus_lock, 0, owner); | ||
#else | ||
return 1; | ||
#endif | ||
} | ||
|
||
void acquire_spi_bus(int owner) { | ||
#if SPI_BUS_ARBITER | ||
while (!try_acquire_spi_bus(owner)); | ||
#endif | ||
} | ||
|
||
int try_release_spi_bus(int owner) { | ||
#if SPI_BUS_ARBITER | ||
__sync_synchronize(); | ||
return spi_bus_lock==0 || __sync_bool_compare_and_swap(&spi_bus_lock, owner, 0); | ||
#else | ||
return 1; | ||
#endif | ||
} | ||
|
||
void release_spi_bus(int owner) { | ||
#if SPI_BUS_ARBITER | ||
while (!try_release_spi_bus(owner)); | ||
#endif | ||
} | ||
|
||
int current_bus_owner() { return spi_bus_lock; } |