[BSP][ls1c]Fixed some bugs of ls1c lib.

This commit is contained in:
sundm75 2018-02-09 09:21:02 +08:00
parent d08ce950e5
commit c121d823fd
3 changed files with 68 additions and 57 deletions

View File

@ -42,6 +42,15 @@ 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);
int i;
/*指定全部pin复用为0*/
for(i =0; i <=4; i++)
{
reg = (volatile unsigned int *)((LS1C_CBUS_FIRST0)+((port)*0x04)+((i)*0x10));
// 置0
reg_clr_one_bit(reg, pin);
}
switch (port)
{

View File

@ -20,6 +20,7 @@ typedef enum
PIN_REMAP_THIRD, // 第三复用
PIN_REMAP_FOURTH, // 第四复用
PIN_REMAP_FIFTH, // 第五复用
PIN_REMAP_DEFAULT, //缺省复用
}pin_remap_t;

View File

@ -19,10 +19,10 @@
*
* Change Logs:
* Date Author Notes
* 2017-10-23 first version
* 2017-10-23 first version
*/
// 硬件spi接口源文件
// 硬件spi接口源文件
#include <string.h>
@ -35,8 +35,8 @@
/*
* SPI模块的基地址
* @SPIx SPI模块的编号
* SPI模块的基地址
* @SPIx SPI模块的编号
*/
inline void *ls1c_spi_get_base(unsigned char SPIx)
{
@ -62,8 +62,8 @@ inline void *ls1c_spi_get_base(unsigned char SPIx)
/*
* SPI模块的所有寄存器的值
* @spi_base
* SPI模块的所有寄存器的值
* @spi_base
*/
void ls1c_spi_print_all_regs_info(void *spi_base)
{
@ -81,9 +81,9 @@ void ls1c_spi_print_all_regs_info(void *spi_base)
/*
* SPI时钟频率计算分频系数
* @max_speed_hz SPI最大通信速度
* @ret
* SPI时钟频率计算分频系数
* @max_speed_hz SPI最大通信速度
* @ret
*/
unsigned int ls1c_spi_get_div(unsigned int max_speed_hz)
{
@ -145,28 +145,28 @@ unsigned int ls1c_spi_get_div(unsigned int max_speed_hz)
/*
*
* @spi_base
* @max_hz hz
*
* @spi_base
* @max_hz hz
*/
void ls1c_spi_set_clock(void *spi_base, unsigned long max_hz)
{
unsigned int div = 0;
unsigned char val = 0;
// 获取分频系数
// 获取分频系数
div = ls1c_spi_get_div(max_hz);
// 设置spr
// 设置spr
val = reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET);
val &= (~LS1C_SPI_SPCR_SPR_MASK); // spr清零
val |= (div & LS1C_SPI_SPCR_SPR_MASK); // 设置新的spr
val &= (~LS1C_SPI_SPCR_SPR_MASK); // spr清零
val |= (div & LS1C_SPI_SPCR_SPR_MASK); // 设置新的spr
reg_write_8(val, spi_base + LS1C_SPI_SPCR_OFFSET);
// 设置spre
// 设置spre
val = reg_read_8(spi_base + LS1C_SPI_SPER_OFFSET);
val &= (~LS1C_SPI_SPER_SPRE_MASK); // spre清零
val |= ((div >> 2) & LS1C_SPI_SPER_SPRE_MASK); // 设置新的spre
val &= (~LS1C_SPI_SPER_SPRE_MASK); // spre清零
val |= ((div >> 2) & LS1C_SPI_SPER_SPRE_MASK); // 设置新的spre
reg_write_8(val, spi_base + LS1C_SPI_SPER_OFFSET);
return ;
@ -174,10 +174,10 @@ void ls1c_spi_set_clock(void *spi_base, unsigned long max_hz)
/*
* ()
* @spi_base
* @cpol
* @cpha
* ()
* @spi_base
* @cpol
* @cpha
*/
void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha)
{
@ -185,13 +185,13 @@ void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha)
val = reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET);
// 设置时钟极性--cpol
val &= (~LS1C_SPI_SPCR_CPOL_MASK); // cpol清0
val |= (cpol << LS1C_SPI_SPCR_CPOL_BIT); // 写入新的cpol
// 设置时钟极性--cpol
val &= (~LS1C_SPI_SPCR_CPOL_MASK); // cpol清0
val |= (cpol << LS1C_SPI_SPCR_CPOL_BIT); // 写入新的cpol
// 设置时钟相位--cpha
val &= (~LS1C_SPI_SPCR_CPHA_MASK); // cpha清0
val |= (cpha << LS1C_SPI_SPCR_CPHA_BIT); // 写入新的cpha
// 设置时钟相位--cpha
val &= (~LS1C_SPI_SPCR_CPHA_MASK); // cpha清0
val |= (cpha << LS1C_SPI_SPCR_CPHA_BIT); // 写入新的cpha
reg_write_8(val, spi_base + LS1C_SPI_SPCR_OFFSET);
@ -200,23 +200,24 @@ void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha)
/*
*
* @spi_base
* @cs
* @new_status 01
*
* @spi_base
* @cs
* @new_status 01
*/
void ls1c_spi_set_cs(void *spi_base, unsigned char cs, int new_status)
{
unsigned char val = 0;
val = 0xf0 | (0x01 << cs); // 全部csn=1指定的csen=1
val = reg_read_8(spi_base + LS1C_SPI_SFC_SOFTCS_OFFSET);
val |= 0x01 << cs ; //对应的csen=1
if (new_status) // cs = 1
{
val |= (0x10 << cs); // 指定csn=1
val |= (0x10 << cs); // 指定csn=1
}
else // cs = 0
{
val &= ~(0x10 << cs); // 指定csn=0
val &= ~(0x10 << cs); // 指定csn=0
}
reg_write_8(val, spi_base + LS1C_SPI_SFC_SOFTCS_OFFSET);
@ -225,8 +226,8 @@ void ls1c_spi_set_cs(void *spi_base, unsigned char cs, int new_status)
/*
*
* @spi_base
*
* @spi_base
*/
inline void ls1c_spi_wait_txrx_done(void *spi_base)
{
@ -243,25 +244,25 @@ inline void ls1c_spi_wait_txrx_done(void *spi_base)
/*
*
* @spi_base
*
* @spi_base
*/
inline void ls1c_spi_clear(void *spi_base)
{
unsigned char val = 0;
// 清中断
// 清中断
val = reg_read_8(spi_base + LS1C_SPI_SPSR_OFFSET);
val |= LS1C_SPI_SPSR_SPIF_MASK;
reg_write_8(val, spi_base + LS1C_SPI_SPSR_OFFSET);
// 清溢出标志位(Write-Collision Clear)
// 清溢出标志位(Write-Collision Clear)
val = reg_read_8(spi_base + LS1C_SPI_SPSR_OFFSET);
if (LS1C_SPI_SPSR_WCOL_MASK & val)
{
rt_kprintf("[%s] clear register SPSR's wcol!\r\n"); // 手册和linux源码中不一样加个打印看看
reg_write_8(val & ~LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // 写0linux源码中是写0
// reg_write_8(val | LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // 写1按照1c手册应该写1
rt_kprintf("[%s] clear register SPSR's wcol!\r\n"); // 手册和linux源码中不一样加个打印看看
reg_write_8(val & ~LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // 写0linux源码中是写0
// reg_write_8(val | LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // 写1按照1c手册应该写1
}
return ;
@ -270,23 +271,23 @@ inline void ls1c_spi_clear(void *spi_base)
/*
* SPI发送接收一个字节
*
* SPI总线上的从设备通信
* 1c的每路SPI上可能接有不同的从设备
* @spi_base
* @tx_ch
* @ret
* SPI发送接收一个字节
*
* SPI总线上的从设备通信
* 1c的每路SPI上可能接有不同的从设备
* @spi_base
* @tx_ch
* @ret
*/
unsigned char ls1c_spi_txrx_byte(void *spi_base, unsigned char tx_ch)
{
unsigned char rx_ch = 0;
// 收发数据
reg_write_8(tx_ch, spi_base + LS1C_SPI_TxFIFO_OFFSET); // 开始发送
ls1c_spi_wait_txrx_done(spi_base); // 等待收发完成
rx_ch = reg_read_8(spi_base + LS1C_SPI_RxFIFO_OFFSET); // 读取收到的数据
ls1c_spi_clear(spi_base); // 清中断和标志位
// 收发数据
reg_write_8(tx_ch, spi_base + LS1C_SPI_TxFIFO_OFFSET); // 开始发送
ls1c_spi_wait_txrx_done(spi_base); // 等待收发完成
rx_ch = reg_read_8(spi_base + LS1C_SPI_RxFIFO_OFFSET); // 读取收到的数据
ls1c_spi_clear(spi_base); // 清中断和标志位
return rx_ch;
}