From 14aff7863e49ce2ff77972c40032f52316c5c45a Mon Sep 17 00:00:00 2001 From: Martin Vladic Date: Mon, 14 Jun 2021 18:38:24 +0200 Subject: [PATCH] @19 --- src/eez/action_impl.cpp | 4 + src/eez/firmware.cpp | 7 +- src/eez/modules/psu/gui/data.cpp | 61 +++++++++++-- src/eez/modules/psu/gui/data.h | 6 +- src/eez/modules/psu/gui/page_sys_settings.cpp | 91 ++++++++++++++++++- src/eez/modules/psu/gui/page_sys_settings.h | 24 ++++- src/eez/modules/psu/io_pins.cpp | 4 + src/eez/modules/psu/io_pins.h | 20 ++++ src/eez/modules/psu/profile.cpp | 22 +++++ src/eez/modules/psu/profile.h | 4 + src/eez/modules/psu/scpi/syst.cpp | 90 ++++++++++++++++++ src/eez/scpi/commands_stm32.h | 8 ++ src/eez/uart.cpp | 35 ++++++- .../stm32_cubeide/Core/Src/usart.c | 20 ++-- 14 files changed, 371 insertions(+), 25 deletions(-) diff --git a/src/eez/action_impl.cpp b/src/eez/action_impl.cpp index 133df49ea..550651602 100644 --- a/src/eez/action_impl.cpp +++ b/src/eez/action_impl.cpp @@ -953,15 +953,19 @@ void action_io_pin_select_uart_mode() { } void action_io_pin_select_uart_baud() { + ((SysSettingsIOPinsPage *)getActivePage())->selectUartBaudRate(); } void action_io_pin_select_uart_data_bits() { + ((SysSettingsIOPinsPage *)getActivePage())->selectUartDataBits(); } void action_io_pin_select_uart_parity() { + ((SysSettingsIOPinsPage *)getActivePage())->selectUartParity(); } void action_io_pin_select_uart_stop_bits() { + ((SysSettingsIOPinsPage *)getActivePage())->selectUartStopBits(); } void action_ntp_toggle() { diff --git a/src/eez/firmware.cpp b/src/eez/firmware.cpp index f2c74c261..1aa50834c 100644 --- a/src/eez/firmware.cpp +++ b/src/eez/firmware.cpp @@ -627,9 +627,10 @@ void updateSpiIrqPin(int slotIndex) { } } - /* EXTI interrupt init*/ - HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); - HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); +#if defined(EEZ_PLATFORM_STM32) + HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); + HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); +#endif } } // namespace eez diff --git a/src/eez/modules/psu/gui/data.cpp b/src/eez/modules/psu/gui/data.cpp index 31d1bd8ae..c34cf9fc2 100644 --- a/src/eez/modules/psu/gui/data.cpp +++ b/src/eez/modules/psu/gui/data.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -313,6 +314,36 @@ EnumItem g_enumDefinition_UART_MODE[] = { { 0, 0 } }; +EnumItem g_enumDefinition_UART_BAUD_RATE[] = { + { 0, "9600" }, + { 1, "14400" }, + { 2, "19200" }, + { 3, "38400" }, + { 4, "57600" }, + { 5, "115200" }, + { 0, 0 } +}; + +EnumItem g_enumDefinition_UART_DATA_BITS[] = { + { 6, "6" }, + { 7, "7" }, + { 8, "8" }, + { 0, 0 } +}; + +EnumItem g_enumDefinition_UART_STOP_BITS[] = { + { 1, "1" }, + { 2, "2" }, + { 0, 0 } +}; + +EnumItem g_enumDefinition_UART_PARITY[] = { + { 0, "None" }, + { 1, "Even" }, + { 2, "Odd" }, + { 0, 0 } +}; + //////////////////////////////////////////////////////////////////////////////// Value MakeValue(float value, Unit unit) { @@ -4643,7 +4674,7 @@ void data_io_pin_uart_baud(DataOperationEnum operation, Cursor cursor, Value &va if (operation == DATA_OPERATION_GET) { SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getPage(PAGE_ID_SYS_SETTINGS_IO); if (page) { - value = "115200"; + value = (int)page->m_uartBaudRate; } } } @@ -4652,7 +4683,7 @@ void data_io_pin_uart_data_bits(DataOperationEnum operation, Cursor cursor, Valu if (operation == DATA_OPERATION_GET) { SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getPage(PAGE_ID_SYS_SETTINGS_IO); if (page) { - value = "8"; + value = (int)page->m_uartDataBits; } } } @@ -4661,7 +4692,7 @@ void data_io_pin_uart_parity(DataOperationEnum operation, Cursor cursor, Value & if (operation == DATA_OPERATION_GET) { SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getPage(PAGE_ID_SYS_SETTINGS_IO); if (page) { - value = "Event"; + value = page->m_uartParity == 0 ? "None" : page->m_uartParity == 1 ? "Even" : "Odd"; } } } @@ -4670,7 +4701,7 @@ void data_io_pin_uart_stop_bits(DataOperationEnum operation, Cursor cursor, Valu if (operation == DATA_OPERATION_GET) { SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getPage(PAGE_ID_SYS_SETTINGS_IO); if (page) { - value = "1"; + value = (int)page->m_uartStopBits; } } } @@ -4833,6 +4864,7 @@ void data_overlay(DataOperationEnum operation, Cursor cursor, Value &value) { RAMP_GRID_WIDGET, DLOG_INFO_WIDGET, SCRIPT_INFO_WIDGET, + FUNCTION_GENERATOR_WIDGET, NUM_WIDGETS }; @@ -4848,9 +4880,10 @@ void data_overlay(DataOperationEnum operation, Cursor cursor, Value &value) { bool areRampCountersVisible = ramp::g_numChannelsWithVisibleCounters > 0; bool isDlogVisible = !dlog_record::isIdle(); bool isScriptVisible = !mp::isIdle(); + bool isFunctionGeneratorVisible = function_generator::isActive(); int state = 0; - if (areListCountersVisible || areRampCountersVisible || isDlogVisible || isScriptVisible) { + if (areListCountersVisible || areRampCountersVisible || isDlogVisible || isScriptVisible || isFunctionGeneratorVisible) { state = 0; if (list::g_numChannelsWithVisibleCounters > 0) { @@ -4884,6 +4917,10 @@ void data_overlay(DataOperationEnum operation, Cursor cursor, Value &value) { if (isScriptVisible) { state |= 0x4000; } + + if (isFunctionGeneratorVisible) { + state |= 0x10000; + } } if (overlay.state != state) { @@ -4899,9 +4936,10 @@ void data_overlay(DataOperationEnum operation, Cursor cursor, Value &value) { const Widget *rampGridWidget = GET_WIDGET_LIST_ELEMENT(containerWidget->widgets, RAMP_GRID_WIDGET); const Widget *dlogInfoWidget = GET_WIDGET_LIST_ELEMENT(containerWidget->widgets, DLOG_INFO_WIDGET); const Widget *scriptInfoWidget = GET_WIDGET_LIST_ELEMENT(containerWidget->widgets, SCRIPT_INFO_WIDGET); + const Widget *functionGeneratorWidget = GET_WIDGET_LIST_ELEMENT(containerWidget->widgets, FUNCTION_GENERATOR_WIDGET); overlay.width = widgetCursor.widget->w; - if (list::g_numChannelsWithVisibleCounters <= 1 && ramp::g_numChannelsWithVisibleCounters <= 1 && !isDlogVisible && !isScriptVisible) { + if (list::g_numChannelsWithVisibleCounters <= 1 && ramp::g_numChannelsWithVisibleCounters <= 1 && !isDlogVisible && !isScriptVisible && !isFunctionGeneratorVisible) { overlay.width -= listGridWidget->w / 2; } @@ -4989,6 +5027,17 @@ void data_overlay(DataOperationEnum operation, Cursor cursor, Value &value) { overlay.height -= scriptInfoWidget->h; } + if (isFunctionGeneratorVisible) { + widgetOverrides[FUNCTION_GENERATOR_WIDGET].isVisible = true; + widgetOverrides[FUNCTION_GENERATOR_WIDGET].x = functionGeneratorWidget->x; + widgetOverrides[FUNCTION_GENERATOR_WIDGET].y = overlay.height - (widgetCursor.widget->h - functionGeneratorWidget->y); + widgetOverrides[FUNCTION_GENERATOR_WIDGET].w = functionGeneratorWidget->w; + widgetOverrides[FUNCTION_GENERATOR_WIDGET].h = functionGeneratorWidget->h; + } else { + widgetOverrides[FUNCTION_GENERATOR_WIDGET].isVisible = false; + overlay.height -= functionGeneratorWidget->h; + } + overlay.x = 480 - overlay.width; overlay.y = 240 - overlay.height; } diff --git a/src/eez/modules/psu/gui/data.h b/src/eez/modules/psu/gui/data.h index da9a70fb7..fd6cc3afc 100644 --- a/src/eez/modules/psu/gui/data.h +++ b/src/eez/modules/psu/gui/data.h @@ -38,7 +38,11 @@ namespace gui { ENUM_DEFINITION(CALIBRATION_VALUE_TYPE) \ ENUM_DEFINITION(USB_MODE) \ ENUM_DEFINITION(USB_DEVICE_CLASS) \ - ENUM_DEFINITION(UART_MODE) + ENUM_DEFINITION(UART_MODE) \ + ENUM_DEFINITION(UART_BAUD_RATE) \ + ENUM_DEFINITION(UART_DATA_BITS) \ + ENUM_DEFINITION(UART_STOP_BITS) \ + ENUM_DEFINITION(UART_PARITY) #define ENUM_DEFINITION(NAME) ENUM_DEFINITION_##NAME, enum EnumDefinition { diff --git a/src/eez/modules/psu/gui/page_sys_settings.cpp b/src/eez/modules/psu/gui/page_sys_settings.cpp index 53f5d5cb4..62d5baacf 100644 --- a/src/eez/modules/psu/gui/page_sys_settings.cpp +++ b/src/eez/modules/psu/gui/page_sys_settings.cpp @@ -840,6 +840,11 @@ void SysSettingsIOPinsPage::pageAlloc() { } m_uartMode = m_uartModeOrig = io_pins::g_uartMode; + + m_uartBaudRate = m_uartBaudRateOrig = io_pins::g_uartBaudRate; + m_uartDataBits = m_uartDataBitsOrig = io_pins::g_uartDataBits; + m_uartStopBits = m_uartStopBitsOrig = io_pins::g_uartStopBits; + m_uartParity = m_uartParityOrig = io_pins::g_uartParity; } void SysSettingsIOPinsPage::togglePolarity() { @@ -925,6 +930,76 @@ void SysSettingsIOPinsPage::selectUartMode() { pushSelectFromEnumPage(ENUM_DEFINITION_UART_MODE, m_uartMode, 0, onUartModeSet); } +void SysSettingsIOPinsPage::onUartBaudRateSet(uint16_t value) { + popPage(); + + SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getActivePage(); + if (value == 1) { + page->m_uartBaudRate = 14400; + } else if (value == 2) { + page->m_uartBaudRate = 19200; + } else if (value == 3) { + page->m_uartBaudRate = 38400; + } else if (value == 4) { + page->m_uartBaudRate = 57600; + } else if (value == 5) { + page->m_uartBaudRate = 115200; + } else { + page->m_uartBaudRate = 9600; + } +} + +void SysSettingsIOPinsPage::selectUartBaudRate() { + uint16_t value; + if (m_uartBaudRate == 14400) { + value = 1; + } else if (m_uartBaudRate == 19200) { + value = 2; + } else if (m_uartBaudRate == 38400) { + value = 3; + } else if (m_uartBaudRate == 57600) { + value = 4; + } else if (m_uartBaudRate == 115200) { + value = 5; + } else { + value = 0; + } + pushSelectFromEnumPage(ENUM_DEFINITION_UART_BAUD_RATE, value, 0, onUartBaudRateSet); +} + +void SysSettingsIOPinsPage::onUartDataBitsSet(uint16_t value) { + popPage(); + + SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getActivePage(); + page->m_uartDataBits = value; +} + +void SysSettingsIOPinsPage::selectUartDataBits() { + pushSelectFromEnumPage(ENUM_DEFINITION_UART_DATA_BITS, m_uartDataBits, 0, onUartDataBitsSet); +} + +void SysSettingsIOPinsPage::onUartParitySet(uint16_t value) { + popPage(); + + SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getActivePage(); + page->m_uartParity = value; +} + +void SysSettingsIOPinsPage::selectUartParity() { + pushSelectFromEnumPage(ENUM_DEFINITION_UART_PARITY, m_uartParity, 0, onUartParitySet); +} + +void SysSettingsIOPinsPage::onUartStopBitsSet(uint16_t value) { + popPage(); + + SysSettingsIOPinsPage *page = (SysSettingsIOPinsPage *)getActivePage(); + page->m_uartStopBits = value; +} + +void SysSettingsIOPinsPage::selectUartStopBits() { + pushSelectFromEnumPage(ENUM_DEFINITION_UART_STOP_BITS, m_uartStopBits, 0, onUartStopBitsSet); +} + int SysSettingsIOPinsPage::getDirty() { for (int i = 0; i < NUM_IO_PINS; ++i) { if (m_polarityOrig[i] != m_polarity[i] || m_functionOrig[i] != m_function[i]) { @@ -936,7 +1011,7 @@ int SysSettingsIOPinsPage::getDirty() { } } } - if (m_uartMode != m_uartModeOrig) { + if (m_uartMode != m_uartModeOrig || m_uartBaudRate != m_uartBaudRateOrig || m_uartDataBits != m_uartDataBitsOrig || m_uartStopBits != m_uartStopBitsOrig || m_uartParity != m_uartParityOrig) { return true; } return false; @@ -945,20 +1020,32 @@ int SysSettingsIOPinsPage::getDirty() { void SysSettingsIOPinsPage::set() { if (getDirty()) { io_pins::g_uartMode = m_uartMode; + io_pins::g_uartBaudRate = m_uartBaudRate; + io_pins::g_uartDataBits = m_uartDataBits; + io_pins::g_uartStopBits = m_uartStopBits; + io_pins::g_uartParity = m_uartParity; + bool uartReinitialized = false; + for (int i = 0; i < NUM_IO_PINS; i++) { io_pins::setPinPolarity(i, m_polarity[i]); io_pins::setPinFunction(i, m_function[i]); if (i >= DOUT1) { if (g_pwmFrequencyOrig[i - DOUT1] != g_pwmFrequency[i - DOUT1]) { io_pins::setPwmFrequency(i, g_pwmFrequency[i - DOUT1]); + uartReinitialized = true; } if (g_pwmDutyOrig[i - DOUT1] != g_pwmDuty[i - DOUT1]) { io_pins::setPwmDuty(i, g_pwmDuty[i - DOUT1]); + uartReinitialized = true; } } } - + + if (!uartReinitialized) { + uart::reinit(); + } + pageAlloc(); } } diff --git a/src/eez/modules/psu/gui/page_sys_settings.h b/src/eez/modules/psu/gui/page_sys_settings.h index bee3993bf..da92912fe 100644 --- a/src/eez/modules/psu/gui/page_sys_settings.h +++ b/src/eez/modules/psu/gui/page_sys_settings.h @@ -317,7 +317,11 @@ class SysSettingsIOPinsPage : public SetPage { void setPwmDuty(int pin, float duty); float getPwmDuty(int pin); - void selectUartMode(); + void selectUartMode(); + void selectUartBaudRate(); + void selectUartDataBits(); + void selectUartStopBits(); + void selectUartParity(); int getDirty(); void set(); @@ -325,7 +329,11 @@ class SysSettingsIOPinsPage : public SetPage { io_pins::Polarity m_polarity[NUM_IO_PINS]; io_pins::Function m_function[NUM_IO_PINS]; - uart::UartMode m_uartMode; + uart::UartMode m_uartMode; + uint32_t m_uartBaudRate; + uint32_t m_uartDataBits; + uint32_t m_uartStopBits; + uint32_t m_uartParity; private: int pinNumber; @@ -339,10 +347,18 @@ class SysSettingsIOPinsPage : public SetPage { float g_pwmDutyOrig[NUM_IO_PINS - DOUT1]; float g_pwmDuty[NUM_IO_PINS - DOUT1]; - uart::UartMode m_uartModeOrig; + uart::UartMode m_uartModeOrig; + uint32_t m_uartBaudRateOrig; + uint32_t m_uartDataBitsOrig; + uint32_t m_uartStopBitsOrig; + uint32_t m_uartParityOrig; static void onFunctionSet(uint16_t value); - static void onUartModeSet(uint16_t value); + static void onUartModeSet(uint16_t value); + static void onUartBaudRateSet(uint16_t value); + static void onUartDataBitsSet(uint16_t value); + static void onUartStopBitsSet(uint16_t value); + static void onUartParitySet(uint16_t value); }; class SysSettingsTrackingPage : public SetPage { diff --git a/src/eez/modules/psu/io_pins.cpp b/src/eez/modules/psu/io_pins.cpp index 89f20d934..a17b13325 100644 --- a/src/eez/modules/psu/io_pins.cpp +++ b/src/eez/modules/psu/io_pins.cpp @@ -61,6 +61,10 @@ static uint32_t g_pwmPeriodInt[NUM_IO_PINS - DOUT1]; static bool g_pwmStarted; uart::UartMode g_uartMode; +uint32_t g_uartBaudRate = 115200; +uint32_t g_uartDataBits = 8; +uint32_t g_uartStopBits = 1; +uint32_t g_uartParity = 1; #if defined EEZ_PLATFORM_STM32 static float g_pwmStartedFrequency; diff --git a/src/eez/modules/psu/io_pins.h b/src/eez/modules/psu/io_pins.h index 28bc972ba..4864bd19a 100644 --- a/src/eez/modules/psu/io_pins.h +++ b/src/eez/modules/psu/io_pins.h @@ -60,7 +60,27 @@ struct IOPin { extern IOPin g_ioPins[NUM_IO_PINS]; extern float g_pwmFrequency[NUM_IO_PINS - DOUT1]; extern float g_pwmDuty[NUM_IO_PINS - DOUT1]; + +#if defined(EEZ_PLATFORM_SIMULATOR) +#define UART_WORDLENGTH_7B USART_CR1_M1 /*!< 7-bit long UART frame */ +#define UART_WORDLENGTH_8B 0x00000000U /*!< 8-bit long UART frame */ +#define UART_WORDLENGTH_9B USART_CR1_M0 /*!< 9-bit long UART frame */ + +#define UART_STOPBITS_0_5 USART_CR2_STOP_0 /*!< UART frame with 0.5 stop bit */ +#define UART_STOPBITS_1 0x00000000U /*!< UART frame with 1 stop bit */ +#define UART_STOPBITS_1_5 (USART_CR2_STOP_0 | USART_CR2_STOP_1) /*!< UART frame with 1.5 stop bits */ +#define UART_STOPBITS_2 USART_CR2_STOP_1 /*!< UART frame with 2 stop bits */ + +#define UART_PARITY_NONE 0x00000000U /*!< No parity */ +#define UART_PARITY_EVEN USART_CR1_PCE /*!< Even parity */ +#define UART_PARITY_ODD (USART_CR1_PCE | USART_CR1_PS) /*!< Odd parity */ +#endif + extern uart::UartMode g_uartMode; +extern uint32_t g_uartBaudRate; +extern uint32_t g_uartDataBits; +extern uint32_t g_uartStopBits; +extern uint32_t g_uartParity; // 0 - None, 1 - Even, 2 - Odd void reset(); void tick(); diff --git a/src/eez/modules/psu/profile.cpp b/src/eez/modules/psu/profile.cpp index c917d016c..9a3229330 100644 --- a/src/eez/modules/psu/profile.cpp +++ b/src/eez/modules/psu/profile.cpp @@ -521,6 +521,12 @@ static void resetProfileToDefaults(Parameters &profile) { profile.tempProt[i].state = OTP_CH_DEFAULT_STATE; } } + + profile.uartMode = uart::UART_MODE_BUFFER; + profile.uartBaudRate = 115200; + profile.uartDataBits = 8; + profile.uartStopBits = 1; + profile.uartParity = 1; } static bool repositionSlotsInProfileToMatchCurrentSlotsConfiguration(Parameters &profile) { @@ -765,6 +771,10 @@ static void saveState(Parameters &profile, List *lists) { memcpy(profile.ioPinsPwmFrequency, io_pins::g_pwmFrequency, sizeof(profile.ioPinsPwmFrequency)); memcpy(profile.ioPinsPwmDuty, io_pins::g_pwmDuty, sizeof(profile.ioPinsPwmDuty)); profile.uartMode = io_pins::g_uartMode; + profile.uartBaudRate = io_pins::g_uartBaudRate; + profile.uartDataBits = io_pins::g_uartDataBits; + profile.uartStopBits = io_pins::g_uartStopBits; + profile.uartParity = io_pins::g_uartParity; eez::psu::gui::edit_mode_step::getProfileParameters(profile); @@ -867,6 +877,10 @@ static bool recallState(Parameters &profile, List *lists, int recallOptions, int memcpy(io_pins::g_pwmFrequency, profile.ioPinsPwmFrequency, sizeof(profile.ioPinsPwmFrequency)); memcpy(io_pins::g_pwmDuty, profile.ioPinsPwmDuty, sizeof(profile.ioPinsPwmDuty)); io_pins::g_uartMode = (uart::UartMode)profile.uartMode; + io_pins::g_uartBaudRate = profile.uartBaudRate; + io_pins::g_uartDataBits = profile.uartDataBits; + io_pins::g_uartStopBits = profile.uartStopBits; + io_pins::g_uartParity = profile.uartParity; io_pins::refresh(); eez::psu::gui::edit_mode_step::setProfileParameters(profile); @@ -1114,6 +1128,10 @@ static bool profileWrite(WriteContext &ctx, const Parameters ¶meters, List * ctx.group("uart"); WRITE_PROPERTY("mode", parameters.uartMode); + WRITE_PROPERTY("baudRate", parameters.uartBaudRate); + WRITE_PROPERTY("dataBits", parameters.uartDataBits); + WRITE_PROPERTY("stopBits", parameters.uartStopBits); + WRITE_PROPERTY("parity", parameters.uartParity); if (!eez::psu::gui::edit_mode_step::writeProfileProperties(ctx, parameters)) { return false; @@ -1472,6 +1490,10 @@ static bool profileReadCallback(ReadContext &ctx, Parameters ¶meters, List * if (ctx.matchGroup("uart")) { READ_PROPERTY("mode", parameters.uartMode); + READ_PROPERTY("baudRate", parameters.uartBaudRate); + READ_PROPERTY("dataBits", parameters.uartDataBits); + READ_PROPERTY("stopBits", parameters.uartStopBits); + READ_PROPERTY("parity", parameters.uartParity); } if (eez::psu::gui::edit_mode_step::readProfileProperties(ctx, parameters)) { diff --git a/src/eez/modules/psu/profile.h b/src/eez/modules/psu/profile.h index 349f298d8..5d0db7d2b 100644 --- a/src/eez/modules/psu/profile.h +++ b/src/eez/modules/psu/profile.h @@ -148,6 +148,10 @@ struct Parameters { float ioPinsPwmFrequency[NUM_IO_PINS - DOUT1]; float ioPinsPwmDuty[NUM_IO_PINS - DOUT1]; uint8_t uartMode; + unsigned int uartBaudRate; + unsigned int uartDataBits; + unsigned int uartStopBits; + unsigned int uartParity; EncoderModes encoderModes; FunctionGeneratorParameters functionGeneratorParameters; }; diff --git a/src/eez/modules/psu/scpi/syst.cpp b/src/eez/modules/psu/scpi/syst.cpp index 1b49fc745..405883bae 100644 --- a/src/eez/modules/psu/scpi/syst.cpp +++ b/src/eez/modules/psu/scpi/syst.cpp @@ -2442,6 +2442,96 @@ scpi_result_t scpi_cmd_systemCommunicateUartModeQ(scpi_t *context) { return SCPI_RES_OK; } +scpi_result_t scpi_cmd_systemCommunicateUartBaud(scpi_t *context) { + uint32_t uartBaudRate; + if (!SCPI_ParamUInt32(context, &uartBaudRate, true)) { + return SCPI_RES_ERR; + } + + if (uartBaudRate != 9600 && uartBaudRate != 14400 && uartBaudRate != 19200 && uartBaudRate != 38400 && uartBaudRate != 57600 && uartBaudRate != 115200) { + SCPI_ErrorPush(context, SCPI_ERROR_DATA_OUT_OF_RANGE); + return SCPI_RES_ERR; + } + + io_pins::g_uartBaudRate = uartBaudRate; + uart::reinit(); + + return SCPI_RES_OK; +} + +scpi_result_t scpi_cmd_systemCommunicateUartBaudQ(scpi_t *context) { + SCPI_ResultUInt32(context, io_pins::g_uartBaudRate); + return SCPI_RES_OK; +} + +static scpi_choice_def_t g_uartParityChoice[] = { + { "NONe", 0 }, + { "EVEN", 1 }, + { "ODD", 2 }, + SCPI_CHOICE_LIST_END +}; + +scpi_result_t scpi_cmd_systemCommunicateUartParity(scpi_t *context) { + int32_t uartParity; + if (!SCPI_ParamChoice(context, g_uartParityChoice, &uartParity, true)) { + return SCPI_RES_ERR; + } + + io_pins::g_uartParity = uartParity; + uart::reinit(); + + return SCPI_RES_OK; +} + +scpi_result_t scpi_cmd_systemCommunicateUartParityQ(scpi_t *context) { + resultChoiceName(context, g_uartParityChoice, io_pins::g_uartParity); + return SCPI_RES_OK; +} + +scpi_result_t scpi_cmd_systemCommunicateUartData(scpi_t *context) { + uint32_t uartDataBits; + if (!SCPI_ParamUInt32(context, &uartDataBits, true)) { + return SCPI_RES_ERR; + } + + if (uartDataBits < 6 || uartDataBits > 8) { + SCPI_ErrorPush(context, SCPI_ERROR_DATA_OUT_OF_RANGE); + return SCPI_RES_ERR; + } + + io_pins::g_uartDataBits = uartDataBits; + uart::reinit(); + + return SCPI_RES_OK; +} + +scpi_result_t scpi_cmd_systemCommunicateUartDataQ(scpi_t *context) { + SCPI_ResultUInt32(context, io_pins::g_uartDataBits); + return SCPI_RES_OK; +} + +scpi_result_t scpi_cmd_systemCommunicateUartStop(scpi_t *context) { + uint32_t uartStopBits; + if (!SCPI_ParamUInt32(context, &uartStopBits, true)) { + return SCPI_RES_ERR; + } + + if (uartStopBits < 1 || uartStopBits > 2) { + SCPI_ErrorPush(context, SCPI_ERROR_DATA_OUT_OF_RANGE); + return SCPI_RES_ERR; + } + + io_pins::g_uartStopBits = uartStopBits; + uart::reinit(); + + return SCPI_RES_OK; +} + +scpi_result_t scpi_cmd_systemCommunicateUartStopQ(scpi_t *context) { + SCPI_ResultUInt32(context, io_pins::g_uartStopBits); + return SCPI_RES_OK; +} + } // namespace scpi } // namespace psu } // namespace eez diff --git a/src/eez/scpi/commands_stm32.h b/src/eez/scpi/commands_stm32.h index 164973dca..7068471c2 100644 --- a/src/eez/scpi/commands_stm32.h +++ b/src/eez/scpi/commands_stm32.h @@ -478,6 +478,14 @@ SCPI_COMMAND("SYSTem:VERSion?", scpi_cmd_systemVersionQ) \ SCPI_COMMAND("SYSTem:LFRequency", scpi_cmd_systemLfrequency) \ SCPI_COMMAND("SYSTem:LFRequency?", scpi_cmd_systemLfrequencyQ) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:BAUD", scpi_cmd_systemCommunicateUartBaud) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:BAUD?", scpi_cmd_systemCommunicateUartBaudQ) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:PARIty", scpi_cmd_systemCommunicateUartParity) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:PARIty?", scpi_cmd_systemCommunicateUartParityQ) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:DATA", scpi_cmd_systemCommunicateUartData) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:DATA?", scpi_cmd_systemCommunicateUartDataQ) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:STOP", scpi_cmd_systemCommunicateUartStop) \ + SCPI_COMMAND("SYSTem:COMMunicate:UART:STOP?", scpi_cmd_systemCommunicateUartStopQ) \ SCPI_COMMAND("TRIGger:DLOG:SOURce", scpi_cmd_triggerDlogSource) \ SCPI_COMMAND("TRIGger:DLOG:SOURce?", scpi_cmd_triggerDlogSourceQ) \ SCPI_COMMAND("TRIGger:DLOG[:IMMediate]", scpi_cmd_triggerDlogImmediate) \ diff --git a/src/eez/uart.cpp b/src/eez/uart.cpp index d56f8d892..2d4285121 100644 --- a/src/eez/uart.cpp +++ b/src/eez/uart.cpp @@ -34,6 +34,11 @@ using namespace eez::scpi; using namespace eez::psu; using namespace eez::psu::scpi; +uint32_t g_uartBaudRate; +uint32_t g_uartWordLength; +uint32_t g_uartStopBits; +uint32_t g_uartParity; + namespace eez { namespace uart { @@ -143,7 +148,7 @@ static size_t uartOutputBufferWriteFunc(const char *data, size_t len) { g_messageAvailable = true; if (g_initialized) { #ifdef EEZ_PLATFORM_STM32 - HAL_UART_Transmit(PHUART, (uint8_t *)data, (uint16_t)len, 10); + HAL_UART_Transmit(PHUART, (uint8_t *)data, (uint16_t)len, 1000); #endif } return len; @@ -299,6 +304,20 @@ void refresh() { } else { if (bp3c::flash_slave::g_bootloaderMode || psu::io_pins::g_ioPins[DIN1].function == psu::io_pins::FUNCTION_UART) { #ifdef EEZ_PLATFORM_STM32 + if (bp3c::flash_slave::g_bootloaderMode) { + g_uartBaudRate = 115200; + g_uartWordLength = UART_WORDLENGTH_9B; + g_uartStopBits = UART_STOPBITS_1; + g_uartParity = UART_PARITY_EVEN; + } else { + int dataBits = io_pins::g_uartDataBits + (io_pins::g_uartParity == 0 ? 0 : 1); + + g_uartBaudRate = io_pins::g_uartBaudRate; + g_uartWordLength = dataBits == 7 ? UART_WORDLENGTH_7B : dataBits == 8 ? UART_WORDLENGTH_8B : UART_WORDLENGTH_9B; + g_uartStopBits = io_pins::g_uartStopBits == 1 ? UART_STOPBITS_1 : UART_STOPBITS_2; + g_uartParity = io_pins::g_uartParity == 0 ? UART_PARITY_NONE : io_pins::g_uartParity == 1 ? UART_PARITY_EVEN : UART_PARITY_ODD; + } + if (g_mcuRevision >= MCU_REVISION_R3B3) { MX_UART4_Init(); } else { @@ -356,6 +375,20 @@ void reinit() { g_initialized = false; #ifdef EEZ_PLATFORM_STM32 + if (bp3c::flash_slave::g_bootloaderMode) { + g_uartBaudRate = 115200; + g_uartWordLength = UART_WORDLENGTH_9B; + g_uartStopBits = UART_STOPBITS_1; + g_uartParity = UART_PARITY_EVEN; + } else { + int dataBits = io_pins::g_uartDataBits + (io_pins::g_uartParity == 0 ? 0 : 1); + + g_uartBaudRate = io_pins::g_uartBaudRate; + g_uartWordLength = dataBits == 7 ? UART_WORDLENGTH_7B : dataBits == 8 ? UART_WORDLENGTH_8B : UART_WORDLENGTH_9B; + g_uartStopBits = io_pins::g_uartStopBits == 1 ? UART_STOPBITS_1 : UART_STOPBITS_2; + g_uartParity = io_pins::g_uartParity == 0 ? UART_PARITY_NONE : io_pins::g_uartParity == 1 ? UART_PARITY_EVEN : UART_PARITY_ODD; + } + if (g_mcuRevision >= MCU_REVISION_R3B3) { MX_UART4_Init(); } else { diff --git a/src/third_party/stm32_cubeide/Core/Src/usart.c b/src/third_party/stm32_cubeide/Core/Src/usart.c index 8b7a57e4f..fb83fb8d8 100644 --- a/src/third_party/stm32_cubeide/Core/Src/usart.c +++ b/src/third_party/stm32_cubeide/Core/Src/usart.c @@ -22,6 +22,10 @@ /* USER CODE BEGIN 0 */ DMA_HandleTypeDef hdma_uart7_rx; +extern uint32_t g_uartBaudRate; // 115200 +extern uint32_t g_uartWordLength; // UART_WORDLENGTH_9B +extern uint32_t g_uartStopBits; // UART_STOPBITS_1 +extern uint32_t g_uartParity; // UART_PARITY_EVEN /* USER CODE END 0 */ UART_HandleTypeDef huart4; @@ -39,10 +43,10 @@ void MX_UART4_Init(void) /* USER CODE END UART4_Init 1 */ huart4.Instance = UART4; - huart4.Init.BaudRate = 115200; - huart4.Init.WordLength = UART_WORDLENGTH_9B; - huart4.Init.StopBits = UART_STOPBITS_1; - huart4.Init.Parity = UART_PARITY_EVEN; + huart4.Init.BaudRate = g_uartBaudRate; + huart4.Init.WordLength = g_uartWordLength; + huart4.Init.StopBits = g_uartStopBits; + huart4.Init.Parity = g_uartParity; huart4.Init.Mode = UART_MODE_TX_RX; huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart4.Init.OverSampling = UART_OVERSAMPLING_16; @@ -196,10 +200,10 @@ void MX_UART7_Init(void) { huart7.Instance = UART7; - huart7.Init.BaudRate = 115200; - huart7.Init.WordLength = UART_WORDLENGTH_9B; - huart7.Init.StopBits = UART_STOPBITS_1; - huart7.Init.Parity = UART_PARITY_EVEN; + huart7.Init.BaudRate = g_uartBaudRate; + huart7.Init.WordLength = g_uartWordLength; + huart7.Init.StopBits = g_uartStopBits; + huart7.Init.Parity = g_uartParity; huart7.Init.Mode = UART_MODE_TX_RX; huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart7.Init.OverSampling = UART_OVERSAMPLING_16;