28#include "eve_example.h"
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_01)\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);
389 SystemClock_Config();
397 MX_USART1_UART_Init();
401 printf(
"\x1b[2J\x1b[H");
409 printf(
"Starting the Bridgetek simple example.\r\n");
429void SystemClock_Config(
void)
431 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
432 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
436 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
440 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
442 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
447 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
448 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
449 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
450 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
451 RCC_OscInitStruct.PLL.PLLM = 1;
452 RCC_OscInitStruct.PLL.PLLN = 120;
453 RCC_OscInitStruct.PLL.PLLP = 2;
454 RCC_OscInitStruct.PLL.PLLQ = 5;
455 RCC_OscInitStruct.PLL.PLLR = 2;
456 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
457 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
458 RCC_OscInitStruct.PLL.PLLFRACN = 0;
459 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
466 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
467 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
468 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
469 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
470 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
471 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
472 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
473 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
474 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
475 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
477 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
488static void MX_SPI1_Init(
void)
499 hspi1.Instance = SPI1;
500 hspi1.Init.Mode = SPI_MODE_MASTER;
501 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
502 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
503 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
504 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
505 hspi1.Init.NSS = SPI_NSS_SOFT;
506 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
507 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
508 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
509 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
510 hspi1.Init.CRCPolynomial = 0x0;
511 hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
512 hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
513 hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
514 hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
515 hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
516 hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
517 hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
518 hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
519 hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
520 hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
521 if (HAL_SPI_Init(&hspi1) != HAL_OK)
536static void MX_USART1_UART_Init(
void)
546 huart1.Instance = USART1;
547 huart1.Init.BaudRate = 19200;
548 huart1.Init.WordLength = UART_WORDLENGTH_8B;
549 huart1.Init.StopBits = UART_STOPBITS_1;
550 huart1.Init.Parity = UART_PARITY_NONE;
551 huart1.Init.Mode = UART_MODE_TX_RX;
552 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
553 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
554 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
555 huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
556 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
557 if (HAL_UART_Init(&huart1) != HAL_OK)
561 if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
565 if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
569 if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
584static void MX_GPIO_Init(
void)
586 GPIO_InitTypeDef GPIO_InitStruct = {0};
592 __HAL_RCC_GPIOC_CLK_ENABLE();
593 __HAL_RCC_GPIOF_CLK_ENABLE();
594 __HAL_RCC_GPIOH_CLK_ENABLE();
595 __HAL_RCC_GPIOA_CLK_ENABLE();
596 __HAL_RCC_GPIOD_CLK_ENABLE();
597 __HAL_RCC_GPIOB_CLK_ENABLE();
600 HAL_GPIO_WritePin(EVE_PD__GPIO_Port, EVE_PD__Pin, GPIO_PIN_RESET);
603 HAL_GPIO_WritePin(EVE_CS__GPIO_Port, EVE_CS__Pin, GPIO_PIN_SET);
606 GPIO_InitStruct.Pin = EVE_INT_Pin;
607 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
608 GPIO_InitStruct.Pull = GPIO_PULLUP;
609 HAL_GPIO_Init(EVE_INT_GPIO_Port, &GPIO_InitStruct);
612 GPIO_InitStruct.Pin = STLINK_RX_Pin|STLINK_TX_Pin;
613 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
614 GPIO_InitStruct.Pull = GPIO_NOPULL;
615 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
616 GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
617 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
620 GPIO_InitStruct.Pin = EVE_PD__Pin;
621 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
622 GPIO_InitStruct.Pull = GPIO_NOPULL;
623 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
624 HAL_GPIO_Init(EVE_PD__GPIO_Port, &GPIO_InitStruct);
627 GPIO_InitStruct.Pin = EVE_CS__Pin;
628 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
629 GPIO_InitStruct.Pull = GPIO_NOPULL;
630 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
631 HAL_GPIO_Init(EVE_CS__GPIO_Port, &GPIO_InitStruct);
634 HAL_NVIC_SetPriority(EVE_INT_EXTI_IRQn, 0, 0);
635 HAL_NVIC_EnableIRQ(EVE_INT_EXTI_IRQn);
651void _Error_Handler(
const char *file,
int line)
655#ifdef REDIRECT_PRINTF
658 snprintf(buf,
sizeof(buf),
659 "Trapped in _Error_Handler(). Called from: %s, line: %d\r\n",
673#ifdef USE_FULL_ASSERT
681void assert_failed(uint8_t *file, uint32_t line)
692#ifdef REDIRECT_PRINTF
696 snprintf(buf,
sizeof(buf),
697 "assert_param failed: file %s, line %lu\r\n",
698 (
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,...
int8_t platform_calib_init(void)
Tell the Bridgetek example whether touch calibration can be stored.
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.
int8_t platform_calib_read(struct touchscreen_calibration *calib)
Read stored touch calibration values (not supported in this project).
#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...
int8_t platform_calib_write(struct touchscreen_calibration *calib)
Store touch calibration values (not supported in this project).
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 our chip-ID start-up check before Bridgetek's example; false = skip it.
: Header for main.c file. This file contains the common defines of the application.