From 1f6a3ad16728992d50e8992317ebf83edc8bafab Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 2 Feb 2021 19:53:19 +1100 Subject: [PATCH] Pinecil uart (#830) * Creating uart debug handler * Simpler get raw uV tip * Update Setup.cpp * Debug out working. Moved Logo Need to update docs around logos before merging this in * Add in current pwm level + fix signed int * Update moving logo page for pinecil by 64k * Update TipThermoModel.h --- Documentation/Logo.md | 2 +- source/Core/BSP/BSP.h | 3 ++ source/Core/BSP/Pine64/Debug.cpp | 47 +++++++++++++++++++ source/Core/BSP/Pine64/Debug.h | 22 +++++++++ source/Core/BSP/Pine64/Model_Config.h | 1 + source/Core/BSP/Pine64/Pins.h | 7 +++ source/Core/BSP/Pine64/Setup.cpp | 30 ++++++++++++ .../Source/GCC/gcc_gd32vf103_flashxip.ld | 2 +- .../gd32vf103/Common/Include/gd32vf103_dma.h | 7 ++- .../Common/Include/gd32vf103_usart.h | 7 +++ source/Core/BSP/Pine64/logo.cpp | 2 +- source/Core/Drivers/TipThermoModel.cpp | 6 +-- source/Core/Drivers/TipThermoModel.h | 2 +- source/Core/Src/main.cpp | 2 +- source/Core/Threads/GUIThread.cpp | 7 +-- source/Core/Threads/MOVThread.cpp | 2 +- source/Core/Threads/PIDThread.cpp | 4 +- 17 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 source/Core/BSP/Pine64/Debug.cpp create mode 100644 source/Core/BSP/Pine64/Debug.h diff --git a/Documentation/Logo.md b/Documentation/Logo.md index 500c1832..b8b0add0 100644 --- a/Documentation/Logo.md +++ b/Documentation/Logo.md @@ -34,7 +34,7 @@ To flash the logo, use the following steps: - `python3 img2ts100.py input.png logo.hex` - `riscv-nuclei-elf-objcopy -I ihex -O binary logo.hex logo.bin` - - `dfu-util -d 28e9:0189 -a 0 -D logo.bin -s 0x0800f800` + - `dfu-util -d 28e9:0189 -a 0 -D logo.bin -s 0x0801F800` This will use the objcopy tool to convert the hex to a binary file, and then use dfu-util to flash it in the right location. If you do not have `riscv-nuclei-elf-objcopy` installed, you can generally use any objcopy tool from any toolchain you do have. diff --git a/source/Core/BSP/BSP.h b/source/Core/BSP/BSP.h index eb54ead0..08ead36a 100644 --- a/source/Core/BSP/BSP.h +++ b/source/Core/BSP/BSP.h @@ -71,6 +71,9 @@ int16_t getRawHallEffect(); // Returns true if power is from dumb "DC" input rather than "smart" QC or PD bool getIsPoweredByDCIN(); +// Logs the system state to a debug interface if supported +void log_system_state(int32_t PWMWattsx10); + #ifdef __cplusplus } #endif diff --git a/source/Core/BSP/Pine64/Debug.cpp b/source/Core/BSP/Pine64/Debug.cpp new file mode 100644 index 00000000..354985fb --- /dev/null +++ b/source/Core/BSP/Pine64/Debug.cpp @@ -0,0 +1,47 @@ +/* + * Debug.cpp + * + * Created on: 26 Jan. 2021 + * Author: Ralim + */ +#include "Debug.h" +#include "Pins.h" +extern "C" { +#include "gd32vf103_usart.h" +} +char uartOutputBuffer[uartOutputBufferLength]; +volatile uint32_t currentOutputPos = 0xFF; +volatile uint32_t outputLength = 0; +extern volatile uint8_t pendingPWM; +void log_system_state(int32_t PWMWattsx10) { + if (currentOutputPos == 0xFF) { + + // Want to print a CSV log out the uart + // Tip_Temp_C,Handle_Temp_C,Output_Power_Wattx10,PWM,Tip_Raw\r\n + // 3+1+3+1+3+1+3+1+5+2 = 23, so sizing at 32 for now + + outputLength = snprintf(uartOutputBuffer, uartOutputBufferLength, "%lu,%u,%li,%u,%lu\r\n", // + TipThermoModel::getTipInC(false), // Tip temp in C + getHandleTemperature(), // Handle temp in C X10 + PWMWattsx10, // Output Wattage + pendingPWM, // PWM + TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0), true) // Tip temp in uV + ); + + // Now print this out the uart via IRQ (DMA cant be used as oled has it) + currentOutputPos = 0; + /* enable USART1 Transmit Buffer Empty interrupt */ + usart_interrupt_enable(UART_PERIF, USART_INT_TBE); + } +} + +void USART1_IRQHandler(void) { + if (RESET != usart_interrupt_flag_get(UART_PERIF, USART_INT_FLAG_TBE)) { + /* write one byte to the transmit data register */ + usart_data_transmit(UART_PERIF, uartOutputBuffer[currentOutputPos++]); + if (currentOutputPos >= outputLength) { + currentOutputPos = 0xFF; // Mark done + usart_interrupt_disable(UART_PERIF, USART_INT_TBE); + } + } +} diff --git a/source/Core/BSP/Pine64/Debug.h b/source/Core/BSP/Pine64/Debug.h new file mode 100644 index 00000000..a9a60ec2 --- /dev/null +++ b/source/Core/BSP/Pine64/Debug.h @@ -0,0 +1,22 @@ +/* + * Debug.h + * + * Created on: 26 Jan. 2021 + * Author: Ralim + */ + +#ifndef CORE_BSP_PINE64_DEBUG_H_ +#define CORE_BSP_PINE64_DEBUG_H_ + +#include "BSP.h" +#include "TipThermoModel.h" +#include +#include + +const unsigned int uartOutputBufferLength = 32; +extern char uartOutputBuffer[uartOutputBufferLength]; +extern "C" { + +void USART1_IRQHandler(void); +} +#endif /* CORE_BSP_PINE64_DEBUG_H_ */ diff --git a/source/Core/BSP/Pine64/Model_Config.h b/source/Core/BSP/Pine64/Model_Config.h index d0ac1e36..1a63894e 100644 --- a/source/Core/BSP/Pine64/Model_Config.h +++ b/source/Core/BSP/Pine64/Model_Config.h @@ -27,6 +27,7 @@ #define HALL_SENSOR #define HALL_SI7210 #define BATTFILTERDEPTH 32 +#define DEBUG_UART_OUTPUT #endif #endif /* BSP_PINE64_MODEL_CONFIG_H_ */ diff --git a/source/Core/BSP/Pine64/Pins.h b/source/Core/BSP/Pine64/Pins.h index d6590081..09d3e9e3 100644 --- a/source/Core/BSP/Pine64/Pins.h +++ b/source/Core/BSP/Pine64/Pins.h @@ -50,4 +50,11 @@ #define FUSB302_IRQ_Pin BIT(5) #define FUSB302_IRQ_GPIO_Port GPIOB +// uart +#define UART_TX_Pin BIT(2) +#define UART_TX_GPIO_Port GPIOA +#define UART_RX_Pin BIT(3) +#define UART_RX_GPIO_Port GPIOA +#define UART_PERIF USART1 + #endif /* BSP_PINE64_PINS_H_ */ diff --git a/source/Core/BSP/Pine64/Setup.cpp b/source/Core/BSP/Pine64/Setup.cpp index 22e39541..76bd8260 100644 --- a/source/Core/BSP/Pine64/Setup.cpp +++ b/source/Core/BSP/Pine64/Setup.cpp @@ -6,6 +6,7 @@ */ #include "Setup.h" #include "BSP.h" +#include "Debug.h" #include "Pins.h" #include "gd32vf103.h" #include @@ -20,6 +21,7 @@ void setup_i2c(); void setup_adc(); void setup_timers(); void setup_iwdg(); +void setup_uart(); void hardware_init() { // GPIO @@ -35,6 +37,8 @@ void hardware_init() { // Watchdog setup_iwdg(); + // uart for debugging + setup_uart(); /* enable TIMER1 - PWM control timing*/ timer_enable(TIMER1); timer_enable(TIMER2); @@ -47,6 +51,32 @@ uint16_t getADC(uint8_t channel) { return sum >> 2; } +void setup_uart() { + // Setup the uart pins as a uart with dma + + /* enable USART clock */ + rcu_periph_clock_enable(RCU_USART1); + + /* connect port to USARTx_Tx */ + gpio_init(UART_TX_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_10MHZ, UART_TX_Pin); + + /* connect port to USARTx_Rx */ + gpio_init(UART_RX_GPIO_Port, GPIO_MODE_IPU, GPIO_OSPEED_10MHZ, UART_RX_Pin); + + /* USART configure */ + usart_deinit(UART_PERIF); + usart_baudrate_set(UART_PERIF, 2 * 1000 * 1000U); + usart_word_length_set(UART_PERIF, USART_WL_8BIT); + usart_stop_bit_set(UART_PERIF, USART_STB_1BIT); + usart_parity_config(UART_PERIF, USART_PM_NONE); + usart_hardware_flow_rts_config(UART_PERIF, USART_RTS_DISABLE); + usart_hardware_flow_cts_config(UART_PERIF, USART_CTS_DISABLE); + usart_receive_config(UART_PERIF, USART_RECEIVE_DISABLE); // Dont use rx for now + usart_transmit_config(UART_PERIF, USART_TRANSMIT_ENABLE); + eclic_irq_enable(USART1_IRQn, 15, 15); + usart_enable(UART_PERIF); +} + void setup_gpio() { /* enable GPIOB clock */ rcu_periph_clock_enable(RCU_GPIOA); diff --git a/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Board/pinecil/Source/GCC/gcc_gd32vf103_flashxip.ld b/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Board/pinecil/Source/GCC/gcc_gd32vf103_flashxip.ld index 42b82a6d..819ae05f 100644 --- a/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Board/pinecil/Source/GCC/gcc_gd32vf103_flashxip.ld +++ b/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Board/pinecil/Source/GCC/gcc_gd32vf103_flashxip.ld @@ -32,7 +32,7 @@ OUTPUT_ARCH( "riscv" ) * */ __ROM_BASE = 0x08000000; -__ROM_SIZE = 0x00020000; +__ROM_SIZE = 0x0001F400; /*--------------------- ILM RAM Configuration --------------------------- * ILM RAM Configuration diff --git a/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_dma.h b/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_dma.h index 85fd6fec..5c070a1d 100644 --- a/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_dma.h +++ b/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_dma.h @@ -38,7 +38,9 @@ OF SUCH DAMAGE. #include "gd32vf103.h" #include "gd32vf103_dbg.h" #include "gd32vf103_rcu.h" - +#ifdef _cplusplus +extern "C" { +#endif /* DMA definitions */ #define DMA0 (DMA_BASE) /*!< DMA0 base address */ #define DMA1 (DMA_BASE + 0x0400U) /*!< DMA1 base address */ @@ -277,5 +279,8 @@ void dma_interrupt_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, ui void dma_interrupt_enable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source); /* disable DMA interrupt */ void dma_interrupt_disable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source); +#ifdef _cplusplus +} +#endif #endif /* GD32VF103_DMA_H */ diff --git a/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_usart.h b/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_usart.h index 8f0021a1..56edca63 100644 --- a/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_usart.h +++ b/source/Core/BSP/Pine64/Vendor/SoC/gd32vf103/Common/Include/gd32vf103_usart.h @@ -38,6 +38,9 @@ OF SUCH DAMAGE. #include "gd32vf103.h" #include "gd32vf103_dbg.h" #include "gd32vf103_rcu.h" +#ifdef _cplusplus +extern "C" { +#endif /* USARTx(x=0,1,2)/UARTx(x=3,4) definitions */ #define USART1 USART_BASE /*!< USART1 base address */ @@ -371,4 +374,8 @@ FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, uint32_t int_flag); void usart_interrupt_flag_clear(uint32_t usart_periph, uint32_t flag); int usart_write(uint32_t usart_periph, int ch); uint8_t usart_read(uint32_t usart_periph); +#ifdef _cplusplus +} +#endif + #endif /* GD32VF103_USART_H */ diff --git a/source/Core/BSP/Pine64/logo.cpp b/source/Core/BSP/Pine64/logo.cpp index 3048f341..d73c165c 100644 --- a/source/Core/BSP/Pine64/logo.cpp +++ b/source/Core/BSP/Pine64/logo.cpp @@ -8,7 +8,7 @@ #include "BSP.h" #include "OLED.hpp" // Second last page of flash set aside for logo image. -#define FLASH_LOGOADDR (0x8000000 | 0xF800) +#define FLASH_LOGOADDR (0x08000000 + (126 * 1024)) // Logo header signature. #define LOGO_HEADER_VALUE 0xF00DAA55 diff --git a/source/Core/Drivers/TipThermoModel.cpp b/source/Core/Drivers/TipThermoModel.cpp index 81fc371a..29bf1e7a 100644 --- a/source/Core/Drivers/TipThermoModel.cpp +++ b/source/Core/Drivers/TipThermoModel.cpp @@ -28,7 +28,7 @@ * This was bought to my attention by */ -uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC) { +uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC, bool skipCalOffset) { // This takes the raw ADC samples, converts these to uV // Then divides this down by the gain to convert to the uV on the input to the op-amp (A+B terminals) // Then remove the calibration value that is stored as a tip offset @@ -41,9 +41,9 @@ uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC) { // Now to divide this down by the gain valueuV /= OP_AMP_GAIN_STAGE; - if (systemSettings.CalibrationOffset) { + if (systemSettings.CalibrationOffset && skipCalOffset == false) { // Remove uV tipOffset - if (valueuV >= systemSettings.CalibrationOffset) + if (valueuV > systemSettings.CalibrationOffset) valueuV -= systemSettings.CalibrationOffset; else valueuV = 0; diff --git a/source/Core/Drivers/TipThermoModel.h b/source/Core/Drivers/TipThermoModel.h index 27a672c2..4af4ab3c 100644 --- a/source/Core/Drivers/TipThermoModel.h +++ b/source/Core/Drivers/TipThermoModel.h @@ -21,7 +21,7 @@ public: static uint32_t convertTipRawADCToDegC(uint16_t rawADC); static uint32_t convertTipRawADCToDegF(uint16_t rawADC); // Returns the uV of the tip reading before the op-amp compensating for pullups - static uint32_t convertTipRawADCTouV(uint16_t rawADC); + static uint32_t convertTipRawADCTouV(uint16_t rawADC,bool skipCalOffset=false); static uint32_t convertCtoF(uint32_t degC); static uint32_t convertFtoC(uint32_t degF); diff --git a/source/Core/Src/main.cpp b/source/Core/Src/main.cpp index 4a29f8fa..e96c47eb 100644 --- a/source/Core/Src/main.cpp +++ b/source/Core/Src/main.cpp @@ -21,7 +21,7 @@ uint32_t GUITaskBuffer[GUITaskStackSize]; osStaticThreadDef_t GUITaskControlBlock; osThreadId PIDTaskHandle; -static const size_t PIDTaskStackSize = 512 / 4; +static const size_t PIDTaskStackSize = 1024 / 4; uint32_t PIDTaskBuffer[PIDTaskStackSize]; osStaticThreadDef_t PIDTaskControlBlock; diff --git a/source/Core/Threads/GUIThread.cpp b/source/Core/Threads/GUIThread.cpp index c9550614..ed5fc8e9 100644 --- a/source/Core/Threads/GUIThread.cpp +++ b/source/Core/Threads/GUIThread.cpp @@ -655,12 +655,7 @@ void showDebugMenu(void) { break; case 6: // Raw Tip - { - uint32_t temp = systemSettings.CalibrationOffset; - systemSettings.CalibrationOffset = 0; - OLED::printNumber(TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0)), 6); - systemSettings.CalibrationOffset = temp; - } + { OLED::printNumber(TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0), true), 6); } break; case 7: // Temp in C diff --git a/source/Core/Threads/MOVThread.cpp b/source/Core/Threads/MOVThread.cpp index fb4d23df..f02374c8 100644 --- a/source/Core/Threads/MOVThread.cpp +++ b/source/Core/Threads/MOVThread.cpp @@ -110,7 +110,7 @@ inline void readAccelerometer(int16_t &tx, int16_t &ty, int16_t &tz, Orientation } } void startMOVTask(void const *argument __unused) { - osDelay(TICKS_100MS / 5);// This is here as the BMA doesnt start up instantly and can wedge the I2C bus if probed too fast after boot + osDelay(TICKS_100MS / 5); // This is here as the BMA doesnt start up instantly and can wedge the I2C bus if probed too fast after boot detectAccelerometerVersion(); osDelay(TICKS_100MS / 2); // wait ~50ms for setup of accel to finalise lastMovementTime = 0; diff --git a/source/Core/Threads/PIDThread.cpp b/source/Core/Threads/PIDThread.cpp index 170522f9..5a35e2a3 100644 --- a/source/Core/Threads/PIDThread.cpp +++ b/source/Core/Threads/PIDThread.cpp @@ -113,7 +113,9 @@ void startPIDTask(void const *argument __unused) { } else { setTipX10Watts(x10WattsOut); } - +#ifdef DEBUG_UART_OUTPUT + log_system_state(x10WattsOut); +#endif resetWatchdog(); } else { // ADC interrupt timeout