ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
isf_util.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2015, Freescale Semiconductor, Inc.
4  *
5 */
6 /*
7  * util.c
8  *
9  * Created on: May 1, 2013
10  * Author: B39109
11  */
12 
13 #include "isf_util.h"
14 #include "isf.h" // uint64
15 #if defined (FSL_RTOS_MQX)
16 extern hwtimer_t systimer;
17 #endif
18 uint32 bitrev(uint32 doubleword)
19 {
20  doubleword = (((doubleword & 0xaaaaaaaa) >> 1) | ((doubleword & 0x55555555) << 1));
21  doubleword = (((doubleword & 0xcccccccc) >> 2) | ((doubleword & 0x33333333) << 2));
22  doubleword = (((doubleword & 0xf0f0f0f0) >> 4) | ((doubleword & 0x0f0f0f0f) << 4));
23  doubleword = (((doubleword & 0xff00ff00) >> 8) | ((doubleword & 0x00ff00ff) << 8));
24  return (( doubleword >> 16) | (doubleword << 16));
25 }
26 
27 
28 uint32 ff1(uint32 doubleword) {
29 
30  uint32 index = 0;
31 
32  if (0 == doubleword) return 32;
33 
34  if (0 == (doubleword & 0xFFFF0000) ){ index += 16; doubleword <<= 16;}
35  if (0 == (doubleword & 0xFF000000) ){ index += 8; doubleword <<= 8;}
36  if (0 == (doubleword & 0xF0000000) ){ index += 4; doubleword <<= 4;}
37  if (0 == (doubleword & 0xC0000000) ){ index += 2, doubleword <<= 2;}
38  if (0 == (doubleword & 0x80000000) ){ index += 1, doubleword <<= 1;}
39  return index;
40 }
41 
42 uint32 ff1_lsb(uint32 doubleword){
43  uint32 index = 0;
44  if( 0 == doubleword) return 0;
45  if (0 == (doubleword & 0x0000FFFF) ){ index += 16; doubleword >>= 16;}
46  if (0 == (doubleword & 0x000000FF) ){ index += 8; doubleword >>= 8;}
47  if (0 == (doubleword & 0x0000000F) ){ index += 4; doubleword >>= 4;}
48  if (0 == (doubleword & 0x00000003) ){ index += 2, doubleword >>= 2;}
49  if (0 == (doubleword & 0x00000001) ){ index += 1, doubleword >>= 1;}
50  return index;
51 }
52 
53 
54 // This is a top level API exposed in isf.h. See that file for function description.
56 {
57  uint32 time_usec = 0;
58 #if defined (FSL_RTOS_MQX)
59  uint64 time_in_increments_of_overflow_ticks_usec64;
60  uint32 time_since_last_overflow_usec;
61 
62 
63  hwtimer_time_t time;
64  uint64 usec_per_tick;
65 
66  static float nsec_per_hw_tick = 0; // isf_get_nsec_per_hw_tick();
67 
68  if (nsec_per_hw_tick == 0)
69  nsec_per_hw_tick = (float)((float)1 / (float)systimer.clockFreq);
70 
71 
72  // Retrieve the overflow count and the hw systick counter value.
73  HWTIMER_SYS_GetTime(&systimer, &time);
74 
75  // Need to know the time per overflow .
76  usec_per_tick = (uint64)1000000 * systimer.divider / (uint64)systimer.clockFreq;
77 
78  time_in_increments_of_overflow_ticks_usec64 = (uint64)time.ticks * (uint64)usec_per_tick;
79 
80  // get the granularity down to hardware ticks.
81  time_since_last_overflow_usec = (uint32)( ( time.subTicks * nsec_per_hw_tick) * 1000000);
82 
83  // Add elapsed rtos time and time since last rtos tick. Then truncate to 32bit.
84  time_usec = (uint32)((uint64)time_in_increments_of_overflow_ticks_usec64 + (uint64)time_since_last_overflow_usec);
85 #endif
86 #if defined (FSL_RTOS_FREE_RTOS)
87  uint64 time_in_increments_of_overflow_ticks_usec64;
88  uint32 time_since_last_overflow_usec;
89  uint64 usec_per_tick;
90  uint32 subTicks;
91  uint32 Ticks;
92 
93  // Calculate the number of nanoseconds in each tick (one time).
94  static float nsec_per_hw_tick = 0;
95  if (nsec_per_hw_tick == 0)
96  {
97  nsec_per_hw_tick = (float)((float)1 / (float)configSYSTICK_CLOCK_HZ);
98  }
99 
100  // Get the current tick value
101  // NOTE: FreeRTOS for FSL KSDK 1.x counts down by default.
102  subTicks = SysTick->LOAD - SysTick->VAL;
103  Ticks = xTaskGetTickCountFromISR();
104 
105  // Need to know the time per overflow .
106  usec_per_tick = (uint64)1000000 / configTICK_RATE_HZ;
107 
108  time_in_increments_of_overflow_ticks_usec64 = (uint64)Ticks * (uint64)usec_per_tick;
109 
110  // get the granularity down to hardware ticks.
111  time_since_last_overflow_usec = (uint32)( ( subTicks * nsec_per_hw_tick) * 1000000);
112 
113  // Add elapsed rtos time and time since last rtos tick. Then truncate to 32bit.
114  time_usec = (uint32)((uint64)time_in_increments_of_overflow_ticks_usec64 + (uint64)time_since_last_overflow_usec);
115 
116 #endif
117 
118  return time_usec;
119 }
120 
121 
122 /***********************************************************************
123  *
124  * Function Name : isf_mem_copy
125  * Description : This function copy the data from destination to scr. This does not support overlayed memory.
126  *
127  ***************************************************************************/
128 void isf_mem_copy(void *src, void* dest, uint32 size)
129 {
130  uint8 *pSource = (uint8 *)src;
131  uint8 *pDestination = (uint8 *)dest;
132  while (size--) {
133  *pDestination++ = *pSource++;
134  }
135 
136 }
137 /***********************************************************************
138  *
139  * Function Name : isf_swap4byte
140  * Description : 4byte swapping
141  *
142  ***************************************************************************/
144 {
145  uint32_t ret;
146 
147  __asm volatile ("rev %0, %1" :"=r" (ret):"r" (n));
148  return(ret);
149 }
150 /***********************************************************************
151  *
152  * Function Name : isf_swap4byte
153  * Description : 2byte swapping
154  *
155  ***************************************************************************/
157 {
158  uint16_t ret;
159 
160  __asm volatile ("rev16 %0, %1" :"=r" (ret):"r" (n));
161  return(ret);
162 }
163 
164 
unsigned char uint8
Definition: isf_types.h:76
uint32 isf_time_util_get_usec(void)
This API returns the time in microseconds.
Definition: isf_util.c:55
unsigned long long uint64
This defines uint64 as unsigned long long.
Definition: isf_types.h:61
uint16 isf_swap2byte(uint16 n)
2 byte swapping method
Definition: isf_util.c:156
uint32 ff1(uint32 doubleword)
find first-in.
Definition: isf_util.c:28
uint32 bitrev(uint32 doubleword)
This function reverse the bits of a double word.
Definition: isf_util.c:18
uint32 ff1_lsb(uint32 doubleword)
find first-in form LSB.
Definition: isf_util.c:42
The isf_util.h file contains the utility method declarations and macros.
uint32 isf_swap4byte(uint32 n)
4 byte swapping method
Definition: isf_util.c:143
#define SysTick
Main ISF header file. Contains code common to all ISF components.
unsigned short int uint16
Definition: isf_types.h:77
unsigned long int uint32
Definition: isf_types.h:78
void isf_mem_copy(void *src, void *dest, uint32 size)
memory copy
Definition: isf_util.c:128