Skip to content

Commit 90cdbc8

Browse files
committed
targets: initial implementation for Tillitis TKey device
Signed-off-by: deadprogram <[email protected]>
1 parent ee6fcd7 commit 90cdbc8

File tree

8 files changed

+479
-0
lines changed

8 files changed

+479
-0
lines changed

GNUmakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ endif
853853
@$(MD5SUM) test.hex
854854
$(TINYGO) build -size short -o test.hex -target=maixbit examples/blinky1
855855
@$(MD5SUM) test.hex
856+
$(TINYGO) build -size short -o test.hex -target=tkey examples/blinky1
857+
@$(MD5SUM) test.hex
856858
ifneq ($(WASM), 0)
857859
$(TINYGO) build -size short -o wasm.wasm -target=wasm examples/wasm/export
858860
$(TINYGO) build -size short -o wasm.wasm -target=wasm examples/wasm/main

src/device/tkey/crt0.S

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: 2022 Tillitis AB <tillitis.se>
2+
// SPDX-License-Identifier: BSD-2-Clause
3+
4+
.section ".text.init"
5+
.global _start
6+
_start:
7+
li x1, 0
8+
li x2, 0
9+
li x3, 0
10+
li x4, 0
11+
li x5, 0
12+
li x6, 0
13+
li x7, 0
14+
li x8, 0
15+
li x9, 0
16+
li x10,0
17+
li x11,0
18+
li x12,0
19+
li x13,0
20+
li x14,0
21+
li x15,0
22+
li x16,0
23+
li x17,0
24+
li x18,0
25+
li x19,0
26+
li x20,0
27+
li x21,0
28+
li x22,0
29+
li x23,0
30+
li x24,0
31+
li x25,0
32+
li x26,0
33+
li x27,0
34+
li x28,0
35+
li x29,0
36+
li x30,0
37+
li x31,0
38+
39+
/* init stack */
40+
la sp, _stack_top
41+
42+
/* zero-init bss section */
43+
la a0, _sbss
44+
la a1, _ebss
45+
bge a0, a1, end_init_bss
46+
47+
loop_init_bss:
48+
sw zero, 0(a0)
49+
addi a0, a0, 4
50+
blt a0, a1, loop_init_bss
51+
52+
end_init_bss:
53+
call main

src/device/tkey/tkey.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//go:build tkey
2+
3+
// Hand written file based on https://github.com/tillitis/tkey-libs/blob/main/include/tkey/tk1_mem.h
4+
5+
package tkey
6+
7+
import (
8+
"runtime/volatile"
9+
"unsafe"
10+
)
11+
12+
// Peripherals
13+
var (
14+
TRNG = (*TRNG_Type)(unsafe.Pointer(TK1_MMIO_TRNG_BASE))
15+
16+
TIMER = (*TIMER_Type)(unsafe.Pointer(TK1_MMIO_TIMER_BASE))
17+
18+
UDS = (*UDS_Type)(unsafe.Pointer(TK1_MMIO_UDS_BASE))
19+
20+
UART = (*UART_Type)(unsafe.Pointer(TK1_MMIO_UART_BASE))
21+
22+
TOUCH = (*TOUCH_Type)(unsafe.Pointer(TK1_MMIO_TOUCH_BASE))
23+
24+
TK1 = (*TK1_Type)(unsafe.Pointer(TK1_MMIO_TK1_BASE))
25+
)
26+
27+
// Memory sections
28+
const (
29+
TK1_ROM_BASE uintptr = 0x00000000
30+
31+
TK1_RAM_BASE uintptr = 0x40000000
32+
33+
TK1_MMIO_BASE uintptr = 0xc0000000
34+
35+
TK1_MMIO_TRNG_BASE uintptr = 0xc0000000
36+
37+
TK1_MMIO_TIMER_BASE uintptr = 0xc1000000
38+
39+
TK1_MMIO_UDS_BASE uintptr = 0xc2000000
40+
41+
TK1_MMIO_UART_BASE uintptr = 0xc3000000
42+
43+
TK1_MMIO_TOUCH_BASE uintptr = 0xc4000000
44+
45+
TK1_MMIO_FW_RAM_BASE uintptr = 0xd0000000
46+
47+
TK1_MMIO_TK1_BASE uintptr = 0xff000000
48+
)
49+
50+
// Memory section sizes
51+
const (
52+
TK1_RAM_SIZE uintptr = 0x20000
53+
54+
TK1_MMIO_SIZE uintptr = 0x3fffffff
55+
)
56+
57+
type TRNG_Type struct {
58+
_ [36]byte
59+
STATUS volatile.Register16
60+
_ [108]byte
61+
ENTROPY volatile.Register16
62+
}
63+
64+
type TIMER_Type struct {
65+
_ [32]byte
66+
CTRL volatile.Register16
67+
_ [2]byte
68+
STATUS volatile.Register16
69+
_ [2]byte
70+
PRESCALER volatile.Register32
71+
TIMER volatile.Register32
72+
}
73+
74+
type UDS_Type struct {
75+
DATA [8]volatile.Register16
76+
}
77+
78+
type UART_Type struct {
79+
_ [40]byte
80+
BIT_RATE volatile.Register16
81+
_ [2]byte
82+
DATA_BITS volatile.Register16
83+
_ [2]byte
84+
STOP_BITS volatile.Register16
85+
_ [58]byte
86+
RX_STATUS volatile.Register16
87+
_ [2]byte
88+
RX_DATA volatile.Register16
89+
_ [2]byte
90+
RX_BYTES volatile.Register16
91+
_ [16]byte
92+
TX_STATUS volatile.Register16
93+
_ [2]byte
94+
TX_DATA volatile.Register16
95+
}
96+
97+
type TOUCH_Type struct {
98+
_ [36]byte
99+
STATUS volatile.Register16
100+
}
101+
102+
type TK1_Type struct {
103+
NAME0 [4]volatile.Register8
104+
NAME1 [4]volatile.Register8
105+
VERSION [4]volatile.Register8
106+
_ [16]byte
107+
SWITCH_APP volatile.Register32
108+
_ [4]byte
109+
LED volatile.Register32
110+
GPIO volatile.Register16
111+
APP_ADDR volatile.Register32
112+
APP_SIZE volatile.Register32
113+
BLAKE2S volatile.Register32
114+
_ [56]byte
115+
CDI_FIRST [8]volatile.Register16
116+
_ [38]byte
117+
UDI_FIRST [2]volatile.Register16
118+
_ [62]byte
119+
RAM_ADDR_RAND volatile.Register16
120+
_ [2]byte
121+
RAM_DATA_RAND volatile.Register16
122+
_ [126]byte
123+
CPU_MON_CTRL volatile.Register16
124+
_ [2]byte
125+
CPU_MON_FIRST volatile.Register32
126+
CPU_MON_LAST volatile.Register32
127+
_ [60]byte
128+
SYSTEM_RESET volatile.Register16
129+
_ [66]byte
130+
SPI_EN volatile.Register16
131+
_ [2]byte
132+
SPI_XFER volatile.Register16
133+
_ [2]byte
134+
SPI_DATA volatile.Register16
135+
}
136+
137+
const (
138+
TK1_MMIO_TIMER_CTRL_START_BIT = 0
139+
TK1_MMIO_TIMER_CTRL_STOP_BIT = 1
140+
TK1_MMIO_TIMER_CTRL_START = 1 << TK1_MMIO_TIMER_CTRL_START_BIT
141+
TK1_MMIO_TIMER_CTRL_STOP = 1 << TK1_MMIO_TIMER_CTRL_STOP_BIT
142+
143+
TK1_MMIO_TK1_LED_R_BIT = 2
144+
TK1_MMIO_TK1_LED_G_BIT = 1
145+
TK1_MMIO_TK1_LED_B_BIT = 0
146+
)

src/machine/machine_tkey.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//go:build tkey
2+
3+
package machine
4+
5+
import (
6+
"device/tkey"
7+
)
8+
9+
const deviceName = "TKey"
10+
11+
const (
12+
PinOutput PinMode = iota
13+
)
14+
15+
var (
16+
LED_BLUE = Pin(tkey.TK1_MMIO_TK1_LED_B_BIT)
17+
LED_GREEN = Pin(tkey.TK1_MMIO_TK1_LED_G_BIT)
18+
LED_RED = Pin(tkey.TK1_MMIO_TK1_LED_R_BIT)
19+
20+
LED = LED_GREEN
21+
)
22+
23+
// No config needed for TKey, just to match the Pin interface.
24+
func (p Pin) Configure(config PinConfig) {
25+
}
26+
27+
// Set GPIO pin to high or low.
28+
func (p Pin) Set(high bool) {
29+
switch p {
30+
case LED_BLUE, LED_GREEN, LED_RED:
31+
if high {
32+
tkey.TK1.LED.SetBits(1 << uint(p))
33+
} else {
34+
tkey.TK1.LED.ClearBits(1 << uint(p))
35+
}
36+
}
37+
}
38+
39+
type UART struct {
40+
Bus *tkey.UART_Type
41+
Buffer *RingBuffer
42+
}
43+
44+
var (
45+
UART0 = &_UART0
46+
_UART0 = UART{Bus: tkey.UART, Buffer: NewRingBuffer()}
47+
)
48+
49+
func (uart *UART) Configure(config UARTConfig) {
50+
if config.BaudRate == 0 {
51+
config.BaudRate = 115200
52+
}
53+
54+
uart.SetBaudRate(config.BaudRate)
55+
}
56+
57+
func (uart *UART) SetBaudRate(br uint32) {
58+
uart.Bus.BIT_RATE.Set(uint16(18e6 / br))
59+
}
60+
61+
func (uart *UART) writeByte(c byte) error {
62+
for uart.Bus.TX_STATUS.Get() == 0 {
63+
}
64+
65+
uart.Bus.TX_DATA.Set(uint16(c))
66+
67+
return nil
68+
}
69+
70+
func (uart *UART) flush() {}

src/runtime/runtime_tkey.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build tkey
2+
3+
// This file implements target-specific things for the TKey.
4+
5+
package runtime
6+
7+
import (
8+
"machine"
9+
"runtime/volatile"
10+
)
11+
12+
type timeUnit int64
13+
14+
//export main
15+
func main() {
16+
preinit()
17+
initPeripherals()
18+
run()
19+
exit(0)
20+
}
21+
22+
// initPeripherals configures peripherals the way the runtime expects them.
23+
func initPeripherals() {
24+
machine.InitSerial()
25+
}
26+
27+
func putchar(c byte) {
28+
machine.Serial.WriteByte(c)
29+
}
30+
31+
func getchar() byte {
32+
for machine.Serial.Buffered() == 0 {
33+
Gosched()
34+
}
35+
v, _ := machine.Serial.ReadByte()
36+
return v
37+
}
38+
39+
func buffered() int {
40+
return machine.Serial.Buffered()
41+
}
42+
43+
var timestamp volatile.Register32
44+
45+
// ticks returns the current value of the timer in ticks.
46+
func ticks() timeUnit {
47+
return timeUnit(timestamp.Get())
48+
}
49+
50+
// sleepTicks sleeps for at least the duration d.
51+
func sleepTicks(d timeUnit) {
52+
target := uint64(ticks() + d)
53+
54+
for {
55+
if uint64(ticks()) >= target {
56+
break
57+
}
58+
timestamp.Set(timestamp.Get() + 1)
59+
}
60+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build tkey && !qemu
2+
3+
package runtime
4+
5+
// ticksToNanoseconds converts ticks (at 18MHz) to nanoseconds.
6+
func ticksToNanoseconds(ticks timeUnit) int64 {
7+
return int64(ticks) * 1800
8+
}
9+
10+
// nanosecondsToTicks converts nanoseconds to ticks (at 18MHz).
11+
func nanosecondsToTicks(ns int64) timeUnit {
12+
return timeUnit(ns / 1800)
13+
}
14+
15+
func exit(code int) {
16+
abort()
17+
}
18+
19+
func abort() {
20+
// lock up forever
21+
for {
22+
// TODO: something here?
23+
}
24+
}

0 commit comments

Comments
 (0)