From 0917aba7cd6a21fec48dd83e12ac3940aae6bdef Mon Sep 17 00:00:00 2001 From: dweller Date: Wed, 18 Sep 2024 15:25:16 +0300 Subject: writing seems to work! --- main.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 48 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 9985c59..ef6da45 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,8 @@ #include "hardware/gpio.h" #include "hardware/pio.h" #include "hardware/clocks.h" +#include "pico/multicore.h" +#include "pico/sync.h" #include "ws2812.pio.h" @@ -18,9 +20,6 @@ #include "tusb_desc.c" -#define PIN_RGB 16 - - #define COL_GRN 0x33000000 #define COL_RED 0x00330000 #define COL_BLU 0x00003300 @@ -45,6 +44,8 @@ * 17 pins */ +#define PIN_RGB 16 + enum { SR_IN = 0, @@ -56,7 +57,7 @@ enum SR_PIN_CNT }; -const uint8_t sr_pins[SR_PIN_CNT] = +const static uint8_t sr_pins[SR_PIN_CNT] = { [SR_IN] = 9, [SR_nOE] = 10, @@ -65,7 +66,7 @@ const uint8_t sr_pins[SR_PIN_CNT] = [SR_nCLR] = 13, }; -const uint8_t sr_pins_def[SR_PIN_CNT] = +const static uint8_t sr_pins_def[SR_PIN_CNT] = { [SR_IN] = 0, [SR_nOE] = 1, @@ -84,14 +85,14 @@ enum ROM_PIN_CNT }; -const uint8_t rom_pins[ROM_PIN_CNT] = +const static uint8_t rom_pins[ROM_PIN_CNT] = { [ROM_nWE] = 14, [ROM_nOE] = 15, [ROM_nCE] = 26, }; -const uint8_t rom_pins_def[ROM_PIN_CNT] = +const static uint8_t rom_pins_def[ROM_PIN_CNT] = { [ROM_nWE] = 1, [ROM_nOE] = 1, @@ -102,30 +103,64 @@ const uint8_t rom_pins_def[ROM_PIN_CNT] = #define PIN_IO_OE 29 #define PIN_IO_OE_DEF 1 -const uint8_t io_pins[8] = +const static uint8_t io_pins[8] = { + 2, 3, 4, 5, 6, 7, - 8, 27, 28 }; - -#define EEPROM_BLK_SZ 64 #define BLKS 64 -#define BLK_SZ 512 +#define BLK_SZ CFG_TUD_MSC_EP_BUFSIZE +#define EEPROM_BLK_SZ 64 + +static semaphore_t core1_sem; +static mutex_t busy_lock; +static uint16_t pending = 0; +static uint16_t writeoffset = 0; +static uint8_t writebuf[BLK_SZ] = {0}; +static uint64_t access = 0; -uint8_t data[BLK_SZ * BLKS]; -uint64_t access = 0; +// XXX: assumes stock clockspeed of 125MHz #define sleep_ns(ns) \ do{ for(int i = 0; i < ((ns) / 8); i++) __asm volatile ("nop\n"); }while(0) +static void readmode(void) +{ + gpio_put(PIN_IO_OE, 0); + + for(int i = 0; i < 8; i++) + { + gpio_init(io_pins[i]); + gpio_set_dir(io_pins[i], GPIO_IN); + gpio_pull_up(io_pins[i]); + } + + gpio_put(PIN_IO_OE, 1); +} + + +static void writemode(void) +{ + gpio_put(PIN_IO_OE, 0); + + for(int i = 0; i < 8; i++) + { + gpio_init(io_pins[i]); + gpio_set_dir(io_pins[i], GPIO_OUT); + gpio_put(io_pins[i], 0); + } + + gpio_put(PIN_IO_OE, 1); +} + static void set_addr(uint16_t addr) { gpio_put(sr_pins[SR_nOE], 1); @@ -152,19 +187,57 @@ static void set_addr(uint16_t addr) sleep_ns(24); } +void core1_main(void) +{ + while(1) + { + sem_acquire_blocking(&core1_sem); + { + mutex_enter_blocking(&busy_lock); + writemode(); + gpio_put(rom_pins[ROM_nOE], 1); + + int pages = (pending / EEPROM_BLK_SZ) + ((pending % EEPROM_BLK_SZ) ? 1 : 0); + for(int i = 0; i < pages; i++) + { + for(int j = 0; j < EEPROM_BLK_SZ; j++) + { + int off = i * EEPROM_BLK_SZ + j; + if(off >= pending) break; + + set_addr(writeoffset + off); + + for(int k = 0; k < 8; k++) + gpio_put(io_pins[k], (writebuf[off] >> k) & 1); + + sleep_ns(50); + + gpio_put(rom_pins[ROM_nWE], 0); + sleep_ns(150); + + gpio_put(rom_pins[ROM_nWE], 1); + sleep_ns(50); + } + + busy_wait_ms(10); + } + printf("core1: wrote %d bytes to %04x\n", pending, writeoffset); + pending = 0; + mutex_exit(&busy_lock); + } + } +} int main(void) { board_init(); tusb_init(); - stdio_init_all(); gpio_init(PIN_RGB); gpio_set_dir(PIN_RGB, GPIO_OUT); gpio_put(PIN_RGB, 0); - gpio_init(PIN_IO_OE); gpio_set_dir(PIN_IO_OE, GPIO_OUT); gpio_put(PIN_IO_OE, PIN_IO_OE_DEF); @@ -183,15 +256,7 @@ int main(void) gpio_put(rom_pins[i], rom_pins_def[i]); } - for(int i = 0; i < 8; i++) - { - gpio_init(io_pins[i]); - gpio_set_dir(io_pins[i], GPIO_IN); - gpio_pull_up(io_pins[i]); - } - - gpio_put(PIN_IO_OE, 1); - + readmode(); int sm = 0; // XXX: ??? uint32_t offset = pio_add_program(pio0, &ws2812_program); @@ -199,24 +264,22 @@ int main(void) ws2812_program_init(pio0, sm, offset, PIN_RGB, 800000, false); pio_sm_put_blocking(pio0, 0, 0); + sem_init(&core1_sem, 0, 1); + mutex_init(&busy_lock); + + multicore_reset_core1(); + multicore_launch_core1(core1_main); + sleep_ms(1000); + + printf("Hell, world...\n"); + uint32_t prev = access; uint32_t start_ms = 0; uint32_t interval = 60; int state = 1; int prevs = state; - - printf("Hell, world...\n"); - - const char msg[] = "Hell, world!\n" - "This is a test string embedded into fake block device to serve as a test\n" - "of USB Mass Storage on Raspberry Pi Pico.\n" - "\n" - "- dweller from cabin.digital\n"; - - memcpy(data, msg, sizeof(msg)); - while(1) { tud_task(); @@ -284,18 +347,30 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { printf("scsi: stop\n"); + + if(load_eject) + { + if(start) {} // load disk storage + else {} // unload disk storage + } + return true; } int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { - if(lba >= BLK_SZ) return -1; + access++; + + if(lba >= BLKS) return -1; + if(bufsize > BLK_SZ) return -1; + + if(!mutex_try_enter(&busy_lock, NULL)) return 0; int addr = (lba * BLK_SZ) + offset; - //memcpy(buffer, data + addr, bufsize); - access++; printf("scsi: read @ %04X\n", addr); + readmode(); + uint8_t* bytes = buffer; for(int i = 0; i < bufsize; i++) { @@ -305,8 +380,8 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buff sleep_ns(150); uint8_t byte = 0; - for(int i = 0; i < 8; i++) - byte |= gpio_get(io_pins[i]) << i; + for(int j = 0; j < 8; j++) + byte |= gpio_get(io_pins[j]) << j; bytes[i] = byte; @@ -315,6 +390,7 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buff } + mutex_exit(&busy_lock); return (int32_t)bufsize; } @@ -327,16 +403,21 @@ bool tud_msc_is_writable_cb(uint8_t lun) int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) { - if(lba >= BLK_SZ) return -1; - - int addr = (lba * BLK_SZ) + offset; - memcpy(data + addr, buffer, bufsize); access++; - printf("scsi: write @ %04x\n", addr); + if(lba >= BLKS) return -1; + if(bufsize > BLK_SZ) return -1; + if(!mutex_try_enter(&busy_lock, NULL)) return 0; - for(int i = 0; i < BLK_SZ; i++) - set_addr(addr + i); + writeoffset = (lba * BLK_SZ) + offset; + + printf("scsi: write req @ %04x\n", writeoffset); + + memcpy(writebuf, buffer, bufsize); + pending = bufsize; + + sem_release(&core1_sem); + mutex_exit(&busy_lock); return (int32_t) bufsize; } -- cgit v1.2.3