|
| 1 | +// +build esp8266 |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/esp" |
| 7 | + "runtime/volatile" |
| 8 | +) |
| 9 | + |
| 10 | +func CPUFrequency() uint32 { |
| 11 | + return 80000000 // 80MHz |
| 12 | +} |
| 13 | + |
| 14 | +type PinMode uint8 |
| 15 | + |
| 16 | +const ( |
| 17 | + PinOutput PinMode = iota |
| 18 | + PinInput |
| 19 | +) |
| 20 | + |
| 21 | +// Pins that are fixed by the chip. |
| 22 | +const ( |
| 23 | + UART_TX_PIN Pin = 1 |
| 24 | + UART_RX_PIN Pin = 3 |
| 25 | +) |
| 26 | + |
| 27 | +// Pin functions are not trivial. The below array maps a pin number (GPIO |
| 28 | +// number) to the pad as used in the IO mux. |
| 29 | +// Tables with the mapping: |
| 30 | +// https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations#pin_functions |
| 31 | +// https://www.espressif.com/sites/default/files/documentation/ESP8266_Pin_List_0.xls |
| 32 | +var pinPadMapping = [...]uint8{ |
| 33 | + 12: 0, |
| 34 | + 13: 1, |
| 35 | + 14: 2, |
| 36 | + 15: 3, |
| 37 | + 3: 4, |
| 38 | + 1: 5, |
| 39 | + 6: 6, |
| 40 | + 7: 7, |
| 41 | + 8: 8, |
| 42 | + 9: 9, |
| 43 | + 10: 10, |
| 44 | + 11: 11, |
| 45 | + 0: 12, |
| 46 | + 2: 13, |
| 47 | + 4: 14, |
| 48 | + 5: 15, |
| 49 | +} |
| 50 | + |
| 51 | +// getPad returns the pad number and the register to configure this pad. |
| 52 | +func (p Pin) getPad() (uint8, *volatile.Register32) { |
| 53 | + pad := pinPadMapping[p] |
| 54 | + var reg *volatile.Register32 |
| 55 | + switch pad { |
| 56 | + case 0: |
| 57 | + reg = &esp.IO_MUX.IO_MUX_MTDI |
| 58 | + case 1: |
| 59 | + reg = &esp.IO_MUX.IO_MUX_MTCK |
| 60 | + case 2: |
| 61 | + reg = &esp.IO_MUX.IO_MUX_MTMS |
| 62 | + case 3: |
| 63 | + reg = &esp.IO_MUX.IO_MUX_MTDO |
| 64 | + case 4: |
| 65 | + reg = &esp.IO_MUX.IO_MUX_U0RXD |
| 66 | + case 5: |
| 67 | + reg = &esp.IO_MUX.IO_MUX_U0TXD |
| 68 | + case 6: |
| 69 | + reg = &esp.IO_MUX.IO_MUX_SD_CLK |
| 70 | + case 7: |
| 71 | + reg = &esp.IO_MUX.IO_MUX_SD_DATA0 |
| 72 | + case 8: |
| 73 | + reg = &esp.IO_MUX.IO_MUX_SD_DATA1 |
| 74 | + case 9: |
| 75 | + reg = &esp.IO_MUX.IO_MUX_SD_DATA2 |
| 76 | + case 10: |
| 77 | + reg = &esp.IO_MUX.IO_MUX_SD_DATA3 |
| 78 | + case 11: |
| 79 | + reg = &esp.IO_MUX.IO_MUX_SD_CMD |
| 80 | + case 12: |
| 81 | + reg = &esp.IO_MUX.IO_MUX_GPIO0 |
| 82 | + case 13: |
| 83 | + reg = &esp.IO_MUX.IO_MUX_GPIO2 |
| 84 | + case 14: |
| 85 | + reg = &esp.IO_MUX.IO_MUX_GPIO4 |
| 86 | + case 15: |
| 87 | + reg = &esp.IO_MUX.IO_MUX_GPIO5 |
| 88 | + } |
| 89 | + return pad, reg |
| 90 | +} |
| 91 | + |
| 92 | +// Configure sets the given pin as output or input pin. |
| 93 | +func (p Pin) Configure(config PinConfig) { |
| 94 | + switch config.Mode { |
| 95 | + case PinInput, PinOutput: |
| 96 | + pad, reg := p.getPad() |
| 97 | + if pad >= 12 { // pin 0, 2, 4, 5 |
| 98 | + reg.Set(0 << 4) // function 0 at bit position 4 |
| 99 | + } else { |
| 100 | + reg.Set(3 << 4) // function 3 at bit position 4 |
| 101 | + } |
| 102 | + if config.Mode == PinOutput { |
| 103 | + esp.GPIO.GPIO_ENABLE_W1TS.Set(1 << uint8(p)) |
| 104 | + } else { |
| 105 | + esp.GPIO.GPIO_ENABLE_W1TC.Set(1 << uint8(p)) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Set sets the output value of this pin to high (true) or low (false). |
| 111 | +func (p Pin) Set(value bool) { |
| 112 | + if value { |
| 113 | + esp.GPIO.GPIO_OUT_W1TS.Set(1 << uint8(p)) |
| 114 | + } else { |
| 115 | + esp.GPIO.GPIO_OUT_W1TC.Set(1 << uint8(p)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// UART0 is a hardware UART that supports both TX and RX. |
| 120 | +var UART0 = UART{Buffer: NewRingBuffer()} |
| 121 | + |
| 122 | +type UART struct { |
| 123 | + Buffer *RingBuffer |
| 124 | +} |
| 125 | + |
| 126 | +// Configure the UART baud rate. TX and RX pins are fixed by the hardware so |
| 127 | +// cannot be modified and will be ignored. |
| 128 | +func (uart UART) Configure(config UARTConfig) { |
| 129 | + if config.BaudRate == 0 { |
| 130 | + config.BaudRate = 115200 |
| 131 | + } |
| 132 | + esp.UART0.UART_CLKDIV.Set(CPUFrequency() / config.BaudRate) |
| 133 | +} |
| 134 | + |
| 135 | +// WriteByte writes a single byte to the output buffer. Note that the hardware |
| 136 | +// includes a buffer of 128 bytes which will be used first. |
| 137 | +func (uart UART) WriteByte(c byte) { |
| 138 | + for (esp.UART0.UART_STATUS.Get()>>16)&0xff >= 128 { |
| 139 | + // Wait until the TX buffer has room. |
| 140 | + } |
| 141 | + esp.UART0.UART_FIFO.Set(uint32(c)) |
| 142 | +} |
0 commit comments