Micro_Controllers - RicoJia/notes GitHub Wiki

STM32

  1. I2C

    1. HAL_I2C_IsDeviceReady(&hi2c1, DEVICE_ADDRESS,1, HAL_MAX_DELAY)
    2. all it does is send the address and wait for the ACK from the slave.
    3. It requires the 7 bit address shifted to the left by 1 bit, last bit is R/W **0x70 -> 0xE0**
    4. In some datasheet: p = stop, s = start
    5. Adding delays:
      • lower the frequency to ping your sensor may help
      • adding delays b/w multiple sequences will help too.
    6. printf will slow down this process, or may cause hanging
    7. last frame / first frame: sending a start bit / stop bit
  2. Timer

    • sysclock is for RTOS
    • clocktree in the manual is very important
    • prescaler = real prescaler - 1, Counter period (value that counts up to) is the same
    • watch dog: if someone doesn't kick it, it will try to restart.
  3. Serial

    • use \r\n (windows style) instead of \n
  4. overclocking: boost up clock frequency.

555 Timer

  • One-shot timer
    • one shot timer is: when you push a button once, an LED will keep on for a while, then it will stay off again.

      • E.g, you press a button and you get a stable on waveform for a while.

      one_shot_waveform
      • pinouts of 555 timer: Discharge is key to having this to work

      555 Timer Pinouts
      • Internal Circuit of 555

        • Set output pin to high:

        Set 555
        • Set output pin to low, because we need to feed high to R thru C2. We want 6 to be Vcc.
          • Note that we assume we have released the button before we want to feed high to R.

        Reset 555
        • So we can put a cap, which takes time to charge and discharge, thus set 555 to high and low

        With capacitor
        • Finally we can drive an LED

        555_full
    • Auto-reload timer:

      • in STM32, there's auto-reload timer, whose value will reset the timer back to 0. (ARR)
        • the timer might be a count-up timer or count-down timer
    • clock stretching

Keystudio CH340

  1. Exactly the same as nano.
  2. "Burn" firmware. 16v is needed to change the physical state of the ROM.

Communication Protocols

I2C

  • Picture
  • Basics

    • 255 devices, 2^7.
    • aka TWI communication, Same logical order as serial pins, PWM pins, SPI pins, Digital Write pins, and external interrupts
    • Slow compared to USB, PCI, but good for EEPROM (e-e-prom) chips -Principle
    • synchronous communication, means clocked, (SCL)
      • Needs pull-up resistor for high/low switching
    • No chip select, because address is being sent thru data (SDA)
      • 8 bit address
      • each device has a unique address. That address should be settable.
      • protocols
        • Write protocol
          start bit 
          8 bit - address (only the right device will keep listening, also, the first bit tells read/write)
          8 bit - command
          8 bit - value
          stop bit
          
        • Read Protocol
          start bit
          8 bit address
          8 bit command (something we want to change)
          restart bit 
          8 bit address
          RECV (receive)
          AK
          repeat...
          stop bit
          
          • You want to read all the bytes from the buffer, otherwise it's not a complete transaction
    • DMA (Direct Memory Access)
      • This allows I2C to directly write to the registers, instead of using the processor
      • Good for slow processes, because will offload the processor even more.
    • Downsides:
      • if mcu freezes, the device doesn't know this and will hang. You need to restart the power.
    • Polling vs Interrupts:
      • Interrupt is hardware line that tells the CPU
      • Polling is a software steady check
      • Interrupt pros:
        • Interrupt is much better than polling, cuz polling might have data loss
        • good for infrequent uses.
      • Polling pros:
        • synchronous (i.e, you know when the poll happens)
        • frequent but not urgent
    • clock-stretching
      • If the slave is too slow, it can hold the clock to ground level, to pause the I2C

      • without clock_stretching, in the above pic, the master will read a NACK (not acknowledged) from SDA

  • I2C in action

    1. Two devices sharing the same I2C bus using interrupts: how does that work?
    • “the slave device sets that GPIO to high, which raises an interrupt with the master device. The master device then reads a “interrupt source” register which indicates why the slave device raised the interrupt, and goes on to query the device for the information.”
  • Libraries:

    • Wire Library: Arduino, ESP8266, STM32
    • TinyWire Library: Attiny
⚠️ **GitHub.com Fallback** ⚠️