low power waiting for uart rx


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)

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

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

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

def test(arg):

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

   if uchar :                     
      if uchar[3] == 0x0D : 

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 ?


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)

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

   if uchar :

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.