Пример #1
0
Файл: uart.c Проект: MatKub/RIOT
void UART_2_ISR(void)
{
    /* Check for RX data valid interrupt */
    if(UART_2_DEV->STATUS & LEUART_STATUS_RXDATAV) {
        /* Copy data into RX Buffer */
        uint8_t rxData = LEUART_Rx(UART_2_DEV);

        irq_read(UART_2, rxData);
    }
    /* Check TX buffer level status */
    if(UART_2_DEV->IF & LEUART_IF_TXBL & UART_2_DEV->IEN) {
#if UART_2_ENABLE_BUF
        int data = ringbuffer_get_one(&rb_uart2);
        if(-1 != data) {
            /* Avoid deadlock if modifying the same register twice when freeze mode is */
            /* activated. */
            if(!(UART_2_DEV->FREEZE & LEUART_FREEZE_REGFREEZE)) {
                /* Wait for any pending previous write operation to have been completed */
                /* in low frequency domain */
                while(UART_2_DEV->SYNCBUSY & LEUART_SYNCBUSY_TXDATA)
                    ;;
            }
            /* Write data to TX register */
            UART_2_DEV->TXDATA = (uint32_t)data;
        } else {
            LEUART_IntDisable(UART_2_DEV, LEUART_IF_TXBL);
        }
#endif
    }

    if(sched_context_switch_request) {
        thread_yield();
    }
}
Пример #2
0
Файл: uart.c Проект: MatKub/RIOT
void UART_1_RX_ISR(void)
{
    /* Check for RX data valid interrupt */
    if(UART_1_DEV->STATUS & UART_STATUS_RXDATAV) {
        /* Copy data into RX Buffer */
        uint8_t rxData = USART_Rx(UART_1_DEV);
        irq_read(UART_1, rxData);
    }
    if(sched_context_switch_request) {
        thread_yield();
    }
}
Пример #3
0
int main(int argc, char **argv) {

    enable_shell_log();
    disable_file_log();
    set_loglevel(LOG_NOTICE);

    progname = malloc((10*sizeof(char))+1);
    progname = strdup("433-debug");

#ifdef USE_LIRC
    lirc_t data;
    char *socket = strdup("/dev/lirc0");
    int have_device = 0;
#else
    int newDuration;
#endif

    int duration = 0;
    int i = 0;
    int y = 0;

    int recording = 1;
    int bit = 0;
    int raw[255];
    int pRaw[255];
    int code[255];
    int binary[255];
    int footer = 0;
    int header = 0;
    int pulse = 0;
    int rawLength = 0;
    int binaryLength = 0;

    int loop = 1;

#ifdef USE_LIRC
    hw_choose_driver(NULL);
#endif

    addOption(&options, 'H', "help", no_value, 0, NULL);
    addOption(&options, 'V', "version", no_value, 0, NULL);
#ifdef USE_LIRC
    addOption(&options, 'S', "socket", has_value, 0, "^/dev/([A-Za-z]+)([0-9]+)");
#endif

    while (1) {
        int c;
        c = getOptions(&options, argc, argv, 1);
        if(c == -1)
            break;
        switch (c) {
        case 'h':
            printf("Usage: %s [options]\n", progname);
            printf("\t -H --help\t\tdisplay usage summary\n");
            printf("\t -V --version\t\tdisplay version\n");
#ifdef USE_LIRC
            printf("\t -S --socket=socket\tread from given socket\n");
#endif
            return (EXIT_SUCCESS);
            break;
        case 'v':
            printf("%s %s\n", progname, "1.0");
            return (EXIT_SUCCESS);
            break;
#ifdef USE_LIRC
        case 'S':
            socket = optarg;
            have_device = 1;
            break;
#endif
        default:
            printf("Usage: %s [options]\n", progname);
            return (EXIT_FAILURE);
            break;
        }
    }

#ifdef USE_LIRC
    if(strcmp(socket, "/var/lirc/lircd") == 0) {
        logprintf(LOG_ERR, "refusing to connect to lircd socket");
        return EXIT_FAILURE;
    }

    if(have_device)
        hw.device = socket;

    if(!hw.init_func()) {
        return EXIT_FAILURE;
    }
#endif

    /*
    End of the original (but stripped) code of mode2
    */

#ifndef USE_LIRC
    if(gpio_request(GPIO_IN_PIN) == 0) {
        /* Attach an interrupt to the requested pin */
        irq_attach(GPIO_IN_PIN, CHANGE);
    } else {
        return EXIT_FAILURE;
    }
#endif

    while(loop) {
#ifdef USE_LIRC
        data = hw.readdata(0);
        duration = (data & PULSE_MASK);
#else

        unsigned short a = 0;

        if((newDuration = irq_read()) == 1) {
            continue;
        }

        for(a=0; a<2; a++) {
            if(a==0) {
                duration = PULSE_LENGTH;
            } else {
                duration = newDuration;
                if(duration < 750) {
                    duration = PULSE_LENGTH;
                }
            }
#endif

        /* If we are recording, keep recording until the next footer has been matched */
        if(recording == 1) {
            if(bit < 255) {
                raw[bit++] = duration;
            } else {
                bit = 0;
                recording = 0;
            }
        }

        /* First try to catch code that seems to be a footer.
           If a real footer has been recognized, start using that as the new footer */
        if((duration > 5000
                && duration < 100000 && footer == 0) || ((footer-(footer*0.1)<duration) && (footer+(footer*0.1)>duration))) {
            recording = 1;

            /* Check if we are recording similar codes */
            for(i=0; i<(bit-1); i++) {
                if(!(((pRaw[i]-(pRaw[i]*0.3)) < raw[i]) && ((pRaw[i]+(pRaw[i]*0.3)) > raw[i]))) {
                    y=0;
                    recording=0;
                }
                pRaw[i]=raw[i];
            }
            y++;

            /* Continue if we have 2 matches */
            if(y>2) {
                /* If we are certain we are recording similar codes.
                   Save the header values and the raw code length */
                if(footer>0) {
                    if(header == 0) {
                        header=raw[1];
                    }
                    if(rawLength == 0)
                        rawLength=bit;
                }
                /* Try to catch the footer, and the low and high values */
                for(i=0; i<bit; i++) {
                    if((i+1)<bit && i > 2 && footer > 0) {
                        if((raw[i]/PULSE_LENGTH) >= 2) {
                            pulse=raw[i];
                        }
                    }
                    if(duration > 5000 && duration < 100000) {
                        footer=raw[i];
                    }
                }

                /* If we have gathered all data, stop with the loop */
                if(header > 0 && footer > 0 && pulse > 0 && rawLength > 0) {
                    loop = 0;
                }
            }
            bit=0;
        }

        fflush(stdout);
#ifndef USE_LIRC
    }
#endif
};

/* Convert the raw code into binary code */
for(i=0; i<rawLength; i++) {
    if((unsigned int)raw[i] > (pulse-PULSE_LENGTH)) {
        code[i]=1;
    } else {
        code[i]=0;
    }
}
for(i=2; i<rawLength; i+=4) {
    if(code[i+1] == 1) {
        binary[i/4]=1;
    } else {
        binary[i/4]=0;
    }
}

binaryLength = (int)((float)i/4);

/* Print everything */
printf("--[RESULTS]--\n");
printf("\n");
printf("header:\t\t%d\n",normalize(header));
printf("pulse:\t\t%d\n",normalize(pulse));
printf("footer:\t\t%d\n",normalize(footer));
printf("rawLength:\t%d\n",rawLength);
printf("binaryLength:\t%d\n",binaryLength);
printf("\n");
printf("Raw code:\n");
for(i=0; i<rawLength; i++) {
    printf("%d ",normalize(raw[i])*PULSE_LENGTH);
}
printf("\n");
printf("Binary code:\n");
for(i=0; i<binaryLength; i++) {
    printf("%d",binary[i]);
}
printf("\n");
return (EXIT_SUCCESS);
}
Пример #4
0
int main(int argc, char **argv) {

	log_shell_enable();
	log_shell_disable();
	log_level_set(LOG_NOTICE);

	progname = strdup("pilight-learn");
	struct options_t *options = NULL;
	
	lirc_t data;
	char *socket = strdup("/dev/lirc0");
	char *args;
	int have_device = 0;
	int use_lirc = USE_LIRC;
	int gpio_in = GPIO_IN_PIN;
	
	int duration = 0;
	int i = 0;
	int y = 0;
	int z = 0;

	steps_t state = CAPTURE;
	steps_t pState = WAIT;

	int recording = 1;
	int bit = 0;
	int raw[255];
	int pRaw[255];
	int code[255];
	int onCode[255];
	int offCode[255];
	int allCode[255];
	int unit1Code[255];
	int unit2Code[255];
	int unit3Code[255];
	int binary[255];
	int pBinary[255];
	int onBinary[255];
	int offBinary[255];
	int allBinary[255];
	int unit1Binary[255];
	int unit2Binary[255];
	int unit3Binary[255];
	int loop = 1;
	
	int temp[75];
	int footer = 0;
	int header = 0;
	int pulse = 0;
	int onoff[75];
	int all[75];
	int unit[75];
	int rawLength = 0;
	int binaryLength = 0;

	memset(onoff,-1,75);
	memset(all,-1,75);
	memset(unit,-1,75);

	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&options, 'S', "socket", has_value, 0, "^/dev/([A-Za-z]+)([0-9]+)");
	options_add(&options, 'L', "lirc", no_value, 0, NULL);
	options_add(&options, 'G', "gpio", has_value, 0, "^[0-7]$");
	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if (c == -1)
			break;
		switch (c) {
			case 'H':
				printf("Usage: %s [options]\n", progname);
				printf("\t -H --help\t\tdisplay usage summary\n");
				printf("\t -V --version\t\tdisplay version\n");		
				printf("\t -S --socket=socket\tread from given socket\n");
				printf("\t -L --lirc\t\tuse the lirc_rpi kernel module\n");
				printf("\t -G --gpio=#\t\tGPIO pin we're directly reading from\n");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, "1.0");
				return (EXIT_SUCCESS);
			break;
			case 'L':
				use_lirc = 1;
			break;
			case 'G':
				gpio_in = atoi(optarg);
				use_lirc = 0;
			break;
			case 'S':
				free(socket);
				socket = optarg;
				have_device = 1;
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	
	if(use_lirc == 1) {
		hw_choose_driver(NULL);
		
		if(strcmp(socket, "/var/lirc/lircd") == 0) {
			logprintf(LOG_ERR, "refusing to connect to lircd socket");
			return EXIT_FAILURE;
		}
	
		if(have_device)
			hw.device = socket;
	
		if(!hw.init_func()) {
			return EXIT_FAILURE;
		}
	} else {
		if(wiringPiSetup() == -1)
			return EXIT_FAILURE;

		if(wiringPiISR(gpio_in, INT_EDGE_BOTH) < 0) {
			logprintf(LOG_ERR, "unable to register interrupt for pin %d", gpio_in) ;
			return 1 ;
		}

		(void)piHiPri(55);
	}
	
	printf("Please make sure the daemon is not running when using this debugger.\n\n");	

	while(loop) {
		switch(state) {
			case CAPTURE:
				printf("1. Please send and hold one of the OFF buttons.");
				break;
			case ON:
				/* Store the previous OFF button code */
				for(i=0;i<binaryLength;i++) {
					offBinary[i] = binary[i];
				}
				printf("2. Please send and hold the ON button for the same device\n");
				printf("   as for which you send the previous OFF button.");
				break;
			case OFF:
				/* Store the previous ON button code */
				for(i=0;i<binaryLength;i++) {
					onBinary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					onCode[i] = code[i];
				}
				z=0;

				/* Compare the ON and OFF codes and save bit that are different */
				for(i=0;i<binaryLength;i++) {
					if(offBinary[i] != onBinary[i]) {
						onoff[z++]=i;
					}
				}
				for(i=0;i<rawLength;i++) {
					offCode[i] = code[i];
				}
				printf("3. Please send and hold (one of the) ALL buttons.\n");
				printf("   If you're remote doesn't support turning ON or OFF\n");
				printf("   all devices at once, press the same OFF button as in\n");
				printf("   the beginning.");
			break;
			case ALL:
				z=0;
				memset(temp,-1,75);
				/* Store the ALL code */
				for(i=0;i<binaryLength;i++) {
					allBinary[i] = binary[i];
					if(allBinary[i] != onBinary[i]) {
						temp[z++] = i;
					}
				}
				for(i=0;i<rawLength;i++) {
					allCode[i] = code[i];
				}
				/* Compare the ALL code to the ON and OFF code and store the differences */
				y=0;
				for(i=0;i<binaryLength;i++) {
					if(allBinary[i] != offBinary[i]) {
						all[y++] = i;
					}
				}
				if((unsigned int)z < y) {
					for(i=0;i<z;i++) {
						all[z]=temp[z];
					}
				}
				printf("4. Please send and hold the ON button with the lowest ID.");
			break;
			case UNIT1:
				/* Store the lowest unit code */
				for(i=0;i<binaryLength;i++) {
					unit1Binary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					unit1Code[i] = code[i];
				}
				printf("5. Please send and hold the ON button with the second to lowest ID.");
			break;
			case UNIT2:
				/* Store the second to lowest unit code */
				for(i=0;i<binaryLength;i++) {
					unit2Binary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					unit2Code[i] = code[i];
				}
				printf("6. Please send and hold the ON button with the highest ID.");
			break;
			case PROCESSUNIT:
				z=0;
				/* Store the highest unit code and compare the three codes. Store all
				   bit that are different */
				for(i=0;i<binaryLength;i++) {
					unit3Binary[i] = binary[i];
					if((unit2Binary[i] != unit1Binary[i]) || (unit1Binary[i] != unit3Binary[i]) || (unit2Binary[i] != unit3Binary[i])) {
						unit[z++]=i;
					}
				}
				for(i=0;i<rawLength;i++) {
					unit3Code[i] = code[i];
				}
				state=STOP;
			break;
			case WAIT:
			case STOP:
			default:;
		}
		fflush(stdout);
		if(state!=WAIT)
			pState=state;	
		if(state==STOP)
			loop = 0;
		else
			state=WAIT;		
		if(use_lirc == 1) {
			data = hw.readdata(0);
			duration = (data & PULSE_MASK);
		} else {
			duration = irq_read(gpio_in);
		}

		/* If we are recording, keep recording until the next footer has been matched */
		if(recording == 1) {
			if(bit < 255) {
				raw[bit++] = duration;
			} else {
				bit = 0;
				recording = 0;
			}
		}

		/* First try to catch code that seems to be a footer.
		   If a real footer has been recognized, start using that as the new footer */
		if((duration > 5000
		   && duration < 100000 && footer == 0) || ((footer-(footer*0.1)<duration) && (footer+(footer*0.1)>duration))) {
			recording = 1;

			/* Check if we are recording similar codes */
			for(i=0;i<(bit-1);i++) {
				if(!(((pRaw[i]-(pRaw[i]*0.3)) < raw[i]) && ((pRaw[i]+(pRaw[i]*0.3)) > raw[i]))) {
					y=0;
					z=0;
					recording=0;
				}
				pRaw[i]=raw[i];
			}
			y++;
			/* Continue if we have 2 matches */
			if(y>2) {
				/* If we are certain we are recording similar codes.
				   Save the header values and the raw code length */
				if(footer>0) {
					if(header == 0) {
						header=raw[1];
					}
					if(rawLength == 0)
						rawLength=bit;
				}
				if(rawLength == 0 || rawLength == bit) {
				   /*|| ((((raw[0]-(raw[0]*0.3)) < header[0]) || ((raw[0]+(raw[0]*0.3)) > header[0]))
 				       && (((raw[1]-(raw[1]*0.3)) < header[1]) || ((raw[1]+(raw[1]*0.3)) > header[1]))
					   && (((raw[bit-1]-(raw[bit-1]*0.1)) < footer) || ((raw[bit-1]+(raw[bit-1]*0.1)) > footer)))) {*/

					/* Try to catch the footer, and the low and high values */
					for(i=0;i<bit;i++) {
						if((i+1)<bit && i > 2 && footer > 0) {
							if((raw[i]/PULSE_LENGTH) >= 2) {
								pulse=raw[i];
							}
						}
						if(duration > 5000 && duration < 100000)
							footer=raw[i];
					}
					/* If we have gathered all data, stop with the loop */
					if(header > 0 && footer > 0 && pulse > 0 && rawLength > 0) {
						/* Convert the raw code into binary code */
						for(i=0;i<rawLength;i++) {
							if((unsigned int)raw[i] > (pulse-PULSE_LENGTH)) {
								code[i]=1;
							} else {
								code[i]=0;
							}
						}
						for(i=2;i<rawLength; i+=4) {
							if(code[i+1] == 1) {
								binary[i/4]=1;
							} else {
								binary[i/4]=0;
							}
						}
						if(binaryLength == 0)
							binaryLength = (int)((float)i/4);

						/* Check if the subsequent binary code matches
						   to check if the same button was still held */
						if(binaryLength == (i/4)) {
							for(i=0;i<binaryLength;i++) {
								if(pBinary[i] != binary[i]) {
									z=1;
								}
							}

							/* If we are capturing a different button
							   continue to the next step */
							if(z==1 || state == CAPTURE) {
								switch(pState) {
									case CAPTURE:
										state=ON;
									break;
									case ON:
										state=OFF;
									break;
									case OFF:
										state=ALL;
									break;
									case ALL:
										state=UNIT1;
									break;
									case UNIT1:
										state=UNIT2;
									break;
									case UNIT2:
										state=PROCESSUNIT;
									break;
									case PROCESSUNIT:
										state=STOP;
									break;
									case WAIT:
									case STOP:
									default:;
								}
							printf(" Done.\n\n");
							pState=WAIT;
							}
						}
					}
				}
			}
			bit=0;
		}

		/* Reset the button repeat counter */
		if(z==1) {
			z=0;
			for(i=0;i<binaryLength;i++) {
				pBinary[i]=binary[i];
			}
		}
	}

	rmDup(all, onoff);
	rmDup(unit, onoff);
	rmDup(all, unit);

	/* Print everything */
	printf("--[RESULTS]--\n");
	printf("\n");
	if(normalize(header) == normalize(pulse)) {
		printf("header:\t\t0\n");
	} else {
		printf("header:\t\t%d\n",normalize(header));
	}
	printf("pulse:\t\t%d\n",normalize(pulse));
	printf("footer:\t\t%d\n",normalize(footer));
	printf("rawLength:\t%d\n",rawLength);
	printf("binaryLength:\t%d\n",binaryLength);
	printf("\n");
	printf("on-off bit(s):\t");
	z=0;
	while(onoff[z] > -1) {
		printf("%d ",onoff[z++]);
	}
	printf("\n");
	printf("all bit(s):\t");
	z=0;
	while(all[z] > -1) {
		printf("%d ",all[z++]);
	}
	printf("\n");
	printf("unit bit(s):\t");
	z=0;
	while(unit[z] > -1) {
		printf("%d ",unit[z++]);
	}
	printf("\n\n");
	printf("Raw code:\n");
	for(i=0;i<rawLength;i++) {
		printf("%d ",normalize(raw[i])*PULSE_LENGTH);
	}
	printf("\n");
	printf("Raw simplified:\n");
	printf("On:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",onCode[i]);
	}
	printf("\n");
	printf("Off:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",offCode[i]);
	}
	printf("\n");
	printf("All:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",allCode[i]);
	}
	printf("\n");
	printf("Unit 1:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit1Code[i]);
	}
	printf("\n");
	printf("Unit 2:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit2Code[i]);
	}
	printf("\n");
	printf("Unit 3:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit3Code[i]);
	}
	printf("\n");
	printf("Binary code:\n");
	printf("On:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",onBinary[i]);
	}
	printf("\n");
	printf("Off:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",offBinary[i]);
	}
	printf("\n");
	printf("All:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",allBinary[i]);
	}
	printf("\n");
	printf("Unit 1:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit1Binary[i]);
	}
	printf("\n");
	printf("Unit 2:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit2Binary[i]);
	}
	printf("\n");
	printf("Unit 3:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit3Binary[i]);
	}
	printf("\n");
	
	free(socket);
	free(progname);
	return (EXIT_SUCCESS);
}
Пример #5
0
int gpio433Receive(void) {
	return irq_read(gpio_433_in);
}