Skip to content

cc

cc 是运行在rp2040上的C编译、执行器,它可以生成:

  • 汇编语言
  • 可执行的二进制文件。

前端词法分析由C4实现.
后端指令生成:AMaCC.

-s 生成汇编

sh
cc -s file.c
cc -s file.c
asm
1: int main(){
2:      httpd();
3:      // lvgl_test();
4:      return 0;
5: }
20039800    b580        push    {r7, lr}
20039802    466f        mov     r7, sp
20039804    4b03        ldr     r3, [pc, #12]   ; 0x20039814
20039806    4798        blx     r3
20039808    2000        movs    r0, #0
2003980a    46bd        mov     sp, r7
2003980c    bd80        pop     {r7, pc}
2003980e    46bd        mov     sp, r7
20039810    bd80        pop     {r7, pc}
20039812    46c0        mov     r8, r8
20039814    1006 b311   .word   0x1006b311      ; httpd
20039818    0000 0000   .word   0x00000000      ; "\0\0\0\0"
1: int main(){
2:      httpd();
3:      // lvgl_test();
4:      return 0;
5: }
20039800    b580        push    {r7, lr}
20039802    466f        mov     r7, sp
20039804    4b03        ldr     r3, [pc, #12]   ; 0x20039814
20039806    4798        blx     r3
20039808    2000        movs    r0, #0
2003980a    46bd        mov     sp, r7
2003980c    bd80        pop     {r7, pc}
2003980e    46bd        mov     sp, r7
20039810    bd80        pop     {r7, pc}
20039812    46c0        mov     r8, r8
20039814    1006 b311   .word   0x1006b311      ; httpd
20039818    0000 0000   .word   0x00000000      ; "\0\0\0\0"

-o 生成可执行文件

sh
cc -o a.out file.c
cc -o a.out file.c

编译执行

sh
cc file.c
cc file.c

视频展示

hello

示例代码

gpio.c

c
#define led_a  16
#define led_b  17
#define led_c  18
#define led_d  19

int mask_val = 1<<led_a|1<<led_b|1<<led_c|1<<led_d; //34537472; 

int led_val = 0;
int led_idx = -1;

void gpio_test_one(){
    sleep_ms(500);
    gpio_put_masked(mask_val,led_val^=0xffffffff);
}


void gpio_test_two(){
    sleep_ms(200);
    led_idx = ++led_idx%4;
    gpio_put_masked(mask_val,1<<(led_a +led_idx));
}

int main(int argc,char ** argv) {
    led_val = mask_val;

    int case_n = -1;
    if(argc >1){
        case_n = atoi(argv[1]);
    }
    gpio_init_mask(mask_val);
    gpio_set_dir_masked(mask_val,mask_val);

    gpio_put_masked(mask_val,led_val);

    printf("GPIO Demo\r\n");
    while (1) {
        if (getchar_timeout_us(0) == 3) break;
        if(-1 == case_n) gpio_test_one();
        else gpio_test_two();
    }
    return 0;
}
#define led_a  16
#define led_b  17
#define led_c  18
#define led_d  19

int mask_val = 1<<led_a|1<<led_b|1<<led_c|1<<led_d; //34537472; 

int led_val = 0;
int led_idx = -1;

void gpio_test_one(){
    sleep_ms(500);
    gpio_put_masked(mask_val,led_val^=0xffffffff);
}


void gpio_test_two(){
    sleep_ms(200);
    led_idx = ++led_idx%4;
    gpio_put_masked(mask_val,1<<(led_a +led_idx));
}

int main(int argc,char ** argv) {
    led_val = mask_val;

    int case_n = -1;
    if(argc >1){
        case_n = atoi(argv[1]);
    }
    gpio_init_mask(mask_val);
    gpio_set_dir_masked(mask_val,mask_val);

    gpio_put_masked(mask_val,led_val);

    printf("GPIO Demo\r\n");
    while (1) {
        if (getchar_timeout_us(0) == 3) break;
        if(-1 == case_n) gpio_test_one();
        else gpio_test_two();
    }
    return 0;
}

flash.c

脱机烧录
c
int main(){
    active_port(0);
    flash("stm32f103","/upload/stm32f103_blink.bin");
    active_port(1);
    flash("stm32f103","/upload/stm32f103_blink.bin");
    return 0;
}
int main(){
    active_port(0);
    flash("stm32f103","/upload/stm32f103_blink.bin");
    active_port(1);
    flash("stm32f103","/upload/stm32f103_blink.bin");
    return 0;
}

flash_ex.c

进阶烧录。当按下return键时进行烧录操作
c
int curr_key = 0;

int gpio_callback(int num){
    curr_key = get_curr_key();
    return 0;
}

int main(){
    reg_gpio_hook(gpio_callback);
    while(1){
        if(curr_key){
            printf("key %d down\r\n",curr_key);
            if(curr_key == 24){
                flash("stm32f103","/upload/stm32f103_blink.bin");
            }
            curr_key = 0 ;
        }
        sleep_ms_v(1);
        if (getchar_timeout_us(0) == 3){
            unreg_gpio_hook(gpio_callback);
            break;
        }
    }
    return 0;
}
int curr_key = 0;

int gpio_callback(int num){
    curr_key = get_curr_key();
    return 0;
}

int main(){
    reg_gpio_hook(gpio_callback);
    while(1){
        if(curr_key){
            printf("key %d down\r\n",curr_key);
            if(curr_key == 24){
                flash("stm32f103","/upload/stm32f103_blink.bin");
            }
            curr_key = 0 ;
        }
        sleep_ms_v(1);
        if (getchar_timeout_us(0) == 3){
            unreg_gpio_hook(gpio_callback);
            break;
        }
    }
    return 0;
}

Released under the MIT License.