GPIO_16 点灯
c
#define DELAY 100
int val = 0;
int LED = 16;
int main() {
gpio_init(LED);
gpio_set_dir(LED,GPIO_OUT);
while (1) {
if (getchar_timeout_us(0) == 3) break;
val = val?0:1;
gpio_put(LED,val);
sleep_ms(DELAY);
}
return 0;
}Button ISR
当按下某一个按钮处理其中断,并做一些特殊的事情如脱机烧录。 下面示例完成烧录 RP2040 的功能
c
#define BTN_UP_12 12
#define BTN_DOWN_23 23
int curr_key = 0;
int gpio_callback(int num){
curr_key = get_curr_key();
return 0;
}
int main(){
reg_gpio_hook(gpio_callback);
lvgl_text("*PUSH BTN TO FLASH*");
while(1){
if(curr_key){
printf("key %d down\r\n",curr_key);
if(curr_key == BTN_UP_12){
lvgl_text("TRY FLASH....");
flash("rp2040","/upload/rp2040_blink.bin");
lvgl_text("FINISHED");
}
curr_key = 0 ;
}
sleep_ms_v(100);
if (getchar_timeout_us(0) == 3){ // CTRL + C
unreg_gpio_hook(gpio_callback);
break;
}
}
return 0;
}I2C SCAN
当按下某一个按钮 扫描 I2C 总线
c
#define BTN_UP_12 12
#define BTN_DOWN_23 23
#define CLK_PIN 5
#define SDA_PIN 4
int gpio_1 = 10;
int gpio_2 = 11;
int curr_key = 0;
int i2c_devs[4] ;
int gpio_callback(int num){
curr_key = get_curr_key();
return 0;
}
int scan_addrs(){
int num = 0;
char rxdata;
int addr = 0;
int ret = -1;
for (addr = 0; addr < (1 << 7); ++addr) {
ret = -1;
if ((addr & 0x78) == 0 || (addr & 0x78) == 0x78) continue;
/*
PICO_ERROR_NONE = 0,
PICO_ERROR_GENERIC = -1,
PICO_ERROR_TIMEOUT = -2,
PICO_ERROR_NO_DATA = -3,
*/
ret = i2c_read_timeout_us(i2c0, addr, &rxdata, 1, false,20*1000);
if(ret>=0){
i2c_devs[num++] = addr;
}
// printf("try read:%d :%d\r\n",addr,ret);
if(num >=2) break;
}
printf("Found %d Devices\r\n",num);
gpio_put(gpio_1,num >=2?1:0);
gpio_put(gpio_2,num >=2?0:1);
return num;
}
int main(){
int ret = 0;
int i = 0;
httpd();
reg_gpio_hook(gpio_callback);
lvgl_text("*PUSH BTN TO SCAN*");
char strMsg[32];
i2c_init(i2c0, 100 * 1000);//Speed 100k
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);//I2C0_SDA
gpio_set_function(CLK_PIN, GPIO_FUNC_I2C);//I2C0_SCL
gpio_pull_up(CLK_PIN);
gpio_pull_up(SDA_PIN);
gpio_init(gpio_1);gpio_set_dir(gpio_1, GPIO_OUT);gpio_put(gpio_1,0);
gpio_init(gpio_2);gpio_set_dir(gpio_2, GPIO_OUT);gpio_put(gpio_2,0);
while(1){
if(curr_key){
if(curr_key == BTN_UP_12){
lvgl_text("TRY SCAN....");
ret = scan_addrs();strMsg[0]=0;
sprintf(strMsg,"Found %d Devs",ret);
lvgl_text(strMsg);
}
curr_key = 0 ;
}
sleep_ms_v(100);
if (getchar_timeout_us(0) == 3){ // CTRL + C
unreg_gpio_hook(gpio_callback);
break;
}
}
return 0;
}I2C Sniffer
C
#define BTN_UP_12 12
#define BTN_DOWN_23 23
#define CTRL_C 3
#define EV_DATA 0
#define EV_START 1
#define EV_STOP 3
int main(){
i2c_sniffer_init();
int i = 0;
int nNum = 0;
int buff[32];
while(1){
nNum = i2c_sniffer_peek();
if(nNum <=0){
sleep_ms_v(100);
if (getchar_timeout_us(0) == CTRL_C){
i2c_sniffer_deinit();
return 0;
}
continue;
}
nNum = i2c_sniffer_read((int)&buff[0],32*4,0);
for(i=0;i<nNum/4;i++){
int msg = buff[i];
/*
第1字节为数据或 I2C 地址
第4字节
bit0 ACK;
bit1 R/W;
bit[2-7] 消息类型;
*/
int Byte3= msg >> 24;
int data = msg &0xff;
int type = Byte3 >>6;
int RW = Byte3&0x2?1:0;
int ACK = Byte3&0x1?1:0;
if(type == EV_START){
printf("\r\nS%02x%s%s :",data,RW?"R":"W",ACK?"N":"A");
}else if(type == EV_DATA){
printf("%02x ",data);
}else if(type == EV_STOP){
printf("P");
}
}
}
return 0;
}