diff options
| author | HubretXie <[email protected]> | 2019-06-09 13:55:19 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-06-09 13:55:19 +0800 |
| commit | 5a5ea94928061548e5c59dd6cd070eaaeb347213 (patch) | |
| tree | 4d88994e08184bc0aa297dfc20c70ca3906d36b1 /components | |
| parent | 36ffdc058b3aeb447d74dd916c26e1dd80b80def (diff) | |
| parent | cf7052f6b7502c20f28eae5b9c096978c7f3a98f (diff) | |
Merge pull request #6 from RT-Thread/master
sync
Diffstat (limited to 'components')
27 files changed, 215 insertions, 2435 deletions
diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index d83e54a8da..45494c6a6e 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -212,6 +212,9 @@ config RT_USING_SPI config RT_USING_SFUD bool "Using Serial Flash Universal Driver" default n + help + An using JEDEC's SFDP standard serial (SPI) flash universal driver library + if RT_USING_SFUD config RT_SFUD_USING_SFDP bool "Using auto probe flash JEDEC SFDP parameter" @@ -231,14 +234,6 @@ config RT_USING_SPI default n endif - config RT_USING_W25QXX - bool "Using W25QXX SPI NorFlash" - default n - - config RT_USING_GD - bool "Using GD SPI NorFlash" - default n - config RT_USING_ENC28J60 bool "Using ENC28J60 SPI Ethernet network interface" select RT_USING_LWIP @@ -283,7 +278,7 @@ menu "Using Hardware Crypto drivers" int "IV max size" default "16" - config HWCRYPTO_KEYBIT_MAX_SIZE + config RT_HWCRYPTO_KEYBIT_MAX_SIZE int "Key max bit length" default 256 diff --git a/components/drivers/hwcrypto/hw_bignum.c b/components/drivers/hwcrypto/hw_bignum.c index 173975c70a..cfc57f8273 100644 --- a/components/drivers/hwcrypto/hw_bignum.c +++ b/components/drivers/hwcrypto/hw_bignum.c @@ -14,7 +14,7 @@ static struct rt_hwcrypto_ctx *bignum_default; -rt_inline rt_err_t rt_hwcrypto_bignum_init(void) +rt_inline rt_err_t hwcrypto_bignum_dev_is_init(void) { struct rt_hwcrypto_device *dev; @@ -55,20 +55,18 @@ rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device) } /** - * @brief Allocate memory for bignum - * - * @return Pointer to allocated bignum obj + * @brief Init bignum obj + * + * @param n bignum obj */ -struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void) +void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n) { - struct hw_bignum_mpi *n; + if(n == RT_NULL) + return; - n = rt_malloc(sizeof(struct hw_bignum_mpi)); - if (n) - { - rt_memset(n, 0, sizeof(struct hw_bignum_mpi)); - } - return n; + n->sign = 1; + n->total = 0; + n->p = RT_NULL; } /** @@ -81,7 +79,9 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n) if (n) { rt_free(n->p); - rt_free(n); + n->sign = 0; + n->total = 0; + n->p = RT_NULL; } } @@ -90,7 +90,7 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n) * * @param n bignum obj * - * @return binary buffer Length + * @return binary buffer length */ int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n) { @@ -111,29 +111,34 @@ int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n) } /** - * @brief Get length of bignum as an unsigned binary buffer + * @brief Export n into unsigned binary data, big endian * * @param n bignum obj * @param buf Buffer for the binary number * @param len Length of the buffer * - * @return binary buffer Length + * @return export bin length */ -int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) +int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) { - int cp_len; + int cp_len, i, j; - if (n == RT_NULL || n->p == RT_NULL || buf == RT_NULL) + if (n == RT_NULL || buf == RT_NULL) { return 0; } + rt_memset(buf, 0, len); cp_len = n->total > len ? len : n->total; - rt_memcpy(n->p, buf, cp_len); + for(i = cp_len, j = 0; i > 0; i--, j++) + { + buf[i - 1] = n->p[j]; + } + return cp_len; } /** - * @brief Set binary buffer to unsigned bignum + * @brief Import n from unsigned binary data, big endian * * @param n bignum obj * @param buf Buffer for the binary number @@ -141,82 +146,39 @@ int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) +rt_err_t rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) { + int cp_len, i, j; void *temp_p; - if (n == RT_NULL) - { - return -RT_EINVAL; - } - if (n->p && n->total >= len) - { - rt_memcpy(n->p, buf, len); - return RT_EOK; - } - temp_p = rt_malloc(len); - if (temp_p == RT_NULL) + if (n == RT_NULL || buf == RT_NULL) { - return -RT_ENOMEM; + return 0; } - if (n->p) + if (n->total < len) { + temp_p = rt_malloc(len); + if (temp_p == RT_NULL) + { + return 0; + } + rt_memset(temp_p, 0, len); rt_free(n->p); n->p = temp_p; - n->total = 0; + n->total = len; } - rt_memcpy(n->p, buf, len); - n->total = len; - return RT_EOK; -} - -/** - * @brief Unsigned comparison - * - * @param a bignum obj - * @param b bignum obj - * - * @return 0 is equal - */ -int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b) -{ - int a_len, b_len; + cp_len = n->total > len ? len : n->total; - if (a == RT_NULL || a->p == RT_NULL - || b == RT_NULL || b->p == RT_NULL) - { - return -1; - } - a_len = rt_hwcrypto_bignum_get_len(a); - b_len = rt_hwcrypto_bignum_get_len(b); - if (a_len != b_len) + for(i = cp_len, j = 0; i > 0; i--, j++) { - return a_len - b_len; + n->p[j] = buf[i - 1]; } - return rt_memcmp(a->p, b->p, a_len); -} - -/** - * @brief Compare bignum to standard Unsigned integer - * - * @param a bignum obj - * @param b Unsigned integer - * - * @return 0 is equal - */ -int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b) -{ - struct hw_bignum_mpi tmp_b; - b = b <= 0 ? -b : b; - tmp_b.total = sizeof(unsigned long); - tmp_b.p = &b; - return rt_hwcrypto_bignum_cmp(a, &tmp_b); + return cp_len; } /** - * @brief a = b + c + * @brief x = a + b * * @param a bignum obj * @param b bignum obj @@ -224,26 +186,26 @@ int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b) * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c) +rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b) { struct hwcrypto_bignum *bignum_ctx; - if (rt_hwcrypto_bignum_init() != RT_EOK) + if (hwcrypto_bignum_dev_is_init() != RT_EOK) { return -RT_ERROR; } bignum_ctx = (struct hwcrypto_bignum *)bignum_default; if (bignum_ctx->ops->add) { - return bignum_ctx->ops->add(bignum_ctx, a, b, c); + return bignum_ctx->ops->add(bignum_ctx, x, a, b); } return -RT_ERROR; } /** - * @brief a = b - c + * @brief x = a - b * * @param a bignum obj * @param b bignum obj @@ -251,26 +213,26 @@ rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c) +rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b) { struct hwcrypto_bignum *bignum_ctx; - if (rt_hwcrypto_bignum_init() != RT_EOK) + if (hwcrypto_bignum_dev_is_init() != RT_EOK) { return -RT_ERROR; } bignum_ctx = (struct hwcrypto_bignum *)bignum_default; if (bignum_ctx->ops->sub) { - return bignum_ctx->ops->sub(bignum_ctx, a, b, c); + return bignum_ctx->ops->sub(bignum_ctx, x, a, b); } return -RT_ERROR; } /** - * @brief a = b * c + * @brief x = a * b * * @param a bignum obj * @param b bignum obj @@ -278,26 +240,26 @@ rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c) +rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b) { struct hwcrypto_bignum *bignum_ctx; - if (rt_hwcrypto_bignum_init() != RT_EOK) + if (hwcrypto_bignum_dev_is_init() != RT_EOK) { return -RT_ERROR; } bignum_ctx = (struct hwcrypto_bignum *)bignum_default; if (bignum_ctx->ops->mul) { - return bignum_ctx->ops->mul(bignum_ctx, a, b, c); + return bignum_ctx->ops->mul(bignum_ctx, x, a, b); } return -RT_ERROR; } /** - * @brief a = b * c (mod d) + * @brief x = a * b (mod c) * * @param a bignum obj * @param b bignum obj @@ -305,27 +267,27 @@ rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a, +rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c, - const struct hw_bignum_mpi *d) + const struct hw_bignum_mpi *c) { struct hwcrypto_bignum *bignum_ctx; - if (rt_hwcrypto_bignum_init() != RT_EOK) + if (hwcrypto_bignum_dev_is_init() != RT_EOK) { return -RT_ERROR; } bignum_ctx = (struct hwcrypto_bignum *)bignum_default; if (bignum_ctx->ops->mulmod) { - return bignum_ctx->ops->mulmod(bignum_ctx, a, b, c, d); + return bignum_ctx->ops->mulmod(bignum_ctx, x, a, b, c); } return -RT_ERROR; } /** - * @brief a = b ^ c (mod d) + * @brief x = a ^ b (mod c) * * @param a bignum obj * @param b bignum obj @@ -333,21 +295,21 @@ rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t bignum_exptmod(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c, - const struct hw_bignum_mpi *d) +rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c) { struct hwcrypto_bignum *bignum_ctx; - if (rt_hwcrypto_bignum_init() != RT_EOK) + if (hwcrypto_bignum_dev_is_init() != RT_EOK) { return -RT_ERROR; } bignum_ctx = (struct hwcrypto_bignum *)bignum_default; if (bignum_ctx->ops->exptmod) { - return bignum_ctx->ops->exptmod(bignum_ctx, a, b, c, d); + return bignum_ctx->ops->exptmod(bignum_ctx, x, a, b, c); } return -RT_ERROR; } diff --git a/components/drivers/hwcrypto/hw_bignum.h b/components/drivers/hwcrypto/hw_bignum.h index 92bd4dd3b6..a1f567e68f 100644 --- a/components/drivers/hwcrypto/hw_bignum.h +++ b/components/drivers/hwcrypto/hw_bignum.h @@ -22,34 +22,35 @@ struct hwcrypto_bignum; /* bignum obj */ struct hw_bignum_mpi { - rt_size_t total; /**< Total length of data */ - rt_ubase_t *p; /**< pointer to data */ + int sign; /**< integer sign */ + rt_size_t total; /**< total of limbs */ + rt_uint8_t *p; /**< pointer to limbs */ }; struct hwcrypto_bignum_ops { rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx, - struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c); /**< a = b + c */ + struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); /**< x = a + b */ rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx, - struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c); /**< a = b - c */ + struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); /**< x = a - b */ rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx, - struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c); /**< a = b * c */ + struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); /**< x = a * b */ rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx, - struct hw_bignum_mpi *a, + struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c, - const struct hw_bignum_mpi *d); /**< a = b * c (mod d) */ + const struct hw_bignum_mpi *c); /**< x = a * b (mod c) */ rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx, - struct hw_bignum_mpi *a, + struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c, - const struct hw_bignum_mpi *d); /**< a = b ^ c (mod d) */ + const struct hw_bignum_mpi *c); /**< x = a ^ b (mod c) */ }; /** @@ -69,11 +70,9 @@ struct hwcrypto_bignum rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device); /** - * @brief Allocate memory for bignum - * - * @return Pointer to allocated bignum obj + * @brief Init bignum obj */ -struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void); +void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n); /** * @brief free a bignum obj @@ -92,18 +91,18 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n); int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n); /** - * @brief Get length of bignum as an unsigned binary buffer + * @brief Export n into unsigned binary data, big endian * * @param n bignum obj * @param buf Buffer for the binary number * @param len Length of the buffer * - * @return binary buffer Length + * @return export bin length */ -int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); +int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); /** - * @brief Set binary buffer to unsigned bignum + * @brief Import n from unsigned binary data, big endian * * @param n bignum obj * @param buf Buffer for the binary number @@ -111,31 +110,10 @@ int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); +rt_err_t rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); /** - * @brief Unsigned comparison - * - * @param a bignum obj - * @param b bignum obj - * - * @return 0 is equal - */ -int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b); - -/** - * @brief Compare bignum to standard Unsigned integer - * - * @param a bignum obj - * @param b Unsigned integer - * - * @return 0 is equal - */ -int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b); - -/** - * @brief a = b + c + * @brief x = a + b * * @param a bignum obj * @param b bignum obj @@ -143,12 +121,12 @@ int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b); * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c); +rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); /** - * @brief a = b - c + * @brief x = a - b * * @param a bignum obj * @param b bignum obj @@ -156,12 +134,12 @@ rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c); +rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); /** - * @brief a = b * c + * @brief x = a * b * * @param a bignum obj * @param b bignum obj @@ -169,12 +147,12 @@ rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c); +rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); /** - * @brief a = b * c (mod d) + * @brief x = a * b (mod c) * * @param a bignum obj * @param b bignum obj @@ -182,13 +160,13 @@ rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a, +rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c, - const struct hw_bignum_mpi *d); + const struct hw_bignum_mpi *c); /** - * @brief a = b ^ c (mod d) + * @brief x = a ^ b (mod c) * * @param a bignum obj * @param b bignum obj @@ -196,10 +174,10 @@ rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a, * * @return RT_EOK on success. */ -rt_err_t bignum_exptmod(struct hw_bignum_mpi *a, - const struct hw_bignum_mpi *b, - const struct hw_bignum_mpi *c, - const struct hw_bignum_mpi *d); +rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x, + const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); #ifdef __cplusplus } diff --git a/components/drivers/hwcrypto/hw_hash.h b/components/drivers/hwcrypto/hw_hash.h index b913f4ebd4..de0310e84c 100644 --- a/components/drivers/hwcrypto/hw_hash.h +++ b/components/drivers/hwcrypto/hw_hash.h @@ -101,7 +101,7 @@ void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx); * * @return RT_EOK on success. */ -rt_err_t rt_hwcrypto_hash_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); +rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); #ifdef __cplusplus } diff --git a/components/drivers/i2c/fm24clxx.c b/components/drivers/i2c/fm24clxx.c deleted file mode 100644 index eecf3d455a..0000000000 --- a/components/drivers/i2c/fm24clxx.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017-07-14 aubr.cool 1st version - */ -#include <rtthread.h> -#include <rtdevice.h> -#include "fm24clxx.h" - -struct fm24clxx_device -{ - struct rt_device parent; - struct rt_i2c_bus_device *bus; -}; - -/* RT-Thread device interface */ - -static rt_err_t fm24clxx_init(rt_device_t dev) -{ - return RT_EOK; -} -static rt_err_t fm24clxx_open(rt_device_t dev, rt_uint16_t oflag) -{ - return RT_EOK; -} - -static rt_err_t fm24clxx_close(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t fm24clxx_control(rt_device_t dev, int cmd, void *args) -{ - return RT_EOK; -} - -static rt_size_t fm24clxx_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) -{ - struct fm24clxx_device *fm24clxx; - const struct fm24clxx_config *cfg; - struct rt_i2c_msg msg[2]; - rt_uint8_t mem_addr[2] = {0,}; - rt_size_t ret = 0; - RT_ASSERT(dev != 0); - - fm24clxx = (struct fm24clxx_device *) dev; - - RT_ASSERT(fm24clxx->parent.user_data != 0); - cfg = (const struct fm24clxx_config *) fm24clxx->parent.user_data; - - if(pos > cfg->size) - { - return 0; - } - - if(pos + size > cfg->size) - { - size = cfg->size - pos; - } - - msg[0].addr = cfg->addr; - msg[0].flags = cfg->flags | RT_I2C_WR; - mem_addr[0] = (pos >> 8); - mem_addr[1] = (rt_uint8_t) pos; - msg[0].buf = (rt_uint8_t *) mem_addr; - msg[0].len = 2; - - msg[1].addr = cfg->addr; - msg[1].flags = cfg->flags | RT_I2C_RD; - msg[1].buf = (rt_uint8_t *) buffer; - msg[1].len = size; - - ret = rt_i2c_transfer(fm24clxx->bus, msg, 2); - return (ret == 2) ? size : 0; -} - -static rt_size_t fm24clxx_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) -{ - struct fm24clxx_device *fm24clxx; - const struct fm24clxx_config *cfg; - struct rt_i2c_msg msg[2]; - rt_uint8_t mem_addr[2] = {0,}; - rt_size_t ret = 0; - RT_ASSERT(dev != 0); - - fm24clxx = (struct fm24clxx_device *) dev; - - RT_ASSERT(fm24clxx->parent.user_data != 0); - cfg = (const struct fm24clxx_config *) fm24clxx->parent.user_data; - - if(pos > cfg->size) - { - return 0; - } - - if(pos + size > cfg->size) - { - size = cfg->size - pos; - } - - msg[0].addr = cfg->addr; - msg[0].flags = cfg->flags | RT_I2C_WR; - mem_addr[0] = (pos >> 8); - mem_addr[1] = (rt_uint8_t) pos; - msg[0].buf = (rt_uint8_t *) mem_addr; - msg[0].len = 2; - - msg[1].addr = cfg->addr; - msg[1].flags = cfg->flags | RT_I2C_WR | RT_I2C_NO_START; - msg[1].buf = (rt_uint8_t *) buffer; - msg[1].len = size; - - ret = rt_i2c_transfer(fm24clxx->bus, msg, 2); - return (ret == 2) ? size : 0; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device fm24clxx_ops = -{ - fm24clxx_init, - fm24clxx_open, - fm24clxx_close, - fm24clxx_read, - fm24clxx_write, - fm24clxx_control -}; -#endif - -rt_err_t fm24clxx_register(const char *fm_device_name, const char *i2c_bus, void *user_data) -{ - static struct fm24clxx_device fm24clxx_drv; - struct rt_i2c_bus_device *bus; - - bus = rt_i2c_bus_device_find(i2c_bus); - if (bus == RT_NULL) - { - return RT_ENOSYS; - } - - fm24clxx_drv.bus = bus; - fm24clxx_drv.parent.type = RT_Device_Class_Block; -#ifdef RT_USING_DEVICE_OPS - fm24clxx_drv.parent.ops = &fm24clxx_ops; -#else - fm24clxx_drv.parent.init = fm24clxx_init; - fm24clxx_drv.parent.open = fm24clxx_open; - fm24clxx_drv.parent.close = fm24clxx_close; - fm24clxx_drv.parent.read = fm24clxx_read; - fm24clxx_drv.parent.write = fm24clxx_write; - fm24clxx_drv.parent.control = fm24clxx_control; -#endif - - fm24clxx_drv.parent.user_data = user_data; - - return rt_device_register(&fm24clxx_drv.parent, fm_device_name, RT_DEVICE_FLAG_RDWR); -}
\ No newline at end of file diff --git a/components/drivers/i2c/fm24clxx.h b/components/drivers/i2c/fm24clxx.h deleted file mode 100644 index 74ee065470..0000000000 --- a/components/drivers/i2c/fm24clxx.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017-07-14 aubr.cool 1st version - */ - -#ifndef __FM24CLXX_H__ -#define __FM24CLXX_H__ - -#include <rtthread.h> - -struct fm24clxx_config -{ - rt_uint32_t size; - rt_uint16_t addr; - rt_uint16_t flags; -}; - -extern rt_err_t fm24clxx_register(const char *e2m_device_name, - const char *i2c_bus, void *user_data); - -#endif /*__FM24CLXX_H__*/
\ No newline at end of file diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c index e76ac51f12..dba20f6555 100755..100644 --- a/components/drivers/sensors/sensor.c +++ b/components/drivers/sensors/sensor.c @@ -30,7 +30,7 @@ static char *const sensor_name_str[] = "hr_", /* Heart Rate */ "tvoc_", /* TVOC Level */ "noi_", /* Noise Loudness */ - "step_" /* Step sensor */ + "step_", /* Step sensor */ "forc_" /* Force sensor */ }; @@ -195,6 +195,11 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) } else { + if (sensor->module) + { + /* release the module mutex */ + rt_mutex_release(sensor->module->lock); + } return -RT_EINVAL; } diff --git a/components/drivers/spi/SConscript b/components/drivers/spi/SConscript index b88671ebc9..e19f30861f 100644 --- a/components/drivers/spi/SConscript +++ b/components/drivers/spi/SConscript @@ -14,24 +14,9 @@ src_device = [] if GetDepend('RT_USING_SPI_WIFI'): src_device += ['spi_wifi_rw009.c'] -if GetDepend('RT_USING_W25QXX'): - src_device += ['spi_flash_w25qxx.c'] - -if GetDepend('RT_USING_W25QXX_MTD'): - src_device += ['spi_flash_w25qxx_mtd.c'] - if GetDepend('RT_USING_ENC28J60'): src_device += ['enc28j60.c'] -if GetDepend('RT_USING_AT45DBXX'): - src_device += ['spi_flash_at45dbxx.c'] - -if GetDepend('RT_USING_SST25VFXX'): - src_device += ['spi_flash_sst25vfxx.c'] - -if GetDepend('RT_USING_GD'): - src_device += ['spi_flash_gd.c'] - if GetDepend('RT_USING_SPI_MSD'): src_device += ['spi_msd.c'] diff --git a/components/drivers/spi/device_driver_list.txt b/components/drivers/spi/device_driver_list.txt index 2c9d6a16b8..939baa7531 100644 --- a/components/drivers/spi/device_driver_list.txt +++ b/components/drivers/spi/device_driver_list.txt @@ -5,11 +5,5 @@ http://www.rt-thread.com/ enc28j60.c/enc28j60.h http://www.microchip.com/ -spi_flash_at45dbxx.c/spi_flash_at45dbxx.h -http://www.atmel.com/ - -spi_flash_sst25vfxx.c/spi_flash_sst25vfxx.h -http://www.microchip.com/ - -spi_flash_w25qxx.c/spi_flash_w25qxx.h -http://www.winbond.com/ +spi_flash_sfud: Serial Flash Universal Driver +https://github.com/armink/SFUD
\ No newline at end of file diff --git a/components/drivers/spi/spi_flash_at45dbxx.c b/components/drivers/spi/spi_flash_at45dbxx.c deleted file mode 100644 index 0a30f0a328..0000000000 --- a/components/drivers/spi/spi_flash_at45dbxx.c +++ /dev/null @@ -1,433 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - */ - -#include <stdint.h> -#include "spi_flash_at45dbxx.h" - -#define FLASH_DEBUG -#define DMA_BUFFER_SIZE 512 - -#ifdef FLASH_DEBUG -#define FLASH_TRACE rt_kprintf -#else -#define FLASH_TRACE(...) -#endif /**< #ifdef FLASH_DEBUG */ - -/* JEDEC Manufacturer’s ID */ -#define MF_ID (0x1F) /* atmel */ -#define DENSITY_CODE_011D (0x02) /* AT45DB011D Density Code : 00010 = 1-Mbit */ -#define DENSITY_CODE_021D (0x03) /* AT45DB021D Density Code : 00011 = 2-Mbit */ -#define DENSITY_CODE_041D (0x04) /* AT45DB041D Density Code : 00100 = 4-Mbit */ -#define DENSITY_CODE_081D (0x05) /* AT45DB081D Density Code : 00101 = 8-Mbit */ -#define DENSITY_CODE_161D (0x06) /* AT45DB161D Density Code : 00110 = 16-Mbit */ -#define DENSITY_CODE_321D (0x07) /* AT45DB321D Density Code : 00111 = 32-Mbit */ -#define DENSITY_CODE_642D (0x08) /* AT45DB642D Density Code : 01000 = 64-Mbit */ - -struct JEDEC_ID -{ - uint8_t manufacturer_id; /* Manufacturer ID */ - uint8_t density_code:5; /* Density Code */ - uint8_t family_code:3; /* Family Code */ - uint8_t version_code:5; /* Product Version Code */ - uint8_t mlc_code:3; /* MLC Code */ - uint8_t byte_count; /* Byte Count */ -}; - -#define AT45DB_BUFFER_1_WRITE 0x84 -#define AT45DB_BUFFER_2_WRITE 0x87 -#define AT45DB_BUFFER_1_READ 0xD4 -#define AT45DB_BUFFER_2_READ 0xD6 -#define AT45DB_B1_TO_MM_PAGE_PROG_WITH_ERASE 0x83 -#define AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE 0x86 -#define AT45DB_MM_PAGE_TO_B1_XFER 0x53 -#define AT45DB_MM_PAGE_TO_B2_XFER 0x55 -#define AT45DB_PAGE_ERASE 0x81 -#define AT45DB_SECTOR_ERASE 0x7C -#define AT45DB_READ_STATE_REGISTER 0xD7 -#define AT45DB_MM_PAGE_READ 0xD2 -#define AT45DB_MM_PAGE_PROG_THRU_BUFFER1 0x82 -#define AT45DB_CMD_JEDEC_ID 0x9F - -static struct spi_flash_at45dbxx spi_flash_at45dbxx; - -/*****************************************************************************/ -/*Status Register Format: */ -/* ------------------------------------------------------------------------- */ -/* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */ -/* |--------|--------|--------|--------|--------|--------|--------|--------| */ -/* |RDY/BUSY| COMP | device density | X | X | */ -/* ------------------------------------------------------------------------- */ -/* 0:busy | | AT45DB041:0111 | protect|page size */ -/* 1:ready | | AT45DB161:1011 | */ -/* --------------------------------------------------------------------------*/ -/*****************************************************************************/ -static uint8_t AT45DB_StatusRegisterRead(void) -{ - return rt_spi_sendrecv8(spi_flash_at45dbxx.rt_spi_device, AT45DB_READ_STATE_REGISTER); -} - -static void wait_busy(void) -{ - uint16_t i = 0; - while (i++ < 10000) - { - if (AT45DB_StatusRegisterRead() & 0x80) - { - return; - } - } - FLASH_TRACE("\r\nSPI_FLASH timeout!!!\r\n"); -} - -/* RT-Thread Device Driver Interface */ -static rt_err_t AT45DB_flash_init(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t AT45DB_flash_open(rt_device_t dev, rt_uint16_t oflag) -{ - - return RT_EOK; -} - -static rt_err_t AT45DB_flash_close(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t AT45DB_flash_control(rt_device_t dev, int cmd, void *args) -{ - RT_ASSERT(dev != RT_NULL); - - if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) - { - struct rt_device_blk_geometry *geometry; - - geometry = (struct rt_device_blk_geometry *)args; - if (geometry == RT_NULL) return -RT_ERROR; - - geometry->bytes_per_sector = 512; - geometry->sector_count = 4096; - geometry->block_size = 4096; /* block erase: 4k */ - } - - return RT_EOK; -} - -static rt_size_t AT45DB_flash_read_page_256(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) -{ - uint32_t index, nr; - uint8_t * read_buffer = buffer; - uint32_t page = pos; - - nr = size; - - for (index = 0; index < nr; index++) - { - uint8_t send_buffer[8]; - uint32_t i; - - for(i=0; i<sizeof(send_buffer); i++) - { - send_buffer[i] = 0; - } - - send_buffer[0] = AT45DB_MM_PAGE_READ; - send_buffer[1] = (uint8_t)(page >> 7); - send_buffer[2] = (uint8_t)(page << 1); - - rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, send_buffer, 8, read_buffer, 256); - read_buffer += 256; - page++; - } - - return size; -} - -static rt_size_t AT45DB_flash_read_page_512(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) -{ - uint32_t index, nr; - uint8_t * read_buffer = buffer; - uint32_t page = pos; - - nr = size; - - for (index = 0; index < nr; index++) - { - uint8_t send_buffer[8]; - uint32_t i; - - for(i=0; i<sizeof(send_buffer); i++) - { - send_buffer[i] = 0; - } - - send_buffer[0] = AT45DB_MM_PAGE_READ; - send_buffer[1] = (uint8_t)(page >> 6); - send_buffer[2] = (uint8_t)(page << 2); - - rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, send_buffer, 8, read_buffer, 512); - read_buffer += 512; - page++; - } - - return size; -} - -static rt_size_t AT45DB_flash_read_page_1024(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) -{ - uint32_t index, nr; - uint8_t * read_buffer = buffer; - uint32_t page = pos; - - nr = size; - - for (index = 0; index < nr; index++) - { - uint8_t send_buffer[8]; - uint32_t i; - - for(i=0; i<sizeof(send_buffer); i++) - { - send_buffer[i] = 0; - } - - send_buffer[0] = AT45DB_MM_PAGE_READ; - send_buffer[1] = (uint8_t)(page >> 5); - send_buffer[2] = (uint8_t)(page << 3); - - rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, send_buffer, 8, read_buffer, 1024); - read_buffer += 1024; - page++; - } - - return size; -} - -static rt_size_t AT45DB_flash_write_page_256(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) -{ - rt_uint32_t index, nr; - const uint8_t * write_buffer = buffer; - uint32_t page = pos; - - nr = size; - - for (index = 0; index < nr; index++) - { - uint8_t send_buffer[4]; - - send_buffer[0] = AT45DB_MM_PAGE_PROG_THRU_BUFFER1; - send_buffer[1] = (uint8_t) (page >> 7); - send_buffer[2] = (uint8_t) (page << 1); - send_buffer[3] = 0; - - rt_spi_send_then_send(spi_flash_at45dbxx.rt_spi_device, send_buffer, 4, write_buffer, 256); - - write_buffer += 256; - page++; - - wait_busy(); - } - - return size; -} - -static rt_size_t AT45DB_flash_write_page_512(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) -{ - rt_uint32_t index, nr; - const uint8_t * write_buffer = buffer; - uint32_t page = pos; - - nr = size; - - for (index = 0; index < nr; index++) - { - uint8_t send_buffer[4]; - - send_buffer[0] = AT45DB_MM_PAGE_PROG_THRU_BUFFER1; - send_buffer[1] = (uint8_t) (page >> 6); - send_buffer[2] = (uint8_t) (page << 2); - send_buffer[3] = 0; - - rt_spi_send_then_send(spi_flash_at45dbxx.rt_spi_device, send_buffer, 4, write_buffer, 512); - - write_buffer += 512; - page++; - - wait_busy(); - } - - return size; -} - -static rt_size_t AT45DB_flash_write_page_1024(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) -{ - rt_uint32_t index, nr; - const uint8_t * write_buffer = buffer; - uint32_t page = pos; - - nr = size; - - for (index = 0; index < nr; index++) - { - uint8_t send_buffer[4]; - - send_buffer[0] = AT45DB_MM_PAGE_PROG_THRU_BUFFER1; - send_buffer[1] = (uint8_t) (page >> 5); - send_buffer[2] = (uint8_t) (page << 3); - send_buffer[3] = 0; - - rt_spi_send_then_send(spi_flash_at45dbxx.rt_spi_device, send_buffer, 4, write_buffer, 1024); - - write_buffer += 1024; - page++; - - wait_busy(); - } - - return size; -} - -rt_err_t at45dbxx_init(const char * flash_device_name, const char * spi_device_name) -{ - struct rt_spi_device * rt_spi_device; - struct JEDEC_ID * JEDEC_ID; - - rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); - if(rt_spi_device == RT_NULL) - { - FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); - return -RT_ENOSYS; - } - spi_flash_at45dbxx.rt_spi_device = rt_spi_device; - - /* config spi */ - { - struct rt_spi_configuration cfg; - cfg.data_width = 8; - cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 and 3 */ - cfg.max_hz = 66000000; /* Atmel RapidS Serial Interface: 66MHz Maximum Clock Frequency */ - rt_spi_configure(spi_flash_at45dbxx.rt_spi_device, &cfg); - } - - /* read JEDEC ID */ - { - uint8_t cmd; - uint8_t id_recv[6]; - JEDEC_ID = (struct JEDEC_ID *)id_recv; - - cmd = AT45DB_CMD_JEDEC_ID; - rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, &cmd, 1, id_recv, 6); - - /**< 1FH = Atmel */ - /**< 001 = Atmel DataFlash */ - if(JEDEC_ID->manufacturer_id != 0x1F || JEDEC_ID->family_code != 0x01) - { - FLASH_TRACE("Manufacturer’s ID or Memory Type error!\r\n"); - FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]); - return -RT_ENOSYS; - } - - if(JEDEC_ID->density_code == DENSITY_CODE_011D) - { - /**< AT45DB011D Density Code : 00010 = 1-Mbit */ - FLASH_TRACE("AT45DB011D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */ - spi_flash_at45dbxx.geometry.sector_count = 512; /* 1-Mbit / 8 / 256 = 512 */ - spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */ - } - else if(JEDEC_ID->density_code == DENSITY_CODE_021D) - { - /**< AT45DB021D Density Code : 00011 = 2-Mbit */ - FLASH_TRACE("AT45DB021D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */ - spi_flash_at45dbxx.geometry.sector_count = 512*2; /* 2-Mbit / 8 / 256 = 1024 */ - spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */ - } - else if(JEDEC_ID->density_code == DENSITY_CODE_041D) - { - /**< AT45DB041D Density Code : 00100 = 4-Mbit */ - FLASH_TRACE("AT45DB041D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */ - spi_flash_at45dbxx.geometry.sector_count = 512*4; /* 4-Mbit / 8 / 256 = 2048 */ - spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */ - } - else if(JEDEC_ID->density_code == DENSITY_CODE_081D) - { - /**< AT45DB081D Density Code : 00101 = 8-Mbit */ - FLASH_TRACE("AT45DB081D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */ - spi_flash_at45dbxx.geometry.sector_count = 512*8; /* 8-Mbit / 8 / 256 = 4096 */ - spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */ - } - else if(JEDEC_ID->density_code == DENSITY_CODE_161D) - { - /**< AT45DB161D Density Code : 00110 = 16-Mbit */ - FLASH_TRACE("AT45DB161D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 512; /* Page Erase (512 Bytes) */ - spi_flash_at45dbxx.geometry.sector_count = 256*16; /* 16-Mbit / 8 / 512 = 4096 */ - spi_flash_at45dbxx.geometry.block_size = 1024*4; /* Block Erase (4-Kbytes) */ - } - else if(JEDEC_ID->density_code == DENSITY_CODE_321D) - { - /**< AT45DB321D Density Code : 00111 = 32-Mbit */ - FLASH_TRACE("AT45DB321D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 512; /* Page Erase (512 Bytes) */ - spi_flash_at45dbxx.geometry.sector_count = 256*32; /* 32-Mbit / 8 / 512 = 8192 */ - spi_flash_at45dbxx.geometry.block_size = 1024*4; /* Block Erase (4-Kbytes) */ - } - else if(JEDEC_ID->density_code == DENSITY_CODE_642D) - { - /**< AT45DB642D Density Code : 01000 = 64-Mbit */ - FLASH_TRACE("AT45DB642D detection\r\n"); - spi_flash_at45dbxx.geometry.bytes_per_sector = 1024; /* Page Erase (1 Kbyte) */ - spi_flash_at45dbxx.geometry.sector_count = 128*64; /* 64-Mbit / 8 / 1024 = 8192 */ - spi_flash_at45dbxx.geometry.block_size = 1024*8; /* Block Erase (8 Kbytes) */ - } - else - { - FLASH_TRACE("Memory Capacity error!\r\n"); - return -RT_ENOSYS; - } - } - - /* register device */ - spi_flash_at45dbxx.flash_device.type = RT_Device_Class_Block; - spi_flash_at45dbxx.flash_device.init = AT45DB_flash_init; - spi_flash_at45dbxx.flash_device.open = AT45DB_flash_open; - spi_flash_at45dbxx.flash_device.close = AT45DB_flash_close; - spi_flash_at45dbxx.flash_device.control = AT45DB_flash_control; - - if(JEDEC_ID->density_code == DENSITY_CODE_642D) - { - spi_flash_at45dbxx.flash_device.read = AT45DB_flash_read_page_1024; - spi_flash_at45dbxx.flash_device.write = AT45DB_flash_write_page_1024; - } - else if(JEDEC_ID->density_code == DENSITY_CODE_161D - || JEDEC_ID->density_code == DENSITY_CODE_321D ) - { - spi_flash_at45dbxx.flash_device.read = AT45DB_flash_read_page_512; - spi_flash_at45dbxx.flash_device.write = AT45DB_flash_write_page_512; - } - else - { - spi_flash_at45dbxx.flash_device.read = AT45DB_flash_read_page_256; - spi_flash_at45dbxx.flash_device.write = AT45DB_flash_write_page_256; - } - - /* no private */ - spi_flash_at45dbxx.flash_device.user_data = RT_NULL; - - rt_device_register(&spi_flash_at45dbxx.flash_device, flash_device_name, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); - - return RT_EOK; -} diff --git a/components/drivers/spi/spi_flash_at45dbxx.h b/components/drivers/spi/spi_flash_at45dbxx.h deleted file mode 100644 index d1bc1a2aeb..0000000000 --- a/components/drivers/spi/spi_flash_at45dbxx.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - */ - -#ifndef SPI_FLASH_AT45DBXX_H_INCLUDED -#define SPI_FLASH_AT45DBXX_H_INCLUDED - -#include <rtthread.h> -#include <drivers/spi.h> - -struct spi_flash_at45dbxx -{ - struct rt_device flash_device; - struct rt_device_blk_geometry geometry; - struct rt_spi_device * rt_spi_device; -}; - -extern rt_err_t at45dbxx_init(const char * flash_device_name, const char * spi_device_name); - - -#endif // SPI_FLASH_AT45DBXX_H_INCLUDED diff --git a/components/drivers/spi/spi_flash_gd.c b/components/drivers/spi/spi_flash_gd.c deleted file mode 100644 index 3c52035372..0000000000 --- a/components/drivers/spi/spi_flash_gd.c +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-10-11 fullhan copy from winbond flash - */ - -#include <stdint.h> -#include <rtthread.h> -#include <rtdevice.h> - -#include "spi_flash.h" -#include "spi_flash_gd.h" - -#define FLASH_DEBUG - -#ifdef FLASH_DEBUG -#define FLASH_TRACE rt_kprintf -#else -#define FLASH_TRACE(...) -#endif /* #ifdef FLASH_DEBUG */ - -#define PAGE_SIZE 4096 - -/* JEDEC Manufacturer's ID */ -#define MF_ID (0xC8) -/* JEDEC Device ID: Memory type and Capacity */ -#define MTC_GD25Q128 (0x4018) - -/* command list */ -#define CMD_WRSR (0x01) /* Write Status Register */ -#define CMD_PP (0x02) /* Page Program */ -#define CMD_READ (0x03) /* Read Data */ -#define CMD_WRDI (0x04) /* Write Disable */ -#define CMD_RDSR1 (0x05) /* Read Status Register-1 */ -#define CMD_WREN (0x06) /* Write Enable */ -#define CMD_FAST_READ (0x0B) /* Fast Read */ -#define CMD_ERASE_4K (0x20) /* Sector Erase:4K */ -#define CMD_RDSR2 (0x35) /* Read Status Register-2 */ -#define CMD_ERASE_32K (0x52) /* 32KB Block Erase */ -#define CMD_JEDEC_ID (0x9F) /* Read JEDEC ID */ -#define CMD_ERASE_full (0xC7) /* Chip Erase */ -#define CMD_ERASE_64K (0xD8) /* 64KB Block Erase */ - -#define DUMMY (0xFF) - -static struct spi_flash_device spi_flash_device; - -static void flash_lock(struct spi_flash_device * flash_device) -{ - rt_mutex_take(&flash_device->lock, RT_WAITING_FOREVER); -} - -static void flash_unlock(struct spi_flash_device * flash_device) -{ - rt_mutex_release(&flash_device->lock); -} - -static uint8_t w25qxx_read_status(void) -{ - return rt_spi_sendrecv8(spi_flash_device.rt_spi_device, CMD_RDSR1); -} - -static void w25qxx_wait_busy(void) -{ - while( w25qxx_read_status() & (0x01)); -} - -/** \brief read [size] byte from [offset] to [buffer] - * - * \param offset uint32_t unit : byte - * \param buffer uint8_t* - * \param size uint32_t unit : byte - * \return uint32_t byte for read - * - */ -static uint32_t w25qxx_read(uint32_t offset, uint8_t * buffer, uint32_t size) -{ - uint8_t send_buffer[4]; - - send_buffer[0] = CMD_WRDI; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_READ; - send_buffer[1] = (uint8_t)(offset>>16); - send_buffer[2] = (uint8_t)(offset>>8); - send_buffer[3] = (uint8_t)(offset); - - rt_spi_send_then_recv(spi_flash_device.rt_spi_device, - send_buffer, 4, - buffer, size); - - return size; -} - -/** \brief write N page on [page] - * - * \param page_addr uint32_t unit : byte (4096 * N,1 page = 4096byte) - * \param buffer const uint8_t* - * \return uint32_t - * - */ -static uint32_t w25qxx_page_write(uint32_t page_addr, const uint8_t* buffer) -{ - uint32_t index; - uint8_t send_buffer[4]; - - RT_ASSERT((page_addr&0xFF) == 0); /* page addr must align to 256byte. */ - - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_ERASE_4K; - send_buffer[1] = (page_addr >> 16); - send_buffer[2] = (page_addr >> 8); - send_buffer[3] = (page_addr); - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 4); - - w25qxx_wait_busy(); // wait erase done. - - for(index=0; index < (PAGE_SIZE / 256); index++) - { - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_PP; - send_buffer[1] = (uint8_t)(page_addr >> 16); - send_buffer[2] = (uint8_t)(page_addr >> 8); - send_buffer[3] = (uint8_t)(page_addr); - - rt_spi_send_then_send(spi_flash_device.rt_spi_device, - send_buffer, - 4, - buffer, - 256); - - buffer += 256; - page_addr += 256; - w25qxx_wait_busy(); - } - - send_buffer[0] = CMD_WRDI; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - return PAGE_SIZE; -} - -/* RT-Thread device interface */ -static rt_err_t w25qxx_flash_init(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t w25qxx_flash_open(rt_device_t dev, rt_uint16_t oflag) -{ - uint8_t send_buffer[3]; - - flash_lock((struct spi_flash_device *)dev); - - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_WRSR; - send_buffer[1] = 0; - send_buffer[2] = 0; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 3); - - w25qxx_wait_busy(); - - flash_unlock((struct spi_flash_device *)dev); - - return RT_EOK; -} - -static rt_err_t w25qxx_flash_close(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t w25qxx_flash_control(rt_device_t dev, int cmd, void *args) -{ - RT_ASSERT(dev != RT_NULL); - - if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) - { - struct rt_device_blk_geometry *geometry; - - geometry = (struct rt_device_blk_geometry *)args; - if (geometry == RT_NULL) return -RT_ERROR; - - geometry->bytes_per_sector = spi_flash_device.geometry.bytes_per_sector; - geometry->sector_count = spi_flash_device.geometry.sector_count; - geometry->block_size = spi_flash_device.geometry.block_size; - } - - return RT_EOK; -} - -static rt_size_t w25qxx_flash_read(rt_device_t dev, - rt_off_t pos, - void* buffer, - rt_size_t size) -{ - flash_lock((struct spi_flash_device *)dev); - - w25qxx_read(pos*spi_flash_device.geometry.bytes_per_sector, - buffer, - size*spi_flash_device.geometry.bytes_per_sector); - - flash_unlock((struct spi_flash_device *)dev); - - return size; -} - -static rt_size_t w25qxx_flash_write(rt_device_t dev, - rt_off_t pos, - const void* buffer, - rt_size_t size) -{ - rt_size_t i = 0; - rt_size_t block = size; - const uint8_t * ptr = buffer; - - flash_lock((struct spi_flash_device *)dev); - - while(block--) - { - w25qxx_page_write((pos + i)*spi_flash_device.geometry.bytes_per_sector, - ptr); - ptr += PAGE_SIZE; - i++; - } - - flash_unlock((struct spi_flash_device *)dev); - - return size; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops gd_device_ops = -{ - w25qxx_flash_init, - w25qxx_flash_open, - w25qxx_flash_close, - w25qxx_flash_read, - w25qxx_flash_write, - w25qxx_flash_control -}; -#endif - -rt_err_t gd_init(const char * flash_device_name, const char * spi_device_name) -{ - struct rt_spi_device * rt_spi_device; - - /* initialize mutex */ - if (rt_mutex_init(&spi_flash_device.lock, spi_device_name, RT_IPC_FLAG_FIFO) != RT_EOK) - { - rt_kprintf("init sd lock mutex failed\n"); - return -RT_ENOSYS; - } - - rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); - if(rt_spi_device == RT_NULL) - { - FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); - return -RT_ENOSYS; - } - spi_flash_device.rt_spi_device = rt_spi_device; - - /* config spi */ - { - struct rt_spi_configuration cfg; - cfg.data_width = 8; - cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ - cfg.max_hz = 50 * 1000 * 1000; /* 50M */ - rt_spi_configure(spi_flash_device.rt_spi_device, &cfg); - } - - /* init flash */ - { - rt_uint8_t cmd; - rt_uint8_t id_recv[3]; - uint16_t memory_type_capacity; - - flash_lock(&spi_flash_device); - - cmd = 0xFF; /* reset SPI FLASH, cancel all cmd in processing. */ - rt_spi_send(spi_flash_device.rt_spi_device, &cmd, 1); - - cmd = CMD_WRDI; - rt_spi_send(spi_flash_device.rt_spi_device, &cmd, 1); - - /* read flash id */ - cmd = CMD_JEDEC_ID; - rt_spi_send_then_recv(spi_flash_device.rt_spi_device, &cmd, 1, id_recv, 3); - - flash_unlock(&spi_flash_device); - - if(id_recv[0] != MF_ID) - { - FLASH_TRACE("Manufacturers ID error!\r\n"); - FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]); - return -RT_ENOSYS; - } - - spi_flash_device.geometry.bytes_per_sector = 4096; - spi_flash_device.geometry.block_size = 4096; /* block erase: 4k */ - - /* get memory type and capacity */ - memory_type_capacity = id_recv[1]; - memory_type_capacity = (memory_type_capacity << 8) | id_recv[2]; - - if(memory_type_capacity == MTC_GD25Q128) - { - FLASH_TRACE("GD128 detection\r\n"); - spi_flash_device.geometry.sector_count = 4096; - } - else - { - FLASH_TRACE("Memory Capacity error!\r\n"); - return -RT_ENOSYS; - } - } - - /* register device */ - spi_flash_device.flash_device.type = RT_Device_Class_Block; -#ifdef RT_USING_DEVICE_OPS - spi_flash_device.flash_device.ops = &gd_device_ops; -#else - spi_flash_device.flash_device.init = w25qxx_flash_init; - spi_flash_device.flash_device.open = w25qxx_flash_open; - spi_flash_device.flash_device.close = w25qxx_flash_close; - spi_flash_device.flash_device.read = w25qxx_flash_read; - spi_flash_device.flash_device.write = w25qxx_flash_write; - spi_flash_device.flash_device.control = w25qxx_flash_control; -#endif - /* no private */ - spi_flash_device.flash_device.user_data = RT_NULL; - - rt_device_register(&spi_flash_device.flash_device, flash_device_name, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); - - return RT_EOK; -} diff --git a/components/drivers/spi/spi_flash_gd.h b/components/drivers/spi/spi_flash_gd.h deleted file mode 100644 index 904d976ed0..0000000000 --- a/components/drivers/spi/spi_flash_gd.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-10-11 fullhan copy from winbond flash - */ - -#ifndef SPI_FLASH_GD_H_ -#define SPI_FLASH_GD_H_ - -#include <rtthread.h> - -extern rt_err_t gd_init(const char * flash_device_name, const char * spi_device_name); - -#endif /* SPI_FLASH_GD_H_ */ diff --git a/components/drivers/spi/spi_flash_sst25vfxx.c b/components/drivers/spi/spi_flash_sst25vfxx.c deleted file mode 100644 index bd618a8e9b..0000000000 --- a/components/drivers/spi/spi_flash_sst25vfxx.c +++ /dev/null @@ -1,358 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - */ - -#include <stdint.h> -#include "spi_flash_sst25vfxx.h" - -#define FLASH_DEBUG - -#ifdef FLASH_DEBUG -#define FLASH_TRACE rt_kprintf -#else -#define FLASH_TRACE(...) -#endif /* #ifdef FLASH_DEBUG */ - -/* JEDEC Manufacturer’s ID */ -#define MF_ID (0xBF) -/* JEDEC Device ID : Memory Type */ -#define MT_ID (0x25) -/* JEDEC Device ID: Memory Capacity */ -#define MC_ID_SST25VF020B (0x8C) /* 2Mbit */ -#define MC_ID_SST25VF040B (0x8D) /* 4Mbit */ -#define MC_ID_SST25VF080B (0x8E) /* 8Mbit */ -#define MC_ID_SST25VF016B (0x41) /* 16Mbit */ -#define MC_ID_SST25VF032B (0x4A) /* 32Mbit */ -#define MC_ID_SST25VF064C (0x4B) /* 64Mbit */ - -/* command list */ -#define CMD_RDSR (0x05) -#define CMD_WRSR (0x01) -#define CMD_EWSR (0x50) -#define CMD_WRDI (0x04) -#define CMD_WREN (0x06) -#define CMD_READ (0x03) -#define CMD_FAST_READ (0x0B) -#define CMD_BP (0x02) -#define CMD_AAIP (0xAD) -#define CMD_ERASE_4K (0x20) -#define CMD_ERASE_32K (0x52) -#define CMD_ERASE_64K (0xD8) -#define CMD_ERASE_full (0xC7) -#define CMD_JEDEC_ID (0x9F) -#define CMD_EBSY (0x70) -#define CMD_DBSY (0x80) - -#define DUMMY (0xFF) - -static struct spi_flash_sst25vfxx spi_flash_sst25vfxx; - -static uint8_t sst25vfxx_read_status(struct spi_flash_sst25vfxx * spi_flash) -{ - return rt_spi_sendrecv8(spi_flash->rt_spi_device, CMD_RDSR); -} - -static void sst25vfxx_wait_busy(struct spi_flash_sst25vfxx * spi_flash) -{ - while( sst25vfxx_read_status(spi_flash) & (0x01)); -} - -/** \brief write N page on [page] - * - * \param page uint32_t unit : byte (4096 * N,1 page = 4096byte) - * \param buffer const uint8_t* - * \param size uint32_t unit : byte ( 4096*N ) - * \return uint32_t - * - */ -static uint32_t sst25vfxx_page_write(struct spi_flash_sst25vfxx * spi_flash, uint32_t page, const uint8_t * buffer, uint32_t size) -{ - uint32_t index; - uint32_t need_wirte = size; - uint8_t send_buffer[6]; - - page &= ~0xFFF; // page size = 4096byte - - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_ERASE_4K; - send_buffer[1] = (page >> 16); - send_buffer[2] = (page >> 8); - send_buffer[3] = (page); - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 4); - - sst25vfxx_wait_busy(spi_flash); // wait erase done. - - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_AAIP; - send_buffer[1] = (uint8_t)(page >> 16); - send_buffer[2] = (uint8_t)(page >> 8); - send_buffer[3] = (uint8_t)(page); - send_buffer[4] = *buffer++; - send_buffer[5] = *buffer++; - need_wirte -= 2; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 6); - - sst25vfxx_wait_busy(spi_flash); - - for(index=0; index < need_wirte/2; index++) - { - send_buffer[0] = CMD_AAIP; - send_buffer[1] = *buffer++; - send_buffer[2] = *buffer++; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 3); - sst25vfxx_wait_busy(spi_flash); - } - - send_buffer[0] = CMD_WRDI; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1); - - return size; -} - -/* RT-Thread device interface */ -static rt_err_t sst25vfxx_flash_init(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t sst25vfxx_flash_open(rt_device_t dev, rt_uint16_t oflag) -{ - rt_err_t result; - uint8_t send_buffer[2]; - struct spi_flash_sst25vfxx * spi_flash = (struct spi_flash_sst25vfxx *)dev; - - /* lock spi flash */ - result = rt_mutex_take(&(spi_flash->lock), RT_WAITING_FOREVER); - if(result != RT_EOK) - { - return result; - } - - send_buffer[0] = CMD_DBSY; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_EWSR; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_WRSR; - send_buffer[1] = 0; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 2); - - /* release lock */ - rt_mutex_release(&(spi_flash->lock)); - - return RT_EOK; -} - -static rt_err_t sst25vfxx_flash_close(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t sst25vfxx_flash_control(rt_device_t dev, int cmd, void *args) -{ - struct spi_flash_sst25vfxx * spi_flash; - - spi_flash = (struct spi_flash_sst25vfxx *)dev; - - RT_ASSERT(dev != RT_NULL); - - if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) - { - struct rt_device_blk_geometry *geometry; - - geometry = (struct rt_device_blk_geometry *)args; - if (geometry == RT_NULL) return -RT_ERROR; - - geometry->bytes_per_sector = spi_flash->geometry.bytes_per_sector; - geometry->sector_count = spi_flash->geometry.sector_count; - geometry->block_size = spi_flash->geometry.block_size; - } - - return RT_EOK; -} - -static rt_size_t sst25vfxx_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) -{ - rt_err_t result; - uint8_t send_buffer[4]; - struct spi_flash_sst25vfxx * spi_flash = (struct spi_flash_sst25vfxx *)dev; - uint32_t offset = pos * spi_flash->geometry.bytes_per_sector; - - /* lock spi flash */ - result = rt_mutex_take(&(spi_flash->lock), RT_WAITING_FOREVER); - if(result != RT_EOK) - { - return 0; - } - - send_buffer[0] = CMD_WRDI; - rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_READ; - send_buffer[1] = (uint8_t)(offset>>16); - send_buffer[2] = (uint8_t)(offset>>8); - send_buffer[3] = (uint8_t)(offset); - rt_spi_send_then_recv(spi_flash->rt_spi_device, send_buffer, 4, buffer, size * spi_flash->geometry.bytes_per_sector); - - /* release lock */ - rt_mutex_release(&(spi_flash->lock)); - - return size; -} - -static rt_size_t sst25vfxx_flash_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) -{ - uint32_t i; - rt_err_t result; - const uint8_t * write_buffer = buffer; - struct spi_flash_sst25vfxx * spi_flash = (struct spi_flash_sst25vfxx *)dev; - - /* lock spi flash */ - result = rt_mutex_take(&(spi_flash->lock), RT_WAITING_FOREVER); - if(result != RT_EOK) - { - return 0; - } - - for(i=0; i<size; i++) - { - sst25vfxx_page_write(spi_flash, - (pos + i) * spi_flash->geometry.bytes_per_sector, - write_buffer, - spi_flash->geometry.bytes_per_sector); - write_buffer += spi_flash->geometry.bytes_per_sector; - } - - /* release lock */ - rt_mutex_release(&(spi_flash->lock)); - - return size; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops sst25vfxx_device_ops = -{ - sst25vfxx_flash_init, - sst25vfxx_flash_open, - sst25vfxx_flash_close, - sst25vfxx_flash_read, - sst25vfxx_flash_write, - sst25vfxx_flash_control -}; -#endif - -rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_name) -{ - struct rt_spi_device * rt_spi_device; - struct spi_flash_sst25vfxx * spi_flash = &spi_flash_sst25vfxx; - - rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); - if(rt_spi_device == RT_NULL) - { - FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); - return -RT_ENOSYS; - } - spi_flash->rt_spi_device = rt_spi_device; - - /* config spi */ - { - struct rt_spi_configuration cfg; - cfg.data_width = 8; - cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ - cfg.max_hz = 50000000; /* 50M */ - rt_spi_configure(spi_flash->rt_spi_device, &cfg); - } - - /* init flash */ - { - rt_uint8_t cmd; - rt_uint8_t id_recv[3]; - - cmd = CMD_WRDI; - rt_spi_send(spi_flash->rt_spi_device, &cmd, 1); - - /* read flash id */ - cmd = CMD_JEDEC_ID; - rt_spi_send_then_recv(spi_flash->rt_spi_device, &cmd, 1, id_recv, 3); - - if(id_recv[0] != MF_ID || id_recv[1] != MT_ID) - { - FLASH_TRACE("Manufacturer’s ID or Memory Type error!\r\n"); - FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]); - return -RT_ENOSYS; - } - - spi_flash->geometry.bytes_per_sector = 4096; - spi_flash->geometry.block_size = 4096; /* block erase: 4k */ - - if(id_recv[2] == MC_ID_SST25VF020B) - { - FLASH_TRACE("SST25VF020B detection\r\n"); - spi_flash->geometry.sector_count = 64; - } - else if(id_recv[2] == MC_ID_SST25VF040B) - { - FLASH_TRACE("SST25VF040B detection\r\n"); - spi_flash->geometry.sector_count = 128; - } - else if(id_recv[2] == MC_ID_SST25VF080B) - { - FLASH_TRACE("SST25VF080B detection\r\n"); - spi_flash->geometry.sector_count = 256; - } - else if(id_recv[2] == MC_ID_SST25VF016B) - { - FLASH_TRACE("SST25VF016B detection\r\n"); - spi_flash->geometry.sector_count = 512; - } - else if(id_recv[2] == MC_ID_SST25VF032B) - { - FLASH_TRACE("SST25VF032B detection\r\n"); - spi_flash->geometry.sector_count = 1024; - } - else if(id_recv[2] == MC_ID_SST25VF064C) - { - FLASH_TRACE("SST25VF064C detection\r\n"); - spi_flash->geometry.sector_count = 2048; - } - else - { - FLASH_TRACE("Memory Capacity error!\r\n"); - return -RT_ENOSYS; - } - } - - /* initialize mutex lock */ - rt_mutex_init(&spi_flash->lock, flash_device_name, RT_IPC_FLAG_PRIO); - - /* register device */ - spi_flash->flash_device.type = RT_Device_Class_Block; -#ifdef RT_USING_DEVICE_OPS - spi_flash->flash_device.ops = &sst25vfxx_device_ops; -#else - spi_flash->flash_device.init = sst25vfxx_flash_init; - spi_flash->flash_device.open = sst25vfxx_flash_open; - spi_flash->flash_device.close = sst25vfxx_flash_close; - spi_flash->flash_device.read = sst25vfxx_flash_read; - spi_flash->flash_device.write = sst25vfxx_flash_write; - spi_flash->flash_device.control = sst25vfxx_flash_control; -#endif - /* no private */ - spi_flash->flash_device.user_data = RT_NULL; - - rt_device_register(&spi_flash->flash_device, flash_device_name, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); - - return RT_EOK; -} diff --git a/components/drivers/spi/spi_flash_sst25vfxx.h b/components/drivers/spi/spi_flash_sst25vfxx.h deleted file mode 100644 index 7a7283f753..0000000000 --- a/components/drivers/spi/spi_flash_sst25vfxx.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - */ - -#ifndef SPI_FLASH_SST25VFXX_H_INCLUDED -#define SPI_FLASH_SST25VFXX_H_INCLUDED - -#include <rtthread.h> -#include <drivers/spi.h> - -struct spi_flash_sst25vfxx -{ - struct rt_device flash_device; - struct rt_device_blk_geometry geometry; - struct rt_spi_device * rt_spi_device; - struct rt_mutex lock; -}; - -extern rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_name); - - -#endif // SPI_FLASH_SST25VFXX_H_INCLUDED diff --git a/components/drivers/spi/spi_flash_w25qxx.c b/components/drivers/spi/spi_flash_w25qxx.c deleted file mode 100644 index bba9c0b5cc..0000000000 --- a/components/drivers/spi/spi_flash_w25qxx.c +++ /dev/null @@ -1,393 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - * 2012-05-06 aozima can page write. - * 2012-08-23 aozima add flash lock. - * 2012-08-24 aozima fixed write status register BUG. - */ - -#include <stdint.h> -#include <rtdevice.h> - -#include "spi_flash.h" -#include "spi_flash_w25qxx.h" - -#define FLASH_DEBUG - -#ifdef FLASH_DEBUG -#define FLASH_TRACE rt_kprintf -#else -#define FLASH_TRACE(...) -#endif /* #ifdef FLASH_DEBUG */ - -#define PAGE_SIZE 4096 - -/* JEDEC Manufacturer ID */ -#define MF_ID (0xEF) - -/* JEDEC Device ID: Memory type and Capacity */ -#define MTC_W25Q80_BV (0x4014) /* W25Q80BV */ -#define MTC_W25Q16_BV_CL_CV (0x4015) /* W25Q16BV W25Q16CL W25Q16CV */ -#define MTC_W25Q16_DW (0x6015) /* W25Q16DW */ -#define MTC_W25Q32_BV (0x4016) /* W25Q32BV */ -#define MTC_W25Q32_DW (0x6016) /* W25Q32DW */ -#define MTC_W25Q64_BV_CV (0x4017) /* W25Q64BV W25Q64CV */ -#define MTC_W25Q64_DW (0x4017) /* W25Q64DW */ -#define MTC_W25Q128_BV (0x4018) /* W25Q128BV */ -#define MTC_W25Q256_FV (TBD) /* W25Q256FV */ - -/* command list */ -#define CMD_WRSR (0x01) /* Write Status Register */ -#define CMD_PP (0x02) /* Page Program */ -#define CMD_READ (0x03) /* Read Data */ -#define CMD_WRDI (0x04) /* Write Disable */ -#define CMD_RDSR1 (0x05) /* Read Status Register-1 */ -#define CMD_WREN (0x06) /* Write Enable */ -#define CMD_FAST_READ (0x0B) /* Fast Read */ -#define CMD_ERASE_4K (0x20) /* Sector Erase:4K */ -#define CMD_RDSR2 (0x35) /* Read Status Register-2 */ -#define CMD_ERASE_32K (0x52) /* 32KB Block Erase */ -#define CMD_JEDEC_ID (0x9F) /* Read JEDEC ID */ -#define CMD_ERASE_full (0xC7) /* Chip Erase */ -#define CMD_ERASE_64K (0xD8) /* 64KB Block Erase */ - -#define DUMMY (0xFF) - -static struct spi_flash_device spi_flash_device; - -static void flash_lock(struct spi_flash_device * flash_device) -{ - rt_mutex_take(&flash_device->lock, RT_WAITING_FOREVER); -} - -static void flash_unlock(struct spi_flash_device * flash_device) -{ - rt_mutex_release(&flash_device->lock); -} - -static uint8_t w25qxx_read_status(void) -{ - return rt_spi_sendrecv8(spi_flash_device.rt_spi_device, CMD_RDSR1); -} - -static void w25qxx_wait_busy(void) -{ - while( w25qxx_read_status() & (0x01)); -} - -/** \brief read [size] byte from [offset] to [buffer] - * - * \param offset uint32_t unit : byte - * \param buffer uint8_t* - * \param size uint32_t unit : byte - * \return uint32_t byte for read - * - */ -static uint32_t w25qxx_read(uint32_t offset, uint8_t * buffer, uint32_t size) -{ - uint8_t send_buffer[4]; - - send_buffer[0] = CMD_WRDI; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_READ; - send_buffer[1] = (uint8_t)(offset>>16); - send_buffer[2] = (uint8_t)(offset>>8); - send_buffer[3] = (uint8_t)(offset); - - rt_spi_send_then_recv(spi_flash_device.rt_spi_device, - send_buffer, 4, - buffer, size); - - return size; -} - -/** \brief write N page on [page] - * - * \param page_addr uint32_t unit : byte (4096 * N,1 page = 4096byte) - * \param buffer const uint8_t* - * \return uint32_t - * - */ -uint32_t w25qxx_page_write(uint32_t page_addr, const uint8_t* buffer) -{ - uint32_t index; - uint8_t send_buffer[4]; - - RT_ASSERT((page_addr&0xFF) == 0); /* page addr must align to 256byte. */ - - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_ERASE_4K; - send_buffer[1] = (page_addr >> 16); - send_buffer[2] = (page_addr >> 8); - send_buffer[3] = (page_addr); - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 4); - - w25qxx_wait_busy(); // wait erase done. - - for(index=0; index < (PAGE_SIZE / 256); index++) - { - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_PP; - send_buffer[1] = (uint8_t)(page_addr >> 16); - send_buffer[2] = (uint8_t)(page_addr >> 8); - send_buffer[3] = (uint8_t)(page_addr); - - rt_spi_send_then_send(spi_flash_device.rt_spi_device, - send_buffer, - 4, - buffer, - 256); - - buffer += 256; - page_addr += 256; - w25qxx_wait_busy(); - } - - send_buffer[0] = CMD_WRDI; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - return PAGE_SIZE; -} - -/* RT-Thread device interface */ -static rt_err_t w25qxx_flash_init(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t w25qxx_flash_open(rt_device_t dev, rt_uint16_t oflag) -{ - uint8_t send_buffer[3]; - - flash_lock((struct spi_flash_device *)dev); - - send_buffer[0] = CMD_WREN; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_WRSR; - send_buffer[1] = 0; - send_buffer[2] = 0; - rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 3); - - w25qxx_wait_busy(); - - flash_unlock((struct spi_flash_device *)dev); - - return RT_EOK; -} - -static rt_err_t w25qxx_flash_close(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t w25qxx_flash_control(rt_device_t dev, int cmd, void *args) -{ - RT_ASSERT(dev != RT_NULL); - - if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) - { - struct rt_device_blk_geometry *geometry; - - geometry = (struct rt_device_blk_geometry *)args; - if (geometry == RT_NULL) return -RT_ERROR; - - geometry->bytes_per_sector = spi_flash_device.geometry.bytes_per_sector; - geometry->sector_count = spi_flash_device.geometry.sector_count; - geometry->block_size = spi_flash_device.geometry.block_size; - } - - return RT_EOK; -} - -static rt_size_t w25qxx_flash_read(rt_device_t dev, - rt_off_t pos, - void* buffer, - rt_size_t size) -{ - flash_lock((struct spi_flash_device *)dev); - - w25qxx_read(pos*spi_flash_device.geometry.bytes_per_sector, - buffer, - size*spi_flash_device.geometry.bytes_per_sector); - - flash_unlock((struct spi_flash_device *)dev); - - return size; -} - -static rt_size_t w25qxx_flash_write(rt_device_t dev, - rt_off_t pos, - const void* buffer, - rt_size_t size) -{ - rt_size_t i = 0; - rt_size_t block = size; - const uint8_t * ptr = buffer; - - flash_lock((struct spi_flash_device *)dev); - - while(block--) - { - w25qxx_page_write((pos + i)*spi_flash_device.geometry.bytes_per_sector, - ptr); - ptr += PAGE_SIZE; - i++; - } - - flash_unlock((struct spi_flash_device *)dev); - - return size; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops w25qxx_device_ops = -{ - w25qxx_flash_init, - w25qxx_flash_open, - w25qxx_flash_close, - w25qxx_flash_read, - w25qxx_flash_write, - w25qxx_flash_control -}; -#endif - -rt_err_t w25qxx_init(const char * flash_device_name, const char * spi_device_name) -{ - struct rt_spi_device * rt_spi_device; - - /* initialize mutex */ - if (rt_mutex_init(&spi_flash_device.lock, spi_device_name, RT_IPC_FLAG_FIFO) != RT_EOK) - { - rt_kprintf("init sd lock mutex failed\n"); - return -RT_ENOSYS; - } - - rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); - if(rt_spi_device == RT_NULL) - { - FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); - return -RT_ENOSYS; - } - spi_flash_device.rt_spi_device = rt_spi_device; - - /* config spi */ - { - struct rt_spi_configuration cfg; - cfg.data_width = 8; - cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ - cfg.max_hz = 50 * 1000 * 1000; /* 50M */ - rt_spi_configure(spi_flash_device.rt_spi_device, &cfg); - } - - /* init flash */ - { - rt_uint8_t cmd; - rt_uint8_t id_recv[3]; - uint16_t memory_type_capacity; - - flash_lock(&spi_flash_device); - - cmd = 0xFF; /* reset SPI FLASH, cancel all cmd in processing. */ - rt_spi_send(spi_flash_device.rt_spi_device, &cmd, 1); - - cmd = CMD_WRDI; - rt_spi_send(spi_flash_device.rt_spi_device, &cmd, 1); - - /* read flash id */ - cmd = CMD_JEDEC_ID; - rt_spi_send_then_recv(spi_flash_device.rt_spi_device, &cmd, 1, id_recv, 3); - - flash_unlock(&spi_flash_device); - - if(id_recv[0] != MF_ID) - { - FLASH_TRACE("Manufacturers ID error!\r\n"); - FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]); - return -RT_ENOSYS; - } - - spi_flash_device.geometry.bytes_per_sector = 4096; - spi_flash_device.geometry.block_size = 4096; /* block erase: 4k */ - - /* get memory type and capacity */ - memory_type_capacity = id_recv[1]; - memory_type_capacity = (memory_type_capacity << 8) | id_recv[2]; - - if(memory_type_capacity == MTC_W25Q128_BV) - { - FLASH_TRACE("W25Q128BV detection\r\n"); - spi_flash_device.geometry.sector_count = 4096; - } - else if(memory_type_capacity == MTC_W25Q64_BV_CV) - { - FLASH_TRACE("W25Q64BV or W25Q64CV detection\r\n"); - spi_flash_device.geometry.sector_count = 2048; - } - else if(memory_type_capacity == MTC_W25Q64_DW) - { - FLASH_TRACE("W25Q64DW detection\r\n"); - spi_flash_device.geometry.sector_count = 2048; - } - else if(memory_type_capacity == MTC_W25Q32_BV) - { - FLASH_TRACE("W25Q32BV detection\r\n"); - spi_flash_device.geometry.sector_count = 1024; - } - else if(memory_type_capacity == MTC_W25Q32_DW) - { - FLASH_TRACE("W25Q32DW detection\r\n"); - spi_flash_device.geometry.sector_count = 1024; - } - else if(memory_type_capacity == MTC_W25Q16_BV_CL_CV) - { - FLASH_TRACE("W25Q16BV or W25Q16CL or W25Q16CV detection\r\n"); - spi_flash_device.geometry.sector_count = 512; - } - else if(memory_type_capacity == MTC_W25Q16_DW) - { - FLASH_TRACE("W25Q16DW detection\r\n"); - spi_flash_device.geometry.sector_count = 512; - } - else if(memory_type_capacity == MTC_W25Q80_BV) - { - FLASH_TRACE("W25Q80BV detection\r\n"); - spi_flash_device.geometry.sector_count = 256; - } - else - { - FLASH_TRACE("Memory Capacity error!\r\n"); - return -RT_ENOSYS; - } - } - - /* register device */ - spi_flash_device.flash_device.type = RT_Device_Class_Block; -#ifdef RT_USING_DEVICE_OPS - spi_flash_device.flash_device.ops = &w25qxx_device_ops; -#else - spi_flash_device.flash_device.init = w25qxx_flash_init; - spi_flash_device.flash_device.open = w25qxx_flash_open; - spi_flash_device.flash_device.close = w25qxx_flash_close; - spi_flash_device.flash_device.read = w25qxx_flash_read; - spi_flash_device.flash_device.write = w25qxx_flash_write; - spi_flash_device.flash_device.control = w25qxx_flash_control; -#endif - /* no private */ - spi_flash_device.flash_device.user_data = RT_NULL; - - rt_device_register(&spi_flash_device.flash_device, flash_device_name, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); - - return RT_EOK; -} diff --git a/components/drivers/spi/spi_flash_w25qxx.h b/components/drivers/spi/spi_flash_w25qxx.h deleted file mode 100644 index 14837bc87c..0000000000 --- a/components/drivers/spi/spi_flash_w25qxx.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - * 2012-08-23 aozima add flash lock. - */ - -#ifndef SPI_FLASH_W25QXX_H_INCLUDED -#define SPI_FLASH_W25QXX_H_INCLUDED - -#include <rtthread.h> - -extern rt_err_t w25qxx_init(const char * flash_device_name, - const char * spi_device_name); - -#endif // SPI_FLASH_W25QXX_H_INCLUDED diff --git a/components/drivers/spi/spi_flash_w25qxx_mtd.c b/components/drivers/spi/spi_flash_w25qxx_mtd.c deleted file mode 100644 index 9f1ef802f6..0000000000 --- a/components/drivers/spi/spi_flash_w25qxx_mtd.c +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - */ -#include <rtthread.h> -#include <rtdevice.h> - -#include "spi_flash.h" -#include "spi_flash_w25qxx_mtd.h" - -#include <stdint.h> -#include <stdio.h> -#include <string.h> -#include <stdlib.h> - -#define FLASH_DEBUG - -#ifdef FLASH_DEBUG -#define FLASH_TRACE printf -#else -#define FLASH_TRACE(...) -#endif /* #ifdef FLASH_DEBUG */ - -/* JEDEC Manufacturer’s ID */ -#define MF_ID (0xEF) -/* JEDEC Device ID: Memory type and Capacity */ -#define MTC_W25Q80_BV (0x4014) /* W25Q80BV */ -#define MTC_W25Q16_BV_CL_CV (0x4015) /* W25Q16BV W25Q16CL W25Q16CV */ -#define MTC_W25Q16_DW (0x6015) /* W25Q16DW */ -#define MTC_W25Q32_BV (0x4016) /* W25Q32BV */ -#define MTC_W25Q32_DW (0x6016) /* W25Q32DW */ -#define MTC_W25Q64_BV_CV (0x4017) /* W25Q64BV W25Q64CV */ -#define MTC_W25Q64_DW (0x4017) /* W25Q64DW */ -#define MTC_W25Q128_BV (0x4018) /* W25Q128BV */ -#define MTC_W25Q256_FV (TBD) /* W25Q256FV */ - -#define MTC_W25X80 (0x3014) - -/* command list */ -#define CMD_WRSR (0x01) /* Write Status Register */ -#define CMD_PP (0x02) /* Page Program */ -#define CMD_READ (0x03) /* Read Data */ -#define CMD_WRDI (0x04) /* Write Disable */ -#define CMD_RDSR1 (0x05) /* Read Status Register-1 */ -#define CMD_WREN (0x06) /* Write Enable */ -#define CMD_FAST_READ (0x0B) /* Fast Read */ -#define CMD_ERASE_4K (0x20) /* Sector Erase:4K */ -#define CMD_RDSR2 (0x35) /* Read Status Register-2 */ -#define CMD_ERASE_32K (0x52) /* 32KB Block Erase */ -#define CMD_JEDEC_ID (0x9F) /* Read JEDEC ID */ -#define CMD_ERASE_full (0xC7) /* Chip Erase */ -#define CMD_ERASE_64K (0xD8) /* 64KB Block Erase */ -#define CMD_MANU_ID (0x90) - -#define DUMMY (0xFF) - -#define FLASH_ERASE_CMD CMD_ERASE_4K -#define FLASH_BLOCK_SIZE 4096 -#define FLASH_PAGE_SIZE 256 - -static void w25qxx_lock(struct rt_mtd_nor_device *device) -{ - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - rt_mutex_take(&mtd->lock, RT_WAITING_FOREVER); -} - -static void w25qxx_unlock(struct rt_mtd_nor_device *device) -{ - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - rt_mutex_release(&mtd->lock); -} - -static rt_uint8_t w25qxx_read_status(struct rt_mtd_nor_device *device) -{ - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - return rt_spi_sendrecv8(mtd->rt_spi_device, CMD_RDSR1); -} - -static void w25qxx_wait_busy(struct rt_mtd_nor_device *device) -{ - while( w25qxx_read_status(device) & (0x01)); -} - -static rt_err_t w25qxx_read_id(struct rt_mtd_nor_device *device) -{ - rt_uint8_t cmd; - rt_uint8_t id_recv[3]; - - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - - w25qxx_lock(device); - - cmd = 0xFF; /* reset SPI FLASH, cancel all cmd in processing. */ - rt_spi_send(mtd->rt_spi_device, &cmd, 1); - - cmd = CMD_WRDI; - rt_spi_send(mtd->rt_spi_device, &cmd, 1); - - /* read flash id */ - cmd = CMD_JEDEC_ID; - rt_spi_send_then_recv(mtd->rt_spi_device, &cmd, 1, id_recv, 3); - - w25qxx_unlock(device); - - return (rt_uint32_t)(id_recv[0] << 16) | (id_recv[1] << 8) | id_recv[2]; -} - -static rt_size_t w25qxx_read(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint8_t *buffer, rt_size_t length) -{ - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - rt_uint8_t send_buffer[4]; - - if((offset + length) > device->block_end * FLASH_BLOCK_SIZE) - return 0; - - - w25qxx_lock(device); - - send_buffer[0] = CMD_WRDI; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - - send_buffer[0] = CMD_READ; - send_buffer[1] = (rt_uint8_t)(offset>>16); - send_buffer[2] = (rt_uint8_t)(offset>>8); - send_buffer[3] = (rt_uint8_t)(offset); - rt_spi_send_then_recv(mtd->rt_spi_device, - send_buffer, 4, - buffer, length); - - w25qxx_unlock(device); - return length; -} - -static rt_size_t w25qxx_write(struct rt_mtd_nor_device *device, rt_off_t offset, const rt_uint8_t *buffer, rt_size_t length) -{ - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - rt_uint8_t send_buffer[4]; - rt_uint8_t *write_ptr ; - rt_size_t write_size,write_total; - - if((offset + length) > device->block_end * FLASH_BLOCK_SIZE) - return 0; - - w25qxx_lock(device); - - send_buffer[0] = CMD_WREN; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - w25qxx_wait_busy(device); // wait erase done. - - write_size = 0; - write_total = 0; - write_ptr = (rt_uint8_t *)buffer; - while(write_total < length) - { - send_buffer[0] = CMD_WREN; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - - //write first page... - send_buffer[0] = CMD_PP; - send_buffer[1] = (rt_uint8_t)(offset >> 16); - send_buffer[2] = (rt_uint8_t)(offset >> 8); - send_buffer[3] = (rt_uint8_t)(offset); - - //address % FLASH_PAGE_SIZE + length - if(((offset & (FLASH_PAGE_SIZE - 1)) + (length - write_total)) > FLASH_PAGE_SIZE) - { - write_size = FLASH_PAGE_SIZE - (offset & (FLASH_PAGE_SIZE - 1)); - } - else - { - write_size = (length - write_total); - } - - rt_spi_send_then_send(mtd->rt_spi_device, - send_buffer, 4, - write_ptr + write_total, write_size); - w25qxx_wait_busy(device); - - - offset += write_size; - write_total += write_size; - } - - send_buffer[0] = CMD_WRDI; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - - w25qxx_unlock(device); - - return length; -} - -static rt_err_t w25qxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length) -{ - struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device; - rt_uint8_t send_buffer[4]; - rt_uint32_t erase_size = 0; - - //offset must be ALIGN_DOWN to BLOCKSIZE - if(offset != RT_ALIGN_DOWN(offset,FLASH_BLOCK_SIZE)) - return 0; - - if((offset + length) > device->block_end * FLASH_BLOCK_SIZE) - return 0; - - /* check length must align to block size */ - if(length % device->block_size != 0) - { - rt_kprintf("param length = %d ,error\n",length); - return 0; - } - - w25qxx_lock(device); - - send_buffer[0] = CMD_WREN; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - w25qxx_wait_busy(device); // wait erase done. - while (erase_size < length) - { - send_buffer[0] = CMD_ERASE_4K; - send_buffer[1] = (rt_uint8_t) (offset >> 16); - send_buffer[2] = (rt_uint8_t) (offset >> 8); - send_buffer[3] = (rt_uint8_t) (offset); - rt_spi_send(mtd->rt_spi_device, send_buffer, 4); - w25qxx_wait_busy(device); // wait erase done. - - erase_size += 4096; - offset += 4096; - } - send_buffer[0] = CMD_WRDI; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - - w25qxx_unlock(device); - return RT_EOK; -} - -const static struct rt_mtd_nor_driver_ops w25qxx_mtd_ops = -{ - w25qxx_read_id, - w25qxx_read, - w25qxx_write, - w25qxx_erase_block, -}; - -rt_err_t w25qxx_mtd_init(const char *mtd_name,const char * spi_device_name) -{ - rt_err_t result = RT_EOK; - rt_uint32_t id; - rt_uint8_t send_buffer[3]; - - struct rt_spi_device* rt_spi_device; - struct spi_flash_mtd* mtd = (struct spi_flash_mtd *)rt_malloc(sizeof(struct spi_flash_mtd)); - - RT_ASSERT(mtd != RT_NULL); - - /* initialize mutex */ - if (rt_mutex_init(&mtd->lock, mtd_name, RT_IPC_FLAG_FIFO) != RT_EOK) - { - FLASH_TRACE("init mtd lock mutex failed\n"); - result = -RT_ENOSYS; - - goto _error_exit; - } - - rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); - if(rt_spi_device == RT_NULL) - { - FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); - result = -RT_ENOSYS; - - goto _error_exit; - } - mtd->rt_spi_device = rt_spi_device; - /* config spi */ - { - struct rt_spi_configuration cfg; - cfg.data_width = 8; - cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ - cfg.max_hz = 20 * 1000 * 1000; /* 20 */ - rt_spi_configure(rt_spi_device, &cfg); - } - - /* Init Flash device */ - { - w25qxx_lock(&mtd->mtd_device); - - send_buffer[0] = CMD_WREN; - rt_spi_send(mtd->rt_spi_device, send_buffer, 1); - w25qxx_wait_busy(&mtd->mtd_device); - - send_buffer[0] = CMD_WRSR; - send_buffer[1] = 0; - send_buffer[2] = 0; - rt_spi_send(mtd->rt_spi_device, send_buffer, 3); - w25qxx_wait_busy(&mtd->mtd_device); - - w25qxx_unlock(&mtd->mtd_device); - } - - id = w25qxx_read_id(&mtd->mtd_device); - - mtd->mtd_device.block_size = 4096; - mtd->mtd_device.block_start = 0; - switch(id & 0xFFFF) - { - case MTC_W25Q80_BV: /* W25Q80BV */ - mtd->mtd_device.block_end = 256; - break; - case MTC_W25Q16_BV_CL_CV: /* W25Q16BV W25Q16CL W25Q16CV */ - case MTC_W25Q16_DW: /* W25Q16DW */ - mtd->mtd_device.block_end = 512; - break; - case MTC_W25Q32_BV: /* W25Q32BV */ - case MTC_W25Q32_DW: /* W25Q32DW */ - mtd->mtd_device.block_end = 1024; - break; - case MTC_W25Q64_BV_CV: /* W25Q64BV W25Q64CV */ - mtd->mtd_device.block_end = 2048; - break; - case MTC_W25Q128_BV: /* W25Q128BV */ - mtd->mtd_device.block_end = 4086; - break; - } - mtd->mtd_device.ops = &w25qxx_mtd_ops; - rt_mtd_nor_register_device(mtd_name,&mtd->mtd_device); - - return RT_EOK; - -_error_exit: - if(mtd != RT_NULL) - rt_free(mtd); - return result; -} diff --git a/components/drivers/spi/spi_flash_w25qxx_mtd.h b/components/drivers/spi/spi_flash_w25qxx_mtd.h deleted file mode 100644 index 28f6828ffb..0000000000 --- a/components/drivers/spi/spi_flash_w25qxx_mtd.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - * 2012-08-23 aozima add flash lock. - * 2017-02-11 urey add mtd fucntion - */ - -#ifndef SPI_FLASH_W25QXX_H_INCLUDED -#define SPI_FLASH_W25QXX_H_INCLUDED - -#include <rtthread.h> - -extern rt_err_t w25qxx_mtd_init(const char *mtd_name,const char * spi_device_name); - -#endif // SPI_FLASH_W25QXX_H_INCLUDED diff --git a/components/finsh/shell.c b/components/finsh/shell.c index 53dcd11c66..3a02e24c2e 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -309,8 +309,8 @@ static void finsh_wait_auth(void) else if (ch == '\b' && cur_pos > 0) { /* backspace */ - password[cur_pos] = '\0'; cur_pos--; + password[cur_pos] = '\0'; rt_kprintf("\b \b"); } else if (ch == '\r' || ch == '\n') diff --git a/components/net/at/at_socket/at_socket.c b/components/net/at/at_socket/at_socket.c index a3b36a06ae..993ec33506 100644 --- a/components/net/at/at_socket/at_socket.c +++ b/components/net/at/at_socket/at_socket.c @@ -359,20 +359,21 @@ __err: return RT_NULL; } -static struct at_socket *alloc_socket(int domain) +static struct at_socket *alloc_socket(void) { extern struct netdev *netdev_default; struct netdev *netdev = RT_NULL; struct at_device *device = RT_NULL; - if (netdev_default && netdev_is_up(netdev_default)) + if (netdev_default && netdev_is_up(netdev_default) && + netdev_family_get(netdev_default) == AF_AT) { netdev = netdev_default; } else { - /* get network interface device by protocol family */ - netdev = netdev_get_by_family(domain); + /* get network interface device by protocol family AF_AT */ + netdev = netdev_get_by_family(AF_AT); if (netdev == RT_NULL) { return RT_NULL; @@ -414,7 +415,7 @@ int at_socket(int domain, int type, int protocol) } /* allocate and initialize a new AT socket */ - sock = alloc_socket(domain); + sock = alloc_socket(); if (sock == RT_NULL) { return -1; diff --git a/components/net/lwip-1.4.1/src/netif/ethernetif.c b/components/net/lwip-1.4.1/src/netif/ethernetif.c index 10bcaa0c95..df960fcde0 100644 --- a/components/net/lwip-1.4.1/src/netif/ethernetif.c +++ b/components/net/lwip-1.4.1/src/netif/ethernetif.c @@ -295,12 +295,6 @@ static int netdev_add(struct netif *lwip_netif) return -ERR_IF; } - netdev->flags = lwip_netif->flags; - netdev->mtu = lwip_netif->mtu; - netdev->ops = &lwip_netdev_ops; - netdev->hwaddr_len = lwip_netif->hwaddr_len; - rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); - #ifdef SAL_USING_LWIP extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev); /* set the lwIP network interface device protocol family information */ @@ -309,7 +303,17 @@ static int netdev_add(struct netif *lwip_netif) rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN); result = netdev_register(netdev, name, (void *)lwip_netif); - + + /* Update netdev info after registered */ + netdev->flags = lwip_netif->flags; + netdev->mtu = lwip_netif->mtu; + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; + #ifdef RT_LWIP_DHCP netdev_low_level_set_dhcp_status(netdev, RT_TRUE); #endif diff --git a/components/net/lwip-2.0.2/src/netif/ethernetif.c b/components/net/lwip-2.0.2/src/netif/ethernetif.c index f19ec094fa..a5a1f94c0b 100644 --- a/components/net/lwip-2.0.2/src/netif/ethernetif.c +++ b/components/net/lwip-2.0.2/src/netif/ethernetif.c @@ -303,12 +303,6 @@ static int netdev_add(struct netif *lwip_netif) return -ERR_IF; } - netdev->flags = lwip_netif->flags; - netdev->mtu = lwip_netif->mtu; - netdev->ops = &lwip_netdev_ops; - netdev->hwaddr_len = lwip_netif->hwaddr_len; - rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); - #ifdef SAL_USING_LWIP extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev); /* set the lwIP network interface device protocol family information */ @@ -317,7 +311,17 @@ static int netdev_add(struct netif *lwip_netif) rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN); result = netdev_register(netdev, name, (void *)lwip_netif); - + + /* Update netdev info after registered */ + netdev->flags = lwip_netif->flags; + netdev->mtu = lwip_netif->mtu; + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; + #ifdef RT_LWIP_DHCP netdev_low_level_set_dhcp_status(netdev, RT_TRUE); #endif diff --git a/components/net/lwip-2.1.0/src/netif/ethernetif.c b/components/net/lwip-2.1.0/src/netif/ethernetif.c index db28a4a55b..5ea03a9ae8 100755 --- a/components/net/lwip-2.1.0/src/netif/ethernetif.c +++ b/components/net/lwip-2.1.0/src/netif/ethernetif.c @@ -303,11 +303,6 @@ static int netdev_add(struct netif *lwip_netif) { return -ERR_IF; } - - netdev->flags = lwip_netif->flags; - netdev->ops = &lwip_netdev_ops; - netdev->hwaddr_len = lwip_netif->hwaddr_len; - rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); #ifdef SAL_USING_LWIP extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev); @@ -317,6 +312,16 @@ static int netdev_add(struct netif *lwip_netif) rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN); result = netdev_register(netdev, name, (void *)lwip_netif); + + /* Update netdev info after registered */ + netdev->flags = lwip_netif->flags; + netdev->mtu = lwip_netif->mtu; + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; #ifdef RT_LWIP_DHCP netdev_low_level_set_dhcp_status(netdev, RT_TRUE); diff --git a/components/net/netdev/include/netdev.h b/components/net/netdev/include/netdev.h index e9d8e38658..0be2a2bda3 100644 --- a/components/net/netdev/include/netdev.h +++ b/components/net/netdev/include/netdev.h @@ -138,6 +138,7 @@ struct netdev *netdev_get_by_ipaddr(ip_addr_t *ip_addr); struct netdev *netdev_get_by_name(const char *name); #ifdef RT_USING_SAL struct netdev *netdev_get_by_family(int family); +int netdev_family_get(struct netdev *netdev); #endif /* RT_USING_SAL */ /* Set default network interface device in list */ diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index 71b1ec7ccd..5f49a46f17 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -294,6 +294,20 @@ struct netdev *netdev_get_by_family(int family) return RT_NULL; } +/** + * This function will get the family type from network interface device + * + * @param netdev network interface device object + * + * @return the network interface device family type + */ +int netdev_family_get(struct netdev *netdev) +{ + RT_ASSERT(netdev); + + return ((struct sal_proto_family *)netdev->sal_user_data)->family; +} + #endif /* RT_USING_SAL */ /** diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index 8f3149abd5..e09b2b7119 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -561,8 +561,7 @@ int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen) { struct sal_socket *sock; struct sal_proto_family *pf; - struct netdev *new_netdev; - ip_addr_t local_addr; + ip_addr_t input_ipaddr; RT_ASSERT(name); @@ -570,44 +569,43 @@ int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen) SAL_SOCKET_OBJ_GET(sock, socket); /* bind network interface by ip address */ - sal_sockaddr_to_ipaddr(name, &local_addr); + sal_sockaddr_to_ipaddr(name, &input_ipaddr); /* check input ipaddr is default netdev ipaddr */ - if (local_addr.addr == INADDR_ANY) + if (input_ipaddr.addr != INADDR_ANY) { - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind); - return pf->skt_ops->bind((int) sock->user_data, name, namelen); - } - - new_netdev = netdev_get_by_ipaddr(&local_addr); - if (new_netdev == RT_NULL) - { - LOG_E("Not find network interface device ipaddr(%s).", inet_ntoa(local_addr)); - return -1; - } + struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL; + struct netdev *new_netdev = RT_NULL; - /* change network interface device parameter in sal socket object */ - if (sock->netdev != new_netdev) - { - struct sal_proto_family *old_pf, *new_pf; - int new_socket = 0; + new_netdev = netdev_get_by_ipaddr(&input_ipaddr); + if (new_netdev == RT_NULL) + { + return -1; + } - /* close old netdev socket */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, old_pf, closesocket); - old_pf->skt_ops->closesocket(socket); + /* get input and local ip address proto_family */ + SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind); + SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind); - /* open new netdev socket */ - SAL_NETDEV_SOCKETOPS_VALID(new_netdev, new_pf, socket); - new_socket = new_pf->skt_ops->socket(sock->domain, sock->type, sock->protocol); - if (new_socket < 0) + /* check the network interface protocol family type */ + if (input_pf->family != local_pf->family) { - return -1; + int new_socket = -1; + + /* protocol family is different, close old socket and create new socket by input ip address */ + local_pf->skt_ops->closesocket(socket); + + new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol); + if (new_socket < 0) + { + return -1; + } + sock->netdev = new_netdev; + sock->user_data = (void *) new_socket; } - sock->netdev = new_netdev; - sock->user_data = (void *) new_socket; } - - /* check the network interface socket opreation */ + + /* check and get protocol families by the network interface device */ SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind); return pf->skt_ops->bind((int) sock->user_data, name, namelen); } |
