gpio: implement majority of the service

This commit is contained in:
Michael Scire 2020-10-26 03:29:40 -07:00 committed by fincs
parent 8718c145f6
commit 0ff42ee69e
2 changed files with 162 additions and 4 deletions

View File

@ -6,12 +6,14 @@
*/ */
#pragma once #pragma once
#include "../types.h" #include "../types.h"
#include "../kernel/event.h"
#include "../sf/service.h" #include "../sf/service.h"
typedef enum { typedef enum {
GpioPadName_AudioCodec = 1, GpioPadName_AudioCodec = 1,
GpioPadName_ButtonVolUp = 25, GpioPadName_ButtonVolUp = 25,
GpioPadName_ButtonVolDown = 26, GpioPadName_ButtonVolDown = 26,
GpioPadName_SdCd = 56,
} GpioPadName; } GpioPadName;
typedef struct { typedef struct {
@ -28,6 +30,19 @@ typedef enum {
GpioValue_High = 1, GpioValue_High = 1,
} GpioValue; } GpioValue;
typedef enum {
GpioInterruptMode_LowLevel = 0,
GpioInterruptMode_HighLevel = 1,
GpioInterruptMode_RisingEdge = 2,
GpioInterruptMode_FallingEdge = 3,
GpioInterruptMode_AnyEdge = 4,
} GpioInterruptMode;
typedef enum {
GpioInterruptStatus_Inactive = 0,
GpioInterruptStatus_Active = 1,
} GpioInterruptStatus;
/// Initialize gpio. /// Initialize gpio.
Result gpioInitialize(void); Result gpioInitialize(void);
@ -38,9 +53,25 @@ void gpioExit(void);
Service* gpioGetServiceSession(void); Service* gpioGetServiceSession(void);
Result gpioOpenSession(GpioPadSession *out, GpioPadName name); Result gpioOpenSession(GpioPadSession *out, GpioPadName name);
Result gpioOpenSession2(GpioPadSession *out, u32 device_code, u32 access_mode);
Result gpioIsWakeEventActive(bool *out, GpioPadName name);
Result gpioIsWakeEventActive2(bool *out, u32 device_code);
Result gpioPadSetDirection(GpioPadSession *p, GpioDirection dir); Result gpioPadSetDirection(GpioPadSession *p, GpioDirection dir);
Result gpioPadGetDirection(GpioPadSession *p, GpioDirection *out); Result gpioPadGetDirection(GpioPadSession *p, GpioDirection *out);
Result gpioPadSetInterruptMode(GpioPadSession *p, GpioInterruptMode mode);
Result gpioPadGetInterruptMode(GpioPadSession *p, GpioInterruptMode *out);
Result gpioPadSetInterruptEnable(GpioPadSession *p, bool en);
Result gpioPadGetInterruptEnable(GpioPadSession *p, bool *out);
Result gpioPadGetInterruptStatus(GpioPadSession *p, GpioInterruptStatus *out);
Result gpioPadClearInterruptStatus(GpioPadSession *p);
Result gpioPadSetValue(GpioPadSession *p, GpioValue val); Result gpioPadSetValue(GpioPadSession *p, GpioValue val);
Result gpioPadGetValue(GpioPadSession *p, GpioValue *out); Result gpioPadGetValue(GpioPadSession *p, GpioValue *out);
Result gpioPadBindInterrupt(GpioPadSession *p, Event *out);
Result gpioPadUnbindInterrupt(GpioPadSession *p);
Result gpioPadSetDebounceEnabled(GpioPadSession *p, bool en);
Result gpioPadGetDebounceEnabled(GpioPadSession *p, bool *out);
Result gpioPadSetDebounceTime(GpioPadSession *p, s32 ms);
Result gpioPadGetDebounceTime(GpioPadSession *p, s32 *out);
void gpioPadClose(GpioPadSession *p); void gpioPadClose(GpioPadSession *p);

View File

@ -1,6 +1,7 @@
#define NX_SERVICE_ASSUME_NON_DOMAIN #define NX_SERVICE_ASSUME_NON_DOMAIN
#include "service_guard.h" #include "service_guard.h"
#include "services/gpio.h" #include "services/gpio.h"
#include "runtime/hosversion.h"
static Service g_gpioSrv; static Service g_gpioSrv;
@ -18,6 +19,10 @@ Service* gpioGetServiceSession(void) {
return &g_gpioSrv; return &g_gpioSrv;
} }
static Result _gpioCmdNoInNoOut(Service *srv, u32 cmd_id) {
return serviceDispatch(srv, cmd_id);
}
static Result _gpioCmdInU32NoOut(Service *srv, u32 value, u32 cmd_id) { static Result _gpioCmdInU32NoOut(Service *srv, u32 value, u32 cmd_id) {
return serviceDispatchIn(srv, cmd_id, value); return serviceDispatchIn(srv, cmd_id, value);
} }
@ -26,7 +31,34 @@ static Result _gpioCmdNoInOutU32(Service *srv, u32 *out_value, u32 cmd_id) {
return serviceDispatchOut(srv, cmd_id, *out_value); return serviceDispatchOut(srv, cmd_id, *out_value);
} }
static Result _gpioCmdInBoolNoOut(Service *srv, bool value, u32 cmd_id) {
const u8 in = value;
return serviceDispatchIn(srv, cmd_id, in);
}
static Result _gpioCmdNoInOutBool(Service *srv, bool *out_value, u32 cmd_id) {
u8 outval = 0;
Result rc = serviceDispatchOut(srv, cmd_id, outval);
if (R_SUCCEEDED(rc)) {
if (out_value) *out_value = outval & 1;
}
return rc;
}
static Result _gpioCmdInU32OutBool(Service *srv, bool *out_value, u32 inval, u32 cmd_id) {
u8 outval = 0;
Result rc = serviceDispatchInOut(srv, cmd_id, inval, outval);
if (R_SUCCEEDED(rc)) {
if (out_value) *out_value = outval & 1;
}
return rc;
}
Result gpioOpenSession(GpioPadSession *out, GpioPadName name) { Result gpioOpenSession(GpioPadSession *out, GpioPadName name) {
if (hosversionAtLeast(7,0,0)) {
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
}
_Static_assert(sizeof(name) == sizeof(u32), "GpioPadName size"); _Static_assert(sizeof(name) == sizeof(u32), "GpioPadName size");
return serviceDispatchIn(&g_gpioSrv, 1, name, return serviceDispatchIn(&g_gpioSrv, 1, name,
.out_num_objects = 1, .out_num_objects = 1,
@ -34,6 +66,39 @@ Result gpioOpenSession(GpioPadSession *out, GpioPadName name) {
); );
} }
Result gpioOpenSession2(GpioPadSession *out, u32 device_code, u32 access_mode) {
if (hosversionBefore(7,0,0)) {
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
}
const struct {
u32 device_code;
u32 access_mode;
} in = { device_code, access_mode };
return serviceDispatchIn(&g_gpioSrv, 7, in,
.out_num_objects = 1,
.out_objects = &out->s,
);
}
Result gpioIsWakeEventActive(bool *out, GpioPadName name) {
if (hosversionAtLeast(7,0,0)) {
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
}
_Static_assert(sizeof(name) == sizeof(u32), "GpioPadName size");
return _gpioCmdInU32OutBool(&g_gpioSrv, out, name, 3);
}
Result gpioIsWakeEventActive2(bool *out, u32 device_code) {
if (hosversionBefore(7,0,0)) {
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
}
return _gpioCmdInU32OutBool(&g_gpioSrv, out, device_code, 8);
}
Result gpioPadSetDirection(GpioPadSession *p, GpioDirection dir) { Result gpioPadSetDirection(GpioPadSession *p, GpioDirection dir) {
return _gpioCmdInU32NoOut(&p->s, dir, 0); return _gpioCmdInU32NoOut(&p->s, dir, 0);
} }
@ -43,6 +108,33 @@ Result gpioPadGetDirection(GpioPadSession *p, GpioDirection *out) {
return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 1); return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 1);
} }
Result gpioPadSetInterruptMode(GpioPadSession *p, GpioInterruptMode mode) {
return _gpioCmdInU32NoOut(&p->s, mode, 2);
}
Result gpioPadGetInterruptMode(GpioPadSession *p, GpioInterruptMode *out) {
_Static_assert(sizeof(*out) == sizeof(u32), "GpioInterruptMode size");
return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 3);
}
Result gpioPadSetInterruptEnable(GpioPadSession *p, bool en) {
return _gpioCmdInBoolNoOut(&p->s, en, 4);
}
Result gpioPadGetInterruptEnable(GpioPadSession *p, bool *out) {
return _gpioCmdNoInOutBool(&p->s, out, 5);
}
Result gpioPadGetInterruptStatus(GpioPadSession *p, GpioInterruptStatus *out) {
_Static_assert(sizeof(*out) == sizeof(u32), "GpioInterruptStatus size");
return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 6);
}
Result gpioPadClearInterruptStatus(GpioPadSession *p) {
return _gpioCmdNoInNoOut(&p->s, 7);
}
Result gpioPadSetValue(GpioPadSession *p, GpioValue val) { Result gpioPadSetValue(GpioPadSession *p, GpioValue val) {
return _gpioCmdInU32NoOut(&p->s, val, 8); return _gpioCmdInU32NoOut(&p->s, val, 8);
} }
@ -52,6 +144,41 @@ Result gpioPadGetValue(GpioPadSession *p, GpioValue *out) {
return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 9); return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 9);
} }
Result gpioPadBindInterrupt(GpioPadSession *p, Event *out) {
Handle evt_handle;
Result rc = serviceDispatch(&p->s, 10,
.out_handle_attrs = { SfOutHandleAttr_HipcCopy },
.out_handles = &evt_handle,
);
if (R_SUCCEEDED(rc)) {
eventLoadRemote(out, evt_handle, false);
}
return rc;
}
Result gpioPadUnbindInterrupt(GpioPadSession *p) {
return _gpioCmdNoInNoOut(&p->s, 11);
}
Result gpioPadSetDebounceEnabled(GpioPadSession *p, bool en) {
return _gpioCmdInBoolNoOut(&p->s, en, 12);
}
Result gpioPadGetDebounceEnabled(GpioPadSession *p, bool *out) {
return _gpioCmdNoInOutBool(&p->s, out, 13);
}
Result gpioPadSetDebounceTime(GpioPadSession *p, s32 ms) {
return _gpioCmdInU32NoOut(&p->s, (u32)ms, 14);
}
Result gpioPadGetDebounceTime(GpioPadSession *p, s32 *out) {
_Static_assert(sizeof(*out) == sizeof(u32), "");
return _gpioCmdNoInOutU32(&p->s, (u32 *)out, 15);
}
void gpioPadClose(GpioPadSession *p) { void gpioPadClose(GpioPadSession *p) {
serviceClose(&p->s); serviceClose(&p->s);
} }