altos/stmf0: Add spi format configuration options
[fw/altos] / src / stmf0 / ao_spi_stm.c
index 5e76d6c3946c31ff534a6006b76d3987d7908bbf..04827cf72c6ee92c8e46bf072690f1a472ff6dfd 100644 (file)
@@ -412,6 +412,18 @@ ao_spi_config(uint8_t spi_index, uint32_t speed)
        uint8_t         id = AO_SPI_INDEX(spi_index);
        struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
 
+       switch (id) {
+#if SPI_1_POWER_MANAGE
+       case 0:
+               stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SPI1EN);
+               break;
+#endif
+#if SPI_2_POWER_MANAGE
+       case 1:
+               stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_SPI2EN);
+               break;
+#endif
+       }
        if (spi_index != ao_spi_index[id]) {
 
                /* Disable old config
@@ -439,8 +451,8 @@ ao_spi_config(uint8_t spi_index, uint32_t speed)
                        (1 << STM_SPI_CR1_SPE) |                        /* Enable SPI unit */
                        (speed << STM_SPI_CR1_BR) |                     /* baud rate to pclk/4 */
                        (1 << STM_SPI_CR1_MSTR) |
-                       (0 << STM_SPI_CR1_CPOL) |                       /* Format 0 */
-                       (0 << STM_SPI_CR1_CPHA));
+                       (AO_SPI_CPOL(spi_index) << STM_SPI_CR1_CPOL) |  /* Format */
+                       (AO_SPI_CPHA(spi_index) << STM_SPI_CR1_CPHA));
 }
 
 uint8_t
@@ -469,6 +481,18 @@ ao_spi_put(uint8_t spi_index)
        struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
 
        stm_spi->cr1 = 0;
+       switch (id) {
+#if SPI_1_POWER_MANAGE
+       case 0:
+               stm_rcc.apb2enr &= ~(1 << STM_RCC_APB2ENR_SPI1EN);
+               break;
+#endif
+#if SPI_2_POWER_MANAGE
+       case 1:
+               stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_SPI2EN);
+               break;
+#endif
+       }
        ao_mutex_put(&ao_spi_mutex[id]);
 }