|
![]() MCU User Forum
![]() USB to I2C request
|
| next newest topic | next oldest topic |
| Author | Topic: USB to I2C request |
|
ymmace New Member |
Hello all: I just got the C8051F343 for development these days. Because I'm not familiar with any USB interface programming. Thanks for your supporting in advance. IP: Logged |
|
Scotty Member |
Hi ymmace, you didn't specify how do you want to communicate with the USB interface. VirtualCOMPort, etc. I think the best way for you as a USB beginner is as follows: From my sight, I think USBXpress implementation is the best choice for you because it takes most of the USB implementation away from you, so you can concentrate on the project itself. On the host side you must communicate with USBXpress DLL. Regards, Scotty IP: Logged |
|
ymmace New Member |
Hi Scotty: Thanks a lot for your reply. After studying, I would like to apply by HID or Bulk mode. If I apply bulk mode, how to I design my firmware flow? However, I have no idea when to handle item-2 and item-5. IP: Logged |
|
Scotty Member |
Hi ymmace, I would not communicate with VirtualCOMPort because of communicate speed and previous bad experience. HID implementation is not very fast if I'm right, and bulk transfer needs deep driver development I think. So to answer your questions I suggest you tell us what you want to do exactly and then we can give better help. Regards, Scotty IP: Logged |
|
ymmace New Member |
Hi Soccty: It sounds good of your reply. All I want is almost written in the original post. How do you suggest? Best regards IP: Logged |
|
Scotty Member |
Hi ymmace, The flash space of target device is about 64KB. And I want to finish the programming within 1 second. So you must rethink about your specification/requirements. Regards, Scotty IP: Logged |
|
erikm Member |
PC <-> USB <-> C8051F343 <-> I2C <-> Target device(8051 like) The flash space of target device is about 64KB. And I want to finish the programming within 1 second. since you do not specify what you want to program [Target device(8051 like] I will guess, without even going into the transmission time (see Scotty's post above), I doubt you can achieve 1 second, just the erase time will, probably, make that impossible. Erik IP: Logged |
|
ymmace New Member |
Hi Scotty: Truly, the bottle neck of my project would be "Flash programming" and "I2C communication" after my applying USB communication between PC and bridge-board. Thank you. Hi erikm: I work my project with an ASIC. The transmission time of I2C and programming time of Flash may not so ideal. Best regards IP: Logged |
|
Talex New Member |
If you are still trying to get your USB to I2C code working I have some code examples I can provide, using both USBXpress and HID to I2C. I have coded something very similar, using a C8051F340 that interfaces to a number of IC's on the I2C/SMBUS (a serial EEPROM, a S/PDIF receiver, a PLL chip, a voltage/current monitoring chip, and a digital amplifier IC) and records data which is read by a PC and displayed in a GUI written in VB6. Originally we had used a Devasys USB to I2C bridge (a C8051F340 on a nice little development board that comes preloaded with USB to I2C firmware and has a windows API) with its original fw until I started writing my own and reflashing them, and modifying the windows GUI from the Devasys drivers to USBXpress, later HID... IP: Logged |
|
hjsteger New Member |
Talex, I am interested in the USB-to-I2C code that you've developed. I've been working on developing code to run on C8051F340D that implements a USB (HID) interface to I2C Master. This function will be used in a system that runs a Linux operating system (so the HID driver must work under Linux). In addition, would you by chance have a bootloader for this as well? IP: Logged |
|
t_alex New Member |
I'm not sure how to attach files here, but I can e-mail you what I've been working on. I have a bootloader for the F340, it's based on the AN200 usbxpress bootloader but I modified it so I can use HID in my main firmware instead of usbxpress (usbxpress is still used for bootloading only). My main firmware is based heavily on the HIDtoUART example, I modified the reporthandler function to decode my packets (I2C read or write, how many bytes, 8 or 16 bit sub-address, etc.), repackage each I2C request into a struct, then send a pointer to the struct to SMB_Read() or SMB_Write(). IP: Logged |
|
t_alex New Member |
this might get you started though: //----------------------------------------------------------------------------- static unsigned char Byte_Counter; switch (SMB0CN & 0xF0) // Status vector if (bSet_Read_Bit) STA = 0; // Manually clear START bit EIE1 |= 0x02; // Re-enable USB0 Interrupts case SMB_MTDB: //0xC0 -> (MT) data byte transmitted if (ACK){ // Slave ACK? } case SMB_MRDB: // 0x80 -> (MR) data byte received pStructAddr->DATA[Byte_Counter] = SMB0DAT; // Store received byte default: SMB0CF &= ~0x80; // Reset communication } // end switch PCA0CPH4 = 0x00; // clear watchdog timer IP: Logged |
|
t_alex New Member |
along with the above ISR goes this code: //----------------------------------------------------------------------------- #define SMB_FREQUENCY 100000 // Target SCL clock rate (100kHz) #define WRITE 0 // SMBus WRITE command // Status vector - top 4 bits only // I2C transfer structure definition // I2C TRANSMODE status vector (sub-addressing mode) // ---------------------------------------------------------------------------- static volatile bit bSMB_RW; // SMBUS Read/write bit struct I2CTrans xdata * data pStructAddr; // SMBUS packet pointer xdata volatile struct I2CTrans I2C_Main; // SMBUS packet (Main) //----------------------------------------------------------------------------- TMR3CN &= ~0x80; // Clear Timer3 interrupt-pending flag //----------------------------------------------------------------------------- // If slave is holding SDA low because of an improper SMBus reset or error //----------------------------------------------------------------------------- SMBUS_CTRL &= 0x3F; bSMB_FAIL = 0; // set FAIL to 0 if (bSMB_FAIL){ // if transfer failed //----------------------------------------------------------------------------- SMBUS_CTRL &= 0x3F; bSMB_FAIL_ISR = 0; // set FAIL to 0 if (bSMB_FAIL_ISR){ // if transfer failed //----------------------------------------------------------------------------- if (!(SMBUS_CTRL & 0x20)) // if not Master Inhibit SMBUS_CTRL &= 0x3F; bSMB_FAIL = 0; // set FAIL to 0 if (bSMB_FAIL) // if transfer failed //----------------------------------------------------------------------------- if (!(SMBUS_CTRL & 0x20)) // if not Master Inhibit SMBUS_CTRL &= 0x3F; bSMB_FAIL_ISR = 0; // set FAIL to 0 if (bSMB_FAIL_ISR) // if transfer failed //----------------------------------------------------------------------------- IP: Logged |
|
t_alex New Member |
and this goes in my Init.c: //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Make sure the Timer can produce the appropriate frequency in 8-bit mode CKCON |= 0x08; // Timer1 clock source = SYSCLK TMOD = 0x20; // Timer1 in 8-bit auto-reload mode // Timer1 configured to overflow at 1/3 the rate defined by SMB_FREQUENCY TL1 = TH1; // Init Timer1 TR1 = 1; // Timer1 enabled //----------------------------------------------------------------------------- CKCON &= ~0x40; // Timer3 uses SYSCLK/12 TMR3RL = -(SYSCLK/12/40); // Timer3 configured to overflow after EIE1 |= 0x80; // Timer3 interrupt enable
unsigned char SMBUS_CTRL = 0; IP: Logged |
All times are CT (US) | next newest topic | next oldest topic |
![]() |
|
Have you seen our MCU Knowledge Base?