0 设备
创龙C6657+Artix-7工业评估板 SEED-XDS560v2
1 实现功能
控制评估底板 LED1 、 LED2 、 LED3 每隔 0.5s 将状态同时翻转一次。
采用查询的方式控制评估版的KEY2控制LED2亮灭。
2 代码
2.1 GPIO驱动
2.1.1 c66x_gpio.c
/* Compiler Header files */
#include <stdint.h>/* CSL Header file */
#include <ti/csl/cslr_gpio.h>
#include <ti/csl/csl_gpio.h>
#include <ti/csl/csl_gpioAux.h>#include "c66x_gpio.h"/*功能:设置指定GPIO引脚的方向(输入或输出)参数:gpio_num - GPIO引脚编号,direction - GPIO方向(GPIO_OUT 或 GPIO_IN)*/
void gpio_set_direction(uint32_t gpio_num, gpio_direction direction) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);if (direction == GPIO_OUT) {/* Set gpio pin as output mode */CSL_GPIO_setPinDirOutput(h_gpio, gpio_num);} else {/* Set gpio pin as input mode */CSL_GPIO_setPinDirInput(h_gpio, gpio_num);}
}/*功能:设置GPIO[7:0](即GPIO0至GPIO7)的方向参数:direction - GPIO方向(GPIO_OUT 或 GPIO_IN)*/
void gpio_set_databus_direction(gpio_direction direction) {uint32_t pin_num;for (pin_num = GPIO_0; pin_num <= GPIO_7; pin_num++) {/* Set gpio pin as output/input mode */gpio_set_direction(pin_num, direction);}
}/*功能:将指定的GPIO引脚状态设置为高电平(1)参数:gpio_num - GPIO引脚编号注意:GPIO引脚必须配置为输出模式*/
void gpio_set_output(uint32_t gpio_num) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);CSL_GPIO_setOutputData(h_gpio, gpio_num);
}/*功能:将指定的GPIO引脚状态设置为低电平(0)参数:gpio_num - GPIO引脚编号注意:GPIO引脚必须配置为输出模式*/
void gpio_clear_output(uint32_t gpio_num) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);CSL_GPIO_clearOutputData(h_gpio, gpio_num);
}/*功能:读取指定GPIO引脚的输入状态参数:gpio_num - GPIO引脚编号返回值:GPIO引脚的输入状态注意:GPIO引脚必须配置为输入模式*/
uint8_t gpio_read_input(uint32_t gpio_num) {uint8_t inData = 0;CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);CSL_GPIO_getInputData(h_gpio, gpio_num, &inData);return inData;
}/*功能:向GPIO[7:0]写入一个8位的值参数:val - 要写入的8位值注意:GPIO[7:0]必须配置为输出模式*/
void gpio_write_databus(uint8_t val) {uint32_t pin_num;uint8_t value;for (pin_num = GPIO_0; pin_num <= GPIO_7; pin_num++) {value = (val >> pin_num) & 0x1;if (value == GPIO_HIGH) {gpio_set_output(pin_num);} else {gpio_clear_output(pin_num);}}
}/*功能:从GPIO[7:0]读取一个8位的值返回值:GPIO[7:0]的输入状态注意:GPIO[7:0]必须配置为输入模式*/
uint8_t gpio_read_databus(void) {uint32_t pin_num;uint8_t value, bitval;/* initialize variables */value = 0;for (pin_num = GPIO_0; pin_num <= GPIO_7; pin_num++) {bitval = gpio_read_input(pin_num);value |= bitval << pin_num;}return value;
}/*功能:使能GPIO全局中断到CPU*/
void gpio_enable_global_interrupt(void) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);/* GPIOREGS->BINTEN |= 0x01 */CSL_GPIO_bankInterruptEnable(h_gpio, GPIOBANKNUM);
}/*功能:禁用GPIO全局中断到CPU*/
void gpio_disable_global_interrupt(void) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);/* GPIOREGS->BINTEN = 0x00 */CSL_GPIO_bankInterruptDisable(h_gpio, GPIOBANKNUM);
}/*功能:设置指定GPIO引脚的上升沿中断参数:gpio_num - GPIO引脚编号*/
void gpio_set_risingedge_interrupt(uint32_t gpio_num) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);/* GPIOREGS->SET_RIS_TRIG |= (1 << gpio_num) */CSL_GPIO_setRisingEdgeDetect(h_gpio, gpio_num);
}/*功能:清除指定GPIO引脚的上升沿中断参数:gpio_num - GPIO引脚编号*/
void gpio_clear_risingedge_interrupt(uint32_t gpio_num) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);/* GPIOREGS->CLR_RIS_TRIG |= (1 << gpio_num) */CSL_GPIO_clearRisingEdgeDetect(h_gpio, gpio_num);
}/*功能:设置指定GPIO引脚的下降沿中断参数:gpio_num - GPIO引脚编号*/
void gpio_set_fallingedge_interrupt(uint32_t gpio_num) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);/* GPIOREGS->SET_FAL_TRIG |= (1 << gpio_num) */CSL_GPIO_setFallingEdgeDetect(h_gpio, gpio_num);
}/*功能:清除指定GPIO引脚的下降沿中断参数:gpio_num - GPIO引脚编号*/
void gpio_clear_fallingedge_interrupt(uint32_t gpio_num) {CSL_GpioHandle h_gpio;/* Open the CSL GPIO Module 0 */h_gpio = CSL_GPIO_open(0);/* GPIOREGS->CLR_FAL_TRIG |= (1 << gpio_num) */CSL_GPIO_clearFallingEdgeDetect(h_gpio, gpio_num);
}
2.1.2 c66x_gpio.h
#ifndef C66X_GPIO_H_
#define C66X_GPIO_H_#define GPIO_0 (0)
#define GPIO_1 (1)
#define GPIO_2 (2)
#define GPIO_3 (3)
#define GPIO_4 (4)
#define GPIO_5 (5)
#define GPIO_6 (6)
#define GPIO_7 (7)
#define GPIO_8 (8)
#define GPIO_9 (9)
#define GPIO_10 (10)
#define GPIO_11 (11)
#define GPIO_12 (12)
#define GPIO_13 (13)
#define GPIO_14 (14)
#define GPIO_15 (15)
#define GPIO_16 (16)
#define GPIO_17 (17)
#define GPIO_18 (18)
#define GPIO_19 (19)
#define GPIO_20 (20)
#define GPIO_21 (21)
#define GPIO_22 (22)
#define GPIO_23 (23)
#define GPIO_24 (24)
#define GPIO_25 (25)
#define GPIO_26 (26)
#define GPIO_27 (27)
#define GPIO_28 (28)
#define GPIO_29 (29)
#define GPIO_30 (30)
#define GPIO_31 (31)#define GPIOBANKNUM (0)#define GPIO_LOW (0)
#define GPIO_HIGH (1)
#define INVALID_GPIO_NUMBER (2)
#define INVALID_GPIO_DIRECTION (3)
#define INVALID_GPIO_STATE (4)typedef enum _gpio_direction {GPIO_OUT = 0, ///< gpio output modeGPIO_IN ///< gpio input mode
} gpio_direction;/* ================ Function declarations ================ */
void gpio_set_direction(uint32_t gpio_num, gpio_direction direction);
void gpio_set_databus_direction(gpio_direction direction);
void gpio_set_output(uint32_t gpio_num);
void gpio_clear_output(uint32_t gpio_num);
uint8_t gpio_read_input(uint32_t gpio_num);
void gpio_write_databus(uint8_t val);
uint8_t gpio_read_databus(void);
void gpio_enable_global_interrupt(void);
void gpio_disable_global_interrupt(void);
void gpio_set_risingedge_interrupt(uint32_t gpio_num);
void gpio_clear_risingedge_interrupt(uint32_t gpio_num);
void gpio_set_fallingedge_interrupt(uint32_t gpio_num);
void gpio_set_fallingedge_interrupt(uint32_t gpio_num);
void gpio_clear_fallingedge_interrupt(uint32_t gpio_num);#endif /* #ifndef C66X_GPIO_H_ */
2.2 cmd文件
-heap 0x4000/* 16KB */
-stack 0x4000/* 16KB */MEMORY
{/* Local L2, 0.5~1MB*/VECTORS: o = 0x00800000 l = 0x00000200 LL2_RW_DATA: o = 0x00800200 l = 0x0003FE00 /* Shared L2 1MB for C6657*/SL2: o = 0x0C000000 l = 0x00100000DDR3: o = 0x80000000 l = 0x40000000 /* 1GB DDR3 */EMIF16_DATA: o = 0x70000000 l = 0x10000000 /* EMIF16 memory space */
}SECTIONS
{vecs > VECTORS .text > LL2_RW_DATA.cinit > LL2_RW_DATA.const > LL2_RW_DATA.switch > LL2_RW_DATA.stack > LL2_RW_DATAGROUP{.neardata.rodata.bss} > LL2_RW_DATA.far:testBuf > LL2_RW_DATA.far > LL2_RW_DATA.fardata > LL2_RW_DATA.cio > LL2_RW_DATA.sysmem > LL2_RW_DATA}
2.3 main.c
2.3.1 LED123亮灭
/* Compiler Header files */
#include <stdint.h>
/* CSL Header file */
#include <ti/csl/csl_chipAux.h>
/* Driver utilities include */
#include "driver/c66x_gpio.h"#define PIN_CONTROL_0 0x02620580 // GPIO控制寄存器基地址
#define C665X_USER_LED0 GPIO_23 // c665x user-led0, GPIO_23
#define C665X_USER_LED1 GPIO_22 // c665x user-led1, GPIO_22
#define C665X_USER_LED2 GPIO_19 // c665x user-led2, GPIO_19//基于CPU周期的延迟函数,500000000=500ms
void cpu_delaycycles(uint32_t cycles) {uint32_t start_val;/* Start TCSL so its free running */CSL_chipWriteTSCL(0);start_val = CSL_chipReadTSCL();while ((CSL_chipReadTSCL() - start_val) < cycles);return;
}int main(void) {/* Set pin as GPIO mode */*((uint32_t *) PIN_CONTROL_0) |= ((1 << C665X_USER_LED0)|(1 << C665X_USER_LED1) |(1 << C665X_USER_LED2));/* Set GPIO as output mode */gpio_set_direction(C665X_USER_LED0, GPIO_OUT);gpio_set_direction(C665X_USER_LED1, GPIO_OUT);gpio_set_direction(C665X_USER_LED2, GPIO_OUT);while (1) {/* Gpio output low level,turn off LED */gpio_set_output(C665X_USER_LED0);gpio_set_output(C665X_USER_LED1);gpio_set_output(C665X_USER_LED2);/* Keep the LED on for 500 ms */cpu_delaycycles(500000000);/* Gpio output high level,Turn on LED */gpio_clear_output(C665X_USER_LED0);gpio_clear_output(C665X_USER_LED1);gpio_clear_output(C665X_USER_LED2);/* Keep the LED off for 500 ms */cpu_delaycycles(500000000);}
}
2.3.2 KEY控制LED亮灭
#include <stdio.h>
/* Compiler Header files */
#include <stdint.h>
/* CSL Header file */
#include <ti/csl/csl_chipAux.h>
#include <ti/csl/src/intc/csl_intc.h>/* Driver utilities include */
#include "driver/c66x_gpio.h"#define PIN_CONTROL_0 0x02620580 // GPIO控制寄存器基地址
#define LED1 GPIO_19
#define LED2 GPIO_22
#define LED3 GPIO_23
#define KEY2 GPIO_0//基于CPU周期的延迟函数,100000000=100ms
void cpu_delaycycles(uint32_t cycles) {uint32_t start_val;/* Start TCSL so its free running */CSL_chipWriteTSCL(0);start_val = CSL_chipReadTSCL();while ((CSL_chipReadTSCL() - start_val) < cycles);return;
}int main(void) {/* Set pin as GPIO mode */*((uint32_t *) PIN_CONTROL_0) |= ((1 << LED1)|(1 << LED2) |(1 << KEY2 ) |(1 << LED3));/* Set GPIO as output mode */gpio_set_direction(LED1, GPIO_OUT);gpio_set_direction(LED2, GPIO_OUT);gpio_set_direction(LED3, GPIO_OUT);gpio_set_direction(KEY2, GPIO_IN);while (1) {if (gpio_read_input(KEY2) == GPIO_LOW) //检测到按键按下{cpu_delaycycles(100000000); //延时消抖100msif (gpio_read_input(KEY2) == GPIO_LOW)//按键真的被按下了{if (gpio_read_input(LED3) == GPIO_LOW) //LED3翻转gpio_set_output(LED3);//GPIO23设置为高电平,LED3熄灭elsegpio_clear_output(LED3);//GPIO23设置为低电平,LED3点亮}while (gpio_read_input(KEY2) == GPIO_LOW); //阻塞,直到按键被松开}}
}