49SPI_HandleTypeDef hspi1;
51UART_HandleTypeDef huart1;
60void SystemClock_Config(
void);
61static void MX_GPIO_Init(
void);
62static void MX_USART1_UART_Init(
void);
63static void MX_SPI1_Init(
void);
77#define EVE_REG_ID 0x302000UL
78#define EVE_REG_CPURESET 0x302020UL
79#define EVE_RAM_CHIPID 0x0C0000UL
80#define EVE_REG_ID_VALUE 0x7CU
81#define EVE_CMD_ACTIVE 0x00U
82#define EVE_CMD_CLKEXT 0x44U
83#define EVE_CMD_RST_PULSE 0x68U
84#define EVE_CMD_CLKSEL 0x61U
85#define EVE_CLKSEL_72MHZ 0x46U
86#define EVE_SPI_RETRIES 3U
87#define EVE_SPI_TIMEOUT_MS 10U
88#define EVE_BOOT_TIMEOUT_MS 300U
89#define EVE_ENGINE_TIMEOUT_MS 3000U
90#define EVE_MAX_READ_BYTES 4U
101 if ((status == HAL_BUSY) || (status == HAL_TIMEOUT))
105 if (status == HAL_ERROR)
107 uint32_t spiError = HAL_SPI_GetError(&hspi1);
108 return (spiError & (HAL_SPI_ERROR_MODF | HAL_SPI_ERROR_CRC | HAL_SPI_ERROR_OVR |
109 HAL_SPI_ERROR_FRE | HAL_SPI_ERROR_DMA | HAL_SPI_ERROR_FLAG)) != 0U;
126 HAL_StatusTypeDef status = HAL_ERROR;
130 HAL_GPIO_WritePin(EVE_CS__GPIO_Port, EVE_CS__Pin, GPIO_PIN_RESET);
131 status = HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)tx, rx, length,
EVE_SPI_TIMEOUT_MS);
132 HAL_GPIO_WritePin(EVE_CS__GPIO_Port, EVE_CS__Pin, GPIO_PIN_SET);
134 if (status == HAL_OK)
138 printf(
"EVE SPI: attempt %lu of %u failed (HAL status %d, SPI error 0x%08lX)\r\n",
140 (
unsigned long)HAL_SPI_GetError(&hspi1));
146 printf(
"EVE SPI: giving up, the transfer did not complete.\r\n");
158 uint8_t tx[3] = { command, parameter, 0x00U };
179 printf(
"eveReadMemory: bad argument (data %p, length %u)\r\n", (
void *)data, length);
183 tx[0] = (uint8_t)((address >> 16) & 0x3FU);
184 tx[1] = (uint8_t)(address >> 8);
185 tx[2] = (uint8_t)address;
188 memcpy(data, &rx[4], length);
200static bool eveWaitForRegister(uint32_t address, uint8_t expected, uint32_t timeoutMs, uint8_t *lastValue)
202 uint32_t start = HAL_GetTick();
207 if (*lastValue == expected)
212 }
while ((HAL_GetTick() - start) < timeoutMs);
223 if ((misoValue == 0x00U) || (misoValue == 0xFFU))
225 printf(
" MISO reads 0x%02X: nothing is answering on the SPI lines.\r\n", misoValue);
227 printf(
" Check the wiring against the schematic (ME817EV CN8 to the Nucleo):\r\n");
228 printf(
" CN8-1 SCK = PA5, CN8-2 CS# = PB6, CN8-3 MISO = PA6, CN8-4 MOSI = PB5,\r\n");
229 printf(
" CN8-13 PD# = PD15, CN8-11/12 GND, plus a heavy ground between the boards.\r\n");
230 printf(
" ME817EV: SW2 all OFF, JP1 on 1-2, USB-C power in CN11.\r\n");
242 static const uint8_t expectedChipId[4] = { 0x08U, 0x17U, 0x01U, 0x00U };
244 uint8_t chipId[4] = { 0U };
246 printf(
"BT817 SPI chip-ID test (H753_CPP_SPI_BT817_EVE_02)\r\n");
249 HAL_GPIO_WritePin(EVE_PD__GPIO_Port, EVE_PD__Pin, GPIO_PIN_RESET);
251 HAL_GPIO_WritePin(EVE_PD__GPIO_Port, EVE_PD__Pin, GPIO_PIN_SET);
266 printf(
"FAIL: REG_ID reads 0x%02X, expected 0x%02X within %u ms.\r\n",
272 printf(
"REG_ID = 0x%02X OK\r\n", value);
275 uint32_t engineStart = HAL_GetTick();
278 printf(
"FAIL: REG_CPURESET reads 0x%02X, expected 0x00 within %u ms"
279 " (bit0 coprocessor, bit1 touch, bit2 audio still in reset).\r\n",
284 printf(
"REG_CPURESET = 0x%02X OK (after %lu ms)\r\n", value,
285 (
unsigned long)(HAL_GetTick() - engineStart));
289 printf(
"Chip ID = %02X %02X %02X %02X\r\n", chipId[0], chipId[1], chipId[2], chipId[3]);
290 if (memcmp(chipId, expectedChipId,
sizeof(chipId)) != 0)
292 printf(
"FAIL: expected 08 17 01 00 (BT817). A BT818 reads 08 18 01 00.\r\n");
296 printf(
"PASS: BT817 found over SPI.\r\n");
300#ifdef REDIRECT_PRINTF
301#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
315 static int previousChar = 0;
316 uint8_t carriageReturn = (uint8_t)
'\r';
318 if ((ch ==
'\n') && (previousChar !=
'\r'))
320 HAL_UART_Transmit(&huart1, &carriageReturn, 1, 0xFFFF);
322 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
352 SystemClock_Config();
360 MX_USART1_UART_Init();
364 printf(
"\x1b[2J\x1b[H");
393void SystemClock_Config(
void)
395 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
396 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
400 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
404 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
406 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
411 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
412 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
413 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
414 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
415 RCC_OscInitStruct.PLL.PLLM = 1;
416 RCC_OscInitStruct.PLL.PLLN = 120;
417 RCC_OscInitStruct.PLL.PLLP = 2;
418 RCC_OscInitStruct.PLL.PLLQ = 5;
419 RCC_OscInitStruct.PLL.PLLR = 2;
420 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
421 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
422 RCC_OscInitStruct.PLL.PLLFRACN = 0;
423 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
430 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
431 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
432 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
433 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
434 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
435 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
436 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
437 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
438 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
439 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
441 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
452static void MX_SPI1_Init(
void)
463 hspi1.Instance = SPI1;
464 hspi1.Init.Mode = SPI_MODE_MASTER;
465 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
466 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
467 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
468 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
469 hspi1.Init.NSS = SPI_NSS_SOFT;
470 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
471 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
472 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
473 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
474 hspi1.Init.CRCPolynomial = 0x0;
475 hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
476 hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
477 hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
478 hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
479 hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
480 hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
481 hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
482 hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
483 hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
484 hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
485 if (HAL_SPI_Init(&hspi1) != HAL_OK)
500static void MX_USART1_UART_Init(
void)
510 huart1.Instance = USART1;
511 huart1.Init.BaudRate = 19200;
512 huart1.Init.WordLength = UART_WORDLENGTH_8B;
513 huart1.Init.StopBits = UART_STOPBITS_1;
514 huart1.Init.Parity = UART_PARITY_NONE;
515 huart1.Init.Mode = UART_MODE_TX_RX;
516 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
517 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
518 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
519 huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
520 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
521 if (HAL_UART_Init(&huart1) != HAL_OK)
525 if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
529 if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
533 if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
548static void MX_GPIO_Init(
void)
550 GPIO_InitTypeDef GPIO_InitStruct = {0};
556 __HAL_RCC_GPIOC_CLK_ENABLE();
557 __HAL_RCC_GPIOF_CLK_ENABLE();
558 __HAL_RCC_GPIOH_CLK_ENABLE();
559 __HAL_RCC_GPIOA_CLK_ENABLE();
560 __HAL_RCC_GPIOD_CLK_ENABLE();
561 __HAL_RCC_GPIOB_CLK_ENABLE();
564 HAL_GPIO_WritePin(EVE_PD__GPIO_Port, EVE_PD__Pin, GPIO_PIN_RESET);
567 HAL_GPIO_WritePin(EVE_CS__GPIO_Port, EVE_CS__Pin, GPIO_PIN_SET);
570 GPIO_InitStruct.Pin = EVE_INT_Pin;
571 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
572 GPIO_InitStruct.Pull = GPIO_PULLUP;
573 HAL_GPIO_Init(EVE_INT_GPIO_Port, &GPIO_InitStruct);
576 GPIO_InitStruct.Pin = STLINK_RX_Pin|STLINK_TX_Pin;
577 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
578 GPIO_InitStruct.Pull = GPIO_NOPULL;
579 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
580 GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
581 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
584 GPIO_InitStruct.Pin = EVE_PD__Pin;
585 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
586 GPIO_InitStruct.Pull = GPIO_NOPULL;
587 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
588 HAL_GPIO_Init(EVE_PD__GPIO_Port, &GPIO_InitStruct);
591 GPIO_InitStruct.Pin = EVE_CS__Pin;
592 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
593 GPIO_InitStruct.Pull = GPIO_NOPULL;
594 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
595 HAL_GPIO_Init(EVE_CS__GPIO_Port, &GPIO_InitStruct);
598 HAL_NVIC_SetPriority(EVE_INT_EXTI_IRQn, 0, 0);
599 HAL_NVIC_EnableIRQ(EVE_INT_EXTI_IRQn);
615void _Error_Handler(
const char *file,
int line)
619#ifdef REDIRECT_PRINTF
622 snprintf(buf,
sizeof(buf),
623 "Trapped in _Error_Handler(). Called from: %s, line: %d\r\n",
637#ifdef USE_FULL_ASSERT
645void assert_failed(uint8_t *file, uint32_t line)
656#ifdef REDIRECT_PRINTF
660 snprintf(buf,
sizeof(buf),
661 "assert_param failed: file %s, line %lu\r\n",
662 (
char *)file, (
unsigned long)line);
static void eveReadMemory(uint32_t address, uint8_t *data, uint16_t length)
Reads bytes from BT817 memory: 3 address bytes (top two bits 00 = read), one dummy byte,...
static void eveHostCommand(uint8_t command, uint8_t parameter)
Sends a 3-byte BT817 host command (command, parameter, 0x00).
static bool eveSpiIsTransient(HAL_StatusTypeDef status)
Decides whether a failed HAL SPI call is worth retrying.
#define EVE_ENGINE_TIMEOUT_MS
#define EVE_BOOT_TIMEOUT_MS
#define EVE_CMD_RST_PULSE
#define EVE_SPI_TIMEOUT_MS
#define EVE_MAX_READ_BYTES
static void evePrintNoAnswerHelp(uint8_t misoValue)
Prints the wiring checklist used when the BT817 does not answer.
int main(void)
The application entry point.
static bool eveWaitForRegister(uint32_t address, uint8_t expected, uint32_t timeoutMs, uint8_t *lastValue)
Polls an 8-bit BT817 register until it holds the expected value or a timeout passes.
static void eveSpiTransaction(const uint8_t *tx, uint8_t *rx, uint16_t length)
Runs one complete SPI transaction with the BT817 (CS low, transfer, CS high). Retries the whole trans...
static void eveChipIdTest(void)
BT817 bring-up test: resets the chip on PD#, starts its clock, waits for boot, then checks REG_ID (0x...
static const bool runChipIdTest
true = run the chip-ID start-up check before the widget tour; false = skip it.
: Header for main.c file. This file contains the common defines of the application.