1 /*******************************************************************************
2  * Copyright 2019-2021 Microchip FPGA Embedded Systems Solutions.
3  *
4  */
5 
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdint.h>
13 
14 #include "mpfs_hal/mss_hal.h"
15 #include "mss_cfm.h"
16 
17 /***************************************************************************//**
18  * See mss_cfm.h for description of this function.
19  */
MSS_CFM_control_start(void)20 uint8_t MSS_CFM_control_start(void)
21 {
22 
23     /* Writing a 1, to this causes measurement circuitry to start. */
24     CFM_REG->controlReg |= 1;
25 
26     return (CFM_REG->controlReg & CFM_CONTROL_REG_START_MASK);
27 
28 }
29 
30 
MSS_CFM_control_stop(void)31 uint8_t MSS_CFM_control_stop(void)
32 {
33 
34     /* Writing a 1, to this causes measurement circuitry to start. */
35     CFM_REG->controlReg |= (1 << CFM_CONTROL_REG_STOP_BITS_SHIFT);
36 
37     return (CFM_REG->controlReg & CFM_CONTROL_REG_START_MASK);
38 
39 }
40 
41 
42 
MSS_CLF_clk_configuration(uint8_t clkSel,uint8_t refsel0,uint8_t refsel1,uint8_t monSEL,uint8_t monEN)43 cfm_error_id_t MSS_CLF_clk_configuration(
44         uint8_t clkSel,
45         uint8_t refsel0,
46         uint8_t refsel1,
47         uint8_t monSEL,
48         uint8_t monEN
49     )
50 {
51 
52     /* Reset the register. */
53     CFM_REG->clkselReg = 0;
54 
55     /* Some error checking on configuration values. */
56     if(clkSel > CFM_CLK_SEL_MASK)
57         return ERROR_INVALID_CLK_SELECTION_GROUP;
58 
59     if(refsel0 > CFM_CLK_REFSEL0_MASK)
60         return ERROR_INVALID_REF_SEL0;
61 
62     if(refsel1 > CFM_CLK_REFSEL1_MASK)
63         return ERROR_INVALID_REF_SEL1;
64 
65     if(monSEL > CFM_CLK_MONSEL_MASK)
66         return ERROR_INVALID_CHANNEL_DRIVE_CLK_MONITOR;
67 
68 
69     CFM_REG->clkselReg |= (clkSel & CFM_CLK_SEL_MASK);
70 
71     if(refsel0)
72         CFM_REG->clkselReg |= (uint32_t)(refsel0 << CFM_CLK_REFSEL0SHIFT);
73 
74     if(refsel1)
75         CFM_REG->clkselReg |= (uint32_t)(refsel1 << CFM_CLK_REFSEL1SHIFT);
76 
77     if(monSEL)
78         CFM_REG->clkselReg |= (uint32_t)(monSEL << CFM_CLK_MONSEL_SHIFT);
79 
80 
81     if(monEN)
82         CFM_REG->clkselReg |= (uint32_t)(monEN << CFM_CLK_MONEN_SHIFT);
83 
84 
85     return CFM_OK;
86 
87 }
88 
89 
MSS_CFM_runtime_register(uint32_t referenceCount)90 void MSS_CFM_runtime_register(uint32_t  referenceCount)
91 {
92 
93     /*Sets how many runtime reference clock cycles the frequency and time
94      * measurement shold be made for.. */
95     CFM_REG->runtimeReg = (referenceCount & CFM_RUNTIME_REG_MASK);
96 
97     return;
98 }
99 
100 
MSS_CFM_channel_mode(cfmChannelMode chMode)101 void MSS_CFM_channel_mode(cfmChannelMode  chMode)
102 {
103 
104 
105     uint32_t chConfiguration = 0;
106 
107     chConfiguration |= (chMode.channel0 & CFM_CHANNEL_MODE_MASK) << CFM_CH0_SHIFT_MASK;
108     chConfiguration |= (chMode.channel1 & CFM_CHANNEL_MODE_MASK) << CFM_CH1_SHIFT_MASK;
109     chConfiguration |= (chMode.channel2 & CFM_CHANNEL_MODE_MASK) << CFM_CH2_SHIFT_MASK;
110     chConfiguration |= (chMode.channel3 & CFM_CHANNEL_MODE_MASK) << CFM_CH3_SHIFT_MASK;
111     chConfiguration |= (chMode.channel4 & CFM_CHANNEL_MODE_MASK) << CFM_CH4_SHIFT_MASK;
112     chConfiguration |= (chMode.channel5 & CFM_CHANNEL_MODE_MASK) << CFM_CH5_SHIFT_MASK;
113     chConfiguration |= (chMode.channel6 & CFM_CHANNEL_MODE_MASK) << CFM_CH6_SHIFT_MASK;
114     chConfiguration |= (chMode.channel7 & CFM_CHANNEL_MODE_MASK) << CFM_CH7_SHIFT_MASK;
115 
116     CFM_REG->modelReg = chConfiguration;
117 
118 
119     return;
120 }
121 
MSS_CFM_get_count(cfm_count_id_t ch,uint32_t * count)122 cfm_error_id_t MSS_CFM_get_count(cfm_count_id_t ch, uint32_t *count)
123 {
124 
125     if(count == NULL)
126         return ERROR_NULL_VALUE;
127 
128     *count = 0;
129 
130     if(CFM_REG->controlReg & CFM_CONTROL_REG_BUSY_MASK)
131         return ERROR_INVALID_CFM_BUSY;
132 
133     switch(ch)
134     {
135         case CFM_COUNT_0:
136             *count = CFM_REG->count0;
137             break;
138 
139         case CFM_COUNT_1:
140             *count = CFM_REG->count1;
141             break;
142 
143         case CFM_COUNT_2:
144             *count = CFM_REG->count2;
145             break;
146 
147         case CFM_COUNT_3:
148             *count = CFM_REG->count3;
149             break;
150 
151         case CFM_COUNT_4:
152             *count = CFM_REG->count4;
153             break;
154 
155         case CFM_COUNT_5:
156             *count = CFM_REG->count5;
157             break;
158 
159         case CFM_COUNT_6:
160             *count = CFM_REG->count6;
161             break;
162 
163         case CFM_COUNT_7:
164             *count = CFM_REG->count7;
165             break;
166 
167         default:
168             return 11;
169 
170 
171     }
172 
173     return CFM_OK;
174 
175 }
176