Skip to content

Commit be491ab

Browse files
deadprogramaykevl
authored andcommitted
machine/stm32: use HasBits() method to simplify bit comparisons
Signed-off-by: Ron Evans <[email protected]>
1 parent 31189de commit be491ab

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

src/machine/machine_stm32f103xx.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (uart UART) SetBaudRate(br uint32) {
157157
func (uart UART) WriteByte(c byte) error {
158158
stm32.USART1.DR.Set(uint32(c))
159159

160-
for (stm32.USART1.SR.Get() & stm32.USART_SR_TXE) == 0 {
160+
for !stm32.USART1.SR.HasBits(stm32.USART_SR_TXE) {
161161
}
162162
return nil
163163
}
@@ -265,15 +265,15 @@ func (spi SPI) Transfer(w byte) (byte, error) {
265265
spi.Bus.DR.Set(uint32(w))
266266

267267
// Wait until transmit complete
268-
for (spi.Bus.SR.Get() & stm32.SPI_SR_TXE) == 0 {
268+
for !spi.Bus.SR.HasBits(stm32.SPI_SR_TXE) {
269269
}
270270

271271
// Wait until receive complete
272-
for (spi.Bus.SR.Get() & stm32.SPI_SR_RXNE) == 0 {
272+
for !spi.Bus.SR.HasBits(stm32.SPI_SR_RXNE) {
273273
}
274274

275275
// Wait until SPI is not busy
276-
for (spi.Bus.SR.Get() & stm32.SPI_SR_BSY) > 0 {
276+
for spi.Bus.SR.HasBits(stm32.SPI_SR_BSY) {
277277
}
278278

279279
// Return received data from SPI data register
@@ -439,7 +439,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
439439

440440
// clear timeout here
441441
timeout := i2cTimeout
442-
for i2c.Bus.SR2.Get()&(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) == 0 {
442+
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) {
443443
timeout--
444444
if timeout == 0 {
445445
return errors.New("I2C timeout on read clear address")
@@ -450,7 +450,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
450450
i2c.Bus.CR1.SetBits(stm32.I2C_CR1_STOP)
451451

452452
timeout = i2cTimeout
453-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_RxNE) == 0 {
453+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_RxNE) {
454454
timeout--
455455
if timeout == 0 {
456456
return errors.New("I2C timeout on read 1 byte")
@@ -478,7 +478,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
478478

479479
// clear address here
480480
timeout := i2cTimeout
481-
for i2c.Bus.SR2.Get()&(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) == 0 {
481+
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) {
482482
timeout--
483483
if timeout == 0 {
484484
return errors.New("I2C timeout on read clear address")
@@ -490,7 +490,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
490490

491491
// wait for btf. we need a longer timeout here than normal.
492492
timeout = 1000
493-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_BTF) == 0 {
493+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
494494
timeout--
495495
if timeout == 0 {
496496
return errors.New("I2C timeout on read 2 bytes")
@@ -524,7 +524,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
524524

525525
// clear address here
526526
timeout := i2cTimeout
527-
for i2c.Bus.SR2.Get()&(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) == 0 {
527+
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) {
528528
timeout--
529529
if timeout == 0 {
530530
return errors.New("I2C timeout on read clear address")
@@ -536,7 +536,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
536536

537537
// wait for btf. we need a longer timeout here than normal.
538538
timeout = 1000
539-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_BTF) == 0 {
539+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
540540
timeout--
541541
if timeout == 0 {
542542
println("I2C timeout on read 3 bytes")
@@ -551,7 +551,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
551551
r[0] = byte(i2c.Bus.DR.Get())
552552

553553
timeout = 1000
554-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_BTF) == 0 {
554+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
555555
timeout--
556556
if timeout == 0 {
557557
return errors.New("I2C timeout on read 3 bytes")
@@ -579,7 +579,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
579579

580580
// clear address here
581581
timeout := i2cTimeout
582-
for i2c.Bus.SR2.Get()&(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) == 0 {
582+
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY) {
583583
timeout--
584584
if timeout == 0 {
585585
return errors.New("I2C timeout on read clear address")
@@ -592,7 +592,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
592592

593593
// wait for btf. we need a longer timeout here than normal.
594594
timeout = 1000
595-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_BTF) == 0 {
595+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
596596
timeout--
597597
if timeout == 0 {
598598
println("I2C timeout on read 3 bytes")
@@ -606,7 +606,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
606606

607607
// wait for btf. we need a longer timeout here than normal.
608608
timeout = 1000
609-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_BTF) == 0 {
609+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
610610
timeout--
611611
if timeout == 0 {
612612
return errors.New("I2C timeout on read more than 3 bytes")
@@ -626,7 +626,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
626626
r[len(r)-2] = byte(i2c.Bus.DR.Get())
627627

628628
timeout = i2cTimeout
629-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_RxNE) == 0 {
629+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_RxNE) {
630630
timeout--
631631
if timeout == 0 {
632632
return errors.New("I2C timeout on read last byte of more than 3")
@@ -650,7 +650,7 @@ const i2cTimeout = 500
650650
func (i2c I2C) signalStart() error {
651651
// Wait until I2C is not busy
652652
timeout := i2cTimeout
653-
for (i2c.Bus.SR2.Get() & stm32.I2C_SR2_BUSY) > 0 {
653+
for i2c.Bus.SR2.HasBits(stm32.I2C_SR2_BUSY) {
654654
timeout--
655655
if timeout == 0 {
656656
return errors.New("I2C busy on start")
@@ -665,7 +665,7 @@ func (i2c I2C) signalStart() error {
665665

666666
// Wait for I2C EV5 aka SB flag.
667667
timeout = i2cTimeout
668-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_SB) == 0 {
668+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_SB) {
669669
timeout--
670670
if timeout == 0 {
671671
return errors.New("I2C timeout on start")
@@ -688,7 +688,7 @@ func (i2c I2C) signalStop() error {
688688
func (i2c I2C) waitForStop() error {
689689
// Wait until I2C is stopped
690690
timeout := i2cTimeout
691-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_STOPF) > 0 {
691+
for i2c.Bus.SR1.HasBits(stm32.I2C_SR1_STOPF) {
692692
timeout--
693693
if timeout == 0 {
694694
println("I2C timeout on wait for stop signal")
@@ -713,23 +713,23 @@ func (i2c I2C) sendAddress(address uint8, write bool) error {
713713
timeout := i2cTimeout
714714
if write {
715715
// EV6 which is ADDR flag.
716-
for i2c.Bus.SR1.Get()&stm32.I2C_SR1_ADDR == 0 {
716+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_ADDR) {
717717
timeout--
718718
if timeout == 0 {
719719
return errors.New("I2C timeout on send write address")
720720
}
721721
}
722722

723723
timeout = i2cTimeout
724-
for i2c.Bus.SR2.Get()&(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY|stm32.I2C_SR2_TRA) == 0 {
724+
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL|stm32.I2C_SR2_BUSY|stm32.I2C_SR2_TRA) {
725725
timeout--
726726
if timeout == 0 {
727727
return errors.New("I2C timeout on send write address")
728728
}
729729
}
730730
} else {
731731
// I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED which is ADDR flag.
732-
for (i2c.Bus.SR1.Get() & stm32.I2C_SR1_ADDR) == 0 {
732+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_ADDR) {
733733
timeout--
734734
if timeout == 0 {
735735
return errors.New("I2C timeout on send read address")
@@ -749,7 +749,7 @@ func (i2c I2C) WriteByte(data byte) error {
749749
// output on the bus.
750750
// I2C_EVENT_MASTER_BYTE_TRANSMITTED is TXE flag.
751751
timeout := i2cTimeout
752-
for i2c.Bus.SR1.Get()&stm32.I2C_SR1_TxE == 0 {
752+
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_TxE) {
753753
timeout--
754754
if timeout == 0 {
755755
return errors.New("I2C timeout on write")

src/machine/machine_stm32f407.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (uart UART) Configure(config UARTConfig) {
209209
func (uart UART) WriteByte(c byte) error {
210210
stm32.USART2.DR.Set(uint32(c))
211211

212-
for (stm32.USART2.SR.Get() & stm32.USART_SR_TXE) == 0 {
212+
for !stm32.USART2.SR.HasBits(stm32.USART_SR_TXE) {
213213
}
214214
return nil
215215
}

src/runtime/runtime_stm32f103xx.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ func initCLK() {
2727
stm32.RCC.CR.SetBits(stm32.RCC_CR_HSEON) // enable HSE clock
2828

2929
// wait for the HSEREADY flag
30-
for (stm32.RCC.CR.Get() & stm32.RCC_CR_HSERDY) == 0 {
30+
for !stm32.RCC.CR.HasBits(stm32.RCC_CR_HSERDY) {
3131
}
3232

3333
stm32.RCC.CR.SetBits(stm32.RCC_CR_HSION) // enable HSI clock
3434

3535
// wait for the HSIREADY flag
36-
for (stm32.RCC.CR.Get() & stm32.RCC_CR_HSIRDY) == 0 {
36+
for !stm32.RCC.CR.HasBits(stm32.RCC_CR_HSIRDY) {
3737
}
3838

3939
stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PLLSRC) // set PLL source to HSE
4040
stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PLLMUL_9) // multiply by 9
4141
stm32.RCC.CR.SetBits(stm32.RCC_CR_PLLON) // enable the PLL
4242

4343
// wait for the PLLRDY flag
44-
for (stm32.RCC.CR.Get() & stm32.RCC_CR_PLLRDY) == 0 {
44+
for !stm32.RCC.CR.HasBits(stm32.RCC_CR_PLLRDY) {
4545
}
4646

4747
stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_SW_PLL) // set clock source to pll
4848

4949
// wait for PLL to be CLK
50-
for (stm32.RCC.CFGR.Get() & stm32.RCC_CFGR_SWS_PLL) == 0 {
50+
for !stm32.RCC.CFGR.HasBits(stm32.RCC_CFGR_SWS_PLL) {
5151
}
5252
}
5353

@@ -74,7 +74,7 @@ func initRTC() {
7474
stm32.RCC.BDCR.SetBits(stm32.RCC_BDCR_LSEON)
7575

7676
// wait until LSE is ready
77-
for stm32.RCC.BDCR.Get()&stm32.RCC_BDCR_LSERDY == 0 {
77+
for !stm32.RCC.BDCR.HasBits(stm32.RCC_BDCR_LSERDY) {
7878
}
7979

8080
// Select LSE
@@ -95,7 +95,7 @@ func initRTC() {
9595
stm32.RTC.CRL.ClearBits(stm32.RTC_CRL_RSF)
9696

9797
// Wait till flag is set
98-
for stm32.RTC.CRL.Get()&stm32.RTC_CRL_RSF == 0 {
98+
for !stm32.RTC.CRL.HasBits(stm32.RTC_CRL_RSF) {
9999
}
100100
}
101101

@@ -184,7 +184,7 @@ func timerSleep(ticks uint32) {
184184

185185
//go:export TIM3_IRQHandler
186186
func handleTIM3() {
187-
if (stm32.TIM3.SR.Get() & stm32.TIM_SR_UIF) > 0 {
187+
if stm32.TIM3.SR.HasBits(stm32.TIM_SR_UIF) {
188188
// Disable the timer.
189189
stm32.TIM3.CR1.ClearBits(stm32.TIM_CR1_CEN)
190190

src/runtime/runtime_stm32f407.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func initCLK() {
4343
// Reset clock registers
4444
// Set HSION
4545
stm32.RCC.CR.SetBits(stm32.RCC_CR_HSION)
46-
for (stm32.RCC.CR.Get() & stm32.RCC_CR_HSIRDY) == 0 {
46+
for !stm32.RCC.CR.HasBits(stm32.RCC_CR_HSIRDY) {
4747
}
4848

4949
// Reset CFGR
@@ -66,11 +66,11 @@ func initCLK() {
6666
// Wait till HSE is ready and if timeout is reached exit
6767
for {
6868
startupCounter++
69-
if (stm32.RCC.CR.Get()&stm32.RCC_CR_HSERDY != 0) || (startupCounter == HSE_STARTUP_TIMEOUT) {
69+
if stm32.RCC.CR.HasBits(stm32.RCC_CR_HSERDY) || (startupCounter == HSE_STARTUP_TIMEOUT) {
7070
break
7171
}
7272
}
73-
if (stm32.RCC.CR.Get() & stm32.RCC_CR_HSERDY) != 0 {
73+
if stm32.RCC.CR.HasBits(stm32.RCC_CR_HSERDY) {
7474
// Enable high performance mode, System frequency up to 168MHz
7575
stm32.RCC.APB1ENR.SetBits(stm32.RCC_APB1ENR_PWREN)
7676
stm32.PWR.CR.SetBits(0x4000) // PWR_CR_VOS
@@ -187,7 +187,7 @@ func timerSleep(ticks uint32) {
187187

188188
//go:export TIM3_IRQHandler
189189
func handleTIM3() {
190-
if (stm32.TIM3.SR.Get() & stm32.TIM_SR_UIF) > 0 {
190+
if stm32.TIM3.SR.HasBits(stm32.TIM_SR_UIF) {
191191
// Disable the timer.
192192
stm32.TIM3.CR1.ClearBits(stm32.TIM_CR1_CEN)
193193

@@ -201,7 +201,7 @@ func handleTIM3() {
201201

202202
//go:export TIM7_IRQHandler
203203
func handleTIM7() {
204-
if (stm32.TIM7.SR.Get() & stm32.TIM_SR_UIF) > 0 {
204+
if stm32.TIM7.SR.HasBits(stm32.TIM_SR_UIF) {
205205
// clear the update flag
206206
stm32.TIM7.SR.ClearBits(stm32.TIM_SR_UIF)
207207
tickCount++

0 commit comments

Comments
 (0)