Show Posts
Pages: 1 ... 3 4 [5] 6
61  Products / Arduino Due / Re: Timer Interrupts on Due on: November 03, 2012, 06:23:54 am
Yeah, I was hoping to get timer interrupt hints on my thread for Tone.cpp code.  I've done timers on Uno and Maple, but Maple IO architecture is quite different from DUE.  There is PWM timer code in
hardware/arduino/sam/cores/arduino/wiring_analog.c
but no interrupt code.  The Timer/Counter support routines (no interrupt routines) are in
hardware/arduino/sam/system/libsam/source/tc.c
with the #define's in
hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3sd8/include/component/component_tc.h
The ISR handler's are named TCx_Handler(), e.g.  TC0_Handler()

I'm guessing (don't have a DUE yet) that interrupt enable would be something like
 tc->TC_CHANNEL[chan].TC_IER = TC_IER_CPCS;
but there is also an interrupt mask register TC_IMR?

not much help, but hopeful ...
62  Products / Arduino Due / tone.cpp TODO DUEless on: November 02, 2012, 03:42:14 pm
Well, I'm still DUE-less  smiley-cry, but trying to figure out how to do a Tone.cpp hacking PWM timer code out of wiring_analog.c, but the tc.c  sam routines don't give any functions for enabling interrupts etc. so the following code has many TODOs, but maybe I can enjoy programming the DUE vicariously with this tease ...

Code:
/*
 Tone generator
 v1  use timer, and toggle any digital pin in ISR
   funky duration from arduino version
   TODO use FindMckDivisor?
   timer selected will preclude using associated pins for PWM etc.
    could also do timer/pwm hardware toggle where caller controls duration
*/
#include "Arduino.h"

#ifdef __cplusplus
extern "C" {
#endif

//  50% duty
#define TONE_DUTY 128

// timers TC0 TC1 TC2   channels 0 1 ids 0-2  3-5  6-8
#define TONE_TIMER TC0
#define TONE_CHNL 1
#define TONE_ID 1

static uint8_t pinEnabled[PINS_COUNT];
static uint8_t TCChanEnabled = 0;
static uint8_t pin_state = 1;
static Tc *chTC = TONE_TIMER;
static uint32_t chNo = TONE_CHNL;

volatile int32_t toggle_count;
static uint32_t tone_pin;


static void TC_SetCMR_ChannelA(Tc *tc, uint32_t chan, uint32_t v)
{
    tc->TC_CHANNEL[chan].TC_CMR = (tc->TC_CHANNEL[chan].TC_CMR & 0xFFF0FFFF) | v;
}

static void TC_SetCMR_ChannelB(Tc *tc, uint32_t chan, uint32_t v)
{
    tc->TC_CHANNEL[chan].TC_CMR = (tc->TC_CHANNEL[chan].TC_CMR & 0xF0FFFFFF) | v;
}



// frequency (in hertz) and duration (in milliseconds).

void tone(uint32_t ulPin, uint32_t frequency, int32_t duration)
{
// We use MCLK/2 as clock.
const uint32_t TC = VARIANT_MCK / 2 / frequency;
uint32_t duty = TONE_DUTY * TC / TC_MAX_DUTY_CYCLE;

tone_pin = ulPin;
toggle_count = 0;  // strange  wipe out previous duration
if (duration > 0 ) toggle_count = 2 * frequency * duration / 1000;
else toggle_count = -1;

uint32_t interfaceID = TONE_ID;
uint32_t chA  = 1;

if (!TCChanEnabled) {
pmc_enable_periph_clk(TC_INTERFACE_ID + interfaceID);
// TODO  enable interrupts  and don't do PWM pin toggle
TC_Configure(chTC, chNo,
TC_CMR_TCCLKS_TIMER_CLOCK1 |
TC_CMR_WAVE |         // Waveform mode
TC_CMR_WAVSEL_UP_RC | // Counter running up and reset when equals to RC
TC_CMR_EEVT_XC0 |     // Set external events from XC0 (this setup TIOB as output)
TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_CLEAR |
TC_CMR_BCPB_CLEAR | TC_CMR_BCPC_CLEAR);
TC_SetRC(chTC, chNo, TC);
}
if (chA) {
TC_SetRA(chTC, chNo, duty);
TC_SetCMR_ChannelA(chTC, chNo, TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_SET);
} else {
TC_SetRB(chTC, chNo, duty);
TC_SetCMR_ChannelB(chTC, chNo, TC_CMR_BCPB_CLEAR | TC_CMR_BCPC_SET);
}
if (!pinEnabled[ulPin]) {
pinMode(ulPin, OUTPUT);
pinEnabled[ulPin] = 1;
}
if (!TCChanEnabled) {
TC_Start(chTC, chNo);
TCChanEnabled = 1;
}

}

void noTone(uint32_t ulPin)
{
// TODO does this disable timer
if (TCChanEnabled) {
TC_Stop(chTC, chNo);
TCChanEnabled = 0;
}
digitalWrite(ulPin,LOW);
}

// timer ISR
void TC0_Handler ( void ) {
if (toggle_count != 0){
// toggle pin  TODO  better
digitalWrite(tone_pin,pin_state);
pin_state = 1 - pin_state;
if (toggle_count > 0) toggle_count--;
} else {
noTone(tone_pin);
}
}

#ifdef __cplusplus
}
#endif
63  Products / Arduino Due / Re: PWM Resolution on: October 29, 2012, 07:23:11 pm
The PWM resolution depends on the pin you select for analogWrite(). See analogWrite() in  
hardware/arduino/sam/cores/arduino/wiring_analog.c
It does analog out using either hardware DAC, PWM, or Timer/Counter pins else does high/low on digital pins

 timer pins: 2-5, 10-13, 58,60,61
  PWM pins: 6-9

mapResolution() limits timer and PWM pins to 8 bit resolution, DAC  12-bit.

pin attributes are set in hardware/arduino/sam/variants/arduino_due_x/variant.cpp    
variant.h has
     #define DACC_RESOLUTION     12
     #define PWM_RESOLUTION      8
     #define TC_RESOLUTION       8




The chip supports 16-bit PWM on many pins, but the DUE seems to "recognize" only PWM pins 6-9
64  Products / Arduino Due / Re: Current from I/O PIN on: October 28, 2012, 10:57:32 am
Here's a somewhat badly formatted and unverified list of the pins

Code:
Pin Port     Func
0   PA8          RX0
1   PA9          TX0                    15
2   PB25         Digital Pin 2
3   PC28         Digital Pin 3          15
4   PA29 and PC26   Digital Pin 4       15
5   PC25         Digital Pin 5          15
6   PC24         Digital Pin 6          15
7   PC23         Digital Pin 7          15
8   PC22         Digital Pin 8          15
9   PC21         Digital Pin 9          15
10   PA28 and PC29   Digital Pin 10      15
11   PD7          Digital Pin 11         15
12   PD8          Digital Pin 12         15
13   PB27   Digital Pin 13 / Amber LED "L"
14   PD4          TX3                   15
15   PD5          RX3                   15
16   PA13         TX2
17   PA12         RX2
18   PA11         TX1
19   PA10         RX1
20   PB12         SDA
21   PB13         SCL
22   PB26         Digital Pin 22
23   PA14         Digital Pin 23         15
24   PA15         Digital Pin 24         15
25   PD0          Digital Pin 25         15
26   PD1          Digital pin 26         15
27   PD2          Digital Pin 27         15
28   PD3          Digital Pin 28         15
29   PD6          Digital Pin 29         15
30   PD9          Digital Pin 30         15
31   PA7          Digital Pin 31         15
32   PD10         Digital Pin 32
33   PC1          Digital Pin 33         15
34   PC2          Digital Pin 34         15
35   PC3          Digital Pin 35         15
36   PC4          Digital Pin 36         15
37   PC5          Digital Pin 37         15
38   PC6          Digital Pin 38         15
39   PC7          Digital Pin 39         15
40   PC8          Digital Pin 40         15
41   PC9          Digital Pin 41         15
42   PA19         Digital Pin 42         15
43   PA20         Digital Pin 43
44   PC19         Digital Pin 44         15
45   PC18         Digital Pin 45         15
46   PC17         Digital Pin 46         15
47   PC16         Digital Pin 47         15
48   PC15         Digital Pin 48         15
49   PC14         Digital Pin 49         15
50   PC13         Digital Pin 50         15  
51   PC12         Digital Pin 51         15  
52   PB21         Digital Pin 52
53   PB14         Digital Pin 53         15
54   PA16         Analog In 0
55   PA24         Analog In 1
56   PA23         Analog In 2
57   PA22         Analog In 3
58   PA6          Analog In 4
59   PA4          Analog In 5
60   PA3          Analog In 6
61   PA2          Analog In 7
62   PB17         Analog In 8
63   PB18         Analog In 9
64   PB19         Analog In 10
65   PB20         Analog In 11
66   PB15         DAC0
67   PB16         DAC1
68   PA1          CANRX
69   PA0          CANTX                15
70   PA17         SDA1
71   PA18         SCL1                 15
72   PC30         LED "RX"
73   PA21         LED "TX"
74   PA25         (MISO)               15
75   PA26         (MOSI)               15
76   PA27         (SCLK)               15
77   PA28         (NPCS0)              15
78   PB23         (unconnected)        15
USB   PB11         ID                  15
USB   PB10         VBOF                15
  

The "15" indicates a high-current pin.

HC pins can source 15mA and sink 9.
LC pins can source 3mA and sink 6.

______
Rob


I think pin 32 is HC and so is pin  72.

Other additions:
  timer pins: 2-5, 10-13, 58,60,61
  PWM pins: 6-9

pin attributes are set in hardware/arduino/sam/variants/arduino_due_x/variant.cpp

analogWrite() in  hardware/arduino/sam/cores/arduino/wiring_analog.c
does analog out using either hardware DAC, PWM, or Timer/Counter pins else does high/low on digital pins.
65  Products / Arduino Due / Re: Error in pinout table on: October 28, 2012, 09:26:28 am
More pin mapping details can be seen in

hardware/arduino/sam/variants/arduino_due_x/variant.cpp
66  Products / Arduino Due / Re: pgmspace.h is not supported? on: October 27, 2012, 06:57:09 pm
maple has  __FLASH__, e.g.,
uint32_t array[] __FLASH__ = {0, 1, 2};
  but that doesn't seem to work for DUE gcc. smiley-sad

But another thread in this Due forum notes that declaring things "const" puts them in flash ...  smiley
67  Products / Arduino Due / Re: random number generator (TRNG) API ? on: October 26, 2012, 10:46:25 am
mantoui, thanks i tried and it works... after a little tweak:

Code:
        TRNG->TRNG_IDR = 0xFFFFFFFF;
}

Why the hole 32bit if I didn't read the spec wrong the first bit would be enough!?

defines let you do something like
Code:
     TRNG->TRNG_IDR = TRNG_IDR_DATRDY; // disable interrupts
68  Products / Arduino Due / Re: random number generator (TRNG) API ? on: October 26, 2012, 06:12:12 am
...
is already available from CMSIS as TRNG. In general i see that registers structures are named using camel case (struct Trng) while actual instances of such structures are capital case (TRNG for example but some has numbering if there are many like TWI0 / TWI1).


Excellent!!

Is there documentation for CMSIS ??

thanks
69  Products / Arduino Due / Re: Error in pinout table on: October 26, 2012, 05:41:42 am
another column for the pin-map table might be the Timer Counter associations (Table 37-4)
70  Products / Arduino Due / random number generator (TRNG) API ? on: October 25, 2012, 07:12:57 pm
Processor has TRNG (datasheet chapter 43), but I don't see any API yet, and I presume it's not incorporated in random/rand.
Has anyone with a board tried it?  There is a Trng struct defined in arduino-1.5/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_trng.h, though I couldn't figure out how to reference it in sketch, so I copied and hacked it to get the following little sketch to compile, but I don't have a board yet to know if this works ...

Code:
// DUE's TRNG  32 bits every 84 ticks
//#include <component/component_trng.h>
#include "component_trng.h"
Trng *trng;

void setup() {
    trng = (Trng *) 0x400BC000;
    trng->TRNG_CR = TRNG_CR_KEY(0x524e47) | TRNG_CR_ENABLE;
    Serial.begin(9600);
}

void loop() {
    unsigned int rng;
    while (! (trng->TRNG_ISR & TRNG_ISR_DATRDY));  //wait til data ready
    rng = trng->TRNG_ODATA;
    Serial.println(rng,HEX);
    delay(3000);
}
71  Products / Arduino Due / Re: DUE temperature sensor API on: October 25, 2012, 07:05:16 pm
datasheet at http://www.atmel.com/Images/doc11057.pdf
72  Products / Arduino Due / Re: Error in pinout table on: October 25, 2012, 05:49:16 pm
Another pin out typo?   PA18 should be SCL1

Any chance of adding sink/source current limits to the table for each pin?  I know it's in Table 46-2 in ref manual , but ...
73  Products / Arduino Due / Re: DUE temperature sensor API on: October 25, 2012, 03:15:10 pm


So analogRead(15) will only work if the TSON bit in ADC_ACR is set by the looks of it. So it's up to the Arduino guys to do that or I guess you can do it yourself.
_____
Rob

in hardware/arduino/sam/system/libsam/source/adc.c there is an  adc_enable_ts() that sets   ADC_ACR_TSON, and the temperature calculation is described in Chapter 46.8 of reference pdf,  2.65mv/deg and 0.8v at 27C
74  Products / Arduino Due / Re: DUE temperature sensor API on: October 25, 2012, 11:14:09 am
Yes, I don't have a board yet either (wrong side of the pond) -- looking at the underlying source, me thinks an analogRead(15) might work ...
75  Products / Arduino Due / DUE temperature sensor API on: October 25, 2012, 10:43:24 am
Reference manual says processor has temperature sensor on ADC channel 15, what is the API for reading temperature?
Pages: 1 ... 3 4 [5] 6