1 /* ----------------------------------------------------------------------------
\r
2 * SAM Software Package License
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2014, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
30 /** \addtogroup pio_capture_module Working with PIO Parallel Capture Mode
\r
31 * \ingroup peripherals_module
\r
32 * The PIO Parallel Capture Mode driver provides the interface to configure and use the
\r
33 * PIO Parallel Capture Mode peripheral.\n
\r
35 * The PIO Controller integrates an interface able to read data from a CMOS digital
\r
36 * image sensor, a high-speed parallel ADC, a DSP synchronous port in synchronous
\r
37 * mode, etc.... For better understanding and to ease reading, the following
\r
38 * description uses an example with a CMOS digital image sensor
\r
40 * To use the PIO Parallel Capture, the user has to follow these few steps:
\r
42 * <li> Enable PIOA peripheral clock </li>
\r
43 * <li> Configure the PDC </li>
\r
44 * <li> Configure the PIO Capture interrupt </li>
\r
45 * <li> Enable the PDC </li>
\r
46 * <li> Enable the PIO Capture </li>
\r
47 * <li> Wait for interrupt </li>
\r
48 * <li> Disable the interrupt </li>
\r
49 * <li> Read the DATA </li>
\r
52 * For more accurate information, please look at the PIO Parallel Capture Mode section of the
\r
57 * -# Configurate the interrupt for PIOA, can be done by PIO_InitializeInterrupts()
\r
58 * -# Initialize the PIO Parallel Capture API by filing the SpioCaptureInit structur.
\r
60 * - alwaysSampling: for sample data with or without take in account ENABLE pins.
\r
61 * - halfSampling: for sample all data or only one time out of two
\r
62 * -# Call PIO_CaptureInit() for init and enable the PDC, init the PIO capture.
\r
63 * -# Call PIO_CaptureEnable() for enable the PIO Parallel Capture.
\r
64 * -# When an interrupt is received, the PIO_CaptureHandler() is call and the respective
\r
65 * callback is launch.
\r
66 * -# When the transfer is complete, the user need to disable interrupt with
\r
67 * PIO_CaptureDisableIt(). Otherway, the PDC will send an interrupt.
\r
68 * -# The data receive by the PIO Parallel Capture is inside the buffer passed in the
\r
69 * PIO_CaptureInit().
\r
72 * \ref pio_capture.c\n
\r
73 * \ref pio_capture.h\n
\r
80 * Implementation of PIO Parallel Capture.
\r
84 /*----------------------------------------------------------------------------
\r
86 *----------------------------------------------------------------------------*/
\r
92 /*----------------------------------------------------------------------------
\r
94 *----------------------------------------------------------------------------*/
\r
95 /** Copy the API structure for interrupt handler */
\r
96 static SpioCaptureInit* _PioCaptureCopy;
\r
98 /*----------------------------------------------------------------------------
\r
100 *----------------------------------------------------------------------------*/
\r
102 /*----------------------------------------------------------------------------*/
\r
104 * \brief The PIO_CaptureHandler must be called by the PIO Capture Interrupt
\r
105 * Service Routine with the corresponding PIO Capture instance.
\r
107 /*----------------------------------------------------------------------------*/
\r
108 extern void PIO_CaptureHandler( void )
\r
110 volatile uint32_t pio_captureSr;
\r
113 /* Read the status register*/
\r
114 pio_captureSr = PIOA->PIO_PCISR ;
\r
116 pio_captureSr = k & PIOA->PIO_PCIMR ;
\r
118 if (pio_captureSr & PIO_PCISR_DRDY)
\r
120 /* Parallel Capture Mode Data Ready */
\r
121 if ( _PioCaptureCopy->CbkDataReady != NULL )
\r
123 _PioCaptureCopy->CbkDataReady( _PioCaptureCopy );
\r
127 TRACE_DEBUG("IT PIO Capture Data Ready received (no callback)\n\r");
\r
131 if (pio_captureSr & PIO_PCISR_OVRE)
\r
133 /* Parallel Capture Mode Overrun Error */
\r
134 if ( _PioCaptureCopy->CbkOverrun != NULL )
\r
136 _PioCaptureCopy->CbkOverrun( _PioCaptureCopy );
\r
140 TRACE_DEBUG("IT PIO Capture Overrun Error received (no callback)\n\r");
\r
144 if (pio_captureSr & PIO_PCISR_RXBUFF)
\r
146 /* Reception Buffer Full */
\r
147 if ( _PioCaptureCopy->CbkBuffFull != NULL )
\r
149 _PioCaptureCopy->CbkBuffFull( _PioCaptureCopy );
\r
153 TRACE_DEBUG("IT PIO Capture Reception Buffer Full received (no callback)\n\r");
\r
157 if (pio_captureSr & PIO_PCISR_ENDRX)
\r
159 /* End of Reception Transfer */
\r
160 if ( _PioCaptureCopy->CbkEndReception != NULL )
\r
162 _PioCaptureCopy->CbkEndReception( _PioCaptureCopy );
\r
166 TRACE_DEBUG("IT PIO Capture End of Reception Transfer received (no callback)\n\r");
\r
171 /*----------------------------------------------------------------------------*/
\r
173 * \brief Disable Interupt of the PIO Capture
\r
174 * \param itToDisable : Interrupt to disable
\r
176 /*----------------------------------------------------------------------------*/
\r
177 void PIO_CaptureDisableIt( uint32_t itToDisable )
\r
179 /* Parallel capture mode is enabled */
\r
180 PIOA->PIO_PCIDR = itToDisable;
\r
183 /*----------------------------------------------------------------------------*/
\r
185 * \brief Enable Interupt of the PIO Capture
\r
186 * \param itToEnable : Interrupt to enable
\r
188 /*----------------------------------------------------------------------------*/
\r
189 void PIO_CaptureEnableIt( uint32_t itToEnable )
\r
191 /* Parallel capture mode is enabled */
\r
192 PIOA->PIO_PCIER = itToEnable;
\r
195 /*----------------------------------------------------------------------------*/
\r
197 * \brief Enable the PIO Capture
\r
199 /*----------------------------------------------------------------------------*/
\r
200 void PIO_CaptureEnable( void )
\r
202 /* PDC: Receive Pointer Register */
\r
203 // PIOA->PIO_RPR = (uint32_t)_PioCaptureCopy->pData ;
\r
204 // /* PDC: Receive Counter Register */
\r
205 // /* Starts peripheral data transfer if corresponding channel is active */
\r
206 // PIOA->PIO_RCR = PIO_RCR_RXCTR(_PioCaptureCopy->dPDCsize) ;
\r
208 /* Parallel capture mode is enabled */
\r
209 PIOA->PIO_PCMR |= PIO_PCMR_PCEN ;
\r
212 /*----------------------------------------------------------------------------*/
\r
214 * \brief Disable the PIO Capture
\r
216 /*----------------------------------------------------------------------------*/
\r
217 void PIO_CaptureDisable( void )
\r
219 /* Parallel capture mode is disabled */
\r
220 PIOA->PIO_PCMR &= (uint32_t)(~PIO_PCMR_PCEN) ;
\r
223 /*----------------------------------------------------------------------------*/
\r
225 * \brief Initialize the PIO Capture
\r
226 * Be careful to configure the PDC before enable interrupt on pio capture.
\r
227 * Otherway, the pdc will go in interrupt handler continuously.
\r
229 * 0 = The reception data in the PIO_PCRHR register is a BYTE (8-bit).
\r
230 * 1 = The reception data in the PIO_PCRHR register is a HALF-WORD (16-bit).
\r
231 * 2/3 = The reception data in the PIO_PCRHR register is a WORD (32-bit).
\r
232 * \param alwaysSampling: ALWYS: Parallel Capture Mode Always Sampling
\r
233 * 0 = The parallel capture mode samples the data when both data enables are active.
\r
234 * 1 = The parallel capture mode samples the data whatever the data enables are.
\r
235 * \param halfSampling: HALFS: Parallel Capture Mode Half Sampling
\r
236 * 0 = The parallel capture mode samples all the data.
\r
237 * 1 = The parallel capture mode samples the data only one time out of two.
\r
238 * \param modeFirstSample: FRSTS: Parallel Capture Mode First Sample
\r
239 * This bit is useful only if the HALFS bit is set to 1. If data are numbered
\r
240 * in the order that they are received with an index from 0 to n:
\r
241 * 0 = Only data with an even index are sampled.
\r
242 * 1 = Only data with an odd index are sampled.
\r
244 /*----------------------------------------------------------------------------*/
\r
245 void PIO_CaptureInit( SpioCaptureInit *pInit )
\r
247 PMC_EnablePeripheral( ID_PIOA );
\r
249 assert( (pInit->dsize < 0x4) ) ;
\r
250 assert( (pInit->alwaysSampling < 2) );
\r
251 assert( (pInit->halfSampling < 2) );
\r
252 assert( (pInit->modeFirstSample < 2) );
\r
254 /* PDC: Transfer Control Register */
\r
255 /* Disables the PDC transmitter channel requests */
\r
256 // PIOA->PIO_PTCR = PIO_PTCR_RXTDIS;
\r
257 /* PDC: Receive Pointer Register */
\r
258 // PIOA->PIO_RPR = (uint32_t)pInit->pData;
\r
259 /* PDC: Receive Counter Register */
\r
260 /* Starts peripheral data transfer if corresponding channel is active */
\r
261 // PIOA->PIO_RCR = PIO_RCR_RXCTR(pInit->dPDCsize);
\r
263 /* PDC: Transfer Control Register */
\r
264 /* Enables PDC receiver channel requests if RXTDIS is not set */
\r
265 // PIOA->PIO_PTCR = PIO_PTCR_RXTEN ;
\r
268 /* Copy the API structure for interrupt handler */
\r
269 _PioCaptureCopy = pInit;
\r
270 /* PIO Parallel Capture Mode */
\r
271 // PIOA->PIO_PCMR = PIO_PCMR_DSIZE(pInit->dsize)
\r
272 // | ((pInit->alwaysSampling<<9) & PIO_PCMR_ALWYS)
\r
273 // | ((pInit->halfSampling<<10) & PIO_PCMR_HALFS)
\r
274 // | ((pInit->modeFirstSample<<11) & PIO_PCMR_FRSTS);
\r
276 if ( pInit->CbkDataReady != NULL )
\r
278 PIOA->PIO_PCIER = PIO_PCISR_DRDY;
\r
281 if ( pInit->CbkOverrun != NULL )
\r
283 PIOA->PIO_PCIER = PIO_PCISR_OVRE;
\r
286 if ( pInit->CbkEndReception != NULL )
\r
288 PIOA->PIO_PCIER = PIO_PCISR_ENDRX;
\r
291 if ( pInit->CbkBuffFull != NULL )
\r
293 PIOA->PIO_PCIER = PIO_PCISR_RXBUFF;
\r
297 // TRACE_INFO("No interruption, no callback\n\r");
\r