2023年4月24日 星期一

STM32F103 開發板燒錄

最近看到 QMK 支援 PS/2 界面滑鼠的說明,可以支援 stm32 開發板。相對於 Teensy 2.0 開發板在淘寶要賣 50RMB,STM32F103C8T6 一塊售價不到 15RMB,stm32 開發板實在便宜多了。Teensy 2.0 使用 8bit CPU,I/O 只有 25 個。STM32 則是 32bit CPU,I/O 有32個以上。無奈 QMK 對 AVR 的 CPU 支援較完整,一直到 Aug 6, 2021,在 feature_ps2_mouse.md 才加入 Interrupt Version (ARM chibios) 的說明。即使如此,加上 trapoint 的設定後,編譯時還是會出錯,要對原始碼做一些修正才能成功編譯。

先前已有買了兩片 STM32F103 開發板,趕快把它翻出來。當時也買了 FTDI 燒錄器,其實就是 USB 轉 RS232 的小裝置。Google 到不少介紹透過 UART 燒錄 STM32F103 開發板的資訊,就照著做。

先到 ST 的網站註冊下載 STM32 Flash loader demonstrator。安裝後,怎麼都連不上,出現無法辨識的裝置的訊息。手邊有兩塊板子,一塊是較便宜國產芯片,一塊是貴一倍的原廠芯片,都一樣連不上。

爬文看到中國網友建議使用 FlyMCU。很容易就找到程式下載,不需安裝就可使用。一樣連不上。有一些貼文說明要用 CTR/RTS 控制 reset 和 boot0,照著接線,還是 connection fail。後來再試,會出現 com port 被佔用的情況。最後發現這程式會自動執行,還不知道它做了什麼手腳,無法清除。

心得是,中國的軟體最後別試,它會像附骨之蛆,擺脫不掉。還好是使用VM測試,只好把整個 VMDK 用備份檔蓋掉。

再看一下各網拍的介紹,都說要用 ST-LINK 來燒錄。找了蝦皮台灣出貨的賣家,一個不到 100元,看評論說買兩個一個不能用,就多買一個吧。週日下單,拖到週一白天還沒出貨,最後在半夜寄出。週一白天忍不住想知道結果,又在蝦皮找到光華商場有現貨的賣家,下班後直接跑去買,一個 220 元。門巿經營的成本,還在可接受的範圍。

回家立刻到 ST 網站下載 STM32 ST-LINK utility。安裝後執行,立刻成功連上開發板,並成功燒錄 DFU 程式。將 boot0 切換到執行狀態,插上電腦就出現可辨識的裝置了。



很容易就可以把 ST-LINK 的電路板拔出來,其實它也是一個 F103CBT6 芯片。

猜測可能是後來出的 STM32 芯片已經不包含 UART 燒錄的功能。ST 在 STM-FLASHER 和 ST-LINK 的下載頁面都有 (replaced by STM32CubeProgrammer) 的提示。下回再用 STM32CubeProgrammer 測測看,它是跨平台的,可以在 Linux 下執行。



2023年4月6日 星期四

MKS MONSTER8 燒錄 firmware

 參考 跟着思兼学习Klipper(21)新一代全能选手: Octopus Max EZ 简要使用记录,github: makerbase-mks / MKS-Monster8

------------------------
$ cd ~/klipper
$ make flash FLASH_DEVICE=/dev/serial/by-id/usb-STMicroelectronics_MARLIN_STM32F407VGT6_CCM_CDC_in_FS_Mode_207F366F4847-if00 

  Flashing out/klipper.bin to /dev/serial/by-id/usb-STMicroelectronics_MARLIN_STM32F407VGT6_CCM_CDC_in_FS_Mode_207F366F4847-if00
Entering bootloader on /dev/serial/by-id/usb-STMicroelectronics_MARLIN_STM32F407VGT6_CCM_CDC_in_FS_Mode_207F366F4847-if00
Device reconnect on /sys/devices/platform/ff5d0000.usb/usb3/3-1/3-1:1.0
sudo dfu-util -p 3-1 -R -a 0 -s 0x800c000:leave -D out/klipper.bin
[sudo] password for mks: 
dfu-util 0.9
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/
dfu-util: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
dfu-util: No DFU capable USB device available
Failed to flash to /dev/serial/by-id/usb-STMicroelectronics_MARLIN_STM32F407VGT6_CCM_CDC_in_FS_Mode_207F366F4847-if00: Error running dfu-util
If the device is already in bootloader mode it can be flashed with the
following command:
  make flash FLASH_DEVICE=0483:df11
  OR
  make flash FLASH_DEVICE=1209:beba
If attempting to flash via 3.3V serial, then use:
  make serialflash FLASH_DEVICE=/dev/serial/by-id/usb-STMicroelectronics_MARLIN_STM32F407VGT6_CCM_CDC_in_FS_Mode_207F366F4847-if00
make: *** [src/stm32/Makefile:101: flash] Error 255
------------------------

失敗,先按 [BOOT0],再按 [RESET],進入 bootloader mode,然後執行燒錄。


------------------------------------

make flash FLASH_DEVICE=0483:df11


出現下列錯誤

Moonraker requires sudo permission to update the system service. Please check your notifications for further intructions.

執行下述指令,我也不清楚做什麼用,網路上找到的

$ scripts/data-path-fix.sh

再升級 pip,反正就亂玩吧

$ /home/mks/moonraker-env/bin/python3 -m pip install --upgrade pip


X 方向相反,改 stepper_x 或 stepper_y 的方向設定,Y 方向相反,把 stepper_x 和 stepper_y 的接線互換。

2023年3月20日 星期一

在 ArchLinux 下安裝 chrome-remote-desktop

 直接用 yay -S aur/chrome-remote-desktop 安裝,出現下面的錯誤

----------------------------
==> Retrieving sources...
  -> Downloading chrome-remote-desktop-111.0.5563.12.deb...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  1449    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 404
==> ERROR: Failure while downloading https://dl.google.com/linux/chrome-remote-desktop/deb/pool/main/c/chrome-remote-desktop/chrome-remote-desktop_111.0.5563.12_amd64.deb
    Aborting...
 -> error making: chrome-remote-desktop
----------------------------

Google 到的答案,參考 Gentoo 的 ebuild,使用 curl 來找出目前的版本

----------------------------
$ curl -q https://dl.google.com/linux/chrome-remote-desktop/deb/dists/stable/main/binary-amd64/Packages | grep ^Filename
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1381  100  1381    0     0  17934      0 --:--:-- --:--:-- --:--:-- 18171
Filename: pool/main/c/chrome-remote-desktop/chrome-remote-desktop_112.0.5615.18_amd64.deb
----------------------------

使用 git clone https://aur.archlinux.org/chrome-remote-desktop.git 下載 AUR 的套件,再修改它的 PKGBUILD 中的版本。再執行安裝,出現錯誤。

----------------------------
$ makepkg -si
==> Making package: chrome-remote-desktop 112.0.5615.18-1 (Tue Mar 21 14:03:01 2023)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
  -> Downloading chrome-remote-desktop-112.0.5615.18.deb...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 17.1M  100 17.1M    0     0  45.8M      0 --:--:-- --:--:-- --:--:-- 45.8M
  -> Found chrome-remote-desktop.service
  -> Found pamrule
  -> Found crd
==> Validating source files with sha256sums...
    chrome-remote-desktop-112.0.5615.18.deb ... FAILED
    chrome-remote-desktop.service ... Passed
    pamrule ... Passed
    crd ... Passed
==> ERROR: One or more files did not pass the validity check!
----------------------------

執行 

---------------------
$ makepkg -g
==> Retrieving sources...
  -> Found chrome-remote-desktop-112.0.5615.18.deb
  -> Found chrome-remote-desktop.service
  -> Found pamrule
  -> Found crd
==> Generating checksums for source files...
sha256sums=('c4dda4236e0d25523fab390e741c7a43334e1b2932d96443af3d2fed43fcc49d'
            'e5da5ae89b5bc599f72f415d1523341b25357931b0de46159fce50ab83615a4b'
            'fcc38269eb1cc902abff9688eda9377a22367e39b9f111f87c0dd8e77adb82e2'
            '021110f49d465294517eec92eeb24ebca41e264ef33cbdda78732add1f269d02')
------------------------

將 checksum 複製到 PKGBUILD 中。即可成功安裝。





2023年3月9日 星期四

Voron 之二

 

上次配的是 MKS-Monster,CPU 是 STM32F407, 168 MHz CPU/210 DMIPS,約 RMB 200。這次配 FYSETC-SPIDER,CPU 是 STM32F446,180 MHz/225 DMIPS,約 RMB 278。兩者都是意法半导体的基本款 CPU。後者貴了不少,但我覺得它的說明文件的表示法不好讀。

2023年3月1日 星期三

MKS PI 安裝與除錯

買到機王? 無法啟動的 PI。後來又拿到一些出問題的板子,心得是這家公司的產品不是很穩定,要買的話,請自求多福。


無線網卡

買 USB wifi 卡,要挑一下,不然會無法驅動,很麻煩。無線網卡 WU816N 可用,WU817N 無法用。

啟始安裝及更新

由 MKS 的網站下載系統的 image,不常更新,可能有點舊。

第一次啟動,使用 debug port 來連線,設定比較方便。要用 sudo 身分,使用 mincom 來連接,指令如下。

# minicom -b 1500000 -D /dev/ttyUSB0

debug port 是一個 ch341-uart converter,在 Linux 下,會是 /dev/ttyUSBx,如 /dev/ttyUSB0。

無線網路的變數太多,最好先用有線連上。一般可以自動取得 IP,若要設定固定 IP,指令如下。

# ip link set dev eth0 up
# ip a add 10.161.86.37/20 dev eth0
# ip route add default via 10.161.80.1

並且設定 /etc/resolv.conf,內容大致如下。

nameserver 8.8.8.8 

上面的 IP 是 Google 的 DNS server。

設定網路後,先執行一次 apt update 和 apt upgrade,才能夠同步時間。下面使用 timedatectl 由 ntp server 取得正確時間,不然執行 apt update 會出錯。

# timedatectl set-timezone Asia/Taipei
# timedatectl set-ntp yes
# timedatectl set-local-rtc no
# hwclock -w 

執行 apt update 和 apt upgrade,更新系統的程式。

接下來就可以透過 KIAUH 安裝 Klipper 了。在裝 Klipper 之前,磁碟使用空間 2.1G,裝完後為 4.2G。

KIAUH stands for "Klipper Installation And Update Helper" and is a tool that helps you install or upgrade Klipper, Moonraker, Mainsail, and other extensions. 

因為 MKS 的 image 已安裝 Klipper,不用再 clone kiauh,可以到 kiauh 的目錄下,執行指令如下。注意,後面都要用 mks 的身分執行,不要用 sudo。

$ cd ~/kiauh
$ git pull origin

接著使用 kiauh 執行 klipper 升級,指令如下。

$ ~/kiauh/kiauh.sh

MKS 安裝的 klipper 不知修改了什麼,更新 klipper 時會出現錯誤,執行下列指令再繼續更新。

$ update klipper 時出現下面的錯誤

Updating 3387a9c2..6cd17420
error: Your local changes to the following files would be overwritten by merge:
	klippy/extras/virtual_sdcard.py
Please commit your changes or stash them before you merge.
Aborting

# 解決辦法
$ cd ~/klipper
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"

$ git stash
Saved working directory and index state WIP on master: 3387a9c2 
config: Use printer-creality-ender3-s1-2021.cfg for both s1 and s1 pro.

因為要使用 klicky probe 的 Automatic Z-Calibration 功能,必須安裝相關程式 protoloft / klipper_z_calibration,才能使用。安裝方式,參考 How To Install It。

使用 Manual Installation,比較知道裝了些什麼。只要

除錯

使用 mincom 來連接,因權限問題,直接使用 sudo 來執行。

---------------------------------------------
# minicom -b 1500000 -D /dev/ttyUSB0

歡迎使用 minicom 2.8

選項: I18n 
編譯於 Jan  9 2021, 12:42:45.
通訊埠 /dev/ttyUSB0, 19:56:30

按 CTRL-A Z 說明特殊鍵                                                  
按 reset 之後,出現下面的錯誤訊息。只有在插入 TF 卡之後,才會出現下面的訊息。若沒插卡,則完全沒有訊息。所以,這段程式是在 TF 卡上,而非在 SOC 的 flash 裡。
 
DDR version 1.16 20190528                                            
ID:0x805 N                                                        
In                                                                
DDR3
333MHz
 data training error 
ERR
---------------------------------------------

正常的板子,按 RESET 重新啟動後的訊息如下。

---------------------------------------------
DDR version 1.16 20190528
ID:0x805 N
In
DDR3
333MHz
Bus Width=32 Col=10 Bank=8 Row=15 CS=1 Die Bus-Width=16 Size=1024MB
ddrconfig:1
OUT
Boot1 Release Time: May 13 2019 17:34:36, version: 2.50
ChipType = 0x11, 232
mmc2:cmd19,100
SdmmcInit=2 0
BootCapSize=2000
UserCapSize=29820MB
FwPartOffset=2000 , 2000
SdmmcInit=0 NOT PRESENT
StorageInit ok = 12018
Raw SecureMode = 0
---------------------------------------------

慢慢地追,才知道這訊息是 rk3288 CPU 的 u-boot 發出的錯誤訊息,原始碼在 drivers/ram/rockchip/sdram_rk3328.c。看來是 DDR3 記憶體出問題,難道是冷焊嗎? 可以用熱風槍吹嗎?

source code: sdram_rk3328.c
---------------------------------------------

static int sdram_init(struct dram_info *dram,
      struct rk3328_sdram_params *sdram_params, u32 pre_init)
{
struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
void __iomem *pctl_base = dram->pctl;

rkclk_ddr_reset(dram, 1, 1, 1, 1);
udelay(10);
/*
* dereset ddr phy psrstn to config pll,
* if using phy pll psrstn must be dereset
* before config pll
*/
rkclk_ddr_reset(dram, 1, 1, 1, 0);
rkclk_configure_ddr(dram, sdram_params);

/* release phy srst to provide clk to ctrl */
rkclk_ddr_reset(dram, 1, 1, 0, 0);
udelay(10);
phy_soft_reset(dram->phy);
/* release ctrl presetn, and config ctl registers */
rkclk_ddr_reset(dram, 1, 0, 0, 0);
pctl_cfg(dram->pctl, &sdram_params->pctl_regs, SR_IDLE, PD_IDLE);
cap_info->ddrconfig = calculate_ddrconfig(sdram_params);
set_ctl_address_map(dram, sdram_params);
phy_cfg(dram->phy, &sdram_params->phy_regs, &sdram_params->skew,
&sdram_params->base, cap_info->bw);

/* enable dfi_init_start to init phy after ctl srstn deassert */
setbits_le32(pctl_base + DDR_PCTL2_DFIMISC, (1 << 5) | (1 << 4));
rkclk_ddr_reset(dram, 0, 0, 0, 0);
/* wait for dfi_init_done and dram init complete */
while ((readl(pctl_base + DDR_PCTL2_STAT) & 0x7) == 0)
continue;

/* do ddr gate training */
if (data_training(dram, 0, sdram_params->base.dramtype) != 0) {
printf("data training error\n");
return -1;
}

if (sdram_params->base.dramtype == DDR4)
pctl_write_vrefdq(dram->pctl, 0x3, 5670,
  sdram_params->base.dramtype);

if (pre_init != 0) {
rx_deskew_switch_adjust(dram);
tx_deskew_switch_adjust(dram);
}

dram_all_config(dram, sdram_params);
enable_low_power(dram, sdram_params);

return 0;
}
---------------------------------------------

這篇是 Firefly 的 ROC-RK3328-PC 開發板的啟動過程的介紹 [Boot mode description]。無論如何,過不了 DDR 測試,進不了下一關,能做的事很有限,可能連啟動 jtag debug 都難。

maskrom 模式的 xrock 工具,不得已時,死馬當活馬醫,進入 maskrom 模式重燒 bootloader。bin 可在 rockchip-linux/rkbin 的 rkbin/bin /rk32/ 取得。

USB 2207:320C => Fuzhou Rockchip Electronics Company,RK3328 in Mask ROM mode






2023年2月17日 星期五

在 Linux 下對 USB 外接 SSD 執行 fstrim

參考連結,Enable Trim on an External SSD on Linux,Fixing Storage Adapters for Raspberry Pi via Firmware Updates。

為了保持 SSD 的效能,在使用一陣子後,必須執行 fstrim,將刪除的 block 回收。但是 USB 外接硬碟盒不一定支援 unmap 的功能,目前 (2022) 可能只有 VIA 的晶片才支援,而且要稍微費一些功夫才能執行 fstrim。

參考

在  Gentoo 下,要安裝 sys-apps/sg3_utils,並且只支援 USB3,USB2 不能用。

買的時候,參考 ArchLinux 的 wiki,選擇 Orico 的外接硬碟盒,使用的晶片是 VIA 的 VL716。

以下整段抄自 ArchLinux 的 wiki,經測試 OK。

Several USB-to-SATA bridge chips (like VL715, VL716 etc.) and also USB-to-PCIe bridge chips (like the JMicron JMS583 used in external NVMe enclosures like IB-1817M-C31) support TRIM-like commands that can be sent through the USB Attached SCSI driver (named "uas" under Linux).

But the kernel may not automatically detect this capability, and therefore might not use it. Assuming your block device in question is /dev/sdX, you can find out whether that is the case by using the command

# sg_readcap -l /dev/sdX

If in its output you find a line stating "Logical block provisioning: lbpme=0" then you know that the kernel assumes the device does not support "Logical Block Provisioning Management" because the (LBPME) bit is not set.

If this is the case, then you should next find out whether the "Vital Product Data" (VPD) page on "Logical Block Provisioning" of your device tells of supported mechanisms for unmapping data. You can do this using the command:

# sg_vpd -a /dev/sdX

Look for lines in the output that look like this:

Unmap command supported (LBPU): 1
Write same (16) with unmap bit supported (LBPWS): 0
Write same (10) with unmap bit supported (LBPWS10): 0

This example would tell you the device supports the "UNMAP" command.

Have a look at the output of

$ cat /sys/block/sdX/device/scsi_disk/*/provisioning_mode

If the kernel did not detect the capability of your device to unmap data, then this will likely return "full". Apart from "full", the kernel SCSI storage driver currently knows the following values for provisioning_mode:

unmap
writesame_16
writesame_10
writesame_zero
disabled

For the example above, you could now write "unmap" to "provisioning_mode" to ask the kernel to use that:

# echo "unmap" >/sys/block/sdX/device/scsi_disk/*/provisioning_mode

This should immediately enable you to use tools like "blkdiscard" on /dev/sdX or "fstrim" on filesystems mounted on /dev/sdX.

If you want to enable a "provisioning_mode" automatically when an external device of a certain vendor/product is attached, this can be automated via the "udev" mechanism. First find the USB Vendor and Product IDs:

$ cat /sys/block/sdX/../../../../../../idVendor
$ cat /sys/block/sdX/../../../../../../idProduct

Then create or append to a udev rule file (example here using idVendor 152d and idProduct 0583):

# echo 'ACTION=="add|change", ATTRS{idVendor}=="152d", ATTRS{idProduct}=="0583", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"' >>/etc/udev/rules.d/10-uas-discard.rules

(You can also use the lsusb command to look for the relevant idVendor/idProduct.)

簡要說明

假設偵測到的外接 SSD 是 sde。

# cat /sys/block/sde/device/scsi_disk/*/provisioning_mode
full
# echo "unmap" >/sys/block/sde/device/scsi_disk/*/provisioning_mode
# cat /sys/block/sde/device/scsi_disk/*/provisioning_mode
unmap
# fstrim -v /mnt/ssd
/mnt/ssd: 270.7 GiB (290687594496 bytes) trimmed

大概如上。

2023年1月10日 星期二

OpenSCAD 學習

最完整的介紹說明,是 良葛格的網頁 https://openhome.cc/zh-tw/openscad/。不幸的是,我想要學 OpenSCAD,找到良葛格的介紹文章時,他已驟然病逝,享年 48歲,真是相遇不逢時,也為英才早逝感到惋惜。

詳細的指令說明可以參考 <OpenSCAD CheatSheet>,很單純,就層層疊疊的,再東削西挖的,把東西建構出來,有點像 DIY 做一個物件。

為了開心放心的使用免費的 3D建構軟體,先學 123D Design,後來改學參數式設計的 FreeCAD,最後學習 OpenSCAD。

一般會問 OpenSCAD 好不好學,好不好用? 答案是非常非常不好學,不好用,不直覺。那為何要用它? 因為 ....

首先,用 FreeCAD,可以用參數來建構物件,但是這參數怎麼來的,只有建的當下知道。可能是算出來的,也可能只是高興這樣設,也許是不想讓程式顯示還有 DoF,就隨便設一個,方便檢查是不是 Fully constrained。

其次,在 Part Design 中,一個物件一定要全部黏在一起,組合件只能分開設計。先設計好一個,再設計另一個。在建構後者時,必需打開前者,看看相對的參數為何。有的要一樣,嚙合的部分要加減一點,這個尤其困擾,讓人不知道怎麼來的。

所以,使用 OpenSCAD,就是為了文件化,抄的時候,比較清楚要改那裡,要怎麼改。

另外,它的 module 功能,可以全部建出來後,再做加工,如 mirror,一次就把兩個建出來了。FreeCAD 當然也行,但我不熟,用得不好,因為要切換 workbench。

檢視 stl 檔案的工具 -- fstl,測量 stl 檔案的工具 -- admesh。

網誌存檔