void init_rs232() { //printf("UART Initialization\n"); uart = alt_up_rs232_open_dev("/dev/rs232"); clear_fifo(); }
/* * Initialization of Serail communication */ struct RS232 initRS232() { struct RS232 com_local; com_local.receivePackets = initQueue(); com_local.sendPackets = initQueue(); com_local.pendingPacketSize = initQueue(); com_local.client_ack = 0; com_local.host_ack = 0; com_local.isRdySend = 0; com_local.failReceive = 0; com_local.stateMachine = (enum States*)malloc(sizeof(enum States)); com_local.pastState = (enum States*)malloc(sizeof(enum States)); *com_local.stateMachine = startInit; *com_local.pastState = startInit; com_local.num_packets = com_local.index_packets = 0; com_local.num_send_packets = com_local.index_send_packets = 0; com_local.packetBuf = NULL; printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev(RS232_0_NAME); up_dev.RS232_dev = uart; printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &com.data[0], &com.parity); } alt_alarm_start(&alarm, alt_ticks_per_second(), RS232_ISR, (void*)&up_dev); printf("UART Initialization finished\n"); return com_local; }
/** * Creates a new motor handler. * @throw UARTOpenException if the handler cannot open the UART device used to talk to the motors */ MotorHandler::MotorHandler() { // Enable the motor controller. IOWR_ALTERA_AVALON_PIO_DATA(PIO_MOTOR_RESET_N_BASE, MOTOR_ENABLE); // Open the serial device to send commands to the motor controller. motor_dev = alt_up_rs232_open_dev(UART_MOTOR_NAME); if (motor_dev == NULL) { throw new UARTOpenException(); } }
alt_up_rs232_dev* init_clear_uart(Comm_data* cd) { printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev("/dev/rs232"); printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &(cd->data), &(cd->parity)); } return uart; }
/* UART task: read and write */ void taskRS232(void* pdata) { alt_u32 write_FIFO_space; alt_u16 read_FIFO_used; alt_u8 data_W8; alt_u8 data_R8; int enter = 0; unsigned p_error; alt_up_rs232_dev* rs232_dev; // open the RS232 UART port rs232_dev = alt_up_rs232_open_dev("/dev/rs232_0"); if (rs232_dev == NULL) alt_printf("Error: could not open RS232 UART\n"); else alt_printf("Opened RS232 UART device\n"); alt_up_rs232_enable_read_interrupt(rs232_dev); while (1) { int sw = OSQPend(SWQ, 0, &err); if (sw == SW_WRITE) { alt_up_rs232_disable_read_interrupt(rs232_dev); if (enter == 0) { data_W8 = 'A'; enter = 1; } else if (enter == 1) { data_W8 = '\n'; enter = 0; } write_FIFO_space = alt_up_rs232_get_available_space_in_write_FIFO( rs232_dev); if (write_FIFO_space >= WRITE_FIFO_EMPTY) { alt_up_rs232_write_data(rs232_dev, data_W8); alt_printf("write %c to RS232 UART\n", data_W8); } OSTimeDlyHMSM(0, 0, 1, 0); alt_up_rs232_enable_read_interrupt(rs232_dev); } if (sw == SW_READ) { read_FIFO_used = alt_up_rs232_get_used_space_in_read_FIFO( rs232_dev); if (read_FIFO_used > READ_FIFO_EMPTY) { alt_printf("char stored in read_FIFO: %x\n", read_FIFO_used); alt_up_rs232_read_data(rs232_dev, &data_R8, &p_error); alt_printf("read %x from RS232 UART\n", data_R8); } OSTimeDlyHMSM(0, 0, 0, 5); } } }
int main() { int i; //1 for receiving file from middleman, 2 for sending to middleman int mode; //Num char in filename array int numFileName; //Num bytes in file int numBytesFile; //Num characters in file array int numFile; char* listName; //File to send or receive (likely a piece of it) unsigned char fileName[MAX_FILENAME]; //variable to hold data received from uart unsigned char data; //parity bit for reading (not using parity atm, but still need the bit) unsigned char parity; short int handle; //handle for the file to create/access int connected = 0; //Variable to keep track of whether the SD CARD is connected or not. while (1) { alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev("/dev/rs232_0"); if(!alt_up_sd_card_is_FAT16()){ printf("SD CARD is not FAT16 Format\n"); } if (device_reference != NULL) { while (alt_up_sd_card_is_Present()) { printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &data, &parity); } // Now receive the instruction from the Middleman printf("Waiting for instruction to come from the Middleman\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; // First byte is the mode, 1 for receiving file from middleman, 2 for sending to middleman alt_up_rs232_read_data(uart, &data, &parity); mode = (int) data; //mode -= 48; printf("Mode:%d\n", mode); //Receive file from middleman and save to SD if (mode == 1) { printf("Waiting for num char:\n"); // The second byte is the number of characters in the file name while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); numFileName = (int) data; //numFileName -= 48; //Now receive the file name printf("About to receive %d characters:\n\n", numFileName); printf("Filename received:"); for (i = 0; i < numFileName; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart)== 0); alt_up_rs232_read_data(uart, &data, &parity); fileName[i] = data; printf("%c", data); } printf("\n"); fileName[i] = '.'; fileName[i+1] = 't'; fileName[i+2] = 'x'; fileName[i+3] = 't'; fileName[i+4]= '\0'; // // TODO: // USE THAT FILENAME TO MAKE A NEW FILE ON SD CARD HERE handle = alt_up_sd_card_fopen(fileName, 1); if(handle < 0){ //TODO: File can't be opened, do something about it printf("send had a neg handle \n\n"); } else{ // The 4 bytes after filename is the number of bytes in the file // // SHIFT BYTES LEFT AND CONCATENATE TO ACCOUNT FOR SEVERAL BYTES WORTH OF FILE // WRITE FIRST 4 BYTES OF FILE AS SIZE OF FILE IN BYTES while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile = (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; //WRITE BYTES TO SD CARD printf("About to receive %d file chars:\n\n",numBytesFile); for (i = 0; i < numBytesFile; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart)== 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); } printf("File done\n"); alt_up_sd_card_fclose(handle); } //This bracket ends receiving a file } //Send file to middleman from SD else if (mode == 2) { printf("Waiting for num char:\n"); // The second byte is the number of characters in the file name while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; alt_up_rs232_read_data(uart, &data, &parity); numFileName = (int) data; //numFileName -= 48; //Now receive the file name printf("About to receive %d characters:\n\n", numFileName); printf("Filename received:"); for (i = 0; i < numFileName; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; alt_up_rs232_read_data(uart, &data, &parity); fileName[i] = data; printf("%c", data); } printf("\n"); fileName[i] = '.'; fileName[i+1] = 't'; fileName[i+2] = 'x'; fileName[i+3] = 't'; fileName[i+4]= '\0'; handle = alt_up_sd_card_fopen(fileName, 0); if (handle == -1) { //SEND ANDROID AN ASCII 2 TO LET THEM KNOW TO RECEIVE FILE NAME alt_up_rs232_write_data(uart, 50); printf("neg handle"); if(alt_up_sd_card_find_first(".",listName) ==-1){ //SEND ANDROID AN ASCII 2 TO LET THEM KNOW THERES NO FILE NAMES alt_up_rs232_write_data(uart, 50); printf("no files"); } else{ //SEND ANDROID LIST OF FILE NAMES printf("some files"); i=0; for(i = 0; listName[i] != '.'; i++){ alt_up_rs232_write_data(uart, listName[i]); } alt_up_rs232_write_data(uart, 32); while(alt_up_sd_card_find_next(listName)!=-1){ i=0; for(i = 0; listName[i] != '.'; i++){ alt_up_rs232_write_data(uart, listName[i]); } alt_up_rs232_write_data(uart, 32); } alt_up_rs232_write_data(uart, 1); printf("done files"); } } else { //SEND ANDROID AN ASCII 1 TO LET THEM KNOW TO RECEIVE A FILE alt_up_rs232_write_data(uart, 49); // SHIFT BYTES LEFT AND CONCATENATE TO ACCOUNT FOR SEVERAL BYTES WORTH OF FILE // WRITE FIRST 4 BYTES OF FILE AS SIZE OF FILE IN BYTES data = alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile = (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; printf("About to send %d file bytes\n", numFile); while (numFile > 0) { while(alt_up_rs232_get_available_space_in_write_FIFO(uart) == 0); data = alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile--; } // WRITE A "FILE DONE" STRING OR WHATEVER WE DECIDE printf("sending end bits\n"); // alt_up_rs232_write_data(uart, 1); // // // alt_up_sd_card_fclose(handle); } //This bracket ends sending a file } //Something broke else { printf("Wrong mode, something broke, starting over\n"); } } } else { printf("Card Reader is not working\n"); } } return 0; //end main }
void initUART(){ uart = alt_up_rs232_open_dev(RS232_0_NAME); }