Skip to content

Commit

Permalink
Merge pull request #136 from 4ms/lennart_wifi_start
Browse files Browse the repository at this point in the history
Start wifi interface
  • Loading branch information
danngreen authored Oct 19, 2023
2 parents d1d380a + fe54b19 commit 870a3d5
Show file tree
Hide file tree
Showing 30 changed files with 1,035 additions and 9 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/build_test_firmware.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ on:
- '!shared/gen_minblep' # Skip generator script (still run CI if generated files change)
- '!shared/tableGen' # Skip generator script (still run CI if generated files change)

jobs:
jobs:
build:
strategy:
matrix:
gcc: ['12.2.Rel1'] # can add other versions if needed
name: "Build firmware"
runs-on: ubuntu-latest
container: ghcr.io/lnnrts/metamodule:latest
steps:
- name: Install Arm GNU Toolchain (arm-none-eabi-gcc)
uses: carlosperate/arm-none-eabi-gcc-action@v1
with:
release: ${{ matrix.gcc }}

- name: Install cmake
uses: jwlawson/actions-setup-cmake@v1.13
with:
cmake-version: '3.26.x'

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Install linux dependencies
run: |
sudo apt install -y ninja-build
token: ${{ secrets.CHECKOUT_TOKEN }}

- name: Build and test
run: cd firmware && make configure && make all
Expand Down
12 changes: 12 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@
[submodule "firmware/lib/jansson/jansson"]
path = firmware/lib/jansson/jansson
url = https://github.com/4ms/jansson.git
[submodule "firmware/lib/esp-serial-flasher"]
path = firmware/lib/esp-serial-flasher
url = https://github.com/4ms/esp-serial-flasher.git
[submodule "firmware/lib/lockfree"]
path = firmware/lib/lockfree
url = https://github.com/DNedic/lockfree.git
[submodule "firmware/lib/flatbuffers"]
path = firmware/lib/flatbuffers
url = https://github.com/google/flatbuffers.git
[submodule "firmware/src/wifi/flat"]
path = firmware/src/wifi/flat
url = https://github.com/4ms/metamodule-bridge-protocol.git
10 changes: 10 additions & 0 deletions firmware/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
"value": "cmake/arm-none-eabi-gcc.cmake"
}
}
},
{
"name": "full",
"inherits": "base",
"cacheVariables": {
"ENABLE_WIFI_BRIDGE":{
"type": "BOOL",
"value": "ON"
}
}
}
],
"buildPresets": [
Expand Down
11 changes: 11 additions & 0 deletions firmware/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LABEL org.opencontainers.image.source=https://github.com/4ms/metamodule
FROM ubuntu:22.04

ARG flatbuffer_version=v23.3.3

RUN apt update && apt upgrade -y && apt clean
RUN apt update && apt install git ninja-build build-essential bsdmainutils xxd python3 -y && apt clean
RUN apt update && apt install -y cmake && git clone --depth 1 https://github.com/google/flatbuffers -b $flatbuffer_version && cd flatbuffers && cmake -S . -B build -G Ninja && cmake --build build --target install && cd .. && rm -rf flatbuffers && apt remove cmake -y && apt clean

RUN flatc --version

1 change: 1 addition & 0 deletions firmware/lib/esp-serial-flasher
Submodule esp-serial-flasher added at 0a0073
1 change: 1 addition & 0 deletions firmware/lib/flatbuffers
Submodule flatbuffers added at 01834d
1 change: 1 addition & 0 deletions firmware/lib/lockfree
Submodule lockfree added at 74e5c6
2 changes: 1 addition & 1 deletion firmware/lib/mdrivlib
88 changes: 88 additions & 0 deletions firmware/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
include(${CMAKE_SOURCE_DIR}/cmake/common.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/arch_mp15xa7.cmake)

option(ENABLE_WIFI_BRIDGE OFF "Enable serial bridge to wifi module")

set(FWDIR ${CMAKE_SOURCE_DIR})
set(SHARED ${FWDIR}/../shared)

Expand Down Expand Up @@ -104,6 +106,36 @@ target_link_libraries(ui PRIVATE lvgl::lvgl mp15xa7_arch)
# Fix for SLS generated files (See https://forum.squareline.io/t/unused-variable-target-warning-when-compiling/1610/3 )
target_compile_options(ui PRIVATE -Wno-unused-variable)


#
# ESP Serial Flasher
#

if (ENABLE_WIFI_BRIDGE)
set(ESP_SERIAL_FLASHER_PORT "CUSTOM")
add_subdirectory(${FWDIR}/lib/esp-serial-flasher ${CMAKE_CURRENT_BINARY_DIR}/esp-serial-flasher)
target_link_libraries(flasher PRIVATE mp15xa7_arch)
endif()

#
# Lockfree
#

if (ENABLE_WIFI_BRIDGE)
add_subdirectory(${FWDIR}/lib/lockfree ${CMAKE_CURRENT_BINARY_DIR}/lockfree)
target_link_libraries(lockfree INTERFACE mp15xa7_arch)
endif()

#
# Flatbuffers
# (Manually create a target because original cmake includes the flatc compiler)
#

if (ENABLE_WIFI_BRIDGE)
add_library(flatbuffers INTERFACE)
target_include_directories(flatbuffers INTERFACE ${FWDIR}/lib/flatbuffers/include/)
endif()

#
# Main App
#
Expand All @@ -126,6 +158,7 @@ add_executable(
${FWDIR}/src/fs/fatfs/diskio.cc
${FWDIR}/src/fs/fatfs/fattime.cc
${FWDIR}/src/fs/time_convert.cc

#
${SHARED}/CoreModules/hub/hub_medium.cc
${SHARED}/patch_convert/yaml_to_patch.cc
Expand Down Expand Up @@ -180,6 +213,36 @@ add_executable(
${FACEPLATE_ARTWORK_SOURCES}
)

if (ENABLE_WIFI_BRIDGE)

target_compile_definitions(main.elf PRIVATE
ENABLE_WIFI_BRIDGE=1
)

target_sources(main.elf PRIVATE
${FWDIR}/src/wifi/wifi_interface.cc
${FWDIR}/src/wifi/flasher/flasher.cpp
${FWDIR}/src/wifi/flasher/flasher.h
${FWDIR}/src/wifi/flasher/implementation.cpp
${FWDIR}/src/wifi/flasher/BufferedUSART.h
${FWDIR}/src/wifi/flasher/BufferedUSART.cpp
${FWDIR}/src/wifi/comm/BufferedUSART2.h
${FWDIR}/src/wifi/comm/BufferedUSART2.cpp
${FWDIR}/src/wifi/comm/framing/Configuration.h
${FWDIR}/src/wifi/comm/framing/Deframer.cpp
${FWDIR}/src/wifi/comm/framing/Deframer.h
${FWDIR}/src/wifi/comm/framing/DynamicDeframer.cpp
${FWDIR}/src/wifi/comm/framing/DynamicDeframer.h
${FWDIR}/src/wifi/comm/framing/Framer.cpp
${FWDIR}/src/wifi/comm/framing/Framer.h
${FWDIR}/src/wifi/comm/framing/StaticDeframer.h
)
else()
target_compile_definitions(main.elf PRIVATE
ENABLE_WIFI_BRIDGE=0
)
endif()

# Fixup for compiler warning on files that include LVGL 8.3.4 headers:
set_source_files_properties(
${FWDIR}/src/core_a7/aux_core_main.cc
Expand Down Expand Up @@ -212,8 +275,33 @@ target_include_directories(
${FWDIR}/lib/jansson/jansson/src
)

if (ENABLE_WIFI_BRIDGE)

# Locate flatbuffer compiler
# This falls back to the local flatabuffers repository if not found on PATH
find_program(FLATBUFFERS_FLATC_EXECUTABLE flatc
PATHS ${FWDIR}/lib/flatbuffers/build
REQUIRED)

message("Found flatbuffers compiler at " ${FLATBUFFERS_FLATC_EXECUTABLE})

# Compile flat buffer definitions to cpp headers
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${FWDIR}/lib/flatbuffers/CMake)
include(BuildFlatBuffers)
build_flatbuffers("${CMAKE_CURRENT_LIST_DIR}/wifi/flat/all.fbs" "" flatbuffer_messages_utils "" ${CMAKE_CURRENT_BINARY_DIR}/flat "" "")

# Create library with generated flatbuffer headers
add_library(flatbuffer_messages INTERFACE)
add_dependencies(flatbuffer_messages flatbuffer_messages_utils)
target_include_directories(flatbuffer_messages INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/flat)
endif()

target_link_libraries(main.elf PRIVATE lvgl::lvgl mp15xa7_arch mdrivlib_interface ui ryml VCV_adaptor)

if (ENABLE_WIFI_BRIDGE)
target_link_libraries(main.elf PRIVATE flasher lockfree flatbuffers flatbuffer_messages)
endif()

foreach(brand ${brands})
target_link_libraries(main.elf PRIVATE ${brand}Library)
endforeach()
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/console/uart_log.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace MetaModule
{

struct UartLog {
static inline mdrivlib::Uart<UartConfig> log_uart;
static inline mdrivlib::Uart<LogUartConfig> log_uart;

UartLog() {
init();
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/medium/conf/console_uart_conf.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "drivers/uart_conf.hh"

//p11:
constexpr inline UartConf UartConfig{
constexpr inline UartConf LogUartConfig{
.base_addr = UART7_BASE,
.TXPin = {mdrivlib::GPIO::B, mdrivlib::PinNum::_4, mdrivlib::PinAF::AltFunc13},
.RXPin = {mdrivlib::GPIO::B, mdrivlib::PinNum::_3, mdrivlib::PinAF::AltFunc13},
Expand Down
36 changes: 36 additions & 0 deletions firmware/src/medium/conf/wifi_uart_conf.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include "drivers/uart_conf.hh"
#include "drivers/pin.hh"


constexpr inline UartConf WifiBootloaderUartConfig{
.base_addr = USART6_BASE,
.TXPin = {mdrivlib::GPIO::G, mdrivlib::PinNum::_14, mdrivlib::PinAF::AltFunc7},
.RXPin = {mdrivlib::GPIO::G, mdrivlib::PinNum::_9, mdrivlib::PinAF::AltFunc7},
.mode = UartConf::Mode::TXRX,
.baud = 115200, // 230400, 921600
.wordlen = 8,
.parity = UartConf::Parity::None,
.stopbits = UartConf::StopBits::_1,
};

constexpr inline UartConf WifiUartConfig{
.base_addr = UART5_BASE,
.TXPin = {mdrivlib::GPIO::B, mdrivlib::PinNum::_13, mdrivlib::PinAF::AltFunc14},
.RXPin = {mdrivlib::GPIO::B, mdrivlib::PinNum::_12, mdrivlib::PinAF::AltFunc14},
.mode = UartConf::Mode::TXRX,
.baud = 115200, // 230400, 921600
.wordlen = 8,
.parity = UartConf::Parity::None,
.stopbits = UartConf::StopBits::_1,
};

constexpr inline mdrivlib::PinDef WifiBootloaderResetConfig{
mdrivlib::GPIO::G,
mdrivlib::PinNum::_13,
};

constexpr inline mdrivlib::PinDef WifiBootloaderBootSelectConfig{
mdrivlib::GPIO::G,
mdrivlib::PinNum::_8,
};
73 changes: 73 additions & 0 deletions firmware/src/wifi/comm/BufferedUSART2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "BufferedUSART2.h"

#include "conf/wifi_uart_conf.hh"
#include "drivers/uart.hh"
#include "drivers/uart_conf.hh"
#include "drivers/interrupt.hh"

#include <stm32mp1xx.h>
#include <stm32mp1xx_ll_usart.h>
#include <stm32mp1xx_ll_usart.h>

#include <console/pr_dbg.hh>

static mdrivlib::Uart<WifiUartConfig> commMain;
Queue<uint8_t,256> BufferedUSART2::queue;

#define USART_PERIPH UART5
#define USART_IRQ UART5_IRQn
#define USART_IRQ_PRIO 3

void BufferedUSART2::init()
{
initPeripheral();
}

bool BufferedUSART2::setBaudrate(uint32_t baudRate)
{
return commMain.set_baudrate(baudRate);
}

void BufferedUSART2::initPeripheral()
{
commMain.init();

mdrivlib::Interrupt::register_and_start_isr(USART_IRQ, USART_IRQ_PRIO, 0, []()
{
if (LL_USART_IsActiveFlag_RXNE_RXFNE(USART_PERIPH))
{
do
{
auto val = USART_PERIPH->RDR;

auto result = queue.Push(val);
if (not result)
{
pr_err("RX Overrun\n");
}
}
while (LL_USART_IsActiveFlag_RXNE(USART_PERIPH));
}
else
{
printf_("No flag\n");
}
});

// read RX from hardware to clear RXNE flag
(void)USART_PERIPH->RDR;

LL_USART_EnableIT_RXNE_RXFNE(USART_PERIPH);
}

void BufferedUSART2::transmit(uint8_t val)
{
commMain.transmit(val);
}

std::optional<uint8_t> BufferedUSART2::receive()
{
return queue.PopOptional();
}


24 changes: 24 additions & 0 deletions firmware/src/wifi/comm/BufferedUSART2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <cstdint>
#include <spsc/queue.hpp>
using namespace lockfree::spsc;



class BufferedUSART2
{
public:
static void init();

static bool setBaudrate(uint32_t);

static void transmit(uint8_t);
static std::optional<uint8_t> receive();

private:
static void initPeripheral();

private:
static Queue<uint8_t,256> queue;
};
18 changes: 18 additions & 0 deletions firmware/src/wifi/comm/framing/Configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Author: Lennart@binarylabs.dev */

#pragma once

#include <cstdint>

namespace Framing
{

struct Configuration_t
{
uint8_t start;
uint8_t end;
uint8_t escape;
};


}
Loading

0 comments on commit 870a3d5

Please sign in to comment.