Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions firmware/generic/src/data/images.cpp

This file was deleted.

59 changes: 53 additions & 6 deletions firmware/generic/src/drivers/adc.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include "adc.h"
extern "C"
{
#include "gd32vf103.h"
#include "drivers/SysTick.h"
#include "gd32vf103.h"
}

// anUpdate (downshifted oversampling)
uint8_t anCount = 0; // counter for oversampling (new value after 255, @44,4KHz => 173Hz, suitable for potentiometers)
uint32_t anTotal[MAX_AN]; // the running total
int anAvg[MAX_AN]; // analog average
uint16_t anRaw[8]; // raw sampled values stored by DMA
//uint8_t anCount = 0; // counter for oversampling (new value after 255, @44,4KHz => 173Hz, suitable for potentiometers)
//uint32_t anTotal[MAX_AN]; // the running total
//int anAvg[MAX_AN]; // analog average
//uint16_t anRaw[8]; // raw sampled values stored by DMA

// analog average (downshifted oversampling)
void adc::update(uint8_t ch)
void adc::updateAVG(uint8_t ch)
{
if (anCount < 255)
{
Expand All @@ -25,4 +26,50 @@ void adc::update(uint8_t ch)
anAvg[ch] = anTotal[ch] >> 8; // inputs and pot 3 is not inverted
anTotal[ch] = 0;
}
}

void adc::update()
{
// Running average is done after DAC since it will add jitter due to uneven number of cycles
anCount++;
updateAVG(0); // ADC0
updateAVG(1); // ADC1
updateAVG(2); // pot0 (not inverted anymore)
updateAVG(3); // pot1 (not inverted anymore)
updateAVG(4); // pot2

// Start ADC sampling (using DMA)
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL); // start ADC: DMA => adc_value[]
}

void adc::init(SysTick &stk) {
/* reset ADC */
adc_deinit(ADC0);
/* ADC mode config */
adc_mode_config(ADC_MODE_FREE); // ADC0 and ADC1 work independently
/* ADC data alignment config */
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
/* Configure word length */
adc_resolution_config(ADC0, ADC_RESOLUTION_12B);
/* ADC channel length config */
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 5);
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_13POINT5); // ADC left 41.4+12.5=54 cycles =5uS *5=20us => 50.1Hz
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_3, ADC_SAMPLETIME_13POINT5); // ADC right
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_6, ADC_SAMPLETIME_13POINT5); // pot0 (UR)
adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_7, ADC_SAMPLETIME_13POINT5); // pot1 (LR)
adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_8, ADC_SAMPLETIME_13POINT5); // pot2 (LL)
/* ADC trigger config */
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE); // software trigger
/* ADC external trigger enable */
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
/* ADC discontinuous mode */
adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 5); // should it be 4?
/* enable ADC interface */
adc_enable(ADC0);
stk.delay(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC0);
stk.delay(1);
/* ADC DMA function enable */
adc_dma_mode_enable(ADC0);
}
11 changes: 7 additions & 4 deletions firmware/generic/src/drivers/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#define __adc_h
extern "C"
{
#include <gd32vf103.h>
#include "drivers/SysTick.h"
#include <gd32vf103.h>
}

#define MAX_AN 5 // number of analog filters
Expand All @@ -11,12 +12,14 @@ class adc
{
public:
adc(){};
void init(SysTick &stk) ; // initialise ADC
uint16_t anRaw[8]; // raw sampled values stored by DMA
int anAvg[MAX_AN]; // analog average
uint8_t anCount; // counter for oversampling (new value after 255, @44,4KHz => 173Hz, suitable for potentiometers)
void update(uint8_t ch);
int anAvg[MAX_AN]; // analog average
uint8_t anCount; // counter for oversampling (new value after 255, @44,4KHz => 173Hz, suitable for potentiometers)
void update() ; // update ADC

private:
uint32_t anTotal[MAX_AN]; // the running total
void updateAVG(uint8_t ch) ;
};
#endif
29 changes: 29 additions & 0 deletions firmware/generic/src/drivers/dac.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "dac.h"
extern "C"
{
#include <gd32vf103.h>
}

void dac::dac_out(uint16_t d0, uint16_t d1)
{
dac_data_set(DAC0, DAC_ALIGN_12B_R, 4095 - d0); // inverted due to inverting op amp
dac_data_set(DAC1, DAC_ALIGN_12B_R, 4095 - d1); // inverted due to inverting op amp
dac_software_trigger_enable(DAC0);
dac_software_trigger_enable(DAC1);
}

void dac::init(void)
{
dac_deinit();
dac_trigger_source_config(DAC0, DAC_TRIGGER_SOFTWARE);
dac_trigger_enable(DAC0);
dac_wave_mode_config(DAC0, DAC_WAVE_DISABLE);
dac_output_buffer_enable(DAC0);
dac_enable(DAC0);

dac_trigger_source_config(DAC1, DAC_TRIGGER_SOFTWARE);
dac_trigger_enable(DAC1);
dac_wave_mode_config(DAC1, DAC_WAVE_DISABLE);
dac_output_buffer_enable(DAC1);
dac_enable(DAC1);
}
14 changes: 14 additions & 0 deletions firmware/generic/src/drivers/dac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __dac_h
#define __dac_h
extern "C"
{
#include <gd32vf103.h>
#include "drivers/display.h"
}

class dac {
public:
void init(void) ;
void dac_out(uint16_t d0, uint16_t d1) ;
};
#endif
40 changes: 30 additions & 10 deletions firmware/generic/src/drivers/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#include "display.h"
#include "spi.h"
#include "SysTick.h"
#include "res/font.h"
extern "C"
{
#include "gd32vf103.h"
#include "gd32vf103.h"

//extern "C" const uint8_t font[1520];
}
extern SysTick stk;
/*
Expand All @@ -29,7 +32,7 @@ extern SysTick stk;
RST PB1
CS PB2
*/
void display::begin(spi *SPI, const uint8_t *font)
void display::begin(spi *SPI)
{

this->SPI = SPI; // remember the SPI interface
Expand Down Expand Up @@ -193,12 +196,11 @@ void display::putChar(char ch, uint8_t x, uint8_t y, uint16_t txtFg, uint16_t tx
if ((' ' <= ch) && (ch <= '~')) // 7bit ASCII
{
openAperture(x, y, x + 8 - 1, y + 16 - 1);
ch += 4;
const uint8_t *charFont = _font + (uint8_t)ch * 16;
for (uint8_t y = 0; y < 16; ++y)
{
for (uint8_t x = 0, c = charFont[y]; x < 8; ++x, c >>= 1)
{
ch -= 32; // why this offset ...
const uint8_t *charFont = &font[(uint8_t)ch * 16];

for (uint8_t y = 0; y < 16; ++y) {
for (uint8_t x = 0, c = charFont[y]; x < 8; ++x, c >>= 1) {
uint16_t rgb = (c & 0x01) ? txtFg : txtBg;
writeData16(rgb);
}
Expand Down Expand Up @@ -231,13 +233,15 @@ void display::putImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height,
}
}
}

void display::drawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t Colour)
{
drawLine(x, y, x + w, y, Colour);
drawLine(x, y, x, y + h, Colour);
drawLine(x + w, y, x + w, y + h, Colour);
drawLine(x, y + h, x + w, y + h, Colour);
}

void display::fillRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t Colour)
{
openAperture(x, y, x + width - 1, y + height - 1);
Expand All @@ -249,6 +253,7 @@ void display::fillRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t hei
}
}
}

void display::drawCircle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t Colour)
{
// Reference : https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
Expand Down Expand Up @@ -292,6 +297,7 @@ void display::drawCircle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t Col
}
}
}

void display::fillCircle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t Colour)
{
// Reference : https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
Expand Down Expand Up @@ -333,6 +339,7 @@ void display::fillCircle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t Col
}
}
}

void display::drawLineLowSlope(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t Colour)
{
// Reference : https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
Expand Down Expand Up @@ -386,6 +393,7 @@ void display::drawLineHighSlope(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t
D = D + 2 * dx;
}
}

void display::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t Colour)
{
// Reference : https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
Expand Down Expand Up @@ -457,55 +465,66 @@ void display::RSLow()
{ // drive D/C pin low
gpio_bit_reset(GPIOA, GPIO_PIN_11);
}

void display::RSHigh()
{ // drive D/C pin high
gpio_bit_set(GPIOA, GPIO_PIN_11);
}

void display::RSTLow()
{ // Drive reset low
gpio_bit_reset(GPIOA, GPIO_PIN_8);
}

void display::RSTHigh()
{ // Drive reset high
gpio_bit_set(GPIOA, GPIO_PIN_8);
}

void display::CSLow()
{ // Drive chip select low
// gpio_bit_reset(GPIOB,GPIO_PIN_2);
}

void display::CSHigh()
{ // Drive chip select high
// gpio_bit_set(GPIOB,GPIO_PIN_2);
}

void display::BLKLow()
{ // Drive backlight low (off)
gpio_bit_reset(GPIOB, GPIO_PIN_10);
}

void display::BLKHigh()
{ // Drive backlight high (on)
gpio_bit_set(GPIOB, GPIO_PIN_10);
}

void display::writeCommand(uint8_t Cmd)
{
RSLow();
CSLow();
SPI->writeData8(Cmd);
CSHigh();
}

void display::writeData8(uint8_t Data)
{
RSHigh();
CSLow();
SPI->writeData8(Data);
CSHigh();
}

void display::writeData16(uint16_t Data)
{
RSHigh();
CSLow();
SPI->writeData16(Data);
CSHigh();
}

void display::exchangeXY()
{
writeCommand(0x36);
Expand All @@ -520,6 +539,7 @@ void display::exchangeXY()
writeData8(0x00);
}
}

uint32_t display::getWidth()
{
if (!XYSwapped)
Expand All @@ -531,6 +551,7 @@ uint32_t display::getWidth()
return SCREEN_HEIGHT;
}
}

uint32_t display::getHeight()
{
if (!XYSwapped)
Expand All @@ -543,8 +564,7 @@ uint32_t display::getHeight()
}
}

void display::showSprite(const uint8_t *rle_data, uint16_t size)
{
void display::showSprite(const uint8_t *rle_data, uint16_t size) {
const uint8_t *__ip, *__il, *__rd;
__ip = (rle_data);
__il = __ip + (size)*2;
Expand Down
6 changes: 4 additions & 2 deletions firmware/generic/src/drivers/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
// Format of colour value: <BGND 1 bit><Red 5 bits><Green 5 bits><Blue 5 bits>
//#define RGBToWord( R, G, B) ( ((G&0xf8) << (11-3)) | ((R&0xfc) << (5-2)) | ((B&0xf8)>>3) )
#define RGBToWord( R, G, B) ( ((B&0xf8) << (11-3)) | ((G&0xfc) << (5-2)) | ((R&0xf8)>>3) )

class display
{
public:
display(){};
void begin(spi *SPI, const uint8_t *font);
//void begin(spi *SPI, const uint8_t *font);
void begin(spi *SPI) ;
void showImg(const uint8_t *img, uint16_t size, uint8_t x, uint8_t y, uint16_t width, uint16_t height, uint16_t color);
void putChar(char ch, uint8_t x, uint8_t y, uint16_t txtFg, uint16_t txtBg);
void putStr(const char *str, uint8_t x, uint8_t y, uint16_t txtFg, uint16_t txtBg);
Expand Down Expand Up @@ -53,6 +55,6 @@ class display
}
spi *SPI;
int XYSwapped;
const uint8_t *_font ; // ' ' .. '~'
//const uint8_t *font ; // ' ' .. '~'
};
#endif
Loading