예제 #1
0
void main()
{
	/* configures the function of the A/D port pins
	 * (ports A and E to digital I/Os) */
	ADCON1=7;		

	TRISD=0x0f;	/* 00001111 -> RD4:RD7 as outputs */
	TRISE=3;	/* 111 -> RE0:RE2 outputs */
	PORTE=4;
	PORTD=0;
	
	/* set the activity LED bit as an output */
	TRISC&=0xf7;
	/* make sure LED is off	*/
	LEDPORT|=0x08;
	/* then flash it 3 times */
	LED_flash(3);
	/* initialise USART interrupts */
	SerIntInit();

	/* enter main program loop */
	while(1)
	{
		/* There's a packet waiting in the buffer.. IP layer
		 * flag is set so we can return up the stack, rather than processing
		 * the packet within the interrupt routine */
		if (rxstate == RXDONE)
		{
			/* call on the IP layer to process awaiting packet */
			ip_receive();
			/* serial line IDLE again */
			rxstate=RXIDLE;
			/* packet has been sent */
			txpos=0;
		}

		/* Interrrupt routine has been trggered since last here, 
		 * there is at least one byte waiting in the USART receive
		 * buffer - call on the SLIP layer to process and add to
		 * stack buffer */
		if (GetRxSize()) slip_recv();
		
		/* ensure activity LED is off */
		LEDPORT|=0x08;
		
		/* TCP counter, used for Initial Sequence Numbers */
		if (tcpcounter == 2147483646) {
			tcpcounter=0;
		}
		tcpcounter++;
	}
}
예제 #2
0
int main(void)
{
	serial_init(E_BAUD_38400);	
	serial_install_interrupts(E_FLAGS_SERIAL_RX_INTERRUPT);
	serial_flush();

	beeper_init(PIN);

	unsigned char size = 0;
	struct packet pack;

	while (1) {

		if (0 >= (size = slip_recv((void *)&pack, sizeof(pack)))) {
			continue;
		}

		// ignore incomplete data chunks
		if (size < 5) {
			continue;
		}

		// verify if the information is genuine
		if (!slip_verify_crc16((unsigned char *)&pack, size, 0)) {
			continue;
		}

		for (unsigned char i = 0; i < pack.num; i++) {
			play(PIN, &pack.notes[i]);
			serial_poll_send(&pack.notes[i].note, 2);
			serial_poll_send(&pack.notes[i].duration, 2);
		}
	}

	return 0;
}
예제 #3
0
int main(void)
{
	serial_init(E_BAUD_4800);	
	serial_install_interrupts(E_FLAGS_SERIAL_RX_INTERRUPT);
	serial_flush();

	while(1) {
		char str[32] = {0x00};
		memset(str, 0x00, sizeof(str));

#if TEST_TYPE == TEST_SIMPLE
#warning Building Test Simple

		strcpy(buffer, "123456789");
	   	snprintf(str, sizeof(str), "CRC: %04x\n", 
				checkcrc((unsigned char *)buffer, strlen(buffer)));
		serial_poll_send(str, strlen(str));

#elif TEST_TYPE == TEST_RECEIVE
#warning Building Test Receive

		unsigned char size = 0x00;
		uint16_t crc = 0x0000;

		if (0 >= (size = slip_recv((unsigned char *)buffer, 128)))
			continue;

		memcpy(&crc, &buffer[size - 2], 2);
		memset(&buffer[size - 2], 0x00, 2);

	   	snprintf(str, sizeof(str), "RECV [%04x], CRC: %04x\r\n", 
				crc,
				checkcrc((unsigned char *)buffer, size));
		serial_poll_send(str, strlen(str));

#elif TEST_TYPE == TEST_VERIFY
#warning Building Test Verify

		unsigned char size = 0x00;

		if (0 >= (size = slip_recv((unsigned char *)buffer, 128)))
			continue;

	   	snprintf(str, sizeof(str), "Verification: [%s]\r\n", 
				slip_verify_crc16(buffer, size, size - 2) ?
				"positive" : "negative");

		serial_poll_send(str, strlen(str));

#elif TEST_TYPE == TEST_SEND
#warning Building Test Send

		strcpy(buffer, "123456789");
		slip_append_crc16((unsigned char *)buffer, strlen(buffer));
		serial_poll_send(buffer, 11);
		serial_poll_send("\n",1);

#else
#error Uknown test type
#endif
		_delay_ms(200);
	}

	return 0;
}
예제 #4
0
int main(int argc, char const *argv[]) {
	
	g_fd = 0x00;
	struct response *resp;
	struct timer_data *data;
	uint8_t buff[8] = {0x00};
	uint16_t crc = 0x00;

	if (argc <= 3) {
		fprintf(stderr, "host_slip <prescaler> <ocr> <st_max>\n");
		return -1;
	}

	if (0 > (g_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_SYNC))) {
		fprintf(stderr, "Unable to open device [%s]\n", SERIAL_PORT);
		return 0;
	}

	tty_attrib_conf(g_fd, BAUD_9600, 0);
	tty_block_conf(g_fd, 1);

	data = (struct timer_data *)buff;

	data->crc16 = 0x0000;
	data->prescaler = atoi(argv[1]);
	data->ocr = atoi(argv[2]);
	data->st_max = atoi(argv[3]);

	// insert the CRC
	crc = crc16(0x00, buff, sizeof(struct timer_data));
	data->crc16 = crc;

	// wait for the board to reset itself
	sleep(4);

	printf("Sending: %d/%d/%d, CRC: 0x%04x\n", 
			data->prescaler,
			data->ocr,
			data->st_max,
			data->crc16);

	slip_send(buff, sizeof(struct timer_data));
	slip_recv(buff, sizeof(buff));

	resp = (struct response *)buff;

	// zero the CRC to perform verification
	buff[6] = buff[0];
	buff[7] = buff[1];
	buff[0] = 0x00;
	buff[1] = 0x00;

	// we've got the calculated CRC now
	crc = crc16(0x00, buff, sizeof(struct response));

	// restore the CRC
	buff[0] = buff[6];
	buff[1] = buff[7];

	printf("CRC/CRC_CALCD/STATUS: 0x%04x/0x%04x/%s\n", 
			resp->crc16, 
			crc,
			(resp->status == 0x10 ? "ACK" : "NACK"));

	close(g_fd);

	return 0;
}