PoC: testapp: Call syscall accessing SPI flash

This commit is contained in:
Mikael Ågren 2025-02-04 08:42:52 +01:00
parent e5e2ab2787
commit e4df068bb4
No known key found for this signature in database
GPG Key ID: E02DA3D397792C46
5 changed files with 127 additions and 0 deletions

View File

@ -43,6 +43,7 @@ all: testapp.bin
TESTAPP_FMTFILES = \
$(P)/main.c \
$(P)/syscall.h
TESTAPP_OBJS = \
$(P)/main.o \

View File

@ -6,8 +6,10 @@
#include "../tk1/led.h"
#include "../tk1/lib.h"
#include "../tk1/proto.h"
#include "../tk1/syscall_nrs.h"
#include "../tk1/types.h"
#include "../tk1_mem.h"
#include "syscall.h"
#define USBMODE_PACKET_SIZE 64
@ -189,6 +191,16 @@ int main(void)
anyfailed = 1;
}
syscall_enable();
// Syscall should be able to access flash
puts("\r\nReading SPI flash capacity using syscall...\r\n");
int flash_capacity = syscall(TK1_SYSCALL_GET_FLASH_CAPACITY, 0);
if (flash_capacity != 0x14) {
failmsg("Expected SPI flash capacity: 0x14 (1 MByte)");
anyfailed = 1;
}
// Test FW_RAM.
*fw_ram = 0x21;
if (*fw_ram == 0x21) {

View File

@ -0,0 +1,94 @@
// SPDX-FileCopyrightText: 2024 Tillitis AB <tillitis.se>
// SPDX-License-Identifier: BSD-2-Clause
#include "../tk1/picorv32/custom_ops.S"
.section ".text"
.globl syscall_enable
.globl syscall
syscall_enable:
// Enable IRQs
li t0, 0x7fffffff // IRQ31 mask
picorv32_maskirq_insn(zero, t0) // Enable IRQs
ret
syscall:
// Save registers to stack
addi sp, sp, -32*4
sw x0, 0*4(sp)
sw x1, 1*4(sp)
// x2 (sp) is assumed to be preserved by the interrupt handler.
sw x3, 3*4(sp)
sw x4, 4*4(sp)
sw x5, 5*4(sp)
sw x6, 6*4(sp)
sw x7, 7*4(sp)
sw x8, 8*4(sp)
sw x9, 9*4(sp)
// x10 (a0) will contain syscall return value. And should not be saved.
sw x11, 11*4(sp)
sw x12, 12*4(sp)
sw x13, 13*4(sp)
sw x14, 14*4(sp)
sw x15, 15*4(sp)
sw x16, 16*4(sp)
sw x17, 17*4(sp)
sw x18, 18*4(sp)
sw x19, 19*4(sp)
sw x20, 20*4(sp)
sw x21, 21*4(sp)
sw x22, 22*4(sp)
sw x23, 23*4(sp)
sw x24, 24*4(sp)
sw x25, 25*4(sp)
sw x26, 26*4(sp)
sw x27, 27*4(sp)
sw x28, 28*4(sp)
sw x29, 29*4(sp)
sw x30, 30*4(sp)
sw x31, 31*4(sp)
// Trigger syscall interrupt
li t0, (1 << 31)
and t0, a0, t0
li t1, 0xe1000000 // Syscall interrupt trigger address
sw zero, 0(t1) // Trigger interrupt
// Restore registers from stack
lw x0, 0*4(sp)
lw x1, 1*4(sp)
// x2 (sp) is assumed to be preserved by the interrupt handler.
lw x3, 3*4(sp)
lw x4, 4*4(sp)
lw x5, 5*4(sp)
lw x6, 6*4(sp)
lw x7, 7*4(sp)
lw x8, 8*4(sp)
lw x9, 9*4(sp)
// x10 (a0) contains syscall return value. And should not be destroyed.
lw x11, 11*4(sp)
lw x12, 12*4(sp)
lw x13, 13*4(sp)
lw x14, 14*4(sp)
lw x15, 15*4(sp)
lw x16, 16*4(sp)
lw x17, 17*4(sp)
lw x18, 18*4(sp)
lw x19, 19*4(sp)
lw x20, 20*4(sp)
lw x21, 21*4(sp)
lw x22, 22*4(sp)
lw x23, 23*4(sp)
lw x24, 24*4(sp)
lw x25, 25*4(sp)
lw x26, 26*4(sp)
lw x27, 27*4(sp)
lw x28, 28*4(sp)
lw x29, 29*4(sp)
lw x30, 30*4(sp)
lw x31, 31*4(sp)
addi sp, sp, 32*4
ret

View File

@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024 Tillitis AB <tillitis.se>
// SPDX-License-Identifier: BSD-2-Clause
#include "../tk1/types.h"
#ifndef TKEY_APP_SYSCALL_H
#define TKEY_APP_SYSCALL_H
void syscall_enable(void);
int syscall(uint32_t number, uint32_t arg1);
#endif

View File

@ -4,6 +4,7 @@
*/
#include "../tk1/assert.h"
#include "../tk1/flash.h"
#include "../tk1/led.h"
#include "../tk1/syscall_nrs.h"
#include "../tk1/types.h"
@ -24,6 +25,13 @@ int32_t syscall_handler(uint32_t syscall_nr, uint32_t arg1)
case TK1_SYSCALL_SET_LED:
set_led(arg1);
return 0;
case TK1_SYSCALL_GET_FLASH_CAPACITY: {
uint8_t jedec_id[3];
flash_release_powerdown();
flash_read_jedec_id(jedec_id);
flash_powerdown();
return jedec_id[2];
}
default:
assert(1 == 2);
}