From f45c9fb5cb3a4e112f00f7ad695807761f8b73b7 Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Tue, 4 May 2021 16:39:59 +0200 Subject: [PATCH] Add Serial example --- Makefile | 22 ++++-- hello_sio.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 hello_sio.c diff --git a/Makefile b/Makefile index 7a2787b..c0bd769 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ TYPE = ps-exe SRCS = hello_world.c \ ../common/crt0/crt0.s \ -## Hello tile +#~ ## Hello tile #~ TARGET = hello_tile #~ TYPE = ps-exe @@ -19,14 +19,14 @@ SRCS = hello_world.c \ #~ SRCS = hello_pad.c \ #~ ../common/crt0/crt0.s \ -## Hello pad 2 +#~ ## Hello pad 2 #~ TARGET = hello_2pads #~ TYPE = ps-exe #~ SRCS = hello_2pads.c \ #~ ../common/crt0/crt0.s \ -## Hello poly +#~ ## Hello poly #~ TARGET = hello_poly #~ TYPE = ps-exe @@ -73,7 +73,7 @@ SRCS = hello_world.c \ #~ SRCS = hello_vag.c \ #~ ../common/crt0/crt0.s \ -#~ VAG/hello_poly.vag +#~ VAG/hello_poly.vag \ #~ ## hello multivag #~ TARGET = hello_multivag @@ -81,10 +81,10 @@ SRCS = hello_world.c \ #~ SRCS = hello_multivag.c \ #~ ../common/crt0/crt0.s \ -#~ VAG/hello.vag -#~ VAG/poly.vag +#~ VAG/hello.vag \ +#~ VAG/poly.vag \ -## Poly fun ! +#~ ## Poly fun ! #~ TARGET = fun_with_poly #~ TYPE = ps-exe @@ -104,6 +104,13 @@ SRCS = hello_world.c \ #~ ../common/crt0/crt0.s \ #~ TIM/cubetex.tim \ +#~ ## Hello sio +#~ TARGET = hello_sio +#~ TYPE = ps-exe + +#~ SRCS = hello_sio.c \ +#~ ../common/crt0/crt0.s \ + CPPFLAGS += -I../psyq/include LDFLAGS += -L../psyq/lib LDFLAGS += -Wl,--start-group @@ -128,6 +135,7 @@ LDFLAGS += -lsio LDFLAGS += -lsnd LDFLAGS += -lspu LDFLAGS += -ltap +LDFLAGS += -lcd LDFLAGS += -Wl,--end-group include ../common.mk \ diff --git a/hello_sio.c b/hello_sio.c new file mode 100644 index 0000000..10aeca7 --- /dev/null +++ b/hello_sio.c @@ -0,0 +1,219 @@ +// hello_sio example +// +// This example will display the RX data in a 64 char rolling buffer. +// +// Use minicom or any other serial comm program and a serial/USB cable. +// +// Relevant doc is libref47.pdf, l.1120-1127 +// +// Schnappy - 04/2021 +// +// Based on : ../psyq/psx/sample/serial/SIO +// +// sio echo back +// 1.00 Jan.28.1997 shino + +#include +#include +#include +#include +#include + +// Needed for SIO operations + +#include + +// Needed for manipulating strings + +#include + +// Display stuff (see hello_tile for the basics) + +#define VMODE 0 + +#define SCREENXRES 320 + +#define SCREENYRES 240 + +#define CENTERX SCREENXRES/2 + +#define CENTERY SCREENYRES/2 + +#define OTLEN 2048 // Maximum number of OT entries + +#define PRIMBUFFLEN 32768 // Maximum number of POLY_GT3 primitives + +// Display and draw environments, double buffered + +DISPENV disp[2]; + +DRAWENV draw[2]; + +u_long ot[2][OTLEN]; // Ordering table (contains addresses to primitives) + +char primbuff[2][PRIMBUFFLEN] = {0}; // Primitive list // That's our prim buffer + +char * nextpri = primbuff[0]; // Primitive counter + +short db = 0; // Current buffer counter + +// SIO + +#define MAX_CHARS 64 + +u_char SIO = 1; // Is SIO enabled ? + +u_char SIOinit = 0; // Is SIO initialized ? + +// Prototypes + +void init(void); + +void display(void); + +void init(){ + + // Reset the GPU before doing anything and the controller + + ResetGraph(0); + + // Set the display and draw environments + + SetDefDispEnv(&disp[0], 0, 0 , SCREENXRES, SCREENYRES); + + SetDefDispEnv(&disp[1], 0, SCREENYRES, SCREENXRES, SCREENYRES); + + SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); + + SetDefDrawEnv(&draw[1], 0, 0, SCREENXRES, SCREENYRES); + + // If in PAL mode, add vertical offset + + if (VMODE) + { + + SetVideoMode(MODE_PAL); + + disp[0].screen.y += 8; + + disp[1].screen.y += 8; + } + + setRGB0(&draw[0], 0, 0, 255); + + setRGB0(&draw[1], 0, 0, 255); + + draw[0].isbg = 1; + + draw[1].isbg = 1; + + PutDispEnv(&disp[db]); + + PutDrawEnv(&draw[db]); + + // Init font system + + FntLoad(960, 0); + + FntOpen(16, 16, 196, 64, 0, 256); + + } + +void display(void){ + + DrawSync(0); + + VSync(0); + + PutDispEnv(&disp[db]); + + PutDrawEnv(&draw[db]); + + SetDispMask(1); + + db = !db; + + } + +int main() { + + init(); + + // Main loop + + while (1) { + + // Buffer for the RX data of size MAX_CHARS + + static char buffer[ MAX_CHARS ] = {0}; + + // If SIO flag is set, initialize and get data + + if( SIO ){ + + // Is SIO is not initialized, dot it + + if( ! SIOinit ){ + + ResetCallback(); + + // Load SIO driver at 115200bps + + AddSIO(115200); + + ResetGraph(0); + + // Use _sio_control to clear driver status error-related bits + // See psyq's libref47.pdf, p.1125 for the commands and status tables + + _sio_control(2,1,0); + + SIOinit = 1; + + } + + // Limit input buffer to MAX_CHARS chars, making it a rolling buffer + + if( strlen(buffer) > MAX_CHARS ){ + + // If that limit is reached, remove first char in string + + memmove(buffer, buffer + 1, strlen(buffer)); + + } + + // Check if sio driver is able to write communications data + // If so, this means reading is not occuring + + if( _sio_control(0,0,0) & SR_RXRDY ){ // SR_RXRDY == 0x2 + + // Read byte + + char c = _sio_control(0,4,0); + + // Add to buffer + + strncat(buffer, &c, 1); + + } + + + } + + // END SIO FUN + + FntPrint("Hello Serial!\n\n"); + + if( buffer ){ + + FntPrint("%s", buffer); + + } + + FntFlush(-1); + + display(); + + } + return 0; +}