Can not initialize STM32L476 UART

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
HollowHead
Posts: 10
Joined: Sun Nov 10, 2019 10:30 pm

Re: Can not initialize STM32L476 UART

Post by HollowHead » Tue Nov 12, 2019 5:38 am

Hello Jimmo

I plugged the NUCLEO into my linux box via the USB mini connector with the ST Link jumpers on and BOOT0 disconnected.

I followed your instructions;

# from wherever you cloned micropython:
cd mpy-cross -easy
make -j -did something
cd ../ports/stm32 -easy
make submodules -did something looks good
make -j BOARD=NUCLEO_L476RG -the screen rolled with error messages for about 20 minutes
make BOARD=NUCLEO_L476RG deploy -it went through for a couple of seconds but shows errors

Hit reset.

No Luck.

Tried again but this time I changed the order. ie changed the config file first then did the make 'stuff'

Looks better but no cigar when it came to deploy, so used window to download the dfu file through the direct usb connection ie PA11 PA12. That upgraded the firmware successfully.

Now plug the NUCLEO back into the Linux box, do a dmesg ;

[24106.404299] usb 6-2: new full-speed USB device number 19 using uhci_hcd
[24106.601101] usb 6-2: New USB device found, idVendor=f055, idProduct=9800
[24106.601104] usb 6-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[24106.601105] usb 6-2: Product: Pyboard Virtual Comm Port in FS Mode
[24106.601106] usb 6-2: Manufacturer: MicroPython
[24106.601108] usb 6-2: SerialNumber: 205737A3424B
[24106.604169] usb-storage 6-2:1.0: USB Mass Storage device detected
[24106.604468] scsi host15: usb-storage 6-2:1.0
[24106.604734] cdc_acm 6-2:1.1: ttyACM0: USB ACM device
[24107.637243] scsi 15:0:0:0: Direct-Access MicroPy pyboard Flash 1.00 PQ: 0 ANSI: 2
[24107.638168] sd 15:0:0:0: Attached scsi generic sg8 type 0
[24107.642139] sd 15:0:0:0: [sdg] 1280 512-byte logical blocks: (655 kB/640 KiB)
[24107.645157] sd 15:0:0:0: [sdg] Write Protect is off
[24107.645160] sd 15:0:0:0: [sdg] Mode Sense: 03 00 00 00
[24107.648123] sd 15:0:0:0: [sdg] No Caching mode page found
[24107.648129] sd 15:0:0:0: [sdg] Assuming drive cache: write through
[24107.669242] sdg: sdg1
[24107.688202] sd 15:0:0:0: [sdg] Attached SCSI removable disk
[24107.933115] FAT-fs (sdg1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
[24108.086606] FAT-fs (sdg1): error, invalid access to FAT (entry 0x00003a0a)
[24108.086610] FAT-fs (sdg1): Filesystem has been set read-only

screen /dev/ttyACM0 sort of works, it starts then it dropped out.


I will have to get back to this later, really have to go now.

Thanks again

User avatar
jimmo
Posts: 885
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Can not initialize STM32L476 UART

Post by jimmo » Tue Nov 12, 2019 6:19 am

HollowHead wrote:
Tue Nov 12, 2019 5:38 am
make -j BOARD=NUCLEO_L476RG -the screen rolled with error messages for about 20 minutes
This shouldn't have errors... mostly should be lots of "CC filename.c" or "GEN filename.h" ending in "Create build-NUCLEO_L476RG/firmware.dfu"

Can you post the errors to a https://gist.github.com/ (or equivalent) so I can take a look?

HollowHead
Posts: 10
Joined: Sun Nov 10, 2019 10:30 pm

Re: Can not initialize STM32L476 UART

Post by HollowHead » Tue Nov 12, 2019 3:01 pm

Hi Jimmo,

"make -j BOARD=NUCLEO_L476RG -the screen rolled with error messages for about 20 minutes "

Was my error, I was looking at the # as a remark but that is wrong when it is a define in c, some sort of directive for the compiler.
Once I realised this I changed the lines as you suggested ie

Code: Select all

#define MICROPY_HW_UART2_TX (pin_A2)
#define MICROPY_HW_UART2_RX (pin_A3)

to

Code: Select all

#define MICROPY_HW_UART1_TX (pin_B6)
#define MICROPY_HW_UART1_RX (pin_B7)
#define MICROPY_HW_UART2_TX (pin_A2)
#define MICROPY_HW_UART2_RX (pin_A3)
#define MICROPY_HW_UART3_TX (pin_C4)
#define MICROPY_HW_UART3_RX (pin_C5)

Not sure how you got those pretty borders!

Then did

Code: Select all

# from wherever you cloned micropython:
cd mpy-cross
make -j
cd ../ports/stm32
make submodules
make -j BOARD=NUCLEO_L476RG
make BOARD=NUCLEO_L476RG deploy

which went smoothly and quickly.

There is a file named firmware.dfu which I copied to my windows machine and used it to upload the dfu file through the direct usb connection ie PA11 PA12 to the NUCLEO. That upgraded the firmware successfully..

Next plug the same USB (PA11 PA12) to a Linux box and check to see what is returned from dmesg (I think device message).
This is last of the dmesg log that relates to the NUCLEO;

[ 3248.205902] usb 8-2: new full-speed USB device number 2 using uhci_hcd
[ 3248.400961] usb 8-2: New USB device found, idVendor=f055, idProduct=9800
[ 3248.400965] usb 8-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3248.400968] usb 8-2: Product: Pyboard Virtual Comm Port in FS Mode
[ 3248.400970] usb 8-2: Manufacturer: MicroPython
[ 3248.400972] usb 8-2: SerialNumber: 205737A3424B
[ 3248.404083] usb-storage 8-2:1.0: USB Mass Storage device detected
[ 3248.404522] scsi host15: usb-storage 8-2:1.0
[ 3248.453307] cdc_acm 8-2:1.1: ttyACM0: USB ACM device
[ 3248.455004] usbcore: registered new interface driver cdc_acm
[ 3248.455004] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters
[ 3249.411175] scsi 15:0:0:0: Direct-Access MicroPy pyboard Flash 1.00 PQ: 0 ANSI: 2
[ 3249.412273] sd 15:0:0:0: Attached scsi generic sg8 type 0
[ 3249.417055] sd 15:0:0:0: [sdg] 1280 512-byte logical blocks: (655 kB/640 KiB)
[ 3249.420122] sd 15:0:0:0: [sdg] Write Protect is off
[ 3249.420126] sd 15:0:0:0: [sdg] Mode Sense: 03 00 00 00
[ 3249.423117] sd 15:0:0:0: [sdg] No Caching mode page found
[ 3249.423124] sd 15:0:0:0: [sdg] Assuming drive cache: write through
[ 3249.444128] sdg: sdg1
[ 3249.463181] sd 15:0:0:0: [sdg] Attached SCSI removable disk
[ 3249.791182] FAT-fs (sdg1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
[ 3250.641965] FAT-fs (sdg1): error, invalid access to FAT (entry 0x00003a0a)
[ 3250.641970] FAT-fs (sdg1): Filesystem has been set read-only
[ 4129.602155] perf: interrupt took too long (2501 > 2500), lowering kernel.perf_event_max_sample_rate to 79750

Now there are errors to do with the volume and suggest to run fsck so I did but ;

fsck from util-linux 2.29.2
e2fsck 1.43.4 (31-Jan-2017)
/dev/sda2 is mounted.
e2fsck: Cannot continue, aborting.

Not sure what this means but sdg1 oh ok so I tried

fsck /dev/sdg1
fsck from util-linux 2.29.2
fsck.fat 4.1 (2017-01-24)
0x25: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
1) Remove dirty bit
2) No action
? 1
/0F001001.0BD
Bad short file name (0F001001.0BD).
1) Drop file
2) Rename file
3) Auto-rename
4) Keep it
?

I am not sure if I should drop this OBD file, maybe Microsoft Office Binder

So I dropped it but then it keeps on going on about 'Bad short file names'. I must of 'dropped' 20 then gave up, most likely wreck it and will need to upload the firmware.dfu file again. Yep no good. Re flashing the dfu in windows using DfuSe Demo.

Still have issues with the mass storage side of things. My file manager complains about the 'drive'. I can not write a text file to it, the manager tell me that the drive is 'read only'.

Is there a way to erase everything off the STM32 and start again??
Seems there is, use something called ST LINK, had to come through the standard mini USB and had both jumpers on to enable it.
The software found the NUCLEO. I then erased it. Then closed that program and then re flashed the firmware through the direct USB connection. Plugged it into the Linux box. We have less errors. The file manager seems happy.

I can screen /dev/ttyACM0 and bring up the REPL but could not exit. Maybe the screen command has a way of dropping out, I will have to find out.

'm a bit confused what you're trying to do...?
So the thought was to upload the firmware (with BOOT0 tied high), program ie REPL the STM via the direct usb connection ie PA11 PA12. These pins would be dedicated for this purpose alone.

Then have a UART for some feedback via a convertor ie FTDI, CH340 UART to USB.

I am new to most of this, especially python, in fact I have virtually no experience with Python.
So writing a program and getting feedback via the UART makes sense to me.
Say we connect an I2C thermometer to NUCLEO, it would be nice to write a script that communicates with the sensor, stores the readings then relay those readings to the UART. This would prove if it worked or not. A sort of debugging.

Then I am also hoping to use another UART again to communicate with another project.

Sorry for the long winded blow by blow account but I am trying to be clear.

Thanks again.

User avatar
jimmo
Posts: 885
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Can not initialize STM32L476 UART

Post by jimmo » Tue Nov 12, 2019 10:48 pm

OK, so it sounds like you're able to build firmware (on your Linux PC). I think it's getting confusing trying to make the USB work at the same time as everything else, so let's just go back to the start and use the ST-Link interface.

The ST-Link does two things:
- Provides a programming/debugging (SWD) interface.
- Provides a UART to USB adaptor (i.e. /dev/ttyACM0)

On Linux, you can install https://github.com/texane/stlink (on Ubuntu, "sudo apt install stlink-tools")

If you put the jumpers back to the default -- so the ST-Link is connected for SWD and UART (on PA2/PA3), then on Linux:

- Install stlink

- Run "st-info --probe"

This should detect the st-link on the board.

Then "st-flash erase" to wipe the flash (this will fix your filesystem issues)

- Run "make BOARD=NUCLEO_L476RG deploy-stlink"

Note the deploy-stlink, which will use stlink rather than DFU to program the chip.

Now you should be back where you started, and can use "screen /dev/ttyACM0 11500" to connect to the REPL over the st-link USB->UART.
However, your original question of using UARTs other than 2 should be solved.

(If any of the st-info / st-flash commands aren't working, try using sudo, but I'm pretty sure on Ubuntu it installs the right udev rules to set the access permissions. You might need to run "sudo sudo udevadm control --reload-rules" then re-connect the board, or just reboot).

Then once that's working we can take a look at the direct USB interface on the L4.

User avatar
jimmo
Posts: 885
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Can not initialize STM32L476 UART

Post by jimmo » Tue Nov 12, 2019 10:49 pm

Oh, and to get the formatted code boxes, select the text and press the </> button on the toolbar. (Or manually type the [ code ] block around it).

User avatar
jimmo
Posts: 885
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Can not initialize STM32L476 UART

Post by jimmo » Tue Nov 12, 2019 10:58 pm

HollowHead wrote:
Tue Nov 12, 2019 3:01 pm
I can screen /dev/ttyACM0 and bring up the REPL but could not exit. Maybe the screen command has a way of dropping out, I will have to find out.
Ctrl-A K Y

(Ctrl-A is how you send commands to screen, K means exit, Y to confirm)

I recommend using miniterm.py (which you'll have if you have Python's serial package installed -- sudo apt install python-serial)

Code: Select all

miniterm.py /dev/ttyACM0 115200
Then it's just Ctrl-] to exit. And it doesn't take over the whole screen.

HollowHead
Posts: 10
Joined: Sun Nov 10, 2019 10:30 pm

Re: Can not initialize STM32L476 UART

Post by HollowHead » Wed Nov 13, 2019 1:32 am

Tried all those and it must just be my luck, as usual, they do not work.

Start RETL as before (screen /dev/ttyACM0) starts but with errors;

MicroPython v1.11-575-g258b14783-dirty on 2019-11-12; NUCLEO-L476RG with STM32L476RG
Type "help()" for more information.
>>> >>> AT
Traceback (most recent call last):
<stdin><module>
NameError: >>>
File "", line 1, in <module>
NameError: name 'AT' isn't defined
>>> ~x~x~
Traceback (most recent call last):
File "<stdin>", line 1
SyntaxError: invalid syntax
>>>

Try help() Works shows these commands;

Control commands:
CTRL-A -- on a blank line, enter raw REPL mode
CTRL-B -- on a blank line, enter normal REPL mode
CTRL-C -- interrupt a running program
CTRL-D -- on a blank line, do a soft reset of the board
CTRL-E -- on a blank line, enter paste mode

Tried CONTROL K . Tried CONTROL A K Y and other variations but it does not drop out/exit.
I have force close the terminal.


Had issues with minicom.py I found it but do not have permission to use it. Changed the permissions but still a problem

/usr/lib/python3/dist-packages/serial/tools$ ./miniterm.py /dev/ttyACM0 115200
Traceback (most recent call last):
File "./miniterm.py", line 15, in <module>
import serial
ImportError: No module named serial

I am starting to think that this whole exercise is a waste of time, too many dependencies and compiling etc. I have spent many days to get to this point and have not even started to program. No doubt there are going to be many more days or weeks to get anything decent to work. I had a lot more success programming a PIC microcontroller in assembly. I wanted to use these more powerful st332 but it has been hell just to do anything other than flash an LED. There must be a better way, please point me to it if there is.

I thank you again for your help.

User avatar
jimmo
Posts: 885
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Can not initialize STM32L476 UART

Post by jimmo » Wed Nov 13, 2019 2:22 am

HollowHead wrote:
Wed Nov 13, 2019 1:32 am
Start RETL as before (screen /dev/ttyACM0) starts but with errors;
Did you type "AT" yourself? Otherwise it sounds like your computer is trying to connect to the device as a modem. Ubuntu has a thing called modemmanager which you should disable (or uninstall):

e.g. viewtopic.php?f=12&t=7086&p=40352&hilit ... ger#p40352

(This comes up a lot...it's super annoying...I have no idea why Ubuntu still has this by default. The underlying reason is that the way these "virtual" USB serial ports work is that they essentially pretend to be modems).
HollowHead wrote:
Wed Nov 13, 2019 1:32 am
Tried CONTROL K . Tried CONTROL A K Y and other variations but it does not drop out/exit.
It's Ctrl-a, then just k, then just y.
HollowHead wrote:
Wed Nov 13, 2019 1:32 am
/usr/lib/python3/dist-packages/serial/tools$ ./miniterm.py /dev/ttyACM0 115200
You should be able to just run miniterm.py from anywhere (i.e. it should be in your $PATH after installing python-serial). You're not supposed to run it directly out of dist-packages.
HollowHead wrote:
Wed Nov 13, 2019 1:32 am
I have spent many days to get to this point and have not even started to program. No doubt there are going to be many more days or weeks to get anything decent to work. I had a lot more success programming a PIC microcontroller in assembly. I wanted to use these more powerful st332 but it has been hell just to do anything other than flash an LED. There must be a better way, please point me to it if there is.
I'm sorry this has been a bad experience, but other than being fairly unlucky that the specific board you chose not having its UARTs defined correctly this is quite different to (what I hope is) most people's experience of MicroPython. A huge majority of people use MicroPython without ever looking at (or being aware of) the underlying C firmware.

The ST nucleo/disco boards do make this tricky because they assume you're working via the embedded st-link, etc (and confusingly on many boards like yours they don't include a USB header for the "real" USB port).

It's a lot easier of course on a board with really good MicroPython support (e.g. Pyboard) because you can just use the USB filesystem directly to copy code.

STM32 isn't a particularly easy environment to get started with. It's a long way from C on AVR, let alone assembly on a PIC.

HollowHead
Posts: 10
Joined: Sun Nov 10, 2019 10:30 pm

Re: Can not initialize STM32L476 UART

Post by HollowHead » Wed Nov 13, 2019 1:46 pm

Hi Jimmo,

Thanks again, that combination is one I did not try ie
It's Ctrl-a, then just k, then just y.
It Worked!!!!
Did you type "AT" yourself? Otherwise it sounds like your computer is trying to connect to the device as a modem. Ubuntu has a thing called modemmanager which you should disable (or uninstall):
No I didn't. I am using Debian which I understand are very similar. I will look into mode manager in a few days as I have to head out of town till the beginning of next week.

I will play some more when I get back and look forward to learning python!

Thanks again for your help and support.

Post Reply