aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/STM32F4xx/EICU/main.c
blob: 4b3d72cd5a010845b5a26054a215f3dd05dddc80 (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
/*
    ChibiOS/RT - Copyright (C) 2014 Uladzimir Pylinsky aka barthess

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/*
 * Make following connections on your discovery board:
 * PD13-PB4
 * PD12-PB5
 * PD14-PB0
 * PD15-PB1
 */

#include "ch.h"
#include "hal.h"
#include "stdlib.h"

typedef struct {
  systime_t high;
  systime_t low;
  uint32_t pad;
} pulse_t;

/*
 * Chose values suitable for measurement using 16-bit timer on 1MHz
 */
static pulse_t pulse_led3 = {MS2ST(2), MS2ST(59), GPIOD_LED3};
static pulse_t pulse_led4 = {MS2ST(3), MS2ST(53), GPIOD_LED4};
static pulse_t pulse_led5 = {MS2ST(5), MS2ST(47), GPIOD_LED5};
static pulse_t pulse_led6 = {MS2ST(7), MS2ST(43), GPIOD_LED6};

/*
 *
 */
static THD_WORKING_AREA(PulseThreadWA_LED3, 128);
static THD_WORKING_AREA(PulseThreadWA_LED4, 128);
static THD_WORKING_AREA(PulseThreadWA_LED5, 128);
static THD_WORKING_AREA(PulseThreadWA_LED6, 128);

static THD_FUNCTION(PulseThread, arg) {
  chRegSetThreadName("Pulse");
  pulse_t *pulse = arg;

  systime_t t = chVTGetSystemTimeX();

  while (!chThdShouldTerminateX()) {
    t += pulse->high;
    palSetPad(GPIOD, pulse->pad);
    chThdSleepUntil(t);
    palClearPad(GPIOD, pulse->pad);
    t+= pulse->low;
    chThdSleepUntil(t);
  }

  chThdExit(MSG_OK);
}

static const int32_t tolerance = 20; // uS
void eicu_cb(EICUDriver *eicup, eicuchannel_t channel, uint32_t w, uint32_t p) {
  (void)eicup;
  (void)p;
  switch (channel) {
  case EICU_CHANNEL_1:
    if (abs((int32_t)w - (int32_t)ST2US(pulse_led3.high)) > tolerance)
      osalSysHalt("ch1");
    break;
  case EICU_CHANNEL_2:
    if (abs((int32_t)w - (int32_t)ST2US(pulse_led4.high)) > tolerance)
      osalSysHalt("ch2");
    break;
  case EICU_CHANNEL_3:
    if (abs((int32_t)w - (int32_t)ST2US(pulse_led5.high)) > tolerance)
      osalSysHalt("ch3");
    break;
  case EICU_CHANNEL_4:
    if (abs((int32_t)w - (int32_t)ST2US(pulse_led6.high)) > tolerance)
      osalSysHalt("ch4");
    break;
  default:
    osalSysHalt("unhandled case");
    break;
  }
}

static const EICUChannelConfig led3cfg = {
    EICU_INPUT_ACTIVE_HIGH,
    EICU_INPUT_PULSE,
    eicu_cb
};

static const EICUConfig eicucfg = {
    1000000,    /* EICU clock frequency in Hz.*/
    {
        &led3cfg,
        &led3cfg,
        &led3cfg,
        &led3cfg
    },
    0
};

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  palSetPadMode(GPIOB, GPIOB_PIN0, PAL_MODE_ALTERNATE(2) | PAL_STM32_PUDR_FLOATING);
  palSetPadMode(GPIOB, GPIOB_PIN1, PAL_MODE_ALTERNATE(2) | PAL_STM32_PUDR_FLOATING);
  palSetPadMode(GPIOB, GPIOB_PIN4, PAL_MODE_ALTERNATE(2) | PAL_STM32_PUDR_FLOATING);
  palSetPadMode(GPIOB, GPIOB_PIN5, PAL_MODE_ALTERNATE(2) | PAL_STM32_PUDR_FLOATING);

  eicuStart(&EICUD3, &eicucfg);
  eicuEnable(&EICUD3);

  osalThreadSleepMicroseconds(10); // need to stabilize input puns

  chThdCreateStatic(PulseThreadWA_LED3, sizeof(PulseThreadWA_LED3),
                    NORMALPRIO+1, PulseThread, &pulse_led3);
  chThdCreateStatic(PulseThreadWA_LED4, sizeof(PulseThreadWA_LED4),
                    NORMALPRIO+1, PulseThread, &pulse_led4);
  chThdCreateStatic(PulseThreadWA_LED5, sizeof(PulseThreadWA_LED5),
                    NORMALPRIO+1, PulseThread, &pulse_led5);
  chThdCreateStatic(PulseThreadWA_LED6, sizeof(PulseThreadWA_LED6),
                    NORMALPRIO+1, PulseThread, &pulse_led6);

  while (true) {
    osalThreadSleepMilliseconds(500);
  }

  return 0;
}