aboutsummaryrefslogtreecommitdiffstats
path: root/mega/drivers/macsc/unit_tests/unit_tests.c
blob: e9f24fb7a2b5e6f7410c9ff82a6e03f8b3a299c3 (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
/**
 * \file
 *
 * \brief Unit tests for MEGARF MAC Symbol Counter
 *
 * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * 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
 * EXPRESSLY AND SPECIFICALLY 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.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */
#include <asf.h>
#include <conf_test.h>
#include <conf_macsc.h>
//
/**
 * \mainpage
 *
 * \section intro Introduction
 * This is the unit test application for the \ref macsc_group. It will
 * test overflow,compare and backoff slot counter functionality of the MACSC.
 *
 * \section files Main Files
 * - \ref unit_tests.c
 * - \ref conf_test.h
 * - \ref conf_board.h
 * - \ref conf_clock.h
 * - \ref conf_macsc.h
 * - \ref conf_usart_serial.h
 *
 * \section device_info Device Info
 * All AVR MEGARF devices with MACSC can be used
 *
 * \section description Description of the unit tests
 * See the documentation for the individual unit test functions
 * \ref unit_tests.c "here" for detailed descriptions of the tests.
 *
 * \section dependencies Dependencies
 * This example depends directly on the following modules:
 * - \ref test_suite_group
 * - \ref macsc_group
 *
 * \section compinfo Compilation info
 * This software was written for the GNU GCC and IAR for AVR. Other compilers
 * may or may not work.
 *
 * \section contactinfo Contact Information
 * For further information, visit <a href="http://www.atmel.com/">Atmel</a>.\n
 * Support and FAQ: http://www.atmel.com/design-support/
 */

//! \name Unit test configuration
//@{
/**
 * \def CONF_TEST_USART
 * \brief USART to redirect STDIO to
 */
/**
 * \def CONF_TEST_BAUDRATE
 * \brief Baudrate of USART
 */
/**
 * \def CONF_TEST_CHARLENGTH
 * \brief Character length (bits) of USART
 */
/**
 * \def CONF_TEST_PARITY
 * \brief Parity mode of USART
 */
/**
 * \def CONF_TEST_STOPBITS
 * \brief Stop bit configuration of USART
 */
//@}

//! Variable to keep track of test results
static bool success = false;
static struct cntstatus
{
  bool cmp1:1;
  bool cmp2:1;
  bool cmp3:1;
  bool backoff:1;
}check;

/**
 * \brief Symbol Counter Compare 1 interrupt callback function
 *
 * This function is called when a compare match has occured on channel 1 of
 * symbol counter.
 */
static void unit_test_cmp1_int_cb(void)
{
	check.cmp1 = true;
}

/**
 * \brief Symbol Counter Compare 2 interrupt callback function
 *
 * This function is called when a compare match has occured on channel 2 of
 * symbol counter.
 */
static void unit_test_cmp2_int_cb(void)
{
	check.cmp2 = true;
}

/**
 * \brief Symbol Counter Compare 3 interrupt callback function
 *
 * This function is called when a compare match has occured on channel 3 of
 * symbol counter.
 */
static void unit_test_cmp3_int_cb(void)
{
	check.cmp3 = true;
}

/**
 * \brief Symbol Counter back-off slot counter interrupt callback function
 *
 * This function is called when a slot counter interrupt has occured 
 */
static void unit_test_backoff_slot_cntr_int_cb(void)
{
	check.backoff = true;
}

/**
 * \brief Run MACSC enable test
 *
 * This test verifies that no timeouts are running after initialization
 *
 * \param test Current test case.
 */
static void test_macsc_enable(const struct test_case *test)
{
	success = true;
	
        if(!(is_macsc_enable()))
		success = false;
		
	test_assert_true(test, success, "MAC symbol counter enable failed");
}

/**
 * \brief Run MACSC compare test 
 *
 * This test verifies compare channel functionality
 * for all 3 cmp channels of the macsc and if cmp match
 * occurs after set compare interval
 * \param test Current test case.
 */
static void test_macsc_cmp(const struct test_case *test)
{
	success = true;
	
	macsc_set_cmp1_int_cb(unit_test_cmp1_int_cb);
	macsc_set_cmp2_int_cb(unit_test_cmp2_int_cb);
	macsc_set_cmp3_int_cb(unit_test_cmp3_int_cb);

	/*
	 * Configure MACSC to generate compare interrupts from channels 1,2,3
	 * Set compare mode to absolute
	 */
	macsc_enable_cmp_int(MACSC_CC1);
	macsc_enable_cmp_int(MACSC_CC2);
	macsc_enable_cmp_int(MACSC_CC3);
	
	macsc_use_cmp(COMPARE_MODE, (MACSC_READ32(SCCNT) + CONFIG_MACSC_TIMEOUT_TICK_HZ) / 4,
			MACSC_CC1);
	macsc_use_cmp(COMPARE_MODE, (MACSC_READ32(SCCNT) + CONFIG_MACSC_TIMEOUT_TICK_HZ / 2),
			MACSC_CC2);
	macsc_use_cmp(COMPARE_MODE, (MACSC_READ32(SCCNT) + CONFIG_MACSC_TIMEOUT_TICK_HZ), MACSC_CC3);

	delay_ms(1500);
        
        if((check.cmp1 == true)&(check.cmp2 == true) & (check.cmp3 == true))
          success = true;
        else 
          success = false;
    
	test_assert_true(test, success,"MACSC compare channels test failed");
}

/**
 * \brief Backoff slot counter test
 *
 * This test starts the backoff slot counter and verifies that the timeout
 * occurs as expected 10 times.
 *
 * \param test Current test case.
 */
static void test_macsc_backoff(const struct test_case *test)
{
    success = true;
	
	macsc_set_backoff_slot_cntr_int_cb(unit_test_backoff_slot_cntr_int_cb);

	macsc_backoff_slot_cnt_enable();
	
	if(!(is_macsc_backoff_enable()))
		success = false;
	
	delay_us(400);
        
        if(!check.backoff)
          success = false;
        
        macsc_backoff_slot_cnt_disable();
    
	test_assert_true(test, success,"MACSC backoff slot counter test failed");
}

/**
 * \brief Run MACSC unit tests
 */
int main (void)
{
	const usart_serial_options_t usart_serial_options =
	{
		.baudrate     = CONF_TEST_BAUDRATE,
		.charlength   = CONF_TEST_CHARLENGTH,
		.paritytype   = CONF_TEST_PARITY,
		.stopbits     = CONF_TEST_STOPBITS,
	};

	board_init();
	sysclk_init();
	ioport_init();

	sysclk_enable_peripheral_clock(&TRX_CTRL_0);
	
	stdio_serial_init(CONF_TEST_USART, &usart_serial_options);
	
	cpu_irq_enable();
	
	macsc_enable();
	macsc_write_clock_source(SYS_CLK_SRC);
	
	// Define all the test cases
	
	DEFINE_TEST_CASE(macsc_enable_test, NULL, test_macsc_enable, NULL,
			"Testing MACSC enable");
			
	DEFINE_TEST_CASE(macsc_cmp_test, NULL, test_macsc_cmp, NULL,
			"Testing MACSC compare channels");


	DEFINE_TEST_CASE(macsc_backoff_test, NULL, test_macsc_backoff, NULL,
			"Testing MACSC backoff slot counter");

	// Put test case addresses in an array
	DEFINE_TEST_ARRAY(macsc_tests) = {
		&macsc_enable_test,
		&macsc_cmp_test,
		&macsc_backoff_test,
	};

	// Define the test suite
	DEFINE_TEST_SUITE(macsc_suite, macsc_tests,
			"MEGARF MAC Symbol Counter test suite");

	// Run all tests in the test suite
	test_suite_run(&macsc_suite);

	while (1) {
		// Loop for infinity
	}
}