black_wind

STM32F407 uSART unable to receive

Feb 25th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.15 KB | None | 0 0
  1.  
  2.  
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. #include "stm32f4xx.h"
  6. #include "stm32f4xx_gpio.h"
  7. #include "stm32f4xx_tim.h"
  8. #include "stm32f4xx_rcc.h"
  9. #include "stm32f4xx_usart.h"
  10. #include "misc.h"
  11. #include "string.h"
  12.  
  13. #define CR 0x0D
  14. #define LR 0x0A
  15. #define ITM_Port8(n)    (*((volatile unsigned char *)(0xE0000000+4*n)))
  16. #define ITM_Port16(n)   (*((volatile unsigned short*)(0xE0000000+4*n)))
  17. #define ITM_Port32(n)   (*((volatile unsigned long *)(0xE0000000+4*n)))
  18. #define DEMCR           (*((volatile unsigned long *)(0xE000EDFC)))
  19. #define TRCENA          0x01000000
  20. #define LINEMAX 300
  21.  
  22.  
  23. struct __FILE
  24. {
  25. int dummy;
  26. };
  27. FILE __stdout;
  28. FILE __stdin;
  29. int fputc(int ch,FILE *f)
  30. {
  31.      if (DEMCR & TRCENA) {
  32.     while (ITM_Port32(0) == 0);
  33.     ITM_Port8(0) = ch;}
  34.     return(ch);
  35. }
  36.  
  37. /*int putcharx(int ch)
  38. {
  39.   while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
  40.   USART_SendData(USART2, (uint8_t)ch);
  41.   return ch;
  42. } */
  43.  
  44. unsigned char payload[40] = {0x40,0xD8,0x6B,0x06,0x40,0xE8,0xB3,0x62,0xB1,0x01,0x00,0x01,0x00,0x01,0x01,0x00,0x02,0x01,0x00,0x03,0x01,0x00,0x04,0x01,0x00,0x05,0x01,0x00,0x06,0x01,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  45. volatile char line_buffer[LINEMAX+1];
  46. volatile int Line_valid=0;
  47.  
  48.  
  49. uint8_t buffer1[50];
  50. uint8_t buffer2[50];
  51. uint8_t buffer3[50];
  52. uint8_t buffer_OK[50];
  53. uint8_t buffer_creg[50];
  54. unsigned char ch1;
  55.  
  56. unsigned char end_flag=0;
  57. unsigned int read_data=0;
  58. unsigned int  count=0;
  59. unsigned char  gsm_rx=0;
  60. unsigned char rx_zigbee=0;
  61. unsigned int  cmd=4;
  62. uint8_t gsm_buf_index=0;
  63. unsigned int rx_index2=0;
  64. unsigned int zigbee_receive_flag=0;
  65. unsigned int pTxData[24];
  66. unsigned char GSM_char[]="00";
  67. unsigned char gsm_buf[LINEMAX];
  68. unsigned char rx_buf_zigbee[LINEMAX];
  69. unsigned char write_buf_pointer=0;
  70. unsigned char server_tx_buf[128];
  71. uint8_t gsm_attempt=0;
  72. int i;
  73. uint8_t global_cell_id=0;  
  74. uint8_t check_gprs=0;
  75. uint8_t set_apn=0;
  76. uint8_t wireless_ok=0;
  77. uint8_t zigbee_flag=0;
  78. uint8_t gsm_flag=0;
  79. char urc_code;
  80. char network_status;
  81. extern __IO uint32_t TimmingDelay;
  82.  
  83. USART_InitTypeDef USART_InitStructure;
  84. NVIC_InitTypeDef NVIC_struct;
  85.  
  86. void Delay_sec(__IO uint32_t time);
  87. void GPIOInitialize(void);
  88. void UART_Initialize(void);
  89. void NVICInitialize(void);
  90. void TIMER_Initialize(void);
  91. void InitializeLED(void);
  92. void USART_puts(volatile char *s);
  93. void extract_Data(char *s1, char *s2);
  94. void delay_routine(uint16_t j);
  95. void USART_SendPayload(void);
  96. void gateway_to_zigbee(void);
  97. void GSM_Send_Command(unsigned char *s1,unsigned char *s2,const unsigned char *s3, const unsigned char *s4, unsigned char *id, unsigned char *gsm_attempt, unsigned char clr);
  98. void received_response_80(unsigned char *s1,unsigned char *s2);
  99. void transmit_response_80(unsigned char *s1,unsigned char *s2);
  100. void request_To_server(const char *string);
  101. void addCharToBuffer(char get_data);
  102. void Blinker(unsigned int val);
  103. void Indication_On(unsigned int val);
  104. const char *CopyConst(char *dest, const char *src);
  105. uint8_t Network_Reg(unsigned char * command,unsigned char *response);
  106. uint8_t Fetch_local_IP(unsigned char *response);
  107. uint8_t GET_Cell_ID(void);
  108. uint8_t Set_APN_Name(unsigned char *response);
  109. uint8_t Check_GPRS(unsigned char * command,unsigned char *response);
  110. const char at_command1[] = "AT\r";
  111. const char at_command2[] = "AT+CREG?\r";
  112. const char at_command3[] = "AT+CGATT=1\r";
  113. const char at_command4[] = "AT+CIPSHUT";
  114. const char at_CSTT[] = "AT+CSTT=";
  115. const char APN_Name[]="\"airtelgprs.com\"\r";
  116. const char at_command6[] = "AT+CIICR";
  117. const char at_command7[] = "AT+CIPSTART=\"TCP\",\"52.25.26.236\",\"80\"";
  118. const char at_command8[] = "AT+CIPSEND\r";
  119. const char get_request[] = "\"GET/52.25.26.236 /test1.php?name=$$";//GET/52.25.26.236 /test1.php?name=ravindra kant HTTP1.1\r\n
  120.  
  121. const char at_commandN[] = "AT+CREG=0\r";
  122.  
  123. const char HTTP[]        = "HTTP1.1\r\n\"";
  124. const char HOST_IP[]     = "\"HOST: 52.25.26.236\r\n\n\"";
  125.  
  126. const char at_response1[] = "\r\nOK\r\n";
  127. const char at_response2[] = "\r\n+CREG: 1,5\r\n\r\nOK\r\n";
  128. const char at_response3[] = "\r\n+CREG: 0,5\r\n\r\nOK\r\n";
  129. const char at_response4[] = "\r\nSHUT OK\r\n";
  130. const char at_response5[] = "ERROR";
  131. const char at_response6[] ="\r\n> ";
  132.  
  133. /*******************************************
  134. *IRQ handler for USART1 STM32 port B PB6,PB7
  135. *AHB perpherals line 1 and APB bus 1
  136. *Modules Connected GSM
  137. ********************************************/
  138.  
  139. void USART1_IRQHandler()
  140. {  
  141.  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //it checks receive complete interrupt(USART_IT_RXNE) on USARTx
  142.  {
  143.      gsm_rx = USART_ReceiveData(USART1);
  144.        gsm_buf[gsm_buf_index++]=gsm_rx;
  145.        gsm_buf[gsm_buf_index]='\0';
  146.  }
  147.      
  148. }
  149.  
  150. /*******************************************
  151. *IRQ handler for USART2 STM32 port D PD5,PD6
  152. *AHB perpherals line 1 and APB bus 2
  153. *Modules Connected Arduino
  154. ********************************************/
  155.  
  156. void USART2_IRQHandler()
  157. {
  158.     if(USART_GetITStatus(USART2,USART_IT_RXNE)!= RESET)
  159.     {
  160.         rx_zigbee = USART_ReceiveData(USART2);
  161.             rx_buf_zigbee[rx_index2++]=rx_zigbee;
  162.         zigbee_flag=1;
  163.    
  164.     }
  165. }
  166. void TIM2_IRQHandler()
  167. {
  168.     if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET) //checking interrupt status
  169.      {
  170.         TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  171.          GPIO_ToggleBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13);
  172.      }
  173. }  
  174. /*******************************************
  175. *Delay Routine for 1ms
  176. *frequecy of oscillator is turned down to 168,000
  177. *from 168 MHz
  178. *********************************************/
  179. void Delay_sec(__IO uint32_t time)
  180. {
  181.   TimmingDelay = time;
  182.   while(TimmingDelay !=0);
  183.     //TimmingDelay--;
  184. }  
  185.  
  186. void TIMER_Initialize(void)
  187. {
  188.     TIM_TimeBaseInitTypeDef timerInitStructure;
  189.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //APB1
  190.    
  191.     timerInitStructure.TIM_Prescaler=40000;
  192.     timerInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  193.     timerInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
  194.     timerInitStructure.TIM_Period=10000;
  195.     timerInitStructure.TIM_RepetitionCounter=0;
  196.     TIM_TimeBaseInit(TIM2,&timerInitStructure);
  197.     TIM_Cmd(TIM2,ENABLE);
  198.     TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //timer interrupt
  199. }
  200.  
  201. /**************************************************
  202. *Initialization of USART 1
  203. *Baudrate=9600
  204. *APB 2
  205. *AHB 1
  206. *Data Format 8N1
  207. *No Hardware flow control (required RTS and CTS)
  208. **************************************************/
  209.  
  210. void UART_Initialize(void)
  211. {
  212.    
  213.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  214.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  215.    
  216.  USART_InitStructure.USART_BaudRate = 9600;
  217.  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  218.  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  219.  USART_InitStructure.USART_Parity = USART_Parity_No;
  220.  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  221.  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  222.    
  223.  USART_Init(USART1, &USART_InitStructure); // USART configuration
  224. // USART_Init(USART2, &USART_InitStructure); // USART configuration
  225.    
  226.  USART_Cmd(USART1, ENABLE); // Enable USART
  227. // USART_Cmd(USART2, ENABLE); // Enable USART  
  228.    
  229.  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  230. // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  231. }
  232. /**************************************************
  233. *Initialization of GPIO
  234. *Pin no 5 and 6 for PORT D
  235. *Pin no 6 and 7 for Port B
  236. *Output Type PushPull
  237. *Speed 100MHz
  238. **************************************************/
  239. void GPIOInitialize(void)
  240. {
  241.   GPIO_InitTypeDef GPIO_InitStructure;
  242.    
  243.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable clock for GPIOB
  244.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  245.  
  246.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7  ;
  247.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2 | GPIO_Pin_3  ;
  248.  
  249.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  250.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  251.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  252.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  253.  
  254.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  255.   GPIO_Init(GPIOA, &GPIO_InitStructure);   
  256.  
  257.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);//Connect PB6 to USART1_Tx
  258.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);//Connect PB7 to USART1_Rx
  259.    
  260.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);//connect Pa2 to USART2_Tx
  261.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);//Connect Pa3 to USART2_Rx
  262.  
  263. }
  264.  
  265. void InitializeLED(void)  //led initialize
  266. {
  267.     GPIO_InitTypeDef GPIO_InitDef;
  268.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  269.   /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  270.   GPIO_InitDef.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  271.   GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
  272.   GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  273.   GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
  274.   GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
  275.   GPIO_Init(GPIOD, &GPIO_InitDef);
  276. }
  277. /**************************************************
  278. *Nested Vectored Interrupt Controller
  279. *Baudrate=9600
  280. *APB 2
  281. *AHB 1
  282. *Data Format 8N1
  283. *No Hardware flow control (required RTS and CTS)
  284. **************************************************/
  285. void NVICInitialize(void)
  286. {
  287.    NVIC_struct.NVIC_IRQChannel = USART2_IRQn;  //USART2 priority
  288.    NVIC_struct.NVIC_IRQChannelPreemptionPriority = 0;
  289.    NVIC_struct.NVIC_IRQChannelSubPriority = 1;
  290.    NVIC_struct.NVIC_IRQChannelCmd = ENABLE;
  291.    NVIC_Init(&NVIC_struct);
  292.    
  293.  //NVIC_struct.NVIC_IRQChannel = TIM2_IRQn;
  294.  
  295.      NVIC_struct.NVIC_IRQChannel = USART1_IRQn; //USART1 priority
  296.    NVIC_struct.NVIC_IRQChannelPreemptionPriority = 0;
  297.    NVIC_struct.NVIC_IRQChannelSubPriority = 0;
  298.    NVIC_struct.NVIC_IRQChannelCmd = ENABLE;
  299.    NVIC_Init(&NVIC_struct);
  300.    
  301.  
  302.  
  303. }
  304.  
  305. void USART_puts(volatile char *s)
  306. {
  307.     //printf("%c",(volatile char *)&s[0]);
  308.  while(*s)
  309.     {
  310.      while( !(USART1->SR & 0x00000040) );
  311.      USART_SendData(USART1, *s);
  312.      *s++;
  313.     }
  314. }
  315.  
  316. void receive_response_80(unsigned char *s1,unsigned char *s2)
  317. {
  318.     uint8_t i=0;
  319.     while(*s1++)
  320.     {
  321.         if(*s1 =='\n')
  322.           i++;
  323.     if(i==11)
  324.      break;    
  325.     }
  326.     while(*s1 && (*s1!='\n'))
  327.     {
  328.         *s2++ = *s1++;
  329.     }
  330.     *s2='\0';
  331.     Delay_sec(200);
  332.   USART_SendData(USART2,*s1);  
  333.     //send received response from server to zigbee
  334. }
  335. void transmit_response_80(unsigned char*s1,unsigned char *s2)
  336. {
  337.     char s3[]=ravindra kant";
  338.     //if(zigbee_flag & 1)
  339. /// {
  340.    // zigbee_flag=0;  
  341.         s1 = &rx_buf_zigbee[0];
  342.    while(*s1)
  343.       {
  344.         *s2++=*s1;
  345.    }
  346.         *s2='\0';
  347. //}
  348.    
  349.     Delay_sec(200);
  350.     USART_puts("AT+CIPSTART=\"TCP\",\"52.25.26.236\",\"80\"\r");//USART_puts(&TCP_request[0]);
  351.     Delay_sec(3000);
  352.     USART_puts("AT+CIPSEND\r");//USART_puts(&at_command8[0]);
  353.     Delay_sec(1000);
  354.    
  355.     request_To_server(get_request);
  356.     request_To_server(s3); //sending data received from zigbee
  357.     request_To_server(HTTP);
  358.     request_To_server(HOST_IP);
  359.     USART_puts(&server_tx_buf[0]);
  360.     USART_SendData(USART1,0x1A); //end of line is determined by ctrl-Z or 0x1A 
  361.     Delay_sec(2000); //this wait has to be checked later upon requirement only  
  362.  
  363. }
  364. void request_To_server(const char *string)
  365. {
  366.     while(*string)
  367.     {
  368.         addCharToBuffer(*string++);
  369.     }
  370. }
  371. void addCharToBuffer(char get_data)
  372. {
  373.     server_tx_buf[write_buf_pointer++]=get_data;
  374.     if(write_buf_pointer >=128)
  375.     {write_buf_pointer=127;}
  376.    
  377. }
  378. void Indication_On(unsigned int val)
  379. {
  380.     GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_14);
  381.     Delay_sec(val);
  382.     GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_14);
  383. }
  384. void Blinker(unsigned int val)
  385. {
  386.  while(val > 0)
  387.  {
  388.      GPIO_ToggleBits(GPIOD,GPIO_Pin_13 | GPIO_Pin_15);
  389.      val--;
  390.  }
  391. }
  392. void delay_routine(uint16_t j)
  393. {
  394.     while(j!=0)
  395.     {
  396.         j--;
  397.     }
  398. }
  399. int main(void)
  400. {
  401.     uint16_t ch;
  402.  SystemInit();
  403.  GPIOInitialize();
  404.  UART_Initialize();
  405.  //NVICInitialize();
  406.  TIMER_Initialize();
  407.  InitializeLED();
  408.    
  409.  
  410.     SysTick_Config(SystemCoreClock/1000); //normal clock is 168,000
  411.    
  412.  
  413.  
  414.  
  415.     while(1)
  416.     {
  417.        
  418.    
  419.      
  420.         if(USART_ReceiveData(USART1))  //unable to receive anything in normal case
  421.         {GPIO_WriteBit( GPIOD, GPIO_Pin_12, Bit_SET);// GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13);
  422.             Delay_sec(1000);
  423.             GPIO_WriteBit(GPIOD, GPIO_Pin_12, Bit_RESET);}
  424.        
  425.        
  426.        
  427.        
  428.    
  429.    
  430.     }          
  431.  
  432. }
  433.  
  434. #ifdef USE_FULL_ASSERT
  435.  
  436. void assert_failed(uint8_t* file, uint32_t line)
  437. {
  438.  while (1)
  439.  {}
  440. }
  441. #endif
Add Comment
Please, Sign In to add comment