/*
* www.linksprite.com
* 20/05/2015
*/
#include "stm8l10x.h"
#include "stdio.h"
#include "string.h"
#define LED_ON() GPIO_ResetBits( GPIOC, GPIO_Pin_4 )
#define LED_OFF() GPIO_SetBits( GPIOC, GPIO_Pin_4 )
#define key GPIOB,GPIO_Pin_1
// CE_BIT: Digital Input Chip Enable Activates RX or TX mode
/**
* @addtogroup USART_Printf
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void USART_Config(void);
void ESP8266_Start(void);
void delay(unsigned int time);
void TCPTEST(void);
void TCPSend(char* senddata);
int Send_Command(char* ATcommand,char* ReturnValue,unsigned long time_out);
void low_gpio(void);
int i=0,j=0;
static int doorflag = 0;
@far @interrupt void NonHandledInterrupt (void)
{
EXTI_ClearITPendingBit(EXTI_Pin_1);
if( (GPIO_ReadInputDataBit(key)==0)&&(doorflag==0) )
{
LED_ON();
TCPSend("OPEN");
doorflag = 1;
}
if( (GPIO_ReadInputDataBit(key)) && (doorflag==1) )
{
LED_OFF();
doorflag=0;
TCPSend("CLOS");
}
}
void main(void)
{
int flag=0;
USART_Config();
delay(2);
low_gpio();
GPIO_Init(GPIOB,GPIO_Pin_1, GPIO_Mode_In_PU_IT);
EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Rising_Falling);
GPIO_Init( GPIOC, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Slow);
LED_ON();
USART_ClearFlag();
enableInterrupts();
flag = Send_Command("AT","OK",50);
flag = Send_Command("AT+RST","OK",20);
flag = Send_Command("AT+CWMODE=1","OK",50);
flag = Send_Command("AT+CWJAP=\"ssid\",\"password\"","OK",50);
flag = Send_Command("AT+CWJAP?","OK",50);
flag = Send_Command("AT+CIPSTART=\"TCP\",\"192.168.1.155\",10000","OK",50);
LED_OFF();
while(1) halt();
}
void delay(unsigned int time)
{
while(time--)
{
for(i=0;i<50;i++)
{
for(j=0;j<2000;j++);
}
}
}
void TCPSend(char* senddata)
{
Send_Command("AT+CIPSEND=4",">",50);
Send_Command(senddata,"OK",50);
//delay(1);
}
void TCPTEST(void)
{
static int led=0;
int answer=0, count=0;
char receive[30]=" ";
unsigned long num=0;
char rec='';
char sw= '';
do{
if(USART->SR & USART_SR_RXNE)
{
rec = USART->DR; //USART_ReceiveData8();
receive[count] = rec ;
count++;
USART_ClearFlag();
}
j++;
if(j==1000)
{
j=0;
num++;
}
if(num>1)
{
answer=0;
break;
}
}
while(strstr(receive,"OK")==NULL);
if( (strstr(receive,"GO")!=NULL)||(strstr(receive,"33")!=NULL) ) led++;
if(led%2) { LED_ON();}
else {LED_OFF();}
memset(receive,'\0',30);
count = 0;
}
int Send_Command(char* ATcommand,char* ReturnValue,unsigned long time_out)
{
int answer=0, count=0;
char receive[100]=" ";
unsigned long num=0;
char rec='';
if (ATcommand[0] != '\0')
{
printf(ATcommand); // Send the AT command
printf("\r\n");
}
do{
if(USART->SR & USART_SR_RXNE)
{
rec = USART->DR;
receive[count] = rec ;
count++;
USART_ClearFlag();
}
j++;
if(j==1000)
{
j = 0;
num++;
if(num>time_out)
{
answer=0;
break;
}
}
}
while(strstr(receive,ReturnValue)==NULL);
memset(receive,'\0',100);
count = 0;
answer=1;
return answer;
}
void low_gpio(void)
{
GPIO_Init(GPIOB,GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOB,GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOB,GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOB,GPIO_Pin_4, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOB,GPIO_Pin_5, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOB,GPIO_Pin_6, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOB,GPIO_Pin_7, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOA,GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOA,GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOA,GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOA,GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOC,GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOC,GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOC,GPIO_Pin_4, GPIO_Mode_In_FL_No_IT);
//GPIO_Init(GPIOC,GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
//GPIO_Init(GPIOC,GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);
}
/**
* @brief Configure USART peripheral to print characters on Hyperteminal
* @param None
* @retval None
*/
static void USART_Config(void)
{/* High speed internal clock prescaler: 1*/
CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv8);
/*Set the USART RX and USART TX at high level*/
GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_3|GPIO_Pin_2, ENABLE);
//GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_3|GPIO_Pin_2, DISABLE);
/* Enable USART clock */
CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);
USART_DeInit();
/* USART configuration ------------------------------------------------------*/
/* USART configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Receive and transmit enabled
*/
USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1,
USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));
//USART_ITConfig(USART_IT_RXNE, ENABLE);
/* Enable general interrupts */
// enableInterrupts();
}
/**
* @brief Retargets the C library printf function to the USART.
* @param c Character to send
* @retval char Character sent
*/
PUTCHAR_PROTOTYPE
{
/* Write a character to the USART */
USART_SendData8(c);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);
return (c);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif