libopencm3 / libopencm3-examples

Simple example projects showing how to use libopencm3.
494 stars 326 forks source link

spi_init_master should be more generic. #68

Open rewolff opened 10 years ago

rewolff commented 10 years ago

I manged to issue a "pull request" for the libopencm3. But somehow my git is now messed up, so I'm getting push/pull/merge errors when I try to clean up the patch. So... here is the patch:

diff --git a/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c b/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c
index dd84f80..17765c5 100644
--- a/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c
+++ b/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c
@@ -345,11 +345,11 @@ int main(void)
        /* Force to SPI mode. This should be default after reset! */
        SPI2_I2SCFGR = 0;
        spi_init_master(SPI2,
-                       SPI_CR1_BAUDRATE_FPCLK_DIV_256,
-                       SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
-                       SPI_CR1_CPHA_CLK_TRANSITION_2,
-                       SPI_CR1_DFF_8BIT,
-                       SPI_CR1_MSBFIRST);
+                       SPI_BAUDRATE_CLK_DIV_256,
+                       SPI_CLK_TO_1_WHEN_IDLE,
+                       SPI_LATCH_RISING,
+                       SPI_FORMAT_8BIT,
+                       SPI_MSBFIRST);
        /* Ignore the stupid NSS pin. */
        spi_enable_software_slave_management(SPI2);
        spi_set_nss_high(SPI2);
diff --git a/examples/stm32/f1/lisa-m-2/spi/spi.c b/examples/stm32/f1/lisa-m-2/spi/spi.c
index f94aaeb..b1f0c0a 100644
--- a/examples/stm32/f1/lisa-m-2/spi/spi.c
+++ b/examples/stm32/f1/lisa-m-2/spi/spi.c
@@ -68,8 +68,8 @@ static void spi_setup(void) {
    * Data frame format: 8-bit
    * Frame format: MSB First
    */
-  spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
-                  SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
+  spi_init_master(SPI1, SPI_BAUDRATE_CLK_DIV_64, SPI_CLK_TO_1_WHEN_IDLE,
+                  SPI_LATCH_RISING, SPI_FORMAT_8BIT, SPI_MSBFIRST);

   /*
    * Set NSS management to software.
diff --git a/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c b/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c
index bf948ba..b460244 100644
--- a/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c
+++ b/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c
@@ -97,12 +97,12 @@ static void spi_setup(void) {
         * Frame format: MSB First
         */
 #if USE_16BIT_TRANSFERS
-       spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
-                       SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST);
+#define MY_FORMAT SPI_FORMAT_16BIT
 #else
-       spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
-                       SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
+#define MY_FORMAT SPI_FORMAT_8BIT
 #endif
+       spi_init_master(SPI1, SPI_BAUDRATE_CLK_DIV_64, SPI_CLK_TO_1_WHEN_IDLE,
+                       SPI_LATCH_RISING, MY_FORMAT, SPI_MSBFIRST);

        /*
         * Set NSS management to software.
diff --git a/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c b/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c
index 2182865..5947fb6 100644
--- a/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c
+++ b/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c
@@ -107,13 +107,14 @@ static void spi_setup(void) {
         * Frame format: MSB First
         */
 #if USE_16BIT_TRANSFERS
-       spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
-                       SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST);
-#else
-       spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
-                       SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
+#define MY_BIT_FORMAT SPI_FORMAT_16BIT
+#else 
+#define MY_BIT_FORMAT SPI_FORMAT_8BIT
 #endif

+       spi_init_master(SPI1, SPI_BAUDRATE_CLK_DIV_64, SPI_CLK_TO_1_WHEN_IDLE,
+                       SPI_LATCH_RISING, MY_BIT_FORMAT, SPI_MSBFIRST);
+
        /*
         * Set NSS management to software.
         *
diff --git a/examples/stm32/f2/jobygps/spi_test/spi_test.c b/examples/stm32/f2/jobygps/spi_test/spi_test.c
index bb570c2..10d6f65 100644
--- a/examples/stm32/f2/jobygps/spi_test/spi_test.c
+++ b/examples/stm32/f2/jobygps/spi_test/spi_test.c
@@ -45,8 +45,8 @@ static void spi_setup(void)
        gpio_set_af(GPIOB, GPIO_AF5, GPIO13 | GPIO14 | GPIO15);

        /* Setup SPI parameters. */
-       spi_init_master(SPI2, SPI_CR1_BAUDRATE_FPCLK_DIV_256, SPI_CR1_CPOL,
-                       SPI_CR1_CPHA, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
+       spi_init_master(SPI2, SPI_BAUDRATE_CLK_DIV_256, SPI_CLK_TO_1_WHEN_IDLE,
+                       SPI_LATCH_RISING, SPI_FORMAT_8BIT, SPI_MSBFIRST);
        spi_enable_ss_output(SPI2); /* Required, see NSS, 25.3.1 section. */

        /* Finally enable the SPI. */
rewolff commented 10 years ago

This depends on the defines I added to libopencm3.