github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/volatile/bitband_nxpmk66f18.go (about) 1 //go:build nxp && mk66f18 2 3 package volatile 4 5 import "unsafe" 6 7 const registerBase = 0x40000000 8 const registerEnd = 0x40100000 9 const bitbandBase = 0x42000000 10 11 //go:inline 12 func bitbandAddress(reg uintptr, bit uint8) uintptr { 13 if uintptr(bit) > 32 { 14 panic("invalid bit position") 15 } 16 if reg < registerBase || reg >= registerEnd { 17 panic("register is out of range") 18 } 19 return (reg-registerBase)*32 + uintptr(bit)*4 + bitbandBase 20 } 21 22 // Special types that causes loads/stores to be volatile (necessary for 23 // memory-mapped registers). 24 type BitRegister struct { 25 Reg uint32 26 } 27 28 // Get returns the of the mapped register bit. It is the volatile equivalent of: 29 // 30 // *r.Reg 31 // 32 //go:inline 33 func (r *BitRegister) Get() bool { 34 return LoadUint32(&r.Reg) != 0 35 } 36 37 // Set sets the mapped register bit. It is the volatile equivalent of: 38 // 39 // *r.Reg = 1 40 // 41 //go:inline 42 func (r *BitRegister) Set(v bool) { 43 var i uint32 44 if v { 45 i = 1 46 } 47 StoreUint32(&r.Reg, i) 48 } 49 50 // Bit maps bit N of register R to the corresponding bitband address. Bit panics 51 // if R is not an AIPS or GPIO register or if N is out of range (greater than 52 // the number of bits in a register minus one). 53 // 54 //go:inline 55 func (r *Register8) Bit(bit uint8) *BitRegister { 56 ptr := bitbandAddress(uintptr(unsafe.Pointer(&r.Reg)), bit) 57 return (*BitRegister)(unsafe.Pointer(ptr)) 58 } 59 60 // Bit maps bit N of register R to the corresponding bitband address. Bit panics 61 // if R is not an AIPS or GPIO register or if N is out of range (greater than 62 // the number of bits in a register minus one). 63 // 64 //go:inline 65 func (r *Register16) Bit(bit uint8) *BitRegister { 66 ptr := bitbandAddress(uintptr(unsafe.Pointer(&r.Reg)), bit) 67 return (*BitRegister)(unsafe.Pointer(ptr)) 68 } 69 70 // Bit maps bit N of register R to the corresponding bitband address. Bit panics 71 // if R is not an AIPS or GPIO register or if N is out of range (greater than 72 // the number of bits in a register minus one). 73 // 74 //go:inline 75 func (r *Register32) Bit(bit uint8) *BitRegister { 76 ptr := bitbandAddress(uintptr(unsafe.Pointer(&r.Reg)), bit) 77 return (*BitRegister)(unsafe.Pointer(ptr)) 78 }