Skip to content

Commit 97a88ff

Browse files
committed
Added sifive-hifive test application with external bsp
1 parent b918014 commit 97a88ff

4 files changed

Lines changed: 190 additions & 27 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ KINETIS?=$(HOME)/src/FRDM-K64F
1212
KINETIS_CPU=MK64FN1M0VLL12
1313
KINETIS_DRIVERS?=$(KINETIS)/devices/MK64F12
1414
KINETIS_CMSIS?=$(KINETIS)/CMSIS
15+
FREEDOM_E_SDK?=$(HOME)/src/freedom-e-sdk
1516
DEBUG?=0
1617
VTOR?=1
1718
CORTEX_M0?=0
@@ -143,7 +144,8 @@ wolfboot-align.bin: wolfboot.bin
143144
test-app/image.bin:
144145
@make -C test-app TARGET=$(TARGET) EXT_FLASH=$(EXT_FLASH) SPI_FLASH=$(SPI_FLASH) ARCH=$(ARCH) V=$(V) \
145146
KINETIS=$(KINETIS) KINETIS_CPU=$(KINETIS_CPU) KINETIS_DRIVERS=$(KINETIS_DRIVERS) \
146-
KINETIS_CMSIS=$(KINETIS_CMSIS) NVM_FLASH_WRITEONCE=$(NVM_FLASH_WRITEONCE)
147+
KINETIS_CMSIS=$(KINETIS_CMSIS) NVM_FLASH_WRITEONCE=$(NVM_FLASH_WRITEONCE) \
148+
FREEDOM_E_SDK=$(FREEDOM_E_SDK)
147149
@rm -f src/*.o hal/*.o
148150

149151
include tools/test.mk

test-app/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ ifeq ($(TARGET),kinetis)
3737
$(KINETIS_DRIVERS)/drivers/fsl_ftfx_controller.o $(KINETIS_DRIVERS)/drivers/fsl_gpio.o
3838
endif
3939

40+
ifeq ($(TARGET),hifive1)
41+
CFLAGS+=-I$(FREEDOM_E_SDK)/freedom-metal/ -D__METAL_MACHINE_HEADER=\"$(FREEDOM_E_SDK)/bsp/sifive-hifive1/metal.h\"
42+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/clock.o
43+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/led.o
44+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/cache.o
45+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/cpu.o
46+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/gpio.o
47+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/interrupt.o
48+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/uart.o
49+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/tty.o
50+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/spi.o
51+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/shutdown.o
52+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/timer.o
53+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,fe310-g000,hfrosc.o
54+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,fe310-g000,hfxosc.o
55+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,fe310-g000,pll.o
56+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,fe310-g000,prci.o
57+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,spi0.o
58+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,uart0.o
59+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,global-external-interrupts0.o
60+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,local-external-interrupts0.o
61+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,gpio0.o
62+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/sifive,gpio-leds.o
63+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/riscv,clint0.o
64+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/riscv,plic0.o
65+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/riscv,cpu.o
66+
APP_OBJS+=$(FREEDOM_E_SDK)/freedom-metal/src/drivers/fixed-clock.o
67+
endif
68+
4069
standalone:CFLAGS+=-DTEST_APP_STANDALONE
4170
standalone:LDFLAGS:=$(CFLAGS) -T standalone.ld -Wl,-gc-sections -Wl,-Map=image.map
4271

test-app/RISCV.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SECTIONS
1919
KEEP(*(.isr_vector))
2020
*(.text*)
2121
*(.rodata*)
22+
*(.srodata*)
2223
. = ALIGN(4);
2324
_end_text = .;
2425
} > FLASH

test-app/hifive1.c

Lines changed: 157 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,163 @@
1-
/* main.c
1+
/*
2+
* # sifive-welcome
3+
* (A simple welcome example which prints SiFive banner and uses board LEDs
24
*
3-
* Copyright (C) 2018 wolfSSL Inc.
4-
*
5-
* This file is part of wolfBoot.
6-
*
7-
* wolfBoot is free software; you can redistribute it and/or modify
8-
* it under the terms of the GNU General Public License as published by
9-
* the Free Software Foundation; either version 2 of the License, or
10-
* (at your option) any later version.
11-
*
12-
* wolfBoot is distributed in the hope that it will be useful,
13-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
* GNU General Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Public License
18-
* along with this program; if not, write to the Free Software
19-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
5+
* Copyright 2018 SiFive, Inc
206
*/
7+
/* SPDX-License-Identifier: Apache-2.0 */
8+
9+
#include <stdio.h>
10+
#include <metal/cpu.h>
11+
#include <metal/led.h>
12+
#include <metal/button.h>
13+
#include <metal/switch.h>
14+
15+
#define RTC_FREQ 32768
16+
17+
struct metal_cpu *cpu0;
18+
struct metal_interrupt *cpu_intr, *tmr_intr;
19+
int tmr_id;
20+
volatile uint32_t timer_isr_flag;
21+
22+
void display_banner (void) {
23+
24+
printf("\n");
25+
printf("\n");
26+
printf(" SIFIVE, INC.\n");
27+
printf("\n");
28+
printf(" 5555555555555555555555555\n");
29+
printf(" 5555 5555\n");
30+
printf(" 5555 5555\n");
31+
printf(" 5555 5555\n");
32+
printf(" 5555 5555555555555555555555\n");
33+
printf(" 5555 555555555555555555555555\n");
34+
printf(" 5555 5555\n");
35+
printf(" 5555 5555\n");
36+
printf(" 5555 5555\n");
37+
printf(" 5555555555555555555555555555 55555\n");
38+
printf(" 55555 555555555 55555\n");
39+
printf(" 55555 55555 55555\n");
40+
printf(" 55555 5 55555\n");
41+
printf(" 55555 55555\n");
42+
printf(" 55555 55555\n");
43+
printf(" 55555 55555\n");
44+
printf(" 55555 55555\n");
45+
printf(" 55555 55555\n");
46+
printf(" 555555555\n");
47+
printf(" 55555\n");
48+
printf(" 5\n");
49+
printf("\n");
2150

22-
#include <stdlib.h>
23-
#include <stdint.h>
24-
#include <string.h>
51+
printf("\n");
52+
printf(" Welcome to SiFive!\n");
2553

26-
#ifdef PLATFORM_hifive1
27-
void main(void) {
28-
while(1)
29-
;
3054
}
31-
#endif
3255

56+
void timer_isr (int id, void *data) {
57+
58+
// Disable Timer interrupt
59+
metal_interrupt_disable(tmr_intr, tmr_id);
60+
61+
// Flag showing we hit timer isr
62+
timer_isr_flag = 1;
63+
}
64+
65+
void wait_for_timer(struct metal_led *which_led) {
66+
67+
// clear global timer isr flag
68+
timer_isr_flag = 0;
69+
70+
// Turn on desired LED
71+
metal_led_on(which_led);
72+
73+
// Set timer
74+
metal_cpu_set_mtimecmp(cpu0, metal_cpu_get_mtime(cpu0) + RTC_FREQ);
75+
76+
// Enable Timer interrupt
77+
metal_interrupt_enable(tmr_intr, tmr_id);
78+
79+
// wait till timer triggers and isr is hit
80+
while (timer_isr_flag == 0){};
81+
82+
timer_isr_flag = 0;
83+
84+
// Turn off this LED
85+
metal_led_off(which_led);
86+
}
87+
88+
int main (void)
89+
{
90+
int rc, up_cnt, dn_cnt;
91+
struct metal_led *led0_red, *led0_green, *led0_blue;
92+
93+
// This demo will toggle LEDs colors so we define them here
94+
led0_red = metal_led_get_rgb("LD0", "red");
95+
led0_green = metal_led_get_rgb("LD0", "green");
96+
led0_blue = metal_led_get_rgb("LD0", "blue");
97+
if ((led0_red == NULL) || (led0_green == NULL) || (led0_blue == NULL)) {
98+
printf("At least one of LEDs is null.\n");
99+
return 1;
100+
}
101+
102+
// Enable each LED
103+
metal_led_enable(led0_red);
104+
metal_led_enable(led0_green);
105+
metal_led_enable(led0_blue);
106+
107+
// All Off
108+
metal_led_off(led0_red);
109+
metal_led_off(led0_green);
110+
metal_led_off(led0_blue);
111+
112+
// Lets get the CPU and and its interrupt
113+
cpu0 = metal_cpu_get(0);
114+
if (cpu0 == NULL) {
115+
printf("CPU null.\n");
116+
return 2;
117+
}
118+
cpu_intr = metal_cpu_interrupt_controller(cpu0);
119+
if (cpu_intr == NULL) {
120+
printf("CPU interrupt controller is null.\n");
121+
return 3;
122+
}
123+
metal_interrupt_init(cpu_intr);
124+
125+
// display welcome banner
126+
display_banner();
127+
128+
// Setup Timer and its interrupt so we can toggle LEDs on 1s cadence
129+
tmr_intr = metal_cpu_timer_interrupt_controller(cpu0);
130+
if (tmr_intr == NULL) {
131+
printf("TIMER interrupt controller is null.\n");
132+
return 4;
133+
}
134+
metal_interrupt_init(tmr_intr);
135+
tmr_id = metal_cpu_timer_get_interrupt_id(cpu0);
136+
rc = metal_interrupt_register_handler(tmr_intr, tmr_id, timer_isr, cpu0);
137+
if (rc < 0) {
138+
printf("TIMER interrupt handler registration failed\n");
139+
return (rc * -1);
140+
}
141+
142+
// Lastly CPU interrupt
143+
if (metal_interrupt_enable(cpu_intr, 0) == -1) {
144+
printf("CPU interrupt enable failed\n");
145+
return 6;
146+
}
147+
148+
// Red -> Green -> Blue, repeat
149+
while (1) {
150+
151+
// Turn on RED
152+
wait_for_timer(led0_red);
153+
154+
// Turn on Green
155+
wait_for_timer(led0_green);
156+
157+
// Turn on Blue
158+
wait_for_timer(led0_blue);
159+
}
160+
161+
// return
162+
return 0;
163+
}

0 commit comments

Comments
 (0)