machine/nrf: refactor to use volatile package/API

Signed-off-by: Ron Evans <ron@hybridgroup.com>
Этот коммит содержится в:
Ron Evans 2019-05-20 10:42:50 +02:00 коммит произвёл Ayke
родитель 0f6873cf02
коммит 9f8340a970
6 изменённых файлов: 151 добавлений и 151 удалений

Просмотреть файл

@ -46,7 +46,7 @@ gen-device-avr:
go fmt ./src/device/avr go fmt ./src/device/avr
gen-device-nrf: gen-device-nrf:
./tools/gen-device-svd.py lib/nrfx/mdk/ src/device/nrf/ --source=https://github.com/NordicSemiconductor/nrfx/tree/master/mdk ./tools/gen-device-svd-vol.py lib/nrfx/mdk/ src/device/nrf/ --source=https://github.com/NordicSemiconductor/nrfx/tree/master/mdk
go fmt ./src/device/nrf go fmt ./src/device/nrf
gen-device-sam: gen-device-sam:

Просмотреть файл

@ -20,7 +20,7 @@ const (
func (p GPIO) Configure(config GPIOConfig) { func (p GPIO) Configure(config GPIOConfig) {
cfg := config.Mode | nrf.GPIO_PIN_CNF_DRIVE_S0S1 | nrf.GPIO_PIN_CNF_SENSE_Disabled cfg := config.Mode | nrf.GPIO_PIN_CNF_DRIVE_S0S1 | nrf.GPIO_PIN_CNF_SENSE_Disabled
port, pin := p.getPortPin() port, pin := p.getPortPin()
port.PIN_CNF[pin] = nrf.RegValue(cfg) port.PIN_CNF[pin].Set(uint32(cfg))
} }
// Set the pin to high or low. // Set the pin to high or low.
@ -28,9 +28,9 @@ func (p GPIO) Configure(config GPIOConfig) {
func (p GPIO) Set(high bool) { func (p GPIO) Set(high bool) {
port, pin := p.getPortPin() port, pin := p.getPortPin()
if high { if high {
port.OUTSET = 1 << pin port.OUTSET.Set(1 << pin)
} else { } else {
port.OUTCLR = 1 << pin port.OUTCLR.Set(1 << pin)
} }
} }
@ -38,20 +38,20 @@ func (p GPIO) Set(high bool) {
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskSet() (*uint32, uint32) { func (p GPIO) PortMaskSet() (*uint32, uint32) {
port, pin := p.getPortPin() port, pin := p.getPortPin()
return (*uint32)(&port.OUTSET), 1 << pin return &port.OUTSET.Reg, 1 << pin
} }
// Return the register and mask to disable a given port. This can be used to // Return the register and mask to disable a given port. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskClear() (*uint32, uint32) { func (p GPIO) PortMaskClear() (*uint32, uint32) {
port, pin := p.getPortPin() port, pin := p.getPortPin()
return (*uint32)(&port.OUTCLR), 1 << pin return &port.OUTCLR.Reg, 1 << pin
} }
// Get returns the current value of a GPIO pin. // Get returns the current value of a GPIO pin.
func (p GPIO) Get() bool { func (p GPIO) Get() bool {
port, pin := p.getPortPin() port, pin := p.getPortPin()
return (port.IN>>pin)&1 != 0 return (port.IN.Get()>>pin)&1 != 0
} }
// UART on the NRF. // UART on the NRF.
@ -77,10 +77,10 @@ func (uart UART) Configure(config UARTConfig) {
// Set TX and RX pins from board. // Set TX and RX pins from board.
uart.setPins(UART_TX_PIN, UART_RX_PIN) uart.setPins(UART_TX_PIN, UART_RX_PIN)
nrf.UART0.ENABLE = nrf.UART_ENABLE_ENABLE_Enabled nrf.UART0.ENABLE.Set(nrf.UART_ENABLE_ENABLE_Enabled)
nrf.UART0.TASKS_STARTTX = 1 nrf.UART0.TASKS_STARTTX.Set(1)
nrf.UART0.TASKS_STARTRX = 1 nrf.UART0.TASKS_STARTRX.Set(1)
nrf.UART0.INTENSET = nrf.UART_INTENSET_RXDRDY_Msk nrf.UART0.INTENSET.Set(nrf.UART_INTENSET_RXDRDY_Msk)
// Enable RX IRQ. // Enable RX IRQ.
arm.SetPriority(nrf.IRQ_UART0, 0xc0) // low priority arm.SetPriority(nrf.IRQ_UART0, 0xc0) // low priority
@ -99,22 +99,22 @@ func (uart UART) SetBaudRate(br uint32) {
// https://devzone.nordicsemi.com/f/nordic-q-a/391/uart-baudrate-register-values/2046#2046 // https://devzone.nordicsemi.com/f/nordic-q-a/391/uart-baudrate-register-values/2046#2046
rate := uint32((uint64(br/400)*uint64(400*0xffffffff/16000000) + 0x800) & 0xffffff000) rate := uint32((uint64(br/400)*uint64(400*0xffffffff/16000000) + 0x800) & 0xffffff000)
nrf.UART0.BAUDRATE = nrf.RegValue(rate) nrf.UART0.BAUDRATE.Set(rate)
} }
// WriteByte writes a byte of data to the UART. // WriteByte writes a byte of data to the UART.
func (uart UART) WriteByte(c byte) error { func (uart UART) WriteByte(c byte) error {
nrf.UART0.EVENTS_TXDRDY = 0 nrf.UART0.EVENTS_TXDRDY.Set(0)
nrf.UART0.TXD = nrf.RegValue(c) nrf.UART0.TXD.Set(uint32(c))
for nrf.UART0.EVENTS_TXDRDY == 0 { for nrf.UART0.EVENTS_TXDRDY.Get() == 0 {
} }
return nil return nil
} }
func (uart UART) handleInterrupt() { func (uart UART) handleInterrupt() {
if nrf.UART0.EVENTS_RXDRDY != 0 { if nrf.UART0.EVENTS_RXDRDY.Get() != 0 {
uart.Receive(byte(nrf.UART0.RXD)) uart.Receive(byte(nrf.UART0.RXD.Get()))
nrf.UART0.EVENTS_RXDRDY = 0x0 nrf.UART0.EVENTS_RXDRDY.Set(0x0)
} }
} }
@ -150,26 +150,26 @@ func (i2c I2C) Configure(config I2CConfig) {
// do config // do config
sclPort, sclPin := GPIO{config.SCL}.getPortPin() sclPort, sclPin := GPIO{config.SCL}.getPortPin()
sclPort.PIN_CNF[sclPin] = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | sclPort.PIN_CNF[sclPin].Set((nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
(nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) |
(nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) |
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) | (nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
(nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos) (nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos))
sdaPort, sdaPin := GPIO{config.SDA}.getPortPin() sdaPort, sdaPin := GPIO{config.SDA}.getPortPin()
sdaPort.PIN_CNF[sdaPin] = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | sdaPort.PIN_CNF[sdaPin].Set((nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
(nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) |
(nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) |
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) | (nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
(nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos) (nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos))
if config.Frequency == TWI_FREQ_400KHZ { if config.Frequency == TWI_FREQ_400KHZ {
i2c.Bus.FREQUENCY = nrf.TWI_FREQUENCY_FREQUENCY_K400 i2c.Bus.FREQUENCY.Set(nrf.TWI_FREQUENCY_FREQUENCY_K400)
} else { } else {
i2c.Bus.FREQUENCY = nrf.TWI_FREQUENCY_FREQUENCY_K100 i2c.Bus.FREQUENCY.Set(nrf.TWI_FREQUENCY_FREQUENCY_K100)
} }
i2c.Bus.ENABLE = nrf.TWI_ENABLE_ENABLE_Enabled i2c.Bus.ENABLE.Set(nrf.TWI_ENABLE_ENABLE_Enabled)
i2c.setPins(config.SCL, config.SDA) i2c.setPins(config.SCL, config.SDA)
} }
@ -177,28 +177,28 @@ func (i2c I2C) Configure(config I2CConfig) {
// It clocks out the given address, writes the bytes in w, reads back len(r) // It clocks out the given address, writes the bytes in w, reads back len(r)
// bytes and stores them in r, and generates a stop condition on the bus. // bytes and stores them in r, and generates a stop condition on the bus.
func (i2c I2C) Tx(addr uint16, w, r []byte) error { func (i2c I2C) Tx(addr uint16, w, r []byte) error {
i2c.Bus.ADDRESS = nrf.RegValue(addr) i2c.Bus.ADDRESS.Set(uint32(addr))
if len(w) != 0 { if len(w) != 0 {
i2c.Bus.TASKS_STARTTX = 1 // start transmission for writing i2c.Bus.TASKS_STARTTX.Set(1) // start transmission for writing
for _, b := range w { for _, b := range w {
i2c.writeByte(b) i2c.writeByte(b)
} }
} }
if len(r) != 0 { if len(r) != 0 {
// To trigger suspend task when a byte is received // To trigger suspend task when a byte is received
i2c.Bus.SHORTS = nrf.TWI_SHORTS_BB_SUSPEND i2c.Bus.SHORTS.Set(nrf.TWI_SHORTS_BB_SUSPEND)
i2c.Bus.TASKS_STARTRX = 1 // re-start transmission for reading i2c.Bus.TASKS_STARTRX.Set(1) // re-start transmission for reading
for i := range r { // read each char for i := range r { // read each char
if i+1 == len(r) { if i+1 == len(r) {
// To trigger stop task when last byte is received, set before resume task. // To trigger stop task when last byte is received, set before resume task.
i2c.Bus.SHORTS = nrf.TWI_SHORTS_BB_STOP i2c.Bus.SHORTS.Set(nrf.TWI_SHORTS_BB_STOP)
} }
i2c.Bus.TASKS_RESUME = 1 // re-start transmission for reading i2c.Bus.TASKS_RESUME.Set(1) // re-start transmission for reading
r[i] = i2c.readByte() r[i] = i2c.readByte()
} }
} }
i2c.signalStop() i2c.signalStop()
i2c.Bus.SHORTS = nrf.TWI_SHORTS_BB_SUSPEND_Disabled i2c.Bus.SHORTS.Set(nrf.TWI_SHORTS_BB_SUSPEND_Disabled)
return nil return nil
} }
@ -206,26 +206,26 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
// it must generate a stop condition after the next character is retrieved when // it must generate a stop condition after the next character is retrieved when
// reading. // reading.
func (i2c I2C) signalStop() { func (i2c I2C) signalStop() {
i2c.Bus.TASKS_STOP = 1 i2c.Bus.TASKS_STOP.Set(1)
for i2c.Bus.EVENTS_STOPPED == 0 { for i2c.Bus.EVENTS_STOPPED.Get() == 0 {
} }
i2c.Bus.EVENTS_STOPPED = 0 i2c.Bus.EVENTS_STOPPED.Set(0)
} }
// writeByte writes a single byte to the I2C bus. // writeByte writes a single byte to the I2C bus.
func (i2c I2C) writeByte(data byte) { func (i2c I2C) writeByte(data byte) {
i2c.Bus.TXD = nrf.RegValue(data) i2c.Bus.TXD.Set(uint32(data))
for i2c.Bus.EVENTS_TXDSENT == 0 { for i2c.Bus.EVENTS_TXDSENT.Get() == 0 {
} }
i2c.Bus.EVENTS_TXDSENT = 0 i2c.Bus.EVENTS_TXDSENT.Set(0)
} }
// readByte reads a single byte from the I2C bus. // readByte reads a single byte from the I2C bus.
func (i2c I2C) readByte() byte { func (i2c I2C) readByte() byte {
for i2c.Bus.EVENTS_RXDREADY == 0 { for i2c.Bus.EVENTS_RXDREADY.Get() == 0 {
} }
i2c.Bus.EVENTS_RXDREADY = 0 i2c.Bus.EVENTS_RXDREADY.Set(0)
return byte(i2c.Bus.RXD) return byte(i2c.Bus.RXD.Get())
} }
// SPI on the NRF. // SPI on the NRF.
@ -252,7 +252,7 @@ type SPIConfig struct {
// Configure is intended to setup the SPI interface. // Configure is intended to setup the SPI interface.
func (spi SPI) Configure(config SPIConfig) { func (spi SPI) Configure(config SPIConfig) {
// Disable bus to configure it // Disable bus to configure it
spi.Bus.ENABLE = nrf.SPI_ENABLE_ENABLE_Disabled spi.Bus.ENABLE.Set(nrf.SPI_ENABLE_ENABLE_Disabled)
// set frequency // set frequency
var freq uint32 var freq uint32
@ -275,7 +275,7 @@ func (spi SPI) Configure(config SPIConfig) {
default: default:
freq = nrf.SPI_FREQUENCY_FREQUENCY_K500 freq = nrf.SPI_FREQUENCY_FREQUENCY_K500
} }
spi.Bus.FREQUENCY = nrf.RegValue(freq) spi.Bus.FREQUENCY.Set(freq)
var conf uint32 var conf uint32
@ -302,22 +302,22 @@ func (spi SPI) Configure(config SPIConfig) {
conf &^= (nrf.SPI_CONFIG_CPOL_ActiveHigh << nrf.SPI_CONFIG_CPOL_Pos) conf &^= (nrf.SPI_CONFIG_CPOL_ActiveHigh << nrf.SPI_CONFIG_CPOL_Pos)
conf &^= (nrf.SPI_CONFIG_CPHA_Leading << nrf.SPI_CONFIG_CPHA_Pos) conf &^= (nrf.SPI_CONFIG_CPHA_Leading << nrf.SPI_CONFIG_CPHA_Pos)
} }
spi.Bus.CONFIG = nrf.RegValue(conf) spi.Bus.CONFIG.Set(conf)
// set pins // set pins
spi.setPins(config.SCK, config.MOSI, config.MISO) spi.setPins(config.SCK, config.MOSI, config.MISO)
// Re-enable bus now that it is configured. // Re-enable bus now that it is configured.
spi.Bus.ENABLE = nrf.SPI_ENABLE_ENABLE_Enabled spi.Bus.ENABLE.Set(nrf.SPI_ENABLE_ENABLE_Enabled)
} }
// Transfer writes/reads a single byte using the SPI interface. // Transfer writes/reads a single byte using the SPI interface.
func (spi SPI) Transfer(w byte) (byte, error) { func (spi SPI) Transfer(w byte) (byte, error) {
spi.Bus.TXD = nrf.RegValue(w) spi.Bus.TXD.Set(uint32(w))
for spi.Bus.EVENTS_READY == 0 { for spi.Bus.EVENTS_READY.Get() == 0 {
} }
r := spi.Bus.RXD r := spi.Bus.RXD.Get()
spi.Bus.EVENTS_READY = 0 spi.Bus.EVENTS_READY.Set(0)
// TODO: handle SPI errors // TODO: handle SPI errors
return byte(r), nil return byte(r), nil

Просмотреть файл

@ -14,8 +14,8 @@ func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) {
} }
func (uart UART) setPins(tx, rx uint32) { func (uart UART) setPins(tx, rx uint32) {
nrf.UART0.PSELTXD = nrf.RegValue(tx) nrf.UART0.PSELTXD.Set(tx)
nrf.UART0.PSELRXD = nrf.RegValue(rx) nrf.UART0.PSELRXD.Set(rx)
} }
//go:export UART0_IRQHandler //go:export UART0_IRQHandler
@ -24,8 +24,8 @@ func handleUART0() {
} }
func (i2c I2C) setPins(scl, sda uint8) { func (i2c I2C) setPins(scl, sda uint8) {
i2c.Bus.PSELSCL = nrf.RegValue(scl) i2c.Bus.PSELSCL.Set(uint32(scl))
i2c.Bus.PSELSDA = nrf.RegValue(sda) i2c.Bus.PSELSDA.Set(uint32(sda))
} }
// SPI // SPI
@ -39,7 +39,7 @@ func (spi SPI) setPins(sck, mosi, miso uint8) {
if miso == 0 { if miso == 0 {
miso = SPI0_MISO_PIN miso = SPI0_MISO_PIN
} }
spi.Bus.PSELSCK = nrf.RegValue(sck) spi.Bus.PSELSCK.Set(uint32(sck))
spi.Bus.PSELMOSI = nrf.RegValue(mosi) spi.Bus.PSELMOSI.Set(uint32(mosi))
spi.Bus.PSELMISO = nrf.RegValue(miso) spi.Bus.PSELMISO.Set(uint32(miso))
} }

Просмотреть файл

@ -15,8 +15,8 @@ func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) {
} }
func (uart UART) setPins(tx, rx uint32) { func (uart UART) setPins(tx, rx uint32) {
nrf.UART0.PSELTXD = nrf.RegValue(tx) nrf.UART0.PSELTXD.Set(tx)
nrf.UART0.PSELRXD = nrf.RegValue(rx) nrf.UART0.PSELRXD.Set(rx)
} }
//go:export UARTE0_UART0_IRQHandler //go:export UARTE0_UART0_IRQHandler
@ -25,8 +25,8 @@ func handleUART0() {
} }
func (i2c I2C) setPins(scl, sda uint8) { func (i2c I2C) setPins(scl, sda uint8) {
i2c.Bus.PSELSCL = nrf.RegValue(scl) i2c.Bus.PSELSCL.Set(uint32(scl))
i2c.Bus.PSELSDA = nrf.RegValue(sda) i2c.Bus.PSELSDA.Set(uint32(sda))
} }
// SPI // SPI
@ -40,9 +40,9 @@ func (spi SPI) setPins(sck, mosi, miso uint8) {
if miso == 0 { if miso == 0 {
miso = SPI0_MISO_PIN miso = SPI0_MISO_PIN
} }
spi.Bus.PSEL.SCK = nrf.RegValue(sck) spi.Bus.PSEL.SCK.Set(uint32(sck))
spi.Bus.PSEL.MOSI = nrf.RegValue(mosi) spi.Bus.PSEL.MOSI.Set(uint32(mosi))
spi.Bus.PSEL.MISO = nrf.RegValue(miso) spi.Bus.PSEL.MISO.Set(uint32(miso))
} }
// InitADC initializes the registers needed for ADC. // InitADC initializes the registers needed for ADC.
@ -89,53 +89,53 @@ func (a ADC) Get() uint16 {
return 0 return 0
} }
nrf.SAADC.RESOLUTION = nrf.SAADC_RESOLUTION_VAL_12bit nrf.SAADC.RESOLUTION.Set(nrf.SAADC_RESOLUTION_VAL_12bit)
// Enable ADC. // Enable ADC.
nrf.SAADC.ENABLE = (nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos) nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos)
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
nrf.SAADC.CH[i].PSELN = nrf.SAADC_CH_PSELP_PSELP_NC nrf.SAADC.CH[i].PSELN.Set(nrf.SAADC_CH_PSELP_PSELP_NC)
nrf.SAADC.CH[i].PSELP = nrf.SAADC_CH_PSELP_PSELP_NC nrf.SAADC.CH[i].PSELP.Set(nrf.SAADC_CH_PSELP_PSELP_NC)
} }
// Configure ADC. // Configure ADC.
nrf.SAADC.CH[0].CONFIG = ((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESP_Pos) & nrf.SAADC_CH_CONFIG_RESP_Msk) | nrf.SAADC.CH[0].CONFIG.Set(((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESP_Pos) & nrf.SAADC_CH_CONFIG_RESP_Msk) |
((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESN_Pos) & nrf.SAADC_CH_CONFIG_RESN_Msk) | ((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESN_Pos) & nrf.SAADC_CH_CONFIG_RESN_Msk) |
((nrf.SAADC_CH_CONFIG_GAIN_Gain1_5 << nrf.SAADC_CH_CONFIG_GAIN_Pos) & nrf.SAADC_CH_CONFIG_GAIN_Msk) | ((nrf.SAADC_CH_CONFIG_GAIN_Gain1_5 << nrf.SAADC_CH_CONFIG_GAIN_Pos) & nrf.SAADC_CH_CONFIG_GAIN_Msk) |
((nrf.SAADC_CH_CONFIG_REFSEL_Internal << nrf.SAADC_CH_CONFIG_REFSEL_Pos) & nrf.SAADC_CH_CONFIG_REFSEL_Msk) | ((nrf.SAADC_CH_CONFIG_REFSEL_Internal << nrf.SAADC_CH_CONFIG_REFSEL_Pos) & nrf.SAADC_CH_CONFIG_REFSEL_Msk) |
((nrf.SAADC_CH_CONFIG_TACQ_3us << nrf.SAADC_CH_CONFIG_TACQ_Pos) & nrf.SAADC_CH_CONFIG_TACQ_Msk) | ((nrf.SAADC_CH_CONFIG_TACQ_3us << nrf.SAADC_CH_CONFIG_TACQ_Pos) & nrf.SAADC_CH_CONFIG_TACQ_Msk) |
((nrf.SAADC_CH_CONFIG_MODE_SE << nrf.SAADC_CH_CONFIG_MODE_Pos) & nrf.SAADC_CH_CONFIG_MODE_Msk) ((nrf.SAADC_CH_CONFIG_MODE_SE << nrf.SAADC_CH_CONFIG_MODE_Pos) & nrf.SAADC_CH_CONFIG_MODE_Msk))
// Set pin to read. // Set pin to read.
nrf.SAADC.CH[0].PSELN = nrf.RegValue(pwmPin) nrf.SAADC.CH[0].PSELN.Set(pwmPin)
nrf.SAADC.CH[0].PSELP = nrf.RegValue(pwmPin) nrf.SAADC.CH[0].PSELP.Set(pwmPin)
// Destination for sample result. // Destination for sample result.
nrf.SAADC.RESULT.PTR = nrf.RegValue(uintptr(unsafe.Pointer(&value))) nrf.SAADC.RESULT.PTR.Set(uint32(uintptr(unsafe.Pointer(&value))))
nrf.SAADC.RESULT.MAXCNT = 1 // One sample nrf.SAADC.RESULT.MAXCNT.Set(1) // One sample
// Start tasks. // Start tasks.
nrf.SAADC.TASKS_START = 1 nrf.SAADC.TASKS_START.Set(1)
for nrf.SAADC.EVENTS_STARTED == 0 { for nrf.SAADC.EVENTS_STARTED.Get() == 0 {
} }
nrf.SAADC.EVENTS_STARTED = 0x00 nrf.SAADC.EVENTS_STARTED.Set(0x00)
// Start the sample task. // Start the sample task.
nrf.SAADC.TASKS_SAMPLE = 1 nrf.SAADC.TASKS_SAMPLE.Set(1)
// Wait until the sample task is done. // Wait until the sample task is done.
for nrf.SAADC.EVENTS_END == 0 { for nrf.SAADC.EVENTS_END.Get() == 0 {
} }
nrf.SAADC.EVENTS_END = 0x00 nrf.SAADC.EVENTS_END.Set(0x00)
// Stop the ADC // Stop the ADC
nrf.SAADC.TASKS_STOP = 1 nrf.SAADC.TASKS_STOP.Set(1)
for nrf.SAADC.EVENTS_STOPPED == 0 { for nrf.SAADC.EVENTS_STOPPED.Get() == 0 {
} }
nrf.SAADC.EVENTS_STOPPED = 0 nrf.SAADC.EVENTS_STOPPED.Set(0)
// Disable the ADC. // Disable the ADC.
nrf.SAADC.ENABLE = (nrf.SAADC_ENABLE_ENABLE_Disabled << nrf.SAADC_ENABLE_ENABLE_Pos) nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Disabled << nrf.SAADC_ENABLE_ENABLE_Pos)
if value < 0 { if value < 0 {
value = 0 value = 0
@ -170,21 +170,21 @@ func (pwm PWM) Set(value uint16) {
p := pwms[i] p := pwms[i]
p.PSEL.OUT[0] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[0].Set(uint32(pwm.Pin))
p.PSEL.OUT[1] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[1].Set(uint32(pwm.Pin))
p.PSEL.OUT[2] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[2].Set(uint32(pwm.Pin))
p.PSEL.OUT[3] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[3].Set(uint32(pwm.Pin))
p.ENABLE = (nrf.PWM_ENABLE_ENABLE_Enabled << nrf.PWM_ENABLE_ENABLE_Pos) p.ENABLE.Set(nrf.PWM_ENABLE_ENABLE_Enabled << nrf.PWM_ENABLE_ENABLE_Pos)
p.PRESCALER = nrf.PWM_PRESCALER_PRESCALER_DIV_2 p.PRESCALER.Set(nrf.PWM_PRESCALER_PRESCALER_DIV_2)
p.MODE = nrf.PWM_MODE_UPDOWN_Up p.MODE.Set(nrf.PWM_MODE_UPDOWN_Up)
p.COUNTERTOP = 16384 // frequency p.COUNTERTOP.Set(16384) // frequency
p.LOOP = 0 p.LOOP.Set(0)
p.DECODER = (nrf.PWM_DECODER_LOAD_Common << nrf.PWM_DECODER_LOAD_Pos) | (nrf.PWM_DECODER_MODE_RefreshCount << nrf.PWM_DECODER_MODE_Pos) p.DECODER.Set((nrf.PWM_DECODER_LOAD_Common << nrf.PWM_DECODER_LOAD_Pos) | (nrf.PWM_DECODER_MODE_RefreshCount << nrf.PWM_DECODER_MODE_Pos))
p.SEQ[0].PTR = nrf.RegValue(uintptr(unsafe.Pointer(&pwmChannelSequence[i]))) p.SEQ[0].PTR.Set(uint32(uintptr(unsafe.Pointer(&pwmChannelSequence[i]))))
p.SEQ[0].CNT = 1 p.SEQ[0].CNT.Set(1)
p.SEQ[0].REFRESH = 1 p.SEQ[0].REFRESH.Set(1)
p.SEQ[0].ENDDELAY = 0 p.SEQ[0].ENDDELAY.Set(0)
p.TASKS_SEQSTART[0] = 1 p.TASKS_SEQSTART[0].Set(1)
break break
} }

Просмотреть файл

@ -19,8 +19,8 @@ func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) {
} }
func (uart UART) setPins(tx, rx uint32) { func (uart UART) setPins(tx, rx uint32) {
nrf.UART0.PSEL.TXD = nrf.RegValue(tx) nrf.UART0.PSEL.TXD.Set(tx)
nrf.UART0.PSEL.RXD = nrf.RegValue(rx) nrf.UART0.PSEL.RXD.Set(rx)
} }
//go:export UARTE0_UART0_IRQHandler //go:export UARTE0_UART0_IRQHandler
@ -29,8 +29,8 @@ func handleUART0() {
} }
func (i2c I2C) setPins(scl, sda uint8) { func (i2c I2C) setPins(scl, sda uint8) {
i2c.Bus.PSEL.SCL = nrf.RegValue(scl) i2c.Bus.PSEL.SCL.Set(uint32(scl))
i2c.Bus.PSEL.SDA = nrf.RegValue(sda) i2c.Bus.PSEL.SDA.Set(uint32(sda))
} }
// SPI // SPI
@ -44,9 +44,9 @@ func (spi SPI) setPins(sck, mosi, miso uint8) {
if miso == 0 { if miso == 0 {
miso = SPI0_MISO_PIN miso = SPI0_MISO_PIN
} }
spi.Bus.PSEL.SCK = nrf.RegValue(sck) spi.Bus.PSEL.SCK.Set(uint32(sck))
spi.Bus.PSEL.MOSI = nrf.RegValue(mosi) spi.Bus.PSEL.MOSI.Set(uint32(mosi))
spi.Bus.PSEL.MISO = nrf.RegValue(miso) spi.Bus.PSEL.MISO.Set(uint32(miso))
} }
// InitADC initializes the registers needed for ADC. // InitADC initializes the registers needed for ADC.
@ -93,53 +93,53 @@ func (a ADC) Get() uint16 {
return 0 return 0
} }
nrf.SAADC.RESOLUTION = nrf.SAADC_RESOLUTION_VAL_12bit nrf.SAADC.RESOLUTION.Set(nrf.SAADC_RESOLUTION_VAL_12bit)
// Enable ADC. // Enable ADC.
nrf.SAADC.ENABLE = (nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos) nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos)
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
nrf.SAADC.CH[i].PSELN = nrf.SAADC_CH_PSELP_PSELP_NC nrf.SAADC.CH[i].PSELN.Set(nrf.SAADC_CH_PSELP_PSELP_NC)
nrf.SAADC.CH[i].PSELP = nrf.SAADC_CH_PSELP_PSELP_NC nrf.SAADC.CH[i].PSELP.Set(nrf.SAADC_CH_PSELP_PSELP_NC)
} }
// Configure ADC. // Configure ADC.
nrf.SAADC.CH[0].CONFIG = ((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESP_Pos) & nrf.SAADC_CH_CONFIG_RESP_Msk) | nrf.SAADC.CH[0].CONFIG.Set(((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESP_Pos) & nrf.SAADC_CH_CONFIG_RESP_Msk) |
((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESN_Pos) & nrf.SAADC_CH_CONFIG_RESN_Msk) | ((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESN_Pos) & nrf.SAADC_CH_CONFIG_RESN_Msk) |
((nrf.SAADC_CH_CONFIG_GAIN_Gain1_5 << nrf.SAADC_CH_CONFIG_GAIN_Pos) & nrf.SAADC_CH_CONFIG_GAIN_Msk) | ((nrf.SAADC_CH_CONFIG_GAIN_Gain1_5 << nrf.SAADC_CH_CONFIG_GAIN_Pos) & nrf.SAADC_CH_CONFIG_GAIN_Msk) |
((nrf.SAADC_CH_CONFIG_REFSEL_Internal << nrf.SAADC_CH_CONFIG_REFSEL_Pos) & nrf.SAADC_CH_CONFIG_REFSEL_Msk) | ((nrf.SAADC_CH_CONFIG_REFSEL_Internal << nrf.SAADC_CH_CONFIG_REFSEL_Pos) & nrf.SAADC_CH_CONFIG_REFSEL_Msk) |
((nrf.SAADC_CH_CONFIG_TACQ_3us << nrf.SAADC_CH_CONFIG_TACQ_Pos) & nrf.SAADC_CH_CONFIG_TACQ_Msk) | ((nrf.SAADC_CH_CONFIG_TACQ_3us << nrf.SAADC_CH_CONFIG_TACQ_Pos) & nrf.SAADC_CH_CONFIG_TACQ_Msk) |
((nrf.SAADC_CH_CONFIG_MODE_SE << nrf.SAADC_CH_CONFIG_MODE_Pos) & nrf.SAADC_CH_CONFIG_MODE_Msk) ((nrf.SAADC_CH_CONFIG_MODE_SE << nrf.SAADC_CH_CONFIG_MODE_Pos) & nrf.SAADC_CH_CONFIG_MODE_Msk))
// Set pin to read. // Set pin to read.
nrf.SAADC.CH[0].PSELN = nrf.RegValue(pwmPin) nrf.SAADC.CH[0].PSELN.Set(pwmPin)
nrf.SAADC.CH[0].PSELP = nrf.RegValue(pwmPin) nrf.SAADC.CH[0].PSELP.Set(pwmPin)
// Destination for sample result. // Destination for sample result.
nrf.SAADC.RESULT.PTR = nrf.RegValue(uintptr(unsafe.Pointer(&value))) nrf.SAADC.RESULT.PTR.Set(uint32(uintptr(unsafe.Pointer(&value))))
nrf.SAADC.RESULT.MAXCNT = 1 // One sample nrf.SAADC.RESULT.MAXCNT.Set(1) // One sample
// Start tasks. // Start tasks.
nrf.SAADC.TASKS_START = 1 nrf.SAADC.TASKS_START.Set(1)
for nrf.SAADC.EVENTS_STARTED == 0 { for nrf.SAADC.EVENTS_STARTED.Get() == 0 {
} }
nrf.SAADC.EVENTS_STARTED = 0x00 nrf.SAADC.EVENTS_STARTED.Set(0x00)
// Start the sample task. // Start the sample task.
nrf.SAADC.TASKS_SAMPLE = 1 nrf.SAADC.TASKS_SAMPLE.Set(1)
// Wait until the sample task is done. // Wait until the sample task is done.
for nrf.SAADC.EVENTS_END == 0 { for nrf.SAADC.EVENTS_END.Get() == 0 {
} }
nrf.SAADC.EVENTS_END = 0x00 nrf.SAADC.EVENTS_END.Set(0x00)
// Stop the ADC // Stop the ADC
nrf.SAADC.TASKS_STOP = 1 nrf.SAADC.TASKS_STOP.Set(1)
for nrf.SAADC.EVENTS_STOPPED == 0 { for nrf.SAADC.EVENTS_STOPPED.Get() == 0 {
} }
nrf.SAADC.EVENTS_STOPPED = 0 nrf.SAADC.EVENTS_STOPPED.Set(0)
// Disable the ADC. // Disable the ADC.
nrf.SAADC.ENABLE = (nrf.SAADC_ENABLE_ENABLE_Disabled << nrf.SAADC_ENABLE_ENABLE_Pos) nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Disabled << nrf.SAADC_ENABLE_ENABLE_Pos)
if value < 0 { if value < 0 {
value = 0 value = 0
@ -174,21 +174,21 @@ func (pwm PWM) Set(value uint16) {
p := pwms[i] p := pwms[i]
p.PSEL.OUT[0] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[0].Set(uint32(pwm.Pin))
p.PSEL.OUT[1] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[1].Set(uint32(pwm.Pin))
p.PSEL.OUT[2] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[2].Set(uint32(pwm.Pin))
p.PSEL.OUT[3] = nrf.RegValue(pwm.Pin) p.PSEL.OUT[3].Set(uint32(pwm.Pin))
p.ENABLE = (nrf.PWM_ENABLE_ENABLE_Enabled << nrf.PWM_ENABLE_ENABLE_Pos) p.ENABLE.Set(nrf.PWM_ENABLE_ENABLE_Enabled << nrf.PWM_ENABLE_ENABLE_Pos)
p.PRESCALER = nrf.PWM_PRESCALER_PRESCALER_DIV_2 p.PRESCALER.Set(nrf.PWM_PRESCALER_PRESCALER_DIV_2)
p.MODE = nrf.PWM_MODE_UPDOWN_Up p.MODE.Set(nrf.PWM_MODE_UPDOWN_Up)
p.COUNTERTOP = 16384 // frequency p.COUNTERTOP.Set(16384) // frequency
p.LOOP = 0 p.LOOP.Set(0)
p.DECODER = (nrf.PWM_DECODER_LOAD_Common << nrf.PWM_DECODER_LOAD_Pos) | (nrf.PWM_DECODER_MODE_RefreshCount << nrf.PWM_DECODER_MODE_Pos) p.DECODER.Set((nrf.PWM_DECODER_LOAD_Common << nrf.PWM_DECODER_LOAD_Pos) | (nrf.PWM_DECODER_MODE_RefreshCount << nrf.PWM_DECODER_MODE_Pos))
p.SEQ[0].PTR = nrf.RegValue(uintptr(unsafe.Pointer(&pwmChannelSequence[i]))) p.SEQ[0].PTR.Set(uint32(uintptr(unsafe.Pointer(&pwmChannelSequence[i]))))
p.SEQ[0].CNT = 1 p.SEQ[0].CNT.Set(1)
p.SEQ[0].REFRESH = 1 p.SEQ[0].REFRESH.Set(1)
p.SEQ[0].ENDDELAY = 0 p.SEQ[0].ENDDELAY.Set(0)
p.TASKS_SEQSTART[0] = 1 p.TASKS_SEQSTART[0].Set(1)
break break
} }

Просмотреть файл

@ -32,16 +32,16 @@ func init() {
func initLFCLK() { func initLFCLK() {
if machine.HasLowFrequencyCrystal { if machine.HasLowFrequencyCrystal {
nrf.CLOCK.LFCLKSRC = nrf.CLOCK_LFCLKSTAT_SRC_Xtal nrf.CLOCK.LFCLKSRC.Set(nrf.CLOCK_LFCLKSTAT_SRC_Xtal)
} }
nrf.CLOCK.TASKS_LFCLKSTART = 1 nrf.CLOCK.TASKS_LFCLKSTART.Set(1)
for nrf.CLOCK.EVENTS_LFCLKSTARTED == 0 { for nrf.CLOCK.EVENTS_LFCLKSTARTED.Get() == 0 {
} }
nrf.CLOCK.EVENTS_LFCLKSTARTED = 0 nrf.CLOCK.EVENTS_LFCLKSTARTED.Set(0)
} }
func initRTC() { func initRTC() {
nrf.RTC1.TASKS_START = 1 nrf.RTC1.TASKS_START.Set(1)
arm.SetPriority(nrf.IRQ_RTC1, 0xc0) // low priority arm.SetPriority(nrf.IRQ_RTC1, 0xc0) // low priority
arm.EnableIRQ(nrf.IRQ_RTC1) arm.EnableIRQ(nrf.IRQ_RTC1)
} }
@ -72,7 +72,7 @@ var (
// overflow the counter, leading to incorrect results. This might be fixed by // overflow the counter, leading to incorrect results. This might be fixed by
// handling the overflow event. // handling the overflow event.
func ticks() timeUnit { func ticks() timeUnit {
rtcCounter := uint32(nrf.RTC1.COUNTER) rtcCounter := uint32(nrf.RTC1.COUNTER.Get())
offset := (rtcCounter - rtcLastCounter) & 0xffffff // change since last measurement offset := (rtcCounter - rtcLastCounter) & 0xffffff // change since last measurement
rtcLastCounter = rtcCounter rtcLastCounter = rtcCounter
timestamp += timeUnit(offset) // TODO: not precise timestamp += timeUnit(offset) // TODO: not precise
@ -85,7 +85,7 @@ type isrFlag bool
var rtc_wakeup isrFlag var rtc_wakeup isrFlag
func rtc_sleep(ticks uint32) { func rtc_sleep(ticks uint32) {
nrf.RTC1.INTENSET = nrf.RTC_INTENSET_COMPARE0 nrf.RTC1.INTENSET.Set(nrf.RTC_INTENSET_COMPARE0)
rtc_wakeup = false rtc_wakeup = false
if ticks == 1 { if ticks == 1 {
// Race condition (even in hardware) at ticks == 1. // Race condition (even in hardware) at ticks == 1.
@ -93,7 +93,7 @@ func rtc_sleep(ticks uint32) {
// describes. // describes.
ticks = 2 ticks = 2
} }
nrf.RTC1.CC[0] = (nrf.RTC1.COUNTER + nrf.RegValue(ticks)) & 0x00ffffff nrf.RTC1.CC[0].Set((nrf.RTC1.COUNTER.Get() + ticks) & 0x00ffffff)
for !rtc_wakeup { for !rtc_wakeup {
arm.Asm("wfi") arm.Asm("wfi")
} }
@ -101,7 +101,7 @@ func rtc_sleep(ticks uint32) {
//go:export RTC1_IRQHandler //go:export RTC1_IRQHandler
func handleRTC1() { func handleRTC1() {
nrf.RTC1.INTENCLR = nrf.RTC_INTENSET_COMPARE0 nrf.RTC1.INTENCLR.Set(nrf.RTC_INTENSET_COMPARE0)
nrf.RTC1.EVENTS_COMPARE[0] = 0 nrf.RTC1.EVENTS_COMPARE[0].Set(0)
rtc_wakeup = true rtc_wakeup = true
} }