Merge pull request #763 from caogos/master

[bsp] Add GPIO/PWM/timer drivers to RT-Thread.
This commit is contained in:
Bernard Xiong 2017-07-18 11:50:30 +08:00 committed by GitHub
commit 8819016f28
18 changed files with 1657 additions and 0 deletions

40
bsp/ls1cdev/drivers/led.c Normal file
View File

@ -0,0 +1,40 @@
// led接口
// 使用低电平点亮led高电平熄灭led
#include "ls1c_gpio.h"
// 初始化led
// @led_gpio led所在gpio引脚
void led_init(unsigned int led_gpio)
{
gpio_init(led_gpio, gpio_mode_output);
gpio_set(led_gpio, gpio_level_high); // 指示灯默认熄灭
return ;
}
// 点亮led
// @led_gpio led所在gpio引脚
void led_on(unsigned int led_gpio)
{
gpio_set(led_gpio, gpio_level_low);
return ;
}
// 熄灭led
// @led_gpio
void led_off(unsigned int led_gpio)
{
gpio_set(led_gpio, gpio_level_high);
return ;
}

27
bsp/ls1cdev/drivers/led.h Normal file
View File

@ -0,0 +1,27 @@
// led接口
// 使用低电平点亮led高电平熄灭led
#ifndef __OPENLOONGSON_LED_H
#define __OPENLOONGSON_LED_H
// 初始化led
// @led_gpio led所在gpio引脚
void led_init(unsigned int led_gpio);
// 点亮led
// @led_gpio led所在gpio引脚
void led_on(unsigned int led_gpio);
// 熄灭led
// @led_gpio
void led_off(unsigned int led_gpio);
#endif

View File

@ -0,0 +1,10 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('Libraries', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@ -0,0 +1,155 @@
/*************************************************************************
*
*
*
*************************************************************************/
#include "ls1c_regs.h"
#include "ls1c_public.h"
// 晶振的频率
#define AHB_CLK (24000000)
#define APB_CLK (AHB_CLK)
// START_FREQ寄存器bits
#define M_PLL_SHIFT (8)
#define M_PLL (0xff << M_PLL_SHIFT) // PLL倍频系数的整数部分
#define FRAC_N_SHIFT (16)
#define FRAC_N (0xff << FRAC_N_SHIFT) // PLL倍频系数的小数部分
#define DIV_SDRAM_SHIFT (0)
#define DIV_SDRAM (0x3 << DIV_SDRAM_SHIFT)
// CLK_DIV_PARAM寄存器bits
#define DIV_PIX_EN (0x1 << 31)
#define DIV_PIX (0x7f << 24)
#define DIV_CAM_EN (0x1 << 23)
#define DIV_CAM (0x7f << 16)
#define DIV_CPU_EN (0x1 << 15)
#define DIV_CPU (0x7f << 8)
#define DIV_PIX_VALID (0x1 << 5)
#define DIV_PIX_SEL (0x1 << 4)
#define DIV_CAM_VALID (0x1 << 3)
#define DIV_CAM_SEL (0x1 << 2)
#define DIV_CPU_VALID (0x1 << 1)
#define DIV_CPU_SEL (0x1 << 0)
#define DIV_PIX_SHIFT (24)
#define DIV_CAM_SHIFT (16)
#define DIV_CPU_SHIFT (8)
/*
* PLL频率
* @ret PLL频率
*/
unsigned long clk_get_pll_rate(void)
{
unsigned int ctrl;
unsigned long pll_rate = 0;
ctrl = reg_read_32((volatile unsigned int *)LS1C_START_FREQ);
pll_rate = (((ctrl & M_PLL) >> M_PLL_SHIFT) + ((ctrl & FRAC_N) >> FRAC_N_SHIFT)) * APB_CLK / 4;
return pll_rate;
}
/*
* CPU频率
* @ret CPU频率
*/
unsigned long clk_get_cpu_rate(void)
{
unsigned long pll_rate, cpu_rate;
unsigned int ctrl;
pll_rate = clk_get_pll_rate();
ctrl = reg_read_32((volatile unsigned int *)LS1C_CLK_DIV_PARAM);
// 选择时钟来源
if (DIV_CPU_SEL & ctrl) // pll分频作为时钟信号
{
if (DIV_CPU_EN & ctrl)
{
cpu_rate = pll_rate / ((ctrl & DIV_CPU) >> DIV_CPU_SHIFT);
}
else
{
cpu_rate = pll_rate / 2;
}
}
else // bypass模式晶振作为时钟输入
{
cpu_rate = APB_CLK;
}
return cpu_rate;
}
/*
* DDR频率
* @ret DDR频率
*/
unsigned long clk_get_ddr_rate(void)
{
unsigned long cpu_rate = 0;
unsigned long ddr_rate = 0;
unsigned int ctrl;
cpu_rate = clk_get_cpu_rate();
ctrl = reg_read_32((volatile unsigned int *)LS1C_START_FREQ);
ctrl = (ctrl & DIV_SDRAM) >> DIV_SDRAM_SHIFT;
switch (ctrl)
{
case 0:
ddr_rate = cpu_rate / 2;
break;
case 1:
ddr_rate = cpu_rate / 4;
break;
case 2:
case 3:
ddr_rate = cpu_rate / 3;
break;
}
return ddr_rate;
}
/*
* APB频率
* @ret APB频率
*/
unsigned long clk_get_apb_rate(void)
{
return clk_get_ddr_rate();
}
/*
* DC频率
* @ret DC频率
*/
unsigned long clk_get_dc_rate(void)
{
unsigned long pll_rate, dc_rate;
unsigned int ctrl;
pll_rate = clk_get_pll_rate();
ctrl = reg_read_32((volatile unsigned int *)LS1C_CLK_DIV_PARAM);
dc_rate = pll_rate / ((ctrl & DIV_PIX) >> DIV_PIX_SHIFT);
return dc_rate;
}

View File

@ -0,0 +1,51 @@
/*************************************************************************
*
*
*
*************************************************************************/
#ifndef __OPENLOONGSON_CLOCK_H
#define __OPENLOONGSON_CLOCK_H
/*
* PLL频率
* @ret PLL频率
*/
unsigned long clk_get_pll_rate(void);
/*
* CPU频率
* @ret CPU频率
*/
unsigned long clk_get_cpu_rate(void);
/*
* DDR频率
* @ret DDR频率
*/
unsigned long clk_get_ddr_rate(void);
/*
* APB频率
* @ret APB频率
*/
unsigned long clk_get_apb_rate(void);
/*
* DC频率
* @ret DC频率
*/
unsigned long clk_get_dc_rate(void);
#endif

View File

@ -0,0 +1,80 @@
// 软件延时源文件
#include "ls1c_clock.h"
/*
* ms
* @j ms
*/
void delay_ms(int j)
{
int k_max = clk_get_cpu_rate()/1000/3; // 除以1000表示ms除以3为测试所得的经验(可以理解为最内层循环执行一次需要的时钟个数)
int k = k_max;
for ( ; j > 0; j--)
{
for (k = k_max; k > 0; k--)
{
__asm__ ("nop"); // 注意,这里必须用内联汇编,否则会被优化掉
}
}
return ;
}
/*
* us
* @n us
*/
void delay_us(int n)
{
int count_1us = clk_get_cpu_rate() / 1000000 / 3; // 延时1us的循环次数
int count_max; // 延时n微秒的循环次数
int tmp;
// 根据延时长短微调(注意这里是手动优化的cpu频率改变了可能需要重新优化此时cpu频率为252Mhz)
if (10 >= n) // <=10us
{
count_1us -= 35;
}
else if (100 >= n) // <= 100us
{
count_1us -= 6;
}
else // > 100us
{
count_1us -= 1;
}
count_max = n * count_1us;
// 延时
for (tmp = count_max; tmp > 0; tmp--)
{
__asm__ ("nop"); // 注意,这里必须用内联汇编,否则会被优化掉
}
return ;
}
/*
* s
* @i s
*/
void delay_s(int i)
{
for ( ; i > 0; i--)
{
delay_ms(1000);
}
return ;
}

View File

@ -0,0 +1,34 @@
// 软件延时头文件
#ifndef __OPENLOONGSON_DELAY_H
#define __OPENLOONGSON_DELAY_H
/*
* ms
* @j ms
*/
void delay_ms(int j);
/*
* us
* @n us
*/
void delay_us(int n);
/*
* s
* @i s
*/
void delay_s(int i);
#endif

View File

@ -0,0 +1,207 @@
// 封装gpio接口
#include "ls1c_public.h"
#include "ls1c_regs.h"
#include "ls1c_gpio.h"
#include "ls1c_pin.h"
/*
* gpio的CFG寄存器
* @gpio gpio编号
* @ret CFG寄存器
*/
volatile unsigned int *gpio_get_cfg_reg(unsigned int gpio)
{
volatile unsigned int *gpio_cfgx = NULL; // GPIO_CFGx寄存器
unsigned int port = GPIO_GET_PORT(gpio);
switch (port)
{
case 0:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG0;
break;
case 1:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG1;
break;
case 2:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG2;
break;
case 3:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG3;
break;
default:
gpio_cfgx = NULL;
break;
}
return gpio_cfgx;
}
/*
* gpio的EN寄存器
* @gpio gpio编号
* @ret EN寄存器
*/
volatile unsigned int *gpio_get_en_reg(unsigned int gpio)
{
volatile unsigned int *gpio_enx = NULL; // GPIO_ENx寄存器
unsigned int port = GPIO_GET_PORT(gpio);
switch (port)
{
case 0:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN0;
break;
case 1:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN1;
break;
case 2:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN2;
break;
case 3:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN3;
break;
default:
gpio_enx = NULL;
return gpio_enx;
}
return gpio_enx;
}
/*
* gpio初始化
* @gpio gpio引脚[0, 127]
* @mode gpio的工作模式()
*
* : gpio50初始化为输出
* gpio_init(50, gpio_mode_output);
*/
void gpio_init(unsigned int gpio, gpio_mode_t mode)
{
volatile unsigned int *gpio_enx = NULL; // GPIO_ENx寄存器
unsigned int pin = GPIO_GET_PIN(gpio);
// 将pin设为普通GPIO
pin_set_purpose(gpio, PIN_PURPOSE_GPIO);
// 设置gpio工作模式(输入、输出)
gpio_enx = gpio_get_en_reg(gpio);
if (gpio_mode_output == mode) // 输出
{
reg_clr_one_bit(gpio_enx, pin);
}
else // 输入
{
reg_set_one_bit(gpio_enx, pin);
}
return ;
}
/*
* gpio输出高电平或低电平
* @gpio gpio引脚[0, 127]
* @level
*
* : gpio50上输出低电平
* gpio_set(50, gpio_level_low);
*/
void gpio_set(unsigned int gpio, gpio_level_t level)
{
volatile unsigned int *gpio_outx = NULL; // GPIO_OUTx寄存器
unsigned int port = GPIO_GET_PORT(gpio);
unsigned int pin = GPIO_GET_PIN(gpio);
// 获取寄存器地址
switch (port)
{
case 0:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT0;
break;
case 1:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT1;
break;
case 2:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT2;
break;
case 3:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT3;
break;
default: // 正确的程序不应该走到这里,直接返回
return ;
}
// 输出
if (gpio_level_low == level)
{
reg_clr_one_bit(gpio_outx, pin);
}
else
{
reg_set_one_bit(gpio_outx, pin);
}
return ;
}
/*
* gpio引脚的值
* @gpio gpio引脚[0,127]
*
* : gpio50引脚上的值
* gpio_level_t level;
* level = gpio_get(50);
*/
unsigned int gpio_get(unsigned int gpio)
{
volatile unsigned int *gpio_inx = NULL; // GPIO_INx寄存器
unsigned int port = GPIO_GET_PORT(gpio);
unsigned int pin = GPIO_GET_PIN(gpio);
// 获取寄存器地址
switch (port)
{
case 0:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN0;
break;
case 1:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN1;
break;
case 2:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN2;
break;
case 3:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN3;
break;
default: // 正常的流程不应该走到这里,直接返回
return 0;
}
// 读取
return reg_get_bit(gpio_inx, pin);
}

View File

@ -0,0 +1,79 @@
#ifndef __OPENLOONGSON_GPIO_H
#define __OPENLOONGSON_GPIO_H
// 龙芯1c的gpio是按照0,1,2,3,4...这样的顺序编号的,
// 但在操作寄存器的时候又是按照每32个一组来分的
// 这里利用这个特性将每组的32个gpio叫做一个"port"每个gpio在每组中的索引叫"pin"
// port = gpio / 32
// pin = gpio % 32
// 例如GPIO50port=1,pin=18
#define GPIO_GET_PORT(gpio) ((gpio) / 32)
#define GPIO_GET_PIN(gpio) ((gpio) % 32)
// gpio的工作模式--输入、输出
typedef enum{
gpio_mode_output = 0, // 输出
gpio_mode_input = 1 // 输入
}gpio_mode_t;
// gpio高低电平值
typedef enum{
gpio_level_low = 0, // 低电平
gpio_level_high = 1 // 高电平
}gpio_level_t;
/*
* gpio的CFG寄存器
* @gpio gpio编号
* @ret CFG寄存器
*/
volatile unsigned int *gpio_get_cfg_reg(unsigned int gpio);
/*
* gpio初始化
* @gpio gpio引脚[0, 127]
* @mode gpio的工作模式()
*
* : gpio50初始化为输出
* gpio_init(50, gpio_mode_output);
*/
void gpio_init(unsigned int gpio, gpio_mode_t mode);
/*
* gpio输出高电平或低电平
* @gpio gpio引脚[0, 127]
* @level
*
* : gpio50上输出低电平
* gpio_set(50, gpio_level_low);
*/
void gpio_set(unsigned int gpio, gpio_level_t level);
/*
* gpio引脚的值
* @gpio gpio引脚[0,127]
*
* : gpio50引脚上的值
* gpio_level_t level;
* level = gpio_get(50);
*/
unsigned int gpio_get(unsigned int gpio);
#endif

View File

@ -0,0 +1,143 @@
// 引脚功能(普通gpiopwm复用等)相关接口
#include "ls1c_public.h"
#include "ls1c_regs.h"
#include "ls1c_gpio.h"
#include "ls1c_pin.h"
/*
* pin设置为指定用途(gpiogpio)
* @gpio gpio引脚编号
* @purpose
*/
void pin_set_purpose(unsigned int gpio, pin_purpose_t purpose)
{
volatile unsigned int *gpio_cfgx; // GPIO_CFGx寄存器
unsigned int pin = GPIO_GET_PIN(gpio);
gpio_cfgx = gpio_get_cfg_reg(gpio);
if (PIN_PURPOSE_GPIO == purpose) // 引脚用作普通gpio
{
reg_set_one_bit(gpio_cfgx, pin);
}
else // 引脚用作其它功能(非gpio)
{
reg_clr_one_bit(gpio_cfgx, pin);
}
return ;
}
/*
* pin为第n复用
* @gpio gpio编号
* @remap n复用
*/
void pin_set_remap(unsigned int gpio, pin_remap_t remap)
{
volatile unsigned int *reg = NULL; // 复用寄存器
unsigned int port = GPIO_GET_PORT(gpio);
unsigned int pin = GPIO_GET_PIN(gpio);
switch (port)
{
case 0:
switch (remap)
{
case PIN_REMAP_FIRST:
reg = (volatile unsigned int *)LS1C_CBUS_FIRST0;
break;
case PIN_REMAP_SECOND:
reg = (volatile unsigned int *)LS1C_CBUS_SECOND0;
break;
case PIN_REMAP_THIRD:
reg = (volatile unsigned int *)LS1C_CBUS_THIRD0;
break;
case PIN_REMAP_FOURTH:
reg = (volatile unsigned int *)LS1C_CBUS_FOURTH0;
break;
case PIN_REMAP_FIFTH:
reg = (volatile unsigned int *)LS1C_CBUS_FIFTH0;
break;
}
break;
case 1:
switch (remap)
{
case PIN_REMAP_FIRST:
reg = (volatile unsigned int *)LS1C_CBUS_FIRST1;
break;
case PIN_REMAP_SECOND:
reg = (volatile unsigned int *)LS1C_CBUS_SECOND1;
break;
case PIN_REMAP_THIRD:
reg = (volatile unsigned int *)LS1C_CBUS_THIRD1;
break;
case PIN_REMAP_FOURTH:
reg = (volatile unsigned int *)LS1C_CBUS_FOURTH1;
break;
case PIN_REMAP_FIFTH:
reg = (volatile unsigned int *)LS1C_CBUS_FIFTH1;
break;
}
break;
case 2:
switch (remap)
{
case PIN_REMAP_FIRST:
reg = (volatile unsigned int *)LS1C_CBUS_FIRST2;
break;
case PIN_REMAP_SECOND:
reg = (volatile unsigned int *)LS1C_CBUS_SECOND2;
break;
case PIN_REMAP_THIRD:
reg = (volatile unsigned int *)LS1C_CBUS_THIRD2;
break;
case PIN_REMAP_FOURTH:
reg = (volatile unsigned int *)LS1C_CBUS_FOURTH2;
break;
case PIN_REMAP_FIFTH:
reg = (volatile unsigned int *)LS1C_CBUS_FIFTH2;
break;
}
break;
case 3:
switch (remap)
{
case PIN_REMAP_FIRST:
reg = (volatile unsigned int *)LS1C_CBUS_FIRST3;
break;
case PIN_REMAP_SECOND:
reg = (volatile unsigned int *)LS1C_CBUS_SECOND3;
break;
case PIN_REMAP_THIRD:
reg = (volatile unsigned int *)LS1C_CBUS_THIRD3;
break;
case PIN_REMAP_FOURTH:
reg = (volatile unsigned int *)LS1C_CBUS_FOURTH3;
break;
case PIN_REMAP_FIFTH:
reg = (volatile unsigned int *)LS1C_CBUS_FIFTH3;
break;
}
break;
default:
return ;
}
// 置1
reg_set_one_bit(reg, pin);
return ;
}

View File

@ -0,0 +1,43 @@
// 引脚功能(普通gpiopwm复用等)相关接口
#ifndef __OPENLOONGSON_PIN_H
#define __OPENLOONGSON_PIN_H
// 引脚用途
typedef enum
{
PIN_PURPOSE_GPIO = 0, // 引脚用作普通gpio
PIN_PURPOSE_OTHER, // 引脚用作其它功能(非gpio)
}pin_purpose_t;
// 引脚复用
typedef enum
{
PIN_REMAP_FIRST = 0, // 第一复用
PIN_REMAP_SECOND, // 第二复用
PIN_REMAP_THIRD, // 第三复用
PIN_REMAP_FOURTH, // 第四复用
PIN_REMAP_FIFTH, // 第五复用
}pin_remap_t;
/*
* pin设置为指定用途(gpiogpio)
* @gpio gpio引脚编号
* @purpose
*/
void pin_set_purpose(unsigned int gpio, pin_purpose_t purpose);
/*
* pin为第n复用
* @gpio gpio编号
* @remap n复用
*/
void pin_set_remap(unsigned int gpio, pin_remap_t remap);
#endif

View File

@ -0,0 +1,79 @@
// 一些常用的、共用的接口
/*
* 1
* @reg
* @bit 1bit
*/
void reg_set_one_bit(volatile unsigned int *reg, unsigned int bit)
{
unsigned int temp, mask;
mask = 1 << bit;
temp = *reg;
temp |= mask;
*reg = temp;
return ;
}
/*
*
* @reg
* @bit bit
*/
void reg_clr_one_bit(volatile unsigned int *reg, unsigned int bit)
{
unsigned int temp, mask;
mask = 1 << bit;
temp = *reg;
temp &= ~mask;
*reg = temp;
return ;
}
/*
*
* @reg
* @bit bit
* @ret
*/
unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit)
{
unsigned int temp;
temp = *reg;
temp = (temp >> bit) & 1;
return temp;
}
/*
* 32bit的数据
* @data
* @addr
*/
void reg_write_32(unsigned int data, volatile unsigned int *addr)
{
*addr = data;
}
/*
* 32bit数据
* @addr
* @ret
*/
unsigned int reg_read_32(volatile unsigned int *addr)
{
return (*addr);
}

View File

@ -0,0 +1,78 @@
// 一些常用的、共用的接口
#ifndef __OPENLOONGSON_PUBLIC_H
#define __OPENLOONGSON_PUBLIC_H
#include <stdio.h>
// pmon提供的打印函数见main()函数
struct callvectors {
int (*open) (char *, int, int);
int (*close) (int);
int (*read) (int, void *, int);
int (*write) (int, void *, int);
long long (*lseek) (int, long long, int);
int (*printf) (const char *, ...);
void (*cacheflush) (void);
char *(*gets) (char *);
};
#define myprintf (*callvec->printf)
#define mygets (*callvec->gets)
extern struct callvectors *callvec;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
typedef enum
{
FALSE=0,
TRUE=1
}BOOL;
/*
* 1
* @reg
* @bit 1bit
*/
void reg_set_one_bit(volatile unsigned int *reg, unsigned int bit);
/*
*
* @reg
* @bit bit
*/
void reg_clr_one_bit(volatile unsigned int *reg, unsigned int bit);
/*
*
* @reg
* @bit bit
* @ret
*/
unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit);
/*
* 32bit的数据
* @data
* @addr
*/
void reg_write_32(unsigned int data, volatile unsigned int *addr);
/*
* 32bit数据
* @addr
* @ret
*/
unsigned int reg_read_32(volatile unsigned int *addr);
#endif

View File

@ -0,0 +1,197 @@
// 封装硬件pwm接口
#include "ls1c_public.h"
#include "ls1c_pin.h"
#include "ls1c_pwm.h"
#include "ls1c_clock.h"
#include "ls1c_regs.h"
// pwm的最大周期
#define PWM_MAX_PERIOD (0xFFFFFF) // 计数器的值
/*
* gpio获取相应pwm的基地址
* @gpio pwm引脚
* @ret pwm基地址
*/
unsigned int pwm_get_reg_base(unsigned int gpio)
{
unsigned int reg_base = 0;
switch (gpio)
{
case LS1C_PWM0_GPIO06:
case LS1C_PWM0_GPIO04:
reg_base = LS1C_REG_BASE_PWM0;
break;
case LS1C_PWM1_GPIO92:
case LS1C_PWM1_GPIO05:
reg_base = LS1C_REG_BASE_PWM1;
break;
case LS1C_PWM2_GPIO52:
case LS1C_PWM2_GPIO46:
reg_base = LS1C_REG_BASE_PWM2;
break;
case LS1C_PWM3_GPIO47:
case LS1C_PWM3_GPIO53:
reg_base = LS1C_REG_BASE_PWM3;
break;
}
return reg_base;
}
/*
* pwm
* @pwm_info PWMn的详细信息
*/
void pwm_disable(pwm_info_t *pwm_info)
{
unsigned int pwm_reg_base = 0;
// 检查入参
if (NULL == pwm_info)
{
return ;
}
pwm_reg_base = pwm_get_reg_base(pwm_info->gpio);
reg_write_32(0, (volatile unsigned int *)(pwm_reg_base + LS1C_PWM_CTRL));
return ;
}
/*
* 使PWM
* @pwm_info PWMn的详细信息
*/
void pwm_enable(pwm_info_t *pwm_info)
{
unsigned int pwm_reg_base = 0;
unsigned int ctrl = 0;
// 检查入参
if (NULL == pwm_info)
{
return ;
}
// 获取基地址
pwm_reg_base = pwm_get_reg_base(pwm_info->gpio);
// 清零计数器
reg_write_32(0, (volatile unsigned int *)(pwm_reg_base + LS1C_PWM_CNTR));
// 设置控制寄存器
ctrl = (0 << LS1C_PWM_INT_LRC_EN)
| (0 << LS1C_PWM_INT_HRC_EN)
| (0 << LS1C_PWM_CNTR_RST)
| (0 << LS1C_PWM_INT_SR)
| (0 << LS1C_PWM_INTEN)
| (0 << LS1C_PWM_OE)
| (1 << LS1C_PWM_CNT_EN);
if (PWM_MODE_PULSE == pwm_info->mode) // 单脉冲
{
ctrl |= (1 << LS1C_PWM_SINGLE);
}
else // 连续脉冲
{
ctrl &= ~(1 << LS1C_PWM_SINGLE);
}
reg_write_32(ctrl, (volatile unsigned int *)(pwm_reg_base + LS1C_PWM_CTRL));
return ;
}
/*
* PWMn
* @pwm_info PWMn的详细信息
*/
void pwm_init(pwm_info_t *pwm_info)
{
unsigned int gpio;
unsigned long pwm_clk = 0; // pwm模块的时钟频率
unsigned long tmp = 0;
unsigned int pwm_reg_base = 0;
unsigned long period = 0;
// 判断入参
if (NULL == pwm_info)
{
// 入参非法,则直接返回
return ;
}
gpio = pwm_info->gpio;
// 配置相应引脚用作pwm而非gpio
pin_set_purpose(gpio, PIN_PURPOSE_OTHER);
// 复用
switch (gpio)
{
// 不需要复用
case LS1C_PWM0_GPIO06:
case LS1C_PWM1_GPIO92:
break;
case LS1C_PWM0_GPIO04: // gpio04的第三复用
pin_set_remap(LS1C_PWM0_GPIO04, PIN_REMAP_THIRD);
break;
case LS1C_PWM1_GPIO05: // gpio05的第三复用
pin_set_remap(LS1C_PWM1_GPIO05, PIN_REMAP_THIRD);
break;
case LS1C_PWM2_GPIO52: // gpio52的第四复用
pin_set_remap(LS1C_PWM2_GPIO52, PIN_REMAP_FOURTH);
break;
case LS1C_PWM2_GPIO46: // gpio46的第四复用
pin_set_remap(LS1C_PWM2_GPIO46, PIN_REMAP_FOURTH);
break;
case LS1C_PWM3_GPIO47: // gpio47的第四复用
pin_set_remap(LS1C_PWM3_GPIO47, PIN_REMAP_FOURTH);
break;
case LS1C_PWM3_GPIO53: // gpio53的第四复用
pin_set_remap(LS1C_PWM3_GPIO53, PIN_REMAP_FOURTH);
break;
default:
break;
}
// 根据占空比和pwm周期计算寄存器HRC和LRC的值
// 两个64位数相乘只能得到低32位linux下却可以得到64位结果
// 暂不清楚原因,用浮点运算代替
pwm_clk = clk_get_apb_rate();
period = (1.0 * pwm_clk * pwm_info->period_ns) / 1000000000;
period = MIN(period, PWM_MAX_PERIOD); // 限制周期不能超过最大值
tmp = period - (period * pwm_info->duty);
// 写寄存器HRC和LRC
pwm_reg_base = pwm_get_reg_base(gpio);
reg_write_32(--tmp, (volatile unsigned int *)(pwm_reg_base + LS1C_PWM_HRC));
reg_write_32(--period, (volatile unsigned int *)(pwm_reg_base + LS1C_PWM_LRC));
// 写主计数器
pwm_enable(pwm_info);
return ;
}

View File

@ -0,0 +1,77 @@
#ifndef __OPENLOONGSON_PWM_H
#define __OPENLOONGSON_PWM_H
// pwm引脚定义
#define LS1C_PWM0_GPIO06 (6) // gpio06用作pwm0
#define LS1C_PWM0_GPIO04 (4) // gpio04复用为pwm0
#define LS1C_PWM1_GPIO92 (92) // gpio92用作pwm1
#define LS1C_PWM1_GPIO05 (5) // gpio05复用为pwm1
#define LS1C_PWM2_GPIO52 (52) // gpio52复用为pwm2
#define LS1C_PWM2_GPIO46 (46) // gpio46复用为pwm2
#define LS1C_PWM3_GPIO47 (47) // gpio47复用为pwm3
#define LS1C_PWM3_GPIO53 (53) // gpio53复用为pwm3
// ...还有一些gpio可以复用为gpio的有需要可以自己添加
// pwm控制寄存器的每个bit
#define LS1C_PWM_INT_LRC_EN (11) // 低脉冲计数器中断使能
#define LS1C_PWM_INT_HRC_EN (10) // 高脉冲计数器中断使能
#define LS1C_PWM_CNTR_RST (7) // 使能CNTR计数器清零
#define LS1C_PWM_INT_SR (6) // 中断状态位
#define LS1C_PWM_INTEN (5) // 中断使能位
#define LS1C_PWM_SINGLE (4) // 单脉冲控制位
#define LS1C_PWM_OE (3) // 脉冲输出使能
#define LS1C_PWM_CNT_EN (0) // 主计数器使能
// 硬件pwm工作模式
enum
{
// 正常模式--连续输出pwm波形
PWM_MODE_NORMAL = 0,
// 单脉冲模式每次调用只发送一个脉冲调用间隔必须大于pwm周期
PWM_MODE_PULSE
};
// 硬件pwm信息
typedef struct
{
unsigned int gpio; // PWMn所在的gpio
unsigned int mode; // 工作模式(单脉冲、连续脉冲)
float duty; // pwm的占空比
unsigned long period_ns; // pwm周期(单位ns)
}pwm_info_t;
/*
* PWMn
* @pwm_info PWMn的详细信息
*/
void pwm_init(pwm_info_t *pwm_info);
/*
* pwm
* @pwm_info PWMn的详细信息
*/
void pwm_disable(pwm_info_t *pwm_info);
/*
* 使PWM
* @pwm_info PWMn的详细信息
*/
void pwm_enable(pwm_info_t *pwm_info);
#endif

View File

@ -0,0 +1,88 @@
// 龙芯1c外设寄存器
#ifndef __OPENLOONGSON_LS1C_REGS_H
#define __OPENLOONGSON_LS1C_REGS_H
// 时钟相关寄存器地址
#define LS1C_START_FREQ (0xbfe78030)
#define LS1C_CLK_DIV_PARAM (0xbfe78034)
// gpio相关寄存器地址
#define LS1C_GPIO_CFG0 (0xbfd010c0)
#define LS1C_GPIO_EN0 (0xbfd010d0)
#define LS1C_GPIO_IN0 (0xbfd010e0)
#define LS1C_GPIO_OUT0 (0xbfd010f0)
#define LS1C_GPIO_CFG1 (0xbfd010c4)
#define LS1C_GPIO_EN1 (0xbfd010d4)
#define LS1C_GPIO_IN1 (0xbfd010e4)
#define LS1C_GPIO_OUT1 (0xbfd010f4)
#define LS1C_GPIO_CFG2 (0xbfd010c8)
#define LS1C_GPIO_EN2 (0xbfd010d8)
#define LS1C_GPIO_IN2 (0xbfd010e8)
#define LS1C_GPIO_OUT2 (0xbfd010f8)
#define LS1C_GPIO_CFG3 (0xbfd010cc)
#define LS1C_GPIO_EN3 (0xbfd010dc)
#define LS1C_GPIO_IN3 (0xbfd010ec)
#define LS1C_GPIO_OUT3 (0xbfd010fc)
// 复用相关寄存器
#define LS1C_CBUS_FIRST0 (0xbfd011c0)
#define LS1C_CBUS_SECOND0 (0xbfd011d0)
#define LS1C_CBUS_THIRD0 (0xbfd011e0)
#define LS1C_CBUS_FOURTH0 (0xbfd011f0)
#define LS1C_CBUS_FIFTH0 (0xbfd01200)
#define LS1C_CBUS_FIRST1 (0xbfd011c4)
#define LS1C_CBUS_SECOND1 (0xbfd011d4)
#define LS1C_CBUS_THIRD1 (0xbfd011e4)
#define LS1C_CBUS_FOURTH1 (0xbfd011f4)
#define LS1C_CBUS_FIFTH1 (0xbfd01204)
#define LS1C_CBUS_FIRST2 (0xbfd011c8)
#define LS1C_CBUS_SECOND2 (0xbfd011d8)
#define LS1C_CBUS_THIRD2 (0xbfd011e8)
#define LS1C_CBUS_FOURTH2 (0xbfd011f8)
#define LS1C_CBUS_FIFTH2 (0xbfd01208)
#define LS1C_CBUS_FIRST3 (0xbfd011cc)
#define LS1C_CBUS_SECOND3 (0xbfd011dc)
#define LS1C_CBUS_THIRD3 (0xbfd011ec)
#define LS1C_CBUS_FOURTH3 (0xbfd011fc)
#define LS1C_CBUS_FIFTH3 (0xbfd0120c)
// PWM寄存器偏移
#define LS1C_PWM_CNTR (0x0)
#define LS1C_PWM_HRC (0x4)
#define LS1C_PWM_LRC (0x8)
#define LS1C_PWM_CTRL (0xC)
// PWM基地址
#define LS1C_REG_BASE_PWM0 (0xbfe5c000)
#define LS1C_REG_BASE_PWM1 (0xbfe5c010)
#define LS1C_REG_BASE_PWM2 (0xbfe5c020)
#define LS1C_REG_BASE_PWM3 (0xbfe5c030)
#endif

View File

@ -0,0 +1,200 @@
// 硬件定时器源码
#include "ls1c_public.h"
#include "ls1c_pin.h"
#include "ls1c_clock.h"
#include "ls1c_regs.h"
#include "ls1c_pwm.h"
#include "ls1c_timer.h"
// 定时器中计数器(CNTR、HRC和LRC)的最大值
#define TIMER_COUNTER_MAX (0xffffff)
/*
*
* @timer
* @ret
*/
unsigned int timer_get_reg_base(ls1c_timer_t timer)
{
unsigned int reg_base = 0;
switch (timer)
{
case TIMER_PWM0:
reg_base = LS1C_REG_BASE_PWM0;
break;
case TIMER_PWM1:
reg_base = LS1C_REG_BASE_PWM1;
break;
case TIMER_PWM2:
reg_base = LS1C_REG_BASE_PWM2;
break;
case TIMER_PWM3:
reg_base = LS1C_REG_BASE_PWM3;
break;
}
return reg_base;
}
/*
*
* @timer_info
*/
void timer_init(timer_info_t *timer_info)
{
unsigned int timer_reg_base = 0; // 寄存器基地址
unsigned long timer_clk = 0; // 硬件定时器的时钟
unsigned long tmp;
unsigned int ctrl = 0; // 控制寄存器中的控制信息
// 判断入参
if (NULL == timer_info)
{
return ;
}
/*
*
* = * (ns) / 1000000000
* 1c的定时器时钟为APB时钟126Mhz
*
*/
timer_clk = clk_get_apb_rate();
tmp = (timer_clk / 1000000) * (timer_info->time_ns / 1000); // 将1000000000拆分为1000000和1000
tmp = MIN(tmp, TIMER_COUNTER_MAX);
// 控制寄存器信息
ctrl = (1 << LS1C_PWM_INT_LRC_EN)
| (0 << LS1C_PWM_INT_HRC_EN)
| (0 << LS1C_PWM_CNTR_RST)
| (0 << LS1C_PWM_INT_SR)
| (1 << LS1C_PWM_INTEN)
| (1 << LS1C_PWM_SINGLE)
| (1 << LS1C_PWM_OE)
| (1 << LS1C_PWM_CNT_EN);
// 设置各个寄存器
timer_reg_base = timer_get_reg_base(timer_info->timer); // 获取寄存器基地址
reg_write_32(0, (volatile unsigned int *)(timer_reg_base + LS1C_PWM_HRC));
reg_write_32(tmp--, (volatile unsigned int *)(timer_reg_base + LS1C_PWM_LRC));
reg_write_32(0, (volatile unsigned int *)(timer_reg_base + LS1C_PWM_CNTR));
reg_write_32(ctrl, (volatile unsigned int *)(timer_reg_base + LS1C_PWM_CTRL));
return ;
}
/*
* ()
* @timer_info
* @ret TRUE or FALSE
*/
BOOL timer_is_time_out(timer_info_t *timer_info)
{
unsigned int timer_reg_base = 0; // 寄存器基地址
unsigned int ctrl; // 控制寄存器的值
// 判断入参
if (NULL == timer_info)
{
return FALSE;
}
// 读取控制寄存器
timer_reg_base = timer_get_reg_base(timer_info->timer);
ctrl = reg_read_32((volatile unsigned int *)(timer_reg_base + LS1C_PWM_CTRL));
// 判断中断状态位
if (ctrl & (1 << LS1C_PWM_INT_SR))
{
return TRUE;
}
else
{
return FALSE;
}
}
/*
*
* @timer_info
*/
void timer_stop(timer_info_t *timer_info)
{
unsigned int timer_reg_base = 0;
// 判断入参
if (NULL == timer_info)
{
return ;
}
timer_reg_base = timer_get_reg_base(timer_info->timer);
reg_write_32(0, (volatile unsigned int *)(timer_reg_base + LS1C_PWM_CTRL));
return ;
}
/*
* ()ns
* @timer_info
* @ret ns
*/
unsigned long timer_get_time_ns(timer_info_t *timer_info)
{
unsigned int timer_reg_base = 0;
unsigned int cntr = 0; // 寄存器CNTR的值
unsigned long time_ns = 0; // 时间单位ns
unsigned long timer_clk = 0; // 定时器时钟
// 读取寄存器CNTR的值
timer_reg_base = timer_get_reg_base(timer_info->timer);
cntr = reg_read_32((volatile unsigned int *)(timer_reg_base + LS1C_PWM_CNTR));
/*
* CNTR值换算为时间us
* = (CNTR * 1000000000) /
* = (CNTR * 1000) / ( / 1000000)
*/
timer_clk = clk_get_apb_rate();
time_ns = (cntr * 1000 ) / (timer_clk /1000000);
// myprintf("[%s] time_us=%lu, cntr=%d, timer_clk=%d\n", __FUNCTION__, time_ns, cntr, timer_clk);
return time_ns;
}
/*
* timer相关寄存器的值
* @timer_info
*/
void timer_print_regs(timer_info_t *timer_info)
{
unsigned int timer_reg_base = 0;
timer_reg_base = timer_get_reg_base(timer_info->timer);
myprintf("CNTR=0x%x, HRC=0x%x, LRC=0x%x, CTRL=0x%x\n",
reg_read_32((volatile unsigned int *)(timer_reg_base + LS1C_PWM_CNTR)),
reg_read_32((volatile unsigned int *)(timer_reg_base + LS1C_PWM_HRC)),
reg_read_32((volatile unsigned int *)(timer_reg_base + LS1C_PWM_LRC)),
reg_read_32((volatile unsigned int *)(timer_reg_base + LS1C_PWM_CTRL)));
return ;
}

View File

@ -0,0 +1,69 @@
// 硬件定时器头文件
#ifndef __OPENLOONGSON_TIMER_H
#define __OPENLOONGSON_TIMER_H
#include "ls1c_public.h"
// 硬件定时器
typedef enum
{
TIMER_PWM0, // PWM0用作硬件定时器
TIMER_PWM1, // PWM1用作硬件定时器
TIMER_PWM2, // PWM2用作硬件定时器
TIMER_PWM3 // PWM3用作硬件定时器
}ls1c_timer_t;
// 硬件定时器信息
typedef struct
{
ls1c_timer_t timer; // 硬件定时器
unsigned long time_ns; // 定时时间
}timer_info_t;
/*
*
* @timer_info
*/
void timer_init(timer_info_t *timer_info);
/*
*
* @timer_info
* @ret TRUE or FALSE
*/
BOOL timer_is_time_out(timer_info_t *timer_info);
/*
*
* @timer_info
*/
void timer_stop(timer_info_t *timer_info);
/*
* ()ns
* @timer_info
* @ret ns
*/
unsigned long timer_get_time_ns(timer_info_t *timer_info);
/*
* timer相关寄存器的值
* @timer_info
*/
void timer_print_regs(timer_info_t *timer_info);
#endif