altos/stm: Clean up SPI pin configuration code
authorKeith Packard <keithp@keithp.com>
Sun, 7 Oct 2012 00:56:23 +0000 (17:56 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 7 Oct 2012 00:56:23 +0000 (17:56 -0700)
Make sure none of the pin configurations are being used at startup
time. Split out the pin configuration into separate functions.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/stm/ao_spi_stm.c

index ade86a2720c024f59029e8d37f511f9fef19baf8..5b823e3e63648a136da3145cb7628b4c0538cba8 100644 (file)
@@ -24,7 +24,7 @@ struct ao_spi_stm_info {
 };
 
 static uint8_t         ao_spi_mutex[STM_NUM_SPI];
-static uint8_t         ao_spi_config[STM_NUM_SPI];
+static uint8_t         ao_spi_index[STM_NUM_SPI];
 
 static const struct ao_spi_stm_info ao_spi_stm_info[STM_NUM_SPI] = {
        {
@@ -267,97 +267,100 @@ ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index)
        ao_dma_done_transfer(miso_dma_index);
 }
 
-void
-ao_spi_get(uint8_t spi_index, uint32_t speed)
+static void
+ao_spi_disable_index(uint8_t spi_index)
 {
-       struct stm_spi  *stm_spi = ao_spi_stm_info[AO_SPI_INDEX(spi_index)].stm_spi;
-       uint8_t         config = AO_SPI_CONFIG(spi_index);
-
-       ao_mutex_get(&ao_spi_mutex[AO_SPI_INDEX(spi_index)]);
-       if (config != ao_spi_config[AO_SPI_INDEX(spi_index)]) {
-               
-               /* Disable current config
-                */
-               switch (AO_SPI_INDEX(spi_index)) {
-               case STM_SPI_INDEX(1):
-                       switch (ao_spi_config[AO_SPI_INDEX(spi_index)]) {
-                       case AO_SPI_1_CONFIG_PA5_PA6_PA7:
-                               stm_gpio_set(&stm_gpioa, 5, 0);
-                               stm_moder_set(&stm_gpioa, 5, STM_MODER_OUTPUT);
-                               stm_moder_set(&stm_gpioa, 6, STM_MODER_INPUT);
-                               stm_moder_set(&stm_gpioa, 7, STM_MODER_OUTPUT);
-                               break;
-                       case AO_SPI_1_CONFIG_PB3_PB4_PB5:
-                               stm_gpio_set(&stm_gpiob, 3, 0);
-                               stm_moder_set(&stm_gpiob, 3, STM_MODER_OUTPUT);
-                               stm_moder_set(&stm_gpiob, 4, STM_MODER_INPUT);
-                               stm_moder_set(&stm_gpiob, 5, STM_MODER_OUTPUT);
-                               break;
-                       case AO_SPI_1_CONFIG_PE13_PE14_PE15:
-                               stm_gpio_set(&stm_gpioe, 13, 0);
-                               stm_moder_set(&stm_gpioe, 13, STM_MODER_OUTPUT);
-                               stm_moder_set(&stm_gpioe, 14, STM_MODER_INPUT);
-                               stm_moder_set(&stm_gpioe, 15, STM_MODER_OUTPUT);
-                               break;
-                       }
+       /* Disable current config
+        */
+       switch (AO_SPI_INDEX(spi_index)) {
+       case STM_SPI_INDEX(1):
+               switch (spi_index) {
+               case AO_SPI_1_PA5_PA6_PA7:
+                       stm_gpio_set(&stm_gpioa, 5, 0);
+                       stm_moder_set(&stm_gpioa, 5, STM_MODER_OUTPUT);
+                       stm_moder_set(&stm_gpioa, 6, STM_MODER_INPUT);
+                       stm_moder_set(&stm_gpioa, 7, STM_MODER_OUTPUT);
+                       break;
+               case AO_SPI_1_PB3_PB4_PB5:
+                       stm_gpio_set(&stm_gpiob, 3, 0);
+                       stm_moder_set(&stm_gpiob, 3, STM_MODER_OUTPUT);
+                       stm_moder_set(&stm_gpiob, 4, STM_MODER_INPUT);
+                       stm_moder_set(&stm_gpiob, 5, STM_MODER_OUTPUT);
                        break;
-               case STM_SPI_INDEX(2):
-                       switch (ao_spi_config[AO_SPI_INDEX(spi_index)]) {
-                       case AO_SPI_2_CONFIG_PB13_PB14_PB15:
-                               stm_gpio_set(&stm_gpiob, 13, 0);
-                               stm_moder_set(&stm_gpiob, 13, STM_MODER_OUTPUT);
-                               stm_moder_set(&stm_gpiob, 14, STM_MODER_INPUT);
-                               stm_moder_set(&stm_gpiob, 15, STM_MODER_OUTPUT);
-                               break;
-                       case AO_SPI_2_CONFIG_PD1_PD3_PD4:
-                               stm_gpio_set(&stm_gpiod, 1, 0);
-                               stm_moder_set(&stm_gpiod, 1, STM_MODER_OUTPUT);
-                               stm_moder_set(&stm_gpiod, 3, STM_MODER_INPUT);
-                               stm_moder_set(&stm_gpiod, 4, STM_MODER_OUTPUT);
-                               break;
-                       }
+               case AO_SPI_1_PE13_PE14_PE15:
+                       stm_gpio_set(&stm_gpioe, 13, 0);
+                       stm_moder_set(&stm_gpioe, 13, STM_MODER_OUTPUT);
+                       stm_moder_set(&stm_gpioe, 14, STM_MODER_INPUT);
+                       stm_moder_set(&stm_gpioe, 15, STM_MODER_OUTPUT);
                        break;
                }
+               break;
+       case STM_SPI_INDEX(2):
+               switch (spi_index) {
+               case AO_SPI_2_PB13_PB14_PB15:
+                       stm_gpio_set(&stm_gpiob, 13, 0);
+                       stm_moder_set(&stm_gpiob, 13, STM_MODER_OUTPUT);
+                       stm_moder_set(&stm_gpiob, 14, STM_MODER_INPUT);
+                       stm_moder_set(&stm_gpiob, 15, STM_MODER_OUTPUT);
+                       break;
+               case AO_SPI_2_PD1_PD3_PD4:
+                       stm_gpio_set(&stm_gpiod, 1, 0);
+                       stm_moder_set(&stm_gpiod, 1, STM_MODER_OUTPUT);
+                       stm_moder_set(&stm_gpiod, 3, STM_MODER_INPUT);
+                       stm_moder_set(&stm_gpiod, 4, STM_MODER_OUTPUT);
+                       break;
+               }
+               break;
+       }
+}
 
-               /* Enable new config
-                */
-               switch (AO_SPI_INDEX(spi_index)) {
-               case 0:
-                       switch (AO_SPI_CONFIG(spi_index)) {
-                       case AO_SPI_1_CONFIG_PA5_PA6_PA7:
-                               stm_afr_set(&stm_gpioa, 5, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpioa, 6, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpioa, 7, STM_AFR_AF5);
-                               break;
-                       case AO_SPI_1_CONFIG_PB3_PB4_PB5:
-                               stm_afr_set(&stm_gpiob, 3, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpiob, 4, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpiob, 5, STM_AFR_AF5);
-                               break;
-                       case AO_SPI_1_CONFIG_PE13_PE14_PE15:
-                               stm_afr_set(&stm_gpioe, 13, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpioe, 14, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpioe, 15, STM_AFR_AF5);
-                               break;
-                       }
+static void
+ao_spi_enable_index(uint8_t spi_index)
+{
+       switch (AO_SPI_INDEX(spi_index)) {
+       case STM_SPI_INDEX(1):
+               switch (spi_index) {
+               case AO_SPI_1_PA5_PA6_PA7:
+                       stm_afr_set(&stm_gpioa, 5, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpioa, 6, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpioa, 7, STM_AFR_AF5);
+                       break;
+               case AO_SPI_1_PB3_PB4_PB5:
+                       stm_afr_set(&stm_gpiob, 3, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpiob, 4, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpiob, 5, STM_AFR_AF5);
+                       break;
+               case AO_SPI_1_PE13_PE14_PE15:
+                       stm_afr_set(&stm_gpioe, 13, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpioe, 14, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpioe, 15, STM_AFR_AF5);
                        break;
-               case 1:
-                       switch (AO_SPI_CONFIG(spi_index)) {
-                       case AO_SPI_2_CONFIG_PB13_PB14_PB15:
-                               stm_afr_set(&stm_gpiob, 13, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpiob, 14, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpiob, 15, STM_AFR_AF5);
-                               break;
-                       case AO_SPI_2_CONFIG_PD1_PD3_PD4:
-                               stm_afr_set(&stm_gpiod, 1, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpiod, 3, STM_AFR_AF5);
-                               stm_afr_set(&stm_gpiod, 4, STM_AFR_AF5);
-                               break;
-                       }
+               }
+               break;
+       case STM_SPI_INDEX(2):
+               switch (spi_index) {
+               case AO_SPI_2_PB13_PB14_PB15:
+                       stm_afr_set(&stm_gpiob, 13, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpiob, 14, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpiob, 15, STM_AFR_AF5);
+                       break;
+               case AO_SPI_2_PD1_PD3_PD4:
+                       stm_afr_set(&stm_gpiod, 1, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpiod, 3, STM_AFR_AF5);
+                       stm_afr_set(&stm_gpiod, 4, STM_AFR_AF5);
                        break;
                }
-               ao_spi_config[AO_SPI_INDEX(spi_index)] = AO_SPI_CONFIG(spi_index);
+               break;
        }
+}
+
+void
+ao_spi_get(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;
+
+       ao_mutex_get(&ao_spi_mutex[id]);
        stm_spi->cr1 = ((0 << STM_SPI_CR1_BIDIMODE) |                   /* Three wire mode */
                        (0 << STM_SPI_CR1_BIDIOE) |
                        (0 << STM_SPI_CR1_CRCEN) |                      /* CRC disabled */
@@ -372,21 +375,39 @@ ao_spi_get(uint8_t spi_index, uint32_t speed)
                        (1 << STM_SPI_CR1_MSTR) |
                        (0 << STM_SPI_CR1_CPOL) |                       /* Format 0 */
                        (0 << STM_SPI_CR1_CPHA));
+       if (spi_index != ao_spi_index[id]) {
+               
+               /* Disable old config
+                */
+               ao_spi_disable_index(ao_spi_index[id]);
+
+               /* Enable new config
+                */
+               ao_spi_enable_index(spi_index);
+               
+               /* Remember current config
+                */
+               ao_spi_index[id] = spi_index;
+       }
 }
 
 void
 ao_spi_put(uint8_t spi_index)
 {
-       struct stm_spi  *stm_spi = ao_spi_stm_info[AO_SPI_INDEX(spi_index)].stm_spi;
+       uint8_t         id = AO_SPI_INDEX(spi_index);
+       struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
 
        stm_spi->cr1 = 0;
-       ao_mutex_put(&ao_spi_mutex[AO_SPI_INDEX(spi_index)]);
+       ao_mutex_put(&ao_spi_mutex[id]);
 }
 
 static void
 ao_spi_channel_init(uint8_t spi_index)
 {
-       struct stm_spi  *stm_spi = ao_spi_stm_info[AO_SPI_INDEX(spi_index)].stm_spi;
+       uint8_t         id = AO_SPI_INDEX(spi_index);
+       struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
+
+       ao_spi_disable_index(spi_index);
 
        stm_spi->cr1 = 0;
        (void) stm_spi->sr;
@@ -412,7 +433,7 @@ ao_spi_init(void)
        stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN);
 # endif
        stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SPI1EN);
-       ao_spi_config[0] = AO_SPI_CONFIG_NONE;
+       ao_spi_index[0] = AO_SPI_CONFIG_NONE;
        ao_spi_channel_init(0);
 #endif
 
@@ -423,10 +444,8 @@ ao_spi_init(void)
 # if SPI_2_PD1_PD3_PD4
        stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
 # endif
-
        stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_SPI2EN);
-       ao_spi_config[1] = AO_SPI_CONFIG_NONE;
-
+       ao_spi_index[1] = AO_SPI_CONFIG_NONE;
        ao_spi_channel_init(1);
 #endif
 }