I found out MICROPY_BEGIN_ATOMIC_SECTION() maybe the key point of this behavior.
If I remove MICROPY_BEGIN_ATOMIC_SECTION() & MICROPY_END_ATOMIC_SECTION, my code is not block anymore.
But HAL_SPI_Transmit() return HAL_TIMEOUT…
But I don’t know why it is happen. I just follow the procedure of spi.c, why does it blocking here?
Would you please point out what I am wrong or miss? Thank you.
I make 2 test and use the oscilloscope to observe its behavior.
But one of them is failed, when I send data with SPI API, it seems block.
Do you have any suggestion about how to debug in this situation or any idea about it?
Below is the detail. Thanks.
Test 1, control SPI and CS pin (with GPIO) in Python.
It works great.
Test 2, control SPI and CS pin in C.
I use HAL_GPIO_Init & HAL_GPIO_WritePin to control CS pin, it works.
And I init SPI with HAL_SPI_Init, it return HAL_OK, but when I send the data with HAL_SPI_Transmit, it seems block (Not crash, just like block in that API, OpenMV IDE show the device is busy.).
Here is my code, and I put the code in ov7725.c get_gain_db() to let me can easy call it with current API sensor.get_gain_db() … (you known, just a test…)
One more strange thing, because my log “ooxx” is not show in terminal, that is why I thought it is block in the API…
SPIHandle.Instance = SPI2;
SPIHandle.Init.Mode = SPI_MODE_MASTER;
SPIHandle.Init.Direction = SPI_DIRECTION_2LINES;
SPIHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SPIHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
SPIHandle.Init.CLKPhase = SPI_CR1_CPHA;
SPIHandle.Init.NSS = SPI_NSS_SOFT;
SPIHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
SPIHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPIHandle.Init.TIMode = SPI_TIMODE_DISABLED;
SPIHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
SPIHandle.Init.CRCPolynomial = 0;
if (HAL_SPI_Init(&SPIHandle) != HAL_OK)
/* Initialization Error */
GPIO_InitStructure.Pin = CS_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
[b]mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();[/b]
bool res = (HAL_SPI_Transmit(
&SPIHandle, data, 1, SPI_TIMEOUT) == HAL_OK);