Exemplo n.º 1
0
void ANTPlus::begin(Stream &serial)
{
  mySerial = &serial;

  pinMode(SUSPEND_PIN, OUTPUT);
  pinMode(SLEEP_PIN,   OUTPUT);
  pinMode(RESET_PIN,   OUTPUT);
  pinMode(RTS_PIN,     INPUT);
  
  //Per datasheet
  digitalWrite(RESET_PIN,   HIGH);
  digitalWrite(SUSPEND_PIN, HIGH);
  digitalWrite(SLEEP_PIN,   LOW);
  
  //Interrupts are set in the main program currently
  //TODO: Look to see if they should be brought 'in lib'
  
  
  //This should not be strictly necessary - the device should always come up by itself....
  //But let's make sure we didn't miss the first RTS in a power-up race
  hardwareReset();
}
Exemplo n.º 2
0
int main(int argc, char **argv){

    SDL_Event event;                   //Event handler
    //init_apu();
    int QUIT = FALSE;
    
	if (argc < 2){
		printf("No Rom\n");
		return 1;
	}
	
	if (loadRom(argv[1])){
		return 1;
	}
    
    if (displayInit()){
        return 1;
    }
    
	
	hardwareReset();
    
    printf("[INFO] Version %s\n",version);
	printf("[INFO] System reset done\n");
    printf("[INFO] BUTTON A  = a\n");
    printf("[INFO] BUTTON B  = s\n");
    printf("[INFO] START     = q\n");
    printf("[INFO] SELECT    = w\n");
    printf("[INFO] DIRECTION = arrow keys\n");
    //display();
    
    int count = 0;
	
    while (!QUIT) {
    
        count++;    
        
        if (count > 100){                   /* Poll for events every 100 loops */  
            while (SDL_PollEvent(&event)) {
                
                inputHandleEvents(event);
                
                if( event.type == SDL_QUIT ) { 
                    QUIT = TRUE;
                }
        
                if (event.type == SDL_KEYDOWN){
                    if (event.key.keysym.sym == SDLK_ESCAPE){
                        QUIT = TRUE;
                    }
                    if (event.key.keysym.sym == SDLK_F1){
                        displayEnd();
                        return 1;
                    }
                    if (event.key.keysym.sym == SDLK_F9){
                        hardwareReset();
                    }
                }
                //inputHandleEvents(event);
                
            }
        count = 0;
        }       
              
		execute();
		//inputHandleEvents(event);
	}

    updateMBC2SRAM();   
    displayEnd();
	
return 0;
}
Exemplo n.º 3
0
int initDV3K(int fd, int hwReset)
{
	struct dv3k_packet responsePacket;
	char prodId[17];
	char version[49];
	
	struct dv3k_packet ctrlPacket = {
        .start_byte = DV3K_START_BYTE,
        .header.packet_type = DV3K_TYPE_CONTROL,
        .header.payload_length = htons(1),
        .payload.ctrl.field_id = DV3K_CONTROL_RESET
    };

	if(hwReset == 1) {
		if(hardwareReset() == 0)
			return 0;
	} else {
		if(write(fd, &ctrlPacket, dv3k_packet_size(ctrlPacket)) == -1) {
			fprintf(stderr, "AMBEserver: error writing reset packet: %s\n", strerror(errno));
			return 0;
		}
	}
	
	if(readSerialPacket(fd, &responsePacket) == 0) {
		fprintf(stderr, "AMBEserver: error receiving response to reset\n");
		return 0;
	}
	
	if(checkResponse(&responsePacket, DV3K_CONTROL_READY) == 0) {
	   fprintf(stderr, "AMBEserver: invalid response to reset\n");
	   return 0;
	}
	
	ctrlPacket.payload.ctrl.field_id = DV3K_CONTROL_PRODID;
	if(write(fd, &ctrlPacket, dv3k_packet_size(ctrlPacket)) == -1) {
		fprintf(stderr, "AMBEserver: error writing product id packet: %s\n", strerror(errno));
		return 0;
	}
	
	if(readSerialPacket(fd, &responsePacket) == 0) {
		fprintf(stderr, "AMBEserver: error receiving response to product id request\n");
		return 0;
	}
	
	if(checkResponse(&responsePacket, DV3K_CONTROL_PRODID) == 0) {
	   fprintf(stderr, "AMBEserver: invalid response to product id query\n");
	   dump("ProdID Response:", &responsePacket, sizeof(responsePacket));
	   return 0;
	}
	strncpy(prodId, responsePacket.payload.ctrl.data.prodid, sizeof(prodId));
	
	ctrlPacket.payload.ctrl.field_id = DV3K_CONTROL_VERSTRING;
	if(write(fd, &ctrlPacket, dv3k_packet_size(ctrlPacket)) == -1) {
		fprintf(stderr, "AMBEserver: error writing version packet: %s\n", strerror(errno));
		return 0;
	}
	
	if(readSerialPacket(fd, &responsePacket) == 0) {
		fprintf(stderr, "AMBEserver: error receiving response to version request\n");
		return 0;
	}
	
	if(checkResponse(&responsePacket, DV3K_CONTROL_VERSTRING) == 0) {
	   fprintf(stderr, "AMBEserver: invalid response to version query\n");
	   return 0;
	}
	strncpy(version, responsePacket.payload.ctrl.data.version, sizeof(version));
	
	fprintf(stdout, "AMBEserver: Initialized %s version %s.\n", prodId, version); 

	return 1;
}

int openSerial(char *ttyname, long baud)
{
	struct termios tty;
	int fd;
    ssize_t bytesWritten;
	
	fd = open(ttyname, O_RDWR | O_NOCTTY | O_SYNC);
	if (fd < 0) {
		fprintf(stderr, "AMBEserver: error when opening %s: %s\n", ttyname, strerror(errno));
		return fd;
	}

	if (tcgetattr(fd, &tty) != 0) {
		fprintf(stderr, "AMBEserver tcgetattr: %s\n", strerror(errno));
		return -1;
	}
	
	if(debug)
		fprintf(stderr, "Baud %ld\n", baud);
		
	//  Input speed = output speed
	cfsetispeed(&tty, B0);
	
	switch(baud) {
		case 230400:
			cfsetospeed(&tty, B230400);
			break;
		case 460800:
			cfsetospeed(&tty, B460800);
			break;
		default:
			fprintf(stderr, "AMBEserver: unsupported baud rate %ld\n", baud);
			return -1;
	}
	
	tty.c_lflag    &= ~(ECHO | ECHOE | ICANON | IEXTEN | ISIG);
	tty.c_iflag    &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
	tty.c_cflag    &= ~(CSIZE | CSTOPB | PARENB);
	tty.c_cflag    |= CS8 | CRTSCTS;
	tty.c_oflag    &= ~(OPOST);
	tty.c_cc[VMIN] = 0;
	tty.c_cc[VTIME] = 1;
	
	if (tcsetattr(fd, TCSANOW, &tty) != 0) {
		fprintf(stderr, "AMBEserver: tcsetattr: %s\n", strerror(errno));
		return -1;
	}

	if (debug)
		fprintf(stdout, "opened %s %ld\n", ttyname, baud);

	return fd; 
}

int openSocket(in_port_t port)
{
	struct sockaddr_in sa = { 0 };
	int fd;

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		fprintf(stderr, "AMBEserver: error when creating the socket: %s\n", strerror(errno));
		return fd;
	}

	sa.sin_family = AF_INET;
	sa.sin_port = htons(port);
	sa.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
		fprintf(stderr, "AMBEserver: error when binding the socket to port %u: %s\n", port, strerror(errno));
		return -1;
	}

	if (debug)
		fprintf(stdout, "opened the UDP socket on port %u\n", port);

	return fd;
}

int processSerial(int sockFd, int serialFd)
{
	struct dv3k_packet packet;

	if(readSerialPacket(serialFd, &packet) == 0) {
		fprintf(stderr, "AMBEserver: processSerial failed to read packet\n");
		return 0;
	}

	if (debug)
		dump("Serial data", &packet, dv3k_packet_size(packet));

	if (sa.sin_port != 0U)
		if (sendto(sockFd, &packet, dv3k_packet_size(packet), 0, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) != dv3k_packet_size(packet)) {
			fprintf(stderr, "AMBEserver: error when writing to the socket: %s\n", strerror(errno));
			return 0;
	}

	return 1;
}

int processSocket(int sockFd, int serialFd)
{
	socklen_t addrLen = sizeof(struct sockaddr_in);
	ssize_t bytesRead;
	struct dv3k_packet packet;
	
	bytesRead = recvfrom(sockFd, &packet, sizeof(packet), 0, (struct sockaddr *) &sa, &addrLen);
	if (bytesRead < 0) {
		fprintf(stderr, "AMBEserver: error when reading from the socket: %s\n", strerror(errno));
		return 0;
	}

	if (debug)
		dump("Socket data", &packet, bytesRead);

	if (packet.start_byte != DV3K_START_BYTE) {
		fprintf(stderr, "AMBEserver: invalid start byte when reading from the socket, 0x%02X", packet.start_byte);
		return 1;
	}

	if (bytesRead != dv3k_packet_size(packet)) {
		fprintf(stderr, "AMBEserver: invalid length when reading from the socket, %zd=%zd", bytesRead, dv3k_packet_size(packet));
		return 1;
	}

	if (write(serialFd, &packet, dv3k_packet_size(packet)) != dv3k_packet_size(packet)) {
		fprintf(stderr, "AMBEserver: error when writing to the serial port: %s\n", strerror(errno));
		return 0;
	}

	return 1;
}