CH554 SDCC SDK

repository·master·Indexed 43 days ago

https://github.com/blinkinlabs/ch554_sdcc

A software development kit for the CH554 microcontroller, ported from the Keil C51 environment to the SDCC (Small Device C Compiler) toolchain. It includes peripheral drivers, build infrastructure, and a Python script to automate the translation of Keil C51 files to SDCC syntax.

Tokens
1.5K
Snippets
5
Records
9
Agent score
6%

What's inside ch554_sdcc

  1. Overview of the USB-I2C Bridge Example

    master

    This example implements a USB-I2C bridge using the CH554 (and CH551) microcontroller. It provides a driverless way to access an I2C bus via a serial interface.

    Hardware Specifications:

    • SCLK Pin: P3.3
    • SDAT Pin: P3.4
    • I2C Frequency: Fixed 100kHz SCLK
    • Transport Limit: 16 bytes (Note: The protocol allows up to 64 bytes, but transport is limited to 16 bytes in this implementation).

    Use Cases:

    • General I2C bus access.
    • Configuring a SI5351 clock generator.
    • Acting as a VFO (Variable Frequency Oscillator) when combined with a TCXO and SI5351.
  2. Install the toolchain on Windows

    master

    To develop for the CH554 on Windows, you need to install SDCC, MinGW (for make), Git Bash, and WCHISPTOOL.

    After installation, add the following to your .bashrc to configure your environment:

    1. Add SDCC to your PATH.
    2. Add MinGW to your PATH.
    3. Alias make to mingw32-make.exe.

    Note: You can use LibreCH551 or ch552tool as open-source alternatives to WCHISPTOOL for automated firmware loading.

    # SDCC compiler tools
    export PATH=$PATH:/c/Program\ Files/SDCC/bin
    
    # Mingw tools (for Make)
    export PATH=$PATH:/c/Qt/Qt5.10.0/Tools/mingw530_32/bin
    
    alias make=mingw32-make.exe
  3. Create a new example project

    master

    To create a new example, follow these steps:

    1. Create a directory in the examples/ folder.
    2. Create a Makefile that includes the master template. The TARGET variable must match your directory name.
    3. Create a main.c file. Ensure you include <ch554.h> and any necessary headers like <debug.h>.
    4. Include dependencies: If you use functions from debug.c (like mDelaymS), you must explicitly add ../../include/debug.c to your C_FILES list in the Makefile.

    Example Makefile structure

    TARGET = fastblink
    
    C_FILES = \
        main.c \
        ../../include/debug.c
    
    include ../Makefile.include

    Example main.c

    #include <ch554.h>
    #include <debug.h>
    
    #define LED_PIN 7
    SBIT(LED, 0x90, LED_PIN);
    
    void main() {
        // Configure pin 1.6 as GPIO output
        P1_DIR_PU &= 0x0C;
        P1_MOD_OC = P1_MOD_OC & ~(1<<LED_PIN);
        P1_DIR_PU = P1_DIR_PU | (1<<LED_PIN);
    
        while (1) {
            mDelaymS(10);
            LED = !LED;
        }
    }
  4. Port Keil C51 files to SDCC syntax

    master

    The Keil C51 and SDCC compilers have syntax incompatibilities, specifically regarding SFR/SBIT defines, absolute addressing, and endianness (SDCC is little endian, C51 is big endian).

    This project provides a Python script to automate approximately 90% of the translation work:

    python tools/c51_to_sdcc.py [source] [destination]
  5. Build the repository examples

    master

    To build all available examples in the repository, clone the repo and run make from the examples directory:

    git clone https://github.com/Blinkinlabs/ch554_sdcc.git
    cd ch554_sdcc/examples
    make

    Flashing the examples

    • Windows: Use the WCHISPTool GUI to flash the generated .ihx image.
    • Linux/macOS (or Windows with LibreCH551): Run make flash to load the example directly onto your board.
  6. USB-I2C Bridge Communication Protocol

    master

    The bridge communicates via a simple serial protocol. All responses from the device are terminated with <CR> <LF> (End of Transmission).

    Request Commands

    CommandDescription
    QRequest the target clock generator's reference frequency
    VGet firmware version
    EEscape to internal bootloader for in-application re-programming
    BGet baudrate (used for debugging)
    TPerform an I2C transmission
    RPerform an I2C reception

    I2C Transmission (T)

    Format: T<1-byte length> <AR & data>

    • Length: 1 byte (must be < 64).
    • Payload: Address (AR) and data must be shorter than 64 bytes total.
    • Success Response: OK\r\n
    • Failure Response: Fnn\r\n (where nn indicates the error stage).
    • Special Behavior: If the highest bit of the address is set, the device will NOT send an I2C STOP condition.

    I2C Reception (R)

    Format: R<AR><Length>

    • AR: Device Address.
    • Length: Number of bytes to read (must be < 64).
    • Success Response: The requested number of bytes from the device.
    • Failure Response: If there is no response from the bus on the send address, the device sends FAIL\r\n to the serial port.
  7. Reference Makefile configuration variables

    master

    The build process is controlled by Makefile.include. You can override the following variables in your local example's Makefile:

    VariableDescription
    TARGETExample name, used to name the .hex file
    C_FILESList of C files to include in the example build
    FREQ_SYSSystem clock frequency. Default is 12000000 (12MHz). See include/debug.c for accepted values
    XRAM_SIZESize of the non-reserved XRAM. Use this to reserve space for absolute variables (e.g., USB DMA pointers)
    XRAM_LOCStarting position of the non-reserved XRAM
    STDIO_UARTSet to '0' to use UART0 for STDIO, or '1' to use UART1 (work in progress)