From fb9d8afb6ca5e12112429fa26834067b61610c48 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Fri, 12 Mar 2021 14:15:27 +0100 Subject: [PATCH] Adapted MK22 vcom sources to miosix kernel but not activating it. Reconfigured x86 emulator. --- meson.build | 16 ++-- .../MK22FN512xxx12/boot/libc_integration.cpp | 45 ++++++----- .../mcu/MK22FN512xxx12/drivers/usb_vcom.c | 2 +- .../mcu/MK22FN512xxx12/drivers/usb_vcom.h | 8 ++ platform/targets/linux/emulator/emulator.c | 74 ++++++------------- platform/targets/linux/emulator/emulator.h | 8 +- platform/targets/linux/platform.c | 2 + 7 files changed, 74 insertions(+), 81 deletions(-) diff --git a/meson.build b/meson.build index 6b9d3716..5b5a379d 100644 --- a/meson.build +++ b/meson.build @@ -104,13 +104,13 @@ mk22fn512_src = ['platform/mcu/MK22FN512xxx12/boot/startup.cpp', 'platform/mcu/MK22FN512xxx12/drivers/gpio.c', 'platform/mcu/MK22FN512xxx12/drivers/delays.cpp', 'platform/mcu/MK22FN512xxx12/drivers/I2C0.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_cdc_acm.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_ch9.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_dci.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_descriptor.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_khci.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_osa_bm.c', -# 'platform/mcu/MK22FN512xxx12/drivers/usb_vcom.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_cdc_acm.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_ch9.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_dci.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_descriptor.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_device_khci.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb/usb_osa_bm.c', + 'platform/mcu/MK22FN512xxx12/drivers/usb_vcom.c', 'platform/mcu/CMSIS/Device/NXP/MK22FN512xxx12/Source/system_MK22F51212.c'] mk22fn512_inc = ['platform/mcu/CMSIS/Include', @@ -233,7 +233,7 @@ dm1801_def = def + mk22fn512_def + {'PLATFORM_DM1801': ''} ## Compilation defines ## linux_c_args = [] -linux_l_args = ['--entry=entry', '-lm'] +linux_l_args = ['-lm'] # Add AddressSanitizer if required if get_option('asan') diff --git a/platform/mcu/MK22FN512xxx12/boot/libc_integration.cpp b/platform/mcu/MK22FN512xxx12/boot/libc_integration.cpp index dc9ab69f..e02109bc 100644 --- a/platform/mcu/MK22FN512xxx12/boot/libc_integration.cpp +++ b/platform/mcu/MK22FN512xxx12/boot/libc_integration.cpp @@ -17,7 +17,8 @@ #include #include -#include "filesystem/file_access.h" +#include +#include using namespace std; @@ -31,11 +32,10 @@ extern "C" { */ int _write_r(struct _reent *ptr, int fd, const void *buf, size_t cnt) { -// if(fd == STDOUT_FILENO || fd == STDERR_FILENO) -// { -// vcom_writeBlock(buf, cnt); -// return cnt; -// } + if(fd == STDOUT_FILENO || fd == STDERR_FILENO) + { + return vcom_writeBlock(buf, cnt); + } /* If fd is not stdout or stderr */ ptr->_errno = EBADF; @@ -48,19 +48,28 @@ int _write_r(struct _reent *ptr, int fd, const void *buf, size_t cnt) */ int _read_r(struct _reent *ptr, int fd, void *buf, size_t cnt) { -// if(fd == STDIN_FILENO) -// { -// for(;;) -// { -// ssize_t r = vcom_readBlock(buf, cnt); -// if((r < 0) || (r == (ssize_t)(cnt))) return r; -// } -// } -// else + int ret = -1; - /* If fd is not stdin */ - ptr->_errno = EBADF; - return -1; + if(fd == STDIN_FILENO) + { + for(;;) + { + ssize_t r = vcom_readBlock(buf, cnt); + if((r < 0) || (r == ((ssize_t) cnt))) + { + ret = ((int) r); + break; + } + } + } + else + { + /* If fd is not stdin */ + ptr->_errno = EBADF; + ret = -1; + } + + return ret; } #ifdef __cplusplus diff --git a/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.c b/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.c index 16276eb0..12411615 100644 --- a/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.c +++ b/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.c @@ -619,7 +619,7 @@ void vcom_init() return; } - NVIC_SetPriority(USB0_IRQn, 3); + NVIC_SetPriority(USB0_IRQn, 14); NVIC_EnableIRQ(USB0_IRQn); USB_DeviceRun(cdcVcom.deviceHandle); diff --git a/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.h b/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.h index 1edf7c17..fbd6ecd3 100644 --- a/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.h +++ b/platform/mcu/MK22FN512xxx12/drivers/usb_vcom.h @@ -21,6 +21,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + /** * Initialise USB virtual com port. Parameters: 115200 baud, 8N1. * @return zero on success, negative value on failure. @@ -45,4 +49,8 @@ ssize_t vcom_writeBlock(const void *buf, size_t len); */ ssize_t vcom_readBlock(void *buf, size_t len); +#ifdef __cplusplus +} +#endif + #endif /* USB_VCOM_H */ diff --git a/platform/targets/linux/emulator/emulator.c b/platform/targets/linux/emulator/emulator.c index e31a1dd3..dc5d8811 100644 --- a/platform/targets/linux/emulator/emulator.c +++ b/platform/targets/linux/emulator/emulator.c @@ -27,9 +27,8 @@ radio_state Radio_State = {12, 8.2f, 3, 4, 1, false}; -void systemBootstrap(); - -int CLIMenu() { +int CLIMenu() +{ int choice = 0; printf("Select the value to change:\n"); printf("1 -> RSSI\n"); @@ -41,20 +40,23 @@ int CLIMenu() { printf("7 -> Print current state\n"); printf("8 -> Exit\n"); printf("> "); - do { + do + { scanf("%d", &choice); } while (choice < 1 || choice > 8); printf("\033[1;1H\033[2J"); return choice; } -void updateValue(float *curr_value) { +void updateValue(float *curr_value) +{ printf("Current value: %f\n", *curr_value); printf("New value: \n"); scanf("%f", curr_value); } -void printState() { +void printState() +{ printf("\nCurrent state\n"); printf("RSSI : %f\n", Radio_State.RSSI); printf("Battery: %f\n", Radio_State.Vbat); @@ -65,16 +67,14 @@ void printState() { } -void *startRadio() { - systemBootstrap(); - return NULL; -} - -void *startCLIMenu() { +void *startCLIMenu() +{ int choice; - do { + do + { choice = CLIMenu(); - switch (choice) { + switch (choice) + { case VAL_RSSI: updateValue(&Radio_State.RSSI); break; @@ -100,48 +100,18 @@ void *startCLIMenu() { continue; } } while (choice != EXIT); - printf("exiting\n"); + printf("73\n"); exit(0); } -int emulator_main() { - pthread_t cli_thread, radio_thread; - int err1, err2 = 0; +void emulator_start() +{ + pthread_t cli_thread; + int err = pthread_create(&cli_thread, NULL, startCLIMenu, NULL); - err1 = pthread_create(&cli_thread, NULL, startCLIMenu, NULL); - err2 = pthread_create(&radio_thread, NULL, startRadio, NULL); - - if (err1 | err2) { - printf("An error occurred starting the threads: %d, %d\n", err1, err2); - return 1; + if(err) + { + printf("An error occurred starting the emulator thread: %d\n", err); } - pthread_join(cli_thread, NULL); - - printf("73\n"); - return 0; } - - -void entry() { - __asm__( - "xorl %ebp, %ebp\n\t" - "mov %RDX, %R9\n\t" - - #ifdef __ILP32__ - "mov (%rsp), %esi\n\t" - "add $4, %esp\n\t" - #else - "popq %rsi\n\t" - #endif - "mov %RSP, %RDX\n\t" - "and $~15, %RSP\n\t" - "pushq %rax\n\t" - "pushq %rsp\n\t" - "mov __libc_csu_fini@GOTPCREL(%rip), %R8\n\t" - "mov __libc_csu_init@GOTPCREL(%rip), %RCX\n\t" - "mov emulator_main@GOTPCREL(%rip), %RDI\n\t" - "call *__libc_start_main@GOTPCREL(%rip)\n\t" - "hlt\n\t"); -} - diff --git a/platform/targets/linux/emulator/emulator.h b/platform/targets/linux/emulator/emulator.h index 04c1422a..f2c209c8 100644 --- a/platform/targets/linux/emulator/emulator.h +++ b/platform/targets/linux/emulator/emulator.h @@ -32,7 +32,8 @@ #define SCREEN_HEIGHT 128 #endif -enum choices { +enum choices +{ VAL_RSSI=1, VAL_BAT, VAL_MIC, @@ -43,7 +44,8 @@ enum choices { EXIT }; -typedef struct { +typedef struct +{ float RSSI; float Vbat; float micLevel; @@ -54,4 +56,6 @@ typedef struct { extern radio_state Radio_State; +void emulator_start(); + #endif /* EMULATOR_H */ diff --git a/platform/targets/linux/platform.c b/platform/targets/linux/platform.c index 31d926c6..ff5fc90b 100644 --- a/platform/targets/linux/platform.c +++ b/platform/targets/linux/platform.c @@ -29,6 +29,8 @@ void platform_init() snprintf(hwInfo.name, 10, "Linux"); hwInfo.vhf_band = 1; hwInfo.uhf_band = 1; + + emulator_start(); } void platform_terminate()