diff options
| author | Meco Jianting Man <[email protected]> | 2021-02-18 01:30:19 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-02-18 01:30:19 -0600 |
| commit | 8ea33c2101cdc8d8d8a5f94479acca310fbbfb75 (patch) | |
| tree | 48ba0df2445feb188eed3df81d9e2a62d6d57f53 /bsp/maxim/libraries | |
| parent | e19873db6d6947f02e0b093251adf3c73daabdd8 (diff) | |
| parent | 6420c2779143f414ca2850cb2ab3d72a4ff10a9b (diff) | |
Merge pull request #18 from RT-Thread/master
pr
Diffstat (limited to 'bsp/maxim/libraries')
| -rw-r--r-- | bsp/maxim/libraries/HAL_Drivers/drv_gpio.c | 208 | ||||
| -rw-r--r-- | bsp/maxim/libraries/HAL_Drivers/drv_gpio.h | 21 | ||||
| -rw-r--r-- | bsp/maxim/libraries/HAL_Drivers/drv_spi.c | 169 | ||||
| -rw-r--r-- | bsp/maxim/libraries/HAL_Drivers/drv_spi.h | 67 | ||||
| -rw-r--r-- | bsp/maxim/libraries/MAX32660PeriphDriver/SConscript | 3 |
5 files changed, 468 insertions, 0 deletions
diff --git a/bsp/maxim/libraries/HAL_Drivers/drv_gpio.c b/bsp/maxim/libraries/HAL_Drivers/drv_gpio.c new file mode 100644 index 0000000000..e2001371d9 --- /dev/null +++ b/bsp/maxim/libraries/HAL_Drivers/drv_gpio.c @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2006-2020, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-11 supperthomas first version + * + */ + + +#include "drv_gpio.h" +#include <stdbool.h> +#include "gpio.h" + +#ifdef RT_USING_PIN + +#define DBG_LEVEL DBG_LOG +#include <rtdbg.h> +#define LOG_TAG "drv.gpio" + +#define PIN_PORT_OFFSET 4 + +#define PIN_NUM(port, no) ((((((port) & 0xFu) << PIN_PORT_OFFSET) | ((no) & 0xFu))) +#define PIN_PORT(pin) ((uint8_t)(((pin) >> PIN_PORT_OFFSET) & 0xFu)) +#define PIN_NO(pin) ((uint8_t)((pin) & 0xFu)) + + +#define PIN_MCU_PORT(pin) PIN_PORT(pin) +#define PIN_MCU_PIN(pin) ((uint32_t)(1u << PIN_NO(pin))) + +static void mcu_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value) +{ + gpio_cfg_t tmp_gpio_cfg; + tmp_gpio_cfg.port = PIN_PORT(pin); + tmp_gpio_cfg.mask = PIN_MCU_PIN(pin); + if (value) + { + GPIO_OutSet(&tmp_gpio_cfg); + } + else + { + GPIO_OutClr(&tmp_gpio_cfg); + } + +} + +static int mcu_pin_read(rt_device_t dev, rt_base_t pin) +{ + int value; + gpio_cfg_t tmp_gpio_cfg; + tmp_gpio_cfg.port = PIN_PORT(pin); + tmp_gpio_cfg.mask = PIN_MCU_PIN(pin); + + if (GPIO_InGet(&tmp_gpio_cfg)) + { + value = 1; + } + else + { + value = 0; + } + + return value; +} + +static void mcu_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode) +{ + gpio_cfg_t tmp_gpio_cfg; + int ret = 0; + tmp_gpio_cfg.port = PIN_PORT(pin); + tmp_gpio_cfg.mask = PIN_MCU_PIN(pin); + + switch (mode) + { + case PIN_MODE_OUTPUT: + tmp_gpio_cfg.func = GPIO_FUNC_OUT; + tmp_gpio_cfg.pad = GPIO_PAD_NONE; + break; + case PIN_MODE_INPUT: + tmp_gpio_cfg.func = GPIO_FUNC_IN; + tmp_gpio_cfg.pad = GPIO_PAD_NONE; + break; + case PIN_MODE_INPUT_PULLUP: + tmp_gpio_cfg.func = GPIO_FUNC_IN; + tmp_gpio_cfg.pad = GPIO_PAD_PULL_UP; + break; + case PIN_MODE_INPUT_PULLDOWN: + tmp_gpio_cfg.func = GPIO_FUNC_IN; + tmp_gpio_cfg.pad = GPIO_PAD_PULL_DOWN; + break; + case PIN_MODE_OUTPUT_OD: + //not support + LOG_E("NOT SUPPORT"); + break; + } + ret = GPIO_Config(&tmp_gpio_cfg); + if (E_NO_ERROR != ret) + { + LOG_E("GPIO_Config error :%d", ret); + } +} + + +static rt_err_t mcu_pin_attach_irq(struct rt_device *device, rt_int32_t pin, + rt_uint32_t irq_mode, void (*hdr)(void *args), void *args) +{ + gpio_cfg_t tmp_gpio_cfg; + tmp_gpio_cfg.port = PIN_MCU_PORT(pin); + tmp_gpio_cfg.mask = PIN_MCU_PIN(pin); + + + tmp_gpio_cfg.pad = GPIO_PAD_PULL_UP; + tmp_gpio_cfg.func = GPIO_FUNC_IN; + GPIO_Config(&tmp_gpio_cfg); + GPIO_RegisterCallback(&tmp_gpio_cfg, hdr, args); + + gpio_int_mode_t mcu_mode; + gpio_int_pol_t mcu_pol; + + switch (irq_mode) + { + case PIN_IRQ_MODE_RISING: + mcu_mode = GPIO_INT_EDGE; + mcu_pol = GPIO_INT_RISING; + break; + case PIN_IRQ_MODE_FALLING: + mcu_mode = GPIO_INT_EDGE; + mcu_pol = GPIO_INT_FALLING; + break; + case PIN_IRQ_MODE_RISING_FALLING: + mcu_mode = GPIO_INT_EDGE; + mcu_pol = GPIO_INT_BOTH; + break; + case PIN_IRQ_MODE_HIGH_LEVEL: + mcu_mode = GPIO_INT_LEVEL; + mcu_pol = GPIO_INT_HIGH; + break; + case PIN_IRQ_MODE_LOW_LEVEL: + mcu_mode = GPIO_INT_LEVEL; + mcu_pol = GPIO_INT_LOW; + break; + } + + GPIO_IntConfig(&tmp_gpio_cfg, mcu_mode, mcu_pol); + + + return RT_EOK; +} + +static rt_err_t mcu_pin_dettach_irq(struct rt_device *device, rt_int32_t pin) +{ + gpio_cfg_t tmp_gpio_cfg; + tmp_gpio_cfg.port = PIN_MCU_PORT(pin); + tmp_gpio_cfg.mask = PIN_MCU_PIN(pin); + tmp_gpio_cfg.pad = GPIO_PAD_PULL_UP; + tmp_gpio_cfg.func = GPIO_FUNC_IN; + GPIO_Config(&tmp_gpio_cfg); + GPIO_IntDisable(&tmp_gpio_cfg); + GPIO_RegisterCallback(&tmp_gpio_cfg, NULL, NULL); + return RT_EOK; +} + +static rt_err_t mcu_pin_irq_enable(struct rt_device *device, rt_base_t pin, + rt_uint32_t enabled) +{ + gpio_cfg_t tmp_gpio_cfg; + tmp_gpio_cfg.port = PIN_MCU_PORT(pin); + tmp_gpio_cfg.mask = PIN_MCU_PIN(pin); + if (enabled) + { + GPIO_IntEnable(&tmp_gpio_cfg); + NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(PIN_MCU_PORT(pin))); + } + else + { + GPIO_IntDisable(&tmp_gpio_cfg); + NVIC_DisableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(PIN_MCU_PORT(pin))); + } + return RT_EOK; +} + +const static struct rt_pin_ops _mcu_pin_ops = +{ + mcu_pin_mode, + mcu_pin_write, + mcu_pin_read, + mcu_pin_attach_irq, + mcu_pin_dettach_irq, + mcu_pin_irq_enable, + NULL, +}; + +int rt_hw_pin_init(void) +{ + GPIO_Init(); + return rt_device_pin_register("pin", &_mcu_pin_ops, RT_NULL); +} +INIT_BOARD_EXPORT(rt_hw_pin_init); + + +void GPIO0_IRQHandler(void) +{ + GPIO_Handler(PORT_0); +} + +#endif /* RT_USING_PIN */ diff --git a/bsp/maxim/libraries/HAL_Drivers/drv_gpio.h b/bsp/maxim/libraries/HAL_Drivers/drv_gpio.h new file mode 100644 index 0000000000..dc81da0d4a --- /dev/null +++ b/bsp/maxim/libraries/HAL_Drivers/drv_gpio.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2006-2020, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-11 supperthomas first version + * + */ + +#ifndef __DRV_GPIO_H__ +#define __DRV_GPIO_H__ + +#include <board.h> +#include <rtdevice.h> + +int rt_hw_pin_init(void); + +#endif /* __DRV_GPIO_H__ */ + diff --git a/bsp/maxim/libraries/HAL_Drivers/drv_spi.c b/bsp/maxim/libraries/HAL_Drivers/drv_spi.c new file mode 100644 index 0000000000..03671652ef --- /dev/null +++ b/bsp/maxim/libraries/HAL_Drivers/drv_spi.c @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-14 supperthomas first version + */ + +#include <stdint.h> +#include <string.h> +#include "board.h" +#include "drv_spi.h" + +#define DBG_LEVEL DBG_LOG +#include <rtdbg.h> +#define LOG_TAG "drv.spi" + +#ifdef BSP_USING_SPI + +#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) +static struct mcu_drv_spi_config spi_config[] = +{ +#ifdef BSP_USING_SPI0 + MCU_SPI0_CONFIG, +#endif +#ifdef BSP_USING_SPI1 + MCU_SPI1_CONFIG, +#endif + +}; + +static struct mcu_drv_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])]; + + +/** + * @brief This function config spi bus + * @param device + * @param configuration + * @retval RT_EOK / RT_ERROR + */ +static rt_err_t spi_configure(struct rt_spi_device *device, + struct rt_spi_configuration *configuration) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(device->bus != RT_NULL); + RT_ASSERT(device->bus->parent.user_data != RT_NULL); + RT_ASSERT(configuration != RT_NULL); + struct mcu_drv_spi *tmp_spi; + tmp_spi = rt_container_of(device->bus, struct mcu_drv_spi, spi_bus); + int mode; + + ///init + + switch (configuration->mode & RT_SPI_MODE_3) + { + case RT_SPI_MODE_0/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */: + case RT_SPI_MODE_1/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */: + case RT_SPI_MODE_2/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */: + case RT_SPI_MODE_3/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */: + mode = configuration->mode & RT_SPI_MODE_3; + break; + default: + LOG_E("spi_configure mode error %x\n", configuration->mode); + return RT_ERROR; + } + + tmp_spi->spixfer_req.width = SPI17Y_WIDTH_1; + tmp_spi->spixfer_req.bits = configuration->data_width; + tmp_spi->spixfer_req.ssel = 0; + tmp_spi->spixfer_req.deass = 1; + tmp_spi->spixfer_req.tx_num = 0; + tmp_spi->spixfer_req.rx_num = 0; + tmp_spi->spixfer_req.callback = NULL; + LOG_D("spi init mode:%d, rate:%d", mode, configuration->max_hz); + if (SPI_Init(tmp_spi->spi_instance, mode, configuration->max_hz) != 0) + { + LOG_E("Error configuring SPI\n"); + while (1) {} + } + //init + return RT_EOK; +} + +static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(device->bus != RT_NULL); + RT_ASSERT(device->bus->parent.user_data != RT_NULL); + + int ret = 0; + struct mcu_drv_spi *tmp_spi; + tmp_spi = rt_container_of(device->bus, struct mcu_drv_spi, spi_bus); + + + tmp_spi->spixfer_req.tx_data = message->send_buf; + tmp_spi->spixfer_req.rx_data = message->recv_buf; + tmp_spi->spixfer_req.len = message->length; + ret = SPI_MasterTrans(tmp_spi->spi_instance, &tmp_spi->spixfer_req); + if (ret == E_NO_ERROR) + { + return message->length; + } + else + { + LOG_E("spixfer faild, ret %d", ret); + return 0; + } +} + +/* spi bus callback function */ +static const struct rt_spi_ops nrfx_spi_ops = +{ + .configure = spi_configure, + .xfer = spixfer, +}; + +/*spi bus init*/ +static int rt_hw_spi_bus_init(void) +{ + rt_err_t result = RT_ERROR; + for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++) + { + spi_bus_obj[i].spi_instance = spi_config[i].spi_instance; + spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i]; //SPI INSTANCE + result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &nrfx_spi_ops); + RT_ASSERT(result == RT_EOK); + } + return result; +} + +int rt_hw_spi_init(void) +{ + return rt_hw_spi_bus_init(); +} +INIT_BOARD_EXPORT(rt_hw_spi_init); + +/** + * Attach the spi device to SPI bus, this function must be used after initialization. + */ +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t cs_pin) +{ + RT_ASSERT(bus_name != RT_NULL); + RT_ASSERT(device_name != RT_NULL); + RT_ASSERT(cs_pin != RT_NULL); + + rt_err_t result; + struct rt_spi_device *spi_device; + /* attach the device to spi bus*/ + spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); + RT_ASSERT(spi_device != RT_NULL); + /* initialize the cs pin */ + result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin); + if (result != RT_EOK) + { + LOG_E("%s attach to %s faild, %d", device_name, bus_name, result); + result = RT_ERROR; + } + /* TODO: SET THE GPIO */ + + RT_ASSERT(result == RT_EOK); + return result; +} + +#endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */ +#endif /*BSP_USING_SPI*/ + + diff --git a/bsp/maxim/libraries/HAL_Drivers/drv_spi.h b/bsp/maxim/libraries/HAL_Drivers/drv_spi.h new file mode 100644 index 0000000000..c478850fa7 --- /dev/null +++ b/bsp/maxim/libraries/HAL_Drivers/drv_spi.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-14 supperthomas first version + */ + +#include <rtthread.h> +#include <rtdevice.h> +#include <rthw.h> + +#include "spi.h" + +#ifndef __DRV_SPI_H_ +#define __DRV_SPI_H_ + +/** + * @brief Attach the spi device to SPI bus, this function must be used after initialization. + * @param bus_name spi bus name "spi0"/"spi1"/"spi2" + * @param device_name spi device name "spi0x"/"spi1x"/"spi2x" + * @param ss_pin spi ss pin number + * @retval RT_ERROR / RT_EOK +*/ +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin); + +//SPI bus config +#ifdef BSP_USING_SPI0 +#define MCU_SPI0_CONFIG \ +{ \ + .bus_name = "spi0", \ + .spi_instance = SPI0A, \ +} +#endif +#ifdef BSP_USING_SPI1 +#ifdef BSP_USING_SPI1A //The SPI1A is conflit with UART1 TX RX P0.10 P0.11 +#define MCU_SPI1_CONFIG \ +{ \ + .bus_name = "spi1", \ + .spi_instance = SPI1A \ +} +#else +#define MCU_SPI1_CONFIG \ +{ \ + .bus_name = "spi1", \ + .spi_instance = SPI1B \ +} +#endif +#endif + +struct mcu_drv_spi_config +{ + char *bus_name; + spi_type spi_instance; +}; + +struct mcu_drv_spi +{ + spi_type spi_instance; + spi_req_t spixfer_req; + struct rt_spi_configuration *cfg; + struct rt_spi_bus spi_bus; +}; + +#endif /*__DRV_SPI_H_*/ diff --git a/bsp/maxim/libraries/MAX32660PeriphDriver/SConscript b/bsp/maxim/libraries/MAX32660PeriphDriver/SConscript index fa62f76fb7..b0f2840147 100644 --- a/bsp/maxim/libraries/MAX32660PeriphDriver/SConscript +++ b/bsp/maxim/libraries/MAX32660PeriphDriver/SConscript @@ -30,6 +30,9 @@ if GetDepend(['RT_USING_I2C']): if GetDepend(['RT_USING_SPI']): src += ['Source/spi.c'] + src += ['Source/spi17y.c'] + src += ['Source/spimss.c'] + if GetDepend(['RT_USING_RTC']): src += ['Source/rtc.c'] |
