Sensor sampling library

Files at this revision

API Documentation at this revision

Comitter:
Joseph_Penikis
Date:
Mon Feb 23 23:10:36 2015 +0000
Parent:
0:eea8d19a7f6b
Commit message:
CURRENTLY BROKEN: NOT FUNCTIONING AS REQUIRED

Changed in this revision

Sensors.h Show annotated file Show diff for this revision Revisions of this file
Sensors.s Show diff for this revision Revisions of this file
sensor_setup.s Show annotated file Show diff for this revision Revisions of this file
--- a/Sensors.h	Mon Feb 23 18:03:29 2015 +0000
+++ b/Sensors.h	Mon Feb 23 23:10:36 2015 +0000
@@ -1,32 +1,23 @@
 #ifndef SENSORS_H
 #define SENSORS_H
  
-#define CHANNEL_0 0x00
 #define CHANNEL_1 0x01
 #define CHANNEL_2 0x02
-#define CHANNEL_3 0x03
  
 #define CPU_CLOCK 48000000
  
 /**
-*   Pulses sets how many edges to detect before returning a time, max of 2^16, affectively refers to sample time
-*   Glitch filter recognizes change on input pin after 2^n rising clock edges
-*   Setting glitch filter to zero disables glitch filter
-*   TODO: DETERMINE EXACT FUNCTIONALITY AND USAFULNESS OF FUNCTION
+*   Start SysTick
 */
-extern "C" void setup_counter(int glitch_filter);
+extern "C" void start_systick();
  
 /**
 *   Measure the period observed on the sensor indicated by "sensor"
-*   TO VERIFY (KL25Z):
-*       PTC1 -> Pulse Counter Input 0 (00)
-*       PTC2 -> Pulse Counter Input 1 (01)
-*       PTC3 -> Pulse Counter Input 2 (10)
-*       PTC4 -> Pulse Counter Input 3 (11)
+*   TODO: Pin Routing
 */
-extern "C" int measure_clock_cycles(char sensor, int samples);
+extern "C" int measure_clock_cycles(int sensor, int samples);
  
-float measure_frequency(char sensor, int samples)
+float measure_frequency(int sensor, int samples)
 {
     // Divide the number of cpu clock cycles by the number of measured periods of the measured waveform to
     // get the number of clock cycles per period
--- a/Sensors.s	Mon Feb 23 18:03:29 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-  AREA asm_func, CODE, READONLY
-; Export my asm function locaiton so that the C compiler can find it and link
-    EXPORT setup_counter
-    EXPORT measure_clock_cycles
-;   
-    ALIGN
-;   
-setup_counter
-;
-; Setup base address for Timer/Counter    
-    LDR r2, =0x40040000
-; Setup data for control register, although don't start it yet
-; TFC set HIGH prevents reset when CNR >= CMR so TCF can be detected
-; TCF is cleared when timer is disabled after reading value
-    LDR r0, =0x06
-    STRB r0, [r2, #0x00]     ; Setup control register
-; Setup compare register
-    ;STRH r0, [r2, #0x08]
-; If prescalar is zero, set the register to all 0, CMPS also sets flags
-    CMPS r1, #0
-    BEQ set_pre_zero
-; Generate prescale register and store, recycle r0 as its finished with
-    LDR r0, =0x03
-    LSLS r1, r1, r0         ; Shifts prescale value to the left 3 places
-    LDR r0, =0x04           
-    ORRS r1, r1, r0         ; Sets prescalar enable flag appropiately
-    LDR r0, =0x78
-    ANDS r1, r1, r0         ; Ensure only the necessary bits are set, maybe redundant
-    B continue              ; Skip setting register value to zero if above code is executed    
-set_pre_zero
-    LDR r1, =0x00000000
-continue
-    STRB r1, [r2, #0x04]     ; Setup prescale register appropriataly
-; Setup base address for SysTick
-    LDR r2, =0xE000E010 
-; Setup SysTick and start
-    LDR r0, [r2, #0x00]     ; Grab current SysTick control state
-    LDR r1, =0x05           ; Mask to set SysTick clock source as processor clock and start it running
-    ORRS r0, r0, r1
-    STR r0, [r2, #0x00]
-; Return to previous code
-    BX  LR
-;
-measure_clock_cycles
-; r0 -> Channel Data, r1 -> SAMPLES
-; Preserve state of all affected registers (ARM convention only requires this on r4 -> r11)
-    PUSH {r4-r6}
-; Setup base address for Timer/Counter    
-    LDR r2, =0x40040000
-; Grab current control status register and prepare with sensor and enable
-    LDR r3, =0x03
-    ANDS r0, r0, r3         ; Trims sensor value to 2 bits in case value larger than 0x03 passed
-    LDR r3, =0x04
-    LSLS r0, r0, r3         ; Shift timer pin select to appropriate location
-    LDR r3, =0x80
-    ORRS r0, r0, r3         ; Set TCF flag
-    LDR r3, [r2, #0x00]     ; Grab current control register state
-    ORRS r0, r0, r3         ; Combine with previous register state
-    STR r0, [r2, #0x00]     ; Save control register
-    LDR r1, [r2, #0x08]     ; Set compare register to number of samples
-    LDR r5, =0x80           ; Mask for TCF
-    BICS r0, r0, r4         ; Disable TCF flag 
-    STR r0, [r2, #0x00]     ; Set control register with TCF disabled        
-; Save current tick count of cpu
-    LDR r3, =0xE000E010     ; Base address of SysTick
-    LDR r4, [r3, #0x08]
-; Start timer
-    STR r0, [r2, #0x00]
-; Wait until TCF = 1 in Timer/Counter
-loop
-    LDR r0, [r2, #0x00]
-    ANDS r0, r0, r5
-    BEQ loop                ; Loops while TCF flag is zero
-; Save current tick count of cpu
-    LDR r5, [r3, #0x08]   
-; Disable timer by toggling bit
-    LDR r3, =0x01
-    LDR r6, [r2, #0x00]
-    EORS r3, r3, r6
-    STR r3, [r2, #0x00]
-; Compute time difference and place it into r0, r0 is the return value of the function
-    MOV r0, r4
-    SBCS r0, r0, r5         ; r0 = r4 - r5 (With sign)
-    BPL finish              ; If result of subtraction creates a negative number, return value, otherwise recalculate 
-    LDR r3, =0x00FFFFFF
-    SUBS r0, r3, r5         ; r0 = (2^24 - 1 - r5) + r4
-    ADDS r0, r0, r4
-finish
-    ;LDR r0, [r2, #0x00]
-; Return registers to previous states
-    POP {r4-r6}
-; Return to previous code
-    BX  LR
-;
-    ALIGN
-    END
-           
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sensor_setup.s	Mon Feb 23 23:10:36 2015 +0000
@@ -0,0 +1,61 @@
+  AREA asm_func, CODE, READONLY
+; Export my asm function locaiton so that the C compiler can find it and link
+    EXPORT start_systick
+    EXPORT measure_clock_cycles
+;   
+    ALIGN
+;   
+start_systick   
+;   
+; Setup base address for SysTick
+    LDR r2, =0xE000E010 
+; Setup SysTick and start
+    LDR r0, [r2, #0x00]     ; Grab current SysTick control state
+    LDR r1, =0x05           ; Mask to set SysTick clock source as processor clock and start it running
+    ORRS r0, r0, r1
+    STR r0, [r2, #0x00]
+; Return to previous code
+    BX  LR
+;
+measure_clock_cycles
+;
+    PUSH {r4-r7}
+; Configure pin to sample from
+    LDR r3, =0xF80FF094         ; Port C data direction register
+    LDR r4, =0x00000020         ; Pin 5
+    LDR r5, [r3]                ; Current config
+    BICS r5, r5, r4             ; Turn PinC5 to 0, General Purpose Input
+    STR r5, [r3]                ; Save config 
+; Setup base address for current SysTick value
+    LDR r5, =0xE000E018
+; Sample starting SysTick   
+    LSLS r0, r0, #2             ; Multiply by 2 as both rising and failing edges detected
+    LDR r0, =0x00               ; Clear counter
+    LDR r2, =0x20               ; Pin mask
+    LDR r6, =0xF80FF090         ; Input register
+    LDR r7, =0x00               ; Current pin state
+    LDR r4, [r5]                ; Read current SysTick
+loop
+    LDR r3, [r6]                ; Read Input register
+    ANDS r3, r3, r2             ; Mask out pin
+    SUBS r3, r3, r7             ; Sneaky method of detecting bit change, equals zero when no change has occurred
+    BEQ loop                    ; Loop Until change
+    MOV r7, r3                  ; Store new pin state  
+    ADDS r0, #1                 ; Increment counter by one
+    SUBS r3, r3, r0             ; Subtract SAMPLES from counter
+    BNE loop                    ; Loop until counter = samples
+    LDR r1, [r5]                ; Read ending SysTick
+; Compute time difference
+    MOV r0, r4
+    SBCS r0, r0, r1             ; r0 = r4 - r1
+    BPL finish                  ; If result of subtraction creates a positive number, return r0, otherwise recalculate
+    LDR r3, =0x00FFFFFF
+    SUBS r0, r3, r1             ; r0 = (2^24 - 1 - r1) + r4
+    ADDS r0, r0, r4
+finish
+; Return to previous code
+    POP {r4-r7}
+    BX LR
+    ALIGN
+    END
+           
\ No newline at end of file