Skip to content

Commit 5877d97

Browse files
authored
Merge pull request RT-Thread#3735 from imgtec/bsp-ls2k/pwm-v2
bsp: ls2k: pwm driver
2 parents 7e5373e + cde1e5d commit 5877d97

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

Diff for: bsp/ls2kdev/drivers/drv_pwm.c

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Copyright(c) 2020, Du Huanpeng<[email protected]>
7+
*
8+
*/
9+
10+
#include <rtthread.h>
11+
#include <rtdevice.h>
12+
#include <ls2k1000.h>
13+
14+
#ifdef RT_USING_PWM
15+
16+
#define PWM0_BASE (0xFFFFFFFFBFe02000)
17+
#define PWM1_BASE (0xFFFFFFFFBFe02010)
18+
#define PWM2_BASE (0xFFFFFFFFBFe02020)
19+
#define PWM3_BASE (0xFFFFFFFFBFe02030)
20+
21+
#define CTRL_EN (1UL<<0)
22+
#define CTRL_OE (1UL<<3)
23+
#define CTRL_SINGL (1UL<<4)
24+
#define CTRL_INTE (1UL<<5)
25+
#define CTRL_INT (1UL<<6)
26+
#define CTRL_RST (1UL<<7)
27+
#define CTRL_CAPTE (1UL<<8)
28+
#define CTRL_INVERT (1UL<<9)
29+
#define CTRL_DZONE (1UL<<10)
30+
31+
struct loongson_pwm {
32+
rt_uint32_t __PAD0;
33+
rt_uint32_t low_buffer;
34+
rt_uint32_t full_buffer;
35+
rt_uint32_t ctrl;
36+
};
37+
38+
rt_err_t loongson_pwm_enable(struct rt_device_pwm *device, int channel)
39+
{
40+
int **priv;
41+
struct loongson_pwm *chip;
42+
volatile rt_uint64_t *config0;
43+
rt_uint64_t m;
44+
45+
channel %= 4;
46+
47+
config0 = (void *)GEN_CONFIG0_REG;
48+
m = 1ULL << 12 << channel;
49+
*config0 |= m;
50+
51+
priv = device->parent.user_data;
52+
chip = (void *)priv[channel];
53+
chip->ctrl = CTRL_EN;
54+
55+
return RT_EOK;
56+
}
57+
58+
rt_err_t loongson_pwm_disable(struct rt_device_pwm *device, int channel)
59+
{
60+
struct loongson_pwm **chip;
61+
rt_uint64_t m;
62+
63+
chip = device->parent.user_data;
64+
channel %= 4;
65+
chip[channel]->ctrl &= ~CTRL_EN;
66+
67+
return RT_EOK;
68+
}
69+
70+
rt_err_t loongson_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
71+
{
72+
struct loongson_pwm *chip;
73+
rt_uint32_t **priv;
74+
75+
priv = device->parent.user_data;
76+
channel %= 4;
77+
chip = (void *)priv[channel];
78+
79+
chip->ctrl &= ~CTRL_EN;
80+
chip->full_buffer = period;
81+
chip->low_buffer = pulse;
82+
chip->ctrl |= CTRL_EN;
83+
84+
return RT_EOK;
85+
}
86+
87+
static rt_err_t loongson_pwm_ioctl(struct rt_device_pwm *device, int cmd, void *arg)
88+
{
89+
rt_err_t rc;
90+
struct rt_pwm_configuration *cfg;
91+
92+
cfg = (void *)arg;
93+
94+
switch (cmd) {
95+
case PWM_CMD_ENABLE:
96+
rc = loongson_pwm_enable(device, cfg->channel);
97+
break;
98+
case PWM_CMD_DISABLE:
99+
rc = loongson_pwm_disable(device, cfg->channel);
100+
break;
101+
case PWM_CMD_SET:
102+
rc = loongson_pwm_set(device, cfg->channel, cfg->period, cfg->pulse);
103+
break;
104+
case PWM_CMD_GET:
105+
rc = RT_ENOSYS;
106+
break;
107+
default:
108+
rc = RT_EINVAL;
109+
break;
110+
}
111+
return rc;
112+
}
113+
114+
struct rt_pwm_ops loongson_pwm_ops = {
115+
.control = loongson_pwm_ioctl,
116+
};
117+
118+
struct rt_device_pwm loongson_pwm = {
119+
.ops = &loongson_pwm_ops,
120+
};
121+
122+
int loongson_pwm_init(void)
123+
{
124+
int rc = RT_EOK;
125+
static rt_uint32_t *priv[] = {
126+
(void *)PWM0_BASE,
127+
(void *)PWM1_BASE,
128+
(void *)PWM2_BASE,
129+
(void *)PWM3_BASE
130+
};
131+
rc = rt_device_pwm_register(&loongson_pwm, "pwm0", &loongson_pwm_ops, &priv);
132+
return rc;
133+
}
134+
INIT_DEVICE_EXPORT(loongson_pwm_init);
135+
136+
#endif /*RT_USING_PWM*/

Diff for: bsp/ls2kdev/drivers/ls2k1000.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define PLL_SYS_BASE 0xFFFFFFFFBFE10480
2323
#define RTC_BASE 0xFFFFFFFFBFE07820
2424

25+
#define GEN_CONFIG0_REG 0xFFFFFFFFBfe10420
26+
2527
void rt_hw_timer_handler(void);
2628
void rt_hw_uart_init(void);
2729

0 commit comments

Comments
 (0)