#include #include #include #include "ql_api_spi.h" #include "ql_api_osi.h" #include "ql_gpio.h" #include "ql_log.h" #include "./inc/lora_test.h" #include "./inc/lora_test_function.h" #define LOGI(msg, ...) QL_LOG(QL_LOG_LEVEL_INFO, "lora_test", msg, ##__VA_ARGS__) #define RST_PIN 59 #define INT_PIN 60 #define BUSY_PIN 61 #define SWITCH_PIN 16 #define RST_GPIO GPIO_2 #define INT_GPIO GPIO_3 #define BUSY_GPIO GPIO_0 #define SWITCH_GPIO GPIO_19 ql_task_t lora_test_task; ql_errcode_gpio gpio_err; unsigned char *out_buf = NULL; unsigned char *out_mal_buf = NULL; unsigned char *in_buf = NULL; unsigned char *in_mal_buf = NULL; unsigned short buf_len = 512; static void int_cb(void *ctx) { LOGI(" enter int cb"); } void lora_test_thread(void *param) { LOGI("lora_test_thread start"); /*初始化收发buffer,32字节对齐*/ out_mal_buf = (unsigned char *)malloc(QL_SPI_DMA_ADDR_ALIN + buf_len); in_mal_buf = (unsigned char *)malloc(QL_SPI_DMA_ADDR_ALIN + buf_len); out_buf = (unsigned char *)OSI_ALIGN_UP(out_mal_buf, QL_SPI_DMA_ADDR_ALIN); in_buf = (unsigned char *)OSI_ALIGN_UP(in_mal_buf, QL_SPI_DMA_ADDR_ALIN); memset(out_buf, 0x00, buf_len); memset(in_buf, 0x00, buf_len); /*初始化引脚功能*/ LOGI("ql_pin_set_gpio(RST_PIN) err = %d", ql_pin_set_gpio(RST_PIN)); LOGI("ql_pin_set_gpio(INT_PIN) err = %d", ql_pin_set_gpio(INT_PIN)); LOGI("ql_pin_set_gpio(BUSY_PIN) err = %d", ql_pin_set_gpio(BUSY_PIN)); /*开启电平开关*/ LOGI("ql_pin_set_gpio(16) err = %d", ql_pin_set_gpio(SWITCH_PIN)); LOGI("GPIO_19 init err = %d", ql_gpio_init(SWITCH_GPIO, GPIO_OUTPUT, PULL_DOWN, LVL_LOW)); /*初始化busyIO为上拉输入*/ LOGI("BUSY_GPIO GPIO_INPUT err = %d", ql_gpio_set_direction(BUSY_GPIO, GPIO_INPUT)); LOGI("BUSY_GPIO PULL_UP err = %d", ql_gpio_set_pull(BUSY_GPIO, PULL_UP)); /*初始化SPI*/ ql_spi_config_s lora_spi_config = {0}; lora_spi_config.input_mode = QL_SPI_INPUT_TRUE; lora_spi_config.port = QL_SPI_PORT1; lora_spi_config.framesize = 8; lora_spi_config.spiclk = QL_SPI_CLK_6_25MHZ; lora_spi_config.cs_polarity0 = QL_SPI_CS_ACTIVE_LOW; lora_spi_config.cs_polarity1 = QL_SPI_CS_ACTIVE_LOW; lora_spi_config.cpol = QL_SPI_CPOL_LOW; lora_spi_config.cpha = QL_SPI_CPHA_2Edge; lora_spi_config.input_sel = QL_SPI_DI_1; lora_spi_config.transmode = QL_SPI_DMA_IRQ; lora_spi_config.cs = QL_SPI_CS0; lora_spi_config.clk_delay = QL_SPI_CLK_DELAY_0; lora_spi_config.release_flag = QL_SPI_NOT_RELEASE; LOGI("ql_spi_init_ext err = %d", ql_spi_init_ext(lora_spi_config)); //LOGI("SPI1 cs auto err = %d", ql_spi_cs_auto(QL_SPI_PORT1)); LOGI("SPI1 request_sys_clk err = %d", ql_spi_request_sys_clk(QL_SPI_PORT1)); /*复位,rst引脚推挽输出低电平20ms后拉高10ms改为上拉输入*/ ql_delay_us(10000); LOGI("RST_GPIO GPIO_OUTPUT err = %d", ql_gpio_set_direction(RST_GPIO, GPIO_OUTPUT)); LOGI("RST_GPIO LVL_LOW err = %d", ql_gpio_set_level(RST_GPIO, LVL_LOW)); ql_delay_us(20000); LOGI("RST_GPIO LVL_HIGH err = %d", ql_gpio_set_level(RST_GPIO, LVL_HIGH));//官方例程中没有拉高,但是不拉高就测不到波形 ql_delay_us(10000); LOGI("RST_GPIO GPIO_INPUT err = %d", ql_gpio_set_direction(RST_GPIO, GPIO_INPUT)); LOGI("RST_GPIO PULL_UP err = %d", ql_gpio_set_pull(RST_GPIO, PULL_UP)); /*配置中断,int初始化为下拉输入,上升沿中断*/ LOGI("ql_int_register err = %d", ql_int_register(INT_GPIO, EDGE_TRIGGER, DEBOUNCE_EN, EDGE_RISING, PULL_DOWN, int_cb, NULL)); LOGI("ql_int_enable err = %d", ql_int_enable(INT_GPIO)); /*唤醒*/ //llcc68_wakeup(); /*获取同步字*/ LOGI("syncword = 0x%04x",llcc68_get_syncword()); // llcc68_set_standby(STDBY_RC); // llcc68_set_rfswitchmode(true); while (1) { out_buf[0] = RADIO_READ_REGISTER; out_buf[1] = ((LORA_SYNCWORD & 0xff00) >> 8); out_buf[2] = (LORA_SYNCWORD & 0xff); out_buf[3] = 0x00; out_buf[4] = 0x00; out_buf[5] = 0x00; // LOGI("SPI1 request_sys_clk err = %d", ql_spi_request_sys_clk(QL_SPI_PORT1)); wait_on_busy(); LOGI("SPI1 ql_spi_cs_low err = %d",ql_spi_cs_low(QL_SPI_PORT1)); LOGI("SPI1 ql_spi_write_read err = %d",ql_spi_write_read(QL_SPI_PORT1,in_buf,out_buf,6)); LOGI("SPI1 ql_spi_cs_low err = %d",ql_spi_cs_high(QL_SPI_PORT1)); wait_on_busy(); // LOGI("SPI1 ql_spi_release_sys_clk err = %d", ql_spi_release_sys_clk(QL_SPI_PORT1)); LOGI("in_buf 0 = 0x%02x",in_buf[0]); LOGI("in_buf 1 = 0x%02x",in_buf[1]); LOGI("in_buf 2 = 0x%02x",in_buf[2]); LOGI("in_buf 3 = 0x%02x",in_buf[3]); LOGI("in_buf 4 = 0x%02x",in_buf[4]); LOGI("in_buf 5 = 0x%02x",in_buf[5]); memset(out_buf, 0x00, buf_len); memset(in_buf, 0x00, buf_len); ql_rtos_task_sleep_ms(20); } } void lora_test_init(void) { QlOSStatus err = ql_rtos_task_create(&lora_test_task, 4096, APP_PRIORITY_NORMAL, "loratest", lora_test_thread, NULL, 1); if (err != 0) { LOGI("create lora_demo_task failed\n"); } } // 测试引脚功能 // uint8_t func = 255; // ql_pin_get_func(66,&func); // LOGI("66 func = %d",func); // ql_pin_get_func(RST_PIN,&func); // LOGI("RST_PIN func = %d",func); // ql_pin_get_func(INT_PIN,&func); // LOGI("INT_PIN func = %d",func); // ql_pin_get_func(BUSY_PIN,&func); // LOGI("BUSY_PIN func = %d",func); //测试中断引脚方向 // ql_GpioDir dir = 1; // LOGI("ql_gpio_get_direction err = %d",ql_gpio_get_direction(INT_GPIO,&dir)); // LOGI("INT_GPIO dir = %d",dir);