Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If there is an spi_init_master, there should be an spi_init_slave. #69

Open
rewolff opened this issue May 18, 2014 · 1 comment
Open

Comments

@rewolff
Copy link

rewolff commented May 18, 2014

I have added spi_init_slave.

The change looks straightforward and should work. But it's untested, will move on to testing this shortly. Hmmm.... We could remove the baud rate parameter in the _init_slave call. ok, we SHOULD remove.... Done.

I renamed the "br" parameter to "baud" because "br" confused me for a split second.

cat ../../spi_init_slave.patch
diff --git a/include/libopencm3/stm32/common/spi_common_all.h b/include/libopencm3/stm32/common/spi_common_all.h
index fa4affb..a77fcbf 100644
--- a/include/libopencm3/stm32/common/spi_common_all.h
+++ b/include/libopencm3/stm32/common/spi_common_all.h
@@ -386,7 +386,9 @@ specific memorymap.h header before including this header file.*/
 BEGIN_DECLS

 void spi_reset(uint32_t spi_peripheral);
-int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha,
+int spi_init_master(uint32_t spi, uint32_t baud, uint32_t cpol, uint32_t cpha,
+                   uint32_t dff, uint32_t lsbfirst);
+int spi_init_slave(uint32_t spi, uint32_t cpol, uint32_t cpha,
                    uint32_t dff, uint32_t lsbfirst);
 void spi_enable(uint32_t spi);
 void spi_disable(uint32_t spi);
diff --git a/lib/stm32/common/spi_common_f03.c b/lib/stm32/common/spi_common_f03.c
index 5713653..486bd48 100644
--- a/lib/stm32/common/spi_common_f03.c
+++ b/lib/stm32/common/spi_common_f03.c
@@ -89,17 +89,17 @@ spi_lsbfirst.
 @returns int. Error code.
 */

-int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha,
-                   uint32_t crcl, uint32_t lsbfirst)
+int spi_init_master_slave (uint32_t spi, uint32_t baud, uint32_t cpol, uint32_t cpha,
+                          uint32_t crcl, uint32_t lsbfirst, uint32_t master)
 {
        uint32_t reg32 = SPI_CR1(spi);

        /* Reset all bits omitting SPE, CRCEN and CRCNEXT bits. */
        reg32 &= SPI_CR1_SPE | SPI_CR1_CRCEN | SPI_CR1_CRCNEXT;

-       reg32 |= SPI_CR1_MSTR;  /* Configure SPI as master. */
+       reg32 |= master;        /* Configure SPI as master. */

-       reg32 |= br;            /* Set baud rate bits. */
+       reg32 |= baud;          /* Set baud rate bits. */
        reg32 |= cpol;          /* Set CPOL value. */
        reg32 |= cpha;          /* Set CPHA value. */
        reg32 |= crcl;          /* Set crc length (8 or 16 bits). */
@@ -112,6 +112,20 @@ int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha,
        return 0; /* TODO */
 }

+int spi_init_master(uint32_t spi, uint32_t baud, uint32_t cpol, uint32_t cpha,
+                    uint32_t dff, uint32_t lsbfirst)
+{
+       return spi_init_master_slave(spi, baud, cpol, cpha, dff, lsbfirst, SPI_CR1_MSTR);
+}
+
+
+int spi_init_slave(uint32_t spi, uint32_t cpol, uint32_t cpha,
+                   uint32_t dff, uint32_t lsbfirst)
+{
+       return spi_init_master_slave(spi, 0, cpol, cpha, dff, lsbfirst, 0); // maybe we should have a define for SLAVE
+}
+
+
 void spi_send8(uint32_t spi, uint8_t data)
 {
        /* Wait for transfer finished. */
diff --git a/lib/stm32/common/spi_common_l1f124.c b/lib/stm32/common/spi_common_l1f124.c
index 6345350..0d05364 100644
--- a/lib/stm32/common/spi_common_l1f124.c
+++ b/lib/stm32/common/spi_common_l1f124.c
@@ -89,17 +89,17 @@ spi_lsbfirst.
 @returns int. Error code.
 */

-int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha,
-                   uint32_t dff, uint32_t lsbfirst)
+int spi_init_master_slave(uint32_t spi, uint32_t baud, uint32_t cpol, uint32_t cpha,
+                         uint32_t dff, uint32_t lsbfirst, uint32_t master)
 {
        uint32_t reg32 = SPI_CR1(spi);

        /* Reset all bits omitting SPE, CRCEN and CRCNEXT bits. */
        reg32 &= SPI_CR1_SPE | SPI_CR1_CRCEN | SPI_CR1_CRCNEXT;

-       reg32 |= SPI_CR1_MSTR;  /* Configure SPI as master. */
+       reg32 |= master;        /* Configure SPI as master (or not). */

-       reg32 |= br;            /* Set baud rate bits. */
+       reg32 |= baud;          /* Set baud rate bits. */
        reg32 |= cpol;          /* Set CPOL value. */
        reg32 |= cpha;          /* Set CPHA value. */
        reg32 |= dff;           /* Set data format (8 or 16 bits). */
@@ -112,6 +112,21 @@ int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha,
        return 0; /* TODO */
 }

+
+int spi_init_master(uint32_t spi, uint32_t baud, uint32_t cpol, uint32_t cpha,
+                   uint32_t dff, uint32_t lsbfirst)
+{
+       return spi_init_master_slave(spi, baud, cpol, cpha, dff, lsbfirst, SPI_CR1_MSTR);
+}
+
+
+int spi_init_slave(uint32_t spi, uint32_t cpol, uint32_t cpha,
+                  uint32_t dff, uint32_t lsbfirst)
+{
+       return spi_init_master_slave(spi, 0, cpol, cpha, dff, lsbfirst, 0); // maybe we should have a define for SLAVE
+}
+
+
 /*---------------------------------------------------------------------------*/
 /** @brief SPI Set Data Frame Format to 8 bits

@rewolff
Copy link
Author

rewolff commented May 18, 2014

Update: Tested!
(and works. :-) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant