Skip to content

Commit

Permalink
psu and scpi threads refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed May 4, 2020
1 parent 1a86e77 commit 10e9e2f
Show file tree
Hide file tree
Showing 33 changed files with 697 additions and 664 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ set(src_eez
src/eez/mqtt.cpp
src/eez/sound.cpp
src/eez/system.cpp
src/eez/tasks.cpp
src/eez/unit.cpp
src/eez/util.cpp
)
Expand All @@ -112,6 +113,7 @@ set(header_eez
src/eez/mqtt.h
src/eez/sound.h
src/eez/system.h
src/eez/tasks.h
src/eez/unit.h
src/eez/util.h
src/eez/value_types.h
Expand Down
5 changes: 2 additions & 3 deletions src/eez/action_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

#include <eez/firmware.h>
#include <eez/system.h>
#include <eez/tasks.h>
#include <eez/sound.h>
#include <eez/index.h>
#include <eez/mqtt.h>

#include <eez/scpi/scpi.h>

#include <eez/modules/psu/psu.h>
#include <eez/modules/psu/channel_dispatcher.h>
#include <eez/modules/psu/event_queue.h>
Expand Down Expand Up @@ -1126,7 +1125,7 @@ void action_user_switch_clicked() {
using namespace scpi;
if (!g_screenshotGenerating) {
g_screenshotGenerating = true;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_MESSAGE(SCPI_QUEUE_MESSAGE_TARGET_NONE, SCPI_QUEUE_MESSAGE_SCREENSHOT, 0), osWaitForever);
sendMessageToLowPriorityThread(THREAD_MESSAGE_SCREENSHOT);
}
break;

Expand Down
26 changes: 13 additions & 13 deletions src/eez/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@

#include <eez/firmware.h>
#include <eez/system.h>
#include <eez/tasks.h>
#include <eez/mp.h>
#include <eez/sound.h>
#include <eez/memory.h>

#include <eez/scpi/scpi.h>

#include <eez/gui/gui.h>

#include <eez/modules/psu/psu.h>
Expand Down Expand Up @@ -107,11 +106,12 @@ void boot() {
#if OPTION_ETHERNET
mcu::ethernet::initMessageQueue();
#endif
scpi::initMessageQueue();
initLowPriorityMessageQueue();

psu::startThread();
startHighPriorityThread();

// INIT
psu::init();

psu::rtc::init();
psu::datetime::init();
Expand Down Expand Up @@ -213,7 +213,7 @@ void boot() {
#if OPTION_ETHERNET
mcu::ethernet::startThread();
#endif
scpi::startThread();
startLowPriorityThread();

mp::initMessageQueue();
mp::startThread();
Expand Down Expand Up @@ -275,10 +275,10 @@ bool test() {
static bool g_testResult;
static bool g_testFinished;

if (osThreadGetId() != g_psuTaskHandle) {
if (!isPsuThread()) {
g_testFinished = false;

osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_TEST, 0), osWaitForever);
sendMessageToPsu(PSU_MESSAGE_TEST);

while (!g_testFinished) {
osDelay(10);
Expand All @@ -295,8 +295,8 @@ bool test() {
bool reset() {
using namespace psu;

if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_RESET, 0), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_RESET);
return true;
}

Expand All @@ -317,9 +317,9 @@ void restart() {
void shutdown() {
using namespace psu;

if (osThreadGetId() != g_psuTaskHandle) {
if (!isPsuThread()) {
psu::gui::showSavingPage();
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_SHUTDOWN, 0), osWaitForever);
sendMessageToPsu(PSU_MESSAGE_SHUTDOWN);
return;
}

Expand All @@ -332,10 +332,10 @@ void shutdown() {
#if !defined(__EMSCRIPTEN__)
// shutdown SCPI thread
using namespace eez::scpi;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_MESSAGE(SCPI_QUEUE_MESSAGE_TARGET_NONE, SCPI_QUEUE_MESSAGE_TYPE_SHUTDOWN, 0), osWaitForever);
sendMessageToLowPriorityThread(THREAD_MESSAGE_SHUTDOWN);
do {
osDelay(10);
} while (isThreadAlive());
} while (isLowPriorityThreadAlive());
#endif

profile::shutdownSave();
Expand Down
11 changes: 5 additions & 6 deletions src/eez/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
#endif

#include <eez/firmware.h>

#include <eez/scpi/scpi.h>
#include <eez/tasks.h>

#include <eez/modules/mcu/encoder.h>

Expand Down Expand Up @@ -204,16 +203,16 @@ extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
}

if (slotIndex != -1) {
using namespace eez::psu;
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_SPI_IRQ, slotIndex), 0);
using namespace eez;
sendMessageToPsu(PSU_MESSAGE_SPI_IRQ, slotIndex, 0);
}
}
#endif

#if defined(EEZ_PLATFORM_SIMULATOR) && !defined(__EMSCRIPTEN__)
void consoleInputTask(const void *) {
using namespace eez::scpi;
osMessagePut(eez::scpi::g_scpiMessageQueueId, SCPI_QUEUE_SERIAL_MESSAGE(SERIAL_LINE_STATE_CHANGED, 1), osWaitForever);
using namespace eez;
sendMessageToLowPriorityThread(SERIAL_LINE_STATE_CHANGED, 1);

while (1) {
int ch = getchar();
Expand Down
4 changes: 1 addition & 3 deletions src/eez/modules/bp3c/flash_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

#include <eez/libs/sd_fat/sd_fat.h>

#include <eez/scpi/scpi.h>

#ifdef EEZ_PLATFORM_STM32

#include <memory.h>
Expand Down Expand Up @@ -470,7 +468,7 @@ bool start(int slotIndex, const char *hexFilePath, int *err) {
g_slotIndex = slotIndex;
strcpy(g_hexFilePath, hexFilePath);

osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_MESSAGE(SCPI_QUEUE_MESSAGE_TARGET_NONE, SCPI_QUEUE_MESSAGE_FLASH_SLAVE_UPLOAD_HEX_FILE, 0), osWaitForever);
sendMessageToLowPriorityThread(THREAD_MESSAGE_FLASH_SLAVE_UPLOAD_HEX_FILE);

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/eez/modules/mcu/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#pragma once

#include <stdint.h>

namespace eez {
namespace mcu {
namespace encoder {
Expand Down
23 changes: 11 additions & 12 deletions src/eez/modules/mcu/ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ extern ip4_addr_t gw;

#include <eez/firmware.h>
#include <eez/system.h>
#include <eez/scpi/scpi.h>
#include <eez/modules/mcu/ethernet.h>
#include <eez/modules/psu/psu.h>
#include <eez/modules/psu/ethernet.h>
Expand Down Expand Up @@ -140,7 +139,7 @@ static void netconnCallback(struct netconn *conn, enum netconn_evt evt, u16_t le
if (conn == g_tcpListenConnection) {
osMessagePut(g_ethernetMessageQueueId, QUEUE_MESSAGE_ACCEPT_CLIENT, osWaitForever);
} else if (conn == g_tcpClientConnection) {
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_INPUT_AVAILABLE, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_INPUT_AVAILABLE);
}
break;

Expand Down Expand Up @@ -172,15 +171,15 @@ static void dhcpStart() {
while(!dhcp_supplied_address(&gnetif)) {
if ((millis() - connectStart) >= CONF_CONNECT_TIMEOUT) {
g_connectionState = CONNECTION_STATE_CONNECT_ERROR;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CONNECTED);
return;
}
osDelay(10);
}
}

g_connectionState = CONNECTION_STATE_CONNECTED;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CONNECTED, 1), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CONNECTED, 1);
return;
}

Expand Down Expand Up @@ -221,7 +220,7 @@ static void onEvent(uint8_t eventType) {
// When the netif link is down this function must be called
netif_set_down(&gnetif);
g_connectionState = CONNECTION_STATE_CONNECT_ERROR;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CONNECTED);
}

}
Expand Down Expand Up @@ -261,7 +260,7 @@ static void onEvent(uint8_t eventType) {
} else {
// connection with the client established
g_tcpClientConnection = newConnection;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CLIENT_CONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CLIENT_CONNECTED);
}
}
}
Expand All @@ -288,7 +287,7 @@ void onIdle() {
/* network cable is dis-connected */
netif_set_link_down(&gnetif);
g_connectionState = CONNECTION_STATE_CONNECT_ERROR;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CONNECTED);
}
}
}
Expand Down Expand Up @@ -628,7 +627,7 @@ void stop() {
void onEvent(uint8_t eventType) {
switch (eventType) {
case QUEUE_MESSAGE_CONNECT:
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CONNECTED, 1), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CONNECTED, 1);
break;

case QUEUE_MESSAGE_CREATE_TCP_SERVER:
Expand Down Expand Up @@ -656,16 +655,16 @@ void onIdle() {
if (connected()) {
if (!g_inputBufferLength && available()) {
g_inputBufferLength = read(g_inputBuffer, INPUT_BUFFER_SIZE);
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_INPUT_AVAILABLE, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_INPUT_AVAILABLE);
}
} else {
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CLIENT_DISCONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CLIENT_DISCONNECTED);
wasConnected = false;
}
} else {
if (client_available()) {
wasConnected = true;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CLIENT_CONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CLIENT_CONNECTED);
}
}
}
Expand Down Expand Up @@ -787,7 +786,7 @@ void getInputBuffer(int bufferPosition, char **buffer, uint32_t *length) {
netconn_close(g_tcpClientConnection);
netconn_delete(g_tcpClientConnection);
g_tcpClientConnection = nullptr;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_ETHERNET_MESSAGE(ETHERNET_CLIENT_DISCONNECTED, 0), osWaitForever);
sendMessageToLowPriorityThread(ETHERNET_CLIENT_DISCONNECTED);

*buffer = nullptr;
length = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ Channel &getCalibrationChannel() {
}

void start(Channel &channel) {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_CALIBRATION_START, channel.channelIndex), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_CALIBRATION_START, channel.channelIndex);
return;
}

Expand Down Expand Up @@ -238,8 +238,8 @@ void start(Channel &channel) {
}

void stop() {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_CALIBRATION_STOP, 0), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_CALIBRATION_STOP);
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ void Channel::enumChannels() {
static uint16_t g_oeSavedState;

void Channel::saveAndDisableOE() {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_TRIGGER_CHANNEL_SAVE_AND_DISABLE_OE, 0), 0);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_TRIGGER_CHANNEL_SAVE_AND_DISABLE_OE, 0, 0);
} else {
if (!g_oeSavedState) {
for (int i = 0; i < CH_NUM; i++) {
Expand All @@ -211,8 +211,8 @@ void Channel::saveAndDisableOE() {
}

void Channel::restoreOE() {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_TRIGGER_CHANNEL_RESTORE_OE, 0), 0);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_TRIGGER_CHANNEL_RESTORE_OE, 0, 0);
} else {
if (g_oeSavedState) {
for (int i = 0; i < CH_NUM; i++) {
Expand Down
24 changes: 12 additions & 12 deletions src/eez/modules/psu/channel_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ bool setCouplingType(CouplingType couplingType, int *err) {
return false;
}

if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_SET_COUPLING_TYPE, couplingType), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_SET_COUPLING_TYPE, couplingType);
} else {
setCouplingTypeInPsuThread(couplingType);
}
Expand Down Expand Up @@ -245,8 +245,8 @@ void setCouplingTypeInPsuThread(CouplingType couplingType) {
}

void setTrackingChannels(uint16_t trackingEnabled) {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_SET_TRACKING_CHANNELS, trackingEnabled), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_SET_TRACKING_CHANNELS, trackingEnabled);
} else {
bool resetTrackingChannels = false;
for (int i = 0; i < CH_NUM; i++) {
Expand Down Expand Up @@ -554,9 +554,9 @@ void setVoltageInPsuThread(int channelIndex) {
}

void setVoltage(Channel &channel, float voltage) {
if (osThreadGetId() != g_psuTaskHandle) {
if (!isPsuThread()) {
g_setVoltageValues[channel.channelIndex] = voltage;
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_SET_VOLTAGE, channel.channelIndex), osWaitForever);
sendMessageToPsu(PSU_MESSAGE_SET_VOLTAGE, channel.channelIndex);
return;
}

Expand Down Expand Up @@ -855,9 +855,9 @@ void setCurrentInPsuThread(int channelIndex) {
}

void setCurrent(Channel &channel, float current) {
if (osThreadGetId() != g_psuTaskHandle) {
if (!isPsuThread()) {
g_setCurrentValues[channel.channelIndex] = current;
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_MESSAGE_TYPE_SET_CURRENT, channel.channelIndex), osWaitForever);
sendMessageToPsu(PSU_MESSAGE_SET_CURRENT, channel.channelIndex);
return;
}

Expand Down Expand Up @@ -1217,8 +1217,8 @@ void outputEnableOnNextSync(Channel &channel, bool enable) {
}

void syncOutputEnable() {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_SYNC_OUTPUT_ENABLE, 0), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_SYNC_OUTPUT_ENABLE);
} else {
Channel::syncOutputEnable();
}
Expand Down Expand Up @@ -1573,8 +1573,8 @@ void setDisplayViewSettings(Channel &channel, int displayValue1, int displayValu
}

if (resetHistory) {
if (osThreadGetId() != g_psuTaskHandle) {
osMessagePut(g_psuMessageQueueId, PSU_QUEUE_MESSAGE(PSU_QUEUE_RESET_CHANNELS_HISTORY, 0), osWaitForever);
if (!isPsuThread()) {
sendMessageToPsu(PSU_MESSAGE_RESET_CHANNELS_HISTORY);
} else {
Channel::resetHistoryForAllChannels();
}
Expand Down
Loading

0 comments on commit 10e9e2f

Please sign in to comment.