/*** * Demo: Waveshare 1.54' E-Paper * * This demo requires 7.1 KByte RAM * * PY32 E-Paper * PA0 ------> Reset * PA1 ------> CLK/SCK * PA4 ------> Busy * PA5 ------> DC/A0 * PA6 ------> CSN * PA7 ------> DIN/MOSI * * PA2 ------> TX * PA3 ------> RX */ #include #include "main.h" #include "py32f0xx_bsp_clock.h" #include "py32f0xx_bsp_printf.h" #include "EPD_Test.h" static void APP_GPIOConfig(void); static void APP_SPIConfig(void); int main(void) { BSP_RCC_HSI_24MConfig(); BSP_USART_Config(115200); printf("SPI Demo: Waveshare 1.54' E-Paper\r\nClock: %ld\r\n", SystemCoreClock); APP_GPIOConfig(); APP_SPIConfig(); EPD_test(); while(1); } uint8_t SPI_TxRxByte(uint8_t data) { uint8_t SPITimeout = 0xFF; /* Check the status of Transmit buffer Empty flag */ while (READ_BIT(SPI1->SR, SPI_SR_TXE) == RESET) { if (SPITimeout-- == 0) return 0; } LL_SPI_TransmitData8(SPI1, data); SPITimeout = 0xFF; while (READ_BIT(SPI1->SR, SPI_SR_RXNE) == RESET) { if (SPITimeout-- == 0) return 0; } // Read from RX buffer return LL_SPI_ReceiveData8(SPI1); } static void APP_GPIOConfig(void) { LL_GPIO_InitTypeDef GPIO_InitStruct; // PA6 CS LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_6, LL_GPIO_MODE_OUTPUT); // PA5 CE LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_5, LL_GPIO_MODE_OUTPUT); // PA0 RESET LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_0, LL_GPIO_MODE_OUTPUT); /* PA4 Busy (input) */ GPIO_InitStruct.Pin = LL_GPIO_PIN_4; GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); } /** * SPI1 Alternative Function Pins * SPI1_SCK: PA1_AF0, PA2_AF10, PA5_AF0, PA9_AF10, PB3_AF0 * SPI1_MISO: PA0_AF10, PA6_AF0, PA7_AF10, PA11_AF0, PA13_AF10, PB4_AF0 * SPI1_MOSI: PA1_AF10, PA2_AF0, PA3_AF10, PA7_AF0, PA8_AF10, PA12_AF0, PB5_AF0 * SPI1_NSS: PA4_AF0, PA10_AF10, PA15_AF0, PB0_AF0, PF1_AF10, PF3_AF10 */ static void APP_SPIConfig(void) { LL_SPI_InitTypeDef SPI_InitStruct; LL_GPIO_InitTypeDef GPIO_InitStruct; LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SPI1); // PA1 SCK GPIO_InitStruct.Pin = LL_GPIO_PIN_1; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; GPIO_InitStruct.Alternate = LL_GPIO_AF_0; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); // PA7 MOSI GPIO_InitStruct.Pin = LL_GPIO_PIN_7; GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; GPIO_InitStruct.Alternate = LL_GPIO_AF_0; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); SPI_InitStruct.TransferDirection = LL_SPI_HALF_DUPLEX_TX; SPI_InitStruct.Mode = LL_SPI_MODE_MASTER; SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT; SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW; SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE; SPI_InitStruct.NSS = LL_SPI_NSS_SOFT; SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16; SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST; LL_SPI_Init(SPI1, &SPI_InitStruct); LL_SPI_Enable(SPI1); } void APP_GPIO_WriteOutputPin(GPIO_TypeDef *GPIOx, uint32_t PinMask, uint8_t Val) { (Val == 0U)? WRITE_REG(GPIOx->BRR, PinMask) : WRITE_REG(GPIOx->BSRR, PinMask); } void APP_ErrorHandler(void) { while (1); } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t *file, uint32_t line) { while (1); } #endif /* USE_FULL_ASSERT */