From cf825bca793729bbe31fabf0c0005cfa57dc20b1 Mon Sep 17 00:00:00 2001 From: Piotr Lewandowski Date: Mon, 10 Jul 2023 02:03:12 +0200 Subject: [PATCH] fsk-tests --- .vscode/settings.json | 37 ++++++++- libs/CMakeLists.txt | 3 +- libs/k5_uv_system/system.hpp | 24 +++++- libs/radio/CMakeLists.txt | 22 ++++++ libs/radio/radio.cpp | 1 + libs/radio/radio.hpp | 118 ++++++++++++++++++++++++++++ src/most_useless_mod/CMakeLists.txt | 1 + src/most_useless_mod/main.cpp | 9 ++- src/most_useless_mod/spectrum.hpp | 75 ++++++++++++------ src/spectrum/spectrum.hpp | 13 +-- 10 files changed, 262 insertions(+), 41 deletions(-) create mode 100644 libs/radio/CMakeLists.txt create mode 100644 libs/radio/radio.cpp create mode 100644 libs/radio/radio.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 32f8de7..77bf794 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,41 @@ "ostream": "cpp", "stdexcept": "cpp", "streambuf": "cpp", - "type_traits": "cpp" + "type_traits": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iostream": "cpp", + "limits": "cpp", + "ranges": "cpp", + "typeinfo": "cpp" } } \ No newline at end of file diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt index 479f9b6..70e1fc2 100644 --- a/libs/CMakeLists.txt +++ b/libs/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(k5_uv_system) -add_subdirectory(lcd) \ No newline at end of file +add_subdirectory(lcd) +add_subdirectory(radio) \ No newline at end of file diff --git a/libs/k5_uv_system/system.hpp b/libs/k5_uv_system/system.hpp index 4af3065..ca67d03 100644 --- a/libs/k5_uv_system/system.hpp +++ b/libs/k5_uv_system/system.hpp @@ -55,7 +55,17 @@ namespace System char* (*sprintf)(char *, const char *, ...); void(*FillWithZero)(unsigned char* p8Data, unsigned int u32Size); char* (*FormatString)(char *, const char *, ...); - + void(*PlayTone)(unsigned int u32Frequency, unsigned int u32TuningGain); + void(*PrintSmallDigits)(unsigned int u32Len, const int* p32Number, int s32X, int s32Y); + void(*PrintFrequency)(int frequency,int xpos,int ypos,int param_4,int param_5); + void(*AirCopy72)(unsigned char*); + void(*AirCopyFskSetup)(); + void(*BK4819Reset)(); + void(*IntDivide)(int s32Divident, int s32Divisor); + void(*BK4819WriteFrequency)(unsigned int u32Frequency); + void(*BK4819SetPaGain)(unsigned short u16PaBias, unsigned int u32Frequency); + void(*BK4819ConfigureAndStartTxFsk)(); + }; struct TOrgData @@ -70,7 +80,7 @@ namespace System { .PrintTextOnScreen = (decltype(TOrgFunctions::PrintTextOnScreen))(0x874C + 1), .FillScreenMemory = (decltype(TOrgFunctions::FillScreenMemory))(0xb70c + 1), - .DelayMs = (decltype(TOrgFunctions::DelayMs)) (0xD0EE + 1), + .DelayMs = (decltype(TOrgFunctions::DelayMs)) (0xD0EC + 1), .DelayUs = (decltype(TOrgFunctions::DelayUs)) (0xD100 + 1), .WriteSerialData = (int(*)(unsigned char*, unsigned char))(0xBE44 + 1), .BK4819Write = (decltype(TOrgFunctions::BK4819Write) (0xAF00 + 1)), @@ -80,6 +90,16 @@ namespace System .sprintf = (decltype(TOrgFunctions::sprintf) (0xc8ec + 1)), .FillWithZero = (decltype(TOrgFunctions::FillWithZero) (0x1AA + 1)), .FormatString = (decltype(TOrgFunctions::FormatString) (0xC6E8 + 1)), + .PlayTone = (decltype(TOrgFunctions::PlayTone) (0x9F14 + 1)), + .PrintSmallDigits = (decltype(TOrgFunctions::PrintSmallDigits) (0x870C + 1)), + .PrintFrequency = (decltype(TOrgFunctions::PrintFrequency) (0x864C + 1)), + .AirCopy72 = (decltype(TOrgFunctions::AirCopy72) (0xA67C + 1)), + .AirCopyFskSetup = (decltype(TOrgFunctions::AirCopyFskSetup) (0xA518 + 1)), + .BK4819Reset = (decltype(TOrgFunctions::BK4819Reset) (0xa7cc + 1)), + .IntDivide = (decltype(TOrgFunctions::IntDivide) (0x128 + 1)), + .BK4819WriteFrequency = (decltype(TOrgFunctions::BK4819WriteFrequency) (0xaabc + 1)), + .BK4819SetPaGain = (decltype(TOrgFunctions::BK4819SetPaGain) (0xaad4 + 1)), + .BK4819ConfigureAndStartTxFsk = (decltype(TOrgFunctions::BK4819ConfigureAndStartTxFsk) (0x1cd8 + 1)), }; inline const TOrgData OrgData_01_26 = diff --git a/libs/radio/CMakeLists.txt b/libs/radio/CMakeLists.txt new file mode 100644 index 0000000..3c62d81 --- /dev/null +++ b/libs/radio/CMakeLists.txt @@ -0,0 +1,22 @@ +set(NAME radio) + +add_library(${NAME} STATIC) + +target_include_directories(${NAME} INTERFACE + . +) + +target_include_directories(${NAME} PRIVATE + . +) + +target_sources(${NAME} PRIVATE + radio.cpp +) + +target_link_libraries(${NAME} + uv_k5_system +) + +target_compile_options(${NAME} PRIVATE ${COMPILER_OPTIONS}) + diff --git a/libs/radio/radio.cpp b/libs/radio/radio.cpp new file mode 100644 index 0000000..c76ff8e --- /dev/null +++ b/libs/radio/radio.cpp @@ -0,0 +1 @@ +#include "radio.hpp" \ No newline at end of file diff --git a/libs/radio/radio.hpp b/libs/radio/radio.hpp new file mode 100644 index 0000000..853c32d --- /dev/null +++ b/libs/radio/radio.hpp @@ -0,0 +1,118 @@ +#pragma once +#include "system.hpp" +#include "registers.hpp" +#include + +namespace Radio +{ + enum class eModulation : unsigned char + { + Ffsk, + Fsk, + }; + + struct TRadioConf + { + eModulation Modulation; + }; + + enum class eState : unsigned char + { + Idle, + RxPending, + }; + + struct IRadioUser + { + virtual void RxDoneHandler(){}; + }; + + template + class CBK4819 + { + public: + CBK4819(){}; + + void SetFrequency(unsigned int u32FrequencyD10) + { + Fw.BK4819WriteFrequency(u32FrequencyD10); + } + + unsigned int GetFrequency() + { + return 0; + } + + void SendSyncAirCopyMode72(unsigned char *p8Data) + { + Fw.BK4819ConfigureAndStartTxFsk(); + Fw.AirCopyFskSetup(); + Fw.AirCopy72(p8Data); + } + + void RecieveAsyncAirCopyMode(unsigned char *p8Data, unsigned char u8DataLen, IRadioUser* pUser) + { + if (!p8Data || !u8DataLen) + { + return; + } + + pRadioUser = pUser; + p8RxBuff = p8Data; + u8RxBuffSize = u8DataLen; + + Fw.AirCopyFskSetup(); + Fw.BK4819Write(0x59, 1 << 14); // clear rx fifo + Fw.BK4819Write(0x59, 0b0011'0000'0110'1000); // enable rx + Fw.BK4819Write(0x3f, 1 << 13); // enable rx done irq + + Fw.BK4819Write(0x30, 0); + Fw.BK4819Write(0x30, 0b1011'1101'1111'0001); + State = eState::RxPending; + } + + bool IsLockedByOrgFw() + { + return !(GPIOC->DATA & 0b1); + } + + void InterruptHandler() + { + if (IsLockedByOrgFw()) + { + return; + } + + if (State == eState::RxPending) + { + auto const IrqReg = Fw.BK4819Read(0x02); + if (IrqReg & (1 << 13)) + { + State = eState::Idle; + Fw.BK4819Write(0x2, 0); + unsigned char u8DataLen = Fw.BK4819Read(0x02); + u8DataLen = (u8DataLen >> 8) & 0xFF; + if (!p8RxBuff) + return; + + u8DataLen = u8RxBuffSize < u8DataLen ? u8RxBuffSize : u8DataLen; + for (unsigned char i = 0; i < u8DataLen; i++) + { + p8RxBuff[i] = Fw.BK4819Read(0x5F); + } + + if(pRadioUser) + { + pRadioUser->RxDoneHandler(); + } + } + } + } + + private: + IRadioUser* pRadioUser; + unsigned char *p8RxBuff; + unsigned char u8RxBuffSize; + eState State; + }; +} \ No newline at end of file diff --git a/src/most_useless_mod/CMakeLists.txt b/src/most_useless_mod/CMakeLists.txt index b9afddb..bb93779 100644 --- a/src/most_useless_mod/CMakeLists.txt +++ b/src/most_useless_mod/CMakeLists.txt @@ -11,6 +11,7 @@ target_link_libraries(${NAME} orginal_fw uv_k5_system lcd + radio ) target_include_directories(${NAME} PUBLIC diff --git a/src/most_useless_mod/main.cpp b/src/most_useless_mod/main.cpp index 0207f60..f18e401 100644 --- a/src/most_useless_mod/main.cpp +++ b/src/most_useless_mod/main.cpp @@ -3,11 +3,14 @@ #include "registers.hpp" #include "uv_k5_display.hpp" #include "spectrum.hpp" +#include "radio.hpp" #include extern "C" void __libc_init_array(); -CSpectrum Spectrum; +Radio::CBK4819 RadioDriver; +CSpectrum Spectrum; int main() { @@ -24,9 +27,11 @@ void MultiIrq_Handler(unsigned int u32IrqSource) __libc_init_array(); bFirstInit = true; } + + RadioDriver.InterruptHandler(); static unsigned int u32StupidCounter = 1; - if((!(u32StupidCounter++ % 15) && u32StupidCounter > 200)) + if((!(u32StupidCounter++ % 64) && u32StupidCounter > 200)) { Spectrum.Handle(); } diff --git a/src/most_useless_mod/spectrum.hpp b/src/most_useless_mod/spectrum.hpp index b1dcef8..7ae23d1 100644 --- a/src/most_useless_mod/spectrum.hpp +++ b/src/most_useless_mod/spectrum.hpp @@ -1,17 +1,29 @@ #pragma once #include "system.hpp" #include "uv_k5_display.hpp" +#include "radio.hpp" -#define REVERSE_UINT16(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) - -template -class CSpectrum +template & RadioDriver> +class CSpectrum : public Radio::IRadioUser { public: static constexpr auto StepSize = 0xFFFF / TUV_K5Display::SizeX; static constexpr auto StepSizeFreq = 10'000; + + enum eState : unsigned char + { + Init, + Idle, + RxPending, + SendData, + RxDone, + Exit, + }; + CSpectrum() - : DisplayBuff(FwData.pDisplayBuffer), FontSmallNr(FwData.pSmallDigs), Display(DisplayBuff), x(DisplayBuff.SizeX/2), y(DisplayBuff.SizeY/2){}; + : DisplayBuff(FwData.pDisplayBuffer), FontSmallNr(FwData.pSmallDigs), Display(DisplayBuff), State(eState::SendData), u8RxCnt(0){}; void Handle() { @@ -20,31 +32,48 @@ public: return; } - if (!(Fw.BK4819Read(0x0C) & 0b10)) + switch(State) { - return; + case eState::Init: + { + RadioDriver.RecieveAsyncAirCopyMode(U8RxBuff, sizeof(U8RxBuff), this); + State = eState::RxPending; + break; + } + + case eState::RxDone: + { + u8RxCnt++; + char kupa[20]; + Fw.FormatString(kupa, "test %u", u8RxCnt); + Fw.PrintTextOnScreen(kupa, 0, 127, 0, 8, 0); + Fw.FlushFramebufferToScreen(); + State = eState::Init; + break; + } + + case eState::SendData: + { + RadioDriver.SendSyncAirCopyMode72((unsigned char*)this); + return; + } + + default: + return; } + } - char kupa[20]; - - Fw.FormatString(kupa, "test %u", 100); - - - Fw.PrintTextOnScreen(kupa, 0, 127, 0, 8, 0); - Fw.FlushFramebufferToScreen(); + void RxDoneHandler() override + { + State = eState::RxDone; } private: - void SetFrequency(unsigned int u32Freq) - { - u32Freq /= 10; - Fw.BK4819Write(0x39, REVERSE_UINT16((u32Freq >> 16) & 0xFFFF)); - Fw.BK4819Write(0x38, REVERSE_UINT16(u32Freq & 0xFFFF)); - } - TUV_K5Display DisplayBuff; const TUV_K5SmallNumbers FontSmallNr; CDisplay Display; - - unsigned char x, y; + eState State; + unsigned char U8RxBuff[72]; + unsigned char u8RxCnt; + }; \ No newline at end of file diff --git a/src/spectrum/spectrum.hpp b/src/spectrum/spectrum.hpp index 5162aed..ca074ec 100644 --- a/src/spectrum/spectrum.hpp +++ b/src/spectrum/spectrum.hpp @@ -194,18 +194,7 @@ private: { bEnabled = false; } - // u8LastBtnPressed = Fw.PollKeyboard(); - // if (u8LastBtnPressed == EnableKey) - // { - // u8PressCnt++; - // } - - // if (u8PressCnt > (bEnabled ? 3 : PressDuration)) - // { - // u8PressCnt = 0; - // bEnabled = !bEnabled; - // } - + return bEnabled; }