移植了一个串口shell程序nr_micro_shell。顺便测试了I2C接口的读写AT24Cxx。移植很简单,主要在nr_micro_shell_config.h文件内配置,以及串口发送接收函数。主函数main内初始化和接收串口数据处理。
代码如下:
/*************<include>****************/
#include "string.h"
#include "ac780x.h"
#include "system_ac780x.h"
#include "ac780x_gpio.h"
#include "ac780x_uart.h"
#include "elog.h"
#include "nr_micro_shell.h"
#include "i2c.h"
/*************<macro>******************/
#define LED2_PORT (GPIOC)
#define LED2_PIN (GPIO_PIN9)
#define LED3_PORT (GPIOC)
#define LED3_PIN (GPIO_PIN7)
#define LED2_ON() do{GPIO_SetPinLevel(LED2_PORT, LED2_PIN, GPIO_LEVEL_HIGH);}while(0)
#define LED2_OFF() do{GPIO_SetPinLevel(LED2_PORT, LED2_PIN, GPIO_LEVEL_LOW);}while(0)
#define LED2_TOGGLE() do{if(GPIO_GetPinLevel(LED2_PORT, LED2_PIN)){LED2_OFF;}else{LED2_ON;}}while(0)
#define LED3_ON() do{GPIO_SetPinLevel(LED3_PORT, LED3_PIN, GPIO_LEVEL_HIGH);}while(0)
#define LED3_OFF() do{GPIO_SetPinLevel(LED3_PORT, LED3_PIN, GPIO_LEVEL_LOW);}while(0)
#define LED3_TOGGLE() do{if(GPIO_GetPinLevel(LED3_PORT, LED3_PIN)){LED3_OFF;}else{LED3_ON;}}while(0)
/*************<enum>*******************/
/*************<union>******************/
/*************<struct>*****************/
/*************<variable>***************/
/*************<prototype>**************/
void shell_ls_cmd(char argc, char *argv)
{
unsigned int i = 0;
if (argc > 1)
{
if (!strcmp("cmd", &argv[argv[1]]))
{
for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
{
shell_printf(nr_shell.static_cmd[i].cmd);
shell_printf("\r\n");
}
}
else if (!strcmp("-v", &argv[argv[1]]))
{
shell_printf("ls version 1.0.\r\n");
}
else if (!strcmp("-h", &argv[argv[1]]))
{
shell_printf("useage: ls [options]\r\n");
shell_printf("options: \r\n");
shell_printf("\t -h \t: show help\r\n");
shell_printf("\t -v \t: show version\r\n");
shell_printf("\t cmd \t: show all commands\r\n");
}
}
else
{
shell_printf("ls need more arguments!\r\n");
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] test command
*/
void shell_test_cmd(char argc, char *argv)
{
unsigned int i;
shell_printf("test command:\r\n");
for (i = 0; i < argc; i++)
{
shell_printf("paras %d: %s\r\n", i, &(argv[argv[i]]));
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] test command
*/
void shell_led_cmd(char argc, char *argv)
{
if (argc > 1)
{
if (!strcmp("on", &argv[argv[1]]))
{
if (!strcmp("1", &argv[argv[2]]))
{
LED2_ON();
}else if (!strcmp("2", &argv[argv[2]]))
{
LED3_ON();
}else
{
shell_printf("useage: led [on/off] [1/2/3/4]\r\n");
}
}else if (!strcmp("off", &argv[argv[1]]))
{
if (!strcmp("1", &argv[argv[2]]))
{
LED2_OFF();
}else if (!strcmp("2", &argv[argv[2]]))
{
LED3_OFF();
}else
{
shell_printf("useage: led [on/off] [1/2/3/4]\r\n");
}
}else{
shell_printf("useage: led [on/off] [1/2/3/4]\r\n");
}
}
}
void at24cxx_cmd(char argc, char *argv)
{
if (argc > 1)
{
if (!strcmp("read", &argv[argv[1]]))
{
I2C_RdDataFromAT24C();
}else if (!strcmp("write", &argv[argv[1]]))
{
I2C_WrDataToAT24C();
}else{
shell_printf("useage: at24 [read/write] \r\n");
}
}else{
shell_printf("useage: at24 [read/write] \r\n");
}
}
NR_SHELL_CMD_EXPORT(at24, at24cxx_cmd);
#ifdef NR_SHELL_USING_EXPORT_CMD
NR_SHELL_CMD_EXPORT(ls, shell_ls_cmd);
NR_SHELL_CMD_EXPORT(test, shell_test_cmd);
NR_SHELL_CMD_EXPORT(led, shell_led_cmd);
#else
const static_cmd_st static_cmd[] =
{
{"ls", shell_ls_cmd},
{"test", shell_test_cmd},
{"led", shell_led_cmd},
{"\0", NULL}
};
#endif
/**
* [url=home.php?mod=space&uid=555622]@prototype[/url] main(void)
*
* @param[in] void
* @return void
*
* @brief main entry.
* mainº¯Êý.
*/
int main(void)
{
uint8_t ch;
InitDelay(); //sysclk Delay
InitDebug(); //Uart Debug printf
//ÉèÖÃLEDÒý½ÅΪGPIOÊä³ö.
GPIO_SetDir(LED2_PORT, LED2_PIN, GPIO_OUT);
GPIO_SetDir(LED3_PORT, LED3_PIN, GPIO_OUT);
I2C_InitHw();
elog_init();
shell_init(); /* nr_micro_shell*/
while(1)
{
if (UART_RxIsDataReady(UART2))
{
ch = UART_ReceiveData(UART2);
shell(ch);
}
}
}
/*************<end>********************/
I2C程序:
/* Copyright Statement:
*
* This software/firmware and related documentation ("AutoChips Software") are
* protected under relevant copyright laws. The information contained herein is
* confidential and proprietary to AutoChips Inc. and/or its licensors. Without
* the prior written permission of AutoChips inc. and/or its licensors, any
* reproduction, modification, use or disclosure of AutoChips Software, and
* information contained herein, in whole or in part, shall be strictly
* prohibited.
*
* AutoChips Inc. (C) 2018. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
* RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
* ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
* RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
* INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
* TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
* RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
* OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
* SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
* RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
* ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
* RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
* AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
* CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
*/
/*************<start>******************/
/*************<include>****************/
#include "i2c.h"
/*************<macro>******************/
#define AT24C02_DEV_ADDR (0x50)
/*************<enum>*******************/
/*************<union>******************/
/*************<struct>*****************/
/*************<variable>***************/
uint8_t g_wrAT24CDataBuf[9];
uint8_t g_rdAT24CDataBuf[9];
/*************<prototype>**************/
/**
* @prototype I2C_InitHw(void)
*
* @param[in] void
* @return void
*
* @brief ³õʼ»¯I2C.
*/
void I2C_InitHw(void)
{
I2C_ConfigType i2cConfig = {0};
/*³õʼ»¯Òý½Å¹¦ÄÜΪI2C.*/
GPIO_SetFunc(I2C0_SCL_PORT, I2C0_SCL_PIN, GPIO_FUN3);
GPIO_SetFunc(I2C0_SDA_PORT, I2C0_SDA_PIN, GPIO_FUN3);
/*³õʼ»¯I2CÍâÉè.*/
i2cConfig.masterConfigs.ARBEn = ENABLE;/*ÉèÖÃÖ÷»úÖٲù¦ÄÜ.*/
i2cConfig.masterConfigs.SYNCEn = ENABLE;/*ÉèÖÃÖ÷»úSCLͬ²½¹¦ÄÜ.*/
/*ÉèÖò¨ÌØÂÊΪ100Kbps,bandrate = 24M / (10 * 12 * 2) = 100Kbps;*/
i2cConfig.masterConfigs.sampleCnt = 9;
i2cConfig.masterConfigs.stepCnt = 11;
i2cConfig.slaveConfigs.addExtEn = DISABLE;/*½ûÄÜÀ©Õ¹µØÖ·.*/
i2cConfig.slaveConfigs.addRangeEn = DISABLE;/*½ûÄÜ·¶Î§µØÖ·.*/
i2cConfig.slaveConfigs.monitorEn = DISABLE;/*½ûÄÜ´Ó»ú¼à²â¹¦ÄÜ.*/
i2cConfig.slaveConfigs.stretchEn = DISABLE;/*´Ó»úSCLÑÓÉ칦ÄÜ.*/
i2cConfig.slaveConfigs.gcaEn = DISABLE;/*´Ó»úSCL¹ã²¥µØÖ·.*/
i2cConfig.slaveConfigs.wakeupEn = DISABLE;/*»½Ðѹ¦ÄÜ,½ö´Ó»úʱÓÐЧ.*/
i2cConfig.slaveConfigs.RXFInterruptEn = DISABLE;/*½ÓÊÕ»º´æÂúÖжÏ.*/
i2cConfig.slaveConfigs.RXOFInterruptEn = DISABLE;/*½ÓÊÕ»º´æÒç³öÖжÏ.*/
i2cConfig.slaveConfigs.TXEInterruptEn = DISABLE;/*·¢ËÍ»º´æ¿ÕÖжÏ.*/
i2cConfig.slaveConfigs.TXUFInterruptEn = DISABLE;/*·¢ËÍ»º´æÒç³öÖжÏ.*/
i2cConfig.slaveConfigs.slaveAddress = AT24C02_DEV_ADDR;/*´Ó»úµØÖ·.*/
i2cConfig.slaveConfigs.slave7BitRangeAddress = 0;/*´Ó»ú·¶Î§µØÖ·.*/
i2cConfig.glitchFilterCnt = 0;/*ë´Ì¹ýÂË.*/
i2cConfig.interruptEn = DISABLE;/*I2CÈ«¾ÖÖжÏ.*/
i2cConfig.nackInterruptEn = DISABLE;/*NACKÖжÏ.*/
i2cConfig.ssInterruptEn = DISABLE;/*×ÜÏßstart»òstopÖжÏ.*/
i2cConfig.dmaRxEn = DISABLE;/*ÉèÖÃDMA½ÓÊÕÊý¾Ý.*/
i2cConfig.dmaTxEn = DISABLE;/*ÉèÖÃDMA·¢ËÍÊý¾Ý.*/
i2cConfig.mode = I2C_MASTER;/*ÉèÖÃÖ÷´Ó»úģʽ.*/
i2cConfig.i2cEn = ENABLE;/*ʹÄÜÄ£¿é.*/
i2cConfig.callBack = NULL;/*Öжϻص÷º¯Êý.*/
I2C_Init(I2C0, &i2cConfig);
}
/**
* @prototype I2C_WrDataToAT24C(void)
*
* @param[in] void
* @return void
*
* @brief дÊý¾Ýµ½EEPROM.
*/
void I2C_WrDataToAT24C(void)
{
g_wrAT24CDataBuf[0] = 0x00;//дÈëµØÖ·
for (uint8_t ii = 0; ii < 8; ii++)
{
g_wrAT24CDataBuf[ii + 1] = ii+10;
printf("%02X ", g_wrAT24CDataBuf[ii + 1]);
}printf("\r\n");
I2C_MasterBurstWrite(I2C0, AT24C02_DEV_ADDR, g_wrAT24CDataBuf, 9, ENABLE);
}
/**
* @prototype I2C_RdDataFromAT24C(void)
*
* @param[in] void
* @return void
*
* @brief ´ÓEEPROM¶ÁÈ¡Êý¾Ý.
*/
void I2C_RdDataFromAT24C(void)
{
g_wrAT24CDataBuf[0] = 0x00;//¶ÁÈ¡µØÖ·
I2C_MasterBurstWrite(I2C0, AT24C02_DEV_ADDR, g_wrAT24CDataBuf, 1, DISABLE);
I2C_MasterBurstRead (I2C0, AT24C02_DEV_ADDR, g_rdAT24CDataBuf, 8);
for (uint8_t ii = 0; ii < 8; ii++)
{
printf("%02X ", g_rdAT24CDataBuf[ii]);
}
printf("\r\n");
}
/*************<end>********************/
工程代码:
|
共2人点赞
|