From bfa7b480a7a537fc5496c8d9ffbf560f3a02314d Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Tue, 30 Sep 2014 22:26:59 +0100
Subject: [PATCH] stmhal: For spi_init, add argument to select if NSS pin is
 enabled.

Most of the time you don't use the NSS pin of the SPI bus, and so it
shouldn't be enabled by default (this gave some bugs in the past).
---
 drivers/cc3000/src/ccspi.c | 2 +-
 stmhal/lcd.c               | 2 +-
 stmhal/spi.c               | 6 +++---
 stmhal/spi.h               | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/cc3000/src/ccspi.c b/drivers/cc3000/src/ccspi.c
index 2abc637fe..81ea5628d 100644
--- a/drivers/cc3000/src/ccspi.c
+++ b/drivers/cc3000/src/ccspi.c
@@ -153,7 +153,7 @@ void SpiOpen(gcSpiHandleRx pfRxHandler)
     SPI_HANDLE->Init.TIMode            = SPI_TIMODE_DISABLED;
     SPI_HANDLE->Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLED;
     SPI_HANDLE->Init.CRCPolynomial     = 7;
-    spi_init(SPI_HANDLE);
+    spi_init(SPI_HANDLE, false);
 
     // configure wlan CS and EN pins
     GPIO_InitTypeDef GPIO_InitStructure;
diff --git a/stmhal/lcd.c b/stmhal/lcd.c
index ea7699be7..a47fffcd9 100644
--- a/stmhal/lcd.c
+++ b/stmhal/lcd.c
@@ -263,7 +263,7 @@ STATIC mp_obj_t pyb_lcd_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
     init->CRCPolynomial = 0;
 
     // init the SPI bus
-    spi_init(lcd->spi);
+    spi_init(lcd->spi, false);
 
     // set the pins to default values
     lcd->pin_cs1->gpio->BSRRL = lcd->pin_cs1->pin_mask;
diff --git a/stmhal/spi.c b/stmhal/spi.c
index 0b825ce1d..007f22daf 100644
--- a/stmhal/spi.c
+++ b/stmhal/spi.c
@@ -86,7 +86,7 @@ void spi_init0(void) {
 }
 
 // TODO allow to take a list of pins to use
-void spi_init(SPI_HandleTypeDef *spi) {
+void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
     // init the GPIO lines
     GPIO_InitTypeDef GPIO_InitStructure;
     GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
@@ -130,7 +130,7 @@ void spi_init(SPI_HandleTypeDef *spi) {
         return;
     }
 
-    for (uint i = 0; i < 4; i++) {
+    for (uint i = (enable_nss_pin ? 0 : 1); i < 4; i++) {
         GPIO_InitStructure.Pin = pins[i]->pin_mask;
         HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
     }
@@ -297,7 +297,7 @@ STATIC mp_obj_t pyb_spi_init_helper(const pyb_spi_obj_t *self, mp_uint_t n_args,
     }
 
     // init the SPI bus
-    spi_init(self->spi);
+    spi_init(self->spi, init->NSS != SPI_NSS_SOFT);
 
     return mp_const_none;
 }
diff --git a/stmhal/spi.h b/stmhal/spi.h
index 1f6e7e7b2..9f8155252 100644
--- a/stmhal/spi.h
+++ b/stmhal/spi.h
@@ -30,5 +30,5 @@ extern SPI_HandleTypeDef SPIHandle3;
 extern const mp_obj_type_t pyb_spi_type;
 
 void spi_init0(void);
-void spi_init(SPI_HandleTypeDef *spi);
+void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin);
 SPI_HandleTypeDef *spi_get_handle(mp_obj_t o);
-- 
GitLab