summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/atsam/libraries/libchip/source/ssc.c
blob: 08f1edde18ebb37c4c30b4279fe68c26ae44e47e (plain) (blame)
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
/* ---------------------------------------------------------------------------- */
/*                  Atmel Microcontroller Software Support                      */
/*                       SAM Software Package License                           */
/* ---------------------------------------------------------------------------- */
/* Copyright (c) 2015, Atmel Corporation                                        */
/*                                                                              */
/* All rights reserved.                                                         */
/*                                                                              */
/* Redistribution and use in source and binary forms, with or without           */
/* modification, are permitted provided that the following condition is met:    */
/*                                                                              */
/* - Redistributions of source code must retain the above copyright notice,     */
/* this list of conditions and the disclaimer below.                            */
/*                                                                              */
/* Atmel's name may not be used to endorse or promote products derived from     */
/* this software without specific prior written permission.                     */
/*                                                                              */
/* 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.                           */
/* ---------------------------------------------------------------------------- */

/** \addtogroup ssc_module Working with SSC
 *  \ingroup peripherals_module
 * The SSC driver provides the interface to configure and use the SSC
 * peripheral.
 *
 * !Usage
 *
 * -# Enable the SSC interface pins.
 * -# Configure the SSC to operate at a specific frequency by calling
 *    SSC_Configure(). This function enables the peripheral clock of the SSC,
 *    but not its PIOs.
 * -# Configure the transmitter and/or the receiver using the
 *    SSC_ConfigureTransmitter() and SSC_ConfigureEmitter() functions.
 * -# Enable the PIOs or the transmitter and/or the received.
 * -# Enable the transmitter and/or the receiver using SSC_EnableTransmitter()
 *    and SSC_EnableReceiver()
 * -# Send data through the transmitter using SSC_Write()
 * -# Receive data from the receiver using SSC_Read()
 * -# Disable the transmitter and/or the receiver using SSC_DisableTransmitter()
 *    and SSC_DisableReceiver()
 *
 * For more accurate information, please look at the SSC section of the
 * Datasheet.
 *
 * Related files :\n
 * \ref ssc.c\n
 * \ref ssc.h.\n
 */
/*@{*/
/*@}*/


/**
 * \file
 *
 * Implementation of Synchronous Serial (SSC) controller.
 *
 */

/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/

#include "chip.h"

/*----------------------------------------------------------------------------
 *       Exported functions
 *----------------------------------------------------------------------------*/

/**
 * \brief Configures a SSC peripheral.If the divided clock is not used, the
 *  master clock frequency can be set to 0.
 * \note The emitter and transmitter are disabled by this function.
 * \param ssc  Pointer to an SSC instance.
 * \param bitRate  bit rate.
 * \param masterClock  master clock.
 */
void SSC_Configure(Ssc *ssc, uint32_t bitRate, uint32_t masterClock)
{
	uint32_t id;
	//    uint32_t maxClock;
	id = ID_SSC;
	//    maxClock = PMC_SetPeriMaxClock(id, masterClock);

	/* Reset, disable receiver & transmitter */
	ssc->SSC_CR = SSC_CR_RXDIS | SSC_CR_TXDIS | SSC_CR_SWRST;

	/* Configure clock frequency */
	if (bitRate != 0)
		ssc->SSC_CMR = masterClock / (2 * bitRate);
	else
		ssc->SSC_CMR = 0;

	/* Enable SSC peripheral clock */
	PMC_EnablePeripheral(id);
}

/**
 * \brief Configures the transmitter of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 * \param tcmr Transmit Clock Mode Register value.
 * \param tfmr Transmit Frame Mode Register value.
 */
void SSC_ConfigureTransmitter(Ssc *ssc, uint32_t tcmr, uint32_t tfmr)
{
	ssc->SSC_TCMR = tcmr;
	ssc->SSC_TFMR = tfmr;
}

/**
 * \brief Configures the receiver of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 * \param rcmr Receive Clock Mode Register value.
 * \param rfmr Receive Frame Mode Register value.
 */
void SSC_ConfigureReceiver(Ssc *ssc, uint32_t rcmr, uint32_t rfmr)
{
	ssc->SSC_RCMR = rcmr;
	ssc->SSC_RFMR = rfmr;
}

/**
 * \brief Enables the transmitter of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 */
void SSC_EnableTransmitter(Ssc *ssc)
{
	ssc->SSC_CR = SSC_CR_TXEN;
}

/**
 * \brief Disables the transmitter of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 */
void SSC_DisableTransmitter(Ssc *ssc)
{
	ssc->SSC_CR = SSC_CR_TXDIS;
}

/**
 * \brief Enables the receiver of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 */
void SSC_EnableReceiver(Ssc *ssc)
{
	ssc->SSC_CR = SSC_CR_RXEN;
}

/**
 * \brief Disables the receiver of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 */
void SSC_DisableReceiver(Ssc *ssc)
{
	ssc->SSC_CR = SSC_CR_RXDIS;
}

/**
 * \brief Enables one or more interrupt sources of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 * \param sources Bitwise OR of selected interrupt sources.
 */
void SSC_EnableInterrupts(Ssc *ssc, uint32_t sources)
{
	ssc->SSC_IER = sources;
}

/**
 * \brief Disables one or more interrupt sources of a SSC peripheral.
 * \param ssc  Pointer to an SSC instance.
 * \param sources Bitwise OR of selected interrupt sources.
 */
void SSC_DisableInterrupts(Ssc *ssc, uint32_t sources)
{
	ssc->SSC_IDR = sources;
}

/**
 * \brief Sends one data frame through a SSC peripheral. If another frame is currently
 * being sent, this function waits for the previous transfer to complete.
 * \param ssc  Pointer to an SSC instance.
 * \param frame Data frame to send.
 */
void SSC_Write(Ssc *ssc, uint32_t frame)
{
	while ((ssc->SSC_SR & SSC_SR_TXRDY) == 0);

	ssc->SSC_THR = frame;
}

/**
 * \brief Waits until one frame is received on a SSC peripheral, and returns it.
 * \param ssc  Pointer to an SSC instance.
 */
uint32_t SSC_Read(Ssc *ssc)
{
	while ((ssc->SSC_SR & SSC_SR_RXRDY) == 0);

	return ssc->SSC_RHR;
}

/**
 * \brief Return 1 if one frame is received, 0 otherwise.
 * \param ssc  Pointer to an SSC instance.
 */
uint8_t SSC_IsRxReady(Ssc *ssc)
{
	return ((ssc->SSC_SR & SSC_SR_RXRDY) > 0);
}