有计划地在调一个无线模块,原子的ATK8266,首先这个模块是一个串口wifi模块,通过串口发送指令给wifi模块来决定运行模式,随后进入某种模式中,来连续发送串口的给定的数据。
所以,具体的as函数大体应该为两块
1. 一串连续的串口字符来决定模式 2. 进入连续发送模式。
void atk_8266_at_response(u8 mode)
{ if(USART3_RX_STA&0X8000) //接收到一次数据了
{
USART3_RX_BUF[USART3_RX_STA&0X7FFF]=0;//添加结束符
printf("%s",USART3_RX_BUF); //发送到串口
if(mode)USART3_RX_STA=0; } }
USART3_RX_STA 这个寄存器相当于一个串口接收中断,当一个信号发过来时,这个寄存器的值置高。
我们翻阅数据手册,其实有一个标志位可以表示buffer内暂存的数据内容
Channel_sts_reg0 0x0000002C的第2位,我们通过检测这个标志位,可以得到接收buffer内是否有数的标志,从而实现polled模式下的接收。
接下来贴具体代码:
//按照ATK8266的手册配置串口传输
/***************************** Include Files *********************************/
#include "xparameters.h"
#include "xstatus.h"
#include "xuartlite.h"
#include "xil_printf.h"
#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "sleep.h"
#include "stdlib.h"
#include "xuartlite_i.h"
#define TEST_BUFFER_SIZE 16
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
int UartLitePolledExample(u16 DeviceId);
/************************** Variable Definitions *****************************/
XUartLite UartLite; /* Instance of the UartLite Device */
/*
* The following buffers are used in this example to send and receive data
* with the UartLite.
*/
u8 SendBuffer[TEST_BUFFER_SIZE]; /* Buffer for Transmitting Data */
u8 RecvBuffer[TEST_BUFFER_SIZE]; /* Buffer for Receiving Data */
char CMD_apmode[] = "AT+CWMODE=2";
char CMD_reset[]="AT+RST";
//设置SSID,密码,通道号,加密方式
char CMD_Setapparam[] = "AT+CWSAP=\"ATK-ESP8266\",\"12345678\",1,4";
//开启多连接
char CMD_MultiLink[] = "AT+CIPMUX=1";
char CMD_setServerMode[] = "AT+CIPSERVER=1,8086";
char CMD_SendPackage[] = "AT+CIPSEND=0,25";
int UartLiteInit(u16 DeviceId);
int UartSendString(char *strAddr , int length);
int main(void)
{
int Status;
int u_cnt;
int i;
init_platform();
printf("this is ok \r\n");
/*
* Run the UartLite polled example, specify the Device ID that is
* generated in xparameters.h
*/
Status = UartLiteInit(UARTLITE_DEVICE_ID);
if (Status != XST_SUCCESS) {
xil_printf("Uartlite polled Example Failed\r\n");
return XST_FAILURE;
}
memset(RecvBuffer, 0x00, 16);
/* 按照顺序一条一条发送网络设置指令 */
UartSendString(CMD_reset, strlen(CMD_reset));
usleep(50000);
UartSendString(CMD_apmode, strlen(CMD_apmode));
usleep(50000);
UartSendString(CMD_MultiLink, strlen(CMD_MultiLink));
usleep(50000);
while(1)
{
u_cnt = UartRcvString(RecvBuffer);
usleep(50000);
if(u_cnt){
printf("the num is %d \n",u_cnt );
usleep(5000);
UartSendString(RecvBuffer ,u_cnt );
memset(RecvBuffer, 0x00, 16);
}
}
xil_printf("Successfully ran Uartlite polled Example\r\n");
cleanup_platform();
return XST_SUCCESS;
}
int UartLiteInit(u16 DeviceId)
{
int Status;
unsigned int SentCount;
int Index;
/*
* Initialize the UartLite driver so that it is ready to use.
*/
Status = XUartLite_Initialize(&UartLite, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XUartLite_SelfTest(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
#if 0
while (1) {
ReceivedCount += XUartLite_Recv(&UartLite,
RecvBuffer + ReceivedCount,
TEST_BUFFER_SIZE - ReceivedCount);
if (ReceivedCount == TEST_BUFFER_SIZE) {
break;
}
}
/*
* Check the receive buffer data against the send buffer and verify the
* data was correctly received.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
if (SendBuffer[Index] != RecvBuffer[Index]) {
return XST_FAILURE;
}
}
#endif
return XST_SUCCESS;
}
int UartSendString(char *strAddr , int length)
{
u8 tmp_buffer[20]; //最大发送字长20,多了报错
int Index;
int SentCount;
for (Index = 0; Index < length; Index++) {
tmp_buffer[Index] = (u8)*(strAddr + Index);
}
while(length > 0)
{
if (length < 16)
{
SentCount = XUartLite_Send(&UartLite, tmp_buffer, length);
if (SentCount != length) {
return XST_FAILURE;}
length = 0;
}
else
{
SentCount = XUartLite_Send(&UartLite, tmp_buffer, 16);
if (SentCount != 16) {
return XST_FAILURE;}
SentCount = XUartLite_Send(&UartLite, tmp_buffer+16, length - 16);
if (SentCount != (length -16) ) {
return XST_FAILURE;}
length-=16;
}
}
return XST_SUCCESS;
}
/* 使用串口接收代码时,应该有一个表示 buffer内缓存数量,或者是表示buffer空的标志位*/
int UartRcvString(char *strAddr)
{
int u_cnt = 0;
int StatusRegister;
StatusRegister =
XUartLite_GetStatusReg(UartLite.RegBaseAddress);
while ( StatusRegister & 0x01 )
{
*(strAddr + u_cnt) = XUartLite_ReadReg(UartLite.RegBaseAddress,0x00);
//XUartLite_UpdateStats(&UartLite, StatusRegister);
StatusRegister = XUartLite_GetStatusReg(UartLite.RegBaseAddress);
u_cnt++;
}
return u_cnt;
}
这只是一个相当简略的demo,但是已经可以通过它来配置8266了。