I am trying to initialize SPI in the On Connect and dis-activate it in the On Disconnect. With the code below, the code hangs in the
do
{
} while (GetBits16(SPI_CTRL_REG, SPI_INT_BIT) == 0); // polling to wait for spi transmission
of uint32_t spi_access(uint32_t dataToSend)
Below is my code for On Connect and On Disconnect:
void user_on_connection(uint8_t connection_idx, struct gapc_connection_req_ind const *param)
{
SPI_Pad_t spi_IT8951_CS_Pad;
GPIO_SetActive(IT8951_CTRL_PORT, PWR_IT8951);
present_gpio_state = true;
default_app_on_connection(connection_idx, param);
conn_idx = connection_idx;
features_timer = app_easy_timer(60, get_features);
arch_printf("Device connected\r\n");
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_CS_PIN, OUTPUT, PID_SPI_EN, true);
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_CLK_PIN, OUTPUT, PID_SPI_CLK, false);
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_DO_PIN, OUTPUT, PID_SPI_DO, false);
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_DI_PIN, INPUT, PID_SPI_DI, false);
SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
spi_IT8951_CS_Pad.port = GPIO_PORT_2;
spi_IT8951_CS_Pad.pin = GPIO_PIN_1;
spi_init(&spi_IT8951_CS_Pad, SPI_MODE_16BIT, SPI_ROLE_MASTER, SPI_CLK_IDLE_POL_LOW, SPI_PHA_MODE_0, SPI_MINT_DISABLE, SPI_XTAL_DIV_8);
}
/**
****************************************************************************************
* @brief Handles disconnection event
* @param[in] param Parameters of disconnect message
* @return void
****************************************************************************************
*/
void user_on_disconnect( struct gapc_disconnect_ind const *param )
{
default_app_on_disconnect(param);
arch_printf("Device disconnected, reason %i\r\n", (int)param->reason);
GPIO_SetInactive(IT8951_CTRL_PORT, PWR_IT8951);
present_gpio_state = false;
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_CS_PIN, INPUT, PID_GPIO, false);
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_CLK_PIN, INPUT, PID_GPIO, false);
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_DO_PIN, INPUT, PID_GPIO, false);
GPIO_ConfigurePin(SPI2_GPIO_PORT, SPI2_DI_PIN, INPUT, PID_GPIO, false);
SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}
Please let me know if I am not initializing it correctly. The reason I can't initialize it in peripheral_init is because the slave device draws power from the GPIO pins when I do not enable its power rail. So I'm waiting for a user to connect before enabling power to the slave device and then initializing the SPI port for communication. After disconnect I need to disable the port so the slave does not try to draw power from the SPI pins.
When the initialization code is placed in Peripheral_init, everything works well, so I know the rest of the code is functional.
Thank you,
DL
Hi dlo,
I am not able to find any mistake in the initialization code. Could you please clarify if you are using any sleep mode configuration? In case you are using sleep mode, when the device wakes ups from sleep, the user_on_connection() function will not be executed, so the GPIOs will not be configured. So, you should configure them again when the device wakes up or in the periph_init() function as you have already mentioned.
Thanks, PM_Dialog
Thank you very much for the tip! I got it solved by enabling/disabling a flag and leaving the initialization in periph_init.