Skip to content

Commit 452ff76

Browse files
committed
PIN MUX & Big Table
1 parent 1fd4d22 commit 452ff76

File tree

5 files changed

+215
-87
lines changed

5 files changed

+215
-87
lines changed

assignment02/part1/hc_sr04-test.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ typedef struct pdata {
1919
void* pfunction(void* datain) {
2020
int i = 0;
2121
pdata* data = (pdata*) datain;
22-
printf("mode:%d, freq:%u\n", data->set.mode, data->set.freq);
23-
ioctl(data->fd, SETMODE,(int)&(data->set), 0);
22+
printf("mode:%d, freq:%u\n", data->set.working_mode.mode, data->set.working_mode.freq);
23+
ioctl(data->fd, SETMODE,(int)&(data->set.working_mode), 0);
2424
i = 1;
2525
read(data->fd, &i, sizeof(int));
2626
sleep(1);
@@ -44,8 +44,8 @@ int main(int argc, char*argv[]) {
4444
int i = 1;
4545
pin_set Apins = {HCSR_A_TRIG_PIN, HCSR_A_ECHO_PIN};
4646
pin_set Bpins = {HCSR_B_TRIG_PIN, HCSR_B_ECHO_PIN};
47-
hcsr_set Aset = {0, 7};
48-
hcsr_set Bset = {1, 7};
47+
mode_set Aset = {0, 7};
48+
mode_set Bset = {2, 7};
4949
int ret = 0;
5050
int fdA = open("/dev/HCSR_1", O_RDWR);
5151
ret = ioctl(fdA, SETPINS, &Apins, 0);
@@ -98,11 +98,11 @@ int main(int argc, char*argv[]) {
9898

9999
//For multi-thread test
100100
Adata.fd = fdA;
101-
Adata.set.mode = 1;
102-
Adata.set.freq = 7;
101+
Adata.set.working_mode.mode = 1;
102+
Adata.set.working_mode.freq = 7;
103103
Bdata.fd = fdB;
104-
Bdata.set.mode = 0;
105-
Bdata.set.freq = 7;
104+
Bdata.set.working_mode.mode = 0;
105+
Bdata.set.working_mode.freq = 7;
106106
pthread_t tA, tB;
107107
pthread_create(&tA, NULL, pfunction, &Adata);
108108
pthread_create(&tB, NULL, pfunction, &Bdata);

assignment02/part1/hc_sr04.c

Lines changed: 28 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static int hc_sr04_release(struct inode* node, struct file* file) {
8484
static 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

134134
static 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) {
193193
static 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

270232
ERR_SETPIN_RETURN:
@@ -281,11 +243,11 @@ static long ioctl_SETPINs(struct file* filp, unsigned long addr) {
281243

282244
static 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

515477
void 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

529482
static void __exit hc_sr04_exit(void) {

assignment02/part1/hc_sr04_common.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ typedef struct pin_set {
1717
int echo_pin;
1818
} pin_set;
1919

20-
typedef struct hcsr_set {
20+
typedef struct mode_set {
2121
int mode;
2222
unsigned int freq;
23+
} mode_set;
24+
25+
typedef struct hcsr_set {
26+
pin_set pins;
27+
mode_set working_mode;
2328
} hcsr_set;
2429

2530
typedef struct record {

0 commit comments

Comments
 (0)