github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/machine_nxpmk66f18.go (about) 1 // Derivative work of Teensyduino Core Library 2 // http://www.pjrc.com/teensy/ 3 // Copyright (c) 2017 PJRC.COM, LLC. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining 6 // a copy of this software and associated documentation files (the 7 // "Software"), to deal in the Software without restriction, including 8 // without limitation the rights to use, copy, modify, merge, publish, 9 // distribute, sublicense, and/or sell copies of the Software, and to 10 // permit persons to whom the Software is furnished to do so, subject to 11 // the following conditions: 12 // 13 // 1. The above copyright notice and this permission notice shall be 14 // included in all copies or substantial portions of the Software. 15 // 16 // 2. If the Software is incorporated into a build system that allows 17 // selection among a list of target devices, then similar target 18 // devices manufactured by PJRC.COM must be included in the list of 19 // target devices and selectable in the same manner. 20 // 21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 25 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 26 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 27 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 // SOFTWARE. 29 30 //go:build nxp && mk66f18 31 32 package machine 33 34 import ( 35 "device/nxp" 36 "runtime/volatile" 37 "unsafe" 38 ) 39 40 const deviceName = nxp.Device 41 42 const ( 43 PinInput PinMode = iota 44 PinInputPullup 45 PinInputPulldown 46 PinOutput 47 PinOutputOpenDrain 48 PinDisable 49 ) 50 51 // Deprecated: use PinInputPullup and PinInputPulldown instead. 52 const ( 53 PinInputPullUp = PinInputPullup 54 PinInputPullDown = PinInputPulldown 55 ) 56 57 const ( 58 PA00 Pin = iota 59 PA01 60 PA02 61 PA03 62 PA04 63 PA05 64 PA06 65 PA07 66 PA08 67 PA09 68 PA10 69 PA11 70 PA12 71 PA13 72 PA14 73 PA15 74 PA16 75 PA17 76 PA18 77 PA19 78 PA20 79 PA21 80 PA22 81 PA23 82 PA24 83 PA25 84 PA26 85 PA27 86 PA28 87 PA29 88 ) 89 90 const ( 91 PB00 Pin = iota + 32 92 PB01 93 PB02 94 PB03 95 PB04 96 PB05 97 PB06 98 PB07 99 PB08 100 PB09 101 PB10 102 PB11 103 _ 104 _ 105 _ 106 _ 107 PB16 108 PB17 109 PB18 110 PB19 111 PB20 112 PB21 113 PB22 114 PB23 115 ) 116 117 const ( 118 PC00 Pin = iota + 64 119 PC01 120 PC02 121 PC03 122 PC04 123 PC05 124 PC06 125 PC07 126 PC08 127 PC09 128 PC10 129 PC11 130 PC12 131 PC13 132 PC14 133 PC15 134 PC16 135 PC17 136 PC18 137 PC19 138 ) 139 140 const ( 141 PD00 Pin = iota + 96 142 PD01 143 PD02 144 PD03 145 PD04 146 PD05 147 PD06 148 PD07 149 PD08 150 PD09 151 PD10 152 PD11 153 PD12 154 PD13 155 PD14 156 PD15 157 ) 158 159 const ( 160 PE00 Pin = iota + 128 161 PE01 162 PE02 163 PE03 164 PE04 165 PE05 166 PE06 167 PE07 168 PE08 169 PE09 170 PE10 171 PE11 172 PE12 173 PE13 174 PE14 175 PE15 176 PE16 177 PE17 178 PE18 179 PE19 180 PE20 181 PE21 182 PE22 183 PE23 184 PE24 185 PE25 186 PE26 187 PE27 188 PE28 189 ) 190 191 //go:inline 192 func (p Pin) reg() (*nxp.GPIO_Type, *volatile.Register32, uint8) { 193 var gpio *nxp.GPIO_Type 194 var pcr *nxp.PORT_Type 195 196 switch p / 32 { 197 case 0: 198 gpio, pcr = nxp.GPIOA, nxp.PORTA 199 case 1: 200 gpio, pcr = nxp.GPIOB, nxp.PORTB 201 case 2: 202 gpio, pcr = nxp.GPIOC, nxp.PORTC 203 case 3: 204 gpio, pcr = nxp.GPIOD, nxp.PORTD 205 case 5: 206 gpio, pcr = nxp.GPIOE, nxp.PORTE 207 default: 208 panic("invalid pin number") 209 } 210 211 return gpio, &(*[32]volatile.Register32)(unsafe.Pointer(pcr))[p%32], uint8(p % 32) 212 } 213 214 // Configure this pin with the given configuration. 215 func (p Pin) Configure(config PinConfig) { 216 gpio, pcr, pos := p.reg() 217 218 switch config.Mode { 219 case PinOutput: 220 gpio.PDDR.SetBits(1 << pos) 221 pcr.Set((1 << nxp.PORT_PCR0_MUX_Pos) | nxp.PORT_PCR0_SRE | nxp.PORT_PCR0_DSE) 222 223 case PinOutputOpenDrain: 224 gpio.PDDR.SetBits(1 << pos) 225 pcr.Set((1 << nxp.PORT_PCR0_MUX_Pos) | nxp.PORT_PCR0_SRE | nxp.PORT_PCR0_DSE | nxp.PORT_PCR0_ODE) 226 227 case PinInput: 228 gpio.PDDR.ClearBits(1 << pos) 229 pcr.Set((1 << nxp.PORT_PCR0_MUX_Pos)) 230 231 case PinInputPullup: 232 gpio.PDDR.ClearBits(1 << pos) 233 pcr.Set((1 << nxp.PORT_PCR0_MUX_Pos) | nxp.PORT_PCR0_PE | nxp.PORT_PCR0_PS) 234 235 case PinInputPulldown: 236 gpio.PDDR.ClearBits(1 << pos) 237 pcr.Set((1 << nxp.PORT_PCR0_MUX_Pos) | nxp.PORT_PCR0_PE) 238 239 case PinDisable: 240 gpio.PDDR.ClearBits(1 << pos) 241 pcr.Set((0 << nxp.PORT_PCR0_MUX_Pos)) 242 } 243 } 244 245 // Set changes the value of the GPIO pin. The pin must be configured as output. 246 func (p Pin) Set(value bool) { 247 gpio, _, pos := p.reg() 248 if value { 249 gpio.PSOR.Set(1 << pos) 250 } else { 251 gpio.PCOR.Set(1 << pos) 252 } 253 } 254 255 // Get returns the current value of a GPIO pin. 256 func (p Pin) Get() bool { 257 gpio, _, pos := p.reg() 258 return gpio.PDIR.HasBits(1 << pos) 259 } 260 261 func (p Pin) Control() *volatile.Register32 { 262 _, pcr, _ := p.reg() 263 return pcr 264 } 265 266 func (p Pin) Fast() FastPin { 267 gpio, _, pos := p.reg() 268 return FastPin{ 269 PDOR: gpio.PDOR.Bit(pos), 270 PSOR: gpio.PSOR.Bit(pos), 271 PCOR: gpio.PCOR.Bit(pos), 272 PTOR: gpio.PTOR.Bit(pos), 273 PDIR: gpio.PDIR.Bit(pos), 274 PDDR: gpio.PDDR.Bit(pos), 275 } 276 } 277 278 type FastPin struct { 279 PDOR *volatile.BitRegister 280 PSOR *volatile.BitRegister 281 PCOR *volatile.BitRegister 282 PTOR *volatile.BitRegister 283 PDIR *volatile.BitRegister 284 PDDR *volatile.BitRegister 285 } 286 287 func (p FastPin) Set() { p.PSOR.Set(true) } 288 func (p FastPin) Clear() { p.PCOR.Set(true) } 289 func (p FastPin) Toggle() { p.PTOR.Set(true) } 290 func (p FastPin) Write(v bool) { p.PDOR.Set(v) } 291 func (p FastPin) Read() bool { return p.PDIR.Get() }