From d07099d0901e157b970e7123c5c184cbf92f8957 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 7 Dec 2011 00:22:03 +0100 Subject: [PATCH] First PWM draft, independent duty cycle for high and low channel should be done. --- hardware/sam/cores/sam/Arduino.h | 14 +- hardware/sam/cores/sam/wiring_analog.c | 108 ++++++---- hardware/sam/variants/arduino_due/variant.cpp | 200 ++++++++---------- hardware/sam/variants/arduino_due/variant.h | 34 ++- 4 files changed, 199 insertions(+), 157 deletions(-) diff --git a/hardware/sam/cores/sam/Arduino.h b/hardware/sam/cores/sam/Arduino.h index e12784529..f7f60716b 100644 --- a/hardware/sam/cores/sam/Arduino.h +++ b/hardware/sam/cores/sam/Arduino.h @@ -94,7 +94,7 @@ typedef void (*voidFuncPtr)( void ) ; /* Definitions and types for pins */ typedef enum _EAnalogChannel { - NONE=-1, + NO_ADC=-1, ADC0=0, ADC1, ADC2, @@ -116,6 +116,17 @@ typedef enum _EAnalogChannel } EAnalogChannel ; #define ADC_CHANNEL_NUMBER_NONE 0xffffffff + +// Definition for PWM channels +typedef enum _EPWMChannel +{ + NO_PWM=-1, + PWM_CH0=0, + PWM_CH1, + PWM_CH2, + PWM_CH3 +} EPWMChannel ; + /** * Pin Attributes to be OR-ed */ @@ -136,6 +147,7 @@ typedef struct _PinDescription uint32_t ulPinAttribute ; EAnalogChannel ulAnalogChannel ; /* Describe which Analog pin is linked to a MCU pin */ EAnalogChannel ulADCChannelNumber ; /* We use the same enum than for ADC pins to describe the ADC channel in use on this pin */ + EPWMChannel ulPWMChannel ; } PinDescription ; /* Pins table to be instanciated into variant.cpp */ diff --git a/hardware/sam/cores/sam/wiring_analog.c b/hardware/sam/cores/sam/wiring_analog.c index 3e879a3b5..7df66af73 100644 --- a/hardware/sam/cores/sam/wiring_analog.c +++ b/hardware/sam/cores/sam/wiring_analog.c @@ -33,15 +33,13 @@ void analogReference(eAnalogReference ulMode) uint32_t analogRead(uint32_t ulPin) { - uint32_t ulValue = 0; - uint32_t ulChannel; + uint32_t ulValue = 0; + uint32_t ulChannel; - if (ulPin < A0) - { - ulPin += A0; - } + if (ulPin < A0) + ulPin += A0; - ulChannel = g_APinDescription[ulPin].ulADCChannelNumber ; + ulChannel = g_APinDescription[ulPin].ulADCChannelNumber ; #if defined __SAM3U4E__ switch ( g_APinDescription[ulPin].ulAnalogChannel ) @@ -112,50 +110,70 @@ uint32_t analogRead(uint32_t ulPin) return ulValue; } +static uint8_t PWMEnabled = 0; +static uint8_t TCEnabled = 0; +static uint8_t pinEnabled[PINS_COUNT]; + +void analogOutputInit() { + uint8_t i; + for (i=0; i 64MHz / ((4+1)*2) = 6.4MHz - * ADC clock = 6.4 MHz - */ adc_init( ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP ) ; adc_configure_timing( ADC, 15 ) ; adc_configure_trigger( ADC, ADC_TRIG_SW ) ; @@ -366,20 +359,15 @@ extern void init( void ) adc_disable_channel( ADC, ADC_ALL_CHANNEL ) ; // Initialize 12bit Analog Controller - /* Enable peripheral clock.*/ PMC_EnablePeripheral( ID_ADC12B ) ; - - /* Initialize ADC12. */ - /* startup = 8: 512 periods of ADCClock - * for prescal = 4 - * prescal: ADCClock = MCK / ( (PRESCAL+1) * 2 ) => 64MHz / ((4+1)*2) = 6.4MHz - * ADC clock = 6.4 MHz - */ adc12_init( ADC12B, SystemCoreClock, ADC12_FREQ_MAX, ADC12_STARTUP_FAST, 1 ) ; adc12_configure_timing( ADC12B, 15 ) ; adc12_configure_trigger( ADC12B, ADC_TRIG_SW ) ; adc12_disable_interrupt( ADC12B, 0xFFFFFFFF ) ; /* Disable all adc interrupt. */ adc12_disable_channel( ADC12B, ADC_ALL_CHANNEL ) ; + + // Initialize analogOutput module + analogOutputInit(); } #ifdef __cplusplus } diff --git a/hardware/sam/variants/arduino_due/variant.h b/hardware/sam/variants/arduino_due/variant.h index f6f962707..729133f31 100644 --- a/hardware/sam/variants/arduino_due/variant.h +++ b/hardware/sam/variants/arduino_due/variant.h @@ -64,6 +64,10 @@ * Pins *----------------------------------------------------------------------------*/ +// Number of pins defined in PinDescription array +#define PINS_COUNT (84u) + +// LEDs #define PIN_LED_13 (13u) #define PIN_LED_RXL (73u) #define PIN_LED_TXL (74u) @@ -71,6 +75,9 @@ #define PIN_LED2 PIN_LED_RXL #define PIN_LED3 PIN_LED_TXL +/* + * SPI Interfaces + */ #define SPI_INTERFACES_COUNT 1 #define SPI_INTERFACE SPI @@ -80,6 +87,14 @@ #define PIN_SPI_MISO (75u) #define PIN_SPI_SCK (77u) +static const uint8_t SS = 4 ; +static const uint8_t MOSI = 76 ; +static const uint8_t MISO = 75 ; +static const uint8_t SCK = 77 ; + +/* + * Wire Interfaces + */ #define WIRE_INTERFACES_COUNT 2 #define PIN_WIRE_SDA (20u) @@ -94,17 +109,18 @@ #define WIRE1_INTERFACE_ID ID_TWI0 #define WIRE1_ISR_HANDLER TWI0_IrqHandler +/* + * UART/USART Interfaces + */ #define PINS_UART (80u) #define PINS_USART0 (81u) #define PINS_USART1 (82u) #define PINS_USART2 (83u) -static const uint8_t SS = 4 ; -static const uint8_t MOSI = 76 ; -static const uint8_t MISO = 75 ; -static const uint8_t SCK = 77 ; - +/* + * Analog pins + */ static const uint8_t A0 = 54; static const uint8_t A1 = 55; static const uint8_t A2 = 56; @@ -122,6 +138,14 @@ static const uint8_t A13 = 67; // static const uint8_t A14 = ; // static const uint8_t A15 = ; +/* + * PWM + */ +#define PWM_INTERFACE PWM +#define PWM_FREQUENCY 1000 +#define PWM_MAX_DUTY_CYCLE 255 +#define PWM_MIN_DUTY_CYCLE 0 + /*---------------------------------------------------------------------------- * Arduino objects - C++ only *----------------------------------------------------------------------------*/