AVR: Cleanup source code.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11386 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
Theodore Ateba 2018-01-21 20:13:51 +00:00
parent f56b8b742b
commit b56329fd3d
13 changed files with 401 additions and 377 deletions

View File

@ -83,9 +83,9 @@ OSAL_IRQ_HANDLER(SPI_STC_vect) {
} }
#endif /* AVR_SPI_USE_SPI1 */ #endif /* AVR_SPI_USE_SPI1 */
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level SPI driver initialization. * @brief Low level SPI driver initialization.

View File

@ -31,14 +31,14 @@
#if HAL_USE_GPT || defined(__DOXYGEN__) #if HAL_USE_GPT || defined(__DOXYGEN__)
/*===========================================================================*/ /*==========================================================================*/
/* Driver local definitions. */ /* Driver local definitions. */
/*===========================================================================*/ /*==========================================================================*/
#define PRESCALER_SIZE_BASE 5 #define PRESCALER_SIZE_BASE 5
#define PRESCALER_SIZE_EXTENDED 7 #define PRESCALER_SIZE_EXTENDED 7
// FIXME: could use better names here! /* FIXME: could use better names here! */
typedef struct { typedef struct {
volatile uint8_t *tccra; volatile uint8_t *tccra;
volatile uint8_t *tccrb; volatile uint8_t *tccrb;
@ -68,9 +68,9 @@ const timer_registers_t regs_table[] = {
#endif #endif
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported variables. */ /* Driver exported variables. */
/*===========================================================================*/ /*==========================================================================*/
#if AVR_GPT_USE_TIM1 || defined(__DOXYGEN__) #if AVR_GPT_USE_TIM1 || defined(__DOXYGEN__)
GPTDriver GPTD1; GPTDriver GPTD1;
@ -88,19 +88,27 @@ GPTDriver GPTD4;
GPTDriver GPTD5; GPTDriver GPTD5;
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables. */ /* Driver local variables. */
/*===========================================================================*/ /*==========================================================================*/
static uint16_t ratio_base[] = { 1024, 256, 64, 8, 1 }; static uint16_t ratio_base[] = { 1024, 256, 64, 8, 1 };
static uint8_t clock_source_base[]= { 5, 4, 3, 2, 1 }; static uint8_t clock_source_base[]= { 5, 4, 3, 2, 1 };
//static uint16_t ratio_extended[] = { 1024, 256, 128, 64, 32, 8, 1 }; //static uint16_t ratio_extended[] = { 1024, 256, 128, 64, 32, 8, 1 };
//static uint8_t clock_source_extended[] = { 7, 6, 5, 4, 3, 2, 1 }; //static uint8_t clock_source_extended[] = { 7, 6, 5, 4, 3, 2, 1 };
/*===========================================================================*/ /*==========================================================================*/
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*==========================================================================*/
/**
* @brief TODO: Comment this function.
*
* @param[in] freq
* @param[in] ratio pointer to the ratio used to calculate the prescaler
* @param[in] n ....
* @return ....
*/
static uint8_t prescaler(uint16_t freq, uint16_t *ratio, uint8_t n) { static uint8_t prescaler(uint16_t freq, uint16_t *ratio, uint8_t n) {
uint8_t i; uint8_t i;
@ -112,9 +120,14 @@ static uint8_t prescaler(uint16_t freq, uint16_t *ratio, uint8_t n) {
if ((result * ratio[i] * freq) == F_CPU) if ((result * ratio[i] * freq) == F_CPU)
return i; return i;
} }
return -1; // TO check return -1;
} }
/**
* @brief TODO: Comment this function.
*
* @param[in] gptp pointer to the General Purpose Timer driver.
*/
static void gpt_lld_serve_interrupt(GPTDriver *gptp) { static void gpt_lld_serve_interrupt(GPTDriver *gptp) {
gptp->counter++; gptp->counter++;
@ -128,9 +141,19 @@ static void gpt_lld_serve_interrupt(GPTDriver *gptp) {
} }
} }
/**
* @brief GPT low level driver dummy callback.
*
* @param[in] gptp pointer to the General Purpose Timer driver.
*/
static void gpt_lld_dummy_callback(GPTDriver *gptp) { static void gpt_lld_dummy_callback(GPTDriver *gptp) {
} }
/**
* @brief Get the timer index.
*
* @param[in] gptp pointer to the General Purpose Timer driver.
*/
static uint8_t getTimerIndex(GPTDriver *gptp) { static uint8_t getTimerIndex(GPTDriver *gptp) {
uint8_t index = 0; uint8_t index = 0;
@ -155,7 +178,7 @@ static uint8_t getTimerIndex(GPTDriver *gptp) {
if (gptp == &GPTD5) return index; if (gptp == &GPTD5) return index;
else index++; else index++;
#endif #endif
return -1; // To check return -1;
} }
/*===========================================================================*/ /*===========================================================================*/
@ -232,9 +255,9 @@ OSAL_IRQ_HANDLER(TIMER5_COMPA_vect) {
} }
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level GPT driver initialization. * @brief Low level GPT driver initialization.

View File

@ -34,13 +34,13 @@
#include "avr_timers.h" #include "avr_timers.h"
/*===========================================================================*/ /*==========================================================================*/
/* Driver constants. */ /* Driver constants. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver pre-compile time settings. */ /* Driver pre-compile time settings. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief GPT1 driver enable switch. * @brief GPT1 driver enable switch.
@ -87,13 +87,13 @@
#define AVR_GPT_USE_TIM5 FALSE #define AVR_GPT_USE_TIM5 FALSE
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief GPT frequency type. * @brief GPT frequency type.
@ -121,7 +121,7 @@ typedef struct {
* @note This callback is invoked on GPT counter events. * @note This callback is invoked on GPT counter events.
*/ */
gptcallback_t callback; gptcallback_t callback;
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
} GPTConfig; } GPTConfig;
/** /**
@ -141,13 +141,13 @@ struct GPTDriver {
GPT_DRIVER_EXT_FIELDS GPT_DRIVER_EXT_FIELDS
#endif #endif
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
/** /**
* @brief input clock from prescaler * @brief input clock from prescaler.
*/ */
uint8_t clock_source; uint8_t clock_source;
/** /**
* @brief Lenght of the period in clock ticks * @brief Lenght of the period in clock ticks.
*/ */
gptcnt_t period; gptcnt_t period;
/** /**
@ -155,14 +155,14 @@ struct GPTDriver {
*/ */
gptcnt_t counter; gptcnt_t counter;
/** /**
* @brief Function called from the interrupt service routine * @brief Function called from the interrupt service routine.
*/ */
gptcallback_t callback; gptcallback_t callback;
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver macros. */ /* Driver macros. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Changes the interval of GPT peripheral. * @brief Changes the interval of GPT peripheral.
@ -178,12 +178,12 @@ struct GPTDriver {
* @notapi * @notapi
*/ */
// FIXME: placeholder to enable compile, should be implemented! /* FIXME: placeholder to enable compile, should be implemented! */
#define gpt_lld_change_interval(gptp, interval) #define gpt_lld_change_interval(gptp, interval)
/*===========================================================================*/ /*==========================================================================*/
/* External declarations. */ /* External declarations. */
/*===========================================================================*/ /*==========================================================================*/
#if AVR_GPT_USE_TIM1 || defined(__DOXYGEN__) #if AVR_GPT_USE_TIM1 || defined(__DOXYGEN__)
extern GPTDriver GPTD1; extern GPTDriver GPTD1;

View File

@ -26,10 +26,13 @@
#if HAL_USE_ICU || defined(__DOXYGEN__) #if HAL_USE_ICU || defined(__DOXYGEN__)
/*===========================================================================*/ /*==========================================================================*/
/* Driver local definitions. */ /* Driver local definitions. */
/*===========================================================================*/ /*==========================================================================*/
/**
* @brief ICU driver type.
*/
typedef struct { typedef struct {
volatile uint8_t *tccra; volatile uint8_t *tccra;
volatile uint8_t *tccrb; volatile uint8_t *tccrb;
@ -53,9 +56,9 @@ static icu_registers_t regs_table[]=
#endif #endif
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported variables. */ /* Driver exported variables. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief ICU1 driver identifier. * @brief ICU1 driver identifier.
@ -82,14 +85,15 @@ ICUDriver ICUD4;
ICUDriver ICUD5; ICUDriver ICUD5;
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables and types. */ /* Driver local variables and types. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*==========================================================================*/
/* TODO: Comment. */
static inline void handle_capture_isr(ICUDriver *icup, static inline void handle_capture_isr(ICUDriver *icup,
volatile uint16_t *icr, volatile uint16_t *icr,
volatile uint8_t *tccrb, volatile uint8_t *tccrb,
@ -107,11 +111,12 @@ static inline void handle_capture_isr(ICUDriver *icup,
icup->period = value; icup->period = value;
if (icup->config->period_cb != NULL) if (icup->config->period_cb != NULL)
icup->config->period_cb(icup); icup->config->period_cb(icup);
/* Reset counter at the end of every cycle */ /* Reset counter at the end of every cycle. */
*tcnt = 0; *tcnt = 0;
} }
} }
/* TODO: Comment. */
static uint8_t tmrIndex(ICUDriver *icup) { static uint8_t tmrIndex(ICUDriver *icup) {
uint8_t index = 0; uint8_t index = 0;
@ -134,9 +139,9 @@ static uint8_t tmrIndex(ICUDriver *icup) {
return 255; return 255;
} }
/*===========================================================================*/ /*==========================================================================*/
/* Driver interrupt handlers. */ /* Driver interrupt handlers. */
/*===========================================================================*/ /*==========================================================================*/
#if AVR_ICU_USE_TIM1 || defined(__DOXYGEN__) #if AVR_ICU_USE_TIM1 || defined(__DOXYGEN__)
OSAL_IRQ_HANDLER(TIMER1_CAPT_vect) { OSAL_IRQ_HANDLER(TIMER1_CAPT_vect) {
@ -202,9 +207,9 @@ OSAL_IRQ_HANDLER(TIMER5_OVF_vect) {
} }
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level ICU driver initialization. * @brief Low level ICU driver initialization.
@ -238,10 +243,10 @@ void icu_lld_start(ICUDriver *icup) {
if (icup->state == ICU_STOP) { if (icup->state == ICU_STOP) {
uint8_t i = tmrIndex(icup); uint8_t i = tmrIndex(icup);
/* Normal waveform generation (counts from 0 to 0xFFFF) */ /* Normal waveform generation (counts from 0 to 0xFFFF). */
*regs_table[i].tccra &= ~((1 << WGM11) | (1 << WGM10)); *regs_table[i].tccra &= ~((1 << WGM11) | (1 << WGM10));
*regs_table[i].tccrb &= ~((1 << WGM13) | (1 << WGM12)); *regs_table[i].tccrb &= ~((1 << WGM13) | (1 << WGM12));
/* Enable noise canceler, set prescale to CLK/1024 */ /* Enable noise canceler, set prescale to CLK/1024. */
*regs_table[i].tccrb |= (1 << ICNC1) | (1 << CS12) | (1 << CS10); *regs_table[i].tccrb |= (1 << ICNC1) | (1 << CS12) | (1 << CS10);
if (icup->config->mode == ICU_INPUT_ACTIVE_HIGH) if (icup->config->mode == ICU_INPUT_ACTIVE_HIGH)
*regs_table[i].tccrb |= (1 << ICES1); *regs_table[i].tccrb |= (1 << ICES1);
@ -260,9 +265,9 @@ void icu_lld_start(ICUDriver *icup) {
void icu_lld_stop(ICUDriver *icup) { void icu_lld_stop(ICUDriver *icup) {
if (icup->state == ICU_READY) { if (icup->state == ICU_READY) {
/* Resets the peripheral.*/ /* Resets the peripheral. */
/* Disables the peripheral.*/ /* Disables the peripheral. */
#if AVR_ICU_USE_TIM1 #if AVR_ICU_USE_TIM1
if (&ICUD1 == icup) { if (&ICUD1 == icup) {

View File

@ -29,13 +29,13 @@
#include "avr_timers.h" #include "avr_timers.h"
/*===========================================================================*/ /*==========================================================================*/
/* Driver constants. */ /* Driver constants. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver pre-compile time settings. */ /* Driver pre-compile time settings. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @name Configuration options * @name Configuration options
@ -71,13 +71,13 @@
#endif #endif
/** @} */ /** @} */
/*===========================================================================*/ /*==========================================================================*/
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief ICU driver mode. * @brief ICU driver mode.
@ -124,7 +124,7 @@ typedef struct {
* @brief Callback for timer overflow. * @brief Callback for timer overflow.
*/ */
icucallback_t overflow_cb; icucallback_t overflow_cb;
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
} ICUConfig; } ICUConfig;
/** /**
@ -142,7 +142,7 @@ struct ICUDriver {
#if defined(ICU_DRIVER_EXT_FIELDS) #if defined(ICU_DRIVER_EXT_FIELDS)
ICU_DRIVER_EXT_FIELDS ICU_DRIVER_EXT_FIELDS
#endif #endif
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
/** /**
* @brief Width value read by ISR. * @brief Width value read by ISR.
*/ */
@ -153,9 +153,9 @@ struct ICUDriver {
icucnt_t period; icucnt_t period;
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver macros. */ /* Driver macros. */
/*===========================================================================*/ /*==========================================================================*/
/** /**

View File

@ -31,9 +31,9 @@
#if HAL_USE_PWM || defined(__DOXYGEN__) #if HAL_USE_PWM || defined(__DOXYGEN__)
/*===========================================================================*/ /*==========================================================================*/
/* Driver local definitions. */ /* Driver local definitions. */
/*===========================================================================*/ /*==========================================================================*/
#define TIM1_CS_OFFSET CS10 #define TIM1_CS_OFFSET CS10
#define TIM1_WGM_OFFSET1 WGM10 #define TIM1_WGM_OFFSET1 WGM10
@ -82,9 +82,9 @@ static timer_registers_t regs_table[]=
#endif #endif
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported variables. */ /* Driver exported variables. */
/*===========================================================================*/ /*==========================================================================*/
/** @brief PWM driver identifiers.*/ /** @brief PWM driver identifiers.*/
#if AVR_PWM_USE_TIM1 || defined(__DOXYGEN__) #if AVR_PWM_USE_TIM1 || defined(__DOXYGEN__)
@ -103,13 +103,13 @@ PWMDriver PWMD4;
PWMDriver PWMD5; PWMDriver PWMD5;
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables. */ /* Driver local variables. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*==========================================================================*/
static void config_channel(volatile uint8_t *tccra, static void config_channel(volatile uint8_t *tccra,
uint8_t com1, uint8_t com1,
@ -118,9 +118,9 @@ static void config_channel(volatile uint8_t *tccra,
*tccra &= ~((1 << com1) | (1 << com0)); *tccra &= ~((1 << com1) | (1 << com0));
if (mode == PWM_OUTPUT_ACTIVE_HIGH) if (mode == PWM_OUTPUT_ACTIVE_HIGH)
*tccra |= ((1 << com1) | (0 << com0)); /* non inverting mode */ *tccra |= ((1 << com1) | (0 << com0)); /* Non inverting mode. */
else if (mode == PWM_OUTPUT_ACTIVE_LOW) else if (mode == PWM_OUTPUT_ACTIVE_LOW)
*tccra |= (1 << com1) | (1 << com0); /* inverting mode */ *tccra |= (1 << com1) | (1 << com0); /* Inverting mode. */
} }
static uint8_t timer_index(PWMDriver *pwmp) { static uint8_t timer_index(PWMDriver *pwmp) {
@ -152,12 +152,12 @@ static uint8_t timer_index(PWMDriver *pwmp) {
return index; return index;
} }
/*===========================================================================*/ /*==========================================================================*/
/* Driver interrupt handlers. */ /* Driver interrupt handlers. */
/*===========================================================================*/ /*==========================================================================*/
/* /*
* interrupt for compare1&2 and clock overflow. pwmd1 & pwmd2 * Interrupt for compare1&2 and clock overflow. pwmd1 & pwmd2.
*/ */
#if AVR_PWM_USE_TIM1 || defined(__DOXYGEN__) #if AVR_PWM_USE_TIM1 || defined(__DOXYGEN__)
OSAL_IRQ_HANDLER(TIMER1_OVF_vect) { OSAL_IRQ_HANDLER(TIMER1_OVF_vect) {
@ -303,9 +303,9 @@ OSAL_IRQ_HANDLER(TIMER5_COMPC_vect) {
} }
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level PWM driver initialization. * @brief Low level PWM driver initialization.
@ -343,7 +343,7 @@ void pwm_lld_init(void) {
/** /**
* @brief Configures and activates the PWM peripheral. * @brief Configures and activates the PWM peripheral.
* @note We do not use the period value in Timer2 in order to * @note We do not use the period value in Timer2 in order to
* be able to use both PWM channels * be able to use both PWM channels.
* *
* @param[in] pwmp pointer to the @p PWMDriver object * @param[in] pwmp pointer to the @p PWMDriver object
* *
@ -356,17 +356,17 @@ void pwm_lld_start(PWMDriver *pwmp) {
#if AVR_PWM_USE_TIM2 || defined(__DOXYGEN__) #if AVR_PWM_USE_TIM2 || defined(__DOXYGEN__)
if (pwmp == &PWMD2) { if (pwmp == &PWMD2) {
/* for now only fast pwm is supported */ /* For now only fast pwm is supported. */
wgm_value = 0x3; wgm_value = 0x3;
cs_value = 1; cs_value = 1;
/* period is fixed for timer2 */ /* Period is fixed for timer2. */
PWMD2.period = 0xFF; PWMD2.period = 0xFF;
/* A prescaler value can only be a suitable power of 2 (1, 8, 32, /* A prescaler value can only be a suitable power of 2 (1, 8, 32,
64, 128 256 or 1024), so we choose the one that makes F_CPU 64, 128 256 or 1024), so we choose the one that makes F_CPU
divided by it equal to the given frequency (fallback value is divided by it equal to the given frequency (fallback value is
1, to keep compatibility with old code */ 1, to keep compatibility with old code. */
const uint8_t log_ratio_timer2[] = {0, 3, 5, 6, 7, 8, 10}; const uint8_t log_ratio_timer2[] = {0, 3, 5, 6, 7, 8, 10};
uint8_t n; uint8_t n;
for (n=0; n<sizeof(log_ratio_timer2)/sizeof(uint8_t); n++) { for (n=0; n<sizeof(log_ratio_timer2)/sizeof(uint8_t); n++) {
@ -385,14 +385,14 @@ void pwm_lld_start(PWMDriver *pwmp) {
} }
#endif #endif
/* for now only fast pwm is supported */ /* For now only fast pwm is supported. */
wgm_value = 0xE; wgm_value = 0xE;
cs_value = 0x5; cs_value = 0x5;
/* A prescaler value can only be a suitable power of 2 (1, 8, 64, /* A prescaler value can only be a suitable power of 2 (1, 8, 64,
256 or 1024), so we choose the one that makes F_CPU divided by 256 or 1024), so we choose the one that makes F_CPU divided by
it equal to the given frequency (fallback value is 1024, to it equal to the given frequency (fallback value is 1024, to
keep compatibility with old code */ keep compatibility with old code. */
const uint8_t log_ratio_timer1[] = {0, 3, 6, 8, 10}; const uint8_t log_ratio_timer1[] = {0, 3, 6, 8, 10};
uint8_t n; uint8_t n;
for (n=0; n<sizeof(log_ratio_timer1)/sizeof(uint8_t); n++) { for (n=0; n<sizeof(log_ratio_timer1)/sizeof(uint8_t); n++) {
@ -455,7 +455,7 @@ void pwm_lld_stop(PWMDriver *pwmp) {
void pwm_lld_change_period(PWMDriver *pwmp, pwmcnt_t period) { void pwm_lld_change_period(PWMDriver *pwmp, pwmcnt_t period) {
#if AVR_PWM_USE_TIM2 || defined(__DOXYGEN__) #if AVR_PWM_USE_TIM2 || defined(__DOXYGEN__)
/* Can't change period in timer2 */ /* Can't change period in timer2. */
if (pwmp == &PWMD2) { if (pwmp == &PWMD2) {
PWMD2.period = 0xFF; PWMD2.period = 0xFF;
return; return;
@ -491,7 +491,7 @@ void pwm_lld_enable_channel(PWMDriver *pwmp,
7 - 2*channel, 7 - 2*channel,
6 - 2*channel, 6 - 2*channel,
pwmp->config->channels[channel].mode); pwmp->config->channels[channel].mode);
/* Timer 2 is 8 bit */ /* Timer 2 is 8 bit. */
if (val > 0xFF) if (val > 0xFF)
val = 0xFF; val = 0xFF;
switch (channel) { switch (channel) {

View File

@ -34,9 +34,9 @@
#include "avr_timers.h" #include "avr_timers.h"
/*===========================================================================*/ /*==========================================================================*/
/* Driver constants. */ /* Driver constants. */
/*===========================================================================*/ /*==========================================================================*/
#if !defined(AVR_PWM_USE_TIM1) #if !defined(AVR_PWM_USE_TIM1)
#define AVR_PWM_USE_TIM1 FALSE #define AVR_PWM_USE_TIM1 FALSE
@ -54,9 +54,9 @@
#define AVR_PWM_USE_TIM5 FALSE #define AVR_PWM_USE_TIM5 FALSE
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver pre-compile time settings. */ /* Driver pre-compile time settings. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Number of PWM channels per PWM driver. * @brief Number of PWM channels per PWM driver.
@ -69,13 +69,13 @@
#endif #endif
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief PWM mode type. * @brief PWM mode type.
@ -113,7 +113,7 @@ typedef struct {
* @p NULL then the callback is disabled. * @p NULL then the callback is disabled.
*/ */
pwmcallback_t callback; pwmcallback_t callback;
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
} PWMChannelConfig; } PWMChannelConfig;
/** /**
@ -144,7 +144,7 @@ typedef struct {
* @brief Channels configurations. * @brief Channels configurations.
*/ */
PWMChannelConfig channels[PWM_CHANNELS]; PWMChannelConfig channels[PWM_CHANNELS];
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
} PWMConfig; } PWMConfig;
/** /**
@ -176,16 +176,16 @@ struct PWMDriver {
#if defined(PWM_DRIVER_EXT_FIELDS) #if defined(PWM_DRIVER_EXT_FIELDS)
PWM_DRIVER_EXT_FIELDS PWM_DRIVER_EXT_FIELDS
#endif #endif
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver macros. */ /* Driver macros. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* External declarations. */ /* External declarations. */
/*===========================================================================*/ /*==========================================================================*/
#if AVR_PWM_USE_TIM1 || defined(__DOXYGEN__) #if AVR_PWM_USE_TIM1 || defined(__DOXYGEN__)
extern PWMDriver PWMD1; extern PWMDriver PWMD1;

View File

@ -26,30 +26,30 @@
#if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__) #if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__)
/*===========================================================================*/ /*==========================================================================*/
/* Driver local definitions. */ /* Driver local definitions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Timer maximum value * @brief Timer maximum value
*/ */
#define AVR_TIMER_COUNTER_MAX 255 #define AVR_TIMER_COUNTER_MAX 255
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported variables. */ /* Driver exported variables. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver local types. */ /* Driver local types. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables and types. */ /* Driver local variables and types. */
/*===========================================================================*/ /*==========================================================================*/
#if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__) #if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__)
/* Work out what the timer interrupt is called on this MCU */ /* Work out what the timer interrupt is called on this MCU. */
#ifdef TIMER0_COMPA_vect #ifdef TIMER0_COMPA_vect
#define AVR_TIMER_VECT TIMER0_COMPA_vect #define AVR_TIMER_VECT TIMER0_COMPA_vect
#elif defined(TIMER_COMPA_vect) #elif defined(TIMER_COMPA_vect)
@ -60,16 +60,16 @@
#error "Cannot find interrupt vector name for timer" #error "Cannot find interrupt vector name for timer"
#endif #endif
/* Find the most suitable prescaler setting for the desired OSAL_ST_FREQUENCY */ /* Find the most suitable prescaler setting for the desired OSAL_ST_FREQUENCY*/
#if ((F_CPU / OSAL_ST_FREQUENCY) <= AVR_TIMER_COUNTER_MAX) #if ((F_CPU / OSAL_ST_FREQUENCY) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 1 #define AVR_TIMER_PRESCALER 1
#define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(0<<CS01)|(1<<CS00)) /* CLK */ #define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(0<<CS01)|(1<<CS00)) /* CLK */
#elif ((F_CPU / OSAL_ST_FREQUENCY / 8) <= AVR_TIMER_COUNTER_MAX) #elif ((F_CPU / OSAL_ST_FREQUENCY / 8) <= AVR_TIMER_COUNTER_MAX)
#define AVR_TIMER_PRESCALER 8 #define AVR_TIMER_PRESCALER 8
#define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(1<<CS01)|(0<<CS00)) /* CLK/8 */ #define AVR_TIMER_PRESCALER_BITS ((0<<CS02)|(1<<CS01)|(0<<CS00)) /* CLK/8 */
#elif ((F_CPU / OSAL_ST_FREQUENCY / 64) <= AVR_TIMER_COUNTER_MAX) #elif ((F_CPU / OSAL_ST_FREQUENCY / 64) <= AVR_TIMER_COUNTER_MAX)
@ -107,7 +107,7 @@
#define AVR_TIMER_COUNTER (F_CPU / OSAL_ST_FREQUENCY / AVR_TIMER_PRESCALER) #define AVR_TIMER_COUNTER (F_CPU / OSAL_ST_FREQUENCY / AVR_TIMER_PRESCALER)
/* Test if OSAL_ST_FREQUENCY can be matched exactly using this timer */ /* Test if OSAL_ST_FREQUENCY can be matched exactly using this timer. */
#define F_CPU_ (AVR_TIMER_COUNTER * AVR_TIMER_PRESCALER * OSAL_ST_FREQUENCY) #define F_CPU_ (AVR_TIMER_COUNTER * AVR_TIMER_PRESCALER * OSAL_ST_FREQUENCY)
#if (F_CPU_ != F_CPU) #if (F_CPU_ != F_CPU)
#warning "OSAL_ST_FREQUENCY cannot be generated exactly using timer" #warning "OSAL_ST_FREQUENCY cannot be generated exactly using timer"
@ -126,13 +126,13 @@
#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */ #endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
/*===========================================================================*/ /*==========================================================================*/
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver interrupt handlers. */ /* Driver interrupt handlers. */
/*===========================================================================*/ /*==========================================================================*/
#if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__) #if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__)
@ -161,7 +161,7 @@ OSAL_IRQ_HANDLER(TIMER1_COMPA_vect) {
OSAL_IRQ_PROLOGUE(); OSAL_IRQ_PROLOGUE();
// TODO: reset status if required /* TODO: reset status if required. */
osalSysLockFromISR(); osalSysLockFromISR();
osalOsTimerHandlerI(); osalOsTimerHandlerI();
@ -172,9 +172,9 @@ OSAL_IRQ_HANDLER(TIMER1_COMPA_vect) {
#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */ #endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level ST driver initialization. * @brief Low level ST driver initialization.
@ -189,15 +189,15 @@ void st_lld_init(void) {
* Periodic mode uses Timer 1 (16 bit). * Periodic mode uses Timer 1 (16 bit).
*/ */
/* CTC mode, no clock source */ /* CTC mode, no clock source. */
TCCR1A = 0; TCCR1A = 0;
TCCR1B = _BV(WGM12); TCCR1B = _BV(WGM12);
/* start disabled */ /* start disabled. */
TCCR1C = 0; TCCR1C = 0;
OCR1A = 0; OCR1A = 0;
TCNT1 = 0; TCNT1 = 0;
TIFR_REG = _BV(OCF1A); /* Reset pending. */ TIFR_REG = _BV(OCF1A); /* Reset pending. */
TIMSK_REG = 0; TIMSK_REG = 0;
TCCR1B = PRESCALER; TCCR1B = PRESCALER;
@ -208,32 +208,33 @@ void st_lld_init(void) {
/* /*
* Periodic mode uses Timer 0 (8 bit). * Periodic mode uses Timer 0 (8 bit).
*/ */
#if defined(TCCR0B) /* Timer has multiple output comparators */ #if defined(TCCR0B) /* Timer has multiple output comparators. */
TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */ TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
(0 << COM0A1) | (0 << COM0A0) | /* OC0A disabled. */ (0 << COM0A1) | (0 << COM0A0) | /* OC0A disabled. */
(0 << COM0B1) | (0 << COM0B0); /* OC0B disabled. */ (0 << COM0B1) | (0 << COM0B0); /* OC0B disabled. */
TCCR0B = (0 << WGM02) | AVR_TIMER_PRESCALER_BITS; /* CTC mode. */ TCCR0B = (0 << WGM02) | AVR_TIMER_PRESCALER_BITS; /* CTC mode. */
OCR0A = AVR_TIMER_COUNTER - 1; OCR0A = AVR_TIMER_COUNTER - 1;
TCNT0 = 0; /* Reset counter. */ TCNT0 = 0; /* Reset counter. */
TIFR0 = (1 << OCF0A); /* Reset pending. */ TIFR0 = (1 << OCF0A); /* Reset pending. */
TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */ TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */
#elif defined(TCCR0A) /* AT90CAN doesn't have TCCR0B and slightly different TCCR0A */ #elif defined(TCCR0A) /* AT90CAN doesn't have TCCR0B and slightly different */
TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */ /* TCCR0A. */
(0 << COM0A1) | (0 << COM0A0); /* OC0A disabled. */ TCCR0A = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
(0 << COM0A1) | (0 << COM0A0); /* OC0A disabled. */
OCR0A = AVR_TIMER_COUNTER - 1; OCR0A = AVR_TIMER_COUNTER - 1;
TCNT0 = 0; /* Reset counter. */ TCNT0 = 0; /* Reset counter. */
TIFR0 = (1 << OCF0A); /* Reset pending. */ TIFR0 = (1 << OCF0A); /* Reset pending. */
TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */ TIMSK0 = (1 << OCIE0A); /* IRQ on compare. */
#elif defined(TCCR0) /* Timer has single output comparator */ #elif defined(TCCR0) /* Timer has single output comparator */
TCCR0 = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */ TCCR0 = (1 << WGM01) | (0 << WGM00) | /* CTC mode. */
(0 << COM01) | (0 << COM00) | /* OC0A disabled. */ (0 << COM01) | (0 << COM00) | /* OC0A disabled. */
AVR_TIMER_PRESCALER_BITS; AVR_TIMER_PRESCALER_BITS;
OCR0 = AVR_TIMER_COUNTER - 1; OCR0 = AVR_TIMER_COUNTER - 1;
TCNT0 = 0; /* Reset counter. */ TCNT0 = 0; /* Reset counter. */
TIFR = (1 << OCF0); /* Reset pending. */ TIFR = (1 << OCF0); /* Reset pending. */
TIMSK = (1 << OCIE0); /* IRQ on compare. */ TIMSK = (1 << OCIE0); /* IRQ on compare. */
#else #else
#error "Neither TCCR0A nor TCCR0 registers are defined" #error "Neither TCCR0A nor TCCR0 registers are defined"
#endif #endif

View File

@ -29,38 +29,33 @@
#include <avr/io.h> #include <avr/io.h>
/*===========================================================================*/ /*==========================================================================*/
/* Driver constants. */ /* Driver constants. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver pre-compile time settings. */ /* Driver pre-compile time settings. */
/*===========================================================================*/ /*==========================================================================*/
/* /* TODO: for models that have many timers, could add AVR_ST_USE_TIMER. */
* TODO: for models that have many timers,
* could add AVR_ST_USE_TIMER
*/
/*===========================================================================*/ /*==========================================================================*/
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*==========================================================================*/
/* /* TODO: error checks for valid timer selected. */
* TODO: error checks for valid timer selected
*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver macros. */ /* Driver macros. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* External declarations. */ /* External declarations. */
/*===========================================================================*/ /*==========================================================================*/
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -78,9 +73,9 @@ extern "C" {
#define TIMSK_REG TIMSK1 #define TIMSK_REG TIMSK1
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver inline functions. */ /* Driver inline functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Returns the time counter value. * @brief Returns the time counter value.
@ -110,7 +105,7 @@ static inline void st_lld_start_alarm(systime_t time) {
/* Reset pending. */ /* Reset pending. */
TIFR_REG = _BV(OCF1A); TIFR_REG = _BV(OCF1A);
/* enable interrupt */ /* Enable interrupt. */
TIMSK_REG = _BV(OCIE1A); TIMSK_REG = _BV(OCIE1A);
} }
@ -153,7 +148,7 @@ static inline systime_t st_lld_get_alarm(void) {
* *
* @return The alarm status. * @return The alarm status.
* @retval false if the alarm is not active. * @retval false if the alarm is not active.
* @retval true is the alarm is active * @retval true is the alarm is active.
* *
* @notapi * @notapi
*/ */

View File

@ -26,9 +26,9 @@
#if HAL_USE_SERIAL || defined(__DOXYGEN__) #if HAL_USE_SERIAL || defined(__DOXYGEN__)
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported variables. */ /* Driver exported variables. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief USART0 serial driver identifier. * @brief USART0 serial driver identifier.
@ -38,7 +38,7 @@
#if AVR_SERIAL_USE_USART0 || defined(__DOXYGEN__) #if AVR_SERIAL_USE_USART0 || defined(__DOXYGEN__)
SerialDriver SD1; SerialDriver SD1;
/* USARTs are not consistently named across the AVR range */ /* USARTs are not consistently named across the AVR range. */
#ifdef USART0_RX_vect #ifdef USART0_RX_vect
#define AVR_SD1_RX_VECT USART0_RX_vect #define AVR_SD1_RX_VECT USART0_RX_vect
#define AVR_SD1_TX_VECT USART0_UDRE_vect #define AVR_SD1_TX_VECT USART0_UDRE_vect
@ -61,7 +61,7 @@ SerialDriver SD1;
#if AVR_SERIAL_USE_USART1 || defined(__DOXYGEN__) #if AVR_SERIAL_USE_USART1 || defined(__DOXYGEN__)
SerialDriver SD2; SerialDriver SD2;
/* Check if USART1 exists for this MCU */ /* Check if USART1 exists for this MCU. */
#ifdef USART1_RX_vect #ifdef USART1_RX_vect
#define AVR_SD2_RX_VECT USART1_RX_vect #define AVR_SD2_RX_VECT USART1_RX_vect
#define AVR_SD2_TX_VECT USART1_UDRE_vect #define AVR_SD2_TX_VECT USART1_UDRE_vect
@ -73,9 +73,9 @@ SerialDriver SD2;
#endif #endif
#endif /* AVR_SERIAL_USE_USART1 */ #endif /* AVR_SERIAL_USE_USART1 */
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables and types. */ /* Driver local variables and types. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Driver default configuration. * @brief Driver default configuration.
@ -85,9 +85,9 @@ static const SerialConfig default_config = {
USART_CHAR_SIZE_8 USART_CHAR_SIZE_8
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*==========================================================================*/
static void set_error(uint8_t sra, SerialDriver *sdp) { static void set_error(uint8_t sra, SerialDriver *sdp) {
eventflags_t sts = 0; eventflags_t sts = 0;
@ -244,9 +244,9 @@ static void usart1_deinit(void) {
} }
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver interrupt handlers. */ /* Driver interrupt handlers. */
/*===========================================================================*/ /*==========================================================================*/
#if AVR_SERIAL_USE_USART0 || defined(__DOXYGEN__) #if AVR_SERIAL_USE_USART0 || defined(__DOXYGEN__)
/** /**
@ -334,9 +334,9 @@ OSAL_IRQ_HANDLER(AVR_SD2_TX_VECT) {
} }
#endif /* AVR_SERIAL_USE_USART1 */ #endif /* AVR_SERIAL_USE_USART1 */
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level serial driver initialization. * @brief Low level serial driver initialization.

View File

@ -27,13 +27,13 @@
#if HAL_USE_SERIAL || defined(__DOXYGEN__) #if HAL_USE_SERIAL || defined(__DOXYGEN__)
/*===========================================================================*/ /*==========================================================================*/
/* Driver constants. */ /* Driver constants. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver pre-compile time settings. */ /* Driver pre-compile time settings. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief USART0 driver enable switch. * @brief USART0 driver enable switch.
@ -53,13 +53,13 @@
#define AVR_SERIAL_USE_USART1 TRUE #define AVR_SERIAL_USE_USART1 TRUE
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief AVR Serial Driver configuration structure. * @brief AVR Serial Driver configuration structure.
@ -82,21 +82,21 @@ typedef struct {
*/ */
#define _serial_driver_data \ #define _serial_driver_data \
_base_asynchronous_channel_data \ _base_asynchronous_channel_data \
/* Driver state.*/ \ /* Driver state. */ \
sdstate_t state; \ sdstate_t state; \
/* Input queue.*/ \ /* Input queue. */ \
input_queue_t iqueue; \ input_queue_t iqueue; \
/* Output queue.*/ \ /* Output queue. */ \
output_queue_t oqueue; \ output_queue_t oqueue; \
/* Input circular buffer.*/ \ /* Input circular buffer. */ \
uint8_t ib[SERIAL_BUFFERS_SIZE]; \ uint8_t ib[SERIAL_BUFFERS_SIZE]; \
/* Output circular buffer.*/ \ /* Output circular buffer. */ \
uint8_t ob[SERIAL_BUFFERS_SIZE]; \ uint8_t ob[SERIAL_BUFFERS_SIZE]; \
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
/*===========================================================================*/ /*==========================================================================*/
/* Driver macros. */ /* Driver macros. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Macro for baud rate computation. * @brief Macro for baud rate computation.
@ -130,9 +130,9 @@ typedef struct {
#define USART_CHAR_SIZE_8 3 #define USART_CHAR_SIZE_8 3
#define USART_CHAR_SIZE_9 4 #define USART_CHAR_SIZE_9 4
/*===========================================================================*/ /*==========================================================================*/
/* External declarations. */ /* External declarations. */
/*===========================================================================*/ /*==========================================================================*/
#if AVR_SERIAL_USE_USART0 && !defined(__DOXYGEN__) #if AVR_SERIAL_USE_USART0 && !defined(__DOXYGEN__)
extern SerialDriver SD1; extern SerialDriver SD1;

View File

@ -30,13 +30,13 @@
#define F_USB F_CPU #define F_USB F_CPU
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver local definitions. */ /* Driver local definitions. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported variables. */ /* Driver exported variables. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief USB1 driver identifier. * @brief USB1 driver identifier.
@ -45,9 +45,9 @@
USBDriver USBD1; USBDriver USBD1;
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables and types. */ /* Driver local variables and types. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief EP0 state. * @brief EP0 state.
@ -79,13 +79,13 @@ static const USBEndpointConfig ep0config = {
&ep0_state.out &ep0_state.out
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver local variables and types. */ /* Driver local variables and types. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*==========================================================================*/
#ifdef AVR_USB_PLL_OFF_IN_SUSPEND #ifdef AVR_USB_PLL_OFF_IN_SUSPEND
static __attribute__((unused)) void usb_pll_off(void) { static __attribute__((unused)) void usb_pll_off(void) {
@ -125,7 +125,7 @@ static void usb_pll_on(void) {
#endif #endif
#ifdef PLLFRQ #ifdef PLLFRQ
/* This initializes PLL on supported devices for USB 48MHz *only* */ /* This initializes PLL on supported devices for USB 48MHz *only*. */
PLLFRQ = (0 << PDIV3) | (1 << PDIV2) | (0 << PDIV1) | (0 << PDIV0); PLLFRQ = (0 << PDIV3) | (1 << PDIV2) | (0 << PDIV1) | (0 << PDIV0);
#endif #endif
@ -138,9 +138,9 @@ static int usb_pll_is_locked(void) {
return !!(PLLCSR & (1 << PLOCK)); return !!(PLLCSR & (1 << PLOCK));
} }
/*===========================================================================*/ /*==========================================================================*/
/* Driver interrupt handlers and threads. */ /* Driver interrupt handlers and threads. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief USB general/OTG/device management event interrupt handler. * @brief USB general/OTG/device management event interrupt handler.
@ -164,41 +164,41 @@ OSAL_IRQ_HANDLER(USB_GEN_vect) {
while (!usb_pll_is_locked()) {} while (!usb_pll_is_locked()) {}
#endif /* AVR_USB_PLL_OFF_IN_SUSPEND */ #endif /* AVR_USB_PLL_OFF_IN_SUSPEND */
/* Attach to bus */ /* Attach to bus. */
usb_lld_connect_bus(usbp); usb_lld_connect_bus(usbp);
USBINT &= ~(1 << VBUSTI); USBINT &= ~(1 << VBUSTI);
} }
/* USB bus SUSPEND condition handling.*/ /* USB bus SUSPEND condition handling. */
if (udint & (1 << SUSPI)) { if (udint & (1 << SUSPI)) {
/* Disable suspend interrupt, enable WAKEUP interrupt */ /* Disable suspend interrupt, enable WAKEUP interrupt. */
UDIEN |= (1 << WAKEUPE); UDIEN |= (1 << WAKEUPE);
UDINT &= ~(1 << WAKEUPI); UDINT &= ~(1 << WAKEUPI);
UDIEN &= ~(1 << SUSPE); UDIEN &= ~(1 << SUSPE);
/* Freeze the clock to reduce power consumption */ /* Freeze the clock to reduce power consumption. */
USBCON |= (1 << FRZCLK); USBCON |= (1 << FRZCLK);
#ifdef AVR_USB_PLL_OFF_IN_SUSPEND #ifdef AVR_USB_PLL_OFF_IN_SUSPEND
usb_pll_off(); usb_pll_off();
#endif /* AVR_USB_PLL_OFF_IN_SUSPEND */ #endif /* AVR_USB_PLL_OFF_IN_SUSPEND */
/* Clear the interrupt */ /* Clear the interrupt. */
UDINT &= ~(1 << SUSPI); UDINT &= ~(1 << SUSPI);
_usb_isr_invoke_event_cb(usbp, USB_EVENT_SUSPEND); _usb_isr_invoke_event_cb(usbp, USB_EVENT_SUSPEND);
} }
/* USB bus WAKEUP condition handling.*/ /* USB bus WAKEUP condition handling. */
if (udint & (1 << WAKEUPI)) { if (udint & (1 << WAKEUPI)) {
#ifdef AVR_USB_PLL_OFF_IN_SUSPEND #ifdef AVR_USB_PLL_OFF_IN_SUSPEND
usb_pll_on(); usb_pll_on();
while (!usb_pll_is_locked()) {} while (!usb_pll_is_locked()) {}
#endif /* AVR_USB_PLL_OFF_IN_SUSPEND */ #endif /* AVR_USB_PLL_OFF_IN_SUSPEND */
/* Unfreeze the clock */ /* Unfreeze the clock. */
USBCON &= ~(1 << FRZCLK); USBCON &= ~(1 << FRZCLK);
/* Clear & disable wakeup interrupt, enable suspend interrupt */ /* Clear & disable wakeup interrupt, enable suspend interrupt. */
UDINT &= ~(1 << WAKEUPI); UDINT &= ~(1 << WAKEUPI);
UDIEN &= ~(1 << WAKEUPE); UDIEN &= ~(1 << WAKEUPE);
UDIEN |= (1 << SUSPE); UDIEN |= (1 << SUSPE);
@ -206,17 +206,17 @@ OSAL_IRQ_HANDLER(USB_GEN_vect) {
_usb_isr_invoke_event_cb(usbp, USB_EVENT_WAKEUP); _usb_isr_invoke_event_cb(usbp, USB_EVENT_WAKEUP);
} }
/* USB bus RESUME condition handling.*/ /* USB bus RESUME condition handling. */
if (udint & (1 << EORSMI)) { if (udint & (1 << EORSMI)) {
UDINT &= ~(1 << EORSMI); UDINT &= ~(1 << EORSMI);
UDIEN &= ~(1 << EORSME); UDIEN &= ~(1 << EORSME);
} }
/* USB bus reset condition handling.*/ /* USB bus reset condition handling. */
if (udint & (1 << EORSTI)) { if (udint & (1 << EORSTI)) {
UDINT &= ~(1 << EORSTI); UDINT &= ~(1 << EORSTI);
/* Clear & disable suspend interrupt, enable WAKEUP interrupt */ /* Clear & disable suspend interrupt, enable WAKEUP interrupt. */
UDINT &= ~(1 << SUSPI); UDINT &= ~(1 << SUSPI);
UDIEN &= ~(1 << SUSPE); UDIEN &= ~(1 << SUSPE);
UDIEN |= (1 << WAKEUPE); UDIEN |= (1 << WAKEUPE);
@ -228,7 +228,7 @@ OSAL_IRQ_HANDLER(USB_GEN_vect) {
_usb_isr_invoke_event_cb(usbp, USB_EVENT_RESET); _usb_isr_invoke_event_cb(usbp, USB_EVENT_RESET);
} }
/* Start-Of-Frame handling, only if enabled */ /* Start-Of-Frame handling, only if enabled. */
if ((UDIEN & (1 << SOFE)) && (udint & (1 << SOFI))) { if ((UDIEN & (1 << SOFE)) && (udint & (1 << SOFI))) {
_usb_isr_invoke_sof_cb(usbp); _usb_isr_invoke_sof_cb(usbp);
UDINT &= ~(1 << SOFI); UDINT &= ~(1 << SOFI);
@ -249,10 +249,10 @@ static void usb_fifo_write(USBDriver *usbp, usbep_t ep, size_t n) {
if (n > epcp->in_maxsize) if (n > epcp->in_maxsize)
n = epcp->in_maxsize; n = epcp->in_maxsize;
/* i is number of bytes remaining to transmit minus 1 (to handle 256b case) */ /* i is number of bytes remaining to transmit minus 1 (to handle 256b case). */
uint8_t i = n - 1; uint8_t i = n - 1;
/* Must lock for entire operation to ensure nothing changes the ENUM value */ /* Must lock for entire operation to ensure nothing changes the ENUM value. */
sts = osalSysGetStatusAndLockX(); sts = osalSysGetStatusAndLockX();
UENUM = ep & 0xf; UENUM = ep & 0xf;
do { do {
@ -271,10 +271,10 @@ static void usb_fifo_read(USBDriver *usbp, usbep_t ep, size_t n) {
return; return;
if (n > epcp->out_maxsize) if (n > epcp->out_maxsize)
n = epcp->out_maxsize; n = epcp->out_maxsize;
// i is number of bytes remaining to receive minus 1 (to handle 256b case) /* i is number of bytes remaining to receive minus 1 (to handle 256b case). */
uint8_t i = n - 1; uint8_t i = n - 1;
/* Must lock for entire operation to ensure nothing changes the ENUM value */ /* Must lock for entire operation to ensure nothing changes the ENUM value. */
sts = osalSysGetStatusAndLockX(); sts = osalSysGetStatusAndLockX();
UENUM = ep & 0xf; UENUM = ep & 0xf;
do { do {
@ -289,64 +289,64 @@ static void ep_isr(USBDriver *usbp, usbep_t ep) {
size_t n; size_t n;
UENUM = ep & 0xf; UENUM = ep & 0xf;
/* TODO: if stalling is needed/expected remove this check */ /* TODO: if stalling is needed/expected remove this check. */
osalDbgAssert(!(UEINTX & (1 << STALLEDI)), "Endpoint stalled!"); osalDbgAssert(!(UEINTX & (1 << STALLEDI)), "Endpoint stalled!");
if ((UEIENX & (1 << TXINE)) && (UEINTX & (1 << TXINI))) { if ((UEIENX & (1 << TXINE)) && (UEINTX & (1 << TXINI))) {
/* Ready to accept more IN data to transmit to host */ /* Ready to accept more IN data to transmit to host. */
/* Update transaction counts to reflect newly transmitted bytes */ /* Update transaction counts to reflect newly transmitted bytes. */
epcp->in_state->txcnt += epcp->in_state->last_tx_size; epcp->in_state->txcnt += epcp->in_state->last_tx_size;
n = epcp->in_state->txsize - epcp->in_state->txcnt; n = epcp->in_state->txsize - epcp->in_state->txcnt;
if (n > 0) { if (n > 0) {
/* Transfer not completed, there are more packets to send. */ /* Transfer not completed, there are more packets to send. */
usb_fifo_write(usbp, ep, n); usb_fifo_write(usbp, ep, n);
/* Clear FIFOCON to send the data in the FIFO and switch bank */ /* Clear FIFOCON to send the data in the FIFO and switch bank. */
UEINTX &= ~((1 << TXINI) | (1 << FIFOCON)); UEINTX &= ~((1 << TXINI) | (1 << FIFOCON));
/* Enable the TX complete interrupt */ /* Enable the TX complete interrupt. */
UEIENX |= (1 << TXINE); UEIENX |= (1 << TXINE);
} else { } else {
/* Disable TXIN interrupt */ /* Disable TXIN interrupt. */
UEIENX &= ~(1 << TXINE); UEIENX &= ~(1 << TXINE);
/* Handshake interrupt status */ /* Handshake interrupt status. */
UEINTX &= ~(1 << TXINI); UEINTX &= ~(1 << TXINI);
_usb_isr_invoke_in_cb(usbp, ep); _usb_isr_invoke_in_cb(usbp, ep);
} }
} else if ((UEIENX & (1 << RXSTPE)) && (UEINTX & (1 << RXSTPI))) { } else if ((UEIENX & (1 << RXSTPE)) && (UEINTX & (1 << RXSTPI))) {
/* Received SETUP data */ /* Received SETUP data. */
/* Reset transaction state for endpoint */ /* Reset transaction state for endpoint. */
epcp->in_state->txcnt = 0; epcp->in_state->txcnt = 0;
epcp->in_state->txsize = 0; epcp->in_state->txsize = 0;
epcp->in_state->last_tx_size = 0; epcp->in_state->last_tx_size = 0;
/* Setup packets handling, setup packets are handled using a /* Setup packets handling, setup packets are handled using a
specific callback.*/ specific callback. */
_usb_isr_invoke_setup_cb(usbp, ep); _usb_isr_invoke_setup_cb(usbp, ep);
} else if ((UEIENX & (1 << RXOUTE)) && (UEINTX & (1 << RXOUTI))) { } else if ((UEIENX & (1 << RXOUTE)) && (UEINTX & (1 << RXOUTI))) {
/* Received OUT data from host */ /* Received OUT data from host. */
if (ep == 0 && usbp->ep0state == USB_EP0_OUT_WAITING_STS) { if (ep == 0 && usbp->ep0state == USB_EP0_OUT_WAITING_STS) {
/* SETUP/control transaction complete, invoke the callback. */ /* SETUP/control transaction complete, invoke the callback. */
UEIENX &= ~(1 << RXOUTE); UEIENX &= ~(1 << RXOUTE);
UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON)); UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
_usb_isr_invoke_out_cb(usbp, ep); _usb_isr_invoke_out_cb(usbp, ep);
} else { } else {
/* Check the FIFO byte count to see how many bytes were received */ /* Check the FIFO byte count to see how many bytes were received. */
n = UEBCX; n = UEBCX;
usb_fifo_read(usbp, ep, n); usb_fifo_read(usbp, ep, n);
/* Transaction state update */ /* Transaction state update. */
epcp->out_state->rxcnt += n; epcp->out_state->rxcnt += n;
epcp->out_state->rxsize -= n; epcp->out_state->rxsize -= n;
epcp->out_state->rxpkts -= 1; epcp->out_state->rxpkts -= 1;
if (n < epcp->out_maxsize || epcp->out_state->rxpkts == 0) { if (n < epcp->out_maxsize || epcp->out_state->rxpkts == 0) {
/* Disable OUT interrupt */ /* Disable OUT interrupt. */
UEIENX &= ~(1 << RXOUTE); UEIENX &= ~(1 << RXOUTE);
/* Mark OUT FIFO processed to allow more data to be received */ /* Mark OUT FIFO processed to allow more data to be received. */
UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON)); UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
/* Transfer complete, invokes the callback.*/ /* Transfer complete, invokes the callback. */
_usb_isr_invoke_out_cb(usbp, ep); _usb_isr_invoke_out_cb(usbp, ep);
} else { } else {
/* Mark OUT FIFO processed to allow more data to be received */ /* Mark OUT FIFO processed to allow more data to be received. */
UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON)); UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
} }
} }
@ -366,22 +366,22 @@ OSAL_IRQ_HANDLER(USB_COM_vect) {
OSAL_IRQ_PROLOGUE(); OSAL_IRQ_PROLOGUE();
/* Figure out which endpoint(s) are interrupting */ /* Figure out which endpoint(s) are interrupting. */
for (i = 0; i < USB_MAX_ENDPOINTS; ++i) { for (i = 0; i < USB_MAX_ENDPOINTS; ++i) {
if (UEINT & (1 << i)) { if (UEINT & (1 << i)) {
ep_isr(usbp, i); ep_isr(usbp, i);
} }
} }
/* Restore endpoint selector to pre-interrupt state */ /* Restore endpoint selector to pre-interrupt state. */
UENUM = epnum_orig; UENUM = epnum_orig;
OSAL_IRQ_EPILOGUE(); OSAL_IRQ_EPILOGUE();
} }
/*===========================================================================*/ /*==========================================================================*/
/* Driver exported functions. */ /* Driver exported functions. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Low level USB driver initialization. * @brief Low level USB driver initialization.
@ -391,10 +391,10 @@ OSAL_IRQ_HANDLER(USB_COM_vect) {
void usb_lld_init(void) { void usb_lld_init(void) {
#if AVR_USB_USE_USB1 == TRUE #if AVR_USB_USE_USB1 == TRUE
/* Driver initialization.*/ /* Driver initialization. */
usbObjectInit(&USBD1); usbObjectInit(&USBD1);
/* Start and lock the USB 48MHz PLL (takes ~100ms) */ /* Start and lock the USB 48MHz PLL (takes ~100ms). */
usb_pll_on(); usb_pll_on();
while (!usb_pll_is_locked()) {} while (!usb_pll_is_locked()) {}
#endif #endif
@ -410,7 +410,7 @@ void usb_lld_init(void) {
void usb_lld_start(USBDriver *usbp) { void usb_lld_start(USBDriver *usbp) {
if (usbp->state == USB_STOP) { if (usbp->state == USB_STOP) {
/* Enables the peripheral.*/ /* Enables the peripheral. */
#if AVR_USB_USE_USB1 == TRUE #if AVR_USB_USE_USB1 == TRUE
if (&USBD1 == usbp) { if (&USBD1 == usbp) {
uint8_t i; uint8_t i;
@ -420,10 +420,10 @@ void usb_lld_start(USBDriver *usbp) {
*/ */
USBCON &= ~(1 << OTGPADE); USBCON &= ~(1 << OTGPADE);
/* Enable the internal 3.3V pad regulator */ /* Enable the internal 3.3V pad regulator. */
UHWCON |= (1 << UVREGE); UHWCON |= (1 << UVREGE);
/* Reset and disable all endpoints */ /* Reset and disable all endpoints. */
UERST = 0x7f; UERST = 0x7f;
UERST = 0; UERST = 0;
for (i = 0; i < USB_MAX_ENDPOINTS; ++i){ for (i = 0; i < USB_MAX_ENDPOINTS; ++i){
@ -435,7 +435,7 @@ void usb_lld_start(USBDriver *usbp) {
} }
} }
#endif #endif
/* Reset procedure enforced on driver start.*/ /* Reset procedure enforced on driver start. */
_usb_reset(usbp); _usb_reset(usbp);
} }
} }
@ -450,10 +450,10 @@ void usb_lld_start(USBDriver *usbp) {
void usb_lld_stop(USBDriver *usbp) { void usb_lld_stop(USBDriver *usbp) {
if (usbp->state == USB_READY) { if (usbp->state == USB_READY) {
/* Disables the peripheral.*/ /* Disables the peripheral. */
#if AVR_USB_USE_USB1 == TRUE #if AVR_USB_USE_USB1 == TRUE
if (&USBD1 == usbp) { if (&USBD1 == usbp) {
/* Disable and clear transition interrupts */ /* Disable and clear transition interrupts. */
#if !defined(__AVR_ATmega32U4__) #if !defined(__AVR_ATmega32U4__)
USBCON &= ~((1 << VBUSTE) | (1 << IDTE)); USBCON &= ~((1 << VBUSTE) | (1 << IDTE));
#else #else
@ -462,15 +462,15 @@ void usb_lld_stop(USBDriver *usbp) {
USBINT = 0; USBINT = 0;
/* Disable and clear device interrupts */ /* Disable and clear device interrupts. */
UDIEN &= ~((1 << UPRSME) | (1 << EORSME) | (1 << WAKEUPE) | (1 << EORSTE) UDIEN &= ~((1 << UPRSME) | (1 << EORSME) | (1 << WAKEUPE) | (1 << EORSTE)
| (1 << SOFE) | (1 << SUSPE)); | (1 << SOFE) | (1 << SUSPE));
UDINT = 0; UDINT = 0;
/* Freeze clock */ /* Freeze clock. */
USBCON |= (1 << FRZCLK); USBCON |= (1 << FRZCLK);
/* Disable USB logic */ /* Disable USB logic. */
USBCON &= ~(1 << USBE); USBCON &= ~(1 << USBE);
} }
#endif #endif
@ -486,34 +486,34 @@ void usb_lld_stop(USBDriver *usbp) {
*/ */
void usb_lld_reset(USBDriver *usbp) { void usb_lld_reset(USBDriver *usbp) {
/* Post-reset initialization.*/ /* Post-reset initialization. */
/* Reset and enable via toggling the USB macro logic overall enable bit */ /* Reset and enable via toggling the USB macro logic overall enable bit. */
USBCON &= ~(1 << USBE); USBCON &= ~(1 << USBE);
USBCON |= (1 << USBE); USBCON |= (1 << USBE);
/* Unfreeze clock */ /* Unfreeze clock. */
USBCON &= ~(1 << FRZCLK); USBCON &= ~(1 << FRZCLK);
/* Set Device mode */ /* Set Device mode. */
/* TODO: Support HOST/OTG mode if needed */ /* TODO: Support HOST/OTG mode if needed. */
#if !defined(__AVR_ATmega32U4__) #if !defined(__AVR_ATmega32U4__)
UHWCON |= (1 << UIMOD); UHWCON |= (1 << UIMOD);
#endif #endif
/* Set FULL 12mbps speed */ /* Set FULL 12mbps speed. */
UDCON &= ~(1 << LSM); UDCON &= ~(1 << LSM);
/* Enable device pin interrupt */ /* Enable device pin interrupt. */
USBCON |= (1 << VBUSTE); USBCON |= (1 << VBUSTE);
/* EP0 initialization.*/ /* EP0 initialization. */
UERST |= (1 << 0); UERST |= (1 << 0);
UERST &= ~(1 << 0); UERST &= ~(1 << 0);
usbp->epc[0] = &ep0config; usbp->epc[0] = &ep0config;
usb_lld_init_endpoint(usbp, 0); usb_lld_init_endpoint(usbp, 0);
/* Enable device-level event interrupts */ /* Enable device-level event interrupts. */
UDINT &= ~(1 << SUSPI); UDINT &= ~(1 << SUSPI);
UDIEN = (1 << UPRSME) | (1 << EORSME) | (1 << WAKEUPE) | (1 << EORSTE) UDIEN = (1 << UPRSME) | (1 << EORSME) | (1 << WAKEUPE) | (1 << EORSTE)
| (1 << SUSPE); | (1 << SUSPE);
@ -553,14 +553,14 @@ void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
uint16_t size = 0; uint16_t size = 0;
const USBEndpointConfig *epcp = usbp->epc[ep]; const USBEndpointConfig *epcp = usbp->epc[ep];
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
UENUM = ep & 0xf; UENUM = ep & 0xf;
/* Enable endpoint to take out of reset */ /* Enable endpoint to take out of reset. */
UECONX |= (1 << EPEN); UECONX |= (1 << EPEN);
UECFG1X = 0; UECFG1X = 0;
/* Set the endpoint type.*/ /* Set the endpoint type. */
switch (epcp->ep_mode & USB_EP_MODE_TYPE) { switch (epcp->ep_mode & USB_EP_MODE_TYPE) {
case USB_EP_MODE_TYPE_ISOC: case USB_EP_MODE_TYPE_ISOC:
UECFG0X = (0 << EPTYPE1) | (1 << EPTYPE0); UECFG0X = (0 << EPTYPE1) | (1 << EPTYPE0);
@ -575,7 +575,7 @@ void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
UECFG0X = (0 << EPTYPE1) | (0 << EPTYPE0); UECFG0X = (0 << EPTYPE1) | (0 << EPTYPE0);
} }
if ((epcp->ep_mode & USB_EP_MODE_TYPE) == USB_EP_MODE_TYPE_CTRL) { if ((epcp->ep_mode & USB_EP_MODE_TYPE) == USB_EP_MODE_TYPE_CTRL) {
/* CTRL endpoint */ /* CTRL endpoint. */
osalDbgCheck(epcp->in_maxsize == epcp->out_maxsize); osalDbgCheck(epcp->in_maxsize == epcp->out_maxsize);
size = epcp->in_maxsize; size = epcp->in_maxsize;
} else { } else {
@ -611,7 +611,7 @@ void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
osalDbgAssert(false, "Invalid size for USB endpoint"); osalDbgAssert(false, "Invalid size for USB endpoint");
} }
UEIENX |= (1 << RXSTPE)/* | (1 << RXOUTE)*/ | (1 << STALLEDE) ; UEIENX |= (1 << RXSTPE)/* | (1 << RXOUTE) */ | (1 << STALLEDE) ;
osalDbgAssert((UESTA0X & (1 << CFGOK)), osalDbgAssert((UESTA0X & (1 << CFGOK)),
"Hardware reports endpoint config is INVALID"); "Hardware reports endpoint config is INVALID");
@ -648,7 +648,7 @@ void usb_lld_disable_endpoints(USBDriver *usbp) {
*/ */
usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) { usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) {
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
UENUM = ep & 0xf; UENUM = ep & 0xf;
if (!(UECONX & (1 << EPEN))) if (!(UECONX & (1 << EPEN)))
@ -692,13 +692,13 @@ usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) { void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) {
uint8_t i; uint8_t i;
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
UENUM = ep & 0xf; UENUM = ep & 0xf;
for (i = 0; i < 8; ++i) { for (i = 0; i < 8; ++i) {
*buf++ = UEDATX; *buf++ = UEDATX;
} }
/* Clear FIFOCON and RXSTPI to drain the setup packet data from the FIFO */ /* Clear FIFOCON and RXSTPI to drain the setup packet data from the FIFO. */
UEINTX &= ~((1 << FIFOCON) | (1 << RXSTPI)); UEINTX &= ~((1 << FIFOCON) | (1 << RXSTPI));
} }
@ -717,18 +717,18 @@ void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) {
*/ */
void usb_lld_end_setup(USBDriver *usbp, usbep_t ep) { void usb_lld_end_setup(USBDriver *usbp, usbep_t ep) {
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
UENUM = ep & 0xf; UENUM = ep & 0xf;
if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST) { if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST) {
/* Enable interrupt and wait for OUT packet */ /* Enable interrupt and wait for OUT packet. */
usbp->epc[ep]->out_state->rxsize = 0; usbp->epc[ep]->out_state->rxsize = 0;
usbp->epc[ep]->out_state->rxpkts = 1; usbp->epc[ep]->out_state->rxpkts = 1;
UEINTX &= ~((1 << FIFOCON) | (1 << RXOUTI)); UEINTX &= ~((1 << FIFOCON) | (1 << RXOUTI));
UEIENX |= (1 << RXOUTE); UEIENX |= (1 << RXOUTE);
} else { } else {
/* Enable interrupt and wait for IN packet */ /* Enable interrupt and wait for IN packet. */
usbp->epc[ep]->in_state->last_tx_size = 0; usbp->epc[ep]->in_state->last_tx_size = 0;
usbp->epc[ep]->in_state->txcnt = 0; usbp->epc[ep]->in_state->txcnt = 0;
usbp->epc[ep]->in_state->txsize = 0; usbp->epc[ep]->in_state->txsize = 0;
@ -752,14 +752,14 @@ void usb_lld_start_out(USBDriver *usbp, usbep_t ep) {
syssts_t sts; syssts_t sts;
/* Initialize transfer by recording how many packets we expect to receive. */ /* Initialize transfer by recording how many packets we expect to receive. */
if (osp->rxsize == 0) /* Special case for zero sized packets.*/ if (osp->rxsize == 0) /* Special case for zero sized packets. */
osp->rxpkts = 1; osp->rxpkts = 1;
else else
osp->rxpkts = (uint8_t)((osp->rxsize + usbp->epc[ep]->out_maxsize - 1) / osp->rxpkts = (uint8_t)((osp->rxsize + usbp->epc[ep]->out_maxsize - 1) /
usbp->epc[ep]->out_maxsize); usbp->epc[ep]->out_maxsize);
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
/* Must lock for entire operation to ensure nothing changes the ENUM value */ /* Must lock for entire operation to ensure nothing changes the ENUM value. */
sts = osalSysGetStatusAndLockX(); sts = osalSysGetStatusAndLockX();
UENUM = ep & 0xf; UENUM = ep & 0xf;
@ -783,15 +783,15 @@ void usb_lld_start_in(USBDriver *usbp, usbep_t ep) {
/* Initialize transfer by filling FIFO with passed data. */ /* Initialize transfer by filling FIFO with passed data. */
usb_fifo_write(usbp, ep, isp->txsize); usb_fifo_write(usbp, ep, isp->txsize);
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
/* Must lock for entire operation to ensure nothing changes the ENUM value */ /* Must lock for entire operation to ensure nothing changes the ENUM value. */
sts = osalSysGetStatusAndLockX(); sts = osalSysGetStatusAndLockX();
UENUM = ep & 0xf; UENUM = ep & 0xf;
/* Clear FIFOCON to send the data in the FIFO and switch bank */ /* Clear FIFOCON to send the data in the FIFO and switch bank. */
UEINTX &= ~((1 << TXINI) | (1 << FIFOCON)); UEINTX &= ~((1 << TXINI) | (1 << FIFOCON));
/* Enable the TX complete interrupt */ /* Enable the TX complete interrupt. */
UEIENX |= (1 << TXINE); UEIENX |= (1 << TXINE);
osalSysRestoreStatusX(sts); osalSysRestoreStatusX(sts);
@ -810,8 +810,8 @@ void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) {
syssts_t sts; syssts_t sts;
(void)usbp; (void)usbp;
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
/* Must lock for entire operation to ensure nothing changes the ENUM value */ /* Must lock for entire operation to ensure nothing changes the ENUM value. */
sts = osalSysGetStatusAndLockX(); sts = osalSysGetStatusAndLockX();
UENUM = ep & 0xf; UENUM = ep & 0xf;
@ -845,8 +845,8 @@ void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) {
syssts_t sts; syssts_t sts;
(void)usbp; (void)usbp;
/* Select this endpoint number for subsequent commands */ /* Select this endpoint number for subsequent commands. */
/* Must lock for entire operation to ensure nothing changes the ENUM value */ /* Must lock for entire operation to ensure nothing changes the ENUM value. */
sts = osalSysGetStatusAndLockX(); sts = osalSysGetStatusAndLockX();
UENUM = ep & 0xf; UENUM = ep & 0xf;

View File

@ -29,9 +29,9 @@
#include "hal_usb.h" #include "hal_usb.h"
/*===========================================================================*/ /*==========================================================================*/
/* Driver constants. */ /* Driver constants. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Maximum endpoint address. * @brief Maximum endpoint address.
@ -53,9 +53,9 @@
*/ */
#define USB_SET_ADDRESS_ACK_HANDLING USB_SET_ADDRESS_ACK_SW #define USB_SET_ADDRESS_ACK_HANDLING USB_SET_ADDRESS_ACK_SW
/*===========================================================================*/ /*==========================================================================*/
/* Driver pre-compile time settings. */ /* Driver pre-compile time settings. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @name AVR configuration options * @name AVR configuration options
@ -88,13 +88,13 @@
#define AVR_USB_TX_BUF_ADDRESS_SPACE #define AVR_USB_TX_BUF_ADDRESS_SPACE
#endif #endif
/*===========================================================================*/ /*==========================================================================*/
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*==========================================================================*/
/*===========================================================================*/ /*==========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*==========================================================================*/
typedef const AVR_USB_TX_BUF_ADDRESS_SPACE uint8_t *usbbufptr_t; typedef const AVR_USB_TX_BUF_ADDRESS_SPACE uint8_t *usbbufptr_t;
@ -120,7 +120,7 @@ typedef struct {
*/ */
thread_reference_t thread; thread_reference_t thread;
#endif #endif
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
/** /**
* @brief Number of expected bytes in the most recent transmission. * @brief Number of expected bytes in the most recent transmission.
*/ */
@ -207,7 +207,7 @@ typedef struct {
* @details This structure maintains the state of the OUT endpoint. * @details This structure maintains the state of the OUT endpoint.
*/ */
USBOutEndpointState *out_state; USBOutEndpointState *out_state;
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
} USBEndpointConfig; } USBEndpointConfig;
/** /**
@ -234,7 +234,7 @@ typedef struct {
* @brief Start Of Frame callback. * @brief Start Of Frame callback.
*/ */
usbcallback_t sof_cb; usbcallback_t sof_cb;
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
} USBConfig; } USBConfig;
/** /**
@ -314,12 +314,12 @@ struct USBDriver {
#if defined(USB_DRIVER_EXT_FIELDS) #if defined(USB_DRIVER_EXT_FIELDS)
USB_DRIVER_EXT_FIELDS USB_DRIVER_EXT_FIELDS
#endif #endif
/* End of the mandatory fields.*/ /* End of the mandatory fields. */
}; };
/*===========================================================================*/ /*==========================================================================*/
/* Driver macros. */ /* Driver macros. */
/*===========================================================================*/ /*==========================================================================*/
/** /**
* @brief Returns the current frame number. * @brief Returns the current frame number.
@ -371,9 +371,9 @@ struct USBDriver {
do{ \ do{ \
} while (true) } while (true)
/*===========================================================================*/ /*==========================================================================*/
/* External declarations. */ /* External declarations. */
/*===========================================================================*/ /*==========================================================================*/
#if (AVR_USB_USE_USB1 == TRUE) && !defined(__DOXYGEN__) #if (AVR_USB_USE_USB1 == TRUE) && !defined(__DOXYGEN__)
extern USBDriver USBD1; extern USBDriver USBD1;