ch569w-evt : add spi master driver, SPI0 tested (#6205)

* ch569w-evt : add spi master driver, SPI0 tested

* Update bsp/wch/risc-v/ch569w-evt/board/Kconfig

* Update bsp/wch/risc-v/ch569w-evt/board/Kconfig

Co-authored-by: Man, Jianting (Meco) <920369182@qq.com>
This commit is contained in:
emuzitandMan, Jianting (Meco) authored and GitHub committed 2022-08-01 22:36:49 -04:00
1 parent 58c4f41ac7
commit a881c05e58
11 files changed
+704 -33

No files matched your search

@@ -15,6 +15,9 @@ if GetDepend('SOC_SERIES_CH569'):
if GetDepend('RT_USING_HWTIMER'):
src += ['ch56x_timer.c']
if GetDepend('RT_USING_SPI'):
src += ['ch56x_spi.c']
if GetDepend('RT_USING_PIN'):
src += ['ch56x_gpio.c']
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -11,6 +11,8 @@
#include <rtdebug.h>
#include "ch56x_sys.h"
static uint32_t hclk_freq;
rt_inline uint8_t _slp_clk_off0_irqn_bit(uint8_t irqn)
{
uint8_t bitpos;
@@ -221,6 +223,8 @@ int sys_hclk_set(uint32_t freq)
sys->CLK_CFG_CTRL.reg = clk_cfg_ctrl_wdat(clksel);
sys_safe_access_leave(sys);
rt_hw_interrupt_enable(level);
/* save to hclk_freq for quick report */
sys_hclk_calc();
clksel = 0;
}
@@ -228,11 +232,24 @@ int sys_hclk_set(uint32_t freq)
}
/**
* @brief Get current HCLK frequency.
* @brief Get saved HCLK frequency.
*
* Valid only if HCLK is set strickly with sys_hclk_set().
* Use sys_hclk_calc() otherwise.
*
* @return Returns saved HCLK frequency (Hz, 0 if not set yet).
*/
uint32_t sys_hclk_get(void)
{
return hclk_freq;
}
/**
* @brief Get current HCLK frequency, calculated from hw setting.
*
* @return Returns current HCLK frequency (Hz).
*/
uint32_t sys_hclk_get(void)
uint32_t sys_hclk_calc(void)
{
volatile struct sys_registers *sys = (void *)SYS_REG_BASE;
@@ -240,10 +257,12 @@ uint32_t sys_hclk_get(void)
if (sys->CLK_CFG_CTRL.sel_pll == CLK_SEL_PLL_USB_480M)
{
return plldiv ? 480000000 / plldiv : 30000000;
hclk_freq = plldiv ? 480000000 / plldiv : 30000000;
}
else
{
return plldiv ? 30000000 / plldiv : 2000000;
hclk_freq = plldiv ? 30000000 / plldiv : 2000000;
}
return hclk_freq;
}
@@ -382,6 +382,7 @@ struct sys_registers
CHECK_STRUCT_SIZE(struct sys_registers, 0x28);
uint32_t sys_hclk_calc(void);
uint32_t sys_hclk_get(void);
int sys_hclk_set(uint32_t freq);
int sys_clk_off_by_irqn(uint8_t irqn, int off);
@@ -14,6 +14,10 @@
#include "ch56x_timer.h"
#include "isr_sp.h"
#if !defined(BSP_USING_TMR0) && !defined(BSP_USING_TMR1) && !defined(BSP_USING_TMR2)
#error "Please define at least one TMRx"
#endif
struct hwtimer_device
{
struct rt_hwtimer_device parent;
+10 -1
View File
@@ -123,7 +123,13 @@ CONFIG_RT_USING_PIN=y
# CONFIG_RT_USING_PM is not set
# CONFIG_RT_USING_RTC is not set
# CONFIG_RT_USING_SDIO is not set
# CONFIG_RT_USING_SPI is not set
CONFIG_RT_USING_SPI=y
# CONFIG_RT_USING_SPI_BITOPS is not set
# CONFIG_RT_USING_QSPI is not set
# CONFIG_RT_USING_SPI_MSD is not set
# CONFIG_RT_USING_SFUD is not set
# CONFIG_RT_USING_ENC28J60 is not set
# CONFIG_RT_USING_SPI_WIFI is not set
CONFIG_RT_USING_WDT=y
# CONFIG_RT_USING_AUDIO is not set
# CONFIG_RT_USING_SENSOR is not set
@@ -654,6 +660,9 @@ CONFIG_BSP_USING_TIMER=y
CONFIG_BSP_USING_TMR0=y
CONFIG_BSP_USING_TMR1=y
# CONFIG_BSP_USING_TMR2 is not set
CONFIG_BSP_USING_SPI=y
CONFIG_BSP_USING_SPI0=y
# CONFIG_BSP_USING_SPI1 is not set
#
# Onboard Peripheral Drivers
+105 -28
View File
@@ -8,12 +8,14 @@
* 2022-07-15 Emuzit first version
* 2022-07-20 Emuzit add watchdog test
* 2022-07-26 Emuzit add hwtimer test
* 2022-07-30 Emuzit add spi master test
*/
#include <rtthread.h>
#include <rtdebug.h>
#include <drivers/pin.h>
#include <drivers/watchdog.h>
#include <drivers/hwtimer.h>
#include <drivers/spi.h>
#include "board.h"
static const rt_base_t gpio_int_pins[8] = GPIO_INT_PINS;
@@ -34,12 +36,9 @@ static const uint32_t gpint_mode[] =
static struct rt_mailbox *gpint_mb = RT_NULL;
static struct rt_thread *gpint_thread = RT_NULL;
static rt_base_t led0, led1;
static rt_device_t wdg_dev;
static struct rt_device *tmr_dev_0;
static struct rt_device *tmr_dev_1;
static rt_base_t led0, led1;
static void gpio_int_callback(void *pin)
{
@@ -73,31 +72,13 @@ static void gpio_int_thread(void *param)
}
}
#ifdef RT_USING_HWTIMER
static rt_err_t tmr_timeout_cb(rt_device_t dev, rt_size_t size)
static void test_gpio_int(void)
{
rt_tick_t tick = rt_tick_get();
int tmr = (dev == tmr_dev_1) ? 1 : 0;
rt_kprintf("hwtimer %d timeout callback fucntion @tick %d\n", tmr, tick);
return RT_EOK;
}
#endif
void main(void)
{
rt_hwtimerval_t timerval;
rt_hwtimer_mode_t mode;
rt_size_t tsize;
uint32_t seconds;
rt_err_t res;
int i;
rt_kprintf("\nCH569W-R0-1v0, HCLK: %dMHz\n\n", sys_hclk_get() / 1000000);
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_pin_write(LED1_PIN, led1 = PIN_HIGH);
/* Enable all gpio interrupt with various modes.
* LED0 or GND touching can be used to trigger pin interrupt.
@@ -136,8 +117,11 @@ void main(void)
}
}
}
}
#ifdef RT_USING_WDT
static void test_watchdog(uint32_t seconds)
{
/* Test watchdog with 30s timeout, keepalive with gpio interrupt.
*
* CAVEAT: With only 8-bit WDOG_COUNT and fixed clocking at Fsys/524288,
@@ -158,9 +142,32 @@ void main(void)
{
rt_kprintf("WDT_TIMEOUT in %d seconds, trigger gpio interrupt to keep alive.\n\n", seconds);
}
}
#else
#define test_watchdog(tov) do {} while(0)
#endif
#ifdef RT_USING_HWTIMER
static struct rt_device *tmr_dev_0;
static struct rt_device *tmr_dev_1;
static rt_err_t tmr_timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_tick_t tick = rt_tick_get();
int tmr = (dev == tmr_dev_1) ? 1 : 0;
rt_kprintf("hwtimer %d timeout callback fucntion @tick %d\n", tmr, tick);
return RT_EOK;
}
static void test_hwtimer(void)
{
rt_hwtimerval_t timerval;
rt_hwtimer_mode_t mode;
rt_size_t tsize;
/* setup two timers, ONESHOT & PERIOD each
*/
tmr_dev_0 = rt_device_find("timer0");
@@ -210,13 +217,83 @@ void main(void)
}
else
{
rt_kprintf("timer1 started !\n");
rt_kprintf("timer1 started !\n\n");
}
}
}
#else
#define test_hwtimer() do {} while(0)
#endif
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_pin_write(LED1_PIN, led1 = PIN_HIGH);
#ifdef RT_USING_SPI
static struct rt_spi_device spi_dev_w25q;
static void test_spi_master(void)
{
struct rt_spi_configuration cfg;
struct rt_spi_message msg1, msg2;
rt_err_t res;
uint8_t buf[16];
cfg.max_hz = 25 * 1000000;
cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_CS_HIGH;
res = rt_spi_bus_attach_device(
&spi_dev_w25q, W25Q32_SPI_NAME, SPI0_BUS_NAME, (void *)W25Q32_CS_PIN);
if (res == RT_EOK && rt_spi_configure(&spi_dev_w25q, &cfg) == RT_EOK)
{
/* cmd : Read Manufacturer / Device ID (90h) */
buf[0] = 0x90;
/* address : 0 */
buf[1] = buf[2] = buf[3] = 0;
msg1.send_buf = buf;
msg1.recv_buf = RT_NULL;
msg1.length = 4;
msg1.cs_take = 1;
msg1.cs_release = 0;
msg1.next = &msg2;
msg2.send_buf = RT_NULL;
msg2.recv_buf = buf;
msg2.length = 2;
msg2.cs_take = 0;
msg2.cs_release = 1;
msg2.next = RT_NULL;
rt_spi_transfer_message(&spi_dev_w25q, &msg1);
rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", buf[0], buf[1]);
/* cmd : Read Data (03h) */
buf[0] = 0x03;
/* address : 0 */
buf[1] = buf[2] = buf[3] = 0;
msg2.length = 16;
if (rt_spi_transfer_message(&spi_dev_w25q, &msg1) == RT_NULL)
{
rt_kprintf("rt_spi_transfer_message() 16-byte-read DMA done\n\n");
}
}
else
{
rt_kprintf("w25q32 attach/configure failed (%d) !\n", res);
}
}
#else
#define test_spi_master() do {} while(0)
#endif
void main(void)
{
uint32_t wdog_timeout = 32;
rt_kprintf("\nCH569W-R0-1v0, HCLK: %dMHz\n\n", sys_hclk_get() / 1000000);
test_gpio_int();
test_watchdog(wdog_timeout);
test_hwtimer();
test_spi_master();
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
rt_pin_write(LED0_PIN, led0 = PIN_LOW);
+15
View File
@@ -51,6 +51,21 @@ config BSP_USING_TIMER
bool "using TMR2"
default n
endif
config BSP_USING_SPI
bool "using on-chip spi"
select RT_USING_SPI
default n
if BSP_USING_SPI
config BSP_USING_SPI0
bool "using SPI0"
default n
config BSP_USING_SPI1
bool "using SPI1"
default n
endif
endmenu
menu "Onboard Peripheral Drivers"
+4
View File
@@ -13,11 +13,15 @@
#include <stdint.h>
#include "ch56x_sys.h"
#include "ch56x_gpio.h"
#include "ch56x_spi.h"
#define LED0_PIN GET_PIN(B, 24)
#define LED1_PIN GET_PIN(B, 22)
#define LED2_PIN GET_PIN(B, 23)
#define W25Q32_CS_PIN GET_PIN(A, 12)
#define W25Q32_SPI_NAME "spi00"
#define SYS_HCLK_FREQ 80000000 // 80 MHz
#define RAMX_SIZE 32 // USER_MEM 00/01/1x : 32/64/96 KB
+3
View File
@@ -72,6 +72,7 @@
#define RT_SERIAL_RB_BUFSZ 64
#define RT_USING_HWTIMER
#define RT_USING_PIN
#define RT_USING_SPI
#define RT_USING_WDT
/* Using USB */
@@ -183,6 +184,8 @@
#define BSP_USING_TIMER
#define BSP_USING_TMR0
#define BSP_USING_TMR1
#define BSP_USING_SPI
#define BSP_USING_SPI0
/* Onboard Peripheral Drivers */