rewrite everything

This commit is contained in:
souldbminersmwc
2025-09-17 19:56:06 -04:00
parent a1bfcebba8
commit f3eae72b47
177 changed files with 49152 additions and 1258 deletions

View File

@@ -0,0 +1,13 @@
# Editor files
*.swp
*~
# Objects
*.o
*.a
*.so
# Lib
lib
release
debug

View File

@@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = https://github.com/compuphase/minIni
branch = master
commit = 8ce144c3c287fa4e59f8ed4c405cd8b7e29f189b
parent = f32c0b1bca87a6d7e55fb341f8db9ef33b13819f
method = merge
cmdver = 0.4.0

View File

@@ -0,0 +1,189 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
EXCEPTION TO THE APACHE 2.0 LICENSE
As a special exception to the Apache License 2.0 (and referring to the
definitions in Section 1 of this license), you may link, statically or
dynamically, the "Work" to other modules to produce an executable file
containing portions of the "Work", and distribute that executable file
in "Object" form under the terms of your choice, without any of the
additional requirements listed in Section 4 of the Apache License 2.0.
This exception applies only to redistributions in "Object" form (not
"Source" form) and only if no modifications have been made to the "Work".
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

View File

@@ -0,0 +1,133 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
SOURCES := dev
DATA := data
INCLUDES := dev
SRC_H_FILES :=
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec
CFLAGS := -g -Wall -Werror \
-ffunction-sections \
-fdata-sections \
$(ARCH) \
$(BUILD_CFLAGS)
CFLAGS += $(INCLUDE)
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(LIBNX)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
CFILES := minIni.c
CPPFILES :=
SFILES :=
BINFILES :=
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
.PHONY: clean all lib/lib$(TARGET).a
#---------------------------------------------------------------------------------
all: lib/lib$(TARGET).a
lib:
@[ -d $@ ] || mkdir -p $@
release:
@[ -d $@ ] || mkdir -p $@
debug:
@[ -d $@ ] || mkdir -p $@
lib/lib$(TARGET).a : lib release $(SOURCES) $(INCLUDES)
@$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \
BUILD_CFLAGS="-DNDEBUG=1 -O2" \
DEPSDIR=$(CURDIR)/release \
--no-print-directory -C release \
-f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr release debug lib
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT) : $(OFILES)
$(OFILES_SRC) : $(HFILES)
#---------------------------------------------------------------------------------
%_bin.h %.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

View File

@@ -0,0 +1,12 @@
minIni is a programmer's library to read and write "INI" files in embedded
systems. The library takes little resources and can be configured for various
kinds of file I/O libraries.
The method for portable INI file management in minIni is, in part based, on the
article "Multiplatform .INI Files" by Joseph J. Graf in the March 1994 issue of
Dr. Dobb's Journal.
The C++ class in minIni.h was contributed by Steven Van Ingelgem.
The option to compile minIni as a read-only library was contributed by Luca
Bassanello.

View File

@@ -0,0 +1,170 @@
# minIni
minIni is a portable and configurable library for reading and writing ".INI" files. At 830 lines of commented source
code
(version 1.2), minIni truly is a "mini" INI file parser, especially considering its features.
The library does not require the file I/O functions from the standard C/C++ library, but instead lets you configure
the file I/O interface to use via macros. minIni uses limited stack space and does not use dynamic memory (malloc and
friends) at all.
Some minor variations on standard INI files are supported too, notably minIni supports INI files that lack sections.
# Acknowledgement
minIni is derived from an earlier INI file parser (which I wrote) for desktop systems.
In turn, that earlier parser was a re-write of the code from the article "Multiplatform .INI Files" by Joseph J. Graf
in the March 1994 issue of Dr. Dobb's Journal. In other words, minIni has its roots in the work of Joseph Graf (even
though the code has been almost completely re-written).
# Features
minIni is a programmer's library to read and write "INI" files in embedded systems. minIni takes little resources,
can be configured for various kinds of file I/O libraries and provides functionality for reading, writing and
deleting keys from an INI file.
Although the main feature of minIni is that it is small and minimal, it has a few other features:
* minIni supports reading keys that are outside a section, and it thereby supports configuration files that do not use sections (but that are otherwise compatible with INI files).
* You may use a colon to separate key and value; the colon is equivalent to the equal sign. That is, the strings "Name: Value" and "Name=Value" have the same meaning.
* The hash character ("#") is an alternative for the semicolon to start a comment. Trailing comments (i.e. behind a key/value pair on a line) are allowed.
* Leading and trailing white space around key names and values is ignored.
* When writing a value that contains a comment character (";" or "#"), that value will automatically be put between double quotes; when reading the value, these quotes are removed. When a double-quote itself appears in the setting, these characters are escaped.
* Section and key enumeration are supported.
* You can optionally set the line termination (for text files) that minIni will use. (This is a compile-time setting, not a run-time setting.)
* Since writing speed is much lower than reading speed in Flash memory (SD/MMC cards, USB memory sticks), minIni minimizes "file writes" at the expense of double "file reads".
* The memory footprint is deterministic. There is no dynamic memory allocation.
## INI file reading paradigms
There are two approaches to reading settings from an INI file. One way is to call a function, such as
GetProfileString() for every section and key that you need. This is especially convenient if there is a large
INI file, but you only need a few settings from that file at any time &mdash;especially if the INI file can also
change while your program runs. This is the approach that the Microsoft Windows API uses.
The above procedure is quite inefficient, however, when you need to retrieve quite a few settings in a row from
the INI file &mdash;especially if the INI file is not cached in memory (which it isn't, in minIni). A different approach
to getting settings from an INI file is to call a "parsing" function and let that function call the application
back with the section and key names plus the associated data. XML parsing libraries often use this approach; see
for example the Expat library.
minIni supports both approaches. For reading a single setting, use functions like ini_gets(). For the callback
approach, implement a callback and call ini_browse(). See the minIni manual for details.
# INI file syntax
INI files are best known from Microsoft Windows, but they are also used with applications that run on other
platforms (although their file extension is sometimes ".cfg" instead of ".ini").
INI files have a simple syntax with name/value pairs in a plain text file. The name must be unique (per section)
and the value must fit on a single line. INI files are commonly separated into sections &mdash;in minIni, this is
optional. A section is a name between square brackets, like "[Network]" in the example below.
```
[Network]
hostname=My Computer
address=dhcp
dns = 192.168.1.1
```
In the API and in this documentation, the "name" for a setting is denoted as the key for the setting. The key
and the value are separated by an equal sign ("="). minIni supports the colon (":") as an alternative to the
equal sign for the key/value delimiter.
Leading a trailing spaces around values or key names are removed. If you need to include leading and/or trailing
spaces in a value, put the value between double quotes. The ini_gets() function (from the minIni library, see the
minIni manual) strips off the double quotes from the returned value. Function ini_puts() adds double quotes if
the value to write contains trailing white space (or special characters).
minIni ignores spaces around the "=" or ":" delimiters, but it does not ignore spaces between the brackets in a
section name. In other words, it is best not to put spaces behind the opening bracket "[" or before the closing
bracket "]" of a section name.
Comments in the INI must start with a semicolon (";") or a hash character ("#"), and run to the end of the line.
A comment can be a line of its own, or it may follow a key/value pair (the "#" character and trailing comments
are extensions of minIni).
For more details on the format, please see http://en.wikipedia.org/wiki/INI_file.
# Adapting minIni to a file system
The minIni library must be configured for a platform with the help of a so- called "glue file". This glue file
contains macros (and possibly functions) that map file reading and writing functions used by the minIni library
to those provided by the operating system. The glue file must be called "minGlue.h".
To get you started, the minIni distribution comes with the following example glue files:
* a glue file that maps to the standard C/C++ library (specifically the file I/O functions from the "stdio" package),
* a glue file for Microchip's "Memory Disk Drive File System Library" (see http://www.microchip.com/),
* a glue file for the FAT library provided with the CCS PIC compiler (see http://www.ccsinfo.com/)
* a glue file for the EFS Library (EFSL, http://www.efsl.be/),
* and a glue file for the FatFs and Petit-FatFs libraries (http://elm-chan.org/fsw/ff/00index_e.html).
The minIni library does not rely on the availability of a standard C library, because embedded operating systems
may have limited support for file I/O. Even on full operating systems, separating the file I/O from the INI format
parsing carries advantages, because it allows you to cache the INI file and thereby enhance performance.
The glue file must specify the type that identifies a file, whether it is a handle or a pointer. For the standard
C/C++ file I/O library, this would be:
```C
#define INI_FILETYPE FILE*
```
If you are not using the standard C/C++ file I/O library, chances are that you need a different handle or
"structure" to identify the storage than the ubiquitous "FILE*" type. For example, the glue file for the FatFs
library uses the following declaration:
```C
#define INI_FILETYPE FIL
```
The minIni functions declare variables of this INI_FILETYPE type and pass these variables to sub-functions
(including the glue interface functions) by reference.
For "write support", another type that must be defined is for variables that hold the "current position" in a
file. For the standard C/C++ I/O library, this is "fpos_t".
Another item that needs to be configured is the buffer size. The functions in the minIni library allocate this
buffer on the stack, so the buffer size is directly related to the stack usage. In addition, the buffer size
determines the maximum line length that is supported in the INI file and the maximum path name length for the
temporary file. For example, minGlue.h could contain the definition:
```C
#define INI_BUFFERSIZE 512
```
The above macro limits the line length of the INI files supported by minIni to 512 characters.
The temporary file is only used when writing to INI files. The minIni routines copy/change the INI file to a
temporary file and then rename that temporary file to the original file. This approach uses the least amount of
memory. The path name of the temporary file is the same as the input file, but with the last character set to a
tilde ("~").
Below is an example of a glue file (this is the one that maps to the C/C++ "stdio" library).
```C
#include <stdio.h>
#define INI_FILETYPE FILE*
#define ini_openread(filename,file) ((*(file) = fopen((filename),"r")) != NULL)
#define ini_openwrite(filename,file) ((*(file) = fopen((filename),"w")) != NULL)
#define ini_close(file) (fclose(*(file)) == 0)
#define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL)
#define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0)
#define ini_rename(source,dest) (rename((source), (dest)) == 0)
#define ini_remove(filename) (remove(filename) == 0)
#define INI_FILEPOS fpos_t
#define ini_tell(file,pos) (fgetpos(*(file), (pos)) == 0)
#define ini_seek(file,pos) (fsetpos(*(file), (pos)) == 0)
```
As you can see, a glue file is mostly a set of macros that wraps one function definition around another.
The glue file may contain more settings, for support of rational numbers, to explicitly set the line termination
character(s), or to disable write support (for example). See the manual that comes with the archive for the details.

View File

@@ -0,0 +1,37 @@
/* Glue functions for the minIni library, based on the FatFs and Petit-FatFs
* libraries, see http://elm-chan.org/fsw/ff/00index_e.html
*
* By CompuPhase, 2008-2012
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*
* (The FatFs and Petit-FatFs libraries are copyright by ChaN and licensed at
* its own terms.)
*/
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
/* You must set _USE_STRFUNC to 1 or 2 in the include file ff.h (or tff.h)
* to enable the "string functions" fgets() and fputs().
*/
#include "ff.h" /* include tff.h for Tiny-FatFs */
#define INI_FILETYPE FIL
#define ini_openread(filename,file) (f_open((file), (filename), FA_READ+FA_OPEN_EXISTING) == FR_OK)
#define ini_openwrite(filename,file) (f_open((file), (filename), FA_WRITE+FA_CREATE_ALWAYS) == FR_OK)
#define ini_close(file) (f_close(file) == FR_OK)
#define ini_read(buffer,size,file) f_gets((buffer), (size),(file))
#define ini_write(buffer,file) f_puts((buffer), (file))
#define ini_remove(filename) (f_unlink(filename) == FR_OK)
#define INI_FILEPOS DWORD
#define ini_tell(file,pos) (*(pos) = f_tell((file)))
#define ini_seek(file,pos) (f_lseek((file), *(pos)) == FR_OK)
static int ini_rename(TCHAR *source, const TCHAR *dest)
{
/* Function f_rename() does not allow drive letters in the destination file */
char *drive = strchr(dest, ':');
drive = (drive == NULL) ? dest : drive + 1;
return (f_rename(source, drive) == FR_OK);
}

View File

@@ -0,0 +1,64 @@
/* minIni glue functions for FAT library by CCS, Inc. (as provided with their
* PIC MCU compiler)
*
* By CompuPhase, 2011-2012
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*
* (The FAT library is copyright (c) 2007 Custom Computer Services, and
* licensed at its own terms.)
*/
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
#ifndef FAT_PIC_C
#error FAT library must be included before this module
#endif
#define const /* keyword not supported by CCS */
#define INI_FILETYPE FILE
#define ini_openread(filename,file) (fatopen((filename), "r", (file)) == GOODEC)
#define ini_openwrite(filename,file) (fatopen((filename), "w", (file)) == GOODEC)
#define ini_close(file) (fatclose((file)) == 0)
#define ini_read(buffer,size,file) (fatgets((buffer), (size), (file)) != NULL)
#define ini_write(buffer,file) (fatputs((buffer), (file)) == GOODEC)
#define ini_remove(filename) (rm_file((filename)) == 0)
#define INI_FILEPOS fatpos_t
#define ini_tell(file,pos) (fatgetpos((file), (pos)) == 0)
#define ini_seek(file,pos) (fatsetpos((file), (pos)) == 0)
#ifndef INI_READONLY
/* CCS FAT library lacks a rename function, so instead we copy the file to the
* new name and delete the old file
*/
static int ini_rename(char *source, char *dest)
{
FILE fr, fw;
int n;
if (fatopen(source, "r", &fr) != GOODEC)
return 0;
if (rm_file(dest) != 0)
return 0;
if (fatopen(dest, "w", &fw) != GOODEC)
return 0;
/* With some "insider knowledge", we can save some memory: the "source"
* parameter holds a filename that was built from the "dest" parameter. It
* was built in a local buffer with the size INI_BUFFERSIZE. We can reuse
* this buffer for copying the file.
*/
while (n=fatread(source, 1, INI_BUFFERSIZE, &fr))
fatwrite(source, 1, n, &fw);
fatclose(&fr);
fatclose(&fw);
/* Now we need to delete the source file. However, we have garbled the buffer
* that held the filename of the source. So we need to build it again.
*/
ini_tempname(source, dest, INI_BUFFERSIZE);
return rm_file(source) == 0;
}
#endif

View File

@@ -0,0 +1,63 @@
/* Glue functions for the minIni library, based on the EFS Library, see
* http://www.efsl.be/
*
* By CompuPhase, 2008-2012
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*
* (EFSL is copyright 2005-2006 Lennart Ysboodt and Michael De Nil, and
* licensed under the GPL with an exception clause for static linking.)
*/
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
#define INI_LINETERM "\r\n" /* set line termination explicitly */
#include "efs.h"
extern EmbeddedFileSystem g_efs;
#define INI_FILETYPE EmbeddedFile
#define ini_openread(filename,file) (file_fopen((file), &g_efs.myFs, (char*)(filename), 'r') == 0)
#define ini_openwrite(filename,file) (file_fopen((file), &g_efs.myFs, (char*)(filename), 'w') == 0)
#define ini_close(file) file_fclose(file)
#define ini_read(buffer,size,file) (file_read((file), (size), (buffer)) > 0)
#define ini_write(buffer,file) (file_write((file), strlen(buffer), (char*)(buffer)) > 0)
#define ini_remove(filename) rmfile(&g_efs.myFs, (char*)(filename))
#define INI_FILEPOS euint32
#define ini_tell(file,pos) (*(pos) = (file)->FilePtr))
#define ini_seek(file,pos) file_setpos((file), (*pos))
#if ! defined INI_READONLY
/* EFSL lacks a rename function, so instead we copy the file to the new name
* and delete the old file
*/
static int ini_rename(char *source, const char *dest)
{
EmbeddedFile fr, fw;
int n;
if (file_fopen(&fr, &g_efs.myFs, source, 'r') != 0)
return 0;
if (rmfile(&g_efs.myFs, (char*)dest) != 0)
return 0;
if (file_fopen(&fw, &g_efs.myFs, (char*)dest, 'w') != 0)
return 0;
/* With some "insider knowledge", we can save some memory: the "source"
* parameter holds a filename that was built from the "dest" parameter. It
* was built in buffer and this buffer has the size INI_BUFFERSIZE. We can
* reuse this buffer for copying the file.
*/
while (n=file_read(&fr, INI_BUFFERSIZE, source))
file_write(&fw, n, source);
file_fclose(&fr);
file_fclose(&fw);
/* Now we need to delete the source file. However, we have garbled the buffer
* that held the filename of the source. So we need to build it again.
*/
ini_tempname(source, dest, INI_BUFFERSIZE);
return rmfile(&g_efs.myFs, source) == 0;
}
#endif

View File

@@ -0,0 +1,26 @@
/* Glue functions for the minIni library, based on the "FAT Filing System"
* library by embedded-code.com
*
* By CompuPhase, 2008-2012
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*
* (The "FAT Filing System" library itself is copyright embedded-code.com, and
* licensed at its own terms.)
*/
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
#include <mem-ffs.h>
#define INI_FILETYPE FFS_FILE*
#define ini_openread(filename,file) ((*(file) = ffs_fopen((filename),"r")) != NULL)
#define ini_openwrite(filename,file) ((*(file) = ffs_fopen((filename),"w")) != NULL)
#define ini_close(file) (ffs_fclose(*(file)) == 0)
#define ini_read(buffer,size,file) (ffs_fgets((buffer),(size),*(file)) != NULL)
#define ini_write(buffer,file) (ffs_fputs((buffer),*(file)) >= 0)
#define ini_rename(source,dest) (ffs_rename((source), (dest)) == 0)
#define ini_remove(filename) (ffs_remove(filename) == 0)
#define INI_FILEPOS long
#define ini_tell(file,pos) (ffs_fgetpos(*(file), (pos)) == 0)
#define ini_seek(file,pos) (ffs_fsetpos(*(file), (pos)) == 0)

View File

@@ -0,0 +1,58 @@
/* minIni glue functions for Microchip's "Memory Disk Drive" file system
* library, as presented in Microchip application note AN1045.
*
* By CompuPhase, 2011-2014
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*
* (The "Microchip Memory Disk Drive File System" is copyright (c) Microchip
* Technology Incorporated, and licensed at its own terms.)
*/
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
#include "MDD File System\fsio.h"
#include <string.h>
#define INI_FILETYPE FSFILE*
#define ini_openread(filename,file) ((*(file) = FSfopen((filename),FS_READ)) != NULL)
#define ini_openwrite(filename,file) ((*(file) = FSfopen((filename),FS_WRITE)) != NULL)
#define ini_openrewrite(filename,file) ((*(file) = fopen((filename),FS_READPLUS)) != NULL)
#define ini_close(file) (FSfclose(*(file)) == 0)
#define ini_write(buffer,file) (FSfwrite((buffer), 1, strlen(buffer), (*file)) > 0)
#define ini_remove(filename) (FSremove((filename)) == 0)
#define INI_FILEPOS long int
#define ini_tell(file,pos) (*(pos) = FSftell(*(file)))
#define ini_seek(file,pos) (FSfseek(*(file), *(pos), SEEK_SET) == 0)
/* Since the Memory Disk Drive file system library reads only blocks of files,
* the function to read a text line does so by "over-reading" a block of the
* of the maximum size and truncating it behind the end-of-line.
*/
static int ini_read(char *buffer, int size, INI_FILETYPE *file)
{
size_t numread = size;
char *eol;
if ((numread = FSfread(buffer, 1, size, *file)) == 0)
return 0; /* at EOF */
if ((eol = strchr(buffer, '\n')) == NULL)
eol = strchr(buffer, '\r');
if (eol != NULL) {
/* terminate the buffer */
*++eol = '\0';
/* "unread" the data that was read too much */
FSfseek(*file, - (int)(numread - (size_t)(eol - buffer)), SEEK_CUR);
} /* if */
return 1;
}
#ifndef INI_READONLY
static int ini_rename(const char *source, const char *dest)
{
FSFILE* ftmp = FSfopen((source), FS_READ);
FSrename((dest), ftmp);
return FSfclose(ftmp) == 0;
}
#endif

View File

@@ -0,0 +1,31 @@
/* Glue functions for the minIni library, based on the C/C++ stdio library
*
* Or better said: this file contains macros that maps the function interface
* used by minIni to the standard C/C++ file I/O functions.
*
* By CompuPhase, 2008-2014
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*/
/* map required file I/O types and functions to the standard C library */
#include <stdio.h>
#define INI_FILETYPE FILE*
#define ini_openread(filename,file) ((*(file) = fopen((filename),"rb")) != NULL)
#define ini_openwrite(filename,file) ((*(file) = fopen((filename),"wb")) != NULL)
#define ini_openrewrite(filename,file) ((*(file) = fopen((filename),"r+b")) != NULL)
#define ini_close(file) (fclose(*(file)) == 0)
#define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL)
#define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0)
#define ini_rename(source,dest) (rename((source), (dest)) == 0)
#define ini_remove(filename) (remove(filename) == 0)
#define INI_FILEPOS long int
#define ini_tell(file,pos) (*(pos) = ftell(*(file)))
#define ini_seek(file,pos) (fseek(*(file), *(pos), SEEK_SET) == 0)
/* for floating-point support, define additional types and functions */
#define INI_REAL float
#define ini_ftoa(string,value) sprintf((string),"%f",(value))
#define ini_atof(string) (INI_REAL)strtod((string),NULL)

View File

@@ -0,0 +1,35 @@
/* Glue functions for the minIni library, based on the C/C++ stdio library
*
* Or better said: this file contains macros that maps the function interface
* used by minIni to the standard C/C++ file I/O functions.
*
* By CompuPhase, 2008-2014
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*/
/* map required file I/O types and functions to the standard C library */
#include <stdio.h>
#define INI_FILETYPE FILE*
#define ini_openread(filename,file) ((*(file) = fopen((filename),"rb")) != NULL)
#define ini_openwrite(filename,file) ((*(file) = fopen((filename),"wb")) != NULL)
#define ini_openrewrite(filename,file) ((*(file) = fopen((filename),"r+b")) != NULL)
#define ini_close(file) (fclose(*(file)) == 0)
#define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL)
#define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0)
#define ini_rename(source,dest) (rename((source), (dest)) == 0)
#define ini_remove(filename) (remove(filename) == 0)
#define INI_FILEPOS long int
#define ini_tell(file,pos) (*(pos) = ftell(*(file)))
#define ini_seek(file,pos) (fseek(*(file), *(pos), SEEK_SET) == 0)
/* for floating-point support, define additional types and functions */
//#define INI_REAL float
//#define ini_ftoa(string,value) sprintf((string),"%f",(value))
//#define ini_atof(string) (INI_REAL)strtod((string),NULL)
#define INI_ANSIONLY
#define INI_LINETERM "\n"
#define PORTABLE_STRNICMP

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,68 @@
/* minIni - Multi-Platform INI file parser, suitable for embedded systems
*
* Copyright (c) CompuPhase, 2008-2017
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Version: $Id: minIni.h 53 2015-01-18 13:35:11Z thiadmer.riemersma@gmail.com $
*/
#ifndef MININI_H
#define MININI_H
#include "minGlue.h"
#if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined INI_ANSIONLY
#include <tchar.h>
#define mTCHAR TCHAR
#else
/* force TCHAR to be "char", but only for minIni */
#define mTCHAR char
#endif
#if !defined INI_BUFFERSIZE
#define INI_BUFFERSIZE 512
#endif
#if defined __cplusplus
extern "C" {
#endif
int ini_getbool(const mTCHAR *Section, const mTCHAR *Key, int DefValue, const mTCHAR *Filename);
long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const mTCHAR *Filename);
int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
#if defined INI_REAL
INI_REAL ini_getf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL DefValue, const mTCHAR *Filename);
#endif
#if !defined INI_READONLY
int ini_putl(const mTCHAR *Section, const mTCHAR *Key, long Value, const mTCHAR *Filename);
int ini_puts(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const mTCHAR *Filename);
int ini_putsection(const mTCHAR *Section, const mTCHAR **Keys, const mTCHAR **Values, const mTCHAR *Filename);
#if defined INI_REAL
int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const mTCHAR *Filename);
#endif
#endif /* INI_READONLY */
#if !defined INI_NOBROWSE
typedef int (*INI_CALLBACK)(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, void *UserData);
int ini_browse(INI_CALLBACK Callback, void *UserData, const mTCHAR *Filename);
#endif /* INI_NOBROWSE */
#if defined __cplusplus
}
#endif
#endif /* MININI_H */

View File

@@ -0,0 +1,117 @@
/* Simple test program
*
* gcc -o test test.c minIni.c
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "minIni.h"
#define sizearray(a) (sizeof(a) / sizeof((a)[0]))
const char inifile[] = "test.ini";
const char inifile2[] = "testplain.ini";
int Callback(const char *section, const char *key, const char *value, void *userdata)
{
(void)userdata; /* this parameter is not used in this example */
printf(" [%s]\t%s=%s\n", section, key, value);
return 1;
}
int main(void)
{
char str[100];
long n;
int s, k;
char section[50];
/* string reading */
n = ini_gets("first", "string", "dummy", str, sizearray(str), inifile);
assert(n==4 && strcmp(str,"noot")==0);
n = ini_gets("second", "string", "dummy", str, sizearray(str), inifile);
assert(n==4 && strcmp(str,"mies")==0);
n = ini_gets("first", "undefined", "dummy", str, sizearray(str), inifile);
assert(n==5 && strcmp(str,"dummy")==0);
/* ----- */
n = ini_gets("", "string", "dummy", str, sizearray(str), inifile2);
assert(n==4 && strcmp(str,"noot")==0);
n = ini_gets(NULL, "string", "dummy", str, sizearray(str), inifile2);
assert(n==4 && strcmp(str,"noot")==0);
/* ----- */
printf("1. String reading tests passed\n");
/* value reading */
n = ini_getl("first", "val", -1, inifile);
assert(n==1);
n = ini_getl("second", "val", -1, inifile);
assert(n==2);
n = ini_getl("first", "undefined", -1, inifile);
assert(n==-1);
/* ----- */
n = ini_getl(NULL, "val", -1, inifile2);
assert(n==1);
/* ----- */
printf("2. Value reading tests passed\n");
/* string writing */
n = ini_puts("first", "alt", "flagged as \"correct\"", inifile);
assert(n==1);
n = ini_gets("first", "alt", "dummy", str, sizearray(str), inifile);
assert(n==20 && strcmp(str,"flagged as \"correct\"")==0);
/* ----- */
n = ini_puts("second", "alt", "correct", inifile);
assert(n==1);
n = ini_gets("second", "alt", "dummy", str, sizearray(str), inifile);
assert(n==7 && strcmp(str,"correct")==0);
/* ----- */
n = ini_puts("third", "test", "correct", inifile);
assert(n==1);
n = ini_gets("third", "test", "dummy", str, sizearray(str), inifile);
assert(n==7 && strcmp(str,"correct")==0);
/* ----- */
n = ini_puts("second", "alt", "overwrite", inifile);
assert(n==1);
n = ini_gets("second", "alt", "dummy", str, sizearray(str), inifile);
assert(n==9 && strcmp(str,"overwrite")==0);
/* ----- */
n = ini_puts("second", "alt", "123456789", inifile);
assert(n==1);
n = ini_gets("second", "alt", "dummy", str, sizearray(str), inifile);
assert(n==9 && strcmp(str,"123456789")==0);
/* ----- */
n = ini_puts(NULL, "alt", "correct", inifile2);
assert(n==1);
n = ini_gets(NULL, "alt", "dummy", str, sizearray(str), inifile2);
assert(n==7 && strcmp(str,"correct")==0);
/* ----- */
printf("3. String writing tests passed\n");
/* section/key enumeration */
printf("4. Section/key enumeration, file structure follows\n");
for (s = 0; ini_getsection(s, section, sizearray(section), inifile) > 0; s++) {
printf(" [%s]\n", section);
for (k = 0; ini_getkey(section, k, str, sizearray(str), inifile) > 0; k++) {
printf("\t%s\n", str);
} /* for */
} /* for */
/* browsing through the file */
printf("5. browse through all settings, file field list follows\n");
ini_browse(Callback, NULL, inifile);
/* string deletion */
n = ini_puts("first", "alt", NULL, inifile);
assert(n==1);
n = ini_puts("second", "alt", NULL, inifile);
assert(n==1);
n = ini_puts("third", NULL, NULL, inifile);
assert(n==1);
/* ----- */
n = ini_puts(NULL, "alt", NULL, inifile2);
assert(n==1);
printf("6. String deletion tests passed\n");
return 0;
}

View File

@@ -0,0 +1,8 @@
[First]
String=noot # trailing commment
Val=1
[Second]
Val = 2
#comment=3
String = mies

View File

@@ -0,0 +1,80 @@
/*
gcc -o minIni.o -c minIni.c
g++ -o test2.o -c test2.cc
g++ -o test2 test2.o minIni.o
./test2
*/
#include <assert>
#include <iostream>
#include <string>
using namespace std ;
#include "minIni.h"
int main(void)
{
minIni ini("test.ini");
string s;
/* string reading */
s = ini.gets( "first", "string" , "aap" );
assert(s == "noot");
s = ini.gets( "second", "string" , "aap" );
assert(s == "mies");
s = ini.gets( "first", "dummy" , "aap" );
assert(s == "aap");
cout << "1. String reading tests passed" << endl ;
/* value reading */
long n;
n = ini.getl("first", "val", -1 );
assert(n==1);
n = ini.getl("second", "val", -1);
assert(n==2);
n = ini.getl("first", "dummy", -1);
assert(n==-1);
cout << "2. Value reading tests passed" << endl ;
/* string writing */
bool b;
b = ini.put("first", "alt", "flagged as \"correct\"");
assert(b);
s = ini.gets("first", "alt", "aap");
assert(s=="flagged as \"correct\"");
b = ini.put("second", "alt", "correct");
assert(b);
s = ini.gets("second", "alt", "aap");
assert(s=="correct");
b = ini.put("third", "alt", "correct");
assert(b);
s = ini.gets("third", "alt", "aap" );
assert(s=="correct");
cout << "3. String writing tests passed" << endl;
/* section/key enumeration */
cout << "4. section/key enumeration; file contents follows" << endl;
string section;
for (int is = 0; section = ini.getsection(is), section.length() > 0; is++) {
cout << " [" << section.c_str() << "]" << endl;
for (int ik = 0; s = ini.getkey(section, ik), s.length() > 0; ik++) {
cout << "\t" << s.c_str() << endl;
}
}
/* string deletion */
b = ini.del("first", "alt");
assert(b);
b = ini.del("second", "alt");
assert(b);
b = ini.del("third");
assert(b);
cout << "5. string deletion passed " << endl;
return 0;
}

View File

@@ -0,0 +1,3 @@
String=noot # trailing commment
#comment=3
Val=1

View File

@@ -0,0 +1,101 @@
/* minIni - Multi-Platform INI file parser, wxWidgets interface
*
* Copyright (c) CompuPhase, 2008-2012
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Version: $Id: wxMinIni.h 44 2012-01-04 15:52:56Z thiadmer.riemersma@gmail.com $
*/
#ifndef WXMININI_H
#define WXMININI_H
#include <wx/wx.h>
#include "minIni.h"
class minIni
{
public:
minIni(const wxString& filename) : iniFilename(filename)
{ }
bool getbool(const wxString& Section, const wxString& Key, bool DefValue=false) const
{ return ini_getbool(Section.utf8_str(), Key.utf8_str(), int(DefValue), iniFilename.utf8_str()) != 0; }
long getl(const wxString& Section, const wxString& Key, long DefValue=0) const
{ return ini_getl(Section.utf8_str(), Key.utf8_str(), DefValue, iniFilename.utf8_str()); }
int geti(const wxString& Section, const wxString& Key, int DefValue=0) const
{ return static_cast<int>(ini_getl(Section.utf8_str(), Key.utf8_str(), (long)DefValue, iniFilename.utf8_str())); }
wxString gets(const wxString& Section, const wxString& Key, const wxString& DefValue=wxT("")) const
{
char buffer[INI_BUFFERSIZE];
ini_gets(Section.utf8_str(), Key.utf8_str(), DefValue.utf8_str(), buffer, INI_BUFFERSIZE, iniFilename.utf8_str());
wxString result = wxString::FromUTF8(buffer);
return result;
}
wxString getsection(int idx) const
{
char buffer[INI_BUFFERSIZE];
ini_getsection(idx, buffer, INI_BUFFERSIZE, iniFilename.utf8_str());
wxString result = wxString::FromUTF8(buffer);
return result;
}
wxString getkey(const wxString& Section, int idx) const
{
char buffer[INI_BUFFERSIZE];
ini_getkey(Section.utf8_str(), idx, buffer, INI_BUFFERSIZE, iniFilename.utf8_str());
wxString result = wxString::FromUTF8(buffer);
return result;
}
#if defined INI_REAL
INI_REAL getf(const wxString& Section, wxString& Key, INI_REAL DefValue=0) const
{ return ini_getf(Section.utf8_str(), Key.utf8_str(), DefValue, iniFilename.utf8_str()); }
#endif
#if ! defined INI_READONLY
bool put(const wxString& Section, const wxString& Key, long Value) const
{ return ini_putl(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; }
bool put(const wxString& Section, const wxString& Key, int Value) const
{ return ini_putl(Section.utf8_str(), Key.utf8_str(), (long)Value, iniFilename.utf8_str()) != 0; }
bool put(const wxString& Section, const wxString& Key, bool Value) const
{ return ini_putl(Section.utf8_str(), Key.utf8_str(), (long)Value, iniFilename.utf8_str()) != 0; }
bool put(const wxString& Section, const wxString& Key, const wxString& Value) const
{ return ini_puts(Section.utf8_str(), Key.utf8_str(), Value.utf8_str(), iniFilename.utf8_str()) != 0; }
bool put(const wxString& Section, const wxString& Key, const char* Value) const
{ return ini_puts(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; }
#if defined INI_REAL
bool put(const wxString& Section, const wxString& Key, INI_REAL Value) const
{ return ini_putf(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; }
#endif
bool del(const wxString& Section, const wxString& Key) const
{ return ini_puts(Section.utf8_str(), Key.utf8_str(), 0, iniFilename.utf8_str()) != 0; }
bool del(const wxString& Section) const
{ return ini_puts(Section.utf8_str(), 0, 0, iniFilename.utf8_str()) != 0; }
#endif
private:
wxString iniFilename;
};
#endif /* WXMININI_H */

Binary file not shown.

View File

@@ -0,0 +1,21 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "../dev/minIni.h"
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,13 @@
# Editor files
*.swp
*~
# Objects
*.o
*.a
*.so
# Lib
lib
release
debug

View File

@@ -0,0 +1,132 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
SOURCES := src
DATA := data
INCLUDES := include
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec
CFLAGS := -g -Wall -Werror \
-ffunction-sections \
-fdata-sections \
$(ARCH) \
$(BUILD_CFLAGS)
CFLAGS += $(INCLUDE) -std=gnu11
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(LIBNX)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
.PHONY: clean all lib/lib$(TARGET).a lib/lib$(TARGET)d.a
#---------------------------------------------------------------------------------
all: lib/lib$(TARGET).a lib/lib$(TARGET)d.a
lib:
@[ -d $@ ] || mkdir -p $@
release:
@[ -d $@ ] || mkdir -p $@
debug:
@[ -d $@ ] || mkdir -p $@
lib/lib$(TARGET).a : lib release $(SOURCES) $(INCLUDES)
@$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \
BUILD_CFLAGS="-DNDEBUG=1 -O2" \
DEPSDIR=$(CURDIR)/release \
--no-print-directory -C release \
-f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr release debug lib
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT) : $(OFILES)
$(OFILES_SRC) : $(HFILES)
#---------------------------------------------------------------------------------
%_bin.h %.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

View File

@@ -0,0 +1,19 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include "nxExt/apm_ext.h"
#include "nxExt/i2c.h"
#include "nxExt/t210.h"
#include "nxExt/max17050.h"
#include "nxExt/tmp451.h"
#include "nxExt/ipc_server.h"
#include "nxExt/cpp/lockable_mutex.h"

View File

@@ -0,0 +1,29 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
Result apmExtInitialize(void);
void apmExtExit(void);
Result apmExtGetPerformanceMode(u32* out_mode);
Result apmExtSysRequestPerformanceMode(u32 mode);
Result apmExtGetCurrentPerformanceConfiguration(u32* out_conf);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,64 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
#include <mutex>
#include <switch.h>
class LockableMutex
{
public:
LockableMutex()
{
mutexInit(&this->m);
}
virtual ~LockableMutex() {}
void Lock()
{
mutexLock(&this->m);
}
bool TryLock()
{
return mutexTryLock(&this->m);
}
void Unlock()
{
mutexUnlock(&this->m);
}
// snake_case aliases in order to implement Lockable
void lock()
{
this->Lock();
}
bool try_lock()
{
return this->TryLock();
}
void unlock()
{
this->Unlock();
}
private:
Mutex m;
};
#endif

View File

@@ -0,0 +1,24 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
Result i2csessionExtRegReceive(I2cSession* s, u8 in, void* out, u8 out_size);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,62 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
#define IPC_SERVER_EXT_RESPONSE_MAX_DATA_SIZE (0x100 - 0x10 - sizeof(IpcServerRawHeader))
typedef struct
{
u64 magic;
union
{
u64 cmdId;
u64 result;
};
} IpcServerRawHeader;
typedef struct
{
SmServiceName srvName;
Handle handles[MAX_WAIT_OBJECTS];
u32 max;
u32 count;
} IpcServer;
typedef struct
{
u64 cmdId;
void* ptr;
size_t size;
} IpcServerRequestData;
typedef struct
{
HipcParsedRequest hipc;
IpcServerRequestData data;
} IpcServerRequest;
typedef Result (*IpcServerRequestHandler)(void* userdata, const IpcServerRequest* r, u8* out_data, size_t* out_dataSize);
Result ipcServerInit(IpcServer* server, const char* name, u32 max_sessions);
Result ipcServerExit(IpcServer* server);
Result ipcServerProcess(IpcServer* server, IpcServerRequestHandler handler, void* userdata);
Result ipcServerParseCommand(const IpcServerRequest* r, size_t* out_datasize, void** out_data, u64* out_cmd);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,27 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
Result max17050Initialize(void);
void max17050Exit(void);
s32 max17050PowerNow(void);
s32 max17050PowerAvg(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,28 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
u32 t210ClkCpuFreq(void);
u32 t210ClkMemFreq(void);
u32 t210ClkGpuFreq(void);
u32 t210EmcLoadAll(void);
u32 t210EmcLoadCpu(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,27 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
Result tmp451Initialize(void);
void tmp451Exit(void);
s32 tmp451TempPcb(void);
s32 tmp451TempSoc(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,66 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include "nxExt/apm_ext.h"
#include <stdatomic.h>
static Service g_apmSrv;
static Service g_apmSysSrv;
static atomic_size_t g_refCnt;
Result apmExtInitialize(void)
{
g_refCnt++;
if (serviceIsActive(&g_apmSrv))
{
return 0;
}
Result rc = 0;
rc = smGetService(&g_apmSrv, "apm");
if(R_SUCCEEDED(rc))
{
rc = smGetService(&g_apmSysSrv, "apm:sys");
}
if (R_FAILED(rc))
{
apmExtExit();
}
return rc;
}
void apmExtExit(void)
{
if (--g_refCnt == 0)
{
serviceClose(&g_apmSrv);
serviceClose(&g_apmSysSrv);
}
}
Result apmExtGetPerformanceMode(u32* out_mode)
{
return serviceDispatchOut(&g_apmSrv, 1, *out_mode);
}
Result apmExtSysRequestPerformanceMode(u32 mode)
{
return serviceDispatchIn(&g_apmSysSrv, 0, mode);
}
Result apmExtGetCurrentPerformanceConfiguration(u32* out_conf)
{
return serviceDispatchOut(&g_apmSysSrv, 7, *out_conf);
}

View File

@@ -0,0 +1,28 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include "nxExt/i2c.h"
#define I2C_CMD_SND 0
#define I2C_CMD_RCV 1
Result i2csessionExtRegReceive(I2cSession* s, u8 in, void* out, u8 out_size)
{
u8 cmdlist[5] = {
I2C_CMD_SND | (I2cTransactionOption_Start << 6),
sizeof(in),
in,
I2C_CMD_RCV | (I2cTransactionOption_All << 6),
out_size
};
return i2csessionExecuteCommandList(s, out, out_size, cmdlist, sizeof(cmdlist));
}

View File

@@ -0,0 +1,204 @@
/*
* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include "nxExt/ipc_server.h"
#include <string.h>
Result ipcServerInit(IpcServer* server, const char* name, u32 max_sessions)
{
if(max_sessions < 1 || max_sessions > (MAX_WAIT_OBJECTS - 1))
{
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
}
server->srvName = smEncodeName(name);
server->max = max_sessions + 1;
server->count = 0;
Result rc = smRegisterService(&server->handles[0], server->srvName, false, max_sessions);
if(R_SUCCEEDED(rc))
{
server->count = 1;
}
return rc;
}
Result ipcServerExit(IpcServer* server)
{
for(u32 i = 0; i < server->count; i++)
{
svcCloseHandle(server->handles[i]);
}
server->count = 0;
return smUnregisterService(server->srvName);
}
static Result _ipcServerAddSession(IpcServer* server, Handle session)
{
if(server->count >= server->max)
{
return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
}
server->handles[server->count] = session;
server->count++;
return 0;
}
static Result _ipcServerDeleteSession(IpcServer* server, u32 index)
{
if(!index || index >= server->count)
{
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
}
svcCloseHandle(server->handles[index]);
for(u32 j = index; j < (server->count - 1); j++)
{
server->handles[j] = server->handles[j + 1];
}
server->count--;
return 0;
}
static Result _ipcServerParseRequest(IpcServerRequest* r)
{
u8* base = armGetTls();
r->hipc = hipcParseRequest(base);
r->data.cmdId = 0;
r->data.size = 0;
r->data.ptr = NULL;
if(r->hipc.meta.type == CmifCommandType_Request)
{
IpcServerRawHeader* header = cmifGetAlignedDataStart(r->hipc.data.data_words, base);
size_t dataSize = r->hipc.meta.num_data_words * 4;
if(!header || dataSize < sizeof(IpcServerRawHeader) || header->magic != CMIF_IN_HEADER_MAGIC)
{
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
}
r->data.cmdId = header->cmdId;
if(dataSize > sizeof(IpcServerRawHeader))
{
r->data.size = dataSize - sizeof(IpcServerRawHeader);
r->data.ptr = ((u8*)header) + sizeof(IpcServerRawHeader);
}
}
return 0;
}
static void _ipcServerPrepareResponse(Result rc, void* data, size_t dataSize)
{
u8* base = armGetTls();
HipcRequest hipc = hipcMakeRequestInline(base,
.type = CmifCommandType_Request,
.num_data_words = (sizeof(IpcServerRawHeader) + dataSize + 0x10) / 4,
);
IpcServerRawHeader* rawHeader = cmifGetAlignedDataStart(hipc.data_words, base);
rawHeader->magic = CMIF_OUT_HEADER_MAGIC;
rawHeader->result = rc;
if(R_SUCCEEDED(rc))
{
memcpy(((u8*)rawHeader) + sizeof(IpcServerRawHeader), data, dataSize);
}
}
static Result _ipcServerProcessNewSession(IpcServer* server)
{
Handle session;
Result rc = svcAcceptSession(&session, server->handles[0]);
if(R_SUCCEEDED(rc) && R_FAILED(rc = _ipcServerAddSession(server, session)))
{
svcCloseHandle(session);
}
return rc;
}
static Result _ipcServerProcessSession(IpcServer* server, IpcServerRequestHandler handler, void* userdata, u32 handleIndex)
{
s32 unusedIndex;
IpcServerRequest r;
size_t dataSize = 0;
u8 data[IPC_SERVER_EXT_RESPONSE_MAX_DATA_SIZE];
bool close = false;
Result rc = svcReplyAndReceive(&unusedIndex, &server->handles[handleIndex], 1, 0, UINT64_MAX);
if(R_SUCCEEDED(rc))
{
rc = _ipcServerParseRequest(&r);
}
if(R_SUCCEEDED(rc))
{
switch(r.hipc.meta.type)
{
case CmifCommandType_Request:
_ipcServerPrepareResponse(
handler(userdata, &r, data, &dataSize),
data,
dataSize
);
break;
case CmifCommandType_Close:
_ipcServerPrepareResponse(0, NULL, 0);
close = true;
break;
default:
_ipcServerPrepareResponse(MAKERESULT(11, 403), NULL, 0);
break;
}
rc = svcReplyAndReceive(&unusedIndex, &server->handles[handleIndex], 0, server->handles[handleIndex], 0);
if(rc == KERNELRESULT(TimedOut))
{
rc = 0;
}
}
if(R_FAILED(rc) || close)
{
_ipcServerDeleteSession(server, handleIndex);
}
return rc;
}
Result ipcServerProcess(IpcServer* server, IpcServerRequestHandler handler, void* userdata)
{
s32 handleIndex = -1;
Result rc = svcWaitSynchronization(&handleIndex, server->handles, server->count, UINT64_MAX);
if(R_SUCCEEDED(rc) && (handleIndex < 0 || handleIndex >= server->count))
{
rc = MAKERESULT(Module_Libnx, LibnxError_NotFound);
}
if(R_SUCCEEDED(rc))
{
if(handleIndex)
{
rc = _ipcServerProcessSession(server, handler, userdata, handleIndex);
}
else
{
rc = _ipcServerProcessNewSession(server);
}
}
return rc;
}

View File

@@ -0,0 +1,124 @@
/*
* Fuel gauge driver for Nintendo Switch's Maxim 17050
*
* Copyright (c) 2011 Samsung Electronics
* MyungJoo Ham <myungjoo.ham@samsung.com>
* Copyright (c) 2018 CTCaer
* Copyright (c) 2022 p-sam
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "nxExt/max17050.h"
#include "nxExt/i2c.h"
#define MAX17050_WAIT_NS 1000000000UL
#define MAX17050_VCELL 0x09
#define MAX17050_Current 0x0A
#define MAX17050_AvgCurrent 0x0B
#define MAX17050_AvgVCELL 0x19
#define MAX17050_BOARD_CGAIN 2
#define MAX17050_BOARD_SNS_RESISTOR_UOHM 5000
static I2cSession g_i2c_session;
static u64 g_update_ticks = 0;
static s32 g_power_now = 0;
static s32 g_power_avg = 0;
static Result _max17050_get_power(s32 *out_mw_now, s32 *out_mw_avg)
{
s64 ma, mv;
u16 values[3] = {0};
Result rc = i2csessionExtRegReceive(&g_i2c_session, MAX17050_VCELL, values, sizeof(values));
if (R_SUCCEEDED(rc))
{
ma = (s16)values[1];
ma = ma * 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
mv = (int)(values[0] >> 3) * 625 / 1000;
*out_mw_now = ma * mv / 1000000;
}
if (R_SUCCEEDED(rc))
{
rc = i2csessionExtRegReceive(&g_i2c_session, MAX17050_AvgVCELL, values, sizeof(u16));
}
if (R_SUCCEEDED(rc))
{
ma = (s16)values[2];
ma = ma * 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
mv = (int)(values[0] >> 3) * 625 / 1000;
*out_mw_avg = ma * mv / 1000000;
}
return rc;
}
static void _max17050_update()
{
u64 ticks = armGetSystemTick();
if(armTicksToNs(ticks - g_update_ticks) <= MAX17050_WAIT_NS)
{
return;
}
g_update_ticks = ticks;
if(!serviceIsActive(&g_i2c_session.s))
{
return;
}
_max17050_get_power(&g_power_now, &g_power_avg);
}
Result max17050Initialize(void)
{
Result rc = i2cInitialize();
if(R_SUCCEEDED(rc))
{
rc = i2cOpenSession(&g_i2c_session, I2cDevice_Max17050);
}
return rc;
}
void max17050Exit(void)
{
i2csessionClose(&g_i2c_session);
i2cExit();
}
s32 max17050PowerNow(void)
{
_max17050_update();
return g_power_now;
}
s32 max17050PowerAvg(void)
{
_max17050_update();
return g_power_avg;
}

View File

@@ -0,0 +1,289 @@
/*
* Copyright (c) 2020-2023 CTCaer
* Copyright (c) 2023 p-sam
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nxExt/t210.h"
#define WAIT_NS 1000000000UL
#define usleep(x) svcSleepThread(1000UL * x)
#define GPU_TRIM_SYS_GPCPLL_COEFF 0x4
#define GPU_TRIM_SYS_GPCPLL(x) (*(volatile u32 *)(g_gpu_base + 0x137000ul + (x)))
#define CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL 0x60
#define CLK_RST_CONTROLLER_PTO_CLK_CNT_STATUS 0x64
#define CLK_RST_CONTROLLER_CLK_OUT_ENB_X 0x280
#define CLK_RST_CONTROLLER_RST_DEVICES_X 0x28C
/*! PTO_CLK_CNT */
#define PTO_REF_CLK_WIN_CFG_MASK 0xF
#define PTO_REF_CLK_WIN_CFG_16P 0xF
#define PTO_CNT_EN BIT(9)
#define PTO_CNT_RST BIT(10)
#define PTO_CLK_ENABLE BIT(13)
#define PTO_SRC_SEL_SHIFT 14
#define PTO_SRC_SEL_MASK 0x1FF
#define PTO_DIV_SEL_MASK (3 << 23)
#define PTO_DIV_SEL_GATED (0 << 23)
#define PTO_DIV_SEL_DIV1 (1 << 23)
#define PTO_DIV_SEL_DIV2_RISING (2 << 23)
#define PTO_DIV_SEL_DIV2_FALLING (3 << 23)
#define PTO_DIV_SEL_CPU_EARLY (0 << 23)
#define PTO_DIV_SEL_CPU_LATE (1 << 23)
#define PTO_CLK_CNT_BUSY BIT(31)
#define PTO_CLK_CNT 0xFFFFFF
#define CLK_PTO_CCLK_G 0x12
#define CLK_PTO_EMC 0x24
#define CLOCK(x) (*(volatile u32 *)(g_clk_base + (x)))
/* Actmon Global registers */
#define ACTMON_GLB_STATUS 0x0
#define ACTMON_MCCPU_MON_ACT BIT(8)
#define ACTMON_MCALL_MON_ACT BIT(9)
#define ACTMON_CPU_FREQ_MON_ACT BIT(10)
#define ACTMON_BPMP_MON_ACT BIT(14)
#define ACTMON_CPU_MON_ACT BIT(15)
#define ACTMON_GLB_PERIOD_CTRL 0x4
#define ACTMON_GLB_PERIOD_USEC BIT(8)
#define ACTMON_GLB_PERIOD_SAMPLE(n) (((n) - 1) & 0xFF)
/* Actmon Device Registers */
#define ACTMON_DEV_SIZE 0x40
/* Actmon CTRL */
#define ACTMON_DEV_CTRL_K_VAL(k) (((k) & 7) << 10)
#define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
#define ACTMON_DEV_CTRL_ENB BIT(31)
#define ACTMON_PERIOD_MS 20
#define DEV_COUNT_WEIGHT 1024
#define ACTMON_BASE (g_act_base + 0x800)
#define ACTMON_DEV_BASE (ACTMON_BASE + 0x80)
#define ACTMON(x) (*(volatile u32 *)(ACTMON_BASE + (x)))
typedef enum _actmon_dev_t
{
ACTMON_DEV_CPU,
ACTMON_DEV_BPMP,
ACTMON_DEV_AHB,
ACTMON_DEV_APB,
ACTMON_DEV_CPU_FREQ,
ACTMON_DEV_MC_ALL,
ACTMON_DEV_MC_CPU,
ACTMON_DEV_NUM,
} actmon_dev_t;
typedef struct _actmon_dev_reg_t
{
vu32 ctrl;
vu32 upper_wnark;
vu32 lower_wmark;
vu32 init_avg;
vu32 avg_upper_wmark;
vu32 avg_lower_wmark;
vu32 count_weight;
vu32 count;
vu32 avg_count;
vu32 intr_status;
vu32 ctrl2;
vu32 rsvd[5];
} actmon_dev_reg_t;
static uintptr_t g_clk_base = 0;
static uintptr_t g_gpu_base = 0;
static uintptr_t g_act_base = 0;
static u64 g_update_ticks = 0;
static u32 g_cpu_freq = 0;
static u32 g_gpu_freq = 0;
static u32 g_mem_freq = 0;
static u32 g_emc_lall = 0;
static u32 g_emc_lcpu = 0;
static u32 _clock_get_dev_freq(u32 id)
{
const u32 pto_win = 16;
const u32 pto_osc = 32768;
u32 val = ((id & PTO_SRC_SEL_MASK) << PTO_SRC_SEL_SHIFT) | PTO_DIV_SEL_DIV1 | PTO_CLK_ENABLE | (pto_win - 1);
CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL) = val;
(void)CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL);
usleep(2);
CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL) = val | PTO_CNT_RST;
(void)CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL);
usleep(2);
CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL) = val;
(void)CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL);
usleep(2);
CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL) = val | PTO_CNT_EN;
(void)CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL);
usleep((1000000ULL * pto_win / pto_osc) + 12 + 2); // 502 us.
while (CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_STATUS) & PTO_CLK_CNT_BUSY)
;
u32 cnt = CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_STATUS) & PTO_CLK_CNT;
CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL) = 0;
(void)CLOCK(CLK_RST_CONTROLLER_PTO_CLK_CNT_CNTL);
usleep(2);
u32 freq_khz = (u64)cnt * pto_osc / pto_win / 1000;
return freq_khz;
}
static void _actmon_dev_enable(actmon_dev_t dev, u32 freq, u32 weight)
{
actmon_dev_reg_t *regs = (actmon_dev_reg_t *)(ACTMON_DEV_BASE + (dev * ACTMON_DEV_SIZE));
regs->init_avg = (u32)freq * ACTMON_PERIOD_MS / 2;
regs->count_weight = weight;
regs->ctrl = ACTMON_DEV_CTRL_ENB | ACTMON_DEV_CTRL_ENB_PERIODIC | ACTMON_DEV_CTRL_K_VAL(3); // 8 samples average.
}
static u32 _actmon_dev_get_count_avg(actmon_dev_t dev)
{
actmon_dev_reg_t *regs = (actmon_dev_reg_t *)(ACTMON_DEV_BASE + (dev * ACTMON_DEV_SIZE));
return regs->avg_count;
}
static inline Result _svcQueryMemoryMappingFallback(u64* virtaddr, u64 physaddr, u64 size)
{
if(hosversionAtLeast(10,0,0))
{
u64 out_size;
return svcQueryMemoryMapping(virtaddr, &out_size, physaddr, size);
}
else
{
return svcLegacyQueryIoMapping(virtaddr, physaddr, size);
}
}
static void _clock_update_freqs(void)
{
u64 ticks = armGetSystemTick();
if(armTicksToNs(ticks - g_update_ticks) <= WAIT_NS)
{
return;
}
g_update_ticks = ticks;
if (!g_clk_base)
{
_svcQueryMemoryMappingFallback(&g_clk_base, 0x60006000ul, 0x1000);
}
if(!g_clk_base)
{
return;
}
g_mem_freq = _clock_get_dev_freq(CLK_PTO_EMC) * 1000;
g_cpu_freq = _clock_get_dev_freq(CLK_PTO_CCLK_G) * 1000;
if (!g_gpu_base)
{
_svcQueryMemoryMappingFallback(&g_gpu_base, 0x57000000ul, 0x1000000);
}
if (!g_gpu_base)
{
return;
}
bool gpu_enabled = (CLOCK(CLK_RST_CONTROLLER_CLK_OUT_ENB_X) & BIT(24)) && !(CLOCK(CLK_RST_CONTROLLER_RST_DEVICES_X) & BIT(24));
if(!gpu_enabled)
{
return;
}
if (!g_act_base)
{
_svcQueryMemoryMappingFallback(&g_act_base, 0x6000C000ul, 0x1000);
}
if(!g_act_base)
{
return;
}
const u32 osc = 38400000;
u32 coeff = GPU_TRIM_SYS_GPCPLL(GPU_TRIM_SYS_GPCPLL_COEFF);
u32 divm = coeff & 0xFF;
u32 divn = (coeff >> 8) & 0xFF;
u32 divp = (coeff >> 16) & 0x3F;
g_gpu_freq = osc * divn / (divm * divp) / 2;
u32 emc_freq = g_mem_freq / 1000;
// Check if actmon is disabled
if (!(ACTMON(ACTMON_GLB_STATUS) & ACTMON_MCALL_MON_ACT))
{
ACTMON(ACTMON_GLB_PERIOD_CTRL) = ACTMON_GLB_PERIOD_SAMPLE(ACTMON_PERIOD_MS);
_actmon_dev_enable(ACTMON_DEV_MC_ALL, emc_freq, 256 * 4);
}
// Check if actmon is disabled
if (!(ACTMON(ACTMON_GLB_STATUS) & ACTMON_MCCPU_MON_ACT))
_actmon_dev_enable(ACTMON_DEV_MC_CPU, emc_freq, 256 * 4);
// Get 1000 -> 100.0.
g_emc_lall = (u64)_actmon_dev_get_count_avg(ACTMON_DEV_MC_ALL) * 10 * 100 / (emc_freq * ACTMON_PERIOD_MS);
g_emc_lcpu = (u64)_actmon_dev_get_count_avg(ACTMON_DEV_MC_CPU) * 10 * 100 / (emc_freq * ACTMON_PERIOD_MS);
}
u32 t210ClkCpuFreq(void)
{
_clock_update_freqs();
return g_cpu_freq;
}
u32 t210ClkMemFreq(void)
{
_clock_update_freqs();
return g_mem_freq;
}
u32 t210ClkGpuFreq(void)
{
_clock_update_freqs();
return g_gpu_freq;
}
u32 t210EmcLoadAll()
{
_clock_update_freqs();
return g_emc_lall;
}
u32 t210EmcLoadCpu()
{
_clock_update_freqs();
return g_emc_lcpu;
}

View File

@@ -0,0 +1,102 @@
/*
* SOC/PCB Temperature driver for Nintendo Switch's TI TMP451
*
* Copyright (c) 2018-2024 CTCaer
* Copyright (c) 2024 p-sam
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nxExt/tmp451.h"
#include "nxExt/i2c.h"
#define TMP451_WAIT_NS 1000000000UL
#define TMP451_PCB_TEMP_REG 0x00
#define TMP451_SOC_TEMP_REG 0x01
#define TMP451_SOC_TMP_DEC_REG 0x10
#define TMP451_PCB_TMP_DEC_REG 0x15
static I2cSession g_i2c_session;
static u64 g_update_ticks = 0;
static s32 g_temp_pcb = 0;
static s32 g_temp_soc = 0;
static Result _tmp451_get_temp(u8 reg, u8 dec_reg, s32* out)
{
u8 val = 0;
Result rc = i2csessionExtRegReceive(&g_i2c_session, reg, &val, sizeof(val));
if(R_SUCCEEDED(rc))
{
*out = (s32)val * 1000;
rc = i2csessionExtRegReceive(&g_i2c_session, dec_reg, &val, sizeof(val));
}
if(R_SUCCEEDED(rc))
{
*out += ((s32)(val >> 4) * 625) / 10;
}
return rc;
}
static void _tmp451_update()
{
u64 ticks = armGetSystemTick();
if(armTicksToNs(ticks - g_update_ticks) <= TMP451_WAIT_NS)
{
return;
}
g_update_ticks = ticks;
if(!serviceIsActive(&g_i2c_session.s))
{
return;
}
_tmp451_get_temp(TMP451_PCB_TEMP_REG, TMP451_PCB_TMP_DEC_REG, &g_temp_pcb);
_tmp451_get_temp(TMP451_SOC_TEMP_REG, TMP451_SOC_TMP_DEC_REG, &g_temp_soc);
}
Result tmp451Initialize(void)
{
Result rc = i2cInitialize();
if(R_SUCCEEDED(rc))
{
rc = i2cOpenSession(&g_i2c_session, I2cDevice_Tmp451);
}
return rc;
}
void tmp451Exit(void)
{
i2csessionClose(&g_i2c_session);
i2cExit();
}
s32 tmp451TempPcb(void)
{
_tmp451_update();
return g_temp_pcb;
}
s32 tmp451TempSoc(void)
{
_tmp451_update();
return g_temp_soc;
}