aboutsummaryrefslogtreecommitdiff
path: root/examples/io/pads
diff options
context:
space:
mode:
Diffstat (limited to 'examples/io/pads')
-rw-r--r--examples/io/pads/CMakeLists.txt4
-rw-r--r--examples/io/pads/main.c37
-rw-r--r--examples/io/pads/spi.c50
-rw-r--r--examples/io/pads/spi.h25
4 files changed, 56 insertions, 60 deletions
diff --git a/examples/io/pads/CMakeLists.txt b/examples/io/pads/CMakeLists.txt
index 5bd7f5d..cf5f817 100644
--- a/examples/io/pads/CMakeLists.txt
+++ b/examples/io/pads/CMakeLists.txt
@@ -3,10 +3,6 @@
cmake_minimum_required(VERSION 3.20)
-if(NOT DEFINED CMAKE_TOOLCHAIN_FILE AND DEFINED ENV{PSN00BSDK_LIBS})
- set(CMAKE_TOOLCHAIN_FILE $ENV{PSN00BSDK_LIBS}/cmake/sdk.cmake)
-endif()
-
project(
pads
LANGUAGES C ASM
diff --git a/examples/io/pads/main.c b/examples/io/pads/main.c
index 92beb1c..d100482 100644
--- a/examples/io/pads/main.c
+++ b/examples/io/pads/main.c
@@ -118,16 +118,16 @@ static volatile uint8_t pad_buff[2][34];
static volatile size_t pad_buff_len[2];
static volatile uint32_t pad_digital_only[2] = { 0, 0 };
-// Just a wrapper around spi_new_request(). This does not send the command
+// Just a wrapper around SPI_CreateRequest(). This does not send the command
// immediately but adds it to the driver's request queue.
void send_pad_cmd(
- uint32_t port,
- PAD_COMMAND cmd,
- uint8_t arg1,
- uint8_t arg2,
- SPICALLBACK callback
+ uint32_t port,
+ PadCommand cmd,
+ uint8_t arg1,
+ uint8_t arg2,
+ SPI_Callback callback
) {
- SPIREQUEST *req = spi_new_request();
+ SPI_Request *req = SPI_CreateRequest();
req->len = 9;
req->port = port;
@@ -150,12 +150,12 @@ void send_pad_cmd(
// actually a DualShock in digital mode by checking if it started identifying
// as CONFIG_MODE after receiving a configuration command.
void dualshock_init_cb(uint32_t port, const volatile uint8_t *buff, size_t rx_len) {
- PADTYPE *pad = (PADTYPE *) buff;
+ PadResponse *pad = (PadResponse *) buff;
if (
(rx_len < 2) ||
- (pad->raw.prefix != 0x5a) ||
- (pad->raw.type != PAD_ID_CONFIG_MODE)
+ (pad->prefix != 0x5a) ||
+ (pad->type != PAD_ID_CONFIG_MODE)
) {
printf("no, pad is digital-only (len = %d)\n", rx_len);
@@ -187,7 +187,7 @@ void poll_cb(uint32_t port, const volatile uint8_t *buff, size_t rx_len) {
if (rx_len)
memcpy((void *) pad_buff[port], (void *) buff, rx_len);
- PADTYPE *pad = (PADTYPE *) buff;
+ PadResponse *pad = (PadResponse *) buff;
// If this pad identifies as a digital pad and hasn't been flagged as a
// digital-only pad already, attempt to put it into analog mode by entering
@@ -196,8 +196,8 @@ void poll_cb(uint32_t port, const volatile uint8_t *buff, size_t rx_len) {
// returning digital pad responses.
if (
rx_len &&
- (pad->raw.prefix == 0x5a) &&
- (pad->raw.type == PAD_ID_DIGITAL)
+ (pad->prefix == 0x5a) &&
+ (pad->type == PAD_ID_DIGITAL)
) {
if (!pad_digital_only[port]) {
printf("Detecting if pad %d supports config mode... ", port + 1);
@@ -221,7 +221,7 @@ static CONTEXT ctx;
int main(int argc, const char* argv[]) {
init_context(&ctx);
- spi_init(&poll_cb);
+ SPI_Init(&poll_cb);
uint32_t counter = 0;
@@ -238,15 +238,14 @@ int main(int argc, const char* argv[]) {
continue;
}
- PADTYPE *pad = (PADTYPE *) pad_buff[port];
- PAD_TYPEID type = pad->raw.type;
+ PadResponse *pad = (PadResponse *) pad_buff[port];
// According to nocash docs, there is a hardware bug in DualShock
// controllers that causes the prefix byte (normally 0x5a) to turn
// into 0x00 if the analog button is pressed after configuration
// commands have been used. Thus making sure the prefix is 0x5a
// isn't enough to reliably detect pads.
- /*if ((pad->raw.prefix != 0x5a) && (type != PAD_ID_ANALOG)) {
+ /*if ((pad->prefix != 0x5a) && (pad->type != PAD_ID_ANALOG)) {
FntPrint(-1, "\n\nPORT %d: INVALID RESPONSE\n", port + 1);
if ((counter % 64) < 32)
FntPrint(-1, " CHECK CONNECTION...");
@@ -258,8 +257,8 @@ int main(int argc, const char* argv[]) {
-1,
"\n\nPORT %d: %s (TYPE=%d)\n",
port + 1,
- PAD_TYPEIDS[type],
- type
+ PAD_TYPEIDS[pad->type],
+ pad->type
);
// Print a hexdump of the payload returned by the pad.
diff --git a/examples/io/pads/spi.c b/examples/io/pads/spi.c
index e01b3f6..ef75ffc 100644
--- a/examples/io/pads/spi.c
+++ b/examples/io/pads/spi.c
@@ -54,21 +54,21 @@
/* Internal structures and globals */
-typedef struct _SPICONTEXT {
- uint8_t tx_buff[SPI_BUFF_LEN];
- uint8_t rx_buff[SPI_BUFF_LEN];
- uint32_t tx_len, rx_len, port;
- SPICALLBACK callback;
-} SPICONTEXT;
+typedef struct _SPI_CONTEXT {
+ uint8_t tx_buff[SPI_BUFF_LEN];
+ uint8_t rx_buff[SPI_BUFF_LEN];
+ uint32_t tx_len, rx_len, port;
+ SPI_Callback callback;
+} SPI_Context;
-static volatile SPICONTEXT ctx;
-static volatile SPIREQUEST volatile *current_req;
-static SPICALLBACK default_cb;
+static volatile SPI_Context ctx;
+static volatile SPI_Request volatile *current_req;
+static SPI_Callback default_cb;
/* Request queue management */
-static void prepare_poll_req(void) {
- PADREQUEST *req = (PADREQUEST *) ctx.tx_buff;
+static void _spi_create_poll_req(void) {
+ PadRequest *req = (PadRequest *) ctx.tx_buff;
req->addr = 0x01;
req->cmd = PAD_CMD_READ;
@@ -82,7 +82,7 @@ static void prepare_poll_req(void) {
ctx.callback = default_cb;
}
-static void prepare_next_req(void) {
+static void _spi_next_req(void) {
// Copy the contents of the first request in the queue into the TX buffer.
memcpy((void *) ctx.tx_buff, (void *) current_req->data, current_req->len);
@@ -93,7 +93,7 @@ static void prepare_next_req(void) {
// Pop the first request from the queue by deallocating it and adjusting
// the pointer to the first queue item.
- SPIREQUEST *next = current_req->next;
+ SPI_Request *next = current_req->next;
free((void *) current_req);
current_req = next;
@@ -101,7 +101,7 @@ static void prepare_next_req(void) {
/* Interrupt handlers */
-static void poll_timer_tick(void) {
+static void _spi_poll_irq_handler(void) {
// Fetch the last response byte, which wasn't followed by a pulse on /ACK,
// from the RX FIFO.
if (JOY_STAT & 0x0002)
@@ -112,9 +112,9 @@ static void poll_timer_tick(void) {
// If the request queue is empty, create a pad polling request.
if (current_req)
- prepare_next_req();
+ _spi_next_req();
else
- prepare_poll_req();
+ _spi_create_poll_req();
// Prepare the SPI port by clearing any pending IRQ, pulling /CS high and
// enabling the /ACK IRQ. In order to communicate with controllers, /CS has
@@ -132,7 +132,7 @@ static void poll_timer_tick(void) {
JOY_TXRX = ctx.tx_buff[0];
}
-static void spi_ack_handler(void) {
+static void _spi_ack_irq_handler(void) {
// Wait until /ACK is pulled up by the controller before sending the next
// byte. According to nocash docs, this has to be done before resetting the
// IRQ.
@@ -166,8 +166,8 @@ static void spi_ack_handler(void) {
/* Public API */
-SPIREQUEST *spi_new_request(void) {
- SPIREQUEST *req = malloc(sizeof(SPIREQUEST));
+SPI_Request *SPI_CreateRequest(void) {
+ SPI_Request *req = malloc(sizeof(SPI_Request));
req->len = 0;
req->port = 0;
@@ -179,7 +179,7 @@ SPIREQUEST *spi_new_request(void) {
if (!current_req) {
current_req = req;
} else {
- volatile SPIREQUEST *volatile last = current_req;
+ volatile SPI_Request *volatile last = current_req;
while (last->next)
last = last->next;
@@ -189,7 +189,7 @@ SPIREQUEST *spi_new_request(void) {
return req;
}
-void spi_set_poll_rate(uint32_t value) {
+void SPI_SetPollRate(uint32_t value) {
TIM_CTRL(2) = 0x0258; // CLK/8 input, IRQ on reload, disable one-shot IRQ
if (value < 65)
@@ -198,21 +198,21 @@ void spi_set_poll_rate(uint32_t value) {
TIM_RELOAD(2) = (F_CPU / 8) / value;
}
-void spi_init(SPICALLBACK callback) {
+void SPI_Init(SPI_Callback callback) {
// Disable the BIOS timer handler (which for some stupid reason is enabled
// by default, even though it does nothing) and set up custom interrupt
// handlers.
EnterCriticalSection();
ChangeClearRCnt(2, 0);
- InterruptCallback(6, &poll_timer_tick);
- InterruptCallback(7, &spi_ack_handler);
+ InterruptCallback(6, &_spi_poll_irq_handler);
+ InterruptCallback(7, &_spi_ack_irq_handler);
ExitCriticalSection();
JOY_CTRL = 0x0040; // Reset all registers
JOY_MODE = 0x000d; // 1x multiplier, 8 data bits, no parity
JOY_BAUD = 0x0088; // 250000 bps
- spi_set_poll_rate(250);
+ SPI_SetPollRate(250);
current_req = 0;
default_cb = callback;
}
diff --git a/examples/io/pads/spi.h b/examples/io/pads/spi.h
index 1c473cd..c50e065 100644
--- a/examples/io/pads/spi.h
+++ b/examples/io/pads/spi.h
@@ -9,23 +9,24 @@
#include <stdint.h>
#include <psxpad.h>
+// Maximum request/response length (34 bytes for pads, 140 for memory cards)
//#define SPI_BUFF_LEN 34
#define SPI_BUFF_LEN 140
/* Request structures */
-typedef void (*SPICALLBACK)(uint32_t port, const volatile uint8_t *buff, size_t rx_len);
+typedef void (*SPI_Callback)(uint32_t port, const volatile uint8_t *buff, size_t rx_len);
-typedef struct _SPIREQUEST {
+typedef struct _SPI_Request {
union {
- uint8_t data[SPI_BUFF_LEN];
- PADREQUEST pad_req;
- MCDREQUEST mcd_req;
+ uint8_t data[SPI_BUFF_LEN];
+ PadRequest pad_req;
+ MemCardRequest mcd_req;
};
- uint32_t len, port;
- SPICALLBACK callback;
- struct _SPIREQUEST *next;
-} SPIREQUEST;
+ uint32_t len, port;
+ SPI_Callback callback;
+ struct _SPI_Request *next;
+} SPI_Request;
/* Public API */
@@ -34,7 +35,7 @@ typedef struct _SPIREQUEST {
* object must be populated afterwards by setting the length, callback and
* filling in the TX data buffer.
*/
-SPIREQUEST *spi_new_request(void);
+SPI_Request *SPI_CreateRequest(void);
/**
* @brief Changes the controller polling rate. The lowest supported rate is 65
@@ -43,7 +44,7 @@ SPIREQUEST *spi_new_request(void);
*
* @param value
*/
-void spi_set_poll_rate(uint32_t value);
+void SPI_SetPollRate(uint32_t value);
/**
* @brief Installs the SPI and timer 2 interrupt handlers and starts the poll
@@ -56,6 +57,6 @@ void spi_set_poll_rate(uint32_t value);
*
* @param callback
*/
-void spi_init(SPICALLBACK callback);
+void SPI_Init(SPI_Callback callback);
#endif