📄 i2c-aa.c
字号:
case I2C_SMBUS_BYTE_DATA: if (read_write == I2C_SMBUS_READ) msg[1].len = 1; else { msg[0].len = 2; msgbuf0[1] = data->byte; } break; case I2C_SMBUS_WORD_DATA: if (read_write == I2C_SMBUS_READ) msg[1].len = 2; else { msg[0].len=3; msgbuf0[1] = data->word & 0xff; msgbuf0[2] = (data->word >> 8) & 0xff; } break; case I2C_SMBUS_PROC_CALL: num = 2; /* Special case */ msg[0].len = 3; msg[1].len = 2; msgbuf0[1] = data->word & 0xff; msgbuf0[2] = (data->word >> 8) & 0xff; break; case I2C_SMBUS_BLOCK_DATA: if (read_write == I2C_SMBUS_READ) { printk("i2c-core.o: Block read not supported under " "I2C emulation!\n"); return -1; } else { msg[0].len = data->block[0] + 2; if (msg[0].len > 34) { printk("i2c-core.o: smbus_access called with " "invalid block write size (%d)\n", msg[0].len); return -1; } for (i = 1; i <= msg[0].len; i++) msgbuf0[i] = data->block[i-1]; } break; default: printk("i2c-core.o: smbus_access called with invalid size (%d)\n", size); return -1; } printk("end of case switch\n"); if (i2c_transfer(adapter, msg, num) < 0) return -1; if (read_write == I2C_SMBUS_READ) switch(size) { case I2C_SMBUS_BYTE: data->byte = msgbuf0[0]; break; case I2C_SMBUS_BYTE_DATA: data->byte = msgbuf1[0]; break; case I2C_SMBUS_WORD_DATA: case I2C_SMBUS_PROC_CALL: data->word = msgbuf1[0] | (msgbuf1[1] << 8); break; } printk("End of i2c_smbus \n"); return 0;#endif}/*----------------------------------------------------------------------- * int i2c_aa_xfer(struct i2c_adapter *i2c_adap, * struct i2c_msg msgs[ ], SINT16 num) * This function is responsible for the transfer of the data through the I2C bus * * Parameter : * i2c_adap the structure associated with the related hardware. * msgs[ ] the body of the message to be send out * num number of message * * Return : * Success Number of message has been transferred * Failure -1 *----------------------------------------------------------------------*/ //#define MSG_LEN#ifdef POLLING_MODEint i2c_aa_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num){ short i,j,dummy,count=0; //printk(" Enter i2c_aa_xfer\n"); //start to enable the i2c-module // //check IBB[I2SR:5] (I2C bus busy bit) until it is not busy // while((i2c_aa_reg->i2sr) & (UINT32)0x20); //set IEN[I2CR:7] (I2C Enable) // i2c_aa_reg->i2cr |= (UINT32)0x80; //set the TXAK[I2CR:6] to disable the I2C transmit ACK // i2c_aa_reg->i2cr |= (UINT32)0x08; //start to deal with the message // for ( i = 0; i < num; i ++){ // Generate the Start Bit if(i){ i2c_aa_repeat_start(); //printk("after i2c_restart\n"); } else{ i2c_aa_start(); //printk("after i2c_aa_start\n"); } // Write Algo// if (!((msgs[i].flags) & (char)I2C_M_RD )){ //printk("write mode\n"); // Set the MTX[I2CR:4] to enable the master transmit // i2c_aa_reg->i2cr |= (UINT32)0x10; //printk("master transmit is enabled\n"); // set the slave address to the I2DR register for WRITE // i2c_aa_reg->i2dr = msgs[i].addr; //printk("slave address(write) assigned\n"); // check transfer is completed(should check RXAK but in this code doesnt)// i2c_aa_transfer_complete(); //printk("transfer completed\n"); //printk("<1>IFDR: 0x%x\n", READREG(IFDR)); //start to deal with the length of the message// for ( j = 0; j < msgs[i].len; j ++ ){ // write the msgs[i].buf[0] // i2c_aa_reg->i2dr = msgs[i].buf[j]; //printk("message assigned to i2dr completed\n"); // check transfer is completed// i2c_aa_transfer_complete(); //printk("<1>IFDR: 0x%x\n", READREG(IFDR)); //printk("transfer completed\n"); } count += msgs[i].len; } // Read Algo// else{ //printk("read mode\n"); // Set the MTX[I2CR:4] to enable the master transmit // i2c_aa_reg->i2cr |= (UINT32)0x10; //printk("master transmit is enabled\n"); // set the slave address to the I2DR register for READ// i2c_aa_reg->i2dr = (UINT32)msgs[i].addr | (UINT32)0x1; //printk("slave address(read) assigned\n"); // check transfer is completed// i2c_aa_transfer_complete(); //printk("transfer completed\n"); // Clear the MTX bit in the I2CR to set the Master receive mode// i2c_aa_reg->i2cr &= ~(0x10); //printk("master receive is enabled\n"); // Set the TXAK[I2CR:6] to enable the I2C transmit ACK // if(msgs[i].len-1) i2c_aa_reg->i2cr &= ~(UINT32)0x08; //printk("TXAK is enabled\n"); // Read the dummy value to trigger the receive of the next byte // dummy = (UINT8)i2c_aa_reg->i2dr; //printk("read dummy to trigger\n"); //check the transfer is completed for read// i2c_aa_transfer_complete(); //printk("<1>IFDR: 0x%x\n", READREG(IFDR)); //printk("transfer completed\n"); //start to deal with the length of the message // for ( j = 0; j < msgs[i].len; j ++ ){ // read the data from I2DR register to the msgs[i].buf[j]// if(j== (msgs[i].len -2)){ // Set the TXAK[I2CR:3] to disable the ack bit before read last byte // i2c_aa_reg->i2cr |= 0x08; //printk("TXAK is disabled\n"); } msgs[i].buf[j] = (UINT8)i2c_aa_reg->i2dr; //printk("buffer is assigned to i2dr\n"); //check the transfer is completed for read// i2c_aa_transfer_complete(); //printk("transfer completed\n"); //printk("<1>IFDR: 0x%x\n", READREG(IFDR)); //There is a problem: after reading the last byte from i2dr, triggering i2dr to receive another byte// //We should wait for the transfer completed to assign stops/restart again// } count += msgs[i].len; } //printk("for loop ends\n"); } // send out the stop sigal to i2c bus // i2c_aa_stop(); //printk("Issue the stop command\n"); // wait until the bus is released // i2c_aa_bus_release(); return count;}#elseint i2c_aa_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num){ return 0;}#endif // POLLING_MODE/*----------------------------------------------------------------------- * void i2c_aa_isr (SINT16 irq, void * dev_id, struct pt_regs * reg) * This function deals with the interrupt for the I2C module. * * Parameters: * irq the interrupt number * dev_id device id * reg processor register * * Return: * None *----------------------------------------------------------------------*/#ifndef POLLING_MODEvoid i2c_aa_isr (SINT16 irq, void * dev_id, struct pt_regs * reg){ return;}#endif // POLLING_MODE/*----------------------------------------------------------------------- * static int i2c_aa_ioctl(struct i2c_adapter * adapter, * unsigned int cmd, unsigned long arg) * This function control the I2C module itself * * Parameters : * Adapter the adapter associated to the I2C module * Cmd IO control command * Arg argument associated with the command * Return : * Success 0 * Failure -1 *----------------------------------------------------------------------*/ int i2c_aa_ioctl(struct i2c_adapter * adapter, unsigned int cmd, unsigned long arg){ FUNC_START; switch(cmd ) { case I2C_IO_CHANGE_FREQ: /* * set the parameter to the IFDR register */ //printk("\n%lx ",i2c_aa_reg->ifdr); //printk("%lx ",arg); i2c_aa_reg->ifdr = (UINT32)(arg & 0x003F); //printk("%lx\n",i2c_aa_reg->ifdr); break; case I2C_IO_GET_STATUS: /* * Store the value of I2SR to the arg */ arg = i2c_aa_reg->i2sr; break; default: } FUNC_END; return 0;}/*----------------------------------------------------------------------- * static void __exit i2c_aa_cleanup( void ) * This routine is called when the driver is unloaded * * Parameter : * None * Return : * None *----------------------------------------------------------------------*/void __exit i2c_aa_cleanup( void ){ FUNC_START; //???should free_irq() first and del_adapter /* * call the i2c_del_adapter() * to deregister the driver from the Linux I2C system */ /* release the I2C_AA ISR handler */#ifndef POLLING_MODE free_irq (I2C_AA_IRQ, "i2c_bus");#endif i2c_del_adapter(&i2c_mx1_adapter); FUNC_END;}/************************************************************************ * EXPORT SYMBOL ************************************************************************/ //EXPORT_SYMBOL(i2c_aa_xfer);//EXPORT_SYMBOL(i2c_aa_ioctl);/************************************************************************ * Init Module ************************************************************************/ #ifdef MODULEMODULE_AUTHOR("GSG-China");MODULE_DESCRIPTION("I2C Adapter driver");MODULE_PARM(i2c_debug, "i");MODULE_PARM_DESC(i2c_debug,"debug level");int init_module(void) { printk("I2C Adapter/Algorithm Driver version 0.3.6\n"); return i2c_aa_init();}void cleanup_module(void){ i2c_aa_cleanup();}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -