1 | /* ---------------------------------------------------------------------------- */ |
---|
2 | /* Atmel Microcontroller Software Support */ |
---|
3 | /* SAM Software Package License */ |
---|
4 | /* ---------------------------------------------------------------------------- */ |
---|
5 | /* Copyright (c) 2015, Atmel Corporation */ |
---|
6 | /* */ |
---|
7 | /* All rights reserved. */ |
---|
8 | /* */ |
---|
9 | /* Redistribution and use in source and binary forms, with or without */ |
---|
10 | /* modification, are permitted provided that the following condition is met: */ |
---|
11 | /* */ |
---|
12 | /* - Redistributions of source code must retain the above copyright notice, */ |
---|
13 | /* this list of conditions and the disclaimer below. */ |
---|
14 | /* */ |
---|
15 | /* Atmel's name may not be used to endorse or promote products derived from */ |
---|
16 | /* this software without specific prior written permission. */ |
---|
17 | /* */ |
---|
18 | /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ |
---|
19 | /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
---|
20 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ |
---|
21 | /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ |
---|
22 | /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ |
---|
23 | /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ |
---|
24 | /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ |
---|
25 | /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ |
---|
26 | /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ |
---|
27 | /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
28 | /* ---------------------------------------------------------------------------- */ |
---|
29 | |
---|
30 | /** \addtogroup ssc_module Working with SSC |
---|
31 | * \ingroup peripherals_module |
---|
32 | * The SSC driver provides the interface to configure and use the SSC |
---|
33 | * peripheral. |
---|
34 | * |
---|
35 | * !Usage |
---|
36 | * |
---|
37 | * -# Enable the SSC interface pins. |
---|
38 | * -# Configure the SSC to operate at a specific frequency by calling |
---|
39 | * SSC_Configure(). This function enables the peripheral clock of the SSC, |
---|
40 | * but not its PIOs. |
---|
41 | * -# Configure the transmitter and/or the receiver using the |
---|
42 | * SSC_ConfigureTransmitter() and SSC_ConfigureEmitter() functions. |
---|
43 | * -# Enable the PIOs or the transmitter and/or the received. |
---|
44 | * -# Enable the transmitter and/or the receiver using SSC_EnableTransmitter() |
---|
45 | * and SSC_EnableReceiver() |
---|
46 | * -# Send data through the transmitter using SSC_Write() |
---|
47 | * -# Receive data from the receiver using SSC_Read() |
---|
48 | * -# Disable the transmitter and/or the receiver using SSC_DisableTransmitter() |
---|
49 | * and SSC_DisableReceiver() |
---|
50 | * |
---|
51 | * For more accurate information, please look at the SSC section of the |
---|
52 | * Datasheet. |
---|
53 | * |
---|
54 | * Related files :\n |
---|
55 | * \ref ssc.c\n |
---|
56 | * \ref ssc.h.\n |
---|
57 | */ |
---|
58 | /*@{*/ |
---|
59 | /*@}*/ |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * \file |
---|
64 | * |
---|
65 | * Implementation of Synchronous Serial (SSC) controller. |
---|
66 | * |
---|
67 | */ |
---|
68 | |
---|
69 | /*---------------------------------------------------------------------------- |
---|
70 | * Headers |
---|
71 | *----------------------------------------------------------------------------*/ |
---|
72 | |
---|
73 | #include "chip.h" |
---|
74 | |
---|
75 | /*---------------------------------------------------------------------------- |
---|
76 | * Exported functions |
---|
77 | *----------------------------------------------------------------------------*/ |
---|
78 | |
---|
79 | /** |
---|
80 | * \brief Configures a SSC peripheral.If the divided clock is not used, the |
---|
81 | * master clock frequency can be set to 0. |
---|
82 | * \note The emitter and transmitter are disabled by this function. |
---|
83 | * \param ssc Pointer to an SSC instance. |
---|
84 | * \param bitRate bit rate. |
---|
85 | * \param masterClock master clock. |
---|
86 | */ |
---|
87 | void SSC_Configure(Ssc *ssc, uint32_t bitRate, uint32_t masterClock) |
---|
88 | { |
---|
89 | uint32_t id; |
---|
90 | // uint32_t maxClock; |
---|
91 | id = ID_SSC; |
---|
92 | // maxClock = PMC_SetPeriMaxClock(id, masterClock); |
---|
93 | |
---|
94 | /* Reset, disable receiver & transmitter */ |
---|
95 | ssc->SSC_CR = SSC_CR_RXDIS | SSC_CR_TXDIS | SSC_CR_SWRST; |
---|
96 | |
---|
97 | /* Configure clock frequency */ |
---|
98 | if (bitRate != 0) |
---|
99 | ssc->SSC_CMR = masterClock / (2 * bitRate); |
---|
100 | else |
---|
101 | ssc->SSC_CMR = 0; |
---|
102 | |
---|
103 | /* Enable SSC peripheral clock */ |
---|
104 | PMC_EnablePeripheral(id); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * \brief Configures the transmitter of a SSC peripheral. |
---|
109 | * \param ssc Pointer to an SSC instance. |
---|
110 | * \param tcmr Transmit Clock Mode Register value. |
---|
111 | * \param tfmr Transmit Frame Mode Register value. |
---|
112 | */ |
---|
113 | void SSC_ConfigureTransmitter(Ssc *ssc, uint32_t tcmr, uint32_t tfmr) |
---|
114 | { |
---|
115 | ssc->SSC_TCMR = tcmr; |
---|
116 | ssc->SSC_TFMR = tfmr; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * \brief Configures the receiver of a SSC peripheral. |
---|
121 | * \param ssc Pointer to an SSC instance. |
---|
122 | * \param rcmr Receive Clock Mode Register value. |
---|
123 | * \param rfmr Receive Frame Mode Register value. |
---|
124 | */ |
---|
125 | void SSC_ConfigureReceiver(Ssc *ssc, uint32_t rcmr, uint32_t rfmr) |
---|
126 | { |
---|
127 | ssc->SSC_RCMR = rcmr; |
---|
128 | ssc->SSC_RFMR = rfmr; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * \brief Enables the transmitter of a SSC peripheral. |
---|
133 | * \param ssc Pointer to an SSC instance. |
---|
134 | */ |
---|
135 | void SSC_EnableTransmitter(Ssc *ssc) |
---|
136 | { |
---|
137 | ssc->SSC_CR = SSC_CR_TXEN; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * \brief Disables the transmitter of a SSC peripheral. |
---|
142 | * \param ssc Pointer to an SSC instance. |
---|
143 | */ |
---|
144 | void SSC_DisableTransmitter(Ssc *ssc) |
---|
145 | { |
---|
146 | ssc->SSC_CR = SSC_CR_TXDIS; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * \brief Enables the receiver of a SSC peripheral. |
---|
151 | * \param ssc Pointer to an SSC instance. |
---|
152 | */ |
---|
153 | void SSC_EnableReceiver(Ssc *ssc) |
---|
154 | { |
---|
155 | ssc->SSC_CR = SSC_CR_RXEN; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * \brief Disables the receiver of a SSC peripheral. |
---|
160 | * \param ssc Pointer to an SSC instance. |
---|
161 | */ |
---|
162 | void SSC_DisableReceiver(Ssc *ssc) |
---|
163 | { |
---|
164 | ssc->SSC_CR = SSC_CR_RXDIS; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * \brief Enables one or more interrupt sources of a SSC peripheral. |
---|
169 | * \param ssc Pointer to an SSC instance. |
---|
170 | * \param sources Bitwise OR of selected interrupt sources. |
---|
171 | */ |
---|
172 | void SSC_EnableInterrupts(Ssc *ssc, uint32_t sources) |
---|
173 | { |
---|
174 | ssc->SSC_IER = sources; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * \brief Disables one or more interrupt sources of a SSC peripheral. |
---|
179 | * \param ssc Pointer to an SSC instance. |
---|
180 | * \param sources Bitwise OR of selected interrupt sources. |
---|
181 | */ |
---|
182 | void SSC_DisableInterrupts(Ssc *ssc, uint32_t sources) |
---|
183 | { |
---|
184 | ssc->SSC_IDR = sources; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * \brief Sends one data frame through a SSC peripheral. If another frame is currently |
---|
189 | * being sent, this function waits for the previous transfer to complete. |
---|
190 | * \param ssc Pointer to an SSC instance. |
---|
191 | * \param frame Data frame to send. |
---|
192 | */ |
---|
193 | void SSC_Write(Ssc *ssc, uint32_t frame) |
---|
194 | { |
---|
195 | while ((ssc->SSC_SR & SSC_SR_TXRDY) == 0); |
---|
196 | |
---|
197 | ssc->SSC_THR = frame; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * \brief Waits until one frame is received on a SSC peripheral, and returns it. |
---|
202 | * \param ssc Pointer to an SSC instance. |
---|
203 | */ |
---|
204 | uint32_t SSC_Read(Ssc *ssc) |
---|
205 | { |
---|
206 | while ((ssc->SSC_SR & SSC_SR_RXRDY) == 0); |
---|
207 | |
---|
208 | return ssc->SSC_RHR; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * \brief Return 1 if one frame is received, 0 otherwise. |
---|
213 | * \param ssc Pointer to an SSC instance. |
---|
214 | */ |
---|
215 | uint8_t SSC_IsRxReady(Ssc *ssc) |
---|
216 | { |
---|
217 | return ((ssc->SSC_SR & SSC_SR_RXRDY) > 0); |
---|
218 | } |
---|
219 | |
---|