Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add img2code #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/badapple/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
Drivers
startup*.s
*.svd
*.ld
77 changes: 77 additions & 0 deletions examples/badapple/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Tools
CC=arm-none-eabi-gcc
LD=arm-none-eabi-gcc

# Code Paths
BUILD_DIR = build
SSD1306_DIR = ../../ssd1306
LDSCRIPT = STM32F411CEUx_FLASH.ld
STARTUP = startup_stm32f411xe.s

# SSD1306 flags
SSD1306_MCU = STM32F4
SSD1306_I2C = hi2c1
SSD1306_SPI = hspi1
SSD1306_FLAGS= -D${SSD1306_MCU}
SSD1306_FLAGS+= -DSSD1306_I2C_PORT=${SSD1306_I2C}
SSD1306_FLAGS+= -DSSD1306_SPI_PORT=${SSD1306_SPI}

# Sources
SRCS = \
${STARTUP} \
src/main.c \
src/badapple.c \
src/system_stm32f4xx.c \
${SSD1306_DIR}/ssd1306.c
SRCS+=$(wildcard Drivers/STM32F4xx_HAL_Driver/Src/*.c)

# Objects
OBJS=$(patsubst %.c,%.o,$(patsubst %.s,%.o,$(SRCS)))
OBJECTS=$(addprefix $(BUILD_DIR)/,$(notdir $(OBJS)))
vpath %.c $(sort $(dir $(SRCS)))

# CPU
CPU = -mcpu=cortex-m4
FPU =
FLOAT-ABI =
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# CMSIS
DEVICE = Drivers/CMSIS/Device/ST/STM32F4xx/Include
CORE = Drivers/CMSIS/Include

# HAL
HAL = Drivers/STM32F4xx_HAL_Driver/Inc

# Compilation Flags
LDFLAGS= -T${LDSCRIPT} ${MCU} --specs=nosys.specs -flto -Wl,--gc-sections -Wl,--print-memory-usage
CFLAGS= ${MCU} --specs=nosys.specs -fdata-sections -ffunction-sections -Og -g -gdwarf-2
CFLAGS+= -I$(DEVICE) -I$(CORE) -I${HAL} -I$(SSD1306_DIR) -Ibuild -Isrc
CFLAGS+= -DUSE_HAL_DRIVER -DSTM32F411xE
CFLAGS+= $(SSD1306_FLAGS)

# Build executable

.PHONY: i2c
i2c: CFLAGS+= -DSSD1306_USE_I2C
i2c: $(BUILD_DIR)/i2c.elf

.PHONY: spi
spi: CFLAGS+= -DSSD1306_USE_SPI
spi: $(BUILD_DIR)/spi.elf

$(BUILD_DIR)/ssd1306_conf.h: $(BUILD_DIR)
touch $(BUILD_DIR)/ssd1306_conf.h

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/%.elf: $(BUILD_DIR)/ssd1306_conf.h $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@

$(BUILD_DIR):
mkdir $@

36 changes: 36 additions & 0 deletions examples/badapple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Bad Apple!! Demo

Touhou - Bad Apple!! ~3FPS animation demo.

Tested with BlackPill STM32F411CEU6 and CH1115 128X64.

## Requirements

- MCU with at least 512 KB Flash memory
- 128x64 OLED display
- ARM GNU Toolchain
- make
- STM32F4 HAL
- Linker script and startup assembler

## Build

Feel free to edit `Makefile` for your configuration
(I2C/SPI port, resolution, etc.).

**I2C:**

```bash
make i2c
```

**SPI:**

```bash
make spi
```

## Disclaimer

The rights to the graphic materials used belong to
[Alstroemeria Records](https://youtu.be/i41KoE0iMYU?si=yKNadXjtwOtBxE6J).
43,700 changes: 43,700 additions & 0 deletions examples/badapple/src/badapple.c

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions examples/badapple/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include "ssd1306.h"
#include "stm32f4xx_hal.h"

extern const SSD1306_animation_t badapple;

#ifdef SSD1306_USE_I2C

I2C_HandleTypeDef SSD1306_I2C_PORT;

static void I2C_Init(void) {
SSD1306_I2C_PORT.Instance = I2C1;
SSD1306_I2C_PORT.Init.ClockSpeed = 400000;
SSD1306_I2C_PORT.Init.DutyCycle = I2C_DUTYCYCLE_2;
SSD1306_I2C_PORT.Init.OwnAddress1 = 0;
SSD1306_I2C_PORT.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
SSD1306_I2C_PORT.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
SSD1306_I2C_PORT.Init.OwnAddress2 = 0;
SSD1306_I2C_PORT.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
SSD1306_I2C_PORT.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&SSD1306_I2C_PORT);
}

void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) {
GPIO_InitTypeDef GPIO_InitStruct;
if (hi2c->Instance == I2C1) {
/* Peripheral clock enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_I2C1_CLK_ENABLE();

/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}

#elif defined(SSD1306_USE_SPI)

SPI_HandleTypeDef SSD1306_SPI_PORT;

static void SPI_Init(void) {
/* SPI1 parameter configuration*/
SSD1306_SPI_PORT.Instance = SPI1;
SSD1306_SPI_PORT.Init.Mode = SPI_MODE_MASTER;
SSD1306_SPI_PORT.Init.Direction = SPI_DIRECTION_2LINES;
SSD1306_SPI_PORT.Init.DataSize = SPI_DATASIZE_8BIT;
SSD1306_SPI_PORT.Init.CLKPolarity = SPI_POLARITY_LOW;
SSD1306_SPI_PORT.Init.CLKPhase = SPI_PHASE_1EDGE;
SSD1306_SPI_PORT.Init.NSS = SPI_NSS_SOFT;
SSD1306_SPI_PORT.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
SSD1306_SPI_PORT.Init.FirstBit = SPI_FIRSTBIT_MSB;
SSD1306_SPI_PORT.Init.TIMode = SPI_TIMODE_DISABLE;
SSD1306_SPI_PORT.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SSD1306_SPI_PORT.Init.CRCPolynomial = 10;
HAL_SPI_Init(&SSD1306_SPI_PORT);
}

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
GPIO_InitTypeDef GPIO_InitStruct;

__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

HAL_GPIO_WritePin(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SSD1306_Reset_Port, SSD1306_Reset_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin : SSD1306_CS_Pin */
GPIO_InitStruct.Pin = SSD1306_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(SSD1306_CS_Port, &GPIO_InitStruct);

/*Configure GPIO pin : SSD1306_DC_Pin */
GPIO_InitStruct.Pin = SSD1306_DC_Pin;
HAL_GPIO_Init(SSD1306_DC_Port, &GPIO_InitStruct);

/*Configure GPIO pin : SSD1306_Reset_Pin */
GPIO_InitStruct.Pin = SSD1306_Reset_Pin;
HAL_GPIO_Init(SSD1306_Reset_Port, &GPIO_InitStruct);

if (hspi->Instance == SPI1) {
/* Peripheral clock enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_SPI1_CLK_ENABLE();

/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA7 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
#endif // SSD1306_USE_I2C

int main(void) {
HAL_Init();

/* Initialize peripherals */
#ifdef SSD1306_USE_I2C
I2C_Init();
#elif defined(SSD1306_USE_SPI)
SPI_Init();
#endif // SSD1306_USE_I2C
ssd1306_Init();
uint32_t tick;
const uint32_t frametime = 306;
while (1) {
tick = HAL_GetTick();
for (uint32_t i = 0; i < badapple.frames; i++) {
tick += frametime;
ssd1306_Fill(Black);
ssd1306_DrawBitmap(21, 0, badapple.frame[i], badapple.w, badapple.h,
White);
ssd1306_UpdateScreen();
while (HAL_GetTick() < tick) {
}
}
}
}

void SysTick_Handler(void) {
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}
Loading