Swapped functions ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
bymb13
Posts: 5
Joined: Tue Sep 26, 2017 5:24 pm

Swapped functions ?

Post by bymb13 » Wed Oct 11, 2017 8:19 am

Hi.

I generate a frame on the pin X9/PB6 :

- During 3000 ms, X9 = 1b

- During 500 ms, X9 = 0b
- During 1500 ms, X9 = 1b

- During 500 ms, X9 = 0b
- During 1500 ms, X9 = 1b
...

I tried :

- with pin.value([True|False]) : it's OK

- with pin.[high|low]() : it's OK

- with assembly code asm_[set|clear]_x9() : it's KO !!!

---> The functions pyb.delay(500) and pyb.delay(1500) are swapped during the execution of the code.

Is it an error from me?

Best Regards.

Code: Select all


#*******************************************************************************
# IMPORT ***********************************************************************
#*******************************************************************************

import pyb
import machine
import stm

#*******************************************************************************
# GLOBAL ***********************************************************************
#*******************************************************************************

# pin X9/PB6
g_pin_x9 = pyb.Pin.board.X9
g_pin_x9.init(pyb.Pin.OUT_PP, pull = pyb.Pin.PULL_UP, af = -1)
# standby value = 1b
g_pin_x9.value(True)

#*******************************************************************************
# DEF ******************************************************************-*******
#*******************************************************************************

# -- asm_set_x9 ----------------------------------------------------------------
#
# Description  : pin X9/PB6 = 1b
#
# Parameters   :
#
# Return       :
#
# Notes        :
#

@micropython.asm_thumb
def asm_set_x9() :

   movw(r0, 1 << 6)
   movwt(r1, stm.GPIOB)
   strh(r0, [r1, stm.GPIO_BSRRH])

# -- asm_clear_x9 --------------------------------------------------------------
#
# Description  : pin X9/PB6 = 0b
#
# Parameters   :
#
# Return       :
#
# Notes        :
#

@micropython.asm_thumb
def asm_clear_x9() :

   movw(r0, 1 << 6)
   movwt(r1, stm.GPIOB)
   strh(r0, [r1, stm.GPIO_BSRRL])

#*******************************************************************************
# MAIN *************************************************************************
#*******************************************************************************

# test with pin.value(True) / pin.value(False) : OK
#
#        !  !  !  !  !  !  !  !  !  !  !  !  !  !
#  ......_________    ________    ________
#  ......         |__|        |__|        |....
# in ms:  3000    500  1500   500  1500   500
#

if 0 :
   pyb.delay(3000)

   for i in range(30) :

      # X9/PB6 = False
      g_pin_x9.value(False)

      pyb.delay(500)

      # X9/PB6 = True
      g_pin_x9.value(True)

      pyb.delay(1500)

# test with pin.high() / pin.low() : OK
#
#        !  !  !  !  !  !  !  !  !  !  !  !  !  !
#  ......_________    ________    ________
#  ......         |__|        |__|        |....
# in ms:  3000    500  1500   500  1500   500
#

if 0 :
   pyb.delay(3000)

   for i in range(30) :

      # X9/PB6 = low
      g_pin_x9.low()

      pyb.delay(500)

      # X9/PB6 = high
      g_pin_x9.high()

      pyb.delay(1500)

# test with asm_set_x9() and asm_clear_x9() : KO
#
#        !  !  !  !  !  !  !  !  !  !  !  !  !  !
#  ......_________          __          __
#  ......         |________|  |________|  |....
# in ms:  3000      1500   500  1500   500
#

if 1 :
   pyb.delay(3000)

   for i in range(30) :

      # X9/PB6 = 0b
      asm_clear_x9()

      pyb.delay(500)

      # X9/PB6 = 1b
      asm_set_x9()

      pyb.delay(1500)


User avatar
pythoncoder
Posts: 1964
Joined: Fri Jul 18, 2014 8:01 am

Re: Swapped functions ?

Post by pythoncoder » Wed Oct 11, 2017 9:52 am

The problem is that asm_set_x9() clears the pin down and asm_clear_x9() sets it high.
Peter Hinch

bymb13
Posts: 5
Joined: Tue Sep 26, 2017 5:24 pm

Re: Swapped functions ?

Post by bymb13 » Wed Oct 11, 2017 12:57 pm

Peter, thank you.

I have just understood my error. :oops:

We have :

- the GPIOX_BSRR port bit set/reset register[31:0] (= [ BR15 ... BR0 ! BS15 ... BS0 ]) where :
---> the bits BSn used to set the corresponding bit PXn
---> the bits BRn used to zero the corresponding bit PXn

and :

- GPIO_BSRRL = GPIOx_BSRR[15:0] is the low half-word of GPIOx_BSRR register and not a set of bits used to reset bits
- GPIO_BSRRH = GPIOx_BSRR[31:16] is the high half-word of GPIOx_BSRR register and not a set of bits used for set bits

Best Regards.

Post Reply