From 6e6b0c4b5f37075794c3bf878451546c94bd9fbd Mon Sep 17 00:00:00 2001 From: bsongis Date: Sun, 17 Mar 2013 16:03:45 +0000 Subject: [PATCH] [MEGA2560] First files from Franck --- src/mega2560/board_mega2560.cpp | 333 ++++++++++++++++++++++++++++++++ src/mega2560/board_mega2560.h | 50 +++++ 2 files changed, 383 insertions(+) create mode 100644 src/mega2560/board_mega2560.cpp create mode 100644 src/mega2560/board_mega2560.h diff --git a/src/mega2560/board_mega2560.cpp b/src/mega2560/board_mega2560.cpp new file mode 100644 index 000000000..b5b20667d --- /dev/null +++ b/src/mega2560/board_mega2560.cpp @@ -0,0 +1,333 @@ +#include "../open9x.h" + +#define PIN_MODE_MASK 0x0003 +#define PIN_INPUT 0x0000 +#define PIN_OUTPUT 0x0001 +#define PIN_PERIPHERAL 0x0002 +#define PIN_ANALOG 0x0003 +#define PIN_PULL_MASK 0x000C +#define PIN_PULLUP 0x0004 +#define PIN_NO_PULLUP 0x0000 +#define PIN_PULLDOWN 0x0008 +#define PIN_NO_PULLDOWN 0x0000 +#define PIN_PERI_MASK 0x00F0 +#define PIN_PUSHPULL 0x0000 +#define PIN_ODRAIN 0x8000 +#define PIN_PORT_MASK 0x0700 +#define PIN_SPEED_MASK 0x6000 + + +#if !defined(SIMU) +void configure_pins( uint32_t pins, uint16_t config ) +{ + uint32_t address ; + GPIO_TypeDef *pgpio ; + uint32_t thispin ; + uint32_t pos ; + + address = ( config & PIN_PORT_MASK ) >> 8 ; + address *= (GPIOB_BASE-GPIOA_BASE) ; + address += GPIOA_BASE ; + pgpio = (GPIO_TypeDef* ) address ; + + /* -------------------------Configure the port pins---------------- */ + /*-- GPIO Mode Configuration --*/ + for (thispin = 0x0001, pos = 0; thispin < 0x10000; thispin <<= 1, pos +=1 ) + { + if ( pins & thispin) + { + pgpio->MODER &= ~(GPIO_MODER_MODER0 << (pos * 2)) ; + pgpio->MODER |= (config & PIN_MODE_MASK) << (pos * 2) ; + + if ( ( (config & PIN_MODE_MASK ) == PIN_OUTPUT) || ( (config & PIN_MODE_MASK) == PIN_PERIPHERAL) ) + { + /* Speed mode configuration */ + pgpio->OSPEEDR &= ~GPIO_OSPEEDER_OSPEEDR0 << (pos * 2) ; + pgpio->OSPEEDR |= ((config & PIN_SPEED_MASK) >> 13 ) << (pos * 2) ; + + /* Output mode configuration*/ + pgpio->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pos)) ; + if ( config & PIN_ODRAIN ) + { + pgpio->OTYPER |= (GPIO_OTYPER_OT_0) << pos ; + } + } + /* Pull-up Pull down resistor configuration*/ + pgpio->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pos * 2)); + pgpio->PUPDR |= ((config & PIN_PULL_MASK) >> 2) << (pos * 2) ; + + pgpio->AFR[pos >> 3] &= ~(0x000F << (pos & 7)) ; + pgpio->AFR[pos >> 3] |= ((config & PIN_PERI_MASK) >> 4) << (pos & 7) ; + } + } +} +#endif + + + + +#ifndef SIMU +inline void boardInit() +{ + ADMUX=ADC_VREF_TYPE; + ADCSRA=0x85; // ADC enabled, pre-scaler division=32 (no interrupt, no auto-triggering) + ADCSRB=(1< 2 + EIMSK = (3< 2 + //configure uart1 here + DDRD &= ~(1 << 2); + PORTD &= ~(1 << 2); + #define EXTRA_ROTARY_ENCODERS_USART_BAUD 9600UL + UBRR1 = F_CPU/(16*EXTRA_ROTARY_ENCODERS_USART_BAUD)-1; + //9 bit mode + UCSR1C = (1< 2 + +uint8_t vpotToChange = 0; +uint8_t vpot_mod_state = 0; + +ISR(USART1_RX_vect) +{ + //receive data from extension board + //bit 9 = 1 mean encoder number, following byte with bit 9 = 0 is increment value + /* Get status and 9th bit, then data */ + /* from buffer */ + uint16_t resh = UCSR1B; + uint16_t resl = UDR1; + uint16_t res = 0; + + /* Filter the 9th bit, then return */ + resh = (resh >> 1) & 0x01; + res = ((resh << 8) | resl); + if((res == 0x180) | (res == 0x1C0)){ //button REb filter + keys[BTN_REa].input((res & 0x1C0) == 0x1C0, BTN_REa); + } else if((res & 0x100) == 0x100){ //rotary filter + vpotToChange = res & 0xEF; + vpot_mod_state = 1; + } + else { + if(vpot_mod_state & (vpotToChange < NUM_ROTARY_ENCODERS)) + { + int8_t vpot_inc = res & 0xFF; + if(vpot_inc){ + incRotaryEncoder(vpotToChange, vpot_inc); + } + vpot_mod_state = 0; + } + } +} +#endif +#endif // !SIMU + +uint8_t pwrCheck() +{ +#if !defined(SIMU) && !defined(REV0) + if ((PING & 0b00000010) && (~PINL & 0b01000000)) + return e_power_off; +#endif + return e_power_on; +} + +void pwrOff() +{ +#ifndef REV0 + PORTL = 0x7f; +#endif +} + +#if ROTARY_ENCODERS <= 2 +#define ROTENC_DOWN() ((~PIND & 0x20) || (~PIND & 0x10)) +#else +#define ROTENC_DOWN() (0) +#endif + +FORCEINLINE uint8_t keyDown() +{ + return ((~PINL) & 0x3F) || ROTENC_DOWN(); +} + +bool switchState(EnumKeys enuk) +{ + uint8_t result = 0 ; + + if (enuk < (int)DIM(keys)) + return keys[enuk].state() ? 1 : 0; + + switch(enuk){ + case SW_ELE: + result = PINC & (1< 0); + break; +#endif + + case SW_GEA: + result = PING & (1<> 3; + + for (int i=1; i<7; i++) + { + //INP_B_KEY_MEN 1 .. INP_B_KEY_LFT 6 + keys[enuk].input(in & (1<