Skip to content

Commit

Permalink
Move CLRStartup to its own OS thread (#425)
Browse files Browse the repository at this point in the history
- this makes it easy to isolate it and adjust stack size, for example
- fixes #416

Signed-off-by: José Simões <[email protected]>
  • Loading branch information
josesimoes authored Aug 11, 2017
1 parent d56a38e commit b5ad6a0
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 84 deletions.
12 changes: 12 additions & 0 deletions targets/CMSIS-OS/ChibiOS/Include/CLR_Startup_Thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

#ifndef _CLRSTARTUPTHREAD_
#define _CLRSTARTUPTHREAD_

// declaration of RTOS thread
void CLRStartupThread(void const * argument);

#endif //_CLRSTARTUPTHREAD_
19 changes: 5 additions & 14 deletions targets/CMSIS-OS/ChibiOS/MBN_QUAIL/nanoCLR/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cmsis_os.h>

#include "usbcfg.h"
#include <CLR_Startup_Thread.h>
#include <WireProtocol_ReceiverThread.h>
#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>
Expand Down Expand Up @@ -41,6 +42,8 @@ void DisplayWrite(uint8_t x, uint8_t y, char * text)

// need to declare the Receiver thread here
osThreadDef(ReceiverThread, osPriorityNormal, 2048, "ReceiverThread");
// declare CLRStartup thread here
osThreadDef(CLRStartupThread, osPriorityNormal, 2048, "CLRStartupThread");

static const I2CConfig i2cconfig = { OPMODE_I2C, 100000U, STD_DUTY_CYCLE };

Expand Down Expand Up @@ -68,6 +71,8 @@ int main(void) {

// create the receiver thread
osThreadCreate(osThread(ReceiverThread), NULL);
// create the CLR Startup thread
osThreadCreate(osThread(CLRStartupThread), NULL);

// start kernel, after this main() will behave like a thread with priority osPriorityNormal
osKernelStart();
Expand All @@ -83,20 +88,6 @@ int main(void) {
osDelay(3000);
DisplayClear (0);

// preparation for the CLR startup
BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

// startup CLR now
ClrStartup(clrSettings);

while (true) {
osDelay(100);
}
Expand Down
19 changes: 5 additions & 14 deletions targets/CMSIS-OS/ChibiOS/ST_NUCLEO144_F746ZG/nanoCLR/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <cmsis_os.h>

#include "usbcfg.h"
#include <CLR_Startup_Thread.h>
#include <WireProtocol_ReceiverThread.h>
#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>

// need to declare the Receiver thread here
osThreadDef(ReceiverThread, osPriorityNormal, 2048, "ReceiverThread");
// declare CLRStartup thread here
osThreadDef(CLRStartupThread, osPriorityNormal, 2048, "CLRStartupThread");

// Application entry point.
int main(void) {
Expand All @@ -39,24 +42,12 @@ int main(void) {

// create the receiver thread
osThreadCreate(osThread(ReceiverThread), NULL);
// create the CLR Startup thread
osThreadCreate(osThread(CLRStartupThread), NULL);

// start kernel, after this main() will behave like a thread with priority osPriorityNormal
osKernelStart();

// preparation for the CLR startup
BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

// startup CLR now
ClrStartup(clrSettings);

while (true) {
osDelay(100);
}
Expand Down
19 changes: 5 additions & 14 deletions targets/CMSIS-OS/ChibiOS/ST_NUCLEO64_F091RC/nanoCLR/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cmsis_os.h>

#include <targetHAL.h>
#include <CLR_Startup_Thread.h>
#include <WireProtocol_ReceiverThread.h>
#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>
Expand All @@ -22,6 +23,8 @@ __IO uint32_t vectorTable[48] __attribute__((section(".RAMVectorTable")));

// need to declare the Receiver thread here
osThreadDef(ReceiverThread, osPriorityNormal, 2048, "ReceiverThread");
// declare CLRStartup thread here
osThreadDef(CLRStartupThread, osPriorityNormal, 2048, "CLRStartupThread");

// Application entry point.
int main(void) {
Expand Down Expand Up @@ -52,24 +55,12 @@ int main(void) {

// create the receiver thread
osThreadCreate(osThread(ReceiverThread), NULL);
// create the CLR Startup thread
osThreadCreate(osThread(CLRStartupThread), NULL);

// start kernel, after this main() will behave like a thread with priority osPriorityNormal
osKernelStart();

// preparation for the CLR startup
BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

// startup CLR now
ClrStartup(clrSettings);

while (true) {
osDelay(100);
}
Expand Down
19 changes: 5 additions & 14 deletions targets/CMSIS-OS/ChibiOS/ST_STM32F429I_DISCOVERY/nanoCLR/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <cmsis_os.h>

#include "usbcfg.h"
#include <CLR_Startup_Thread.h>
#include <WireProtocol_ReceiverThread.h>
#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>

// need to declare the Receiver thread here
osThreadDef(ReceiverThread, osPriorityNormal, 2048, "ReceiverThread");
// declare CLRStartup thread here
osThreadDef(CLRStartupThread, osPriorityNormal, 2048, "CLRStartupThread");

// Application entry point.
int main(void) {
Expand All @@ -39,24 +42,12 @@ int main(void) {

// create the receiver thread
osThreadCreate(osThread(ReceiverThread), NULL);
// create the CLR Startup thread
osThreadCreate(osThread(CLRStartupThread), NULL);

// start kernel, after this main() will behave like a thread with priority osPriorityNormal
osKernelStart();

// preparation for the CLR startup
BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

// startup CLR now
ClrStartup(clrSettings);

while (true) {
osDelay(100);
}
Expand Down
19 changes: 5 additions & 14 deletions targets/CMSIS-OS/ChibiOS/ST_STM32F4_DISCOVERY/nanoCLR/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <cmsis_os.h>

#include "usbcfg.h"
#include <CLR_Startup_Thread.h>
#include <WireProtocol_ReceiverThread.h>
#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>

// need to declare the Receiver thread here
osThreadDef(ReceiverThread, osPriorityNormal, 2048, "ReceiverThread");
// declare CLRStartup thread here
osThreadDef(CLRStartupThread, osPriorityNormal, 2048, "CLRStartupThread");

// Application entry point.
int main(void) {
Expand All @@ -39,24 +42,12 @@ int main(void) {

// create the receiver thread
osThreadCreate(osThread(ReceiverThread), NULL);
// create the CLR Startup thread
osThreadCreate(osThread(CLRStartupThread), NULL);

// start kernel, after this main() will behave like a thread with priority osPriorityNormal
osKernelStart();

// preparation for the CLR startup
BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

// startup CLR now
ClrStartup(clrSettings);

while (true) {
osDelay(100);
}
Expand Down
19 changes: 5 additions & 14 deletions targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/nanoCLR/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <cmsis_os.h>

#include "usbcfg.h"
#include <CLR_Startup_Thread.h>
#include <WireProtocol_ReceiverThread.h>
#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>

// need to declare the Receiver thread here
osThreadDef(ReceiverThread, osPriorityNormal, 2048, "ReceiverThread");
// declare CLRStartup thread here
osThreadDef(CLRStartupThread, osPriorityNormal, 2048, "CLRStartupThread");

// Application entry point.
int main(void) {
Expand All @@ -39,24 +42,12 @@ int main(void) {

// create the receiver thread
osThreadCreate(osThread(ReceiverThread), NULL);
// create the CLR Startup thread
osThreadCreate(osThread(CLRStartupThread), NULL);

// start kernel, after this main() will behave like a thread with priority osPriorityNormal
osKernelStart();

// preparation for the CLR startup
BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

// startup CLR now
ClrStartup(clrSettings);

while (true) {
osDelay(100);
}
Expand Down
37 changes: 37 additions & 0 deletions targets/CMSIS-OS/ChibiOS/nanoCLR/CLR_Startup_Thread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

#include <ch.h>
#include <cmsis_os.h>

#include <nanoCLR_Application.h>
#include <nanoPAL_BlockStorage.h>

// This thread needs to be implemented at ChibiOS level because it has to include a call to chThdShouldTerminateX()
// in case the thread is requested to terminate by the CMSIS call osThreadTerminate()

void CLRStartupThread(void const * argument)
{
(void)argument;

BlockStorage_AddDevices();

CLR_SETTINGS clrSettings;

memset(&clrSettings, 0, sizeof(CLR_SETTINGS));

clrSettings.MaxContextSwitches = 50;
clrSettings.WaitForDebugger = false;
clrSettings.EnterDebuggerLoopAfterExit = true;

ClrStartup(clrSettings);

// loop until thread receives a request to terminate
while (!chThdShouldTerminateX()) {
osDelay(500);
}

// nothing to deinitialize or cleanup, so it's safe to return
}
1 change: 1 addition & 0 deletions targets/CMSIS-OS/ChibiOS/nanoCLR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/target_platform.h.in"
list(APPEND TARGET_CHIBIOS_NANOCLR_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/WireProtocol_App_Interface.c)
list(APPEND TARGET_CHIBIOS_NANOCLR_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/WireProtocol_MonitorCommands.c)
list(APPEND TARGET_CHIBIOS_NANOCLR_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Aborts_CortexM3.cpp)
list(APPEND TARGET_CHIBIOS_NANOCLR_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/CLR_Startup_Thread.c")

# append target HAL source files
list(APPEND TARGET_CHIBIOS_NANOCLR_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL_Time.cpp")
Expand Down

0 comments on commit b5ad6a0

Please sign in to comment.