/* Name: usbconfig.h * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers * Author: Christian Starkjohann * Creation Date: 2005-04-01 * Tabsize: 4 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) * This Revision: $Id: usbconfig-prototype.h 785 2010-05-30 17:57:07Z cs $ */ #pragma once #include "config.h" /* General Description: This file is an example configuration (with inline documentation) for the USB driver. It configures V-USB for USB D+ connected to Port D bit 2 (which is also hardware interrupt 0 on many devices) and USB D- to Port D bit 4. You may wire the lines to any other port, as long as D+ is also wired to INT0 (or any other hardware interrupt, as long as it is the highest level interrupt, see section at the end of this file). */ /* ---------------------------- Hardware Config ---------------------------- */ #define USB_CFG_IOPORTNAME D /* This is the port where the USB bus is connected. When you configure it to * "B", the registers PORTB, PINB and DDRB will be used. */ #define USB_CFG_DMINUS_BIT 3 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected. * This may be any bit in the port. */ #define USB_CFG_DPLUS_BIT 2 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected. * This may be any bit in the port. Please note that D+ must also be connected * to interrupt pin INT0! [You can also use other interrupts, see section * "Optional MCU Description" below, or you can connect D- to the interrupt, as * it is required if you use the USB_COUNT_SOF feature. If you use D- for the * interrupt, the USB interrupt will also be triggered at Start-Of-Frame * markers every millisecond.] */ #define USB_CFG_CLOCK_KHZ (F_CPU/1000) /* Clock rate of the AVR in kHz. Legal values are 12000, 12800, 15000, 16000, * 16500, 18000 and 20000. The 12.8 MHz and 16.5 MHz versions of the code * require no crystal, they tolerate +/- 1% deviation from the nominal * frequency. All other rates require a precision of 2000 ppm and thus a * crystal! * Since F_CPU should be defined to your actual clock rate anyway, you should * not need to modify this setting. */ #define USB_CFG_CHECK_CRC 0 /* Define this to 1 if you want that the driver checks integrity of incoming * data packets (CRC checks). CRC checks cost quite a bit of code size and are * currently only available for 18 MHz crystal clock. You must choose * USB_CFG_CLOCK_KHZ = 18000 if you enable this option. */ /* ----------------------- Optional Hardware Config ------------------------ */ /* #define USB_CFG_PULLUP_IOPORTNAME D */ /* If you connect the 1.5k pullup resistor from D- to a port pin instead of * V+, you can connect and disconnect the device from firmware by calling * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h). * This constant defines the port on which the pullup resistor is connected. */ /* #define USB_CFG_PULLUP_BIT 4 */ /* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined * above) where the 1.5k pullup resistor is connected. See description * above for details. */ /* --------------------------- Functional Range ---------------------------- */ #define USB_CFG_HAVE_INTRIN_ENDPOINT 1 /* Define this to 1 if you want to compile a version with two endpoints: The * default control endpoint 0 and an interrupt-in endpoint (any other endpoint * number). */ #define USB_CFG_HAVE_INTRIN_ENDPOINT3 1 /* Define this to 1 if you want to compile a version with three endpoints: The * default control endpoint 0, an interrupt-in endpoint 3 (or the number * configured below) and a catch-all default interrupt-in endpoint as above. * You must also define USB_CFG_HAVE_INTRIN_ENDPOINT to 1 for this feature. */ #define USB_CFG_EP3_NUMBER 3 /* If the so-called endpoint 3 is used, it can now be configured to any other * endpoint number (except 0) with this macro. Default if undefined is 3. */ /* #define USB_INITIAL_DATATOKEN USBPID_DATA1 */ /* The above macro defines the startup condition for data toggling on the * interrupt/bulk endpoints 1 and 3. Defaults to USBPID_DATA1. * Since the token is toggled BEFORE sending any data, the first packet is * sent with the oposite value of this configuration! */ #define USB_CFG_IMPLEMENT_HALT 0 /* Define this to 1 if you also want to implement the ENDPOINT_HALT feature * for endpoint 1 (interrupt endpoint). Although you may not need this feature, * it is required by the standard. We have made it a config option because it * bloats the code considerably. */ #define USB_CFG_SUPPRESS_INTR_CODE 0 /* Define this to 1 if you want to declare interrupt-in endpoints, but don't * want to send any data over them. If this macro is defined to 1, functions * usbSetInterrupt() and usbSetInterrupt3() are omitted. This is useful if * you need the interrupt-in endpoints in order to comply to an interface * (e.g. HID), but never want to send any data. This option saves a couple * of bytes in flash memory and the transmit buffers in RAM. */ #define USB_CFG_IS_SELF_POWERED 0 /* Define this to 1 if the device has its own power supply. Set it to 0 if the * device is powered from the USB bus. */ #define USB_CFG_IMPLEMENT_FN_WRITE 1 /* Set this to 1 if you want usbFunctionWrite() to be called for control-out * transfers. Set it to 0 if you don't need it and want to save a couple of * bytes. */ #define USB_CFG_IMPLEMENT_FN_READ 0 /* Set this to 1 if you need to send control replies which are generated * "on the fly" when usbFunctionRead() is called. If you only want to send * data from a static buffer, set it to 0 and return the data from * usbFunctionSetup(). This saves a couple of bytes. */ #define USB_CFG_IMPLEMENT_FN_WRITEOUT 0 /* Define this to 1 if you want to use interrupt-out (or bulk out) endpoints. * You must implement the function usbFunctionWriteOut() which receives all * interrupt/bulk data sent to any endpoint other than 0. The endpoint number * can be found in 'usbRxToken'. */ #define USB_CFG_HAVE_FLOWCONTROL 0 /* Define this to 1 if you want flowcontrol over USB data. See the definition * of the macros usbDisableAllRequests() and usbEnableAllRequests() in * usbdrv.h. */ #define USB_CFG_DRIVER_FLASH_PAGE 0 /* If the device has more than 64 kBytes of flash, define this to the 64 k page * where the driver's constants (descriptors) are located. Or in other words: * Define this to 1 for boot loaders on the ATMega128. */ #define USB_CFG_LONG_TRANSFERS 0 /* Define this to 1 if you want to send/receive blocks of more than 254 bytes * in a single control-in or control-out transfer. Note that the capability * for long transfers increases the driver size. */ /* #define USB_RX_USER_HOOK(data, len) if(usbRxToken == (uchar)USBPID_SETUP) blinkLED(); */ /* This macro is a hook if you want to do unconventional things. If it is * defined, it's inserted at the beginning of received message processing. * If you eat the received message and don't want default processing to * proceed, do a return after doing your things. One possible application * (besides debugging) is to flash a status LED on each packet. */ /* #define USB_RESET_HOOK(resetStarts) if(!resetStarts){hadUsbReset();} */ /* This macro is a hook if you need to know when an USB RESET occurs. It has * one parameter which distinguishes between the start of RESET state and its * end. */ /* #define USB_SET_ADDRESS_HOOK() hadAddressAssigned(); */ /* This macro (if defined) is executed when a USB SET_ADDRESS request was * received. */ #define USB_COUNT_SOF 1 /* define this macro to 1 if you need the global variable "usbSofCount" which * counts SOF packets. This feature requires that the hardware interrupt is * connected to D- instead of D+. */ /* #ifdef __ASSEMBLER__ * macro myAssemblerMacro * in YL, TCNT0 * sts timer0Snapshot, YL * endm * #endif * #define USB_SOF_HOOK myAssemblerMacro * This macro (if defined) is executed in the assembler module when a * Start Of Frame condition is detected. It is recommended to define it to * the name of an assembler macro which is defined here as well so that more * than one assembler instruction can be used. The macro may use the register * YL and modify SREG. If it lasts longer than a couple of cycles, USB messages * immediately after an SOF pulse may be lost and must be retried by the host. * What can you do with this hook? Since the SOF si
/*
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
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.
*/
#include "ch.h"
#include "hal.h"
#define ADC_GRP1_NUM_CHANNELS 1
#define ADC_GRP1_BUF_DEPTH 8
#define ADC_GRP2_NUM_CHANNELS 1
#define ADC_GRP2_BUF_DEPTH 32
static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
static adcsample_t samples2[ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH];
/*
* ADC streaming callback.
*/
size_t nx = 0, ny = 0;
static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
(void)adcp;
if (samples2 == buffer) {
nx += n;
}
else {
ny += n;
}
}
static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {
(void)adcp;
(void)err;
}
/*
* SDADC configuration.
*/
static const ADCConfig sdadc_config = {
0,
{
SDADC_CONFR_GAIN_1X | SDADC_CONFR_SE_ZERO_VOLT | SDADC_CONFR_COMMON_VSSSD,
0,
0
}
};
/*
* ADC conversion group.
* Mode: Linear buffer, 8 samples of 1 channel, SW triggered.
* Channels: ADC_IN5P.
*/
static const ADCConversionGroup adcgrpcfg1 = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
NULL,
adcerrorcallback,
.u.sdadc = {
SDADC_CR2_JSWSTART, /* CR2 */
SDADC_JCHGR_CH(5), /* JCHGR */
{ /* CONFCHR[2]*/
SDADC_CONFCHR1_CH5(0),
0
}
}
};
/*
* ADC conversion group.
* Mode: Continuous, 32 samples of 1 channel, SW triggered.
* Channels: ADC_IN5P.
*/
static const ADCConversionGroup adcgrpcfg2 = {
TRUE,
ADC_GRP2_NUM_CHANNELS,
adccallback,
adcerrorcallback,
.u.sdadc = {
SDADC_CR2_JSWSTART, /* CR2 */
SDADC_JCHGR_CH(5), /* JCHGR */
{ /* CONFCHR[2]*/
SDADC_CONFCHR1_CH5(0),
0
}
}
};
/*
* Red LEDs blinker thread, times are in milliseconds.
*/
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {
(void)arg;
chRegSetThreadName("blinker");
while (true) {
palClearPad(GPIOC, GPIOC_LED1);
chThdSleepMilliseconds(500);
palSetPad(GPIOC, GPIOC_LED1);
chThdSleepMilliseconds(500);
}
}
/*
* 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();
/*
* Creates the blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/*
* Activates the SDADC1 driver.
*/
adcStart(&SDADCD1, &sdadc_config);
adcSTM32Calibrate(&SDADCD1);
/*
* Linear conversion.
*/
adcConvert(&SDADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
/*
* Starts an ADC continuous conversion.
*/
adcStartConversion(&SDADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);
/*
* Normal main() thread activity, in this demo it does nothing.
*/
while (true) {
if (palReadPad(GPIOA, GPIOA_WKUP_BUTTON)) {
adcStopConversion(&SDADCD1);
}
chThdSleepMilliseconds(500);
}
}