rtt-f030/bsp/ls1cdev/libraries/ls1c_spi.c

296 lines
7.2 KiB
C
Raw Normal View History

2017-10-23 17:10:48 +08:00
/*
* File : ls1c_spi.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-10-23 <EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> first version
*/
// Ӳ<><D3B2>spi<70>ӿ<EFBFBD>Դ<EFBFBD>ļ<EFBFBD>
#include <string.h>
#include "ls1c_public.h"
#include "ls1c_regs.h"
#include "ls1c_clock.h"
#include "ls1c_spi.h"
/*
* <EFBFBD><EFBFBD>ȡָ<EFBFBD><EFBFBD>SPIģ<EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD>ַ
* @SPIx SPIģ<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>
*/
inline void *ls1c_spi_get_base(unsigned char SPIx)
2017-10-23 17:10:48 +08:00
{
void *base = NULL;
switch (SPIx)
{
case LS1C_SPI_0:
base = (void *)LS1C_SPI0_BASE;
break;
case LS1C_SPI_1:
base = (void *)LS1C_SPI1_BASE;
break;
default:
base = NULL;
break;
}
return base;
}
/*
* <EFBFBD><EFBFBD>ӡָ<EFBFBD><EFBFBD>SPIģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>мĴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2017-10-23 17:10:48 +08:00
*/
void ls1c_spi_print_all_regs_info(void *spi_base)
2017-10-23 17:10:48 +08:00
{
rt_kprintf("[%s] SPCR=0x%x, SPSR=0x%x, SPER=0x%x, SFC_PARAM=0x%x, SFC_SOFTCS=0x%x, SFC_TIMING=0x%x\r\n",
__FUNCTION__,
2017-10-23 17:10:48 +08:00
reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SPSR_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SPER_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SFC_PARAM_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SFC_SOFTCS_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SFC_TIMING_OFFSET));
return ;
}
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>SPIʱ<EFBFBD><EFBFBD>Ƶ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶϵ<EFBFBD><EFBFBD>
* @max_speed_hz SPI<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
* @ret <EFBFBD><EFBFBD>Ƶϵ<EFBFBD><EFBFBD>
*/
unsigned int ls1c_spi_get_div(unsigned int max_speed_hz)
2017-10-23 17:10:48 +08:00
{
unsigned long clk = 0;
unsigned int div = 0;
unsigned int div_tmp = 0;
unsigned int bit = 0;
clk = clk_get_apb_rate();
div = DIV_ROUND_UP(clk, max_speed_hz);
if (div < 2)
div = 2;
if (div > 4096)
div = 4096;
bit = ls1c_fls(div) - 1;
switch (1 << bit)
{
case 16:
div_tmp = 2;
if (div > (1 << bit))
{
div_tmp++;
}
break;
case 32:
div_tmp = 3;
if (div > (1 << bit))
{
div_tmp += 2;
}
break;
case 8:
div_tmp = 4;
if (div > (1 << bit))
{
div_tmp -= 2;
}
break;
default:
div_tmp = bit - 1;
if (div > (1 << bit))
{
div_tmp++;
}
break;
}
/*
rt_kprintf("[%s] clk=%ld, max_speed_hz=%d, div_tmp=%d, bit=%d\r\n",
__FUNCTION__, clk, max_speed_hz, div_tmp, bit);
*/
return div_tmp;
}
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @max_hz <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD>λhz
2017-10-23 17:10:48 +08:00
*/
void ls1c_spi_set_clock(void *spi_base, unsigned long max_hz)
2017-10-23 17:10:48 +08:00
{
unsigned int div = 0;
unsigned char val = 0;
// <20><>ȡ<EFBFBD><C8A1>Ƶϵ<C6B5><CFB5>
div = ls1c_spi_get_div(max_hz);
2017-10-23 17:10:48 +08:00
// <20><><EFBFBD><EFBFBD>spr
val = reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET);
val &= (~LS1C_SPI_SPCR_SPR_MASK); // spr<70><72><EFBFBD><EFBFBD>
val |= (div & LS1C_SPI_SPCR_SPR_MASK); // <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>spr
reg_write_8(val, spi_base + LS1C_SPI_SPCR_OFFSET);
// <20><><EFBFBD><EFBFBD>spre
val = reg_read_8(spi_base + LS1C_SPI_SPER_OFFSET);
val &= (~LS1C_SPI_SPER_SPRE_MASK); // spre<72><65><EFBFBD><EFBFBD>
val |= ((div >> 2) & LS1C_SPI_SPER_SPRE_MASK); // <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>spre
reg_write_8(val, spi_base + LS1C_SPI_SPER_OFFSET);
return ;
}
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>ģʽ(ʱ<EFBFBD>Ӽ<EFBFBD><EFBFBD>Ժ<EFBFBD><EFBFBD><EFBFBD>λ)
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @cpol ʱ<EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD>
* @cpha ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
2017-10-23 17:10:48 +08:00
*/
void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha)
2017-10-23 17:10:48 +08:00
{
unsigned char val = 0;
val = reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET);
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD>--cpol
val &= (~LS1C_SPI_SPCR_CPOL_MASK); // cpol<6F><6C>0
val |= (cpol << LS1C_SPI_SPCR_CPOL_BIT); // д<><D0B4><EFBFBD>µ<EFBFBD>cpol
2017-10-23 17:10:48 +08:00
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>λ--cpha
val &= (~LS1C_SPI_SPCR_CPHA_MASK); // cpha<68><61>0
val |= (cpha << LS1C_SPI_SPCR_CPHA_BIT); // д<><D0B4><EFBFBD>µ<EFBFBD>cpha
2017-10-23 17:10:48 +08:00
reg_write_8(val, spi_base + LS1C_SPI_SPCR_OFFSET);
return ;
}
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ƬѡΪָ<EFBFBD><EFBFBD>״̬
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @cs Ƭѡ
2017-10-23 17:10:48 +08:00
* @new_status Ƭѡ<EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>ȡֵΪ0<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ
*/
void ls1c_spi_set_cs(void *spi_base, unsigned char cs, int new_status)
2017-10-23 17:10:48 +08:00
{
unsigned char val = 0;
val = 0xf0 | (0x01 << cs); // ȫ<><C8AB>csn=1<><31>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>csen=1
if (new_status) // cs = 1
{
val |= (0x10 << cs); // ָ<><D6B8>csn=1
}
else // cs = 0
{
val &= ~(0x10 << cs); // ָ<><D6B8>csn=0
}
reg_write_8(val, spi_base + LS1C_SPI_SFC_SOFTCS_OFFSET);
return ;
}
/*
* <EFBFBD>ȴ<EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2017-10-23 17:10:48 +08:00
*/
inline void ls1c_spi_wait_txrx_done(void *spi_base)
2017-10-23 17:10:48 +08:00
{
int timeout = LS1C_SPI_TX_TIMEOUT;
while (timeout--)
{
if (LS1C_SPI_SPSR_SPIF_MASK & reg_read_8(spi_base + LS1C_SPI_SPSR_OFFSET))
break;
}
return ;
}
/*
* <EFBFBD><EFBFBD><EFBFBD>жϺͱ<EFBFBD>־λ
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2017-10-23 17:10:48 +08:00
*/
inline void ls1c_spi_clear(void *spi_base)
2017-10-23 17:10:48 +08:00
{
unsigned char val = 0;
// <20><><EFBFBD>ж<EFBFBD>
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);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ(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"); // <20>ֲ<EFBFBD><D6B2><EFBFBD>linuxԴ<78><D4B4><EFBFBD>в<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
reg_write_8(val & ~LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // д0<D0B4><30>linuxԴ<78><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д0
// reg_write_8(val | LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // д1<D0B4><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1c<31>ֲᣬӦ<E1A3AC><D3A6>д1
}
return ;
}
/*
* ͨ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>SPI<EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
* ע<EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<EFBFBD>У<EFBFBD><EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>ں<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>ֽڵĹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܱ<EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬһ<EFBFBD><EFBFBD>SPI<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵĴ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>о1c<EFBFBD><EFBFBD>ÿ·SPI<EFBFBD>Ͽ<EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD>в<EFBFBD>ͬ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>Ƶ<EFBFBD>ʡ<EFBFBD>ģʽ<EFBFBD>ȿ<EFBFBD><EFBFBD>ܲ<EFBFBD>ͬ
* @spi_base <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2017-10-23 17:10:48 +08:00
* @tx_ch <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @ret <EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
unsigned char ls1c_spi_txrx_byte(void *spi_base, unsigned char tx_ch)
2017-10-23 17:10:48 +08:00
{
unsigned char rx_ch = 0;
// <20>շ<EFBFBD><D5B7><EFBFBD><EFBFBD><EFBFBD>
reg_write_8(tx_ch, spi_base + LS1C_SPI_TxFIFO_OFFSET); // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
ls1c_spi_wait_txrx_done(spi_base); // <20>ȴ<EFBFBD><C8B4>շ<EFBFBD><D5B7><EFBFBD><EFBFBD><EFBFBD>
2017-10-23 17:10:48 +08:00
rx_ch = reg_read_8(spi_base + LS1C_SPI_RxFIFO_OFFSET); // <20><>ȡ<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ls1c_spi_clear(spi_base); // <20><><EFBFBD>жϺͱ<CFBA>־λ
2017-10-23 17:10:48 +08:00
return rx_ch;
}