aboutsummaryrefslogtreecommitdiff
path: root/Source/Serial.c
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-07-20 22:36:19 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2017-07-20 22:36:19 +0200
commitcb1c0345c766fada621b521ca39aac02ae25056b (patch)
treee7d20a64ba04886a540e8387b023758dac361b24 /Source/Serial.c
parent98d3232ef413351380d2299af7058fc06a40ff86 (diff)
downloadairport-cb1c0345c766fada621b521ca39aac02ae25056b.tar.gz
+ Added support for SIO (Serial Input Output).
* On SystemLoadFileToBuffer(), files can be now uploaded to PSX using QPSXSerial.
Diffstat (limited to 'Source/Serial.c')
-rw-r--r--Source/Serial.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/Source/Serial.c b/Source/Serial.c
new file mode 100644
index 0000000..714581d
--- /dev/null
+++ b/Source/Serial.c
@@ -0,0 +1,102 @@
+/* *************************************
+ * Includes
+ * *************************************/
+
+#include "Serial.h"
+
+/* *************************************
+ * Defines
+ * *************************************/
+
+#define SERIAL_BAUDRATE 115200
+#define SERIAL_TX_RX_TIMEOUT 20000
+#define SERIAL_RX_FIFO_EMPTY 0
+#define SERIAL_TX_NOT_READY 0
+#define SERIAL_PRINTF_INTERNAL_BUFFER_SIZE 256
+
+/* **************************************
+ * Structs and enums *
+ * *************************************/
+
+typedef enum
+{
+ SERIAL_STATE_INIT = 0,
+ SERIAL_STATE_STANDBY,
+ SERIAL_STATE_WRITING_INITIAL
+}SERIAL_STATE;
+
+/* *************************************
+ * Local Variables
+ * *************************************/
+
+static volatile SERIAL_STATE SerialState;
+
+/* *************************************
+ * Local Prototypes
+ * *************************************/
+
+void SerialInit(void)
+{
+ SIOStart(115200);
+}
+
+bool SerialRead(uint8_t* ptrArray, size_t nBytes)
+{
+ if(nBytes == 0)
+ {
+ dprintf("SerialRead: invalid size %d\n", nBytes);
+ return false;
+ }
+
+ do
+ {
+ //uint16_t timeout = SERIAL_TX_RX_TIMEOUT;
+
+ while( (SIOCheckInBuffer() == SERIAL_RX_FIFO_EMPTY)); // Wait for RX FIFO not empty
+
+ *(ptrArray++) = SIOReadByte();
+ }while(--nBytes);
+
+ return true;
+}
+
+bool SerialWrite(void* ptrArray, size_t nBytes)
+{
+
+ if(nBytes == 0)
+ {
+ dprintf("SerialWrite: invalid size %d\n", nBytes);
+ return false;
+ }
+
+ do
+ {
+ //uint16_t timeout = SERIAL_TX_RX_TIMEOUT;
+
+ while( (SIOCheckOutBuffer() == SERIAL_TX_NOT_READY)); // Wait for TX FIFO empty.
+
+ SIOSendByte(*(uint8_t*)ptrArray++);
+
+ }while(--nBytes);
+
+ return true;
+}
+
+#ifdef SERIAL_INTERFACE
+void Serial_printf(const char* str, ...)
+{
+ va_list ap;
+ int result;
+ static char internal_buffer[SERIAL_PRINTF_INTERNAL_BUFFER_SIZE];
+
+ va_start(ap, str);
+
+ result = vsnprintf( internal_buffer,
+ SERIAL_PRINTF_INTERNAL_BUFFER_SIZE,
+ str,
+ ap );
+
+ SerialWrite(internal_buffer, result);
+}
+#endif // SERIAL_INTERFACE
+