void SerialPort::irq_handler() { debug(A_SERIALPORT, "irq_handler: Entered SerialPort IRQ handler"); uint8 int_id_reg = read_UART( SC::IIR ); if( int_id_reg & 0x01 ) return; // it is not my IRQ or IRQ is handled uint8 int_id = (int_id_reg & 0x06) >> 1; switch( int_id ) { case 0: // Modem status changed break; case 1: // Output buffer is empty WriteLock = 0; break; case 2: // Data is available int_id = read_UART( 0 ); _in_buffer->put( int_id ); break; case 3: // Line status changed break; default: // This will never be executed break; } return; }
void UART2_task(void){ /*Performs a set task depenedent upon command received over UART from the dsPIC on the primary board. These tasks include turning on/off the lower magnet, reading the motor/lower magnet encoder, reading the IMU on the secondary board, etc.*/ unsigned char task; if(uart_buffer.len>0){ task = read_UART(); LED2 = !LED2; if(task == LOWMAG_ON){ LOWMAG = 1; //Turn on low magnet } else if(task == LOWMAG_OFF){ LOWMAG = 0; //Turn off low magnet } else if(task == LOWMAG_TOGGLE){ LOWMAG = !LOWMAG; //Toggle low magnet write_UART2('w'); //Simple test to ensure UART communication between boards is working } else if(task == LOWMAGENC_READ){ //Reads the encoder value of the lower magnet long value; unsigned short i; unsigned char temp[4]; value = read_LOWMAGENC(); temp[0] = value; temp[1] = value>>8; temp[2] = value>>16; temp[3] = value>>24; for (i=0; i<4; i++){ write_UART2(temp[i]); } //write_string_UART2(temp,4); }
void read_Gyro_Secondary(unsigned char *data){ unsigned short i; write_UART2('a'); while(!(uart_buffer.len>5)); for (i=0;i<6;i++){ data[i] = read_UART(); } }
int main(void) { initialize(); //initialize_ADC_Offset(); //initialize_UART(); //TOPMAG=1; unsigned char c=0; motoron = 1; direction = CW; kick(); write_duty(512); //write_duty(0); //timer1_on(); printf("hello world!\n"); while (1) { c = read_UART(); if (c=='o'){ TOPMAG=1; } else if (c=='f'){ TOPMAG=0; } else if(c == 'g') { //Turn on Bottom Magnet write_UART2('1'); } else if(c =='h') { //Turn off Bottom Magnet write_UART2('2'); } else if (c=='u'){ // inc duty // for now, increase the duty by 10% of max duty write_duty(read_duty()+100); c = 'x'; } else if (c=='d'){ // dec duty write_duty(read_duty()+100); c = 'x'; } else if (c=='m'){ // motor off motoron = 0; } else if (c=='x'){ // make sure duty is only updated once per call continue; } } return 0; }
void read_string_UART(unsigned char *data, int n){ int i = 0,j = 0; while(i < n && j != 1){ data[i] = read_UART(); if(data[i] == '\n'){ j = 1; } else{ j = 0; } i++; } data[i] = '\n'; }
int32 SerialPort::writeData(int32 offset, int32 num_bytes, const char*buffer) { if( offset != 0 ) return -1; uint32 jiffies = 0, bytes_written = 0; while( ArchThreads::testSetLock( SerialLock ,1 ) && jiffies++ < 50000 ); if( jiffies == 50000 ) { WriteLock = 0; return -1; } WriteLock = bytes_written = 0; while( num_bytes -- ) { jiffies = 0; while( !(read_UART( SC::LSR ) & 0x40) && jiffies++ < 50000 ); if( jiffies == 50000 ) // TIMEOUT { SerialLock = 0; WriteLock = 0; return -1; } write_UART( 0, *(buffer++) ); bytes_written++; } SerialLock = 0; return bytes_written; };
void read_MPU_test_secondary(unsigned char *data){ write_UART2('c'); while(!(uart_buffer.len>0)); data[0] = read_UART(); printf("%x\n",data[0]); }