summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/hal/spi
diff options
context:
space:
mode:
authorNicolas Schodet2013-02-24 23:43:23 +0100
committerNicolas Schodet2013-03-01 22:58:04 +0100
commit3a4bd5948939030b960c3363c04a33032d517fc7 (patch)
tree00fed9e08ae4640f0738f690e6ba735c62891802 /digital/ucoolib/ucoolib/hal/spi
parent54848f43e39e38ca31cfa4e95b93baa60e1cb20b (diff)
digital/ucoolib/ucoolib: change setup to enable/disable
Diffstat (limited to 'digital/ucoolib/ucoolib/hal/spi')
-rw-r--r--digital/ucoolib/ucoolib/hal/spi/spi_soft.cc50
-rw-r--r--digital/ucoolib/ucoolib/hal/spi/spi_soft.hh14
-rw-r--r--digital/ucoolib/ucoolib/hal/spi/test/test_spi.cc5
3 files changed, 36 insertions, 33 deletions
diff --git a/digital/ucoolib/ucoolib/hal/spi/spi_soft.cc b/digital/ucoolib/ucoolib/hal/spi/spi_soft.cc
index af5ac124..796b8fbe 100644
--- a/digital/ucoolib/ucoolib/hal/spi/spi_soft.cc
+++ b/digital/ucoolib/ucoolib/hal/spi/spi_soft.cc
@@ -28,40 +28,39 @@
namespace ucoo {
-SpiSoftMaster::SpiSoftMaster (Io &sck, Io &mosi, Io &miso, int speed,
- SpiMode mode)
- : sck_ (sck), mosi_ (mosi), miso_ (miso)
+SpiSoftMaster::SpiSoftMaster (Io &sck, Io &mosi, Io &miso)
+ : sck_ (sck), mosi_ (mosi), miso_ (miso), enabled_ (false)
{
- setup (speed, mode);
}
SpiSoftMaster::~SpiSoftMaster ()
{
- setup (0);
+ disable ();
}
void
-SpiSoftMaster::setup (int speed, SpiMode mode)
+SpiSoftMaster::enable (int speed, SpiMode mode)
{
- if (speed)
- {
- // Take pins ownership, set SCK according to CPOL.
- sck_.set (mode >= SPI_MODE_2);
- sck_.output ();
- mosi_.reset ();
- mosi_.output ();
- miso_.input ();
- // Set clock period and clock phase.
- half_period_ns_ = 1000 * 1000 * 1000 / 2 / speed;
- cpha_ = mode & 1;
- }
- else
- {
- // Release pins.
- sck_.input ();
- sck_.reset ();
- mosi_.input ();
- }
+ enabled_ = true;
+ // Take pins ownership, set SCK according to CPOL.
+ sck_.set (mode >= SPI_MODE_2);
+ sck_.output ();
+ mosi_.reset ();
+ mosi_.output ();
+ miso_.input ();
+ // Set clock period and clock phase.
+ half_period_ns_ = 1000 * 1000 * 1000 / 2 / speed;
+ cpha_ = mode & 1;
+}
+
+void
+SpiSoftMaster::disable ()
+{
+ enabled_ = false;
+ // Release pins.
+ sck_.input ();
+ sck_.reset ();
+ mosi_.input ();
}
void
@@ -74,6 +73,7 @@ SpiSoftMaster::send_and_recv (const char *tx_buf, char *rx_buf, int count)
char
SpiSoftMaster::send_and_recv (char tx)
{
+ assert (enabled_);
uint8_t i, rx = 0;
// Depending on clock phase, sample is done on first transition or second
// transition.
diff --git a/digital/ucoolib/ucoolib/hal/spi/spi_soft.hh b/digital/ucoolib/ucoolib/hal/spi/spi_soft.hh
index dcf0441d..c9f7886a 100644
--- a/digital/ucoolib/ucoolib/hal/spi/spi_soft.hh
+++ b/digital/ucoolib/ucoolib/hal/spi/spi_soft.hh
@@ -32,13 +32,14 @@ namespace ucoo {
class SpiSoftMaster : public SpiMaster
{
public:
- /// Constructor, need all SPI pins. Speed is given in Hz.
- SpiSoftMaster (Io &sck, Io &mosi, Io &miso, int speed = 0,
- SpiMode mode = SPI_MODE_0);
- /// Destructor, release SPI pins.
+ /// Constructor, need all SPI pins.
+ SpiSoftMaster (Io &sck, Io &mosi, Io &miso);
+ /// Destructor, disable.
~SpiSoftMaster ();
- /// Change settings, use speed 0 to stop.
- void setup (int speed, SpiMode mode = SPI_MODE_0);
+ /// Enable and setup.
+ void enable (int speed_hz, SpiMode mode = SPI_MODE_0);
+ /// Disable.
+ void disable ();
/// See SpiMaster::send_and_recv.
void send_and_recv (const char *tx_buf, char *rx_buf, int count);
/// Send and receive one byte.
@@ -55,6 +56,7 @@ class SpiSoftMaster : public SpiMaster
Io &sck_, &mosi_, &miso_;
int half_period_ns_;
bool cpha_;
+ bool enabled_;
};
} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/hal/spi/test/test_spi.cc b/digital/ucoolib/ucoolib/hal/spi/test/test_spi.cc
index 6e229fd3..24ad5e0a 100644
--- a/digital/ucoolib/ucoolib/hal/spi/test/test_spi.cc
+++ b/digital/ucoolib/ucoolib/hal/spi/test/test_spi.cc
@@ -43,9 +43,10 @@ main (int argc, const char **argv)
| RCC_AHB1ENR_IOPEEN);
ucoo::Gpio ss (GPIOE, 3);
ss.set ();
- gpio_mode_setup (GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3);
+ ss.output ();
ucoo::Gpio sck (GPIOA, 5), mosi (GPIOA, 7), miso (GPIOA, 6);
- ucoo::SpiSoftMaster spi (sck, mosi, miso, 1000000, ucoo::SPI_MODE_3);
+ ucoo::SpiSoftMaster spi (sck, mosi, miso);
+ spi.enable (1000000, ucoo::SPI_MODE_3);
// Loop with simple IU.
char buf[64];
unsigned int r;