@@ -84,7 +84,7 @@ static int hc_sr04_release(struct inode* node, struct file* file) {
8484static int do_send (struct hcsr_struct * hcsr ){
8585 unsigned int counter = 0 ;
8686 char cnt = 0 ;
87- int trig_pin = hcsr -> pins [ TRIGGER_INDEX ][ HC_GPIO_LINUX ][ PIN_INDEX ] ;
87+ int trig_pin = hcsr -> kconfig . set . pins . trigger_pin ;
8888
8989 gpio_set_value (trig_pin , 1 );
9090 udelay (10 );
@@ -133,7 +133,7 @@ static int send(struct hcsr_struct* hcsr, int retry_cnt) {
133133
134134static int thread_function (void * data ) {
135135 struct hcsr_struct * hcsr = (struct hcsr_struct * ) data ;
136- unsigned long ms = (1000 )/(hcsr -> kconfig .set .freq );
136+ unsigned long ms = (1000 )/(hcsr -> kconfig .set .working_mode . freq );
137137 do {
138138 send (hcsr , 3 );
139139 msleep_interruptible (ms );
@@ -170,11 +170,11 @@ static irqreturn_t echo_recv_isr(int irq, void *data) {
170170 spin_lock (& (hcsr -> cirb .cir_buf_lock ));
171171 hcsr -> cirb .newest = (hcsr -> cirb .newest + 1 )%NUM_DATA ;
172172 hcsr -> cirb .buf .data [(hcsr -> cirb .newest )] = distance ;
173- printk (KERN_ALERT "echo_recv_isr: %d CM, newest idx:%d, from %s, mode:%d \n" ,distance , hcsr -> cirb .newest , hcsr -> hc_sr04 -> name , hcsr -> kconfig .set .mode );
173+ printk (KERN_ALERT "echo_recv_isr: %d CM, newest idx:%d, from %s, mode:%d \n" ,distance , hcsr -> cirb .newest , hcsr -> hc_sr04 -> name , hcsr -> kconfig .set .working_mode . mode );
174174 spin_unlock (& (hcsr -> cirb .cir_buf_lock ));
175175
176176 spin_lock (& (hcsr -> kconfig .kconfig_lock ));
177- if (ONE_SHOT == hcsr -> kconfig .set .mode ) {
177+ if (ONE_SHOT == hcsr -> kconfig .set .working_mode . mode ) {
178178 spin_unlock (& (hcsr -> kconfig .kconfig_lock ));
179179 } else {
180180 spin_unlock (& (hcsr -> kconfig .kconfig_lock ));
@@ -193,7 +193,6 @@ static irqreturn_t echo_recv_isr(int irq, void *data) {
193193static long ioctl_SETPINs (struct file * filp , unsigned long addr ) {
194194 pin_set pins ;
195195 int ret = 0 ;
196- int i , j ;
197196 struct hcsr_struct * hcsr = NULL ;
198197 printk (KERN_ALERT "ioctl_SETPIN\n" );
199198 if (0 != copy_from_user (& pins , (void * )addr , sizeof (pin_set ))) {
@@ -204,67 +203,30 @@ static long ioctl_SETPINs(struct file* filp, unsigned long addr) {
204203
205204 //hcsr = get_curr_hcsr(filp->f_dentry->d_inode);
206205 hcsr = (struct hcsr_struct * )filp -> private_data ;
207- //if(...)
208- if (hcsr -> pins [TRIGGER_INDEX ][HC_GPIO_LINUX ][PIN_INDEX ] != pins .trigger_pin || hcsr -> pins [ECHO_INDEX ][HC_GPIO_LINUX ][PIN_INDEX ] != pins .echo_pin ) {
209- ret = - EINVAL ;
210- goto ERR_SETPIN_RETURN ;
211- }
206+ hcsr -> kconfig .set .pins = pins ;
207+ printk (KERN_ALERT "ioctl_SETPIN: Trig:%d, Echo:%d\n" , hcsr -> kconfig .set .pins .trigger_pin , hcsr -> kconfig .set .pins .echo_pin );
212208
213209 spin_lock (& (hcsr -> irq_done_lock ));
214210 if (IRQ_DONE == hcsr -> irq_done ) {
215211 goto SUCCESS_SETPIN_RETURN ;
216212 }
217213
218- for (i = 0 ; i < SET_SIZE ; i ++ ) {
219- for (j = 0 ; j < PIN_SIZE ; j ++ ) {
220- if (-1 == hcsr -> pins [i ][j ][PIN_INDEX ])
221- continue ;
222- ret = gpio_request (hcsr -> pins [i ][j ][PIN_INDEX ], hcsr -> pin_str [i ][j ]);
223- if (ret ) {
224- printk (KERN_ALERT "SETPIN: gpio_request,pin:%d, ret=%d\n" , hcsr -> pins [i ][j ][PIN_INDEX ], ret );
225- goto ERR_SETPIN_RETURN ;
226- }
227-
228- //if(unlikely(ECHO_INDEX == i && HC_GPIO_LINUX == j))
229- if (ECHO_INDEX == i && HC_GPIO_LINUX == j )
230- {
231- ret = gpio_direction_input (hcsr -> pins [i ][j ][PIN_INDEX ]);
232- if (ret ) {
233- printk (KERN_ALERT "SETPIN: gpio_dir_input:%d\n" , ret );
234- goto ERR_SETPIN_RETURN ;
235- }
236- }
237-
238- else if (HC_GPIO_MUX0 == j || HC_GPIO_MUX1 == j ) {
239- gpio_set_value (hcsr -> pins [i ][j ][PIN_INDEX ], hcsr -> pins [i ][j ][VAL_INDEX ]);
240- }
241-
242- else {
243- ret = gpio_direction_output (hcsr -> pins [i ][j ][PIN_INDEX ], hcsr -> pins [i ][j ][VAL_INDEX ]);
244- if (ret ) {
245- printk (KERN_ALERT "SETPIN: gpio_dir_output:%d:%d\n" , hcsr -> pins [i ][j ][VAL_INDEX ],ret );
246- goto ERR_SETPIN_RETURN ;
247- }
248- }
249- }
250- }
251-
252- //For Echo ISR
253- ret = gpio_to_irq (hcsr -> pins [ECHO_INDEX ][HC_GPIO_LINUX ][PIN_INDEX ]);
254- if (0 > ret ) {
255- printk (KERN_ALERT "SETPIN: gpio_to_irq:%d\n" , ret );
214+ ret = setTrig (hcsr , hcsr -> kconfig .set .pins .trigger_pin );
215+ if (ret ) {
216+ printk (KERN_ALERT "setTrig Error,pin %d" , hcsr -> kconfig .set .pins .trigger_pin );
256217 goto ERR_SETPIN_RETURN ;
257- } else {
258- hcsr -> echo_isr_number = ret ;
259218 }
260- printk ("HERE" );
261- //ret = request_irq(hcsr->echo_isr_number, echo_recv_isr, IRQ_FLAG, hcsr->pin_str[ECHO_INDEX][HC_GPIO_LINUX], hcsr);
262- ret = request_irq (hcsr -> echo_isr_number , echo_recv_isr , IRQ_FLAG , "HC-SR04" , hcsr );
219+ ret = setEcho (hcsr , hcsr -> kconfig .set .pins .echo_pin );
263220 if (ret ) {
264- printk (KERN_ALERT "SETPIN: request_irq:%d\n " , ret );
221+ printk (KERN_ALERT "setEcho Error,pin %d " , hcsr -> kconfig . set . pins . echo_pin );
265222 goto ERR_SETPIN_RETURN ;
266223 }
267224
225+ ret = set_ISR (hcsr );
226+ if (ret ) {
227+ printk (KERN_ALERT "setEcho: set_ISR failed \n" );
228+ goto ERR_SETPIN_RETURN ;
229+ }
268230 goto SUCCESS_SETPIN_RETURN ;
269231
270232ERR_SETPIN_RETURN :
@@ -281,11 +243,11 @@ static long ioctl_SETPINs(struct file* filp, unsigned long addr) {
281243
282244static long ioctl_SETMODE (struct file * file , unsigned long addr ) {
283245 int ret = 0 ;
284- hcsr_set set ;
246+ mode_set working_mode ;
285247 struct hcsr_struct * hcsr = NULL ;
286248
287249 printk (KERN_ALERT "ioctl_SETMODE\n" );
288- if (0 != copy_from_user (& set , (void * )addr , sizeof (hcsr_set ))) {
250+ if (0 != copy_from_user (& working_mode , (void * )addr , sizeof (mode_set ))) {
289251 printk (KERN_ALERT "SETPIN: copy_from_user ERROR\n" );
290252 ret = - EAGAIN ;
291253 goto ERR_SETMODE_RETURN ;
@@ -297,13 +259,13 @@ static long ioctl_SETMODE(struct file* file, unsigned long addr) {
297259 // still peeriodical sampling
298260 //before: one-shot ; after: one-shot
299261 /*For Periodic Task*/
300- hcsr -> kconfig .set = set ;
301- printk (KERN_ALERT "%s: mode:%d, freq:%d\n" , hcsr -> hc_sr04 -> name , hcsr -> kconfig .set .mode , hcsr -> kconfig .set .freq );
302- if (ONE_SHOT != hcsr -> kconfig .set .mode && NULL != hcsr -> kthread ) {
262+ hcsr -> kconfig .set . working_mode = working_mode ;
263+ printk (KERN_ALERT "%s: mode:%d, freq:%d\n" , hcsr -> hc_sr04 -> name , hcsr -> kconfig .set .working_mode . mode , hcsr -> kconfig .set . working_mode .freq );
264+ if (ONE_SHOT != hcsr -> kconfig .set .working_mode . mode && NULL != hcsr -> kthread ) {
303265 printk (KERN_ALERT "Keep Thread Going with period change: %s\n" , hcsr -> hc_sr04 -> name );
304266 kthread_stop (hcsr -> kthread );
305267 hcsr -> kthread = kthread_run (thread_function , hcsr , hcsr -> hc_sr04 -> name );
306- }else if (ONE_SHOT == hcsr -> kconfig .set .mode && NULL != hcsr -> kthread ) {
268+ }else if (ONE_SHOT == hcsr -> kconfig .set .working_mode . mode && NULL != hcsr -> kthread ) {
307269 printk (KERN_ALERT "Enter OneShot, Stop previous thread: %s\n" , hcsr -> hc_sr04 -> name );
308270 kthread_stop (hcsr -> kthread );
309271 hcsr -> kthread = NULL ;
@@ -355,7 +317,7 @@ static ssize_t hc_sr04_read(struct file *file, char *buf, size_t count, loff_t *
355317 printk (KERN_ALERT "hc_sr04: Read\n" );
356318
357319 spin_lock (& (hcsr -> kconfig .kconfig_lock ));
358- if (ONE_SHOT == hcsr -> kconfig .set .mode ) {
320+ if (ONE_SHOT == hcsr -> kconfig .set .working_mode . mode ) {
359321 spin_unlock (& (hcsr -> kconfig .kconfig_lock ));
360322
361323 do {
@@ -413,7 +375,7 @@ static ssize_t hc_sr04_write(struct file *file, const char __user *buf, size_t c
413375
414376
415377 spin_lock (& (hcsr -> kconfig .kconfig_lock ));
416- if (ONE_SHOT == hcsr -> kconfig .set .mode ) {
378+ if (ONE_SHOT == hcsr -> kconfig .set .working_mode . mode ) {
417379 spin_unlock (& (hcsr -> kconfig .kconfig_lock ));
418380
419381 if (0 != act ) {
@@ -483,8 +445,8 @@ static void init_hcsr_struct(hcsr_struct* hcsr, struct miscdevice* md, char(*pin
483445 hcsr -> hc_sr04 = md ;
484446 hcsr -> irq_done = IRQ_NOT_DONE ;
485447 hcsr -> echo_isr_number = -1 ;
486- hcsr -> pins = pins ;
487- hcsr -> kconfig .set .mode = 0 ;//__DEBUG__ ********
448+ hcsr -> pins_default = pins ;
449+ hcsr -> kconfig .set .working_mode . mode = 0 ;//__DEBUG__ ********
488450// hcsr->kconfig.set.mode = -1;
489451 hcsr -> cirb .newest = -1 ;
490452 hcsr -> pin_str = pin_str ;
@@ -513,17 +475,8 @@ static int __init hc_sr04_init(void) {
513475}
514476
515477void free_gpio (struct hcsr_struct * hcsr ) {
516- int i , j ;
517- for (i = 0 ; i < 2 ; i ++ ) {
518- for (j = 0 ; j < PIN_SIZE ; j ++ ) {
519- if (-1 == hcsr -> pins [i ][j ][PIN_INDEX ]) {
520- continue ;
521- }
522- //gpio_set_value(hcsr->pins[i][j][PIN_INDEX],0);
523- printk (KERN_ALERT "gpinfree:%d\n" , hcsr -> pins [i ][j ][PIN_INDEX ]);
524- gpio_free (hcsr -> pins [i ][j ][PIN_INDEX ]);
525- }
526- }
478+ freeTrig (hcsr -> kconfig .set .pins .trigger_pin );
479+ freeEcho (hcsr -> kconfig .set .pins .echo_pin );
527480}
528481
529482static void __exit hc_sr04_exit (void ) {
0 commit comments