P100脱机下载器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.5 KiB

4 years ago
  1. #include "sys.h"
  2. #include "usart.h"
  3. //////////////////////////////////////////////////////////////////////////////////
  4. //����ʹ��ucos,������������ͷ�ļ�����.
  5. #if SYSTEM_SUPPORT_OS
  6. #include "includes.h" //ucos ʹ��
  7. #endif
  8. //////////////////////////////////////////////////////////////////////////////////
  9. //������ֻ��ѧϰʹ�ã�δ���������ɣ��������������κ���;
  10. //ALIENTEK STM32������
  11. //����1��ʼ��
  12. //����ԭ��@ALIENTEK
  13. //������̳:www.openedv.com
  14. //�޸�����:2012/8/18
  15. //�汾��V1.5
  16. //��Ȩ���У������ؾ���
  17. //Copyright(C) �������������ӿƼ����޹�˾ 2009-2019
  18. //All rights reserved
  19. //********************************************************************************
  20. //V1.3�޸�˵��
  21. //֧����Ӧ��ͬƵ���µĴ��ڲ���������.
  22. //�����˶�printf��֧��
  23. //�����˴��ڽ��������.
  24. //������printf��һ���ַ���ʧ��bug
  25. //V1.4�޸�˵��
  26. //1,�޸Ĵ��ڳ�ʼ��IO��bug
  27. //2,�޸���USART_RX_STA,ʹ�ô������������ֽ���Ϊ2��14�η�
  28. //3,������USART_REC_LEN,���ڶ��崮�������������յ��ֽ���(������2��14�η�)
  29. //4,�޸���EN_USART1_RX��ʹ�ܷ�ʽ
  30. //V1.5�޸�˵��
  31. //1,�����˶�UCOSII��֧��
  32. //////////////////////////////////////////////////////////////////////////////////
  33. //////////////////////////////////////////////////////////////////
  34. //�������´���,֧��printf����,������Ҫѡ��use MicroLIB
  35. #if 1
  36. #pragma import(__use_no_semihosting)
  37. //��׼����Ҫ��֧�ֺ���
  38. struct __FILE
  39. {
  40. int handle;
  41. };
  42. FILE __stdout;
  43. //����_sys_exit()�Ա���ʹ�ð�����ģʽ
  44. _sys_exit(int x)
  45. {
  46. x = x;
  47. }
  48. //�ض���fputc����
  49. int fputc(int ch, FILE *f)
  50. {
  51. while((USART1->SR&0X40)==0);//ѭ������,ֱ����������
  52. USART1->DR = (u8) ch;
  53. return ch;
  54. }
  55. #endif
  56. #if EN_USART1_RX //����ʹ���˽���
  57. //����1�жϷ�������
  58. //ע��,��ȡUSARTx->SR�ܱ���Ī�������Ĵ���
  59. u8 USART_RX_BUF[USART_REC_LEN]; //���ջ���,����USART_REC_LEN���ֽ�.
  60. //����״̬
  61. //bit15�� �������ɱ�־
  62. //bit14�� ���յ�0x0d
  63. //bit13~0�� ���յ�����Ч�ֽ���Ŀ
  64. u16 USART_RX_STA=0; //����״̬����
  65. void uart_init(u32 bound){
  66. //GPIO�˿�����
  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. USART_InitTypeDef USART_InitStructure;
  69. NVIC_InitTypeDef NVIC_InitStructure;
  70. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹ��USART1��GPIOAʱ��
  71. //USART1_TX GPIOA.9
  72. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  73. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //������������
  75. GPIO_Init(GPIOA, &GPIO_InitStructure);//��ʼ��GPIOA.9
  76. //USART1_RX GPIOA.10��ʼ��
  77. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  78. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//��������
  79. GPIO_Init(GPIOA, &GPIO_InitStructure);//��ʼ��GPIOA.10
  80. //Usart1 NVIC ����
  81. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  82. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//��ռ���ȼ�3
  83. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //�����ȼ�3
  84. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨ��ʹ��
  85. NVIC_Init(&NVIC_InitStructure); //����ָ���IJ�����ʼ��VIC�Ĵ���
  86. //USART ��ʼ������
  87. USART_InitStructure.USART_BaudRate = bound;//���ڲ�����
  88. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//�ֳ�Ϊ8λ���ݸ�ʽ
  89. USART_InitStructure.USART_StopBits = USART_StopBits_1;//һ��ֹͣλ
  90. USART_InitStructure.USART_Parity = USART_Parity_No;//����żУ��λ
  91. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//��Ӳ������������
  92. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //�շ�ģʽ
  93. USART_Init(USART1, &USART_InitStructure); //��ʼ������1
  94. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//�������ڽ����ж�
  95. USART_Cmd(USART1, ENABLE); //ʹ�ܴ���1
  96. }
  97. void USART1_IRQHandler(void) //����1�жϷ�������
  98. {
  99. u8 Res;
  100. #if SYSTEM_SUPPORT_OS //����SYSTEM_SUPPORT_OSΪ�棬����Ҫ֧��OS.
  101. OSIntEnter();
  102. #endif
  103. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //�����ж�(���յ������ݱ�����0x0d 0x0a��β)
  104. {
  105. Res =USART_ReceiveData(USART1); //��ȡ���յ�������
  106. if((USART_RX_STA&0x8000)==0)//�������
  107. {
  108. if(USART_RX_STA&0x4000)//���յ���0x0d
  109. {
  110. if(Res!=0x0a)USART_RX_STA=0;//���մ���,���¿�ʼ
  111. else USART_RX_STA|=0x8000; //����������
  112. }
  113. else //��û�յ�0X0D
  114. {
  115. if(Res==0x0d)USART_RX_STA|=0x4000;
  116. else
  117. {
  118. USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
  119. USART_RX_STA++;
  120. if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//�������ݴ���,���¿�ʼ����
  121. }
  122. }
  123. }
  124. }
  125. #if SYSTEM_SUPPORT_OS //����SYSTEM_SUPPORT_OSΪ�棬����Ҫ֧��OS.
  126. OSIntExit();
  127. #endif
  128. }
  129. #endif