Skip to content

Commit

Permalink
Set GPIO to reset state when deinit SPI, I2C and UART.
Browse files Browse the repository at this point in the history
  • Loading branch information
chwon64 committed Mar 27, 2019
1 parent 373996b commit 44be266
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cores/arduino/stm32/spi_com.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,43 @@ void spi_deinit(spi_t *obj)
return;

SPI_HandleTypeDef *handle = &(obj->handle);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_TypeDef *port;

HAL_SPI_DeInit(handle);

if(obj->pin_mosi != NC) {
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_mosi));
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_mosi);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

if(obj->pin_miso != NC) {
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_miso));
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_miso);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

if(obj->pin_sclk != NC) {
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_sclk));
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_sclk);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK));
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

if(obj->pin_ssel != NC) {
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_ssel));
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_ssel);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

#if defined SPI1_BASE
// Reset SPI and disable clock
if (handle->Instance == SPI1) {
Expand Down
17 changes: 17 additions & 0 deletions cores/arduino/stm32/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,29 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
*/
void i2c_deinit(i2c_t *obj)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_TypeDef *port;

HAL_NVIC_DisableIRQ(obj->irq);
#if !defined(STM32F0xx) && !defined(STM32L0xx)
HAL_NVIC_DisableIRQ(obj->irqER);
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
HAL_I2C_DeInit(&(obj->handle));

//SCL
port = set_GPIO_Port_Clock(STM_PORT(obj->scl));
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->scl);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->scl,PinMap_I2C_SCL));
HAL_GPIO_Init(port, &GPIO_InitStruct);

//SDA
port = set_GPIO_Port_Clock(STM_PORT(obj->sda));
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->sda);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->sda,PinMap_I2C_SDA));
HAL_GPIO_Init(port, &GPIO_InitStruct);

#if defined I2C1_BASE
// Disable I2C1 clock if not done
if (obj->i2c == I2C1) {
Expand Down
18 changes: 18 additions & 0 deletions cores/arduino/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ void uart_init(serial_t *obj)
*/
void uart_deinit(serial_t *obj)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_TypeDef *port;
uint32_t function;

// Reset UART and disable clock
switch (obj->index) {
#if defined(USART1_BASE)
Expand Down Expand Up @@ -400,6 +404,20 @@ void uart_deinit(serial_t *obj)
#endif
}

port = set_GPIO_Port_Clock(STM_PORT(obj->pin_rx));
function = pinmap_function(obj->pin_rx, PinMap_UART_RX);
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_rx);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(function);
HAL_GPIO_Init(port, &GPIO_InitStruct);

port = set_GPIO_Port_Clock(STM_PORT(obj->pin_tx));
function = pinmap_function(obj->pin_tx, PinMap_UART_TX);
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_tx);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = STM_PIN_PUPD(function);
HAL_GPIO_Init(port, &GPIO_InitStruct);

HAL_UART_DeInit(uart_handlers[obj->index]);
}

Expand Down

0 comments on commit 44be266

Please sign in to comment.