Skip to content

Instantly share code, notes, and snippets.

@eeucalyptus
Last active February 6, 2025 13:19
Show Gist options
  • Select an option

  • Save eeucalyptus/076c6728e79dc5a932c83588f9f3c649 to your computer and use it in GitHub Desktop.

Select an option

Save eeucalyptus/076c6728e79dc5a932c83588f9f3c649 to your computer and use it in GitHub Desktop.
Generate project boilerplate for ch32v003fun
#!/bin/bash
# Copyright 2024 eeucalyptus
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# This script is used to create a new ch32v003fun project. It
# - creates a new directory
# - initializes a git repository
# - adds the ch32v003fun submodule
# - creates a Makefile, README.md, funconfig.h, and a main project file
#
# Usage:
# To create a new project in a new directory:
# ./ch32v003fun_project.sh projectname
# To create a new project in the current (empty!) directory:
# ./ch32v003fun_project.sh
if ! command -v git 2>&1 >/dev/null
then
echo "git is required to run this script, but it is not installed."
exit 1
fi
# check if an argument was provided and create a new project directory
if [ -n "$1" ]; then
mkdir $1
cd $1
PROJECTNAME=$1
elif [ -z "$(ls -A)" ]; then
PROJECTNAME=$(basename $(pwd))
else
echo "Current directory is not empty. Go to empty directory or provide a new project name."
exit 1
fi
# ch32v003fun submodule
git init
git submodule add https://github.com/cnlohr/ch32v003fun.git
cat - <<EOF > README.md
# $PROJECTNAME
This is a new ch32v003fun project, based on the ch32v003fun library
# Building
Run `make` to build and flash the project to a.
EOF
# .gitignore
cat - <<EOF > .gitignore
*.o
*.bin
*.elf
*.hex
*.lst
*.map
compile_commands.json
EOF
# Makefile
cat - <<EOF > Makefile
all : flash
TARGET:=$PROJECTNAME
TARGET_MCU?=CH32V003
CH32V003FUN:=ch32v003fun/ch32fun
MINICHLINK:=ch32v003fun/minichlink
include ch32v003fun/ch32fun/ch32fun.mk
flash : cv_flash
clean : cv_clean
EOF
# funconfig.h
cat - <<EOF > funconfig.h
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H
#define CH32V003 1
#endif
EOF
# $PROJECTNAME.c
cat - <<EOF > $PROJECTNAME.c
#include "ch32fun.h"
int main(void)
{
while(1)
{
}
}
EOF
git add .
if ! git commit -m "Initial commit" ; then
echo "Could not commit, check messages above"
fi
echo Initialized ch32v003fun project \"$PROJECTNAME\"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment