From 652b7b69e72d9ee38e3ed75b4e6e35303157cd3e Mon Sep 17 00:00:00 2001 From: HASUMI Hitoshi Date: Tue, 26 Nov 2024 19:37:01 +0900 Subject: [PATCH] PRK Firmware for RP2350 --- CMakeLists.txt | 40 +++++++-- Rakefile | 136 +++++++++++++++++++++++-------- {build => build_pico}/.gitignore | 0 {build => build_pico}/.keep | 0 build_pico2/.gitignore | 3 + build_pico2/.keep | 0 lib/picoruby | 2 +- src/main.c | 26 ++++-- 8 files changed, 157 insertions(+), 50 deletions(-) rename {build => build_pico}/.gitignore (100%) rename {build => build_pico}/.keep (100%) create mode 100644 build_pico2/.gitignore create mode 100644 build_pico2/.keep diff --git a/CMakeLists.txt b/CMakeLists.txt index a7deb9e7..e2cd7ed8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,27 @@ if(DEFINED ENV{PICORUBY_SQLITE3}) add_definitions(-DPICORUBY_SQLITE3) endif() -project("prk_firmware-${MSC_NAME}-${PRK_VERSION}-${PRK_BUILDDATE}-${PRK_REVISION}") +if(DEFINED ENV{PICO2}) + set(PICO_BOARD pico2) + set(R2P2_NAME R2P2_PICO2) + set(BUILD_CONFIG prk_firmware-cortex-m33) + set(BUILD_DIR build_pico2) + add_compile_options( + -mcpu=cortex-m33 + #-march=armv8-m.main+fp+dsp + #-mabi=aapcs-linux + #-mfloat-abi=softfp + -fno-strict-aliasing + -fno-unroll-loops + -mslow-flash-data + ) +else() + set(R2P2_NAME R2P2) + set(BUILD_CONFIG prk_firmware-cortex-m0plus) + set(BUILD_DIR build_pico) +endif() + +project("prk_firmware-${PICO_BOARD}-${MSC_NAME}-${PRK_VERSION}-${PRK_BUILDDATE}-${PRK_REVISION}") # Initializing the Raspberry Pi Pico SDK should happen after project created pico_sdk_init() @@ -99,7 +119,7 @@ if(DEFINED ENV{PICORUBY_NO_MSC}) endif() add_custom_target(mrbdir ALL - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/build/mrb + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/${BUILD_DIR}/mrb ) set(RUBY_TASK_FILES @@ -108,7 +128,7 @@ set(RUBY_TASK_FILES foreach(rb ${RUBY_TASK_FILES}) add_custom_target(${rb} COMMAND ${PICORBC} -B${rb} - -o${CMAKE_SOURCE_DIR}/build/mrb/${rb}.c + -o${CMAKE_SOURCE_DIR}/${BUILD_DIR}/mrb/${rb}.c ${CMAKE_SOURCE_DIR}/mrblib/${rb}.rb ) endforeach(rb) @@ -124,21 +144,21 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/lib/picoruby/mrbgems/picoruby-mrubyc/lib/mrubyc/src ${CMAKE_SOURCE_DIR}/lib/picoruby/mrbgems/picoruby-filesystem-fat/lib/ff14b/source ${CMAKE_SOURCE_DIR}/lib/picoruby/mrbgems - ${CMAKE_SOURCE_DIR}/lib/picoruby/build/$ENV{MRUBY_CONFIG}/mrbgems + ${CMAKE_SOURCE_DIR}/lib/picoruby/build/${BUILD_CONFIG}/mrbgems ${CMAKE_SOURCE_DIR}/lib/picoruby/build/repos/${BUILD_CONFIG}/mruby-compiler2/include + ${CMAKE_SOURCE_DIR}/${BUILD_DIR}/mrb ) target_link_directories(${PROJECT_NAME} PRIVATE - ${CMAKE_SOURCE_DIR}/lib/picoruby/build/$ENV{MRUBY_CONFIG}/lib + ${CMAKE_SOURCE_DIR}/lib/picoruby/build/${BUILD_CONFIG}/lib ) -target_link_libraries(${PROJECT_NAME} +target_link_libraries(${PROJECT_NAME} PRIVATE pico_stdlib pico_bootsel_via_double_reset tinyusb_device tinyusb_board hardware_pio - hardware_sleep hardware_flash hardware_adc hardware_uart @@ -150,6 +170,12 @@ target_link_libraries(${PROJECT_NAME} hardware_pwm mruby ) +# Workaround for Pico2 +if(NOT DEFINED ENV{PICO2}) + target_link_libraries(${PROJECT_NAME} PRIVATE + hardware_sleep + ) +endif() # create map/bin/hex/uf2 file in addition to ELF. pico_add_extra_outputs(${PROJECT_NAME}) diff --git a/Rakefile b/Rakefile index 21fc6ae0..31412b99 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,50 @@ require "fileutils" -ENV['MRUBY_CONFIG'] = "prk_firmware-cortex-m0plus" -PICO_SDK_TAG = "2.0.0" +PICO_SDK_TAG = "2.1.0" -task :default => :production +def mruby_config + case ENV['BOARD']&.downcase + when 'pico2' + 'prk_firmware-cortex-m33' + else + 'prk_firmware-cortex-m0plus' + end +end + +def cmake_flags + flags = [] + case ENV['BOARD']&.downcase + when 'pico2' + flags << "PICO2=yes" + end + flags.join(" ") +end + +def def_board + case ENV['BOARD']&.downcase + when 'pico2' + '-DPICO_PLATFORM=rp2350 -DPICO_BOARD=pico2' + else + '-DPICO_PLATFORM=rp2040 -DPICO_BOARD=pico' + end +end + +def build_dir + dir = case ENV['BOARD']&.downcase + when 'pico2' + "#{ENV['WITH_KEYMAP_DIR']}build_pico2" + else + "#{ENV['WITH_KEYMAP_DIR']}build_pico" + end + FileUtils.mkdir_p dir + dir +end + +task :default do + puts "Specify a task:" + puts " rake pico # build for RP2040" + puts " rake pico2 # build for RP2350" +end task :setup do sh "bundle install" @@ -15,36 +56,65 @@ end task :all => [:libmruby, :cmake, :build] -desc "build debug (you may need to rake clean before this)" -task :debug do +desc "build debug for RP2040 (you may need to rake clean before this)" +task :pico_debug do ENV['PICORUBY_DEBUG'] = '1' + ENV['BOARD'] = 'pico' ENV['CMAKE_BUILD_TYPE'] = 'Debug' Rake::Task[:all].invoke end -desc "build production" -task :production do +desc "build production for RP2040" +task :pico do + ENV['BOARD'] = 'pico' ENV['CMAKE_BUILD_TYPE'] = 'Release' Rake::Task[:all].invoke end -desc "build PRK Firmware inclusive of keymap.rb (without mass storage)" -task :build_with_keymap, ['keyboard_name'] do |_t, args| +desc "build debug for RP2350 (you may need to rake clean before this)" +task :pico2_debug do + ENV['PICORUBY_DEBUG'] = '1' + ENV['BOARD'] = 'pico2' + ENV['CMAKE_BUILD_TYPE'] = 'Debug' + Rake::Task[:all].invoke +end + +desc "build production for RP2350" +task :pico2 do + ENV['BOARD'] = 'pico2' + ENV['CMAKE_BUILD_TYPE'] = 'Release' + Rake::Task[:all].invoke +end + +def with_keymap(board) + ENV['BOARD'] = board + ENV['PICORUBY_NO_MSC'] = '1' + ENV['WITH_KEYMAP_DIR'] = "keyboards/#{args.keyboard_name}/" + Rake::Task[:all].invoke +end + +desc "build PRK Firmware inclusive of keymap.rb (without mass storage) for RP2040" +task :build_with_keymap_pico, ['keyboard_name'] do |_t, args| unless args.keyboard_name raise "Argument `keyboard_name` missing.\nUsage: rake build_with_keymap[prk_meishi2]" end - dir = "keyboards/#{args.keyboard_name}" - FileUtils.mkdir_p "#{dir}/build" - ENV['PICORUBY_NO_MSC'] = '1' - ENV['PRK_BUILD_DIR'] = "#{dir}/" - Rake::Task[:all].invoke + with_keymap('pico') +end + +desc "build PRK Firmware inclusive of keymap.rb (without mass storage) for RP2350 (Pico2)" +task :build_with_keymap_pico2, ['keyboard_name'] do |_t, args| + unless args.keyboard_name + raise "Argument `keyboard_name` missing.\nUsage: rake build_with_keymap[prk_meishi2]" + end + with_keymap('pico2') end -desc "build production with SQLite3 and SD card" +desc "build production with SQLite3 and SD card for RP2350 (Pico2)" task :sqlite3 do ENV['PICORUBY_SQLITE3'] = '1' ENV['PICORUBY_SD_CARD'] = '1' ENV['PICORUBY_MSC_SD'] = '1' + ENV['BOARD'] = 'pico2' Rake::Task[:all].invoke end @@ -55,12 +125,12 @@ end task :libmruby => "lib/picoruby" do FileUtils.cd "lib/picoruby" do sh "MRUBY_CONFIG=default rake test" - sh "rake" + sh "MRUBY_CONFIG=#{mruby_config} rake" end end task :cmake do - sh "cmake -DCMAKE_BUILD_TYPE=#{ENV['CMAKE_BUILD_TYPE']} -B #{ENV['PRK_BUILD_DIR']}build" + sh "#{cmake_flags} cmake #{def_board} -DCMAKE_BUILD_TYPE=#{ENV['CMAKE_BUILD_TYPE']} -B #{build_dir}" end task :check_pico_sdk => :check_pico_sdk_path do @@ -85,15 +155,7 @@ end desc "build without cmake preparation" task :build => :check_pico_sdk do - sh "cmake --build #{ENV['PRK_BUILD_DIR']}build" -end - -desc "clean built that includes keymap" -task :clean_with_keymap , ['keyboard_name'] do |_t, args| - unless args.keyboard_name - raise "Argument `keyboard_name` missing.\nUsage: rake clean_with_keymap[prk_meishi2]" - end - FileUtils.rm_r Dir.glob("keyboards/#{args.keyboard_name}/build/*") + sh "cmake --build #{build_dir}" end desc "clean built" @@ -101,13 +163,15 @@ task :clean do FileUtils.cd "lib/picoruby" do sh "rake clean" end - FileUtils.cd "build" do - FileUtils.rm_rf Dir.glob("prk_firmware-*.*") - end - begin - sh "cmake --build build --target clean" - rescue => e - puts "Ignoring an error: #{e.message}" + %(build_pico build_pico2).each do |dir| + FileUtils.cd dir do + FileUtils.rm_rf Dir.glob("prk_firmware-*.*") + end + begin + sh "cmake --build #{dir} --target clean" + rescue => e + puts "Ignoring an error: #{e.message}" + end end end @@ -116,8 +180,10 @@ task :deep_clean do FileUtils.cd "lib/picoruby" do sh "rake deep_clean" end - FileUtils.cd "build" do - FileUtils.rm_rf Dir.glob("*") + %(build_pico build_pico2).each do |dir| + FileUtils.cd dir do + FileUtils.rm_rf Dir.glob("*") + end end end diff --git a/build/.gitignore b/build_pico/.gitignore similarity index 100% rename from build/.gitignore rename to build_pico/.gitignore diff --git a/build/.keep b/build_pico/.keep similarity index 100% rename from build/.keep rename to build_pico/.keep diff --git a/build_pico2/.gitignore b/build_pico2/.gitignore new file mode 100644 index 00000000..edd99d5c --- /dev/null +++ b/build_pico2/.gitignore @@ -0,0 +1,3 @@ +* +!.keep +!.gitignore diff --git a/build_pico2/.keep b/build_pico2/.keep new file mode 100644 index 00000000..e69de29b diff --git a/lib/picoruby b/lib/picoruby index 156b21ba..0517a881 160000 --- a/lib/picoruby +++ b/lib/picoruby @@ -1 +1 @@ -Subproject commit 156b21ba42fcb7babb924c0165c7fac20852022c +Subproject commit 0517a8812b80d833fbda504e9f77670dee7e55dc diff --git a/src/main.c b/src/main.c index c0839590..ea3d9a73 100644 --- a/src/main.c +++ b/src/main.c @@ -21,19 +21,31 @@ #include "../include/sounder.h" /* tasks */ -#include "../build/mrb/usb_task.c" +#include "usb_task.c" #ifdef PICORUBY_NO_MSC #include #endif -#if defined(PICORUBY_SQLITE3) - #define MEMORY_SIZE (1024*184) -#else - #define MEMORY_SIZE (1024*194) + +#if !defined(HEAP_SIZE) + #if defined(PICO_RP2040) + #if defined(PICORUBY_SQLITE3) + #error "Not enough memory for SQLite3 in RP2040" + #endif + #define HEAP_SIZE (1024 * 194) + #elif defined(PICO_RP2350) + #if defined(PICORUBY_SQLITE3) + #define HEAP_SIZE (1024 * (194 + 240)) + #else + #define HEAP_SIZE (1024 * (194 + 260)) + #endif + #else + #error "Unknown board" + #endif #endif -static uint8_t memory_pool[MEMORY_SIZE]; +static uint8_t memory_pool[HEAP_SIZE]; int autoreload_state; /* from keyboard.h */ @@ -83,7 +95,7 @@ main(void) stdio_init_all(); board_init(); /* PicoRuby */ - mrbc_init(memory_pool, MEMORY_SIZE); + mrbc_init(memory_pool, HEAP_SIZE); prk_init_picoruby(); /* Tasks */ mrbc_create_task(usb_task, 0);