Skip to content

Commit

Permalink
experimental pio support
Browse files Browse the repository at this point in the history
  • Loading branch information
cuckydev committed Oct 1, 2023
1 parent 0178c25 commit 90e3624
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmake/CKSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ function(cksdk_executable name target_exe target_map)
"${INC_DIR}/DLL.h"
"${INC_DIR}/ELF.h"

# PIO
"${INC_DIR}/PIO.h"

# Util
"${INC_DIR}/Util/Fixed.h"
"${INC_DIR}/Util/Queue.h"
Expand Down
6 changes: 6 additions & 0 deletions include/CKSDK/OS.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ namespace CKSDK
/// @brief DUART data port
inline constexpr volatile uint8_t &DuartHra() { return MMIO<uint8_t>(0x2023); }

// PIO
/// @brief PIO data port
inline constexpr volatile uint8_t &PioData() { return *((volatile uint8_t *)0xBF060004); }
/// @brief PIO status port
inline constexpr volatile uint8_t &PioStatus() { return *((volatile uint8_t *)0xBF060005); }

// Clocks
/// @brief CPU clock rate
static constexpr uint32_t CpuHz = 33868800UL;
Expand Down
83 changes: 83 additions & 0 deletions include/CKSDK/PIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
[ CKSDK ]
Copyright 2023 Regan "CKDEV" Green
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/// @file CKSDK/PIO.h
/// @brief CKSDK PIO API

#pragma once

#include <CKSDK/CKSDK.h>

#include <CKSDK/OS.h>

/// @brief CKSDK namespace
namespace CKSDK
{
/// @brief CKSDK PIO namespace
namespace PIO
{
// PIO functions
/// @brief Check if there is a PIO device connected
/// @return True if there is a device connected, false otherwise
static inline bool Connected()
{
return OS::PioStatus() != 0xFF;
}

/// @brief Gheck if the PIO FIFO has data
/// @return True if the FIFO has data, false otherwise
/// @note This function assumes that there is a device connected, if not, it will return true
static inline bool HasData()
{
return OS::PioStatus() & 0x01;
}

/// @brief Check if the PIO FIFO is full
/// @return True if the FIFO is full, false otherwise
/// @note This function assumes that there is a device connected, if not, it will return true
static inline bool IsFull()
{
return OS::PioStatus() & 0x02;
}

/// @brief Flush the PIO FIFO
/// @note This function assumes that there is a device connected, will freeze otherwise
static inline void Flush()
{
while (HasData())
OS::PioData();
}

/// @brief Read a byte from the PIO FIFO
/// @return The byte read from the FIFO
/// @note This function will block until there is data in the FIFO, it assumes that there is a device connected, will freeze otherwise
static inline uint8_t Read()
{
while (!HasData());
return OS::PioData();
}

/// @brief Write a byte to the PIO FIFO
/// @param data The byte to write to the FIFO
/// @note This function will block until there is space in the FIFO, it assumes that there is a device connected, will freeze otherwise
static inline void Write(uint8_t data)
{
while (IsFull());
OS::PioData(data);
}
}
}

0 comments on commit 90e3624

Please sign in to comment.