// // Created by wuqiyang on 24-8-18. // #include "u8g2_stm32_spi.h" u8g2_t u8g2; #include "u8g2.h" uint8_t u8g2_byte_sw_spi_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { switch (msg) { case U8X8_MSG_BYTE_SEND: /*通过SPI发送arg_int个字节数据*/ // HAL_SPI_Transmit_DMA(&hspi1, (uint8_t *)arg_ptr, arg_int);while(hspi1.TxXferCount); /*配置了DMA取消上一行注释即可*/ HAL_SPI_Transmit(&hspi2, (uint8_t *) arg_ptr, arg_int, 200); /*这是CubeMX生成的初始化*/ break; case U8X8_MSG_BYTE_INIT: /*初始化函数*/ break; case U8X8_MSG_BYTE_SET_DC: /*设AS引脚,表明发送的是数据还是命令*/ HAL_GPIO_WritePin(SPI2_RS_GPIO_Port, SPI2_RS_Pin, (GPIO_PinState) arg_int); break; case U8X8_MSG_BYTE_START_TRANSFER: u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level); u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL); break; case U8X8_MSG_BYTE_END_TRANSFER: u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL); u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level); break; default: return 0; } return 1; } uint8_t u8x8_gpio_and_delay_stm32(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr) { switch (msg) { case U8X8_MSG_GPIO_AND_DELAY_INIT: /*delay和GPIO的初始化,在main中已经初始化完成了*/ break; case U8X8_MSG_DELAY_MILLI: /*延时函数*/ HAL_Delay(arg_int); //调用谁stm32系统延时函数 break; case U8X8_MSG_GPIO_CS: /*片选信号*/ //由于只有一个SPI设备,所以片选信号在初始化时已经设置为常有效 HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, (GPIO_PinState) arg_int); break; case U8X8_MSG_GPIO_DC: /*设置RS引脚,表明发送的是数据还是命令*/ HAL_GPIO_WritePin(SPI2_RS_GPIO_Port, SPI2_RS_Pin, (GPIO_PinState) arg_int); break; case U8X8_MSG_GPIO_RESET: HAL_GPIO_WritePin(SPI2_RST_GPIO_Port, SPI2_RST_Pin, (GPIO_PinState) arg_int); break; } return 1; } /*官方logo的Demo*/ void drawDemo() { u8g2_SetFontMode(&u8g2, 1); /*字体模式选择*/ u8g2_SetFontDirection(&u8g2, 0); /*字体方向选择*/ u8g2_SetFont(&u8g2, u8g2_font_inb24_mf); /*字库选择*/ u8g2_DrawStr(&u8g2, 0, 20, "U"); u8g2_SetFontDirection(&u8g2, 1); u8g2_SetFont(&u8g2, u8g2_font_inb30_mn); u8g2_DrawStr(&u8g2, 21, 8, "8"); u8g2_SetFontDirection(&u8g2, 0); u8g2_SetFont(&u8g2, u8g2_font_inb24_mf); u8g2_DrawStr(&u8g2, 51, 30, "g"); u8g2_DrawStr(&u8g2, 67, 30, "\xb2"); u8g2_DrawHLine(&u8g2, 2, 35, 47); u8g2_DrawHLine(&u8g2, 3, 36, 47); u8g2_DrawVLine(&u8g2, 45, 32, 12); u8g2_DrawVLine(&u8g2, 46, 33, 12); u8g2_SetFont(&u8g2, u8g2_font_4x6_tr); u8g2_DrawStr(&u8g2, 1, 54, "github.com/olikraus/u8g2"); }