2026-01-17 12:26 PM - edited 2026-01-17 1:04 PM
Hi!
I want my ESP32F411CE Blackpill to operate as an SPI slave device. The Master is not my creation, it is an already existing hardware, and my task is to emulate some Slave.
Well, almost SPI. Here is what happening: Master sets CS line high, and reads 3 byte from the Slave on MISO line. Then it sets CS line low, and sends one byte on MOSI line. CLK frequency is 1MHz, SPI Mode2.
I know it is not a proper SPI communication, so there is a transistor added that inverts the CS line. So the MCU doesn't see this in the image above, but the CS line inverted. It is a win-win, because it make the second short clock signal skipped, and that is exactly my intention.
I don't care what Master sends, I only want to send that 3 bytes. This is all I want to do!
For the first step I created a code, that sends the same 3 bytes, so I can see whan is happening on the oscilloscope / logic analyzer. These three bytes are:
volatile uint8_t buttonsOutSPI[3] = { 0xAA, 0xAA, 0xAA };
OR
volatile uint8_t buttonsOutSPI[3] = { 0xAA, 0xAA, 0xF0 };
(I tried both)
It doesn't work.
- It seems it cannot generate a proper 0xAA, it almost seems like noise spikes from clock
- It doesn't send the first byte. The second byte is the first, then it sends the third byte two times
- The second byte is one bit shifted to the left, the third byte is two bits shifted to the left
Images: AA AA AA is sent; AA AA F0, AA AA F0, AA AA F0 again, but I checked the other clock signal, when CS line is HIGH. (from the point of view of the Blackpill)
My config:
Code:
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
/* USER CODE BEGIN PV */
volatile uint8_t buttonsOutSPI[3] = { 0xAA, 0xAA, 0xF0 };
uint8_t rxDummy[3];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
if (hspi->Instance == SPI1)
{
// Immediately prepare next 3 bytes for the next master transaction
HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*)buttonsOutSPI, rxDummy, 3);
}
}
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
{
if (hspi->Instance == SPI1)
{
// Clear and re-arm (simple recovery)
__HAL_SPI_DISABLE(&hspi1);
// Clear OVR if it happened: read DR then SR
volatile uint32_t tmp;
tmp = hspi1.Instance->DR;
tmp = hspi1.Instance->SR;
(void)tmp;
__HAL_SPI_ENABLE(&hspi1);
HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*)buttonsOutSPI, rxDummy, 3);
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
/* USER CODE BEGIN 2 */
HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*)buttonsOutSPI, rxDummy, 3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 12;
RCC_OscInitStruct.PLL.PLLN = 96;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief SPI1 Initialization Function
* None
* @retval None
*/
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}What is wrong here? Can it even handle 1MHz SPI as slave?
2026-01-17 12:46 PM
This isn't SPI. A slave doesn't respond when NSS is high and the MISO line is released (high impedance).
You'll have to set the slave to be always active (software NSS) and track the NSS line yourself.
Depending on the delay between and edge and the data, this may be difficult.
The second data the master sends has 9 negative-going pulses, not 8, which is going to complicate things even more. Seems like a bad implementation of a protocol.
2026-01-17 12:57 PM - edited 2026-01-17 1:18 PM
Sorry, my bad. I forgot to mention there is a transitor added to the PCB, that inverts the NSS line. I checked it with the scope, and the NSS line is proper: It goes LOW quickly, before the first clock signal from the 3 bytes package, it stays low, and then goes HIGH after it. So take the image from the logic analyzer I posted, and invert the NSS line in your head. That is what MCU actually gets.
Other than that, I don't care what Master sends, it doesn't matter. I only want to send my 3 bytes, that is all. So the CS/NSS line if perfect for my goals, after it is inverted.