diff options
Diffstat (limited to 'drivers/avr')
-rw-r--r-- | drivers/avr/analog.c | 110 | ||||
-rw-r--r-- | drivers/avr/analog.h | 37 | ||||
-rw-r--r-- | drivers/avr/ws2812.c | 141 | ||||
-rw-r--r-- | drivers/avr/ws2812.h | 3 | ||||
-rw-r--r-- | drivers/avr/ws2812_i2c.c | 12 |
5 files changed, 117 insertions, 186 deletions
diff --git a/drivers/avr/analog.c b/drivers/avr/analog.c index 1a8da4261d..abe478b712 100644 --- a/drivers/avr/analog.c +++ b/drivers/avr/analog.c @@ -14,24 +14,31 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -// Simple analog to digitial conversion - #include <avr/io.h> #include <avr/pgmspace.h> #include <stdint.h> #include "analog.h" -static uint8_t aref = (1 << REFS0); // default to AREF = Vcc +static uint8_t aref = ADC_REF_POWER; -void analogReference(uint8_t mode) { aref = mode & 0xC0; } +void analogReference(uint8_t mode) { aref = mode & (_BV(REFS1) | _BV(REFS0)); } // Arduino compatible pin input int16_t analogRead(uint8_t pin) { #if defined(__AVR_ATmega32U4__) - static const uint8_t PROGMEM pin_to_mux[] = {0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20}; + // clang-format off + static const uint8_t PROGMEM pin_to_mux[] = { + //A0 A1 A2 A3 A4 A5 + //F7 F6 F5 F4 F1 F0 + 0x07, 0x06, 0x05, 0x04, 0x01, 0x00, + //A6 A7 A8 A9 A10 A11 + //D4 D7 B4 B5 B6 D6 + 0x20, 0x22, 0x23, 0x24, 0x25, 0x21 + }; + // clang-format on if (pin >= 12) return 0; return adc_read(pgm_read_byte(pin_to_mux + pin)); -#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega328P__) if (pin >= 8) return 0; return adc_read(pin); #else @@ -39,20 +46,87 @@ int16_t analogRead(uint8_t pin) { #endif } -// Mux input +int16_t analogReadPin(pin_t pin) { return adc_read(pinToMux(pin)); } + +uint8_t pinToMux(pin_t pin) { + switch (pin) { + // clang-format off +#if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) + case F0: return 0; // ADC0 + case F1: return _BV(MUX0); // ADC1 + case F2: return _BV(MUX1); // ADC2 + case F3: return _BV(MUX1) | _BV(MUX0); // ADC3 + case F4: return _BV(MUX2); // ADC4 + case F5: return _BV(MUX2) | _BV(MUX0); // ADC5 + case F6: return _BV(MUX2) | _BV(MUX1); // ADC6 + case F7: return _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // ADC7 + default: return _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V +#elif defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) + case F0: return 0; // ADC0 + case F1: return _BV(MUX0); // ADC1 + case F4: return _BV(MUX2); // ADC4 + case F5: return _BV(MUX2) | _BV(MUX0); // ADC5 + case F6: return _BV(MUX2) | _BV(MUX1); // ADC6 + case F7: return _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // ADC7 + case D4: return _BV(MUX5); // ADC8 + case D6: return _BV(MUX5) | _BV(MUX0); // ADC9 + case D7: return _BV(MUX5) | _BV(MUX1); // ADC10 + case B4: return _BV(MUX5) | _BV(MUX1) | _BV(MUX0); // ADC11 + case B5: return _BV(MUX5) | _BV(MUX2); // ADC12 + case B6: return _BV(MUX5) | _BV(MUX2) | _BV(MUX0); // ADC13 + default: return _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V +#elif defined(__AVR_ATmega32A__) + case A0: return 0; // ADC0 + case A1: return _BV(MUX0); // ADC1 + case A2: return _BV(MUX1); // ADC2 + case A3: return _BV(MUX1) | _BV(MUX0); // ADC3 + case A4: return _BV(MUX2); // ADC4 + case A5: return _BV(MUX2) | _BV(MUX0); // ADC5 + case A6: return _BV(MUX2) | _BV(MUX1); // ADC6 + case A7: return _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // ADC7 + default: return _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V +#elif defined(__AVR_ATmega328P__) + case C0: return 0; // ADC0 + case C1: return _BV(MUX0); // ADC1 + case C2: return _BV(MUX1); // ADC2 + case C3: return _BV(MUX1) | _BV(MUX0); // ADC3 + case C4: return _BV(MUX2); // ADC4 + case C5: return _BV(MUX2) | _BV(MUX0); // ADC5 + // ADC7:6 not present in DIP package and not shared by GPIO pins + default: return _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V +#endif + // clang-format on + } +} + int16_t adc_read(uint8_t mux) { -#if defined(__AVR_AT90USB162__) - return 0; -#else uint8_t low; - ADCSRA = (1 << ADEN) | ADC_PRESCALER; // enable ADC - ADCSRB = (1 << ADHSM) | (mux & 0x20); // high speed mode - ADMUX = aref | (mux & 0x1F); // configure mux input - ADCSRA = (1 << ADEN) | ADC_PRESCALER | (1 << ADSC); // start the conversion - while (ADCSRA & (1 << ADSC)) - ; // wait for result - low = ADCL; // must read LSB first - return (ADCH << 8) | low; // must read MSB only once! + // Enable ADC and configure prescaler + ADCSRA = _BV(ADEN) | ADC_PRESCALER; + +#if defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) + // High speed mode and ADC8-13 + ADCSRB = _BV(ADHSM) | (mux & _BV(MUX5)); +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) + // High speed mode only + ADCSRB = _BV(ADHSM); +#endif + + // Configure mux input +#if defined(MUX4) + ADMUX = aref | (mux & (_BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0))); +#else + ADMUX = aref | (mux & (_BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0))); #endif + + // Start the conversion + ADCSRA |= _BV(ADSC); + // Wait for result + while (ADCSRA & _BV(ADSC)) + ; + // Must read LSB first + low = ADCL; + // Must read MSB only once! + return (ADCH << 8) | low; } diff --git a/drivers/avr/analog.h b/drivers/avr/analog.h index 1b773d82ce..058882450d 100644 --- a/drivers/avr/analog.h +++ b/drivers/avr/analog.h @@ -14,45 +14,40 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef _analog_h_included__ -#define _analog_h_included__ +#pragma once #include <stdint.h> +#include "quantum.h" #ifdef __cplusplus extern "C" { #endif void analogReference(uint8_t mode); int16_t analogRead(uint8_t pin); + +int16_t analogReadPin(pin_t pin); +uint8_t pinToMux(pin_t pin); + int16_t adc_read(uint8_t mux); #ifdef __cplusplus } #endif -#define ADC_REF_POWER (1 << REFS0) -#define ADC_REF_INTERNAL ((1 << REFS1) | (1 << REFS0)) -#define ADC_REF_EXTERNAL (0) +#define ADC_REF_EXTERNAL 0 // AREF, Internal Vref turned off +#define ADC_REF_POWER _BV(REFS0) // AVCC with external capacitor on AREF pin +#define ADC_REF_INTERNAL (_BV(REFS1) | _BV(REFS0)) // Internal 2.56V Voltage Reference with external capacitor on AREF pin (1.1V for 328P) // These prescaler values are for high speed mode, ADHSM = 1 -#if F_CPU == 16000000L -# define ADC_PRESCALER ((1 << ADPS2) | (1 << ADPS1)) +#if F_CPU == 16000000L || F_CPU == 12000000L +# define ADC_PRESCALER (_BV(ADPS2) | _BV(ADPS1)) // /64 #elif F_CPU == 8000000L -# define ADC_PRESCALER ((1 << ADPS2) | (1 << ADPS0)) +# define ADC_PRESCALER (_BV(ADPS2) | _BV(ADPS0)) // /32 #elif F_CPU == 4000000L -# define ADC_PRESCALER ((1 << ADPS2)) +# define ADC_PRESCALER (_BV(ADPS2)) // /16 #elif F_CPU == 2000000L -# define ADC_PRESCALER ((1 << ADPS1) | (1 << ADPS0)) +# define ADC_PRESCALER (_BV(ADPS1) | _BV(ADPS0)) // /8 #elif F_CPU == 1000000L -# define ADC_PRESCALER ((1 << ADPS1)) +# define ADC_PRESCALER _BV(ADPS1) // /4 #else -# define ADC_PRESCALER ((1 << ADPS0)) -#endif - -// some avr-libc versions do not properly define ADHSM -#if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) -# if !defined(ADHSM) -# define ADHSM (7) -# endif -#endif - +# define ADC_PRESCALER _BV(ADPS0) // /2 #endif diff --git a/drivers/avr/ws2812.c b/drivers/avr/ws2812.c index 5c733c4ab0..82d985c20a 100644 --- a/drivers/avr/ws2812.c +++ b/drivers/avr/ws2812.c @@ -36,109 +36,6 @@ void ws2812_sendarray(uint8_t *array, uint16_t length); void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask); - -#ifdef RGBW_BB_TWI - -// Port for the I2C -# define I2C_DDR DDRD -# define I2C_PIN PIND -# define I2C_PORT PORTD - -// Pins to be used in the bit banging -# define I2C_CLK 0 -# define I2C_DAT 1 - -# define I2C_DATA_HI() \ - I2C_DDR &= ~(1 << I2C_DAT); \ - I2C_PORT |= (1 << I2C_DAT); -# define I2C_DATA_LO() \ - I2C_DDR |= (1 << I2C_DAT); \ - I2C_PORT &= ~(1 << I2C_DAT); - -# define I2C_CLOCK_HI() \ - I2C_DDR &= ~(1 << I2C_CLK); \ - I2C_PORT |= (1 << I2C_CLK); -# define I2C_CLOCK_LO() \ - I2C_DDR |= (1 << I2C_CLK); \ - I2C_PORT &= ~(1 << I2C_CLK); - -# define I2C_DELAY 1 - -void I2C_WriteBit(unsigned char c) { - if (c > 0) { - I2C_DATA_HI(); - } else { - I2C_DATA_LO(); - } - - I2C_CLOCK_HI(); - _delay_us(I2C_DELAY); - - I2C_CLOCK_LO(); - _delay_us(I2C_DELAY); - - if (c > 0) { - I2C_DATA_LO(); - } - - _delay_us(I2C_DELAY); -} - -// Inits bitbanging port, must be called before using the functions below -// -void I2C_Init(void) { - I2C_PORT &= ~((1 << I2C_DAT) | (1 << I2C_CLK)); - - I2C_CLOCK_HI(); - I2C_DATA_HI(); - - _delay_us(I2C_DELAY); -} - -// Send a START Condition -// -void I2C_Start(void) { - // set both to high at the same time - I2C_DDR &= ~((1 << I2C_DAT) | (1 << I2C_CLK)); - _delay_us(I2C_DELAY); - - I2C_DATA_LO(); - _delay_us(I2C_DELAY); - - I2C_CLOCK_LO(); - _delay_us(I2C_DELAY); -} - -// Send a STOP Condition -// -void I2C_Stop(void) { - I2C_CLOCK_HI(); - _delay_us(I2C_DELAY); - - I2C_DATA_HI(); - _delay_us(I2C_DELAY); -} - -// write a byte to the I2C slave device -// -unsigned char I2C_Write(unsigned char c) { - for (char i = 0; i < 8; i++) { - I2C_WriteBit(c & 128); - - c <<= 1; - } - - I2C_WriteBit(0); - _delay_us(I2C_DELAY); - _delay_us(I2C_DELAY); - - // _delay_us(I2C_DELAY); - // return I2C_ReadBit(); - return 0; -} - -#endif - // Setleds for standard RGB void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin)); @@ -146,45 +43,15 @@ void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { } void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask) { - // ws2812_DDRREG |= pinmask; // Enable DDR // new universal format (DDR) _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask; - ws2812_sendarray_mask((uint8_t *)ledarray, leds + leds + leds, pinmask); - _delay_us(50); -} - -// Setleds for SK6812RGBW -void inline ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds) { -#ifdef RGBW_BB_TWI - uint8_t sreg_prev, twcr_prev; - sreg_prev = SREG; - twcr_prev = TWCR; - cli(); - TWCR &= ~(1 << TWEN); - I2C_Init(); - I2C_Start(); - I2C_Write(0x84); - uint16_t datlen = leds << 2; - uint8_t curbyte; - uint8_t *data = (uint8_t *)ledarray; - while (datlen--) { - curbyte = *data++; - I2C_Write(curbyte); - } - I2C_Stop(); - SREG = sreg_prev; - TWCR = twcr_prev; -#endif - - // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR - // new universal format (DDR) - _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF); - - ws2812_sendarray_mask((uint8_t *)ledarray, leds << 2, _BV(RGB_DI_PIN & 0xF)); + ws2812_sendarray_mask((uint8_t *)ledarray, leds * sizeof(LED_TYPE), pinmask); -#ifndef RGBW_BB_TWI +#ifdef RGBW _delay_us(80); +#else + _delay_us(50); #endif } diff --git a/drivers/avr/ws2812.h b/drivers/avr/ws2812.h index 9652b94bbe..b869fb28c8 100644 --- a/drivers/avr/ws2812.h +++ b/drivers/avr/ws2812.h @@ -34,8 +34,7 @@ * The functions will perform the following actions: * - Set the data-out pin as output * - Send out the LED data - * - Wait 50�s to reset the LEDs + * - Wait 50us to reset the LEDs */ void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds); void ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t number_of_leds, uint8_t pinmask); -void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds); diff --git a/drivers/avr/ws2812_i2c.c b/drivers/avr/ws2812_i2c.c index 8525a026c7..1c332e24b6 100644 --- a/drivers/avr/ws2812_i2c.c +++ b/drivers/avr/ws2812_i2c.c @@ -1,6 +1,10 @@ #include "ws2812.h" #include "i2c_master.h" +#ifdef RGBW +# error "RGBW not supported" +#endif + #ifndef WS2812_ADDRESS # define WS2812_ADDRESS 0xb0 #endif @@ -21,11 +25,3 @@ void ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { i2c_transmit(WS2812_ADDRESS, (uint8_t *)ledarray, sizeof(LED_TYPE) * leds, WS2812_TIMEOUT); } - -// Setleds for SK6812RGBW -void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds) { -// not supported - for now error out if its enabled -#ifdef RGBW -# error "RGBW not supported" -#endif -} |