First init.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"build": {
|
||||
"cpu": "cortex-m3",
|
||||
"extra_flags": "-DSTM32 -DSTM32F1 -DSTM32F103x8",
|
||||
"f_cpu": "72000000L",
|
||||
"mcu": "stm32f103c8t6",
|
||||
"product_line": "STM32F103x8"
|
||||
},
|
||||
"debug": {
|
||||
"default_tools": [
|
||||
"stlink"
|
||||
],
|
||||
"openocd_target": "stm32f1x",
|
||||
"svd_path": "STM32F103xx.svd"
|
||||
},
|
||||
"frameworks": [
|
||||
"libopencm3"
|
||||
],
|
||||
"name": "STM32F103C8",
|
||||
"upload": {
|
||||
"maximum_ram_size": 20480,
|
||||
"maximum_size": 65536,
|
||||
"protocol": "stlink",
|
||||
"protocols": [
|
||||
"cmsis-dap",
|
||||
"stlink",
|
||||
"blackmagic"
|
||||
]
|
||||
},
|
||||
"url": "https://www.st.com/",
|
||||
"vendor": "ST"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
[env:bluepill]
|
||||
platform = ststm32
|
||||
board = stm32f103c8
|
||||
framework = libopencm3
|
||||
lib_deps =
|
||||
FooLib=symlink://../../../mcu-renderer
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* MCU renderer
|
||||
* ST7789 on STM32
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include <libopencm3/stm32/spi.h>
|
||||
|
||||
#include "display.h"
|
||||
#include "system.h"
|
||||
|
||||
// Connections
|
||||
|
||||
#define DISPLAY_RESX_PORT GPIOB
|
||||
#define DISPLAY_RESX_PIN GPIO0
|
||||
#define DISPLAY_CSX_PORT GPIOB
|
||||
#define DISPLAY_CSX_PIN GPIO1
|
||||
#define DISPLAY_DCX_PORT GPIOB
|
||||
#define DISPLAY_DCX_PIN GPIO10
|
||||
#define DISPLAY_SCL_PORT GPIOA
|
||||
#define DISPLAY_SCL_PIN GPIO5
|
||||
#define DISPLAY_SDA_PORT GPIOA
|
||||
#define DISPLAY_SDA_PIN GPIO7
|
||||
|
||||
// Display
|
||||
|
||||
static uint8_t display_text_buffer[80 * 80];
|
||||
|
||||
static const uint8_t display_init_sequence[] = {
|
||||
MR_SEND_COMMAND(MR_ST7789_INVON), // Inverse for IPS displays
|
||||
MR_END(),
|
||||
};
|
||||
|
||||
void on_display_sleep(uint32_t value);
|
||||
void on_display_set_reset(bool value);
|
||||
void on_display_set_command(bool value);
|
||||
void on_display_send(uint16_t value);
|
||||
void on_display_send16(uint16_t value);
|
||||
|
||||
void on_display_sleep(uint32_t value)
|
||||
{
|
||||
sleep(value);
|
||||
}
|
||||
|
||||
void on_display_set_reset(bool value)
|
||||
{
|
||||
if (value)
|
||||
gpio_clear(DISPLAY_RESX_PORT,
|
||||
DISPLAY_RESX_PIN);
|
||||
else
|
||||
gpio_set(DISPLAY_RESX_PORT,
|
||||
DISPLAY_RESX_PIN);
|
||||
}
|
||||
|
||||
void on_display_set_command(bool value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
// Trigger CS before command
|
||||
gpio_set(DISPLAY_CSX_PORT,
|
||||
DISPLAY_CSX_PIN);
|
||||
gpio_clear(DISPLAY_CSX_PORT,
|
||||
DISPLAY_CSX_PIN);
|
||||
|
||||
gpio_clear(DISPLAY_DCX_PORT,
|
||||
DISPLAY_DCX_PIN);
|
||||
}
|
||||
else
|
||||
gpio_set(DISPLAY_DCX_PORT,
|
||||
DISPLAY_DCX_PIN);
|
||||
}
|
||||
|
||||
void on_display_send(uint16_t value)
|
||||
{
|
||||
spi_send(SPI1, value);
|
||||
}
|
||||
|
||||
void on_display_send16(uint16_t value)
|
||||
{
|
||||
spi_send(SPI1, (value >> 8) & 0xff);
|
||||
spi_send(SPI1, (value >> 0) & 0xff);
|
||||
}
|
||||
|
||||
void init_display(mr_t *mr)
|
||||
{
|
||||
// Setup GPIO
|
||||
gpio_set(DISPLAY_RESX_PORT,
|
||||
DISPLAY_RESX_PIN);
|
||||
gpio_set(DISPLAY_CSX_PORT,
|
||||
DISPLAY_CSX_PIN);
|
||||
|
||||
gpio_set_mode(DISPLAY_RESX_PORT,
|
||||
GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL,
|
||||
DISPLAY_RESX_PIN);
|
||||
gpio_set_mode(DISPLAY_CSX_PORT,
|
||||
GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL,
|
||||
DISPLAY_CSX_PIN);
|
||||
gpio_set_mode(DISPLAY_DCX_PORT,
|
||||
GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL,
|
||||
DISPLAY_DCX_PIN);
|
||||
gpio_set_mode(DISPLAY_SCL_PORT,
|
||||
GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
|
||||
DISPLAY_SCL_PIN);
|
||||
gpio_set_mode(DISPLAY_SDA_PORT,
|
||||
GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
|
||||
DISPLAY_SDA_PIN);
|
||||
|
||||
// Setup SPI
|
||||
rcc_periph_clock_enable(RCC_SPI1);
|
||||
spi_init_master(SPI1,
|
||||
SPI_CR1_BAUDRATE_FPCLK_DIV_2,
|
||||
SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2,
|
||||
SPI_CR1_DFF_8BIT,
|
||||
SPI_CR1_MSBFIRST);
|
||||
spi_enable_software_slave_management(SPI1);
|
||||
spi_set_nss_high(SPI1);
|
||||
spi_enable(SPI1);
|
||||
|
||||
// Setup display
|
||||
mr_st7789_init(mr,
|
||||
DISPLAY_HEIGHT,
|
||||
DISPLAY_WIDTH,
|
||||
MR_DISPLAY_ROTATION_270,
|
||||
display_text_buffer,
|
||||
sizeof(display_text_buffer),
|
||||
on_display_sleep,
|
||||
on_display_set_reset,
|
||||
on_display_set_command,
|
||||
on_display_send,
|
||||
on_display_send16);
|
||||
|
||||
mr_send_sequence(mr, display_init_sequence);
|
||||
}
|
||||
|
||||
void set_display(mr_t *mr, bool value)
|
||||
{
|
||||
mr_st7789_set_display(mr, value);
|
||||
mr_st7789_set_sleep(mr, !value);
|
||||
}
|
||||
|
||||
void update_display(mr_t *mr)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* MCU renderer
|
||||
* ST7789 on STM32
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#if !defined(DISPLAY_H)
|
||||
#define DISPLAY_H
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
#include "mcu-renderer-st7789.h"
|
||||
|
||||
#define DISPLAY_WIDTH 320
|
||||
#define DISPLAY_HEIGHT 240
|
||||
|
||||
void init_display(mr_t *mr);
|
||||
void set_display(mr_t *mr, bool value);
|
||||
void update_display(mr_t *mr);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* MCU renderer
|
||||
* STM32 keyboard
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
|
||||
#include "keyboard.h"
|
||||
|
||||
void init_keyboard(void)
|
||||
{
|
||||
gpio_set(GPIOA,
|
||||
GPIO8 | GPIO9 | GPIO10 | GPIO11);
|
||||
gpio_set_mode(GPIOA,
|
||||
GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_PULL_UPDOWN,
|
||||
GPIO8 | GPIO9 | GPIO10 | GPIO11);
|
||||
}
|
||||
|
||||
bool get_key_down(key_t index)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case KEY_LEFT:
|
||||
return !gpio_get(GPIOA, GPIO8);
|
||||
|
||||
case KEY_RIGHT:
|
||||
return !gpio_get(GPIOA, GPIO9);
|
||||
|
||||
case KEY_UP:
|
||||
return !gpio_get(GPIOA, GPIO10);
|
||||
|
||||
case KEY_DOWN:
|
||||
return !gpio_get(GPIOA, GPIO11);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* MCU renderer
|
||||
* STM32 keyboard
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#if !defined(KEYBOARD_H)
|
||||
#define KEYBOARD_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
KEY_LEFT,
|
||||
KEY_RIGHT,
|
||||
KEY_UP,
|
||||
KEY_DOWN,
|
||||
} key_t;
|
||||
|
||||
void init_keyboard(void);
|
||||
|
||||
bool get_key_down(key_t index);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* MCU renderer example
|
||||
* Gamma calibration demo
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "display.h"
|
||||
#include "keyboard.h"
|
||||
#include "system.h"
|
||||
|
||||
#include "mcu-renderer-fonts/font_robotoM24_4.h"
|
||||
|
||||
#define GRADIENT_HEIGHT ((DISPLAY_HEIGHT - 40) / 4)
|
||||
|
||||
struct gamma_entry
|
||||
{
|
||||
const char *label;
|
||||
uint8_t profile_index;
|
||||
uint8_t bit_num;
|
||||
uint8_t bit_offset;
|
||||
};
|
||||
|
||||
struct gamma_entry gamma_entries[] = {
|
||||
{"V0P", 0, 4, 0},
|
||||
{"V0N", 0 + 14, 4, 0},
|
||||
{"V1P", 1, 6, 0},
|
||||
{"V1N", 1 + 14, 6, 0},
|
||||
{"V2P", 2, 6, 0},
|
||||
{"V2N", 2 + 14, 6, 0},
|
||||
{"V4P", 3, 5, 0},
|
||||
{"V4N", 3 + 14, 5, 0},
|
||||
{"V6P", 4, 5, 0},
|
||||
{"V6N", 4 + 14, 5, 0},
|
||||
{"V13P", 5, 4, 0},
|
||||
{"V13N", 5 + 14, 4, 0},
|
||||
{"V20P", 6, 7, 0},
|
||||
{"V20N", 6 + 14, 7, 0},
|
||||
{"V27P", 7, 3, 0},
|
||||
{"V27N", 7 + 14, 3, 0},
|
||||
{"V36P", 7, 3, 4},
|
||||
{"V36N", 7 + 14, 3, 4},
|
||||
{"V43P", 8, 7, 0},
|
||||
{"V43N", 8 + 14, 7, 0},
|
||||
{"V50P", 9, 4, 0},
|
||||
{"V50N", 9 + 14, 4, 0},
|
||||
{"V57P", 10, 5, 0},
|
||||
{"V57N", 10 + 14, 5, 0},
|
||||
{"V59P", 11, 5, 0},
|
||||
{"V59N", 11 + 14, 5, 0},
|
||||
{"V61P", 12, 6, 0},
|
||||
{"V61N", 12 + 14, 6, 0},
|
||||
{"V62P", 13, 6, 0},
|
||||
{"V62N", 13 + 14, 6, 0},
|
||||
{"V63P", 0, 4, 4},
|
||||
{"V63N", 0 + 14, 4, 4},
|
||||
{"JP0P", 5, 2, 4},
|
||||
{"JP0N", 5 + 14, 2, 4},
|
||||
{"JP1P", 9, 2, 4},
|
||||
{"JP1N", 9 + 14, 2, 4},
|
||||
};
|
||||
|
||||
#define GAMMA_ENTRY_NUM (sizeof(gamma_entries) / sizeof(struct gamma_entry))
|
||||
|
||||
uint8_t gamma_profile[14 * 2] = {
|
||||
0xd0, 0x00, 0x02, 0x07, 0x0b, 0x1a, 0x31, 0x54, 0x40, 0x29, 0x12, 0x12, 0x12, 0x17,
|
||||
0xd0, 0x00, 0x02, 0x07, 0x05, 0x25, 0x2d, 0x44, 0x44, 0x1c, 0x18, 0x16, 0x1c, 0x1d};
|
||||
|
||||
void strcat_uint8(char *text, uint8_t value);
|
||||
|
||||
void strcat_uint8(char *text, uint8_t value)
|
||||
{
|
||||
uint32_t index = strlen(text);
|
||||
|
||||
uint8_t digit;
|
||||
|
||||
digit = value / 100;
|
||||
if (digit)
|
||||
{
|
||||
text[index++] = '0' + digit;
|
||||
value -= 100 * digit;
|
||||
}
|
||||
|
||||
digit = value / 10;
|
||||
if (digit)
|
||||
{
|
||||
text[index++] = '0' + digit;
|
||||
value -= 10 * digit;
|
||||
}
|
||||
|
||||
digit = value;
|
||||
text[index++] = '0' + digit;
|
||||
|
||||
text[index] = '\0';
|
||||
}
|
||||
|
||||
// Main
|
||||
|
||||
int main(void)
|
||||
{
|
||||
mr_t mr;
|
||||
|
||||
init_system();
|
||||
init_display(&mr);
|
||||
init_keyboard();
|
||||
|
||||
// Draw color gradients
|
||||
mr_rectangle_t rectangle = {
|
||||
0, 0,
|
||||
1, GRADIENT_HEIGHT};
|
||||
|
||||
for (uint32_t x = 0; x < DISPLAY_WIDTH; x++)
|
||||
{
|
||||
rectangle.x = x;
|
||||
|
||||
uint32_t value = x * 64 / DISPLAY_WIDTH;
|
||||
mr_color_t red = (value >> 1) << 11;
|
||||
mr_color_t green = value << 5;
|
||||
mr_color_t blue = (value >> 1);
|
||||
|
||||
rectangle.y = 0 * GRADIENT_HEIGHT;
|
||||
mr_set_fill_color(&mr, red);
|
||||
mr_draw_rectangle(&mr, &rectangle);
|
||||
|
||||
rectangle.y = 1 * GRADIENT_HEIGHT;
|
||||
mr_set_fill_color(&mr, green);
|
||||
mr_draw_rectangle(&mr, &rectangle);
|
||||
|
||||
rectangle.y = 2 * GRADIENT_HEIGHT;
|
||||
mr_set_fill_color(&mr, blue);
|
||||
mr_draw_rectangle(&mr, &rectangle);
|
||||
|
||||
rectangle.y = 3 * GRADIENT_HEIGHT;
|
||||
mr_set_fill_color(&mr, red | green | blue);
|
||||
mr_draw_rectangle(&mr, &rectangle);
|
||||
}
|
||||
|
||||
const mr_rectangle_t text_rectangle = {
|
||||
0, 4 * GRADIENT_HEIGHT,
|
||||
DISPLAY_WIDTH, DISPLAY_HEIGHT - 4 * GRADIENT_HEIGHT};
|
||||
|
||||
int32_t gamma_entry_index = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Draw text bar
|
||||
mr_send_command(&mr, MR_ST7789_PVGAMCTRL);
|
||||
for (uint32_t i = 0; i < 14; i++)
|
||||
mr_send_data(&mr, gamma_profile[i]);
|
||||
mr_send_command(&mr, MR_ST7789_NVGAMCTRL);
|
||||
for (uint32_t i = 14; i < 28; i++)
|
||||
mr_send_data(&mr, gamma_profile[i]);
|
||||
|
||||
uint8_t value = gamma_profile[gamma_entries[gamma_entry_index].profile_index];
|
||||
uint8_t bit_offset = gamma_entries[gamma_entry_index].bit_offset;
|
||||
uint8_t mask = (1 << gamma_entries[gamma_entry_index].bit_num) - 1;
|
||||
value >>= bit_offset;
|
||||
value &= mask;
|
||||
|
||||
char text[16];
|
||||
strcpy(text, gamma_entries[gamma_entry_index].label);
|
||||
strcat(text, "=");
|
||||
strcat_uint8(text, value);
|
||||
|
||||
mr_set_font(&mr, font_robotoM24_4);
|
||||
mr_set_fill_color(&mr, mr_get_color(0x000000));
|
||||
mr_set_text_color(&mr, mr_get_color(0xffffff));
|
||||
|
||||
const mr_point_t textOffset = {
|
||||
(DISPLAY_WIDTH - mr_get_text_width(&mr, text)) / 2, 0};
|
||||
|
||||
mr_draw_text(&mr, text, &text_rectangle, &textOffset);
|
||||
|
||||
set_display(&mr, true);
|
||||
|
||||
// Wait for keyboard event
|
||||
while (get_key_down(KEY_LEFT) ||
|
||||
get_key_down(KEY_RIGHT) ||
|
||||
get_key_down(KEY_UP) ||
|
||||
get_key_down(KEY_DOWN))
|
||||
{
|
||||
sleep(50);
|
||||
}
|
||||
|
||||
// Handle keyboard event
|
||||
while (true)
|
||||
{
|
||||
if (get_key_down(KEY_LEFT))
|
||||
{
|
||||
gamma_entry_index--;
|
||||
if (gamma_entry_index < 0)
|
||||
gamma_entry_index = GAMMA_ENTRY_NUM - 1;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (get_key_down(KEY_RIGHT))
|
||||
{
|
||||
gamma_entry_index++;
|
||||
if (gamma_entry_index >= (int32_t)GAMMA_ENTRY_NUM)
|
||||
gamma_entry_index = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (get_key_down(KEY_UP))
|
||||
{
|
||||
value++;
|
||||
value &= mask;
|
||||
value <<= bit_offset;
|
||||
gamma_profile[gamma_entries[gamma_entry_index].profile_index] &= ~mask;
|
||||
gamma_profile[gamma_entries[gamma_entry_index].profile_index] |= value;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (get_key_down(KEY_DOWN))
|
||||
{
|
||||
value--;
|
||||
value &= mask;
|
||||
value <<= bit_offset;
|
||||
gamma_profile[gamma_entries[gamma_entry_index].profile_index] &= ~mask;
|
||||
gamma_profile[gamma_entries[gamma_entry_index].profile_index] |= value;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
sleep(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* MCU renderer
|
||||
* STM32 system code
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
|
||||
#include "system.h"
|
||||
|
||||
static volatile uint32_t system_current_tick;
|
||||
|
||||
void sys_tick_handler(void)
|
||||
{
|
||||
system_current_tick++;
|
||||
}
|
||||
|
||||
void init_system(void)
|
||||
{
|
||||
// Setup MCU clock
|
||||
rcc_clock_setup_in_hse_8mhz_out_72mhz();
|
||||
|
||||
// Setup systick
|
||||
systick_set_frequency(SYSTICK_FREQUENCY, rcc_ahb_frequency);
|
||||
systick_clear();
|
||||
nvic_set_priority(NVIC_SYSTICK_IRQ, 0xc0);
|
||||
systick_interrupt_enable();
|
||||
systick_counter_enable();
|
||||
|
||||
// Setup GPIO
|
||||
rcc_periph_clock_enable(RCC_GPIOA);
|
||||
rcc_periph_clock_enable(RCC_GPIOB);
|
||||
rcc_periph_clock_enable(RCC_GPIOC);
|
||||
}
|
||||
|
||||
void sleep(uint32_t value)
|
||||
{
|
||||
uint32_t start_tick = system_current_tick;
|
||||
|
||||
while ((system_current_tick - start_tick) < value)
|
||||
;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* MCU renderer
|
||||
* STM32 system code
|
||||
*
|
||||
* (C) 2023-2024 Gissio
|
||||
*
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#if !defined(SYSTEM_H)
|
||||
#define SYSTEM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SYSTICK_FREQUENCY 1000
|
||||
|
||||
void init_system(void);
|
||||
|
||||
void sleep(uint32_t value);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user