summaryrefslogtreecommitdiff
path: root/src/Serial.c
blob: 4bfb45c25405d6a887511b2143e69bea14615bf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* *************************************
 *  Includes
 * *************************************/

#include "Serial.h"
#include "Interrupts.h"
#include <psxsio.h>
#include <stdarg.h>
#include <stdio.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                   *
 * *************************************/

/* *************************************
 *  Local Variables
 * *************************************/

/* *************************************
 *  Local Prototypes
 * *************************************/

void SerialInit(void)
{
    SIOStart(115200);
}

void SerialRead(uint8_t *ptrArray, size_t nBytes)
{
    if (nBytes)
    {
        InterruptsDisableInt(INT_SOURCE_VBLANK);

        do
        {
            while ( (SIOCheckInBuffer() == SERIAL_RX_FIFO_EMPTY)); // Wait for RX FIFO not empty

            *(ptrArray++) = SIOReadByte();
        } while (--nBytes);

        InterruptsEnableInt(INT_SOURCE_VBLANK);
    }
}

void SerialWrite(const void* ptrArray, size_t nBytes)
{
    if (nBytes)
    {
        InterruptsDisableInt(INT_SOURCE_VBLANK);
        do
        {
            while ( (SIOCheckOutBuffer() == SERIAL_TX_NOT_READY)); // Wait for TX FIFO empty.

            SIOSendByte(*(uint8_t*)ptrArray++);

        } while (--nBytes);

        InterruptsEnableInt(INT_SOURCE_VBLANK);
    }
    else
    {
    }
}

#ifdef SERIAL_INTERFACE
void Serial_printf(const char* const 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