aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.cpp
blob: eb77a5d281e319ee8affa21cbb00850c275f0e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "ch.h"
#include "hal.h"
#include "usbcfg.h"

class dDAC
{
public:
    constexpr dDAC(DACDriver& driver, const DACConfig& config) :
        m_driver(&driver), m_config(config) {}

    void init() {
        dacStart(m_driver, &m_config);
    }

    void writeX(unsigned int channel, uint16_t value) {
        if (channel < 2)
            dacPutChannelX(m_driver, channel, value);
    }

private:
    DACDriver *m_driver;
    DACConfig m_config;
};

//static const DACConversionGroup dacGroupConfig = {
//  .num_channels = 1,
//  .end_cb       = NULL,
//  .error_cb     = NULL,
//  .trigger      = DAC_TRG(0)
//};

class dGPT {
public:
    constexpr dGPT(GPTDriver& driver, const GPTConfig& config) :
        m_driver(&driver), m_config(config) {}

    void init() {
        gptStart(m_driver, &m_config);
    }

    void startContinuous(unsigned int interval) {
        gptStartContinuous(m_driver, interval);
    }

    void stop() {
        gptStopTimer(m_driver);
    }

private:
    GPTDriver *m_driver;
    GPTConfig m_config;
};

static dGPT gpt (GPTD4, {
  .frequency    =  1000000,
  .callback     =  NULL,
  .cr2          =  TIM_CR2_MMS_1,   /* MMS = 010 = TRGO on Update Event.    */
  .dier         =  0
});

static const ADCConfig adcConfig = {
  .difsel = 0
};

volatile bool adcFinished = false;
void adcEndCallback(ADCDriver *adcd)
{
    (void)adcd;
    gpt.stop();
    adcFinished = true;
}

static const ADCConversionGroup adcGroupConfig = {
  .circular     = false,
  .num_channels = 1,
  .end_cb       = adcEndCallback,
  .error_cb     = NULL,
  .cfgr         = ADC_CFGR_EXTEN_RISING |
                  ADC_CFGR_EXTSEL_SRC(12),  /* TIM4_TRGO */
  .cfgr2        = 0,
  .tr1          = ADC_TR(0, 4095),
  .smpr         = {
    ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_247P5), 0
  },
  .sqr          = {
    ADC_SQR1_SQ1_N(ADC_CHANNEL_IN5),
    0, 0, 0
  }
};

#if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE)
#endif
adcsample_t samples[CACHE_SIZE_ALIGN(adcsample_t, 10)];

int main(void) {
	halInit();
	chSysInit();

	palSetPadMode(GPIOA, 5,  PAL_MODE_OUTPUT_PUSHPULL); // LED
	palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10));   // USB
	palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10));
    palSetPadMode(GPIOA, 0,  PAL_MODE_INPUT_ANALOG);    // Channel A in (1in5)

    palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);     // DAC out1, out2
    palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);

    gpt.init();

    //dDAC dac (DACD1, {
    //    .init         = 0,
    //    .datamode     = DAC_DHRM_12BIT_RIGHT,
    //    .cr           = 0
    //});

    //dac.init();
    //dac.writeX(0, 1024);

    adcStart(&ADCD1, &adcConfig);
    adcSTM32EnableVREF(&ADCD1);

	sduObjectInit(&SDU1);
	sduStart(&SDU1, &serusbcfg);
	usbDisconnectBus(serusbcfg.usbp);
	chThdSleepMilliseconds(1500);
	usbStart(serusbcfg.usbp, &usbcfg);
	usbConnectBus(serusbcfg.usbp);

	while (true) {
        if (SDU1.config->usbp->state == USB_ACTIVE) {
            BaseSequentialStream *bss = (BaseSequentialStream *)&SDU1;
            char c = 0;
            if (streamRead(bss, (uint8_t *)&c, 1) > 0 && c == 's') {
                adcFinished = false;
                adcStartConversion(&ADCD1, &adcGroupConfig, samples, 10);
                gpt.startContinuous(100);
                while (!adcFinished);
                for (int i = 0; i < 10; i++) {
                    uint8_t str[5] = {
                        static_cast<uint8_t>(samples[i] / 1000 % 10 + '0'),
                        static_cast<uint8_t>(samples[i] / 100 % 10 + '0'),
                        static_cast<uint8_t>(samples[i] / 10 % 10 + '0'),
                        static_cast<uint8_t>(samples[i] % 10 + '0'),
                        ' '
                    };
                    streamWrite(bss, str, 5);
                }
                streamWrite(bss, (uint8_t *)"\r\n", 2);
            }
        }
		chThdSleepMilliseconds(250);
	}
}