From bce32faac26a88051a7679dda40d115cf43f6519 Mon Sep 17 00:00:00 2001 From: acevest Date: Thu, 9 Jan 2020 20:18:18 +0800 Subject: [PATCH] fix bumblebee irq entry aligment, fix eclic --- arch/risc-v/bumblebee/gcc/riscv_port_c.c | 21 ++++++---- arch/risc-v/bumblebee/gcc/riscv_port_s.S | 10 ++++- arch/risc-v/common/tos_cpu.c | 22 +++++++---- arch/risc-v/rv32i/gcc/port_s.S | 2 +- .../BSP/Inc/EVB_LX_IoT_gd32vf103.h | 10 ++--- board/TencentOS_tiny_EVB_LX/BSP/Inc/usart.h | 2 +- board/TencentOS_tiny_EVB_LX/BSP/Src/usart.c | 20 +++++++++- .../eclipse/tencent_os_mqtt/.cproject | 2 +- .../eclipse/tencent_os_mqtt/main.c | 39 ++++++++++++++++--- examples/tencent_os_mqtt/mqtt_config.h | 14 +++---- examples/tencent_os_mqtt/mqtt_example.c | 4 +- platform/hal/gd/gd32vf1xx/src/tos_hal_uart.c | 8 ++++ 12 files changed, 113 insertions(+), 41 deletions(-) diff --git a/arch/risc-v/bumblebee/gcc/riscv_port_c.c b/arch/risc-v/bumblebee/gcc/riscv_port_c.c index a5173726..5b6b06dd 100644 --- a/arch/risc-v/bumblebee/gcc/riscv_port_c.c +++ b/arch/risc-v/bumblebee/gcc/riscv_port_c.c @@ -75,13 +75,13 @@ static void eclic_set_irq_level(uint32_t source, uint8_t level) { return ; } - uint8_t intctrl_val = eclic_get_intctrl(CLIC_INT_TMR); + uint8_t intctrl_val = eclic_get_intctrl(source); intctrl_val <<= nlbits; intctrl_val >>= nlbits; intctrl_val |= (level << (8- nlbits)); - eclic_set_intctrl(CLIC_INT_TMR, intctrl_val); + eclic_set_intctrl(source, intctrl_val); } static void eclic_set_irq_priority(uint32_t source, uint8_t priority) { @@ -98,29 +98,34 @@ static void eclic_set_irq_priority(uint32_t source, uint8_t priority) { pad >>= cicbits; - uint8_t intctrl_val = eclic_get_intctrl(CLIC_INT_TMR); + uint8_t intctrl_val = eclic_get_intctrl(source); intctrl_val >>= (8 - nlbits); intctrl_val <<= (8 - nlbits); intctrl_val |= (priority << (8 - cicbits)); intctrl_val |= pad; - eclic_set_intctrl(CLIC_INT_TMR, intctrl_val); + eclic_set_intctrl(source, intctrl_val); } -void rv32_exception_entry(); +#define USE_DEFAULT_IRQ_ENTRY 1 + __PORT__ void port_cpu_init() { - __ASM__ __VOLATILE__("csrw mtvec, %0"::"r"(rv32_exception_entry)); + void rv32_exception_entry(); + uint32_t entry = (uint32_t) rv32_exception_entry; + + // 0x03 means use eclic + __ASM__ __VOLATILE__("csrw mtvec, %0"::"r"(entry | 0x03)); // MTVT2: 0x7EC + // set mtvt2.MTVT2EN = 0 needs to clear bit 0 // use mtvec as entry of irq and other trap __ASM__ __VOLATILE__("csrc 0x7EC, 0x1"); eclic_enable_interrupt(CLIC_INT_TMR); eclic_set_irq_level(CLIC_INT_TMR, 0); - } __PORT__ void port_systick_priority_set(uint32_t priority) { @@ -129,7 +134,7 @@ __PORT__ void port_systick_priority_set(uint32_t priority) { __PORT__ void *port_get_irq_vector_table() { - void *base = 0; + unsigned int *base = 0; // MTVT: 0x307 __ASM__ __VOLATILE__("csrr %0, 0x307":"=r"(base)); diff --git a/arch/risc-v/bumblebee/gcc/riscv_port_s.S b/arch/risc-v/bumblebee/gcc/riscv_port_s.S index e1747b09..7385e38a 100644 --- a/arch/risc-v/bumblebee/gcc/riscv_port_s.S +++ b/arch/risc-v/bumblebee/gcc/riscv_port_s.S @@ -15,15 +15,23 @@ * within TencentOS. *---------------------------------------------------------------------------*/ -.global eclic_mtip_handler +//.global eclic_mtip_handler +//.section .text +#if 0 .global irq_entry .global trap_entry .align 2 irq_entry: + nop + nop + nop + nop + j rv32_exception_entry j irq_entry .align 2 trap_entry: j trap_entry +#endif diff --git a/arch/risc-v/common/tos_cpu.c b/arch/risc-v/common/tos_cpu.c index 639b0338..25e9cd81 100644 --- a/arch/risc-v/common/tos_cpu.c +++ b/arch/risc-v/common/tos_cpu.c @@ -159,15 +159,21 @@ void cpu_trap_entry(cpu_data_t cause, cpu_context_t *regs) } } +void eclic_mtip_handler(); void cpu_irq_entry(cpu_data_t irq) { - void (*irq_handler)(); + typedef void (*irq_handler_t)(); - irq_handler = *((void (**)())(port_get_irq_vector_table() + irq*sizeof(cpu_addr_t))); - if((*irq_handler) == 0) { + + irq_handler_t *irq_handler_base = port_get_irq_vector_table(); + + irq_handler_t irq_handler = irq_handler_base[irq]; + + if(irq_handler == 0) { return; } + (*irq_handler)(); } @@ -175,27 +181,27 @@ __API__ uint32_t tos_cpu_clz(uint32_t val) { uint32_t nbr_lead_zeros = 0; - if (!(val & 0XFFFF0000)) { + if (!(val & 0xFFFF0000)) { val <<= 16; nbr_lead_zeros += 16; } - if (!(val & 0XFF000000)) { + if (!(val & 0xFF000000)) { val <<= 8; nbr_lead_zeros += 8; } - if (!(val & 0XF0000000)) { + if (!(val & 0xF0000000)) { val <<= 4; nbr_lead_zeros += 4; } - if (!(val & 0XC0000000)) { + if (!(val & 0xC0000000)) { val <<= 2; nbr_lead_zeros += 2; } - if (!(val & 0X80000000)) { + if (!(val & 0x80000000)) { nbr_lead_zeros += 1; } diff --git a/arch/risc-v/rv32i/gcc/port_s.S b/arch/risc-v/rv32i/gcc/port_s.S index 69868946..f085f874 100644 --- a/arch/risc-v/rv32i/gcc/port_s.S +++ b/arch/risc-v/rv32i/gcc/port_s.S @@ -267,7 +267,7 @@ restore_context: mret -.align 2 +.align 6 .global rv32_exception_entry rv32_exception_entry: addi sp, sp, -128 diff --git a/board/TencentOS_tiny_EVB_LX/BSP/Inc/EVB_LX_IoT_gd32vf103.h b/board/TencentOS_tiny_EVB_LX/BSP/Inc/EVB_LX_IoT_gd32vf103.h index f02805f0..4e0e1860 100644 --- a/board/TencentOS_tiny_EVB_LX/BSP/Inc/EVB_LX_IoT_gd32vf103.h +++ b/board/TencentOS_tiny_EVB_LX/BSP/Inc/EVB_LX_IoT_gd32vf103.h @@ -104,10 +104,10 @@ typedef enum #define COMn 2U -#define EVAL_COM0 USART2 -#define EVAL_COM0_CLK RCU_USART2 -#define EVAL_COM0_TX_PIN GPIO_PIN_2 -#define EVAL_COM0_RX_PIN GPIO_PIN_3 +#define EVAL_COM0 USART0 +#define EVAL_COM0_CLK RCU_USART0 +#define EVAL_COM0_TX_PIN GPIO_PIN_9 +#define EVAL_COM0_RX_PIN GPIO_PIN_10 #define EVAL_COM0_GPIO_PORT GPIOA #define EVAL_COM0_GPIO_CLK RCU_GPIOA @@ -123,7 +123,7 @@ typedef enum #define EVAL_COM3_TX_PIN GPIO_PIN_10 #define EVAL_COM3_RX_PIN GPIO_PIN_11 #define EVAL_COM3_GPIO_PORT GPIOC -#define EVAL_COM3_GPIO_CLK RCU_GPIOC +#define EVAL_COM3_GPIO_CLK RCU_GPIOB #define KEYn 3U diff --git a/board/TencentOS_tiny_EVB_LX/BSP/Inc/usart.h b/board/TencentOS_tiny_EVB_LX/BSP/Inc/usart.h index 68631a13..8e2cf040 100644 --- a/board/TencentOS_tiny_EVB_LX/BSP/Inc/usart.h +++ b/board/TencentOS_tiny_EVB_LX/BSP/Inc/usart.h @@ -11,7 +11,7 @@ #define USART2_GPIO_TX_PIN GPIO_PIN_10 #define USART2_GPIO_RX_PIN GPIO_PIN_11 -#define USART2_GPIO_PORT GPIOC +#define USART2_GPIO_PORT GPIOB void usart0_init(int baud); diff --git a/board/TencentOS_tiny_EVB_LX/BSP/Src/usart.c b/board/TencentOS_tiny_EVB_LX/BSP/Src/usart.c index 284eb060..2be4e63b 100644 --- a/board/TencentOS_tiny_EVB_LX/BSP/Src/usart.c +++ b/board/TencentOS_tiny_EVB_LX/BSP/Src/usart.c @@ -2,6 +2,10 @@ #include "usart.h" void usart0_init(int baud) { + eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1); + eclic_irq_enable(USART0_IRQn, 1, 0); + + /* enable GPIO clock */ rcu_periph_clock_enable(RCU_GPIOA); @@ -25,10 +29,14 @@ void usart0_init(int baud) usart_receive_config(USART0, USART_RECEIVE_ENABLE); usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); usart_enable(USART0); + + usart_interrupt_enable(USART0, USART_INT_RBNE); } void usart1_init(int baud) { + eclic_irq_enable(USART1_IRQn, 1, 0); + /* enable GPIO clock */ rcu_periph_clock_enable(RCU_GPIOA); @@ -52,14 +60,19 @@ void usart1_init(int baud) usart_receive_config(USART1, USART_RECEIVE_ENABLE); usart_transmit_config(USART1, USART_TRANSMIT_ENABLE); usart_enable(USART1); + + usart_interrupt_enable(USART1, USART_INT_RBNE); + } void usart2_init(int baud) { + eclic_irq_enable(USART2_IRQn, 1, 0); + /* enable GPIO clock */ - rcu_periph_clock_enable(RCU_GPIOC); + rcu_periph_clock_enable(RCU_GPIOB); /* enable USART2 clock */ rcu_periph_clock_enable(RCU_USART2); @@ -70,7 +83,7 @@ void usart2_init(int baud) /* connect port to USART0_Rx */ gpio_init(USART2_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, USART2_GPIO_RX_PIN); - gpio_pin_remap_config(GPIO_USART2_FULL_REMAP,ENABLE); + //gpio_pin_remap_config(GPIO_USART2_FULL_REMAP,ENABLE); /* USART1 configure */ usart_deinit(USART2); @@ -83,6 +96,9 @@ void usart2_init(int baud) usart_receive_config(USART2, USART_RECEIVE_ENABLE); usart_transmit_config(USART2, USART_TRANSMIT_ENABLE); usart_enable(USART2); + + + usart_interrupt_enable(USART2, USART_INT_RBNE); } diff --git a/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/.cproject b/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/.cproject index daa6d5b0..565b7ba7 100644 --- a/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/.cproject +++ b/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/.cproject @@ -187,7 +187,7 @@ diff --git a/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/main.c b/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/main.c index ac77b773..1be52bde 100644 --- a/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/main.c +++ b/board/TencentOS_tiny_EVB_LX/eclipse/tencent_os_mqtt/main.c @@ -1,11 +1,38 @@ #include "mcu_init.h" #include "tos_k.h" -#define TASK_SIZE 1024 +#define BLED_TASK_SIZE 1024 +#define WIFI_TASK_SIZE 4096 k_task_t k_task_bled; k_task_t k_task_wifi; -uint8_t k_bled_stk[TASK_SIZE]; -uint8_t k_wifi_stk[TASK_SIZE]; +uint8_t k_bled_stk[BLED_TASK_SIZE]; +uint8_t k_wifi_stk[WIFI_TASK_SIZE]; + + +void USART0_IRQHandler() { +#if 0 + if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)){ + uint8_t data = usart_data_receive(USART0); + printf("%c\n", data); + } +#endif +} + +void USART1_IRQHandler() { + if(RESET != usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)){ + uint8_t data = usart_data_receive(USART1); + //tos_at_uart_input_byte(data); + printf("-%c\n", data); + } +} + +void USART2_IRQHandler() { + if(RESET != usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE)){ + uint8_t data = usart_data_receive(USART2); + tos_at_uart_input_byte(data); + } + //printf("usart2\n"); +} void task_bled(void *pdata) { @@ -33,8 +60,10 @@ void main(void) { tos_knl_init(); - tos_task_create(&k_task_bled, "bled", task_bled, NULL, 5, k_bled_stk, TASK_SIZE, 0); - tos_task_create(&k_task_wifi, "wifi", task_wifi, NULL, 3, k_wifi_stk, TASK_SIZE, 0); + usart0_init(115200); + + tos_task_create(&k_task_bled, "bled", task_bled, NULL, 5, k_bled_stk, BLED_TASK_SIZE, 0); + tos_task_create(&k_task_wifi, "wifi", task_wifi, NULL, 3, k_wifi_stk, WIFI_TASK_SIZE, 0); tos_knl_start(); diff --git a/examples/tencent_os_mqtt/mqtt_config.h b/examples/tencent_os_mqtt/mqtt_config.h index 83fe2543..f8c28e04 100644 --- a/examples/tencent_os_mqtt/mqtt_config.h +++ b/examples/tencent_os_mqtt/mqtt_config.h @@ -3,13 +3,13 @@ #define MQTT_SERVER_IP "111.230.189.156" #define MQTT_SERVER_PORT "1883" -#define MQTT_PRODUCT_ID "RUAP1R610V" -#define MQTT_DEV_NAME "supowang" -#define MQTT_CLIENT_ID "RUAP1R610Vsupowang" -#define MQTT_USR_NAME "RUAP1R610Vsupowang;21010406;12365;4294967295" -#define MQTT_PASSWORD "1371ae55cd1036f088bd7e39d230712bc3c32c40;hmacsha1" -#define MQTT_SUBSCRIBE_TOPIC "RUAP1R610V/supowang/data" -#define MQTT_PUBLISH_TOPIC "RUAP1R610V/supowang/data" +#define MQTT_PRODUCT_ID "Z774IYDTH0" +#define MQTT_DEV_NAME "ChinaMobileStandardBoard" +#define MQTT_CLIENT_ID "Z774IYDTH0ChinaMobileStandardBoard" +#define MQTT_USR_NAME "Z774IYDTH0ChinaMobileStandardBoard;12010126;RJW8Z;1614485713" +#define MQTT_PASSWORD "83c5188935eb67708ca5cf3245f4eb3f19b78004e6a0e7d438206939705fc808;hmacsha256" +#define MQTT_SUBSCRIBE_TOPIC "Z774IYDTH0/ChinaMobileStandardBoard/cmd" +#define MQTT_PUBLISH_TOPIC "Z774IYDTH0/ChinaMobileStandardBoard/gps" #endif diff --git a/examples/tencent_os_mqtt/mqtt_example.c b/examples/tencent_os_mqtt/mqtt_example.c index 2de33566..bfe8fff4 100644 --- a/examples/tencent_os_mqtt/mqtt_example.c +++ b/examples/tencent_os_mqtt/mqtt_example.c @@ -35,8 +35,8 @@ void mqtt_demo(void) sub_opt.topic = MQTT_SUBSCRIBE_TOPIC; #ifdef USE_ESP8266 - esp8266_sal_init(HAL_UART_PORT_0); - esp8266_join_ap("SheldonDai", "srnr6x9xbhmb0"); + esp8266_sal_init(HAL_UART_PORT_1); + esp8266_join_ap("AceiPhone", "12345678"); #endif #ifdef USE_NB_BC35 diff --git a/platform/hal/gd/gd32vf1xx/src/tos_hal_uart.c b/platform/hal/gd/gd32vf1xx/src/tos_hal_uart.c index f407e8d7..93732517 100644 --- a/platform/hal/gd/gd32vf1xx/src/tos_hal_uart.c +++ b/platform/hal/gd/gd32vf1xx/src/tos_hal_uart.c @@ -19,15 +19,23 @@ __API__ int tos_hal_uart_init(hal_uart_t *uart, hal_uart_port_t port) } if (port == HAL_UART_PORT_0) { + uart->private_uart = &huart0; usart0_init(115200); } else if (port == HAL_UART_PORT_1) { + uart->private_uart = &huart1; usart1_init(115200); } else if (port == HAL_UART_PORT_2) { + uart->private_uart = &huart2; usart2_init(115200); } else { return -1; } + + uart->port = ((UART_HandleTypeDef*)(uart->private_uart))->port; + + + return 0; }