summaryrefslogtreecommitdiff
path: root/openpicc/os/core/ARM7_AT91SAM7S/lib_AT91SAM7.c
blob: bf9ae6d5aab4b7c5f19799e57d992e6a01d03b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//* ----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//* ----------------------------------------------------------------------------
//* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
//* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
//* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
//* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
//* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
//* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//* ----------------------------------------------------------------------------
//* File Name           : lib_AT91SAM7S64.h
//* Object              : AT91SAM7S64 inlined functions
//* Generated           : AT91 SW Application Group  08/30/2005 (15:52:59)
//*

#include <sys/types.h>
#include <AT91SAM7.h>
#include <lib_AT91SAM7.h>

//*----------------------------------------------------------------------------
//* \fn    AT91F_AIC_ConfigureIt
//* \brief Interrupt Handler Initialization
//*----------------------------------------------------------------------------
unsigned int
AT91F_AIC_ConfigureIt (unsigned int irq_id,	// \arg interrupt number to initialize
		       unsigned int priority,	// \arg priority to give to the interrupt
		       unsigned int src_type,	// \arg activation and sense of activation
		       THandler handler)	// \arg address of the interrupt handler
{
  unsigned int oldHandler;
  unsigned int mask;

  oldHandler = AT91C_BASE_AIC->AIC_SVR[irq_id];

  mask = 0x1 << irq_id;
  //* Disable the interrupt on the interrupt controller
  AT91C_BASE_AIC->AIC_IDCR = mask;
  //* Save the interrupt handler routine pointer and the interrupt priority
  AT91C_BASE_AIC->AIC_SVR[irq_id] = (unsigned int) handler;
  //* Store the Source Mode Register
  AT91C_BASE_AIC->AIC_SMR[irq_id] = src_type | priority;
  //* Clear the interrupt on the interrupt controller
  AT91C_BASE_AIC->AIC_ICCR = mask;

  return (unsigned int) handler;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_AIC_SetExceptionVector
//* \brief Configure vector handler
//*----------------------------------------------------------------------------
unsigned int
AT91F_AIC_SetExceptionVector (unsigned int *pVector,	// \arg pointer to the AIC registers
			      THandler Handler)	// \arg Interrupt Handler
{
  unsigned int oldVector = *pVector;

  if ((unsigned int) Handler == (unsigned int) AT91C_AIC_BRANCH_OPCODE)
    *pVector = (unsigned int) AT91C_AIC_BRANCH_OPCODE;
  else
    *pVector =
      (((((unsigned int) Handler) - ((unsigned int) pVector) -
	 0x8) >> 2) & 0x00FFFFFF) | 0xEA000000;

  return oldVector;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_AIC_Open
//* \brief Set exception vectors and AIC registers to default values
//*----------------------------------------------------------------------------
void
AT91F_AIC_Open (THandler IrqHandler,	// \arg Default IRQ vector exception
		THandler FiqHandler,	// \arg Default FIQ vector exception
		THandler DefaultHandler,	// \arg Default Handler set in ISR
		THandler SpuriousHandler,	// \arg Default Spurious Handler
		unsigned int protectMode)	// \arg Debug Control Register
{
  int i;

  // Disable all interrupts and set IVR to the default handler
  for (i = 0; i < 32; ++i)
    {
      AT91F_AIC_DisableIt (i);
      AT91F_AIC_ConfigureIt (i, AT91C_AIC_PRIOR_LOWEST,
			     AT91C_AIC_SRCTYPE_HIGH_LEVEL, DefaultHandler);
    }

  // Set the IRQ exception vector
  AT91F_AIC_SetExceptionVector ((unsigned int *) 0x18, IrqHandler);
  // Set the Fast Interrupt exception vector
  AT91F_AIC_SetExceptionVector ((unsigned int *) 0x1C, FiqHandler);

  AT91C_BASE_AIC->AIC_SPU = (unsigned int) SpuriousHandler;
  AT91C_BASE_AIC->AIC_DCR = protectMode;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_PDC_Open
//* \brief Open PDC: disable TX and RX reset transfer descriptors, re-enable RX and TX
//*----------------------------------------------------------------------------
void
AT91F_PDC_Open (AT91PS_PDC pPDC)	// \arg pointer to a PDC controller
{
  //* Disable the RX and TX PDC transfer requests
  AT91F_PDC_DisableRx (pPDC);
  AT91F_PDC_DisableTx (pPDC);

  //* Reset all Counter register Next buffer first
  AT91F_PDC_SetNextTx (pPDC, NULL, 0);
  AT91F_PDC_SetNextRx (pPDC, NULL, 0);
  AT91F_PDC_SetTx (pPDC, NULL, 0);
  AT91F_PDC_SetRx (pPDC, NULL, 0);

  //* Enable the RX and TX PDC transfer requests
  AT91F_PDC_EnableRx (pPDC);
  AT91F_PDC_EnableTx (pPDC);
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_PDC_Close
//* \brief Close PDC: disable TX and RX reset transfer descriptors
//*----------------------------------------------------------------------------
void
AT91F_PDC_Close (AT91PS_PDC pPDC)	// \arg pointer to a PDC controller
{
  //* Disable the RX and TX PDC transfer requests
  AT91F_PDC_DisableRx (pPDC);
  AT91F_PDC_DisableTx (pPDC);

  //* Reset all Counter register Next buffer first
  AT91F_PDC_SetNextTx (pPDC, NULL, 0);
  AT91F_PDC_SetNextRx (pPDC, NULL, 0);
  AT91F_PDC_SetTx (pPDC, NULL, 0);
  AT91F_PDC_SetRx (pPDC, NULL, 0);

}

//*----------------------------------------------------------------------------
//* \fn    AT91F_PDC_SendFrame
//* \brief Close PDC: disable TX and RX reset transfer descriptors
//*----------------------------------------------------------------------------
unsigned int
AT91F_PDC_SendFrame (AT91PS_PDC pPDC,
		     const unsigned char *pBuffer,
		     unsigned int szBuffer,
		     const unsigned char *pNextBuffer,
		     unsigned int szNextBuffer)
{
  if (AT91F_PDC_IsTxEmpty (pPDC))
    {
      //* Buffer and next buffer can be initialized
      AT91F_PDC_SetTx (pPDC, pBuffer, szBuffer);
      AT91F_PDC_SetNextTx (pPDC, pNextBuffer, szNextBuffer);
      return 2;
    }
  else if (AT91F_PDC_IsNextTxEmpty (pPDC))
    {
      //* Only one buffer can be initialized
      AT91F_PDC_SetNextTx (pPDC, pBuffer, szBuffer);
      return 1;
    }
  else
    {
      //* All buffer are in use...
      return 0;
    }
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_PDC_ReceiveFrame
//* \brief Close PDC: disable TX and RX reset transfer descriptors
//*----------------------------------------------------------------------------
unsigned int
AT91F_PDC_ReceiveFrame (AT91PS_PDC pPDC,
			unsigned char *pBuffer,
			unsigned int szBuffer,
			unsigned char *pNextBuffer, unsigned int szNextBuffer)
{
  if (AT91F_PDC_IsRxEmpty (pPDC))
    {
      //* Buffer and next buffer can be initialized
      AT91F_PDC_SetRx (pPDC, pBuffer, szBuffer);
      AT91F_PDC_SetNextRx (pPDC, pNextBuffer, szNextBuffer);
      return 2;
    }
  else if (AT91F_PDC_IsNextRxEmpty (pPDC))
    {
      //* Only one buffer can be initialized
      AT91F_PDC_SetNextRx (pPDC, pBuffer, szBuffer);
      return 1;
    }
  else
    {
      //* All buffer are in use...
      return 0;
    }
}

//*------------------------------------------------------------------------------
//* \fn    AT91F_PMC_GetMasterClock
//* \brief Return master clock in Hz which correponds to processor clock for ARM7
//*------------------------------------------------------------------------------
unsigned int
AT91F_PMC_GetMasterClock (AT91PS_PMC pPMC,	// \arg pointer to PMC controller
			  AT91PS_CKGR pCKGR,	// \arg pointer to CKGR controller
			  unsigned int slowClock)	// \arg slowClock in Hz
{
  unsigned int reg = pPMC->PMC_MCKR;
  unsigned int prescaler = (1 << ((reg & AT91C_PMC_PRES) >> 2));
  unsigned int pllDivider, pllMultiplier;

  switch (reg & AT91C_PMC_CSS)
    {
    case AT91C_PMC_CSS_SLOW_CLK:	// Slow clock selected
      return slowClock / prescaler;
    case AT91C_PMC_CSS_MAIN_CLK:	// Main clock is selected
      return AT91F_CKGR_GetMainClock (pCKGR, slowClock) / prescaler;
    case AT91C_PMC_CSS_PLL_CLK:	// PLLB clock is selected
      reg = pCKGR->CKGR_PLLR;
      pllDivider = (reg & AT91C_CKGR_DIV);
      pllMultiplier = ((reg & AT91C_CKGR_MUL) >> 16) + 1;
      return AT91F_CKGR_GetMainClock (pCKGR,
				      slowClock) / pllDivider *
	pllMultiplier / prescaler;
    }
  return 0;
}

//*--------------------------------------------------------------------------------------
//* \fn     AT91F_RTT_ReadValue()
//* \brief  Read the RTT value
//*--------------------------------------------------------------------------------------
unsigned int
AT91F_RTTReadValue (AT91PS_RTTC pRTTC)
{
  register volatile unsigned int val1, val2;
  do
    {
      val1 = pRTTC->RTTC_RTVR;
      val2 = pRTTC->RTTC_RTVR;
    }
  while (val1 != val2);
  return (val1);
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_SPI_Close
//* \brief Close SPI: disable IT disable transfert, close PDC
//*----------------------------------------------------------------------------
void
AT91F_SPI_Close (AT91PS_SPI pSPI)	// \arg pointer to a SPI controller
{
  //* Reset all the Chip Select register
  pSPI->SPI_CSR[0] = 0;
  pSPI->SPI_CSR[1] = 0;
  pSPI->SPI_CSR[2] = 0;
  pSPI->SPI_CSR[3] = 0;

  //* Reset the SPI mode
  pSPI->SPI_MR = 0;

  //* Disable all interrupts
  pSPI->SPI_IDR = 0xFFFFFFFF;

  //* Abort the Peripheral Data Transfers
  AT91F_PDC_Close ((AT91PS_PDC) & (pSPI->SPI_RPR));

  //* Disable receiver and transmitter and stop any activity immediately
  pSPI->SPI_CR = AT91C_SPI_SPIDIS;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_ADC_CfgTimings
//* \brief Configure the different necessary timings of the ADC controller
//*----------------------------------------------------------------------------
void
AT91F_ADC_CfgTimings (AT91PS_ADC pADC,	// pointer to a ADC controller
		      unsigned int mck_clock,	// in MHz 
		      unsigned int adc_clock,	// in MHz 
		      unsigned int startup_time,	// in us 
		      unsigned int sample_and_hold_time)	// in ns  
{
  unsigned int prescal, startup, shtim;

  prescal = mck_clock / (2 * adc_clock) - 1;
  startup = adc_clock * startup_time / 8 - 1;
  shtim = adc_clock * sample_and_hold_time / 1000 - 1;

  //* Write to the MR register
  pADC->ADC_MR =
    ((prescal << 8) & AT91C_ADC_PRESCAL) | ((startup << 16) &
					    AT91C_ADC_STARTUP) | ((shtim <<
								   24) &
								  AT91C_ADC_SHTIM);
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_SSC_SetBaudrate
//* \brief Set the baudrate according to the CPU clock
//*----------------------------------------------------------------------------
void
AT91F_SSC_SetBaudrate (AT91PS_SSC pSSC,	// \arg pointer to a SSC controller
		       unsigned int mainClock,	// \arg peripheral clock
		       unsigned int speed)	// \arg SSC baudrate
{
  unsigned int baud_value;
  //* Define the baud rate divisor register
  if (speed == 0)
    baud_value = 0;
  else
    {
      baud_value = (unsigned int) (mainClock * 10) / (2 * speed);
      if ((baud_value % 10) >= 5)
	baud_value = (baud_value / 10) + 1;
      else
	baud_value /= 10;
    }

  pSSC->SSC_CMR = baud_value;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_SSC_Configure
//* \brief Configure SSC
//*----------------------------------------------------------------------------
void
AT91F_SSC_Configure (AT91PS_SSC pSSC,	// \arg pointer to a SSC controller
		     unsigned int syst_clock,	// \arg System Clock Frequency
		     unsigned int baud_rate,	// \arg Expected Baud Rate Frequency
		     unsigned int clock_rx,	// \arg Receiver Clock Parameters
		     unsigned int mode_rx,	// \arg mode Register to be programmed
		     unsigned int clock_tx,	// \arg Transmitter Clock Parameters
		     unsigned int mode_tx)	// \arg mode Register to be programmed
{
  //* Disable interrupts
  pSSC->SSC_IDR = (unsigned int) -1;

  //* Reset receiver and transmitter
  pSSC->SSC_CR = AT91C_SSC_SWRST | AT91C_SSC_RXDIS | AT91C_SSC_TXDIS;

  //* Define the Clock Mode Register
  AT91F_SSC_SetBaudrate (pSSC, syst_clock, baud_rate);

  //* Write the Receive Clock Mode Register
  pSSC->SSC_RCMR = clock_rx;

  //* Write the Transmit Clock Mode Register
  pSSC->SSC_TCMR = clock_tx;

  //* Write the Receive Frame Mode Register
  pSSC->SSC_RFMR = mode_rx;

  //* Write the Transmit Frame Mode Register
  pSSC->SSC_TFMR = mode_tx;

  //* Clear Transmit and Receive Counters
  AT91F_PDC_Open ((AT91PS_PDC) & (pSSC->SSC_RPR));


}

//*----------------------------------------------------------------------------
//* \fn    AT91F_US_Configure
//* \brief Configure USART
//*----------------------------------------------------------------------------
void
AT91F_US_Configure (AT91PS_USART pUSART,	// \arg pointer to a USART controller
		    unsigned int mainClock,	// \arg peripheral clock
		    unsigned int mode,	// \arg mode Register to be programmed
		    unsigned int baudRate,	// \arg baudrate to be programmed
		    unsigned int timeguard)	// \arg timeguard to be programmed
{
  //* Disable interrupts
  pUSART->US_IDR = (unsigned int) -1;

  //* Reset receiver and transmitter
  pUSART->US_CR =
    AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS;

  //* Define the baud rate divisor register
  AT91F_US_SetBaudrate (pUSART, mainClock, baudRate);

  //* Write the Timeguard Register
  AT91F_US_SetTimeguard (pUSART, timeguard);

  //* Clear Transmit and Receive Counters
  AT91F_PDC_Open ((AT91PS_PDC) & (pUSART->US_RPR));

  //* Define the USART mode
  pUSART->US_MR = mode;

}

//*----------------------------------------------------------------------------
//* \fn    AT91F_US_Close
//* \brief Close USART: disable IT disable receiver and transmitter, close PDC
//*----------------------------------------------------------------------------
void
AT91F_US_Close (AT91PS_USART pUSART)	// \arg pointer to a USART controller
{
  //* Reset the baud rate divisor register
  pUSART->US_BRGR = 0;

  //* Reset the USART mode
  pUSART->US_MR = 0;

  //* Reset the Timeguard Register
  pUSART->US_TTGR = 0;

  //* Disable all interrupts
  pUSART->US_IDR = 0xFFFFFFFF;

  //* Abort the Peripheral Data Transfers
  AT91F_PDC_Close ((AT91PS_PDC) & (pUSART->US_RPR));

  //* Disable receiver and transmitter and stop any activity immediately
  pUSART->US_CR =
    AT91C_US_TXDIS | AT91C_US_RXDIS | AT91C_US_RSTTX | AT91C_US_RSTRX;
}
personal git repositories of Harald Welte. Your mileage may vary