1 /*
2  * Copyright (c) 2024 Renesas Electronics Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "zephyr/kernel.h"
8 #include <stdint.h>
9 #include <zephyr/ztest.h>
10 #include <soc.h>
11 
12 #define STACK_SIZE (512)
13 
14 ZTEST_SUITE(rx_acc_tests, NULL, NULL, NULL, NULL, NULL);
15 
16 /*local variables*/
17 static K_THREAD_STACK_DEFINE(tstack_thread_1, STACK_SIZE);
18 static K_THREAD_STACK_DEFINE(tstack_thread_2, STACK_SIZE);
19 static struct k_thread thread_1;
20 static struct k_thread thread_2;
21 struct k_event my_event;
22 static volatile int32_t thread_1_m, thread_1_h, thread_2_m, thread_2_h;
23 
thread_1_entry(void * p1,void * p2,void * p3)24 static void thread_1_entry(void *p1, void *p2, void *p3)
25 {
26 
27 	/* Set the initial accumulator registers */
28 	__asm volatile("MOV #0x90ABCDEF,R15\n"
29 			"MVTACLO	R15\n"
30 			"MOV #0x12345678,R15\n"
31 			"MVTACHI	R15\n");
32 
33 	/* yield the current thread. */
34 	k_yield();
35 
36 	while (1) {
37 		/* Get Accumulator registers */
38 		__asm volatile("MVFACMI	R15\n"
39 				"MOV.L R15, %0"
40 				: "=r"(thread_1_m));
41 
42 		__asm volatile("MVFACHI	R15\n"
43 				"MOV.L R15, %0"
44 				: "=r"(thread_1_h));
45 
46 		/* Set Event flag */
47 		k_event_post(&my_event, 0x001);
48 		k_sleep(K_SECONDS(10U));
49 	}
50 }
51 
thread_2_entry(void * p1,void * p2,void * p3)52 static void thread_2_entry(void *p1, void *p2, void *p3)
53 {
54 
55 	/* Set the initial accumulator registers */
56 	__asm volatile("MOV #0x23456789,R15\n"
57 			"MVTACLO	R15\n"
58 			"MOV #0xABCDEF01,R15\n"
59 			"MVTACHI	R15\n");
60 
61 	/* yield the current thread. */
62 	k_yield();
63 
64 	while (1) {
65 		/* Get Accumulator registers */
66 		__asm volatile("MVFACMI	R15\n"
67 				"MOV.L R15, %0"
68 				: "=r"(thread_2_m));
69 		__asm volatile("MVFACHI	R15\n"
70 				"MOV.L R15, %0"
71 				: "=r"(thread_2_h));
72 
73 		/* Set Event flag */
74 		k_event_post(&my_event, 0x010);
75 		k_sleep(K_SECONDS(10U));
76 	}
77 }
78 
79 /**
80  * @brief Test switching accumulator
81  *
82  * This test verifies various assert macros provided by ztest.
83  *
84  */
ZTEST(rx_acc_tests,test_counting_value)85 ZTEST(rx_acc_tests, test_counting_value)
86 {
87 
88 	k_event_init(&my_event);
89 
90 	k_tid_t tid_1 = k_thread_create(&thread_1, tstack_thread_1, STACK_SIZE, thread_1_entry,
91 					NULL, NULL, NULL, K_PRIO_COOP(1), 0, K_NO_WAIT);
92 
93 	k_tid_t tid_2 = k_thread_create(&thread_2, tstack_thread_2, STACK_SIZE, thread_2_entry,
94 					NULL, NULL, NULL, K_PRIO_COOP(1), 0, K_NO_WAIT);
95 
96 	k_event_wait_all(&my_event, 0x11, false, K_FOREVER);
97 
98 	/* cleanup environment */
99 	k_thread_abort(tid_1);
100 	k_thread_abort(tid_2);
101 
102 	/* Asset the value */
103 	zassert_equal(thread_1_m, 0x567890AB, "Failed thread_1_m");
104 	zassert_equal(thread_1_h, 0x12345678, "Failed thread_1_h");
105 	zassert_equal(thread_2_m, 0xEF012345, "Failed thread_2_m");
106 	zassert_equal(thread_2_h, 0xABCDEF01, "Failed thread_2_h");
107 }
108