1

I have an ATtiny13a running code at 9.6 MHz (internal oscillator) for single pin Tx Rx half-duplex serial port at 115200 baud rate using a lib which I found online

Using FTDI 232RL or BT HC-06 in between I am able to send & receive arbitrary data between the MCU circuit and Putty/Android.

What would be the cheapest and smallest (in terms of PCB size and ATTiny memory) way to achieve the same arbitrary data communication to an Android device (non-rooted) instead of BT.

Also is there a way to achieve it keeping the MCU circuit in accessory mode for Android and NOT using Android's host mode since the MCU circuit will be powered externally and not from Android. The MCU's power source will also be delivering power to Android on the USB's Power buses.

How can I achieve arbitrary data communication between ATTiny in accessory mode and Android over USB?

Will VUSB work in this case? or do I have to buy a converter chip (CHG340?) but will that chip work in accessory mode? Also will there be any changes to support USB Type C port?

Thanks

riteshtch
  • 171
  • 1
  • 7
  • 1
    It's not clear to me what you want to achieve. Do you want your stuff behave as an USB device or as an USB host? – Janka Dec 30 '16 at 13:58
  • @Janka well, Android device certainly **wont** be the host since it is powered/charged externally from the MCU's power source but I do want to have arbitrary data communication with the MCU. Similar to the scenario when we transfer data over USB between Android and a PC: Android phone gets charged but we have 2-way data communication as well. Well for that we do use complex protocols such as MTP etc. But I want to do the same except between Android and ATTiny. So its like the MCU circuit would be an accessory to Android. Is that achievable? – riteshtch Dec 30 '16 at 14:11
  • So you want to create an USB device. Sure, that's what VUSB is for. I successfully created a keyboard emulator with it: http://vusb.wikidot.com/project:vusb. Was about 2 days of work. Certainly worth it. Speed is limited though. If you want to create anything but a token, a keyboard simulator or other low-speed device, you better use a MCU which has an USB device controller on board. – Janka Dec 30 '16 at 14:15
  • @Janka I mainly come from a software background in degree and profession. AVR is just a hobby for me. so wanted to know if VUSB needs any support on the Android side? Have you tried using MCU's with Android before with VUSB communication? does it work normally like an ATTiny VUSB communication with a PC? Also not using Android in host mode? – riteshtch Dec 30 '16 at 14:54
  • I used an ATtiny45. VUSB can use the internal oscillator at 12.6MHz, but then every single piece needs to be calibrated; so I added the crystal. The VUSB device I did declares itself as a keyboard and Android sees it as a keyboard. You can implement a number of other device classes the Linux kernel in your phone has drivers for but in reality you are limited to USB-HID on Android. For cdc_anything, VUSB isn't fast enough and you said you don't want to implement your own driver. – Janka Dec 30 '16 at 14:54
  • What do you mean by "not using Android in host mode"? When you connect an USB device, your phone is sure an USB host. – Janka Dec 30 '16 at 14:59
  • @Janka Ohh I see. Yeah ATtiny13a's internal oscillator is restricted to ~10MHz i suppose and afaik it takes an external CLK signal and not a crystal but anyways I'll try out using an ATTiny45 instead. Yes you are right, I can present the device as an HID device to Android. – riteshtch Dec 30 '16 at 15:02
  • @Janka what I meant was that I **wont** be powering my ATTiny circuit using power from the usb port i.e Android **wont** act as a power host for the device. Instead i'll be powering it from an external source and also delivering charge to Android. Think of my device acting as a data communicator and powerbank at the same time. Is that possible along with data transfer? – riteshtch Dec 30 '16 at 15:04
  • You have to connect the GNDs. Where you get the +5V from is irrelevant to the phone. Also see the circuit diagram in the archive on the page I linked. About the charging, no clue whether this works at the same time. – Janka Dec 30 '16 at 15:11
  • @Janka Ohh sure i'll checkout the schematic you linked. and yes that was my question; whether both of these would work at same time. And as you mentioned I would be delivering power to Android's USB port from the MCU's power source, but when I'm coding my Android app should i use host mode API's or accessory mode(ADK)? Since I cannot charge my phone in host mode I wont have to use the phone in that mode .. right? – riteshtch Dec 30 '16 at 15:22
  • @Janka - no. The poster wants to power the phone from the external circuit, which points toward's Androids earlier "USB Accessory" mode where the phone is the DEVICE and the external circuit is the HOST, rather than its later partial (varies by model) support for the phone being a USB host. – Chris Stratton Dec 30 '16 at 16:15
  • The Android phones I have at least allow to connect an USB keyboard so I think host mode is pretty standard by now. But when charging at the same time isn't possible, the OP can't go that way. – Janka Dec 30 '16 at 16:18
  • @ChrisStratton yes you are right; I need 2 functions at the same time: deliver power to phone and have arbitrary data communication. Is that possible? – riteshtch Dec 30 '16 at 22:49
  • That's what Android Accessory mode is *supposed* to do, how well it works in a given case will be for you to determine. – Chris Stratton Dec 30 '16 at 23:26
  • @ChrisStratton ohh ok thanks. I'll give Android Accessory mode a shot with VUSB+ATtiny. – riteshtch Dec 30 '16 at 23:43
  • 1
    You can't do Android Accessory with VUSB, you need to use something that can be a USB host. I don't think there are any ATmega or ATtiny parts that can, and the outboard USB hosts are not cost effective compared to using an host-capable MCU to start with. – Chris Stratton Dec 31 '16 at 00:32
  • @ChrisStratton Can't I use this USB Host library for arduino due: https://www.arduino.cc/en/Reference/USBHost and present android device as a keyboard to arduino? Arduino due has full blown 32bit and other hardware capabilities, is there any ported version of the same library for less powerful mcus? – riteshtch Dec 31 '16 at 04:58
  • A number of ARM MCU's apparently including the one on the Due have USB host capability. As far as I know, *none* of the ATmega or ATtiny series do. There are cheaper solutions than the Due which do as well (like the KL25Z) but you may be more on your own to figure out software. And no, you can't use the Android device as a keyboard, you need to talk the protocol it knows, at least unless you root it and install a custom gadget driver or similar. – Chris Stratton Dec 31 '16 at 05:01
  • @ChrisStratton ohh .. Do you have any idea how is the internal circuit for [this](https://www.amazon.com/dp/B009YPYORM/?tag=stackoverfl08-20) cable which allows OTG/Host mode and charging at the same time. Can we replicate the same circuit onto a pcb? Will it be compatible with all devices? Btw i got the info from these stackexchange links: [link1](http://android.stackexchange.com/questions/34227/using-an-otg-cable-while-charging) and [link2](http://electronics.stackexchange.com/questions/34741/can-an-android-tablet-serve-as-usb-host-and-be-charged-simultaneously-through-a) – riteshtch Jan 01 '17 at 02:41
  • Those cables arnt universal. Look at the reviews. If this is just a one off project or a specific phone, then you could copy those cables. If it requires a specific sequence, you can control that too. – Passerby Jan 04 '17 at 03:23

2 Answers2

1

There is no easy way to do usb host and charging at the same time. Most phones do not support this.

But you can bypass the need for usb communication by using a bluetooth to serial module. Nothing really changes on your ATtiny aside from some minor configuration. Same serial port as the FTDI. Then provide power as a normal usb connection.

V-USB acts like a FTDI or other usb serial IC, so Android Host Mode is still required.

Passerby
  • 72,580
  • 7
  • 90
  • 202
  • Thanks.. And yes Bluetooth does work in this case.. I've tested it out and works nicely with ATtiny13a but a Bluetooth chip increases my pcb size.. I wanted something smaller than currently available HC 05/6 or SPP-C SMT chips. Thanks anyways.. I'll have to resort to Bluetooth if nothing else works. – riteshtch Jan 01 '17 at 04:57
  • @ritesht93 I'm sure there are smaller modules. Or skip the modules with back boards and wire to the ICs directly. The base boards add too much. – Passerby Jan 04 '17 at 03:14
  • @ritesht93 like http://m.gearbest.com/other-accessories/pp_341243.html (I haven't personally used these.) – Passerby Jan 04 '17 at 03:17
0

What would be the cheapest and smallest (in terms of PCB size and ATTiny memory) way to achieve the same arbitrary data

Either use a different and USB Host/OTG capable MCU or use some analogue trick on the headphone port. The latter requires some passives and an ADC pin on the Tiny.

Turbo J
  • 9,969
  • 1
  • 20
  • 28
  • There are a fair number of ARM Cortex-M MCUs with embedded USB host from Freescale, ST, etc though the poster may want to approach the project from the sense of what they can find android USB Accessory mode already supported on, rather than what is the smallest/cheapest hardware as the latter may require more original work. – Chris Stratton Dec 30 '16 at 16:16