diff options
author | skeys <skeys@google.com> | 2022-02-27 11:46:35 -0800 |
---|---|---|
committer | CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-03-15 16:58:31 +0000 |
commit | af80d98c190eae507855268f8a75dad4c20a25df (patch) | |
tree | 8844c119eef3c035a8f631aebd09ca8159da9f7e | |
parent | cc62942d1b66d58e686ccb49035c746a9b354bff (diff) | |
download | pigweed-af80d98c190eae507855268f8a75dad4c20a25df.tar.gz |
targets: Add Emcraft SmartFusion2 SoM
Change-Id: I5e51ffbb898eecb8f62e6570659ad16339a75223
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/85720
Reviewed-by: Keir Mierle <keir@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Sean Keys <skeys@google.com>
-rw-r--r-- | docs/BUILD.gn | 1 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/BUILD.bazel | 58 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/BUILD.gn | 94 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/boot.cc | 99 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/config/FreeRTOSConfig.h | 81 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/config/sf2_mss_hal_conf.h | 21 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/target_docs.rst | 35 | ||||
-rw-r--r-- | targets/emcraft_sf2_som/vector_table.c | 75 |
8 files changed, 464 insertions, 0 deletions
diff --git a/docs/BUILD.gn b/docs/BUILD.gn index 2400ca6ce..3fffce501 100644 --- a/docs/BUILD.gn +++ b/docs/BUILD.gn @@ -58,6 +58,7 @@ group("target_docs") { "$dir_pigweed/targets/android:target_docs", "$dir_pigweed/targets/arduino:target_docs", "$dir_pigweed/targets/docs:target_docs", + "$dir_pigweed/targets/emcraft_sf2_som:docs", "$dir_pigweed/targets/host:target_docs", "$dir_pigweed/targets/host_device_simulator:target_docs", "$dir_pigweed/targets/lm3s6965evb_qemu:target_docs", diff --git a/targets/emcraft_sf2_som/BUILD.bazel b/targets/emcraft_sf2_som/BUILD.bazel new file mode 100644 index 000000000..26d04260a --- /dev/null +++ b/targets/emcraft_sf2_som/BUILD.bazel @@ -0,0 +1,58 @@ +# Copyright 2022 The Pigweed Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. + +load( + "//pw_build:pigweed.bzl", + "pw_cc_binary", + "pw_cc_library", +) + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) + +pw_cc_library( + name = "pre_init", + srcs = [ + "boot.cc", + "vector_table.c", + ], + hdrs = [ + "config/FreeRTOSConfig.h", + "config/sf2_mss_hal_conf.h", + ], + deps = [ + "//pw_boot", + "//pw_boot_cortex_m", + "//pw_malloc", + "//pw_preprocessor", + "//pw_string", + "//pw_sys_io_emcraft_sf2", + "//third_party/freertos", + "//third_party/smartfusion_mss", + ], +) + +pw_cc_binary( + name = "demo", + srcs = [ + "main.cc", + ], + deps = [ + "//pw_thread:thread", + "//pw_thread:thread_core", + "//pw_thread_freertos:thread", + "//third_party/freertos", + ], +) diff --git a/targets/emcraft_sf2_som/BUILD.gn b/targets/emcraft_sf2_som/BUILD.gn new file mode 100644 index 000000000..007fa4672 --- /dev/null +++ b/targets/emcraft_sf2_som/BUILD.gn @@ -0,0 +1,94 @@ +# Copyright 2022 The Pigweed Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. + +import("//build_overrides/pigweed.gni") + +import("$dir_pw_build/target_types.gni") +import("$dir_pw_docgen/docs.gni") +import("$dir_pw_malloc/backend.gni") +import("$dir_pw_system/system_target.gni") +import("$dir_pw_tokenizer/backend.gni") +import("$dir_pw_toolchain/generate_toolchain.gni") +config("pw_malloc_active") { + if (pw_malloc_BACKEND != "") { + defines = [ "PW_MALLOC_ACTIVE=1" ] + } +} + +if (current_toolchain != default_toolchain) { + pw_source_set("pre_init") { + configs = [ ":pw_malloc_active" ] + deps = [ + "$dir_pw_boot", + "$dir_pw_boot_cortex_m", + "$dir_pw_malloc", + "$dir_pw_preprocessor", + "$dir_pw_string", + "$dir_pw_system", + "$dir_pw_third_party/freertos", + "$dir_pw_third_party/smartfusion_mss", + ] + sources = [ + "boot.cc", + "vector_table.c", + ] + } + + config("config_includes") { + include_dirs = [ "config" ] + } + + pw_source_set("sf2_mss_hal_config") { + public_configs = [ ":config_includes" ] + public = [ "config/sf2_mss_hal_conf.h" ] + } + + pw_source_set("sf2_freertos_config") { + public_configs = [ ":config_includes" ] + public_deps = [ "$dir_pw_third_party/freertos:config_assert" ] + public = [ "config/FreeRTOSConfig.h" ] + } +} + +pw_system_target("emcraft_sf2_som") { + cpu = PW_SYSTEM_CPU.CORTEX_M3 + scheduler = PW_SYSTEM_SCHEDULER.FREERTOS + + link_deps = [ "$dir_pigweed/targets/emcraft_sf2_som:pre_init" ] + build_args = { + pw_log_BACKEND = dir_pw_log_tokenized + pw_tokenizer_GLOBAL_HANDLER_WITH_PAYLOAD_BACKEND = "//pw_system:log" + pw_third_party_freertos_CONFIG = + "$dir_pigweed/targets/emcraft_sf2_som:sf2_freertos_config" + pw_third_party_freertos_PORT = "$dir_pw_third_party/freertos:arm_cm3" + pw_boot_cortex_m_LINK_CONFIG_DEFINES = [ + "PW_BOOT_FLASH_BEGIN=0x00000200", + "PW_BOOT_FLASH_SIZE=200K", + + # TODO(pwbug/219): Currently "pw_tokenizer/detokenize_test" requires at + # least 6K bytes in heap when using pw_malloc_freelist. The heap size + # required for tests should be investigated. + "PW_BOOT_HEAP_SIZE=7K", + "PW_BOOT_MIN_STACK_SIZE=1K", + "PW_BOOT_RAM_BEGIN=0x20000000", + "PW_BOOT_RAM_SIZE=64K", + "PW_BOOT_VECTOR_TABLE_BEGIN=0x00000000", + "PW_BOOT_VECTOR_TABLE_SIZE=512", + ] + } +} + +pw_doc_group("docs") { + sources = [ "target_docs.rst" ] +} diff --git a/targets/emcraft_sf2_som/boot.cc b/targets/emcraft_sf2_som/boot.cc new file mode 100644 index 000000000..284860b2f --- /dev/null +++ b/targets/emcraft_sf2_som/boot.cc @@ -0,0 +1,99 @@ +// Copyright 2022 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +#include "pw_boot/boot.h" + +#include <array> + +#include "FreeRTOS.h" +#include "pw_boot_cortex_m/boot.h" +#include "pw_malloc/malloc.h" +#include "pw_preprocessor/compiler.h" +#include "pw_string/util.h" +#include "pw_sys_io_emcraft_sf2/init.h" +#include "pw_system/init.h" +#include "task.h" + +namespace { + +std::array<StackType_t, configMINIMAL_STACK_SIZE> freertos_idle_stack; +StaticTask_t freertos_idle_tcb; + +std::array<StackType_t, configTIMER_TASK_STACK_DEPTH> freertos_timer_stack; +StaticTask_t freertos_timer_tcb; + +std::array<char, configMAX_TASK_NAME_LEN> temp_thread_name_buffer; + +} // namespace + +// Functions needed when configGENERATE_RUN_TIME_STATS is on. +extern "C" void configureTimerForRunTimeStats(void) {} +extern "C" unsigned long getRunTimeCounterValue(void) { return 10 /* FIXME */; } +// uwTick is an uint32_t incremented each Systick interrupt 1ms. uwTick is used +// to execute HAL_Delay function. + +// Required for configCHECK_FOR_STACK_OVERFLOW. +extern "C" void vApplicationStackOverflowHook(TaskHandle_t, char* pcTaskName) { + pw::string::Copy(pcTaskName, temp_thread_name_buffer); + PW_CRASH("Stack OVF for task %s", temp_thread_name_buffer.data()); +} + +// Required for configUSE_TIMERS. +extern "C" void vApplicationGetTimerTaskMemory( + StaticTask_t** ppxIdleTaskTCBBuffer, + StackType_t** ppxIdleTaskStackBuffer, + uint32_t* pulIdleTaskStackSize) { + *ppxIdleTaskTCBBuffer = &freertos_idle_tcb; + *ppxIdleTaskStackBuffer = freertos_idle_stack.data(); + *pulIdleTaskStackSize = freertos_idle_stack.size(); +} + +extern "C" void vApplicationGetIdleTaskMemory( + StaticTask_t** ppxIdleTaskTCBBuffer, + StackType_t** ppxIdleTaskStackBuffer, + uint32_t* pulIdleTaskStackSize) { + *ppxIdleTaskTCBBuffer = &freertos_timer_tcb; + *ppxIdleTaskStackBuffer = freertos_timer_stack.data(); + *pulIdleTaskStackSize = freertos_timer_stack.size(); +} + +extern "C" void pw_boot_PreStaticMemoryInit() {} + +extern "C" void pw_boot_PreStaticConstructorInit() { + // TODO(skeys) add "#if no_bootLoader" and the functions needed for init. + +#if PW_MALLOC_ACTIVE + pw_MallocInit(&pw_boot_heap_low_addr, &pw_boot_heap_high_addr); +#endif // PW_MALLOC_ACTIVE + pw_sys_io_Init(); +} + +// TODO(amontanez): pw_boot_PreMainInit() should get renamed to +// pw_boot_FinalizeBoot or similar when main() is removed. +extern "C" void pw_boot_PreMainInit() { + pw::system::Init(); + vTaskStartScheduler(); + PW_UNREACHABLE; +} + +// This `main()` stub prevents another main function from being linked since +// this target deliberately doesn't run `main()`. +extern "C" int main() {} + +extern "C" PW_NO_RETURN void pw_boot_PostMain() { + // In case main() returns, just sit here until the device is reset. + while (true) { + } + PW_UNREACHABLE; +} diff --git a/targets/emcraft_sf2_som/config/FreeRTOSConfig.h b/targets/emcraft_sf2_som/config/FreeRTOSConfig.h new file mode 100644 index 000000000..c024e3e60 --- /dev/null +++ b/targets/emcraft_sf2_som/config/FreeRTOSConfig.h @@ -0,0 +1,81 @@ +// Copyright 2022 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +#pragma once + +#include <stdint.h> + +// Externally defined variables that must be forward-declared for FreeRTOS to +// use them. +extern uint32_t SystemCoreClock; +extern void configureTimerForRunTimeStats(void); +extern unsigned long getRunTimeCounterValue(void); + +#define configSUPPORT_DYNAMIC_ALLOCATION 0 +#define configSUPPORT_STATIC_ALLOCATION 1 + +#define configUSE_16_BIT_TICKS 0 +#define configUSE_CO_ROUTINES 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_MUTEXES 1 +#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 +#define configUSE_PREEMPTION 1 +#define configUSE_TICK_HOOK 0 +#define configUSE_TIMERS 1 +#define configUSE_TRACE_FACILITY 1 + +#define configGENERATE_RUN_TIME_STATS 1 +#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS configureTimerForRunTimeStats +#define portGET_RUN_TIME_COUNTER_VALUE getRunTimeCounterValue + +#define configCHECK_FOR_STACK_OVERFLOW 2 +#define configCPU_CLOCK_HZ (SystemCoreClock) +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configMAX_CO_ROUTINE_PRIORITIES (2) +#define configMAX_PRIORITIES (7) +#define configMAX_TASK_NAME_LEN (16) +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t +#define configMINIMAL_STACK_SIZE ((uint16_t)128) +#define configQUEUE_REGISTRY_SIZE 8 +#define configRECORD_STACK_HIGH_ADDRESS 1 +#define configTICK_RATE_HZ ((TickType_t)1000) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_PRIORITY (6) +#define configTIMER_TASK_STACK_DEPTH 512 + +/* __NVIC_PRIO_BITS in CMSIS */ +#define configPRIO_BITS 4 + +#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 +#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 +#define configKERNEL_INTERRUPT_PRIORITY \ + (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS)) +#define configMAX_SYSCALL_INTERRUPT_PRIORITY \ + (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS)) + +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskCleanUpResources 0 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_vTaskDelayUntil 0 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_xTaskGetSchedulerState 1 + +// Instead of defining configASSERT(), include a header that provides a +// definition that redirects to pw_assert. +#include "pw_third_party/freertos/config_assert.h" + +#define vPortSVCHandler SVC_Handler +#define xPortPendSVHandler PendSV_Handler +#define xPortSysTickHandler SysTick_Handler diff --git a/targets/emcraft_sf2_som/config/sf2_mss_hal_conf.h b/targets/emcraft_sf2_som/config/sf2_mss_hal_conf.h new file mode 100644 index 000000000..2f16d2e40 --- /dev/null +++ b/targets/emcraft_sf2_som/config/sf2_mss_hal_conf.h @@ -0,0 +1,21 @@ +// Copyright 2022 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +#pragma once + +#define HAL_GPIO_MODULE_ENABLED +#include "mss_gpio.h" + +#define HAL_UART_MODULE_ENABLED +#include "mss_uart.h" diff --git a/targets/emcraft_sf2_som/target_docs.rst b/targets/emcraft_sf2_som/target_docs.rst new file mode 100644 index 000000000..adcc077e8 --- /dev/null +++ b/targets/emcraft_sf2_som/target_docs.rst @@ -0,0 +1,35 @@ +.. _target-emcraft-sf2-som: + +------------------------------------- +_target-emcraft-sf2-som: SmartFusion2 +------------------------------------- +The Emcraft SmartFusion2 system-on-module target configuration +uses FreeRTOS and the Microchip MSS HAL rather than a from-the-ground-up +baremetal approach. + +----- +Setup +----- +To use this target, pigweed must be set up to use FreeRTOS and the Microchip +MSS HAL for the SmartFusion series. The supported repositories can be +downloaded via ``pw package``, and then the build must be manually configured +to point to the locations the repositories were downloaded to. + +.. code:: sh + + pw package install freertos + pw package install smartfusion_mss + pw package install nanopb + + gn args out + # Add these lines, replacing ${PW_ROOT} with the path to the location that + # Pigweed is checked out at. + dir_pw_third_party_freertos = "${PW_ROOT}/.environment/packages/freertos" + dir_pw_third_party_smartfusion_mss = + "${PW_ROOT}/.environment/packages/smartfusion_mss" + dir_pw_third_party_nanopb = "${PW_ROOT}/.environment/packages/nanopb" + +Building and running the demo +============================= +This target does not yet build as part of Pigweed, but will later be +available though the pw_system_demo build target. diff --git a/targets/emcraft_sf2_som/vector_table.c b/targets/emcraft_sf2_som/vector_table.c new file mode 100644 index 000000000..a4f5f7fc4 --- /dev/null +++ b/targets/emcraft_sf2_som/vector_table.c @@ -0,0 +1,75 @@ +// Copyright 2022 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +#include <stdbool.h> + +#include "pw_boot/boot.h" +#include "pw_boot_cortex_m/boot.h" +#include "pw_preprocessor/compiler.h" + +// Default handler to insert into the ARMv7-M vector table (below). +// This function exists for convenience. If a device isn't doing what you +// expect, it might have hit a fault and ended up here. +static void DefaultFaultHandler(void) { + while (true) { + // Wait for debugger to attach. + } +} + +// This is the device's interrupt vector table. It's not referenced in any +// code because the platform (SmartFusion) expects this table to be present at +// the beginning of flash. The exact address is specified in the pw_boot_armv7m +// configuration as part of the target config. +// +// For more information, see ARMv7-M Architecture Reference Manual DDI 0403E.b +// section B1.5.3. + +// This typedef is for convenience when building the vector table. With the +// exception of SP_main (0th entry in the vector table), all the entries of the +// vector table are function pointers. +typedef void (*InterruptHandler)(void); + +// Interrupt handlers critical for OS operation. +void SVC_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); + +PW_KEEP_IN_SECTION(".vector_table") +const InterruptHandler vector_table[] = { + // The starting location of the stack pointer. + // This address is NOT an interrupt handler/function pointer, it is simply + // the address that the main stack pointer should be initialized to. The + // value is reinterpret casted because it needs to be in the vector table. + [0] = (InterruptHandler)(&pw_boot_stack_high_addr), + + // Reset handler, dictates how to handle reset interrupt. This is the + // address that the Program Counter (PC) is initialized to at boot. + [1] = pw_boot_Entry, + + // NMI handler. + [2] = DefaultFaultHandler, + // HardFault handler. + [3] = DefaultFaultHandler, + // 4-6: Specialized fault handlers. + // 7-10: Reserved. + // SVCall handler. + [11] = SVC_Handler, + // DebugMon handler. + [12] = DefaultFaultHandler, + // 13: Reserved. + // PendSV handler. + [14] = PendSV_Handler, + // SysTick handler. + [15] = SysTick_Handler, +}; |