Captain's Universe Home
Captain's Universe Home
Cosmic Ray Muon DetectorTeleGarden Pages
Time on MarsBryophyllum Plants
Jupiter Radio AstronomyAncient Pages
Salzburg Tourist GuideEarth Magnetometer
  H O M E     AJAX & MORE     LINUX & MORE     RTAI     XENOMAI     ADEOS IPIPE      
    JAVA & BROWSERS     *NIX     ELECTRONICS     REVIEWS     ARTEMIA     FAIRY SHRIMP      



PIC - MMC (MultiMediaCard) Flash Memory Extension - Linux Sources

Download tar.gz's at the PIC - MMC main page

ser.c
/* ser.c
	(C) 2004 Captain http://www.captain.at
	
	Sends 3 characters (ABC) via the serial port (/dev/ttyS0) and reads
	them back if they are returned from the PIC.
	
	Used for testing the PIC-MMC test-board
	http://www.captain.at/electronics/
*/
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

#include "captain/capser.h"

int fd;

int initport(int fd) {
	struct termios options;
	// Get the current options for the port...
	tcgetattr(fd, &options);
	// Set the baud rates to 19200...
	cfsetispeed(&options, B9600);
	cfsetospeed(&options, B9600);
	// Enable the receiver and set local mode...
	options.c_cflag |= (CLOCAL | CREAD);

	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;

	// Set the new options for the port...
	tcsetattr(fd, TCSANOW, &options);
	return 1;
}

int main(int argc, char **argv) {

	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1) {
		perror("open_port: Unable to open /dev/ttyS0 - ");
		return 1;
	} else {
		fcntl(fd, F_SETFL, 0);
	}
	
	printf("baud=%d\n", getbaud(fd));
	initport(fd);
	printf("baud=%d\n", getbaud(fd));

	char sCmd[254];
	sCmd[0] = 0x41;
	sCmd[1] = 0x42;
	sCmd[2] = 0x43;
	sCmd[3] = 0x00;

	if (!writeport(fd, sCmd)) {
		printf("write failed\n");
		close(fd);
		return 1;
	}

	printf("written:%s\n", sCmd);
	
	usleep(500000);
	char sResult[254];
	fcntl(fd, F_SETFL, FNDELAY); // don't block serial read

	if (!readport(fd,sResult)) {
		printf("read failed\n");
		close(fd);
		return 1;
	}
	printf("readport=%s\n", sResult);
	close(fd);
	return 0;
}
Makefile for ser.c:
CFLAGS=-g -Wall 
ser: captain/capser.c ser.c
	gcc -g -c -Wall captain/capser.c -o capser.o
	gcc -g -c -Wall ser.c -o ser.o
	gcc ser.o capser.o -o ser
captain/capser.c for ser.c:
/* capser.c
	(C) 2004 Captain http://www.captain.at
	
	Helper functions for "ser"
	
	Used for testing the PIC-MMC test-board
	http://www.captain.at/electronics/
*/
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int writeport(int fd, char *chars) {
	int len = strlen(chars);
	chars[len] = 0x0d; // stick a <CR> after the command
	chars[len+1] = 0x00; // terminate the string properly
	int n = write(fd, chars, strlen(chars));
	if (n < 0) {
		fputs("write failed!\n", stderr);
		return 0;
	}
	return 1;
}

int readport(int fd, char *result) {
	int iIn = read(fd, result, 254);
	int len = strlen(result);
	result[len-1] = 0x00;
	if (iIn < 0) {
		if (errno == EAGAIN) {
			printf("SERIAL EAGAIN ERROR\n");
			return 0;
		} else {
			printf("SERIAL read error %d %s\n", errno, strerror(errno));
			return 0;
		}
	}                    
	return 1;
}

int getbaud(int fd) {
	struct termios termAttr;
	int inputSpeed = -1;
	speed_t baudRate;
	tcgetattr(fd, &termAttr);
	/* Get the input speed.                              */
	baudRate = cfgetispeed(&termAttr);
	switch (baudRate) {
		case B0:      inputSpeed = 0; break;
		case B50:     inputSpeed = 50; break;
		case B110:    inputSpeed = 110; break;
		case B134:    inputSpeed = 134; break;
		case B150:    inputSpeed = 150; break;
		case B200:    inputSpeed = 200; break;
		case B300:    inputSpeed = 300; break;
		case B600:    inputSpeed = 600; break;
		case B1200:   inputSpeed = 1200; break;
		case B1800:   inputSpeed = 1800; break;
		case B2400:   inputSpeed = 2400; break;
		case B4800:   inputSpeed = 4800; break;
		case B9600:   inputSpeed = 9600; break;
		case B19200:  inputSpeed = 19200; break;
		case B38400:  inputSpeed = 38400; break;
	}
	return inputSpeed;
}
captain/capser.h for captain/capser.c:
int writeport(int fd, char *chars);
int readport(int fd, char *result);
int getbaud(int fd);


serterm.c
/* serterm.c V2
	(C) 2004 Captain http://www.captain.at
	
	Reads 512bytes from the serial port (/dev/ttyS0) and then exits
	
	Used for testing the PIC-MMC test-board
	http://www.captain.at/electronics/

*/
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int fd;
FILE* serport;

int initport(int fd) {
	struct termios options;
	// Get the current options for the port...
	tcgetattr(fd, &options);
	// Set the baud rates to 9600...
	cfsetispeed(&options, B9600);
	cfsetospeed(&options, B9600);
	// Enable the receiver and set local mode...
	options.c_cflag |= (CLOCAL | CREAD | CRTSCTS);

	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;

	options.c_iflag = IGNPAR | ICRNL;
	options.c_oflag = 0;
	options.c_lflag = ICANON;

	tcflush (fd, TCIFLUSH);
	// Set the new options for the port...
	tcsetattr(fd, TCSANOW, &options);

	return 1;
}

int getbaud(int fd) {
	struct termios termAttr;
	int inputSpeed = -1;
	speed_t baudRate;
	tcgetattr(fd, &termAttr);
	/* Get the input speed.                              */
	baudRate = cfgetispeed(&termAttr);
	switch (baudRate) {
		case B0:      inputSpeed = 0; break;
		case B50:     inputSpeed = 50; break;
		case B110:    inputSpeed = 110; break;
		case B134:    inputSpeed = 134; break;
		case B150:    inputSpeed = 150; break;
		case B200:    inputSpeed = 200; break;
		case B300:    inputSpeed = 300; break;
		case B600:    inputSpeed = 600; break;
		case B1200:   inputSpeed = 1200; break;
		case B1800:   inputSpeed = 1800; break;
		case B2400:   inputSpeed = 2400; break;
		case B4800:   inputSpeed = 4800; break;
		case B9600:   inputSpeed = 9600; break;
		case B19200:  inputSpeed = 19200; break;
		case B38400:  inputSpeed = 38400; break;
	}
	return inputSpeed;
}

int main(int argc, char **argv) {
	fd_set rfds;
	char sResult;
	int end = 1;
	int cnt = 0;
	char line[1024];
	int retval = 0;
	
	if ((serport = fopen("/dev/ttyS1", "rb")) == 0) {
		char *err;
		perror(err);
		fprintf(stderr, "error: %s\n", err );
		exit(1);
	} 	
	
	fd = fileno (serport);
	fcntl(fd, F_SETFL, 0);

	printf("baud=%d\n", getbaud(fd));
	initport(fd);
	printf("baud=%d\n", getbaud(fd));
	printf("listening...\n");

	FD_ZERO (&rfds);
	FD_SET (fd, &rfds);
	
	while(end) {
		FD_ZERO (&rfds);
		FD_SET (fd, &rfds);
		retval = select (fd + 1, &rfds, NULL, NULL, NULL);
		if (retval) {
			fgets (line, 1024, serport);
			printf ("%s\n", line);
		}

	}
	fclose(serport);
	printf("\nexiting...\n");
	return 0;
}
Makefile for serterm.c:
CFLAGS=-g -Wall 
serterm:
	gcc serterm.c -o serterm


Download tar.gz's at the PIC - MMC main page

Last-Modified: Thu, 02 Dec 2004 18:55:48 GMT

Google
 
Web www.captain.at
go to top
© 1996-2010 . All rights reserved.
No reproduction, distribution, publishing or transmission of the copyrighted materials at this site is permitted. Policy
go to top