Skip to content

Commit

Permalink
allow for simulator build
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Oct 29, 2023
1 parent dbbe2aa commit 0ae9a64
Show file tree
Hide file tree
Showing 53 changed files with 694 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- id: set-targets
run: echo "targets=[$(grep '\[env:' platformio.ini | sed 's/.*:\(.*\)].*/"\1"/' | tr '\n' ',')]" >> $GITHUB_OUTPUT
run: echo "targets=[$(grep '\[env:' platformio.ini | sed 's/.*:\(.*\)].*/"\1"/' | grep -v simulator | tr '\n' ',')]" >> $GITHUB_OUTPUT

build:
needs: targets
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ go.mod
go.sum

/.pio
__pycache__
__pycache__
flash.bin
15 changes: 15 additions & 0 deletions boards/SIMULATOR.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "native",
"build": {
"core": "native",
"cpu": "native",
"extra_flags": "",
"mcu": "native"
},
"upload": {
"maximum_ram_size": 104857600,
"maximum_size": 10485760
},
"url": "",
"vendor": ""
}
27 changes: 23 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ extra_configs =

[common]
lib_archive = no
debug_tool = stlink
upload_protocol = dfu
platform_packages = toolchain-gccarmnoneeabi@~1.120301.0
build_src_filter = +<*> -<.git/> -<.svn/> -<example/> -<examples/> -<test/> -<tests/> -<system/> -<driver/stm32> -<driver/at32>
build_src_filter = +<*> -<.git/> -<.svn/> -<example/> -<examples/> -<test/> -<tests/> -<system/> -<driver/stm32> -<driver/at32> -<driver/native>
extra_scripts =
pre:script/pre_script.py
post:script/post_script.py
Expand All @@ -33,9 +30,28 @@ build_flags =
-DUSE_FULL_LL_DRIVER
-Isrc

[env:simulator]
extends = common
board = SIMULATOR
platform = native
debug_tool = custom
build_src_filter = ${common.build_src_filter} +<driver/native> +<system/native>
debug_build_flags =
-O0
-ggdb3
-g3
build_flags =
${common.build_flags}
-lm
-DSIMULATOR
-Isrc/system/native

[stm32]
extends = common
debug_tool = stlink
upload_protocol = dfu
platform = ststm32@~17.0.0
platform_packages = toolchain-gccarmnoneeabi@~1.120301.0
build_src_filter = ${common.build_src_filter} +<driver/stm32>
framework = stm32cube
board_build.stm32cube.custom_system_setup = yes
Expand Down Expand Up @@ -135,7 +151,10 @@ extends = stm32h743

[at32]
extends = common
debug_tool = stlink
upload_protocol = dfu
platform = https://github.com/ArteryTek/platform-arterytekat32.git#5729d36
platform_packages = toolchain-gccarmnoneeabi@~1.120301.0
framework = at32firmlib
board_build.at32firmlib.custom_system_setup = yes
build_src_filter = ${common.build_src_filter} +<driver/at32>
Expand Down
10 changes: 10 additions & 0 deletions src/config/feature.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#pragma once

#ifndef SIMULATOR
#define USE_ADC
#define USE_SPI
#define USE_SERIAL
#define USE_GYRO
#define USE_SOFT_SERIAL
#define USE_SDCARD
#define USE_DATA_FLASH
#define USE_BLACKBOX

#define USE_VTX
#define USE_DIGITAL_VTX
#define USE_MAX7456

#define USE_MOTOR_DSHOT
#define USE_MOTOR_PWM

#define USE_RX_UNIFIED

#ifndef AT32F4
#define USE_RX_SPI_FRSKY
#define USE_RX_SPI_FLYSKY
#define USE_RX_SPI_EXPRESS_LRS
#endif
#endif
6 changes: 4 additions & 2 deletions src/core/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void flash_save() {
fmc_write_buf(offset, buffer, size);
}

#ifdef USE_VTX
{
const uint32_t offset = VTX_STORAGE_OFFSET;
const uint32_t size = VTX_STORAGE_SIZE;
Expand All @@ -125,6 +126,7 @@ void flash_save() {

fmc_write_buf(offset, buffer, size);
}
#endif

fmc_lock();
__enable_irq();
Expand Down Expand Up @@ -188,11 +190,11 @@ void flash_load() {

cbor_result_t res = cbor_decode_profile_t(&dec, &profile);
if (res < CBOR_OK) {
fmc_lock();
failloop(FAILLOOP_FAULT);
}
}

#ifdef USE_VTX
if (fmc_read(VTX_STORAGE_OFFSET) == (FMC_MAGIC | VTX_STORAGE_OFFSET)) {
const uint32_t offset = VTX_STORAGE_OFFSET + FMC_MAGIC_SIZE;
const uint32_t size = VTX_STORAGE_SIZE - FMC_MAGIC_SIZE;
Expand All @@ -206,8 +208,8 @@ void flash_load() {

cbor_result_t res = cbor_decode_vtx_settings_t(&dec, &vtx_settings);
if (res < CBOR_OK) {
fmc_lock();
failloop(FAILLOOP_FAULT);
}
}
#endif
}
3 changes: 3 additions & 0 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "io/buzzer.h"
#include "io/led.h"
#include "io/rgb_led.h"
#include "io/simulator.h"
#include "io/usb_configurator.h"
#include "io/vbat.h"
#include "io/vtx.h"
Expand Down Expand Up @@ -100,6 +101,7 @@ __attribute__((__used__)) int main() {
buzzer_init();

usb_init();
simulator_init();

rx_spektrum_bind();

Expand Down Expand Up @@ -146,6 +148,7 @@ __attribute__((__used__)) int main() {
while (1) {
// updates looptime counters & runs auto detect
looptime_update();
simulator_update();

perf_counter_start(PERF_COUNTER_TOTAL);

Expand Down
4 changes: 4 additions & 0 deletions src/core/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "driver/at32/system.h"
#endif

#ifdef SIMULATOR
#include "driver/native/system.h"
#endif

#ifdef USE_FAST_RAM
#define FAST_RAM __attribute__((section(".fast_ram"), aligned(4)))
#else
Expand Down
15 changes: 14 additions & 1 deletion src/driver/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "core/profile.h"
#include "core/project.h"

#ifdef USE_ADC

#define VBAT_SCALE (profile.voltage.vbat_scale * (1.f / 10000.f))

uint16_t adc_array[ADC_CHAN_MAX];
Expand Down Expand Up @@ -41,4 +43,15 @@ float adc_read(adc_chan_t chan) {
default:
return adc_read_raw(chan);
}
}
}
#else
void adc_init() {}
float adc_read(adc_chan_t chan) {
switch (chan) {
case ADC_CHAN_VBAT:
return 4.20f;
default:
return 0;
}
}
#endif
4 changes: 4 additions & 0 deletions src/driver/interrupt.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "driver/interrupt.h"

void interrupt_enable(IRQn_Type irq, uint32_t prio) {
#ifndef SIMULATOR
NVIC_SetPriorityGrouping(3);
NVIC_SetPriority(irq, prio);
NVIC_EnableIRQ(irq);
#endif
}

void interrupt_disable(IRQn_Type irq) {
#ifndef SIMULATOR
NVIC_DisableIRQ(irq);
#endif
}
10 changes: 10 additions & 0 deletions src/driver/interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
#define USB_PRIORITY 0x6

static inline void __basepri_restore(uint8_t *val) {
#ifndef SIMULATOR
__set_BASEPRI(*val);
#endif
}

static inline uint8_t __basepri_set_max(uint8_t prio) {
#ifndef SIMULATOR
__set_BASEPRI_MAX(prio << (8U - __NVIC_PRIO_BITS));
#endif
return 1;
}

Expand All @@ -26,15 +30,21 @@ static inline uint8_t __basepri_set_max(uint8_t prio) {
for (uint8_t __basepri_save __attribute__((__cleanup__(__basepri_restore), __unused__)) = __get_BASEPRI(), __todo = __basepri_set_max(prio); __todo; __todo = 0)

__attribute__((always_inline)) static inline int __int_disable_irq() {
#ifndef SIMULATOR
volatile int primask = __get_PRIMASK();
__disable_irq();
return primask & 1;
#else
return 0;
#endif
}

__attribute__((always_inline)) static inline void __int_restore_irq(int *primask) {
#ifndef SIMULATOR
if ((*primask) == 0) {
__enable_irq();
}
#endif
}

#define ATOMIC_BLOCK_ALL \
Expand Down
6 changes: 6 additions & 0 deletions src/driver/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ void motor_update() {
motor_pwm_write(motor_values);
#endif
}
#ifdef SIMULATOR
extern float simulator_motor_values[MOTOR_PIN_MAX];
for (uint32_t i = 0; i < MOTOR_PIN_MAX; i++) {
simulator_motor_values[i] = motor_values[i];
}
#endif
}

void motor_set_direction(motor_direction_t dir) {
Expand Down
56 changes: 56 additions & 0 deletions src/driver/native/fmc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "driver/fmc.h"

#include <stdio.h>
#include <string.h>

#include "core/project.h"

#define FILENAME "flash.bin"

void fmc_lock() {
}

void fmc_unlock() {
}

void fmc_erase() {
remove(FILENAME);
}

static FILE *open_file() {
FILE *file = fopen(FILENAME, "rb+");
if (file == NULL) {
file = fopen(FILENAME, "wb+");
}
return file;
}

flash_word_t fmc_read(uint32_t addr) {
FILE *file = open_file();
fseek(file, addr, SEEK_SET);
flash_word_t value;
fread(&value, sizeof(flash_word_t), 1, file);
fclose(file);
return value;
}

void fmc_read_buf(uint32_t addr, uint8_t *data, uint32_t size) {
FILE *file = open_file();
fseek(file, addr, SEEK_SET);
fread(data, size, 1, file);
fclose(file);
}

void fmc_write(uint32_t addr, flash_word_t value) {
FILE *file = open_file();
fseek(file, addr, SEEK_SET);
fwrite(&value, sizeof(flash_word_t), 1, file);
fclose(file);
}

void fmc_write_buf(uint32_t addr, uint8_t *data, uint32_t size) {
FILE *file = open_file();
fseek(file, addr, SEEK_SET);
fwrite(data, size, 1, file);
fclose(file);
}
Loading

0 comments on commit 0ae9a64

Please sign in to comment.