47
47
#include "adc.h"
48
48
#include "fsl_port.h"
49
49
#include "fsl_gpio.h"
50
+ #include "fsl_tpm.h"
50
51
52
+ volatile bool tpmIsrFlag = false;
53
+ uint32_t tpm_source_clock ;
54
+
55
+ void TPM0_IRQHandler (void )
56
+ {
57
+ /* Clear interrupt flag.*/
58
+ TPM_ClearStatusFlags (TPM0 , kTPM_TimeOverflowFlag );
59
+ tpmIsrFlag = true;
60
+ TPM_StopTimer (TPM0 );
61
+ __DSB ();
62
+ }
51
63
#endif
52
64
53
65
#ifdef DRAG_N_DROP_SUPPORT
@@ -109,6 +121,25 @@ static mb_version_t read_brd_rev_id(void) {
109
121
mb_version_t board_version = BOARD_VERSION_2_DEF ;
110
122
uint32_t board_rev_id_adc = 0 ;
111
123
uint32_t board_rev_id_mv = 0 ;
124
+
125
+ tpm_config_t tpmInfo ;
126
+
127
+ /* Select the clock source for the TPM counter as kCLOCK_McgIrc48MClk */
128
+ CLOCK_SetTpmClock (1U );
129
+
130
+ TPM_GetDefaultConfig (& tpmInfo );
131
+
132
+ /* TPM clock divide by TPM_PRESCALER */
133
+ tpmInfo .prescale = kTPM_Prescale_Divide_4 ;
134
+
135
+ /* Initialize TPM module */
136
+ TPM_Init (TPM0 , & tpmInfo );
137
+
138
+ TPM_EnableInterrupts (TPM0 , kTPM_TimeOverflowInterruptEnable );
139
+
140
+ EnableIRQ (TPM0_IRQn );
141
+
142
+ tpm_source_clock = (CLOCK_GetFreq (kCLOCK_McgIrc48MClk ) / 4 );
112
143
113
144
// Set Board Rev ID pin as output but pin disabled
114
145
PORT_SetPinMux (PIN_BOARD_REV_ID_PORT , PIN_BOARD_REV_ID_BIT , kPORT_PinDisabledOrAnalog );
@@ -118,19 +149,26 @@ static mb_version_t read_brd_rev_id(void) {
118
149
adc_init ();
119
150
120
151
// 1. Discharge capacitor
152
+ /* Set timer period 3ms*/
153
+ TPM_SetTimerPeriod (TPM0 , USEC_TO_COUNT (3000U , tpm_source_clock ));
121
154
// Drive BRD_REV_ID pin to low
122
155
GPIO_PortClear (PIN_BOARD_REV_ID_GPIO , PIN_BOARD_REV_ID );
123
156
PORT_SetPinMux (PIN_BOARD_REV_ID_PORT , PIN_BOARD_REV_ID_BIT , kPORT_MuxAsGpio );
124
157
// Add a 3ms delay to allow the 100nF Cap to discharge
125
158
// at least 5*RC with 4700R.
126
- for (uint32_t count = 16 * 3000 ; count > 0UL ; count -- );
159
+ TPM_StartTimer (TPM0 , kTPM_SystemClock );
160
+ while (false == tpmIsrFlag );
161
+ tpmIsrFlag = false;
127
162
128
163
// 2. Charge capacitor for 100us
164
+ /* Set timer period slightly below 100us to account for overheads */
165
+ TPM_SetTimerPeriod (TPM0 , USEC_TO_COUNT (98U , tpm_source_clock ));
129
166
// Drive BRD_REV_ID pin to high
130
167
GPIO_PortSet (PIN_BOARD_REV_ID_GPIO , PIN_BOARD_REV_ID );
131
168
// Add a ~100us delay
132
- // 3 clock cycles per loop at -O2 ARMCC optimization
133
- for (uint32_t count = 1600 ; count > 0UL ; count -- );
169
+ TPM_StartTimer (TPM0 , kTPM_SystemClock );
170
+ while (false == tpmIsrFlag );
171
+ tpmIsrFlag = false;
134
172
// Change pin to ADC (High-Z). Capacitor will stop charging
135
173
PORT_SetPinMux (PIN_BOARD_REV_ID_PORT , PIN_BOARD_REV_ID_BIT , kPORT_PinDisabledOrAnalog );
136
174
@@ -139,12 +177,16 @@ static mb_version_t read_brd_rev_id(void) {
139
177
board_rev_id_mv = board_rev_id_adc * 3300 / 0xFFF ; // Convert ADC 12-bit value to mV with 3.3V reference
140
178
141
179
// 4. Discharge capacitor
180
+ /* Set timer period 3ms*/
181
+ TPM_SetTimerPeriod (TPM0 , USEC_TO_COUNT (3000U , tpm_source_clock ));
142
182
// Drive BRD_REV_ID pin to low
143
183
GPIO_PortClear (PIN_BOARD_REV_ID_GPIO , PIN_BOARD_REV_ID );
144
184
PORT_SetPinMux (PIN_BOARD_REV_ID_PORT , PIN_BOARD_REV_ID_BIT , kPORT_MuxAsGpio );
145
185
// Add a 3ms delay to allow the 100nF Cap to discharge
146
186
// at least 5*RC with 4700R.
147
- for (uint32_t count = 16 * 3000 ; count > 0UL ; count -- );
187
+ TPM_StartTimer (TPM0 , kTPM_SystemClock );
188
+ while (false == tpmIsrFlag );
189
+ tpmIsrFlag = false;
148
190
149
191
// 5. Identify board ID depending on voltage
150
192
if ( board_rev_id_mv > BRD_ID_1_LOWER_THR_V && board_rev_id_mv < BRD_ID_1_UPPER_THR_V ) {
@@ -153,8 +195,7 @@ static mb_version_t read_brd_rev_id(void) {
153
195
board_version = BOARD_VERSION_2_DEF ;
154
196
}
155
197
156
- return BOARD_VERSION_2_0 ;
157
- //return board_version;
198
+ return board_version ;
158
199
}
159
200
160
201
#elif defined(INTERFACE_NRF52820 )
0 commit comments