low power waiting for uart rx

Hello,

I would like to optimize consumption during the time the cpu wait the uart receipt.
I wrote below the different way and names i found on internet documentation about :

STM32 ------------ machine ---------- pyb --------------- time
sleep ---------------- idle ------------ wfi ---------- time.sleep(time_ms)
stop --------------- sleep ---------- stop
standby --------- deepsleep ----- standby

STMicroelectronics answered me it is possible to wake up from stop mode with uart here :

altough i dont know if i can get the first uart receipt data.

I connected Rx of uart 3 with PB3 to get an external interruption on uart receipt, then I tried this code with different ways below :

import sensor,image,pyb,time, machine
red_led   = pyb.LED(1)
red_led.off()

UARTSIZE = 8
uart=pyb.UART(3,14400)
uart.init(14400, bits=8, parity=None, stop=1, timeout_char=20) #, timeout_char=20

def readUART():
   if uart.any():
      uchar=uart.read(UARTSIZE)
      if len(uchar) == UARTSIZE :
         return uchar
   return

rtc = pyb.RTC()
rtc.datetime((2014, 5, 1, 4, 13, 0, 0, 0))
#rtc.wakeup(5)

def test(arg):
   #extint.disable()
   pass

extint = pyb.ExtInt('P3', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, test)

while(True):
   time.sleep(10)
   #rtc.wakeup(20,test)
   #extint.enable()
   #machine.idle() 
   #extint.enable()
   #pyb.stop()
   #pyb.wfi()
   uchar=readUART()
   if uchar :                     
      #red_led.toggle()
      if uchar[3] == 0x0D : 
          red_led.toggle()

Unfortunately, i dont get the full uart rx if i stop the cpu.
The time.sleep(10) is the best way to have the lowest consumption.

Am i right ?

Thanks,

Ibrahim can you help out here?

It should wake up on any interrupt (including RXNE) you don’t need to configure UART pins as EXTI. Please remove that and all the comments and RTC code and try it again. Also I don’t understand this part: “i dont get the full uart rx if i stop the cpu.” ?

Here is the simplified code which works without activating the sleep mode

import pyb, machine

red_led = pyb.LED(1)
red_led.off()

uart = pyb.UART(3,14400)
uart.init(14400, bits=8, parity=None, stop=1, timeout_char=20)

while(True):
   #machine.sleep()
   uchar=uart.read(1)
   if uchar :
      uart.writechar(uchar[0])
      red_led.toggle()

If the sleep is activated, there is no answer.

About : Also I don’t understand this part: “i dont get the full uart rx if i stop the cpu.” ?
I receive a uart frame (8 bytes), and when i wake up with the external interrupt, i dont get the first data when i use sleep mode.

Actually with the clock disabled UART won’t generate the interrupt (I think this should work on the H7 with LPUART). You could either use an external GPIO as EXTI (a different pin other than UART pins), or use pyb.wfi(). pyb.wfi will be interrupted by UART or SYSTICK.