Software APIs
freertos_port.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "dt/dt_rv_timer.h"
6 #include "external/freertos/include/FreeRTOS.h"
7 #include "external/freertos/include/task.h"
8 #include "external/freertos/portable/GCC/RISC-V/portmacro.h"
11 #include "sw/device/lib/runtime/irq.h"
13 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h"
14 #include "sw/device/lib/testing/test_framework/check.h"
15 
16 // NOTE: some of the function names below do NOT, and cannot, conform to the
17 // style guide, since they are specific implementations of FreeRTOS defined
18 // functions.
19 
20 // ----------------------------------------------------------------------------
21 // Heap Setup
22 //
23 // We allocate the heap here and mark it so the linker can make a
24 // NOLOAD section. Otherwise, it will end up in the `.bss` section, which gets
25 // zeroed during boot initializations which wastes simulation cycles.
26 // ----------------------------------------------------------------------------
27 OT_SET_BSS_SECTION(".freertos.heap", uint8_t ucHeap[configTOTAL_HEAP_SIZE];)
28 
29 // ----------------------------------------------------------------------------
30 // Timer Setup (for use when preemptive scheduling is enabled)
31 // ----------------------------------------------------------------------------
32 #if configUSE_PREEMPTION
33 
34 static dif_rv_timer_t timer;
35 static const uint32_t kTimerHartId = (uint32_t)0;
36 static const uint32_t kTimerComparatorId = 0;
37 static const uint64_t kTimerDeadline =
38  100; // Counter must reach 100 for an IRQ to be triggered.
39 
40 // Override the timer ISR to support preemptive context switching.
41 void ottf_timer_isr(uint32_t *exc_info) {
42  LOG_INFO("Handling timer IQR ...");
43  dif_rv_timer_irq_enable_snapshot_t irq_enable_snapshot;
44  CHECK_DIF_OK(
45  dif_rv_timer_irq_disable_all(&timer, kTimerHartId, &irq_enable_snapshot));
46  CHECK_DIF_OK(dif_rv_timer_counter_write(&timer, kTimerHartId, 0));
47  CHECK_DIF_OK(dif_rv_timer_irq_acknowledge(
48  &timer, kDifRvTimerIrqTimerExpiredHart0Timer0));
49  // TODO: increment scheduler tick and switch context if necessary
50  CHECK_DIF_OK(
51  dif_rv_timer_irq_restore_all(&timer, kTimerHartId, &irq_enable_snapshot));
52  LOG_INFO("Done.");
53 }
54 
55 void vPortSetupTimerInterrupt(void) {
56  LOG_INFO("Configuring timer interrupt ...");
57 
58  // Initialize and reset the timer.
59  CHECK_DIF_OK(dif_rv_timer_init_from_dt(kDtRvTimer, &timer));
60  CHECK_DIF_OK(dif_rv_timer_reset(&timer));
61 
62  // Compute and set tick parameters (i.e., step, prescale, etc.).
63  dif_rv_timer_tick_params_t tick_params;
65  kClockFreqPeripheralHz, configTICK_RATE_HZ * kTimerDeadline,
66  &tick_params));
67  LOG_INFO("Tick Freq. (Hz): %u, Prescale: %u; Tick Step: %u",
68  (uint32_t)kClockFreqPeripheralHz, (uint32_t)tick_params.prescale,
69  (uint32_t)tick_params.tick_step);
70  CHECK_DIF_OK(dif_rv_timer_set_tick_params(&timer, kTimerHartId, tick_params));
71 
72  // Enable RV Timer interrupts and arm/enable the timer.
73  CHECK_DIF_OK(dif_rv_timer_irq_set_enabled(
74  &timer, kDifRvTimerIrqTimerExpiredHart0Timer0, kDifToggleEnabled));
75  CHECK_DIF_OK(dif_rv_timer_arm(&timer, kTimerHartId, kTimerComparatorId,
76  kTimerDeadline));
77 
78  CHECK_DIF_OK(dif_rv_timer_counter_set_enabled(&timer, kTimerHartId,
80 }
81 
82 #endif // configUSE_PREEMPTION
83 
84 // ----------------------------------------------------------------------------
85 // Scheduler Setup
86 // ----------------------------------------------------------------------------
87 extern void xPortStartFirstTask(void);
88 
89 BaseType_t xPortStartScheduler(void) {
90 #if configUSE_PREEMPTION
91  vPortSetupTimerInterrupt();
92 #endif // configUSE_PREEMPTION
93  irq_timer_ctrl(true);
94  irq_external_ctrl(true);
95  irq_software_ctrl(true);
96  // Note: no need to call 'irq_global_ctrl(true)' since the global interrupt
97  // enable is set in the xPortStartFirstTask sub-routine in
98  // sw/device/lib/testing/test_framework/freertos_port.S.
99  xPortStartFirstTask();
100 
101  // Unreachable.
102  return pdFAIL;
103 }