Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI DMA PSRAM QuadSPI half_duplex_write() doesn't resolve #2863

Open
i404788 opened this issue Dec 23, 2024 · 19 comments
Open

SPI DMA PSRAM QuadSPI half_duplex_write() doesn't resolve #2863

i404788 opened this issue Dec 23, 2024 · 19 comments
Labels
bug Something isn't working status:needs-attention This should be prioritized

Comments

@i404788
Copy link
Contributor

i404788 commented Dec 23, 2024

Bug description

Exact cause in not clear to me, but attempting to send a larger buffer with SPI DMA using PSRAM causes no interrupts to trigger (manually verified), and thus never resolves is_done().

I can only reliably reproduce it with half_duplex_write, although I think I've also seen it with regular write().

To Reproduce

Run example: https://github.com/i404788/esp-hal/blob/spi-dma-wip/examples/src/bin/spi_loopback_dma_psram.rs

Logs:

 Chip type:         esp32s3 (revision v0.1)
Crystal frequency: 40 MHz
Flash size:        16MB
Features:          WiFi, BLE
MAC address:       68:b6:b3:31:64:e8
App/part. size:    113,824/1,048,576 bytes, 10.86%
[2024-12-23T13:22:30Z INFO ] Segment at address '0x0' has not changed, skipping write
[2024-12-23T13:22:30Z INFO ] Segment at address '0x8000' has not changed, skipping write
[00:00:04] [========================================]      44/44      0x10000                                                                                                                                       [2024-12-23T13:22:36Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x2b (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fce3818,len:0x16f8
load:0x403c9700,len:0x4
load:0x403c9704,len:0xc00
load:0x403cc700,len:0x2eb0
entry 0x403c9908
I (30) boot: ESP-IDF v5.1-beta1-378-gea5e0ff298-dirt 2nd stage bootloader
I (30) boot: compile time Jun  7 2023 08:07:32
I (31) boot: Multicore bootloader
I (35) boot: chip revision: v0.1
I (39) boot.esp32s3: Boot SPI Speed : 40MHz
I (44) boot.esp32s3: SPI Mode       : DIO
I (49) boot.esp32s3: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3c010020 size=03fe4h ( 16356) map
I (109) esp_image: segment 1: paddr=0001400c vaddr=3fc8a010 size=00aa8h (  2728) load
I (114) esp_image: segment 2: paddr=00014abc vaddr=40378000 size=02010h (  8208) load
I (124) esp_image: segment 3: paddr=00016ad4 vaddr=00000000 size=09544h ( 38212)
I (139) esp_image: segment 4: paddr=00020020 vaddr=42000020 size=0bc50h ( 48208) map
I (153) boot: Loaded app from partition at offset 0x10000
I (153) boot: Disabling RNG early entropy source...
INFO - Starting SPI loopback test
INFO - chip id = 1b5d0d
INFO - size is 2097152
INFO - PSRAM core_clock SpiTimingConfigCoreClock80m, flash_div = 1, psram_div = 2
INFO - TX: 0x3c020000 len 8192 (3 descripters)
INFO - First dmabuffer: Some(DmaTxBuf { descriptors: DescriptorSet { descriptors: [DmaDescriptor { flags: DmaDescriptorFlags { size: 4095, length: 4095, suc_eof: false, owner: "CPU" }, buffer: 0x3c020000, next: 0x3fc8aac4 }, DmaDescriptor { flags: DmaDescriptorFlags { size: 4095, length: 4095, suc_eof: false, owner: "CPU" }, buffer: 0x3c020fff, next: 0x3fc8aad0 }, DmaDescriptor { flags: DmaDescriptorFlags { size: 2, length: 2, suc_eof: false, owner: "CPU" }, buffer: 0x3c021ffe, next: 0x0 }] }, buffer: [0, 0, 32, 0, 0, 0, 0, 0, 8, 9, 10, 11, 12, 13, 14, 14, 0, 17, 18, 18, 148, 21, 18, 19, 24, 9, 26, 59, 28, 31, 30, 31, 34, 33, 34, 163, 40, 37, 38, 39, 40, 41, 42, 171, 44, 45, 174, 43, 48, 57, 50, 50, 36, 53, 38, 167, 56, 57, 58, 58, 60, 61, 58, 63, 64, 65, 66, 3, 68, 69, 66, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 80, 82, 83, 68, 85, 210, 87, 88, 89, 90, 91, 76, 93, 78, 95, 96, 97, 98, 98, 100, 97, 102, 238, 104, 105, 106, 235, 110, 108, 110, 111, 120, 97, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 128, 130, 131, 132, 133, 134, 135, 136, 137, 138, 138, 142, 141, 142, 174, 128, 145, 146, 147, 148, 149, 134, 146, 136, 153, 154, 139, 156, 157, 158, 191, 160, 161, 162, 163, 164, 165, 166, 162, 170, 170, 170, 170, 168, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 168, 171, 170, 187, 190, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 206, 207, 238, 239, 192, 209, 210, 211, 196, 213, 214, 191, 216, 217, 218, 219, 204, 221, 222, 223, 224, 233, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 238, 237, 238, 239, 240, 249, 242, 243, 240, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 7, 8, 24, 26, 27, 24, 29, 26, 31, 32, 33, 34, 35, 32, 37, 38, 39, 40, 41, 170, 42, 44, 41, 46, 47, 48, 49, 186, 51, 52, 53, 54, 55, 40, 56, 58, 58, 44, 45, 58, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 13, 78, 15, 80, 65, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 41, 98, 99, 100, 101, 102, 103, 40, 105, 106, 107, 108, 106, 110, 111, 112, 113, 114, 115, 116, 119, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 129, 134, 135, 168, 137, 138, 171, 140, 141, 142, 143, 128, 145, 146, 147, 132, 149, 150, 151, 136, 153, 154, 155, 140, 153, 154, 190, 160, 161, 162, 163, 160, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 165, 182, 178, 184, 169, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 200, 201, 202, 203, 200, 205, 206, 207, 208, 193, 210, 211, 212, 213, 246, 223, 200, 201, 218, 219, 220, 221, 254, 223, 232, 225, 162, 227, 238, 229, 174, 230, 234, 235, 234, 234, 236, 237, 238, 239, 240, 163, 242, 243, 160, 245, 246, 247, 232, 233, 250, 250, 236, 253, 254, 255, 0, 1, 2, 3, 4, 5, 6, 135, 8, 9, 10, 11, 12, 13, 14, 14, 0, 1, 18, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 158, 63, 32, 33, 162, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 47, 48, 49, 34, 50, 32, 53, 54, 39, 56, 57, 58, 59, 60, 61, 62, 191, 64, 1, 66, 67, 68, 69, 66, 71, 72, 9, 74, 75, 76, 77, 78, 78, 80, 81, 82, 83, 84, 85, 214, 87, 88, 89, 90, 91, 76, 95, 94, 95, 96, 97, 98, 99, 100, 33, 102, 230, 104, 105, 106, 107, 108, 108, 110, 111, 120, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 138, 140, 141, 142, 174, 144, 145, 146, 147, 148, 149, 134, 147, 136, 153, 154, 139, 156, 157, 158, 187, 160, 161, 162, 163, 172, 165, 166, 162, 168, 171, 170, 170, 168, 169, 174, 175, 176, 177, 178, 179, 180, 181, 182, 182, 184, 169, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 206, 207, 206, 207, 208, 209, 210, 211, 196, 213, 246, 223, 216, 217, 250, 219, 204, 221, 222, 223, 224, 169, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 6, 8, 25, 26, 27, 24, 29, 26, 31, 32, 33, 34, 35, 36, 37, 38, 35, 40, 41, 170, 42, 44, 41, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 58, 58, 60, 44, 186, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 64, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 33, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 111, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 62, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 156, 157, 158, 158, 160, 161, 162, 163, 160, 165, 162, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 182, 184, 169, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 134, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 222, 223, 232, 225, 226, 227, 228, 229, 230, 231, 232, 235, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 180, 245, 246, 247, 248, 249, 250, 250, 236, 253, 254, 254, 0, 1, 2, 3, 4, 5, 6, 135, 8, 9, 10, 11, 12, 141, 14, 142, 0, 1, 18, 146, 20, 21, 22, 55, 24, 25, 26, 27, 28, 29, 30, 63, 34, 33, 34, 35, 40, 37, 38, 39, 40, 41, 42, 43, 44, 45, 174, 43, 48, 49, 34, 50, 36, 53, 38, 39, 40, 57, 186, 59, 60, 61, 62, 63, 64, 1, 66, 67, 68, 69, 66, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 68, 85, 210, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 97, 98, 99, 100, 33, 102, 238, 104, 105, 106, 107, 46, 109, 110, 111, 120, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 142, 141, 142, 174, 128, 145, 146, 147, 148, 149, 130, 151, 136, 153, 154, 139, 156, 157, 158, 191, 160, 161, 162, 163, 172, 165, 166, 162, 168, 169, 170, 171, 168, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 168, 171, 170, 187, 190, 189, 190, 191, 192, 193, 194, 195, 196, 133, 198, 199, 200, 137, 202, 203, 206, 205, 206, 239, 192, 209, 210, 211, 196, 213, 214, 247, 216, 217, 250, 219, 220, 221, 222, 223, 224, 169, 234, 235, 228, 229, 166, 231, 232, 233, 234, 234, 236, 237, 174, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 15, 16, 17, 18, 19, 4, 21, 22, 23, 8, 25, 26, 27, 24, 29, 26, 31, 32, 33, 34, 35, 36, 37, 38, 34, 40, 41, 42, 43, 44, 41, 46, 47, 48, 49, 186, 51, 52, 53, 54, 63, 56, 43, 58, 59, 60, 61, 58, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 65, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 105, 98, 99, 100, 101, 102, 231, 40, 105, 106, 107, 108, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 108, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 154, 158, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 193, 210, 211, 208, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 230, 231, 234, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 234, 249, 250, 250, 236, 252, 254, 255, 0, 1, 2, 3, 4, 5, 6, 135, 8, 9, 10, 11, 12, 13, 14, 14, 0, 1, 18, 18, 148, 21, 146, 23, 24, 25, 26, 27, 28, 29, 30, 63, 32, 33, 34, 35, 40, 37, 38, 39, 40, 41, 42, 43, 44, 45, 174, 43, 48, 49, 34, 50, 32, 53, 54, 167, 56, 41, 186, 43, 60, 61, 58, 63, 64, 1, 66, 67, 68, 69, 66, 71, 72, 9, 74, 75, 76, 77, 14, 78, 80, 80, 82, 83, 84, 85, 210, 87, 24, 89, 90, 91, 76, 95, 94, 95, 96, 97, 98, 99, 100, 33, 102, 238, 104, 105, 106, 235, 46, 109, 110, 111, 120, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 254, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 140, 141, 142, 142, 128, 145, 146, 147, 148, 149, 130, 147, 136, 153, 154, 139, 156, 157, 158, 191, 160, 161, 162, 163, 172, 165, 166, 162, 168, 169, 170, 170, 168, 169, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 168, 171, 186, 187, 188, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 206, 205, 206, 207, 192, 209, 210, 211, 196, 213, 214, 247, 216, 217, 218, 219, 204, 220, 222, 223, 224, 233, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 6, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 146, 19, 20, 21, 22, 23, 8, 24, 26, 27, 24, 29, 26, 31, 32, 33, 34, 35, 32, 37, 38, 34, 40, 41, 170, 42, 44, 41, 46, 46, 48, 49, 186, 51, 52, 53, 54, 55, 40, 58, 58, 58, 44, 44, 186, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 33, 98, 99, 102, 101, 102, 103, 104, 105, 106, 107, 110, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 62, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 136, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 158, 158, 160, 161, 162, 163, 160, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 175, 176, 177, 178, 179, 164, 181, 182, 182, 184, 169, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 193, 210, 211, 212, 213, 214, 223, 200, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 230, 231, 232, 235, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 164, 245, 246, 247, 250, 233, 250, 250, 236, 252, 254, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 138, 11, 12, 13, 10, 142, 16, 17, 18, 18, 20, 21, 22, 55, 24, 9, 26, 59, 28, 29, 30, 63, 32, 33, 162, 35, 32, 37, 38, 39, 40, 41, 42, 43, 44, 45, 174, 47, 48, 49, 50, 50, 36, 53, 38, 55, 40, 57, 170, 58, 60, 61, 190, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 202, 75, 76, 77, 78, 78, 80, 80, 82, 83, 68, 85, 18, 87, 88, 89, 90, 91, 76, 95, 94, 95, 96, 97, 98, 99, 100, 97, 102, 238, 104, 233, 106, 107, 44, 108, 238, 111, 112, 241, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 130, 131, 132, 129, 142, 135, 136, 137, 138, 138, 140, 141, 142, 142, 128, 145, 146, 147, 148, 149, 134, 151, 136, 153, 154, 139, 152, 157, 158, 191, 160, 161, 162, 163, 164, 165, 166, 162, 168, 171, 170, 171, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 183, 168, 169, 170, 187, 190, 189, 190, 190, 192, 193, 194, 195, 196, 197, 198, 199, 200, 137, 202, 203, 204, 205, 206, 207, 192, 209, 210, 211, 212, 213, 214, 223, 216, 217, 218, 219, 220, 253, 222, 223, 224, 233, 234, 235, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 6, 24, 25, 26, 26, 8, 29, 26, 31, 32, 33, 34, 35, 38, 37, 38, 35, 40, 41, 42, 42, 44, 41, 46, 47, 48, 49, 58, 50, 54, 53, 54, 55, 56, 58, 58, 58, 44, 45, 58, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 13, 78, 15, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 222, 95, 96, 97, 98, 99, 100, 101, 102, 103, 40, 105, 106, 107, 44, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 171, 136, 141, 174, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 156, 157, 158, 158, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 184, 185, 186, 186, 172, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 193, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 174, 231, 232, 235, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 244, 245, 246, 247, 250, 249, 250, 250, 236, 248, 254, 255, 0, 1, 2, 11, 4, 5, 6, 7, 8, 9, 138, 11, 12, 13, 14, 142, 0, 1, 18, 19, 148, 21, 150, 23, 24, 9, 26, 27, 60, 28, 30, 63, 32, 33, 162, 35, 40, 37, 38, 38, 40, 169, 42, 43, 44, 45, 174, 47, 48, 33, 34, 50, 36, 53, 38, 39, 40, 57, 170, 59, 60, 61, 58, 63, 64, 1, 66, 67, 68, 69, 66, 71, 72, 9, 202, 75, 76, 77, 78, 79, 80, 80, 82, 83, 68, 85, 214, 87, 8, 89, 90, 91, 76, 95, 78, 95, 96, 99, 98, 99, 100, 97, 102, 238, 104, 105, 106, 235, 46, 108, 110, 111, 120, 113, 114, 115, 116, 117, 118, 118, 120, 121, 122, 123, 60, 125, 254, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 140, 141, 142, 174, 128, 145, 146, 147, 148, 149, 134, 147, 136, 153, 154, 139, 156, 157, 158, 187, 160, 161, 162, 163, 172, 165, 166, 162, 168, 171, 170, 170, 168, 172, 174, 174, 176, 177, 178, 179, 180, 181, 182, 183, 184, 171, 186, 187, 190, 173, 186, 191, 192, 193, 194, 195, 196, 197, 198, 199, 232, 201, 202, 203, 206, 207, 238, 239, 192, 209, 210, 211, 212, 213, 214, 247, 216, 217, 218, 219, 204, 221, 222, 223, 224, 232, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 224, 241, 242, 243, 240, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 39, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 18, 19, 20, 21, 22, 22, 24, 25, 26, 27, 24, 29, 26, 31, 32, 33, 34, 35, 36, 37, 38, 42, 40, 41, 42, 42, 44, 41, 46, 47, 32, 49, 58, 50, 180, 181, 54, 55, 56, 40, 58, 58, 60, 45, 58, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 13, 78, 79, 80, 65, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 41, 98, 99, 102, 101, 102, 103, 104, 105, 106, 107, 44, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 118, 120, 121, 122, 122, 252, 125, 62, 127, 128, 129, 130, 131, 132, 133, 134, 134, 136, 137, 138, 171, 136, 141, 174, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 158, 158, 160, 161, 162, 163, 160, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 165, 182, 182, 184, 169, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 200, 201, 202, 203, 206, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 162, 227, 228, 229, 238, 231, 234, 235, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 160, 245, 246, 247, 250, 249, 250, 250, 236, 252, 254, 255, 0, 1, 2, 3, 4, 5, 6, 135, 8, 9, 10, 11, 12, 13, 14, 142, 0, 17, 18, 147, 148, 21, 150, 23, 24, 25, 26, 27, 28, 29, 30, 63, 32, 33, 162, 35, 40, 37, 38, 39, 40, 169, 42, 171, 44, 45, 174, 43, 48, 49, 50, 50, 36, 53, 38, 39, 40, 41, 186, 58, 60, 61, 62, 63, 64, 1, 66, 67, 68, 69, 66, 71, 72, 73, 74, 75, 76, 77, 78, 78, 80, 81, 82, 83, 68, 85, 82, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 97, 98, 99, 100, 97, 102, 238, 104, 105, 106, 107, 46, 109, 110, 111, 120, 113, 114, 123, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 140, 141, 142, 174, 128, 145, 146, 147, 132, 149, 130, 147, 136, 137, 154, 139, 156, 157, 158, 190, 160, 161, 162, 163, 172, 164, 166, 162, 168, 171, 170, 170, 168, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 168, 169, 186, 187, 188, 189, 190, 191, 192, 193, 226, 195, 196, 197, 198, 199, 200, 201, 202, 203, 206, 205, 238, 239, 192, 209, 210, 211, 212, 212, 214, 247, 216, 217, 250, 219, 204, 220, 222, 223, 224, 169, 234, 235, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 18, 19, 4, 21, 22, 7, 24, 25, 26, 26, 8, 29, 26, 31, 160, 33, 34, 35, 36, 37, 38, 38, 40, 41, 42, 42, 44, 41, 46, 46, 32, 57, 186, 51, 54, 53, 54, 55, 56, 58, 58, 58, 60, 45, 58, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 69, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 105, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 104, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 62, 127, 128, 129, 130, 131, 132, 129, 134, 135, 168, 137, 138, 171, 136, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 154, 158, 160, 161, 162, 163, 160, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 175, 176, 177, 178, 179, 164, 181, 182, 183, 184, 185, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 200, 201, 202, 203, 206, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 230, 229, 230, 231, 234, 233, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 164, 245, 246, 247, 234, 249, 250, 250, 236, 253, 250, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 0, 1, 18, 18, 148, 21, 22, 23, 24, 9, 26, 27, 28, 29, 30, 63, 34, 33, 34, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 43, 48, 49, 34, 50, 36, 53, 54, 39, 56, 41, 186, 59, 60, 61, 62, 191, 64, 65, 66, 67, 68, 69, 66, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 68, 85, 86, 87, 88, 89, 90, 91, 76, 95, 78, 95, 96, 97, 98, 99, 108, 97, 102, 238, 104, 105, 106, 107, 110, 108, 46, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 254, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 138, 140, 141, 142, 174, 128, 145, 146, 147, 148, 149, 134, 147, 136, 153, 154, 155, 156, 157, 158, 191, 160, 161, 162, 163, 172, 164, 166, 162, 168, 171, 170, 171, 168, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 168, 169, 186, 187, 188, 189, 186, 190, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 206, 205, 206, 239, 192, 209, 210, 211, 196, 213, 214, 247, 216, 217, 250, 219, 204, 221, 222, 223, 224, 169, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 6, 24, 25, 26, 27, 24, 29, 26, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 44, 41, 46, 47, 56, 41, 186, 51, 52, 181, 54, 55, 56, 58, 58, 59, 60, 44, 186, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 65, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 222, 95, 96, 105, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 62, 127, 128, 129, 130, 131, 132, 133, 134, 135, 168, 137, 138, 171, 136, 141, 142, 143, 128, 145, 146, 147, 132, 149, 150, 151, 136, 153, 154, 155, 140, 157, 158, 158, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 171, 172, 173, 174, 175, 178, 177, 178, 179, 180, 181, 182, 182, 184, 185, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 130, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 246, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 230, 229, 238, 231, 232, 235, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 244, 245, 246, 247, 248, 249, 250, 250, 252, 253, 254, 255, 0, 1, 2, 3, 4, 5, 6, 134, 8, 9, 138, 11, 12, 13, 10, 14, 0, 17, 146, 18, 148, 21, 22, 55, 24, 9, 26, 59, 28, 29, 190, 63, 34, 33, 162, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 47, 48, 49, 34, 50, 36, 53, 38, 167, 40, 57, 170, 59, 60, 61, 62, 191, 64, 1, 66, 67, 68, 69, 70, 103, 72, 9, 202, 75, 76, 13, 14, 78, 80, 80, 82, 83, 64, 85, 210, 87, 24, 89, 90, 91, 12, 93, 94, 95, 96, 97, 98, 98, 100, 33, 102, 238, 104, 105, 106, 107, 46, 109, 46, 107, 120, 97, 114, 115, 116, 117, 118, 119, 120, 121, 122, 59, 60, 125, 126, 127, 128, 129, 130, 131, 132, 129, 142, 135, 136, 137, 138, 138, 142, 141, 142, 142, 128, 145, 146, 147, 148, 149, 130, 147, 136, 137, 154, 155, 156, 157, 158, 191, 160, 161, 162, 163, 168, 164, 166, 162, 168, 170, 170, 170, 168, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 168, 169, 170, 187, 190, 173, 186, 190, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 206, 205, 206, 239, 192, 209, 210, 211, 196, 213, 246, 247, 216, 217, 250, 219, 204, 221, 222, 223, 224, 233, 234, 235, 228, 229, 230, 231, 232, 233, 234, 234, 238, 237, 238, 239, 240, 225, 242, 227, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 18, 19, 20, 21, 22, 7, 8, 25, 26, 26, 8, 29, 26, 31, 32, 33, 34, 35, 32, 37, 38, 42, 40, 41, 42, 42, 44, 41, 46, 47, 48, 49, 186, 51, 52, 53, 54, 55, 56, 58, 58, 59, 60, 45, 186, 63, 64, 65, 66, 67, 68, 5, 198, 70, 8, 73, 74, 203, 76, 77, 74, 79, 80, 81, 66, 83, 86, 85, 86, 95, 88, 89, 90, 91, 76, 93, 94, 95, 96, 41, 98, 99, 108, 101, 102, 103, 104, 105, 106, 107, 108, 109, 46, 111, 112, 113, 114, 115, 116, 119, 118, 119, 120, 121, 122, 251, 124, 125, 62, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 139, 136, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 154, 186, 160, 161, 162, 163, 160, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 184, 169, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 232, 201, 202, 203, 200, 205, 206, 207, 208, 193, 210, 211, 208, 213, 246, 223, 216, 217, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 230, 231, 234, 233, 234, 234, 236, 237, 238, 239, 176, 169, 242, 243, 164, 245, 246, 247, 250, 233, 250, 250, 236, 253, 254, 255, 0, 1, 2, 11, 0, 5, 6, 135, 8, 9, 10, 10, 12, 13, 10, 142, 0, 17, 18, 18, 148, 21, 22, 23, 24, 25, 26, 59, 28, 29, 30, 63, 32, 33, 162, 163, 40, 37, 38, 39, 40, 40, 42, 43, 44, 45, 174, 47, 48, 33, 34, 50, 32, 37, 38, 39, 56, 57, 170, 42, 60, 61, 186, 63, 0, 65, 66, 3, 68, 69, 70, 71, 72, 9, 74, 107, 76, 77, 78, 79, 80, 80, 82, 83, 68, 85, 210, 87, 8, 89, 90, 91, 76, 95, 78, 95, 96, 97, 98, 99, 100, 97, 102, 238, 104, 105, 106, 107, 110, 109, 46, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 58, 60, 125, 126, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 140, 141, 142, 174, 128, 145, 154, 147, 148, 149, 130, 146, 136, 153, 154, 171, 152, 157, 158, 191, 160, 161, 162, 163, 160, 165, 166, 162, 168, 171, 170, 171, 170, 169, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 168, 171, 170, 187, 190, 189, 190, 191, 192, 193, 194, 195, 204, 197, 198, 199, 232, 201, 202, 203, 206, 205, 206, 239, 192, 209, 210, 211, 212, 213, 214, 247, 216, 217, 218, 219, 204, 221, 222, 223, 224, 233, 234, 235, 228, 229, 166, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 244, 245, 246, 247, 248, 233, 250, 250, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 6, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 18, 19, 4, 31, 22, 6, 8, 25, 26, 27, 8, 29, 26, 31, 32, 33, 34, 35, 32, 37, 38, 34, 40, 41, 42, 42, 44, 41, 46, 47, 32, 57, 58, 50, 36, 53, 54, 55, 40, 58, 58, 59, 44, 45, 186, 63, 74, 65, 66, 67, 68, 69, 70, 70, 8, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 86, 213, 86, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 41, 98, 99, 110, 101, 102, 167, 104, 105, 106, 107, 108, 111, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 252, 125, 126, 127, 128, 129, 130, 131, 134, 133, 134, 134, 168, 137, 138, 171, 136, 141, 142, 143, 130, 145, 146, 147, 132, 149, 150, 151, 136, 153, 154, 155, 140, 157, 154, 158, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 164, 181, 182, 178, 184, 169, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 193, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 248, 241, 242, 243, 228, 245, 246, 247, 234, 233, 250, 250, 236, 253, 254, 255, 0, 1, 2, 11, 4, 5, 6, 135, 8, 9, 138, 11, 12, 141, 138, 142, 0, 1, 18, 18, 148, 21, 22, 23, 24, 9, 26, 27, 28, 29, 30, 63, 32, 35, 34, 35, 40, 37, 38, 38, 40, 169, 42, 43, 44, 45, 174, 47, 48, 33, 34, 50, 44, 37, 38, 167, 56, 41, 186, 58, 60, 61, 58, 63, 64, 1, 66, 3, 68, 69, 66, 231, 72, 9, 202, 75, 76, 77, 78, 79, 88, 16, 82, 83, 68, 85, 210, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 97, 98, 99, 100, 33, 102, 238, 104, 105, 106, 107, 110, 108, 110, 107, 112, 113, 114, 123, 116, 117, 118, 119, 120, 121, 122, 123, 60, 61, 126, 255, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 142, 141, 142, 174, 128, 145, 146, 147, 148, 149, 130, 146, 136, 137, 154, 171, 156, 157, 158, 191, 160, 161, 162, 163, 164, 165, 166, 162, 168, 171, 170, 171, 168, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 171, 170, 187, 190, 173, 186, 190, 192, 193, 194, 195, 204, 197, 198, 199, 200, 201, 202, 203, 202, 205, 206, 207, 192, 209, 210, 211, 196, 213, 214, 247, 216, 217, 250, 219, 204, 253, 222, 223, 224, 169, 234, 227, 228, 229, 166, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 225, 242, 227, 244, 245, 246, 247, 248, 249, 250, 250, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 15, 16, 17, 146, 18, 20, 21, 22, 23, 24, 25, 26, 26, 8, 29, 26, 31, 160, 33, 34, 35, 36, 37, 38, 38, 42, 41, 170, 43, 44, 41, 46, 47, 32, 49, 186, 51, 52, 181, 54, 55, 40, 58, 58, 58, 60, 61, 186, 63, 66, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 203, 76, 73, 78, 79, 80, 81, 82, 83, 94, 213, 86, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 41, 98, 99, 108, 103, 38, 231, 40, 105, 106, 107, 44, 111, 110, 111, 112, 113, 114, 115, 116, 119, 118, 119, 120, 121, 122, 251, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 168, 137, 138, 171, 136, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 158, 158, 160, 161, 162, 163, 160, 173, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 164, 165, 182, 182, 184, 169, 186, 187, 172, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 202, 205, 206, 207, 208, 195, 210, 211, 212, 213, 214, 223, 200, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 166, 231, 234, 235, 234, 234, 236, 237, 238, 239, 248, 233, 242, 243, 164, 245, 246, 247, 234, 249, 250, 250, 236, 252, 254, 255, 0, 1, 2, 11, 4, 5, 6, 135, 8, 9, 10, 11, 12, 13, 14, 142, 0, 1, 18, 18, 20, 21, 22, 23, 24, 9, 26, 59, 28, 28, 30, 63, 32, 33, 34, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 47, 32, 33, 34, 50, 36, 53, 54, 39, 40, 41, 186, 59, 60, 61, 58, 63, 64, 1, 66, 67, 68, 69, 66, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 80, 82, 83, 84, 85, 210, 87, 88, 89, 90, 91, 76, 95, 78, 95, 96, 97, 98, 99, 100, 97, 102, 238, 104, 105, 106, 235, 110, 108, 46, 111, 120, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 138, 142, 141, 142, 174, 128, 145, 146, 147, 148, 149, 130, 147, 136, 153, 154, 139, 156, 157, 158, 191, 160, 161, 162, 163, 164, 164, 166, 162, 168, 171, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 183, 168, 171, 186, 187, 190, 189, 186, 190, 192, 193, 194, 195, 204, 197, 198, 199, 200, 201, 202, 203, 206, 207, 206, 207, 192, 209, 210, 211, 196, 213, 214, 247, 216, 217, 218, 219, 204, 221, 222, 223, 224, 169, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 238, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 0, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 146, 19, 20, 21, 22, 6, 24, 25, 26, 27, 8, 29, 30, 31, 32, 33, 34, 35, 32, 37, 38, 34, 40, 41, 42, 42, 44, 41, 46, 47, 48, 49, 58, 51, 36, 53, 54, 55, 56, 56, 58, 58, 60, 45, 58, 63, 64, 65, 66, 67, 68, 69, 70, 70, 72, 73, 10, 203, 76, 77, 78, 79, 80, 81, 82, 83, 86, 85, 86, 87, 88, 89, 90, 91, 92, 93, 222, 95, 96, 105, 98, 99, 102, 101, 102, 103, 40, 105, 106, 107, 108, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 171, 140, 141, 142, 143, 128, 145, 146, 147, 132, 149, 150, 151, 136, 153, 154, 155, 140, 157, 154, 158, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 183, 184, 169, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 130, 199, 200, 203, 202, 203, 200, 205, 206, 207, 208, 193, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 174, 230, 234, 235, 234, 234, 236, 237, 238, 239, 176, 169, 242, 243, 244, 245, 246, 247, 234, 249, 250, 250, 236, 252, 254, 255, 0, 1, 2, 11, 4, 5, 6, 135, 8, 9, 138, 11, 12, 141, 14, 142, 0, 17, 18, 18, 20, 21, 22, 23, 24, 9, 26, 59, 28, 29, 30, 63, 32, 33, 162, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 47, 48, 49, 50, 50, 32, 53, 38, 39, 40, 57, 186, 58, 60, 61, 62, 63, 64, 1, 66, 3, 68, 69, 66, 103, 72, 73, 74, 75, 76, 77, 78, 78, 80, 80, 82, 83, 68, 85, 82, 87, 88, 89, 90, 91, 76, 95, 94, 95, 96, 97, 98, 99, 100, 97, 102, 238, 104, 105, 106, 107, 46, 109, 238, 111, 120, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 130, 131, 132, 129, 134, 167, 136, 137, 138, 138, 142, 141, 142, 142, 128, 145, 146, 147, 148, 149, 130, 147, 136, 153, 154, 139, 156, 157, 158, 191, 160, 161, 162, 163, 172, 164, 166, 162, 168, 171, 170, 171, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 183, 168, 169, 170, 187, 190, 189, 190, 191, 192, 193, 194, 195, 196, 133, 198, 199, 200, 201, 202, 203, 206, 207, 206, 207, 192, 209, 210, 211, 212, 213, 214, 247, 216, 217, 250, 219, 200, 221, 222, 223, 224, 233, 234, 235, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 6, 24, 25, 26, 27, 8, 29, 30, 31, 32, 33, 34, 35, 32, 37, 38, 35, 40, 41, 170, 42, 44, 41, 46, 46, 32, 49, 186, 50, 52, 53, 54, 55, 40, 58, 58, 59, 60, 61, 58, 63, 66, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 65, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 76, 93, 94, 95, 96, 105, 98, 99, 110, 101, 102, 103, 40, 105, 106, 107, 44, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 122, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 168, 137, 138, 171, 136, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 154, 190, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 164, 165, 182, 178, 168, 169, 186, 187, 172, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 193, 210, 211, 212, 213, 182, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 166, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 232, 233, 250, 250, 236, 253, 254, 255, 0, 1, 2, 3, 4, 5, 6, 135, 8, 9, 138, 10, 12, 13, 10, 142, 0, 17, 18, 18, 20, 21, 22, 23, 24, 25, 26, 59, 28, 29, 30, 63, 32, 33, 162, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 47, 48, 49, 34, 50, 32, 53, 54, 167, 56, 57, 186, 58, 60, 61, 62, 63, 64, 1, 66, 67, 68, 69, 70, 71, 72, 9, 74, 75, 76, 13, 206, 78, 80, 16, 82, 83, 84, 85, 210, 87, 72, 89, 90, 91, 76, 93, 94, 95, 96, 97, 98, 99, 100, 33, 102, 238, 104, 105, 106, 107, 108, 109, 110, 111, 112, 97, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 126, 127, 128, 129, 138, 131, 132, 133, 142, 135, 136, 137, 138, 138, 140, 141, 142, 174, 128, 145, 146, 147, 132, 149, 130, 146, 136, 153, 154, 155, 156, 157, 158, 187, 160, 161, 162, 163, 172, 164, 166, 162, 168, 171, 170, 170, 168, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 184, 169, 186, 187, 190, 189, 190, 191, 192, 193, 194, 195, 196, 133, 198, 199, 200, 201, 202, 203, 206, 205, 238, 207, 192, 209, 210, 211, 196, 213, 214, 183, 152, 217, 250, 219, 204, 221, 222, 223, 224, 169, 234, 227, 228, 229, 166, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 227, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 6, 8, 9, 10, 10, 14, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 6, 8, 25, 26, 26, 8, 29, 26, 31, 32, 33, 34, 35, 32, 37, 38, 34, 40, 41, 42, 42, 44, 41, 46, 47, 32, 49, 186, 51, 52, 53, 54, 55, 56, 59, 58, 58, 44, 61, 186, 63, 64, 65, 66, 67, 68, 5, 70, 70, 8, 73, 74, 75, 76, 77, 74, 79, 80, 65, 66, 83, 86, 85, 86, 87, 88, 89, 90, 91, 76, 93, 222, 95, 96, 41, 98, 99, 100, 101, 102, 231, 40, 105, 106, 107, 108, 108, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 62, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 136, 141, 142, 143, 136, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 154, 158, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 165, 182, 183, 184, 185, 186, 187, 168, 173, 190, 191, 192, 193, 194, 195, 196, 197, 194, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 238, 231, 234, 235, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 224, 245, 246, 247, 234, 249, 250, 250, 236, 252, 254, 255, 0, 1, 2, 11, 4, 5, 6, 135, 8, 9, 138, 11, 12, 13, 14, 142, 0, 17, 18, 18, 148, 21, 150, 151, 24, 9, 26, 27, 28, 28, 30, 63, 32, 35, 34, 35, 40, 37, 38, 38, 40, 41, 42, 171, 44, 45, 174, 47, 48, 49, 34, 50, 36, 53, 54, 39, 56, 57, 186, 58, 56, 61, 62, 63, 64, 65, 66, 2, 68, 69, 70, 71, 72, 9, 202, 75, 76, 77, 78, 79, 80, 80, 82, 83, 68, 85, 210, 87, 24, 89, 90, 91, 76, 93, 78, 95, 96, 97, 98, 99, 100, 97, 102, 238, 104, 105, 106, 107, 110, 108, 46, 107, 120, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 60, 125, 254, 127, 128, 129, 130, 131, 132, 129, 134, 135, 136, 137, 138, 138, 142, 141, 142, 174, 128, 145, 146, 147, 132, 149, 130, 146, 136, 153, 154, 139, 156, 157, 158, 191, 160, 161, 162, 163, 172, 165, 166, 162, 168, 170, 170, 170, 168, 169, 174, 175, 176, 177, 178, 179, 180, 181, 182, 182, 168, 171, 170, 187, 190, 189, 186, 191, 192, 193, 194, 195, 204, 197, 198, 199, 200, 137, 202, 203, 204, 207, 206, 239, 192, 209, 210, 211, 212, 213, 214, 247, 216, 217, 250, 219, 204, 221, 222, 223, 224, 169, 234, 227, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 14, 16, 17, 18, 19, 4, 21, 22, 22, 8, 25, 26, 26, 28, 29, 26, 31, 32, 33, 34, 35, 32, 37, 38, 39, 40, 41, 170, 42, 40, 41, 46, 47, 32, 57, 58, 51, 52, 53, 54, 55, 56, 59, 58, 59, 60, 45, 58, 63, 66, 65, 66, 67, 68, 69, 70, 70, 72, 73, 74, 203, 76, 77, 78, 79, 80, 65, 82, 83, 86, 85, 86, 95, 88, 89, 90, 91, 76, 93, 94, 95, 96, 41, 98, 99, 110, 101, 102, 103, 104, 105, 106, 107, 44, 110, 110, 111, 112, 113, 242, 115, 116, 119, 118, 119, 120, 121, 122, 123, 252, 125, 62, 127, 128, 129, 130, 131, 134, 133, 134, 135, 168, 137, 138, 171, 140, 141, 142, 143, 128, 145, 146, 147, 132, 149, 150, 151, 136, 153, 154, 155, 140, 157, 158, 158, 168, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 173, 174, 175, 178, 177, 178, 179, 164, 181, 182, 182, 184, 185, 186, 187, 172, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 200, 205, 206, 207, 208, 193, 210, 211, 212, 213, 214, 223, 200, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 174, 231, 234, 233, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 228, 245, 246, 247, 232, 233, 250, 250, 236, 253, 254, 255, 0, 1, 2, 11, 4, 5, 6, 135, 8, 9, 138, 10, 12, 141, 14, 143, 0, 1, 18, 18, 156, 21, 22, 23, 24, 9, 26, 59, 60, 28, 30, 31, 32, 35, 162, 35, 40, 37, 38, 38, 40, 41, 42, 43, 44, 45, 174, 47, 48, 49, 34, 50, 36, 53, 54, 39, 56, 57, 186, 59, 60, 61, 62, 63, 64, 1, 66, 3, 68, 69, 70, 103, 72, 9, 74, 75, 76, 77, 78, 79, 80, 16, 82, 83, 68, 85, 210, 87, 88, 89, 90, 91, 76, 93, 94, 95, 32, 97, 98, 99, 100, 33, 102, 238, 104, 105, 106, 107, 110, 108, 110, 239, 120, 113, 114, 115, 116, 117, 118, 118, 120, 121, 122, 123, 60, 125, 254, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 138, 140, 141, 142, 174, 128, 145, 146, 147, 148, 149, 130, 147, 136, 153, 154, 155, 156, 157, 158, 191, 160, 161, 162, 163, 172, 165, 166, 162, 168, 171, 170, 170, 172, 173, 174, 174, 176, 177, 178, 179, 180, 181, 182, 182, 168, 169, 186, 187, 190, 189, 190, 191, 192, 193, 194, 195, 204, 197, 198, 199, 200, 201, 202, 203, 206, 205, 206, 239, 192, 209, 210, 211, 212, 212, 246, 247, 216, 217, 250, 219, 204, 221, 222, 223, 224, 169, 234, 235, 228, 229, 230, 231, 232, 233, 234, 234, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 14, 13, 14, 15, 16, 17, 18, 19, 20, 5, 22, 6, 24, 25, 26, 26, 24, 29, 26, 31, 160, 33, 34, 35, 32, 37, 38, 34, 40, 41, 42, 42, 44, 41, 46, 47, 40, 41, 186, 51, 52, 53, 54, 55, 56, 56, 58, 58, 44, 61, 186, 63, 64, 65, 66, 67, 68, 69, 70, 71, 8, 73, 74, 75, 76, 13, 78, 79, 80, 81, 66, 83, 84, 85, 86, 87, 88, 89, 90, 91, 76, 92, 222, 95, 96, 41, 98, 99, 108, 101, 102, 231, 40, 105, 106, 106, 108, 110, 110, 111, 112, 113, 242, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 134, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 128, 145, 146, 147, 148, 149, 150, 151, 136, 153, 154, 155, 140, 157, 158, 190, 160, 161, 162, 163, 164, 165, 162, 167, 168, 169, 170, 170, 172, 175, 174, 175, 176, 177, 178, 179, 180, 181, 182, 182, 184, 169, 186, 186, 172, 173, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 235, 204, 205, 206, 207, 208, 211, 210, 211, 212, 213, 246, 223, 216, 201, 218, 219, 220, 221, 254, 223, 232, 225, 226, 227, 228, 229, 174, 230, 234, 233, 234, 234, 236, 237, 238, 239, 240, 225, 242, 243, 164, 245, 246, 247, 234, 249, 250, 250, 232, 253, 254, 255], burst: BurstConfig { external_memory: Size64, internal_memory: Disabled } })
0x3fc8aac4 - spi_loopback_dma_psram::__xtensa_lx_rt_main::DESCRIPTORS
    at ??:??
0x3fc8aad0 - spi_loopback_dma_psram::__xtensa_lx_rt_main::DESCRIPTORS
    at ??:??

Expected behavior

Expected it to either error, or complete successfully. See the Done log repeatedly.

Environment

  • Target device: ESP32-S3]
  • Crate name and version: main
@i404788 i404788 added bug Something isn't working status:needs-attention This should be prioritized labels Dec 23, 2024
@i404788
Copy link
Contributor Author

i404788 commented Dec 23, 2024

Update: found that the bus speed is relevant: <=20Mhz <=23.25Mhz still works 25Mhz fails

@Dominaezzz
Copy link
Collaborator

Thanks for the tidbit about bus speed. It's possible that PSRAM can't keep up with the SPI.

Would you mind printing out the SPI interrupt registers (with the PAC) a few seconds after the transfer starts?

@i404788
Copy link
Contributor Author

i404788 commented Dec 23, 2024

@Dominaezzz attempted to add your change but it seems adding a delay of >600us (bisected to +-50us, in this range it occasionally works) causes it to succeed again. So it seems like there might be a race condition with an interrupt or something.

The result I'm getting is always 0 but I may be doing it wrong:

      log::info!("SPI2: {}", unsafe {
          *(SPI2::ptr().add(0x0040) as *const u32).as_ref().unwrap()
      });

@Dominaezzz
Copy link
Collaborator

I'm looking for INT_RAW rather than INT_ST, which is 0x003C.

Did you add the delay to the example or to the SPI driver? I was thinking of here.

Since this example is blocking and not async, a race condition with an interrupt is very unlikely.

@i404788
Copy link
Contributor Author

i404788 commented Dec 23, 2024

Did you add the delay to the example or to the SPI driver? I was thinking of here.

Yes exactly there, I've pushed my latest version now. I'll check 0x003C

Update: 0x003C also seems to be 0 both at success and fail (with the 593us delay)

@Dominaezzz
Copy link
Collaborator

Can you add a larger delay? I still want to see what the interrupts look like if it succeeds

@i404788
Copy link
Contributor Author

i404788 commented Dec 23, 2024

Hmm switched to read_volatile but still no change either way:

        log::info!("pre-SPI2: {}", unsafe {
            core::ptr::read_volatile(SPI2::ptr().add(0x003C) as *const u32)
        });

        delay.delay_millis(2000);

        log::info!("post-SPI2: {}", unsafe {
            core::ptr::read_volatile(SPI2::ptr().add(0x003C) as *const u32)
        });
        (spi, dma_buf) = transfer.wait();
INFO - pre-SPI2: 0
INFO - post-SPI2: 0

However it does not resolve again anymore. Given that; maybe it has to do with memory layout since that will only be randomized with some significant change? This seems to be confirmed by placing the delay between the prints also fails with 593us, even though placing it before the prints will succeed once then fail once. Likewise placing 750us between prints fails, placing it before the prints get repeated successes.

I'm not sure which parts require a specific alignment and how I can check if the are aligned.

@Dominaezzz
Copy link
Collaborator

The alignment requirements should be checked by DmaTxBuf, which Daniel improved the checks for recently.

Can you print out the addresses of tx_descriptors and tx_buffer? We need to make sure the latter is 64 byte aligned.

@Dominaezzz
Copy link
Collaborator

Oh looks like it's already done here.

    info!(
        "TX: {:p} len {} ({} descripters)",
        tx_buffer.as_ptr(),
        tx_buffer.len(),
        tx_descriptors.len()
    );

@i404788
Copy link
Contributor Author

i404788 commented Dec 23, 2024

Hmm yeah the descriptors/buffer addresses don't seem to different:

Working:

INFO - TX: 0x3c020000 len 8192 (3 descripters)
INFO - Alignment: 64, Chunk size: 4032
INFO - First dmabuffer: Some(DmaTxBuf { descriptors: DescriptorSet { descriptors: [DmaDescriptor { flags: DmaDescriptorFlags { size: 4095, length: 4095, suc_eof: false, owner: "CPU" }, buffer: 0x3c020000, next: 0x3fc8aacc }, DmaDescriptor { flags: DmaDescriptorFlags { size: 4095, length: 4095, suc_eof: false, owner: "CPU" }, buffer: 0x3c020fff, next: 0x3fc8aad8 }, DmaDescriptor { flags: DmaDescriptorFlags { size: 2, length: 2, suc_eof: false, owner: "CPU" }, buffer: 0x3c021ffe, next: 0x0 }] }

Failing:

INFO - TX: 0x3c020000 len 8192 (3 descripters)
INFO - Alignment: 64, Chunk size: 4032
INFO - First dmabuffer: Some(DmaTxBuf { descriptors: DescriptorSet { descriptors: [DmaDescriptor { flags: DmaDescriptorFlags { size: 4095, length: 4095, suc_eof: false, owner: "CPU" }, buffer: 0x3c020000, next: 0x3fc8aacc }, DmaDescriptor { flags: DmaDescriptorFlags { size: 4095, length: 4095, suc_eof: false, owner: "CPU" }, buffer: 0x3c020fff, next: 0x3fc8aad8 }, DmaDescriptor { flags: DmaDescriptorFlags { size: 2, length: 2, suc_eof: false, owner: "CPU" }, buffer: 0x3c021ffe, next: 0x0 }] }, 

@i404788
Copy link
Contributor Author

i404788 commented Dec 23, 2024

After much testing it's seems to be an very perplexing issue. Ghidra shows effectively no difference between a working example and a broken example (apart from the delay moving):
image

Also of note it seems debug builds are always broken, esp-hal@main requires 1.83 so I haven't been able to test the previous compiler versions.

Further looking through Ghidra, some of the data sections do have a layout shift of 3:
image

So the only conclusion I can make is that, the layout of some unknown data (non-descriptor non-buffer?) is important.

@Dominaezzz
Copy link
Collaborator

I've run out of ideas for what could be going on but as a final hail mary, try adding a delay to the update function here. You can copy that into the gdma section and make it bigger, like 10 instead of just 1.

@bjoernQ
Copy link
Contributor

bjoernQ commented Dec 24, 2024

I tried the linked code and can reproduce the issue.

When I change DataMode to Dual it works fine - even at higher frequencies. On a device with octal-PSRAM it also works fine (with Quad)

Placing most of the involved code into RAM lets me go up to 32MHz
(maybe not all is needed but I used ESP_HAL_PLACE_SWITCH_TABLES_IN_RAM, ESP_HAL_PLACE_ANON_IN_RAM, ESP_HAL_PLACE_SPI_DRIVER_IN_RAM, put delay_micros, wait and main into RAM, removing the logs before wait )

Seems to be a bandwidth issue (i.e. placing code in RAM "eliminates" concurrently accessing flash)

@i404788
Copy link
Contributor Author

i404788 commented Dec 24, 2024

I'll try both suggestions and report back. @bjoernQ assuming it's the same issue I encountered; any chance it can be detected; I'd rather it panic or Err than hang.

@i404788
Copy link
Contributor Author

i404788 commented Dec 24, 2024

Hmm I put everything relevant in ram removed logs before wait, and added:

ESP_HAL_PLACE_SPI_DRIVER_IN_RAM = "true"
ESP_HAL_PLACE_ANON_IN_RAM = "true"
ESP_HAL_PLACE_SWITCH_TABLES_IN_RAM = "true"

However the env variables actually causes it to fail (commenting succeeds again). If it is flash contention, it seems like it would be at the half_duplex_write()-side rather than the wait() side, since placing a log after the delay works, but before doesn't.

@Dominaezzz I tried your hail-mary, unfortunately doesn't seem to change anything.

I'll see if I can get a minimal break condition (e.g. call to an empty function or similar) to maybe get a better look in ghidra.

@i404788
Copy link
Contributor Author

i404788 commented Dec 24, 2024

I've reduced the break condition to a few vec::pushes: https://github.com/i404788/esp-hal/blob/spi-dma-wip/examples/src/bin/spi_loopback_dma_psram2.rs (note new file). Even with black_box, ghidra shows large code shifts when changing the hard-coded bool which makes it hard to compare, I tried using bindiff but it seems like the ghidra plugin is broken on xtensa.

I've also tried manually allocating and generating some PSRAM traffic (see commented fail code) but that didn't seem to matter much.

In the fail=false condition, it just works.

In the fail=true condition without tuning I'm getting the original failure mode (infinite wait).
In the fail=true condition with the ram envs i'm getting:

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x28 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fce3818,len:0x16f8
load:0x403c9700,len:0x4
load:0x403c9704,len:0xc00
load:0x403cc700,len:0x2eb0
entry 0x403c9908
I (30) boot: ESP-IDF v5.1-beta1-378-gea5e0ff298-dirt 2nd stage bootloader
I (30) boot: compile time Jun  7 2023 08:07:32
I (31) boot: Multicore bootloader
I (35) boot: chip revision: v0.1
I (39) boot.esp32s3: Boot SPI Speed : 40MHz
I (44) boot.esp32s3: SPI Mode       : DIO
I (49) boot.esp32s3: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=00c20h (  3104) map
I (105) esp_image: segment 1: paddr=00010c48 vaddr=3fc8a968 size=040dch ( 16604) load
I (118) esp_image: segment 2: paddr=00014d2c vaddr=40378000 size=02968h ( 10600) load
I (125) esp_image: segment 3: paddr=0001769c vaddr=00000000 size=0897ch ( 35196)
I (139) esp_image: segment 4: paddr=00020020 vaddr=42000020 size=0c0b4h ( 49332) map
I (153) boot: Loaded app from partition at offset 0x10000
I (153) boot: Disabling RNG early entropy source...
INFO - Starting SPI loopback test
INFO - chip id = 1b5d0d
INFO - size is 2097152
INFO - PSRAM core_clock SpiTimingConfigCoreClock80m, flash_div = 1, psram_div = 2


====================== PANIC ======================
panicked at /home/null/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/panicking.rs:221:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`

Backtrace:

0x4200b878
0x4200b878 - core::slice::raw::from_raw_parts::precondition_check
    at /home/null/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/ub_checks.rs:70
0x4200b586
0x4200b586 - <esp_println::Printer as core::fmt::Write>::write_str
    at /home/null/Projects/esp-hal-spi/esp-println/src/lib.rs:82
0x42005237
0x42005237 - core::fmt::Formatter::pad
    at /home/null/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:1438
0x4200bb88
0x4200bb88 - <log::Level as core::fmt::Display>::fmt
    at /home/null/.cargo/registry/src/index.crates.io-6f17d22bba15001f/log-0.4.22/src/lib.rs:534
0x42005950
0x42005950 - core::fmt::rt::Argument::fmt
    at /home/null/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/rt.rs:177
0x4200b54c
0x4200b54c - <&mut W as core::fmt::Write::write_fmt::SpecWriteFmt>::spec_write_fmt
    at /home/null/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:226
0x42003372
0x42003372 - log::__private_api::log_impl
    at /home/null/.cargo/registry/src/index.crates.io-6f17d22bba15001f/log-0.4.22/src/__private_api.rs:61
0x4200c037
0x4200c037 - Reset
    at /home/null/Projects/esp-hal-spi/xtensa-lx-rt/src/lib.rs:82
0x40379aa6
0x40379aa6 - ESP32Reset
    at /home/null/Projects/esp-hal-spi/esp-hal/src/soc/esp32s3/mod.rs:164

Which seems to trigger before even starting a DMA transfer, which again would indicate a codegen layout issue.

@bjoernQ
Copy link
Contributor

bjoernQ commented Dec 24, 2024

The panic looks sus - we have seen some miscompiles / misoptimizations lately.

However I tried your changed code on 36095e4 plus my changes and some more changes and it seems to work

Here are my changes (on top of the latest commit on main)
diff --git a/esp-hal/src/delay.rs b/esp-hal/src/delay.rs
index a361aabc..5fb26144 100644
--- a/esp-hal/src/delay.rs
+++ b/esp-hal/src/delay.rs
@@ -68,6 +68,7 @@ impl Delay {
     }
 
     /// Delay for the specified number of microseconds
+    #[procmacros::ram]
     pub fn delay_micros(&self, us: u32) {
         let delay = MicrosDurationU64::micros(us as u64);
         self.delay(delay);
diff --git a/esp-hal/src/spi/master.rs b/esp-hal/src/spi/master.rs
index 48116c03..6e3d6d01 100644
--- a/esp-hal/src/spi/master.rs
+++ b/esp-hal/src/spi/master.rs
@@ -1316,6 +1316,7 @@ mod dma {
         /// This method blocks until the transfer is finished and returns the
         /// `SpiDma` instance and the associated buffer.
         #[instability::unstable]
+        #[procmacros::ram]
         pub fn wait(mut self) -> (SpiDma<'d, Dm, T>, Buf) {
             self.spi_dma.wait_for_idle();
             let retval = unsafe {
@@ -1581,6 +1582,7 @@ mod dma {
         #[allow(clippy::type_complexity)]
         #[cfg_attr(place_spi_driver_in_ram, ram)]
         #[instability::unstable]
+        #[ram]
         pub fn half_duplex_write<TX: DmaTxBuffer>(
             mut self,
             data_mode: DataMode,
diff --git a/examples/.cargo/config.toml b/examples/.cargo/config.toml
index 600f74b2..d25c3d15 100644
--- a/examples/.cargo/config.toml
+++ b/examples/.cargo/config.toml
@@ -35,5 +35,9 @@ GATEWAY_IP = "1.1.1.1"
 HOST_IP = "1.1.1.1"
 ESP_WIFI_CSI_ENABLE = "true"
 
+ESP_HAL_PLACE_SPI_DRIVER_IN_RAM = "true"
+ESP_HAL_PLACE_ANON_IN_RAM = "true"
+ESP_HAL_PLACE_SWITCH_TABLES_IN_RAM = "true"
+
 [unstable]
 build-std = ["alloc", "core"]
diff --git a/examples/src/bin/spi_loopback_dma_psram.rs b/examples/src/bin/spi_loopback_dma_psram.rs
index 41b785b4..61937507 100644
--- a/examples/src/bin/spi_loopback_dma_psram.rs
+++ b/examples/src/bin/spi_loopback_dma_psram.rs
@@ -16,13 +16,16 @@
 //! If your module is quad PSRAM then you need to change the `psram` feature in the
 //! in the features line below to `quad-psram`.
 
-//% FEATURES: esp-hal/log esp-hal/octal-psram esp-hal/unstable
+//% FEATURES: esp-hal/log esp-hal/quad-psram esp-hal/unstable
 //% CHIPS: esp32s3
 
 #![no_std]
 #![no_main]
+#![feature(vec_push_within_capacity)]
 
 use esp_backtrace as _;
+use esp_hal::macros::ram;
+use esp_hal::peripherals::SPI2;
 use esp_hal::{
     delay::Delay,
     dma::{DmaRxBuf, DmaTxBuf, ExternalBurstConfig},
@@ -51,13 +54,48 @@ macro_rules! dma_alloc_buffer {
     }};
 }
 
-const DMA_BUFFER_SIZE: usize = 8192;
 const DMA_ALIGNMENT: ExternalBurstConfig = ExternalBurstConfig::Size64;
 const DMA_CHUNK_SIZE: usize = 4096 - DMA_ALIGNMENT as usize;
+const DMA_BUFFER_SIZE: usize = 8192;
+
+// #[ram]
+fn effectively_empty(fail: bool) // -> bool
+{
+    const size: usize = 2;
+    let mut x: alloc::vec::Vec<usize> = alloc::vec::Vec::with_capacity(size);
+    // unsafe { x.set_len(size) };
+
+    // let layout = core::alloc::Layout::new::<[usize; size]>();
+    // let ptr = unsafe { alloc::alloc::alloc(layout) as *mut usize };
+    // let mut lsize = 0;
+
+    if fail {
+        // for i in 0..size {
+        //     // unsafe { *(ptr.add(i)) = i }
+        //     unsafe { core::ptr::write(ptr.add(i), i) };
+        //     let j = unsafe { *(ptr.add(i)) };
+        //     lsize += j;
+        // }
+        for i in 0..size {
+            // ram op
+            // x.push(i);
+            x.push_within_capacity(i);
+            // x.get(i).unwrap_or(&0);
+        }
+    } else {
+        for i in 0..size {
+            // nop
+            unsafe { core::ptr::read_volatile(SPI2::ptr().add(0x003C) as *const u32) };
+        }
+    }
+
+    // x[size - 2] % 2 == 0
+}
 
+#[link_section = ".rwtext"]
 #[entry]
 fn main() -> ! {
-    esp_println::logger::init_logger(log::LevelFilter::Info);
+    esp_println::logger::init_logger(log::LevelFilter::Trace);
     info!("Starting SPI loopback test");
     let peripherals = esp_hal::init(esp_hal::Config::default());
     esp_alloc::psram_allocator!(peripherals.PSRAM, esp_hal::psram);
@@ -67,6 +105,8 @@ fn main() -> ! {
     let mosi = peripherals.GPIO48;
     let miso = unsafe { mosi.clone_unchecked() };
     let cs = peripherals.GPIO38;
+    let sio2 = peripherals.GPIO7;
+    let sio3 = peripherals.GPIO6;
 
     let (_, tx_descriptors) =
         esp_hal::dma_descriptors_chunk_size!(0, DMA_BUFFER_SIZE, DMA_CHUNK_SIZE);
@@ -78,66 +118,85 @@ fn main() -> ! {
         tx_descriptors.len()
     );
     let mut dma_tx_buf =
-        DmaTxBuf::new_with_config(tx_descriptors, tx_buffer, DMA_ALIGNMENT).unwrap();
-    let (rx_buffer, rx_descriptors, _, _) = esp_hal::dma_buffers!(DMA_BUFFER_SIZE, 0);
-    info!(
-        "RX: {:p} len {} ({} descripters)",
-        rx_buffer.as_ptr(),
-        rx_buffer.len(),
-        rx_descriptors.len()
+        Some(DmaTxBuf::new_with_config(tx_descriptors, tx_buffer, DMA_ALIGNMENT).unwrap());
+
+    log::info!(
+        "Alignment: {}, Chunk size: {}",
+        ExternalBurstConfig::Size64 as usize,
+        DMA_CHUNK_SIZE
     );
-    let mut dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
-    // Need to set miso first so that mosi can overwrite the
-    // output connection (because we are using the same pin to loop back)
+
+    let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) =
+        esp_hal::dma_buffers!(DMA_BUFFER_SIZE);
+
+    let dma_rx_buf2 = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
+    let mut dma_tx_buf2 = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
+
     let mut spi = Spi::new(
         peripherals.SPI2,
         Config::default()
-            .with_frequency(100.kHz())
+            .with_frequency(25.MHz())
             .with_mode(Mode::Mode0),
     )
     .unwrap()
     .with_sck(sclk)
     .with_miso(miso)
     .with_mosi(mosi)
+    .with_sio2(sio2)
+    .with_sio3(sio3)
     .with_cs(cs)
     .with_dma(peripherals.DMA_CH0);
 
     delay.delay_millis(100); // delay to let the above messages display
 
-    for (i, v) in dma_tx_buf.as_mut_slice().iter_mut().enumerate() {
+    for (i, v) in dma_tx_buf
+        .as_mut()
+        .unwrap()
+        .as_mut_slice()
+        .iter_mut()
+        .enumerate()
+    {
         *v = (i % 256) as u8;
     }
 
+    log::info!("First dmabuffer: {:?}", dma_tx_buf.as_mut());
+
     let mut i = 0;
 
     loop {
-        dma_tx_buf.as_mut_slice()[0] = i;
-        *dma_tx_buf.as_mut_slice().last_mut().unwrap() = i;
-        i = i.wrapping_add(1);
-
+        use esp_hal::spi::master::{Address, Command};
+        let mut dma_buf = dma_tx_buf.take().unwrap();
         let transfer = spi
-            .transfer(dma_rx_buf.len(), dma_rx_buf, dma_tx_buf.len(), dma_tx_buf)
-            .map_err(|e| e.0)
+            .half_duplex_write(
+                esp_hal::spi::DataMode::Quad,
+                Command::None,
+                Address::Address24(0, esp_hal::spi::DataMode::Quad),
+                0u8,
+                dma_buf.len(),
+                dma_buf,
+            )
             .unwrap();
+        // delay.delay_micros(1000);
+        // delay.delay_micros(1000);
+        // delay.delay_micros(1000);
+        // let transfer = spi.write(dma_buf.len(), dma_buf).unwrap();
+        // log::info!("pre-SPI2: {}", unsafe {
+        //     core::ptr::read_volatile(SPI2::ptr().add(0x003C) as *const u32)
+        // });
+
+      //  core::hint::black_box(effectively_empty(false));
+
+     //   delay.delay_micros(1000);
+        // delay.delay_millis(1000);
+
+        // log::info!("post-SPI2: {}", unsafe {
+        //     core::ptr::read_volatile(SPI2::ptr().add(0x003C) as *const u32)
+        // });
+        (spi, dma_buf) = transfer.wait();
+        dma_tx_buf.replace(dma_buf);
+
+        log::info!("Done");
 
-        (spi, (dma_rx_buf, dma_tx_buf)) = transfer.wait();
-        for (i, v) in dma_tx_buf.as_mut_slice().iter_mut().enumerate() {
-            if dma_rx_buf.as_slice()[i] != *v {
-                error!(
-                    "Mismatch at index {}: expected {}, got {}",
-                    i,
-                    *v,
-                    dma_rx_buf.as_slice()[i]
-                );
-                break;
-            }
-        }
-        info!(
-            "{:0x?} .. {:0x?}",
-            &dma_rx_buf.as_slice()[..10],
-            &dma_rx_buf.as_slice().last_chunk::<10>().unwrap()
-        );
-        dma_tx_buf.as_mut_slice().reverse();
         delay.delay_millis(1000);
     }
-}
+}
\ No newline at end of file

(I copied your new example as the original one)

@i404788
Copy link
Contributor Author

i404788 commented Dec 24, 2024

@bjoernQ Can confirm your example works (similar to fail=false in my case), however I'm a bit concerned on the fragility. Adding the log back will halt it again, and so the thing I'm trying to find out is if half_duplex_write().wait() always works or if it just happens to work in this exact configuration. And those experiments do point toward fairly consistent miscompiles/layout mismatches, but I'll see if I can unexpectedly break it without inserting anything in between.

@i404788
Copy link
Contributor Author

i404788 commented Dec 24, 2024

Yeah it doesn't work in the project I originally found the issue (although I can't set ESP_HAL_PLACE_ANON_IN_RAM because it breaks embassy) even without anything in-between.

Update: I removed the .with_cpu_clock(CpuClock::max()) and for now it works again at 23MHz not yet at 25MHz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working status:needs-attention This should be prioritized
Projects
Status: Todo
Development

No branches or pull requests

3 participants