diff --git a/platform/gpio/gpio_bes.c b/platform/gpio/gpio_bes.c index 6699d51fc409b4f88f6efdcb62d53998b51be38d..5f93f7caa79b27b6e14b057c005b19e816375c3d 100644 --- a/platform/gpio/gpio_bes.c +++ b/platform/gpio/gpio_bes.c @@ -69,26 +69,10 @@ typedef int32_t (*oem_gpio_irq_handler_t)(uint16_t gpio, void *data); #define OCTALNUM 8 static struct GpioCntlr g_gpioCntlr; -struct OemGpioIrqHandler { - uint8_t port; - GpioIrqFunc func; - void *arg; -}; enum HAL_GPIO_PIN_T g_gpioPinReflectionMap[HAL_GPIO_PIN_LED_NUM] = {0}; -static struct HAL_GPIO_IRQ_CFG_T g_gpioIrqCfg[HAL_GPIO_PIN_LED_NUM] = {0}; - -static struct HAL_GPIO_IRQ_CFG_T HalGpioGetIrqConfig(enum HAL_GPIO_PIN_T pin) -{ - struct HAL_GPIO_IRQ_CFG_T irqCfg; - irqCfg.irq_enable = g_gpioIrqCfg[pin].irq_enable; - irqCfg.irq_debounce = g_gpioIrqCfg[pin].irq_debounce; - irqCfg.irq_type = g_gpioIrqCfg[pin].irq_type; - irqCfg.irq_polarity = g_gpioIrqCfg[pin].irq_polarity; - - return irqCfg; -} +static struct HAL_GPIO_IRQ_CFG_T g_gpioIrqCfg[HAL_GPIO_PIN_LED_NUM] = {0}; static void OemGpioIrqHdl(enum HAL_GPIO_PIN_T pin) { @@ -271,12 +255,10 @@ static uint32_t GetGpioDeviceResource( return HDF_FAILURE; } } - return HDF_SUCCESS; } #endif - static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObject *device) { int32_t ret; @@ -302,12 +284,12 @@ static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObj ret = GetGpioDeviceResource(gpioDevice, device->property); #endif if (ret != HDF_SUCCESS) { - (void)OsalMemFree(gpioDevice); + OsalMemFree(gpioDevice); return HDF_FAILURE; } gpioCntlr->count = gpioDevice->resource.pinNum; - + gpioCntlr->priv = (void *)gpioDevice; return HDF_SUCCESS; } @@ -363,7 +345,8 @@ static void GpioDriverRelease(struct HdfDeviceObject *device) return HDF_DEV_ERR_NO_DEVICE_SERVICE; } - (void)OsalMemFree(gpioCntlr->priv); + gpioCntlr->ops = NULL; + OsalMemFree(gpioCntlr->priv); gpioCntlr->count = 0; } @@ -388,7 +371,7 @@ static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val uint16_t value; uint16_t halGpio = g_gpioPinReflectionMap[gpio]; if ((enum HAL_GPIO_PIN_T)halGpio >= HAL_GPIO_PIN_LED_NUM) { - HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, halGpio); + HDF_LOGE("%s %d, error pin:%hu", __func__, __LINE__, halGpio); return HDF_ERR_NOT_SUPPORT; } @@ -403,7 +386,7 @@ static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t di (void)cntlr; uint16_t halGpio = g_gpioPinReflectionMap[gpio]; if ((enum HAL_GPIO_PIN_T)halGpio >= HAL_GPIO_PIN_LED_NUM) { - HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, halGpio); + HDF_LOGE("%s %d, error pin:%hu", __func__, __LINE__, halGpio); return HDF_ERR_NOT_SUPPORT; } @@ -418,7 +401,7 @@ static int32_t GpioDevGetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *d uint16_t value; uint16_t halGpio = g_gpioPinReflectionMap[gpio]; if ((enum HAL_GPIO_PIN_T)halGpio >= HAL_GPIO_PIN_LED_NUM) { - HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, gpio); + HDF_LOGE("%s %d, error pin:%hu", __func__, __LINE__, gpio); return HDF_ERR_NOT_SUPPORT; } @@ -442,10 +425,16 @@ static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mo } else if ((mode == OSAL_IRQF_TRIGGER_HIGH) || (mode == OSAL_IRQF_TRIGGER_LOW)) { g_gpioIrqCfg[pin].irq_type = HAL_GPIO_IRQ_TYPE_LEVEL_SENSITIVE; } else { - HDF_LOGE("%s %d, error mode:%d", __func__, __LINE__, mode); + HDF_LOGE("%s %d, error mode:%hu", __func__, __LINE__, mode); return HDF_ERR_NOT_SUPPORT; } + if (mode == OSAL_IRQF_TRIGGER_HIGH || mode == OSAL_IRQF_TRIGGER_RISING) { + mode = HAL_GPIO_IRQ_POLARITY_HIGH_RISING; + } else { + mode = HAL_GPIO_IRQ_POLARITY_LOW_FALLING; + } + g_gpioIrqCfg[pin].irq_polarity = mode; return HDF_SUCCESS; diff --git a/platform/i2c/i2c_bes.c b/platform/i2c/i2c_bes.c index af822f714382b72a3a0781ecdec2a00dbdaa51fb..39597f6601333a40bbffe095b5006c4ee704a9d2 100644 --- a/platform/i2c/i2c_bes.c +++ b/platform/i2c/i2c_bes.c @@ -48,25 +48,9 @@ struct I2cMethod g_i2cHostMethod = { .transfer = i2cHostTransfer, }; -int32_t InitI2cDevice(struct I2cDevice *device) +static void I2cDeviceIomuxInit(uint32_t i2cId, struct I2cResource *resource) { - int32_t ret = -1; - uint32_t i2cPort; - struct I2cResource *resource = NULL; - struct HAL_I2C_CONFIG_T *i2cConfig = NULL; - if (device == NULL) { - HDF_LOGE("device is NULL\r\n"); - return HDF_ERR_INVALID_PARAM; - } - - resource = &device->resource; - if (resource == NULL) { - HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_PARAM; - } - - i2cConfig = &device->i2cCfg; - if (i2cConfig == NULL) { + if (i2cId > HAL_I2C_ID_NUM || resource == NULL) { HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } @@ -76,63 +60,51 @@ int32_t InitI2cDevice(struct I2cDevice *device) {0, 0, HAL_IOMUX_PIN_VOLTAGE_VIO, HAL_IOMUX_PIN_PULLUP_ENABLE}, }; - device->port = resource->port; - i2cPort = device->port; - if (i2cPort > HAL_I2C_ID_NUM) { - HDF_LOGE("i2c port %u not support\r\n", i2cPort); - return HDF_ERR_NOT_SUPPORT; - } - - if (i2cPort == 0) { +#ifdef LOSCFG_SOC_SERIES_BES2600 + if (i2cId == 0) { pinMuxI2c[0].function = HAL_IOMUX_FUNC_I2C_M0_SCL; pinMuxI2c[1].function = HAL_IOMUX_FUNC_I2C_M0_SDA; } else { pinMuxI2c[0].function = HAL_IOMUX_FUNC_I2C_M1_SCL; pinMuxI2c[1].function = HAL_IOMUX_FUNC_I2C_M1_SDA; } - - if (OsalMutexInit(&device->mutex) != HDF_SUCCESS) { - HDF_LOGE("%s %d OsalMutexInit fail\r\n", __func__, __LINE__); - return HDF_FAILURE; - } - - if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { - HDF_LOGE("%s %d osMutexWait fail\r\n", __func__, __LINE__); - return HDF_ERR_TIMEOUT; +#elif defined (LOSCFG_SOC_SERIES_BES2700) + if (i2cId == 0) { + pinMuxI2c[0].function = HAL_IOMUX_FUNC_SYS_I2C_M0_SCL; + pinMuxI2c[1].function = HAL_IOMUX_FUNC_SYS_I2C_M0_SDA; + } else { + pinMuxI2c[0].function = HAL_IOMUX_FUNC_SYS_I2C_M1_SCL; + pinMuxI2c[1].function = HAL_IOMUX_FUNC_SYS_I2C_M1_SDA; } - +#endif pinMuxI2c[0].pin = resource->sclPin; pinMuxI2c[1].pin = resource->sdaPin; hal_iomux_init(pinMuxI2c, ARRAY_SIZE(pinMuxI2c)); - - ret = hal_i2c_open(i2cPort, i2cConfig); - if (ret == HDF_SUCCESS) { - HDF_LOGD("open %u i2c succ.\r\n", i2cPort); - } - OsalMutexUnlock(&device->mutex); - return ret; } -static int32_t HostRestI2cDevice(struct I2cDevice *device) +int32_t InitI2cDevice(struct I2cDevice *device) { - int32_t ret = -1; + int32_t ret; + uint32_t i2cPort; struct I2cResource *resource = NULL; struct HAL_I2C_CONFIG_T *i2cConfig = NULL; - uint32_t i2cPort; if (device == NULL) { - HDF_LOGE("%s %d device is null\r\n", __func__, __LINE__); + HDF_LOGE("device is NULL\r\n"); return HDF_ERR_INVALID_PARAM; } + resource = &device->resource; if (resource == NULL) { HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_OBJECT; + return HDF_ERR_INVALID_PARAM; } + i2cConfig = &device->i2cCfg; if (i2cConfig == NULL) { HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_OBJECT; + return HDF_ERR_INVALID_PARAM; } + device->port = resource->port; i2cPort = device->port; if (i2cPort > HAL_I2C_ID_NUM) { @@ -140,15 +112,28 @@ static int32_t HostRestI2cDevice(struct I2cDevice *device) return HDF_ERR_NOT_SUPPORT; } - hal_i2c_close(i2cPort); + if (OsalMutexInit(&device->mutex) != HDF_SUCCESS) { + HDF_LOGE("%s %d OsalMutexInit fail\r\n", __func__, __LINE__); + return HDF_FAILURE; + } + + if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { + HDF_LOGE("%s %d osMutexWait fail\r\n", __func__, __LINE__); + return HDF_ERR_TIMEOUT; + } + + I2cDeviceIomuxInit(i2cPort, resource); + ret = hal_i2c_open(i2cPort, i2cConfig); if (ret == HDF_SUCCESS) { HDF_LOGD("open %u i2c succ.\r\n", i2cPort); } + OsalMutexUnlock(&device->mutex); return ret; } + #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO -#define I2C_FIND_CONFIG(node, name, resource) \ +#define I2C_FIND_CONFIG(node, name, resource, result, tempPin) \ do { \ if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ resource->port = HCS_PROP(node, port); \ @@ -161,34 +146,35 @@ static int32_t HostRestI2cDevice(struct I2cDevice *device) resource->useDma = HCS_PROP(node, useDma); \ resource->useSync = HCS_PROP(node, useSync); \ resource->asMaster = HCS_PROP(node, asMaster); \ + result = HDF_SUCCESS; \ break; \ } \ } while (0) - #define PLATFORM_I2C_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), i2c_config) static uint32_t GetI2cDeviceResource(struct I2cDevice *device, const char *deviceMatchAttr) { - uint32_t tempPin = 0; + uint32_t tempPin; + int32_t result = HDF_FAILURE; struct I2cResource *resource = NULL; - if (device == NULL) { - HDF_LOGE("device or resourceNode is NULL\r\n"); + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL"); return HDF_ERR_INVALID_PARAM; } resource = &device->resource; - if (resource == NULL) { - HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_OBJECT; +#if HCS_NODE_EXISTS(PLATFORM_I2C_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_I2C_CONFIG, I2C_FIND_CONFIG, deviceMatchAttr, resource, result, tempPin); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL", deviceMatchAttr); } - - HCS_FOREACH_CHILD_VARGS(PLATFORM_I2C_CONFIG, I2C_FIND_CONFIG, deviceMatchAttr, resource); - return HDF_SUCCESS; + return result; } #else static uint32_t GetI2cDeviceResource(struct I2cDevice *device, const struct DeviceResourceNode *resourceNode) { - uint32_t tempPin = 0; + uint32_t tempPin; struct I2cResource *resource = NULL; struct DeviceResourceIface *dri = NULL; if (device == NULL || resourceNode == NULL) { diff --git a/platform/i2c/i2c_bes.h b/platform/i2c/i2c_bes.h index aa62df769e95091ebf7e8f85854ca9827836a83e..404e8ff60c0cae6b752044989078ca4c890549d3 100644 --- a/platform/i2c/i2c_bes.h +++ b/platform/i2c/i2c_bes.h @@ -11,7 +11,9 @@ #include "hal_i2c.h" #include "hal_iomux.h" +#ifdef CHIP_BEST2003 #include "hal_iomux_best2003.h" +#endif #include "device_resource_if.h" #include "osal_mutex.h" #ifdef __cplusplus diff --git a/platform/pwm/pwm_bes.c b/platform/pwm/pwm_bes.c index 31348f8e0b12bf2106de99b8d151ee1493e0b800..386e7809664d24555486cdc11c5d1bb10eb1055d 100644 --- a/platform/pwm/pwm_bes.c +++ b/platform/pwm/pwm_bes.c @@ -12,7 +12,6 @@ #include "hdf_device_desc.h" #include "hal_trace.h" #include "hal_timer.h" -#include "hal_iomux.h" #include "pwm_core.h" #include "hdf_log.h" #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO @@ -22,7 +21,13 @@ #include "device_resource_if.h" #endif +#if defined (LOSCFG_SOC_SERIES_BES2700) +#define PWM_MAX_FUNCTION 4 +#elif defined (LOSCFG_SOC_SERIES_BES2600) +#include "hal_iomux.h" #define PWM_MAX_FUNCTION 8 +#endif + #define UNTIL_NAN0SECONDS 1000000000 #define PERCENT 100 #define DEC_TEN 10 @@ -33,10 +38,12 @@ static uint32_t g_pwmFunction[PWM_MAX_FUNCTION] = { HAL_IOMUX_FUNC_PWM1, HAL_IOMUX_FUNC_PWM2, HAL_IOMUX_FUNC_PWM3, +#if defined (LOSCFG_SOC_SERIES_BES2600) HAL_IOMUX_FUNC_PWM4, HAL_IOMUX_FUNC_PWM5, HAL_IOMUX_FUNC_PWM6, HAL_IOMUX_FUNC_PWM7, +#endif }; static int32_t PwmDevSetConfig(struct PwmDev *pwm, struct PwmConfig *config); @@ -54,7 +61,7 @@ static int InitPwmDevice(struct PwmDev *host) struct PwmDevice *pwmDevice = NULL; struct PwmResource *resource = NULL; if (host == NULL || host->priv == NULL) { - HDF_LOGE("%s: invaild parameter\r\n", __func__); + HDF_LOGE("%s: invalid parameter\r\n", __func__); return HDF_ERR_INVALID_PARAM; } pwmDevice = (struct PwmDevice *)host->priv; @@ -82,8 +89,34 @@ static int InitPwmDevice(struct PwmDev *host) } #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO -static uint32_t GetPwmDeviceResource(struct PwmDevice *device) +#define PWM_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + tempPin = HCS_PROP(node, pwmPin); \ + resource->pwmPin = ((tempPin / DEC_TEN) * PIN_GROUP_NUM) + (tempPin % DEC_TEN); \ + resource->pwmId = HCS_PROP(node, pwmId); \ + result = HDF_SUCCESS; \ + break; \ + } \ + } while (0) +#define PLATFORM_PWM_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), pwm_config) +static uint32_t GetPwmDeviceResource(struct PwmDevice *device, const char *deviceMatchAttr) { + uint32_t tempPin; + int32_t result = HDF_FAILURE; + struct PwmResource *resource = NULL; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("%s: device or deviceMatchAttr is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; +#if HCS_NODE_EXISTS(PLATFORM_PWM_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_PWM_CONFIG, PWM_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } + return result; } #else static uint32_t GetPwmDeviceResource( @@ -142,7 +175,7 @@ static int32_t AttachPwmDevice(struct PwmDev *host, struct HdfDeviceObject *devi return HDF_ERR_MALLOC_FAIL; } #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO - ret = GetPwmDeviceResource(pwmDevice); + ret = GetPwmDeviceResource(pwmDevice, device->deviceMatchAttr); #else ret = GetPwmDeviceResource(pwmDevice, device->property); #endif diff --git a/platform/pwm/pwm_bes.h b/platform/pwm/pwm_bes.h index fedef7ec452a665b51be9392b731c6a487ef92bf..98af98e78b414e3edfb740aaf21cbe462eb6469f 100644 --- a/platform/pwm/pwm_bes.h +++ b/platform/pwm/pwm_bes.h @@ -11,7 +11,9 @@ #include "hal_pwm.h" #include "hal_gpio.h" +#ifdef CHIP_BEST2003 #include "hal_iomux.h" +#endif #include "pwm_core.h" #ifdef __cplusplus diff --git a/platform/spi/spi_bes.c b/platform/spi/spi_bes.c index e027e7194ca201f871ed64791891273c8b04485e..5a83d949b669c1464941a2aa8b66eedb9f68fd30 100644 --- a/platform/spi/spi_bes.c +++ b/platform/spi/spi_bes.c @@ -37,10 +37,17 @@ static void Spi1DmaIrq(int error); static struct SPI_CTX_OBJ_T spiCtx[MAX_SPI_NUMBER] = { { .spiPinCS0 = 0, +#if defined (LOSCFG_SOC_SERIES_BES2700) + .spiFunDI0 = HAL_IOMUX_FUNC_SYS_SPI_DI0, + .spiFunCLK = HAL_IOMUX_FUNC_SYS_SPI_CLK, + .spiFunCS0 = HAL_IOMUX_FUNC_SYS_SPI_CS0, + .spiFunDIO = HAL_IOMUX_FUNC_SYS_SPI_DIO, +#elif defined (LOSCFG_SOC_SERIES_BES2600) .spiFunDI0 = HAL_IOMUX_FUNC_SPI_DI0, .spiFunCLK = HAL_IOMUX_FUNC_SPI_CLK, .spiFunCS0 = HAL_IOMUX_FUNC_SPI_CS0, .spiFunDIO = HAL_IOMUX_FUNC_SPI_DIO, +#endif .sem = { NULL }, .mutex = { NULL }, .SpiOpen = hal_spi_open, @@ -53,10 +60,17 @@ static struct SPI_CTX_OBJ_T spiCtx[MAX_SPI_NUMBER] = { }, { .spiPinCS0 = 0, +#if defined (LOSCFG_SOC_SERIES_BES2700) + .spiFunDI0 = HAL_IOMUX_FUNC_SYS_SPILCD_DI0, + .spiFunCLK = HAL_IOMUX_FUNC_SYS_SPILCD_CLK, + .spiFunCS0 = HAL_IOMUX_FUNC_SYS_SPILCD_CS0, + .spiFunDIO = HAL_IOMUX_FUNC_SYS_SPILCD_DIO, +#elif defined (LOSCFG_SOC_SERIES_BES2600) .spiFunDI0 = HAL_IOMUX_FUNC_SPILCD_DI0, .spiFunCLK = HAL_IOMUX_FUNC_SPILCD_CLK, .spiFunCS0 = HAL_IOMUX_FUNC_SPILCD_CS0, .spiFunDIO = HAL_IOMUX_FUNC_SPILCD_DIO, +#endif .sem = { NULL }, .mutex = { NULL }, .SpiOpen = hal_spilcd_open, @@ -107,12 +121,12 @@ static void SpiIomuxInit(struct SpiDevice *spiDevice) resource = &spiDevice->resource; if (resource == NULL) { HDF_LOGE("resource is null\r\n"); - return HDF_ERR_INVALID_OBJECT; + return; } spiDevCfg = &spiDevice->spiDevCfg; if (spiDevCfg == NULL) { HDF_LOGE("resource is null\r\n"); - return HDF_ERR_INVALID_OBJECT; + return; } spiDevCfg->rate = resource->speed; @@ -135,14 +149,55 @@ static void SpiIomuxInit(struct SpiDevice *spiDevice) pinMuxSpi[SPI_PIN_2].function = spiCtx[spiPort].spiFunCS0; pinMuxSpi[SPI_PIN_3].function = spiCtx[spiPort].spiFunDIO; - if (spiDevCfg->rate > MAX_SPI_SPEED) { - hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_1].pin, DRIVER_LEVEL); - hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_3].pin, DRIVER_LEVEL); - } + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_0].pin, DRIVER_LEVEL); + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_1].pin, DRIVER_LEVEL); + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_2].pin, DRIVER_LEVEL); + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_3].pin, DRIVER_LEVEL); hal_iomux_init(pinMuxSpi, ARRAY_SIZE(pinMuxSpi)); } +static int32_t SpiDevCfgInit(struct HAL_SPI_CFG_T *spiDevCfg, struct SpiResource *resource) +{ + if (spiDevCfg == NULL || resource == NULL) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + switch (resource->mode) { + case SPI_WORK_MODE_0: + spiDevCfg->clk_delay_half = false; + spiDevCfg->clk_polarity = false; + break; + case SPI_WORK_MODE_1: + spiDevCfg->clk_delay_half = true; + spiDevCfg->clk_polarity = false; + break; + case SPI_WORK_MODE_2: + spiDevCfg->clk_delay_half = false; + spiDevCfg->clk_polarity = true; + break; + case SPI_WORK_MODE_3: + spiDevCfg->clk_delay_half = true; + spiDevCfg->clk_polarity = true; + break; + default: + spiDevCfg->clk_delay_half = true; + spiDevCfg->clk_polarity = true; + } + spiDevCfg->slave = 0; + if (resource->transmode == SPI_TRANSFER_DMA) { + spiDevCfg->dma_rx = true; + spiDevCfg->dma_tx = true; + } else { + spiDevCfg->dma_rx = false; + spiDevCfg->dma_tx = false; + } + spiDevCfg->cs = 0; + spiDevCfg->rx_bits = resource->dataSize; + spiDevCfg->tx_bits = resource->dataSize; + spiDevCfg->rx_frame_bits = 0; + return HDF_SUCCESS; +} /** * Spi send * @@ -160,7 +215,7 @@ static void SpiIomuxInit(struct SpiDevice *spiDevice) #endif int32_t HalSpiSend(struct SpiDevice *spiDevice, const uint8_t *data, uint16_t size, uint32_t timeOut) { - int32_t ret = 0; + int32_t ret; uint32_t spiId; uint32_t len = size; struct SpiResource *resource = NULL; @@ -182,7 +237,9 @@ int32_t HalSpiSend(struct SpiDevice *spiDevice, const uint8_t *data, uint16_t si HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); return HDF_ERR_TIMEOUT; } - +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif if (resource->transmode == SPI_TRANSFER_DMA) { ret = spiCtx[spiId].SpiDmaSend(data, len, spiCtx[spiId].SpiDmaIrq); if (OsalSemWait(&spiCtx[spiId].sem, timeOut) != HDF_SUCCESS) { @@ -217,11 +274,11 @@ OUT: * * @return 0 : on success, EIO : if the SPI device could not be initialised */ -int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, uint32_t timeOut) +static int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, uint32_t timeOut) { - int32_t ret = 0; - uint32_t len = size; - uint32_t remainder = 0; + int32_t ret; + int32_t len = (int32_t)size; + uint32_t remainder; int32_t status = HDF_FAILURE; uint8_t *cmd = NULL; uint32_t spiId; @@ -237,50 +294,46 @@ int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, ui HDF_LOGE("resource is null\r\n"); return HDF_ERR_INVALID_OBJECT; } - cmd = (uint8_t *)OsalMemAlloc(len); - if (cmd == NULL) { - HDF_LOGE("%s OsalMemAlloc size %ld error\r\n", __FUNCTION__, len); - return HDF_ERR_MALLOC_FAIL; - } - - memset_s(cmd, len, 0, len); + cmd = (uint8_t *)0x20020000; // 0x20020000 : non secure sram base. tx buf is useless, use sram instead of malloc. status = OsalMutexLock(&spiCtx[spiId].mutex); if (HDF_SUCCESS != status) { HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); - OsalMemFree(cmd); return HDF_ERR_TIMEOUT; } - remainder = len <= SPI_DMA_MAX ? len : SPI_DMA_MAX; - - if (resource->transmode == SPI_TRANSFER_DMA) { - ret = spiCtx[spiId].SpiDmaRecv(cmd, data, remainder, spiCtx[spiId].SpiDmaIrq); - if (OsalSemWait(&spiCtx[spiId].sem, timeOut) <= 0) { - HDF_LOGE("SPI Read timeOut!\r\n"); - goto OUT; +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif + do { + remainder = len <= SPI_DMA_MAX ? len : SPI_DMA_MAX; + if (resource->transmode == SPI_TRANSFER_DMA) { + ret = spiCtx[spiId].SpiDmaRecv(cmd, data, remainder, spiCtx[spiId].SpiDmaIrq); + if (OsalSemWait(&spiCtx[spiId].sem, timeOut) <= 0) { + HDF_LOGE("SPI Read timeOut!\r\n"); + goto OUT; + } + } else { + ret = spiCtx[spiId].SpiRecv(cmd, data, remainder); } - } else { - ret = spiCtx[spiId].SpiRecv(cmd, data, remainder); - } - len -= remainder; - data += remainder; + len -= remainder; + data += remainder; - if (ret) { - HDF_LOGE("spi tail fail %ld, size %ld\r\n", ret, len); - goto OUT; - } + if (ret != 0) { + HDF_LOGE("spi tail fail %ld, size %ld\r\n", ret, len); + goto OUT; + } + } while (len); OUT: OsalMutexUnlock(&spiCtx[spiId].mutex); - OsalMemFree(cmd); return ret; } #ifdef HalSpiSendRecv #undef HalSpiSendRecv #endif -int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t txSize, uint8_t *rxData, - uint16_t rxSize, uint32_t timeOut) +static int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t txSize, uint8_t *rxData, + uint16_t rxSize) { int32_t ret; int32_t status; @@ -294,13 +347,15 @@ int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t tx resource = &spiDevice->resource; status = OsalMutexLock(&spiCtx[spiId].mutex); if (HDF_SUCCESS != status) { - HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); + HDF_LOGE("%s OsalMutexLock error = 0x%X!\r\n", __func__, status); return HDF_ERR_TIMEOUT; } - +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif if (resource->transmode == SPI_TRANSFER_DMA) { ret = spiCtx[spiId].SpiDmaRecv(txData, rxData, rxSize, spiCtx[spiId].SpiDmaIrq); - if (OsalSemWait(&spiCtx[spiId].sem, timeOut) <= 0) { + if (OsalSemWait(&spiCtx[spiId].sem, TIMEOUT) <= 0) { HDF_LOGE("%s:SPI Read timeOut!\r\n", __func__); goto OUT; } @@ -309,7 +364,6 @@ int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t tx } if (ret) { HDF_LOGE("spi dma tail fail %d\r\n", ret); - goto OUT; } OUT: OsalMutexUnlock(&spiCtx[spiId].mutex); @@ -319,6 +373,7 @@ OUT: static int32_t InitSpiDevice(struct SpiDevice *spiDevice) { uint32_t spiPort; + int32_t ret; struct HAL_SPI_CFG_T *spiDevCfg = NULL; struct SpiResource *resource = NULL; if (spiDevice == NULL) { @@ -331,40 +386,11 @@ static int32_t InitSpiDevice(struct SpiDevice *spiDevice) spiPort = spiDevice->spiId; SpiIomuxInit(spiDevice); - - switch (resource->mode) { - case SPI_WORK_MODE_0: - spiDevCfg->clk_delay_half = false; - spiDevCfg->clk_polarity = false; - break; - case SPI_WORK_MODE_1: - spiDevCfg->clk_delay_half = true; - spiDevCfg->clk_polarity = false; - break; - case SPI_WORK_MODE_2: - spiDevCfg->clk_delay_half = false; - spiDevCfg->clk_polarity = true; - break; - case SPI_WORK_MODE_3: - spiDevCfg->clk_delay_half = true; - spiDevCfg->clk_polarity = true; - break; - default: - spiDevCfg->clk_delay_half = true; - spiDevCfg->clk_polarity = true; - } - spiDevCfg->slave = 0; - if (resource->transmode == SPI_TRANSFER_DMA) { - spiDevCfg->dma_rx = true; - spiDevCfg->dma_tx = true; - } else { - spiDevCfg->dma_rx = false; - spiDevCfg->dma_tx = false; + ret = SpiDevCfgInit(spiDevCfg, resource); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: SPI config init failed\r\n", __func__); + return HDF_FAILURE; } - spiDevCfg->cs = 0; - spiDevCfg->rx_bits = resource->dataSize; - spiDevCfg->tx_bits = resource->dataSize; - spiDevCfg->rx_frame_bits = 0; /* spi open move to hdf open api */ /* if cs use as gpio ,pull up at first */ if (spiCtx[spiPort].spiFunCS0 == HAL_IOMUX_FUNC_AS_GPIO) { @@ -389,8 +415,47 @@ static int32_t InitSpiDevice(struct SpiDevice *spiDevice) /* get spi config from hcs file */ #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO -static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice) +#define SPI_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->num = HCS_PROP(node, busNum); \ + resource->speed = HCS_PROP(node, speed); \ + resource->transmode = HCS_PROP(node, transmode); \ + resource->spiCsSoft = HCS_PROP(node, spiCsSoft); \ + resource->mode = HCS_PROP(node, mode); \ + resource->dataSize = HCS_PROP(node, dataSize); \ + resource->csNum = HCS_PROP(node, csNum); \ + tempPin = HCS_PROP(node, spiClkPin); \ + resource->spiClkPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + tempPin = HCS_PROP(node, spiMosiPin); \ + resource->spiMosiPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + tempPin = HCS_PROP(node, spiMisoPin); \ + resource->spiMisoPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + tempPin = HCS_PROP(node, spiCsPin); \ + resource->spiCsPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + result = HDF_SUCCESS; \ + break; \ + } \ + } while (0) + +#define PLATFORM_SPI_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), spi_config) +static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const char *deviceMatchAttr) { + uint32_t tempPin; + int32_t result = HDF_FAILURE; + struct SpiResource *resource = NULL; + if (spiDevice == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &spiDevice->resource; +#if HCS_NODE_EXISTS(PLATFORM_SPI_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_SPI_CONFIG, SPI_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } + return result; } #else static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const struct DeviceResourceNode *resourceNode) @@ -483,6 +548,7 @@ static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const struct De return HDF_SUCCESS; } #endif + int32_t AttachSpiDevice(struct SpiCntlr *spiCntlr, struct HdfDeviceObject *device) { int32_t ret; @@ -502,7 +568,7 @@ int32_t AttachSpiDevice(struct SpiCntlr *spiCntlr, struct HdfDeviceObject *devic return HDF_ERR_MALLOC_FAIL; } #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO - ret = GetSpiDeviceResource(spiDevice); + ret = GetSpiDeviceResource(spiDevice, device->deviceMatchAttr); #else ret = GetSpiDeviceResource(spiDevice, device->property); #endif @@ -687,6 +753,7 @@ static int32_t SpiDevGetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) return HDF_SUCCESS; } + static int32_t SpiDevSetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) { struct SpiDevice *spiDevice = NULL; @@ -717,11 +784,11 @@ static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, struct SpiDevice *spiDevice = NULL; struct HAL_SPI_CFG_T *spiDevCfg = NULL; struct SpiMsg *msg = NULL; + int32_t ret; if (spiCntlr == NULL || spiCntlr->priv == NULL) { HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); return HDF_ERR_INVALID_PARAM; } - HDF_LOGI("%s: %u Enter\r\n", __func__, spiId); spiDevice = (struct SpiDevice *)spiCntlr->priv; spiId = spiDevice->spiId; for (size_t i = 0; i < count; i++) { @@ -732,19 +799,22 @@ static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, } if ((msg->wbuf != NULL) && (msg->rbuf == NULL)) { - HalSpiSend(spiDevice, msg->wbuf, msg->len, TIMEOUT); + ret = HalSpiSend(spiDevice, msg->wbuf, msg->len, TIMEOUT); } if ((msg->rbuf != NULL) && (msg->wbuf == NULL)) { - HalSpiRecv(spiDevice, msg->rbuf, msg->len, TIMEOUT); + ret = HalSpiRecv(spiDevice, msg->rbuf, msg->len, TIMEOUT); } if ((msg->wbuf != NULL) && (msg->rbuf != NULL)) { - HalSpiSendRecv(spiDevice, msg->wbuf, msg->len, msg->rbuf, msg->len, TIMEOUT); + ret = HalSpiSendRecv(spiDevice, msg->wbuf, msg->len, msg->rbuf, msg->len); } /* pull pull up cs at the end */ - if (msg->csChange) { + if (msg->keepCs == 0) { hal_gpio_pin_set_dir(spiCtx[spiId].spiPinCS0, HAL_GPIO_DIR_OUT, 1); } + if (ret < 0) { + HDF_LOGE("%s send error!\r\n", __func__); + } DelayUs(msg->delayUs); } return HDF_SUCCESS; diff --git a/platform/spi/spi_bes.h b/platform/spi/spi_bes.h index 5242cfaf3ae56d9a8df63ac0dca1c1693d67b427..8cdf91633c0b2fd00a2b863791852121d4c7c5cd 100644 --- a/platform/spi/spi_bes.h +++ b/platform/spi/spi_bes.h @@ -12,7 +12,9 @@ #include "hal_spi.h" #include "hal_gpio.h" #include "hal_iomux.h" +#ifdef CHIP_BEST2003 #include "hal_iomux_best2003.h" +#endif #include "osal_mutex.h" #include "osal_sem.h" diff --git a/platform/uart/uart_bes.c b/platform/uart/uart_bes.c index e12c0bcf9aab0ea0b50fb9be89dce4835c2107be..cc2a26af89f26276b20d7833aceb75787c4fbbec 100644 --- a/platform/uart/uart_bes.c +++ b/platform/uart/uart_bes.c @@ -11,23 +11,39 @@ #include #include "hal_iomux.h" #include "hal_timer.h" +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else #include "device_resource_if.h" +#endif #include "hal_trace.h" #include "hal_cache.h" #include "hdf_log.h" -#define HDF_UART_TMO 1000 + +#define HDF_UART_TMO OSAL_WAIT_FOREVER #define HDF_LOG_TAG uartDev #define UART_FIFO_MAX_BUFFER 2048 #define UART_DMA_RING_BUFFER_SIZE 256 // mast be 2^n - +#ifdef LOSCFG_SOC_SERIES_BES2700 +#include "hal_location.h" +#define MAX_UART_NUMBER 2 +#define MAX_UART_ID HAL_UART_ID_1 +static SRAM_BSS_LOC unsigned char g_halUartBuf[UART_DMA_RING_BUFFER_SIZE]; +static SRAM_BSS_LOC unsigned char g_halUart1Buf[UART_DMA_RING_BUFFER_SIZE]; +static unsigned char *g_uartKfifoBuffer[MAX_UART_NUMBER] = { NULL, NULL }; +#elif defined (LOSCFG_SOC_SERIES_BES2600) +#define MAX_UART_NUMBER 3 +#define MAX_UART_ID HAL_UART_ID_2 static __SRAMBSS unsigned char g_halUartBuf[UART_DMA_RING_BUFFER_SIZE]; static __SRAMBSS unsigned char g_halUart1Buf[UART_DMA_RING_BUFFER_SIZE]; static __SRAMBSS unsigned char g_halUart2Buf[UART_DMA_RING_BUFFER_SIZE]; +static unsigned char *g_uartKfifoBuffer[MAX_UART_NUMBER] = {NULL, NULL, NULL}; +#endif -static struct UART_CTX_OBJ g_uartCtx[4] = {0}; -static unsigned char *g_uartKfifoBuffer[4] = {NULL, NULL, NULL, NULL}; +static struct UART_CTX_OBJ g_uartCtx[MAX_UART_NUMBER] = {0}; struct HAL_UART_CFG_T g_lowUartCfg = { // used for tgdb cli console .parity = HAL_UART_PARITY_NONE, @@ -44,21 +60,31 @@ struct HAL_UART_CFG_T g_lowUartCfg = { static void HalSetUartIomux(enum HAL_UART_ID_T uartId) { + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d not support!\r\n", __func__, uartId); + return; + } if (uartId == HAL_UART_ID_0) { hal_iomux_set_uart0(); - } else if (uartId == HAL_UART_ID_1) { + } + if (uartId == HAL_UART_ID_1) { hal_iomux_set_uart1(); - } else if (uartId == HAL_UART_ID_2) { + } +#ifdef LOSCFG_SOC_SERIES_BES2600 + if (uartId == HAL_UART_ID_2) { hal_iomux_set_uart2(); - } else { - hal_iomux_set_uart3(); } +#endif } -static void HalUartRxStart(uint32_t uartId) +static void HalUartStartDmaRx(uint32_t uartId) { + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d not support!\r\n", __func__, uartId); + return; + } struct HAL_DMA_DESC_T dmaDescRx; - unsigned int descCnt = 1; + uint32_t descCnt; union HAL_UART_IRQ_T mask; mask.reg = 0; @@ -67,58 +93,61 @@ static void HalUartRxStart(uint32_t uartId) mask.OE = 0; mask.PE = 0; mask.RT = 1; - + descCnt = 1; hal_uart_dma_recv_mask(uartId, g_uartCtx[uartId].buffer, UART_DMA_RING_BUFFER_SIZE, &dmaDescRx, &descCnt, &mask); } -static void UartRxHandler(enum HAL_UART_ID_T id, union HAL_UART_IRQ_T status) +static void UartRxHandler(enum HAL_UART_ID_T uartId, union HAL_UART_IRQ_T status) { - int32_t ret; - + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d not support!\r\n", __func__, uartId); + return; + } if (status.TX) { - ret = OsalSemPost(&g_uartCtx[id].txSem); - ASSERT(ret == HDF_SUCCESS, "%s: Failed to release write_sem: %d", __func__, ret); + if (OsalSemPost(&g_uartCtx[uartId].txSem) != HDF_SUCCESS) { + HDF_LOGE("%s OsalSemPost txSem failed!\r\n", __func__); + return; + } } if (status.RX || status.RT) { - ret = OsalSemPost(&g_uartCtx[id].rxSem); - ASSERT(ret == HDF_SUCCESS, "%s: Failed to release read_sem: %d", __func__, ret); + if (OsalSemPost(&g_uartCtx[uartId].rxSem) != HDF_SUCCESS) { + HDF_LOGE("%s OsalSemPost rxSem failed!\r\n", __func__); + return; + } } } static void UartDmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status) { - uint32_t len = 0; - uint32_t uartid = 0; - - len = kfifo_put(&g_uartCtx[uartid].fifo, g_uartCtx[uartid].buffer, xferSize); + uint32_t len; + len = kfifo_put(&g_uartCtx[HAL_UART_ID_0].fifo, g_uartCtx[HAL_UART_ID_0].buffer, xferSize); if (len < xferSize) { HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize); return; } - memset_s(g_uartCtx[uartid].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); - OsalSemPost(&g_uartCtx[uartid].rxSem); - HalUartRxStart(uartid); + (void)memset_s(g_uartCtx[HAL_UART_ID_0].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); + OsalSemPost(&g_uartCtx[HAL_UART_ID_0].rxSem); + HalUartStartDmaRx(HAL_UART_ID_0); } static void UartDmaTxHandler(uint32_t xferSize, int dmaError) { - OsalSemPost(&g_uartCtx[0].txSem); + OsalSemPost(&g_uartCtx[HAL_UART_ID_0].txSem); } static void Uart1DmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status) { - uint32_t len = 0; - uint32_t uartid = HAL_UART_ID_1; - len = kfifo_put(&g_uartCtx[uartid].fifo, g_uartCtx[uartid].buffer, xferSize); + uint32_t len; + len = kfifo_put(&g_uartCtx[HAL_UART_ID_1].fifo, g_uartCtx[HAL_UART_ID_1].buffer, xferSize); if (len < xferSize) { HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize); return; } - memset_s(g_uartCtx[uartid].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); - OsalSemPost(&g_uartCtx[uartid].rxSem); - HalUartRxStart(uartid); + memset_s(g_uartCtx[HAL_UART_ID_1].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); + OsalSemPost(&g_uartCtx[HAL_UART_ID_1].rxSem); + HalUartStartDmaRx(HAL_UART_ID_1); } static void Uart1DmaTxHandler(uint32_t xferSize, int dmaError) @@ -126,29 +155,34 @@ static void Uart1DmaTxHandler(uint32_t xferSize, int dmaError) OsalSemPost(&g_uartCtx[HAL_UART_ID_1].txSem); } +#ifdef LOSCFG_SOC_SERIES_BES2600 /* uart2 */ static void Uart2DmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status) { - uint32_t len = 0; - uint32_t uartid = HAL_UART_ID_2; - len = kfifo_put(&g_uartCtx[uartid].fifo, g_uartCtx[uartid].buffer, xferSize); + uint32_t len ; + len = kfifo_put(&g_uartCtx[HAL_UART_ID_2].fifo, g_uartCtx[HAL_UART_ID_2].buffer, xferSize); if (len < xferSize) { HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize); return; } - memset_s(g_uartCtx[uartid].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); - OsalSemPost(&g_uartCtx[uartid].rxSem); - HalUartRxStart(uartid); + memset_s(g_uartCtx[HAL_UART_ID_2].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); + OsalSemPost(&g_uartCtx[HAL_UART_ID_2].rxSem); + HalUartStartDmaRx(HAL_UART_ID_2); } static void Uart2DmaTxHandler(uint32_t xferSize, int dmaError) { OsalSemPost(&g_uartCtx[HAL_UART_ID_2].txSem); } +#endif static void HalUartStartRx(uint32_t uartId) { + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); + return; + } union HAL_UART_IRQ_T mask; mask.reg = 0; mask.RT = 1; @@ -160,20 +194,22 @@ static void HalUartStartRx(uint32_t uartId) static int32_t HalUartSend(uint32_t uartId, const void *data, uint32_t size, uint32_t timeOut) { - int32_t ret = HDF_FAILURE; + int32_t ret; struct HAL_DMA_DESC_T dmaSescTx; - unsigned int descCnt = 1; - + uint32_t descCnt; if (data == NULL || size == 0) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_INVALID_PARAM; } - if (uartId > HAL_UART_ID_2) { + if (uartId > MAX_UART_ID) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_NOT_SUPPORT; } - + descCnt = 1; +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif hal_uart_dma_send(uartId, data, size, &dmaSescTx, &descCnt); OsalSemWait(&g_uartCtx[uartId].txSem, timeOut); @@ -183,19 +219,18 @@ static int32_t HalUartSend(uint32_t uartId, const void *data, uint32_t size, uin static int32_t HalUartRecv(uint8_t uartId, void *data, uint32_t expectSize, uint32_t *recvSize, uint32_t timeOut) { - int32_t ret = HDF_FAILURE; - uint32_t beginTime = 0; - uint32_t nowTime = 0; - uint32_t fifoPopLen = 0; + uint32_t beginTime; + uint32_t nowTime; + uint32_t fifoPopLen; uint32_t recvedLen = 0; - uint32_t expectLen = expectSize; + int32_t expectLen = (int32_t)expectSize; - if (data == NULL || expectSize == 0 || recvSize == NULL) { + if (data == NULL || expectLen == 0 || recvSize == NULL) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_INVALID_PARAM; } - if (uartId > HAL_UART_ID_2) { + if (uartId > MAX_UART_ID) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_NOT_SUPPORT; } @@ -223,19 +258,31 @@ static int32_t HalUartRecv(uint8_t uartId, void *data, uint32_t expectSize, if (recvSize != NULL) { *recvSize = recvedLen; } - return HDF_SUCCESS; } -static void HalUartHandlerInit(struct UartDevice *device) +static int32_t InitUartCtxCfg(struct UartDevice *device) { uint32_t uartId; + struct HAL_UART_CFG_T *uartCfg = NULL; if (device == NULL) { HDF_LOGE("%s: INVALID PARAM", __func__); - return HDF_ERR_INVALID_PARAM; + return HDF_ERR_INVALID_OBJECT; + } + uartCfg = &device->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; } uartId = device->uartId; - HDF_LOGI("%s %ld\r\n", __func__, uartId); + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } + memset_s(&g_uartCtx[uartId], sizeof(struct UART_CTX_OBJ), 0, sizeof(struct UART_CTX_OBJ)); + g_uartCtx[uartId].txDMA = uartCfg->dma_tx; + g_uartCtx[uartId].rxDMA = uartCfg->dma_rx; + if (uartId == HAL_UART_ID_0) { g_uartCtx[uartId].UartDmaRxHandler = UartDmaRxHandler; g_uartCtx[uartId].UartDmaTxHandler = UartDmaTxHandler; @@ -247,13 +294,36 @@ static void HalUartHandlerInit(struct UartDevice *device) g_uartCtx[uartId].UartDmaTxHandler = Uart1DmaTxHandler; g_uartCtx[uartId].buffer = g_halUart1Buf; } - +#ifdef LOSCFG_SOC_SERIES_BES2600 if (uartId == HAL_UART_ID_2) { g_uartCtx[uartId].UartDmaRxHandler = Uart2DmaRxHandler; g_uartCtx[uartId].UartDmaTxHandler = Uart2DmaTxHandler; g_uartCtx[uartId].buffer = g_halUart2Buf; } +#endif + return HDF_SUCCESS; +} + +static void HalUartHandlerInit(struct UartDevice *device) +{ + uint32_t uartId; + int32_t ret; + struct HAL_UART_CFG_T *uartCfg = NULL; + if (device == NULL) { + HDF_LOGE("%s: INVALID PARAM!\r\n", __func__); + return; + } + uartId = device->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT!\r\n", __FILE__, __LINE__); + return; + } + ret = InitUartCtxCfg(device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s %d InitUartCtxCfg failed\r\n", __FILE__, __LINE__); + return; + } if (!g_uartKfifoBuffer[uartId]) { g_uartKfifoBuffer[uartId] = (char *)OsalMemAlloc(UART_FIFO_MAX_BUFFER); if (!g_uartKfifoBuffer[uartId]) { @@ -263,13 +333,19 @@ static void HalUartHandlerInit(struct UartDevice *device) kfifo_init(&g_uartCtx[uartId].fifo, g_uartKfifoBuffer[uartId], UART_FIFO_MAX_BUFFER); } - OsalSemInit(&g_uartCtx[uartId].rxSem, 0); - OsalSemInit(&g_uartCtx[uartId].txSem, 0); + if (OsalSemInit(&g_uartCtx[uartId].rxSem, 0) != HDF_SUCCESS) { + HDF_LOGE("UART rxsem init failed!"); + return; + } + if (OsalSemInit(&g_uartCtx[uartId].txSem, 0) != HDF_SUCCESS) { + HDF_LOGE("UART txsem init failed!"); + return; + } - if (g_uartCtx[uartId].rxDMA) { - HDF_LOGE("uart %ld start dma rx\r\n", uartId); + if (g_uartCtx[uartId].rxDMA == true) { + HDF_LOGI("uart %ld start dma rx\r\n", uartId); hal_uart_irq_set_dma_handler(uartId, g_uartCtx[uartId].UartDmaRxHandler, g_uartCtx[uartId].UartDmaTxHandler); - HalUartRxStart(uartId); + HalUartStartDmaRx(uartId); } else { HalUartStartRx(uartId); } @@ -284,13 +360,16 @@ static void UartStart(struct UartDevice *device) return; } uartId = device->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg = &device->config; if (uartCfg == NULL) { HDF_LOGE("%s: INVALID OBJECT", __func__); return; } hal_uart_open(uartId, uartCfg); - HDF_LOGI("%s %ld\r\n", __FUNCTION__, uartId); HalUartHandlerInit(device); } @@ -361,20 +440,15 @@ static int InitUartDevice(struct UartHost *host) HDF_LOGE("%s: INVALID OBJECT", __func__); return HDF_ERR_INVALID_OBJECT; } - uartDevice->uartId = resource->num; - uartCfg->parity = resource->parity; - uartCfg->stop = resource->stopBit; - uartCfg->data = resource->wLen; + uint32_t uartId = resource->num; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg->flow = HAL_UART_FLOW_CONTROL_NONE; - uartCfg->tx_level = HAL_UART_FIFO_LEVEL_1_2; + uartCfg->tx_level = HAL_UART_FIFO_LEVEL_1_8; uartCfg->rx_level = HAL_UART_FIFO_LEVEL_1_2; - uartCfg->baud = resource->baudRate; uartCfg->dma_rx_stop_on_err = false; - uartCfg->dma_rx = resource->rxDMA; - uartCfg->dma_tx = resource->txDMA; - - g_uartCtx[uartDevice->uartId].txDMA = resource->txDMA; - g_uartCtx[uartDevice->uartId].rxDMA = resource->rxDMA; if (!uartDevice->initFlag) { HDF_LOGE("uart %ld device init\r\n", uartDevice->uartId); @@ -382,10 +456,53 @@ static int InitUartDevice(struct UartHost *host) UartStart(uartDevice); uartDevice->initFlag = true; } - return HDF_SUCCESS; } - +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define UART_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->num = HCS_PROP(node, num); \ + resource->baudRate = HCS_PROP(node, baudRate); \ + resource->parity = HCS_PROP(node, parity); \ + resource->stopBit = HCS_PROP(node, stopBit); \ + resource->wLen = HCS_PROP(node, data); \ + resource->rxDMA = HCS_PROP(node, rxDMA); \ + resource->txDMA = HCS_PROP(node, txDMA); \ + result = HDF_SUCCESS; \ + break; \ + } \ + } while (0) + +#define PLATFORM_UART_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), uart_config) +static uint32_t GetUartDeviceResource(struct UartDevice *device, const char *deviceMatchAttr) +{ + struct UartResource *resource = NULL; + int32_t result = HDF_FAILURE; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; +#if HCS_NODE_EXISTS(PLATFORM_UART_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_UART_CONFIG, UART_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + return result; + } + // copy config + device->initFlag = false; + device->uartId = resource->num; + device->config.baud = resource->baudRate; + device->config.parity = resource->parity; + device->config.stop = resource->stopBit; + device->config.data = resource->wLen; + device->config.dma_rx = (resource->rxDMA == true) ? true : false; + device->config.dma_tx = (resource->txDMA == true) ? true : false; + return HDF_SUCCESS; +} +#else static uint32_t GetUartDeviceResource( struct UartDevice *device, const struct DeviceResourceNode *resourceNode) { @@ -431,6 +548,7 @@ static uint32_t GetUartDeviceResource( resource->rxDMA = dri->GetBool(resourceNode, "rxDMA"); // copy config + device->initFlag = false; device->uartId = resource->num; device->config.baud = resource->baudRate; device->config.parity = resource->parity; @@ -440,13 +558,16 @@ static uint32_t GetUartDeviceResource( device->config.dma_tx = resource->txDMA; return HDF_SUCCESS; } - +#endif static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device) { int32_t ret; struct UartDevice *uartDevice = NULL; - +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL || uartHost == NULL) { +#else if (uartHost == NULL || device == NULL || device->property == NULL) { +#endif HDF_LOGE("%s: property is NULL", __func__); return HDF_ERR_INVALID_PARAM; } @@ -456,8 +577,11 @@ static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObjec HDF_LOGE("%s: OsalMemCalloc uartDevice error", __func__); return HDF_ERR_MALLOC_FAIL; } - +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetUartDeviceResource(uartDevice, device->deviceMatchAttr); +#else ret = GetUartDeviceResource(uartDevice, device->property); +#endif if (ret != HDF_SUCCESS) { (void)OsalMemFree(uartDevice); return HDF_FAILURE; @@ -470,7 +594,7 @@ static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObjec static int32_t UartDriverBind(struct HdfDeviceObject *device) { - struct UartHost *devService; + struct UartHost *devService = NULL; if (device == NULL) { HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; @@ -554,8 +678,8 @@ static int32_t UartHostDevInit(struct UartHost *host) HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; } - InitUartDevice(host); - return HDF_SUCCESS; + + return InitUartDevice(host); } static int32_t UartHostDevDeinit(struct UartHost *host) @@ -584,7 +708,7 @@ static int32_t UartHostDevDeinit(struct UartHost *host) static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size) { struct UartDevice *device = NULL; - uint32_t portId; + uint32_t uartId; if (host == NULL || data == NULL || size == 0 || host->priv == NULL) { HDF_LOGE("%s: invalid parameter", __func__); @@ -597,15 +721,19 @@ static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t s return HDF_ERR_INVALID_OBJECT; } - portId = device->uartId; - if (g_uartCtx[portId].txDMA) { - HalUartSend(portId, data, size, HDF_UART_TMO); + uartId = device->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } + if (g_uartCtx[uartId].txDMA) { + HalUartSend(uartId, data, size, HDF_UART_TMO); } else { for (uint32_t idx = 0; idx < size; idx++) { - if (g_uartCtx[portId].isBlock) { - hal_uart_blocked_putc(portId, data[idx]); + if (g_uartCtx[uartId].isBlock) { + hal_uart_blocked_putc(uartId, data[idx]); } else { - hal_uart_putc(portId, data[idx]); + hal_uart_putc(uartId, data[idx]); } } } @@ -615,7 +743,7 @@ static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t s static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size) { - uint32_t recvSize = 0; + uint32_t recvSize; int32_t ret; uint32_t uartId; struct UartDevice *uartDevice = NULL; @@ -642,7 +770,9 @@ static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t si if (g_uartCtx[uartId].isBlock) { data[0] = hal_uart_blocked_getc(uartId); } else { - data[0] = hal_uart_getc(uartId); + if (hal_uart_readable(uartId) > 0) { + data[0] = hal_uart_getc(uartId); + } } ret = 1; } @@ -667,7 +797,10 @@ static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) return HDF_ERR_INVALID_OBJECT; } uartId = uartDevice->uartId; - + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg = &uartDevice->config; if (uartCfg == NULL) { HDF_LOGE("%s: device config is NULL", __func__); @@ -675,7 +808,7 @@ static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) } uartCfg->baud = baudRate; - hal_uart_reopen(uartId, uartCfg); + hal_uart_open(uartId, uartCfg); return HDF_SUCCESS; } @@ -696,6 +829,10 @@ static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) return HDF_ERR_INVALID_OBJECT; } uartId = uartDevice->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg = &uartDevice->config; if (uartCfg == NULL) { HDF_LOGE("%s: device is NULL", __func__); @@ -709,22 +846,14 @@ static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) return HDF_SUCCESS; } -static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) +static int32_t SetUartDevConfig(struct UartAttribute *attribute, struct UartDevice *uartDevice) { - HDF_LOGI("%s: Enter", __func__); - struct UartDevice *uartDevice = NULL; struct HAL_UART_CFG_T *uartCfg = NULL; uint32_t uartId; - if (host == NULL || attribute == NULL || host->priv == NULL) { + if (attribute == NULL || uartDevice == NULL) { HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; } - - uartDevice = (struct UartDevice *)host->priv; - if (uartDevice == NULL) { - HDF_LOGE("%s: device is NULL", __func__); - return HDF_ERR_INVALID_OBJECT; - } uartId = uartDevice->uartId; uartCfg = &uartDevice->config; if (uartCfg == NULL) { @@ -771,17 +900,15 @@ static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribu } else { uartCfg->flow = HAL_UART_FLOW_CONTROL_NONE; } - - hal_uart_reopen(uartId, uartCfg); - + hal_uart_open(uartId, uartCfg); return HDF_SUCCESS; } -static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) { HDF_LOGI("%s: Enter", __func__); struct UartDevice *uartDevice = NULL; - struct HAL_UART_CFG_T *uartCfg = NULL; + int ret; if (host == NULL || attribute == NULL || host->priv == NULL) { HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; @@ -792,12 +919,21 @@ static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribu HDF_LOGE("%s: device is NULL", __func__); return HDF_ERR_INVALID_OBJECT; } - uartCfg = &uartDevice->config; - if (uartCfg == NULL) { - HDF_LOGE("%s: config is NULL", __func__); - return HDF_ERR_INVALID_OBJECT; + + ret = SetUartDevConfig(attribute, uartDevice); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: SetUartDevConfig error", __func__); + return HDF_FAILURE; } + return HDF_SUCCESS; +} +static int32_t GetUartDevConfig(struct UartAttribute *attribute, struct HAL_UART_CFG_T *uartCfg) +{ + if (attribute == NULL || uartCfg == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } switch (uartCfg->data) { case HAL_UART_DATA_BITS_8: attribute->dataBits = UART_ATTR_DATABIT_8; @@ -841,10 +977,33 @@ static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribu attribute->cts = 0; break; } - return HDF_SUCCESS; } +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + struct HAL_UART_CFG_T *uartCfg = NULL; + if (host == NULL || attribute == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartCfg = &uartDevice->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + return GetUartDevConfig(attribute, uartCfg); +} + static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode) { HDF_LOGI("%s: Enter", __func__); @@ -861,7 +1020,10 @@ static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode return HDF_ERR_INVALID_OBJECT; } uartId = uartDevice->uartId; - + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } switch (mode) { case UART_MODE_RD_BLOCK: g_uartCtx[uartId].isBlock = true; diff --git a/platform/watchdog/watchdog_bes.c b/platform/watchdog/watchdog_bes.c index e70f33650ff400624d38c26403b1cbbdc2fb4c78..7071f41ca8b0b426d3ed4e4ea34ec19c74caf971 100644 --- a/platform/watchdog/watchdog_bes.c +++ b/platform/watchdog/watchdog_bes.c @@ -47,7 +47,7 @@ static int InitWatchdogDevice(struct WatchdogDevice *watchdogDevice) struct WatchdogResource *resource = NULL; int32_t watchdogId; if (watchdogDevice == NULL) { - HDF_LOGE("%s: invaild parameter\r\n", __func__); + HDF_LOGE("%s: invalid parameter\r\n", __func__); return HDF_ERR_INVALID_PARAM; } @@ -182,7 +182,7 @@ static int32_t WatchdogDriverInit(struct HdfDeviceObject *device) } watchdogCntlr->ops = &g_WatchdogCntlrMethod; - + HDF_LOGE("WatchdogDriverInit success!\r\n"); return ret; }