Add example-lcd for Longan nano board.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka
2019-12-03 13:41:17 +09:00
parent 1dbd9811c2
commit 6e1c791b04
9 changed files with 2275 additions and 0 deletions

View File

@@ -1,3 +1,7 @@
2019-12-03 NIIBE Yutaka <gniibe@fsij.org>
* example-lcd: New.
2019-12-03 NIIBE Yutaka <gniibe@fsij.org> 2019-12-03 NIIBE Yutaka <gniibe@fsij.org>
* contrib/usart-gd32vf103.c: New. * contrib/usart-gd32vf103.c: New.

BIN
example-lcd/FSIJ.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

56
example-lcd/Makefile Normal file
View File

@@ -0,0 +1,56 @@
# Makefile for example application of Chopstx
PROJECT = sample
### Currently, it is for GD32VF103
###
### Please change lines started with '###' for STM32 Nucleo L432.
###
### Please change lines started with '###' for Cortex-M3 board.
###
### Please change lines started with '###' for Cortex-M0+ board
### (STM32F0 Discovery).
CHOPSTX = ..
LDSCRIPT= sample.ld.gd32vf103
### LDSCRIPT= sample.ld.m4
### LDSCRIPT= sample.ld
### LDSCRIPT= sample.ld.m3
CSRC = sample.c
ARCH=riscv32
CHIP=gd32vf103
### CHIP=stm32l4
### USE_SYS = yes
USE_USART = yes
###################################
### CROSS = arm-none-eabi-
CROSS = riscv64-unknown-elf-
CC = $(CROSS)gcc
LD = $(CROSS)gcc
OBJCOPY = $(CROSS)objcopy
### MCU = cortex-m4
### MCU = cortex-m3
### MCU = cortex-m0
CWARN = -Wall -Wextra -Wstrict-prototypes
DEFS = -DFREE_STANDING -DMHZ=96
### DEFS = -DUSE_SYS3 -DFREE_STANDING -DMHZ=80
### DEFS = -DFREE_STANDING -DUSE_SYS3 -DBUSY_LOOP -DCHX_FLAGS_MAIN=CHOPSTX_SCHED_RR
OPT = -O3 -Os -g
LIBS =
CSRC += $(CHOPSTX)/contrib/spi-st.c
####################
include ../rules.mk
board.h:
@echo Please make a symbolic link \'board.h\' to a file in ../board;
@exit 1
sys.c: board.h
distclean: clean
rm -f board.h

1
example-lcd/board.h Symbolic link
View File

@@ -0,0 +1 @@
../board/board-longan-nano.h

1611
example-lcd/fsij-logo.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,29 @@
l = [ 0b00000_000000_00000,
0b00000_000000_01111,
0b00000_000000_11111,
0b00000_011111_00000,
0b00000_011111_01111,
0b00000_011111_11111,
0b00000_111111_00000,
0b00000_111111_01111,
0b00000_111111_11111,
0b01111_000000_00000,
0b01111_000000_01111,
0b01111_000000_11111,
0b01111_011111_00000,
0b01111_011111_01111,
0b01111_011111_11111,
0b01111_111111_00000,
0b01111_111111_01111,
0b01111_111111_11111,
0b11111_000000_00000,
0b11111_000000_01111,
0b11111_000000_11111,
0b11111_011111_00000,
0b11111_011111_01111,
0b11111_011111_11111,
0b11111_111111_00000,
0b11111_111111_01111,
0b11111_111111_11111 ]
for color in l:
print("0x%04x," % color)

429
example-lcd/sample.c Normal file
View File

@@ -0,0 +1,429 @@
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include "board.h"
#include <contrib/spi.h>
#include <contrib/usart.h>
#include <mcu/gd32vf103.h>
void
set_led_b (void)
{
GPIOA->ODR |= (1 << 1);
GPIOA->ODR &= ~(1 << 2);
GPIOC->ODR |= (1 << 13);
}
void
set_led_g (void)
{
GPIOA->ODR &= ~(1 << 1);
GPIOA->ODR |= (1 << 2);
GPIOC->ODR |= (1 << 13);
}
void
set_led (int on)
{
if (on)
GPIOC->ODR &= ~(1 << 13);
else
GPIOC->ODR |= (1 << 13);
}
static chopstx_mutex_t mtx;
static chopstx_cond_t cnd0;
static chopstx_cond_t cnd1;
static uint8_t u, v;
static uint8_t m; /* 0..100 */
static void
wait_for (uint32_t usec)
{
#if defined(BUSY_LOOP)
uint32_t count = usec * 6;
uint32_t i;
for (i = 0; i < count; i++)
asm volatile ("" : : "r" (i) : "memory");
#else
chopstx_usec_wait (usec);
#endif
}
static void *
pwm (void *arg)
{
(void)arg;
chopstx_mutex_lock (&mtx);
chopstx_cond_wait (&cnd0, &mtx);
chopstx_mutex_unlock (&mtx);
while (1)
{
set_led (u&v);
wait_for (m);
set_led (0);
wait_for (100-m);
}
return NULL;
}
static void *
blk (void *arg)
{
(void)arg;
chopstx_mutex_lock (&mtx);
chopstx_cond_wait (&cnd1, &mtx);
chopstx_mutex_unlock (&mtx);
while (1)
{
v = 0;
wait_for (200*1000);
v = 1;
wait_for (200*1000);
}
return NULL;
}
#if defined(BUSY_LOOP)
#define PRIO_PWM (CHOPSTX_SCHED_RR|1)
#define PRIO_BLK (CHOPSTX_SCHED_RR|1)
#else
#define PRIO_PWM 3
#define PRIO_BLK 2
#endif
#define STACK_MAIN
#define STACK_PROCESS_1
#define STACK_PROCESS_2
#define STACK_PROCESS_3
#include "stack-def.h"
#define STACK_ADDR_PWM ((uint32_t)process1_base)
#define STACK_SIZE_PWM (sizeof process1_base)
#define STACK_ADDR_BLK ((uint32_t)process2_base)
#define STACK_SIZE_BLK (sizeof process2_base)
#define PRIO_USART 4
#define STACK_ADDR_USART ((uint32_t)process3_base)
#define STACK_SIZE_USART (sizeof process3_base)
#define LCD_WIDTH 160
#define LCD_HEIGHT 80
/*
* GPIO PB1: RES#
* low active
*/
static void
lcd_reset (int assert)
{
if (assert)
GPIOB->ODR &= ~(1 << 1);
else
GPIOB->ODR |= (1 << 1);
}
/*
* GPIO PB2: CS#
* low active
*/
static void
lcd_chip_select (int assert)
{
if (assert)
GPIOB->ODR &= ~(1 << 2);
else
GPIOB->ODR |= (1 << 2);
}
/*
* GPIO PB0: D/C#
* Display: 1
* Command: 0
*/
#define LCD_DISPLAY 1
#define LCD_COMMAND 0
static void
lcd_control (int control)
{
if (control == LCD_COMMAND)
GPIOB->ODR &= ~(1 << 0);
else
GPIOB->ODR |= (1 << 0);
}
static void
lcd_send (uint8_t data)
{
lcd_chip_select (1);
spi_send (data);
spi_recv ();
lcd_chip_select (0);
}
static void
lcd_data8 (uint8_t data)
{
lcd_send (data);
}
static void
lcd_data16 (uint16_t data)
{
lcd_send (data>>8);
lcd_send (data);
}
static void
lcd_command (uint8_t cmd)
{
lcd_control (LCD_COMMAND);
lcd_send (cmd);
lcd_control (LCD_DISPLAY);
}
/* LCD: ST7735S */
static void
lcd_init (void)
{
spi_init ();
lcd_reset (1);
chopstx_usec_wait (500*1000);
lcd_reset (0);
chopstx_usec_wait (50*1000);
lcd_command (0x11); /* SLPOUT: Sleep out */
chopstx_usec_wait (100*1000);
lcd_command (0x21); /* INVON: display inversion on */
/* INVOFF: display inversion off: 0x20 */
lcd_command (0x3a); /* COLMOD: Pixel data format */
lcd_data8 (0x05); /* 16-bit/pixel */
lcd_command (0x36); /* MADCTL: Memory Data Access Control*/
lcd_data8 (0x78);
lcd_command (0xb1); /* FRMCTR1: frame rate control 1 */
lcd_data8 (0x05);
lcd_data8 (0x3a);
lcd_data8 (0x3a);
lcd_command (0xb2); /* FRMCTR2: frame rate control 2 */
lcd_data8 (0x05);
lcd_data8 (0x3a);
lcd_data8 (0x3a);
lcd_command (0xb3); /* FRMCTR3: frame rate control 3 */
lcd_data8 (0x05);
lcd_data8 (0x3a);
lcd_data8 (0x3a);
lcd_data8 (0x05);
lcd_data8 (0x3a);
lcd_data8 (0x3a);
lcd_command (0xb4); /* INVCTR: inversion control */
lcd_data8 (0x03);
lcd_command (0xc0); /* PWCTR1: power control 1 */
lcd_data8 (0x62);
lcd_data8 (0x02);
lcd_data8 (0x04);
lcd_command (0xc1); /* PWCTR1: power control 2 */
lcd_data8 (0xc0);
lcd_command (0xc2); /* PWCTR1: power control 3 */
lcd_data8 (0x0d);
lcd_data8 (0x00);
lcd_command (0xc3); /* PWCTR1: power control 4 */
lcd_data8 (0x8d);
lcd_data8 (0x6a);
lcd_command (0xc4); /* PWCTR1: power control 5 */
lcd_data8 (0x8d);
lcd_data8 (0xee);
lcd_command (0xc5); /* VCMCTR1: VCOM control 1 */
lcd_data8 (0x0e);
lcd_command (0xe0); /* GMCTRP1: Gamma correction setting */
lcd_data8 (0x10);
lcd_data8 (0x0e);
lcd_data8 (0x02);
lcd_data8 (0x03);
lcd_data8 (0x0e);
lcd_data8 (0x07);
lcd_data8 (0x02);
lcd_data8 (0x07);
lcd_data8 (0x0a);
lcd_data8 (0x12);
lcd_data8 (0x27);
lcd_data8 (0x37);
lcd_data8 (0x00);
lcd_data8 (0x0d);
lcd_data8 (0x0e);
lcd_data8 (0x10);
lcd_command (0xe1); /* GMCTRN1: Gamma correction setting */
lcd_data8 (0x10);
lcd_data8 (0x0e);
lcd_data8 (0x03);
lcd_data8 (0x03);
lcd_data8 (0x0f);
lcd_data8 (0x06);
lcd_data8 (0x02);
lcd_data8 (0x08);
lcd_data8 (0x0a);
lcd_data8 (0x13);
lcd_data8 (0x26);
lcd_data8 (0x36);
lcd_data8 (0x00);
lcd_data8 (0x0d);
lcd_data8 (0x0e);
lcd_data8 (0x10);
/* All set up! Now, turn on the display. */
lcd_command (0x29); /* DISPON: Display On */
}
static void
lcd_address (uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
lcd_command (0x2a); /* CASET: Column address */
lcd_data16 (x1+1);
lcd_data16 (x2+1);
lcd_command (0x2b); /* RASET: Row address */
lcd_data16 (y1+26);
lcd_data16 (y2+26);
lcd_command (0x2c); /* RAMWR: memory write */
}
static void
lcd_clear (uint16_t color)
{
uint16_t i, j;
lcd_address (0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
for (i = 0; i < LCD_WIDTH; i++)
for (j = 0; j < LCD_HEIGHT; j++)
lcd_data16 (color);
}
static void
lcd_load_image (const uint16_t *image)
{
int i;
lcd_address (0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
for(i = 0; i < LCD_WIDTH*LCD_HEIGHT; i++)
lcd_data16 (image[i]);
}
/* Generated with list_565_colors.py */
static uint16_t colors[27] =
{
0x0000, 0x000f, 0x001f, 0x03e0, 0x03ef, 0x03ff, 0x07e0, 0x07ef, 0x07ff,
0x7800, 0x780f, 0x781f, 0x7be0, 0x7bef, 0x7bff, 0x7fe0, 0x7fef, 0x7fff,
0xf800, 0xf80f, 0xf81f, 0xfbe0, 0xfbef, 0xfbff, 0xffe0, 0xffef, 0xffff,
};
static int
ss_notify (uint8_t dev_no, uint16_t state_bits)
{
(void)dev_no;
(void)state_bits;
return 0;
}
#include "fsij-logo.c"
int
main (int argc, const char *argv[])
{
chopstx_poll_cond_t poll_desc;
struct chx_poll_head *ph[1];
uint32_t timeout;
int cl = 0;
(void)argc;
(void)argv;
set_led (1);
chopstx_mutex_init (&mtx);
chopstx_cond_init (&cnd0);
chopstx_cond_init (&cnd1);
m = 10;
chopstx_create (PRIO_PWM, STACK_ADDR_PWM, STACK_SIZE_PWM, pwm, NULL);
chopstx_create (PRIO_BLK, STACK_ADDR_BLK, STACK_SIZE_BLK, blk, NULL);
chopstx_usec_wait (200*1000);
chopstx_mutex_lock (&mtx);
chopstx_cond_signal (&cnd0);
chopstx_cond_signal (&cnd1);
chopstx_mutex_unlock (&mtx);
lcd_init ();
usart_init (PRIO_USART, STACK_ADDR_USART, STACK_SIZE_USART, ss_notify);
usart_config (0, B115200 | CS8 | STOP1B);
usart_read_prepare_poll (0, &poll_desc);
ph[0] = (struct chx_poll_head *)&poll_desc;
timeout = 200*1000*6;
while (1)
{
chopstx_poll (&timeout, 1, ph);
if (timeout == 0)
{
usart_write (0, "Hello\r\n", 7);
u ^= 1;
timeout = 200*1000*6;
lcd_clear (colors[cl]);
chopstx_usec_wait (1000*1000);
lcd_load_image (fsij_logo);
chopstx_usec_wait (1000*1000);
cl++;
if (cl >= 27)
cl = 0;
}
else
{
char buf[256];
int r;
r = usart_read (0, buf, 256);
if (r)
usart_write (0, buf, r);
}
}
return 0;
}

View File

@@ -0,0 +1,91 @@
/*
* GD32VF103 memory setup.
*/
MEMORY
{
flash : org = 0x08000000, len = 128k
ram : org = 0x20000000, len = 32k
}
__ram_start__ = ORIGIN(ram);
__ram_size__ = 32k;
__ram_end__ = __ram_start__ + __ram_size__;
SECTIONS
{
. = 0;
_text = .;
.text :
{
*(.text.startup.0)
*(.text.startup.1)
*(.text.startup.*)
*(.text)
*(.text.*)
*(.gnu.linkonce.t.*)
*(.rodata)
*(.rodata.*)
. = ALIGN(4);
} > flash
PROVIDE (_etext = .);
_textdata = _etext;
.stacks (NOLOAD) :
{
. = ALIGN(8);
*(.main_stack)
*(.process_stack.0)
*(.process_stack.1)
*(.process_stack.2)
*(.process_stack.3)
. = ALIGN(8);
} > ram
.data :
{
. = ALIGN(4);
PROVIDE(_data = .);
*(.gnu.linkonce.r.*)
*(.data)
. = ALIGN(4);
*(.data.*)
*(.gnu.linkonce.d.*)
. = ALIGN(8);
PROVIDE( __global_pointer$ = . + 0x800);
*(.sdata .sdata.*)
*(.gnu.linkonce.s.*)
. = ALIGN(8);
*(.srodata.cst16)
*(.srodata.cst8)
*(.srodata.cst4)
*(.srodata.cst2)
*(.srodata .srodata.*)
} > ram AT > flash
. = ALIGN(4);
PROVIDE(_edata = .);
PROVIDE(_bss_start = .);
.bss :
{
*(.sbss*)
*(.gnu.linkonce.sb.*)
*(.bss .bss.*)
*(.gnu.linkonce.b.*)
*(COMMON)
. = ALIGN(4);
} > ram
. = ALIGN(8);
PROVIDE(_bss_end = .);
PROVIDE(end = .);
PROVIDE(_end = .);
}
__heap_base__ = _end;
__heap_end__ = __ram_end__;

54
example-lcd/stack-def.h Normal file
View File

@@ -0,0 +1,54 @@
#define MAIN_SIZE 0x0100 /* Idle+Exception handlers */
#define SIZE_0 0x0400 /* Main program */
#define SIZE_1 0x0200 /* first thread program */
#define SIZE_2 0x0200 /* second thread program */
#define SIZE_3 0x0600 /* second thread program */
#if defined(STACK_MAIN)
/*
* The terminology of "main" is confusing in ARM architecture.
* Here, "main_base" is for exception handlers.
*/
/* Idle+Exception handlers */
char __main_stack_end__[0] __attribute__ ((section(".main_stack")));
char main_base[MAIN_SIZE] __attribute__ ((section(".main_stack")));
/* Main program */
char __process0_stack_end__[0] __attribute__ ((section(".process_stack.0")));
char process0_base[SIZE_0] __attribute__ ((section(".process_stack.0")));
#endif
/* First thread program */
#if defined(STACK_PROCESS_1)
char process1_base[SIZE_1] __attribute__ ((section(".process_stack.1")));
#endif
/* Second thread program */
#if defined(STACK_PROCESS_2)
char process2_base[SIZE_2] __attribute__ ((section(".process_stack.2")));
#endif
/* Third thread program */
#if defined(STACK_PROCESS_3)
char process3_base[SIZE_3] __attribute__ ((section(".process_stack.3")));
#endif
/* Fourth thread program */
#if defined(STACK_PROCESS_4)
char process4_base[SIZE_4] __attribute__ ((section(".process_stack.4")));
#endif
/* Fifth thread program */
#if defined(STACK_PROCESS_5)
char process5_base[SIZE_5] __attribute__ ((section(".process_stack.5")));
#endif
/* Sixth thread program */
#if defined(STACK_PROCESS_6)
char process6_base[SIZE_6] __attribute__ ((section(".process_stack.6")));
#endif
/* Seventh thread program */
#if defined(STACK_PROCESS_7)
char process7_base[SIZE_7] __attribute__ ((section(".process_stack.7")));
#endif