1
0
forked from me/IronOS

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
This commit is contained in:
Ben V. Brown
2021-02-02 19:53:19 +11:00
committed by GitHub
parent 15e51f9faa
commit 1f6a3ad167
17 changed files with 136 additions and 17 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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);
}
}
}

View File

@@ -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 <stdio.h>
#include <string.h>
const unsigned int uartOutputBufferLength = 32;
extern char uartOutputBuffer[uartOutputBufferLength];
extern "C" {
void USART1_IRQHandler(void);
}
#endif /* CORE_BSP_PINE64_DEBUG_H_ */

View File

@@ -27,6 +27,7 @@
#define HALL_SENSOR
#define HALL_SI7210
#define BATTFILTERDEPTH 32
#define DEBUG_UART_OUTPUT
#endif
#endif /* BSP_PINE64_MODEL_CONFIG_H_ */

View File

@@ -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_ */

View File

@@ -6,6 +6,7 @@
*/
#include "Setup.h"
#include "BSP.h"
#include "Debug.h"
#include "Pins.h"
#include "gd32vf103.h"
#include <string.h>
@@ -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);

View File

@@ -32,7 +32,7 @@ OUTPUT_ARCH( "riscv" )
* </h>
*/
__ROM_BASE = 0x08000000;
__ROM_SIZE = 0x00020000;
__ROM_SIZE = 0x0001F400;
/*--------------------- ILM RAM Configuration ---------------------------
* <h> ILM RAM Configuration

View File

@@ -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 */

View File

@@ -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 */

View File

@@ -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

View File

@@ -28,7 +28,7 @@
* This was bought to my attention by <Kuba Sztandera>
*/
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;

View File

@@ -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);

View File

@@ -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;

View File

@@ -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

View File

@@ -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;

View File

@@ -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