]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libchip_samv7/source/pio_capture.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_IAR_Keil / libchip_samv7 / source / pio_capture.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2014, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \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
34  *\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
39  *\r
40  * To use the PIO Parallel Capture, the user has to follow these few steps:\r
41  * <ul>\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
50  * </ul>\r
51  *\r
52  * For more accurate information, please look at the PIO Parallel Capture Mode section of the\r
53  * Datasheet.\r
54  *\r
55  * <b>API Usage:</b>\r
56  *\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
59  *     2 options:\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
70  *\r
71  * Related files :\n\r
72  * \ref pio_capture.c\n\r
73  * \ref pio_capture.h\n\r
74  */\r
75 /*@{*/\r
76 /*@}*/\r
77 /**\r
78  * \file\r
79  *\r
80  * Implementation of PIO Parallel Capture.\r
81  *\r
82  */\r
83 \r
84 /*----------------------------------------------------------------------------\r
85  *        Headers\r
86  *----------------------------------------------------------------------------*/\r
87 \r
88 #include "chip.h"\r
89 \r
90 #include <assert.h>\r
91 \r
92 /*----------------------------------------------------------------------------\r
93  *        Local Functions\r
94  *----------------------------------------------------------------------------*/\r
95 /** Copy the API structure for interrupt handler */\r
96 static SpioCaptureInit* _PioCaptureCopy;\r
97 \r
98 /*----------------------------------------------------------------------------\r
99  *        Global Functions\r
100  *----------------------------------------------------------------------------*/\r
101 \r
102 /*----------------------------------------------------------------------------*/\r
103 /**\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
106  */\r
107 /*----------------------------------------------------------------------------*/\r
108 extern void PIO_CaptureHandler( void )\r
109 {\r
110     volatile uint32_t pio_captureSr;\r
111     uint32_t k;\r
112 \r
113     /* Read the status register*/\r
114     pio_captureSr = PIOA->PIO_PCISR ;\r
115     k = pio_captureSr;\r
116     pio_captureSr = k & PIOA->PIO_PCIMR ;\r
117 \r
118     if (pio_captureSr & PIO_PCISR_DRDY)\r
119     {\r
120         /* Parallel Capture Mode Data Ready */\r
121         if ( _PioCaptureCopy->CbkDataReady != NULL )\r
122         {\r
123             _PioCaptureCopy->CbkDataReady( _PioCaptureCopy );\r
124         }\r
125         else\r
126         {\r
127             TRACE_DEBUG("IT PIO Capture Data Ready received (no callback)\n\r");\r
128         }\r
129     }\r
130 \r
131     if (pio_captureSr & PIO_PCISR_OVRE)\r
132     {\r
133         /* Parallel Capture Mode Overrun Error */\r
134         if ( _PioCaptureCopy->CbkOverrun != NULL )\r
135         {\r
136             _PioCaptureCopy->CbkOverrun( _PioCaptureCopy );\r
137         }\r
138         else\r
139         {\r
140             TRACE_DEBUG("IT PIO Capture Overrun Error received (no callback)\n\r");\r
141         }\r
142     }\r
143 \r
144     if (pio_captureSr & PIO_PCISR_RXBUFF)\r
145     {\r
146         /* Reception Buffer Full */\r
147         if ( _PioCaptureCopy->CbkBuffFull != NULL )\r
148         {\r
149             _PioCaptureCopy->CbkBuffFull( _PioCaptureCopy );\r
150         }\r
151         else\r
152         {\r
153             TRACE_DEBUG("IT PIO Capture Reception Buffer Full received (no callback)\n\r");\r
154         }\r
155     }\r
156 \r
157     if (pio_captureSr & PIO_PCISR_ENDRX)\r
158     {\r
159         /* End of Reception Transfer */\r
160         if ( _PioCaptureCopy->CbkEndReception != NULL )\r
161         {\r
162             _PioCaptureCopy->CbkEndReception( _PioCaptureCopy );\r
163         }\r
164         else\r
165         {\r
166             TRACE_DEBUG("IT PIO Capture End of Reception Transfer received (no callback)\n\r");\r
167         }\r
168     }\r
169 }\r
170 \r
171 /*----------------------------------------------------------------------------*/\r
172 /**\r
173  * \brief Disable Interupt of the PIO Capture\r
174  * \param itToDisable : Interrupt to disable\r
175  */\r
176 /*----------------------------------------------------------------------------*/\r
177 void PIO_CaptureDisableIt( uint32_t itToDisable )\r
178 {\r
179     /* Parallel capture mode is enabled */\r
180     PIOA->PIO_PCIDR = itToDisable;\r
181 }\r
182 \r
183 /*----------------------------------------------------------------------------*/\r
184 /**\r
185  * \brief Enable Interupt of the PIO Capture\r
186  * \param itToEnable : Interrupt to enable\r
187  */\r
188 /*----------------------------------------------------------------------------*/\r
189 void PIO_CaptureEnableIt( uint32_t itToEnable )\r
190 {\r
191     /* Parallel capture mode is enabled */\r
192     PIOA->PIO_PCIER = itToEnable;\r
193 }\r
194 \r
195 /*----------------------------------------------------------------------------*/\r
196 /**\r
197  * \brief Enable the PIO Capture\r
198  */\r
199 /*----------------------------------------------------------------------------*/\r
200 void PIO_CaptureEnable( void )\r
201 {\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
207 \r
208     /* Parallel capture mode is enabled */\r
209     PIOA->PIO_PCMR |= PIO_PCMR_PCEN ;\r
210 }\r
211 \r
212 /*----------------------------------------------------------------------------*/\r
213 /**\r
214  * \brief Disable the PIO Capture\r
215  */\r
216 /*----------------------------------------------------------------------------*/\r
217 void PIO_CaptureDisable( void )\r
218 {\r
219     /* Parallel capture mode is disabled */\r
220     PIOA->PIO_PCMR &= (uint32_t)(~PIO_PCMR_PCEN) ;\r
221 }\r
222 \r
223 /*----------------------------------------------------------------------------*/\r
224 /**\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
228  * \param dsize :\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
243  */\r
244 /*----------------------------------------------------------------------------*/\r
245 void PIO_CaptureInit( SpioCaptureInit *pInit )\r
246 {\r
247     PMC_EnablePeripheral( ID_PIOA );\r
248 \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
253 \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
262 \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
266 \r
267 \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
275 \r
276     if ( pInit->CbkDataReady != NULL )\r
277     {\r
278         PIOA->PIO_PCIER = PIO_PCISR_DRDY;\r
279     }\r
280 \r
281     if ( pInit->CbkOverrun != NULL )\r
282     {\r
283         PIOA->PIO_PCIER = PIO_PCISR_OVRE;\r
284     }\r
285 \r
286     if ( pInit->CbkEndReception != NULL )\r
287     {\r
288         PIOA->PIO_PCIER = PIO_PCISR_ENDRX;\r
289     }\r
290 \r
291     if ( pInit->CbkBuffFull != NULL )\r
292     {\r
293         PIOA->PIO_PCIER = PIO_PCISR_RXBUFF;\r
294     }\r
295 //    else\r
296 //    {\r
297 //        TRACE_INFO("No interruption, no callback\n\r");\r
298 //    }\r
299 \r
300 }\r
301 \r