[style] format files by clang-format

This commit is contained in:
jzlv 2021-06-20 12:25:46 +08:00
parent 47ce9f871c
commit d427e7fdda
1131 changed files with 7338846 additions and 422042 deletions

View file

@ -26,130 +26,70 @@
#include "bl602_glb.h" #include "bl602_glb.h"
#include "pinmux_config.h" #include "pinmux_config.h"
struct pin_mux_cfg struct pin_mux_cfg {
{
uint8_t pin; uint8_t pin;
uint16_t func; uint16_t func;
}; };
static const struct pin_mux_cfg af_pin_table[] = static const struct pin_mux_cfg af_pin_table[] = {
{ { .pin = GPIO_PIN_0,
{ .func = CONFIG_GPIO0_FUNC },
.pin = GPIO_PIN_0, { .pin = GPIO_PIN_1,
.func = CONFIG_GPIO0_FUNC .func = CONFIG_GPIO1_FUNC },
}, { .pin = GPIO_PIN_2,
{ .func = CONFIG_GPIO2_FUNC },
.pin = GPIO_PIN_1, { .pin = GPIO_PIN_3,
.func = CONFIG_GPIO1_FUNC .func = CONFIG_GPIO3_FUNC },
}, { .pin = GPIO_PIN_4,
{ .func = CONFIG_GPIO4_FUNC },
.pin = GPIO_PIN_2, { .pin = GPIO_PIN_5,
.func = CONFIG_GPIO2_FUNC .func = CONFIG_GPIO5_FUNC },
}, { .pin = GPIO_PIN_6,
{ .func = CONFIG_GPIO6_FUNC },
.pin = GPIO_PIN_3, { .pin = GPIO_PIN_7,
.func = CONFIG_GPIO3_FUNC .func = CONFIG_GPIO7_FUNC },
}, { .pin = GPIO_PIN_8,
{ .func = CONFIG_GPIO8_FUNC },
.pin = GPIO_PIN_4, { .pin = GPIO_PIN_9,
.func = CONFIG_GPIO4_FUNC .func = CONFIG_GPIO9_FUNC },
}, { .pin = GPIO_PIN_10,
{ .func = CONFIG_GPIO10_FUNC },
.pin = GPIO_PIN_5, { .pin = GPIO_PIN_11,
.func = CONFIG_GPIO5_FUNC .func = CONFIG_GPIO11_FUNC },
}, { .pin = GPIO_PIN_12,
{ .func = CONFIG_GPIO12_FUNC },
.pin = GPIO_PIN_6, { .pin = GPIO_PIN_13,
.func = CONFIG_GPIO6_FUNC .func = CONFIG_GPIO13_FUNC },
}, { .pin = GPIO_PIN_14,
{ .func = CONFIG_GPIO14_FUNC },
.pin = GPIO_PIN_7, { .pin = GPIO_PIN_15,
.func = CONFIG_GPIO7_FUNC .func = CONFIG_GPIO15_FUNC },
}, { .pin = GPIO_PIN_16,
{ .func = CONFIG_GPIO16_FUNC },
.pin = GPIO_PIN_8, { .pin = GPIO_PIN_17,
.func = CONFIG_GPIO8_FUNC .func = CONFIG_GPIO17_FUNC },
}, { .pin = GPIO_PIN_18,
{ .func = CONFIG_GPIO18_FUNC },
.pin = GPIO_PIN_9, { .pin = GPIO_PIN_19,
.func = CONFIG_GPIO9_FUNC .func = CONFIG_GPIO19_FUNC },
}, { .pin = GPIO_PIN_20,
{ .func = CONFIG_GPIO20_FUNC },
.pin = GPIO_PIN_10, { .pin = GPIO_PIN_21,
.func = CONFIG_GPIO10_FUNC .func = CONFIG_GPIO21_FUNC },
}, { .pin = GPIO_PIN_22,
{ .func = CONFIG_GPIO22_FUNC },
.pin = GPIO_PIN_11, { .pin = GPIO_PIN_23,
.func = CONFIG_GPIO11_FUNC .func = CONFIG_GPIO23_FUNC },
}, { .pin = GPIO_PIN_24,
{ .func = CONFIG_GPIO24_FUNC },
.pin = GPIO_PIN_12, { .pin = GPIO_PIN_25,
.func = CONFIG_GPIO12_FUNC .func = CONFIG_GPIO25_FUNC },
}, { .pin = GPIO_PIN_26,
{ .func = CONFIG_GPIO26_FUNC },
.pin = GPIO_PIN_13, { .pin = GPIO_PIN_27,
.func = CONFIG_GPIO13_FUNC .func = CONFIG_GPIO27_FUNC },
}, { .pin = GPIO_PIN_28,
{ .func = CONFIG_GPIO28_FUNC },
.pin = GPIO_PIN_14,
.func = CONFIG_GPIO14_FUNC
},
{
.pin = GPIO_PIN_15,
.func = CONFIG_GPIO15_FUNC
},
{
.pin = GPIO_PIN_16,
.func = CONFIG_GPIO16_FUNC
},
{
.pin = GPIO_PIN_17,
.func = CONFIG_GPIO17_FUNC
},
{
.pin = GPIO_PIN_18,
.func = CONFIG_GPIO18_FUNC
},
{
.pin = GPIO_PIN_19,
.func = CONFIG_GPIO19_FUNC
},
{
.pin = GPIO_PIN_20,
.func = CONFIG_GPIO20_FUNC
},
{
.pin = GPIO_PIN_21,
.func = CONFIG_GPIO21_FUNC
},
{
.pin = GPIO_PIN_22,
.func = CONFIG_GPIO22_FUNC
},
{
.pin = GPIO_PIN_23,
.func = CONFIG_GPIO23_FUNC
},
{
.pin = GPIO_PIN_24,
.func = CONFIG_GPIO24_FUNC
},
{
.pin = GPIO_PIN_25,
.func = CONFIG_GPIO25_FUNC
},
{
.pin = GPIO_PIN_26,
.func = CONFIG_GPIO26_FUNC
},
{
.pin = GPIO_PIN_27,
.func = CONFIG_GPIO27_FUNC
},
{
.pin = GPIO_PIN_28,
.func = CONFIG_GPIO28_FUNC
},
}; };
static void board_pin_mux_init(void) static void board_pin_mux_init(void)
@ -159,19 +99,15 @@ static void board_pin_mux_init(void)
gpio_cfg.drive = 0; gpio_cfg.drive = 0;
gpio_cfg.smtCtrl = 1; gpio_cfg.smtCtrl = 1;
for (int i = 0; i < sizeof(af_pin_table)/sizeof(af_pin_table[0]); i++) for (int i = 0; i < sizeof(af_pin_table) / sizeof(af_pin_table[0]); i++) {
{
gpio_cfg.gpioMode = GPIO_MODE_AF; gpio_cfg.gpioMode = GPIO_MODE_AF;
gpio_cfg.pullType = GPIO_PULL_UP; gpio_cfg.pullType = GPIO_PULL_UP;
gpio_cfg.gpioPin = af_pin_table[i].pin; gpio_cfg.gpioPin = af_pin_table[i].pin;
gpio_cfg.gpioFun = af_pin_table[i].func; gpio_cfg.gpioFun = af_pin_table[i].func;
if(af_pin_table[i].func == GPIO_FUN_UNUSED) if (af_pin_table[i].func == GPIO_FUN_UNUSED) {
{
continue; continue;
} } else if (af_pin_table[i].func == GPIO_FUN_PWM) {
else if(af_pin_table[i].func == GPIO_FUN_PWM)
{
gpio_cfg.pullType = GPIO_PULL_DOWN; gpio_cfg.pullType = GPIO_PULL_DOWN;
} }
// else if((af_pin_table[i].func == GPIO_FUN_DAC)|| (af_pin_table[i].func == GPIO_FUN_ADC)) // else if((af_pin_table[i].func == GPIO_FUN_DAC)|| (af_pin_table[i].func == GPIO_FUN_ADC))
@ -179,15 +115,14 @@ static void board_pin_mux_init(void)
// gpio_cfg.gpioFun = GPIO_FUN_ANALOG; // gpio_cfg.gpioFun = GPIO_FUN_ANALOG;
// gpio_cfg.gpioMode = GPIO_MODE_ANALOG; // gpio_cfg.gpioMode = GPIO_MODE_ANALOG;
// } // }
else if((af_pin_table[i].func & 0x70) == 0x70) else if ((af_pin_table[i].func & 0x70) == 0x70) {
{
gpio_cfg.gpioFun = GPIO_FUN_UART; gpio_cfg.gpioFun = GPIO_FUN_UART;
uint8_t sig = af_pin_table[i].func & 0x07; uint8_t sig = af_pin_table[i].func & 0x07;
GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig); GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig);
} }
GLB_GPIO_Init(&gpio_cfg); GLB_GPIO_Init(&gpio_cfg);
} }
} }
static void board_clock_init(void) static void board_clock_init(void)
@ -198,6 +133,7 @@ static void board_clock_init(void)
void bl_show_info(void) void bl_show_info(void)
{ {
MSG("\r\n");
MSG(" ____ __ __ _ _ _ \r\n"); MSG(" ____ __ __ _ _ _ \r\n");
MSG(" | _ \\ / _|/ _| | | | | | | \r\n"); MSG(" | _ \\ / _|/ _| | | | | | | \r\n");
MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n"); MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n");

View file

@ -39,7 +39,6 @@
#define BSP_USING_DMA0_CH6 #define BSP_USING_DMA0_CH6
#define BSP_USING_DMA0_CH7 #define BSP_USING_DMA0_CH7
/* PERIPHERAL CONFIG */ /* PERIPHERAL CONFIG */
#if defined(BSP_USING_ADC0) #if defined(BSP_USING_ADC0)
#ifndef ADC0_CONFIG #ifndef ADC0_CONFIG
@ -187,7 +186,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH3) #if defined(BSP_USING_DMA0_CH3)
#ifndef DMA0_CH3_CONFIG #ifndef DMA0_CH3_CONFIG
#define DMA0_CH3_CONFIG \ #define DMA0_CH3_CONFIG \
@ -220,7 +218,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH5) #if defined(BSP_USING_DMA0_CH5)
#ifndef DMA0_CH5_CONFIG #ifndef DMA0_CH5_CONFIG
#define DMA0_CH5_CONFIG \ #define DMA0_CH5_CONFIG \
@ -253,7 +250,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH7) #if defined(BSP_USING_DMA0_CH7)
#ifndef DMA0_CH7_CONFIG #ifndef DMA0_CH7_CONFIG
#define DMA0_CH7_CONFIG \ #define DMA0_CH7_CONFIG \
@ -270,8 +266,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_I2C0) #if defined(BSP_USING_I2C0)
#ifndef I2C0_CONFIG #ifndef I2C0_CONFIG
#define I2C0_CONFIG \ #define I2C0_CONFIG \
@ -283,7 +277,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_TIMER_CH0) #if defined(BSP_USING_TIMER_CH0)
#ifndef TIMER_CH0_CONFIG #ifndef TIMER_CH0_CONFIG
#define TIMER_CH0_CONFIG \ #define TIMER_CH0_CONFIG \

View file

@ -3,166 +3,88 @@
#include "bl702_glb.h" #include "bl702_glb.h"
#include "pinmux_config.h" #include "pinmux_config.h"
struct pin_mux_cfg struct pin_mux_cfg {
{
uint8_t pin; uint8_t pin;
uint16_t func; uint16_t func;
}; };
static const struct pin_mux_cfg af_pin_table[] = static const struct pin_mux_cfg af_pin_table[] = {
{ { .pin = GPIO_PIN_0,
{ .func = CONFIG_GPIO0_FUNC },
.pin = GPIO_PIN_0, { .pin = GPIO_PIN_1,
.func = CONFIG_GPIO0_FUNC .func = CONFIG_GPIO1_FUNC },
}, { .pin = GPIO_PIN_2,
{ .func = CONFIG_GPIO2_FUNC },
.pin = GPIO_PIN_1, { .pin = GPIO_PIN_3,
.func = CONFIG_GPIO1_FUNC .func = CONFIG_GPIO3_FUNC },
}, { .pin = GPIO_PIN_4,
{ .func = CONFIG_GPIO4_FUNC },
.pin = GPIO_PIN_2, { .pin = GPIO_PIN_5,
.func = CONFIG_GPIO2_FUNC .func = CONFIG_GPIO5_FUNC },
}, { .pin = GPIO_PIN_6,
{ .func = CONFIG_GPIO6_FUNC },
.pin = GPIO_PIN_3, { .pin = GPIO_PIN_7,
.func = CONFIG_GPIO3_FUNC .func = CONFIG_GPIO7_FUNC },
}, { .pin = GPIO_PIN_8,
{ .func = CONFIG_GPIO8_FUNC },
.pin = GPIO_PIN_4, { .pin = GPIO_PIN_9,
.func = CONFIG_GPIO4_FUNC .func = CONFIG_GPIO9_FUNC },
}, { .pin = GPIO_PIN_10,
{ .func = CONFIG_GPIO10_FUNC },
.pin = GPIO_PIN_5, { .pin = GPIO_PIN_11,
.func = CONFIG_GPIO5_FUNC .func = CONFIG_GPIO11_FUNC },
}, { .pin = GPIO_PIN_12,
{ .func = CONFIG_GPIO12_FUNC },
.pin = GPIO_PIN_6, { .pin = GPIO_PIN_13,
.func = CONFIG_GPIO6_FUNC .func = CONFIG_GPIO13_FUNC },
}, { .pin = GPIO_PIN_14,
{ .func = CONFIG_GPIO14_FUNC },
.pin = GPIO_PIN_7, { .pin = GPIO_PIN_15,
.func = CONFIG_GPIO7_FUNC .func = CONFIG_GPIO15_FUNC },
}, { .pin = GPIO_PIN_16,
{ .func = CONFIG_GPIO16_FUNC },
.pin = GPIO_PIN_8, { .pin = GPIO_PIN_17,
.func = CONFIG_GPIO8_FUNC .func = CONFIG_GPIO17_FUNC },
}, { .pin = GPIO_PIN_18,
{ .func = CONFIG_GPIO18_FUNC },
.pin = GPIO_PIN_9, { .pin = GPIO_PIN_19,
.func = CONFIG_GPIO9_FUNC .func = CONFIG_GPIO19_FUNC },
}, { .pin = GPIO_PIN_20,
{ .func = CONFIG_GPIO20_FUNC },
.pin = GPIO_PIN_10, { .pin = GPIO_PIN_21,
.func = CONFIG_GPIO10_FUNC .func = CONFIG_GPIO21_FUNC },
}, { .pin = GPIO_PIN_22,
{ .func = CONFIG_GPIO22_FUNC },
.pin = GPIO_PIN_11, { .pin = GPIO_PIN_23,
.func = CONFIG_GPIO11_FUNC .func = CONFIG_GPIO23_FUNC },
}, { .pin = GPIO_PIN_24,
{ .func = CONFIG_GPIO24_FUNC },
.pin = GPIO_PIN_12, { .pin = GPIO_PIN_25,
.func = CONFIG_GPIO12_FUNC .func = CONFIG_GPIO25_FUNC },
}, { .pin = GPIO_PIN_26,
{ .func = CONFIG_GPIO26_FUNC },
.pin = GPIO_PIN_13, { .pin = GPIO_PIN_27,
.func = CONFIG_GPIO13_FUNC .func = CONFIG_GPIO27_FUNC },
}, { .pin = GPIO_PIN_28,
{ .func = CONFIG_GPIO28_FUNC },
.pin = GPIO_PIN_14, { .pin = GPIO_PIN_29,
.func = CONFIG_GPIO14_FUNC .func = CONFIG_GPIO29_FUNC },
}, { .pin = GPIO_PIN_30,
{ .func = CONFIG_GPIO30_FUNC },
.pin = GPIO_PIN_15, { .pin = GPIO_PIN_31,
.func = CONFIG_GPIO15_FUNC .func = CONFIG_GPIO31_FUNC },
}, { .pin = GPIO_PIN_32,
{ .func = CONFIG_GPIO32_FUNC },
.pin = GPIO_PIN_16, { .pin = GPIO_PIN_33,
.func = CONFIG_GPIO16_FUNC .func = CONFIG_GPIO33_FUNC },
}, { .pin = GPIO_PIN_34,
{ .func = CONFIG_GPIO34_FUNC },
.pin = GPIO_PIN_17, { .pin = GPIO_PIN_35,
.func = CONFIG_GPIO17_FUNC .func = CONFIG_GPIO35_FUNC },
}, { .pin = GPIO_PIN_36,
{ .func = CONFIG_GPIO36_FUNC },
.pin = GPIO_PIN_18, { .pin = GPIO_PIN_37,
.func = CONFIG_GPIO18_FUNC .func = CONFIG_GPIO37_FUNC }
},
{
.pin = GPIO_PIN_19,
.func = CONFIG_GPIO19_FUNC
},
{
.pin = GPIO_PIN_20,
.func = CONFIG_GPIO20_FUNC
},
{
.pin = GPIO_PIN_21,
.func = CONFIG_GPIO21_FUNC
},
{
.pin = GPIO_PIN_22,
.func = CONFIG_GPIO22_FUNC
},
{
.pin = GPIO_PIN_23,
.func = CONFIG_GPIO23_FUNC
},
{
.pin = GPIO_PIN_24,
.func = CONFIG_GPIO24_FUNC
},
{
.pin = GPIO_PIN_25,
.func = CONFIG_GPIO25_FUNC
},
{
.pin = GPIO_PIN_26,
.func = CONFIG_GPIO26_FUNC
},
{
.pin = GPIO_PIN_27,
.func = CONFIG_GPIO27_FUNC
},
{
.pin = GPIO_PIN_28,
.func = CONFIG_GPIO28_FUNC
},
{
.pin = GPIO_PIN_29,
.func = CONFIG_GPIO29_FUNC
},
{
.pin = GPIO_PIN_30,
.func = CONFIG_GPIO30_FUNC
},
{
.pin = GPIO_PIN_31,
.func = CONFIG_GPIO31_FUNC
},
{
.pin = GPIO_PIN_32,
.func = CONFIG_GPIO32_FUNC
},
{
.pin = GPIO_PIN_33,
.func = CONFIG_GPIO33_FUNC
},
{
.pin = GPIO_PIN_34,
.func = CONFIG_GPIO34_FUNC
},
{
.pin = GPIO_PIN_35,
.func = CONFIG_GPIO35_FUNC
},
{
.pin = GPIO_PIN_36,
.func = CONFIG_GPIO36_FUNC
},
{
.pin = GPIO_PIN_37,
.func = CONFIG_GPIO37_FUNC
}
}; };
@ -173,43 +95,32 @@ static void board_pin_mux_init(void)
gpio_cfg.drive = 0; gpio_cfg.drive = 0;
gpio_cfg.smtCtrl = 1; gpio_cfg.smtCtrl = 1;
for (int i = 0; i < sizeof(af_pin_table)/sizeof(af_pin_table[0]); i++) for (int i = 0; i < sizeof(af_pin_table) / sizeof(af_pin_table[0]); i++) {
{
gpio_cfg.gpioMode = GPIO_MODE_AF; gpio_cfg.gpioMode = GPIO_MODE_AF;
gpio_cfg.pullType = GPIO_PULL_UP; gpio_cfg.pullType = GPIO_PULL_UP;
gpio_cfg.gpioPin = af_pin_table[i].pin; gpio_cfg.gpioPin = af_pin_table[i].pin;
gpio_cfg.gpioFun = af_pin_table[i].func; gpio_cfg.gpioFun = af_pin_table[i].func;
if(af_pin_table[i].func == GPIO_FUN_UNUSED) if (af_pin_table[i].func == GPIO_FUN_UNUSED) {
{
continue; continue;
} } else if (af_pin_table[i].func == GPIO_FUN_PWM) {
else if(af_pin_table[i].func == GPIO_FUN_PWM)
{
gpio_cfg.pullType = GPIO_PULL_DOWN; gpio_cfg.pullType = GPIO_PULL_DOWN;
} } else if ((af_pin_table[i].func == GPIO_FUN_DAC) || (af_pin_table[i].func == GPIO_FUN_ADC)) {
else if((af_pin_table[i].func == GPIO_FUN_DAC)|| (af_pin_table[i].func == GPIO_FUN_ADC))
{
gpio_cfg.gpioFun = GPIO_FUN_ANALOG; gpio_cfg.gpioFun = GPIO_FUN_ANALOG;
gpio_cfg.gpioMode = GPIO_MODE_ANALOG; gpio_cfg.gpioMode = GPIO_MODE_ANALOG;
} } else if ((af_pin_table[i].func & 0x70) == 0x70) {
else if((af_pin_table[i].func & 0x70) == 0x70)
{
gpio_cfg.gpioFun = GPIO_FUN_UART; gpio_cfg.gpioFun = GPIO_FUN_UART;
uint8_t sig = af_pin_table[i].func & 0x07; uint8_t sig = af_pin_table[i].func & 0x07;
if (gpio_cfg.gpioPin > 31) if (gpio_cfg.gpioPin > 31) {
{
GLB_UART_Fun_Sel(((gpio_cfg.gpioPin - 9) % 8), sig); GLB_UART_Fun_Sel(((gpio_cfg.gpioPin - 9) % 8), sig);
} } else {
else
{
GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig); GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig);
} }
} }
GLB_GPIO_Init(&gpio_cfg); GLB_GPIO_Init(&gpio_cfg);
} }
} }
static void board_clock_init(void) static void board_clock_init(void)
@ -220,6 +131,7 @@ static void board_clock_init(void)
void bl_show_info(void) void bl_show_info(void)
{ {
MSG("\r\n");
MSG(" ____ __ __ _ _ _ \r\n"); MSG(" ____ __ __ _ _ _ \r\n");
MSG(" | _ \\ / _|/ _| | | | | | | \r\n"); MSG(" | _ \\ / _|/ _| | | | | | | \r\n");
MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n"); MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n");

View file

@ -44,6 +44,10 @@
#define BSP_SPI_CLOCK_SOURCE ROOT_CLOCK_SOURCE_BCLK #define BSP_SPI_CLOCK_SOURCE ROOT_CLOCK_SOURCE_BCLK
#define BSP_SPI_CLOCK_DIV 0 #define BSP_SPI_CLOCK_DIV 0
#endif #endif
#if defined(BSP_USING_PWM_CH0) || defined(BSP_USING_PWM_CH1) || defined(BSP_USING_PWM_CH2) || defined(BSP_USING_PWM_CH3) || defined(BSP_USING_PWM_CH4) || defined(BSP_USING_PWM_CH5)
#define BSP_PWM_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK
#define BSP_PWM_CLOCK_DIV 0
#endif
#if defined(BSP_USING_IR) #if defined(BSP_USING_IR)
#define BSP_IR_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK #define BSP_IR_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK
#define BSP_IR_CLOCK_DIV 0 #define BSP_IR_CLOCK_DIV 0

View file

@ -28,7 +28,6 @@
#define BSP_USING_DMA0_CH6 #define BSP_USING_DMA0_CH6
#define BSP_USING_DMA0_CH7 #define BSP_USING_DMA0_CH7
/* PERIPHERAL CONFIG */ /* PERIPHERAL CONFIG */
#if defined(BSP_USING_ADC0) #if defined(BSP_USING_ADC0)
#ifndef ADC0_CONFIG #ifndef ADC0_CONFIG
@ -100,13 +99,58 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_PWM_CH0)
#ifndef PWM_CH0_CONFIG
#define PWM_CH0_CONFIG \
{ \
.ch = 0, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH1)
#ifndef PWM_CH1_CONFIG
#define PWM_CH1_CONFIG \
{ \
.ch = 1, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH2) #if defined(BSP_USING_PWM_CH2)
#ifndef PWM_CH2_CONFIG #ifndef PWM_CH2_CONFIG
#define PWM_CH2_CONFIG \ #define PWM_CH2_CONFIG \
{ \ { \
.ch = 2, \ .ch = 2, \
.frequency = 1000000, \ .polarity_invert_mode = DISABLE, \
.dutycycle = 0, \ .period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH3)
#ifndef PWM_CH3_CONFIG
#define PWM_CH3_CONFIG \
{ \
.ch = 3, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
} }
#endif #endif
#endif #endif
@ -175,7 +219,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH3) #if defined(BSP_USING_DMA0_CH3)
#ifndef DMA0_CH3_CONFIG #ifndef DMA0_CH3_CONFIG
#define DMA0_CH3_CONFIG \ #define DMA0_CH3_CONFIG \
@ -208,7 +251,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH5) #if defined(BSP_USING_DMA0_CH5)
#ifndef DMA0_CH5_CONFIG #ifndef DMA0_CH5_CONFIG
#define DMA0_CH5_CONFIG \ #define DMA0_CH5_CONFIG \
@ -241,7 +283,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH7) #if defined(BSP_USING_DMA0_CH7)
#ifndef DMA0_CH7_CONFIG #ifndef DMA0_CH7_CONFIG
#define DMA0_CH7_CONFIG \ #define DMA0_CH7_CONFIG \
@ -258,8 +299,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_I2C0) #if defined(BSP_USING_I2C0)
#ifndef I2C0_CONFIG #ifndef I2C0_CONFIG
#define I2C0_CONFIG \ #define I2C0_CONFIG \
@ -271,7 +310,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_TIMER_CH0) #if defined(BSP_USING_TIMER_CH0)
#ifndef TIMER_CH0_CONFIG #ifndef TIMER_CH0_CONFIG
#define TIMER_CH0_CONFIG \ #define TIMER_CH0_CONFIG \

View file

@ -26,166 +26,88 @@
#include "bl702_glb.h" #include "bl702_glb.h"
#include "pinmux_config.h" #include "pinmux_config.h"
struct pin_mux_cfg struct pin_mux_cfg {
{
uint8_t pin; uint8_t pin;
uint16_t func; uint16_t func;
}; };
static const struct pin_mux_cfg af_pin_table[] = static const struct pin_mux_cfg af_pin_table[] = {
{ { .pin = GPIO_PIN_0,
{ .func = CONFIG_GPIO0_FUNC },
.pin = GPIO_PIN_0, { .pin = GPIO_PIN_1,
.func = CONFIG_GPIO0_FUNC .func = CONFIG_GPIO1_FUNC },
}, { .pin = GPIO_PIN_2,
{ .func = CONFIG_GPIO2_FUNC },
.pin = GPIO_PIN_1, { .pin = GPIO_PIN_3,
.func = CONFIG_GPIO1_FUNC .func = CONFIG_GPIO3_FUNC },
}, { .pin = GPIO_PIN_4,
{ .func = CONFIG_GPIO4_FUNC },
.pin = GPIO_PIN_2, { .pin = GPIO_PIN_5,
.func = CONFIG_GPIO2_FUNC .func = CONFIG_GPIO5_FUNC },
}, { .pin = GPIO_PIN_6,
{ .func = CONFIG_GPIO6_FUNC },
.pin = GPIO_PIN_3, { .pin = GPIO_PIN_7,
.func = CONFIG_GPIO3_FUNC .func = CONFIG_GPIO7_FUNC },
}, { .pin = GPIO_PIN_8,
{ .func = CONFIG_GPIO8_FUNC },
.pin = GPIO_PIN_4, { .pin = GPIO_PIN_9,
.func = CONFIG_GPIO4_FUNC .func = CONFIG_GPIO9_FUNC },
}, { .pin = GPIO_PIN_10,
{ .func = CONFIG_GPIO10_FUNC },
.pin = GPIO_PIN_5, { .pin = GPIO_PIN_11,
.func = CONFIG_GPIO5_FUNC .func = CONFIG_GPIO11_FUNC },
}, { .pin = GPIO_PIN_12,
{ .func = CONFIG_GPIO12_FUNC },
.pin = GPIO_PIN_6, { .pin = GPIO_PIN_13,
.func = CONFIG_GPIO6_FUNC .func = CONFIG_GPIO13_FUNC },
}, { .pin = GPIO_PIN_14,
{ .func = CONFIG_GPIO14_FUNC },
.pin = GPIO_PIN_7, { .pin = GPIO_PIN_15,
.func = CONFIG_GPIO7_FUNC .func = CONFIG_GPIO15_FUNC },
}, { .pin = GPIO_PIN_16,
{ .func = CONFIG_GPIO16_FUNC },
.pin = GPIO_PIN_8, { .pin = GPIO_PIN_17,
.func = CONFIG_GPIO8_FUNC .func = CONFIG_GPIO17_FUNC },
}, { .pin = GPIO_PIN_18,
{ .func = CONFIG_GPIO18_FUNC },
.pin = GPIO_PIN_9, { .pin = GPIO_PIN_19,
.func = CONFIG_GPIO9_FUNC .func = CONFIG_GPIO19_FUNC },
}, { .pin = GPIO_PIN_20,
{ .func = CONFIG_GPIO20_FUNC },
.pin = GPIO_PIN_10, { .pin = GPIO_PIN_21,
.func = CONFIG_GPIO10_FUNC .func = CONFIG_GPIO21_FUNC },
}, { .pin = GPIO_PIN_22,
{ .func = CONFIG_GPIO22_FUNC },
.pin = GPIO_PIN_11, { .pin = GPIO_PIN_23,
.func = CONFIG_GPIO11_FUNC .func = CONFIG_GPIO23_FUNC },
}, { .pin = GPIO_PIN_24,
{ .func = CONFIG_GPIO24_FUNC },
.pin = GPIO_PIN_12, { .pin = GPIO_PIN_25,
.func = CONFIG_GPIO12_FUNC .func = CONFIG_GPIO25_FUNC },
}, { .pin = GPIO_PIN_26,
{ .func = CONFIG_GPIO26_FUNC },
.pin = GPIO_PIN_13, { .pin = GPIO_PIN_27,
.func = CONFIG_GPIO13_FUNC .func = CONFIG_GPIO27_FUNC },
}, { .pin = GPIO_PIN_28,
{ .func = CONFIG_GPIO28_FUNC },
.pin = GPIO_PIN_14, { .pin = GPIO_PIN_29,
.func = CONFIG_GPIO14_FUNC .func = CONFIG_GPIO29_FUNC },
}, { .pin = GPIO_PIN_30,
{ .func = CONFIG_GPIO30_FUNC },
.pin = GPIO_PIN_15, { .pin = GPIO_PIN_31,
.func = CONFIG_GPIO15_FUNC .func = CONFIG_GPIO31_FUNC },
}, { .pin = GPIO_PIN_32,
{ .func = CONFIG_GPIO32_FUNC },
.pin = GPIO_PIN_16, { .pin = GPIO_PIN_33,
.func = CONFIG_GPIO16_FUNC .func = CONFIG_GPIO33_FUNC },
}, { .pin = GPIO_PIN_34,
{ .func = CONFIG_GPIO34_FUNC },
.pin = GPIO_PIN_17, { .pin = GPIO_PIN_35,
.func = CONFIG_GPIO17_FUNC .func = CONFIG_GPIO35_FUNC },
}, { .pin = GPIO_PIN_36,
{ .func = CONFIG_GPIO36_FUNC },
.pin = GPIO_PIN_18, { .pin = GPIO_PIN_37,
.func = CONFIG_GPIO18_FUNC .func = CONFIG_GPIO37_FUNC }
},
{
.pin = GPIO_PIN_19,
.func = CONFIG_GPIO19_FUNC
},
{
.pin = GPIO_PIN_20,
.func = CONFIG_GPIO20_FUNC
},
{
.pin = GPIO_PIN_21,
.func = CONFIG_GPIO21_FUNC
},
{
.pin = GPIO_PIN_22,
.func = CONFIG_GPIO22_FUNC
},
{
.pin = GPIO_PIN_23,
.func = CONFIG_GPIO23_FUNC
},
{
.pin = GPIO_PIN_24,
.func = CONFIG_GPIO24_FUNC
},
{
.pin = GPIO_PIN_25,
.func = CONFIG_GPIO25_FUNC
},
{
.pin = GPIO_PIN_26,
.func = CONFIG_GPIO26_FUNC
},
{
.pin = GPIO_PIN_27,
.func = CONFIG_GPIO27_FUNC
},
{
.pin = GPIO_PIN_28,
.func = CONFIG_GPIO28_FUNC
},
{
.pin = GPIO_PIN_29,
.func = CONFIG_GPIO29_FUNC
},
{
.pin = GPIO_PIN_30,
.func = CONFIG_GPIO30_FUNC
},
{
.pin = GPIO_PIN_31,
.func = CONFIG_GPIO31_FUNC
},
{
.pin = GPIO_PIN_32,
.func = CONFIG_GPIO32_FUNC
},
{
.pin = GPIO_PIN_33,
.func = CONFIG_GPIO33_FUNC
},
{
.pin = GPIO_PIN_34,
.func = CONFIG_GPIO34_FUNC
},
{
.pin = GPIO_PIN_35,
.func = CONFIG_GPIO35_FUNC
},
{
.pin = GPIO_PIN_36,
.func = CONFIG_GPIO36_FUNC
},
{
.pin = GPIO_PIN_37,
.func = CONFIG_GPIO37_FUNC
}
}; };
@ -196,43 +118,32 @@ static void board_pin_mux_init(void)
gpio_cfg.drive = 0; gpio_cfg.drive = 0;
gpio_cfg.smtCtrl = 1; gpio_cfg.smtCtrl = 1;
for (int i = 0; i < sizeof(af_pin_table)/sizeof(af_pin_table[0]); i++) for (int i = 0; i < sizeof(af_pin_table) / sizeof(af_pin_table[0]); i++) {
{
gpio_cfg.gpioMode = GPIO_MODE_AF; gpio_cfg.gpioMode = GPIO_MODE_AF;
gpio_cfg.pullType = GPIO_PULL_UP; gpio_cfg.pullType = GPIO_PULL_UP;
gpio_cfg.gpioPin = af_pin_table[i].pin; gpio_cfg.gpioPin = af_pin_table[i].pin;
gpio_cfg.gpioFun = af_pin_table[i].func; gpio_cfg.gpioFun = af_pin_table[i].func;
if(af_pin_table[i].func == GPIO_FUN_UNUSED) if (af_pin_table[i].func == GPIO_FUN_UNUSED) {
{
continue; continue;
} } else if (af_pin_table[i].func == GPIO_FUN_PWM) {
else if(af_pin_table[i].func == GPIO_FUN_PWM)
{
gpio_cfg.pullType = GPIO_PULL_DOWN; gpio_cfg.pullType = GPIO_PULL_DOWN;
} } else if ((af_pin_table[i].func == GPIO_FUN_DAC) || (af_pin_table[i].func == GPIO_FUN_ADC)) {
else if((af_pin_table[i].func == GPIO_FUN_DAC)|| (af_pin_table[i].func == GPIO_FUN_ADC))
{
gpio_cfg.gpioFun = GPIO_FUN_ANALOG; gpio_cfg.gpioFun = GPIO_FUN_ANALOG;
gpio_cfg.gpioMode = GPIO_MODE_ANALOG; gpio_cfg.gpioMode = GPIO_MODE_ANALOG;
} } else if ((af_pin_table[i].func & 0x70) == 0x70) {
else if((af_pin_table[i].func & 0x70) == 0x70)
{
gpio_cfg.gpioFun = GPIO_FUN_UART; gpio_cfg.gpioFun = GPIO_FUN_UART;
uint8_t sig = af_pin_table[i].func & 0x07; uint8_t sig = af_pin_table[i].func & 0x07;
if (gpio_cfg.gpioPin > 31) if (gpio_cfg.gpioPin > 31) {
{
GLB_UART_Fun_Sel(((gpio_cfg.gpioPin - 9) % 8), sig); GLB_UART_Fun_Sel(((gpio_cfg.gpioPin - 9) % 8), sig);
} } else {
else
{
GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig); GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig);
} }
} }
GLB_GPIO_Init(&gpio_cfg); GLB_GPIO_Init(&gpio_cfg);
} }
} }
static void board_clock_init(void) static void board_clock_init(void)
@ -243,6 +154,7 @@ static void board_clock_init(void)
void bl_show_info(void) void bl_show_info(void)
{ {
MSG("\r\n");
MSG(" ____ __ __ _ _ _ \r\n"); MSG(" ____ __ __ _ _ _ \r\n");
MSG(" | _ \\ / _|/ _| | | | | | | \r\n"); MSG(" | _ \\ / _|/ _| | | | | | | \r\n");
MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n"); MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n");

View file

@ -44,6 +44,10 @@
#define BSP_SPI_CLOCK_SOURCE ROOT_CLOCK_SOURCE_BCLK #define BSP_SPI_CLOCK_SOURCE ROOT_CLOCK_SOURCE_BCLK
#define BSP_SPI_CLOCK_DIV 0 #define BSP_SPI_CLOCK_DIV 0
#endif #endif
#if defined(BSP_USING_PWM_CH0) || defined(BSP_USING_PWM_CH1) || defined(BSP_USING_PWM_CH2) || defined(BSP_USING_PWM_CH3) || defined(BSP_USING_PWM_CH4) || defined(BSP_USING_PWM_CH5)
#define BSP_PWM_CLOCK_SOURCE ROOT_CLOCK_SOURCE_RC_32K
#define BSP_PWM_CLOCK_DIV 32
#endif
#if defined(BSP_USING_IR) #if defined(BSP_USING_IR)
#define BSP_IR_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK #define BSP_IR_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK
#define BSP_IR_CLOCK_DIV 0 #define BSP_IR_CLOCK_DIV 0

View file

@ -33,7 +33,10 @@
#define BSP_USING_I2C0 #define BSP_USING_I2C0
#define BSP_USING_I2S0 #define BSP_USING_I2S0
#define BSP_USING_USB #define BSP_USING_USB
#define BSP_USING_PWM_CH0
#define BSP_USING_PWM_CH1
#define BSP_USING_PWM_CH2 #define BSP_USING_PWM_CH2
#define BSP_USING_PWM_CH3
#define BSP_USING_TIMER_CH0 #define BSP_USING_TIMER_CH0
#define BSP_USING_TIMER_CH1 #define BSP_USING_TIMER_CH1
/* ----------------------*/ /* ----------------------*/
@ -120,13 +123,57 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_PWM_CH0)
#ifndef PWM_CH0_CONFIG
#define PWM_CH0_CONFIG \
{ \
.ch = 0, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH1)
#ifndef PWM_CH1_CONFIG
#define PWM_CH1_CONFIG \
{ \
.ch = 1, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH2) #if defined(BSP_USING_PWM_CH2)
#ifndef PWM_CH2_CONFIG #ifndef PWM_CH2_CONFIG
#define PWM_CH2_CONFIG \ #define PWM_CH2_CONFIG \
{ \ { \
.ch = 2, \ .ch = 2, \
.frequency = 1000000, \ .polarity_invert_mode = DISABLE, \
.dutycycle = 0, \ .period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH3)
#ifndef PWM_CH3_CONFIG
#define PWM_CH3_CONFIG \
{ \
.ch = 3, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \ .it_pulse_count = 0, \
} }
#endif #endif
@ -196,7 +243,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH3) #if defined(BSP_USING_DMA0_CH3)
#ifndef DMA0_CH3_CONFIG #ifndef DMA0_CH3_CONFIG
#define DMA0_CH3_CONFIG \ #define DMA0_CH3_CONFIG \
@ -229,7 +275,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH5) #if defined(BSP_USING_DMA0_CH5)
#ifndef DMA0_CH5_CONFIG #ifndef DMA0_CH5_CONFIG
#define DMA0_CH5_CONFIG \ #define DMA0_CH5_CONFIG \
@ -262,7 +307,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH7) #if defined(BSP_USING_DMA0_CH7)
#ifndef DMA0_CH7_CONFIG #ifndef DMA0_CH7_CONFIG
#define DMA0_CH7_CONFIG \ #define DMA0_CH7_CONFIG \
@ -279,8 +323,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_I2C0) #if defined(BSP_USING_I2C0)
#ifndef I2C0_CONFIG #ifndef I2C0_CONFIG
#define I2C0_CONFIG \ #define I2C0_CONFIG \
@ -292,7 +334,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_TIMER_CH0) #if defined(BSP_USING_TIMER_CH0)
#ifndef TIMER_CH0_CONFIG #ifndef TIMER_CH0_CONFIG
#define TIMER_CH0_CONFIG \ #define TIMER_CH0_CONFIG \

View file

@ -39,7 +39,7 @@
// <q> GPIO3 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_UART0_RX//GPIO_FUN_UART1_RX//GPIO_FUN_QDEC] // <q> GPIO3 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_UART0_RX//GPIO_FUN_UART1_RX//GPIO_FUN_QDEC]
// <i> config gpio3 function // <i> config gpio3 function
#define CONFIG_GPIO3_FUNC GPIO_FUN_UNUSED #define CONFIG_GPIO3_FUNC GPIO_FUN_PWM
// <q> GPIO4 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC] // <q> GPIO4 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC]
// <i> config gpio4 function // <i> config gpio4 function
@ -67,15 +67,15 @@
// <q> GPIO10 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC] // <q> GPIO10 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC]
// <i> config gpio10 function // <i> config gpio10 function
#define CONFIG_GPIO10_FUNC GPIO_FUN_UNUSED #define CONFIG_GPIO10_FUNC GPIO_FUN_PWM
// <q> GPIO11 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC] // <q> GPIO11 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC]
// <i> config gpio11 function // <i> config gpio11 function
#define CONFIG_GPIO11_FUNC GPIO_FUN_ADC #define CONFIG_GPIO11_FUNC GPIO_FUN_PWM
// <q> GPIO12 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC] // <q> GPIO12 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC]
// <i> config gpio12 function // <i> config gpio12 function
#define CONFIG_GPIO12_FUNC GPIO_FUN_ADC #define CONFIG_GPIO12_FUNC GPIO_FUN_PWM
// <q> GPIO13 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC] // <q> GPIO13 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC]
// <i> config gpio13 function // <i> config gpio13 function
@ -115,7 +115,7 @@
// <q> GPIO22 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_ETHER_MAC//GPIO_FUN_QDEC] // <q> GPIO22 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_ETHER_MAC//GPIO_FUN_QDEC]
// <i> config gpio22 function // <i> config gpio22 function
#define CONFIG_GPIO22_FUNC GPIO_FUN_PWM #define CONFIG_GPIO22_FUNC GPIO_FUN_UNUSED
// <q> GPIO23 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC] // <q> GPIO23 <2> [GPIO_FUN_UNUSED//GPIO_FUN_I2S//GPIO_FUN_SPI//GPIO_FUN_I2C//GPIO_FUN_PWM//GPIO_FUN_CAM//GPIO_FUN_ANALOG//GPIO_FUN_UART0_RTS//GPIO_FUN_UART1_RTS//GPIO_FUN_QDEC]
// <i> config gpio23 function // <i> config gpio23 function

View file

@ -26,166 +26,88 @@
#include "bl702_glb.h" #include "bl702_glb.h"
#include "pinmux_config.h" #include "pinmux_config.h"
struct pin_mux_cfg struct pin_mux_cfg {
{
uint8_t pin; uint8_t pin;
uint16_t func; uint16_t func;
}; };
static const struct pin_mux_cfg af_pin_table[] = static const struct pin_mux_cfg af_pin_table[] = {
{ { .pin = GPIO_PIN_0,
{ .func = CONFIG_GPIO0_FUNC },
.pin = GPIO_PIN_0, { .pin = GPIO_PIN_1,
.func = CONFIG_GPIO0_FUNC .func = CONFIG_GPIO1_FUNC },
}, { .pin = GPIO_PIN_2,
{ .func = CONFIG_GPIO2_FUNC },
.pin = GPIO_PIN_1, { .pin = GPIO_PIN_3,
.func = CONFIG_GPIO1_FUNC .func = CONFIG_GPIO3_FUNC },
}, { .pin = GPIO_PIN_4,
{ .func = CONFIG_GPIO4_FUNC },
.pin = GPIO_PIN_2, { .pin = GPIO_PIN_5,
.func = CONFIG_GPIO2_FUNC .func = CONFIG_GPIO5_FUNC },
}, { .pin = GPIO_PIN_6,
{ .func = CONFIG_GPIO6_FUNC },
.pin = GPIO_PIN_3, { .pin = GPIO_PIN_7,
.func = CONFIG_GPIO3_FUNC .func = CONFIG_GPIO7_FUNC },
}, { .pin = GPIO_PIN_8,
{ .func = CONFIG_GPIO8_FUNC },
.pin = GPIO_PIN_4, { .pin = GPIO_PIN_9,
.func = CONFIG_GPIO4_FUNC .func = CONFIG_GPIO9_FUNC },
}, { .pin = GPIO_PIN_10,
{ .func = CONFIG_GPIO10_FUNC },
.pin = GPIO_PIN_5, { .pin = GPIO_PIN_11,
.func = CONFIG_GPIO5_FUNC .func = CONFIG_GPIO11_FUNC },
}, { .pin = GPIO_PIN_12,
{ .func = CONFIG_GPIO12_FUNC },
.pin = GPIO_PIN_6, { .pin = GPIO_PIN_13,
.func = CONFIG_GPIO6_FUNC .func = CONFIG_GPIO13_FUNC },
}, { .pin = GPIO_PIN_14,
{ .func = CONFIG_GPIO14_FUNC },
.pin = GPIO_PIN_7, { .pin = GPIO_PIN_15,
.func = CONFIG_GPIO7_FUNC .func = CONFIG_GPIO15_FUNC },
}, { .pin = GPIO_PIN_16,
{ .func = CONFIG_GPIO16_FUNC },
.pin = GPIO_PIN_8, { .pin = GPIO_PIN_17,
.func = CONFIG_GPIO8_FUNC .func = CONFIG_GPIO17_FUNC },
}, { .pin = GPIO_PIN_18,
{ .func = CONFIG_GPIO18_FUNC },
.pin = GPIO_PIN_9, { .pin = GPIO_PIN_19,
.func = CONFIG_GPIO9_FUNC .func = CONFIG_GPIO19_FUNC },
}, { .pin = GPIO_PIN_20,
{ .func = CONFIG_GPIO20_FUNC },
.pin = GPIO_PIN_10, { .pin = GPIO_PIN_21,
.func = CONFIG_GPIO10_FUNC .func = CONFIG_GPIO21_FUNC },
}, { .pin = GPIO_PIN_22,
{ .func = CONFIG_GPIO22_FUNC },
.pin = GPIO_PIN_11, { .pin = GPIO_PIN_23,
.func = CONFIG_GPIO11_FUNC .func = CONFIG_GPIO23_FUNC },
}, { .pin = GPIO_PIN_24,
{ .func = CONFIG_GPIO24_FUNC },
.pin = GPIO_PIN_12, { .pin = GPIO_PIN_25,
.func = CONFIG_GPIO12_FUNC .func = CONFIG_GPIO25_FUNC },
}, { .pin = GPIO_PIN_26,
{ .func = CONFIG_GPIO26_FUNC },
.pin = GPIO_PIN_13, { .pin = GPIO_PIN_27,
.func = CONFIG_GPIO13_FUNC .func = CONFIG_GPIO27_FUNC },
}, { .pin = GPIO_PIN_28,
{ .func = CONFIG_GPIO28_FUNC },
.pin = GPIO_PIN_14, { .pin = GPIO_PIN_29,
.func = CONFIG_GPIO14_FUNC .func = CONFIG_GPIO29_FUNC },
}, { .pin = GPIO_PIN_30,
{ .func = CONFIG_GPIO30_FUNC },
.pin = GPIO_PIN_15, { .pin = GPIO_PIN_31,
.func = CONFIG_GPIO15_FUNC .func = CONFIG_GPIO31_FUNC },
}, { .pin = GPIO_PIN_32,
{ .func = CONFIG_GPIO32_FUNC },
.pin = GPIO_PIN_16, { .pin = GPIO_PIN_33,
.func = CONFIG_GPIO16_FUNC .func = CONFIG_GPIO33_FUNC },
}, { .pin = GPIO_PIN_34,
{ .func = CONFIG_GPIO34_FUNC },
.pin = GPIO_PIN_17, { .pin = GPIO_PIN_35,
.func = CONFIG_GPIO17_FUNC .func = CONFIG_GPIO35_FUNC },
}, { .pin = GPIO_PIN_36,
{ .func = CONFIG_GPIO36_FUNC },
.pin = GPIO_PIN_18, { .pin = GPIO_PIN_37,
.func = CONFIG_GPIO18_FUNC .func = CONFIG_GPIO37_FUNC }
},
{
.pin = GPIO_PIN_19,
.func = CONFIG_GPIO19_FUNC
},
{
.pin = GPIO_PIN_20,
.func = CONFIG_GPIO20_FUNC
},
{
.pin = GPIO_PIN_21,
.func = CONFIG_GPIO21_FUNC
},
{
.pin = GPIO_PIN_22,
.func = CONFIG_GPIO22_FUNC
},
{
.pin = GPIO_PIN_23,
.func = CONFIG_GPIO23_FUNC
},
{
.pin = GPIO_PIN_24,
.func = CONFIG_GPIO24_FUNC
},
{
.pin = GPIO_PIN_25,
.func = CONFIG_GPIO25_FUNC
},
{
.pin = GPIO_PIN_26,
.func = CONFIG_GPIO26_FUNC
},
{
.pin = GPIO_PIN_27,
.func = CONFIG_GPIO27_FUNC
},
{
.pin = GPIO_PIN_28,
.func = CONFIG_GPIO28_FUNC
},
{
.pin = GPIO_PIN_29,
.func = CONFIG_GPIO29_FUNC
},
{
.pin = GPIO_PIN_30,
.func = CONFIG_GPIO30_FUNC
},
{
.pin = GPIO_PIN_31,
.func = CONFIG_GPIO31_FUNC
},
{
.pin = GPIO_PIN_32,
.func = CONFIG_GPIO32_FUNC
},
{
.pin = GPIO_PIN_33,
.func = CONFIG_GPIO33_FUNC
},
{
.pin = GPIO_PIN_34,
.func = CONFIG_GPIO34_FUNC
},
{
.pin = GPIO_PIN_35,
.func = CONFIG_GPIO35_FUNC
},
{
.pin = GPIO_PIN_36,
.func = CONFIG_GPIO36_FUNC
},
{
.pin = GPIO_PIN_37,
.func = CONFIG_GPIO37_FUNC
}
}; };
@ -196,43 +118,32 @@ static void board_pin_mux_init(void)
gpio_cfg.drive = 0; gpio_cfg.drive = 0;
gpio_cfg.smtCtrl = 1; gpio_cfg.smtCtrl = 1;
for (int i = 0; i < sizeof(af_pin_table)/sizeof(af_pin_table[0]); i++) for (int i = 0; i < sizeof(af_pin_table) / sizeof(af_pin_table[0]); i++) {
{
gpio_cfg.gpioMode = GPIO_MODE_AF; gpio_cfg.gpioMode = GPIO_MODE_AF;
gpio_cfg.pullType = GPIO_PULL_UP; gpio_cfg.pullType = GPIO_PULL_UP;
gpio_cfg.gpioPin = af_pin_table[i].pin; gpio_cfg.gpioPin = af_pin_table[i].pin;
gpio_cfg.gpioFun = af_pin_table[i].func; gpio_cfg.gpioFun = af_pin_table[i].func;
if(af_pin_table[i].func == GPIO_FUN_UNUSED) if (af_pin_table[i].func == GPIO_FUN_UNUSED) {
{
continue; continue;
} } else if (af_pin_table[i].func == GPIO_FUN_PWM) {
else if(af_pin_table[i].func == GPIO_FUN_PWM)
{
gpio_cfg.pullType = GPIO_PULL_DOWN; gpio_cfg.pullType = GPIO_PULL_DOWN;
} } else if ((af_pin_table[i].func == GPIO_FUN_DAC) || (af_pin_table[i].func == GPIO_FUN_ADC)) {
else if((af_pin_table[i].func == GPIO_FUN_DAC)|| (af_pin_table[i].func == GPIO_FUN_ADC))
{
gpio_cfg.gpioFun = GPIO_FUN_ANALOG; gpio_cfg.gpioFun = GPIO_FUN_ANALOG;
gpio_cfg.gpioMode = GPIO_MODE_ANALOG; gpio_cfg.gpioMode = GPIO_MODE_ANALOG;
} } else if ((af_pin_table[i].func & 0x70) == 0x70) {
else if((af_pin_table[i].func & 0x70) == 0x70)
{
gpio_cfg.gpioFun = GPIO_FUN_UART; gpio_cfg.gpioFun = GPIO_FUN_UART;
uint8_t sig = af_pin_table[i].func & 0x07; uint8_t sig = af_pin_table[i].func & 0x07;
if (gpio_cfg.gpioPin > 31) if (gpio_cfg.gpioPin > 31) {
{
GLB_UART_Fun_Sel(((gpio_cfg.gpioPin - 9) % 8), sig); GLB_UART_Fun_Sel(((gpio_cfg.gpioPin - 9) % 8), sig);
} } else {
else
{
GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig); GLB_UART_Fun_Sel((gpio_cfg.gpioPin % 8), sig);
} }
} }
GLB_GPIO_Init(&gpio_cfg); GLB_GPIO_Init(&gpio_cfg);
} }
} }
static void board_clock_init(void) static void board_clock_init(void)
@ -243,6 +154,7 @@ static void board_clock_init(void)
void bl_show_info(void) void bl_show_info(void)
{ {
MSG("\r\n");
MSG(" ____ __ __ _ _ _ \r\n"); MSG(" ____ __ __ _ _ _ \r\n");
MSG(" | _ \\ / _|/ _| | | | | | | \r\n"); MSG(" | _ \\ / _|/ _| | | | | | | \r\n");
MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n"); MSG(" | |_) | ___ _ _| |_| |_ __ _| | ___ | | __ _| |__ \r\n");

View file

@ -44,6 +44,10 @@
#define BSP_SPI_CLOCK_SOURCE ROOT_CLOCK_SOURCE_BCLK #define BSP_SPI_CLOCK_SOURCE ROOT_CLOCK_SOURCE_BCLK
#define BSP_SPI_CLOCK_DIV 0 #define BSP_SPI_CLOCK_DIV 0
#endif #endif
#if defined(BSP_USING_PWM_CH0) || defined(BSP_USING_PWM_CH1) || defined(BSP_USING_PWM_CH2) || defined(BSP_USING_PWM_CH3) || defined(BSP_USING_PWM_CH4) || defined(BSP_USING_PWM_CH5)
#define BSP_PWM_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK
#define BSP_PWM_CLOCK_DIV 0
#endif
#if defined(BSP_USING_IR) #if defined(BSP_USING_IR)
#define BSP_IR_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK #define BSP_IR_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK
#define BSP_IR_CLOCK_DIV 0 #define BSP_IR_CLOCK_DIV 0

View file

@ -33,8 +33,12 @@
#define BSP_USING_I2C0 #define BSP_USING_I2C0
#define BSP_USING_I2S0 #define BSP_USING_I2S0
#define BSP_USING_USB #define BSP_USING_USB
#define BSP_USING_PWM_CH0
#define BSP_USING_PWM_CH1
#define BSP_USING_PWM_CH2 #define BSP_USING_PWM_CH2
#define BSP_USING_PWM_CH3
#define BSP_USING_TIMER_CH0 #define BSP_USING_TIMER_CH0
#define BSP_USING_TIMER_CH1
/* ----------------------*/ /* ----------------------*/
/* PERIPHERAL With DMA LIST */ /* PERIPHERAL With DMA LIST */
@ -48,7 +52,6 @@
#define BSP_USING_DMA0_CH6 #define BSP_USING_DMA0_CH6
#define BSP_USING_DMA0_CH7 #define BSP_USING_DMA0_CH7
/* PERIPHERAL CONFIG */ /* PERIPHERAL CONFIG */
#if defined(BSP_USING_ADC0) #if defined(BSP_USING_ADC0)
#ifndef ADC0_CONFIG #ifndef ADC0_CONFIG
@ -120,13 +123,57 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_PWM_CH0)
#ifndef PWM_CH0_CONFIG
#define PWM_CH0_CONFIG \
{ \
.ch = 0, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH1)
#ifndef PWM_CH1_CONFIG
#define PWM_CH1_CONFIG \
{ \
.ch = 1, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH2) #if defined(BSP_USING_PWM_CH2)
#ifndef PWM_CH2_CONFIG #ifndef PWM_CH2_CONFIG
#define PWM_CH2_CONFIG \ #define PWM_CH2_CONFIG \
{ \ { \
.ch = 2, \ .ch = 2, \
.frequency = 1000000, \ .polarity_invert_mode = DISABLE, \
.dutycycle = 0, \ .period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \
}
#endif
#endif
#if defined(BSP_USING_PWM_CH3)
#ifndef PWM_CH3_CONFIG
#define PWM_CH3_CONFIG \
{ \
.ch = 3, \
.polarity_invert_mode = DISABLE, \
.period = 0, \
.threshold_low = 0, \
.threshold_high = 0, \
.it_pulse_count = 0, \ .it_pulse_count = 0, \
} }
#endif #endif
@ -196,7 +243,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH3) #if defined(BSP_USING_DMA0_CH3)
#ifndef DMA0_CH3_CONFIG #ifndef DMA0_CH3_CONFIG
#define DMA0_CH3_CONFIG \ #define DMA0_CH3_CONFIG \
@ -229,7 +275,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH5) #if defined(BSP_USING_DMA0_CH5)
#ifndef DMA0_CH5_CONFIG #ifndef DMA0_CH5_CONFIG
#define DMA0_CH5_CONFIG \ #define DMA0_CH5_CONFIG \
@ -262,7 +307,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_DMA0_CH7) #if defined(BSP_USING_DMA0_CH7)
#ifndef DMA0_CH7_CONFIG #ifndef DMA0_CH7_CONFIG
#define DMA0_CH7_CONFIG \ #define DMA0_CH7_CONFIG \
@ -279,8 +323,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_I2C0) #if defined(BSP_USING_I2C0)
#ifndef I2C0_CONFIG #ifndef I2C0_CONFIG
#define I2C0_CONFIG \ #define I2C0_CONFIG \
@ -292,7 +334,6 @@
#endif #endif
#endif #endif
#if defined(BSP_USING_TIMER_CH0) #if defined(BSP_USING_TIMER_CH0)
#ifndef TIMER_CH0_CONFIG #ifndef TIMER_CH0_CONFIG
#define TIMER_CH0_CONFIG \ #define TIMER_CH0_CONFIG \
@ -312,7 +353,7 @@
.id = 0, \ .id = 0, \
.ch = 1, \ .ch = 1, \
.cnt_mode = TIMER_CNT_PRELOAD, \ .cnt_mode = TIMER_CNT_PRELOAD, \
.pl_trig_src = TIMER_PL_TRIG_COMP0, \ .pl_trig_src = TIMER_PL_TRIG_COMP2, \
} }
#endif #endif
#endif #endif

View file

@ -97,7 +97,6 @@ BL_Err_Type ES8388_Write_Reg(uint8_t addr, uint8_t data)
msg1.subaddr = addr; msg1.subaddr = addr;
bflb_platform_delay_ms(10); bflb_platform_delay_ms(10);
return i2c_transfer(es8388_i2c, &msg1, 1); return i2c_transfer(es8388_i2c, &msg1, 1);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -118,7 +117,6 @@ BL_Err_Type ES8388_Read_Reg(uint8_t addr, uint8_t* rdata)
msg1.slaveaddr = ES8388_I2C_SLAVE_ADDR, msg1.slaveaddr = ES8388_I2C_SLAVE_ADDR,
msg1.flags = SUB_ADDR_1BYTE | I2C_RD; msg1.flags = SUB_ADDR_1BYTE | I2C_RD;
return i2c_transfer(es8388_i2c, &msg1, 1); return i2c_transfer(es8388_i2c, &msg1, 1);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -129,7 +127,8 @@ BL_Err_Type ES8388_Read_Reg(uint8_t addr, uint8_t* rdata)
* @return None * @return None
* *
*******************************************************************************/ *******************************************************************************/
void ES8388_Codec_Mode(ES8388_Cfg_Type * cfg){ void ES8388_Codec_Mode(ES8388_Cfg_Type *cfg)
{
uint8_t tempVal = 0; uint8_t tempVal = 0;
ES8388_Write_Reg(0x01, 0x58); //power down whole chip analog ES8388_Write_Reg(0x01, 0x58); //power down whole chip analog
@ -229,10 +228,8 @@ void ES8388_Codec_Mode(ES8388_Cfg_Type * cfg){
ES8388_Write_Reg(0x04, 0x24); ES8388_Write_Reg(0x04, 0x24);
ES8388_Write_Reg(0x26, 0x01); ES8388_Write_Reg(0x26, 0x01);
ES8388_Write_Reg(0x03, 0x09); //power up ADC Enable LIN &RIN. ES8388_Write_Reg(0x03, 0x09); //power up ADC Enable LIN &RIN.
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief ES8388 recording mode * @brief ES8388 recording mode
* *
@ -241,7 +238,8 @@ void ES8388_Codec_Mode(ES8388_Cfg_Type * cfg){
* @return None * @return None
* *
*******************************************************************************/ *******************************************************************************/
void ES8388_Recording_Mode(ES8388_Cfg_Type * cfg){ void ES8388_Recording_Mode(ES8388_Cfg_Type *cfg)
{
uint8_t tempVal = 0; uint8_t tempVal = 0;
MSG("RECORDING\r\n"); MSG("RECORDING\r\n");
ES8388_Write_Reg(0x01, 0x58); //power down whole chip analog ES8388_Write_Reg(0x01, 0x58); //power down whole chip analog
@ -312,7 +310,6 @@ void ES8388_Recording_Mode(ES8388_Cfg_Type * cfg){
ES8388_Write_Reg(0x03, 0x09); //power up adc , enable LIN and RIN ES8388_Write_Reg(0x03, 0x09); //power up adc , enable LIN and RIN
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief ES8388 Playback mode * @brief ES8388 Playback mode
* *
@ -321,7 +318,8 @@ void ES8388_Recording_Mode(ES8388_Cfg_Type * cfg){
* @return None * @return None
* *
*******************************************************************************/ *******************************************************************************/
void ES8388_Playback_Mode(ES8388_Cfg_Type * cfg){ void ES8388_Playback_Mode(ES8388_Cfg_Type *cfg)
{
uint8_t tempVal = 0; uint8_t tempVal = 0;
ES8388_Write_Reg(0x00, 0x80); //Reset control port register to default ES8388_Write_Reg(0x00, 0x80); //Reset control port register to default
@ -342,7 +340,6 @@ void ES8388_Playback_Mode(ES8388_Cfg_Type * cfg){
ES8388_Write_Reg(0x03, 0x00); //Power on ADC and LIN/RIN input ES8388_Write_Reg(0x03, 0x00); //Power on ADC and LIN/RIN input
ES8388_Write_Reg(0x04, 0x3F); //Power on DAC and LOUT/ROUT input ES8388_Write_Reg(0x04, 0x3F); //Power on DAC and LOUT/ROUT input
/* Set ADC */ /* Set ADC */
ES8388_Write_Reg(0x09, 0x77); //MicBoost PGA = +21dB ES8388_Write_Reg(0x09, 0x77); //MicBoost PGA = +21dB
ES8388_Write_Reg(0x0A, 0xF0); //Differential input ES8388_Write_Reg(0x0A, 0xF0); //Differential input
@ -381,7 +378,6 @@ void ES8388_Playback_Mode(ES8388_Cfg_Type * cfg){
ES8388_Write_Reg(0x30, 0x1E); //Set Lout/Rout volume:0dB ES8388_Write_Reg(0x30, 0x1E); //Set Lout/Rout volume:0dB
ES8388_Write_Reg(0x31, 0x1E); //Set Lout/Rout volume:0dB ES8388_Write_Reg(0x31, 0x1E); //Set Lout/Rout volume:0dB
ES8388_Write_Reg(0x02, 0x00); //Power up DEM and STM ES8388_Write_Reg(0x02, 0x00); //Power up DEM and STM
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -395,10 +391,13 @@ void ES8388_Playback_Mode(ES8388_Cfg_Type * cfg){
int ES8388_Set_Voice_Volume(int volume) int ES8388_Set_Voice_Volume(int volume)
{ {
int res; int res;
if (volume < 0)
if (volume < 0) {
volume = 0; volume = 0;
else if (volume > 100) } else if (volume > 100) {
volume = 100; volume = 100;
}
volume /= 3; volume /= 3;
res = ES8388_Write_Reg(0x2e, volume); res = ES8388_Write_Reg(0x2e, volume);
res |= ES8388_Write_Reg(0x2f, volume); res |= ES8388_Write_Reg(0x2f, volume);
@ -407,7 +406,6 @@ int ES8388_Set_Voice_Volume(int volume)
return res; return res;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief ES8388_I2C_Init * @brief ES8388_I2C_Init
* *
@ -420,12 +418,12 @@ void ES8388_I2C_Init(void)
{ {
i2c_register(I2C0_INDEX, "i2c", DEVICE_OFLAG_RDWR); i2c_register(I2C0_INDEX, "i2c", DEVICE_OFLAG_RDWR);
es8388_i2c = device_find("i2c"); es8388_i2c = device_find("i2c");
if (es8388_i2c) { if (es8388_i2c) {
device_open(es8388_i2c, 0); device_open(es8388_i2c, 0);
} }
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief ES8388_Reg_Dump * @brief ES8388_Reg_Dump
* *
@ -440,13 +438,15 @@ BL_Err_Type ES8388_Reg_Dump(void)
uint8_t tmp; uint8_t tmp;
for (i = 0; i < 0X3F; i++) { for (i = 0; i < 0X3F; i++) {
if(ES8388_Read_Reg(i,&tmp) != SUCCESS) if (ES8388_Read_Reg(i, &tmp) != SUCCESS) {
bflb_platform_printf("iic read err\r\n"); bflb_platform_printf("iic read err\r\n");
bflb_platform_printf("Reg[%02x]=%02x \n", i,tmp);
}
return SUCCESS;
} }
bflb_platform_printf("Reg[%02x]=%02x \n", i, tmp);
}
return SUCCESS;
}
/*@} end of group ES8388_Private_Functions */ /*@} end of group ES8388_Private_Functions */
@ -462,17 +462,26 @@ BL_Err_Type ES8388_Reg_Dump(void)
* @return None * @return None
* *
*******************************************************************************/ *******************************************************************************/
void ES8388_Init(ES8388_Cfg_Type * cfg){ void ES8388_Init(ES8388_Cfg_Type *cfg)
{
ES8388_I2C_Init(); ES8388_I2C_Init();
switch (cfg->work_mode) { switch (cfg->work_mode) {
case ES8388_CODEC_MDOE: ES8388_Codec_Mode(cfg);break; case ES8388_CODEC_MDOE:
case ES8388_RECORDING_MODE:ES8388_Recording_Mode(cfg);break; ES8388_Codec_Mode(cfg);
case ES8388_PLAY_BACK_MODE:ES8388_Playback_Mode(cfg);break; break;
default:break;
}
case ES8388_RECORDING_MODE:
ES8388_Recording_Mode(cfg);
break;
case ES8388_PLAY_BACK_MODE:
ES8388_Playback_Mode(cfg);
break;
default:
break;
}
} }
/*@} end of group ES8388_Public_Functions */ /*@} end of group ES8388_Public_Functions */

View file

@ -103,7 +103,8 @@ typedef enum {
/** /**
* @brief ES8388_Cfg_Type * @brief ES8388_Cfg_Type
*/ */
typedef struct { typedef struct
{
ES8388_Work_Mode work_mode; /*!< ES8388 work mode */ ES8388_Work_Mode work_mode; /*!< ES8388 work mode */
ES8388_Role_Type role; /*!< ES8388 role */ ES8388_Role_Type role; /*!< ES8388 role */
ES8388_MIC_Input_Type mic_input_mode; /*!< ES8388 mic input mode */ ES8388_MIC_Input_Type mic_input_mode; /*!< ES8388 mic input mode */

View file

@ -69,6 +69,7 @@ int MMC_disk_initialize()
return -1; return -1;
} }
} }
return 0; return 0;
} }
@ -122,12 +123,12 @@ int RAM_disk_ioctl(BYTE cmd, void* buff)
int MMC_disk_ioctl(BYTE cmd, void *buff) int MMC_disk_ioctl(BYTE cmd, void *buff)
{ {
switch (cmd) switch (cmd) {
{
// Get R/W sector size (WORD) // Get R/W sector size (WORD)
case GET_SECTOR_SIZE: case GET_SECTOR_SIZE:
*(WORD *)buff = gSDCardInfo.blockSize; *(WORD *)buff = gSDCardInfo.blockSize;
break; break;
// Get erase block size in unit of sector (DWORD) // Get erase block size in unit of sector (DWORD)
case GET_BLOCK_SIZE: case GET_BLOCK_SIZE:
*(DWORD *)buff = 1; *(DWORD *)buff = 1;
@ -136,11 +137,14 @@ int MMC_disk_ioctl(BYTE cmd, void* buff)
case GET_SECTOR_COUNT: case GET_SECTOR_COUNT:
*(DWORD *)buff = gSDCardInfo.blockCount; *(DWORD *)buff = gSDCardInfo.blockCount;
break; break;
case CTRL_SYNC: case CTRL_SYNC:
break; break;
default: default:
break; break;
} }
return 0; return 0;
} }
@ -149,7 +153,8 @@ int USB_disk_ioctl(BYTE cmd, void* buff)
return 0; return 0;
} }
DWORD get_fattime(void) { DWORD get_fattime(void)
{
return ((DWORD)(2015 - 1980) << 25) /* Year 2015 */ return ((DWORD)(2015 - 1980) << 25) /* Year 2015 */
| ((DWORD)1 << 21) /* Month 1 */ | ((DWORD)1 << 21) /* Month 1 */
| ((DWORD)1 << 16) /* Mday 1 */ | ((DWORD)1 << 16) /* Mday 1 */
@ -158,3 +163,21 @@ DWORD get_fattime(void) {
| ((DWORD)0 >> 1); /* Sec 0 */ | ((DWORD)0 >> 1); /* Sec 0 */
} }
DSTATUS Translate_Result_Code(int result)
{
// MSG("%s\r\n",FR_Table[result]);
return result;
}
FATFS_DiskioDriverTypeDef pNewDiskioDriver;
void fatfs_sdio_driver_register(void)
{
pNewDiskioDriver.MMC_disk_status = MMC_disk_status;
pNewDiskioDriver.MMC_disk_initialize = MMC_disk_initialize;
pNewDiskioDriver.MMC_disk_write = MMC_disk_write;
pNewDiskioDriver.MMC_disk_read = MMC_disk_read;
pNewDiskioDriver.MMC_disk_ioctl = MMC_disk_ioctl;
pNewDiskioDriver.Translate_Result_Code = Translate_Result_Code;
disk_driver_callback_init(&pNewDiskioDriver);
}

View file

@ -25,8 +25,7 @@
#include "bsp_spi_sd.h" #include "bsp_spi_sd.h"
#include "string.h" #include "string.h"
const char * FR_Table[]= const char *FR_Table[] = {
{
"FR_OK成功", /* (0) Succeeded */ "FR_OK成功", /* (0) Succeeded */
"FR_DISK_ERR底层硬件错误", /* (1) A hard error occurred in the low level disk I/O layer */ "FR_DISK_ERR底层硬件错误", /* (1) A hard error occurred in the low level disk I/O layer */
"FR_INT_ERR断言失败", /* (2) Assertion failed */ "FR_INT_ERR断言失败", /* (2) Assertion failed */
@ -68,33 +67,37 @@ int sd_disk_write(const BYTE *buff, LBA_t sector, UINT count)
int sd_disk_ioctl(BYTE cmd, void *buff) int sd_disk_ioctl(BYTE cmd, void *buff)
{ {
int result = 0; int result = 0;
switch (cmd)
{ switch (cmd) {
case CTRL_SYNC: case CTRL_SYNC:
result = RES_OK; result = RES_OK;
break; break;
case GET_SECTOR_SIZE: case GET_SECTOR_SIZE:
*(DWORD *)buff = SD_CardInfo.CardBlockSize; *(DWORD *)buff = SD_CardInfo.CardBlockSize;
result = RES_OK; result = RES_OK;
break; break;
case GET_BLOCK_SIZE: case GET_BLOCK_SIZE:
*(WORD *)buff = SD_CardInfo.CardBlockSize; *(WORD *)buff = SD_CardInfo.CardBlockSize;
result = RES_OK; result = RES_OK;
break; break;
case GET_SECTOR_COUNT: case GET_SECTOR_COUNT:
*(DWORD *)buff = SD_CardInfo.CardCapacity / SD_CardInfo.CardBlockSize; *(DWORD *)buff = SD_CardInfo.CardCapacity / SD_CardInfo.CardBlockSize;
result = RES_OK; result = RES_OK;
break; break;
default: default:
result = RES_PARERR; result = RES_PARERR;
break; break;
} }
return result; return result;
} }
DSTATUS Translate_Result_Code(int result) DSTATUS Translate_Result_Code(int result)
{ {
// MSG("%s\r\n",FR_Table[result]); // MSG("%s\r\n",FR_Table[result]);
return result; return result;
} }
@ -115,9 +118,10 @@ int shellGetcwd(char *path, unsigned int len)
{ {
int err; int err;
err = f_getcwd(path, len); err = f_getcwd(path, len);
if(err)
{ if (err) {
strcpy(path, "unknow"); strcpy(path, "unknow");
} }
return 0; return 0;
} }

View file

@ -73,7 +73,6 @@ static struct device* dma_ch4;
void dma_ch3_callback(struct device *dev, void *args, uint32_t size, uint32_t event) void dma_ch3_callback(struct device *dev, void *args, uint32_t size, uint32_t event)
{ {
} }
/*@} end of group TFT_LCD_Private_Functions */ /*@} end of group TFT_LCD_Private_Functions */
@ -89,30 +88,28 @@ void spi0_init(void)
gpio_write(LCD_DC_PIN, 1); //DC gpio_write(LCD_DC_PIN, 1); //DC
spi0 = device_find("spi0"); spi0 = device_find("spi0");
if(spi0)
{ if (spi0) {
device_close(spi0); device_close(spi0);
} } else {
else{
spi_register(SPI0_INDEX, "spi0", DEVICE_OFLAG_RDWR); spi_register(SPI0_INDEX, "spi0", DEVICE_OFLAG_RDWR);
spi0 = device_find("spi0"); spi0 = device_find("spi0");
} }
if(spi0)
{ if (spi0) {
device_open(spi0, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_STREAM_RX); device_open(spi0, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_STREAM_RX);
} }
dma_ch3 = device_find("dma0_ch3"); dma_ch3 = device_find("dma0_ch3");
if(dma_ch3)
{ if (dma_ch3) {
device_close(dma_ch3); device_close(dma_ch3);
} } else {
else{
dma_register(DMA0_CH3_INDEX, "dma0_ch3", DEVICE_OFLAG_RDWR); dma_register(DMA0_CH3_INDEX, "dma0_ch3", DEVICE_OFLAG_RDWR);
dma_ch3 = device_find("dma0_ch3"); dma_ch3 = device_find("dma0_ch3");
} }
if (dma_ch3)
{ if (dma_ch3) {
DMA_DEV(dma_ch3)->direction = DMA_MEMORY_TO_PERIPH; DMA_DEV(dma_ch3)->direction = DMA_MEMORY_TO_PERIPH;
DMA_DEV(dma_ch3)->transfer_mode = DMA_LLI_ONCE_MODE; DMA_DEV(dma_ch3)->transfer_mode = DMA_LLI_ONCE_MODE;
DMA_DEV(dma_ch3)->src_req = DMA_REQUEST_NONE; DMA_DEV(dma_ch3)->src_req = DMA_REQUEST_NONE;
@ -125,16 +122,15 @@ void spi0_init(void)
} }
dma_ch4 = device_find("dma0_ch4"); dma_ch4 = device_find("dma0_ch4");
if(dma_ch4)
{ if (dma_ch4) {
device_close(dma_ch4); device_close(dma_ch4);
} } else {
else{
dma_register(DMA0_CH4_INDEX, "dma0_ch4", DEVICE_OFLAG_RDWR); dma_register(DMA0_CH4_INDEX, "dma0_ch4", DEVICE_OFLAG_RDWR);
dma_ch4 = device_find("dma0_ch4"); dma_ch4 = device_find("dma0_ch4");
} }
if (dma_ch4)
{ if (dma_ch4) {
DMA_DEV(dma_ch4)->direction = DMA_PERIPH_TO_MEMORY; DMA_DEV(dma_ch4)->direction = DMA_PERIPH_TO_MEMORY;
DMA_DEV(dma_ch4)->transfer_mode = DMA_LLI_ONCE_MODE; DMA_DEV(dma_ch4)->transfer_mode = DMA_LLI_ONCE_MODE;
DMA_DEV(dma_ch4)->src_req = DMA_REQUEST_SPI0_RX; DMA_DEV(dma_ch4)->src_req = DMA_REQUEST_SPI0_RX;
@ -145,6 +141,7 @@ void spi0_init(void)
device_set_callback(dma_ch4, NULL); device_set_callback(dma_ch4, NULL);
device_control(dma_ch4, DEVICE_CTRL_CLR_INT, NULL); device_control(dma_ch4, DEVICE_CTRL_CLR_INT, NULL);
} }
device_control(spi0, DEVICE_CTRL_TX_DMA_SUSPEND, NULL); device_control(spi0, DEVICE_CTRL_TX_DMA_SUSPEND, NULL);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -163,7 +160,6 @@ void LCD_WR_Cmd(uint8_t command)
CS1_HIGH; CS1_HIGH;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD write 8-bit data * @brief LCD write 8-bit data
* *
@ -180,7 +176,6 @@ void LCD_WR_Byte(uint8_t data)
CS1_HIGH; CS1_HIGH;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD write 16-bit data * @brief LCD write 16-bit data
* *
@ -197,7 +192,6 @@ void LCD_WR_HalfWord(uint16_t data)
CS1_HIGH; CS1_HIGH;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD write 32-bit data * @brief LCD write 32-bit data
* *
@ -224,7 +218,10 @@ void LCD_WR_SPI_DMA(uint16_t *img, uint32_t len)
DC_HIGH; DC_HIGH;
dma_reload(dma_ch3, (uint32_t)img, (uint32_t)DMA_ADDR_SPI_TDR, len); dma_reload(dma_ch3, (uint32_t)img, (uint32_t)DMA_ADDR_SPI_TDR, len);
dma_channel_start(dma_ch3); dma_channel_start(dma_ch3);
while(dma_channel_check_busy(dma_ch3));
while (dma_channel_check_busy(dma_ch3))
;
device_control(spi0, DEVICE_CTRL_TX_DMA_SUSPEND, NULL); device_control(spi0, DEVICE_CTRL_TX_DMA_SUSPEND, NULL);
CS1_HIGH; CS1_HIGH;
} }
@ -240,20 +237,24 @@ void LCD_WR_SPI_DMA(uint16_t *img, uint32_t len)
void LCD_Set_Dir(uint8_t dir) void LCD_Set_Dir(uint8_t dir)
{ {
LCD_WR_Cmd(0x36); LCD_WR_Cmd(0x36);
switch (dir)
{ switch (dir) {
case 0: case 0:
LCD_WR_Byte(0x08); LCD_WR_Byte(0x08);
break; break;
case 1: case 1:
LCD_WR_Byte(0xA8); LCD_WR_Byte(0xA8);
break; break;
case 2: case 2:
LCD_WR_Byte(0xC8); LCD_WR_Byte(0xC8);
break; break;
case 3: case 3:
LCD_WR_Byte(0x68); LCD_WR_Byte(0x68);
break; break;
default: default:
break; break;
} }
@ -272,7 +273,6 @@ void LCD_Set_Dir(uint8_t dir )
*******************************************************************************/ *******************************************************************************/
void LCD_Set_Addr(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2) void LCD_Set_Addr(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
{ {
LCD_WR_Cmd(0x2a); LCD_WR_Cmd(0x2a);
LCD_WR_Word(x2 << 24 | (x2 << 8 & 0xff0000) | (x1 << 8 & 0xff00) | (x1 >> 8 & 0xff)); LCD_WR_Word(x2 << 24 | (x2 << 8 & 0xff0000) | (x1 << 8 & 0xff00) | (x1 >> 8 & 0xff));
@ -282,7 +282,6 @@ void LCD_Set_Addr(uint32_t x1,uint32_t y1,uint32_t x2,uint32_t y2)
LCD_WR_Cmd(0x2C); LCD_WR_Cmd(0x2C);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief SPI LCD init * @brief SPI LCD init
* *
@ -360,7 +359,6 @@ void LCD_Init(void)
LCD_WR_Cmd(0x29); /* Display on */ LCD_WR_Cmd(0x29); /* Display on */
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD clear display * @brief LCD clear display
* *
@ -373,6 +371,7 @@ void LCD_Clear(uint16_t color)
{ {
uint16_t i, j; uint16_t i, j;
LCD_Set_Addr(0, 0, LCD_W - 1, LCD_H - 1); LCD_Set_Addr(0, 0, LCD_W - 1, LCD_H - 1);
for (i = 0; i < LCD_W; i++) { for (i = 0; i < LCD_W; i++) {
for (j = 0; j < LCD_H; j++) { for (j = 0; j < LCD_H; j++) {
LCD_WR_HalfWord(color); LCD_WR_HalfWord(color);
@ -380,7 +379,6 @@ void LCD_Clear(uint16_t color)
} }
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD draw a point * @brief LCD draw a point
* *
@ -397,7 +395,6 @@ void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
LCD_WR_HalfWord(color); LCD_WR_HalfWord(color);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD draw line * @brief LCD draw line
* *
@ -422,16 +419,17 @@ void LCD_DrawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color
if (absX > absY) { if (absX > absY) {
for (i = 0; i < absX + 1; i++) { for (i = 0; i < absX + 1; i++) {
temp = yVariation * 100 / absX * i / 100; temp = yVariation * 100 / absX * i / 100;
if (xVariation > 0) { if (xVariation > 0) {
LCD_DrawPoint(x1 + i, y1 + temp, color); LCD_DrawPoint(x1 + i, y1 + temp, color);
} else { } else {
LCD_DrawPoint(x1 - i, y1 + temp, color); LCD_DrawPoint(x1 - i, y1 + temp, color);
} }
} }
} } else {
else{
for (i = 0; i < absY + 1; i++) { for (i = 0; i < absY + 1; i++) {
temp = xVariation * 100 / absY * i / 100; temp = xVariation * 100 / absY * i / 100;
if (yVariation > 0) { if (yVariation > 0) {
LCD_DrawPoint(x1 + temp, y1 + i, color); LCD_DrawPoint(x1 + temp, y1 + i, color);
} else { } else {
@ -441,7 +439,6 @@ void LCD_DrawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color
} }
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD draw rectangle * @brief LCD draw rectangle
* *
@ -462,7 +459,6 @@ void LCD_DrawRectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t
LCD_DrawLine(x1, y2, x1, y1, color); LCD_DrawLine(x1, y2, x1, y1, color);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD draw circle * @brief LCD draw circle
* *
@ -480,8 +476,8 @@ void LCD_DrawCircle(uint16_t x,uint16_t y,uint16_t r,uint16_t color)
int di; int di;
b = r; b = r;
di = 3 - (r << 1); di = 3 - (r << 1);
while(a <= b)
{ while (a <= b) {
LCD_DrawPoint(x - b, y - a, color); LCD_DrawPoint(x - b, y - a, color);
LCD_DrawPoint(x + b, y - a, color); LCD_DrawPoint(x + b, y - a, color);
LCD_DrawPoint(x - a, y + b, color); LCD_DrawPoint(x - a, y + b, color);
@ -492,17 +488,18 @@ void LCD_DrawCircle(uint16_t x,uint16_t y,uint16_t r,uint16_t color)
LCD_DrawPoint(x + a, y + b, color); LCD_DrawPoint(x + a, y + b, color);
LCD_DrawPoint(x - b, y + a, color); LCD_DrawPoint(x - b, y + a, color);
a++; a++;
if (di < 0) { if (di < 0) {
di += 4 * a + 6; di += 4 * a + 6;
} else { } else {
di += 10 + 4 * (a - b); di += 10 + 4 * (a - b);
b--; b--;
} }
LCD_DrawPoint(x + a, y + b, color); LCD_DrawPoint(x + a, y + b, color);
} }
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD fill the area with color * @brief LCD fill the area with color
* *
@ -519,12 +516,13 @@ void LCD_DrawArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color
{ {
uint16_t i, j; uint16_t i, j;
LCD_Set_Addr(x1, y1, x2, y2); LCD_Set_Addr(x1, y1, x2, y2);
for(i=y1;i<=y2;i++)
{
for(j=x1;j<=x2;j++)LCD_WR_HalfWord(color);
}
}
for (i = y1; i <= y2; i++) {
for (j = x1; j <= x2; j++) {
LCD_WR_HalfWord(color);
}
}
}
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD draw picture * @brief LCD draw picture
@ -542,8 +540,8 @@ void LCD_DrawPicture(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t* p
{ {
uint32_t i; uint32_t i;
LCD_Set_Addr(x1, y1, x2, y2); LCD_Set_Addr(x1, y1, x2, y2);
for(i=0;i<ABS16((x2-x1+1)*(y2-y1+1));i++)
{ for (i = 0; i < ABS16((x2 - x1 + 1) * (y2 - y1 + 1)); i++) {
LCD_WR_HalfWord(picture[i]); LCD_WR_HalfWord(picture[i]);
} }
} }
@ -552,8 +550,8 @@ void LCD_DrawPicture_cam(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_
{ {
uint32_t i; uint32_t i;
LCD_Set_Addr(x1, y1, x2, y2); LCD_Set_Addr(x1, y1, x2, y2);
for(i=0;i<ABS16((x2-x1+1)*(y2-y1+1));i++)
{ for (i = 0; i < ABS16((x2 - x1 + 1) * (y2 - y1 + 1)); i++) {
// LCD_WR_Byte(picture[i]); // LCD_WR_Byte(picture[i]);
// LCD_WR_Word(picture[i]); // LCD_WR_Word(picture[i]);
LCD_WR_HalfWord(picture[i]); LCD_WR_HalfWord(picture[i]);
@ -592,7 +590,6 @@ void LCD_DrawPicture_cam(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_
// UART_Disable(uartId,UART_RX); // UART_Disable(uartId,UART_RX);
// } // }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief LCD draw a 32*32 chinese character in lattice mode * @brief LCD draw a 32*32 chinese character in lattice mode
* *
@ -609,24 +606,20 @@ void LCD_DrawChinese(uint16_t x,uint16_t y,uint8_t* character,uint16_t bColor,ui
{ {
uint8_t i, j; uint8_t i, j;
LCD_Set_Addr(x, y, x + 31, y + 31); LCD_Set_Addr(x, y, x + 31, y + 31);
for(j=0;j<128;j++)
{ for (j = 0; j < 128; j++) {
for(i=0;i<8;i++) for (i = 0; i < 8; i++) {
{ if ((*character & (1 << i)) != 0) {
if((*character&(1<<i)) != 0)
{
LCD_WR_HalfWord(cColor); LCD_WR_HalfWord(cColor);
} } else {
else
{
LCD_WR_HalfWord(bColor); LCD_WR_HalfWord(bColor);
} }
} }
character++; character++;
} }
} }
/*@} end of group TFT_LCD_Public_Functions */ /*@} end of group TFT_LCD_Public_Functions */
/*@} end of group TFT_LCD */ /*@} end of group TFT_LCD */

View file

@ -49,8 +49,7 @@
struct device *image_sensor_i2c = NULL; struct device *image_sensor_i2c = NULL;
static const uint8_t sensorRegList[][2] = static const uint8_t sensorRegList[][2] = {
{
#if (IMAGE_SENSOR_USE == IMAGE_SENSOR_BF2013) #if (IMAGE_SENSOR_USE == IMAGE_SENSOR_BF2013)
{ 0x12, 0x80 }, { 0x12, 0x80 },
{ 0x67, 0x00 }, { 0x67, 0x00 },
@ -653,16 +652,17 @@ uint8_t image_sensor_init(BL_Fun_Type mjpeg_en, cam_device_t *cam_cfg, mjpeg_dev
{ {
i2c_register(I2C0_INDEX, "i2c", DEVICE_OFLAG_RDWR); i2c_register(I2C0_INDEX, "i2c", DEVICE_OFLAG_RDWR);
image_sensor_i2c = device_find("i2c"); image_sensor_i2c = device_find("i2c");
if (image_sensor_i2c) { if (image_sensor_i2c) {
device_open(image_sensor_i2c, 0); device_open(image_sensor_i2c, 0);
} }
if(image_sensor_read_id())
{ if (image_sensor_read_id()) {
MSG("err1\r\n"); MSG("err1\r\n");
return 1; return 1;
} }
if(image_sensor_regs_config())
{ if (image_sensor_regs_config()) {
MSG("err2\r\n"); MSG("err2\r\n");
return 1; return 1;
} }
@ -670,13 +670,10 @@ uint8_t image_sensor_init(BL_Fun_Type mjpeg_en, cam_device_t *cam_cfg, mjpeg_dev
cam_stop(); cam_stop();
mjpeg_stop(); mjpeg_stop();
if(mjpeg_en) if (mjpeg_en) {
{
cam_init(cam_cfg); cam_init(cam_cfg);
mjpeg_init(mjpeg_cfg); mjpeg_init(mjpeg_cfg);
} } else {
else
{
cam_init(cam_cfg); cam_init(cam_cfg);
} }
@ -703,7 +700,6 @@ uint8_t image_sensor_write_byte(uint8_t cmd, uint8_t data)
msg1.subaddr = cmd; msg1.subaddr = cmd;
return i2c_transfer(image_sensor_i2c, &msg1, 1); return i2c_transfer(image_sensor_i2c, &msg1, 1);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -729,7 +725,6 @@ uint8_t image_sensor_read_byte(uint8_t cmd)
return temp; return temp;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief CAMERA Read ID * @brief CAMERA Read ID
* *
@ -743,13 +738,11 @@ static uint8_t image_sensor_read_id(void)
uint8_t buf[2] = { 0 }; uint8_t buf[2] = { 0 };
buf[0] = image_sensor_read_byte(GC0308_ID); buf[0] = image_sensor_read_byte(GC0308_ID);
// MSG("image_sensor id:0x%2x\r\n",buf[0]); // MSG("image_sensor id:0x%2x\r\n",buf[0]);
if (buf[0] == 0x9b) if (buf[0] == 0x9b) {
{
return SUCCESS; return SUCCESS;
} } else {
else
{
return ERROR; return ERROR;
} }
} }
@ -758,18 +751,17 @@ static uint8_t image_sensor_regs_config(void)
{ {
int i; int i;
for (i = 0; i < sizeof(sensorRegList) / sizeof(sensorRegList[0]); i++) for (i = 0; i < sizeof(sensorRegList) / sizeof(sensorRegList[0]); i++) {
{ if (image_sensor_write_byte(sensorRegList[i][0], sensorRegList[i][1]) != SUCCESS) {
if (image_sensor_write_byte(sensorRegList[i][0],sensorRegList[i][1]) != SUCCESS)
{
return ERROR; return ERROR;
} }
bflb_platform_delay_ms(1); bflb_platform_delay_ms(1);
} }
return SUCCESS; return SUCCESS;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Dump image sensor register while is set * @brief Dump image sensor register while is set
* *
@ -782,8 +774,7 @@ void image_sensor_dump_regs(void)
{ {
uint32_t i; uint32_t i;
for (i = 0; i < sizeof(sensorRegList) / sizeof(sensorRegList[0]); i++) for (i = 0; i < sizeof(sensorRegList) / sizeof(sensorRegList[0]); i++) {
{
MSG("reg[%02x]: %02x\n", sensorRegList[i][0], image_sensor_read_byte(sensorRegList[i][0])); MSG("reg[%02x]: %02x\n", sensorRegList[i][0], image_sensor_read_byte(sensorRegList[i][0]));
} }
} }
@ -801,12 +792,10 @@ struct device* timer_ch0_comp2 = NULL;
void timer_ch0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state) void timer_ch0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{ {
static uint32_t i = 0; static uint32_t i = 0;
if( i & 1)
{ if (i & 1) {
gpio_write(GPIO_PIN_22, 1); gpio_write(GPIO_PIN_22, 1);
} } else {
else
{
gpio_write(GPIO_PIN_22, 0); gpio_write(GPIO_PIN_22, 0);
} }
} }
@ -820,8 +809,8 @@ void tr_timer_init(void)
timer_register(TIMER_CH0_INDEX, "timer_ch0_comp2", DEVICE_OFLAG_RDWR); timer_register(TIMER_CH0_INDEX, "timer_ch0_comp2", DEVICE_OFLAG_RDWR);
timer_ch0_comp2 = device_find("timer_ch0_comp2"); timer_ch0_comp2 = device_find("timer_ch0_comp2");
if(timer_ch0_comp2)
{ if (timer_ch0_comp2) {
device_open(timer_ch0_comp2, 0); device_open(timer_ch0_comp2, 0);
device_set_callback(timer_ch0_comp2, timer_ch0_irq_callback); device_set_callback(timer_ch0_comp2, timer_ch0_irq_callback);
device_control(timer_ch0_comp2, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg)); device_control(timer_ch0_comp2, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg));

View file

@ -103,7 +103,6 @@ void lv_port_disp_init(void)
* Register the display in LVGL * Register the display in LVGL
*----------------------------------*/ *----------------------------------*/
lv_disp_drv_init(&Disp_Drv); /*Basic initialization*/ lv_disp_drv_init(&Disp_Drv); /*Basic initialization*/
/*Set up the functions to access to your display*/ /*Set up the functions to access to your display*/
@ -170,8 +169,7 @@ static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_colo
static uint8_t rotated_dir = 0; static uint8_t rotated_dir = 0;
uint32_t length = (area->y2 - area->y1 + 1) * (area->x2 - area->x1 + 1); uint32_t length = (area->y2 - area->y1 + 1) * (area->x2 - area->x1 + 1);
if(rotated_dir != disp_drv->rotated) if (rotated_dir != disp_drv->rotated) {
{
rotated_dir = disp_drv->rotated; rotated_dir = disp_drv->rotated;
LCD_Set_Dir(rotated_dir); LCD_Set_Dir(rotated_dir);
} }
@ -202,6 +200,7 @@ static void gpu_blend(lv_disp_drv_t * disp_drv, lv_color_t * dest, const lv_colo
{ {
/*It's an example code which should be done by your GPU*/ /*It's an example code which should be done by your GPU*/
uint32_t i; uint32_t i;
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
dest[i] = lv_color_mix(dest[i], src[i], opa); dest[i] = lv_color_mix(dest[i], src[i], opa);
} }
@ -220,6 +219,7 @@ static void gpu_fill(lv_disp_drv_t * disp_drv, lv_color_t * dest_buf, lv_coord_t
for (x = fill_area->x1; x <= fill_area->x2; x++) { for (x = fill_area->x1; x <= fill_area->x2; x++) {
dest_buf[x] = color; dest_buf[x] = color;
} }
dest_buf += dest_width; /*Go to the next line*/ dest_buf += dest_width; /*Go to the next line*/
} }
} }

View file

@ -115,46 +115,53 @@ void lv_port_fs_init(void)
**********************/ **********************/
lv_fs_res_t res_fatfs_to_lv(FRESULT res) lv_fs_res_t res_fatfs_to_lv(FRESULT res)
{ {
if(res==FR_OK) if (res == FR_OK) {
{
return LV_FS_RES_OK; return LV_FS_RES_OK;
} }
switch (res) switch (res) {
{
case (FR_DISK_ERR): case (FR_DISK_ERR):
res = LV_FS_RES_HW_ERR; res = LV_FS_RES_HW_ERR;
break; break;
case (FR_NO_FILE): case (FR_NO_FILE):
res = LV_FS_RES_NOT_EX; res = LV_FS_RES_NOT_EX;
break; break;
case (FR_NO_PATH): case (FR_NO_PATH):
res = LV_FS_RES_NOT_EX; res = LV_FS_RES_NOT_EX;
break; break;
case (FR_NOT_ENOUGH_CORE): case (FR_NOT_ENOUGH_CORE):
res = LV_FS_RES_OUT_OF_MEM; res = LV_FS_RES_OUT_OF_MEM;
break; break;
case (FR_LOCKED): case (FR_LOCKED):
res = LV_FS_RES_LOCKED; res = LV_FS_RES_LOCKED;
case (FR_TOO_MANY_OPEN_FILES): case (FR_TOO_MANY_OPEN_FILES):
res = LV_FS_RES_LOCKED; res = LV_FS_RES_LOCKED;
break; break;
case (FR_NO_FILESYSTEM): case (FR_NO_FILESYSTEM):
res = LV_FS_RES_FS_ERR; res = LV_FS_RES_FS_ERR;
break; break;
case (FR_WRITE_PROTECTED): case (FR_WRITE_PROTECTED):
res = LV_FS_RES_DENIED; res = LV_FS_RES_DENIED;
break; break;
case (FR_TIMEOUT): case (FR_TIMEOUT):
res = LV_FS_RES_TOUT; res = LV_FS_RES_TOUT;
break; break;
default: default:
res = LV_FS_RES_UNKNOWN; res = LV_FS_RES_UNKNOWN;
} }
return res; return res;
} }
/* Initialize your Storage device and File system. */ /* Initialize your Storage device and File system. */
static void fs_init(void) static void fs_init(void)
{ {
@ -163,7 +170,6 @@ static void fs_init(void)
/*You code here*/ /*You code here*/
fatfs_sd_driver_register(); fatfs_sd_driver_register();
f_mount(&FS_OBJ_SD, "sd:", 1); f_mount(&FS_OBJ_SD, "sd:", 1);
} }
/** /**
@ -180,8 +186,7 @@ static lv_fs_res_t fs_open (lv_fs_drv_t * drv, void * file_p, const char * path,
BYTE fatfs_mode; BYTE fatfs_mode;
char *path_buf = NULL; char *path_buf = NULL;
switch (drv->letter) switch (drv->letter) {
{
case 'S': case 'S':
path_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(path) + 4)); path_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(path) + 4));
sprintf(path_buf, "SD:/%s", path); sprintf(path_buf, "SD:/%s", path);
@ -192,17 +197,19 @@ static lv_fs_res_t fs_open (lv_fs_drv_t * drv, void * file_p, const char * path,
break; break;
} }
switch (mode) switch (mode) {
{
case (LV_FS_MODE_RD): case (LV_FS_MODE_RD):
fatfs_mode = FA_READ; fatfs_mode = FA_READ;
break; break;
case (LV_FS_MODE_WR): case (LV_FS_MODE_WR):
fatfs_mode = FA_WRITE; fatfs_mode = FA_WRITE;
break; break;
case (LV_FS_MODE_WR | LV_FS_MODE_RD): case (LV_FS_MODE_WR | LV_FS_MODE_RD):
fatfs_mode = FA_WRITE | FA_READ; fatfs_mode = FA_WRITE | FA_READ;
break; break;
default: default:
fatfs_mode = LV_FS_MODE_RD; fatfs_mode = LV_FS_MODE_RD;
break; break;
@ -338,12 +345,12 @@ static lv_fs_res_t fs_remove (lv_fs_drv_t * drv, const char *path)
/* Add your code here*/ /* Add your code here*/
char *path_buf = NULL; char *path_buf = NULL;
switch (drv->letter) switch (drv->letter) {
{
case 'S': case 'S':
path_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(path) + 4)); path_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(path) + 4));
sprintf(path_buf, "SD:/%s", path); sprintf(path_buf, "SD:/%s", path);
break; break;
default: default:
return LV_FS_RES_NOT_EX; return LV_FS_RES_NOT_EX;
break; break;
@ -389,14 +396,14 @@ static lv_fs_res_t fs_rename (lv_fs_drv_t * drv, const char * oldname, const cha
char *path_old_buf = NULL; char *path_old_buf = NULL;
char *path_new_buf = NULL; char *path_new_buf = NULL;
switch (drv->letter) switch (drv->letter) {
{
case 'S': case 'S':
path_old_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(oldname) + 4)); path_old_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(oldname) + 4));
path_new_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(newname) + 4)); path_new_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(newname) + 4));
sprintf(path_old_buf, "SD:/%s", oldname); sprintf(path_old_buf, "SD:/%s", oldname);
sprintf(path_new_buf, "SD:/%s", newname); sprintf(path_new_buf, "SD:/%s", newname);
break; break;
default: default:
return LV_FS_RES_NOT_EX; return LV_FS_RES_NOT_EX;
break; break;
@ -425,12 +432,12 @@ static lv_fs_res_t fs_free (lv_fs_drv_t * drv, uint32_t * total_p, uint32_t * fr
FATFS *fs_obj; FATFS *fs_obj;
/* Add your code here*/ /* Add your code here*/
switch (drv->letter) switch (drv->letter) {
{
case 'S': case 'S':
path = "SD:"; path = "SD:";
fs_obj = &FS_OBJ_SD; fs_obj = &FS_OBJ_SD;
break; break;
default: default:
return LV_FS_RES_NOT_EX; return LV_FS_RES_NOT_EX;
break; break;
@ -458,12 +465,12 @@ static lv_fs_res_t fs_dir_open (lv_fs_drv_t * drv, void * rddir_p, const char *p
/* Add your code here*/ /* Add your code here*/
char *path_buf = NULL; char *path_buf = NULL;
switch (drv->letter) switch (drv->letter) {
{
case 'S': case 'S':
path_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(path) + 4)); path_buf = (char *)lv_mem_alloc(sizeof(char) * (strlen(path) + 4));
sprintf(path_buf, "SD:/%s", path); sprintf(path_buf, "SD:/%s", path);
break; break;
default: default:
return LV_FS_RES_NOT_EX; return LV_FS_RES_NOT_EX;
break; break;
@ -493,8 +500,7 @@ static lv_fs_res_t fs_dir_read (lv_fs_drv_t * drv, void * rddir_p, char *fn)
res = f_readdir(rddir_p, &entry); res = f_readdir(rddir_p, &entry);
res = res_fatfs_to_lv(res); res = res_fatfs_to_lv(res);
if(res == LV_FS_RES_OK) if (res == LV_FS_RES_OK) {
{
sprintf(fn, "%s%s", (entry.fattrib & AM_DIR) ? "/" : "", entry.fname); sprintf(fn, "%s%s", (entry.fattrib & AM_DIR) ? "/" : "", entry.fname);
} }

View file

@ -227,7 +227,9 @@ static lv_coord_t touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
p_disp_drv_cb = lv_disp_get_default(); p_disp_drv_cb = lv_disp_get_default();
while(p_disp_drv_cb->driver.buffer->flushing); while (p_disp_drv_cb->driver.buffer->flushing)
;
device_control(touch_spi, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)3600000); device_control(touch_spi, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)3600000);
res = touch_read(x, y); res = touch_read(x, y);
device_control(touch_spi, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)36000000); device_control(touch_spi, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)36000000);
@ -305,8 +307,8 @@ static void keypad_init(void)
adc_register(ADC0_INDEX, "adc_key", DEVICE_OFLAG_STREAM_RX); adc_register(ADC0_INDEX, "adc_key", DEVICE_OFLAG_STREAM_RX);
adc_key = device_find("adc_key"); adc_key = device_find("adc_key");
if(adc_key)
{ if (adc_key) {
ADC_DEV(adc_key)->continuous_conv_mode = ENABLE; ADC_DEV(adc_key)->continuous_conv_mode = ENABLE;
device_open(adc_key, DEVICE_OFLAG_STREAM_RX); device_open(adc_key, DEVICE_OFLAG_STREAM_RX);
device_control(adc_key, DEVICE_CTRL_ADC_CHANNEL_CONFIG, &adc_channel_cfg); device_control(adc_key, DEVICE_CTRL_ADC_CHANNEL_CONFIG, &adc_channel_cfg);
@ -324,28 +326,36 @@ static bool keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
/*Get whether the a key is pressed and save the pressed key*/ /*Get whether the a key is pressed and save the pressed key*/
uint32_t act_key = keypad_get_key(); uint32_t act_key = keypad_get_key();
if (act_key != 0) { if (act_key != 0) {
data->state = LV_INDEV_STATE_PR; data->state = LV_INDEV_STATE_PR;
/*Translate the keys to LVGL control characters according to your key definitions*/ /*Translate the keys to LVGL control characters according to your key definitions*/
switch (act_key) { switch (act_key) {
case 1: case 1:
act_key = LV_KEY_LEFT; act_key = LV_KEY_LEFT;
break; break;
case 2: case 2:
act_key = LV_KEY_RIGHT; act_key = LV_KEY_RIGHT;
break; break;
case 3: case 3:
act_key = LV_KEY_UP; act_key = LV_KEY_UP;
break; break;
case 4: case 4:
act_key = LV_KEY_DOWN; act_key = LV_KEY_DOWN;
break; break;
case 5: case 5:
act_key = LV_KEY_ENTER; act_key = LV_KEY_ENTER;
break; break;
default: default:
break; break;
} }
last_key = act_key; last_key = act_key;
} else { } else {
@ -370,35 +380,29 @@ static uint32_t keypad_get_key(void)
device_read(adc_key, 0, (void *)&result_val, sizeof(result_val) / sizeof(adc_channel_val_t)); device_read(adc_key, 0, (void *)&result_val, sizeof(result_val) / sizeof(adc_channel_val_t));
key_voltage = result_val.volt * 1000; key_voltage = result_val.volt * 1000;
for(key=0;key<sizeof(key_value)/sizeof(key_value[0]);key++) for (key = 0; key < sizeof(key_value) / sizeof(key_value[0]); key++) {
{ if (DIFF(key_voltage, key_value[key]) < KEY_ADC_DIFF_MAX) {
if(DIFF(key_voltage,key_value[key]) < KEY_ADC_DIFF_MAX )
{
break; break;
} }
} }
key += 1; key += 1;
if( key > sizeof(key_value))
{ if (key > sizeof(key_value)) {
key = 0; key = 0;
} }
if(key == last_key) if (key == last_key) {
{
old_key_v = key; old_key_v = key;
old_key_num = 0; old_key_num = 0;
} } else if (key == old_key_v) {
else if(key==old_key_v)
{
old_key_num++; old_key_num++;
if(old_key_num >= KEY_NOISE_NUM_MAX)
{ if (old_key_num >= KEY_NOISE_NUM_MAX) {
last_key = key; last_key = key;
old_key_num = 0; old_key_num = 0;
} }
} } else {
else
{
old_key_num = 0; old_key_num = 0;
old_key_v = key; old_key_v = key;
} }

View file

@ -0,0 +1 @@
#include "phy_8720.c"

View file

@ -0,0 +1,402 @@
/**
* @file ethernetif.h
* @brief
*
* Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
/* Includes ------------------------------------------------------------------*/
#include "lwip/opt.h"
#include "lwip/timeouts.h"
#include "lwip/netif.h"
#include "netif/etharp.h"
#include "ethernetif.h"
#include <string.h>
#include "hal_emac.h"
#include <FreeRTOS.h>
#include "semphr.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Network interface name */
#define IFNAME0 'b'
#define IFNAME1 'l'
#define ETH_RX_BUFFER_SIZE (1536UL)
#define ETH_DMA_TRANSMIT_TIMEOUT (20U)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/*
@Note: This interface is implemented to operate in zero-copy mode only:
- Rx buffers are allocated statically and passed directly to the LwIP stack,
they will return back to ETH DMA after been processed by the stack.
- Tx Buffers will be allocated from LwIP stack memory heap,
then passed to ETH HAL driver.
@Notes:
1.a. ETH DMA Rx descriptors must be contiguous, the default count is 4,
to customize it please redefine ETH_RX_DESC_CNT in stm32xxxx_hal_conf.h
1.b. ETH DMA Tx descriptors must be contiguous, the default count is 4,
to customize it please redefine ETH_TX_DESC_CNT in stm32xxxx_hal_conf.h
2.a. Rx Buffers number must be between ETH_RX_DESC_CNT and 2*ETH_RX_DESC_CNT
2.b. Rx Buffers must have the same size: ETH_RX_BUFFER_SIZE, this value must
passed to ETH DMA in the init field (EthHandle.Init.RxBuffLen)
2.c The RX Ruffers addresses and sizes must be properly defined to be aligned
to L1-CACHE line size (32 bytes).
*/
#if LWIP_DHCP
#define MAX_DHCP_TRIES 4
uint32_t DHCPfineTimer = 0;
uint8_t DHCP_state = DHCP_OFF;
#endif
/* Private function prototypes -----------------------------------------------*/
void pbuf_free_custom(struct pbuf *p);
void ethernetif_input(void *argument);
SemaphoreHandle_t emac_rx_sem = NULL;
static StackType_t emac_rx_stack[256];
static StaticTask_t emac_rx_handle;
static uint8_t emac_rx_buffer[ETH_RX_BUFFER_SIZE] __attribute__((aligned(16))) = { 0 };
LWIP_MEMPOOL_DECLARE(RX_POOL, 10, sizeof(struct pbuf_custom), "Zero-copy RX PBUF pool");
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
LL Driver Interface ( LwIP stack --> ETH)
*******************************************************************************/
/**
* @brief In this function, the hardware should be initialized.
* Called from ethernetif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
extern void emac_init_txrx_buffer(void);
extern int emac_phy_init(emac_phy_cfg_t *cfg);
void low_level_init(struct netif *netif)
{
emac_device_t emac_cfg = {
.mac_addr[0] = 0x18,
.mac_addr[1] = 0xB9,
.mac_addr[2] = 0x05,
.mac_addr[3] = 0x12,
.mac_addr[4] = 0x34,
.mac_addr[5] = 0x56,
};
/* set MAC hardware address length */
netif->hwaddr_len = ETH_HWADDR_LEN;
/* set MAC hardware address */
netif->hwaddr[0] = emac_cfg.mac_addr[0];
netif->hwaddr[1] = emac_cfg.mac_addr[1];
netif->hwaddr[2] = emac_cfg.mac_addr[2];
netif->hwaddr[3] = emac_cfg.mac_addr[3];
netif->hwaddr[4] = emac_cfg.mac_addr[4];
netif->hwaddr[5] = emac_cfg.mac_addr[5];
/* maximum transfer unit */
netif->mtu = 1500;
/* emac init,configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
MSG("emac_init\r\n");
emac_init(&emac_cfg);
emac_phy_init(NULL);
emac_init_txrx_buffer();
/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
/* Initialize the RX POOL */
LWIP_MEMPOOL_INIT(RX_POOL);
/* create a binary semaphore used for informing ethernetif of frame reception */
//vSemaphoreCreateBinary(emac_rx_sem);
emac_rx_sem = xSemaphoreCreateBinary();
/* create the task that handles the ETH_MAC */
MSG("[OS] Starting emac rx task...\r\n");
xTaskCreateStatic(ethernetif_input, (char *)"emac_rx_task", sizeof(emac_rx_stack) / 4, netif, 15, emac_rx_stack, &emac_rx_handle);
//xTaskCreateStatic(test_task, (char*)"test_task", sizeof(test_stack)/4, NULL, 16, test_stack, &test_handle);
ethernet_link_check_state(netif);
netif_set_up(netif);
netif_set_link_up(netif);
}
void emac_tx_error_callback_app()
{
MSG("EMAC tx error callback\r\n");
}
void emac_rx_error_callback_app()
{
MSG("EMAC rx error callback\r\n");
}
/**
* @brief This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
* might be chained.
*
* @param netif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
*
* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
* strange results. You might consider waiting for space in the DMA queue
* to become available since the stack doesn't retry to send a packet
* dropped because of memory failure (except for the TCP timers).
*/
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
err_t errval = ERR_OK;
struct pbuf *q;
for (q = p; q != NULL; q = q->next) {
if (0 != emac_bd_tx_enqueue(-1, q->len, q->payload)) {
MSG("emac_bd_tx_enqueue error!\r\n");
return ERR_IF;
}
}
return errval;
}
/**
* @brief Should allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*
* @param netif the lwip network interface structure for this ethernetif
* @return a pbuf filled with the received packet (including MAC header)
* NULL on memory error
*/
static struct pbuf *low_level_input(struct netif *netif)
{
uint32_t rx_len = 0;
struct pbuf *p = NULL, *q;
emac_bd_rx_dequeue(-1, &rx_len, emac_rx_buffer);
if (rx_len <= 0) {
//MSG("Recv Null Data\r\n");
return NULL;
}
//MSG("Recv full Data\r\n");
p = pbuf_alloc(PBUF_RAW, rx_len, PBUF_POOL);
if (p != NULL) {
for (q = p; q != NULL; q = q->next) {
memcpy(q->payload, emac_rx_buffer + rx_len - q->tot_len, q->len);
}
}
return p;
}
void emac_rx_done_callback_app(void)
{
BaseType_t xHigherPriorityTaskWoken;
/* Is it time for vATask() to run? */
xHigherPriorityTaskWoken = pdFALSE;
//MSG("emac_rx_done_callback_app\r\n");
//low_level_input(NULL);
xSemaphoreGiveFromISR(emac_rx_sem, &xHigherPriorityTaskWoken);
/* If xHigherPriorityTaskWoken was set to true you
we should yield. The actual macro used here is
port specific. */
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/**
* @brief This function is the ethernetif_input task, it is processed when a packet
* is ready to be read from the interface. It uses the function low_level_input()
* that should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
*
* @param netif the lwip network interface structure for this ethernetif
*/
void ethernetif_input(void *argument)
{
struct pbuf *p = NULL;
struct netif *netif = (struct netif *)argument;
for (;;) {
if (xSemaphoreTake(emac_rx_sem, portMAX_DELAY) == pdTRUE) {
do {
//MSG("ethernetif_input\r\n");
p = low_level_input(netif);
if (p != NULL) {
if (netif->input(p, netif) != ERR_OK) {
pbuf_free(p);
}
}
} while (p != NULL);
}
}
}
/**
* @brief Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t ethernetif_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */
netif->output = etharp_output;
netif->linkoutput = low_level_output;
/* initialize the hardware */
low_level_init(netif);
return ERR_OK;
}
/**
* @brief Custom Rx pbuf free callback
* @param pbuf: pbuf to be freed
* @retval None
*/
void pbuf_free_custom(struct pbuf *p)
{
struct pbuf_custom *custom_pbuf = (struct pbuf_custom *)p;
LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
}
/**
* @brief Notify the User about the network interface config status
* @param netif: the network interface
* @retval None
*/
void ethernet_link_status_updated(struct netif *netif)
{
if (netif_is_link_up(netif)) {
#if LWIP_DHCP
/* Update DHCP state machine */
DHCP_state = DHCP_START;
MSG("DHCP Start\n");
#else
uint8_t iptxt[20];
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
MSG("Static IP address: %s\n", iptxt);
#endif
} else {
#if LWIP_DHCP
/* Update DHCP state machine */
DHCP_state = DHCP_LINK_DOWN;
#else
MSG("The network cable is not connected \n");
#endif /* LWIP_DHCP */
}
}
/**
* @brief
* @retval None
*/
void ethernet_link_check_state(struct netif *netif)
{
#if 0
ETH_MACConfigTypeDef MACConf;
uint32_t PHYLinkState;
uint32_t linkchanged = 0, speed = 0, duplex = 0;
PHYLinkState = LAN8742_GetLinkState(&LAN8742);
if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN))
{
HAL_ETH_Stop(&EthHandle);
netif_set_down(netif);
netif_set_link_down(netif);
}
else if(!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN))
{
switch(PHYLinkState)
{
case LAN8742_STATUS_100MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_100M;
linkchanged = 1;
break;
case LAN8742_STATUS_100MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_100M;
linkchanged = 1;
break;
case LAN8742_STATUS_10MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_10M;
linkchanged = 1;
break;
case LAN8742_STATUS_10MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_10M;
linkchanged = 1;
break;
default:
break;
}
if(linkchanged)
{
/* Get MAC Config MAC */
HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
MACConf.DuplexMode = duplex;
MACConf.Speed = speed;
HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
HAL_ETH_Start(&EthHandle);
netif_set_up(netif);
netif_set_link_up(netif);
}
}
#endif
}

View file

@ -0,0 +1,40 @@
/**
* @file ethernetif.h
* @brief
*
* Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
#ifndef __ETHERNETIF_H__
#define __ETHERNETIF_H__
#include "lwip/err.h"
#include "lwip/netif.h"
#define DHCP_OFF (uint8_t)0
#define DHCP_START (uint8_t)1
#define DHCP_WAIT_ADDRESS (uint8_t)2
#define DHCP_ADDRESS_ASSIGNED (uint8_t)3
#define DHCP_TIMEOUT (uint8_t)4
#define DHCP_LINK_DOWN (uint8_t)5
/* Exported types ------------------------------------------------------------*/
err_t ethernetif_init(struct netif *netif);
void ethernet_link_check_state(struct netif *netif);
void ethernet_link_status_updated(struct netif *netif);
#endif

View file

@ -0,0 +1,318 @@
#include <string.h>
#include "hal_emac.h"
/* LAN8720 PHY Address*/
#define EMAC_PHY_ADDRESS 0x00U
#define PHY_LINK_TO ((uint32_t)0x00000FFFU)
#define PHY_AUTONEGO_COMPLETED_TO ((uint32_t)0x00000FFFU)
/* Section 3: Common PHY Registers */
#define PHY_BCR ((uint16_t)0x00U) /*!< Transceiver Basic Control Register */
#define PHY_BSR ((uint16_t)0x01U) /*!< Transceiver Basic Status Register */
#define PHY_BSR_100BASETXFULL (1 << 14)
#define PHY_BSR_100BASETXHALF (1 << 13)
#define PHY_BSR_10BASETXFULL (1 << 12)
#define PHY_BSR_10BASETXHALF (1 << 11)
#define PHY_RESET ((uint16_t)0x8000U) /*!< PHY Reset */
#define PHY_LOOPBACK ((uint16_t)0x4000U) /*!< Select loop-back mode */
#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100U) /*!< Set the full-duplex mode at 100 Mb/s */
#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000U) /*!< Set the half-duplex mode at 100 Mb/s */
#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100U) /*!< Set the full-duplex mode at 10 Mb/s */
#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000U) /*!< Set the half-duplex mode at 10 Mb/s */
#define PHY_AUTONEGOTIATION ((uint16_t)0x1000U) /*!< Enable auto-negotiation function */
#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200U) /*!< Restart auto-negotiation function */
#define PHY_POWERDOWN ((uint16_t)0x0800U) /*!< Select the power down mode */
#define PHY_ISOLATE ((uint16_t)0x0400U) /*!< Isolate PHY from MII */
#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020U) /*!< Auto-Negotiation process completed */
#define PHY_LINKED_STATUS ((uint16_t)0x0004U) /*!< Valid link established */
#define PHY_JABBER_DETECTION ((uint16_t)0x0002U) /*!< Jabber condition detected */
#define PHY_PHYID1 ((uint16_t)0x02U) /*!< PHY ID 1 */
#define PHY_PHYID2 ((uint16_t)0x03U) /*!< PHY ID 2 */
#define PHY_ADVERTISE ((uint16_t)0x04U) /*!< Auto-negotiation advertisement */
#define PHY_ADVERTISE_100BASETXFULL (1 << 8)
#define PHY_ADVERTISE_100BASETXHALF (1 << 7)
#define PHY_ADVERTISE_10BASETXFULL (1 << 6)
#define PHY_ADVERTISE_10BASETXHALF (1 << 5)
#define PHY_ADVERTISE_8023 (1 << 0)
#define PHY_LPA ((uint16_t)0x05U) /*!< Auto-negotiation link partner base page ability */
#define PHY_EXPANSION ((uint16_t)0x06U) /*!< Auto-negotiation expansion */
/* Section 4: Extended PHY Registers */
#define PHY_SR ((uint16_t)0x1FU) /*!< PHY special control/ status register Offset */
#define PHY_SR_SPEED_OFFSET (2)
#define PHY_SR_SPEED_MASK (0x7 << PHY_SR_SPEED_OFFSET)
#define PHY_SR_SPEED_10BASETXHALF (0x1 << PHY_SR_SPEED_OFFSET)
#define PHY_SR_SPEED_10BASETXFULL (0x5 << PHY_SR_SPEED_OFFSET)
#define PHY_SR_SPEED_100BASETXHALF (0x2 << PHY_SR_SPEED_OFFSET)
#define PHY_SR_SPEED_100BASETXFULL (0x6 << PHY_SR_SPEED_OFFSET)
#define PHY_SR_SPEED_MODE_COMPARE(status, mode) (!!(mode == (status & PHY_SR_SPEED_MASK)))
#define PHY_SPEED_STATUS ((uint16_t)0x0004U) /*!< PHY Speed mask */
#define PHY_DUPLEX_STATUS ((uint16_t)0x0010U) /*!< PHY Duplex mask */
#define PHY_ISFR ((uint16_t)0x1DU) /*!< PHY Interrupt Source Flag register Offset */
#define PHY_ISFR_INT4 ((uint16_t)0x0010U) /*!< PHY Link down inturrupt */
int phy_8720_reset(void)
{
int timeout = 10;
uint16_t regval = PHY_RESET;
if (emac_phy_reg_write(PHY_BCR, PHY_RESET) != 0) {
return -1;
}
for (; timeout; timeout--) {
if (0 != emac_phy_reg_read(PHY_BCR, &regval)) {
return -1;
}
if (!(regval & PHY_RESET)) {
return 0;
}
bflb_platform_delay_ms(1);
}
return -1;
}
int phy_8720_auto_negotiate(emac_phy_cfg_t *cfg)
{
uint16_t regval = 0;
uint16_t advertise = 0;
uint16_t lpa = 0;
uint32_t timeout = 100; //10s,in 100ms
if (0 != emac_phy_reg_read(PHY_PHYID1, &regval)) {
return -1;
}
if (0 != emac_phy_reg_read(PHY_PHYID2, &regval)) {
return -1;
}
if (0 != emac_phy_reg_read(PHY_BCR, &regval)) {
return -1;
}
regval &= ~PHY_AUTONEGOTIATION;
regval &= ~(PHY_LOOPBACK | PHY_POWERDOWN);
regval |= PHY_ISOLATE;
if (emac_phy_reg_write(PHY_BCR, regval) != 0) {
return -1;
}
/* set advertisement mode */
advertise = PHY_ADVERTISE_100BASETXFULL | PHY_ADVERTISE_100BASETXHALF |
PHY_ADVERTISE_10BASETXFULL | PHY_ADVERTISE_10BASETXHALF |
PHY_ADVERTISE_8023;
if (emac_phy_reg_write(PHY_ADVERTISE, advertise) != 0) {
return -1;
}
bflb_platform_delay_ms(16);
if (0 != emac_phy_reg_read(PHY_BCR, &regval)) {
return -1;
}
bflb_platform_delay_ms(16);
regval |= (PHY_FULLDUPLEX_100M | PHY_AUTONEGOTIATION);
if (emac_phy_reg_write(PHY_BCR, regval) != 0) {
return -1;
}
bflb_platform_delay_ms(16);
regval |= PHY_RESTART_AUTONEGOTIATION;
regval &= ~PHY_ISOLATE;
if (emac_phy_reg_write(PHY_BCR, regval) != 0) {
return -1;
}
bflb_platform_delay_ms(2000);
while (1) {
if (0 != emac_phy_reg_read(PHY_BSR, &regval)) {
return -1;
}
if (regval & PHY_AUTONEGO_COMPLETE) {
/* complete */
break;
}
if (!(--timeout)) {
return -1;
}
bflb_platform_delay_ms(100);
}
bflb_platform_delay_ms(5000);
if (0 != emac_phy_reg_read(PHY_LPA, &lpa)) {
return -1;
}
if (((advertise & lpa) & PHY_ADVERTISE_100BASETXFULL) != 0) {
/* 100BaseTX and Full Duplex */
cfg->full_duplex = 1;
cfg->speed = 100;
} else if (((advertise & lpa) & PHY_ADVERTISE_10BASETXFULL) != 0) {
/* 10BaseT and Full Duplex */
cfg->full_duplex = 1;
cfg->speed = 10;
} else if (((advertise & lpa) & PHY_ADVERTISE_100BASETXHALF) != 0) {
/* 100BaseTX and half Duplex */
cfg->full_duplex = 0;
cfg->speed = 100;
} else if (((advertise & lpa) & PHY_ADVERTISE_10BASETXHALF) != 0) {
/* 10BaseT and half Duplex */
cfg->full_duplex = 0;
cfg->speed = 10;
}
return 0;
}
int phy_8720_link_up(emac_phy_cfg_t *cfg)
{
uint16_t phy_bsr = 0;
uint16_t phy_sr = 0;
bflb_platform_delay_ms(16);
if (0 != emac_phy_reg_read(PHY_BSR, &phy_bsr)) {
return -1;
}
bflb_platform_delay_ms(16);
if (!(PHY_LINKED_STATUS & phy_bsr)) {
return ERROR;
}
bflb_platform_delay_ms(16);
if (0 != emac_phy_reg_read(PHY_SR, &phy_sr)) {
return -1;
}
if ((phy_bsr & PHY_BSR_100BASETXFULL) && PHY_SR_SPEED_MODE_COMPARE(phy_sr, PHY_SR_SPEED_100BASETXFULL)) {
/* 100BaseTX and Full Duplex */
cfg->full_duplex = 1;
cfg->speed = 100;
} else if ((phy_bsr & PHY_BSR_10BASETXFULL) && PHY_SR_SPEED_MODE_COMPARE(phy_sr, PHY_SR_SPEED_10BASETXFULL)) {
/* 10BaseT and Full Duplex */
cfg->full_duplex = 1;
cfg->speed = 10;
} else if ((phy_bsr & PHY_BSR_100BASETXHALF) && PHY_SR_SPEED_MODE_COMPARE(phy_sr, PHY_SR_SPEED_100BASETXHALF)) {
/* 100BaseTX and half Duplex */
cfg->full_duplex = 0;
cfg->speed = 100;
} else if ((phy_bsr & PHY_BSR_10BASETXHALF) && PHY_SR_SPEED_MODE_COMPARE(phy_sr, PHY_SR_SPEED_10BASETXHALF)) {
/* 10BaseT and half Duplex */
cfg->full_duplex = 0;
cfg->speed = 10;
} else {
/* 10BaseT and half Duplex */
cfg->full_duplex = -1;
cfg->speed = -1;
return -1;
}
if (cfg->full_duplex == 1) {
MSG("PHY_FULLDUPLEX\r\n");
} else {
MSG("PHY_HALFDUPLEX\r\n");
}
if (cfg->speed == 100) {
MSG("PHY_SPEED_100M\r\n");
} else {
MSG("EMAC_SPEED_50M\r\n");
}
MSG("PHY Init done\r\n");
return 0;
}
/****************************************************************************/ /**
* @brief Initialize EMAC PHY module
*
* @param cfg: EMAC PHY configuration pointer
*
* @return SUCCESS or ERROR
*
*******************************************************************************/
int emac_phy_init(emac_phy_cfg_t *cfg)
{
uint16_t phyReg;
emac_phy_cfg_t phy_cfg = {
.auto_negotiation = 1, /*!< Speed and mode auto negotiation */
.full_duplex = 1, /*!< Duplex mode */
.speed = 100, /*!< Speed mode */
.phy_address = 0, /*!< PHY address */
};
if (cfg != NULL) {
memcpy(&phy_cfg, cfg, sizeof(emac_phy_cfg_t));
}
cfg = &phy_cfg;
emac_phy_set_address(cfg->phy_address);
if (0 != phy_8720_reset()) {
return -1;
}
if (cfg->auto_negotiation) {
/*
uint32_t cnt=0;
do{
if(emac_phy_reg_read(PHY_BSR, &phyReg) != SUCCESS){
return ERROR;
}
cnt++;
if(cnt>PHY_LINK_TO){
return ERROR;
}
}while((phyReg & PHY_LINKED_STATUS) != PHY_LINKED_STATUS);
*/
if (0 != phy_8720_auto_negotiate(cfg)) {
return -1;
}
} else {
if (emac_phy_reg_read(PHY_BCR, &phyReg) != 0) {
return -1;
}
phyReg &= (~PHY_FULLDUPLEX_100M);
if (cfg->speed == 10) {
if (cfg->full_duplex == 1) {
phyReg |= PHY_FULLDUPLEX_10M;
} else {
phyReg |= PHY_HALFDUPLEX_10M;
}
} else {
if (cfg->full_duplex == 1) {
phyReg |= PHY_FULLDUPLEX_100M;
} else {
phyReg |= PHY_HALFDUPLEX_100M;
}
}
if ((emac_phy_reg_write(PHY_BCR, phyReg)) != 0) {
return -1;
}
}
emac_phy_config_full_duplex(cfg->full_duplex);
return phy_8720_link_up(cfg);
}

View file

@ -25,67 +25,17 @@
#include "hal_mtimer.h" #include "hal_mtimer.h"
#include "drv_mmheap.h" #include "drv_mmheap.h"
#include "ring_buffer.h" #include "ring_buffer.h"
#include "drv_shell.h"
extern uint32_t _HeapBase; extern uint32_t __HeapBase;
extern uint32_t _HeapSize; extern uint32_t __HeapLimit;
static uint8_t uart_dbg_disable = 0; static uint8_t uart_dbg_disable = 0;
#ifdef SHELL_SUPPORT
Shell shell;
char shellBuffer[512];
void uart_iqr_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{
uint8_t data;
if (state == UART_EVENT_RX_FIFO)
{
data = *(uint8_t *)args;
shellHandler(&shell, data);
}
}
void userShellWrite(char data)
{
struct device *uart = device_find("debug_log");
device_write(uart, 0, (uint8_t *)&data, 1);
}
#if SHELL_FS == 1
__WEAK__ int shellGetcwd(char *path, unsigned int len)
{
return 0;
}
#endif
void shell_init(void)
{
shell.write = userShellWrite;
#if SHELL_FS == 1
shell.getcwd = shellGetcwd;
#endif
shellInit(&shell, shellBuffer, 512);
}
#else
void uart_iqr_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{
if (state == UART_EVENT_RX_FIFO)
{
}
}
#endif
__WEAK__ void board_init(void) __WEAK__ void board_init(void)
{ {
} }
__WEAK__ void bl_show_info(void) __WEAK__ void bl_show_info(void)
{ {
} }
__WEAK__ enum uart_index_type board_get_debug_uart_index(void) __WEAK__ enum uart_index_type board_get_debug_uart_index(void)
@ -103,26 +53,18 @@ void bflb_platform_init(uint32_t baudrate)
uart_register(board_get_debug_uart_index(), "debug_log", DEVICE_OFLAG_RDWR); uart_register(board_get_debug_uart_index(), "debug_log", DEVICE_OFLAG_RDWR);
struct device *uart = device_find("debug_log"); struct device *uart = device_find("debug_log");
if (uart) if (uart) {
{
device_open(uart, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_INT_RX); device_open(uart, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_INT_RX);
device_set_callback(uart, uart_iqr_callback); device_set_callback(uart, NULL);
device_control(uart, DEVICE_CTRL_SET_INT, (void *)(UART_RX_FIFO_IT)); device_control(uart, DEVICE_CTRL_CLR_INT, (void *)(UART_RX_FIFO_IT));
} }
bl_show_info(); bl_show_info();
} }
#ifdef SHELL_SUPPORT if (!mmheap_init_with_pool(&__HeapBase, (size_t)&__HeapLimit - (size_t)&__HeapBase)) {
shell_init(); MSG("dynamic memory init success,heap size = %d Kbyte \r\n", ((size_t)&__HeapLimit - (size_t)&__HeapBase) / 1000);
#endif } else {
if (!mmheap_init_with_pool(&_HeapBase, (size_t)&_HeapSize))
{
// MSG("dynamic memory init success,heap size = 0x%x \r\n", &_HeapSize);
}
else
{
MSG("dynamic memory init error\r\n"); MSG("dynamic memory init error\r\n");
} }
@ -156,7 +98,6 @@ uint8_t bflb_platform_print_get(void)
void bflb_platform_deinit(void) void bflb_platform_deinit(void)
{ {
} }
void bflb_platform_dump(uint8_t *data, uint32_t len) void bflb_platform_dump(uint8_t *data, uint32_t len)
@ -164,26 +105,24 @@ void bflb_platform_dump(uint8_t *data, uint32_t len)
uint32_t i = 0; uint32_t i = 0;
if (!uart_dbg_disable) { if (!uart_dbg_disable) {
for (i = 0; i < len; i++) for (i = 0; i < len; i++) {
{ if (i % 16 == 0) {
if (i % 16 == 0)
{
bflb_platform_printf("\r\n"); bflb_platform_printf("\r\n");
} }
bflb_platform_printf("%02x ", data[i]); bflb_platform_printf("%02x ", data[i]);
} }
bflb_platform_printf("\r\n"); bflb_platform_printf("\r\n");
} }
} }
void bflb_platform_init_time() void bflb_platform_init_time()
{ {
} }
void bflb_platform_deinit_time() void bflb_platform_deinit_time()
{ {
} }
void bflb_platform_set_alarm_time(uint64_t time, void (*interruptFun)(void)) void bflb_platform_set_alarm_time(uint64_t time, void (*interruptFun)(void))
@ -193,17 +132,14 @@ void bflb_platform_set_alarm_time(uint64_t time,void( *interruptFun )( void ))
void bflb_platform_clear_time() void bflb_platform_clear_time()
{ {
} }
void bflb_platform_start_time() void bflb_platform_start_time()
{ {
} }
void bflb_platform_stop_time() void bflb_platform_stop_time()
{ {
} }
uint64_t bflb_platform_get_time_ms() uint64_t bflb_platform_get_time_ms()
@ -225,7 +161,8 @@ void bflb_platform_delay_us(uint32_t us)
mtimer_delay_us(us); mtimer_delay_us(us);
} }
void bflb_print_device_list(void){ void bflb_print_device_list(void)
{
struct device *dev; struct device *dev;
dlist_t *node; dlist_t *node;
uint8_t device_index = 0; uint8_t device_index = 0;
@ -239,33 +176,59 @@ void bflb_print_device_list(void){
MSG("Index %d\r\nDevice Name = %s \r\n", device_index, dev->name); MSG("Index %d\r\nDevice Name = %s \r\n", device_index, dev->name);
switch (dev->type) { switch (dev->type) {
case DEVICE_CLASS_GPIO: case DEVICE_CLASS_GPIO:
MSG("Device Type = %s \r\n","GPIO");break; MSG("Device Type = %s \r\n", "GPIO");
break;
case DEVICE_CLASS_UART: case DEVICE_CLASS_UART:
MSG("Device Type = %s \r\n","UART");break; MSG("Device Type = %s \r\n", "UART");
break;
case DEVICE_CLASS_SPI: case DEVICE_CLASS_SPI:
MSG("Device Type = %s \r\n","SPI");break; MSG("Device Type = %s \r\n", "SPI");
break;
case DEVICE_CLASS_I2C: case DEVICE_CLASS_I2C:
MSG("Device Type = %s \r\n","I2C");break; MSG("Device Type = %s \r\n", "I2C");
break;
case DEVICE_CLASS_ADC: case DEVICE_CLASS_ADC:
MSG("Device Type = %s \r\n","ADC");break; MSG("Device Type = %s \r\n", "ADC");
break;
case DEVICE_CLASS_DMA: case DEVICE_CLASS_DMA:
MSG("Device Type = %s \r\n","DMA");break; MSG("Device Type = %s \r\n", "DMA");
break;
case DEVICE_CLASS_TIMER: case DEVICE_CLASS_TIMER:
MSG("Device Type = %s \r\n","TIMER");break; MSG("Device Type = %s \r\n", "TIMER");
break;
case DEVICE_CLASS_PWM: case DEVICE_CLASS_PWM:
MSG("Device Type = %s \r\n","PWM");break; MSG("Device Type = %s \r\n", "PWM");
break;
case DEVICE_CLASS_SDIO: case DEVICE_CLASS_SDIO:
MSG("Device Type = %s \r\n","SDIO");break; MSG("Device Type = %s \r\n", "SDIO");
break;
case DEVICE_CLASS_USB: case DEVICE_CLASS_USB:
MSG("Device Type = %s \r\n","USB");break; MSG("Device Type = %s \r\n", "USB");
break;
case DEVICE_CLASS_I2S: case DEVICE_CLASS_I2S:
MSG("Device Type = %s \r\n","I2S");break; MSG("Device Type = %s \r\n", "I2S");
break;
case DEVICE_CLASS_CAMERA: case DEVICE_CLASS_CAMERA:
MSG("Device Type = %s \r\n","CAMERA");break; MSG("Device Type = %s \r\n", "CAMERA");
case DEVICE_CLASS_NONE: break; break;
default : break;
case DEVICE_CLASS_NONE:
break;
default:
break;
} }
MSG("Device Handle = 0x%x \r\n", dev); MSG("Device Handle = 0x%x \r\n", dev);

View file

@ -24,6 +24,10 @@
#ifndef _BFLB_PLATFORM_H #ifndef _BFLB_PLATFORM_H
#define _BFLB_PLATFORM_H #define _BFLB_PLATFORM_H
#ifdef __cplusplus
extern "C" {
#endif
#include "misc.h" #include "misc.h"
//#include "mcu_sdk_version.h" //#include "mcu_sdk_version.h"
@ -31,8 +35,20 @@
#define MSG(a, ...) bflb_platform_printf(a, ##__VA_ARGS__) #define MSG(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
#define MSG_DBG(a, ...) bflb_platform_printf(a, ##__VA_ARGS__) #define MSG_DBG(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
#define MSG_ERR(a, ...) bflb_platform_printf(a, ##__VA_ARGS__) #define MSG_ERR(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
#define BL_CASE_FAIL {MSG(" Case Fail\r\n");while(1){bflb_platform_delay_ms(1);}} #define BL_CASE_FAIL \
#define BL_CASE_SUCCESS {MSG(" Case Success\r\n");while(1){bflb_platform_delay_ms(1);}} { \
MSG(" Case Fail\r\n"); \
while (1) { \
bflb_platform_delay_ms(1); \
} \
}
#define BL_CASE_SUCCESS \
{ \
MSG(" Case Success\r\n"); \
while (1) { \
bflb_platform_delay_ms(1); \
} \
}
/* compatible with old version */ /* compatible with old version */
#ifndef DBG_TAG #ifndef DBG_TAG
@ -57,13 +73,11 @@
bflb_platform_printf("\033[0m\n") bflb_platform_printf("\033[0m\n")
#define dbg_log_line(lvl, color_n, fmt, ...) \ #define dbg_log_line(lvl, color_n, fmt, ...) \
do \ do { \
{ \
_DBG_LOG_HDR(lvl, color_n); \ _DBG_LOG_HDR(lvl, color_n); \
bflb_platform_printf(fmt, ##__VA_ARGS__); \ bflb_platform_printf(fmt, ##__VA_ARGS__); \
_DBG_LOG_X_END; \ _DBG_LOG_X_END; \
} \ } while (0)
while (0)
#define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__) #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
#define LOG_I(fmt, ...) dbg_log_line("I", 35, fmt, ##__VA_ARGS__) #define LOG_I(fmt, ...) dbg_log_line("I", 35, fmt, ##__VA_ARGS__)
@ -98,4 +112,8 @@ void bflb_platform_delay_us(uint32_t us);
void bflb_print_device_list(void); void bflb_print_device_list(void);
#ifdef __cplusplus
}
#endif
#endif #endif

View file

@ -100,6 +100,7 @@ void ATTR_TCM_SECTION bsp_sf_psram_gpio_init(void)
for (i = 0; i < sizeof(gpiopins); i++) { for (i = 0; i < sizeof(gpiopins); i++) {
cfg.gpioPin = gpiopins[i]; cfg.gpioPin = gpiopins[i];
if (i == 0 || i == 1 || i == 6) { if (i == 0 || i == 1 || i == 6) {
/*flash clk and cs is output*/ /*flash clk and cs is output*/
cfg.gpioMode = GPIO_MODE_OUTPUT; cfg.gpioMode = GPIO_MODE_OUTPUT;
@ -107,6 +108,7 @@ void ATTR_TCM_SECTION bsp_sf_psram_gpio_init(void)
/*data are bidir*/ /*data are bidir*/
cfg.gpioMode = GPIO_MODE_AF; cfg.gpioMode = GPIO_MODE_AF;
} }
GLB_GPIO_Init(&cfg); GLB_GPIO_Init(&cfg);
} }
} }
@ -126,9 +128,11 @@ void ATTR_TCM_SECTION bsp_sf_psram_init(uint8_t sw_reset)
bsp_sf_psram_gpio_init(); bsp_sf_psram_gpio_init();
Psram_Init(&apMemory1604, &cmdsCfg, &sfCtrlPsramCfg); Psram_Init(&apMemory1604, &cmdsCfg, &sfCtrlPsramCfg);
if (sw_reset) { if (sw_reset) {
Psram_SoftwareReset(&apMemory1604, apMemory1604.ctrlMode); Psram_SoftwareReset(&apMemory1604, apMemory1604.ctrlMode);
} }
Psram_ReadId(&apMemory1604, psramId); Psram_ReadId(&apMemory1604, psramId);
Psram_Cache_Write_Set(&apMemory1604, SF_CTRL_QIO_MODE, ENABLE, DISABLE, DISABLE); Psram_Cache_Write_Set(&apMemory1604, SF_CTRL_QIO_MODE, ENABLE, DISABLE, DISABLE);
L1C_Cache_Enable_Set(L1C_WAY_DISABLE_NONE); L1C_Cache_Enable_Set(L1C_WAY_DISABLE_NONE);
@ -138,5 +142,3 @@ void ATTR_TCM_SECTION bsp_sf_psram_read_id(uint8_t *data)
{ {
Psram_ReadId(&apMemory1604, data); Psram_ReadId(&apMemory1604, data);
} }

View file

@ -42,5 +42,4 @@ void bsp_sf_psram_gpio_init(void);
void bsp_sf_psram_init(uint8_t sw_reset); void bsp_sf_psram_init(uint8_t sw_reset);
void bsp_sf_psram_read_id(uint8_t *data); void bsp_sf_psram_read_id(uint8_t *data);
#endif /* __BSP_SF_PSRAM_H__ */ #endif /* __BSP_SF_PSRAM_H__ */

View file

@ -33,37 +33,34 @@ static struct device* dma_ch4;
SD_CardInfoTypedef SD_CardInfo = { 0 }; SD_CardInfoTypedef SD_CardInfo = { 0 };
uint8_t SD_SPI_Init(void) uint8_t SD_SPI_Init(void)
{ {
gpio_set_mode(SPI_PIN_CS, GPIO_OUTPUT_MODE); gpio_set_mode(SPI_PIN_CS, GPIO_OUTPUT_MODE);
gpio_write(SPI_PIN_CS, 1); gpio_write(SPI_PIN_CS, 1);
spi0 = device_find("spi0"); spi0 = device_find("spi0");
if(spi0)
{ if (spi0) {
device_close(spi0); device_close(spi0);
} } else {
else{
spi_register(SPI0_INDEX, "spi0", DEVICE_OFLAG_RDWR); spi_register(SPI0_INDEX, "spi0", DEVICE_OFLAG_RDWR);
spi0 = device_find("spi0"); spi0 = device_find("spi0");
} }
if(spi0)
{ if (spi0) {
device_open(spi0, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_STREAM_RX); device_open(spi0, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_STREAM_RX);
} }
dma_ch3 = device_find("dma0_ch3"); dma_ch3 = device_find("dma0_ch3");
if(dma_ch3)
{ if (dma_ch3) {
device_close(dma_ch3); device_close(dma_ch3);
} } else {
else{
dma_register(DMA0_CH3_INDEX, "dma0_ch3", DEVICE_OFLAG_RDWR); dma_register(DMA0_CH3_INDEX, "dma0_ch3", DEVICE_OFLAG_RDWR);
dma_ch3 = device_find("dma0_ch3"); dma_ch3 = device_find("dma0_ch3");
} }
if (dma_ch3)
{ if (dma_ch3) {
((dma_device_t *)dma_ch3)->direction = DMA_MEMORY_TO_PERIPH; ((dma_device_t *)dma_ch3)->direction = DMA_MEMORY_TO_PERIPH;
((dma_device_t *)dma_ch3)->transfer_mode = DMA_LLI_ONCE_MODE; ((dma_device_t *)dma_ch3)->transfer_mode = DMA_LLI_ONCE_MODE;
((dma_device_t *)dma_ch3)->src_req = DMA_REQUEST_NONE; ((dma_device_t *)dma_ch3)->src_req = DMA_REQUEST_NONE;
@ -74,16 +71,15 @@ uint8_t SD_SPI_Init(void)
} }
dma_ch4 = device_find("dma0_ch4"); dma_ch4 = device_find("dma0_ch4");
if(dma_ch4)
{ if (dma_ch4) {
device_close(dma_ch4); device_close(dma_ch4);
} } else {
else{
dma_register(DMA0_CH4_INDEX, "dma0_ch4", DEVICE_OFLAG_RDWR); dma_register(DMA0_CH4_INDEX, "dma0_ch4", DEVICE_OFLAG_RDWR);
dma_ch4 = device_find("dma0_ch4"); dma_ch4 = device_find("dma0_ch4");
} }
if (dma_ch4)
{ if (dma_ch4) {
((dma_device_t *)dma_ch4)->direction = DMA_PERIPH_TO_MEMORY; ((dma_device_t *)dma_ch4)->direction = DMA_PERIPH_TO_MEMORY;
((dma_device_t *)dma_ch4)->transfer_mode = DMA_LLI_ONCE_MODE; ((dma_device_t *)dma_ch4)->transfer_mode = DMA_LLI_ONCE_MODE;
((dma_device_t *)dma_ch4)->src_req = DMA_REQUEST_SPI0_RX; ((dma_device_t *)dma_ch4)->src_req = DMA_REQUEST_SPI0_RX;
@ -92,20 +88,18 @@ uint8_t SD_SPI_Init(void)
((dma_device_t *)dma_ch4)->dst_width = DMA_TRANSFER_WIDTH_8BIT; ((dma_device_t *)dma_ch4)->dst_width = DMA_TRANSFER_WIDTH_8BIT;
device_open(dma_ch4, 0); device_open(dma_ch4, 0);
} }
return SUCCESS; return SUCCESS;
} }
BL_Err_Type SPI_ReadWriteByte(uint8_t *txBuff, uint8_t *rxBuff, uint32_t length) BL_Err_Type SPI_ReadWriteByte(uint8_t *txBuff, uint8_t *rxBuff, uint32_t length)
{ {
while (device_control(dma_ch3, DMA_CHANNEL_GET_STATUS, NULL) || device_control(dma_ch4, DMA_CHANNEL_GET_STATUS, NULL))
;
while(device_control(dma_ch3,DMA_CHANNEL_GET_STATUS,NULL)||device_control(dma_ch4,DMA_CHANNEL_GET_STATUS,NULL)); if (length < 500) {
if(length<500)
{
spi_transmit_receive(spi0, txBuff, rxBuff, length, SPI_DATASIZE_8BIT); spi_transmit_receive(spi0, txBuff, rxBuff, length, SPI_DATASIZE_8BIT);
} } else {
else
{
device_control(spi0, DEVICE_CTRL_TX_DMA_RESUME, NULL); device_control(spi0, DEVICE_CTRL_TX_DMA_RESUME, NULL);
device_control(spi0, DEVICE_CTRL_RX_DMA_RESUME, NULL); device_control(spi0, DEVICE_CTRL_RX_DMA_RESUME, NULL);
device_control(dma_ch3, DEVICE_CTRL_CLR_INT, NULL); device_control(dma_ch3, DEVICE_CTRL_CLR_INT, NULL);
@ -116,7 +110,9 @@ BL_Err_Type SPI_ReadWriteByte(uint8_t *txBuff, uint8_t *rxBuff, uint32_t length)
dma_channel_start(dma_ch3); dma_channel_start(dma_ch3);
dma_channel_start(dma_ch4); dma_channel_start(dma_ch4);
while(device_control(dma_ch3,DMA_CHANNEL_GET_STATUS,NULL)||device_control(dma_ch4,DMA_CHANNEL_GET_STATUS,NULL)); while (device_control(dma_ch3, DMA_CHANNEL_GET_STATUS, NULL) || device_control(dma_ch4, DMA_CHANNEL_GET_STATUS, NULL))
;
device_control(spi0, DEVICE_CTRL_TX_DMA_SUSPEND, NULL); device_control(spi0, DEVICE_CTRL_TX_DMA_SUSPEND, NULL);
device_control(spi0, DEVICE_CTRL_RX_DMA_SUSPEND, NULL); device_control(spi0, DEVICE_CTRL_RX_DMA_SUSPEND, NULL);
} }
@ -138,12 +134,15 @@ static void SD_SPI_SetSpeed(uint8_t mode)
case (0): case (0):
device_control(spi0, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)(300 * 1000)); device_control(spi0, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)(300 * 1000));
break; break;
case (1): case (1):
device_control(spi0, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)(18 * 1000 * 1000)); device_control(spi0, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)(18 * 1000 * 1000));
break; break;
case (2): case (2):
device_control(spi0, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)(40 * 1000 * 1000)); device_control(spi0, DEVICE_CTRL_SPI_CONFIG_CLOCK, (void *)(40 * 1000 * 1000));
break; break;
default: default:
break; break;
} }
@ -169,16 +168,18 @@ static BL_Err_Type SD_GetResponse(uint8_t Response)
uint8_t rx; uint8_t rx;
uint8_t t_0xFF = 0xFF; uint8_t t_0xFF = 0xFF;
uint16_t Count = 0xFFFE; uint16_t Count = 0xFFFE;
do { do {
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
Count--; Count--;
} while (rx != Response && Count); } while (rx != Response && Count);
if (Count == 0) if (Count == 0) {
return ERROR; return ERROR;
else } else {
return SUCCESS; return SUCCESS;
} }
}
/**************************************************************************** /****************************************************************************
* @brief SD_SendCommand * @brief SD_SendCommand
@ -276,28 +277,24 @@ static BL_Err_Type SD_Idle_Sta(void)
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++) {
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
}
do do {
{
rx = SD_SendCommand(CMD0, 0, 0x95); rx = SD_SendCommand(CMD0, 0, 0x95);
retry++; retry++;
} while (rx != MSD_IN_IDLE_STATE && retry < 100); } while (rx != MSD_IN_IDLE_STATE && retry < 100);
if (retry >= 100) if (retry >= 100) {
{
//MSG("SD IDLE err:%d\r\n",rx); //MSG("SD IDLE err:%d\r\n",rx);
return ERROR; return ERROR;
} } else {
else
{
//MSG("SD IDLE success\r\n"); //MSG("SD IDLE success\r\n");
return SUCCESS; return SUCCESS;
} }
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief SD_ReceiveData * @brief SD_ReceiveData
* *
@ -311,28 +308,28 @@ uint8_t SD_ReceiveData(uint8_t *data, uint16_t len, uint8_t release)
uint16_t i; uint16_t i;
SPI_CS_WriteBit(0); SPI_CS_WriteBit(0);
/* get token */ /* get token */
if (SD_GetResponse(0xFE)) if (SD_GetResponse(0xFE)) {
{
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
MSG("sd receive err: no token\r\n"); MSG("sd receive err: no token\r\n");
return ERROR; return ERROR;
} } else {
else
{
//MSG("sd get token\r\n"); //MSG("sd get token\r\n");
} }
for (i = 0; i < len; i++) for (i = 0; i < len; i++) {
{
SPI_ReadWriteByte(&t_0xFF, data + i, 1); SPI_ReadWriteByte(&t_0xFF, data + i, 1);
} }
/* two dummy CRC */ /* two dummy CRC */
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
/* */ /* */
if (release) if (release) {
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
}
return SUCCESS; return SUCCESS;
} }
@ -349,9 +346,9 @@ uint8_t SD_SendBlock(uint8_t *buf, uint8_t cmd)
uint8_t rxbuff[512]; uint8_t rxbuff[512];
uint8_t t_0xFF = 0xFF; uint8_t t_0xFF = 0xFF;
uint16_t i; uint16_t i;
/* Waiti for free */ /* Waiti for free */
if (SD_GetResponse(0xFF)) if (SD_GetResponse(0xFF)) {
{
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
return ERROR; return ERROR;
} }
@ -359,8 +356,7 @@ uint8_t SD_SendBlock(uint8_t *buf, uint8_t cmd)
/* send token */ /* send token */
SPI_ReadWriteByte(&cmd, &rx, 1); SPI_ReadWriteByte(&cmd, &rx, 1);
if (cmd != 0xFD) if (cmd != 0xFD) {
{
/* send block data */ /* send block data */
SPI_ReadWriteByte(buf, rxbuff, 512); SPI_ReadWriteByte(buf, rxbuff, 512);
/* two dummy CRC */ /* two dummy CRC */
@ -369,19 +365,20 @@ uint8_t SD_SendBlock(uint8_t *buf, uint8_t cmd)
/* 8 clock */ /* 8 clock */
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
if ((rx & 0x1F) != 0x05) if ((rx & 0x1F) != 0x05) {
{
//MSG("sd send err:%d\r\n",rx,1); //MSG("sd send err:%d\r\n",rx,1);
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
return rx; return rx;
} }
i = 0; i = 0;
do
{ do {
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
i++; i++;
} while (rx != 0xFF && i <= 0xFFFE); } while (rx != 0xFF && i <= 0xFFFE);
} }
return 0; return 0;
} }
@ -502,14 +499,20 @@ uint8_t SD_Get_CardInfo(SD_CardInfoTypedef *pCardInfo)
uint32_t tmp = 0; uint32_t tmp = 0;
/* get cid */ /* get cid */
rx = SD_SendCommand(CMD10, 0, 0xFF); rx = SD_SendCommand(CMD10, 0, 0xFF);
if (rx)
if (rx) {
return 1; return 1;
}
SD_ReceiveData(pCardInfo->SD_cid.CID, 16, RELEASE); SD_ReceiveData(pCardInfo->SD_cid.CID, 16, RELEASE);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
/* get csd */ /* get csd */
rx = SD_SendCommand(CMD9, 0, 0xFF); rx = SD_SendCommand(CMD9, 0, 0xFF);
if (rx)
if (rx) {
return 1; return 1;
}
SD_ReceiveData(pCardInfo->SD_csd.CSD, 16, RELEASE); SD_ReceiveData(pCardInfo->SD_csd.CSD, 16, RELEASE);
/* Byte 0 */ /* Byte 0 */
@ -541,8 +544,7 @@ uint8_t SD_Get_CardInfo(SD_CardInfoTypedef *pCardInfo)
pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4); pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */ pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */
if ((pCardInfo->CardType == SD_TYPE_V1) || (pCardInfo->CardType == SD_TYPE_V2)) if ((pCardInfo->CardType == SD_TYPE_V1) || (pCardInfo->CardType == SD_TYPE_V2)) {
{
pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10; pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
/* Byte 7 */ /* Byte 7 */
tmp = pCardInfo->SD_csd.CSD[7]; tmp = pCardInfo->SD_csd.CSD[7];
@ -565,9 +567,7 @@ uint8_t SD_Get_CardInfo(SD_CardInfoTypedef *pCardInfo)
pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2)); pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen); pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
pCardInfo->CardCapacity *= pCardInfo->CardBlockSize; pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
} } else if (pCardInfo->CardType == SD_TYPE_V2HC) {
else if (pCardInfo->CardType == SD_TYPE_V2HC)
{
/* Byte 7 */ /* Byte 7 */
tmp = pCardInfo->SD_csd.CSD[7]; tmp = pCardInfo->SD_csd.CSD[7];
pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16; pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
@ -582,11 +582,10 @@ uint8_t SD_Get_CardInfo(SD_CardInfoTypedef *pCardInfo)
pCardInfo->CardCapacity = (uint64_t)((((uint64_t)pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024); pCardInfo->CardCapacity = (uint64_t)((((uint64_t)pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024);
pCardInfo->CardBlockSize = 512; pCardInfo->CardBlockSize = 512;
} } else {
else
{
/* Not supported card type */ /* Not supported card type */
} }
pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6; pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1; pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
/* Byte 11 */ /* Byte 11 */
@ -681,45 +680,50 @@ uint8_t SD_ReadBlock(uint32_t sector, uint8_t *buffer, uint32_t num)
uint8_t rx; uint8_t rx;
uint8_t t_0xFF; uint8_t t_0xFF;
SPI_CS_WriteBit(0); SPI_CS_WriteBit(0);
/* sector to byte */ /* sector to byte */
if (SD_CardInfo.CardType != SD_TYPE_V2HC) { if (SD_CardInfo.CardType != SD_TYPE_V2HC) {
sector = sector << 9; sector = sector << 9;
} }
if (num == 1) { if (num == 1) {
rx = SD_SendCommand(CMD17, sector, 0); rx = SD_SendCommand(CMD17, sector, 0);
if (rx)
{ if (rx) {
MSG("SD CMD17 err:%02X\r\n", rx); MSG("SD CMD17 err:%02X\r\n", rx);
return rx; return rx;
} }
rx = SD_ReceiveData(buffer, 512, RELEASE); rx = SD_ReceiveData(buffer, 512, RELEASE);
if (rx != 0)
{ if (rx != 0) {
MSG("SD sing read err:%02X\r\n", rx); MSG("SD sing read err:%02X\r\n", rx);
return rx; return rx;
} }
} } else {
else{
rx = SD_SendCommand(CMD18, sector, 0); rx = SD_SendCommand(CMD18, sector, 0);
if (rx)
{ if (rx) {
MSG("SD CMD18 err:%02X\r\n", rx); MSG("SD CMD18 err:%02X\r\n", rx);
return rx; return rx;
} }
while (num--)
{ while (num--) {
rx = SD_ReceiveData(buffer, 512, NO_RELEASE); rx = SD_ReceiveData(buffer, 512, NO_RELEASE);
if (rx != 0)
{ if (rx != 0) {
MSG("SD read err:%02X\r\n", rx); MSG("SD read err:%02X\r\n", rx);
return rx; return rx;
} }
buffer += 512; buffer += 512;
} }
rx = SD_SendCommand(CMD12, 0, 0); rx = SD_SendCommand(CMD12, 0, 0);
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
} }
return SUCCESS; return SUCCESS;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -740,48 +744,54 @@ uint8_t SD_WriteBlock(uint32_t sector, uint8_t *data, uint32_t num)
if (SD_GetResponse(0xFF)) { if (SD_GetResponse(0xFF)) {
return 1; return 1;
} }
if (num == 1) { if (num == 1) {
rx = SD_SendCommand(CMD24, sector, 1); rx = SD_SendCommand(CMD24, sector, 1);
if (rx)
{ if (rx) {
MSG("SD CMD24 err:%d\r\n", rx); MSG("SD CMD24 err:%d\r\n", rx);
return rx; return rx;
} }
SPI_CS_WriteBit(0); SPI_CS_WriteBit(0);
rx = SD_SendBlock(data, 0xFE); rx = SD_SendBlock(data, 0xFE);
if (rx)
{ if (rx) {
MSG("write err\r\n"); MSG("write err\r\n");
SPI_CS_WriteBit(0); SPI_CS_WriteBit(0);
return rx; return rx;
} }
} } else if (num > 1) {
else if (num > 1){ if (SD_CardInfo.CardType != SD_TYPE_MMC) {
if (SD_CardInfo.CardType != SD_TYPE_MMC)
{
rx = SD_SendCommand(CMD55, 0, 1); rx = SD_SendCommand(CMD55, 0, 1);
if (rx == 0)
if (rx == 0) {
rx = SD_SendCommand(ACMD23, num, 1); rx = SD_SendCommand(ACMD23, num, 1);
if (rx != 0) }
{
if (rx != 0) {
MSG("SD ACMD23 err:%d\r\n", rx); MSG("SD ACMD23 err:%d\r\n", rx);
return rx; return rx;
} }
} }
rx = SD_SendCommand(CMD25, sector, 1); rx = SD_SendCommand(CMD25, sector, 1);
if (rx != 0)
{ if (rx != 0) {
MSG("SD ACMD25 err:%d\r\n", rx); MSG("SD ACMD25 err:%d\r\n", rx);
return rx; return rx;
} }
SPI_CS_WriteBit(0); SPI_CS_WriteBit(0);
do
{ do {
rx = SD_SendBlock(data, 0xFC); rx = SD_SendBlock(data, 0xFC);
data += 512; data += 512;
} while (--num && rx == 0); } while (--num && rx == 0);
rx = SD_SendBlock(data, 0xFD); rx = SD_SendBlock(data, 0xFD);
} }
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
return 0; return 0;
@ -807,75 +817,67 @@ uint8_t SD_Init(SD_CardInfoTypedef *pCardInfo)
/* low speed */ /* low speed */
SD_SPI_SetSpeed(0); SD_SPI_SetSpeed(0);
if (SD_Idle_Sta()) if (SD_Idle_Sta()) {
{
MSG("SD IDLE err:%d\r\n", rx); MSG("SD IDLE err:%d\r\n", rx);
return ERROR; return ERROR;
} } else {
else
{
MSG("SD IDLE success\r\n"); MSG("SD IDLE success\r\n");
} }
/* send CMD8 */ /* send CMD8 */
rx = SD_SendCommand_NoDeassert(CMD8, 0x1aa, 0x87); rx = SD_SendCommand_NoDeassert(CMD8, 0x1aa, 0x87);
/* sd card v1.0 */
if (rx == 0x05)
{
/* sd card v1.0 */
if (rx == 0x05) {
pCardInfo->CardType = SD_TYPE_V1; pCardInfo->CardType = SD_TYPE_V1;
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
/* send CMD55+CMD41 sd init */ /* send CMD55+CMD41 sd init */
do do {
{
rx = SD_SendCommand(CMD55, 0, 0); rx = SD_SendCommand(CMD55, 0, 0);
if (rx != 0x01)
{ if (rx != 0x01) {
MSG("sdv1 CMD55 err:%d\r\n", rx); MSG("sdv1 CMD55 err:%d\r\n", rx);
return rx; return rx;
} }
rx = SD_SendCommand(ACMD41, 0, 0); rx = SD_SendCommand(ACMD41, 0, 0);
retry++; retry++;
} while (rx != 0x00 && retry < 100); } while (rx != 0x00 && retry < 100);
/* mmc card init */ /* mmc card init */
if (retry >= 100) if (retry >= 100) {
{
retry = 0; retry = 0;
do
{ do {
rx = SD_SendCommand(CMD1, 0, 0); rx = SD_SendCommand(CMD1, 0, 0);
retry++; retry++;
} while (rx != 0x00 && retry < 100); } while (rx != 0x00 && retry < 100);
if (retry >= 100) if (retry >= 100) {
{
MSG("sdv2 CMD1 timeout err:%d\r\n", rx); MSG("sdv2 CMD1 timeout err:%d\r\n", rx);
return ERROR; return ERROR;
} } else {
else
{
pCardInfo->CardType = SD_TYPE_MMC; pCardInfo->CardType = SD_TYPE_MMC;
} }
} }
/* high speed */ /* high speed */
SD_SPI_SetSpeed(1); SD_SPI_SetSpeed(1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
/* set sector size */ /* set sector size */
rx = SD_SendCommand(CMD16, 512, 0xff); rx = SD_SendCommand(CMD16, 512, 0xff);
if (rx != 0x00)
{ if (rx != 0x00) {
MSG("sdv1 CMD16 err:%d\r\n", rx); MSG("sdv1 CMD16 err:%d\r\n", rx);
return rx; return rx;
} }
} }
/* sd card v2.0 */ /* sd card v2.0 */
else if (rx == 0x01) else if (rx == 0x01) {
{
SPI_ReadWriteByte(&t_0xFF, rxbuff + 0, 1); SPI_ReadWriteByte(&t_0xFF, rxbuff + 0, 1);
SPI_ReadWriteByte(&t_0xFF, rxbuff + 1, 1); SPI_ReadWriteByte(&t_0xFF, rxbuff + 1, 1);
SPI_ReadWriteByte(&t_0xFF, rxbuff + 2, 1); SPI_ReadWriteByte(&t_0xFF, rxbuff + 2, 1);
@ -884,33 +886,36 @@ uint8_t SD_Init(SD_CardInfoTypedef *pCardInfo)
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
/* next 8 clocks */ /* next 8 clocks */
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
/* 2.7V - 3.6V */ /* 2.7V - 3.6V */
if (rxbuff[2] == 0x01 && rxbuff[3] == 0xAA) if (rxbuff[2] == 0x01 && rxbuff[3] == 0xAA) {
{
retry = 0; retry = 0;
do
{ do {
rx = SD_SendCommand(CMD55, 0, 0); rx = SD_SendCommand(CMD55, 0, 0);
if (rx != 0x01)
{ if (rx != 0x01) {
MSG("sdv2 CMD55 err:%d\r\n", rx); MSG("sdv2 CMD55 err:%d\r\n", rx);
return rx; return rx;
} }
rx = SD_SendCommand(ACMD41, 0x40000000, 0); rx = SD_SendCommand(ACMD41, 0x40000000, 0);
retry++; retry++;
if (retry > 200)
{ if (retry > 200) {
MSG("sdv2 CMD41 timeout err:%d\r\n", rx); MSG("sdv2 CMD41 timeout err:%d\r\n", rx);
return rx; return rx;
} }
} while (rx != 0); } while (rx != 0);
/* send CMD58 */ /* send CMD58 */
rx = SD_SendCommand_NoDeassert(CMD58, 0, 0); rx = SD_SendCommand_NoDeassert(CMD58, 0, 0);
if (rx != 0X00)
{ if (rx != 0X00) {
MSG("sdv2 CMD58 err:%d\r\n", rx); MSG("sdv2 CMD58 err:%d\r\n", rx);
return rx; return rx;
} }
/* get OCR */ /* get OCR */
SPI_ReadWriteByte(&t_0xFF, rxbuff + 0, 1); SPI_ReadWriteByte(&t_0xFF, rxbuff + 0, 1);
SPI_ReadWriteByte(&t_0xFF, rxbuff + 1, 1); SPI_ReadWriteByte(&t_0xFF, rxbuff + 1, 1);
@ -919,41 +924,47 @@ uint8_t SD_Init(SD_CardInfoTypedef *pCardInfo)
SPI_CS_WriteBit(1); SPI_CS_WriteBit(1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
/* bit32 CCS: 1-SDHC 0-SD2.0 */ /* bit32 CCS: 1-SDHC 0-SD2.0 */
if (rxbuff[0] & 0x40) if (rxbuff[0] & 0x40) {
{
pCardInfo->CardType = SD_TYPE_V2HC; pCardInfo->CardType = SD_TYPE_V2HC;
MSG("sdv2hc success\r\n"); MSG("sdv2hc success\r\n");
} } else {
else
{
pCardInfo->CardType = SD_TYPE_V2; pCardInfo->CardType = SD_TYPE_V2;
MSG("sdv2.0 success\r\n"); MSG("sdv2.0 success\r\n");
} }
/* set sector size */ /* set sector size */
rx = SD_SendCommand(CMD16, 512, 0xff); rx = SD_SendCommand(CMD16, 512, 0xff);
if (rx != 0x00)
if (rx != 0x00) {
MSG("sdv1 CMD16 err:%d\r\n", rx); MSG("sdv1 CMD16 err:%d\r\n", rx);
}
/* high speed */ /* high speed */
SD_SPI_SetSpeed(1); SD_SPI_SetSpeed(1);
SPI_ReadWriteByte(&t_0xFF, &rx, 1); SPI_ReadWriteByte(&t_0xFF, &rx, 1);
} }
} } else {
else
{
return 1; return 1;
} }
MSG("sd init success\r\n"); MSG("sd init success\r\n");
SD_Get_CardInfo(pCardInfo); SD_Get_CardInfo(pCardInfo);
MSG(" sd csd:"); MSG(" sd csd:");
for (i = 0; i < 4; i++)
for (i = 0; i < 4; i++) {
MSG("%X ", pCardInfo->SD_csd.CSD[i]); MSG("%X ", pCardInfo->SD_csd.CSD[i]);
}
MSG("end\r\n sd cid:"); MSG("end\r\n sd cid:");
for (i = 0; i < 4; i++)
for (i = 0; i < 4; i++) {
MSG("%X ", pCardInfo->SD_cid.CID[i]); MSG("%X ", pCardInfo->SD_cid.CID[i]);
}
MSG("end\r\n"); MSG("end\r\n");
MSG("SDHC CardBlockSize:%d Byte\r\n", (pCardInfo->CardBlockSize)); MSG("SDHC CardBlockSize:%d Byte\r\n", (pCardInfo->CardBlockSize));
MSG("SDHC CardCapacity:%lld MByte\r\n", (SD_CardInfo.CardCapacity / 1024 / 1024)); MSG("SDHC CardCapacity:%lld MByte\r\n", (SD_CardInfo.CardCapacity / 1024 / 1024));

View file

@ -128,8 +128,7 @@ typedef struct
} SD_CIDTypedef; } SD_CIDTypedef;
typedef struct SD_CardInfoTypedef typedef struct SD_CardInfoTypedef {
{
SD_CSDTypedef SD_csd; /*!< SD card specific data register */ SD_CSDTypedef SD_csd; /*!< SD card specific data register */
SD_CIDTypedef SD_cid; /*!< SD card identification number register */ SD_CIDTypedef SD_cid; /*!< SD card identification number register */
uint64_t CardCapacity; /*!< Card capacity */ uint64_t CardCapacity; /*!< Card capacity */

View file

@ -35,7 +35,3 @@ uint8_t touch_read(int16_t * x, int16_t * y)
return xpt2046_read(x, y); return xpt2046_read(x, y);
#endif #endif
} }

View file

@ -26,12 +26,10 @@
#define TOUCH_CONTROLLER_XPT2046 #define TOUCH_CONTROLLER_XPT2046
#ifdef TOUCH_CONTROLLER_XPT2046 #ifdef TOUCH_CONTROLLER_XPT2046
#include "xpt2046.h" #include "xpt2046.h"
#endif #endif
void touch_init(void); void touch_init(void);
uint8_t touch_read(int16_t *x, int16_t *y); uint8_t touch_read(int16_t *x, int16_t *y);

View file

@ -37,8 +37,6 @@ struct device* touch_spi;
uint16_t avg_buf_x[XPT2046_AVG_NUM]; uint16_t avg_buf_x[XPT2046_AVG_NUM];
uint16_t avg_buf_y[XPT2046_AVG_NUM]; uint16_t avg_buf_y[XPT2046_AVG_NUM];
static void xpt2046_spi_read_reg(uint8_t reg, uint8_t *data, uint8_t byte_count) static void xpt2046_spi_read_reg(uint8_t reg, uint8_t *data, uint8_t byte_count)
{ {
gpio_write(TOUCH_PIN_CS, 0); gpio_write(TOUCH_PIN_CS, 0);
@ -75,19 +73,17 @@ void xpt2046_init(void)
gpio_set_mode(TOUCH_PIN_CS, GPIO_OUTPUT_MODE); gpio_set_mode(TOUCH_PIN_CS, GPIO_OUTPUT_MODE);
gpio_write(TOUCH_PIN_CS, 1); gpio_write(TOUCH_PIN_CS, 1);
touch_spi = device_find("spi0"); touch_spi = device_find("spi0");
if(touch_spi)
{ if (touch_spi) {
device_close(touch_spi); device_close(touch_spi);
} } else {
else{
spi_register(SPI0_INDEX, "spi0", DEVICE_OFLAG_RDWR); spi_register(SPI0_INDEX, "spi0", DEVICE_OFLAG_RDWR);
touch_spi = device_find("spi0"); touch_spi = device_find("spi0");
} }
if(touch_spi)
{ if (touch_spi) {
device_open(touch_spi, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_STREAM_RX); device_open(touch_spi, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_STREAM_RX);
} }
} }
/****************************************************************************** /******************************************************************************
@ -108,17 +104,13 @@ static uint8_t xpt2048_is_touch_detected()
// be enough to detect real touches on the panel // be enough to detect real touches on the panel
uint16_t z = z1 + 4096 - z2; uint16_t z = z1 + 4096 - z2;
if (z > XPT2046_TOUCH_THRESHOLD) if (z > XPT2046_TOUCH_THRESHOLD) {
{
return 1; return 1;
} } else {
else
{
return 0; return 0;
} }
} }
/****************************************************************************** /******************************************************************************
* @brief xpt2046_ads_get * @brief xpt2046_ads_get
* *
@ -129,12 +121,11 @@ static uint8_t xpt2048_is_touch_detected()
*******************************************************************************/ *******************************************************************************/
static uint8_t xpt2046_ads_get(int16_t *x, int16_t *y) static uint8_t xpt2046_ads_get(int16_t *x, int16_t *y)
{ {
for(uint8_t i=0; i<XPT2046_AVG_NUM; i++) for (uint8_t i = 0; i < XPT2046_AVG_NUM; i++) {
{ if (xpt2048_is_touch_detected() == 0) {
if(xpt2048_is_touch_detected()==0)
{
return 0; return 0;
} }
avg_buf_x[i] = xpt2046_cmd(CMD_X_READ); avg_buf_x[i] = xpt2046_cmd(CMD_X_READ);
avg_buf_y[i] = xpt2046_cmd(CMD_Y_READ); avg_buf_y[i] = xpt2046_cmd(CMD_Y_READ);
} }
@ -143,19 +134,31 @@ static uint8_t xpt2046_ads_get(int16_t *x, int16_t *y)
int16_t y_min = avg_buf_y[0], y_max = avg_buf_y[0]; int16_t y_min = avg_buf_y[0], y_max = avg_buf_y[0];
int32_t x_sum = avg_buf_x[0], y_sum = avg_buf_y[0]; int32_t x_sum = avg_buf_x[0], y_sum = avg_buf_y[0];
for(uint8_t i=1; i<XPT2046_AVG_NUM; i++) for (uint8_t i = 1; i < XPT2046_AVG_NUM; i++) {
{ if (x_min < avg_buf_x[i]) {
if(x_min<avg_buf_x[i]) x_min=avg_buf_x[i]; x_min = avg_buf_x[i];
if(x_max>avg_buf_x[i]) x_max=avg_buf_x[i]; }
if(y_min<avg_buf_y[i]) y_min=avg_buf_y[i];
if(y_max>avg_buf_y[i]) y_max=avg_buf_y[i]; if (x_max > avg_buf_x[i]) {
x_max = avg_buf_x[i];
}
if (y_min < avg_buf_y[i]) {
y_min = avg_buf_y[i];
}
if (y_max > avg_buf_y[i]) {
y_max = avg_buf_y[i];
}
x_sum += avg_buf_x[i]; x_sum += avg_buf_x[i];
y_sum += avg_buf_y[i]; y_sum += avg_buf_y[i];
} }
if(x_max-x_min>40 || y_max-y_min>50)
{ if (x_max - x_min > 40 || y_max - y_min > 50) {
return 0; return 0;
} }
*x = (x_sum - x_min - x_max) / (XPT2046_AVG_NUM - 2); *x = (x_sum - x_min - x_max) / (XPT2046_AVG_NUM - 2);
*y = (y_sum - y_min - y_max) / (XPT2046_AVG_NUM - 2); *y = (y_sum - y_min - y_max) / (XPT2046_AVG_NUM - 2);
return 2; return 2;
@ -171,15 +174,17 @@ static uint8_t xpt2046_ads_get(int16_t *x, int16_t *y)
*******************************************************************************/ *******************************************************************************/
static uint8_t xpt2046_adc2xy(int16_t *x, int16_t *y) static uint8_t xpt2046_adc2xy(int16_t *x, int16_t *y)
{ {
if((*x) > XPT2046_X_MIN) if ((*x) > XPT2046_X_MIN) {
(*x) -= XPT2046_X_MIN; (*x) -= XPT2046_X_MIN;
else } else {
(*x) = 0; (*x) = 0;
}
if((*y) > XPT2046_Y_MIN) if ((*y) > XPT2046_Y_MIN) {
(*y) -= XPT2046_Y_MIN; (*y) -= XPT2046_Y_MIN;
else } else {
(*y) = 0; (*y) = 0;
}
(*x) = (uint32_t)((uint32_t)(*x) * 240) / (*x) = (uint32_t)((uint32_t)(*x) * 240) /
(XPT2046_X_MAX - XPT2046_X_MIN); (XPT2046_X_MAX - XPT2046_X_MIN);
@ -204,39 +209,34 @@ uint8_t xpt2046_read(int16_t * x, int16_t * y)
static uint8_t avg_last = 0; static uint8_t avg_last = 0;
int16_t x1, y1; int16_t x1, y1;
if(xpt2046_ads_get(&x1, &y1)==0) if (xpt2046_ads_get(&x1, &y1) == 0) {
{
goto end; goto end;
} }
if(xpt2046_adc2xy(&x1,&y1)==0) if (xpt2046_adc2xy(&x1, &y1) == 0) {
{
goto end; goto end;
} }
if(avg_last == 0) if (avg_last == 0) {
{
avg_last = 1; avg_last = 1;
return 0; return 0;
} } else {
else
{
avg_last = 2; avg_last = 2;
xt = x1; *x = x1; xt = x1;
yt = y1; *y = y1; *x = x1;
yt = y1;
*y = y1;
return 1; return 1;
} }
end: end:
if (avg_last == 2)
{ if (avg_last == 2) {
avg_last = 1; avg_last = 1;
*x = xt; *x = xt;
*y = yt; *y = yt;
return 1; return 1;
} } else {
else
{
avg_last = 0; avg_last = 0;
return 0; return 0;
} }

View file

@ -43,30 +43,19 @@ Ring_Buffer_Type uart1_rx_rb;
void uart_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state) void uart_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{ {
if (state == UART_EVENT_RX_FIFO) if (state == UART_EVENT_RX_FIFO) {
{ if (size && size < Ring_Buffer_Get_Empty_Length(&uart1_rx_rb)) {
if (size && size < Ring_Buffer_Get_Empty_Length(&uart1_rx_rb))
{
Ring_Buffer_Write(&uart1_rx_rb, (uint8_t *)args, size); Ring_Buffer_Write(&uart1_rx_rb, (uint8_t *)args, size);
} } else {
else
{
MSG("RF\r\n"); MSG("RF\r\n");
} }
} } else if (state == UART_EVENT_RTO) {
else if (state == UART_EVENT_RTO) if (size && size < Ring_Buffer_Get_Empty_Length(&uart1_rx_rb)) {
{
if (size && size < Ring_Buffer_Get_Empty_Length(&uart1_rx_rb))
{
Ring_Buffer_Write(&uart1_rx_rb, (uint8_t *)args, size); Ring_Buffer_Write(&uart1_rx_rb, (uint8_t *)args, size);
} } else {
else
{
MSG("RTO\r\n"); MSG("RTO\r\n");
} }
} } else if (state == UART_RX_FER_IT) {
else if (state == UART_RX_FER_IT)
{
MSG("ov\r\n"); MSG("ov\r\n");
} }
} }
@ -75,8 +64,7 @@ void uart1_init(void)
uart_register(UART1_INDEX, "uart1", DEVICE_OFLAG_RDWR); uart_register(UART1_INDEX, "uart1", DEVICE_OFLAG_RDWR);
uart1 = device_find("uart1"); uart1 = device_find("uart1");
if (uart1) if (uart1) {
{
device_open(uart1, DEVICE_OFLAG_DMA_TX | DEVICE_OFLAG_INT_RX); //uart0 tx dma mode device_open(uart1, DEVICE_OFLAG_DMA_TX | DEVICE_OFLAG_INT_RX); //uart0 tx dma mode
device_control(uart1, DEVICE_CTRL_SUSPEND, NULL); device_control(uart1, DEVICE_CTRL_SUSPEND, NULL);
device_set_callback(uart1, uart_irq_callback); device_set_callback(uart1, uart_irq_callback);
@ -85,14 +73,14 @@ void uart1_init(void)
dma_register(DMA0_CH2_INDEX, "ch2", DEVICE_OFLAG_RDWR); dma_register(DMA0_CH2_INDEX, "ch2", DEVICE_OFLAG_RDWR);
dma_ch2 = device_find("ch2"); dma_ch2 = device_find("ch2");
if (dma_ch2)
{ if (dma_ch2) {
device_open(dma_ch2, 0); device_open(dma_ch2, 0);
//device_set_callback(dma_ch2, NULL); //device_set_callback(dma_ch2, NULL);
//device_control(dma_ch2, DEVICE_CTRL_SET_INT, NULL); //device_control(dma_ch2, DEVICE_CTRL_SET_INT, NULL);
} }
//device_control(uart1, DEVICE_CTRL_ATTACH_TX_DMA, dma_ch2);
//device_control(uart1, DEVICE_CTRL_ATTACH_TX_DMA, dma_ch2);
} }
void uart1_config(uint32_t baudrate, uart_databits_t databits, uart_parity_t parity, uart_stopbits_t stopbits) void uart1_config(uint32_t baudrate, uart_databits_t databits, uart_parity_t parity, uart_stopbits_t stopbits)
@ -102,20 +90,13 @@ void uart1_config(uint32_t baudrate, uart_databits_t databits, uart_parity_t par
cfg.stopbits = stopbits; cfg.stopbits = stopbits;
cfg.parity = parity; cfg.parity = parity;
if (databits == 5) if (databits == 5) {
{
cfg.databits = UART_DATA_LEN_5; cfg.databits = UART_DATA_LEN_5;
} } else if (databits == 6) {
else if (databits == 6)
{
cfg.databits = UART_DATA_LEN_6; cfg.databits = UART_DATA_LEN_6;
} } else if (databits == 7) {
else if (databits == 7)
{
cfg.databits = UART_DATA_LEN_7; cfg.databits = UART_DATA_LEN_7;
} } else if (databits == 8) {
else if (databits == 8)
{
cfg.databits = UART_DATA_LEN_8; cfg.databits = UART_DATA_LEN_8;
} }
@ -175,8 +156,7 @@ void uart_ringbuffer_init(void)
Ring_Buffer_Init(&uart1_rx_rb, uart_rx_mem, UART_RX_RINGBUFFER_SIZE, ringbuffer_lock, ringbuffer_unlock); Ring_Buffer_Init(&uart1_rx_rb, uart_rx_mem, UART_RX_RINGBUFFER_SIZE, ringbuffer_lock, ringbuffer_unlock);
} }
static dma_control_data_t uart_dma_ctrl_cfg = static dma_control_data_t uart_dma_ctrl_cfg = {
{
.bits.fix_cnt = 0, .bits.fix_cnt = 0,
.bits.dst_min_mode = 0, .bits.dst_min_mode = 0,
.bits.dst_add_mode = 0, .bits.dst_add_mode = 0,
@ -189,8 +169,7 @@ static dma_control_data_t uart_dma_ctrl_cfg =
.bits.I = 0, .bits.I = 0,
.bits.TransferSize = 4095 .bits.TransferSize = 4095
}; };
static dma_lli_ctrl_t uart_lli_list = static dma_lli_ctrl_t uart_lli_list = {
{
.src_addr = (uint32_t)src_buffer, .src_addr = (uint32_t)src_buffer,
.dst_addr = DMA_ADDR_UART1_TDR, .dst_addr = DMA_ADDR_UART1_TDR,
.nextlli = 0 .nextlli = 0
@ -198,14 +177,11 @@ static dma_lli_ctrl_t uart_lli_list =
void uart_send_from_ringbuffer(void) void uart_send_from_ringbuffer(void)
{ {
if(Ring_Buffer_Get_Length(&usb_rx_rb)) if (Ring_Buffer_Get_Length(&usb_rx_rb)) {
{ if (!device_control(dma_ch2, DMA_CHANNEL_GET_STATUS, NULL)) {
if (!device_control(dma_ch2, DMA_CHANNEL_GET_STATUS, NULL))
{
uint32_t avalibleCnt = Ring_Buffer_Read(&usb_rx_rb, src_buffer, UART_TX_DMA_SIZE); uint32_t avalibleCnt = Ring_Buffer_Read(&usb_rx_rb, src_buffer, UART_TX_DMA_SIZE);
if (avalibleCnt) if (avalibleCnt) {
{
dma_channel_stop(dma_ch2); dma_channel_stop(dma_ch2);
uart_dma_ctrl_cfg.bits.TransferSize = avalibleCnt; uart_dma_ctrl_cfg.bits.TransferSize = avalibleCnt;
memcpy(&uart_lli_list.cfg, &uart_dma_ctrl_cfg, sizeof(dma_control_data_t)); memcpy(&uart_lli_list.cfg, &uart_dma_ctrl_cfg, sizeof(dma_control_data_t));
@ -214,5 +190,4 @@ void uart_send_from_ringbuffer(void)
} }
} }
} }
} }

View file

@ -29,32 +29,39 @@ struct device *usb;
static void usb_dc_event_callback(struct device *dev, void *args, uint32_t size, uint32_t state) static void usb_dc_event_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{ {
switch (state) switch (state) {
{
case USB_DC_EVENT_ERROR: case USB_DC_EVENT_ERROR:
usbd_event_notify_handler(USB_EVENT_ERROR, NULL); usbd_event_notify_handler(USB_EVENT_ERROR, NULL);
break; break;
case USB_DC_EVENT_RESET: case USB_DC_EVENT_RESET:
usbd_event_notify_handler(USB_EVENT_RESET, NULL); usbd_event_notify_handler(USB_EVENT_RESET, NULL);
break; break;
case USB_DC_EVENT_SOF: case USB_DC_EVENT_SOF:
usbd_event_notify_handler(USB_EVENT_SOF, NULL); usbd_event_notify_handler(USB_EVENT_SOF, NULL);
break; break;
case USB_DC_EVENT_SETUP_NOTIFY: case USB_DC_EVENT_SETUP_NOTIFY:
usbd_event_notify_handler(USB_EVENT_SETUP_NOTIFY, NULL); usbd_event_notify_handler(USB_EVENT_SETUP_NOTIFY, NULL);
break; break;
case USB_DC_EVENT_EP0_IN_NOTIFY: case USB_DC_EVENT_EP0_IN_NOTIFY:
usbd_event_notify_handler(USB_EVENT_EP0_IN_NOTIFY, NULL); usbd_event_notify_handler(USB_EVENT_EP0_IN_NOTIFY, NULL);
break; break;
case USB_DC_EVENT_EP0_OUT_NOTIFY: case USB_DC_EVENT_EP0_OUT_NOTIFY:
usbd_event_notify_handler(USB_EVENT_EP0_OUT_NOTIFY, NULL); usbd_event_notify_handler(USB_EVENT_EP0_OUT_NOTIFY, NULL);
break; break;
case USB_DC_EVENT_EP_IN_NOTIFY: case USB_DC_EVENT_EP_IN_NOTIFY:
usbd_event_notify_handler(USB_EVENT_EP_IN_NOTIFY, args); usbd_event_notify_handler(USB_EVENT_EP_IN_NOTIFY, args);
break; break;
case USB_DC_EVENT_EP_OUT_NOTIFY: case USB_DC_EVENT_EP_OUT_NOTIFY:
usbd_event_notify_handler(USB_EVENT_EP_OUT_NOTIFY, args); usbd_event_notify_handler(USB_EVENT_EP_OUT_NOTIFY, args);
break; break;
default: default:
break; break;
} }

View file

@ -50,8 +50,8 @@ int device_register(struct device *dev, const char *name, uint16_t flag)
{ {
struct device *dev_obj; struct device *dev_obj;
dev_obj = dlist_entry(node, struct device, list); dev_obj = dlist_entry(node, struct device, list);
if (dev_obj == dev)
{ if (dev_obj == dev) {
return -DEVICE_EEXIST; return -DEVICE_EEXIST;
} }
} }
@ -70,7 +70,8 @@ int device_register(struct device *dev, const char *name, uint16_t flag)
* *
* @return device header * @return device header
*/ */
dlist_t * device_get_list_header(void){ dlist_t *device_get_list_header(void)
{
return &device_head; return &device_head;
} }
/** /**
@ -85,15 +86,16 @@ dlist_t * device_get_list_header(void){
int device_unregister(const char *name) int device_unregister(const char *name)
{ {
struct device *dev = device_find(name); struct device *dev = device_find(name);
if(!dev)
if (!dev) {
return -DEVICE_ENODEV; return -DEVICE_ENODEV;
}
/* remove from old list */ /* remove from old list */
dlist_remove(&(dev->list)); dlist_remove(&(dev->list));
return DEVICE_EOK; return DEVICE_EOK;
} }
/** /**
* This function finds a device driver by specified name. * This function finds a device driver by specified name.
* *
@ -109,8 +111,8 @@ struct device *device_find(const char *name)
dlist_for_each(node, &device_head) dlist_for_each(node, &device_head)
{ {
dev = dlist_entry(node, struct device, list); dev = dlist_entry(node, struct device, list);
if (strncmp(dev->name, name, NAME_MAX) == 0)
{ if (strncmp(dev->name, name, NAME_MAX) == 0) {
return dev; return dev;
} }
} }
@ -129,19 +131,16 @@ int device_open(struct device *dev, uint16_t oflag)
{ {
int retval = DEVICE_EOK; int retval = DEVICE_EOK;
if ((dev->status == DEVICE_REGISTERED) || (dev->status == DEVICE_CLOSED)) if ((dev->status == DEVICE_REGISTERED) || (dev->status == DEVICE_CLOSED)) {
{ if (dev_open != NULL) {
if (dev_open != NULL)
{
retval = dev_open(dev, oflag); retval = dev_open(dev, oflag);
dev->status = DEVICE_OPENED; dev->status = DEVICE_OPENED;
dev->oflag |= oflag; dev->oflag |= oflag;
} }
} } else {
else
{
retval = -DEVICE_EFAULT; retval = -DEVICE_EFAULT;
} }
return retval; return retval;
} }
/** /**
@ -155,19 +154,16 @@ int device_close(struct device *dev)
{ {
int retval = DEVICE_EOK; int retval = DEVICE_EOK;
if (dev->status == DEVICE_OPENED) if (dev->status == DEVICE_OPENED) {
{ if (dev_close != NULL) {
if (dev_close != NULL)
{
retval = dev_close(dev); retval = dev_close(dev);
dev->status = DEVICE_CLOSED; dev->status = DEVICE_CLOSED;
dev->oflag = 0; dev->oflag = 0;
} }
} } else {
else
{
retval = -DEVICE_EFAULT; retval = -DEVICE_EFAULT;
} }
return retval; return retval;
} }
/** /**
@ -183,17 +179,14 @@ int device_control(struct device *dev, int cmd, void *args)
{ {
int retval = DEVICE_EOK; int retval = DEVICE_EOK;
if (dev->status >= DEVICE_REGISTERED) if (dev->status >= DEVICE_REGISTERED) {
{ if (dev_control != NULL) {
if (dev_control != NULL)
{
retval = dev_control(dev, cmd, args); retval = dev_control(dev, cmd, args);
} }
} } else {
else
{
retval = -DEVICE_EFAULT; retval = -DEVICE_EFAULT;
} }
return retval; return retval;
} }
/** /**
@ -210,17 +203,14 @@ int device_write(struct device *dev, uint32_t pos, const void *buffer, uint32_t
{ {
int retval = DEVICE_EOK; int retval = DEVICE_EOK;
if (dev->status == DEVICE_OPENED) if (dev->status == DEVICE_OPENED) {
{ if (dev_write != NULL) {
if (dev_write != NULL)
{
retval = dev_write(dev, pos, buffer, size); retval = dev_write(dev, pos, buffer, size);
} }
} } else {
else
{
retval = -DEVICE_EFAULT; retval = -DEVICE_EFAULT;
} }
return retval; return retval;
} }
/** /**
@ -237,17 +227,14 @@ int device_read(struct device *dev, uint32_t pos, void *buffer, uint32_t size)
{ {
int retval = DEVICE_EOK; int retval = DEVICE_EOK;
if (dev->status == DEVICE_OPENED) if (dev->status == DEVICE_OPENED) {
{ if (dev_read != NULL) {
if (dev_read != NULL)
{
retval = dev_read(dev, pos, buffer, size); retval = dev_read(dev, pos, buffer, size);
} }
} } else {
else
{
retval = -DEVICE_EFAULT; retval = -DEVICE_EFAULT;
} }
return retval; return retval;
} }
/** /**
@ -264,16 +251,13 @@ int device_set_callback(struct device *dev, void (*callback)(struct device *dev,
{ {
int retval = DEVICE_EOK; int retval = DEVICE_EOK;
if (dev->status > DEVICE_UNREGISTER) if (dev->status > DEVICE_UNREGISTER) {
{ if (callback != NULL) {
if (callback != NULL)
{
dev->callback = callback; dev->callback = callback;
} }
} } else {
else
{
retval = -DEVICE_EFAULT; retval = -DEVICE_EFAULT;
} }
return retval; return retval;
} }

View file

@ -26,7 +26,7 @@
#include "drv_list.h" #include "drv_list.h"
#include "bflb_platform.h" #include "bflb_platform.h"
#define NAME_MAX 10 /* max device name*/ #define NAME_MAX 20 /* max device name*/
#define DEVICE_OFLAG_RDONLY 0x1000 /* open with read only */ #define DEVICE_OFLAG_RDONLY 0x1000 /* open with read only */
#define DEVICE_OFLAG_WRONLY 0x2000 /* open with write only */ #define DEVICE_OFLAG_WRONLY 0x2000 /* open with write only */
@ -54,6 +54,7 @@
#define DEVICE_CTRL_RX_DMA_RESUME 0x0d #define DEVICE_CTRL_RX_DMA_RESUME 0x0d
#define DEVICE_CTRL_RESVD1 0x0E #define DEVICE_CTRL_RESVD1 0x0E
#define DEVICE_CTRL_RESVD2 0x0F #define DEVICE_CTRL_RESVD2 0x0F
/* /*
* POSIX Error codes * POSIX Error codes
*/ */
@ -64,6 +65,7 @@
#define DEVICE_ENODEV 19 /* No such device */ #define DEVICE_ENODEV 19 /* No such device */
#define DEVICE_EINVAL 22 /* Invalid argument */ #define DEVICE_EINVAL 22 /* Invalid argument */
#define DEVICE_ENOSPACE 23 /* No more Device for Allocate */ #define DEVICE_ENOSPACE 23 /* No more Device for Allocate */
#define __ASSERT_PRINT(fmt, ...) bflb_platform_printf(fmt, ##__VA_ARGS__) #define __ASSERT_PRINT(fmt, ...) bflb_platform_printf(fmt, ##__VA_ARGS__)
#define __ASSERT_LOC(test) \ #define __ASSERT_LOC(test) \
@ -79,9 +81,7 @@
} \ } \
} while (0) } while (0)
enum device_class_type {
enum device_class_type
{
DEVICE_CLASS_NONE = 0, DEVICE_CLASS_NONE = 0,
DEVICE_CLASS_GPIO, DEVICE_CLASS_GPIO,
DEVICE_CLASS_UART, DEVICE_CLASS_UART,
@ -98,16 +98,14 @@ enum device_class_type
DEVICE_CLASS_SEC_HASH, DEVICE_CLASS_SEC_HASH,
}; };
enum device_status_type enum device_status_type {
{
DEVICE_UNREGISTER = 0, DEVICE_UNREGISTER = 0,
DEVICE_REGISTERED, DEVICE_REGISTERED,
DEVICE_OPENED, DEVICE_OPENED,
DEVICE_CLOSED DEVICE_CLOSED
}; };
struct device struct device {
{
char name[NAME_MAX]; /*name of device */ char name[NAME_MAX]; /*name of device */
dlist_t list; /*list node of device */ dlist_t list; /*list node of device */
enum device_status_type status; /*status of device */ enum device_status_type status; /*status of device */

View file

@ -1,6 +1,5 @@
#ifndef _COMPAT_ATTRIBUTE_H_ #ifndef _COMPAT_ATTRIBUTE_H_
#define _COMPAT_ATTRIBUTE_H_ #define _COMPAT_ATTRIBUTE_H_

View file

@ -3,4 +3,3 @@
#ifndef _COMPAT_COMPILER_H_ #ifndef _COMPAT_COMPILER_H_
#define _COMPAT_COMPILER_H_ #define _COMPAT_COMPILER_H_
#endif /* _COMPAT_COMPILER_H_ */ #endif /* _COMPAT_COMPILER_H_ */

View file

@ -3,7 +3,6 @@
#ifndef _COMPAT_ERRNO_H_ #ifndef _COMPAT_ERRNO_H_
#define _COMPAT_ERRNO_H_ #define _COMPAT_ERRNO_H_
#include <errno.h> #include <errno.h>
#endif /* _COMPAT_ERRNO_H_ */ #endif /* _COMPAT_ERRNO_H_ */

View file

@ -1,10 +1,8 @@
#ifndef _COMPAT_TIME_H_ #ifndef _COMPAT_TIME_H_
#define _COMPAT_TIME_H_ #define _COMPAT_TIME_H_
#include <sys/time.h> #include <sys/time.h>
#endif /* _COMPAT_TIME_H_ */ #endif /* _COMPAT_TIME_H_ */

View file

@ -4,8 +4,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
_GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// @brief Base class for ctype. /// @brief Base class for ctype.
struct ctype_base struct ctype_base {
{
// Non-standard typedefs. // Non-standard typedefs.
typedef const int *__to_type; typedef const int *__to_type;
@ -26,4 +25,4 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}; };
_GLIBCXX_END_NAMESPACE_VERSION _GLIBCXX_END_NAMESPACE_VERSION
} // namespace } // namespace )

View file

@ -26,7 +26,6 @@
#define SHRT_MAX 32767 #define SHRT_MAX 32767
#define USHRT_MAX 65535 #define USHRT_MAX 65535
#define INT_MIN (-2147483647 - 1) #define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647 #define INT_MAX 2147483647
#define UINT_MAX 4294967295U #define UINT_MAX 4294967295U
@ -49,5 +48,4 @@
#define LONGLONG_MAX 9223372036854775807LL #define LONGLONG_MAX 9223372036854775807LL
#define ULONGLONG_MAX 18446744073709551615ULL #define ULONGLONG_MAX 18446744073709551615ULL
#endif /* _LIMITS_H */ #endif /* _LIMITS_H */

View file

@ -25,7 +25,9 @@ typedef unsigned int uint_fast32_t;
typedef int intptr_t; typedef int intptr_t;
typedef unsigned int uintptr_t; typedef unsigned int uintptr_t;
#undef __INT64_C
#define __INT64_C(c) c##LL #define __INT64_C(c) c##LL
#undef __UINT64_C
#define __UINT64_C(c) c##ULL #define __UINT64_C(c) c##ULL
#elif __riscv_xlen == 64 #elif __riscv_xlen == 64
typedef long long intptr_t; typedef long long intptr_t;

View file

@ -41,6 +41,7 @@ double strntof(const char *nptr, char **endptr, int base, size_t n)
/* Single optional + or - */ /* Single optional + or - */
if (n) { if (n) {
char c = *nptr; char c = *nptr;
if (c == '-' || c == '+') { if (c == '-' || c == '+') {
minus = (c == '-'); minus = (c == '-');
nptr++; nptr++;
@ -78,6 +79,7 @@ double strntof(const char *nptr, char **endptr, int base, size_t n)
if (*nptr == '.') { if (*nptr == '.') {
n--; n--;
nptr++; nptr++;
while (n && (d = digitval(*nptr)) >= 0 && d < base) { while (n && (d = digitval(*nptr)) >= 0 && d < base) {
m = m * base + d; m = m * base + d;
n--; n--;
@ -86,17 +88,16 @@ double strntof(const char *nptr, char **endptr, int base, size_t n)
} }
} }
if (endptr) if (endptr) {
*endptr = (char *)nptr; *endptr = (char *)nptr;
}
v = v + (m / divisor); v = v + (m / divisor);
return minus ? -v : v; return minus ? -v : v;
} }
double atof(const char *nptr) double atof(const char *nptr)
{ {
return strntof(nptr, NULL, 0, strlen(nptr)); return strntof(nptr, NULL, 0, strlen(nptr));
} }

View file

@ -17,15 +17,17 @@ void *bsearch(const void *key, const void *base, size_t nmemb,
mididx * size; mididx * size;
int diff = cmp(key, midobj); int diff = cmp(key, midobj);
if (diff == 0) if (diff == 0) {
return (void *)midobj; return (void *)midobj;
}
if (diff > 0) { if (diff > 0) {
base = (const unsigned char *)midobj + size; base = (const unsigned char *)midobj + size;
nmemb -= mididx + 1; nmemb -= mididx + 1;
} else } else {
nmemb = mididx; nmemb = mididx;
} }
}
return NULL; return NULL;
} }

View file

@ -15,6 +15,7 @@ void __assert_func(const char *file, int line,
/* Ensure that nothing runs after this */ /* Ensure that nothing runs after this */
while(1) while(1)
; ;
#endif #endif
} }

View file

@ -8,6 +8,7 @@
#include <ctype.h> #include <ctype.h>
#define CTYPEFUNC(X) \ #define CTYPEFUNC(X) \
int X(int c) { \ int X(int c) \
{ \
return __ctype_##X(c); \ return __ctype_##X(c); \
} }

View file

@ -12,10 +12,13 @@
int fnmatch(const char *p, const char *s, int flags) int fnmatch(const char *p, const char *s, int flags)
{ {
if (flags & FNM_PATHNAME && *s == '/') if (flags & FNM_PATHNAME && *s == '/') {
return (*p != '/') || fnmatch(p + 1, s + 1, flags); return (*p != '/') || fnmatch(p + 1, s + 1, flags);
if (flags & FNM_PERIOD && *s == '.') }
if (flags & FNM_PERIOD && *s == '.') {
return (*p != '.') || fnmatch(p + 1, s + 1, flags); return (*p != '.') || fnmatch(p + 1, s + 1, flags);
}
flags &= ~FNM_PERIOD; /* Only applies at beginning */ flags &= ~FNM_PERIOD; /* Only applies at beginning */
@ -25,52 +28,70 @@ int fnmatch(const char *p, const char *s, int flags)
} }
if (*s == '\0') { if (*s == '\0') {
while (*p == '*') while (*p == '*') {
p++; p++;
}
return (*p != '\0'); return (*p != '\0');
} }
switch (*p) { switch (*p) {
case '[': case '[': {
{
int not_mark = 0; int not_mark = 0;
p++; p++;
if (*p == '!') { if (*p == '!') {
not_mark = 1; not_mark = 1;
p++; p++;
} }
while ((*p != '\0') && (*p != ']')) { while ((*p != '\0') && (*p != ']')) {
int match = 0; int match = 0;
if (p[1] == '-') { if (p[1] == '-') {
if ((*s >= *p) && (*s <= p[2])) if ((*s >= *p) && (*s <= p[2])) {
match = 1; match = 1;
}
p += 3; p += 3;
} else { } else {
match = (*p == *s); match = (*p == *s);
p++; p++;
} }
if (match ^ not_mark) { if (match ^ not_mark) {
while ((*p != '\0') && (*p != ']')) while ((*p != '\0') && (*p != ']')) {
p++; p++;
if (*p == ']') }
if (*p == ']') {
return fnmatch(p + 1, s + 1, flags); return fnmatch(p + 1, s + 1, flags);
} }
} }
} }
break; } break;
case '*': case '*':
if (fnmatch(p, s+1, flags)) if (fnmatch(p, s + 1, flags)) {
return fnmatch(p + 1, s, flags); return fnmatch(p + 1, s, flags);
}
return 0; return 0;
case '\0': case '\0':
if (*s == '\0') { if (*s == '\0') {
return 0; return 0;
} }
break; break;
default: default:
if ((*p == *s) || (*p == '?')) if ((*p == *s) || (*p == '?')) {
return fnmatch(p + 1, s + 1, flags); return fnmatch(p + 1, s + 1, flags);
}
break; break;
} }
return 1; return 1;
} }

View file

@ -19,9 +19,11 @@ void *memccpy(void *dst, const void *src, int c, size_t n)
while (n--) { while (n--) {
*q++ = ch = *p++; *q++ = ch = *p++;
if (ch == (char)c)
if (ch == (char)c) {
return q; return q;
} }
}
return NULL; /* No instance of "c" found */ return NULL; /* No instance of "c" found */
} }

View file

@ -14,8 +14,10 @@ void *memchr(const void *s, int c, size_t n)
const unsigned char *sp = (unsigned char *)s; const unsigned char *sp = (unsigned char *)s;
while (n--) { while (n--) {
if (*sp == (unsigned char)c) if (*sp == (unsigned char)c) {
return (void *)sp; return (void *)sp;
}
sp++; sp++;
} }

View file

@ -15,9 +15,11 @@ int memcmp(const void *s1, const void *s2, size_t n)
while (n--) { while (n--) {
d = (int)*c1++ - (int)*c2++; d = (int)*c1++ - (int)*c2++;
if (d)
if (d) {
break; break;
} }
}
return d; return d;
} }

View file

@ -9,8 +9,10 @@ void *memcpy(void *dst, const void *src, size_t n)
{ {
const char *p = (char *)src; const char *p = (char *)src;
char *q = (char *)dst; char *q = (char *)dst;
while (n--) { while (n--) {
*q++ = *p++; *q++ = *p++;
} }
return dst; return dst;
} }

View file

@ -22,8 +22,9 @@ void *memmem(const void *haystack, size_t n, const void *needle, size_t m)
size_t j, k, l; size_t j, k, l;
if (m > n || !m || !n) if (m > n || !m || !n) {
return NULL; return NULL;
}
if (1 != m) { if (1 != m) {
if (x[0] == x[1]) { if (x[0] == x[1]) {
@ -35,20 +36,24 @@ void *memmem(const void *haystack, size_t n, const void *needle, size_t m)
} }
j = 0; j = 0;
while (j <= n - m) { while (j <= n - m) {
if (x[1] != y[j + 1]) { if (x[1] != y[j + 1]) {
j += k; j += k;
} else { } else {
if (!memcmp(x + 2, y + j + 2, m - 2) if (!memcmp(x + 2, y + j + 2, m - 2) && x[0] == y[j]) {
&& x[0] == y[j])
return (void *)&y[j]; return (void *)&y[j];
}
j += l; j += l;
} }
} }
} else } else
do { do {
if (*y == *x) if (*y == *x) {
return (void *)y; return (void *)y;
}
y++; y++;
} while (--n); } while (--n);

View file

@ -14,8 +14,10 @@ void *memrchr(const void *s, int c, size_t n)
const unsigned char *sp = (const unsigned char *)s + n - 1; const unsigned char *sp = (const unsigned char *)s + n - 1;
while (n--) { while (n--) {
if (*sp == (unsigned char)c) if (*sp == (unsigned char)c) {
return (void *)sp; return (void *)sp;
}
sp--; sp--;
} }

View file

@ -8,9 +8,12 @@
void *memset(void *dst, int c, size_t n) void *memset(void *dst, int c, size_t n)
{ {
char *q = (char *)dst; char *q = (char *)dst;
while (n--) { while (n--) {
*q++ = c; *q++ = c;
__asm volatile ("":::"memory"); __asm volatile("" ::
: "memory");
} }
return dst; return dst;
} }

View file

@ -18,11 +18,15 @@
static __inline size_t newgap(size_t gap) static __inline size_t newgap(size_t gap)
{ {
gap = (gap * 10) / 13; gap = (gap * 10) / 13;
if (gap == 9 || gap == 10)
gap = 11;
if (gap < 1) if (gap == 9 || gap == 10) {
gap = 11;
}
if (gap < 1) {
gap = 1; gap = 1;
}
return gap; return gap;
} }
@ -34,8 +38,9 @@ void qsort(void *base, size_t nmemb, size_t size,
char *p1, *p2; char *p1, *p2;
int swapped; int swapped;
if (!nmemb) if (!nmemb) {
return; return;
}
do { do {
gap = newgap(gap); gap = newgap(gap);
@ -43,6 +48,7 @@ void qsort(void *base, size_t nmemb, size_t size,
for (i = 0, p1 = (char *)base; i < nmemb - gap; i++, p1 += size) { for (i = 0, p1 = (char *)base; i < nmemb - gap; i++, p1 += size) {
j = i + gap; j = i + gap;
if (compar(p1, p2 = (char *)base + j * size) > 0) { if (compar(p1, p2 = (char *)base + j * size) > 0) {
memswap(p1, p2, size); memswap(p1, p2, size);
swapped = 1; swapped = 1;

View file

@ -20,9 +20,11 @@ int strcasecmp(const char *s1, const char *s2)
/* toupper() expects an unsigned char (implicitly cast to int) /* toupper() expects an unsigned char (implicitly cast to int)
as input, and returns an int, which is exactly what we want. */ as input, and returns an int, which is exactly what we want. */
d = toupper(ch = *c1++) - toupper(*c2++); d = toupper(ch = *c1++) - toupper(*c2++);
if (d || !ch)
if (d || !ch) {
break; break;
} }
}
return d; return d;
} }

View file

@ -13,8 +13,10 @@ __WEAK__
char *strchr(const char *s, int c) char *strchr(const char *s, int c)
{ {
while (*s != (char)c) { while (*s != (char)c) {
if (!*s) if (!*s) {
return NULL; return NULL;
}
s++; s++;
} }

View file

@ -21,9 +21,11 @@ int strcmp(const char *s1, const char *s2)
while (1) { while (1) {
d = (int)(ch = *c1++) - (int)*c2++; d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
if (d || !ch) {
break; break;
} }
}
return d; return d;
} }

View file

@ -19,12 +19,15 @@ size_t strlcat(char *dst, const char *src, size_t size)
q++; q++;
bytes++; bytes++;
} }
if (bytes == size)
if (bytes == size) {
return (bytes + strlen(src)); return (bytes + strlen(src));
}
while ((ch = *p++)) { while ((ch = *p++)) {
if (bytes + 1 < size) if (bytes + 1 < size) {
*q++ = ch; *q++ = ch;
}
bytes++; bytes++;
} }

View file

@ -16,15 +16,17 @@ size_t strlcpy(char *dst, const char *src, size_t size)
char ch; char ch;
while ((ch = *p++)) { while ((ch = *p++)) {
if (bytes + 1 < size) if (bytes + 1 < size) {
*q++ = ch; *q++ = ch;
}
bytes++; bytes++;
} }
/* If size == 0 there is no space for a final null... */ /* If size == 0 there is no space for a final null... */
if (size) if (size) {
*q = '\0'; *q = '\0';
}
return bytes; return bytes;
} }

View file

@ -11,7 +11,10 @@
size_t strlen(const char *s) size_t strlen(const char *s)
{ {
const char *ss = s; const char *ss = s;
while (*ss)
while (*ss) {
ss++; ss++;
}
return ss - s; return ss - s;
} }

View file

@ -20,9 +20,11 @@ int strncasecmp(const char *s1, const char *s2, size_t n)
/* toupper() expects an unsigned char (implicitly cast to int) /* toupper() expects an unsigned char (implicitly cast to int)
as input, and returns an int, which is exactly what we want. */ as input, and returns an int, which is exactly what we want. */
d = toupper(ch = *c1++) - toupper(*c2++); d = toupper(ch = *c1++) - toupper(*c2++);
if (d || !ch)
if (d || !ch) {
break; break;
} }
}
return d; return d;
} }

View file

@ -16,9 +16,12 @@ char *strncat(char *dst, const char *src, size_t n)
while (n--) { while (n--) {
*q++ = ch = *p++; *q++ = ch = *p++;
if (!ch)
if (!ch) {
return dst; return dst;
} }
}
*q = '\0'; *q = '\0';
return dst; return dst;

View file

@ -17,9 +17,11 @@ int strncmp(const char *s1, const char *s2, size_t n)
while (n--) { while (n--) {
d = (int)(ch = *c1++) - (int)*c2++; d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
if (d || !ch) {
break; break;
} }
}
return d; return d;
} }

View file

@ -17,9 +17,11 @@ char *strncpy(char *dst, const char *src, size_t n)
while (n) { while (n) {
n--; n--;
*q++ = ch = *p++; *q++ = ch = *p++;
if (!ch)
if (!ch) {
break; break;
} }
}
/* The specs say strncpy() fills the entire buffer with NUL. Sigh. */ /* The specs say strncpy() fills the entire buffer with NUL. Sigh. */
memset(q, 0, n); memset(q, 0, n);

View file

@ -18,5 +18,6 @@ size_t strnlen(const char *s, size_t maxlen)
ss++; ss++;
maxlen--; maxlen--;
} }
return ss - s; return ss - s;
} }

View file

@ -40,6 +40,7 @@ uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
/* Single optional + or - */ /* Single optional + or - */
if (n) { if (n) {
char c = *nptr; char c = *nptr;
if (c == '-' || c == '+') { if (c == '-' || c == '+') {
minus = (c == '-'); minus = (c == '-');
nptr++; nptr++;
@ -74,8 +75,9 @@ uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
nptr++; nptr++;
} }
if (endptr) if (endptr) {
*endptr = (char *)nptr; *endptr = (char *)nptr;
}
return minus ? -v : v; return minus ? -v : v;
} }

View file

@ -13,8 +13,10 @@ char *strrchr(const char *s, int c)
const char *found = NULL; const char *found = NULL;
while (*s) { while (*s) {
if (*s == (char)c) if (*s == (char)c) {
found = s; found = s;
}
s++; s++;
} }

View file

@ -13,12 +13,15 @@ char *strsep(char **stringp, const char *delim)
char *s = *stringp; char *s = *stringp;
char *e; char *e;
if (!s) if (!s) {
return NULL; return NULL;
}
e = strpbrk(s, delim); e = strpbrk(s, delim);
if (e)
if (e) {
*e++ = '\0'; *e++ = '\0';
}
*stringp = e; *stringp = e;
return s; return s;

View file

@ -6,8 +6,9 @@
char *strtok_r(char *s, const char *delim, char **holder) char *strtok_r(char *s, const char *delim, char **holder)
{ {
if (s) if (s) {
*holder = s; *holder = s;
}
do { do {
s = strsep(holder, delim); s = strsep(holder, delim);

View file

@ -19,15 +19,18 @@ size_t __strxspn(const char *s, const char *map, int parity)
/* Create bitmap */ /* Create bitmap */
memset(matchmap, 0, sizeof matchmap); memset(matchmap, 0, sizeof matchmap);
while (*map)
while (*map) {
matchmap[(unsigned char)*map++] = 1; matchmap[(unsigned char)*map++] = 1;
}
/* Make sure the null character never matches */ /* Make sure the null character never matches */
matchmap[0] = parity; matchmap[0] = parity;
/* Calculate span length */ /* Calculate span length */
while (matchmap[(unsigned char)*s++] ^ parity) while (matchmap[(unsigned char)*s++] ^ parity) {
n++; n++;
}
return n; return n;
} }

View file

@ -43,9 +43,14 @@ enum ranks {
#define SIZE_T_RANK rank_long #define SIZE_T_RANK rank_long
#define PTRDIFF_T_RANK rank_long #define PTRDIFF_T_RANK rank_long
#define EMIT(x) { if (o < n) { *q++ = (x); } o++; } #define EMIT(x) \
static size_t { \
format_int(char *q, size_t n, uintmax_t val, unsigned int flags, if (o < n) { \
*q++ = (x); \
} \
o++; \
}
static size_t format_int(char *q, size_t n, uintmax_t val, unsigned int flags,
int base, int width, int prec) int base, int width, int prec)
{ {
char *qq; char *qq;
@ -69,6 +74,7 @@ format_int(char *q, size_t n, uintmax_t val, unsigned int flags,
/* Count the number of digits needed. This returns zero for 0. */ /* Count the number of digits needed. This returns zero for 0. */
tmpval = val; tmpval = val;
while (tmpval) { while (tmpval) {
tmpval /= base; tmpval /= base;
ndigits++; ndigits++;
@ -77,9 +83,10 @@ format_int(char *q, size_t n, uintmax_t val, unsigned int flags,
/* Adjust ndigits for size of output */ /* Adjust ndigits for size of output */
if (flags & FL_HASH && base == 8) { if (flags & FL_HASH && base == 8) {
if (prec < ndigits + 1) if (prec < ndigits + 1) {
prec = ndigits + 1; prec = ndigits + 1;
} }
}
if (ndigits < prec) { if (ndigits < prec) {
ndigits = prec; /* Mandatory number padding */ ndigits = prec; /* Mandatory number padding */
@ -100,8 +107,10 @@ format_int(char *q, size_t n, uintmax_t val, unsigned int flags,
/* Now compute the number of nondigits */ /* Now compute the number of nondigits */
nchars = ndigits; nchars = ndigits;
if (minus || (flags & (FL_PLUS | FL_SPACE))) if (minus || (flags & (FL_PLUS | FL_SPACE))) {
nchars++; /* Need space for sign */ nchars++; /* Need space for sign */
}
if ((flags & FL_HASH) && base == 16) { if ((flags & FL_HASH) && base == 16) {
nchars += 2; /* Add 0x for hex */ nchars += 2; /* Add 0x for hex */
} }
@ -143,20 +152,28 @@ format_int(char *q, size_t n, uintmax_t val, unsigned int flags,
oo = o; /* Temporary values */ oo = o; /* Temporary values */
b4tick = tickskip; b4tick = tickskip;
while (ndigits > 0) { while (ndigits > 0) {
if (!b4tick--) { if (!b4tick--) {
qq--; qq--;
oo--; oo--;
ndigits--; ndigits--;
if (oo < n)
if (oo < n) {
*qq = '_'; *qq = '_';
}
b4tick = tickskip - 1; b4tick = tickskip - 1;
} }
qq--; qq--;
oo--; oo--;
ndigits--; ndigits--;
if (oo < n)
if (oo < n) {
*qq = digits[val % base]; *qq = digits[val % base];
}
val /= base; val /= base;
} }
@ -188,75 +205,91 @@ static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int
double fi, fj; double fi, fj;
char *p, *p1; char *p, *p1;
if (ndigits < 0) ndigits = 0; if (ndigits < 0) {
if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2; ndigits = 0;
}
if (ndigits >= CVTBUFSIZE - 1) {
ndigits = CVTBUFSIZE - 2;
}
r2 = 0; r2 = 0;
*sign = 0; *sign = 0;
p = &buf[0]; p = &buf[0];
if (arg < 0)
{ if (arg < 0) {
*sign = 1; *sign = 1;
arg = -arg; arg = -arg;
} }
arg = modf(arg, &fi); arg = modf(arg, &fi);
p1 = &buf[CVTBUFSIZE]; p1 = &buf[CVTBUFSIZE];
if (fi != 0) if (fi != 0) {
{
p1 = &buf[CVTBUFSIZE]; p1 = &buf[CVTBUFSIZE];
while (fi != 0)
{ while (fi != 0) {
fj = modf(fi / 10, &fi); fj = modf(fi / 10, &fi);
*--p1 = (int)((fj + 0.03) * 10) + '0'; *--p1 = (int)((fj + 0.03) * 10) + '0';
r2++; r2++;
} }
while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++;
while (p1 < &buf[CVTBUFSIZE]) {
*p++ = *p1++;
} }
else if (arg > 0) } else if (arg > 0) {
{ while ((fj = arg * 10) < 1) {
while ((fj = arg * 10) < 1)
{
arg = fj; arg = fj;
r2--; r2--;
} }
} }
p1 = &buf[ndigits]; p1 = &buf[ndigits];
if (eflag == 0) p1 += r2;
if (eflag == 0) {
p1 += r2;
}
*decpt = r2; *decpt = r2;
if (p1 < &buf[0])
{ if (p1 < &buf[0]) {
buf[0] = '\0'; buf[0] = '\0';
return buf; return buf;
} }
while (p <= p1 && p < &buf[CVTBUFSIZE])
{ while (p <= p1 && p < &buf[CVTBUFSIZE]) {
arg *= 10; arg *= 10;
arg = modf(arg, &fj); arg = modf(arg, &fj);
*p++ = (int)fj + '0'; *p++ = (int)fj + '0';
} }
if (p1 >= &buf[CVTBUFSIZE])
{ if (p1 >= &buf[CVTBUFSIZE]) {
buf[CVTBUFSIZE - 1] = '\0'; buf[CVTBUFSIZE - 1] = '\0';
return buf; return buf;
} }
p = p1; p = p1;
*p1 += 5; *p1 += 5;
while (*p1 > '9')
{ while (*p1 > '9') {
*p1 = '0'; *p1 = '0';
if (p1 > buf)
if (p1 > buf) {
++*--p1; ++*--p1;
else } else {
{
*p1 = '1'; *p1 = '1';
(*decpt)++; (*decpt)++;
if (eflag == 0)
{ if (eflag == 0) {
if (p > buf) *p = '0'; if (p > buf) {
*p = '0';
}
p++; p++;
} }
} }
} }
*p = '\0'; *p = '\0';
return buf; return buf;
} }
@ -273,11 +306,14 @@ char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
static void ee_bufcpy(char *d, char *s, int count); static void ee_bufcpy(char *d, char *s, int count);
void ee_bufcpy(char *pd, char *ps, int count) { void ee_bufcpy(char *pd, char *ps, int count)
{
char *pe = ps + count; char *pe = ps + count;
while (ps!=pe)
while (ps != pe) {
*pd++ = *ps++; *pd++ = *ps++;
} }
}
static void parse_float(double value, char *buffer, char fmt, int precision) static void parse_float(double value, char *buffer, char fmt, int precision)
{ {
@ -287,56 +323,57 @@ static void parse_float(double value, char *buffer, char fmt, int precision)
int capexp = 0; int capexp = 0;
int magnitude; int magnitude;
if (fmt == 'G' || fmt == 'E') if (fmt == 'G' || fmt == 'E') {
{
capexp = 1; capexp = 1;
fmt += 'a' - 'A'; fmt += 'a' - 'A';
} }
if (fmt == 'g') if (fmt == 'g') {
{
digits = ecvtbuf(value, precision, &decpt, &sign, cvtbuf); digits = ecvtbuf(value, precision, &decpt, &sign, cvtbuf);
magnitude = decpt - 1; magnitude = decpt - 1;
if (magnitude < -4 || magnitude > precision - 1)
{ if (magnitude < -4 || magnitude > precision - 1) {
fmt = 'e'; fmt = 'e';
precision -= 1; precision -= 1;
} } else {
else
{
fmt = 'f'; fmt = 'f';
precision -= decpt; precision -= decpt;
} }
} }
if (fmt == 'e') if (fmt == 'e') {
{
digits = ecvtbuf(value, precision + 1, &decpt, &sign, cvtbuf); digits = ecvtbuf(value, precision + 1, &decpt, &sign, cvtbuf);
if (sign) *buffer++ = '-'; if (sign) {
*buffer++ = '-';
}
*buffer++ = *digits; *buffer++ = *digits;
if (precision > 0) *buffer++ = '.';
if (precision > 0) {
*buffer++ = '.';
}
ee_bufcpy(buffer, digits + 1, precision); ee_bufcpy(buffer, digits + 1, precision);
buffer += precision; buffer += precision;
*buffer++ = capexp ? 'E' : 'e'; *buffer++ = capexp ? 'E' : 'e';
if (decpt == 0) if (decpt == 0) {
{ if (value == 0.0) {
if (value == 0.0)
exp = 0; exp = 0;
else } else {
exp = -1; exp = -1;
} }
else } else {
exp = decpt - 1; exp = decpt - 1;
}
if (exp < 0) if (exp < 0) {
{
*buffer++ = '-'; *buffer++ = '-';
exp = -exp; exp = -exp;
} } else {
else
*buffer++ = '+'; *buffer++ = '+';
}
buffer[2] = (exp % 10) + '0'; buffer[2] = (exp % 10) + '0';
exp = exp / 10; exp = exp / 10;
@ -344,37 +381,45 @@ static void parse_float(double value, char *buffer, char fmt, int precision)
exp = exp / 10; exp = exp / 10;
buffer[0] = (exp % 10) + '0'; buffer[0] = (exp % 10) + '0';
buffer += 3; buffer += 3;
} } else if (fmt == 'f') {
else if (fmt == 'f')
{
digits = fcvtbuf(value, precision, &decpt, &sign, cvtbuf); digits = fcvtbuf(value, precision, &decpt, &sign, cvtbuf);
if (sign) *buffer++ = '-';
if (*digits) if (sign) {
{ *buffer++ = '-';
if (decpt <= 0) }
{
if (*digits) {
if (decpt <= 0) {
*buffer++ = '0'; *buffer++ = '0';
*buffer++ = '.'; *buffer++ = '.';
for (pos = 0; pos < -decpt; pos++) *buffer++ = '0';
while (*digits) *buffer++ = *digits++; for (pos = 0; pos < -decpt; pos++) {
*buffer++ = '0';
} }
else
{ while (*digits) {
*buffer++ = *digits++;
}
} else {
pos = 0; pos = 0;
while (*digits)
{ while (*digits) {
if (pos++ == decpt) *buffer++ = '.'; if (pos++ == decpt) {
*buffer++ = '.';
}
*buffer++ = *digits++; *buffer++ = *digits++;
} }
} }
} } else {
else
{
*buffer++ = '0'; *buffer++ = '0';
if (precision > 0)
{ if (precision > 0) {
*buffer++ = '.'; *buffer++ = '.';
for (pos = 0; pos < precision; pos++) *buffer++ = '0';
for (pos = 0; pos < precision; pos++) {
*buffer++ = '0';
}
} }
} }
} }
@ -384,26 +429,28 @@ static void parse_float(double value, char *buffer, char fmt, int precision)
static void decimal_point(char *buffer) static void decimal_point(char *buffer)
{ {
while (*buffer) while (*buffer) {
{ if (*buffer == '.') {
if (*buffer == '.') return; return;
if (*buffer == 'e' || *buffer == 'E') break; }
if (*buffer == 'e' || *buffer == 'E') {
break;
}
buffer++; buffer++;
} }
if (*buffer) if (*buffer) {
{
int n = strnlen(buffer, 256); int n = strnlen(buffer, 256);
while (n > 0)
{ while (n > 0) {
buffer[n + 1] = buffer[n]; buffer[n + 1] = buffer[n];
n--; n--;
} }
*buffer = '.'; *buffer = '.';
} } else {
else
{
*buffer++ = '.'; *buffer++ = '.';
*buffer = '\0'; *buffer = '\0';
} }
@ -413,17 +460,30 @@ static void cropzeros(char *buffer)
{ {
char *stop; char *stop;
while (*buffer && *buffer != '.') buffer++; while (*buffer && *buffer != '.') {
if (*buffer++) buffer++;
{ }
while (*buffer && *buffer != 'e' && *buffer != 'E') buffer++;
if (*buffer++) {
while (*buffer && *buffer != 'e' && *buffer != 'E') {
buffer++;
}
stop = buffer--; stop = buffer--;
while (*buffer == '0') buffer--;
if (*buffer == '.') buffer--; while (*buffer == '0') {
while (buffer!=stop) buffer--;
}
if (*buffer == '.') {
buffer--;
}
while (buffer != stop) {
*++buffer = 0; *++buffer = 0;
} }
} }
}
static char *flt(char *str, double num, int size, int precision, char fmt, int flags) static char *flt(char *str, double num, int size, int precision, char fmt, int flags)
{ {
@ -432,50 +492,70 @@ static char *flt(char *str, double num, int size, int precision, char fmt, int f
int n, i; int n, i;
// Left align means no zero padding // Left align means no zero padding
if (flags & LEFT) flags &= ~ZEROPAD; if (flags & LEFT) {
flags &= ~ZEROPAD;
}
// Determine padding and sign char // Determine padding and sign char
c = (flags & ZEROPAD) ? '0' : ' '; c = (flags & ZEROPAD) ? '0' : ' ';
sign = 0; sign = 0;
if (flags & SIGN)
{ if (flags & SIGN) {
if (num < 0.0) if (num < 0.0) {
{
sign = '-'; sign = '-';
num = -num; num = -num;
size--; size--;
} } else if (flags & PLUS) {
else if (flags & PLUS)
{
sign = '+'; sign = '+';
size--; size--;
} } else if (flags & SPACE) {
else if (flags & SPACE)
{
sign = ' '; sign = ' ';
size--; size--;
} }
} }
// Compute the precision value // Compute the precision value
if (precision < 0) if (precision < 0) {
precision = 6; // Default precision: 6 precision = 6; // Default precision: 6
}
// Convert floating point number to text // Convert floating point number to text
parse_float(num, tmp, fmt, precision); parse_float(num, tmp, fmt, precision);
if ((flags & HEX_PREP) && precision == 0) decimal_point(tmp); if ((flags & HEX_PREP) && precision == 0) {
if (fmt == 'g' && !(flags & HEX_PREP)) cropzeros(tmp); decimal_point(tmp);
}
if (fmt == 'g' && !(flags & HEX_PREP)) {
cropzeros(tmp);
}
n = strnlen(tmp, 256); n = strnlen(tmp, 256);
// Output number with alignment and padding // Output number with alignment and padding
size -= n; size -= n;
if (!(flags & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' ';
if (sign) *str++ = sign; if (!(flags & (ZEROPAD | LEFT)))
if (!(flags & LEFT)) while (size-- > 0) *str++ = c; while (size-- > 0) {
for (i = 0; i < n; i++) *str++ = tmp[i]; *str++ = ' ';
while (size-- > 0) *str++ = ' '; }
if (sign) {
*str++ = sign;
}
if (!(flags & LEFT))
while (size-- > 0) {
*str++ = c;
}
for (i = 0; i < n; i++) {
*str++ = tmp[i];
}
while (size-- > 0) {
*str++ = ' ';
}
return str; return str;
} }
@ -517,6 +597,7 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
} else { } else {
EMIT(ch); EMIT(ch);
} }
break; break;
case st_flags: case st_flags:
@ -524,26 +605,33 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
case '-': case '-':
flags |= FL_MINUS; flags |= FL_MINUS;
break; break;
case '+': case '+':
flags |= FL_PLUS; flags |= FL_PLUS;
break; break;
case '\'': case '\'':
flags |= FL_TICK; flags |= FL_TICK;
break; break;
case ' ': case ' ':
flags |= FL_SPACE; flags |= FL_SPACE;
break; break;
case '#': case '#':
flags |= FL_HASH; flags |= FL_HASH;
break; break;
case '0': case '0':
flags |= FL_ZERO; flags |= FL_ZERO;
break; break;
default: default:
state = st_width; state = st_width;
p--; /* Process this character again */ p--; /* Process this character again */
break; break;
} }
break; break;
case st_width: case st_width:
@ -551,6 +639,7 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
width = width * 10 + (ch - '0'); width = width * 10 + (ch - '0');
} else if (ch == '*') { } else if (ch == '*') {
width = va_arg(ap, int); width = va_arg(ap, int);
if (width < 0) { if (width < 0) {
width = -width; width = -width;
flags |= FL_MINUS; flags |= FL_MINUS;
@ -562,6 +651,7 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
state = st_modifiers; state = st_modifiers;
p--; /* Process this character again */ p--; /* Process this character again */
} }
break; break;
case st_prec: case st_prec:
@ -569,12 +659,15 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
prec = prec * 10 + (ch - '0'); prec = prec * 10 + (ch - '0');
} else if (ch == '*') { } else if (ch == '*') {
prec = va_arg(ap, int); prec = va_arg(ap, int);
if (prec < 0)
if (prec < 0) {
prec = -1; prec = -1;
}
} else { } else {
state = st_modifiers; state = st_modifiers;
p--; /* Process this character again */ p--; /* Process this character again */
} }
break; break;
case st_modifiers: case st_modifiers:
@ -583,22 +676,28 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
case 'h': case 'h':
rank--; /* Shorter rank */ rank--; /* Shorter rank */
break; break;
case 'l': case 'l':
rank++; /* Longer rank */ rank++; /* Longer rank */
break; break;
case 'j': case 'j':
rank = INTMAX_RANK; rank = INTMAX_RANK;
break; break;
case 'z': case 'z':
rank = SIZE_T_RANK; rank = SIZE_T_RANK;
break; break;
case 't': case 't':
rank = PTRDIFF_T_RANK; rank = PTRDIFF_T_RANK;
break; break;
case 'L': case 'L':
case 'q': case 'q':
rank += 2; rank += 2;
break; break;
default: default:
/* Output modifiers - terminal sequences */ /* Output modifiers - terminal sequences */
@ -606,15 +705,17 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
state = st_normal; state = st_normal;
/* Canonicalize rank */ /* Canonicalize rank */
if (rank < MIN_RANK) if (rank < MIN_RANK) {
rank = MIN_RANK; rank = MIN_RANK;
else if (rank > MAX_RANK) } else if (rank > MAX_RANK) {
rank = MAX_RANK; rank = MAX_RANK;
}
switch (ch) { switch (ch) {
case 'P': /* Upper case pointer */ case 'P': /* Upper case pointer */
flags |= FL_UPPER; flags |= FL_UPPER;
__attribute__((fallthrough)); __attribute__((fallthrough));
/* fall through */ /* fall through */
case 'p': /* Pointer */ case 'p': /* Pointer */
base = 16; base = 16;
@ -628,82 +729,91 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
case 'i': case 'i':
base = 10; base = 10;
flags |= FL_SIGNED; flags |= FL_SIGNED;
switch (rank) { switch (rank) {
case rank_char: case rank_char:
/* Yes, all these casts are /* Yes, all these casts are
needed... */ needed... */
val = (uintmax_t)(intmax_t) val = (uintmax_t)(intmax_t)(signed char)
(signed char)
va_arg(ap, signed int); va_arg(ap, signed int);
break; break;
case rank_short: case rank_short:
val = (uintmax_t)(intmax_t) val = (uintmax_t)(intmax_t)(signed short)
(signed short)
va_arg(ap, signed int); va_arg(ap, signed int);
break; break;
case rank_int: case rank_int:
val = (uintmax_t)(intmax_t) val = (uintmax_t)(intmax_t)
va_arg(ap, signed int); va_arg(ap, signed int);
break; break;
case rank_long: case rank_long:
val = (uintmax_t)(intmax_t) val = (uintmax_t)(intmax_t)
va_arg(ap, signed long); va_arg(ap, signed long);
break; break;
case rank_longlong: case rank_longlong:
val = (uintmax_t)(intmax_t) val = (uintmax_t)(intmax_t)
va_arg(ap, va_arg(ap,
signed long long); signed long long);
break; break;
default: default:
break; break;
} }
goto is_integer; goto is_integer;
case 'o': /* Octal */ case 'o': /* Octal */
base = 8; base = 8;
goto is_unsigned; goto is_unsigned;
case 'u': /* Unsigned decimal */ case 'u': /* Unsigned decimal */
base = 10; base = 10;
goto is_unsigned; goto is_unsigned;
case 'X': /* Upper case hexadecimal */ case 'X': /* Upper case hexadecimal */
flags |= FL_UPPER; flags |= FL_UPPER;
__attribute__((fallthrough)); __attribute__((fallthrough));
/* fall through */ /* fall through */
case 'x': /* Hexadecimal */ case 'x': /* Hexadecimal */
base = 16; base = 16;
goto is_unsigned; goto is_unsigned;
is_unsigned: is_unsigned:
switch (rank) { switch (rank) {
case rank_char: case rank_char:
val = (uintmax_t) val = (uintmax_t)(unsigned char)
(unsigned char) va_arg(ap, unsigned int);
va_arg(ap, unsigned
int);
break; break;
case rank_short: case rank_short:
val = (uintmax_t) val = (uintmax_t)(unsigned short)
(unsigned short) va_arg(ap, unsigned int);
va_arg(ap, unsigned
int);
break; break;
case rank_int: case rank_int:
val = (uintmax_t) val = (uintmax_t)
va_arg(ap, unsigned va_arg(ap, unsigned int);
int);
break; break;
case rank_long: case rank_long:
val = (uintmax_t) val = (uintmax_t)
va_arg(ap, unsigned va_arg(ap, unsigned long);
long);
break; break;
case rank_longlong: case rank_longlong:
val = (uintmax_t) val = (uintmax_t)
va_arg(ap, unsigned va_arg(ap, unsigned long long);
long long);
break; break;
default: default:
break; break;
} }
/* fall through */ /* fall through */
is_integer: is_integer:
@ -719,86 +829,87 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
sarg = &carg; sarg = &carg;
slen = 1; slen = 1;
goto is_string; goto is_string;
case 's': /* String */ case 's': /* String */
sarg = va_arg(ap, const char *); sarg = va_arg(ap, const char *);
sarg = sarg ? sarg : "(null)"; sarg = sarg ? sarg : "(null)";
slen = strlen(sarg); slen = strlen(sarg);
goto is_string; goto is_string;
is_string: is_string : {
{
char sch; char sch;
int i; int i;
if (prec != -1 && slen > prec) if (prec != -1 && slen > prec) {
slen = prec; slen = prec;
}
if (width > slen if (width > slen && !(flags & FL_MINUS)) {
&& !(flags & FL_MINUS)) {
char pad = char pad =
(flags & FL_ZERO) ? (flags & FL_ZERO) ?
'0' : ' '; '0' :
' ';
while (width > slen) { while (width > slen) {
EMIT(pad); EMIT(pad);
width--; width--;
} }
} }
for (i = slen; i; i--) { for (i = slen; i; i--) {
sch = *sarg++; sch = *sarg++;
EMIT(sch); EMIT(sch);
} }
if (width > slen
&& (flags & FL_MINUS)) { if (width > slen && (flags & FL_MINUS)) {
while (width > slen) { while (width > slen) {
EMIT(' '); EMIT(' ');
width--; width--;
} }
} }
} } break;
break;
case 'n': case 'n': {
{
/* Output the number of /* Output the number of
characters written */ characters written */
switch (rank) { switch (rank) {
case rank_char: case rank_char:
*va_arg(ap, *va_arg(ap,
signed char *) signed char *) = o;
= o;
break; break;
case rank_short: case rank_short:
*va_arg(ap, *va_arg(ap,
signed short *) signed short *) = o;
= o;
break; break;
case rank_int: case rank_int:
*va_arg(ap, *va_arg(ap,
signed int *) signed int *) = o;
= o;
break; break;
case rank_long: case rank_long:
*va_arg(ap, *va_arg(ap,
signed long *) signed long *) = o;
= o;
break; break;
case rank_longlong: case rank_longlong:
*va_arg(ap, *va_arg(ap,
signed long long *) signed long long *) = o;
= o;
break; break;
default: default:
break; break;
} }
} } break;
break;
#ifdef BFLB_PRINT_FLOAT_SUPPORT #ifdef BFLB_PRINT_FLOAT_SUPPORT
case 'f':
{ case 'f': {
q = flt(q, va_arg(ap, double), width, prec, ch, SIGN); q = flt(q, va_arg(ap, double), width, prec, ch, SIGN);
continue; continue;
} }
#endif #endif
default: /* Anything else, including % */ default: /* Anything else, including % */
@ -806,16 +917,18 @@ int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
break; break;
} }
} }
default: default:
break; break;
} }
} }
/* Null-terminate the string */ /* Null-terminate the string */
if (o < n) if (o < n) {
*q = '\0'; /* No overflow */ *q = '\0'; /* No overflow */
else if (n > 0) } else if (n > 0) {
buffer[n - 1] = '\0'; /* Overflow - terminate at end of buffer */ buffer[n - 1] = '\0'; /* Overflow - terminate at end of buffer */
}
return o; return o;
} }

View file

@ -54,8 +54,10 @@ enum bail {
static __inline const char *skipspace(const char *p) static __inline const char *skipspace(const char *p)
{ {
while (isspace((unsigned char)*p)) while (isspace((unsigned char)*p)) {
p++; p++;
}
return p; return p;
} }
@ -111,11 +113,13 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
} else if (isspace((unsigned char)ch)) { } else if (isspace((unsigned char)ch)) {
q = skipspace(q); q = skipspace(q);
} else { } else {
if (*q == ch) if (*q == ch) {
q++; q++;
else } else {
bail = bail_err; /* Match failure */ bail = bail_err; /* Match failure */
} }
}
break; break;
case st_flags: case st_flags:
@ -123,6 +127,7 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
case '*': case '*':
flags |= FL_SPLAT; flags |= FL_SPLAT;
break; break;
case '0': case '0':
case '1': case '1':
case '2': case '2':
@ -137,11 +142,13 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
state = st_width; state = st_width;
flags |= FL_WIDTH; flags |= FL_WIDTH;
break; break;
default: default:
state = st_modifiers; state = st_modifiers;
p--; /* Process this character again */ p--; /* Process this character again */
break; break;
} }
break; break;
case st_width: case st_width:
@ -151,6 +158,7 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
state = st_modifiers; state = st_modifiers;
p--; /* Process this character again */ p--; /* Process this character again */
} }
break; break;
case st_modifiers: case st_modifiers:
@ -159,18 +167,23 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
case 'h': case 'h':
rank--; /* Shorter rank */ rank--; /* Shorter rank */
break; break;
case 'l': case 'l':
rank++; /* Longer rank */ rank++; /* Longer rank */
break; break;
case 'j': case 'j':
rank = INTMAX_RANK; rank = INTMAX_RANK;
break; break;
case 'z': case 'z':
rank = SIZE_T_RANK; rank = SIZE_T_RANK;
break; break;
case 't': case 't':
rank = PTRDIFF_T_RANK; rank = PTRDIFF_T_RANK;
break; break;
case 'L': case 'L':
case 'q': case 'q':
rank = rank_longlong; /* long double/long long */ rank = rank_longlong; /* long double/long long */
@ -182,10 +195,11 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
state = st_normal; state = st_normal;
/* Canonicalize rank */ /* Canonicalize rank */
if (rank < MIN_RANK) if (rank < MIN_RANK) {
rank = MIN_RANK; rank = MIN_RANK;
else if (rank > MAX_RANK) } else if (rank > MAX_RANK) {
rank = MAX_RANK; rank = MAX_RANK;
}
switch (ch) { switch (ch) {
case 'P': /* Upper case pointer */ case 'P': /* Upper case pointer */
@ -227,64 +241,75 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
scan_int: scan_int:
q = skipspace(q); q = skipspace(q);
if (!*q) { if (!*q) {
bail = bail_eof; bail = bail_eof;
break; break;
} }
val = val =
strntoumax(q, (char **)&qq, base, strntoumax(q, (char **)&qq, base,
width); width);
if (qq == q) { if (qq == q) {
bail = bail_err; bail = bail_err;
break; break;
} }
q = qq; q = qq;
if (!(flags & FL_SPLAT))
if (!(flags & FL_SPLAT)) {
converted++; converted++;
}
/* fall through */ /* fall through */
set_integer: set_integer:
if (!(flags & FL_SPLAT)) { if (!(flags & FL_SPLAT)) {
switch (rank) { switch (rank) {
case rank_char: case rank_char:
*va_arg(ap, *va_arg(ap,
unsigned char *) unsigned char *) = val;
= val;
break; break;
case rank_short: case rank_short:
*va_arg(ap, *va_arg(ap,
unsigned short unsigned short
*) = val; *) = val;
break; break;
case rank_int: case rank_int:
*va_arg(ap, *va_arg(ap,
unsigned int *) unsigned int *) = val;
= val;
break; break;
case rank_long: case rank_long:
*va_arg(ap, *va_arg(ap,
unsigned long *) unsigned long *) = val;
= val;
break; break;
case rank_longlong: case rank_longlong:
*va_arg(ap, *va_arg(ap,
unsigned long unsigned long long *) = val;
long *) = val;
break; break;
case rank_ptr: case rank_ptr:
*va_arg(ap, void **) = *va_arg(ap, void **) =
(void *) (void *)(uintptr_t)val;
(uintptr_t)val;
break; break;
default: default:
break; break;
} }
} }
break; break;
case 'c': /* Character */ case 'c': /* Character */
/* Default width == 1 */ /* Default width == 1 */
width = (flags & FL_WIDTH) ? width : 1; width = (flags & FL_WIDTH) ? width : 1;
if (flags & FL_SPLAT) { if (flags & FL_SPLAT) {
while (width--) { while (width--) {
if (!*q) { if (!*q) {
@ -294,20 +319,26 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
} }
} else { } else {
sarg = va_arg(ap, char *); sarg = va_arg(ap, char *);
while (width--) { while (width--) {
if (!*q) { if (!*q) {
bail = bail_eof; bail = bail_eof;
break; break;
} }
*sarg++ = *q++; *sarg++ = *q++;
} }
if (!bail)
if (!bail) {
converted++; converted++;
} }
}
break; break;
case 's': /* String */ case 's': /* String */
uc = 1; /* Anything nonzero */ uc = 1; /* Anything nonzero */
if (flags & FL_SPLAT) { if (flags & FL_SPLAT) {
while (width-- && (uc = *q) && while (width-- && (uc = *q) &&
!isspace(uc)) { !isspace(uc)) {
@ -316,34 +347,40 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
} else { } else {
char *sp; char *sp;
sp = sarg = va_arg(ap, char *); sp = sarg = va_arg(ap, char *);
while (width-- && (uc = *q) && while (width-- && (uc = *q) &&
!isspace(uc)) { !isspace(uc)) {
*sp++ = uc; *sp++ = uc;
q++; q++;
} }
if (sarg != sp) { if (sarg != sp) {
/* Terminate output */ /* Terminate output */
*sp = '\0'; *sp = '\0';
converted++; converted++;
} }
} }
if (!uc)
if (!uc) {
bail = bail_eof; bail = bail_eof;
}
break; break;
case '[': /* Character range */ case '[': /* Character range */
sarg = (flags & FL_SPLAT) ? NULL sarg = (flags & FL_SPLAT) ? NULL : va_arg(ap, char *);
: va_arg(ap, char *);
state = st_match_init; state = st_match_init;
matchinv = 0; matchinv = 0;
memset(matchmap, 0, sizeof matchmap); memset(matchmap, 0, sizeof matchmap);
break; break;
case '%': /* %% sequence */ case '%': /* %% sequence */
if (*q == '%') if (*q == '%') {
q++; q++;
else } else {
bail = bail_err; bail = bail_err;
}
break; break;
default: /* Anything else */ default: /* Anything else */
@ -352,6 +389,7 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
break; break;
} }
} }
break; break;
case st_match_init: /* Initial state for %[ match */ case st_match_init: /* Initial state for %[ match */
@ -361,6 +399,7 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
set_bit(matchmap, (unsigned char)ch); set_bit(matchmap, (unsigned char)ch);
state = st_match; state = st_match;
} }
break; break;
case st_match: /* Main state for %[ match */ case st_match: /* Main state for %[ match */
@ -372,6 +411,7 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
} else { } else {
set_bit(matchmap, (unsigned char)ch); set_bit(matchmap, (unsigned char)ch);
} }
break; break;
case st_match_range: /* %[ match after - */ case st_match_range: /* %[ match after - */
@ -381,38 +421,50 @@ int vsscanf(const char *buffer, const char *format, va_list ap)
goto match_run; goto match_run;
} else { } else {
int i; int i;
for (i = range_start; i < (unsigned char)ch; for (i = range_start; i < (unsigned char)ch;
i++) i++) {
set_bit(matchmap, i); set_bit(matchmap, i);
}
state = st_match; state = st_match;
} }
break; break;
match_run: /* Match expression finished */ match_run: /* Match expression finished */
qq = q; qq = q;
uc = 1; /* Anything nonzero */ uc = 1; /* Anything nonzero */
while (width && (uc = *q)
&& test_bit(matchmap, uc)^matchinv) { while (width && (uc = *q) && test_bit(matchmap, uc) ^ matchinv) {
if (sarg) if (sarg) {
*sarg++ = uc; *sarg++ = uc;
}
q++; q++;
} }
if (q != qq && sarg) { if (q != qq && sarg) {
*sarg = '\0'; *sarg = '\0';
converted++; converted++;
} else { } else {
bail = bail_err; bail = bail_err;
} }
if (!uc)
if (!uc) {
bail = bail_eof; bail = bail_eof;
}
break; break;
default: default:
break; break;
} }
} }
if (bail == bail_eof && !converted) if (bail == bail_eof && !converted) {
converted = -1; /* Return EOF (-1) */ converted = -1; /* Return EOF (-1) */
}
return converted; return converted;
} }

View file

@ -36,18 +36,15 @@ extern "C" {
#define container_of(ptr, type, member) \ #define container_of(ptr, type, member) \
((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member))) ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
/** /**
* Double List structure * Double List structure
*/ */
struct dlist_node struct dlist_node {
{
struct dlist_node *next; /**< point to next node. */ struct dlist_node *next; /**< point to next node. */
struct dlist_node *prev; /**< point to prev node. */ struct dlist_node *prev; /**< point to prev node. */
}; };
typedef struct dlist_node dlist_t; /**< Type for lists. */ typedef struct dlist_node dlist_t; /**< Type for lists. */
/** /**
* @brief initialize a list * @brief initialize a list
* *
@ -137,8 +134,8 @@ static inline unsigned int dlist_len(const dlist_t *l)
{ {
unsigned int len = 0; unsigned int len = 0;
const dlist_t *p = l; const dlist_t *p = l;
while (p->next != l)
{ while (p->next != l) {
p = p->next; p = p->next;
len++; len++;
} }
@ -149,7 +146,10 @@ static inline unsigned int dlist_len(const dlist_t *l)
/** /**
* @brief initialize a dlist object * @brief initialize a dlist object
*/ */
#define DLIST_OBJECT_INIT(object) { &(object), &(object) } #define DLIST_OBJECT_INIT(object) \
{ \
&(object), &(object) \
}
/** /**
* @brief initialize a dlist object * @brief initialize a dlist object
*/ */
@ -266,13 +266,11 @@ static inline unsigned int dlist_len(const dlist_t *l)
/** /**
* Single List structure * Single List structure
*/ */
struct slist_node struct slist_node {
{
struct slist_node *next; /**< point to next node. */ struct slist_node *next; /**< point to next node. */
}; };
typedef struct slist_node slist_t; /**< Type for single list. */ typedef struct slist_node slist_t; /**< Type for single list. */
/** /**
* @brief initialize a single list * @brief initialize a single list
* *
@ -291,7 +289,9 @@ static inline void slist_add_head(slist_t *l, slist_t *n)
static inline void slist_add_tail(slist_t *l, slist_t *n) static inline void slist_add_tail(slist_t *l, slist_t *n)
{ {
while (l->next) l = l->next; while (l->next) {
l = l->next;
}
/* append the node to the tail */ /* append the node to the tail */
l->next = n; l->next = n;
@ -300,16 +300,13 @@ static inline void slist_add_tail(slist_t *l, slist_t *n)
static inline void slist_insert(slist_t *l, slist_t *next, slist_t *n) static inline void slist_insert(slist_t *l, slist_t *next, slist_t *n)
{ {
if (!next) if (!next) {
{
slist_add_tail(next, l); slist_add_tail(next, l);
return; return;
} }
while (l->next) while (l->next) {
{ if (l->next == next) {
if (l->next == next)
{
l->next = n; l->next = n;
n->next = next; n->next = next;
} }
@ -321,10 +318,14 @@ static inline void slist_insert(slist_t *l, slist_t *next, slist_t *n)
static inline slist_t *slist_remove(slist_t *l, slist_t *n) static inline slist_t *slist_remove(slist_t *l, slist_t *n)
{ {
/* remove slist head */ /* remove slist head */
while (l->next && l->next != n) l = l->next; while (l->next && l->next != n) {
l = l->next;
}
/* remove node */ /* remove node */
if (l->next != (slist_t *)0) l->next = l->next->next; if (l->next != (slist_t *)0) {
l->next = l->next->next;
}
return l; return l;
} }
@ -333,8 +334,8 @@ static inline unsigned int slist_len(const slist_t *l)
{ {
unsigned int len = 0; unsigned int len = 0;
const slist_t *list = l->next; const slist_t *list = l->next;
while (list != NULL)
{ while (list != NULL) {
list = list->next; list = list->next;
len++; len++;
} }
@ -344,10 +345,8 @@ static inline unsigned int slist_len(const slist_t *l)
static inline unsigned int slist_contains(slist_t *l, slist_t *n) static inline unsigned int slist_contains(slist_t *l, slist_t *n)
{ {
while (l->next) while (l->next) {
{ if (l->next == n) {
if (l->next == n)
{
return 0; return 0;
} }
@ -364,7 +363,9 @@ static inline slist_t *slist_head(slist_t *l)
static inline slist_t *slist_tail(slist_t *l) static inline slist_t *slist_tail(slist_t *l)
{ {
while (l->next) l = l->next; while (l->next) {
l = l->next;
}
return l; return l;
} }
@ -382,7 +383,10 @@ static inline int slist_isempty(slist_t *l)
/** /**
* @brief initialize a slist object * @brief initialize a slist object
*/ */
#define SLIST_OBJECT_INIT(object) { NULL } #define SLIST_OBJECT_INIT(object) \
{ \
NULL \
}
/** /**
* @brief initialize a slist object * @brief initialize a slist object

View file

@ -27,29 +27,36 @@ mmheap_ctl_t mmheap_ctl;
static int generic_fls(uint32_t x) static int generic_fls(uint32_t x)
{ {
int r = 32; int r = 32;
if (!x)
if (!x) {
return 0; return 0;
}
if (!(x & 0xffff0000u)) { if (!(x & 0xffff0000u)) {
x <<= 16; x <<= 16;
r -= 16; r -= 16;
} }
if (!(x & 0xff000000u)) { if (!(x & 0xff000000u)) {
x <<= 8; x <<= 8;
r -= 8; r -= 8;
} }
if (!(x & 0xf0000000u)) { if (!(x & 0xf0000000u)) {
x <<= 4; x <<= 4;
r -= 4; r -= 4;
} }
if (!(x & 0xc0000000u)) { if (!(x & 0xc0000000u)) {
x <<= 2; x <<= 2;
r -= 2; r -= 2;
} }
if (!(x & 0x80000000u)) { if (!(x & 0x80000000u)) {
x <<= 1; x <<= 1;
r -= 1; r -= 1;
} }
return r; return r;
} }
@ -196,6 +203,7 @@ static size_t adjust_request_size(size_t size, size_t align)
} }
adjust_size = align_up(size, align); adjust_size = align_up(size, align);
if (adjust_size > MMHEAP_BLK_SIZE_MAX) { if (adjust_size > MMHEAP_BLK_SIZE_MAX) {
return 0; return 0;
} }
@ -221,6 +229,7 @@ static void mapping_insert(size_t size, int *fli, int *sli)
sl = ((int)size >> (fl - MMHEAP_SL_INDEX_COUNT_LOG2)) ^ (1 << MMHEAP_SL_INDEX_COUNT_LOG2); sl = ((int)size >> (fl - MMHEAP_SL_INDEX_COUNT_LOG2)) ^ (1 << MMHEAP_SL_INDEX_COUNT_LOG2);
fl -= (MMHEAP_FL_INDEX_SHIFT - 1); fl -= (MMHEAP_FL_INDEX_SHIFT - 1);
} }
*fli = fl; *fli = fl;
*sli = sl; *sli = sl;
} }
@ -234,6 +243,7 @@ static void mapping_search(size_t size, int *fli, int *sli)
round = (1 << (__fls(size) - MMHEAP_SL_INDEX_COUNT_LOG2)) - 1; round = (1 << (__fls(size) - MMHEAP_SL_INDEX_COUNT_LOG2)) - 1;
size += round; size += round;
} }
mapping_insert(size, fli, sli); mapping_insert(size, fli, sli);
} }
@ -250,9 +260,11 @@ static mmheap_blk_t *blk_search_suitable(int *fli, int *sli)
** fl/sl index. ** fl/sl index.
*/ */
sl_map = mmheap_ctl.sl_bitmap[fl] & (~0U << sl); sl_map = mmheap_ctl.sl_bitmap[fl] & (~0U << sl);
if (!sl_map) { if (!sl_map) {
/* No block exists. Search in the next largest first-level list. */ /* No block exists. Search in the next largest first-level list. */
fl_map = mmheap_ctl.fl_bitmap & (~0U << (fl + 1)); fl_map = mmheap_ctl.fl_bitmap & (~0U << (fl + 1));
if (!fl_map) { if (!fl_map) {
/* No free blocks available, memory has been exhausted. */ /* No free blocks available, memory has been exhausted. */
return 0; return 0;
@ -262,6 +274,7 @@ static mmheap_blk_t *blk_search_suitable(int *fli, int *sli)
*fli = fl; *fli = fl;
sl_map = mmheap_ctl.sl_bitmap[fl]; sl_map = mmheap_ctl.sl_bitmap[fl];
} }
sl = __ffs(sl_map); sl = __ffs(sl_map);
*sli = sl; *sli = sl;
@ -384,6 +397,7 @@ static mmheap_blk_t *blk_merge_next(mmheap_blk_t *blk)
mmheap_blk_t *next_blk; mmheap_blk_t *next_blk;
next_blk = blk_next(blk); next_blk = blk_next(blk);
if (blk_is_free(next_blk)) { if (blk_is_free(next_blk)) {
blk_remove(next_blk); blk_remove(next_blk);
blk = blk_absorb(blk, next_blk); blk = blk_absorb(blk, next_blk);
@ -425,6 +439,7 @@ static mmheap_blk_t *blk_trim_free_leading(mmheap_blk_t *blk, size_t size)
mmheap_blk_t *remaining_blk; mmheap_blk_t *remaining_blk;
remaining_blk = blk; remaining_blk = blk;
if (blk_can_split(blk, size)) { if (blk_can_split(blk, size)) {
/* We want the 2nd block. */ /* We want the 2nd block. */
remaining_blk = blk_split(blk, size - MMHEAP_BLK_HEADER_OVERHEAD); remaining_blk = blk_split(blk, size - MMHEAP_BLK_HEADER_OVERHEAD);
@ -470,6 +485,7 @@ static void *blk_prepare_used(mmheap_blk_t *blk, size_t size)
if (!blk) { if (!blk) {
return NULL; return NULL;
} }
blk_trim_free(blk, size); blk_trim_free(blk, size);
blk_mark_as_used(blk); blk_mark_as_used(blk);
return blk_to_ptr(blk); return blk_to_ptr(blk);
@ -480,6 +496,7 @@ static void control_construct(void)
int i, j; int i, j;
mmheap_ctl.pool_cnt = 0u; mmheap_ctl.pool_cnt = 0u;
for (i = 0; i < MMHEAP_POOL_MAX; ++i) { for (i = 0; i < MMHEAP_POOL_MAX; ++i) {
mmheap_ctl.pool_start[i] = (void *)NULL; mmheap_ctl.pool_start[i] = (void *)NULL;
} }
@ -488,8 +505,10 @@ static void control_construct(void)
mmheap_ctl.block_null.prev_free = &mmheap_ctl.block_null; mmheap_ctl.block_null.prev_free = &mmheap_ctl.block_null;
mmheap_ctl.fl_bitmap = 0; mmheap_ctl.fl_bitmap = 0;
for (i = 0; i < MMHEAP_FL_INDEX_COUNT; ++i) { for (i = 0; i < MMHEAP_FL_INDEX_COUNT; ++i) {
mmheap_ctl.sl_bitmap[i] = 0; mmheap_ctl.sl_bitmap[i] = 0;
for (j = 0; j < MMHEAP_SL_INDEX_COUNT; ++j) { for (j = 0; j < MMHEAP_SL_INDEX_COUNT; ++j) {
mmheap_ctl.blocks[i][j] = &mmheap_ctl.block_null; mmheap_ctl.blocks[i][j] = &mmheap_ctl.block_null;
} }
@ -510,6 +529,7 @@ static int mmheap_pool_is_exist(void *pool_start)
return 1; return 1;
} }
} }
return 0; return 0;
} }
@ -527,13 +547,14 @@ static void mmheap_pool_unrecord(void *pool_start)
break; break;
} }
} }
if (i != mmheap_ctl.pool_cnt - 1) { if (i != mmheap_ctl.pool_cnt - 1) {
mmheap_ctl.pool_start[i] = mmheap_ctl.pool_start[mmheap_ctl.pool_cnt - 1]; mmheap_ctl.pool_start[i] = mmheap_ctl.pool_start[mmheap_ctl.pool_cnt - 1];
} }
--mmheap_ctl.pool_cnt; --mmheap_ctl.pool_cnt;
} }
int mmheap_init_with_pool(void *pool_start, size_t pool_size) int mmheap_init_with_pool(void *pool_start, size_t pool_size)
{ {
control_construct(); control_construct();
@ -548,6 +569,7 @@ void *mmheap_alloc(size_t size)
adjust_size = adjust_request_size(size, MMHEAP_ALIGN_SIZE); adjust_size = adjust_request_size(size, MMHEAP_ALIGN_SIZE);
blk = blk_locate_free(adjust_size); blk = blk_locate_free(adjust_size);
if (!blk) { if (!blk) {
return NULL; return NULL;
} }
@ -560,6 +582,7 @@ void *mmheap_calloc(size_t num, size_t size)
void *ptr; void *ptr;
ptr = mmheap_alloc(num * size); ptr = mmheap_alloc(num * size);
if (ptr) { if (ptr) {
memset(ptr, 0, num * size); memset(ptr, 0, num * size);
} }
@ -580,6 +603,7 @@ void *mmheap_aligned_alloc(size_t size, size_t align)
aligned_size = (adjust_size && align > MMHEAP_ALIGN_SIZE) ? size_with_gap : adjust_size; aligned_size = (adjust_size && align > MMHEAP_ALIGN_SIZE) ? size_with_gap : adjust_size;
blk = blk_locate_free(aligned_size); blk = blk_locate_free(aligned_size);
if (!blk) { if (!blk) {
return NULL; return NULL;
} }
@ -643,6 +667,7 @@ void *mmheap_realloc(void *ptr, size_t size)
if (adjust_size > curr_size && (!blk_is_free(next_blk) || adjust_size > combined_size)) { if (adjust_size > curr_size && (!blk_is_free(next_blk) || adjust_size > combined_size)) {
p = mmheap_alloc(size); p = mmheap_alloc(size);
if (p) { if (p) {
min_size = curr_size < size ? curr_size : size; min_size = curr_size < size ? curr_size : size;
memcpy(p, ptr, min_size); memcpy(p, ptr, min_size);
@ -739,13 +764,13 @@ int mmheap_pool_add(void *pool_start, size_t pool_size)
} else { } else {
info->used += blk_size(blk); info->used += blk_size(blk);
} }
blk = blk_next(blk); blk = blk_next(blk);
} }
return MEMHEAP_STATUS_OK; return MEMHEAP_STATUS_OK;
} }
int mmheap_check(mmheap_info_t *info) int mmheap_check(mmheap_info_t *info)
{ {
int i; int i;
@ -756,6 +781,7 @@ int mmheap_check(mmheap_info_t *info)
for (i = 0; i < mmheap_ctl.pool_cnt; ++i) { for (i = 0; i < mmheap_ctl.pool_cnt; ++i) {
err = mmheap_pool_check(mmheap_ctl.pool_start[i], &pool_info); err = mmheap_pool_check(mmheap_ctl.pool_start[i], &pool_info);
if (err != MEMHEAP_STATUS_OK) { if (err != MEMHEAP_STATUS_OK) {
return err; return err;
} }

View file

@ -189,8 +189,6 @@ void *mmheap_realloc(void *ptr, size_t size);
*/ */
void mmheap_free(void *ptr); void mmheap_free(void *ptr);
int mmheap_init_with_pool(void *pool_start, size_t pool_size); int mmheap_init_with_pool(void *pool_start, size_t pool_size);
#endif /* _DRV_MMHEAP_H_ */ #endif /* _DRV_MMHEAP_H_ */

View file

@ -39,10 +39,10 @@ void *ATTR_TCM_SECTION arch_memcpy(void *dst, const void *src, uint32_t n)
const uint8_t *p = src; const uint8_t *p = src;
uint8_t *q = dst; uint8_t *q = dst;
while (n--) while (n--) {
{
*q++ = *p++; *q++ = *p++;
} }
return dst; return dst;
} }
@ -62,10 +62,10 @@ uint32_t *ATTR_TCM_SECTION arch_memcpy4(uint32_t *dst, const uint32_t *src, uint
const uint32_t *p = src; const uint32_t *p = src;
uint32_t *q = dst; uint32_t *q = dst;
while (n--) while (n--) {
{
*q++ = *p++; *q++ = *p++;
} }
return dst; return dst;
} }
@ -86,21 +86,19 @@ void *ATTR_TCM_SECTION arch_memcpy_fast(void *pdst, const void *psrc, uint32_t n
uint8_t *dst = (uint8_t *)pdst; uint8_t *dst = (uint8_t *)pdst;
uint8_t *src = (uint8_t *)psrc; uint8_t *src = (uint8_t *)psrc;
if (((uint32_t)dst & 0x3) == 0 && ((uint32_t)src & 0x3) == 0) if (((uint32_t)dst & 0x3) == 0 && ((uint32_t)src & 0x3) == 0) {
{
arch_memcpy4((uint32_t *)dst, (const uint32_t *)src, n >> 2); arch_memcpy4((uint32_t *)dst, (const uint32_t *)src, n >> 2);
left = n % 4; left = n % 4;
done = n - left; done = n - left;
while (i < left)
{ while (i < left) {
dst[done + i] = src[done + i]; dst[done + i] = src[done + i];
i++; i++;
} }
} } else {
else
{
memcpy(dst, src, n); memcpy(dst, src, n);
} }
return dst; return dst;
} }
@ -119,11 +117,11 @@ void *ATTR_TCM_SECTION arch_memset(void *s, uint8_t c, uint32_t n)
{ {
uint8_t *p = (uint8_t *)s; uint8_t *p = (uint8_t *)s;
while (n > 0) while (n > 0) {
{
*p++ = (uint8_t)c; *p++ = (uint8_t)c;
--n; --n;
} }
return s; return s;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
@ -141,10 +139,10 @@ uint32_t *ATTR_TCM_SECTION arch_memset4(uint32_t *dst, const uint32_t val, uint3
{ {
uint32_t *q = dst; uint32_t *q = dst;
while (n--) while (n--) {
{
*q++ = val; *q++ = val;
} }
return dst; return dst;
} }
@ -164,12 +162,13 @@ int ATTR_TCM_SECTION arch_memcmp(const void *s1, const void *s2, uint32_t n)
const unsigned char *c1 = s1, *c2 = s2; const unsigned char *c1 = s1, *c2 = s2;
int d = 0; int d = 0;
while (n--) while (n--) {
{
d = (int)*c1++ - (int)*c2++; d = (int)*c1++ - (int)*c2++;
if (d)
if (d) {
break; break;
} }
}
return d; return d;
} }

View file

@ -45,7 +45,14 @@
#define BL_RD_BYTE(addr) (*((volatile uint8_t *)(uintptr_t)(addr))) #define BL_RD_BYTE(addr) (*((volatile uint8_t *)(uintptr_t)(addr)))
#define BL_WR_BYTE(addr, val) ((*(volatile uint8_t *)(uintptr_t)(addr)) = (val)) #define BL_WR_BYTE(addr, val) ((*(volatile uint8_t *)(uintptr_t)(addr)) = (val))
#define BL_RDWD_FRM_BYTEP(p) ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0])) #define BL_RDWD_FRM_BYTEP(p) ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0]))
#define BL_WRWD_TO_BYTEP(p,val) {p[0]=val&0xff;p[1]=(val>>8)&0xff;p[2]=(val>>16)&0xff;p[3]=(val>>24)&0xff;}
#define BL_WRWD_TO_BYTEP(p, val) \
{ \
p[0] = val & 0xff; \
p[1] = (val >> 8) & 0xff; \
p[2] = (val >> 16) & 0xff; \
p[3] = (val >> 24) & 0xff; \
}
/** /**
* @brief Register access macro * @brief Register access macro
*/ */
@ -58,7 +65,13 @@
#define BL_GET_REG_BITS_VAL(val, bitname) (((val)&bitname##_MSK) >> bitname##_POS) #define BL_GET_REG_BITS_VAL(val, bitname) (((val)&bitname##_MSK) >> bitname##_POS)
#define BL_SET_REG_BITS_VAL(val, bitname, bitval) (((val)&bitname##_UMSK) | ((uint32_t)(bitval) << bitname##_POS)) #define BL_SET_REG_BITS_VAL(val, bitname, bitval) (((val)&bitname##_UMSK) | ((uint32_t)(bitval) << bitname##_POS))
#define BL_IS_REG_BIT_SET(val, bitname) (((val) & (1U << (bitname##_POS))) != 0) #define BL_IS_REG_BIT_SET(val, bitname) (((val) & (1U << (bitname##_POS))) != 0)
#define BL_DRV_DUMMY {__NOP();__NOP();__NOP();__NOP();} #define BL_DRV_DUMMY \
{ \
__NOP(); \
__NOP(); \
__NOP(); \
__NOP(); \
}
/* Std driver attribute macro*/ /* Std driver attribute macro*/
#define ATTR_CLOCK_SECTION __attribute__((section(".sclock_rlt_code"))) #define ATTR_CLOCK_SECTION __attribute__((section(".sclock_rlt_code")))
@ -67,6 +80,7 @@
#define ATTR_TCM_CONST_SECTION __attribute__((section(".tcm_const"))) #define ATTR_TCM_CONST_SECTION __attribute__((section(".tcm_const")))
#define ATTR_DTCM_SECTION __attribute__((section(".tcm_data"))) #define ATTR_DTCM_SECTION __attribute__((section(".tcm_data")))
#define ATTR_HSRAM_SECTION __attribute__((section(".hsram_code"))) #define ATTR_HSRAM_SECTION __attribute__((section(".hsram_code")))
#define ATTR_DMA_RAM_SECTION __attribute__((section(".system_ram")))
#define ATTR_EALIGN(x) __attribute((aligned(x))) #define ATTR_EALIGN(x) __attribute((aligned(x)))
#define ATTR_FALLTHROUGH() __attribute__((fallthrough)) #define ATTR_FALLTHROUGH() __attribute__((fallthrough))
#define ATTR_USED __attribute__((__used__)) #define ATTR_USED __attribute__((__used__))
@ -86,8 +100,7 @@ __attribute__( ( always_inline ) ) __STATIC_INLINE__ void disable_irq(void)
/** /**
* @brief Error type definition * @brief Error type definition
*/ */
typedef enum typedef enum {
{
SUCCESS = 0, SUCCESS = 0,
ERROR = 1, ERROR = 1,
TIMEOUT = 2, TIMEOUT = 2,
@ -98,8 +111,7 @@ typedef enum
/** /**
* @brief Functional type definition * @brief Functional type definition
*/ */
typedef enum typedef enum {
{
DISABLE = 0, DISABLE = 0,
ENABLE = 1, ENABLE = 1,
} BL_Fun_Type; } BL_Fun_Type;
@ -107,8 +119,7 @@ typedef enum
/** /**
* @brief Status type definition * @brief Status type definition
*/ */
typedef enum typedef enum {
{
RESET = 0, RESET = 0,
SET = 1, SET = 1,
} BL_Sts_Type; } BL_Sts_Type;
@ -116,8 +127,7 @@ typedef enum
/** /**
* @brief Mask type definition * @brief Mask type definition
*/ */
typedef enum typedef enum {
{
UNMASK = 0, UNMASK = 0,
MASK = 1 MASK = 1
} BL_Mask_Type; } BL_Mask_Type;
@ -125,8 +135,7 @@ typedef enum
/** /**
* @brief Logical status Type definition * @brief Logical status Type definition
*/ */
typedef enum typedef enum {
{
LOGIC_LO = 0, LOGIC_LO = 0,
LOGIC_HI = !LOGIC_LO LOGIC_HI = !LOGIC_LO
} LogicalStatus; } LogicalStatus;
@ -134,8 +143,7 @@ typedef enum
/** /**
* @brief Active status Type definition * @brief Active status Type definition
*/ */
typedef enum typedef enum {
{
DEACTIVE = 0, DEACTIVE = 0,
ACTIVE = !DEACTIVE ACTIVE = !DEACTIVE
} ActiveStatus; } ActiveStatus;
@ -175,8 +183,6 @@ typedef void( *pFunc )( void );
#define ARCH_MemCpy_Fast arch_memcpy_fast #define ARCH_MemCpy_Fast arch_memcpy_fast
/** /**
* @brief Null Type definition * @brief Null Type definition
*/ */
@ -202,5 +208,3 @@ void fifocopy_to_mem(void* fifo_addr,uint8_t* data,uint32_t length);
/*@} end of group DRIVER_COMMON */ /*@} end of group DRIVER_COMMON */
#endif /* __BL602_COMMON_H__ */ #endif /* __BL602_COMMON_H__ */

View file

@ -105,22 +105,26 @@ static uint8_t pt_table_valid(pt_table_stuff_config *pt_stuff)
MSG("PT Entry Count Error\r\n"); MSG("PT Entry Count Error\r\n");
return 0; return 0;
} }
if (pt_table->crc32 != if (pt_table->crc32 !=
BFLB_Soft_CRC32((uint8_t *)pt_table, sizeof(pt_table_config) - 4)) { BFLB_Soft_CRC32((uint8_t *)pt_table, sizeof(pt_table_config) - 4)) {
MSG("PT CRC Error\r\n"); MSG("PT CRC Error\r\n");
return 0; return 0;
} }
p_crc32 = (uint32_t *)((uintptr_t)pt_entries + entriesLen); p_crc32 = (uint32_t *)((uintptr_t)pt_entries + entriesLen);
if (*p_crc32 != BFLB_Soft_CRC32((uint8_t *)pt_entries, entriesLen)) { if (*p_crc32 != BFLB_Soft_CRC32((uint8_t *)pt_entries, entriesLen)) {
MSG("PT Entry CRC Error\r\n"); MSG("PT Entry CRC Error\r\n");
return 0; return 0;
} }
return 1; return 1;
} }
return 0; return 0;
} }
/*@} end of group PARTITION_Private_Functions */ /*@} end of group PARTITION_Private_Functions */
/** @defgroup PARTITION_Public_Functions /** @defgroup PARTITION_Public_Functions
@ -160,6 +164,7 @@ pt_table_id_type pt_table_get_active_partition_need_lock(pt_table_stuff_config p
if (ptStuff == NULL) { if (ptStuff == NULL) {
return PT_TABLE_ID_INVALID; return PT_TABLE_ID_INVALID;
} }
activePtID = PT_TABLE_ID_INVALID; activePtID = PT_TABLE_ID_INVALID;
gp_pt_table_flash_read(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&ptStuff[0], sizeof(pt_table_stuff_config)); gp_pt_table_flash_read(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&ptStuff[0], sizeof(pt_table_stuff_config));
@ -179,6 +184,7 @@ pt_table_id_type pt_table_get_active_partition_need_lock(pt_table_stuff_config p
} else if (pt_valid[1] == 1) { } else if (pt_valid[1] == 1) {
activePtID = PT_TABLE_ID_1; activePtID = PT_TABLE_ID_1;
} }
return activePtID; return activePtID;
} }
@ -201,12 +207,14 @@ pt_table_error_type pt_table_get_active_entries_by_id(pt_table_stuff_config *pt_
if (pt_stuff == NULL || pt_entry == NULL) { if (pt_stuff == NULL || pt_entry == NULL) {
return PT_ERROR_PARAMETER; return PT_ERROR_PARAMETER;
} }
for (i = 0; i < pt_stuff->pt_table.entryCnt; i++) { for (i = 0; i < pt_stuff->pt_table.entryCnt; i++) {
if (pt_stuff->pt_entries[i].type == type) { if (pt_stuff->pt_entries[i].type == type) {
ARCH_MemCpy_Fast(pt_entry, &pt_stuff->pt_entries[i], sizeof(pt_table_entry_config)); ARCH_MemCpy_Fast(pt_entry, &pt_stuff->pt_entries[i], sizeof(pt_table_entry_config));
return PT_ERROR_SUCCESS; return PT_ERROR_SUCCESS;
} }
} }
return PT_ERROR_ENTRY_NOT_FOUND; return PT_ERROR_ENTRY_NOT_FOUND;
} }
@ -230,6 +238,7 @@ pt_table_error_type pt_table_get_active_entries_by_name(pt_table_stuff_config *p
if (pt_stuff == NULL || pt_entry == NULL) { if (pt_stuff == NULL || pt_entry == NULL) {
return PT_ERROR_PARAMETER; return PT_ERROR_PARAMETER;
} }
for (i = 0; i < pt_stuff->pt_table.entryCnt; i++) { for (i = 0; i < pt_stuff->pt_table.entryCnt; i++) {
if (strlen((char *)pt_stuff->pt_entries[i].name) == len && if (strlen((char *)pt_stuff->pt_entries[i].name) == len &&
memcmp((char *)pt_stuff->pt_entries[i].name, (char *)name, len) == 0) { memcmp((char *)pt_stuff->pt_entries[i].name, (char *)name, len) == 0) {
@ -237,6 +246,7 @@ pt_table_error_type pt_table_get_active_entries_by_name(pt_table_stuff_config *p
return PT_ERROR_SUCCESS; return PT_ERROR_SUCCESS;
} }
} }
return PT_ERROR_ENTRY_NOT_FOUND; return PT_ERROR_ENTRY_NOT_FOUND;
} }
@ -278,12 +288,14 @@ pt_table_error_type pt_table_update_entry(pt_table_id_type target_table_id,
} else { } else {
write_addr = BFLB_PT_TABLE1_ADDRESS; write_addr = BFLB_PT_TABLE1_ADDRESS;
} }
for (i = 0; i < pt_table->entryCnt; i++) { for (i = 0; i < pt_table->entryCnt; i++) {
if (pt_entries[i].type == pt_entry->type) { if (pt_entries[i].type == pt_entry->type) {
ARCH_MemCpy_Fast(&pt_entries[i], pt_entry, sizeof(pt_table_entry_config)); ARCH_MemCpy_Fast(&pt_entries[i], pt_entry, sizeof(pt_table_entry_config));
break; break;
} }
} }
if (i == pt_table->entryCnt) { if (i == pt_table->entryCnt) {
/* Not found this entry ,add new one */ /* Not found this entry ,add new one */
if (pt_table->entryCnt < PT_ENTRY_MAX) { if (pt_table->entryCnt < PT_ENTRY_MAX) {
@ -307,12 +319,15 @@ pt_table_error_type pt_table_update_entry(pt_table_id_type target_table_id,
/* Write back to flash */ /* Write back to flash */
/* Erase flash first */ /* Erase flash first */
ret = gp_pt_table_flash_erase(write_addr, write_addr + sizeof(pt_table_config) + entries_len + 4 - 1); ret = gp_pt_table_flash_erase(write_addr, write_addr + sizeof(pt_table_config) + entries_len + 4 - 1);
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Erase error\r\n"); MSG_ERR("Flash Erase error\r\n");
return PT_ERROR_FALSH_WRITE; return PT_ERROR_FALSH_WRITE;
} }
/* Write flash */ /* Write flash */
ret = gp_pt_table_flash_write(write_addr, (uint8_t *)pt_stuff, sizeof(pt_table_stuff_config)); ret = gp_pt_table_flash_write(write_addr, (uint8_t *)pt_stuff, sizeof(pt_table_stuff_config));
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Write error\r\n"); MSG_ERR("Flash Write error\r\n");
return PT_ERROR_FALSH_WRITE; return PT_ERROR_FALSH_WRITE;
@ -353,19 +368,22 @@ pt_table_error_type pt_table_create(pt_table_id_type pt_id)
pt_table.crc32 = BFLB_Soft_CRC32((uint8_t *)&pt_table, sizeof(pt_table_config) - 4); pt_table.crc32 = BFLB_Soft_CRC32((uint8_t *)&pt_table, sizeof(pt_table_config) - 4);
/* Write back to flash */ /* Write back to flash */
ret = gp_pt_table_flash_erase(write_addr, write_addr + sizeof(pt_table_config) - 1); ret = gp_pt_table_flash_erase(write_addr, write_addr + sizeof(pt_table_config) - 1);
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Erase error\r\n"); MSG_ERR("Flash Erase error\r\n");
return PT_ERROR_FALSH_ERASE; return PT_ERROR_FALSH_ERASE;
} }
ret = gp_pt_table_flash_write(write_addr, (uint8_t *)&pt_table, sizeof(pt_table_config)); ret = gp_pt_table_flash_write(write_addr, (uint8_t *)&pt_table, sizeof(pt_table_config));
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Write error\r\n"); MSG_ERR("Flash Write error\r\n");
return PT_ERROR_FALSH_WRITE; return PT_ERROR_FALSH_WRITE;
} }
return PT_ERROR_SUCCESS; return PT_ERROR_SUCCESS;
} }
pt_table_error_type pt_table_dump(void) pt_table_error_type pt_table_dump(void)
{ {
uint32_t pt_valid[2] = { 0, 0 }; uint32_t pt_valid[2] = { 0, 0 };
@ -374,32 +392,30 @@ pt_table_error_type pt_table_dump(void)
gp_pt_table_flash_read(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&pt_stuff[0], sizeof(pt_table_stuff_config)); gp_pt_table_flash_read(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&pt_stuff[0], sizeof(pt_table_stuff_config));
pt_valid[0] = pt_table_valid(&pt_stuff[0]); pt_valid[0] = pt_table_valid(&pt_stuff[0]);
gp_pt_table_flash_read(BFLB_PT_TABLE1_ADDRESS, (uint8_t *)&pt_stuff[1], sizeof(pt_table_stuff_config)); gp_pt_table_flash_read(BFLB_PT_TABLE1_ADDRESS, (uint8_t *)&pt_stuff[1], sizeof(pt_table_stuff_config));
pt_valid[1] = pt_table_valid(&pt_stuff[1]); pt_valid[1] = pt_table_valid(&pt_stuff[1]);
if(pt_valid[0]) if (pt_valid[0]) {
MSG("PT TABLE0 valid\r\n"); MSG("PT TABLE0 valid\r\n");
else } else {
MSG("PT TABLE0 invalid\r\n"); MSG("PT TABLE0 invalid\r\n");
}
if(pt_valid[1]) if (pt_valid[1]) {
MSG("PT TABLE1 valid\r\n"); MSG("PT TABLE1 valid\r\n");
else } else {
MSG("PT TABLE1 invalid\r\n"); MSG("PT TABLE1 invalid\r\n");
}
for (int i = 0; i < 2; i++) {
for(int i = 0; i < 2; i++) if (pt_valid[i] == 1) {
{
if(pt_valid[i] == 1)
{
MSG("ptStuff[%d].pt_table.magicCode 0x%08x\r\n", i, pt_stuff[i].pt_table.magicCode); MSG("ptStuff[%d].pt_table.magicCode 0x%08x\r\n", i, pt_stuff[i].pt_table.magicCode);
MSG("ptStuff[%d].pt_table.version 0x%08x\r\n", i, pt_stuff[i].pt_table.version); MSG("ptStuff[%d].pt_table.version 0x%08x\r\n", i, pt_stuff[i].pt_table.version);
MSG("ptStuff[%d].pt_table.entryCnt 0x%08x\r\n", i, pt_stuff[i].pt_table.entryCnt); MSG("ptStuff[%d].pt_table.entryCnt 0x%08x\r\n", i, pt_stuff[i].pt_table.entryCnt);
MSG("ptStuff[%d].pt_table.age 0x%08x\r\n", i, pt_stuff[i].pt_table.age); MSG("ptStuff[%d].pt_table.age 0x%08x\r\n", i, pt_stuff[i].pt_table.age);
MSG("ptStuff[%d].pt_table.crc32 0x%08x\r\n", i, pt_stuff[i].pt_table.crc32); MSG("ptStuff[%d].pt_table.crc32 0x%08x\r\n", i, pt_stuff[i].pt_table.crc32);
for(int j = 0; j < pt_stuff[i].pt_table.entryCnt; j++)
{ for (int j = 0; j < pt_stuff[i].pt_table.entryCnt; j++) {
MSG("ptStuff[%d].pt_entries[%d].type 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].type); MSG("ptStuff[%d].pt_entries[%d].type 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].type);
MSG("ptStuff[%d].pt_entries[%d].device 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].device); MSG("ptStuff[%d].pt_entries[%d].device 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].device);
MSG("ptStuff[%d].pt_entries[%d].active_index 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].active_index); MSG("ptStuff[%d].pt_entries[%d].active_index 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].active_index);
@ -411,14 +427,11 @@ pt_table_error_type pt_table_dump(void)
MSG("ptStuff[%d].pt_entries[%d].age 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].age); MSG("ptStuff[%d].pt_entries[%d].age 0x%08x\r\n", i, j, pt_stuff[i].pt_entries[j].age);
} }
} }
} }
return PT_ERROR_SUCCESS; return PT_ERROR_SUCCESS;
} }
pt_table_error_type pt_table_get_iap_para(pt_table_iap_param_type *para) pt_table_error_type pt_table_get_iap_para(pt_table_iap_param_type *para)
{ {
uint32_t pt_valid[2] = { 0, 0 }; uint32_t pt_valid[2] = { 0, 0 };
@ -428,7 +441,6 @@ pt_table_error_type pt_table_get_iap_para(pt_table_iap_param_type * para)
gp_pt_table_flash_read(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&pt_stuff[0], sizeof(pt_table_stuff_config)); gp_pt_table_flash_read(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&pt_stuff[0], sizeof(pt_table_stuff_config));
pt_valid[0] = pt_table_valid(&pt_stuff[0]); pt_valid[0] = pt_table_valid(&pt_stuff[0]);
gp_pt_table_flash_read(BFLB_PT_TABLE1_ADDRESS, (uint8_t *)&pt_stuff[1], sizeof(pt_table_stuff_config)); gp_pt_table_flash_read(BFLB_PT_TABLE1_ADDRESS, (uint8_t *)&pt_stuff[1], sizeof(pt_table_stuff_config));
pt_valid[1] = pt_table_valid(&pt_stuff[1]); pt_valid[1] = pt_table_valid(&pt_stuff[1]);
@ -464,8 +476,6 @@ pt_table_error_type pt_table_get_iap_para(pt_table_iap_param_type * para)
return PT_ERROR_SUCCESS; return PT_ERROR_SUCCESS;
} }
pt_table_error_type pt_table_set_iap_para(pt_table_iap_param_type *para) pt_table_error_type pt_table_set_iap_para(pt_table_iap_param_type *para)
{ {
pt_table_stuff_config pt_stuff, pt_stuff_write; pt_table_stuff_config pt_stuff, pt_stuff_write;
@ -490,22 +500,28 @@ pt_table_error_type pt_table_set_iap_para(pt_table_iap_param_type * para)
if (para->inactive_table_index == 1) { if (para->inactive_table_index == 1) {
ret = gp_pt_table_flash_erase(BFLB_PT_TABLE1_ADDRESS, BFLB_PT_TABLE1_ADDRESS + sizeof(pt_table_stuff_config) - 1); ret = gp_pt_table_flash_erase(BFLB_PT_TABLE1_ADDRESS, BFLB_PT_TABLE1_ADDRESS + sizeof(pt_table_stuff_config) - 1);
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Erase error\r\n"); MSG_ERR("Flash Erase error\r\n");
return PT_ERROR_FALSH_ERASE; return PT_ERROR_FALSH_ERASE;
} }
ret = gp_pt_table_flash_write(BFLB_PT_TABLE1_ADDRESS, (uint8_t *)&pt_stuff_write, sizeof(pt_table_stuff_config)); ret = gp_pt_table_flash_write(BFLB_PT_TABLE1_ADDRESS, (uint8_t *)&pt_stuff_write, sizeof(pt_table_stuff_config));
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Write error\r\n"); MSG_ERR("Flash Write error\r\n");
return PT_ERROR_FALSH_WRITE; return PT_ERROR_FALSH_WRITE;
} }
} else if (para->inactive_table_index == 0) { } else if (para->inactive_table_index == 0) {
ret = gp_pt_table_flash_erase(BFLB_PT_TABLE0_ADDRESS, BFLB_PT_TABLE0_ADDRESS + sizeof(pt_table_stuff_config) - 1); ret = gp_pt_table_flash_erase(BFLB_PT_TABLE0_ADDRESS, BFLB_PT_TABLE0_ADDRESS + sizeof(pt_table_stuff_config) - 1);
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Erase error\r\n"); MSG_ERR("Flash Erase error\r\n");
return PT_ERROR_FALSH_ERASE; return PT_ERROR_FALSH_ERASE;
} }
ret = gp_pt_table_flash_write(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&pt_stuff_write, sizeof(pt_table_stuff_config)); ret = gp_pt_table_flash_write(BFLB_PT_TABLE0_ADDRESS, (uint8_t *)&pt_stuff_write, sizeof(pt_table_stuff_config));
if (ret != SUCCESS) { if (ret != SUCCESS) {
MSG_ERR("Flash Write error\r\n"); MSG_ERR("Flash Write error\r\n");
return PT_ERROR_FALSH_WRITE; return PT_ERROR_FALSH_WRITE;
@ -516,7 +532,6 @@ pt_table_error_type pt_table_set_iap_para(pt_table_iap_param_type * para)
return PT_ERROR_SUCCESS; return PT_ERROR_SUCCESS;
} }
/*@} end of group PARTITION_Public_Functions */ /*@} end of group PARTITION_Public_Functions */
/*@} end of group PARTITION */ /*@} end of group PARTITION */

View file

@ -89,7 +89,8 @@ typedef enum {
/** /**
* @brief Partition table config definition * @brief Partition table config definition
*/ */
typedef struct { typedef struct
{
uint32_t magicCode; /*!< Partition table magic code */ uint32_t magicCode; /*!< Partition table magic code */
uint16_t version; /*!< Partition table verdion */ uint16_t version; /*!< Partition table verdion */
uint16_t entryCnt; /*!< Partition table entry count */ uint16_t entryCnt; /*!< Partition table entry count */
@ -100,7 +101,8 @@ typedef struct {
/** /**
* @brief Partition table entry config definition * @brief Partition table entry config definition
*/ */
typedef struct { typedef struct
{
uint8_t type; /*!< Partition entry type */ uint8_t type; /*!< Partition entry type */
uint8_t device; /*!< Partition entry device */ uint8_t device; /*!< Partition entry device */
uint8_t active_index; /*!< Partition entry active index */ uint8_t active_index; /*!< Partition entry active index */
@ -114,7 +116,8 @@ typedef struct {
/** /**
* @brief Partition table stuff config definition * @brief Partition table stuff config definition
*/ */
typedef struct { typedef struct
{
pt_table_config pt_table; /*!< Partition table */ pt_table_config pt_table; /*!< Partition table */
pt_table_entry_config pt_entries[PT_ENTRY_MAX]; /*!< Partition entries */ pt_table_entry_config pt_entries[PT_ENTRY_MAX]; /*!< Partition entries */
uint32_t crc32; /*!< Partition entries crc32 */ uint32_t crc32; /*!< Partition entries crc32 */
@ -123,7 +126,8 @@ typedef struct {
/** /**
* @brief Partition table iap param definition * @brief Partition table iap param definition
*/ */
typedef struct { typedef struct
{
uint32_t iap_start_addr; uint32_t iap_start_addr;
uint32_t iap_write_addr; uint32_t iap_write_addr;
uint32_t iap_img_len; uint32_t iap_img_len;
@ -203,7 +207,6 @@ pt_table_error_type pt_table_set_iap_para(pt_table_iap_param_type * para);
/*@} end of group BFLB_Common_Driver */ /*@} end of group BFLB_Common_Driver */
extern pt_table_iap_param_type p_iap_param; extern pt_table_iap_param_type p_iap_param;
#endif /* __PARTITION_H__ */ #endif /* __PARTITION_H__ */

View file

@ -103,7 +103,6 @@ BL_Err_Type Ring_Buffer_Init(Ring_Buffer_Type* rbType,uint8_t* buffer,uint32_t s
return SUCCESS; return SUCCESS;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Ring buffer reset function * @brief Ring buffer reset function
* *
@ -131,7 +130,6 @@ BL_Err_Type Ring_Buffer_Reset(Ring_Buffer_Type* rbType)
return SUCCESS; return SUCCESS;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Use callback function to write ring buffer function * @brief Use callback function to write ring buffer function
* *
@ -160,6 +158,7 @@ uint32_t Ring_Buffer_Write_Callback(Ring_Buffer_Type* rbType,uint32_t length,rin
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 0; return 0;
} }
@ -186,10 +185,10 @@ uint32_t Ring_Buffer_Write_Callback(Ring_Buffer_Type* rbType,uint32_t length,rin
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return length; return length;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Copy data from data buffer to ring buffer function * @brief Copy data from data buffer to ring buffer function
* *
@ -208,7 +207,6 @@ static void Ring_Buffer_Write_Copy(void* parameter,uint8_t* dest,uint32_t length
*src += length; *src += length;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Write ring buffer function * @brief Write ring buffer function
* *
@ -224,7 +222,6 @@ uint32_t Ring_Buffer_Write(Ring_Buffer_Type* rbType,const uint8_t* data,uint32_t
return Ring_Buffer_Write_Callback(rbType, length, Ring_Buffer_Write_Copy, &data); return Ring_Buffer_Write_Callback(rbType, length, Ring_Buffer_Write_Copy, &data);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Write 1 byte to ring buffer function * @brief Write 1 byte to ring buffer function
* *
@ -245,6 +242,7 @@ uint32_t Ring_Buffer_Write_Byte(Ring_Buffer_Type* rbType,const uint8_t data)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 0; return 0;
} }
@ -261,10 +259,10 @@ uint32_t Ring_Buffer_Write_Byte(Ring_Buffer_Type* rbType,const uint8_t data)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 1; return 1;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Write ring buffer function, old data will be covered by new data when ring buffer is * @brief Write ring buffer function, old data will be covered by new data when ring buffer is
* full * full
@ -317,10 +315,10 @@ uint32_t Ring_Buffer_Write_Force(Ring_Buffer_Type* rbType,const uint8_t* data,ui
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return length; return length;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Write 1 byte to ring buffer function, old data will be covered by new data when ring * @brief Write 1 byte to ring buffer function, old data will be covered by new data when ring
* buffer is full * buffer is full
@ -363,10 +361,10 @@ uint32_t Ring_Buffer_Write_Byte_Force(Ring_Buffer_Type* rbType,const uint8_t dat
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 1; return 1;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Use callback function to read ring buffer function * @brief Use callback function to read ring buffer function
* *
@ -395,6 +393,7 @@ uint32_t Ring_Buffer_Read_Callback(Ring_Buffer_Type* rbType,uint32_t length,ring
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 0; return 0;
} }
@ -421,10 +420,10 @@ uint32_t Ring_Buffer_Read_Callback(Ring_Buffer_Type* rbType,uint32_t length,ring
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return length; return length;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Copy data from ring buffer to data buffer function * @brief Copy data from ring buffer to data buffer function
* *
@ -443,7 +442,6 @@ static void Ring_Buffer_Read_Copy(void* parameter,uint8_t* data,uint32_t length)
*dest += length; *dest += length;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Read ring buffer function * @brief Read ring buffer function
* *
@ -459,7 +457,6 @@ uint32_t Ring_Buffer_Read(Ring_Buffer_Type* rbType,uint8_t* data,uint32_t length
return Ring_Buffer_Read_Callback(rbType, length, Ring_Buffer_Read_Copy, &data); return Ring_Buffer_Read_Callback(rbType, length, Ring_Buffer_Read_Copy, &data);
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Read 1 byte from ring buffer function * @brief Read 1 byte from ring buffer function
* *
@ -480,6 +477,7 @@ uint32_t Ring_Buffer_Read_Byte(Ring_Buffer_Type* rbType,uint8_t* data)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 0; return 0;
} }
@ -497,10 +495,10 @@ uint32_t Ring_Buffer_Read_Byte(Ring_Buffer_Type* rbType,uint8_t* data)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 1; return 1;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Read ring buffer function, do not remove from buffer actually * @brief Read ring buffer function, do not remove from buffer actually
* *
@ -524,6 +522,7 @@ uint32_t Ring_Buffer_Peek(Ring_Buffer_Type* rbType,uint8_t* data,uint32_t length
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 0; return 0;
} }
@ -547,10 +546,10 @@ uint32_t Ring_Buffer_Peek(Ring_Buffer_Type* rbType,uint8_t* data,uint32_t length
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return length; return length;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Read 1 byte from ring buffer function, do not remove from buffer actually * @brief Read 1 byte from ring buffer function, do not remove from buffer actually
* *
@ -571,6 +570,7 @@ uint32_t Ring_Buffer_Peek_Byte(Ring_Buffer_Type* rbType,uint8_t* data)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 0; return 0;
} }
@ -580,10 +580,10 @@ uint32_t Ring_Buffer_Peek_Byte(Ring_Buffer_Type* rbType,uint8_t* data)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return 1; return 1;
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Get length of data in ring buffer function * @brief Get length of data in ring buffer function
* *
@ -621,7 +621,6 @@ uint32_t Ring_Buffer_Get_Length(Ring_Buffer_Type* rbType)
} }
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Get space remained in ring buffer function * @brief Get space remained in ring buffer function
* *
@ -635,7 +634,6 @@ uint32_t Ring_Buffer_Get_Empty_Length(Ring_Buffer_Type* rbType)
return (rbType->size - Ring_Buffer_Get_Length(rbType)); return (rbType->size - Ring_Buffer_Get_Length(rbType));
} }
/****************************************************************************/ /** /****************************************************************************/ /**
* @brief Get ring buffer status function * @brief Get ring buffer status function
* *
@ -656,21 +654,24 @@ Ring_Buffer_Status_Type Ring_Buffer_Get_Status(Ring_Buffer_Type* rbType)
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return RING_BUFFER_EMPTY; return RING_BUFFER_EMPTY;
} else { } else {
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return RING_BUFFER_FULL; return RING_BUFFER_FULL;
} }
} }
if (rbType->unlock != NULL) { if (rbType->unlock != NULL) {
rbType->unlock(); rbType->unlock();
} }
return RING_BUFFER_PARTIAL; return RING_BUFFER_PARTIAL;
} }
/*@} end of group RING_BUFFER_Public_Functions */ /*@} end of group RING_BUFFER_Public_Functions */
/*@} end of group RING_BUFFER */ /*@} end of group RING_BUFFER */

View file

@ -49,7 +49,8 @@ typedef enum {
/** /**
* @brief Ring buffer structure definition * @brief Ring buffer structure definition
*/ */
typedef struct { typedef struct
{
uint8_t *pointer; /*!< Pointer of ring buffer */ uint8_t *pointer; /*!< Pointer of ring buffer */
uint8_t readMirror; /*!< Read mirror,used to judge empty or full */ uint8_t readMirror; /*!< Read mirror,used to judge empty or full */
uint32_t readIndex; /*!< Index of read address */ uint32_t readIndex; /*!< Index of read address */

View file

@ -27,8 +27,7 @@
// CRC-16: x^16 + x^15 + x^2 + x^0 (0x8005,init 0xFFFF) // CRC-16: x^16 + x^15 + x^2 + x^0 (0x8005,init 0xFFFF)
// we use 0x8005 here and // we use 0x8005 here and
const uint8_t chCRCHTalbe[] = const uint8_t chCRCHTalbe[] = {
{
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
@ -53,8 +52,7 @@ const uint8_t chCRCHTalbe[] =
0x00, 0xC1, 0x81, 0x40 0x00, 0xC1, 0x81, 0x40
}; };
const uint8_t chCRCLTalbe[] = const uint8_t chCRCLTalbe[] = {
{
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
@ -151,8 +149,9 @@ uint32_t BFLB_Soft_CRC32_Table( void *dataIn, uint32_t len)
crc = crc ^ 0xffffffff; crc = crc ^ 0xffffffff;
while (len--) while (len--) {
crc = crc32Tab[(crc ^ *data++) & 0xFF] ^ (crc >> 8); crc = crc32Tab[(crc ^ *data++) & 0xFF] ^ (crc >> 8);
}
return crc ^ 0xffffffff; return crc ^ 0xffffffff;
} }
@ -176,6 +175,7 @@ uint32_t ATTR_TCM_SECTION BFLB_Soft_CRC32(void *dataIn, uint32_t len)
while (len--) { while (len--) {
crc ^= *data++; // crc ^= *data; data++; crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i) { for (i = 0; i < 8; ++i) {
if (crc & 1) { if (crc & 1) {
crc = (crc >> 1) ^ 0xEDB88320; // 0xEDB88320= reverse 0x04C11DB7 crc = (crc >> 1) ^ 0xEDB88320; // 0xEDB88320= reverse 0x04C11DB7
@ -184,6 +184,7 @@ uint32_t ATTR_TCM_SECTION BFLB_Soft_CRC32(void *dataIn, uint32_t len)
} }
} }
} }
return ~crc; return ~crc;
} }
#endif #endif

View file

@ -19,7 +19,6 @@
#include "byteorder.h" #include "byteorder.h"
#include "hci_onchip.h" #include "hci_onchip.h"
extern int hci_host_recv_pkt_handler(uint8_t *data, uint16_t len); extern int hci_host_recv_pkt_handler(uint8_t *data, uint16_t len);
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
@ -39,9 +38,10 @@ struct rx_msg_struct* bl_find_valid_queued_entry(void)
memset(&empty_msg, 0, sizeof(struct rx_msg_struct)); memset(&empty_msg, 0, sizeof(struct rx_msg_struct));
for (int i = 0; i < sizeof(msg_array) / (sizeof(struct rx_msg_struct)); i++) { for (int i = 0; i < sizeof(msg_array) / (sizeof(struct rx_msg_struct)); i++) {
if(!memcmp(&msg_array[i], &empty_msg, sizeof(struct rx_msg_struct))) if (!memcmp(&msg_array[i], &empty_msg, sizeof(struct rx_msg_struct))) {
return (msg_array + i); return (msg_array + i);
} }
}
return NULL; return NULL;
} }
@ -52,20 +52,25 @@ void bl_handle_queued_msg(void)
struct net_buf *buf; struct net_buf *buf;
struct rx_msg_struct *msg; struct rx_msg_struct *msg;
if(k_queue_is_empty(&msg_queue)) if (k_queue_is_empty(&msg_queue)) {
return; return;
}
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT); buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
if(!buf)
if (!buf) {
return; return;
}
msg = k_fifo_get(&msg_queue, K_NO_WAIT); msg = k_fifo_get(&msg_queue, K_NO_WAIT);
BT_ASSERT(buf); BT_ASSERT(buf);
bl_onchiphci_rx_packet_handler(msg->pkt_type, msg->src_id, msg->param, msg->param_len, buf); bl_onchiphci_rx_packet_handler(msg->pkt_type, msg->src_id, msg->param, msg->param_len, buf);
if (msg->param) { if (msg->param) {
k_free(msg->param); k_free(msg->param);
} }
memset(msg, 0, sizeof(struct rx_msg_struct)); memset(msg, 0, sizeof(struct rx_msg_struct));
} }
@ -75,6 +80,7 @@ void bl_onchiphci_interface_deinit(void)
do { do {
msg = k_fifo_get(&msg_queue, K_NO_WAIT); msg = k_fifo_get(&msg_queue, K_NO_WAIT);
if (msg) { if (msg) {
if (msg->param) { if (msg->param) {
k_free(msg->param); k_free(msg->param);
@ -106,26 +112,27 @@ int bl_onchiphci_send_2_controller(struct net_buf *buf)
hci_pkt_struct pkt; hci_pkt_struct pkt;
buf_type = bt_buf_get_type(buf); buf_type = bt_buf_get_type(buf);
switch(buf_type)
{ switch (buf_type) {
case BT_BUF_CMD: case BT_BUF_CMD: {
{
struct bt_hci_cmd_hdr *chdr; struct bt_hci_cmd_hdr *chdr;
if(buf->len < sizeof(struct bt_hci_cmd_hdr)) if (buf->len < sizeof(struct bt_hci_cmd_hdr)) {
return -EINVAL; return -EINVAL;
}
chdr = (void *)buf->data; chdr = (void *)buf->data;
if(buf->len < chdr->param_len) if (buf->len < chdr->param_len) {
return -EINVAL; return -EINVAL;
}
pkt_type = BT_HCI_CMD; pkt_type = BT_HCI_CMD;
opcode = sys_le16_to_cpu(chdr->opcode); opcode = sys_le16_to_cpu(chdr->opcode);
//move buf to the payload //move buf to the payload
net_buf_pull(buf, sizeof(struct bt_hci_cmd_hdr)); net_buf_pull(buf, sizeof(struct bt_hci_cmd_hdr));
switch(opcode)
{ switch (opcode) {
//Refer to hci_cmd_desc_tab_le, for the ones of which dest_ll is BLE_CTRL //Refer to hci_cmd_desc_tab_le, for the ones of which dest_ll is BLE_CTRL
case BT_HCI_OP_LE_CONN_UPDATE: case BT_HCI_OP_LE_CONN_UPDATE:
case BT_HCI_OP_LE_READ_CHAN_MAP: case BT_HCI_OP_LE_READ_CHAN_MAP:
@ -137,14 +144,15 @@ int bl_onchiphci_send_2_controller(struct net_buf *buf)
case BT_HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY: case BT_HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY:
case BT_HCI_OP_LE_SET_DATA_LEN: case BT_HCI_OP_LE_SET_DATA_LEN:
case BT_HCI_OP_LE_READ_PHY: case BT_HCI_OP_LE_READ_PHY:
case BT_HCI_OP_LE_SET_PHY: case BT_HCI_OP_LE_SET_PHY: {
{
//dest_id is connectin handle //dest_id is connectin handle
dest_id = buf->data[0]; dest_id = buf->data[0];
} }
default: default:
break; break;
} }
pkt.p.hci_cmd.opcode = opcode; pkt.p.hci_cmd.opcode = opcode;
pkt.p.hci_cmd.param_len = chdr->param_len; pkt.p.hci_cmd.param_len = chdr->param_len;
pkt.p.hci_cmd.params = buf->data; pkt.p.hci_cmd.params = buf->data;
@ -152,14 +160,15 @@ int bl_onchiphci_send_2_controller(struct net_buf *buf)
break; break;
break; break;
} }
case BT_BUF_ACL_OUT:
{ case BT_BUF_ACL_OUT: {
struct bt_hci_acl_hdr *acl; struct bt_hci_acl_hdr *acl;
//connhandle +l2cap field //connhandle +l2cap field
uint16_t connhdl_l2cf, tlt_len; uint16_t connhdl_l2cf, tlt_len;
if(buf->len < sizeof(struct bt_hci_acl_hdr)) if (buf->len < sizeof(struct bt_hci_acl_hdr)) {
return -EINVAL; return -EINVAL;
}
pkt_type = BT_HCI_ACL_DATA; pkt_type = BT_HCI_ACL_DATA;
acl = (void *)buf->data; acl = (void *)buf->data;
@ -168,8 +177,9 @@ int bl_onchiphci_send_2_controller(struct net_buf *buf)
//move buf to the payload //move buf to the payload
net_buf_pull(buf, sizeof(struct bt_hci_acl_hdr)); net_buf_pull(buf, sizeof(struct bt_hci_acl_hdr));
if(buf->len < tlt_len) if (buf->len < tlt_len) {
return -EINVAL; return -EINVAL;
}
//get connection_handle //get connection_handle
dest_id = bt_acl_handle(connhdl_l2cf); dest_id = bt_acl_handle(connhdl_l2cf);
@ -206,27 +216,22 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
struct rx_msg_struct *rx_msg; struct rx_msg_struct *rx_msg;
#endif #endif
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
if(!rx_buf)
{ if (!rx_buf) {
#endif #endif
buf_type = (pkt_type == BT_HCI_ACL_DATA) ? BT_BUF_ACL_IN : BT_BUF_EVT; buf_type = (pkt_type == BT_HCI_ACL_DATA) ? BT_BUF_ACL_IN : BT_BUF_EVT;
if(pkt_type == BT_HCI_CMD_CMP_EVT || pkt_type == BT_HCI_CMD_STAT_EVT) if (pkt_type == BT_HCI_CMD_CMP_EVT || pkt_type == BT_HCI_CMD_STAT_EVT) {
{
buf = bt_buf_get_cmd_complete(K_FOREVER); buf = bt_buf_get_cmd_complete(K_FOREVER);
} } else {
else
{
do { do {
// When deal with LE ADV report, Don't use reserve buf // When deal with LE ADV report, Don't use reserve buf
if(((pkt_type == BT_HCI_LE_EVT && param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT) if (((pkt_type == BT_HCI_LE_EVT && param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT) || (pkt_type == BT_HCI_ACL_DATA)) &&
|| (pkt_type == BT_HCI_ACL_DATA)) && (bt_buf_get_rx_avail_cnt() <= CONFIG_BT_RX_BUF_RSV_COUNT)) {
(bt_buf_get_rx_avail_cnt() <= CONFIG_BT_RX_BUF_RSV_COUNT))
{
break; break;
} }
//not use K_FOREVER, rw main loop thread cannot be blocked here. if there is no rx buffer,directly igore. //not use K_FOREVER, rw main loop thread cannot be blocked here. if there is no rx buffer,directly igore.
//otherwise, if rw main loop blocked here, hci command cannot be handled. //otherwise, if rw main loop blocked here, hci command cannot be handled.
buf = bt_buf_get_rx(buf_type, K_NO_WAIT); buf = bt_buf_get_rx(buf_type, K_NO_WAIT);
@ -234,37 +239,35 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
} }
if (!buf) { if (!buf) {
if(((++monitor)&0xff) == 0) if (((++monitor) & 0xff) == 0) {
{
BT_WARN("hci_rx_pool is not available\n"); BT_WARN("hci_rx_pool is not available\n");
} }
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
//if it is le adv pkt, discard it. //if it is le adv pkt, discard it.
if(pkt_type == BT_HCI_LE_EVT && param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT) if (pkt_type == BT_HCI_LE_EVT && param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT) {
{
return; return;
} } else {
else
{
rx_msg = bl_find_valid_queued_entry(); rx_msg = bl_find_valid_queued_entry();
if(!rx_msg)
{ if (!rx_msg) {
return; return;
} } else {
else
{
rx_msg->pkt_type = pkt_type; rx_msg->pkt_type = pkt_type;
rx_msg->src_id = src_id; rx_msg->src_id = src_id;
if(param_len)
{ if (param_len) {
rx_msg->param = k_malloc(param_len); rx_msg->param = k_malloc(param_len);
memcpy(rx_msg->param, param, param_len); memcpy(rx_msg->param, param, param_len);
} }
rx_msg->param_len = param_len; rx_msg->param_len = param_len;
k_fifo_put(&msg_queue, rx_msg); k_fifo_put(&msg_queue, rx_msg);
return; return;
} }
} }
#else //OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER #else //OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER
return; return;
#endif #endif
@ -272,11 +275,10 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
monitor = 0; monitor = 0;
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
} } else {
else
{
buf = rx_buf; buf = rx_buf;
} }
#endif #endif
buf_data = net_buf_tail(buf); buf_data = net_buf_tail(buf);
@ -285,10 +287,8 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
bt_buf_set_rx_adv(buf, false); bt_buf_set_rx_adv(buf, false);
#endif #endif
switch(pkt_type) switch (pkt_type) {
{ case BT_HCI_CMD_CMP_EVT: {
case BT_HCI_CMD_CMP_EVT:
{
tlt_len = BT_HCI_EVT_CC_PARAM_OFFSET + param_len; tlt_len = BT_HCI_EVT_CC_PARAM_OFFSET + param_len;
*buf_data++ = BT_HCI_EVT_CMD_COMPLETE; *buf_data++ = BT_HCI_EVT_CMD_COMPLETE;
*buf_data++ = BT_HCI_CCEVT_HDR_PARLEN + param_len; *buf_data++ = BT_HCI_CCEVT_HDR_PARLEN + param_len;
@ -298,8 +298,8 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
memcpy(buf_data, param, param_len); memcpy(buf_data, param, param_len);
break; break;
} }
case BT_HCI_CMD_STAT_EVT:
{ case BT_HCI_CMD_STAT_EVT: {
tlt_len = BT_HCI_CSEVT_LEN; tlt_len = BT_HCI_CSEVT_LEN;
*buf_data++ = BT_HCI_EVT_CMD_STATUS; *buf_data++ = BT_HCI_EVT_CMD_STATUS;
*buf_data++ = BT_HCI_CSVT_PARLEN; *buf_data++ = BT_HCI_CSVT_PARLEN;
@ -308,16 +308,17 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
sys_put_le16(src_id, buf_data); sys_put_le16(src_id, buf_data);
break; break;
} }
case BT_HCI_LE_EVT:
{ case BT_HCI_LE_EVT: {
prio = false; prio = false;
bt_buf_set_type(buf, BT_BUF_EVT); bt_buf_set_type(buf, BT_BUF_EVT);
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
if(param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT)
{ if (param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT) {
bt_buf_set_rx_adv(buf, true); bt_buf_set_rx_adv(buf, true);
} }
#endif #endif
tlt_len = BT_HCI_EVT_LE_PARAM_OFFSET + param_len; tlt_len = BT_HCI_EVT_LE_PARAM_OFFSET + param_len;
@ -326,12 +327,12 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
memcpy(buf_data, param, param_len); memcpy(buf_data, param, param_len);
break; break;
} }
case BT_HCI_EVT:
{ case BT_HCI_EVT: {
if(src_id != BT_HCI_EVT_NUM_COMPLETED_PACKETS) if (src_id != BT_HCI_EVT_NUM_COMPLETED_PACKETS) {
{
prio = false; prio = false;
} }
bt_buf_set_type(buf, BT_BUF_EVT); bt_buf_set_type(buf, BT_BUF_EVT);
tlt_len = BT_HCI_EVT_LE_PARAM_OFFSET + param_len; tlt_len = BT_HCI_EVT_LE_PARAM_OFFSET + param_len;
*buf_data++ = src_id; *buf_data++ = src_id;
@ -339,8 +340,8 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
memcpy(buf_data, param, param_len); memcpy(buf_data, param, param_len);
break; break;
} }
case BT_HCI_ACL_DATA:
{ case BT_HCI_ACL_DATA: {
prio = false; prio = false;
bt_buf_set_type(buf, BT_BUF_ACL_IN); bt_buf_set_type(buf, BT_BUF_ACL_IN);
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
@ -351,8 +352,8 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
#endif #endif
break; break;
} }
default:
{ default: {
net_buf_unref(buf); net_buf_unref(buf);
return; return;
} }
@ -360,12 +361,9 @@ static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, ui
net_buf_add(buf, tlt_len); net_buf_add(buf, tlt_len);
if(prio) if (prio) {
{
bt_recv_prio(buf); bt_recv_prio(buf);
} } else {
else
{
hci_driver_enque_recvq(buf); hci_driver_enque_recvq(buf);
} }
} }

View file

@ -110,26 +110,32 @@ const struct cli_command btStackCmdSet[] STATIC_CLI_CMD_ATTRIBUTE = {
{"ble_set_device_name", "\r\nble_set_device_name:\r\n\[Lenth of name]\r\n\[name]\r\n", blecli_set_device_name}, {"ble_set_device_name", "\r\nble_set_device_name:\r\n\[Lenth of name]\r\n\[name]\r\n", blecli_set_device_name},
#if defined(CONFIG_BLE_TP_SERVER) #if defined(CONFIG_BLE_TP_SERVER)
{"ble_tp_start", "\r\nble_tp_start:\r\n\ {
[TP test,1:enable, 0:disable]\r\n", blecli_tp_start}, "ble_tp_start", "\r\nble_tp_start:\r\n\
[TP test,1:enable, 0:disable]\r\n", blecli_tp_start
},
#endif #endif
#if defined(CONFIG_BT_OBSERVER) #if defined(CONFIG_BT_OBSERVER)
{"ble_start_scan", "\r\nble_start_scan:\r\n\ {
"ble_start_scan", "\r\nble_start_scan:\r\n\
[Scan type, 0:passive scan, 1:active scan]\r\n\ [Scan type, 0:passive scan, 1:active scan]\r\n\
[Duplicate filtering, 0:Disable duplicate filtering, 1:Enable duplicate filtering]\r\n\ [Duplicate filtering, 0:Disable duplicate filtering, 1:Enable duplicate filtering]\r\n\
[Scan interval, 0x0004-4000,e.g.0080]\r\n\ [Scan interval, 0x0004-4000,e.g.0080]\r\n\
[Scan window, 0x0004-4000,e.g.0050]\r\n", blecli_start_scan}, [Scan window, 0x0004-4000,e.g.0050]\r\n", blecli_start_scan
},
{"ble_stop_scan", "\r\nble_stop_scan:[Stop scan]\r\nParameter[Null]\r\n", blecli_stop_scan}, {"ble_stop_scan", "\r\nble_stop_scan:[Stop scan]\r\nParameter[Null]\r\n", blecli_stop_scan},
#endif #endif
#if defined(CONFIG_BT_PERIPHERAL) #if defined(CONFIG_BT_PERIPHERAL)
{"ble_start_adv", "\r\nble_start_adv:\r\n\ {
"ble_start_adv", "\r\nble_start_adv:\r\n\
[Adv type,0:adv_ind,1:adv_scan_ind,2:adv_nonconn_ind]\r\n\ [Adv type,0:adv_ind,1:adv_scan_ind,2:adv_nonconn_ind]\r\n\
[Mode, 0:discov, 1:non-discov]\r\n\ [Mode, 0:discov, 1:non-discov]\r\n\
[Adv Interval Min,0x0020-4000,e.g.0030]\r\n\ [Adv Interval Min,0x0020-4000,e.g.0030]\r\n\
[Adv Interval Max,0x0020-4000,e.g.0060]\r\n", blecli_start_advertise}, [Adv Interval Max,0x0020-4000,e.g.0060]\r\n", blecli_start_advertise
},
{"ble_stop_adv", "\r\nble_stop_adv:[Stop advertising]\r\nParameter[Null]\r\n", blecli_stop_advertise}, {"ble_stop_adv", "\r\nble_stop_adv:[Stop advertising]\r\nParameter[Null]\r\n", blecli_stop_advertise},
@ -138,33 +144,45 @@ const struct cli_command btStackCmdSet[] STATIC_CLI_CMD_ATTRIBUTE = {
#if defined(CONFIG_BT_CONN) #if defined(CONFIG_BT_CONN)
#if defined(CONFIG_BT_CENTRAL) #if defined(CONFIG_BT_CENTRAL)
{"ble_connect", "\r\nble_connect:[Connect remote device]\r\n\ {
"ble_connect", "\r\nble_connect:[Connect remote device]\r\n\
[Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\ [Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\
[Address value, e.g.112233AABBCC]\r\n", blecli_connect}, [Address value, e.g.112233AABBCC]\r\n", blecli_connect
},
#endif //CONFIG_BT_CENTRAL #endif //CONFIG_BT_CENTRAL
{"ble_disconnect", "\r\nble_disconnect:[Disconnect remote device]\r\n\ {
"ble_disconnect", "\r\nble_disconnect:[Disconnect remote device]\r\n\
[Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\ [Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\
[Address value,e.g.112233AABBCC]\r\n", blecli_disconnect}, [Address value,e.g.112233AABBCC]\r\n", blecli_disconnect
},
{"ble_select_conn", "\r\nble_select_conn:[Select a specific connection]\r\n\ {
"ble_select_conn", "\r\nble_select_conn:[Select a specific connection]\r\n\
[Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\ [Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\
[Address value, e.g.112233AABBCC]\r\n", blecli_select_conn}, [Address value, e.g.112233AABBCC]\r\n", blecli_select_conn
},
{"ble_unpair", "\r\nble_unpair:[Unpair connection]\r\n\ {
"ble_unpair", "\r\nble_unpair:[Unpair connection]\r\n\
[Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\ [Address type, 0:ADDR_PUBLIC, 1:ADDR_RAND, 2:ADDR_RPA_OR_PUBLIC, 3:ADDR_RPA_OR_RAND]\r\n\
[Address value, all 0: unpair all connection, otherwise:unpair specific connection]\r\n", blecli_unpair}, [Address value, all 0: unpair all connection, otherwise:unpair specific connection]\r\n", blecli_unpair
},
{"ble_conn_update", "\r\nble_conn_update:\r\n\ {
"ble_conn_update", "\r\nble_conn_update:\r\n\
[Conn Interval Min,0x0006-0C80,e.g.0030]\r\n\ [Conn Interval Min,0x0006-0C80,e.g.0030]\r\n\
[Conn Interval Max,0x0006-0C80,e.g.0030]\r\n\ [Conn Interval Max,0x0006-0C80,e.g.0030]\r\n\
[Conn Latency,0x0000-01f3,e.g.0004]\r\n\ [Conn Latency,0x0000-01f3,e.g.0004]\r\n\
[Supervision Timeout,0x000A-0C80,e.g.0010]\r\n", blecli_conn_update}, [Supervision Timeout,0x000A-0C80,e.g.0010]\r\n", blecli_conn_update
},
#endif //CONFIG_BT_CONN #endif //CONFIG_BT_CONN
#if defined(CONFIG_BT_SMP) #if defined(CONFIG_BT_SMP)
{"ble_security", "\r\nble_security:[Start security]\r\n\ {
[Security level, Default value 4, 2:BT_SECURITY_MEDIUM, 3:BT_SECURITY_HIGH, 4:BT_SECURITY_FIPS]\r\n", blecli_security}, "ble_security", "\r\nble_security:[Start security]\r\n\
[Security level, Default value 4, 2:BT_SECURITY_MEDIUM, 3:BT_SECURITY_HIGH, 4:BT_SECURITY_FIPS]\r\n", blecli_security
},
{"ble_auth", "\r\nble_auth:[Register auth callback]\r\n", blecli_auth}, {"ble_auth", "\r\nble_auth:[Register auth callback]\r\n", blecli_auth},
@ -181,49 +199,63 @@ const struct cli_command btStackCmdSet[] STATIC_CLI_CMD_ATTRIBUTE = {
#if defined(CONFIG_BT_GATT_CLIENT) #if defined(CONFIG_BT_GATT_CLIENT)
{"ble_exchange_mtu", "\r\nble_exchange_mtu:[Exchange mtu]\r\n Parameter[Null]\r\n", blecli_exchange_mtu}, {"ble_exchange_mtu", "\r\nble_exchange_mtu:[Exchange mtu]\r\n Parameter[Null]\r\n", blecli_exchange_mtu},
{"ble_discover", "\r\nble_discover:[Gatt discovery]\r\n\ {
"ble_discover", "\r\nble_discover:[Gatt discovery]\r\n\
[Discovery type, 0:Primary, 1:Secondary, 2:Include, 3:Characteristic, 4:Descriptor]\r\n\ [Discovery type, 0:Primary, 1:Secondary, 2:Include, 3:Characteristic, 4:Descriptor]\r\n\
[Uuid value, 2 Octets, e.g.1800]\r\n\ [Uuid value, 2 Octets, e.g.1800]\r\n\
[Start handle, 2 Octets, e.g.0001]\r\n\ [Start handle, 2 Octets, e.g.0001]\r\n\
[End handle, 2 Octets, e.g.ffff]\r\n", blecli_discover}, [End handle, 2 Octets, e.g.ffff]\r\n", blecli_discover
},
{"ble_read", "\r\nble_read:[Gatt Read]\r\n\ {
"ble_read", "\r\nble_read:[Gatt Read]\r\n\
[Attribute handle, 2 Octets]\r\n\ [Attribute handle, 2 Octets]\r\n\
[Value offset, 2 Octets]\r\n", blecli_read}, [Value offset, 2 Octets]\r\n", blecli_read
},
{"ble_write", "\r\nble_write:[Gatt write]\r\n\ {
"ble_write", "\r\nble_write:[Gatt write]\r\n\
[Attribute handle, 2 Octets]\r\n\ [Attribute handle, 2 Octets]\r\n\
[Value offset, 2 Octets]\r\n\ [Value offset, 2 Octets]\r\n\
[Value length, 2 Octets]\r\n\ [Value length, 2 Octets]\r\n\
[Value data]\r\n", blecli_write}, [Value data]\r\n", blecli_write
},
{"ble_write_without_rsp", "\r\nble_write_without_rsp:[Gatt write without response]\r\n\ {
"ble_write_without_rsp", "\r\nble_write_without_rsp:[Gatt write without response]\r\n\
[Sign, 0: No need signed, 1:Signed write cmd if no smp]\r\n\ [Sign, 0: No need signed, 1:Signed write cmd if no smp]\r\n\
[Attribute handle, 2 Octets]\r\n\ [Attribute handle, 2 Octets]\r\n\
[Value length, 2 Octets]\r\n\ [Value length, 2 Octets]\r\n\
[Value data]\r\n", blecli_write_without_rsp}, [Value data]\r\n", blecli_write_without_rsp
},
{"ble_subscribe", "\r\nble_subscribe:[Gatt subscribe]\r\n\ {
"ble_subscribe", "\r\nble_subscribe:[Gatt subscribe]\r\n\
[CCC handle, 2 Octets]\r\n\ [CCC handle, 2 Octets]\r\n\
[Value handle, 2 Octets]\r\n\ [Value handle, 2 Octets]\r\n\
[Value, 1:notify, 2:indicate]\r\n", blecli_subscribe}, [Value, 1:notify, 2:indicate]\r\n", blecli_subscribe
},
{"ble_unsubscribe", "\r\nble_unsubscribe:[Gatt unsubscribe]\r\n Parameter[Null]\r\n", blecli_unsubscribe}, {"ble_unsubscribe", "\r\nble_unsubscribe:[Gatt unsubscribe]\r\n Parameter[Null]\r\n", blecli_unsubscribe},
#endif /*CONFIG_BT_GATT_CLIENT*/ #endif /*CONFIG_BT_GATT_CLIENT*/
{"ble_set_data_len", {
"ble_set_data_len",
"\r\nble_set_data_len:[LE Set Data Length]\r\n\ "\r\nble_set_data_len:[LE Set Data Length]\r\n\
[tx octets, 2 octets]\r\n\ [tx octets, 2 octets]\r\n\
[tx time, 2 octets]\r\n", [tx time, 2 octets]\r\n",
blecli_set_data_len}, blecli_set_data_len
},
{"ble_conn_info", "\r\nble_conn_info:[LE get all connection devices info]\r\n", blecli_get_all_conn_info}, {"ble_conn_info", "\r\nble_conn_info:[LE get all connection devices info]\r\n", blecli_get_all_conn_info},
#if defined(CONFIG_SET_TX_PWR) #if defined(CONFIG_SET_TX_PWR)
{"ble_set_tx_pwr", {
"ble_set_tx_pwr",
"\r\nble_set_tx_pwr:[Set tx power mode]\r\n\ "\r\nble_set_tx_pwr:[Set tx power mode]\r\n\
[mode, 1 octet, value:5,6,7]\r\n", [mode, 1 octet, value:5,6,7]\r\n",
blecli_set_tx_pwr}, blecli_set_tx_pwr
},
#endif #endif
#else #else
@ -304,9 +336,11 @@ static void connected(struct bt_conn *conn, u8_t err)
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
#if defined(CONFIG_BLE_MULTI_ADV) #if defined(CONFIG_BLE_MULTI_ADV)
if (ble_adv_id && !bt_le_multi_adv_stop(ble_adv_id)) { if (ble_adv_id && !bt_le_multi_adv_stop(ble_adv_id)) {
ble_adv_id = 0; ble_adv_id = 0;
} }
#endif /* CONFIG_BLE_MULTI_ADV */ #endif /* CONFIG_BLE_MULTI_ADV */
if (err) { if (err) {
@ -321,6 +355,7 @@ static void connected(struct bt_conn *conn, u8_t err)
} }
#if defined(CONFIG_BLE_RECONNECT_TEST) #if defined(CONFIG_BLE_RECONNECT_TEST)
if (conn->role == BT_CONN_ROLE_MASTER) { if (conn->role == BT_CONN_ROLE_MASTER) {
if (bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN)) { if (bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN)) {
vOutputString("Disconnection fail. \r\n"); vOutputString("Disconnection fail. \r\n");
@ -328,6 +363,7 @@ static void connected(struct bt_conn *conn, u8_t err)
vOutputString("Disconnect success. \r\n"); vOutputString("Disconnect success. \r\n");
} }
} }
#endif #endif
} }
@ -339,6 +375,7 @@ static void disconnected(struct bt_conn *conn, u8_t reason)
vOutputString("Disconnected: %s (reason %u) \r\n", addr, reason); vOutputString("Disconnected: %s (reason %u) \r\n", addr, reason);
#if defined(CONFIG_BLE_RECONNECT_TEST) #if defined(CONFIG_BLE_RECONNECT_TEST)
if (conn->role == BT_CONN_ROLE_SLAVE) { if (conn->role == BT_CONN_ROLE_SLAVE) {
if (set_adv_enable(true)) { if (set_adv_enable(true)) {
vOutputString("Restart adv fail. \r\n"); vOutputString("Restart adv fail. \r\n");
@ -346,6 +383,7 @@ static void disconnected(struct bt_conn *conn, u8_t reason)
vOutputString("Restart adv success. \r\n"); vOutputString("Restart adv success. \r\n");
} }
} }
#endif #endif
if (default_conn == conn) { if (default_conn == conn) {
@ -413,12 +451,12 @@ static void blecli_set_2M_phy(char *pcWriteBuffer, int xWriteBufferLen, int argc
vOutputString("Not connected \r\n"); vOutputString("Not connected \r\n");
return; return;
} }
if (!hci_le_set_phy(default_conn)) { if (!hci_le_set_phy(default_conn)) {
vOutputString("Set ble 2M Phy successfully \r\n"); vOutputString("Set ble 2M Phy successfully \r\n");
} else { } else {
vOutputString("Failed to set ble 2M Phy\r\n"); vOutputString("Failed to set ble 2M Phy\r\n");
} }
} }
static void blecli_set_default_phy(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_set_default_phy(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
@ -430,6 +468,7 @@ static void blecli_set_default_phy(char *pcWriteBuffer, int xWriteBufferLen, int
vOutputString("Not connected \r\n"); vOutputString("Not connected \r\n");
return; return;
} }
get_uint8_from_string(&argv[1], &phy); get_uint8_from_string(&argv[1], &phy);
if (phy == 0) { if (phy == 0) {
@ -455,9 +494,10 @@ static void blecli_get_device_name(char *pcWriteBuffer, int xWriteBufferLen, int
if (device_name) { if (device_name) {
vOutputString("device_name: %s\r\n", device_name); vOutputString("device_name: %s\r\n", device_name);
}else } else {
vOutputString("Failed to read device name\r\n"); vOutputString("Failed to read device name\r\n");
} }
}
static void blecli_set_device_name(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_set_device_name(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
{ {
@ -465,10 +505,12 @@ static void blecli_set_device_name(char *pcWriteBuffer, int xWriteBufferLen, int
if (strlen(argv[1]) > 0 && strlen(argv[1]) <= CONFIG_BT_DEVICE_NAME_MAX) { if (strlen(argv[1]) > 0 && strlen(argv[1]) <= CONFIG_BT_DEVICE_NAME_MAX) {
err = bt_set_name((char *)argv[1]); err = bt_set_name((char *)argv[1]);
if (err) { if (err) {
vOutputString("Failed to set device name\r\n"); vOutputString("Failed to set device name\r\n");
}else } else {
vOutputString("Set the device name successfully\r\n"); vOutputString("Set the device name successfully\r\n");
}
} else { } else {
vOutputString("Invaild lenth(%d)\r\n", strlen(argv[1])); vOutputString("Invaild lenth(%d)\r\n", strlen(argv[1]));
} }
@ -479,6 +521,7 @@ static void blecli_tp_start(char *pcWriteBuffer, int xWriteBufferLen, int argc,
extern u8_t tp_start; extern u8_t tp_start;
get_uint8_from_string(&argv[1], &tp_start); get_uint8_from_string(&argv[1], &tp_start);
if (tp_start == 1) { if (tp_start == 1) {
vOutputString("Ble Throughput enable\r\n"); vOutputString("Ble Throughput enable\r\n");
} else if (tp_start == 0) { } else if (tp_start == 0) {
@ -502,12 +545,12 @@ static bool data_cb(struct bt_data *data, void *user_data)
len = (data->data_len > NAME_LEN - 1) ? (NAME_LEN - 1) : (data->data_len); len = (data->data_len > NAME_LEN - 1) ? (NAME_LEN - 1) : (data->data_len);
memcpy(name, data->data, len); memcpy(name, data->data, len);
return false; return false;
default: default:
return true; return true;
} }
} }
static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t evtype, static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t evtype,
struct net_buf_simple *buf) struct net_buf_simple *buf)
{ {
@ -550,12 +593,12 @@ static void blecli_start_scan(char *pcWriteBuffer, int xWriteBufferLen, int argc
} }
} }
static void blecli_stop_scan(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_stop_scan(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
{ {
int err; int err;
err = bt_le_scan_stop(); err = bt_le_scan_stop();
if (err) { if (err) {
vOutputString("Stopping scanning failed (err %d)\r\n", err); vOutputString("Stopping scanning failed (err %d)\r\n", err);
} else { } else {
@ -577,6 +620,7 @@ static void blecli_scan_filter_size(char *pcWriteBuffer, int xWriteBufferLen, in
get_uint8_from_string(&argv[1], &size); get_uint8_from_string(&argv[1], &size);
err = ble_controller_set_scan_filter_table_size(size); err = ble_controller_set_scan_filter_table_size(size);
if (err) { if (err) {
vOutputString("Set failed (err %d)\r\n", err); vOutputString("Set failed (err %d)\r\n", err);
} else { } else {
@ -587,7 +631,6 @@ static void blecli_scan_filter_size(char *pcWriteBuffer, int xWriteBufferLen, in
#endif //#if defined(CONFIG_BT_OBSERVER) #endif //#if defined(CONFIG_BT_OBSERVER)
#if defined(CONFIG_BT_PERIPHERAL) #if defined(CONFIG_BT_PERIPHERAL)
static void blecli_read_local_address(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_read_local_address(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
{ {
@ -608,20 +651,16 @@ static void blecli_set_adv_channel(char *pcWriteBuffer, int xWriteBufferLen, int
{ {
u8_t channel = 7; u8_t channel = 7;
if(argc != 2) if (argc != 2) {
{
vOutputString("Number of Parameters is not correct\r\n"); vOutputString("Number of Parameters is not correct\r\n");
return; return;
} }
get_uint8_from_string(&argv[1], &channel); get_uint8_from_string(&argv[1], &channel);
if (set_adv_channel_map(channel) == 0) if (set_adv_channel_map(channel) == 0) {
{
vOutputString("Set adv channel success\r\n"); vOutputString("Set adv channel success\r\n");
} } else {
else
{
vOutputString("Failed to Set adv channel\r\n"); vOutputString("Failed to Set adv channel\r\n");
} }
} }
@ -646,6 +685,7 @@ static void blecli_start_advertise(char *pcWriteBuffer, int xWriteBufferLen, int
/*Get adv type, 0:adv_ind, 1:adv_scan_ind, 2:adv_nonconn_ind 3: adv_direct_ind*/ /*Get adv type, 0:adv_ind, 1:adv_scan_ind, 2:adv_nonconn_ind 3: adv_direct_ind*/
get_uint8_from_string(&argv[1], &adv_type); get_uint8_from_string(&argv[1], &adv_type);
vOutputString("adv_type 0x%x\r\n", adv_type); vOutputString("adv_type 0x%x\r\n", adv_type);
if (adv_type == 0) { if (adv_type == 0) {
param.options = (BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_USE_NAME | BT_LE_ADV_OPT_ONE_TIME); param.options = (BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_USE_NAME | BT_LE_ADV_OPT_ONE_TIME);
} else if (adv_type == 1) { } else if (adv_type == 1) {
@ -660,8 +700,8 @@ static void blecli_start_advertise(char *pcWriteBuffer, int xWriteBufferLen, int
/*Get mode, 0:General discoverable, 1:non discoverable, 2:limit discoverable*/ /*Get mode, 0:General discoverable, 1:non discoverable, 2:limit discoverable*/
get_uint8_from_string(&argv[2], &mode); get_uint8_from_string(&argv[2], &mode);
vOutputString("mode 0x%x\r\n", mode); vOutputString("mode 0x%x\r\n", mode);
if(mode == 0 || mode == 1 || mode == 2){
if (mode == 0 || mode == 1 || mode == 2) {
if (mode == 0) { if (mode == 0) {
struct bt_data gen_disc_data = (struct bt_data)BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_NO_BREDR | BT_LE_AD_GENERAL)); struct bt_data gen_disc_data = (struct bt_data)BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_NO_BREDR | BT_LE_AD_GENERAL));
ad_discov[0] = gen_disc_data; ad_discov[0] = gen_disc_data;
@ -697,17 +737,21 @@ static void blecli_start_advertise(char *pcWriteBuffer, int xWriteBufferLen, int
if (adv_type == 1 || adv_type == 0) { if (adv_type == 1 || adv_type == 0) {
#if defined(CONFIG_BLE_MULTI_ADV) #if defined(CONFIG_BLE_MULTI_ADV)
if (ble_adv_id == 0) { if (ble_adv_id == 0) {
err = bt_le_multi_adv_start(&param, ad, ad_len, &ad_discov[0], 1, &ble_adv_id); err = bt_le_multi_adv_start(&param, ad, ad_len, &ad_discov[0], 1, &ble_adv_id);
} }
#else #else
err = bt_le_adv_start(&param, ad, ad_len, &ad_discov[0], 1); err = bt_le_adv_start(&param, ad, ad_len, &ad_discov[0], 1);
#endif /*CONFIG_BLE_MULTI_ADV*/ #endif /*CONFIG_BLE_MULTI_ADV*/
} else { } else {
#if defined(CONFIG_BLE_MULTI_ADV) #if defined(CONFIG_BLE_MULTI_ADV)
if (ble_adv_id == 0) { if (ble_adv_id == 0) {
err = bt_le_multi_adv_start(&param, ad, ad_len, NULL, 0, &ble_adv_id); err = bt_le_multi_adv_start(&param, ad, ad_len, NULL, 0, &ble_adv_id);
} }
#else #else
err = bt_le_adv_start(&param, ad, ad_len, NULL, 0); err = bt_le_adv_start(&param, ad, ad_len, NULL, 0);
#endif #endif
@ -718,19 +762,21 @@ static void blecli_start_advertise(char *pcWriteBuffer, int xWriteBufferLen, int
} else { } else {
vOutputString("Advertising started\r\n"); vOutputString("Advertising started\r\n");
} }
} }
static void blecli_stop_advertise(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_stop_advertise(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
{ {
#if defined(CONFIG_BLE_MULTI_ADV) #if defined(CONFIG_BLE_MULTI_ADV)
bool err = -1; bool err = -1;
if (ble_adv_id && !bt_le_multi_adv_stop(ble_adv_id)) { if (ble_adv_id && !bt_le_multi_adv_stop(ble_adv_id)) {
ble_adv_id = 0; ble_adv_id = 0;
err = 0; err = 0;
} }
if (err) { if (err) {
#else #else
if (bt_le_adv_stop()) { if (bt_le_adv_stop()) {
#endif #endif
vOutputString("Failed to stop advertising\r\n"); vOutputString("Failed to stop advertising\r\n");
@ -762,6 +808,7 @@ static void blecli_start_multi_advertise(char *pcWriteBuffer, int xWriteBufferLe
ad_len_1 = ARRAY_SIZE(ad_discov); ad_len_1 = ARRAY_SIZE(ad_discov);
err_1 = bt_le_multi_adv_start(&param_1, ad_1, ad_len_1, NULL, 0, &instant_id_1); err_1 = bt_le_multi_adv_start(&param_1, ad_1, ad_len_1, NULL, 0, &instant_id_1);
if (err_1) { if (err_1) {
vOutputString("Failed to start multi adv_1 (err_1%d)\r\n", err_1); vOutputString("Failed to start multi adv_1 (err_1%d)\r\n", err_1);
} else { } else {
@ -782,6 +829,7 @@ static void blecli_start_multi_advertise(char *pcWriteBuffer, int xWriteBufferLe
ad_len_2 = ARRAY_SIZE(ad_discov); ad_len_2 = ARRAY_SIZE(ad_discov);
err_2 = bt_le_multi_adv_start(&param_2, ad_2, ad_len_2, NULL, 0, &instant_id_2); err_2 = bt_le_multi_adv_start(&param_2, ad_2, ad_len_2, NULL, 0, &instant_id_2);
if (err_2) { if (err_2) {
vOutputString("Failed to start multi adv_2 (err_2: %d)\r\n", err_2); vOutputString("Failed to start multi adv_2 (err_2: %d)\r\n", err_2);
} else { } else {
@ -798,9 +846,7 @@ static void blecli_stop_multi_advertise(char *pcWriteBuffer, int xWriteBufferLen
if (bt_le_multi_adv_stop(instant_id)) { if (bt_le_multi_adv_stop(instant_id)) {
vOutputString("Multi adv instant %d stop failed\r\n", instant_id); vOutputString("Multi adv instant %d stop failed\r\n", instant_id);
} } else {
else
{
vOutputString("Multi adv instant %d stop successed\r\n", instant_id); vOutputString("Multi adv instant %d stop successed\r\n", instant_id);
} }
} }
@ -838,6 +884,7 @@ static void blecli_connect(char *pcWriteBuffer, int xWriteBufferLen, int argc, c
addr.type = type; addr.type = type;
get_bytearray_from_string(&argv[2], addr_val, 6); get_bytearray_from_string(&argv[2], addr_val, 6);
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
vOutputString("addr[%d]:[0x%x]\r\n", i, addr_val[i]); vOutputString("addr[%d]:[0x%x]\r\n", i, addr_val[i]);
} }
@ -866,6 +913,7 @@ static void blecli_disconnect(char *pcWriteBuffer, int xWriteBufferLen, int argc
vOutputString("Number of Parameters is not correct\r\n"); vOutputString("Number of Parameters is not correct\r\n");
return; return;
} }
get_uint8_from_string(&argv[1], (u8_t *)&type); get_uint8_from_string(&argv[1], (u8_t *)&type);
get_bytearray_from_string(&argv[2], addr_val, 6); get_bytearray_from_string(&argv[2], addr_val, 6);
reverse_bytearray(addr_val, addr.a.val, 6); reverse_bytearray(addr_val, addr.a.val, 6);
@ -959,6 +1007,7 @@ static void blecli_conn_update(char *pcWriteBuffer, int xWriteBufferLen, int arg
vOutputString("Number of Parameters is not correct\r\n"); vOutputString("Number of Parameters is not correct\r\n");
return; return;
} }
get_uint16_from_string(&argv[1], &param.interval_min); get_uint16_from_string(&argv[1], &param.interval_min);
get_uint16_from_string(&argv[2], &param.interval_max); get_uint16_from_string(&argv[2], &param.interval_max);
get_uint16_from_string(&argv[3], &param.latency); get_uint16_from_string(&argv[3], &param.latency);
@ -984,8 +1033,9 @@ static void blecli_security(char *pcWriteBuffer, int xWriteBufferLen, int argc,
return; return;
} }
if(argc == 2) if (argc == 2) {
get_uint8_from_string(&argv[1], &sec_level); get_uint8_from_string(&argv[1], &sec_level);
}
err = bt_conn_set_security(default_conn, sec_level); err = bt_conn_set_security(default_conn, sec_level);
@ -1101,7 +1151,6 @@ static void blecli_auth_cancel(char *pcWriteBuffer, int xWriteBufferLen, int arg
static void blecli_auth_passkey_confirm(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_auth_passkey_confirm(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
{ {
if (!default_conn) { if (!default_conn) {
vOutputString("Not connected\r\n"); vOutputString("Not connected\r\n");
return; return;
@ -1112,7 +1161,6 @@ static void blecli_auth_passkey_confirm(char *pcWriteBuffer, int xWriteBufferLen
static void blecli_auth_pairing_confirm(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_auth_pairing_confirm(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
{ {
if (!default_conn) { if (!default_conn) {
vOutputString("Not connected\r\n"); vOutputString("Not connected\r\n");
return; return;
@ -1136,6 +1184,7 @@ static void blecli_auth_passkey(char *pcWriteBuffer, int xWriteBufferLen, int ar
} }
passkey = atoi(argv[1]); passkey = atoi(argv[1]);
if (passkey > PASSKEY_MAX) { if (passkey > PASSKEY_MAX) {
vOutputString("Passkey should be between 0-999999\r\n"); vOutputString("Passkey should be between 0-999999\r\n");
return; return;
@ -1167,6 +1216,7 @@ static void blecli_exchange_mtu(char *pcWriteBuffer, int xWriteBufferLen, int ar
exchange_params.func = exchange_func; exchange_params.func = exchange_func;
err = bt_gatt_exchange_mtu(default_conn, &exchange_params); err = bt_gatt_exchange_mtu(default_conn, &exchange_params);
if (err) { if (err) {
vOutputString("Exchange failed (err %d)\r\n", err); vOutputString("Exchange failed (err %d)\r\n", err);
} else { } else {
@ -1234,18 +1284,21 @@ u8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct
bt_uuid_to_str(gatt_service->uuid, str, sizeof(str)); bt_uuid_to_str(gatt_service->uuid, str, sizeof(str));
vOutputString("Service %s found: start handle %x, end_handle %x\r\n", str, attr->handle, gatt_service->end_handle); vOutputString("Service %s found: start handle %x, end_handle %x\r\n", str, attr->handle, gatt_service->end_handle);
break; break;
case BT_GATT_DISCOVER_CHARACTERISTIC: case BT_GATT_DISCOVER_CHARACTERISTIC:
gatt_chrc = attr->user_data; gatt_chrc = attr->user_data;
bt_uuid_to_str(gatt_chrc->uuid, str, sizeof(str)); bt_uuid_to_str(gatt_chrc->uuid, str, sizeof(str));
vOutputString("Characteristic %s found: attr->handle %x chrc->handle %x \r\n", str, attr->handle, gatt_chrc->value_handle); vOutputString("Characteristic %s found: attr->handle %x chrc->handle %x \r\n", str, attr->handle, gatt_chrc->value_handle);
print_chrc_props(gatt_chrc->properties); print_chrc_props(gatt_chrc->properties);
break; break;
case BT_GATT_DISCOVER_INCLUDE: case BT_GATT_DISCOVER_INCLUDE:
gatt_include = attr->user_data; gatt_include = attr->user_data;
bt_uuid_to_str(gatt_include->uuid, str, sizeof(str)); bt_uuid_to_str(gatt_include->uuid, str, sizeof(str));
vOutputString("Include %s found: handle %x, start %x, end %x\r\n", str, attr->handle, vOutputString("Include %s found: handle %x, start %x, end %x\r\n", str, attr->handle,
gatt_include->start_handle, gatt_include->end_handle); gatt_include->start_handle, gatt_include->end_handle);
break; break;
default: default:
bt_uuid_to_str(attr->uuid, str, sizeof(str)); bt_uuid_to_str(attr->uuid, str, sizeof(str));
vOutputString("Descriptor %s found: handle %x\r\n", str, attr->handle); vOutputString("Descriptor %s found: handle %x\r\n", str, attr->handle);
@ -1275,6 +1328,7 @@ static void blecli_discover(char *pcWriteBuffer, int xWriteBufferLen, int argc,
discover_params.end_handle = 0xffff; discover_params.end_handle = 0xffff;
get_uint8_from_string(&argv[1], &disc_type); get_uint8_from_string(&argv[1], &disc_type);
if (disc_type == 0) { if (disc_type == 0) {
discover_params.type = BT_GATT_DISCOVER_PRIMARY; discover_params.type = BT_GATT_DISCOVER_PRIMARY;
} else if (disc_type == 1) { } else if (disc_type == 1) {
@ -1289,17 +1343,20 @@ static void blecli_discover(char *pcWriteBuffer, int xWriteBufferLen, int argc,
vOutputString("Invalid discovery type\r\n"); vOutputString("Invalid discovery type\r\n");
return; return;
} }
get_uint16_from_string(&argv[2], &uuid.val); get_uint16_from_string(&argv[2], &uuid.val);
if(uuid.val) if (uuid.val) {
discover_params.uuid = &uuid.uuid; discover_params.uuid = &uuid.uuid;
else } else {
discover_params.uuid = NULL; discover_params.uuid = NULL;
}
get_uint16_from_string(&argv[3], &discover_params.start_handle); get_uint16_from_string(&argv[3], &discover_params.start_handle);
get_uint16_from_string(&argv[4], &discover_params.end_handle); get_uint16_from_string(&argv[4], &discover_params.end_handle);
err = bt_gatt_discover(default_conn, &discover_params); err = bt_gatt_discover(default_conn, &discover_params);
if (err) { if (err) {
vOutputString("Discover failed (err %d)\r\n", err); vOutputString("Discover failed (err %d)\r\n", err);
} else { } else {
@ -1322,6 +1379,7 @@ static u8_t read_func(struct bt_conn *conn, u8_t err, struct bt_gatt_read_params
if (length > 0 && length <= sizeof(str)) { if (length > 0 && length <= sizeof(str)) {
memcpy(str, buf, length); memcpy(str, buf, length);
vOutputString("device name : %s \r\n", str); vOutputString("device name : %s \r\n", str);
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
vOutputString("buf=[0x%x]\r\n", buf[i]); vOutputString("buf=[0x%x]\r\n", buf[i]);
} }
@ -1356,6 +1414,7 @@ static void blecli_read(char *pcWriteBuffer, int xWriteBufferLen, int argc, char
read_params.handle_count = 1; read_params.handle_count = 1;
err = bt_gatt_read(default_conn, &read_params); err = bt_gatt_read(default_conn, &read_params);
if (err) { if (err) {
vOutputString("Read failed (err %d)\r\n", err); vOutputString("Read failed (err %d)\r\n", err);
} else { } else {
@ -1399,14 +1458,16 @@ static void blecli_write(char *pcWriteBuffer, int xWriteBufferLen, int argc, cha
get_uint16_from_string(&argv[3], &write_params.length); get_uint16_from_string(&argv[3], &write_params.length);
data_len = write_params.length; data_len = write_params.length;
gatt_write_buf = k_malloc(data_len); gatt_write_buf = k_malloc(data_len);
if(!gatt_write_buf)
{ if (!gatt_write_buf) {
vOutputString("Failed to alloc buffer for the data\r\n"); vOutputString("Failed to alloc buffer for the data\r\n");
return; return;
} }
get_bytearray_from_string(&argv[4], gatt_write_buf, data_len); get_bytearray_from_string(&argv[4], gatt_write_buf, data_len);
write_params.data = k_malloc(data_len);; write_params.data = k_malloc(data_len);
;
write_params.length = data_len; write_params.length = data_len;
write_params.func = write_func; write_params.func = write_func;
@ -1443,11 +1504,12 @@ static void blecli_write_without_rsp(char *pcWriteBuffer, int xWriteBufferLen, i
get_uint16_from_string(&argv[2], &handle); get_uint16_from_string(&argv[2], &handle);
get_uint16_from_string(&argv[3], &len); get_uint16_from_string(&argv[3], &len);
gatt_write_buf = k_malloc(len); gatt_write_buf = k_malloc(len);
if(!gatt_write_buf)
{ if (!gatt_write_buf) {
vOutputString("Failed to alloc buffer for the data\r\n"); vOutputString("Failed to alloc buffer for the data\r\n");
return; return;
} }
get_bytearray_from_string(&argv[4], gatt_write_buf, len); get_bytearray_from_string(&argv[4], gatt_write_buf, len);
err = bt_gatt_write_without_response(default_conn, handle, gatt_write_buf, len, sign); err = bt_gatt_write_without_response(default_conn, handle, gatt_write_buf, len, sign);
@ -1475,15 +1537,19 @@ static u8_t notify_func(struct bt_conn *conn,
} }
#if defined(CONFIG_BLE_TP_TEST) #if defined(CONFIG_BLE_TP_TEST)
if (!time) { if (!time) {
time = k_now_ms(); time = k_now_ms();
} }
len += length; len += length;
if (k_now_ms() - time >= 1000) { if (k_now_ms() - time >= 1000) {
vOutputString("data_len=[%d]\r\n", len); vOutputString("data_len=[%d]\r\n", len);
time = k_now_ms(); time = k_now_ms();
len = 0; len = 0;
} }
#endif #endif
//vOutputString("Notification: data length %u\r\n", length); //vOutputString("Notification: data length %u\r\n", length);
@ -1508,12 +1574,12 @@ static void blecli_subscribe(char *pcWriteBuffer, int xWriteBufferLen, int argc,
subscribe_params.notify = notify_func; subscribe_params.notify = notify_func;
int err = bt_gatt_subscribe(default_conn, &subscribe_params); int err = bt_gatt_subscribe(default_conn, &subscribe_params);
if (err) { if (err) {
vOutputString("Subscribe failed (err %d)\r\n", err); vOutputString("Subscribe failed (err %d)\r\n", err);
} else { } else {
vOutputString("Subscribed\r\n"); vOutputString("Subscribed\r\n");
} }
} }
static void blecli_unsubscribe(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv) static void blecli_unsubscribe(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
@ -1529,6 +1595,7 @@ static void blecli_unsubscribe(char *pcWriteBuffer, int xWriteBufferLen, int arg
} }
int err = bt_gatt_unsubscribe(default_conn, &subscribe_params); int err = bt_gatt_unsubscribe(default_conn, &subscribe_params);
if (err) { if (err) {
vOutputString("Unsubscribe failed (err %d)\r\n", err); vOutputString("Unsubscribe failed (err %d)\r\n", err);
} else { } else {
@ -1557,11 +1624,10 @@ static void blecli_set_data_len(char *pcWriteBuffer, int xWriteBufferLen, int ar
get_uint16_from_string(&argv[2], &tx_time); get_uint16_from_string(&argv[2], &tx_time);
err = bt_le_set_data_len(default_conn, tx_octets, tx_time); err = bt_le_set_data_len(default_conn, tx_octets, tx_time);
if (err) { if (err) {
vOutputString("ble_set_data_len, LE Set Data Length (err %d)\r\n", err); vOutputString("ble_set_data_len, LE Set Data Length (err %d)\r\n", err);
} } else {
else
{
vOutputString("ble_set_data_len, LE Set Data Length success\r\n"); vOutputString("ble_set_data_len, LE Set Data Length success\r\n");
} }
} }
@ -1574,15 +1640,14 @@ static void blecli_get_all_conn_info(char *pcWriteBuffer, int xWriteBufferLen, i
link_num = bt_conn_get_remote_dev_info(info); link_num = bt_conn_get_remote_dev_info(info);
if(link_num > 0) if (link_num > 0) {
{
bt_addr_le_to_str(info[0].le.local, le_addr, sizeof(le_addr)); bt_addr_le_to_str(info[0].le.local, le_addr, sizeof(le_addr));
vOutputString("ble local device address: %s\r\n", le_addr); vOutputString("ble local device address: %s\r\n", le_addr);
} }
vOutputString("ble connected devices count: %d\r\n", link_num); vOutputString("ble connected devices count: %d\r\n", link_num);
for(int i = 0; i < link_num; i++)
{ for (int i = 0; i < link_num; i++) {
bt_addr_le_to_str(info[i].le.remote, le_addr, sizeof(le_addr)); bt_addr_le_to_str(info[i].le.remote, le_addr, sizeof(le_addr));
vOutputString("[%d]: address %s\r\n", i, le_addr); vOutputString("[%d]: address %s\r\n", i, le_addr);
} }
@ -1601,25 +1666,27 @@ static void blecli_set_tx_pwr(char *pcWriteBuffer, int xWriteBufferLen, int argc
get_uint8_from_string(&argv[1], &power); get_uint8_from_string(&argv[1], &power);
#if defined(BL602) #if defined(BL602)
if (power > 21) { if (power > 21) {
vOutputString("ble_set_tx_pwr, invalid value, value shall be in [%d - %d]\r\n", 0, 21); vOutputString("ble_set_tx_pwr, invalid value, value shall be in [%d - %d]\r\n", 0, 21);
return; return;
} }
#elif defined(BL702) #elif defined(BL702)
if (power > 14) { if (power > 14) {
vOutputString("ble_set_tx_pwr, invalid value, value shall be in [%d - %d]\r\n", 0, 14); vOutputString("ble_set_tx_pwr, invalid value, value shall be in [%d - %d]\r\n", 0, 14);
return; return;
} }
#endif #endif
err = bt_set_tx_pwr((int8_t)power); err = bt_set_tx_pwr((int8_t)power);
if (err) { if (err) {
vOutputString("ble_set_tx_pwr, Fail to set tx power (err %d)\r\n", err); vOutputString("ble_set_tx_pwr, Fail to set tx power (err %d)\r\n", err);
} } else {
else{
vOutputString("ble_set_tx_pwr, Set tx power successfully\r\n"); vOutputString("ble_set_tx_pwr, Set tx power successfully\r\n");
} }
} }
#endif #endif
@ -1629,6 +1696,7 @@ static void blecli_disable(char *pcWriteBuffer, int xWriteBufferLen, int argc, c
int err; int err;
err = bt_disable(); err = bt_disable();
if (err) { if (err) {
vOutputString("Fail to disable bt, there is existed scan/adv/conn event \r\n"); vOutputString("Fail to disable bt, there is existed scan/adv/conn event \r\n");
} else { } else {
@ -1653,6 +1721,7 @@ static void blecli_hog_srv_notify(char *pcWriteBuffer, int xWriteBufferLen, int
get_uint8_from_string(&argv[2], &press); get_uint8_from_string(&argv[2], &press);
err = hog_notify(default_conn, hid_usage, press); err = hog_notify(default_conn, hid_usage, press);
if (err) { if (err) {
vOutputString("Failed to send notification\r\n"); vOutputString("Failed to send notification\r\n");
} else { } else {

View file

@ -19,8 +19,6 @@
#include "a2dp.h" #include "a2dp.h"
#endif #endif
static void bredr_connected(struct bt_conn *conn, u8_t err); static void bredr_connected(struct bt_conn *conn, u8_t err);
static void bredr_disconnected(struct bt_conn *conn, u8_t reason); static void bredr_disconnected(struct bt_conn *conn, u8_t reason);
@ -41,8 +39,6 @@ static void bredr_write_eir(char *p_write_buffer, int write_buffer_len, int argc
static void bredr_discoverable(char *p_write_buffer, int write_buffer_len, int argc, char **argv); static void bredr_discoverable(char *p_write_buffer, int write_buffer_len, int argc, char **argv);
static void bredr_connectable(char *p_write_buffer, int write_buffer_len, int argc, char **argv); static void bredr_connectable(char *p_write_buffer, int write_buffer_len, int argc, char **argv);
const struct cli_command bredr_cmd_set[] STATIC_CLI_CMD_ATTRIBUTE = { const struct cli_command bredr_cmd_set[] STATIC_CLI_CMD_ATTRIBUTE = {
#if PCM_PRINTF #if PCM_PRINTF
{ "pcm", "", pcm }, { "pcm", "", pcm },
@ -54,7 +50,6 @@ const struct cli_command bredr_cmd_set[] STATIC_CLI_CMD_ATTRIBUTE = {
}; };
#if PCM_PRINTF #if PCM_PRINTF
extern OI_BYTE sbc_frame[]; extern OI_BYTE sbc_frame[];
extern OI_UINT32 sbc_frame_len; extern OI_UINT32 sbc_frame_len;
@ -77,11 +72,9 @@ static void pcm(char *p_write_buffer, int write_buffer_len, int argc, char **arg
printf("SAMPLERATE: 44100\n"); printf("SAMPLERATE: 44100\n");
printf("NORMALIZED: FALSE\n"); printf("NORMALIZED: FALSE\n");
for(int i = 0; i < samps; i ++) for (int i = 0; i < samps; i++) {
{
printf("%d\n", cool_edit[i]); printf("%d\n", cool_edit[i]);
} }
} }
#endif #endif
@ -99,7 +92,6 @@ static void bredr_init(char *pcWriteBuffer, int xWriteBufferLen, int argc, char
printf("bredr init successfully\n"); printf("bredr init successfully\n");
} }
static void bredr_connected(struct bt_conn *conn, u8_t err) static void bredr_connected(struct bt_conn *conn, u8_t err)
{ {
char addr[BT_ADDR_STR_LEN]; char addr[BT_ADDR_STR_LEN];
@ -114,8 +106,7 @@ static void bredr_connected(struct bt_conn *conn, u8_t err)
printf("bredr connected: %s \r\n", addr); printf("bredr connected: %s \r\n", addr);
if (!default_conn) if (!default_conn) {
{
default_conn = conn; default_conn = conn;
} }
} }
@ -129,8 +120,7 @@ static void bredr_disconnected(struct bt_conn *conn, u8_t reason)
printf("bredr disconnected: %s (reason %u) \r\n", addr, reason); printf("bredr disconnected: %s (reason %u) \r\n", addr, reason);
if (default_conn == conn) if (default_conn == conn) {
{
default_conn = NULL; default_conn = NULL;
} }
} }
@ -146,13 +136,14 @@ static void bredr_write_eir(char *p_write_buffer, int write_buffer_len, int argc
data[1] = 0x09; data[1] = 0x09;
memcpy(data + 2, name, strlen(name)); memcpy(data + 2, name, strlen(name));
for(int i = 0; i < strlen(name); i++) for (int i = 0; i < strlen(name); i++) {
{
printf("0x%02x ", data[2 + i]); printf("0x%02x ", data[2 + i]);
} }
printf("\n"); printf("\n");
err = bt_br_write_eir(rec, data); err = bt_br_write_eir(rec, data);
if (err) { if (err) {
printf("BR/EDR write EIR failed, (err %d)\n", err); printf("BR/EDR write EIR failed, (err %d)\n", err);
} else { } else {
@ -216,7 +207,6 @@ static void bredr_connectable(char *p_write_buffer, int write_buffer_len, int ar
} }
} }
int bredr_cli_register(void) int bredr_cli_register(void)
{ {
// static command(s) do NOT need to call aos_cli_register_command(s) to register. // static command(s) do NOT need to call aos_cli_register_command(s) to register.
@ -225,4 +215,3 @@ int bredr_cli_register(void)
//aos_cli_register_commands(bredr_cmd_set, sizeof(bredr_cmd_set)/sizeof(bredr_cmd_set[0])); //aos_cli_register_commands(bredr_cmd_set, sizeof(bredr_cmd_set)/sizeof(bredr_cmd_set[0]));
return 0; return 0;
} }

File diff suppressed because it is too large Load diff

View file

@ -31,9 +31,12 @@
#define NET_BUF_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__) #define NET_BUF_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__)
#define NET_BUF_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__) #define NET_BUF_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__)
#define NET_BUF_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__) #define NET_BUF_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__)
#define NET_BUF_ASSERT(cond) do { if (!(cond)) { \ #define NET_BUF_ASSERT(cond) \
do { \
if (!(cond)) { \
NET_BUF_ERR("assert: '" #cond "' failed"); \ NET_BUF_ERR("assert: '" #cond "' failed"); \
} } while (0) } \
} while (0)
#else #else
#define NET_BUF_DBG(fmt, ...) #define NET_BUF_DBG(fmt, ...)
@ -88,7 +91,9 @@ extern struct net_buf_pool server_pool;
extern struct net_buf_pool data_pool; extern struct net_buf_pool data_pool;
#endif #endif
struct net_buf_pool *_net_buf_pool_list[] = {&hci_cmd_pool, &hci_rx_pool, struct net_buf_pool *_net_buf_pool_list[] = {
&hci_cmd_pool,
&hci_rx_pool,
#if defined(CONFIG_BT_CONN) #if defined(CONFIG_BT_CONN)
&acl_tx_pool, &acl_tx_pool,
@ -179,6 +184,7 @@ static int pool_id(struct net_buf_pool *pool)
break; break;
} }
} }
NET_BUF_ASSERT(index < (sizeof(_net_buf_pool_list) / 4)); NET_BUF_ASSERT(index < (sizeof(_net_buf_pool_list) / 4));
return index; return index;
#else #else
@ -255,6 +261,7 @@ static void mem_pool_data_unref(struct net_buf *buf, u8_t *data)
u8_t *ref_count; u8_t *ref_count;
ref_count = data - 1; ref_count = data - 1;
if (--(*ref_count)) { if (--(*ref_count)) {
return; return;
} }
@ -298,6 +305,7 @@ static u8_t *heap_data_alloc(struct net_buf *buf, size_t *size, s32_t timeout)
u8_t *ref_count; u8_t *ref_count;
ref_count = k_malloc(1 + *size); ref_count = k_malloc(1 + *size);
if (!ref_count) { if (!ref_count) {
return NULL; return NULL;
} }
@ -312,6 +320,7 @@ static void heap_data_unref(struct net_buf *buf, u8_t *data)
u8_t *ref_count; u8_t *ref_count;
ref_count = data - 1; ref_count = data - 1;
if (--(*ref_count)) { if (--(*ref_count)) {
return; return;
} }
@ -391,6 +400,7 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
*/ */
if (pool->uninit_count < pool->buf_count) { if (pool->uninit_count < pool->buf_count) {
buf = k_lifo_get(&pool->free, K_NO_WAIT); buf = k_lifo_get(&pool->free, K_NO_WAIT);
if (buf) { if (buf) {
irq_unlock(key); irq_unlock(key);
goto success; goto success;
@ -407,9 +417,11 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
irq_unlock(key); irq_unlock(key);
#if defined(CONFIG_NET_BUF_LOG) && (CONFIG_NET_BUF_LOG_LEVEL >= LOG_LEVEL_WRN) #if defined(CONFIG_NET_BUF_LOG) && (CONFIG_NET_BUF_LOG_LEVEL >= LOG_LEVEL_WRN)
if (timeout == K_FOREVER) { if (timeout == K_FOREVER) {
u32_t ref = k_uptime_get_32(); u32_t ref = k_uptime_get_32();
buf = k_lifo_get(&pool->free, K_NO_WAIT); buf = k_lifo_get(&pool->free, K_NO_WAIT);
while (!buf) { while (!buf) {
#if defined(CONFIG_NET_BUF_POOL_USAGE) #if defined(CONFIG_NET_BUF_POOL_USAGE)
NET_BUF_WARN("%s():%d: Pool %s low on buffers.", NET_BUF_WARN("%s():%d: Pool %s low on buffers.",
@ -432,9 +444,11 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
} else { } else {
buf = k_lifo_get(&pool->free, timeout); buf = k_lifo_get(&pool->free, timeout);
} }
#else #else
buf = k_lifo_get(&pool->free, timeout); buf = k_lifo_get(&pool->free, timeout);
#endif #endif
if (!buf) { if (!buf) {
NET_BUF_ERR("%s():%d: Failed to get free buffer", func, line); NET_BUF_ERR("%s():%d: Failed to get free buffer", func, line);
return NULL; return NULL;
@ -451,6 +465,7 @@ success:
} }
buf->__buf = data_alloc(buf, &size, timeout); buf->__buf = data_alloc(buf, &size, timeout);
if (!buf->__buf) { if (!buf->__buf) {
NET_BUF_ERR("%s():%d: Failed to allocate data", NET_BUF_ERR("%s():%d: Failed to allocate data",
func, line); func, line);
@ -512,6 +527,7 @@ struct net_buf *net_buf_alloc_with_data(struct net_buf_pool *pool,
#else #else
buf = net_buf_alloc_len(pool, 0, timeout); buf = net_buf_alloc_len(pool, 0, timeout);
#endif #endif
if (!buf) { if (!buf) {
return NULL; return NULL;
} }
@ -537,6 +553,7 @@ struct net_buf *net_buf_get(struct k_fifo *fifo, s32_t timeout)
NET_BUF_DBG("%s():%d: fifo %p timeout %d", func, line, fifo, timeout); NET_BUF_DBG("%s():%d: fifo %p timeout %d", func, line, fifo, timeout);
buf = k_fifo_get(fifo, timeout); buf = k_fifo_get(fifo, timeout);
if (!buf) { if (!buf) {
return NULL; return NULL;
} }
@ -662,20 +679,24 @@ void net_buf_unref(struct net_buf *buf)
#endif #endif
#if defined(CONFIG_NET_BUF_LOG) #if defined(CONFIG_NET_BUF_LOG)
if (!buf->ref) { if (!buf->ref) {
NET_BUF_ERR("%s():%d: buf %p double free", func, line, NET_BUF_ERR("%s():%d: buf %p double free", func, line,
buf); buf);
return; return;
} }
#endif #endif
NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref, NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref,
buf->pool_id, buf->frags); buf->pool_id, buf->frags);
unsigned int key = irq_lock(); /* Added by bouffalo lab, to protect ref decrease */ unsigned int key = irq_lock(); /* Added by bouffalo lab, to protect ref decrease */
if (--buf->ref > 0) { if (--buf->ref > 0) {
irq_unlock(key); /* Added by bouffalo lab */ irq_unlock(key); /* Added by bouffalo lab */
return; return;
} }
irq_unlock(key); /* Added by bouffalo lab */ irq_unlock(key); /* Added by bouffalo lab */
if (buf->__buf) { if (buf->__buf) {
@ -702,10 +723,12 @@ void net_buf_unref(struct net_buf *buf)
buf = frags; buf = frags;
#if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER) #if defined(OPTIMIZE_DATA_EVT_FLOW_FROM_CONTROLLER)
if (pool == &hci_rx_pool && (buf_type == BT_BUF_ACL_IN || adv_report == true)) { if (pool == &hci_rx_pool && (buf_type == BT_BUF_ACL_IN || adv_report == true)) {
bl_handle_queued_msg(); bl_handle_queued_msg();
return; return;
} }
#endif #endif
} }
} }
@ -734,6 +757,7 @@ struct net_buf *net_buf_clone(struct net_buf *buf, s32_t timeout)
pool = net_buf_pool_get(buf->pool_id); pool = net_buf_pool_get(buf->pool_id);
clone = net_buf_alloc_len(pool, 0, timeout); clone = net_buf_alloc_len(pool, 0, timeout);
if (!clone) { if (!clone) {
return NULL; return NULL;
} }
@ -756,6 +780,7 @@ struct net_buf *net_buf_clone(struct net_buf *buf, s32_t timeout)
} }
clone->__buf = data_alloc(clone, &size, timeout); clone->__buf = data_alloc(clone, &size, timeout);
if (!clone->__buf || size < buf->size) { if (!clone->__buf || size < buf->size) {
net_buf_destroy(clone); net_buf_destroy(clone);
return NULL; return NULL;
@ -788,6 +813,7 @@ void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag)
if (parent->frags) { if (parent->frags) {
net_buf_frag_last(frag)->frags = parent->frags; net_buf_frag_last(frag)->frags = parent->frags;
} }
/* Take ownership of the fragment reference */ /* Take ownership of the fragment reference */
parent->frags = frag; parent->frags = frag;
} }
@ -855,6 +881,7 @@ size_t net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src,
/* traverse the fragment chain until len bytes are copied */ /* traverse the fragment chain until len bytes are copied */
copied = 0; copied = 0;
while (frag && len > 0) { while (frag && len > 0) {
to_copy = MIN(len, frag->len - offset); to_copy = MIN(len, frag->len - offset);
memcpy((u8_t *)dst + copied, frag->data + offset, to_copy); memcpy((u8_t *)dst + copied, frag->data + offset, to_copy);
@ -897,6 +924,7 @@ size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
} }
frag = allocate_cb(timeout, user_data); frag = allocate_cb(timeout, user_data);
if (!frag) { if (!frag) {
return added_len; return added_len;
} }

View file

@ -14,6 +14,7 @@ u8_t u8_to_dec(char *buf, u8_t buflen, u8_t value)
while (buflen > 0 && divisor > 0) { while (buflen > 0 && divisor > 0) {
digit = value / divisor; digit = value / divisor;
if (digit != 0 || divisor == 1 || num_digits != 0) { if (digit != 0 || divisor == 1 || num_digits != 0) {
*buf = (char)digit + '0'; *buf = (char)digit + '0';
buf++; buf++;
@ -31,4 +32,3 @@ u8_t u8_to_dec(char *buf, u8_t buflen, u8_t value)
return num_digits; return num_digits;
} }

View file

@ -47,6 +47,7 @@ size_t bin2hex(const u8_t *buf, size_t buflen, char *hex, size_t hexlen)
if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) { if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) {
return 0; return 0;
} }
if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) { if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) {
return 0; return 0;
} }
@ -69,6 +70,7 @@ size_t hex2bin(const char *hex, size_t hexlen, u8_t *buf, size_t buflen)
if (char2hex(hex[0], &dec) < 0) { if (char2hex(hex[0], &dec) < 0) {
return 0; return 0;
} }
buf[0] = dec; buf[0] = dec;
hex++; hex++;
buf++; buf++;
@ -79,11 +81,13 @@ size_t hex2bin(const char *hex, size_t hexlen, u8_t *buf, size_t buflen)
if (char2hex(hex[2 * i], &dec) < 0) { if (char2hex(hex[2 * i], &dec) < 0) {
return 0; return 0;
} }
buf[i] = dec << 4; buf[i] = dec << 4;
if (char2hex(hex[2 * i + 1], &dec) < 0) { if (char2hex(hex[2 * i + 1], &dec) < 0) {
return 0; return 0;
} }
buf[i] += dec; buf[i] += dec;
} }

View file

@ -276,7 +276,6 @@ static inline atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value)
extern atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value); extern atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value);
#endif #endif
/** /**
* @brief Initialize an atomic variable. * @brief Initialize an atomic variable.
* *

View file

@ -21,7 +21,6 @@
extern "C" { extern "C" {
#endif #endif
extern int *__errno(void); extern int *__errno(void);
#define errno (*__errno()) #define errno (*__errno())

View file

@ -19,7 +19,6 @@
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#endif #endif
/* Internal helpers only used by the sys_* APIs further below */ /* Internal helpers only used by the sys_* APIs further below */
#define __bswap_16(x) ((u16_t)((((x) >> 8) & 0xff) | (((x)&0xff) << 8))) #define __bswap_16(x) ((u16_t)((((x) >> 8) & 0xff) | (((x)&0xff) << 8)))
#define __bswap_32(x) ((u32_t)((((x) >> 24) & 0xff) | \ #define __bswap_32(x) ((u32_t)((((x) >> 24) & 0xff) | \
@ -115,10 +114,28 @@
/******************************************************************************** /********************************************************************************
** Macros to get and put bytes to a stream (Little Endian format). ** Macros to get and put bytes to a stream (Little Endian format).
*/ */
#define UINT32_TO_STREAM(p, u32) {*(p)++ = (u8_t)(u32); *(p)++ = (u8_t)((u32) >> 8); *(p)++ = (u8_t)((u32) >> 16); *(p)++ = (u8_t)((u32) >> 24);} #define UINT32_TO_STREAM(p, u32) \
#define UINT24_TO_STREAM(p, u24) {*(p)++ = (u8_t)(u24); *(p)++ = (u8_t)((u24) >> 8); *(p)++ = (u8_t)((u24) >> 16);} { \
#define UINT16_TO_STREAM(p, u16) {*(p)++ = (u8_t)(u16); *(p)++ = (u8_t)((u16) >> 8);} *(p)++ = (u8_t)(u32); \
#define UINT8_TO_STREAM(p, u8) {*(p)++ = (u8_t)(u8);} *(p)++ = (u8_t)((u32) >> 8); \
*(p)++ = (u8_t)((u32) >> 16); \
*(p)++ = (u8_t)((u32) >> 24); \
}
#define UINT24_TO_STREAM(p, u24) \
{ \
*(p)++ = (u8_t)(u24); \
*(p)++ = (u8_t)((u24) >> 8); \
*(p)++ = (u8_t)((u24) >> 16); \
}
#define UINT16_TO_STREAM(p, u16) \
{ \
*(p)++ = (u8_t)(u16); \
*(p)++ = (u8_t)((u16) >> 8); \
}
#define UINT8_TO_STREAM(p, u8) \
{ \
*(p)++ = (u8_t)(u8); \
}
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define sys_le16_to_cpu(val) __bswap_16(val) #define sys_le16_to_cpu(val) __bswap_16(val)
@ -136,10 +153,28 @@
/******************************************************************************** /********************************************************************************
** Macros to get and put bytes to a stream (Big Endian format) ** Macros to get and put bytes to a stream (Big Endian format)
*/ */
#define UINT32_TO_STREAM(p, u32) {*(p)++ = (u8_t)((u32) >> 24); *(p)++ = (u8_t)((u32) >> 16); *(p)++ = (u8_t)((u32) >> 8); *(p)++ = (u8_t)(u32); } #define UINT32_TO_STREAM(p, u32) \
#define UINT24_TO_STREAM(p, u24) {*(p)++ = (u8_t)((u24) >> 16); *(p)++ = (u8_t)((u24) >> 8); *(p)++ = (u8_t)(u24);} { \
#define UINT16_TO_STREAM(p, u16) {*(p)++ = (u8_t)((u16) >> 8); *(p)++ = (u8_t)(u16);} *(p)++ = (u8_t)((u32) >> 24); \
#define UINT8_TO_STREAM(p, u8) {*(p)++ = (u8_t)(u8);} *(p)++ = (u8_t)((u32) >> 16); \
*(p)++ = (u8_t)((u32) >> 8); \
*(p)++ = (u8_t)(u32); \
}
#define UINT24_TO_STREAM(p, u24) \
{ \
*(p)++ = (u8_t)((u24) >> 16); \
*(p)++ = (u8_t)((u24) >> 8); \
*(p)++ = (u8_t)(u24); \
}
#define UINT16_TO_STREAM(p, u16) \
{ \
*(p)++ = (u8_t)((u16) >> 8); \
*(p)++ = (u8_t)(u16); \
}
#define UINT8_TO_STREAM(p, u8) \
{ \
*(p)++ = (u8_t)(u8); \
}
#else #else
#error "Unknown byte order" #error "Unknown byte order"

View file

@ -80,8 +80,7 @@ typedef struct _dnode sys_dnode_t;
* it contains the starting node, or NULL to start from the head * it contains the starting node, or NULL to start from the head
*/ */
#define SYS_DLIST_ITERATE_FROM_NODE(__dl, __dn) \ #define SYS_DLIST_ITERATE_FROM_NODE(__dl, __dn) \
for (__dn = __dn ? sys_dlist_peek_next_no_check(__dl, __dn) \ for (__dn = __dn ? sys_dlist_peek_next_no_check(__dl, __dn) : sys_dlist_peek_head(__dl); \
: sys_dlist_peek_head(__dl); \
__dn; \ __dn; \
__dn = sys_dlist_peek_next(__dl, __dn)) __dn = sys_dlist_peek_next(__dl, __dn))
@ -136,7 +135,8 @@ typedef struct _dnode sys_dnode_t;
*/ */
#define SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n) \ #define SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n) \
((__cn) ? SYS_DLIST_CONTAINER(sys_dlist_peek_next(__dl, &(__cn->__n)), \ ((__cn) ? SYS_DLIST_CONTAINER(sys_dlist_peek_next(__dl, &(__cn->__n)), \
__cn, __n) : NULL) __cn, __n) : \
NULL)
/** /**
* @brief Provide the primitive to iterate on a list under a container * @brief Provide the primitive to iterate on a list under a container
@ -173,7 +173,8 @@ typedef struct _dnode sys_dnode_t;
*/ */
#define SYS_DLIST_FOR_EACH_CONTAINER_SAFE(__dl, __cn, __cns, __n) \ #define SYS_DLIST_FOR_EACH_CONTAINER_SAFE(__dl, __cn, __cns, __n) \
for (__cn = SYS_DLIST_PEEK_HEAD_CONTAINER(__dl, __cn, __n), \ for (__cn = SYS_DLIST_PEEK_HEAD_CONTAINER(__dl, __cn, __n), \
__cns = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n); __cn; \ __cns = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n); \
__cn; \
__cn = __cns, \ __cn = __cns, \
__cns = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n)) __cns = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n))
@ -191,7 +192,13 @@ static inline void sys_dlist_init(sys_dlist_t *list)
list->tail = (sys_dnode_t *)list; list->tail = (sys_dnode_t *)list;
} }
#define SYS_DLIST_STATIC_INIT(ptr_to_list) {{(ptr_to_list)}, {(ptr_to_list)}} #define SYS_DLIST_STATIC_INIT(ptr_to_list) \
{ \
{ (ptr_to_list) }, \
{ \
(ptr_to_list) \
} \
}
/** /**
* @brief check if a node is the list's head * @brief check if a node is the list's head
@ -443,6 +450,7 @@ static inline void sys_dlist_insert_at(sys_dlist_t *list, sys_dnode_t *node,
while (pos && !cond(pos, data)) { while (pos && !cond(pos, data)) {
pos = sys_dlist_peek_next(list, pos); pos = sys_dlist_peek_next(list, pos);
} }
sys_dlist_insert_before(list, pos, node); sys_dlist_insert_before(list, pos, node);
} }
} }

View file

@ -14,7 +14,6 @@
#include <zephyr.h> #include <zephyr.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View file

@ -24,7 +24,6 @@
extern "C" { extern "C" {
#endif #endif
struct _snode { struct _snode {
struct _snode *next; struct _snode *next;
}; };
@ -78,8 +77,7 @@ typedef struct _slist sys_slist_t;
* it contains the starting node, or NULL to start from the head * it contains the starting node, or NULL to start from the head
*/ */
#define SYS_SLIST_ITERATE_FROM_NODE(__sl, __sn) \ #define SYS_SLIST_ITERATE_FROM_NODE(__sl, __sn) \
for (__sn = __sn ? sys_slist_peek_next_no_check(__sn) \ for (__sn = __sn ? sys_slist_peek_next_no_check(__sn) : sys_slist_peek_head(__sl); \
: sys_slist_peek_head(__sl); \
__sn; \ __sn; \
__sn = sys_slist_peek_next(__sn)) __sn = sys_slist_peek_next(__sn))
@ -144,7 +142,8 @@ typedef struct _slist sys_slist_t;
#define SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n) \ #define SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n) \
((__cn) ? SYS_SLIST_CONTAINER(sys_slist_peek_next(&((__cn)->__n)), \ ((__cn) ? SYS_SLIST_CONTAINER(sys_slist_peek_next(&((__cn)->__n)), \
__cn, __n) : NULL) __cn, __n) : \
NULL)
/** /**
* @brief Provide the primitive to iterate on a list under a container * @brief Provide the primitive to iterate on a list under a container
@ -181,7 +180,8 @@ typedef struct _slist sys_slist_t;
*/ */
#define SYS_SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \ #define SYS_SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \
for (__cn = SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n), \ for (__cn = SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n), \
__cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n); __cn; \ __cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n); \
__cn; \
__cn = __cns, __cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n)) __cn = __cns, __cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n))
/** /**
@ -195,7 +195,10 @@ static inline void sys_slist_init(sys_slist_t *list)
list->tail = NULL; list->tail = NULL;
} }
#define SYS_SLIST_STATIC_INIT(ptr_to_list) {NULL, NULL} #define SYS_SLIST_STATIC_INIT(ptr_to_list) \
{ \
NULL, NULL \
}
/** /**
* @brief Test if the given list is empty * @brief Test if the given list is empty
@ -378,6 +381,7 @@ static inline sys_snode_t *sys_slist_get_not_empty(sys_slist_t *list)
sys_snode_t *node = list->head; sys_snode_t *node = list->head;
list->head = node->next; list->head = node->next;
if (list->tail == node) { if (list->tail == node) {
list->tail = list->head; list->tail = list->head;
} }
@ -448,7 +452,8 @@ static inline bool sys_slist_find_and_remove(sys_slist_t *list,
sys_snode_t *prev = NULL; sys_snode_t *prev = NULL;
sys_snode_t *test; sys_snode_t *test;
SYS_SLIST_FOR_EACH_NODE(list, test) { SYS_SLIST_FOR_EACH_NODE(list, test)
{
if (test == node) { if (test == node) {
sys_slist_remove(list, prev, node); sys_slist_remove(list, prev, node);
return true; return true;
@ -460,7 +465,6 @@ static inline bool sys_slist_find_and_remove(sys_slist_t *list,
return false; return false;
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -34,6 +34,7 @@ static inline size_t stack_unused_space_get(const char *stack, size_t size)
* that correct Kconfig option is used. * that correct Kconfig option is used.
*/ */
#if defined(STACK_GROWS_UP) #if defined(STACK_GROWS_UP)
for (i = size - 1; i >= 0; i--) { for (i = size - 1; i >= 0; i--) {
if ((unsigned char)stack[i] == 0xaa) { if ((unsigned char)stack[i] == 0xaa) {
unused++; unused++;
@ -41,7 +42,9 @@ static inline size_t stack_unused_space_get(const char *stack, size_t size)
break; break;
} }
} }
#else #else
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if ((unsigned char)stack[i] == 0xaa) { if ((unsigned char)stack[i] == 0xaa) {
unused++; unused++;
@ -49,6 +52,7 @@ static inline size_t stack_unused_space_get(const char *stack, size_t size)
break; break;
} }
} }
#endif #endif
return unused; return unused;
} }

Some files were not shown because too many files have changed in this diff Show more