diff options
| author | xph <[email protected]> | 2020-04-09 16:35:28 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-04-09 16:35:28 +0800 |
| commit | ca7f01d79b3e0a997c771cec298e4ebd65b75809 (patch) | |
| tree | 3d2c48aae7203b14038ba7b98b500b5343ed7f2a /bsp/ls1cdev/drivers | |
| parent | f763f8e7284b0492c5a65ad0abf94ee1963d7dcc (diff) | |
| parent | baa3145bfe61b3330b6a160523cc5470fa5db3a7 (diff) | |
Merge pull request #1 from RT-Thread/master
sync rtt
Diffstat (limited to 'bsp/ls1cdev/drivers')
43 files changed, 2505 insertions, 981 deletions
diff --git a/bsp/ls1cdev/drivers/SConscript b/bsp/ls1cdev/drivers/SConscript index 2363e655a8..33310b6d7e 100644 --- a/bsp/ls1cdev/drivers/SConscript +++ b/bsp/ls1cdev/drivers/SConscript @@ -1,14 +1,10 @@ from building import * cwd = GetCurrentDir() -src = Glob('*.c') +src = Glob('*.c') + Glob('*.cpp') + Glob('*_gcc.S') CPPPATH = [cwd] - -if GetDepend('RT_USING_RTGUI') == False: - SrcRemove(src, 'touch.c') - group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) objs = [] diff --git a/bsp/ls1cdev/drivers/board.c b/bsp/ls1cdev/drivers/board.c index a7100a04c4..b000daafc5 100644 --- a/bsp/ls1cdev/drivers/board.c +++ b/bsp/ls1cdev/drivers/board.c @@ -1,92 +1,27 @@ /* - * File : board.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006-2012, RT-Thread Develop Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2010-06-25 Bernard first version * 2011-08-08 lgnq modified for Loongson LS1B * 2015-07-06 chinesebear modified for Loongson LS1C + * 2019-12-04 Jiaxun Yang Adapt new generic MIPS code */ #include <rtthread.h> #include <rthw.h> +#include <mips.h> #include "board.h" #include "drv_uart.h" #include "ls1c.h" -#define A_K0BASE 0x80000000 - extern unsigned char __bss_end; - -extern void tlb_refill_exception(void); -extern void general_exception(void); -extern void irq_exception(void); -extern void rt_hw_cache_init(void); -extern void invalidate_writeback_dcache_all(void); -extern void invalidate_icache_all(void); - - -/** - * @addtogroup Loongson LS1B - */ - -/*@{*/ - -/** - * This is the timer interrupt service routine. - */ -void rt_hw_timer_handler(void) -{ - unsigned int count; - - count = read_c0_compare(); - write_c0_compare(count); - write_c0_count(0); - - /* increase a OS tick */ - rt_tick_increase(); -} - -/** - * This function will initial OS timer - */ -void rt_hw_timer_init(void) -{ - write_c0_compare(CPU_HZ/2/RT_TICK_PER_SECOND); - write_c0_count(0); -} - - -/** - * init hardware FPU - */ -void rt_hw_fpu_init(void) -{ - rt_uint32_t c0_status = 0; - rt_uint32_t c1_status = 0; - - // 使能协处理器1--FPU - c0_status = read_c0_status(); - c0_status |= (ST0_CU1 | ST0_FR); - write_c0_status(c0_status); - - // 配置FPU - c1_status = read_c1_status(); - c1_status |= (FPU_CSR_FS | FPU_CSR_FO | FPU_CSR_FN); // set FS, FO, FN - c1_status &= ~(FPU_CSR_ALL_E); // disable exception - c1_status = (c1_status & (~FPU_CSR_RM)) | FPU_CSR_RN; // set RN - write_c1_status(c1_status); - - return ; -} +#define RT_HW_HEAP_END (0x80000000 + MEM_SIZE) /** @@ -94,35 +29,24 @@ void rt_hw_fpu_init(void) */ void rt_hw_board_init(void) { - /* init cache */ - rt_hw_cache_init(); - - /* init hardware interrupt */ - rt_hw_interrupt_init(); - - /* clear bev */ - write_c0_status(read_c0_status()&(~(1<<22))); - - /* copy vector */ - rt_memcpy((void *)A_K0BASE, tlb_refill_exception, 0x80); - rt_memcpy((void *)(A_K0BASE + 0x180), general_exception, 0x80); - rt_memcpy((void *)(A_K0BASE + 0x200), irq_exception, 0x80); - - invalidate_writeback_dcache_all(); - invalidate_icache_all(); - + /* init hardware interrupt */ + rt_hw_exception_init(); + + /* init hardware interrupt */ + rt_hw_interrupt_init(); + #ifdef RT_USING_HEAP - rt_system_heap_init((void*)&__bss_end, (void*)RT_HW_HEAP_END); + rt_system_heap_init((void*)&__bss_end, (void*)RT_HW_HEAP_END); #endif #ifdef RT_USING_SERIAL - /* init hardware UART device */ - rt_hw_uart_init(); + /* init hardware UART device */ + rt_hw_uart_init(); #endif #ifdef RT_USING_CONSOLE - /* set console device */ - rt_console_set_device(RT_CONSOLE_DEVICE_NAME); + /* set console device */ + rt_console_set_device(RT_CONSOLE_DEVICE_NAME); #endif /* init operating system timer */ rt_hw_timer_init(); diff --git a/bsp/ls1cdev/drivers/board.h b/bsp/ls1cdev/drivers/board.h index 8124eae7a5..089384e348 100644 --- a/bsp/ls1cdev/drivers/board.h +++ b/bsp/ls1cdev/drivers/board.h @@ -1,26 +1,62 @@ /* - * File : board.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006-2012, RT-Thread Develop Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2010-06-25 Bernard first version * 2011-08-08 lgnq modified for Loongson LS1B * 2015-07-06 chinesebear modified for Loongson LS1C + * 2019-12-04 Jiaxun Yang Add board config defines */ #ifndef __BOARD_H__ #define __BOARD_H__ +#include <rtconfig.h> +#ifndef __ASSEMBLY__ void rt_hw_board_init(void); +#endif + +/* +* SDRAM config +* +* Take IS42S16400 SDRAM chip as a example +* The specfication is +* Size 8MB +* WIDTH: 16 bits +* COL WIDTH: 8 bits, so we should take the 8 power of 2, it's 256 +* ROW WIDTH: 12 bits, so we take the 12 power of 2, it's 4K +* +* Please reference macro SD_PARA0 and SDRAM registers intruduction +* in user's manual to edit the SDRAM config. +*/ + +#if defined(RT_LS1C_BAICAIBOARD) +/* 8MByte IS42S16400 appeared in bacai_board V1.x V2.x */ +#define MEM_SIZE (0x800000) +#define SDRAM_WIDTH (WIDTH_16) +#define SDRAM_COL (COL_256) +#define SDRAM_ROW (ROW_4K) +#elif defined(RT_LS1C_OPENLOONGSON) +/* 32MByte appeared in SmartLoong board */ +#define MEM_SIZE (0x2000000) +#define SDRAM_WIDTH (WIDTH_16) +#define SDRAM_COL (COL_512) +#define SDRAM_ROW (ROW_8K) +#else +#error Unknown Board +#endif + +/* Early Debug config */ +#define EARLY_DEBUG /* UART2 serial print debug in early stage */ +#define EARLY_DEBUG_BAUD (115200) -/* 32M SDRAM */ -#define RT_HW_HEAP_END (0x80000000 + 32 * 1024 * 1024) -#define CPU_HZ (252 * 1000000) +/* Clock config */ +#define CPU_HZ (252 * 1000000) +#define PLL_MULT (0x54) /* When external clock is 24Mhz PLL=504Mhz */ +#define SDRAM_DIV (0) /* SDRAM is CPU divided by 2*/ +#define CPU_DIV (2) /* CPU clock is PLL divided by 2 */ #endif diff --git a/bsp/ls1cdev/drivers/display_controller.c b/bsp/ls1cdev/drivers/display_controller.c index a09fe0269f..2a0bfcc619 100644 --- a/bsp/ls1cdev/drivers/display_controller.c +++ b/bsp/ls1cdev/drivers/display_controller.c @@ -1,237 +1,242 @@ /* - * File :display_controller.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: - * Date Author Notes + * Date Author Notes * 2011-08-09 lgnq first version for LS1B DC * 2015-07-06 chinesebear modified for loongson 1c * 2018-01-06 sundm75 modified for smartloong */ + #include <rtthread.h> -#include <rtthread.h> #include "display_controller.h" #include "../../libraries/ls1c_pwm.h" #include "../../libraries/ls1c_public.h" #include "../../libraries/ls1c_gpio.h" #include "../../libraries/ls1c_pin.h" -#ifdef RT_USING_RTGUI - +#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI) struct vga_struct vga_mode[] = { - {/*"480x272_60.00"*/ 111000, 480, 482, 523, 525, 272, 274, 284, 286, }, - {/*"640x480_70.00"*/ 28560, 640, 664, 728, 816, 480, 481, 484, 500, }, - {/*"640x640_60.00"*/ 33100, 640, 672, 736, 832, 640, 641, 644, 663, }, - {/*"640x768_60.00"*/ 39690, 640, 672, 736, 832, 768, 769, 772, 795, }, - {/*"640x800_60.00"*/ 42130, 640, 680, 744, 848, 800, 801, 804, 828, }, - {/*"800x480_70.00"*/ 35840, 800, 832, 912, 1024, 480, 481, 484, 500, }, - {/*"800x600_60.00"*/ 38220, 800, 832, 912, 1024, 600, 601, 604, 622, }, - {/*"800x640_60.00"*/ 40730, 800, 832, 912, 1024, 640, 641, 644, 663, }, - {/*"832x600_60.00"*/ 40010, 832, 864, 952, 1072, 600, 601, 604, 622, }, - {/*"832x608_60.00"*/ 40520, 832, 864, 952, 1072, 608, 609, 612, 630, }, - {/*"1024x480_60.00"*/ 38170, 1024, 1048, 1152, 1280, 480, 481, 484, 497, }, - {/*"1024x600_60.00"*/ 48960, 1024, 1064, 1168, 1312, 600, 601, 604, 622, }, - {/*"1024x640_60.00"*/ 52830, 1024, 1072, 1176, 1328, 640, 641, 644, 663, }, - {/*"1024x768_60.00"*/ 64110, 1024, 1080, 1184, 1344, 768, 769, 772, 795, }, - {/*"1152x764_60.00"*/ 71380, 1152, 1208, 1328, 1504, 764, 765, 768, 791, }, - {/*"1280x800_60.00"*/ 83460, 1280, 1344, 1480, 1680, 800, 801, 804, 828, }, - {/*"1280x1024_55.00"*/ 98600, 1280, 1352, 1488, 1696, 1024, 1025, 1028, 1057, }, - {/*"1440x800_60.00"*/ 93800, 1440, 1512, 1664, 1888, 800, 801, 804, 828, }, - {/*"1440x900_67.00"*/ 120280, 1440, 1528, 1680, 1920, 900, 901, 904, 935, }, + {/*"480x272_60.00"*/ 111000, 480, 482, 523, 525, 272, 274, 284, 286, }, + {/*"640x480_70.00"*/ 28560, 640, 664, 728, 816, 480, 481, 484, 500, }, + {/*"640x640_60.00"*/ 33100, 640, 672, 736, 832, 640, 641, 644, 663, }, + {/*"640x768_60.00"*/ 39690, 640, 672, 736, 832, 768, 769, 772, 795, }, + {/*"640x800_60.00"*/ 42130, 640, 680, 744, 848, 800, 801, 804, 828, }, + {/*"800x480_70.00"*/ 35840, 800, 832, 912, 1024, 480, 481, 484, 500, }, + {/*"800x600_60.00"*/ 38220, 800, 832, 912, 1024, 600, 601, 604, 622, }, + {/*"800x640_60.00"*/ 40730, 800, 832, 912, 1024, 640, 641, 644, 663, }, + {/*"832x600_60.00"*/ 40010, 832, 864, 952, 1072, 600, 601, 604, 622, }, + {/*"832x608_60.00"*/ 40520, 832, 864, 952, 1072, 608, 609, 612, 630, }, + {/*"1024x480_60.00"*/ 38170, 1024, 1048, 1152, 1280, 480, 481, 484, 497, }, + {/*"1024x600_60.00"*/ 48960, 1024, 1064, 1168, 1312, 600, 601, 604, 622, }, + {/*"1024x640_60.00"*/ 52830, 1024, 1072, 1176, 1328, 640, 641, 644, 663, }, + {/*"1024x768_60.00"*/ 64110, 1024, 1080, 1184, 1344, 768, 769, 772, 795, }, + {/*"1152x764_60.00"*/ 71380, 1152, 1208, 1328, 1504, 764, 765, 768, 791, }, + {/*"1280x800_60.00"*/ 83460, 1280, 1344, 1480, 1680, 800, 801, 804, 828, }, + {/*"1280x1024_55.00"*/ 98600, 1280, 1352, 1488, 1696, 1024, 1025, 1028, 1057, }, + {/*"1440x800_60.00"*/ 93800, 1440, 1512, 1664, 1888, 800, 801, 804, 828, }, + {/*"1440x900_67.00"*/ 120280, 1440, 1528, 1680, 1920, 900, 901, 904, 935, }, }; +static volatile int fb_index = 0; + ALIGN(16) volatile rt_uint16_t _rt_framebuffer[FB_YSIZE][FB_XSIZE]; +volatile rt_uint16_t _rt_framebuffer0[FB_YSIZE][FB_XSIZE]; +volatile rt_uint16_t _rt_framebuffer1[FB_YSIZE][FB_XSIZE]; static struct rt_device_graphic_info _dc_info; static void pwminit(void) { pwm_info_t pwm_info; pwm_info.gpio = LS1C_PWM0_GPIO06; // pwm引脚位gpio06 - pwm_info.mode = PWM_MODE_NORMAL; // 正常模式--连续输出pwm波形 - pwm_info.duty = 0.85; // pwm占空比 85% - pwm_info.period_ns = 5*1000*1000; // pwm周期5ms + pwm_info.mode = PWM_MODE_NORMAL; // 正常模式--连续输出pwm波形 + pwm_info.duty = 0.85; // pwm占空比 85% + pwm_info.period_ns = 5*1000*1000; // pwm周期5ms - /*pwm初始化,初始化后立即产生pwm波形*/ - pwm_init(&pwm_info); + /*pwm初始化,初始化后立即产生pwm波形*/ + pwm_init(&pwm_info); - /* 使能pwm */ - pwm_enable(&pwm_info); + /* 使能pwm */ + pwm_enable(&pwm_info); } int caclulate_freq(rt_uint32_t XIN, rt_uint32_t PCLK) { - rt_uint32_t divider_int; - rt_uint32_t needed_pixclk; - rt_uint32_t pll_clk, pix_div; - rt_uint32_t regval; + rt_uint32_t divider_int; + rt_uint32_t needed_pixclk; + rt_uint32_t pll_clk, pix_div; + rt_uint32_t regval; - pll_clk = PLL_FREQ; // 读CPU的 PLL及SDRAM 分频系数 - pll_clk =( pll_clk>>8 )& 0xff; - pll_clk = XIN * pll_clk / 4 ; - pix_div = PLL_DIV_PARAM;//读CPU的 CPU/CAMERA/DC 分频系数 - pix_div = (pix_div>>24)&0xff; - rt_kprintf("old pll_clk=%d, pix_div=%d\n", pll_clk, pix_div); + pll_clk = PLL_FREQ; // 读CPU的 PLL及SDRAM 分频系数 + pll_clk =( pll_clk>>8 )& 0xff; + pll_clk = XIN * pll_clk / 4 ; + pix_div = PLL_DIV_PARAM;//读CPU的 CPU/CAMERA/DC 分频系数 + pix_div = (pix_div>>24)&0xff; + rt_kprintf("old pll_clk=%d, pix_div=%d\n", pll_clk, pix_div); - divider_int = pll_clk/(1000000) *PCLK/1000; - if(divider_int%1000>=500) - divider_int = divider_int/1000+1; - else - divider_int = divider_int/1000; - rt_kprintf("divider_int = %d\n", divider_int); - - /* check whether divisor is too small. */ - if (divider_int < 1) { - rt_kprintf("Warning: clock source is too slow.Try smaller resolution\n"); - divider_int = 1; - } - else if(divider_int > 100) { - rt_kprintf("Warning: clock source is too fast.Try smaller resolution\n"); - divider_int = 100; - } - /* 配置分频寄存器 */ - { - rt_uint32_t regval = 0; - regval = PLL_DIV_PARAM; - /*首先需要把分频使能位清零 */ - regval &= ~0x80000030; //PIX_DIV_VALID PIX_SEL 置0 - regval &= ~(0x3f<<24); //PIX_DIV 清零 - regval |= divider_int << 24; - PLL_DIV_PARAM = regval; - regval |= 0x80000030; //PIX_DIV_VALID PIX_SEL 置1 - PLL_DIV_PARAM = regval; - } - rt_kprintf("new PLL_FREQ=0x%x, PLL_DIV_PARAM=0x%x\n", PLL_FREQ, PLL_DIV_PARAM); - rt_thread_delay(10); - return 0; + divider_int = pll_clk/(1000000) *PCLK/1000; + if(divider_int%1000>=500) + divider_int = divider_int/1000+1; + else + divider_int = divider_int/1000; + rt_kprintf("divider_int = %d\n", divider_int); + + /* check whether divisor is too small. */ + if (divider_int < 1) { + rt_kprintf("Warning: clock source is too slow.Try smaller resolution\n"); + divider_int = 1; + } + else if(divider_int > 100) { + rt_kprintf("Warning: clock source is too fast.Try smaller resolution\n"); + divider_int = 100; + } + /* 配置分频寄存器 */ + { + rt_uint32_t regval = 0; + regval = PLL_DIV_PARAM; + /*首先需要把分频使能位清零 */ + regval &= ~0x80000030; //PIX_DIV_VALID PIX_SEL 置0 + regval &= ~(0x3f<<24); //PIX_DIV 清零 + regval |= divider_int << 24; + PLL_DIV_PARAM = regval; + regval |= 0x80000030; //PIX_DIV_VALID PIX_SEL 置1 + PLL_DIV_PARAM = regval; + } + rt_kprintf("new PLL_FREQ=0x%x, PLL_DIV_PARAM=0x%x\n", PLL_FREQ, PLL_DIV_PARAM); + rt_thread_delay(10); + return 0; } static rt_err_t rt_dc_init(rt_device_t dev) { - int i, out, mode=-1; - int val; - - rt_kprintf("PWM initied\n"); - /* Set the back light PWM. */ - pwminit(); - - for (i=0; i<sizeof(vga_mode)/sizeof(struct vga_struct); i++) - { - if (vga_mode[i].hr == FB_XSIZE && vga_mode[i].vr == FB_YSIZE) - { - mode=i; - /* 计算时钟 配置频率*/ - caclulate_freq(OSC, vga_mode[i].pclk); - break; - } - } + int i, out, mode=-1; + int val; + + rt_kprintf("PWM initied\n"); + /* Set the back light PWM. */ + pwminit(); + + for (i=0; i<sizeof(vga_mode)/sizeof(struct vga_struct); i++) + { + if (vga_mode[i].hr == FB_XSIZE && vga_mode[i].vr == FB_YSIZE) + { + mode=i; + /* 计算时钟 配置频率*/ + caclulate_freq(OSC, vga_mode[i].pclk); + break; + } + } - if (mode<0) - { - rt_kprintf("\n\n\nunsupported framebuffer resolution\n\n\n"); - return; - } + if (mode<0) + { + rt_kprintf("\n\n\nunsupported framebuffer resolution\n\n\n"); + return RT_ERROR; + } - DC_FB_CONFIG = 0x0; - DC_FB_CONFIG = 0x3; // // framebuffer configuration RGB565 - DC_FB_BUFFER_ADDR0 = (rt_uint32_t)_rt_framebuffer - 0x80000000; - DC_FB_BUFFER_ADDR1 = (rt_uint32_t)_rt_framebuffer - 0x80000000; - DC_DITHER_CONFIG = 0x0; //颜色抖动配置寄存器 - DC_DITHER_TABLE_LOW = 0x0; //颜色抖动查找表低位寄存器 - DC_DITHER_TABLE_HIGH = 0x0; //颜色抖动查找表高位寄存器 - DC_PANEL_CONFIG = 0x80001311; //液晶面板配置寄存器 - DC_PANEL_TIMING = 0x0; + DC_FB_CONFIG = 0x0; + DC_FB_CONFIG = 0x3; // // framebuffer configuration RGB565 + DC_DITHER_CONFIG = 0x0; //颜色抖动配置寄存器 + DC_DITHER_TABLE_LOW = 0x0; //颜色抖动查找表低位寄存器 + DC_DITHER_TABLE_HIGH = 0x0; //颜色抖动查找表高位寄存器 + DC_PANEL_CONFIG = 0x80001311; //液晶面板配置寄存器 + DC_PANEL_TIMING = 0x0; - DC_HDISPLAY = (vga_mode[mode].hfl<<16) | vga_mode[mode].hr; - DC_HSYNC = 0x40000000 | (vga_mode[mode].hse<<16) | vga_mode[mode].hss; - DC_VDISPLAY = (vga_mode[mode].vfl<<16) | vga_mode[mode].vr; - DC_VSYNC = 0x40000000 | (vga_mode[mode].vse<<16) | vga_mode[mode].vss; + DC_HDISPLAY = (vga_mode[mode].hfl<<16) | vga_mode[mode].hr; + DC_HSYNC = 0x40000000 | (vga_mode[mode].hse<<16) | vga_mode[mode].hss; + DC_VDISPLAY = (vga_mode[mode].vfl<<16) | vga_mode[mode].vr; + DC_VSYNC = 0x40000000 | (vga_mode[mode].vse<<16) | vga_mode[mode].vss; #if defined(CONFIG_VIDEO_32BPP) - DC_FB_CONFIG = 0x00100105; - DC_FB_BUFFER_STRIDE = FB_XSIZE*4; + DC_FB_CONFIG = 0x00100105; + DC_FB_BUFFER_STRIDE = FB_XSIZE*4; #elif defined(CONFIG_VIDEO_24BPP) - DC_FB_CONFIG = 0x00100104; - DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255); + DC_FB_CONFIG = 0x00100104; + DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255); #elif defined(CONFIG_VIDEO_16BPP)// 使用这个选项 - DC_FB_CONFIG = 0x00100103; - DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255); + DC_FB_CONFIG = 0x00100103; + DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+0x7f)&(~0x7f); #elif defined(CONFIG_VIDEO_15BPP) - DC_FB_CONFIG = 0x00100102; - DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255); + DC_FB_CONFIG = 0x00100102; + DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255); #elif defined(CONFIG_VIDEO_12BPP) - DC_FB_CONFIG = 0x00100101; - DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255); + DC_FB_CONFIG = 0x00100101; + DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255); #else - DC_FB_CONFIG = 0x00100104; - DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255); + DC_FB_CONFIG = 0x00100104; + DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255); #endif - return RT_EOK; + return RT_EOK; } static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args) { - switch (cmd) - { - case RTGRAPHIC_CTRL_RECT_UPDATE: - break; - case RTGRAPHIC_CTRL_POWERON: - break; - case RTGRAPHIC_CTRL_POWEROFF: - break; - case RTGRAPHIC_CTRL_GET_INFO: - rt_memcpy(args, &_dc_info, sizeof(_dc_info)); - break; - case RTGRAPHIC_CTRL_SET_MODE: - break; - } + switch (cmd) + { + case RTGRAPHIC_CTRL_RECT_UPDATE: + { + if (fb_index == 0) + { + DC_FB_BUFFER_ADDR0 = (rt_uint32_t)_rt_framebuffer1 - 0x80000000; + DC_FB_BUFFER_ADDR1 = (rt_uint32_t)_rt_framebuffer1 - 0x80000000; + rt_memcpy((void *)_rt_framebuffer1, (const void *)_rt_framebuffer, sizeof(_rt_framebuffer)); + rt_memcpy((void *)_rt_framebuffer1, (const void *)_rt_framebuffer, sizeof(_rt_framebuffer)); + fb_index =1; + } + else + { + DC_FB_BUFFER_ADDR0 = (rt_uint32_t)_rt_framebuffer0 - 0x80000000; + DC_FB_BUFFER_ADDR1 = (rt_uint32_t)_rt_framebuffer0 - 0x80000000; + rt_memcpy((void *)_rt_framebuffer0, (const void *)_rt_framebuffer, sizeof(_rt_framebuffer)); + rt_memcpy((void *)_rt_framebuffer0, (const void *)_rt_framebuffer, sizeof(_rt_framebuffer)); + fb_index =0; + } + break; + } + case RTGRAPHIC_CTRL_POWERON: + break; + case RTGRAPHIC_CTRL_POWEROFF: + break; + case RTGRAPHIC_CTRL_GET_INFO: + rt_memcpy(args, &_dc_info, sizeof(_dc_info)); + break; + case RTGRAPHIC_CTRL_SET_MODE: + break; + } - return RT_EOK; + return RT_EOK; } void rt_hw_dc_init(void) { - rt_device_t dc = rt_malloc(sizeof(struct rt_device)); - if (dc == RT_NULL) - { - rt_kprintf("dc == RT_NULL\n"); - return; /* no memory yet */ - } + rt_device_t dc = rt_malloc(sizeof(struct rt_device)); + if (dc == RT_NULL) + { + rt_kprintf("dc == RT_NULL\n"); + return; /* no memory yet */ + } - _dc_info.bits_per_pixel = 16; - _dc_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P; - _dc_info.framebuffer = (rt_uint8_t*)HW_FB_ADDR; - _dc_info.width = FB_XSIZE; - _dc_info.height = FB_YSIZE; + _dc_info.bits_per_pixel = 16; + _dc_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565; + _dc_info.framebuffer = (rt_uint8_t*)HW_FB_ADDR; + _dc_info.width = FB_XSIZE; + _dc_info.height = FB_YSIZE; - /* init device structure */ - dc->type = RT_Device_Class_Graphic; - dc->init = rt_dc_init; - dc->open = RT_NULL; - dc->close = RT_NULL; - dc->control = rt_dc_control; - dc->user_data = (void*)&_dc_info; - - /* register Display Controller device to RT-Thread */ - rt_device_register(dc, "dc", RT_DEVICE_FLAG_RDWR); - - rt_device_init(dc); + /* init device structure */ + dc->type = RT_Device_Class_Graphic; + dc->init = rt_dc_init; + dc->open = RT_NULL; + dc->close = RT_NULL; + dc->control = rt_dc_control; + dc->user_data = (void*)&_dc_info; + + /* register Display Controller device to RT-Thread */ + rt_device_register(dc, "dc", RT_DEVICE_FLAG_RDWR); + + rt_device_init(dc); } #include <rtgui/driver.h> diff --git a/bsp/ls1cdev/drivers/display_controller.h b/bsp/ls1cdev/drivers/display_controller.h index ad252a9c2f..39f6ae8d52 100644 --- a/bsp/ls1cdev/drivers/display_controller.h +++ b/bsp/ls1cdev/drivers/display_controller.h @@ -1,11 +1,7 @@ /* - * File : display_controller.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006-2012, RT-Thread Develop Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -20,7 +16,7 @@ #include <rtthread.h> #include "ls1c.h" -#define DC_BASE 0xBC301240 //Display Controller +#define DC_BASE 0xBC301240 //Display Controller /* Frame Buffer registers */ #define DC_FB_CONFIG __REG32(DC_BASE + 0x000) @@ -51,9 +47,9 @@ struct vga_struct { - long pclk; - int hr,hss,hse,hfl; - int vr,vss,vse,vfl; + long pclk; + int hr,hss,hse,hfl; + int vr,vss,vse,vfl; }; #endif diff --git a/bsp/ls1cdev/drivers/drv_can.c b/bsp/ls1cdev/drivers/drv_can.c index 92095d0669..66388b4ea8 100644 --- a/bsp/ls1cdev/drivers/drv_can.c +++ b/bsp/ls1cdev/drivers/drv_can.c @@ -1,21 +1,7 @@ /* - * File : drv_can.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -119,6 +105,7 @@ static rt_err_t setfilter(struct ls1c_bxcan *pbxcan, struct rt_can_filter_config } return RT_EOK; } + static void bxcan0_filter_init(struct rt_can_device *can) { struct ls1c_bxcan *pbxcan; @@ -242,14 +229,13 @@ static void bxcan0_hw_init(void) #ifdef USING_BXCAN1 static void bxcan1_hw_init(void) { - pin_set_purpose(56, PIN_PURPOSE_GPIO); - pin_set_purpose(57, PIN_PURPOSE_GPIO); - pin_set_remap(56, PIN_REMAP_DEFAULT); - pin_set_remap(57, PIN_REMAP_DEFAULT); + pin_set_purpose(56, PIN_PURPOSE_OTHER); + pin_set_purpose(57, PIN_PURPOSE_OTHER); + pin_set_remap(56, PIN_REMAP_THIRD); + pin_set_remap(57, PIN_REMAP_THIRD); } #endif - static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg) { CAN_TypeDef *pbxcan; @@ -444,7 +430,7 @@ void ls1c_can0_irqhandler(int irq, void *param) rt_kprintf("\r\nCan0 int TX happened!\r\n"); } /*数据溢出中断*/ - else if (( status & CAN_IR_TI) == CAN_IR_DOI) + else if (( status & CAN_IR_DOI) == CAN_IR_DOI) { rt_hw_can_isr(&bxcan0, RT_CAN_EVENT_RXOF_IND); rt_kprintf("\r\nCan0 int RX OF happened!\r\n"); @@ -484,7 +470,7 @@ void ls1c_can1_irqhandler(int irq, void *param) rt_kprintf("\r\nCan1 int TX happened!\r\n"); } /*数据溢出中断*/ - else if (( status & CAN_IR_TI) == CAN_IR_DOI) + else if (( status & CAN_IR_DOI) == CAN_IR_DOI) { rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RXOF_IND); rt_kprintf("\r\nCan1 int RX OF happened!\r\n"); @@ -515,7 +501,7 @@ int ls1c_bxcan_init(void) rt_kprintf("\r\ncan0 register! \r\n"); rt_hw_interrupt_install(LS1C_CAN0_IRQ,( rt_isr_handler_t)bxcan0data.irq , RT_NULL, "can0"); - rt_hw_interrupt_umask(LS1C_CAN0_IRQ); + rt_hw_interrupt_umask(LS1C_CAN0_IRQ); #endif #ifdef USING_BXCAN1 bxcan1.config.baud_rate = CAN250kBaud; diff --git a/bsp/ls1cdev/drivers/drv_can.h b/bsp/ls1cdev/drivers/drv_can.h index 0d846dee25..7d5d1604d4 100644 --- a/bsp/ls1cdev/drivers/drv_can.h +++ b/bsp/ls1cdev/drivers/drv_can.h @@ -1,21 +1,7 @@ /* - * File : bxcan.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/drv_gpio.c b/bsp/ls1cdev/drivers/drv_gpio.c index 3a21b54a33..2203db7775 100644 --- a/bsp/ls1cdev/drivers/drv_gpio.c +++ b/bsp/ls1cdev/drivers/drv_gpio.c @@ -1,21 +1,7 @@ /* - * File : drv_gpio.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -87,8 +73,24 @@ rt_err_t ls1c_pin_attach_irq(struct rt_device *device, rt_int32_t pin, { unsigned int gpio = pin; char irq_name[10]; - - gpio_set_irq_type(gpio, mode); + rt_uint32_t type; + switch (mode) + { + case PIN_IRQ_MODE_RISING: + type=IRQ_TYPE_EDGE_RISING; + break; + case PIN_IRQ_MODE_FALLING: + type=IRQ_TYPE_EDGE_FALLING; + break; + case PIN_IRQ_MODE_HIGH_LEVEL: + type=IRQ_TYPE_LEVEL_HIGH; + break; + case PIN_IRQ_MODE_LOW_LEVEL: + type=IRQ_TYPE_LEVEL_LOW; + break; + } + gpio_set_irq_type(gpio, type); + rt_sprintf(irq_name, "PIN_%d", gpio); rt_hw_interrupt_install(LS1C_GPIO_TO_IRQ(gpio), (rt_isr_handler_t)hdr, args, irq_name); diff --git a/bsp/ls1cdev/drivers/drv_gpio.h b/bsp/ls1cdev/drivers/drv_gpio.h index ab67d9f32a..33fc0f18ed 100644 --- a/bsp/ls1cdev/drivers/drv_gpio.h +++ b/bsp/ls1cdev/drivers/drv_gpio.h @@ -1,21 +1,7 @@ /* - * File : drv_gpio.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/drv_i2c.c b/bsp/ls1cdev/drivers/drv_i2c.c index 998f0f6ec3..374b625c70 100644 --- a/bsp/ls1cdev/drivers/drv_i2c.c +++ b/bsp/ls1cdev/drivers/drv_i2c.c @@ -1,25 +1,11 @@ /* - * File : drv_i2c.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2017-11-14 ��Ϊ�� first version + * 2017-11-14 勤为本 first version */ #include <rtthread.h> @@ -30,8 +16,8 @@ #include "../libraries/ls1c_delay.h" -#define LS1C_I2C_SCL (57) // gpio57 -#define LS1C_I2C_SDA (56) // gpio56 +#define LS1C_I2C_SCL (51) // gpio57 +#define LS1C_I2C_SDA (50) // gpio56 #define LS1C_SET_GPIO_MODE @@ -104,8 +90,8 @@ static const struct rt_i2c_bit_ops bit_ops = { .udelay = ls1c_udelay, - .delay_us = 20, // ��ֵΪ����(us) - .timeout = 10, // ��λΪtick + .delay_us = 20, // 此值为周期(us) + .timeout = 10, // 单位为tick }; @@ -118,7 +104,7 @@ int ls1c_i2c_init(void) ls1c_i2c_gpio_init(); - rt_i2c_bit_add_bus(&bus, "i2c2"); + rt_i2c_bit_add_bus(&bus, "i2c3"); return RT_EOK; } diff --git a/bsp/ls1cdev/drivers/drv_i2c.h b/bsp/ls1cdev/drivers/drv_i2c.h index 6d07039c2e..cbacdbb0f5 100644 --- a/bsp/ls1cdev/drivers/drv_i2c.h +++ b/bsp/ls1cdev/drivers/drv_i2c.h @@ -1,34 +1,17 @@ /* - * File : drv_i2c.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2017-11-14 ��Ϊ�� first version + * 2017-11-14 勤为本 first version */ #ifndef LS1C_DRV_I2C_H #define LS1C_DRV_I2C_H - int ls1c_i2c_init(void); - - #endif diff --git a/bsp/ls1cdev/drivers/drv_pwm.c b/bsp/ls1cdev/drivers/drv_pwm.c new file mode 100644 index 0000000000..779a7e52b9 --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_pwm.c @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-12-04 Sundm75 the first version + */ +#include <stdint.h> +#include <string.h> +#include <stdlib.h> + +#include <rtthread.h> +#include <rtdevice.h> + +#include "ls1c.h" +#include "../libraries/ls1c_public.h" +#include "../libraries/ls1c_regs.h" +#include "../libraries/ls1c_clock.h" +#include "../libraries/ls1c_pwm.h" +#include "../libraries/ls1c_pin.h" + +#define PWM_CHANNEL_MAX (4) /* 0-3*/ + +#ifdef RT_USING_PWM + +struct rt_ls1c_pwm +{ + struct rt_device_pwm parent; + + rt_uint32_t period[PWM_CHANNEL_MAX]; + rt_uint32_t pulse[PWM_CHANNEL_MAX]; +}; + +static struct rt_ls1c_pwm _ls1c_pwm_device; + +static rt_err_t set(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration) +{ + rt_err_t result = RT_EOK; + struct rt_ls1c_pwm *ls1c_pwm_device = (struct rt_ls1c_pwm *)device; + + if (configuration->channel > (PWM_CHANNEL_MAX - 1)) + { + result = -RT_EIO; + goto _exit; + } + + rt_kprintf("drv_pwm.c set channel %d: period: %d, pulse: %d\n", configuration->channel, configuration->period, configuration->pulse); + + ls1c_pwm_device->period[configuration->channel] = configuration->period; + ls1c_pwm_device->pulse[configuration->channel] = configuration->pulse; + +_exit: + return result; +} + +static rt_err_t get(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration) +{ + rt_err_t result = RT_EOK; + struct rt_ls1c_pwm *ls1c_pwm_device = (struct rt_ls1c_pwm *)device; + + if (configuration->channel > (PWM_CHANNEL_MAX - 1)) + { + result = -RT_EIO; + goto _exit; + } + + configuration->period = ls1c_pwm_device->period[configuration->channel]; + configuration->pulse = ls1c_pwm_device->pulse[configuration->channel]; + rt_kprintf("drv_pwm.c get channel %d: period: %d, pulse: %d\n", configuration->channel, configuration->period, configuration->pulse); + +_exit: + return result; +} + +static rt_err_t control(struct rt_device_pwm *device, int cmd, void *arg) +{ + rt_err_t result = RT_EOK; + struct rt_pwm_configuration * configuration = (struct rt_pwm_configuration *)arg; + + rt_kprintf("drv_pwm.c control cmd: %d. \n", cmd); + + if (cmd == PWM_CMD_ENABLE) + { + rt_kprintf("PWM_CMD_ENABLE\n"); + + pwm_info_t pwm_info; + switch ( configuration->channel) + { + case 0: + pwm_info.gpio = LS1C_PWM0_GPIO06; + //pwm_info.gpio = LS1C_PWM0_GPIO04; + break; + case 1: + pwm_info.gpio = LS1C_PWM1_GPIO92; + //pwm_info.gpio = LS1C_PWM1_GPIO05; + break; + case 2: + pwm_info.gpio = LS1C_PWM2_GPIO52; + //pwm_info.gpio = LS1C_PWM2_GPIO46; + break; + case 3: + pwm_info.gpio = LS1C_PWM3_GPIO47; + //pwm_info.gpio = LS1C_PWM3_GPIO53; + break; + default: + break; + } + pwm_info.mode = PWM_MODE_NORMAL; + pwm_info.duty = ( (float)configuration->pulse ) / ((float)configuration->period ); + pwm_info.period_ns = configuration->period; + pwm_init(&pwm_info); + pwm_enable(&pwm_info); + } + else if (cmd == PWM_CMD_DISABLE) + { + rt_kprintf("PWM_CMD_DISABLE\n"); + pwm_info_t pwm_info; + switch ( configuration->channel) + { + case 0: + pwm_info.gpio = LS1C_PWM0_GPIO06; + //pwm_info.gpio = LS1C_PWM0_GPIO04; + break; + case 1: + pwm_info.gpio = LS1C_PWM1_GPIO92; + //pwm_info.gpio = LS1C_PWM1_GPIO05; + break; + case 2: + pwm_info.gpio = LS1C_PWM2_GPIO52; + //pwm_info.gpio = LS1C_PWM2_GPIO46; + break; + case 3: + pwm_info.gpio = LS1C_PWM3_GPIO47; + //pwm_info.gpio = LS1C_PWM3_GPIO53; + break; + default: + break; + } + pwm_info.mode = PWM_MODE_NORMAL; + pwm_info.duty = ( (float)configuration->pulse ) / ((float)configuration->period ); + pwm_info.period_ns = configuration->period; + pwm_init(&pwm_info); + pwm_disable(&pwm_info); + } + else if (cmd == PWM_CMD_SET) + { + rt_kprintf("PWM_CMD_SET\n"); + result = set(device, (struct rt_pwm_configuration *)arg); + } + else if (cmd == PWM_CMD_GET) + { + rt_kprintf("PWM_CMD_GET\n"); + result = get(device, (struct rt_pwm_configuration *)arg); + } + + return result; +} + +static const struct rt_pwm_ops pwm_ops = +{ + control, +}; + +int rt_hw_pwm_init(void) +{ + int ret = RT_EOK; + + /* add pwm initial. */ + + ret = rt_device_pwm_register(&_ls1c_pwm_device.parent, "pwm", &pwm_ops, RT_NULL); + + return ret; +} +INIT_DEVICE_EXPORT(rt_hw_pwm_init); + +#endif /*RT_USING_PWM*/ diff --git a/bsp/ls1cdev/drivers/drv_pwm.h b/bsp/ls1cdev/drivers/drv_pwm.h new file mode 100644 index 0000000000..427ba0c619 --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_pwm.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-12-04 Sundm75 the first version + */ + +#ifndef LS1C_DRV_PWM_H +#define LS1C_DRV_PWM_H + +int ls1c_pwm_init(void); + +#endif /*DRV_CAN_H_*/ diff --git a/bsp/ls1cdev/drivers/drv_rtc.c b/bsp/ls1cdev/drivers/drv_rtc.c new file mode 100644 index 0000000000..0569992c61 --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_rtc.c @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-05-05 sundm75 first version + */ + +/* Includes ------------------------------------------------------------------*/ +#include "board.h" +#include "drv_rtc.h" +#include <rtdevice.h> + +#include "../libraries/ls1c_regs.h" +#include "../libraries/ls1c_rtc.h" + +//#define RT_RTC_DEBUG + +#if defined(RT_USING_RTC) +#ifdef RT_RTC_DEBUG +#define rtc_debug(format,args...) rt_kprintf(format, ##args) +#else +#define rtc_debug(format,args...) +#endif + +static struct rt_device rtc; + +RTC_TypeDef * RTC_Handler; + +static time_t get_timestamp(void) +{ + struct tm tm_new = {0}; + RTC_TimeTypeDef rtcDate; + + RTC_GetTime(RTC_Handler, &rtcDate); + + tm_new.tm_sec = rtcDate.Seconds; + tm_new.tm_min = rtcDate.Minutes; + tm_new.tm_hour = rtcDate.Hours; + + tm_new.tm_mday = rtcDate.Date; + tm_new.tm_mon = rtcDate.Month- 1; + tm_new.tm_year = rtcDate.Year + 2000 - 1900; + + return mktime(&tm_new); +} + +static int set_timestamp(time_t timestamp) +{ + struct tm *p_tm; + RTC_TimeTypeDef rtcDate; + + p_tm = localtime(×tamp); + + rtcDate.Seconds= p_tm->tm_sec ; + rtcDate.Minutes= p_tm->tm_min ; + rtcDate.Hours= p_tm->tm_hour; + + rtcDate.Date= p_tm->tm_mday; + rtcDate.Month= p_tm->tm_mon + 1; + rtcDate.Year= p_tm->tm_year + 1900 - 2000; + + RTC_SetTime(RTC_Handler, &rtcDate); + rt_kprintf("\r\nrtcDate is %d.%d.%d - %d:%d:%d",rtcDate.Year, rtcDate.Month, rtcDate.Date, rtcDate.Hours, rtcDate.Minutes, rtcDate.Seconds); + return RT_EOK; +} + +rt_uint8_t RTC_Init(void) +{ + RTC_Handler = RTC; + return 0; +} + +static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag) +{ + if (dev->rx_indicate != RT_NULL) + { + /* Open Interrupt */ + } + + return RT_EOK; +} + +static rt_size_t rt_rtc_read( + rt_device_t dev, + rt_off_t pos, + void* buffer, + rt_size_t size) +{ + + return 0; +} + + /** + * This function configure RTC device. + * + * @param dev, pointer to device descriptor. + * @param cmd, RTC control command. + * + * @return the error code. + */ +static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args) +{ + rt_err_t result; + RT_ASSERT(dev != RT_NULL); + switch (cmd) + { + case RT_DEVICE_CTRL_RTC_GET_TIME: + + *(rt_uint32_t *)args = get_timestamp(); + rtc_debug("RTC: get rtc_time %x\n", *(rt_uint32_t *)args); + break; + + case RT_DEVICE_CTRL_RTC_SET_TIME: + { + result = set_timestamp(*(rt_uint32_t *)args); + rtc_debug("RTC: set rtc_time %x\n", *(rt_uint32_t *)args); + } + break; + } + + return result; +} + + /** + * This function register RTC device. + * + * @param device, pointer to device descriptor. + * @param name, device name. + * @param flag, configuration flags. + * + * @return the error code. + */ +rt_err_t rt_hw_rtc_register( + rt_device_t device, + const char *name, + rt_uint32_t flag) +{ + RT_ASSERT(device != RT_NULL); + + device->type = RT_Device_Class_RTC; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + device->init = RT_NULL; + device->open = rt_rtc_open; + device->close = RT_NULL; + device->read = rt_rtc_read; + device->write = RT_NULL; + device->control = rt_rtc_control; + device->user_data = RT_NULL; /* no private */ + + /* register a character device */ + return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag); +} + + /** + * This function initialize RTC module related hardware and register RTC device to kernel. + * + * @param none. + * + * @return the error code. + */ +int rt_hw_rtc_init(void) +{ + RTC_Init(); + /* register rtc device */ + rt_hw_rtc_register(&rtc, RT_RTC_NAME, 0); + return RT_EOK; +} +INIT_BOARD_EXPORT(rt_hw_rtc_init); +#endif + + + + diff --git a/bsp/ls1cdev/drivers/drv_rtc.h b/bsp/ls1cdev/drivers/drv_rtc.h new file mode 100644 index 0000000000..cea8ebe633 --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_rtc.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-05-05 sundm75 first version + */ + +#ifndef __DRV_RTC_H__ +#define __DRV_RTC_H__ + +#include <rtthread.h> +#include <rtdevice.h> + +int rt_hw_rtc_init(void); + +#endif diff --git a/bsp/ls1cdev/drivers/drv_spi.c b/bsp/ls1cdev/drivers/drv_spi.c index 4b81d55d71..3cc2212850 100644 --- a/bsp/ls1cdev/drivers/drv_spi.c +++ b/bsp/ls1cdev/drivers/drv_spi.c @@ -25,13 +25,14 @@ #include <rtthread.h> #include <drivers/spi.h> +#include <rthw.h> #include "drv_spi.h" +#include "ls1c_public.h" +#include "spi_msd.h" +#include "dfs_fs.h" #include "../libraries/ls1c_pin.h" #ifdef RT_USING_SPI - -//#define DEBUG - #ifdef DEBUG #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__) #else @@ -265,20 +266,24 @@ int ls1c_hw_spi_init(void) #ifdef RT_USING_SPI0 /* attach cs */ { - static struct rt_spi_device spi_device0; - static struct rt_spi_device spi_device1; - static struct rt_spi_device spi_device2; - static struct ls1c_spi_cs spi_cs0; - static struct ls1c_spi_cs spi_cs1; - static struct ls1c_spi_cs spi_cs2; - - /* spi02: CS2 SD Card*/ - spi_cs2.cs = LS1C_SPI_CS_2; - rt_spi_bus_attach_device(&spi_device2, "spi02", "spi0", (void *)&spi_cs2); - spi_cs1.cs = LS1C_SPI_CS_1; - rt_spi_bus_attach_device(&spi_device1, "spi01", "spi0", (void *)&spi_cs1); - spi_cs0.cs = LS1C_SPI_CS_0; - rt_spi_bus_attach_device(&spi_device0, "spi00", "spi0", (void *)&spi_cs0); + static struct rt_spi_device spi_device0; + static struct rt_spi_device spi_device1; + static struct rt_spi_device spi_device2; + static struct rt_spi_device spi_device3; + static struct ls1c_spi_cs spi_cs0; + static struct ls1c_spi_cs spi_cs1; + static struct ls1c_spi_cs spi_cs2; + static struct ls1c_spi_cs spi_cs3; + /* spi02: CS2 SD Card*/ + spi_cs2.cs = LS1C_SPI_CS_2; + rt_spi_bus_attach_device(&spi_device2, "spi02", "spi0", (void *)&spi_cs2); + spi_cs1.cs = LS1C_SPI_CS_1; + rt_spi_bus_attach_device(&spi_device1, "spi01", "spi0", (void *)&spi_cs1); + spi_cs0.cs = LS1C_SPI_CS_0; + rt_spi_bus_attach_device(&spi_device0, "spi00", "spi0", (void *)&spi_cs0); + spi_cs3.cs = LS1C_SPI_CS_3; + rt_spi_bus_attach_device(&spi_device3, "spi03", "spi0", (void*)&spi_cs3); + msd_init("sd0", "spi02"); } #endif #ifdef RT_USING_SPI1 @@ -296,5 +301,21 @@ int ls1c_hw_spi_init(void) INIT_BOARD_EXPORT(ls1c_hw_spi_init); +static int board_sd_init(void) +{ + #if defined(RT_USING_DFS) && defined(RT_USING_DFS_ELMFAT) + /* mount sd card fat partition 1 as root directory */ + if( dfs_mount("sd0", "/", "elm", 0, 0) == 0) + { + rt_kprintf("File System initialized!\n"); + } + else + { + rt_kprintf("File System initialzation failed!\n"); + } + #endif /* RT_USING_DFS && RT_USING_DFS_ELMFAT */ +} +INIT_APP_EXPORT(board_sd_init); + #endif diff --git a/bsp/ls1cdev/drivers/drv_spi.h b/bsp/ls1cdev/drivers/drv_spi.h index 94fbb58097..8b1aa315be 100644 --- a/bsp/ls1cdev/drivers/drv_spi.h +++ b/bsp/ls1cdev/drivers/drv_spi.h @@ -1,25 +1,11 @@ /* - * File : drv_spi.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2017-11-02 ��Ϊ�� first version + * 2017-11-02 勤为本 first version */ #ifndef LS1C_DRV_SPI_H @@ -43,9 +29,9 @@ struct ls1c_spi_cs /* - * ��ʼ����ע����о1c��spi���� - * @SPI SPI���ߣ�����LS1C_SPI_0�� LS1C_SPI_1 - * @spi_bus_name �������� + * 初始化并注册龙芯1c的spi总线 + * @SPI SPI总线,比如LS1C_SPI_0, LS1C_SPI_1 + * @spi_bus_name 总线名字 * @ret */ rt_err_t ls1c_spi_bus_register(rt_uint8_t SPI, const char *spi_bus_name); diff --git a/bsp/ls1cdev/drivers/drv_touch.c b/bsp/ls1cdev/drivers/drv_touch.c new file mode 100644 index 0000000000..092260aaed --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_touch.c @@ -0,0 +1,270 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-02-08 Zhangyihong the first version + * 2018-10-29 XY + * 2019-04-11 sundm75 modify for ls1c300 & RTGUI + */ + +#include "drv_touch.h" + +#define TOUCH_I2C_NAME "i2c1" + +#ifndef TOUCH_SAMPLE_HZ +#define TOUCH_SAMPLE_HZ (50) +#endif + +#ifndef TOUCH_I2C_NAME +#error "Please define touch i2c name!" +#endif + +#ifdef TINA_USING_TOUCH +#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI) +#include <rtgui/event.h> +#include <rtgui/rtgui_server.h> +#endif + +#if 0 +#define TPDEBUG rt_kprintf +#else +#define TPDEBUG(...) +#endif + +static rt_slist_t _driver_list; +static struct rt_i2c_bus_device *i2c_bus = RT_NULL; + +static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id) +{ +#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI) + rt_err_t result; + struct rtgui_event_mouse emouse; + + emouse.parent.sender = RT_NULL; + emouse.wid = RT_NULL; + + emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; + emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN; + emouse.x = x; + emouse.y = y; +#ifdef PKG_USING_GUIENGINE + emouse.ts = rt_tick_get(); + emouse.id = id; +#endif + +#ifdef PKG_USING_GUIENGINE + do + { + result = rtgui_server_post_event(&emouse.parent, sizeof(emouse)); + if (result != RT_EOK) + { + rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ); + } + } + while (result != RT_EOK); +#else + rtgui_server_post_event(&emouse.parent, sizeof(emouse)); +#endif + + TPDEBUG("[TP] touch down [%d, %d]\n", emouse.x, emouse.y); +#endif +} + +static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id) +{ +#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI) + struct rtgui_event_mouse emouse; + + emouse.parent.sender = RT_NULL; + emouse.wid = RT_NULL; + + emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN; + emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION; + emouse.x = x; + emouse.y = y; +#ifdef PKG_USING_GUIENGINE + emouse.ts = rt_tick_get(); + emouse.id = id; +#endif + rtgui_server_post_event(&emouse.parent, sizeof(emouse)); + TPDEBUG("[TP] touch motion [%d, %d]\n", emouse.x, emouse.y); +#endif +} + +static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id) +{ +#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI) + rt_err_t result; + struct rtgui_event_mouse emouse; + + emouse.parent.sender = RT_NULL; + emouse.wid = RT_NULL; + + emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; + emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP; + emouse.x = x; + emouse.y = y; +#ifdef PKG_USING_GUIENGINE + emouse.ts = rt_tick_get(); + emouse.id = id; +#endif + +#ifdef PKG_USING_GUIENGINE + do + { + result = rtgui_server_post_event(&emouse.parent, sizeof(emouse)); + if (result != RT_EOK) + { + rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ); + } + } + while (result != RT_EOK); +#else + rtgui_server_post_event(&emouse.parent, sizeof(emouse)); +#endif + + TPDEBUG("[TP] touch up [%d, %d]\n", emouse.x, emouse.y); +#endif +} + +static void touch_run(void* parameter) +{ + rt_tick_t emouse_id = 0; + struct touch_message msg; + rt_slist_t *driver_list = NULL; + struct touch_driver *current_driver = RT_NULL; + + i2c_bus = rt_i2c_bus_device_find(TOUCH_I2C_NAME); + RT_ASSERT(i2c_bus); + + if(rt_device_open(&i2c_bus->parent, RT_DEVICE_OFLAG_RDWR) != RT_EOK) + { + return; + } + + rt_slist_for_each(driver_list, &_driver_list) + { + current_driver = (struct touch_driver *)driver_list; + if(current_driver->probe(i2c_bus) == RT_TRUE) + { + break; + } + current_driver = RT_NULL; + } + + if(current_driver == RT_NULL) + { + rt_kprintf("[TP] No touch pad or driver.\n"); + rt_device_close((rt_device_t)i2c_bus); + + return; + } + + current_driver->ops->init(i2c_bus); + + while (1) + { + if (rt_sem_take(current_driver->isr_sem, RT_WAITING_FOREVER) != RT_EOK) + { + continue; + } + + if (current_driver->ops->read_point(&msg) != RT_EOK) + { + continue; + } + + switch (msg.event) + { + case TOUCH_EVENT_MOVE: + post_motion_event(msg.x, msg.y, emouse_id); + break; + + case TOUCH_EVENT_DOWN: + emouse_id = rt_tick_get(); + post_down_event(msg.x, msg.y, emouse_id); + break; + + case TOUCH_EVENT_UP: + post_up_event(msg.x, msg.y, emouse_id); + break; + + default: + break; + } + + rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ); + } +} + +rt_err_t rt_touch_drivers_register(touch_driver_t drv) +{ + RT_ASSERT(drv != RT_NULL); + RT_ASSERT(drv->ops != RT_NULL); + RT_ASSERT(drv->probe != RT_NULL); + + rt_slist_append(&_driver_list, &drv->list); + + return RT_EOK; +} + +static int rt_touch_list_init(void) +{ + rt_slist_init(&_driver_list); + + return RT_EOK; +} +INIT_BOARD_EXPORT(rt_touch_list_init); + +static int rt_touch_init(void) +{ + rt_thread_t thread = RT_NULL; + + thread = rt_thread_create("touch", touch_run, RT_NULL, 2048, 28, 20); + if(thread) + { + return rt_thread_startup(thread); + } + + return RT_ERROR; +} +INIT_APP_EXPORT(rt_touch_init); + +int rt_touch_read(rt_uint16_t addr, void *cmd_buf, size_t cmd_len, void *data_buf, size_t data_len) +{ + struct rt_i2c_msg msgs[2]; + + msgs[0].addr = addr; + msgs[0].flags = RT_I2C_WR; + msgs[0].buf = cmd_buf; + msgs[0].len = cmd_len; + + msgs[1].addr = addr; + msgs[1].flags = RT_I2C_RD; + msgs[1].buf = data_buf; + msgs[1].len = data_len; + + if (rt_i2c_transfer(i2c_bus, msgs, 2) == 2) + return 0; + else + return -1; +} + +int rt_touch_write(rt_uint16_t addr, void *data_buf, size_t data_len) +{ + struct rt_i2c_msg msgs[1]; + + msgs[0].addr = addr; + msgs[0].flags = RT_I2C_WR; + msgs[0].buf = data_buf; + msgs[0].len = data_len; + + if (rt_i2c_transfer(i2c_bus, msgs, 1) == 1) + return 0; + else + return -1; +} +#endif diff --git a/bsp/ls1cdev/drivers/drv_touch.h b/bsp/ls1cdev/drivers/drv_touch.h new file mode 100644 index 0000000000..1ae4c71549 --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_touch.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-02-08 Zhangyihong the first version + * 2018-10-29 XY + */ + +#ifndef __DRV_TOUCH_H__ +#define __DRV_TOUCH_H__ + +#include "rtthread.h" +#include "rtdevice.h" + +#define TOUCH_EVENT_UP (0x01) +#define TOUCH_EVENT_DOWN (0x02) +#define TOUCH_EVENT_MOVE (0x03) +#define TOUCH_EVENT_NONE (0x80) + +struct touch_message +{ + rt_uint16_t x; + rt_uint16_t y; + rt_uint8_t event; +}; +typedef struct touch_message *touch_message_t; + +struct touch_ops +{ + void (*init)(struct rt_i2c_bus_device *); + void (*deinit)(void); + rt_err_t (*read_point)(touch_message_t); +}; +typedef struct touch_ops *touch_ops_t; + +struct touch_driver +{ + rt_slist_t list; + rt_bool_t (*probe)(struct rt_i2c_bus_device *i2c_bus); + rt_sem_t isr_sem; + touch_ops_t ops; + void *user_data; +}; +typedef struct touch_driver *touch_driver_t; + +rt_err_t rt_touch_drivers_register(touch_driver_t drv); + +int rt_touch_read(rt_uint16_t addr, void *cmd_buf, size_t cmd_len, void *data_buf, size_t data_len); +int rt_touch_write(rt_uint16_t addr, void *data_buf, size_t data_len); + +#endif diff --git a/bsp/ls1cdev/drivers/drv_touch_gt9xx.c b/bsp/ls1cdev/drivers/drv_touch_gt9xx.c new file mode 100644 index 0000000000..801ec762cb --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_touch_gt9xx.c @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-02-08 Zhangyihong the first version + * 2018-04-03 XY gt9xx for 1024 * 600 + * 2018-04-14 liu2guang optimize int and rst to pin framework + * 2017-08-08 XY imxrt1052 + * 2018-10-29 XY + */ + +#include "drv_touch.h" +#include "string.h" + +#ifdef TINA_USING_TOUCH + +#define TP_INT_PIN 89 +#define TP_RST_PIN 87 + +#ifndef TP_INT_PIN +#error "Please config touch panel INT pin." +#endif +#ifndef TP_RST_PIN +#error "Please config touch panel RST pin." +#endif + +#ifndef IIC_RETRY_NUM +#define IIC_RETRY_NUM 2 +#endif + +#define GT9xx_TS_ADDR (0x5D) + +#define gt9xx_READ_XY_REG (0x814E) /* 坐标寄存器 */ +#define gt9xx_CLEARBUF_REG (0x814E) /* 清除坐标寄存器 */ +#define gt9xx_CONFIG_REG (0x8047) /* 配置参数寄存器 */ +#define gt9xx_COMMAND_REG (0x8040) /* 实时命令 */ +#define gt9xx_PRODUCT_ID_REG (0x8140) /* 产品ID */ +#define gt9xx_VENDOR_ID_REG (0x814A) /* 当前模组选项信息 */ +#define gt9xx_CONFIG_VERSION_REG (0x8047) /* 配置文件版本号 */ +#define gt9xx_CONFIG_CHECKSUM_REG (0x80FF) /* 配置文件校验码 */ +#define gt9xx_FIRMWARE_VERSION_REG (0x8144) /* 固件版本号 */ + +#if 0 +#define TPDEBUG rt_kprintf +#else +#define TPDEBUG(...) +#endif + +static struct touch_driver gt9xx_driver; + +void gt9xx_hw_reset(rt_uint8_t address) +{ + rt_tick_t delay = rt_tick_from_millisecond(30); + + rt_pin_mode(TP_RST_PIN, PIN_MODE_OUTPUT); + rt_pin_mode(TP_INT_PIN, PIN_MODE_OUTPUT); + + if (address == 0x5D) + { + rt_pin_write(TP_RST_PIN, PIN_LOW); + rt_pin_write(TP_INT_PIN, PIN_LOW); + rt_thread_delay(delay); + rt_pin_write(TP_RST_PIN, PIN_HIGH); + rt_pin_write(TP_INT_PIN, PIN_LOW); + rt_thread_delay(delay); + rt_pin_write(TP_INT_PIN, PIN_LOW); + rt_thread_delay(delay); + rt_pin_write(TP_INT_PIN, PIN_HIGH); + } + else + { + rt_pin_write(TP_RST_PIN, PIN_LOW); + rt_pin_write(TP_INT_PIN, PIN_HIGH); + rt_thread_delay(delay); + rt_pin_write(TP_RST_PIN, PIN_HIGH); + rt_pin_write(TP_INT_PIN, PIN_HIGH); + rt_thread_delay(delay); + rt_pin_write(TP_INT_PIN, PIN_LOW); + rt_thread_delay(delay); + rt_pin_write(TP_INT_PIN, PIN_HIGH); + } +} + +static void gt9xx_soft_reset(struct rt_i2c_bus_device *i2c_bus) +{ + rt_uint8_t buf[3]; + + buf[0] = (rt_uint8_t)((gt9xx_COMMAND_REG >> 8) & 0xFF); + buf[1] = (rt_uint8_t)(gt9xx_COMMAND_REG & 0xFF); + buf[2] = 0x02; + rt_touch_write(GT9xx_TS_ADDR, buf, 3); +} + +static rt_bool_t gt9xx_probe(struct rt_i2c_bus_device *i2c_bus) +{ + rt_uint8_t cmd[2]; + rt_uint8_t buffer[5] = {0}; + + gt9xx_hw_reset(GT9xx_TS_ADDR); + //gt9xx_soft_reset(i2c_bus); + rt_thread_delay(RT_TICK_PER_SECOND / 5); + + cmd[0] = (rt_uint8_t)((gt9xx_PRODUCT_ID_REG >> 8) & 0xFF); + cmd[1] = (rt_uint8_t)(gt9xx_PRODUCT_ID_REG & 0xFF); + if (rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buffer, 4) != 0) + { + TPDEBUG("[TP] %s failed!\n", __func__); + return RT_FALSE; + } + + buffer[4] = '\0'; + + TPDEBUG("%#X %#X %#X %#X %#X\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]); + + if(!rt_strcmp((const char*)buffer, "911")) + { + rt_kprintf("[TP] Found chip gt911\n"); + return RT_TRUE; + } + else if(!rt_strcmp((const char*)buffer, "9147")) + { + rt_kprintf("[TP] Found chip gt9147\n"); + return RT_TRUE; + } + else if(!rt_strcmp((const char*)buffer, "9157")) + { + rt_kprintf("[TP] Found chip gt9157\n"); + return RT_TRUE; + } + else + { + rt_kprintf("[TP] Uknow chip gt9xx device: [%s]\n", buffer); + } + + return RT_FALSE; +} + +static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus) +{ + rt_uint8_t buf = 0; + rt_uint8_t cmd[2]; + + gt9xx_driver.isr_sem = rt_sem_create("gt9xx", 0, RT_IPC_FLAG_FIFO); + RT_ASSERT(gt9xx_driver.isr_sem); + + cmd[0] = (rt_uint8_t)((gt9xx_CONFIG_VERSION_REG >> 8) & 0xFF); + cmd[1] = (rt_uint8_t)(gt9xx_CONFIG_VERSION_REG & 0xFF); + rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1); + rt_kprintf("[TP] GT9xx Config version: 0x%02X\n", buf); + + cmd[0] = (rt_uint8_t)((gt9xx_VENDOR_ID_REG >> 8) & 0xFF); + cmd[1] = (rt_uint8_t)(gt9xx_VENDOR_ID_REG & 0xFF); + rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1); + rt_kprintf("[TP] GT9xx Sensor id: 0x%02X\n", buf); + + rt_sem_release(gt9xx_driver.isr_sem); +} + +static void gt9xx_deinit(void) +{ + rt_sem_delete(gt9xx_driver.isr_sem); +} + +static rt_err_t gt9xx_read_point(touch_message_t msg) +{ + rt_uint8_t cmd[2]; + rt_uint8_t buf[8] = {0}; + static rt_uint8_t s_tp_down = 0; + + cmd[0] = (rt_uint8_t)((gt9xx_READ_XY_REG >> 8) & 0xFF); + cmd[1] = (rt_uint8_t)(gt9xx_READ_XY_REG & 0xFF); + rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buf, 8); + + if((buf[0] & 0x01) == 0) + { + if(s_tp_down) + { + s_tp_down = 0; + msg->event = TOUCH_EVENT_UP; + } + else + { + msg->event = TOUCH_EVENT_NONE; + } + } + else + { + msg->x = ((rt_uint16_t)buf[3] << 8) | buf[2]; + msg->y = ((rt_uint16_t)buf[5] << 8) | buf[4]; + + if(s_tp_down) + { + msg->event = TOUCH_EVENT_MOVE; + } + else + { + msg->event = TOUCH_EVENT_DOWN; + s_tp_down = 1; + } + } + + buf[0] = ((gt9xx_CLEARBUF_REG >> 8) & 0xFF); + buf[1] = (gt9xx_CLEARBUF_REG & 0xFF); + buf[2] = 0x00; + rt_touch_write(GT9xx_TS_ADDR, buf, 3); + + rt_sem_release(gt9xx_driver.isr_sem); + + return RT_EOK; +} + +struct touch_ops gt9xx_ops = +{ + .init = gt9xx_init, + .deinit = gt9xx_deinit, + .read_point = gt9xx_read_point, +}; + +static int gt9xx_driver_register(void) +{ + gt9xx_driver.probe = gt9xx_probe; + gt9xx_driver.ops = >9xx_ops; + gt9xx_driver.user_data = RT_NULL; + + rt_touch_drivers_register(>9xx_driver); + + return RT_EOK; +} +INIT_ENV_EXPORT(gt9xx_driver_register); + +#endif diff --git a/bsp/ls1cdev/drivers/drv_uart.c b/bsp/ls1cdev/drivers/drv_uart.c index de0fe1f852..0578eea4fc 100644 --- a/bsp/ls1cdev/drivers/drv_uart.c +++ b/bsp/ls1cdev/drivers/drv_uart.c @@ -1,21 +1,7 @@ /* - * File : drv_uart.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -131,7 +117,7 @@ static void uart_irq_handler(int vector, void *param) } -static const struct rt_uart_ops stm32_uart_ops = +static const struct rt_uart_ops ls1c_uart_ops = { ls1c_uart_configure, ls1c_uart_control, @@ -146,8 +132,26 @@ struct rt_uart_ls1c uart2 = LS1C_UART2_IRQ, }; struct rt_serial_device serial2; +#endif /* RT_USING_UART2 */ + +#if defined(RT_USING_UART1) +struct rt_uart_ls1c uart1 = +{ + LS1C_UART1, + LS1C_UART1_IRQ, +}; +struct rt_serial_device serial1; #endif /* RT_USING_UART1 */ +#if defined(RT_USING_UART3) +struct rt_uart_ls1c uart3 = +{ + LS1C_UART3, + LS1C_UART3_IRQ, +}; +struct rt_serial_device serial3; +#endif /* RT_USING_UART3 */ + void rt_hw_uart_init(void) { struct rt_uart_ls1c *uart; @@ -156,7 +160,7 @@ void rt_hw_uart_init(void) #ifdef RT_USING_UART2 uart = &uart2; - serial2.ops = &stm32_uart_ops; + serial2.ops = &ls1c_uart_ops; serial2.config = config; pin_set_purpose(36, PIN_PURPOSE_OTHER); @@ -166,12 +170,55 @@ void rt_hw_uart_init(void) rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial2, "UART2"); - /* register UART1 device */ + /* register UART2 device */ rt_hw_serial_register(&serial2, "uart2", //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); +#endif /* RT_USING_UART2 */ + +#ifdef RT_USING_UART1 + uart = &uart1; + + serial1.ops = &ls1c_uart_ops; + serial1.config = config; + + pin_set_purpose(2, PIN_PURPOSE_OTHER); + pin_set_purpose(3, PIN_PURPOSE_OTHER); + pin_set_remap(2, PIN_REMAP_FOURTH); + pin_set_remap(3, PIN_REMAP_FOURTH); + + rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial1, "UART1"); + + /* register UART1 device */ + rt_hw_serial_register(&serial1, + "uart1", + //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + uart); #endif /* RT_USING_UART1 */ + +#ifdef RT_USING_UART3 + uart = &uart3; + + serial3.ops = &ls1c_uart_ops; + serial3.config = config; + + pin_set_purpose(0, PIN_PURPOSE_OTHER); + pin_set_purpose(1, PIN_PURPOSE_OTHER); + pin_set_remap(0, PIN_REMAP_FOURTH); + pin_set_remap(1, PIN_REMAP_FOURTH); + + rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial3, "UART3"); + + /* register UART1 device */ + rt_hw_serial_register(&serial3, + "uart3", + //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + uart); +#endif /* RT_USING_UART3 */ + } diff --git a/bsp/ls1cdev/drivers/drv_uart.h b/bsp/ls1cdev/drivers/drv_uart.h index 2c6553bffd..dcc90ea3f9 100644 --- a/bsp/ls1cdev/drivers/drv_uart.h +++ b/bsp/ls1cdev/drivers/drv_uart.h @@ -1,21 +1,7 @@ /* - * File : drv_uart.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -26,6 +12,7 @@ #define __DRV_UART_H__ #include "ls1c.h" +#include <rthw.h> #define DEV_CLK 252000000 // 252MHz #define UART_BAUDRATE 115200 @@ -45,43 +32,43 @@ #define UART11_BASE 0xBFE4Cb00 /* UART registers */ -#define UART_DAT(base) __REG8(base + 0x00) -#define UART_IER(base) __REG8(base + 0x01) -#define UART_IIR(base) __REG8(base + 0x02) -#define UART_FCR(base) __REG8(base + 0x02) -#define UART_LCR(base) __REG8(base + 0x03) -#define UART_MCR(base) __REG8(base + 0x04) -#define UART_LSR(base) __REG8(base + 0x05) -#define UART_MSR(base) __REG8(base + 0x06) +#define UART_DAT(base) HWREG8(base + 0x00) +#define UART_IER(base) HWREG8(base + 0x01) +#define UART_IIR(base) HWREG8(base + 0x02) +#define UART_FCR(base) HWREG8(base + 0x02) +#define UART_LCR(base) HWREG8(base + 0x03) +#define UART_MCR(base) HWREG8(base + 0x04) +#define UART_LSR(base) HWREG8(base + 0x05) +#define UART_MSR(base) HWREG8(base + 0x06) -#define UART_LSB(base) __REG8(base + 0x00) -#define UART_MSB(base) __REG8(base + 0x01) +#define UART_LSB(base) HWREG8(base + 0x00) +#define UART_MSB(base) HWREG8(base + 0x01) /* UART0 registers */ -#define UART0_DAT __REG8(UART0_BASE + 0x00) -#define UART0_IER __REG8(UART0_BASE + 0x01) -#define UART0_IIR __REG8(UART0_BASE + 0x02) -#define UART0_FCR __REG8(UART0_BASE + 0x02) -#define UART0_LCR __REG8(UART0_BASE + 0x03) -#define UART0_MCR __REG8(UART0_BASE + 0x04) -#define UART0_LSR __REG8(UART0_BASE + 0x05) -#define UART0_MSR __REG8(UART0_BASE + 0x06) +#define UART0_DAT HWREG8(UART0_BASE + 0x00) +#define UART0_IER HWREG8(UART0_BASE + 0x01) +#define UART0_IIR HWREG8(UART0_BASE + 0x02) +#define UART0_FCR HWREG8(UART0_BASE + 0x02) +#define UART0_LCR HWREG8(UART0_BASE + 0x03) +#define UART0_MCR HWREG8(UART0_BASE + 0x04) +#define UART0_LSR HWREG8(UART0_BASE + 0x05) +#define UART0_MSR HWREG8(UART0_BASE + 0x06) -#define UART0_LSB __REG8(UART0_BASE + 0x00) -#define UART0_MSB __REG8(UART0_BASE + 0x01) +#define UART0_LSB HWREG8(UART0_BASE + 0x00) +#define UART0_MSB HWREG8(UART0_BASE + 0x01) /* UART1 registers */ -#define UART1_DAT __REG8(UART1_BASE + 0x00) -#define UART1_IER __REG8(UART1_BASE + 0x01) -#define UART1_IIR __REG8(UART1_BASE + 0x02) -#define UART1_FCR __REG8(UART1_BASE + 0x02) -#define UART1_LCR __REG8(UART1_BASE + 0x03) -#define UART1_MCR __REG8(UART1_BASE + 0x04) -#define UART1_LSR __REG8(UART1_BASE + 0x05) -#define UART1_MSR __REG8(UART1_BASE + 0x06) +#define UART1_DAT HWREG8(UART1_BASE + 0x00) +#define UART1_IER HWREG8(UART1_BASE + 0x01) +#define UART1_IIR HWREG8(UART1_BASE + 0x02) +#define UART1_FCR HWREG8(UART1_BASE + 0x02) +#define UART1_LCR HWREG8(UART1_BASE + 0x03) +#define UART1_MCR HWREG8(UART1_BASE + 0x04) +#define UART1_LSR HWREG8(UART1_BASE + 0x05) +#define UART1_MSR HWREG8(UART1_BASE + 0x06) -#define UART1_LSB __REG8(UART1_BASE + 0x00) -#define UART1_MSB __REG8(UART1_BASE + 0x01) +#define UART1_LSB HWREG8(UART1_BASE + 0x00) +#define UART1_MSB HWREG8(UART1_BASE + 0x01) /* UART interrupt enable register value */ #define UARTIER_IME (1 << 3) diff --git a/bsp/ls1cdev/drivers/drv_wdt.c b/bsp/ls1cdev/drivers/drv_wdt.c new file mode 100644 index 0000000000..4a42220000 --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_wdt.c @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-06 sundm75 first version + */ + +#include <rtthread.h> +#include <rtdevice.h> + +#ifdef RT_USING_WDT + +#include <drivers/watchdog.h> +#include "drv_wdt.h" + +#include "ls1c_wdog.h" +#include "ls1c_clock.h" + +typedef enum +{ + RESTENABLE = 0x0, + INTERRUPTENABLE = 0x1, +}wdt_enable_mode; + +static rt_uint32_t heartbeat = 0; + +static rt_err_t wdt_stop(void) +{ + rt_err_t ret = RT_EOK; + + Wdog_Reset(); + ret = (rt_err_t) Wdog_Disable(); + if (ret != RT_EOK) + { + rt_kprintf("Wdog_Disable error!\n"); + return RT_ERROR; + } + return ret; +} + +static rt_err_t wdt_start(int mode) +{ + rt_err_t ret = RT_EOK; + wdt_enable_mode wdt_mode = RESTENABLE; + + ret = (rt_err_t) Wdog_Disable(); + if (ret != RT_EOK) + { + rt_kprintf("Wdog_Disable error!\n"); + return RT_ERROR; + } + + if((mode == RESTENABLE) || (mode == INTERRUPTENABLE)) + { + wdt_mode = mode; + } + Wdog_Enable(); + Wdog_Set(); + if (ret != RT_EOK) + { + rt_kprintf("Wdog_Enable error!\n"); + return RT_ERROR; + } + + return ret; +} + +static rt_err_t wdt_keepalive(void) +{ + rt_err_t ret = RT_EOK; + rt_uint32_t index = 0; + + index = heartbeat * clk_get_apb_rate(); + ret = (rt_err_t) Wdog_LoadValue(index); + Wdog_Set(); + if (ret != 0) + { + rt_kprintf("LS1C_Wdog_ClrTimeout error!\n"); + return RT_ERROR; + } + + return ret; +} + +static rt_uint32_t wdt_get_timeleft(void) +{ + rt_uint32_t cnt = 0; + rt_uint32_t second = 0; + + cnt = (rt_uint32_t) Wdog_GetValue(); + second = cnt/clk_get_apb_rate(); + + return second; +} + +static rt_err_t wdt_set_timeout(rt_uint32_t second) +{ + rt_err_t ret = RT_EOK; + rt_uint32_t index = 0; + + index = second * clk_get_apb_rate(); + ret = (rt_err_t) Wdog_LoadValue(index); + if (ret != RT_EOK) + { + rt_kprintf("Wdog_LoadValue error!\n"); + return RT_ERROR; + } + return ret; +} + +static rt_err_t watchdog_init(rt_watchdog_t *wdt) +{ + struct wdt_driver *wdt_drv = wdt->parent.user_data; + if (wdt_drv->in_use) return -RT_EBUSY; + + Wdog_Init(); + + return RT_EOK; +} + +static rt_err_t watchdog_ctrl(rt_watchdog_t *wdt, int cmd, void *arg) +{ + rt_uint32_t val; + int mode; + + switch (cmd) + { + case RT_DEVICE_CTRL_WDT_START: + mode = *((int *)(arg)); + wdt_start(mode); + break; + + case RT_DEVICE_CTRL_WDT_STOP: + Wdog_Disable(); + break; + + case RT_DEVICE_CTRL_WDT_KEEPALIVE: + wdt_keepalive(); + break; + + case RT_DEVICE_CTRL_WDT_SET_TIMEOUT: + heartbeat = *((rt_uint32_t *)(arg)); + wdt_set_timeout(heartbeat); + break; + + case RT_DEVICE_CTRL_WDT_GET_TIMEOUT: + arg = &heartbeat; + break; + + case RT_DEVICE_CTRL_WDT_GET_TIMELEFT: + val = (rt_uint32_t) wdt_get_timeleft(); + arg = &val; + break; + + default: + return -RT_EIO; + } + return RT_EOK; +} + +struct rt_watchdog_ops watchdog_ops = +{ + .init = &watchdog_init, + .control = &watchdog_ctrl, +}; + +int wdt_exit(void *priv_data) +{ + return 0; +} + +int rt_hw_wdt_init(void) +{ + rt_watchdog_t *wdt_dev; + struct wdt_driver *wdt_drv; + + wdt_drv = (struct wdt_driver *)rt_malloc(sizeof(struct wdt_driver)); + rt_memset(wdt_drv, 0, sizeof(struct wdt_driver)); + + wdt_dev = (rt_watchdog_t *)rt_malloc(sizeof(rt_watchdog_t)); + + if (wdt_dev == RT_NULL) + { + rt_kprintf("ERROR: %s rt_watchdog_t malloc failed\n", __func__); + } + + wdt_dev->ops = &watchdog_ops; + + rt_hw_watchdog_register(wdt_dev, "wdt", RT_DEVICE_OFLAG_RDWR, wdt_drv); + + return 0; +} + +INIT_BOARD_EXPORT(rt_hw_wdt_init); + +#endif diff --git a/bsp/ls1cdev/drivers/drv_wdt.h b/bsp/ls1cdev/drivers/drv_wdt.h new file mode 100644 index 0000000000..a017edb19a --- /dev/null +++ b/bsp/ls1cdev/drivers/drv_wdt.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-06 sundm75 first version + */ + +#ifndef WDT_H_ +#define WDT_H_ + +struct wdt_driver +{ + unsigned long in_use; + + void* priv; +}; + +int rt_hw_wdt_init(void); + +#endif /* WDT_H_ */ diff --git a/bsp/ls1cdev/drivers/hw_i2c.c b/bsp/ls1cdev/drivers/hw_i2c.c index 6d427701ed..1f63df1ab9 100644 --- a/bsp/ls1cdev/drivers/hw_i2c.c +++ b/bsp/ls1cdev/drivers/hw_i2c.c @@ -1,24 +1,10 @@ /* - * File :hw_i2c.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: - * Date Author Notes + * Date Author Notes * 2018-01-04 Sundm75 the first version */ @@ -40,31 +26,31 @@ rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus, rt_uint32_t num) { struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus; - ls1c_i2c_info_t i2c_info; + ls1c_i2c_info_t i2c_info; struct rt_i2c_msg *msg; int i; rt_int32_t ret = RT_EOK; i2c_info.clock = 50000; // 50kb/s - i2c_info.I2Cx = i2c_bus->u32Module; - i2c_init(&i2c_info); - + i2c_info.I2Cx = i2c_bus->u32Module; + i2c_init(&i2c_info); + for (i = 0; i < num; i++) { msg = &msgs[i]; if (msg->flags == RT_I2C_RD) { - i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_READ); - i2c_receive_ack(&i2c_info); - i2c_receive_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len); - i2c_send_stop(&i2c_info); - } + i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_READ); + i2c_receive_ack(&i2c_info); + i2c_receive_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len); + i2c_send_stop(&i2c_info); + } else if(msg->flags == RT_I2C_WR) { - i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_WRITE); - i2c_receive_ack(&i2c_info); - i2c_send_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len); - i2c_send_stop(&i2c_info); - } + i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_WRITE); + i2c_receive_ack(&i2c_info); + i2c_send_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len); + i2c_send_stop(&i2c_info); + } ret++; } return ret; diff --git a/bsp/ls1cdev/drivers/hw_i2c.h b/bsp/ls1cdev/drivers/hw_i2c.h index 1bd4e991ed..cc3e25506f 100644 --- a/bsp/ls1cdev/drivers/hw_i2c.h +++ b/bsp/ls1cdev/drivers/hw_i2c.h @@ -1,21 +1,7 @@ /* - * File : hw_i2c.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/SConscript b/bsp/ls1cdev/drivers/net/SConscript index 1eea3171de..7e5532099e 100644 --- a/bsp/ls1cdev/drivers/net/SConscript +++ b/bsp/ls1cdev/drivers/net/SConscript @@ -5,6 +5,12 @@ src = Glob('*.c') CPPPATH = [cwd] +if GetDepend('RT_USING_LWIP') == False: + SrcRemove(src, 'mii.c') + SrcRemove(src, 'synopGMAC.c') + SrcRemove(src, 'synopGMAC_Dev.c') + SrcRemove(src, 'synopGMAC_plat.c') + group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/bsp/ls1cdev/drivers/net/mii.c b/bsp/ls1cdev/drivers/net/mii.c index b53592f87e..43ade097cf 100644 --- a/bsp/ls1cdev/drivers/net/mii.c +++ b/bsp/ls1cdev/drivers/net/mii.c @@ -1,21 +1,7 @@ /* - * File : mii.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/mii.h b/bsp/ls1cdev/drivers/net/mii.h index cbdd3d5cef..24563834ec 100644 --- a/bsp/ls1cdev/drivers/net/mii.h +++ b/bsp/ls1cdev/drivers/net/mii.h @@ -1,21 +1,7 @@ /* - * File : mii.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC.c b/bsp/ls1cdev/drivers/net/synopGMAC.c index 3ddc1af583..da8c3c89fb 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC.c +++ b/bsp/ls1cdev/drivers/net/synopGMAC.c @@ -1,21 +1,7 @@ /* - * File : synopGMAC.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -586,47 +572,37 @@ struct pbuf *rt_eth_rx(rt_device_t device) } /*Handle the Receive Descriptors*/ - // do{ - desc_index = synopGMAC_get_rx_qptr(gmacdev, &status, &dma_addr1, NULL, &data1, &dma_addr2, NULL, &data2); - - if (desc_index >= 0 && data1 != 0) - { - DEBUG_MES("Received Data at Rx Descriptor %d for skb 0x%08x whose status is %08x\n", desc_index, dma_addr1, status); - - if (synopGMAC_is_rx_desc_valid(status) || SYNOP_PHY_LOOPBACK) - { - pbuf = pbuf_alloc(PBUF_LINK, MAX_ETHERNET_PAYLOAD, PBUF_RAM); - if (pbuf == 0) rt_kprintf("===error in pbuf_alloc\n"); - - - dma_addr1 = plat_dma_map_single(gmacdev, (void *)data1, RX_BUF_SIZE); - len = synopGMAC_get_rx_desc_frame_length(status); //Not interested in Ethernet CRC bytes - rt_memcpy(pbuf->payload, (char *)data1, len); - DEBUG_MES("==get pkg len: %d\n", len); - } - else - { - rt_kprintf("s: %08x\n", status); - adapter->synopGMACNetStats.rx_errors++; - adapter->synopGMACNetStats.collisions += synopGMAC_is_rx_frame_collision(status); - adapter->synopGMACNetStats.rx_crc_errors += synopGMAC_is_rx_crc(status); - adapter->synopGMACNetStats.rx_frame_errors += synopGMAC_is_frame_dribbling_errors(status); - adapter->synopGMACNetStats.rx_length_errors += synopGMAC_is_rx_frame_length_errors(status); - } - - desc_index = synopGMAC_set_rx_qptr(gmacdev, dma_addr1, RX_BUF_SIZE, (u32)data1, 0, 0, 0); - - if (desc_index < 0) + desc_index = synopGMAC_get_rx_qptr(gmacdev, &status, &dma_addr1, NULL, &data1, &dma_addr2, NULL, &data2); + if (desc_index >= 0 && data1 != 0) { + DEBUG_MES("Received Data at Rx Descriptor %d for skb 0x%08x whose status is %08x\n", desc_index, dma_addr1, status); + if (synopGMAC_is_rx_desc_valid(status) || SYNOP_PHY_LOOPBACK) + { + dma_addr1 = plat_dma_map_single(gmacdev, (void *)data1, RX_BUF_SIZE); + len = synopGMAC_get_rx_desc_frame_length(status)-4; //Not interested in Ethernet CRC bytes + pbuf = pbuf_alloc(PBUF_LINK, len, PBUF_RAM); + if (pbuf == 0) rt_kprintf("===error in pbuf_alloc\n"); + rt_memcpy(pbuf->payload, (char *)data1, len); + DEBUG_MES("==get pkg len: %d\n", len); + } + else + { + rt_kprintf("s: %08x\n", status); + adapter->synopGMACNetStats.rx_errors++; + adapter->synopGMACNetStats.collisions += synopGMAC_is_rx_frame_collision(status); + adapter->synopGMACNetStats.rx_crc_errors += synopGMAC_is_rx_crc(status); + adapter->synopGMACNetStats.rx_frame_errors += synopGMAC_is_frame_dribbling_errors(status); + adapter->synopGMACNetStats.rx_length_errors += synopGMAC_is_rx_frame_length_errors(status); + } + desc_index = synopGMAC_set_rx_qptr(gmacdev, dma_addr1, RX_BUF_SIZE, (u32)data1, 0, 0, 0); + if (desc_index < 0) + { #if SYNOP_RX_DEBUG - rt_kprintf("Cannot set Rx Descriptor for data1 %08x\n", (u32)data1); + rt_kprintf("Cannot set Rx Descriptor for data1 %08x\n", (u32)data1); #endif - plat_free_memory((void *)data1); + plat_free_memory((void *)data1); + } } - - } - - // }while(desc_index >= 0); rt_sem_release(&sem_lock); DEBUG_MES("%s : before return \n", __FUNCTION__); return pbuf; @@ -979,6 +955,8 @@ int rt_hw_eth_init(void) eth_device_init(&(eth_dev.parent), "e0"); + eth_device_linkchange(ð_dev.parent, RT_TRUE); //linkup the e0 for lwip to check + return 0; } diff --git a/bsp/ls1cdev/drivers/net/synopGMAC.h b/bsp/ls1cdev/drivers/net/synopGMAC.h index d4d06e6a65..e244c14892 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC.h @@ -1,21 +1,7 @@ /* - * File : synopGMAC.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_Dev.c b/bsp/ls1cdev/drivers/net/synopGMAC_Dev.c index 6c5b4a2cde..a41038f35c 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_Dev.c +++ b/bsp/ls1cdev/drivers/net/synopGMAC_Dev.c @@ -1,33 +1,7 @@ -/** \file - * This file defines the synopsys GMAC device dependent functions. - * Most of the operations on the GMAC device are available in this file. - * Functions for initiliasing and accessing MAC/DMA/PHY registers and the DMA descriptors - * are encapsulated in this file. The functions are platform/host/OS independent. - * These functions in turn use the low level device dependent (HAL) functions to - * access the register space. - * \internal - * ------------------------REVISION HISTORY--------------------------------- - * Synopsys 01/Aug/2007 Created - */ - /* - * File : synopGMAC_Dev.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear - * - * 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. + * Copyright (c) 2006-2018, RT-Thread Development Team * - * 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_Dev.h b/bsp/ls1cdev/drivers/net/synopGMAC_Dev.h index 25c7814a16..74bc850d86 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_Dev.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC_Dev.h @@ -1,32 +1,7 @@ -/**\file - * This file defines the function prototypes for the Synopsys GMAC device and the - * Marvell 88E1011/88E1011S integrated 10/100/1000 Gigabit Ethernet Transceiver. - * Since the phy register mapping are standardised, the phy register map and the - * bit definitions remain the same for other phy as well. - * This also defines some of the Ethernet related parmeters. - * \internal - * -----------------------------REVISION HISTORY------------------------------------ - * Synopsys 01/Aug/2007 Created - */ - /* - * File : synopGMAC_Dev.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear - * - * 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. + * Copyright (c) 2006-2018, RT-Thread Development Team * - * 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_Host.h b/bsp/ls1cdev/drivers/net/synopGMAC_Host.h index 05f289c8c7..e6d31d551d 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_Host.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC_Host.h @@ -1,21 +1,7 @@ /* - * File : synopGMAC_Host.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_debug.h b/bsp/ls1cdev/drivers/net/synopGMAC_debug.h index 9e65d1d945..9766587e1a 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_debug.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC_debug.h @@ -1,21 +1,7 @@ /* - * File : synopGMAC_debug.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_network_interface.h b/bsp/ls1cdev/drivers/net/synopGMAC_network_interface.h index e93ab6a66e..8ded91d359 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_network_interface.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC_network_interface.h @@ -1,30 +1,7 @@ -/** \file - * Header file for the nework dependent functionality. - * The function prototype listed here are linux dependent. - * - * \internal - * ---------------------------REVISION HISTORY------------------- - * Synopsys 01/Aug/2007 Created - */ - /* - * File : synopGMAC_network_interface.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear - * - * 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. + * Copyright (c) 2006-2018, RT-Thread Development Team * - * 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_plat.c b/bsp/ls1cdev/drivers/net/synopGMAC_plat.c index 8f70ad99d7..3dfbe077d7 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_plat.c +++ b/bsp/ls1cdev/drivers/net/synopGMAC_plat.c @@ -1,34 +1,7 @@ -/**\file - * This file defines the wrapper for the platform/OS related functions - * The function definitions needs to be modified according to the platform - * and the Operating system used. - * This file should be handled with greatest care while porting the driver - * to a different platform running different operating system other than - * Linux 2.6.xx. - * \internal - * ----------------------------REVISION HISTORY----------------------------- - * Synopsys 01/Aug/2007 Created - */ - - /* - * File : synopGMAC_plat.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear - * - * 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. + * Copyright (c) 2006-2018, RT-Thread Development Team * - * 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_plat.h b/bsp/ls1cdev/drivers/net/synopGMAC_plat.h index 1b1283f2fc..173015a53f 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_plat.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC_plat.h @@ -1,33 +1,7 @@ -/**\file - * This file serves as the wrapper for the platform/OS dependent functions - * It is needed to modify these functions accordingly based on the platform and the - * OS. Whenever the synopsys GMAC driver ported on to different platform, this file - * should be handled at most care. - * The corresponding function definitions for non-inline functions are available in - * synopGMAC_plat.c file. - * \internal - * -------------------------------------REVISION HISTORY--------------------------- - * Synopsys 01/Aug/2007 Created - */ - /* - * File : synopGMAC_plat.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear - * - * 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. + * Copyright (c) 2006-2018, RT-Thread Development Team * - * 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -65,14 +39,6 @@ typedef int bool; //typedef unsigned long dma_addr_t; -//sw -/* write/read MMIO register */ -#define writeb(val, addr) (*(volatile u8*)(addr) = (val)) -#define writew(val, addr) (*(volatile u16*)(addr) = (val)) -#define writel(val, addr) (*(volatile u32*)(addr) = (val)) -#define readb(addr) (*(volatile u8*)(addr)) -#define readw(addr) (*(volatile u16*)(addr)) -#define readl(addr) (*(volatile u32*)(addr)) #define KUSEG_ADDR 0x0 diff --git a/bsp/ls1cdev/drivers/net/synopGMAC_types.h b/bsp/ls1cdev/drivers/net/synopGMAC_types.h index d808bec846..88504eebf7 100644 --- a/bsp/ls1cdev/drivers/net/synopGMAC_types.h +++ b/bsp/ls1cdev/drivers/net/synopGMAC_types.h @@ -1,21 +1,7 @@ /* - * File : synopGMAC_types.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) chinesebear + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/bsp/ls1cdev/drivers/selfboot.h b/bsp/ls1cdev/drivers/selfboot.h new file mode 100644 index 0000000000..def9e38229 --- /dev/null +++ b/bsp/ls1cdev/drivers/selfboot.h @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-12-04 Jiaxun Yang Initial version + */ + +#ifndef __RT_LS1C_SELFBOOT_H +#define __RT_LS1C_SELFBOOT_H + +/* SDRAM PARAM macro */ +#define SD_FREQ (((APB_CLK / 4) * (PLL_MULT / CPU_DIV)) / SDRAM_PARAM_DIV_NUM) + + +/* SDRAM ROW */ +#define ROW_1K 0x7 +#define ROW_2K 0x0 +#define ROW_4K 0x1 +#define ROW_8K 0x2 +#define ROW_16K 0x3 +/* SDRAM COL */ +#define COL_256 0x7 +#define COL_512 0x0 +#define COL_1K 0x1 +#define COL_2K 0x2 +#define COL_4K 0x3 +/* SDRAM WIDTH */ +#define WIDTH_8 0x0 +#define WIDTH_16 0x1 +#define WIDTH_32 0x2 + +#define TRCD 3 +#define TCL 3 +#define TRP 3 +#define TRFC 8 +#define TRAS 6 +#define TREF 0x818 +#define TWR 2 + +#define DEF_SEL 0x1 +#define DEF_SEL_N 0x0 +#define HANG_UP 0x1 +#define HANG_UP_N 0x0 +#define CFG_VALID 0x1 + +#include "board.h" + +#define SD_PARA0 (0x7f<<25 | \ + (TRAS << 21) | \ + (TRFC << 17) | (TRP << 14) | (TCL << 11) | \ + (TRCD << 8) | (SDRAM_WIDTH << 6) | (SDRAM_COL << 3) | \ + SDRAM_ROW) + +#define SD_PARA1 ((HANG_UP_N << 8) | (DEF_SEL_N << 7) | (TWR << 5) | (TREF >> 7)) + +#define SD_PARA1_EN ((CFG_VALID << 9) | (HANG_UP_N << 8) | \ + (DEF_SEL_N << 7) | (TWR << 5) | (TREF >> 7)) + +#define LS1C_CBUS_FIRST1 0xBFE011C4 +#define LS1C_UART2_BASE 0xBFE48000 + +/* UART register offset*/ +#define LS1C_UART_DAT_OFFSET (0) +#define LS1C_UART_IER_OFFSET (1) +#define LS1C_UART_IIR_OFFSET (2) +#define LS1C_UART_FCR_OFFSET (2) +#define LS1C_UART_LCR_OFFSET (3) +#define LS1C_UART_MCR_OFFSET (4) +#define LS1C_UART_LSR_OFFSET (5) +#define LS1C_UART_MSR_OFFSET (6) + +#define LS1C_UART_LSB_OFFSET (0) +#define LS1C_UART_MSB_OFFSET (1) + +/* interrupt enable register */ +#define IER_IRxE 0x1 +#define IER_ITxE 0x2 +#define IER_ILE 0x4 +#define IER_IME 0x8 + +/* interrupt identification register */ +#define IIR_IMASK 0xf /* mask */ +#define IIR_RXTOUT 0xc /* receive timeout */ +#define IIR_RLS 0x6 /* receive line status */ +#define IIR_RXRDY 0x4 /* receive ready */ +#define IIR_TXRDY 0x2 /* transmit ready */ +#define IIR_NOPEND 0x1 /* nothing */ +#define IIR_MLSC 0x0 /* modem status */ +#define IIR_FIFO_MASK 0xc0 /* set if FIFOs are enabled */ + +/* fifo control register */ +#define FIFO_ENABLE 0x01 /* enable fifo */ +#define FIFO_RCV_RST 0x02 /* reset receive fifo */ +#define FIFO_XMT_RST 0x04 /* reset transmit fifo */ +#define FIFO_DMA_MODE 0x08 /* enable dma mode */ +#define FIFO_TRIGGER_1 0x00 /* trigger at 1 char */ +#define FIFO_TRIGGER_4 0x40 /* trigger at 4 chars */ +#define FIFO_TRIGGER_8 0x80 /* trigger at 8 chars */ +#define FIFO_TRIGGER_14 0xc0 /* trigger at 14 chars */ + +/* character format control register */ +#define CFCR_DLAB 0x80 /* divisor latch */ +#define CFCR_SBREAK 0x40 /* send break */ +#define CFCR_PZERO 0x30 /* zero parity */ +#define CFCR_PONE 0x20 /* one parity */ +#define CFCR_PEVEN 0x10 /* even parity */ +#define CFCR_PODD 0x00 /* odd parity */ +#define CFCR_PENAB 0x08 /* parity enable */ +#define CFCR_STOPB 0x04 /* 2 stop bits */ +#define CFCR_8BITS 0x03 /* 8 data bits */ +#define CFCR_7BITS 0x02 /* 7 data bits */ +#define CFCR_6BITS 0x01 /* 6 data bits */ +#define CFCR_5BITS 0x00 /* 5 data bits */ + +/* modem control register */ +#define MCR_LOOPBACK 0x10 /* loopback */ +#define MCR_IENABLE 0x08 /* output 2 = int enable */ +#define MCR_DRS 0x04 /* output 1 = xxx */ +#define MCR_RTS 0x02 /* enable RTS */ +#define MCR_DTR 0x01 /* enable DTR */ + +/* line status register */ +#define LSR_RCV_FIFO 0x80 /* error in receive fifo */ +#define LSR_TSRE 0x40 /* transmitter empty */ +#define LSR_TXRDY 0x20 /* transmitter ready */ +#define LSR_BI 0x10 /* break detected */ +#define LSR_FE 0x08 /* framing error */ +#define LSR_PE 0x04 /* parity error */ +#define LSR_OE 0x02 /* overrun error */ +#define LSR_RXRDY 0x01 /* receiver ready */ +#define LSR_RCV_MASK 0x1f + + +/* External clock frequency */ +#define AHB_CLK (24000000) +#define APB_CLK (AHB_CLK) + + +/* START_FREQ register bits */ +#define M_PLL_SHIFT (8) +#define M_PLL (0xff << M_PLL_SHIFT) +#define FRAC_N_SHIFT (16) +#define FRAC_N (0xff << FRAC_N_SHIFT) +#define DIV_SDRAM_SHIFT (0) +#define DIV_SDRAM (0x3 << DIV_SDRAM_SHIFT) + +/* CLK_DIV_PARAM register 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) + +#endif diff --git a/bsp/ls1cdev/drivers/selfboot_gcc.S b/bsp/ls1cdev/drivers/selfboot_gcc.S new file mode 100644 index 0000000000..03dfc206d4 --- /dev/null +++ b/bsp/ls1cdev/drivers/selfboot_gcc.S @@ -0,0 +1,549 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Origin Authors: Loongson Technology Corporation Limited, + * caogos <[email protected]>, Jiaxun Yang <[email protected]>, + * + * Also thanks to Liu Shiwei <[email protected]> and other Loongson + * Community developers. + * + * Change Logs: + * Date Author Notes + * 2019-12-04 Jiaxun Yang Initial version + */ + +#include <rtconfig.h> + +#ifdef RT_USING_SELF_BOOT + +#ifndef __ASSEMBLY__ +#define __ASSEMBLY__ +#endif + +#include <mips.h> +#include "selfboot.h" +#include "ls1c.h" +#include "cache.h" + +/* + * Register usage: + * + * s0 link versus load offset, used to relocate absolute adresses. + * s1 free + * s2 memory size + * s3 free + * s4 free + * s5 dbg + * s6 sdCfg + * s7 rasave + * s8 free + */ + +#define tmpsize s1 +#define msize s2 +#define bonito s4 +#define dbg s5 +#define sdCfg s6 + + +/* Macros */ +#ifdef RT_SELF_BOOT_DEBUG +#define PRINTSTR(str) \ + .pushsection .selfboot_data; .align 4; 98: .asciz str; .popsection; la a0, 98b; bal stringserial; nop +#else +#define PRINTSTR(x) +#endif + +#define DELAY(count) \ + li v0, count; \ +99: \ + bnez v0, 99b;\ + addiu v0, -1 + +.section ".selfboot", "ax" +.set noreorder +.set mips32 +.globl _start +.extern start +_start: + +/* NMI/Reset vector starts here*/ + mtc0 zero, CP0_STATUS /* set cp0 status register to zero */ + mtc0 zero, CP0_CAUSE /* set cp0 cause register to zero */ + + li t0, ST0_BEV /* set exception vector to in flash location */ + mtc0 t0, CP0_STATUS + + /* Speed up SPI reading */ + li t0, 0xbfe80000 /* load SPI0 controler base address to t0 */ + li t1, 0x17 /* load "div 4, fast_read + burst_en + memory_en double I/O" to + * to t0 for write, not all the flash chips support this mode */ + sb t1, 0x4(t0) /* set sfc_param register */ + li t1, 0x05 + sb t1, 0x6(t0) /* set sfc_timing register */ + + bal locate /* branch out of vector and get current address to ra */ + nop + +/* in-flash exception vectors start here */ +/* save the exception types to a0 and print out PANIC message in exc_common */ +#define EXC_TLB_REFILL 0x0 +#define EXC_CACHE_ERR 0x1 +#define EXC_GEN 0x2 +#define EXC_INT 0x3 + +.org 0x200 /* 0xbfc00200 TLB_REFILL exception */ + li a0, EXC_TLB_REFILL + b exc_common + nop + +.org 0x300 /* 0xbfc00300 Cache Error exception */ + li a0, EXC_CACHE_ERR + b exc_common + nop + +.org 0x380 /* 0xbfc00300 General exception */ + li a0,EXC_GEN + b exc_common + nop + +.org 0x400 /* 0xbfc00400 Interrupt exception */ + li a0, EXC_INT + b exc_common + nop + +1: /* impossible to reach here, so make a dead loop */ + b 1b + nop + +exc_common: /* try one cause and pass to next */ + li s1, EXC_TLB_REFILL + bne a0, s1, 1f + nop + PRINTSTR("\r\nEARLY_PANIC: Exception TLB Refill") + b print_cause + nop +1: + li s1, EXC_CACHE_ERR + bne a0, s1, 1f + nop + PRINTSTR("\r\nEARLY_PANIC: CACHE Error: ") + mfc0 a0, CP0_CACHEERR + bal hexserial + nop + b print_cause + nop +1: + li s1, EXC_GEN + bne a0, s1, 1f + nop + PRINTSTR("\r\nEARLY_PANIC: General Exception") + b print_cause + nop +1: + li s1, EXC_INT + bne a0, s1, print_cause /* if all exceptions in a0 not reached, + * print_cause directly*/ + nop + PRINTSTR("\r\nEARLY_PANIC: Interrupt Exception") +print_cause: + PRINTSTR("\r\nCAUSE=") + mfc0 a0, CP0_CAUSE + bal hexserial + nop + + PRINTSTR("\r\nSTATUS=") + mfc0 a0, CP0_STATUS + bal hexserial + nop + + PRINTSTR("\r\nERRORPC=") + mfc0 a0, CP0_ERROREPC + bal hexserial + nop + + PRINTSTR("\r\nEPC=") + mfc0 a0, CP0_EPC + bal hexserial + nop + + PRINTSTR("\r\nBADADDR=") + mfc0 a0, CP0_BADVADDR + bal hexserial + nop + + PRINTSTR("\r\nEARLY: LOOP! Noting to do") +1: /* Make a dead loop here, wait user to reset the MCU */ + b 1b + nop + +/* locate here, continue the start progress */ + +locate: + /* fix the absolute address by ra */ + la s0, start /* s0 = start */ + subu s0, ra, s0 /* s0 = ra - s0 */ + and s0, 0xffff0000 /* s0 = s0 & 0xffff0000 */ + + li t0, 0xbfe78030 /* load PLL/SDRAM freq config register base to t0 */ + li t2, (0x80000008 | (PLL_MULT << 8) | (0x3 << 2) | SDRAM_DIV) /* Set PLL + * MULT and PLL DIV */ + li t3, (0x00008003 | (CPU_DIV << 8)) /* set CPU DEV */ + + li t1, 0x2 + sw t1, 0x4(t0) /* disable CPU_DIV_VALID firstly for adjustment */ + sw t2, 0x0(t0) /* write START_FREQ */ + sw t3, 0x4(t0) /* write CLK_DIV_PARAM */ + +/* start to initialize debug uart port */ + la v0, LS1C_UART2_BASE /* load UART2 base to v0, only UART2 can be debug port */ +1: + li v1, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4 /* clear Rx,Tx FIFO + * declear 4 bit int trigger */ + sb v1, LS1C_UART_FCR_OFFSET(v0) /* write FCR (FIFO control register) */ + li v1, CFCR_DLAB /* reach freq div register */ + sb v1, LS1C_UART_LCR_OFFSET(v0) /* write LCR (Line control register)*/ + /* Set UART2 reuse with GPIO36,37*/ + li a0, LS1C_CBUS_FIRST1 /* load CBUS_FIRST1 offset to a0 */ + lw a1, 0x10(a0) /* load value from CBUS_SECOND1 to a1 */ + ori a1, 0x30 /* a1 |= 0x30, GPIO36,37 as secondary function */ + sw a1, 0x10(a0) /* write back modified CBUS_SECOND1 from a1 */ + /* Caculate PLL and bit rate */ + li a0, 0xbfe78030 /* load START_FREQ register address to a0 */ + lw a1, 0(a0) /* load value from START_FREQ to a1*/ + srl a1, 8 /* a1 >>= 8 */ + andi a1, 0xff /* a1 &= 0xff, as a1=PLL_MULT */ + li a2, APB_CLK /* a2 = APB_CLK = 24Mhz (External Clock Freq) */ + srl a2, 2 /* a2 = a2 >> 2 = APB_CLK/4 */ + multu a1, a2 /* hilo = a1 * a2 = PLL_MULT * APB_CLK /4 */ + mflo v1 /* v1 = lo. put low 32 bit of a1 * a2 to v1 as PLL freq */ + /* Determine if we need to devide the clock */ + lw a1, 4(a0) /* load value frm CLK_DIV_PARAM to a1 */ + andi a2, a1, DIV_CPU_SEL /* a2 = a1 & DIV_CPU_SEL, if CPU_SEL=1, devide the clock, + * if CPU_SEL=0, bypass the clock */ + bnez a2, 1f /* if (a2 != 0), branch to next tag 1 */ + nop + li v1, APB_CLK /* v1 = APB_CLK */ + b 3f + nop +1: /* Determine if the CPU_DIV is valid*/ + andi a2, a1, DIV_CPU_EN /* a2 = a1 & DIV_CPU_EN */ + bnez a2, 2f /* if (a2 != 0), branch to next tag 2 */ + nop + srl v1, 1 /* v1 >>= 1, so v1 = APB_CLK/4 * PLL_MULT/2 */ + b 3f + nop +2: /* caculate CPU freq */ + andi a1, DIV_CPU /* a1 &= DIV_CPU */ + srl a1, DIV_CPU_SHIFT /* a1 >>= DIV_CPU_SHIFT */ + divu v1, a1 /* lo = v1/a1, hi = v1 % a1 */ + mflo v1 /* v1 = lo, CPU Freq */ +3: + li a1, (16 * EARLY_DEBUG_BAUD) /* a1 = 16 * BIT RATE */ + divu v1, v1, a1 /* v1 = v1 / a1 */ + srl v1, 1 /* v1 >>= 1 */ + sb v1, LS1C_UART_LSB_OFFSET(v0) /* write 8bit low into LSB */ + srl v1, 8 /* v1 >>= 8 */ + sb v1, LS1C_UART_MSB_OFFSET(v0) /* write 8bit low into MSB */ + + li v1, CFCR_8BITS /* 8n1, no check */ + sb v1, LS1C_UART_LCR_OFFSET(v0) /* write to LCR (Line Control Register) */ + +#ifdef EARLY_DEBUG_UART_FLOW_CTRL + li v1, MCR_DTR|MCR_RTS /* valid DTR and RTS */ + sb v1, LS1C_UART_MCR_OFFSET(v0) /* write to MCR (MODEM Control Register) */ +#endif + + li v1, 0x0 /* disable all the interruptions */ + sb v1, LS1C_UART_IER_OFFSET(v0) /* write to IER (Interruptions Enable Registers) */ + + PRINTSTR("\r\INFO: Loongson 1C300 Starting :) \r\n") + + /* disable all GPIOs for conflict functions */ + li a0,0xbfd00000 + sw zero,0x10c0(a0) /* disable GPIO 0-31 */ + sw zero,0x10c4(a0) /* disable GPIO 32-63 */ + sw zero,0x10c8(a0) /* disable GPIO 64-95 */ + sw zero,0x10cc(a0) + + li t0, 0xffffffff + sw t0, 0x10d0(a0) + sw t0, 0x10d4(a0) + sw t0, 0x10d8(a0) + sw t0, 0x10dc(a0) + + sw t0, 0x10f0(a0) + sw t0, 0x10f4(a0) + sw t0, 0x10f8(a0) + sw t0, 0x10fc(a0) + + PRINTSTR("\r\INFO: All GPIOs are disabled\r\n") + + /* SDRAM initialize starts here */ + + li msize, MEM_SIZE + +#ifdef EJTAG_SEL_AS_SDRAM_CS1 + li a0, 0xbfd011c0 + lw a1, 0x40(a0) + ori a1, 0x01 + sw a1, 0x40(a0) + PRINTSTR("\r\INFO: EJTAG_SEL PIN as SDRAM_CS1\r\n") +#endif + +/* +* recommanded by user manual, we should write SD_CONFIG[31:0] first, then +* write SD_CONFIG[63:32]. Repeat writing for three times, valid the config in +* the last time. +*/ + +/* write first time */ +li t1, 0xbfd00410 /* load SD_CONFIG[31:0] address to t1 */ +li a1, SD_PARA0 /* get the memory config from macro SD_PARA0 */ +sw a1, 0x0(t1) /* write to SD_CONFIG[31:0] */ +li a1, SD_PARA1 +sw a1, 0x4(t1) /* write to SD_CONFIG[63:32] with offset */ +PRINTSTR("\r\INFO: SDRAM Config Pass1\r\n") + +/* write second time,the same */ +li a1, SD_PARA0 +sw a1, 0x0(t1) +li a1, SD_PARA1 +sw a1, 0x4(t1) +PRINTSTR("\r\INFO: SDRAM Config Pass2\r\n") + +/* write third time, enable controller this time */ +li a1, SD_PARA0 +sw a1, 0x0(t1) +li a1, SD_PARA1_EN /* enable it */ +sw a1, 0x4(t1) +PRINTSTR("\r\INFO: SDRAM initialize compeleted\r\n") + + +/* initialize cache */ +bal cache_init /* branch to cache_init */ +nop + +/* enable cache */ +mfc0 a0, CP0_CONFIG /* load cp0 config to a0 */ +and a0, a0, ~((1<<12) | 7) /* a0 = a0 & ~((1<<12) | 7) */ +or a0, a0, 2 /* a0 |= 2 */ +mtc0 a0, CP0_CONFIG /* write back to CP0 config */ + +/* +* relocate: copy selfboot code to memory in kseg0, fix PC and jump to kseg0. +* in order to speed up the copy progress, we will execute copy code in kseg0 +*/ + +PRINTSTR("\r\INFO: Relocating") +la t0, text_copy_start /* load the adress of start tag to t0 */ +move t2, t0 +addu t0, s0 /* correct t0 address in rom by s0 */ +la t1, text_copy_end + +selfboot_copy_loop: +lw v0, (t0) /* copy from memory address in t0 to register v0 */ +sw v0, (t2) /* write data in register v0 to memory address t0 */ +addiu t0, 0x4 /* t0 moves forward 4 bytes */ +addiu t2, 0x4 /* t2 moves forward 4 bytes */ +ble t2, t1, selfboot_copy_loop /* if t1 <= t2 loop to continue the copy */ +nop + +la t0, text_copy_start /* load start address to t0 */ +jr t0 /* jump to 122 in kseg0 to start copy code progress */ +nop + +text_copy_start: +/* Copy code to memory*/ +la a0, start /* load address of start symbol to a0 */ +addu a1, a0, s0 /* correct a0 to address in flash */ +la a2, _edata /* load symbol _edata address to a2 */ +subu t1, a2, a0 /* t1 = a2 - a0, the space of text area */ + +move t0, a0 /* the start address in ram */ +move t1, a1 /* the start address in rom */ +move t2, a2 /* the end address in rom (symbol _edata) */ + +/* copy text section */ +1: +and t3, t0, 0x0000ffff /* t3 = t0 & 0x0000ffff, get low 16 bit */ +bnez t3, 2f /* if t3 != 0, jump to next tag 2 */ +nop +2: +lw t3, 0(t1) /* copy 4 bit from memory address t1 to register t3 */ +nop +sw t3, 0(t0) /* copy 4 bit from register t3 to memory address in t0 */ +addu t0, 4 /* t0 move forward 4 bytes */ +addu t1, 4 /* t1 move forward 4 bytes */ +bne t2, t0, 1b /* if t2 != t0, branch to last tag 1 to continue copy */ +nop +/* copy text section done. */ + +move a0, msize /* a0 = msize, will be passed to main */ +srl a0, 20 /* a0 >>= 20, convert to unit in MB */ + +/* execute main */ +la v0, _rtthread_entry /* load address of function main to v0 */ +jalr v0 /* call address in v0, congrats! all low_level things done! + * switch brain out of assembly */ +nop +text_copy_end: /* end of self-copy in memory */ + +loop: /* impossible to reach here, make a dead loop */ +b loop +nop + +/* functions here */ + +LEAF(stringserial) /* print out the string in address passed in a0 */ + nop + move a2, ra /* save the return address to a2 */ + addu a1, a0, s0 /* correct the address in ROM */ + lbu a0, 0(a1) /* read the first byte in memory address a1 to a0 */ +1: + beqz a0, 2f /* if a0 == 0, jump to next tag 2, empty char */ + nop + bal tgt_putchar /* print a char */ + addiu a1, 1 /* a1 += 1 move forward to next byte */ + b 1b /* branch to the last tag 1, continue */ + lbu a0, 0(a1) /* load the next bit from address a1 to a0, in delay solt, + * will be execuated before branch */ +2: + j a2 /* return */ + nop +END(stringserial) + + +LEAF(hexserial) /* print out single hex char passed in register a0 */ + nop + move a2, ra /* move return address from ra to a2 */ + move a1, a0 /* move hex char from register a0 to a1 */ + li a3, 7 /* load 7 to a3 */ +1: + rol a0, a1, 4 /* rotate left ward shift for 4 bit in a1 to a0 */ + move a1, a0 + and a0, 0xf + la v0, hexchar + +.pushsection .selfboot_data +.align 4 +hexchar: + .ascii "0123456789abcdef" +.popsection +.align 4 + + addu v0, s0 + addu v0, a0 + bal tgt_putchar + lbu a0, 0(v0) + + bnez a3, 1b + addu a3, -1 + + j a2 + nop +END(hexserial) + +LEAF(tgt_putchar) /* print out a char in a0 */ + la v0, LS1C_UART2_BASE /* load UART register address to a0 */ + lbu v1, LS1C_UART_LSR_OFFSET(v0) /* load value from LSR to v0 */ +1: + and v1, LSR_TXRDY /* v1 &= LSR_TXRDY determine wether we can send by TFE bit */ + beqz v1, 1b /* if (v1 == 0) jump to last 1 tag, waiting until TFE is 1 */ + lbu v1, LS1C_UART_LSR_OFFSET(v0) /* load value from LSR to v0 again, in delay solt */ + + sb a0, LS1C_UART_DAT_OFFSET(v0) /* write a0 into DAT, send out */ + j ra /* */ + nop +END(tgt_putchar) + +LEAF(CPU_SetSR) /* modify SR value, arg 1 = set bits, arg 2 = clear bits. */ + mfc0 v0, CP0_STATUS + not v1, a1 + and v1, v0 + or v1, a0 + mtc0 v1, CP0_STATUS + nop + nop + nop + nop + nop + nop + nop + nop + j ra + nop +END(CPU_SetSR) + +cache_init: + move t1, ra +####part 2#### +cache_detect_4way: + mfc0 t4, CP0_CONFIG,1 /* move CP0 CONFIG to t4 */ + lui v0, 0x7 /* v0 = 0x7 << 16 */ + and v0, t4, v0 /* v0 = t4 & v0 */ + srl t3, v0, 16 /* t3 = v0 >> 16 Icache组相联数 IA */ + + li t5, 0x800 //32*64 + srl v1, t4,22 //v1 = t4 >> 22 + andi v1, 7 //Icache每路的组数 64x2^S IS + sll t5, v1 //InstCacheSetSize + sll t5, t3 //t5 InstCacheSize + + + andi v0, t4, 0x0380 + srl t7, v0, 7 //DA + + li t6, 0x800 // 32*64 + srl v1, t4,13 + andi v1, 7 //DS + sll t6, v1 // DataCacheSetSize + sll t6, t7 // t5 DataCacheSize + +####part 3#### + lui a0, 0x8000 //a0 = 0x8000 << 16 + addu a1, $0, t5 + addu a2, $0, t6 +cache_init_d2way: +/* a0=0x80000000, a1=icache_size, a2=dcache_size */ +/* a3, v0 and v1 used as local registers */ + mtc0 $0, CP0_TAGHI + addu v0, $0, a0 /* v0 = 0 + a0 */ + addu v1, a0, a2 /* v1 = a0 + a2 */ +1: slt a3, v0, v1 /* a3 = v0 < v1 ? 1 : 0 */ + beq a3, $0, 1f /* if (a3 == 0) goto 1f */ + nop + mtc0 $0, CP0_TAGLO + cache Index_Store_Tag_D, 0x0(v0) /* 1 way */ +4: beq $0, $0, 1b + addiu v0, v0, 0x20 +1: +cache_flush_i2way: + addu v0, $0, a0 + addu v1, a0, a1 +1: + slt a3, v0, v1 + beq a3, $0, 1f + nop + cache Index_Invalidate_I, 0x0(v0) /* 1 way */ +4: + beq $0, $0, 1b + addiu v0, v0, 0x20 +1: +cache_flush_d2way: + addu v0, $0, a0 + addu v1, a0, a2 +1: slt a3, v0, v1 + beq a3, $0, 1f + nop + cache Index_Writeback_Inv_D, 0x0(v0) /* 1 way */ +4: beq $0, $0, 1b + addiu v0, v0, 0x20 + +1: +cache_init_finish: + jr t1 + nop +#endif diff --git a/bsp/ls1cdev/drivers/touch.c b/bsp/ls1cdev/drivers/touch.c index 249e7682bf..37a4b19213 100644 --- a/bsp/ls1cdev/drivers/touch.c +++ b/bsp/ls1cdev/drivers/touch.c @@ -1,21 +1,7 @@ /* - * File : touch.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -32,7 +18,7 @@ #include "drv_spi.h" #include "touch.h" -#ifdef RT_USING_RTGUI +#ifdef XPT2046_USING_TOUCH #include <rtgui/calibration.h> #include <rtgui/event.h> @@ -40,8 +26,9 @@ #include <rtgui/rtgui_server.h> #include <rtgui/rtgui_system.h> -//����Ļ ����Ҫ _ILI_HORIZONTAL_DIRECTION_ -//����Ļ ��Ҫ _ILI_HORIZONTAL_DIRECTION_ + +//竖屏幕 不需要 _ILI_HORIZONTAL_DIRECTION_ +//横屏幕 需要 _ILI_HORIZONTAL_DIRECTION_ //#define _ILI_HORIZONTAL_DIRECTION_ @@ -57,7 +44,7 @@ TOUCH INT: 84 */ #define IS_TOUCH_UP() gpio_get(TOUCH_INT_PIN) -#define led_gpio 52 // led1ָʾ +#define led_gpio 52 // led1指示 #define DUMMY 0x00 @@ -97,7 +84,7 @@ s A2-A0 MODE SER/DFR PD1-PD0 #define TOUCH_MSR_Y (START | MEASURE_Y | MODE_12BIT | DIFFERENTIAL | POWER_MODE0) -/* ���¶���XPT2046 �Ĵ�����λ��*/ +/* 以下定义XPT2046 的触摸屏位置*/ #if defined(_ILI_HORIZONTAL_DIRECTION_) #define MIN_X_DEFAULT 2047 #define MAX_X_DEFAULT 47 @@ -117,7 +104,7 @@ s A2-A0 MODE SER/DFR PD1-PD0 #define SH 10 // Valve value -/*�궨�� */ +/*宏定义 */ #define TOUCH_SPI_X SPI1 #define TOUCH_INT_PIN 84 #define TOUCH_CS_PIN 49 @@ -126,21 +113,21 @@ s A2-A0 MODE SER/DFR PD1-PD0 #define TOUCH_MOSI_PIN 48 -/*�����ṹ�彫��Ҫ�õ��Ķ������д��*/ +/*创建结构体将需要用到的东西进行打包*/ struct rtgui_touch_device { - struct rt_device parent; /* ����ע���豸*/ + struct rt_device parent; /* 用于注册设备*/ - rt_uint16_t x, y; /* ��¼��ȡ����λ��ֵ */ + rt_uint16_t x, y; /* 记录读取到的位置值 */ - rt_bool_t calibrating; /* ����У��־ */ - rt_touch_calibration_func_t calibration_func;/* �������� ����ָ�� */ + rt_bool_t calibrating; /* 触摸校准标志 */ + rt_touch_calibration_func_t calibration_func;/* 触摸函数 函数指针 */ - rt_uint16_t min_x, max_x; /* У�� X ������С ���ֵ */ - rt_uint16_t min_y, max_y; /* У�� Y ������С ���ֵ */ + rt_uint16_t min_x, max_x; /* 校准后 X 方向最小 最大值 */ + rt_uint16_t min_y, max_y; /* 校准后 Y 方向最小 最大值 */ - struct rt_spi_device * spi_device; /* SPI �豸 ����ͨ�� */ - struct rt_event event; /* �¼�ͬ�������ڡ����жϡ� */ + struct rt_spi_device * spi_device; /* SPI 设备 用于通信 */ + struct rt_event event; /* 事件同步,用于“笔中断” */ }; static struct rtgui_touch_device *touch = RT_NULL; @@ -230,14 +217,14 @@ static void rtgui_touch_calculate(void) /* read touch */ { rt_uint8_t i, j, k, min; - rt_uint16_t temp; + rt_uint16_t temp; rt_uint16_t tmpxy[2][SAMP_CNT]; rt_uint8_t send_buffer[1]; rt_uint8_t recv_buffer[2]; for(i=0; i<SAMP_CNT; i++) { send_buffer[0] = TOUCH_MSR_X; - touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2); + touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2); rt_kprintf("touch x: %d ",(recv_buffer[0]*256|recv_buffer[1])>>4); #if defined(_ILI_HORIZONTAL_DIRECTION_) tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ; @@ -249,7 +236,7 @@ static void rtgui_touch_calculate(void) #endif send_buffer[0] = TOUCH_MSR_Y; touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2); - rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4); + rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4); #if defined(_ILI_HORIZONTAL_DIRECTION_) tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ; @@ -259,46 +246,46 @@ static void rtgui_touch_calculate(void) tmpxy[1][i] >>= 4; #endif } - /*�ٴδ����ж�*/ + /*再次打开触摸中断*/ send_buffer[0] = 1 << 7; - touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2); + touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2); touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2); /* calculate average */ - { - rt_uint32_t total_x = 0; - rt_uint32_t total_y = 0; - for(k=0; k<2; k++) - { - // sorting the ADC value - for(i=0; i<SAMP_CNT-1; i++) - { - min=i; - for (j=i+1; j<SAMP_CNT; j++) - { - if (tmpxy[k][min] > tmpxy[k][j]) - min=j; - } - temp = tmpxy[k][i]; - tmpxy[k][i] = tmpxy[k][min]; - tmpxy[k][min] = temp; - } - //check value for Valve value - if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH) - { - return; - } - } - total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1]; - total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1]; - //calculate average value - touch->x=total_x>>2; - touch->y=total_y>>2; + { + rt_uint32_t total_x = 0; + rt_uint32_t total_y = 0; + for(k=0; k<2; k++) + { + // sorting the ADC value + for(i=0; i<SAMP_CNT-1; i++) + { + min=i; + for (j=i+1; j<SAMP_CNT; j++) + { + if (tmpxy[k][min] > tmpxy[k][j]) + min=j; + } + temp = tmpxy[k][i]; + tmpxy[k][i] = tmpxy[k][min]; + tmpxy[k][min] = temp; + } + //check value for Valve value + if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH) + { + return; + } + } + total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1]; + total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1]; + //calculate average value + touch->x=total_x>>2; + touch->y=total_y>>2; rt_kprintf("touch->x:%d touch->y:%d\r\n", touch->x, touch->y); } /* calculate average */ } /* read touch */ /* if it's not in calibration status */ - /*����ֵ����*/ + /*触摸值缩放*/ if (touch->calibrating != RT_TRUE) { if (touch->max_x > touch->min_x) @@ -322,19 +309,19 @@ static void rtgui_touch_calculate(void) } } #include "ls1c_regs.h" -#define TOUCH_INT_EN __REG32(LS1C_INT4_EN) +#define TOUCH_INT_EN __REG32(LS1C_INT4_EN) rt_inline void touch_int_cmd(rt_bool_t NewState) { - if(NewState == RT_TRUE) - { - //TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64)); - reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64)); - } - else - { - //TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64))); - reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64)); - } + if(NewState == RT_TRUE) + { + //TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64)); + reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64)); + } + else + { + //TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64))); + reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64)); + } } @@ -342,29 +329,29 @@ void ls1c_touch_irqhandler(void) /* TouchScreen */ { if(gpio_get(TOUCH_INT_PIN)==0) { - /* ���������º���� */ - if (gpio_level_low == gpio_get(led_gpio)) - gpio_set(led_gpio, gpio_level_high); - else - gpio_set(led_gpio, gpio_level_low); - touch_int_cmd(RT_FALSE); - rt_event_send(&touch->event, 1); + /* 触摸屏按下后操作 */ + if (gpio_level_low == gpio_get(led_gpio)) + gpio_set(led_gpio, gpio_level_high); + else + gpio_set(led_gpio, gpio_level_low); + touch_int_cmd(RT_FALSE); + rt_event_send(&touch->event, 1); } } -/*�ܽų�ʼ���������жϴ�SPI1 CS0 �豸*/ +/*管脚初始化,配置中断打开SPI1 CS0 设备*/ rt_inline void touch_init(void) -{ - unsigned int touch_int_gpio = TOUCH_INT_PIN; // �������ж� +{ + unsigned int touch_int_gpio = TOUCH_INT_PIN; // 触摸屏中断 int touch_irq = LS1C_GPIO_TO_IRQ(touch_int_gpio); - // ��ʼ�������ж� + // 初始化按键中断 gpio_set_irq_type(touch_int_gpio, IRQ_TYPE_EDGE_FALLING); rt_hw_interrupt_install(touch_irq, ls1c_touch_irqhandler, RT_NULL, "touch"); rt_hw_interrupt_umask(touch_irq); gpio_init(touch_int_gpio, gpio_mode_input); - // ��ʼ��led + // 初始化led gpio_init(led_gpio, gpio_mode_output); gpio_set(led_gpio, gpio_level_high); } @@ -374,9 +361,9 @@ rt_inline void touch_init(void) static rt_err_t rtgui_touch_init (rt_device_t dev) { rt_uint8_t send; - rt_uint8_t recv_buffer[2]; + rt_uint8_t recv_buffer[2]; struct rtgui_touch_device * touch_device = (struct rtgui_touch_device *)dev; - + touch_init(); rt_kprintf("touch_init ...\n"); send = START | DIFFERENTIAL | POWER_MODE0; @@ -433,7 +420,7 @@ static void touch_thread_entry(void *parameter) while(1) { - /* ���յ������ж��¼� */ + /* 接收到触摸中断事件 */ if(rt_event_recv(&touch->event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, @@ -445,7 +432,7 @@ static void touch_thread_entry(void *parameter) { if (IS_TOUCH_UP()) { - /* ������̧�� */ + /* 触摸笔抬起 */ /* touch up */ emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP); @@ -461,14 +448,14 @@ static void touch_thread_entry(void *parameter) if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL)) { - /* ����У���� */ + /* 触摸校准处理 */ /* callback function */ touch->calibration_func(emouse.x, emouse.y); - + } else { - /* ��ui���ʹ������� */ + /* 向ui发送触摸坐标 */ rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); } rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y); @@ -503,7 +490,7 @@ static void touch_thread_entry(void *parameter) emouse.x = touch->x; emouse.y = touch->y; _set_mouse_position(emouse.x, emouse.y); - /* ������ */ + /* 光标跟随 */ /* init mouse button */ emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN); @@ -519,7 +506,7 @@ static void touch_thread_entry(void *parameter) { touch_previous.x = touch->x; touch_previous.y = touch->y; - /* ��ui���ʹ������� */ + /* 向ui发送触摸坐标 */ rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); if(touch_down == RT_FALSE) { @@ -545,24 +532,24 @@ static void touch_thread_entry(void *parameter) rt_err_t rtgui_touch_hw_init(const char * spi_device_name) { - rt_uint32_t arg[2]; - struct rt_device * spi_device; - struct rt_thread * touch_thread; - rt_err_t err; + rt_uint32_t arg[2]; + struct rt_device * spi_device; + struct rt_thread * touch_thread; + rt_err_t err; - rt_kprintf("spi1 cs0 start...\n"); - spi_device = rt_device_find("spi10"); - if(spi_device == RT_NULL) - { - rt_kprintf("Did not find spi1, exit thread....\n"); - return; - } - err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR); - if(err != RT_EOK) - { - rt_kprintf("Open spi1 failed %08X, exit thread....\n", err); - return; - } + rt_kprintf("spi1 cs0 start...\n"); + spi_device = rt_device_find("spi10"); + if(spi_device == RT_NULL) + { + rt_kprintf("Did not find spi1, exit thread....\n"); + return; + } + err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR); + if(err != RT_EOK) + { + rt_kprintf("Open spi1 failed %08X, exit thread....\n", err); + return; + } /* config spi */ { @@ -573,7 +560,7 @@ rt_err_t rtgui_touch_hw_init(const char * spi_device_name) rt_spi_configure((struct rt_spi_device *)spi_device, &cfg); } - touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device)); + touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device)); if (touch == RT_NULL) return RT_ENOMEM; /* no memory yet */ /* clear device structure */ @@ -597,7 +584,7 @@ rt_err_t rtgui_touch_hw_init(const char * spi_device_name) /* register touch device to RT-Thread */ rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR); - + touch_thread = rt_thread_create("touch_thread", touch_thread_entry, RT_NULL, diff --git a/bsp/ls1cdev/drivers/touch.h b/bsp/ls1cdev/drivers/touch.h index 0892d2858f..5dac833510 100644 --- a/bsp/ls1cdev/drivers/touch.h +++ b/bsp/ls1cdev/drivers/touch.h @@ -1,21 +1,7 @@ /* - * File : touch.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes |
