static int mphi_pumprx(mphi_driver_t* mphidrv) { mphi_slot_t slot; MPHI_HANDLE_T* mphi; int ret; int8_t ctrl; assert(mphidrv); // check if message is available for read ctrl = vchost_msgavail(); if(ctrl < 0) return -1; // get correct driver state assert(ctrl < 2); mphi = ctrl ? (mphidrv->mphi + MPHI_CHANNEL_IN_CONTROL) : (mphidrv->mphi + MPHI_CHANNEL_IN_DATA); assert(mphi); // get next available slot ret = del_slotin(mphi, &slot); if(ret != 0) { VC_DEBUG(MPHI, "error: no empty slot, ch=%d\n", mphi->channel); ++mphi->msgcnt_skipped; return -1; } // read the message lock_bus(); memset((void*)slot.addr, 0, slot.len); slot.len = vchost_readmsg((void*)slot.addr, slot.len, &(slot.ctrl)); unlock_bus(); // sanity check assert(ctrl == slot.ctrl); if(slot.len <= 0) { assert(0); // message should've been available return -1; } // update message stats ++mphi->msgcnt; mphi->bytecnt += slot.len; add_slotout(mphi, &slot); mphi_dumpmsg(mphidrv, "RX", &slot); if(mphi->event_callback) { // signal above that something happened mphi->event_callback(mphi->event_callback_data); } return 0; }
/* * Wait until current output operation completes. * Optionally purge queued output. * XXX * XXX May want to purge input slots also? * XXX */ static void mphi_finish_io(int purge) { if (purge) { // Purge all slots queued for output. mphi_slot_t slot; MPHI_HANDLE_T* mphi = (MPHI_HANDLE_T*)(mphidrv.mphi + MPHI_CHANNEL_OUT); while(0 == del_slotin(mphi, &slot)); } // Wait for any pending output to finish. lock_bus(); unlock_bus(); }
static int mphi_pumptx(mphi_driver_t* mphidrv) { MPHI_HANDLE_T* mphi; mphi_slot_t slot; int ret; int8_t msgType; mphi = (MPHI_HANDLE_T*)(mphidrv->mphi + MPHI_CHANNEL_OUT); ret = del_slotin(mphi, &slot); if(ret != 0) { // no free slots return -1; } mphi_dumpmsg(mphidrv, "TX", &slot); msgType = !slot.ctrl; if (( slot.flags & MPHI_FLAGS_TERMINATE_DMA ) != 0 ) { msgType |= VC_HOSTPORT_PARAM_TERMINATE_DMA; } // transmit lock_bus(); vchost_writemsg((void*)slot.addr, slot.len, msgType); vchost_writeparam(0); unlock_bus(); ++mphi->msgcnt; mphi->bytecnt += slot.len; // put it to "out" fifo add_slotout(mphi, &slot); if(mphi->event_callback) { // signal above that something happened mphi->event_callback(mphi->event_callback_data); } return 0; }
int main(void) { u16 i; u08 data[2]; int j=0; init(); test_motor(); clear_screen(); _delay_ms(100); data[0] = 0x1; //change to WAKE mode send_address(0x2A,0); write_register(&data[0], 1); _delay_ms(100); unlock_bus(); //send_address(0x2); //read_register(&data[1], 1); print_num(data[0]); //while(1) {} while(1) { //X send_address(0x1,1); read_register(&(data[0]), 1); print_num(data[0]); print_string(" ",1); //Y send_address(0x3,1); read_register(&(data[0]), 1); print_num(data[0]); print_string(" ", 1); //Z lcd_cursor(0,1); send_address(0x5,1); read_register(&(data[0]), 1); print_num(data[0]); print_string(" ",1); print_num(count); _delay_ms(50); clear_screen(); OCR2A = 28; } /* USI_TWI_Master_Initialize(); i2cMessageBuf[0] = 0x1c << 1; // Odd numbers for read i2cMessageBuf[1] = 0x0d; // Register 0 contains version number i2cMessageBuf[2] = (0x1c << 1) + 1; // Odd numbers for read i2cMessageBuf[3] = 0x00; // Register 0 contains version number USI_TWI_Start_Read_Write( i2cMessageBuf, 1 ); */ while(get_sw1() == 0) {} DDRD |= _BV(IR_PIN); led_on(1); led_on(0); clear_screen(); while(1) { _delay_ms(1500); if (j) { clear_screen(); print_string("test",4); led_on(1); led_off(0); //sbi(PORTC,LCD_E_PIN); //sbi(PORTC,LCD_RS_PIN); //PORTA = 0xff; sbi(PORTD,IR_PIN); //turn on IR pin sbi(PORTC,SERVO0_PIN); //set servo pins sbi(PORTC,SERVO1_PIN); sbi(PORTC,SERVO2_PIN); sbi(PORTF,SERVO3_PIN); //test i2c pins //sbi(PORTE,4); //sbi(PORTE,5); //motor test sbi(PORTB,MOTOR0_EN_PIN); sbi(PORTB,MOTOR1_EN_PIN); sbi(PORTD,MOTOR0_DIR0_PIN); sbi(PORTD,MOTOR0_DIR1_PIN); sbi(PORTD,MOTOR1_DIR0_PIN); sbi(PORTD,MOTOR1_DIR1_PIN); j=0; } else { led_off(1); led_on(0); clear_screen(); print_string("program",7); j=1; //cbi(PORTC,LCD_E_PIN); //cbi(PORTC,LCD_RS_PIN); //PORTA = 0; cbi(PORTD,IR_PIN); //turn off IR pin cbi(PORTC,SERVO0_PIN); //clear servo pins cbi(PORTC,SERVO1_PIN); cbi(PORTC,SERVO2_PIN); cbi(PORTF,SERVO3_PIN); //test i2c pins //cbi(PORTE,4); //cbi(PORTE,5); //motor test cbi(PORTB,MOTOR0_EN_PIN); cbi(PORTB,MOTOR1_EN_PIN); cbi(PORTD,MOTOR0_DIR0_PIN); cbi(PORTD,MOTOR0_DIR1_PIN); cbi(PORTD,MOTOR1_DIR0_PIN); cbi(PORTD,MOTOR1_DIR1_PIN); } } return 0; }
int main(void) { u16 i; u08 data[2]; int j=0; init(); test_motor(); clear_screen(); /* while(1){ set_position(0,0); set_position(1,0); set_position(2,0); set_position(3,0); _delay_ms(1000); set_position(0,255); set_position(1,255); set_position(2,255); set_position(3,255); _delay_ms(1000); } */ _delay_ms(100); for (i=0;i<1;i++) { led_on(1); _delay_ms(100); led_off(1); _delay_ms(100); } data[0] = 0x1; //change to WAKE mode send_address(0x2A,0); write_register(&data[0], 1); _delay_ms(100); unlock_bus(); //send_address(0x2); //read_register(&data[1], 1); print_num(data[0]); //while(1) {} while(1) { //X send_address(0x1,1); read_register(&(data[0]), 1); print_num(data[0]); print_string(" "); //Y send_address(0x3,1); read_register(&(data[0]), 1); print_num(data[0]); print_string(" "); //Z lcd_cursor(0,1); send_address(0x5,1); read_register(&(data[0]), 1); print_num(data[0]); print_string(" "); print_num(count); count++; _delay_ms(50); clear_screen(); OCR2A = 28; } while(get_sw() == 0) {} led_on(1); led_on(0); clear_screen(); while(1) { _delay_ms(1500); if (j) { clear_screen(); print_string("test"); led_on(1); led_off(0); //sbi(PORTC,LCD_E_PIN); //sbi(PORTC,LCD_RS_PIN); //PORTA = 0xff; sbi(PORTC,SERVO0_PIN); //set servo pins sbi(PORTC,SERVO1_PIN); sbi(PORTC,SERVO2_PIN); sbi(PORTF,SERVO3_PIN); //test i2c pins //sbi(PORTE,4); //sbi(PORTE,5); //motor test sbi(PORTB,MOTOR0_EN_PIN); sbi(PORTB,MOTOR1_EN_PIN); sbi(PORTD,MOTOR0_DIR0_PIN); sbi(PORTD,MOTOR0_DIR1_PIN); sbi(PORTD,MOTOR1_DIR0_PIN); sbi(PORTD,MOTOR1_DIR1_PIN); j=0; } else { led_off(1); led_on(0); clear_screen(); print_string("program"); j=1; //cbi(PORTC,LCD_E_PIN); //cbi(PORTC,LCD_RS_PIN); //PORTA = 0; cbi(PORTC,SERVO0_PIN); //clear servo pins cbi(PORTC,SERVO1_PIN); cbi(PORTC,SERVO2_PIN); cbi(PORTF,SERVO3_PIN); //test i2c pins //cbi(PORTE,4); //cbi(PORTE,5); //motor test cbi(PORTB,MOTOR0_EN_PIN); cbi(PORTB,MOTOR1_EN_PIN); cbi(PORTD,MOTOR0_DIR0_PIN); cbi(PORTD,MOTOR0_DIR1_PIN); cbi(PORTD,MOTOR1_DIR0_PIN); cbi(PORTD,MOTOR1_DIR1_PIN); } } return 0; }
//send the register address to read void send_address(u08 reg, u08 read) { u08 i; //u08 addr = MMA8453_ADDR; u08 addr = 0x1C; unlock_bus(); i2c_start(); //send start bit //send address bits 6:0 for (i=0;i<7;i++) { if (addr & _BV(6)) { //send 1 SDA_HI; } else { //send 0 SDA_LO; } _delay_us(I2C_DELAY); clock_scl(); addr = addr << 1; } //send W bit of 0 SDA_LO; _delay_us(I2C_DELAY); clock_scl(); SDA_HI; //release the data line _delay_us(1); SCL_HI; //read the ACK if (I2C_PIN & _BV(SDA_PIN)) { print_string ("addrNACK",8); while(1){} } else { //received ACK //print_string ("ACK",3); } _delay_us(I2C_DELAY); SCL_LO; _delay_us(I2C_DELAY); /////////send the register address //register address for (i=0;i<8;i++) { if (reg & _BV(7)) { //send 1 SDA_HI; } else { //send 0 SDA_LO; } _delay_us(I2C_DELAY); clock_scl(); reg = reg << 1; } SDA_HI; //release the data line _delay_us(1); SCL_HI; //read the ACK if (I2C_PIN & _BV(SDA_PIN)) { print_string ("NACK",4); while(1){} } else { //received ACK //print_string ("ACK",3); } _delay_us(I2C_DELAY); SCL_LO; _delay_us(I2C_DELAY); if (read) i2c_stop(); }