2010-11-15 19:44:09 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2012-01-21 14:29:42 +00:00
|
|
|
2011,2012 Giovanni Di Sirio.
|
2010-11-15 19:44:09 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
2010-11-21 13:45:22 +00:00
|
|
|
static void pwmpcb(PWMDriver *pwmp);
|
|
|
|
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);
|
2010-11-21 14:21:11 +00:00
|
|
|
static void spicb(SPIDriver *spip);
|
2010-11-21 13:45:22 +00:00
|
|
|
|
2010-11-21 14:21:11 +00:00
|
|
|
/* Total number of channels to be sampled by a single ADC operation.*/
|
2010-11-21 13:45:22 +00:00
|
|
|
#define ADC_GRP1_NUM_CHANNELS 2
|
2010-11-21 14:21:11 +00:00
|
|
|
|
|
|
|
/* Depth of the conversion buffer, channels are sampled four times each.*/
|
2010-11-21 13:45:22 +00:00
|
|
|
#define ADC_GRP1_BUF_DEPTH 4
|
|
|
|
|
|
|
|
/*
|
2010-12-24 20:49:28 +00:00
|
|
|
* ADC samples buffer.
|
2010-11-21 13:45:22 +00:00
|
|
|
*/
|
2010-11-21 14:21:11 +00:00
|
|
|
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
|
2010-11-21 13:45:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ADC conversion group.
|
|
|
|
* Mode: Linear buffer, 4 samples of 2 channels, SW triggered.
|
2010-11-23 16:16:53 +00:00
|
|
|
* Channels: IN10 (41.5 cycles sample time)
|
|
|
|
* Sensor (239.5 cycles sample time)
|
2010-11-21 13:45:22 +00:00
|
|
|
*/
|
|
|
|
static const ADCConversionGroup adcgrpcfg = {
|
|
|
|
FALSE,
|
|
|
|
ADC_GRP1_NUM_CHANNELS,
|
|
|
|
adccb,
|
2012-01-21 13:53:53 +00:00
|
|
|
NULL,
|
2010-11-23 16:16:53 +00:00
|
|
|
/* HW dependent part.*/
|
2010-11-21 13:45:22 +00:00
|
|
|
0,
|
2010-12-11 17:35:55 +00:00
|
|
|
ADC_CR2_TSVREFE,
|
2010-11-23 16:16:53 +00:00
|
|
|
ADC_SMPR1_SMP_AN10(ADC_SAMPLE_41P5) | ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_239P5),
|
2010-11-21 13:45:22 +00:00
|
|
|
0,
|
|
|
|
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
|
|
|
|
0,
|
2011-01-20 19:07:07 +00:00
|
|
|
ADC_SQR3_SQ2_N(ADC_CHANNEL_IN10) | ADC_SQR3_SQ1_N(ADC_CHANNEL_SENSOR)
|
2010-11-21 13:45:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2010-11-21 14:21:11 +00:00
|
|
|
* PWM configuration structure.
|
|
|
|
* Cyclic callback enabled, channels 3 and 4 enabled without callbacks,
|
2010-12-24 20:49:28 +00:00
|
|
|
* the active state is a logic one.
|
2010-11-21 13:45:22 +00:00
|
|
|
*/
|
|
|
|
static PWMConfig pwmcfg = {
|
2012-04-01 09:13:04 +00:00
|
|
|
10000, /* 10kHz PWM clock frequency. */
|
2011-03-31 12:35:42 +00:00
|
|
|
10000, /* PWM period 1S (in ticks). */
|
2010-11-21 13:45:22 +00:00
|
|
|
pwmpcb,
|
|
|
|
{
|
|
|
|
{PWM_OUTPUT_DISABLED, NULL},
|
|
|
|
{PWM_OUTPUT_DISABLED, NULL},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, NULL}
|
|
|
|
},
|
2010-11-23 16:16:53 +00:00
|
|
|
/* HW dependent part.*/
|
2011-04-01 13:06:44 +00:00
|
|
|
0,
|
|
|
|
#if STM32_PWM_USE_ADVANCED
|
2010-11-21 13:45:22 +00:00
|
|
|
0
|
2011-04-01 13:06:44 +00:00
|
|
|
#endif
|
2010-11-21 13:45:22 +00:00
|
|
|
};
|
|
|
|
|
2010-11-21 14:21:11 +00:00
|
|
|
/*
|
|
|
|
* SPI configuration structure.
|
|
|
|
* Maximum speed (12MHz), CPHA=0, CPOL=0, 16bits frames, MSb transmitted first.
|
|
|
|
* The slave select line is the pin GPIOA_SPI1NSS on the port GPIOA.
|
|
|
|
*/
|
|
|
|
static const SPIConfig spicfg = {
|
|
|
|
spicb,
|
2010-11-23 16:16:53 +00:00
|
|
|
/* HW dependent part.*/
|
2010-11-21 14:21:11 +00:00
|
|
|
GPIOA,
|
|
|
|
GPIOA_SPI1NSS,
|
|
|
|
SPI_CR1_DFF
|
|
|
|
};
|
|
|
|
|
2010-11-21 13:45:22 +00:00
|
|
|
/*
|
2010-11-26 19:24:34 +00:00
|
|
|
* PWM cyclic callback.
|
|
|
|
* A new ADC conversion is started.
|
2010-11-21 13:45:22 +00:00
|
|
|
*/
|
|
|
|
static void pwmpcb(PWMDriver *pwmp) {
|
2010-11-22 17:35:15 +00:00
|
|
|
|
2010-11-22 17:54:09 +00:00
|
|
|
(void)pwmp;
|
|
|
|
|
2010-11-21 13:45:22 +00:00
|
|
|
/* Starts an asynchronous ADC conversion operation, the conversion
|
|
|
|
will be executed in parallel to the current PWM cycle and will
|
|
|
|
terminate before the next PWM cycle.*/
|
2010-11-22 17:50:39 +00:00
|
|
|
chSysLockFromIsr();
|
2010-11-21 14:21:11 +00:00
|
|
|
adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
|
2010-11-21 13:45:22 +00:00
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
|
|
|
|
2010-11-15 19:44:09 +00:00
|
|
|
/*
|
2010-11-21 14:21:11 +00:00
|
|
|
* ADC end conversion callback.
|
2010-11-26 19:24:34 +00:00
|
|
|
* The PWM channels are reprogrammed using the latest ADC samples.
|
2010-12-24 20:49:28 +00:00
|
|
|
* The latest samples are transmitted into a single SPI transaction.
|
2010-11-21 13:45:22 +00:00
|
|
|
*/
|
|
|
|
void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
|
|
|
|
|
2010-11-21 14:21:11 +00:00
|
|
|
(void) buffer; (void) n;
|
|
|
|
/* Note, only in the ADC_COMPLETE state because the ADC driver fires an
|
|
|
|
intermediate callback when the buffer is half full.*/
|
2011-03-31 12:35:42 +00:00
|
|
|
if (adcp->state == ADC_COMPLETE) {
|
2010-11-22 17:50:39 +00:00
|
|
|
adcsample_t avg_ch1, avg_ch2;
|
|
|
|
|
|
|
|
/* Calculates the average values from the ADC samples.*/
|
|
|
|
avg_ch1 = (samples[0] + samples[2] + samples[4] + samples[6]) / 4;
|
|
|
|
avg_ch2 = (samples[1] + samples[3] + samples[5] + samples[7]) / 4;
|
|
|
|
|
2010-11-21 14:21:11 +00:00
|
|
|
chSysLockFromIsr();
|
2010-11-22 17:50:39 +00:00
|
|
|
|
|
|
|
/* Changes the channels pulse width, the change will be effective
|
|
|
|
starting from the next cycle.*/
|
|
|
|
pwmEnableChannelI(&PWMD3, 2, PWM_FRACTION_TO_WIDTH(&PWMD3, 4096, avg_ch1));
|
|
|
|
pwmEnableChannelI(&PWMD3, 3, PWM_FRACTION_TO_WIDTH(&PWMD3, 4096, avg_ch2));
|
|
|
|
|
|
|
|
/* SPI slave selection and transmission start.*/
|
2010-11-21 14:21:11 +00:00
|
|
|
spiSelectI(&SPID1);
|
|
|
|
spiStartSendI(&SPID1, ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH, samples);
|
2010-11-22 17:50:39 +00:00
|
|
|
|
2010-11-21 14:21:11 +00:00
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-12-24 20:49:28 +00:00
|
|
|
* SPI end transfer callback.
|
2010-11-21 14:21:11 +00:00
|
|
|
*/
|
|
|
|
static void spicb(SPIDriver *spip) {
|
|
|
|
|
|
|
|
/* On transfer end just releases the slave select line.*/
|
|
|
|
chSysLockFromIsr();
|
|
|
|
spiUnselectI(spip);
|
|
|
|
chSysUnlockFromIsr();
|
2010-11-21 13:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-22 17:57:16 +00:00
|
|
|
* This is a periodic thread that does absolutely nothing except increasing
|
|
|
|
* a seconds counter.
|
2010-11-15 19:44:09 +00:00
|
|
|
*/
|
|
|
|
static WORKING_AREA(waThread1, 128);
|
|
|
|
static msg_t Thread1(void *arg) {
|
2010-11-21 14:21:11 +00:00
|
|
|
static uint32_t seconds_counter;
|
2010-11-15 19:44:09 +00:00
|
|
|
|
|
|
|
(void)arg;
|
2011-07-10 06:50:12 +00:00
|
|
|
chRegSetThreadName("counter");
|
2010-11-15 19:44:09 +00:00
|
|
|
while (TRUE) {
|
2010-11-21 14:21:11 +00:00
|
|
|
chThdSleepMilliseconds(1000);
|
|
|
|
seconds_counter++;
|
2010-11-15 19:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-12-19 09:13:54 +00:00
|
|
|
* Application entry point.
|
2010-11-15 19:44:09 +00:00
|
|
|
*/
|
2010-12-21 10:30:39 +00:00
|
|
|
int main(void) {
|
2010-11-15 19:44:09 +00:00
|
|
|
|
2010-12-19 09:13:54 +00:00
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
|
2010-11-15 19:44:09 +00:00
|
|
|
/*
|
|
|
|
* Activates the serial driver 1 using the driver default configuration.
|
|
|
|
*/
|
|
|
|
sdStart(&SD1, NULL);
|
|
|
|
|
2010-11-27 10:07:20 +00:00
|
|
|
/*
|
|
|
|
* If the user button is pressed after the reset then the test suite is
|
|
|
|
* executed immediately before activating the various device drivers in
|
2010-12-24 20:49:28 +00:00
|
|
|
* order to not alter the benchmark scores.
|
2010-11-27 10:07:20 +00:00
|
|
|
*/
|
|
|
|
if (palReadPad(GPIOA, GPIOA_BUTTON))
|
|
|
|
TestThread(&SD1);
|
|
|
|
|
2010-11-21 14:21:11 +00:00
|
|
|
/*
|
2010-12-24 20:49:28 +00:00
|
|
|
* Initializes the SPI driver 1.
|
2010-11-21 14:21:11 +00:00
|
|
|
*/
|
|
|
|
spiStart(&SPID1, &spicfg);
|
|
|
|
|
2010-12-24 21:07:11 +00:00
|
|
|
/*
|
|
|
|
* Initializes the ADC driver 1.
|
|
|
|
* The pin PC0 on the port GPIOC is programmed as analog input.
|
|
|
|
*/
|
|
|
|
adcStart(&ADCD1, NULL);
|
2011-12-31 10:07:43 +00:00
|
|
|
palSetGroupMode(GPIOC, PAL_PORT_BIT(0), 0, PAL_MODE_INPUT_ANALOG);
|
2010-12-24 21:07:11 +00:00
|
|
|
|
2010-11-21 13:45:22 +00:00
|
|
|
/*
|
|
|
|
* Initializes the PWM driver 1, re-routes the TIM3 outputs, programs the
|
2010-11-25 18:32:45 +00:00
|
|
|
* pins as alternate functions.
|
2010-11-21 13:45:22 +00:00
|
|
|
* Note, the AFIO access routes the TIM3 output pins on the PC6...PC9
|
|
|
|
* where the LEDs are connected.
|
|
|
|
*/
|
|
|
|
pwmStart(&PWMD3, &pwmcfg);
|
|
|
|
AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_0 | AFIO_MAPR_TIM3_REMAP_1;
|
|
|
|
palSetGroupMode(GPIOC, PAL_PORT_BIT(GPIOC_LED3) | PAL_PORT_BIT(GPIOC_LED4),
|
2011-12-31 10:07:43 +00:00
|
|
|
0,
|
2010-11-21 13:45:22 +00:00
|
|
|
PAL_MODE_STM32_ALTERNATE_PUSHPULL);
|
|
|
|
|
2010-11-15 19:44:09 +00:00
|
|
|
/*
|
2010-11-21 14:21:11 +00:00
|
|
|
* Creates the example thread.
|
2010-11-15 19:44:09 +00:00
|
|
|
*/
|
|
|
|
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normal main() thread activity, in this demo it does nothing except
|
2010-11-22 18:05:35 +00:00
|
|
|
* sleeping in a loop and check the button state, when the button is
|
|
|
|
* pressed the test procedure is launched with output on the serial
|
|
|
|
* driver 1.
|
2010-11-15 19:44:09 +00:00
|
|
|
*/
|
|
|
|
while (TRUE) {
|
2010-11-21 19:10:46 +00:00
|
|
|
if (palReadPad(GPIOA, GPIOA_BUTTON))
|
2010-11-15 19:44:09 +00:00
|
|
|
TestThread(&SD1);
|
|
|
|
chThdSleepMilliseconds(500);
|
|
|
|
}
|
|
|
|
}
|