From c121d823fd15fafddba0931e1809b1edaf2044e2 Mon Sep 17 00:00:00 2001 From: sundm75 Date: Fri, 9 Feb 2018 09:21:02 +0800 Subject: [PATCH] [BSP][ls1c]Fixed some bugs of ls1c lib. --- bsp/ls1cdev/libraries/ls1c_pin.c | 9 +++ bsp/ls1cdev/libraries/ls1c_pin.h | 1 + bsp/ls1cdev/libraries/ls1c_spi.c | 115 ++++++++++++++++--------------- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/bsp/ls1cdev/libraries/ls1c_pin.c b/bsp/ls1cdev/libraries/ls1c_pin.c index 2dd71d79b..15b4157b1 100644 --- a/bsp/ls1cdev/libraries/ls1c_pin.c +++ b/bsp/ls1cdev/libraries/ls1c_pin.c @@ -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) { diff --git a/bsp/ls1cdev/libraries/ls1c_pin.h b/bsp/ls1cdev/libraries/ls1c_pin.h index ca8ef3158..c483f5659 100644 --- a/bsp/ls1cdev/libraries/ls1c_pin.h +++ b/bsp/ls1cdev/libraries/ls1c_pin.h @@ -20,6 +20,7 @@ typedef enum PIN_REMAP_THIRD, // 第三复用 PIN_REMAP_FOURTH, // 第四复用 PIN_REMAP_FIFTH, // 第五复用 + PIN_REMAP_DEFAULT, //缺省复用 }pin_remap_t; diff --git a/bsp/ls1cdev/libraries/ls1c_spi.c b/bsp/ls1cdev/libraries/ls1c_spi.c index 7da50e1c3..e3df72906 100644 --- a/bsp/ls1cdev/libraries/ls1c_spi.c +++ b/bsp/ls1cdev/libraries/ls1c_spi.c @@ -19,10 +19,10 @@ * * Change Logs: * Date Author Notes - * 2017-10-23 勤为本 first version + * 2017-10-23 鍕や负鏈 first version */ -// 硬件spi接口源文件 +// 纭欢spi鎺ュ彛婧愭枃浠 #include @@ -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 片选引脚的新状态,取值为0或1,即高电平或低电平 + * 璁剧疆鎸囧畾鐗囬変负鎸囧畾鐘舵 + * @spi_base 鍩哄湴鍧 + * @cs 鐗囬 + * @new_status 鐗囬夊紩鑴氱殑鏂扮姸鎬侊紝鍙栧间负0鎴1锛屽嵆楂樼數骞虫垨浣庣數骞 */ 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 ; //瀵瑰簲鐨刢sen=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); // 写0,linux源码中是写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"); // 鎵嬪唽鍜宭inux婧愮爜涓笉涓鏍凤紝鍔犱釜鎵撳嵃鐪嬬湅 + reg_write_8(val & ~LS1C_SPI_SPSR_WCOL_MASK, spi_base + LS1C_SPI_SPSR_OFFSET); // 鍐0锛宭inux婧愮爜涓槸鍐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鍙戦佹帴鏀朵竴涓瓧鑺 + * 娉ㄦ剰锛屽湪澶氫换鍔$殑绯荤粺涓紝姝ゅ嚱鏁伴渶瑕佷簰鏂ャ + * 鍗充繚璇佸湪鍜屾煇涓粠璁惧鏀跺彂鏌愪釜瀛楄妭鐨勮繃绋嬩腑锛屼笉鑳借鍒囨崲鍒板叾瀹冧换鍔″悓鏃朵笌鍙﹀鐨勫湪鍚屼竴涓猄PI鎬荤嚎涓婄殑浠庤澶囬氫俊 + * 鍥犱负榫欒姱1c鐨勬瘡璺疭PI涓婂彲鑳芥帴鏈変笉鍚岀殑浠庤澶囷紝閫氫俊棰戠巼銆佹ā寮忕瓑鍙兘涓嶅悓 + * @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; }