From 4ecc7f948bf1a3f8702c02a3450927af239c121e Mon Sep 17 00:00:00 2001 From: klassents Date: Mon, 9 Sep 2024 10:31:33 +0700 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=B2=20?= =?UTF-8?q?spi.begin,=20spi.end.=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/SPI/src/SPI.cpp | 39 +++++----------------- libraries/SPI/src/utility/spiElbear.cpp | 44 ++++++++++++++----------- libraries/SPI/src/utility/spiElbear.h | 3 +- 3 files changed, 35 insertions(+), 51 deletions(-) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 3885866..4166d74 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -62,7 +62,8 @@ uint8_t SPIClass::interruptMask = 0; void SPIClass::begin() { - spiElbear_prepareBegin(); + spi_onBegin(); + spiInUse = true; } @@ -73,36 +74,14 @@ void SPIClass::end() // turn off spi HAL_SPI_Disable(&hspi); - // // deinit spi gpio pins - // if (hspi.Instance == SPI_1) // only spi1 is using in currunt version - // { - // HAL_GPIO_PinConfig(GPIO_1, (HAL_PinsTypeDef)(GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2), - // HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); - // } + // deinit spi gpio pins + if (hspi.Instance == SPI_1) // only spi1 is using in currunt version + { + HAL_GPIO_PinConfig(GPIO_1, (HAL_PinsTypeDef)(GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2), + HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); + } - // // return D10 with common gpio config to pin 1.3 - // PAD_CONFIG->PORT_1_CFG &= (~(0b11<<(2*PIN_1_3_GPIO_S))); // set config to 0 - common gpio mode - - // // pin direction - // uint8_t direction = ((GPIO_1->DIRECTION_IN) & (1<> PIN_1_4_GPIO_S; - // if (direction == 1) // input - // GPIO_1->DIRECTION_IN |= (1<DIRECTION_OUT |= (1<PORT_1_PUPD) & (0b11<<(2*PIN_1_4_GPIO_S))) >> (2*PIN_1_4_GPIO_S); - // PAD_CONFIG ->PORT_1_PUPD &= (~(0b11<<(2*PIN_1_3_GPIO_S))); // clear - // PAD_CONFIG ->PORT_1_PUPD |= ((pupd_1_4&0b11)<<(2*PIN_1_3_GPIO_S)); // set new - - // // current state - // uint8_t state1_4 = ((GPIO_1->OUTPUT_) & (1<> PIN_1_4_GPIO_S; - // GPIO_1->OUTPUT_ &= (~(0b1<OUTPUT_ |= ((state1_4&0b1)<PORT_1_PUPD >> (pinShift<<1)) & 0b11) #define PORT1_GET_GPIO_STATE(pinShift) ((GPIO_1->OUTPUT_ >> pinShift) & 0b1) -void spiElbear_prepareBegin(void) +void spi_onBegin(void) { // there is a seller on pin 1.6 which replace D10 from spi NSS pin 1.3 to pin 1.4, // because spi needs pin 1.3 for correct work - + // replace config from 1.3 to 1.4 uint8_t config = PORT1_GET_PAD_CONFIG(PIN_3_SHIFT); if (config == 0) // common gpio { - // pin direction - HAL_GPIO_ModeTypeDef pin3_direction = HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_3); - // pull up/down - HAL_GPIO_PullTypeDef pin3_pupd = (HAL_GPIO_PullTypeDef)PORT1_GET_PAD_PUPD(PIN_3_SHIFT); - //current state - uint8_t pin3_state = PORT1_GET_GPIO_STATE(PIN_3_SHIFT); - - // init and set state - HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_4, pin3_direction, pin3_pupd, HAL_GPIO_DS_2MA); - HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, pin3_state); - // todo ПРОВЕРИТЬ, что по факту устанавливается то, что я говорю в HAL_GPIO_PinConfig - + // get info from pin gpio1.3 and set config to gpio1.4 + HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_4, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_3), + (HAL_GPIO_PullTypeDef)PORT1_GET_PAD_PUPD(PIN_3_SHIFT), HAL_GPIO_DS_2MA); + HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, (GPIO_PinState)PORT1_GET_GPIO_STATE(PIN_3_SHIFT)); + + // pin D10 was switched to different gpio and can be used further } else if(config == 2) // timer for pwm { - // вместо это возвращать что-то, что покажет, что это не gpio и снаружи выбросит предупреждение это - - // // if D10 (spi NSS pin) was used as pwm, we need to stop timer, - // // because 1.4 don't support timer - // analogWriteStop(10); - // ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running"); + // if D10 (spi NSS pin) was used as pwm, we need to stop timer, + // because 1.4 don't support timer + analogWriteStop(10); + ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running"); } // switch seller to pin 1.4 HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_6, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_HIGH); blockSpiPin(); +} + +void spi_onEnd(void) +{ + // get info from pin gpio1.4 and set config to gpio1.3 + HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_3, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_4), + (HAL_GPIO_PullTypeDef)PORT1_GET_PAD_PUPD(PIN_4_SHIFT), HAL_GPIO_DS_2MA); + HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_3, (GPIO_PinState)PORT1_GET_GPIO_STATE(PIN_4_SHIFT)); + + // switch seller back to pin 1.3 + HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_LOW); + unblockSpiPin(); } \ No newline at end of file diff --git a/libraries/SPI/src/utility/spiElbear.h b/libraries/SPI/src/utility/spiElbear.h index 8367111..53a286b 100644 --- a/libraries/SPI/src/utility/spiElbear.h +++ b/libraries/SPI/src/utility/spiElbear.h @@ -1,2 +1,3 @@ -void spiElbear_prepareBegin(void); \ No newline at end of file +void spi_onBegin(void); +void spi_onEnd(void); \ No newline at end of file