• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <float.h>
20 
21 #include <eventnums.h>
22 #include <gpio.h>
23 #include <heap.h>
24 #include <hostIntf.h>
25 #include <isr.h>
26 #include <nanohubPacket.h>
27 #include <sensors.h>
28 #include <seos.h>
29 #include <slab.h>
30 #include <timer.h>
31 #include <plat/inc/gpio.h>
32 #include <plat/inc/exti.h>
33 #include <plat/inc/syscfg.h>
34 #include <variant/inc/variant.h>
35 
36 #define VSYNC_APP_ID      APP_ID_MAKE(APP_ID_VENDOR_GOOGLE, 7)
37 #define VSYNC_APP_VERSION 2
38 
39 // This defines how many vsync events we could handle being backed up in the
40 // queue. Use this to size our slab
41 #define MAX_VSYNC_EVENTS  4
42 
43 #ifndef VSYNC_PIN
44 #error "VSYNC_PIN is not defined; please define in variant.h"
45 #endif
46 
47 #ifndef VSYNC_IRQ
48 #error "VSYNC_IRQ is not defined; please define in variant.h"
49 #endif
50 
51 #define INFO_PRINT(fmt, ...) do { \
52         osLog(LOG_INFO, "%s " fmt, "[VSYNC]", ##__VA_ARGS__); \
53     } while (0);
54 
55 #define ERROR_PRINT(fmt, ...) INFO_PRINT("%s" fmt, "ERROR: ", ##__VA_ARGS__); \
56 
57 #define DEBUG_PRINT(fmt, ...) do { \
58         if (enable_debug) {  \
59             INFO_PRINT(fmt, ##__VA_ARGS__); \
60         } \
61     } while (0);
62 
63 static const bool enable_debug = 0;
64 
65 static struct SensorTask
66 {
67     struct Gpio *pin;
68     struct ChainedIsr isr;
69     struct SlabAllocator *evtSlab;
70 
71 
72     uint32_t id;
73     uint32_t sensorHandle;
74 
75     bool on;
76 } mTask;
77 
vsyncAllocateEvt(struct SingleAxisDataEvent ** evPtr,uint64_t time)78 static bool vsyncAllocateEvt(struct SingleAxisDataEvent **evPtr, uint64_t time)
79 {
80     struct SingleAxisDataEvent *ev;
81 
82     *evPtr = slabAllocatorAlloc(mTask.evtSlab);
83 
84     ev = *evPtr;
85     if (!ev) {
86         ERROR_PRINT("slabAllocatorAlloc() failed\n");
87         return false;
88     }
89 
90     memset(&ev->samples[0].firstSample, 0x00, sizeof(struct SensorFirstSample));
91     ev->referenceTime = time;
92     ev->samples[0].firstSample.numSamples = 1;
93     ev->samples[0].idata = 1;
94 
95     return true;
96 }
97 
vsyncFreeEvt(void * ptr)98 static void vsyncFreeEvt(void *ptr)
99 {
100     slabAllocatorFree(mTask.evtSlab, ptr);
101 }
102 
vsyncIsr(struct ChainedIsr * localIsr)103 static bool vsyncIsr(struct ChainedIsr *localIsr)
104 {
105     struct SensorTask *data = container_of(localIsr, struct SensorTask, isr);
106     struct SingleAxisDataEvent *ev;
107 
108     if (!extiIsPendingGpio(data->pin)) {
109         return false;
110     }
111 
112     if (data->on) {
113         if (vsyncAllocateEvt(&ev, sensorGetTime())) {
114             if (!osEnqueueEvtOrFree(EVENT_TYPE_BIT_DISCARDABLE | sensorGetMyEventType(SENS_TYPE_VSYNC), ev, vsyncFreeEvt)) {
115                 ERROR_PRINT("osEnqueueEvtOrFree() failed\n");
116             }
117         }
118     }
119 
120     extiClearPendingGpio(data->pin);
121     return true;
122 }
123 
enableInterrupt(struct Gpio * pin,struct ChainedIsr * isr)124 static bool enableInterrupt(struct Gpio *pin, struct ChainedIsr *isr)
125 {
126     gpioConfigInput(pin, GPIO_SPEED_LOW, GPIO_PULL_NONE);
127     syscfgSetExtiPort(pin);
128     extiEnableIntGpio(pin, EXTI_TRIGGER_FALLING);
129     extiChainIsr(VSYNC_IRQ, isr);
130     return true;
131 }
132 
disableInterrupt(struct Gpio * pin,struct ChainedIsr * isr)133 static bool disableInterrupt(struct Gpio *pin, struct ChainedIsr *isr)
134 {
135     extiUnchainIsr(VSYNC_IRQ, isr);
136     extiDisableIntGpio(pin);
137     return true;
138 }
139 
140 static const struct SensorInfo mSensorInfo =
141 {
142     .sensorName = "Camera Vsync",
143     .sensorType = SENS_TYPE_VSYNC,
144     .numAxis = NUM_AXIS_ONE,
145     .interrupt = NANOHUB_INT_NONWAKEUP,
146     .minSamples = 20,
147 };
148 
vsyncPower(bool on,void * cookie)149 static bool vsyncPower(bool on, void *cookie)
150 {
151     INFO_PRINT("power %d\n", on);
152 
153     if (on) {
154         extiClearPendingGpio(mTask.pin);
155         enableInterrupt(mTask.pin, &mTask.isr);
156     } else {
157         disableInterrupt(mTask.pin, &mTask.isr);
158         extiClearPendingGpio(mTask.pin);
159     }
160 
161     mTask.on = on;
162     sensorSignalInternalEvt(mTask.sensorHandle, SENSOR_INTERNAL_EVT_POWER_STATE_CHG, on, 0);
163     return true;
164 }
165 
vsyncFirmwareUpload(void * cookie)166 static bool vsyncFirmwareUpload(void *cookie)
167 {
168     return sensorSignalInternalEvt(mTask.sensorHandle, SENSOR_INTERNAL_EVT_FW_STATE_CHG, 1, 0);
169 }
170 
vsyncSetRate(uint32_t rate,uint64_t latency,void * cookie)171 static bool vsyncSetRate(uint32_t rate, uint64_t latency, void *cookie)
172 {
173     INFO_PRINT("setRate\n");
174     return sensorSignalInternalEvt(mTask.sensorHandle, SENSOR_INTERNAL_EVT_RATE_CHG, rate, latency);
175 }
176 
vsyncFlush(void * cookie)177 static bool vsyncFlush(void *cookie)
178 {
179     INFO_PRINT("flush\n");
180     return osEnqueueEvt(sensorGetMyEventType(SENS_TYPE_VSYNC), SENSOR_DATA_EVENT_FLUSH, NULL);
181 }
182 
183 static const struct SensorOps mSensorOps =
184 {
185     .sensorPower = vsyncPower,
186     .sensorFirmwareUpload = vsyncFirmwareUpload,
187     .sensorSetRate = vsyncSetRate,
188     .sensorFlush = vsyncFlush,
189 };
190 
handleEvent(uint32_t evtType,const void * evtData)191 static void handleEvent(uint32_t evtType, const void* evtData)
192 {
193 }
194 
startTask(uint32_t taskId)195 static bool startTask(uint32_t taskId)
196 {
197     mTask.id = taskId;
198     mTask.sensorHandle = sensorRegister(&mSensorInfo, &mSensorOps, NULL, true);
199     mTask.pin = gpioRequest(VSYNC_PIN);
200     mTask.isr.func = vsyncIsr;
201 
202     mTask.evtSlab = slabAllocatorNew(sizeof(struct SingleAxisDataEvent) + sizeof(struct SingleAxisDataPoint), 4, MAX_VSYNC_EVENTS);
203     if (!mTask.evtSlab) {
204         ERROR_PRINT("slabAllocatorNew() failed\n");
205         return false;
206     }
207 
208     return true;
209 }
210 
endTask(void)211 static void endTask(void)
212 {
213     disableInterrupt(mTask.pin, &mTask.isr);
214     extiUnchainIsr(VSYNC_IRQ, &mTask.isr);
215     extiClearPendingGpio(mTask.pin);
216     gpioRelease(mTask.pin);
217     sensorUnregister(mTask.sensorHandle);
218 }
219 
220 INTERNAL_APP_INIT(VSYNC_APP_ID, VSYNC_APP_VERSION, startTask, endTask, handleEvent);
221