|
| 1 | +//go:build tkey |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/tkey" |
| 7 | + "errors" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +const deviceName = "TKey" |
| 12 | + |
| 13 | +// GPIO pins modes are only here to match the Pin interface. |
| 14 | +// The actual configuration is fixed in the hardware. |
| 15 | +const ( |
| 16 | + PinOutput PinMode = iota |
| 17 | + PinInput |
| 18 | + PinInputPullup |
| 19 | + PinInputPulldown |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + LED_BLUE = Pin(tkey.TK1_MMIO_TK1_LED_B_BIT) |
| 24 | + LED_GREEN = Pin(tkey.TK1_MMIO_TK1_LED_G_BIT) |
| 25 | + LED_RED = Pin(tkey.TK1_MMIO_TK1_LED_R_BIT) |
| 26 | + |
| 27 | + LED = LED_GREEN |
| 28 | + |
| 29 | + TKEY_TOUCH = Pin(3) // 3 is unused, but we need a value here to match the Pin interface. |
| 30 | + BUTTON = TKEY_TOUCH |
| 31 | + |
| 32 | + GPIO1 = Pin(tkey.TK1_MMIO_TK1_GPIO1_BIT + 8) |
| 33 | + GPIO2 = Pin(tkey.TK1_MMIO_TK1_GPIO2_BIT + 8) |
| 34 | + GPIO3 = Pin(tkey.TK1_MMIO_TK1_GPIO3_BIT + 8) |
| 35 | + GPIO4 = Pin(tkey.TK1_MMIO_TK1_GPIO4_BIT + 8) |
| 36 | +) |
| 37 | + |
| 38 | +var touchConfig, gpio1Config, gpio2Config PinConfig |
| 39 | + |
| 40 | +// No config needed for TKey, just to match the Pin interface. |
| 41 | +func (p Pin) Configure(config PinConfig) { |
| 42 | + switch p { |
| 43 | + case BUTTON: |
| 44 | + touchConfig = config |
| 45 | + |
| 46 | + // Clear any pending touch events. |
| 47 | + tkey.TOUCH.STATUS.Set(0) |
| 48 | + case GPIO1: |
| 49 | + gpio1Config = config |
| 50 | + case GPIO2: |
| 51 | + gpio2Config = config |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Set pin to high or low. |
| 56 | +func (p Pin) Set(high bool) { |
| 57 | + switch p { |
| 58 | + case LED_BLUE, LED_GREEN, LED_RED: |
| 59 | + if high { |
| 60 | + tkey.TK1.LED.SetBits(1 << uint(p)) |
| 61 | + } else { |
| 62 | + tkey.TK1.LED.ClearBits(1 << uint(p)) |
| 63 | + } |
| 64 | + case GPIO3, GPIO4: |
| 65 | + if high { |
| 66 | + tkey.TK1.GPIO.SetBits(1 << uint(p-8)) |
| 67 | + } else { |
| 68 | + tkey.TK1.GPIO.ClearBits(1 << uint(p-8)) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Get returns the current value of a pin. |
| 74 | +func (p Pin) Get() bool { |
| 75 | + pushed := false |
| 76 | + mode := PinInput |
| 77 | + |
| 78 | + switch p { |
| 79 | + case BUTTON: |
| 80 | + mode = touchConfig.Mode |
| 81 | + if tkey.TOUCH.STATUS.HasBits(1) { |
| 82 | + tkey.TOUCH.STATUS.Set(0) |
| 83 | + pushed = true |
| 84 | + } |
| 85 | + case GPIO1: |
| 86 | + mode = gpio1Config.Mode |
| 87 | + pushed = tkey.TK1.GPIO.HasBits(1 << uint(p-8)) |
| 88 | + case GPIO2: |
| 89 | + mode = gpio2Config.Mode |
| 90 | + pushed = tkey.TK1.GPIO.HasBits(1 << uint(p-8)) |
| 91 | + case GPIO3, GPIO4: |
| 92 | + mode = PinOutput |
| 93 | + pushed = tkey.TK1.GPIO.HasBits(1 << uint(p-8)) |
| 94 | + case LED_BLUE, LED_GREEN, LED_RED: |
| 95 | + mode = PinOutput |
| 96 | + pushed = tkey.TK1.LED.HasBits(1 << uint(p)) |
| 97 | + } |
| 98 | + |
| 99 | + switch mode { |
| 100 | + case PinInputPullup: |
| 101 | + return !pushed |
| 102 | + case PinInput, PinInputPulldown, PinOutput: |
| 103 | + return pushed |
| 104 | + } |
| 105 | + |
| 106 | + return false |
| 107 | +} |
| 108 | + |
| 109 | +type UART struct { |
| 110 | + Bus *tkey.UART_Type |
| 111 | +} |
| 112 | + |
| 113 | +var ( |
| 114 | + DefaultUART = UART0 |
| 115 | + UART0 = &_UART0 |
| 116 | + _UART0 = UART{Bus: tkey.UART} |
| 117 | +) |
| 118 | + |
| 119 | +// The TKey UART is fixed at 62500 baud, 8N1. |
| 120 | +func (uart *UART) Configure(config UARTConfig) error { |
| 121 | + if !(config.BaudRate == 62500 || config.BaudRate == 0) { |
| 122 | + return errors.New("uart: only 62500 baud rate is supported") |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// Write a slice of data bytes to the UART. |
| 129 | +func (uart *UART) Write(data []byte) (n int, err error) { |
| 130 | + for _, c := range data { |
| 131 | + if err := uart.WriteByte(c); err != nil { |
| 132 | + return n, err |
| 133 | + } |
| 134 | + } |
| 135 | + return len(data), nil |
| 136 | +} |
| 137 | + |
| 138 | +// WriteByte writes a byte of data to the UART. |
| 139 | +func (uart *UART) WriteByte(c byte) error { |
| 140 | + for uart.Bus.TX_STATUS.Get() == 0 { |
| 141 | + } |
| 142 | + |
| 143 | + uart.Bus.TX_DATA.Set(uint32(c)) |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// Buffered returns the number of bytes buffered in the UART. |
| 149 | +func (uart *UART) Buffered() int { |
| 150 | + return int(uart.Bus.RX_BYTES.Get()) |
| 151 | +} |
| 152 | + |
| 153 | +// ReadByte reads a byte of data from the UART. |
| 154 | +func (uart *UART) ReadByte() (byte, error) { |
| 155 | + for uart.Bus.RX_STATUS.Get() == 0 { |
| 156 | + } |
| 157 | + |
| 158 | + return byte(uart.Bus.RX_DATA.Get()), nil |
| 159 | +} |
| 160 | + |
| 161 | +// DTR is not available on the TKey. |
| 162 | +func (uart *UART) DTR() bool { |
| 163 | + return false |
| 164 | +} |
| 165 | + |
| 166 | +// RTS is not available on the TKey. |
| 167 | +func (uart *UART) RTS() bool { |
| 168 | + return false |
| 169 | +} |
| 170 | + |
| 171 | +// GetRNG returns 32 bits of cryptographically secure random data |
| 172 | +func GetRNG() (uint32, error) { |
| 173 | + for tkey.TRNG.STATUS.Get() == 0 { |
| 174 | + } |
| 175 | + |
| 176 | + return uint32(tkey.TRNG.ENTROPY.Get()), nil |
| 177 | +} |
| 178 | + |
| 179 | +// DesignName returns the FPGA design name. |
| 180 | +func DesignName() (string, string) { |
| 181 | + n0 := tkey.TK1.NAME0.Get() |
| 182 | + name0 := string([]byte{byte(n0 >> 24), byte(n0 >> 16), byte(n0 >> 8), byte(n0)}) |
| 183 | + n1 := tkey.TK1.NAME1.Get() |
| 184 | + name1 := string([]byte{byte(n1 >> 24), byte(n1 >> 16), byte(n1 >> 8), byte(n1)}) |
| 185 | + |
| 186 | + return name0, name1 |
| 187 | +} |
| 188 | + |
| 189 | +// DesignVersion returns the FPGA design version. |
| 190 | +func DesignVersion() string { |
| 191 | + version := tkey.TK1.VERSION.Get() |
| 192 | + |
| 193 | + return strconv.Itoa(int(version)) |
| 194 | +} |
| 195 | + |
| 196 | +// CDI returns 8 words of Compound Device Identifier (CDI) generated and written by the firmware when the application is loaded. |
| 197 | +func CDI() []byte { |
| 198 | + cdi := make([]byte, 32) |
| 199 | + for i := 0; i < 8; i++ { |
| 200 | + c := tkey.TK1.CDI_FIRST[i].Get() |
| 201 | + cdi[i*4] = byte(c >> 24) |
| 202 | + cdi[i*4+1] = byte(c >> 16) |
| 203 | + cdi[i*4+2] = byte(c >> 8) |
| 204 | + cdi[i*4+3] = byte(c) |
| 205 | + } |
| 206 | + return cdi |
| 207 | +} |
| 208 | + |
| 209 | +// UDI returns 2 words of Unique Device Identifier (UDI). Only available in firmware mode. |
| 210 | +func UDI() []byte { |
| 211 | + udi := make([]byte, 8) |
| 212 | + for i := 0; i < 2; i++ { |
| 213 | + c := tkey.TK1.UDI_FIRST[i].Get() |
| 214 | + udi[i*4] = byte(c >> 24) |
| 215 | + udi[i*4+1] = byte(c >> 16) |
| 216 | + udi[i*4+2] = byte(c >> 8) |
| 217 | + udi[i*4+3] = byte(c) |
| 218 | + } |
| 219 | + return udi |
| 220 | +} |
| 221 | + |
| 222 | +// UDS returns 8 words of Unique Device Secret. Part of the FPGA design, changed when provisioning a TKey. |
| 223 | +// Only available in firmware mode. UDS is only readable once per power cycle. |
| 224 | +func UDS() []byte { |
| 225 | + uds := make([]byte, 32) |
| 226 | + for i := 0; i < 8; i++ { |
| 227 | + c := tkey.UDS.DATA[i].Get() |
| 228 | + uds[i*4] = byte(c >> 24) |
| 229 | + uds[i*4+1] = byte(c >> 16) |
| 230 | + uds[i*4+2] = byte(c >> 8) |
| 231 | + uds[i*4+3] = byte(c) |
| 232 | + } |
| 233 | + return uds |
| 234 | +} |
0 commit comments