1
|
//* ----------------------------------------------------------------------------
|
2
|
//* ATMEL Microcontroller Software Support - ROUSSET -
|
3
|
//* ----------------------------------------------------------------------------
|
4
|
//* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
5
|
//* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
6
|
//* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
7
|
//* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
8
|
//* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
//* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
10
|
//* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
11
|
//* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
12
|
//* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
13
|
//* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
//* ----------------------------------------------------------------------------
|
15
|
//* File Name : lib_AT91SAM7S64.h
|
16
|
//* Object : AT91SAM7S64 inlined functions
|
17
|
//* Generated : AT91 SW Application Group 08/30/2005 (15:52:59)
|
18
|
//*
|
19
|
|
20
|
#include <sys/types.h>
|
21
|
#include <AT91SAM7.h>
|
22
|
#include <lib_AT91SAM7.h>
|
23
|
|
24
|
//*----------------------------------------------------------------------------
|
25
|
//* \fn AT91F_AIC_ConfigureIt
|
26
|
//* \brief Interrupt Handler Initialization
|
27
|
//*----------------------------------------------------------------------------
|
28
|
unsigned int
|
29
|
AT91F_AIC_ConfigureIt (unsigned int irq_id, // \arg interrupt number to initialize
|
30
|
unsigned int priority, // \arg priority to give to the interrupt
|
31
|
unsigned int src_type, // \arg activation and sense of activation
|
32
|
THandler handler) // \arg address of the interrupt handler
|
33
|
{
|
34
|
unsigned int oldHandler;
|
35
|
unsigned int mask;
|
36
|
|
37
|
oldHandler = AT91C_BASE_AIC->AIC_SVR[irq_id];
|
38
|
|
39
|
mask = 0x1 << irq_id;
|
40
|
//* Disable the interrupt on the interrupt controller
|
41
|
AT91C_BASE_AIC->AIC_IDCR = mask;
|
42
|
//* Save the interrupt handler routine pointer and the interrupt priority
|
43
|
AT91C_BASE_AIC->AIC_SVR[irq_id] = (unsigned int) handler;
|
44
|
//* Store the Source Mode Register
|
45
|
AT91C_BASE_AIC->AIC_SMR[irq_id] = src_type | priority;
|
46
|
//* Clear the interrupt on the interrupt controller
|
47
|
AT91C_BASE_AIC->AIC_ICCR = mask;
|
48
|
|
49
|
return (unsigned int) handler;
|
50
|
}
|
51
|
|
52
|
//*----------------------------------------------------------------------------
|
53
|
//* \fn AT91F_AIC_SetExceptionVector
|
54
|
//* \brief Configure vector handler
|
55
|
//*----------------------------------------------------------------------------
|
56
|
unsigned int
|
57
|
AT91F_AIC_SetExceptionVector (unsigned int *pVector, // \arg pointer to the AIC registers
|
58
|
THandler Handler) // \arg Interrupt Handler
|
59
|
{
|
60
|
unsigned int oldVector = *pVector;
|
61
|
|
62
|
if ((unsigned int) Handler == (unsigned int) AT91C_AIC_BRANCH_OPCODE)
|
63
|
*pVector = (unsigned int) AT91C_AIC_BRANCH_OPCODE;
|
64
|
else
|
65
|
*pVector =
|
66
|
(((((unsigned int) Handler) - ((unsigned int) pVector) -
|
67
|
0x8) >> 2) & 0x00FFFFFF) | 0xEA000000;
|
68
|
|
69
|
return oldVector;
|
70
|
}
|
71
|
|
72
|
//*----------------------------------------------------------------------------
|
73
|
//* \fn AT91F_AIC_Open
|
74
|
//* \brief Set exception vectors and AIC registers to default values
|
75
|
//*----------------------------------------------------------------------------
|
76
|
void
|
77
|
AT91F_AIC_Open (THandler IrqHandler, // \arg Default IRQ vector exception
|
78
|
THandler FiqHandler, // \arg Default FIQ vector exception
|
79
|
THandler DefaultHandler, // \arg Default Handler set in ISR
|
80
|
THandler SpuriousHandler, // \arg Default Spurious Handler
|
81
|
unsigned int protectMode) // \arg Debug Control Register
|
82
|
{
|
83
|
int i;
|
84
|
|
85
|
// Disable all interrupts and set IVR to the default handler
|
86
|
for (i = 0; i < 32; ++i)
|
87
|
{
|
88
|
AT91F_AIC_DisableIt (i);
|
89
|
AT91F_AIC_ConfigureIt (i, AT91C_AIC_PRIOR_LOWEST,
|
90
|
AT91C_AIC_SRCTYPE_HIGH_LEVEL, DefaultHandler);
|
91
|
}
|
92
|
|
93
|
// Set the IRQ exception vector
|
94
|
AT91F_AIC_SetExceptionVector ((unsigned int *) 0x18, IrqHandler);
|
95
|
// Set the Fast Interrupt exception vector
|
96
|
AT91F_AIC_SetExceptionVector ((unsigned int *) 0x1C, FiqHandler);
|
97
|
|
98
|
AT91C_BASE_AIC->AIC_SPU = (unsigned int) SpuriousHandler;
|
99
|
AT91C_BASE_AIC->AIC_DCR = protectMode;
|
100
|
}
|
101
|
|
102
|
//*----------------------------------------------------------------------------
|
103
|
//* \fn AT91F_PDC_Open
|
104
|
//* \brief Open PDC: disable TX and RX reset transfer descriptors, re-enable RX and TX
|
105
|
//*----------------------------------------------------------------------------
|
106
|
void
|
107
|
AT91F_PDC_Open (AT91PS_PDC pPDC) // \arg pointer to a PDC controller
|
108
|
{
|
109
|
//* Disable the RX and TX PDC transfer requests
|
110
|
AT91F_PDC_DisableRx (pPDC);
|
111
|
AT91F_PDC_DisableTx (pPDC);
|
112
|
|
113
|
//* Reset all Counter register Next buffer first
|
114
|
AT91F_PDC_SetNextTx (pPDC, NULL, 0);
|
115
|
AT91F_PDC_SetNextRx (pPDC, NULL, 0);
|
116
|
AT91F_PDC_SetTx (pPDC, NULL, 0);
|
117
|
AT91F_PDC_SetRx (pPDC, NULL, 0);
|
118
|
|
119
|
//* Enable the RX and TX PDC transfer requests
|
120
|
AT91F_PDC_EnableRx (pPDC);
|
121
|
AT91F_PDC_EnableTx (pPDC);
|
122
|
}
|
123
|
|
124
|
//*----------------------------------------------------------------------------
|
125
|
//* \fn AT91F_PDC_Close
|
126
|
//* \brief Close PDC: disable TX and RX reset transfer descriptors
|
127
|
//*----------------------------------------------------------------------------
|
128
|
void
|
129
|
AT91F_PDC_Close (AT91PS_PDC pPDC) // \arg pointer to a PDC controller
|
130
|
{
|
131
|
//* Disable the RX and TX PDC transfer requests
|
132
|
AT91F_PDC_DisableRx (pPDC);
|
133
|
AT91F_PDC_DisableTx (pPDC);
|
134
|
|
135
|
//* Reset all Counter register Next buffer first
|
136
|
AT91F_PDC_SetNextTx (pPDC, NULL, 0);
|
137
|
AT91F_PDC_SetNextRx (pPDC, NULL, 0);
|
138
|
AT91F_PDC_SetTx (pPDC, NULL, 0);
|
139
|
AT91F_PDC_SetRx (pPDC, NULL, 0);
|
140
|
|
141
|
}
|
142
|
|
143
|
//*----------------------------------------------------------------------------
|
144
|
//* \fn AT91F_PDC_SendFrame
|
145
|
//* \brief Close PDC: disable TX and RX reset transfer descriptors
|
146
|
//*----------------------------------------------------------------------------
|
147
|
unsigned int
|
148
|
AT91F_PDC_SendFrame (AT91PS_PDC pPDC,
|
149
|
const unsigned char *pBuffer,
|
150
|
unsigned int szBuffer,
|
151
|
const unsigned char *pNextBuffer,
|
152
|
unsigned int szNextBuffer)
|
153
|
{
|
154
|
if (AT91F_PDC_IsTxEmpty (pPDC))
|
155
|
{
|
156
|
//* Buffer and next buffer can be initialized
|
157
|
AT91F_PDC_SetTx (pPDC, pBuffer, szBuffer);
|
158
|
AT91F_PDC_SetNextTx (pPDC, pNextBuffer, szNextBuffer);
|
159
|
return 2;
|
160
|
}
|
161
|
else if (AT91F_PDC_IsNextTxEmpty (pPDC))
|
162
|
{
|
163
|
//* Only one buffer can be initialized
|
164
|
AT91F_PDC_SetNextTx (pPDC, pBuffer, szBuffer);
|
165
|
return 1;
|
166
|
}
|
167
|
else
|
168
|
{
|
169
|
//* All buffer are in use...
|
170
|
return 0;
|
171
|
}
|
172
|
}
|
173
|
|
174
|
//*----------------------------------------------------------------------------
|
175
|
//* \fn AT91F_PDC_ReceiveFrame
|
176
|
//* \brief Close PDC: disable TX and RX reset transfer descriptors
|
177
|
//*----------------------------------------------------------------------------
|
178
|
unsigned int
|
179
|
AT91F_PDC_ReceiveFrame (AT91PS_PDC pPDC,
|
180
|
unsigned char *pBuffer,
|
181
|
unsigned int szBuffer,
|
182
|
unsigned char *pNextBuffer, unsigned int szNextBuffer)
|
183
|
{
|
184
|
if (AT91F_PDC_IsRxEmpty (pPDC))
|
185
|
{
|
186
|
//* Buffer and next buffer can be initialized
|
187
|
AT91F_PDC_SetRx (pPDC, pBuffer, szBuffer);
|
188
|
AT91F_PDC_SetNextRx (pPDC, pNextBuffer, szNextBuffer);
|
189
|
return 2;
|
190
|
}
|
191
|
else if (AT91F_PDC_IsNextRxEmpty (pPDC))
|
192
|
{
|
193
|
//* Only one buffer can be initialized
|
194
|
AT91F_PDC_SetNextRx (pPDC, pBuffer, szBuffer);
|
195
|
return 1;
|
196
|
}
|
197
|
else
|
198
|
{
|
199
|
//* All buffer are in use...
|
200
|
return 0;
|
201
|
}
|
202
|
}
|
203
|
|
204
|
//*------------------------------------------------------------------------------
|
205
|
//* \fn AT91F_PMC_GetMasterClock
|
206
|
//* \brief Return master clock in Hz which correponds to processor clock for ARM7
|
207
|
//*------------------------------------------------------------------------------
|
208
|
unsigned int
|
209
|
AT91F_PMC_GetMasterClock (AT91PS_PMC pPMC, // \arg pointer to PMC controller
|
210
|
AT91PS_CKGR pCKGR, // \arg pointer to CKGR controller
|
211
|
unsigned int slowClock) // \arg slowClock in Hz
|
212
|
{
|
213
|
unsigned int reg = pPMC->PMC_MCKR;
|
214
|
unsigned int prescaler = (1 << ((reg & AT91C_PMC_PRES) >> 2));
|
215
|
unsigned int pllDivider, pllMultiplier;
|
216
|
|
217
|
switch (reg & AT91C_PMC_CSS)
|
218
|
{
|
219
|
case AT91C_PMC_CSS_SLOW_CLK: // Slow clock selected
|
220
|
return slowClock / prescaler;
|
221
|
case AT91C_PMC_CSS_MAIN_CLK: // Main clock is selected
|
222
|
return AT91F_CKGR_GetMainClock (pCKGR, slowClock) / prescaler;
|
223
|
case AT91C_PMC_CSS_PLL_CLK: // PLLB clock is selected
|
224
|
reg = pCKGR->CKGR_PLLR;
|
225
|
pllDivider = (reg & AT91C_CKGR_DIV);
|
226
|
pllMultiplier = ((reg & AT91C_CKGR_MUL) >> 16) + 1;
|
227
|
return AT91F_CKGR_GetMainClock (pCKGR,
|
228
|
slowClock) / pllDivider *
|
229
|
pllMultiplier / prescaler;
|
230
|
}
|
231
|
return 0;
|
232
|
}
|
233
|
|
234
|
//*--------------------------------------------------------------------------------------
|
235
|
//* \fn AT91F_RTT_ReadValue()
|
236
|
//* \brief Read the RTT value
|
237
|
//*--------------------------------------------------------------------------------------
|
238
|
unsigned int
|
239
|
AT91F_RTTReadValue (AT91PS_RTTC pRTTC)
|
240
|
{
|
241
|
register volatile unsigned int val1, val2;
|
242
|
do
|
243
|
{
|
244
|
val1 = pRTTC->RTTC_RTVR;
|
245
|
val2 = pRTTC->RTTC_RTVR;
|
246
|
}
|
247
|
while (val1 != val2);
|
248
|
return (val1);
|
249
|
}
|
250
|
|
251
|
//*----------------------------------------------------------------------------
|
252
|
//* \fn AT91F_SPI_Close
|
253
|
//* \brief Close SPI: disable IT disable transfert, close PDC
|
254
|
//*----------------------------------------------------------------------------
|
255
|
void
|
256
|
AT91F_SPI_Close (AT91PS_SPI pSPI) // \arg pointer to a SPI controller
|
257
|
{
|
258
|
//* Reset all the Chip Select register
|
259
|
pSPI->SPI_CSR[0] = 0;
|
260
|
pSPI->SPI_CSR[1] = 0;
|
261
|
pSPI->SPI_CSR[2] = 0;
|
262
|
pSPI->SPI_CSR[3] = 0;
|
263
|
|
264
|
//* Reset the SPI mode
|
265
|
pSPI->SPI_MR = 0;
|
266
|
|
267
|
//* Disable all interrupts
|
268
|
pSPI->SPI_IDR = 0xFFFFFFFF;
|
269
|
|
270
|
//* Abort the Peripheral Data Transfers
|
271
|
AT91F_PDC_Close ((AT91PS_PDC) & (pSPI->SPI_RPR));
|
272
|
|
273
|
//* Disable receiver and transmitter and stop any activity immediately
|
274
|
pSPI->SPI_CR = AT91C_SPI_SPIDIS;
|
275
|
}
|
276
|
|
277
|
//*----------------------------------------------------------------------------
|
278
|
//* \fn AT91F_ADC_CfgTimings
|
279
|
//* \brief Configure the different necessary timings of the ADC controller
|
280
|
//*----------------------------------------------------------------------------
|
281
|
void
|
282
|
AT91F_ADC_CfgTimings (AT91PS_ADC pADC, // pointer to a ADC controller
|
283
|
unsigned int mck_clock, // in MHz
|
284
|
unsigned int adc_clock, // in MHz
|
285
|
unsigned int startup_time, // in us
|
286
|
unsigned int sample_and_hold_time) // in ns
|
287
|
{
|
288
|
unsigned int prescal, startup, shtim;
|
289
|
|
290
|
prescal = mck_clock / (2 * adc_clock) - 1;
|
291
|
startup = adc_clock * startup_time / 8 - 1;
|
292
|
shtim = adc_clock * sample_and_hold_time / 1000 - 1;
|
293
|
|
294
|
//* Write to the MR register
|
295
|
pADC->ADC_MR =
|
296
|
((prescal << 8) & AT91C_ADC_PRESCAL) | ((startup << 16) &
|
297
|
AT91C_ADC_STARTUP) | ((shtim <<
|
298
|
24) &
|
299
|
AT91C_ADC_SHTIM);
|
300
|
}
|
301
|
|
302
|
//*----------------------------------------------------------------------------
|
303
|
//* \fn AT91F_SSC_SetBaudrate
|
304
|
//* \brief Set the baudrate according to the CPU clock
|
305
|
//*----------------------------------------------------------------------------
|
306
|
void
|
307
|
AT91F_SSC_SetBaudrate (AT91PS_SSC pSSC, // \arg pointer to a SSC controller
|
308
|
unsigned int mainClock, // \arg peripheral clock
|
309
|
unsigned int speed) // \arg SSC baudrate
|
310
|
{
|
311
|
unsigned int baud_value;
|
312
|
//* Define the baud rate divisor register
|
313
|
if (speed == 0)
|
314
|
baud_value = 0;
|
315
|
else
|
316
|
{
|
317
|
baud_value = (unsigned int) (mainClock * 10) / (2 * speed);
|
318
|
if ((baud_value % 10) >= 5)
|
319
|
baud_value = (baud_value / 10) + 1;
|
320
|
else
|
321
|
baud_value /= 10;
|
322
|
}
|
323
|
|
324
|
pSSC->SSC_CMR = baud_value;
|
325
|
}
|
326
|
|
327
|
//*----------------------------------------------------------------------------
|
328
|
//* \fn AT91F_SSC_Configure
|
329
|
//* \brief Configure SSC
|
330
|
//*----------------------------------------------------------------------------
|
331
|
void
|
332
|
AT91F_SSC_Configure (AT91PS_SSC pSSC, // \arg pointer to a SSC controller
|
333
|
unsigned int syst_clock, // \arg System Clock Frequency
|
334
|
unsigned int baud_rate, // \arg Expected Baud Rate Frequency
|
335
|
unsigned int clock_rx, // \arg Receiver Clock Parameters
|
336
|
unsigned int mode_rx, // \arg mode Register to be programmed
|
337
|
unsigned int clock_tx, // \arg Transmitter Clock Parameters
|
338
|
unsigned int mode_tx) // \arg mode Register to be programmed
|
339
|
{
|
340
|
//* Disable interrupts
|
341
|
pSSC->SSC_IDR = (unsigned int) -1;
|
342
|
|
343
|
//* Reset receiver and transmitter
|
344
|
pSSC->SSC_CR = AT91C_SSC_SWRST | AT91C_SSC_RXDIS | AT91C_SSC_TXDIS;
|
345
|
|
346
|
//* Define the Clock Mode Register
|
347
|
AT91F_SSC_SetBaudrate (pSSC, syst_clock, baud_rate);
|
348
|
|
349
|
//* Write the Receive Clock Mode Register
|
350
|
pSSC->SSC_RCMR = clock_rx;
|
351
|
|
352
|
//* Write the Transmit Clock Mode Register
|
353
|
pSSC->SSC_TCMR = clock_tx;
|
354
|
|
355
|
//* Write the Receive Frame Mode Register
|
356
|
pSSC->SSC_RFMR = mode_rx;
|
357
|
|
358
|
//* Write the Transmit Frame Mode Register
|
359
|
pSSC->SSC_TFMR = mode_tx;
|
360
|
|
361
|
//* Clear Transmit and Receive Counters
|
362
|
AT91F_PDC_Open ((AT91PS_PDC) & (pSSC->SSC_RPR));
|
363
|
|
364
|
|
365
|
}
|
366
|
|
367
|
//*----------------------------------------------------------------------------
|
368
|
//* \fn AT91F_US_Configure
|
369
|
//* \brief Configure USART
|
370
|
//*----------------------------------------------------------------------------
|
371
|
void
|
372
|
AT91F_US_Configure (AT91PS_USART pUSART, // \arg pointer to a USART controller
|
373
|
unsigned int mainClock, // \arg peripheral clock
|
374
|
unsigned int mode, // \arg mode Register to be programmed
|
375
|
unsigned int baudRate, // \arg baudrate to be programmed
|
376
|
unsigned int timeguard) // \arg timeguard to be programmed
|
377
|
{
|
378
|
//* Disable interrupts
|
379
|
pUSART->US_IDR = (unsigned int) -1;
|
380
|
|
381
|
//* Reset receiver and transmitter
|
382
|
pUSART->US_CR =
|
383
|
AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS;
|
384
|
|
385
|
//* Define the baud rate divisor register
|
386
|
AT91F_US_SetBaudrate (pUSART, mainClock, baudRate);
|
387
|
|
388
|
//* Write the Timeguard Register
|
389
|
AT91F_US_SetTimeguard (pUSART, timeguard);
|
390
|
|
391
|
//* Clear Transmit and Receive Counters
|
392
|
AT91F_PDC_Open ((AT91PS_PDC) & (pUSART->US_RPR));
|
393
|
|
394
|
//* Define the USART mode
|
395
|
pUSART->US_MR = mode;
|
396
|
|
397
|
}
|
398
|
|
399
|
//*----------------------------------------------------------------------------
|
400
|
//* \fn AT91F_US_Close
|
401
|
//* \brief Close USART: disable IT disable receiver and transmitter, close PDC
|
402
|
//*----------------------------------------------------------------------------
|
403
|
void
|
404
|
AT91F_US_Close (AT91PS_USART pUSART) // \arg pointer to a USART controller
|
405
|
{
|
406
|
//* Reset the baud rate divisor register
|
407
|
pUSART->US_BRGR = 0;
|
408
|
|
409
|
//* Reset the USART mode
|
410
|
pUSART->US_MR = 0;
|
411
|
|
412
|
//* Reset the Timeguard Register
|
413
|
pUSART->US_TTGR = 0;
|
414
|
|
415
|
//* Disable all interrupts
|
416
|
pUSART->US_IDR = 0xFFFFFFFF;
|
417
|
|
418
|
//* Abort the Peripheral Data Transfers
|
419
|
AT91F_PDC_Close ((AT91PS_PDC) & (pUSART->US_RPR));
|
420
|
|
421
|
//* Disable receiver and transmitter and stop any activity immediately
|
422
|
pUSART->US_CR =
|
423
|
AT91C_US_TXDIS | AT91C_US_RXDIS | AT91C_US_RSTTX | AT91C_US_RSTRX;
|
424
|
}
|