From 3f378517e7e482f5a36eb3555b9ca3e2e8ca0f6e Mon Sep 17 00:00:00 2001 From: nino-porcino Date: Tue, 18 Jan 2022 15:20:29 +0100 Subject: [PATCH] client sdcard test --- .../sdcard}/apple1_sdcard/apple1_sdcard.ino | 121 +++++++++--------- demos/sdcard/m.bat | 1 + demos/sdcard/sdcard.c | 95 ++++++++++++++ 3 files changed, 160 insertions(+), 57 deletions(-) rename {sdcard => demos/sdcard}/apple1_sdcard/apple1_sdcard.ino (50%) create mode 100644 demos/sdcard/m.bat create mode 100644 demos/sdcard/sdcard.c diff --git a/sdcard/apple1_sdcard/apple1_sdcard.ino b/demos/sdcard/apple1_sdcard/apple1_sdcard.ino similarity index 50% rename from sdcard/apple1_sdcard/apple1_sdcard.ino rename to demos/sdcard/apple1_sdcard/apple1_sdcard.ino index 234de32..4ddbe5b 100644 --- a/sdcard/apple1_sdcard/apple1_sdcard.ino +++ b/demos/sdcard/apple1_sdcard/apple1_sdcard.ino @@ -11,44 +11,59 @@ #define D6 7 /* I/O data bit 6, connects to PA6 on the VIA */ #define D7 8 /* I/O data bit 7, connects to PA7 on the VIA */ -#define CPU_CLOCK 9 /* cpu send data clock, VIA.PB0 => MCU */ -#define MCU_CLOCK 10 /* mcu send data clock, VIA.PB1 <= MCU */ +#define CPU_STROBE 9 /* PB0 => MCU 1=cpu byte is available on the data port; 0 cpu is waiting */ +#define MCU_STROBE 10 /* PB1 <= MCU 1=mcu byte is available on the data port; 0 mcu is waiting */ -// utility function for read a bit -#define BIT(data,n) (((data) >> (n)) & 1) +// indicates that a timeout occurred during wait() +int TIMEOUT = 0; void setup() { // debug on serial Serial.begin(9600); + + pinMode(CPU_STROBE, INPUT); + pinMode(MCU_STROBE, OUTPUT); - pinMode(CPU_CLOCK, INPUT); - pinMode(MCU_CLOCK, OUTPUT); + digitalWrite(MCU_STROBE, LOW); } void loop() { // applicazione di esempio: manda un messaggio quando riceve il comando 42 + TIMEOUT = 0; + int data = receive_byte_from_cpu(); - if(data == 42) { + if(data == 42 && !TIMEOUT) { + Serial.println("command 42 received from CPU"); char *msg = "HELLO WORLD!\r\n"; for(int t=0; t 500) { + TIMEOUT = 1; + break; + } + } } -void receive_byte_from_cpu() { - - // when here, both CPU_CLOCK and MCU_CLOCK are LOW - +int receive_byte_from_cpu() { + // set data port as input pinMode(D0, INPUT); pinMode(D1, INPUT); @@ -57,14 +72,13 @@ void receive_byte_from_cpu() { pinMode(D4, INPUT); pinMode(D5, INPUT); pinMode(D6, INPUT); - pinMode(D7, INPUT); - - // tells CPU we are ready to receive - digitalWrite(MCU_CLOCK, HIGH); - - // wait data to be signaled as "ready" on the CPU - wait(CPU_CLOCK, HIGH); + pinMode(D7, INPUT); + // both strobes are 0 + + // CPU deposits data byte and sets strobe high + wait(CPU_STROBE, HIGH); + // read the data byte int data = (digitalRead(D0) << 0) | @@ -76,23 +90,19 @@ void receive_byte_from_cpu() { (digitalRead(D6) << 6) | (digitalRead(D7) << 7); - // tells CPU data was received - digitalWrite(MCU_CLOCK, HIGH); - - // wait the CPU to end operation - wait(CPU_CLOCK, LOW); - - // we also terminate - digitalWrite(MCU_CLOCK, LOW); - - // when here, both CPU_CLOCK and MCU_CLOCK are LOW - - return byte; + // after reading the byte, MCU sets strobe high + digitalWrite(MCU_STROBE, HIGH); + + // CPU now sets strobe low + wait(CPU_STROBE, LOW); + + // and MCU sets strobe low + digitalWrite(MCU_STROBE, LOW); + + return data; } void send_byte_to_cpu(int data) { - - // when here, both CPU_CLOCK and MCU_CLOCK are LOW // set data port as output pinMode(D0, OUTPUT); @@ -103,31 +113,28 @@ void send_byte_to_cpu(int data) { pinMode(D5, OUTPUT); pinMode(D6, OUTPUT); pinMode(D7, OUTPUT); - - // wait until CPU is ready to receive (should be already) - wait(CPU_CLOCK, LOW); - // put byte on D0-D7 - digitalWrite(D0, BIT(data,0) ); - digitalWrite(D1, BIT(data,1) ); - digitalWrite(D2, BIT(data,2) ); - digitalWrite(D3, BIT(data,3) ); - digitalWrite(D4, BIT(data,4) ); - digitalWrite(D5, BIT(data,5) ); - digitalWrite(D6, BIT(data,6) ); - digitalWrite(D7, BIT(data,7) ); + // both strobes are 0 - // tells CPU byte is ready on the data port - digitalWrite(MCU_CLOCK, HIGH); + // put byte on the data port + digitalWrite(D0, data & 1); + digitalWrite(D1, data & 2); + digitalWrite(D2, data & 4); + digitalWrite(D3, data & 8); + digitalWrite(D4, data & 16); + digitalWrite(D5, data & 32); + digitalWrite(D6, data & 64); + digitalWrite(D7, data & 128); - // wait for CPU to read the data - wait(CPU_CLOCK, HIGH); + // after depositing data byte, MCU sets strobe high + digitalWrite(MCU_STROBE, HIGH); + + // wait for CPU to set strobe high + wait(CPU_STROBE, HIGH); // tells CPU byte we are finished - digitalWrite(MCU_CLOCK, LOW); - - // wait for CPU end as well - wait(CPU_CLOCK, LOW); - - // when here, both CPU_CLOCK and MCU_CLOCK are LOW + digitalWrite(MCU_STROBE, LOW); + + // wait for CPU to set strobe low + wait(CPU_STROBE, LOW); } diff --git a/demos/sdcard/m.bat b/demos/sdcard/m.bat new file mode 100644 index 0000000..fbba712 --- /dev/null +++ b/demos/sdcard/m.bat @@ -0,0 +1 @@ +call ..\..\tools\build sdcard \ No newline at end of file diff --git a/demos/sdcard/sdcard.c b/demos/sdcard/sdcard.c new file mode 100644 index 0000000..e3572c0 --- /dev/null +++ b/demos/sdcard/sdcard.c @@ -0,0 +1,95 @@ +#include +#include + +byte *const PORTB = (byte *) 0xA000; // port B register +byte *const PORTA = (byte *) 0xA001; // port A register +byte *const DDRB = (byte *) 0xA002; // port A data direction register +byte *const DDRA = (byte *) 0xA003; // port B data direction register + +#define CPU_STROBE(v) (*PORTB = (v)) /* CPU strobe is bit 0 OUTPUT */ +#define MCU_STROBE (*PORTB & 128) /* MCU strobe is bit 7 INPUT */ + +byte TIMEOUT = 0; + +void wait_mcu_strobe(byte v) { + if(TIMEOUT) return; + + unsigned int time = 0; + while((v==0 && MCU_STROBE != 0) || (v!=0 && MCU_STROBE == 0)) { + time++; + if(time > 5000) { + TIMEOUT = 1; + return; + } + } +} + +void send_byte_to_MCU(byte data) { + *DDRA = 0xFF; // set port A as output + *PORTA = data; // deposit byte on the data port + CPU_STROBE(1); // set strobe high + wait_mcu_strobe(1); // wait for the MCU to read the data + CPU_STROBE(0); // set strobe low + wait_mcu_strobe(0); // wait for the MCU to set strobe low +} + +byte receive_byte_from_MCU() { + *DDRA = 0; // set port A as input + CPU_STROBE(0); // set listen + wait_mcu_strobe(1); // wait for the MCU to deposit data + byte data = *PORTA; // read data + CPU_STROBE(1); // set strobe high + wait_mcu_strobe(0); // wait for the MCU to set strobe low + CPU_STROBE(0); // set strobe low + return data; +} + +void VIA_init() { + *DDRB = 1; // pin 1 is output (CPU_STROBE), pin7 is input (MCU_STROBE) + CPU_STROBE(0); // initial state +} + +void messagio_test() { + + TIMEOUT = 0; // resetta il timeout + send_byte_to_MCU(42); // manda il comando 42 + + if(TIMEOUT) { + woz_puts("\rTIMEOUT\r"); + return; + } + + // legge la stringa di ritorno + while(1) { + byte data = receive_byte_from_MCU(); + if(TIMEOUT) { + woz_puts("\rTIMEOUT\r"); + break; + } + if(data == 0) break; // end of string + woz_putc(data); + } +} + +void main() { + + VIA_init(); + + woz_puts("\rMCU TEST\r\r"); + woz_puts("[4] SEND 42 TO MCU\r"); + woz_puts("[0] EXIT\r"); + + byte data; + + // loop continuo + while(1) { + byte key = apple1_getkey(); + + if(key == '4') messagio_test(); + else if(key == '0') { + woz_puts("BYE\r"); + woz_mon(); + } + } +} +