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.

138 lines
3.6 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 STM32F407������
  11. //����1��ʼ��
  12. //����ԭ��@ALIENTEK
  13. //������̳:www.openedv.com
  14. //�޸�����:2014/5/2
  15. //�汾��V1.1
  16. //��Ȩ���У������ؾ���
  17. //Copyright(C) �������������ӿƼ����޹�˾ 2014-2024
  18. //All rights reserved
  19. //********************************************************************************
  20. //�޸�˵��
  21. //V1.1 20150411
  22. //�޸�OS_CRITICAL_METHOD���ж�Ϊ��SYSTEM_SUPPORT_OS
  23. //////////////////////////////////////////////////////////////////////////////////
  24. //�������´���,֧��printf����,������Ҫѡ��use MicroLIB
  25. #if 1
  26. #pragma import(__use_no_semihosting)
  27. //��׼����Ҫ��֧�ֺ���
  28. struct __FILE
  29. {
  30. int handle;
  31. /* Whatever you require here. If the only file you are using is */
  32. /* standard output using printf() for debugging, no file handling */
  33. /* is required. */
  34. };
  35. /* FILE is typedef�� d in stdio.h. */
  36. FILE __stdout;
  37. //����_sys_exit()�Ա���ʹ�ð�����ģʽ
  38. void _sys_exit(int x)
  39. {
  40. x = x;
  41. }
  42. //�ض���fputc����
  43. int fputc(int ch, FILE *f)
  44. {
  45. while((USART1->SR&0X40)==0);//ѭ������,ֱ����������
  46. USART1->DR = (u8) ch;
  47. return ch;
  48. }
  49. #endif
  50. //end
  51. //////////////////////////////////////////////////////////////////
  52. #if EN_USART1_RX //����ʹ���˽���
  53. //����1�жϷ�������
  54. //ע��,��ȡUSARTx->SR�ܱ���Ī�������Ĵ���
  55. u8 USART_RX_BUF[USART_REC_LEN]; //���ջ���,����USART_REC_LEN���ֽ�.
  56. //����״̬
  57. //bit15�� �������ɱ�־
  58. //bit14�� ���յ�0x0d
  59. //bit13~0�� ���յ�����Ч�ֽ���Ŀ
  60. u16 USART_RX_STA=0; //����״̬����
  61. void USART1_IRQHandler(void)
  62. {
  63. u8 res;
  64. #if SYSTEM_SUPPORT_OS //����SYSTEM_SUPPORT_OSΪ�棬����Ҫ֧��OS.
  65. OSIntEnter();
  66. #endif
  67. if(USART1->SR&(1<<5))//���յ�����
  68. {
  69. res=USART1->DR;
  70. if((USART_RX_STA&0x8000)==0)//�������
  71. {
  72. if(USART_RX_STA&0x4000)//���յ���0x0d
  73. {
  74. if(res!=0x0a)USART_RX_STA=0;//���մ���,���¿�ʼ
  75. else USART_RX_STA|=0x8000; //����������
  76. }else //��û�յ�0X0D
  77. {
  78. if(res==0x0d)USART_RX_STA|=0x4000;
  79. else
  80. {
  81. USART_RX_BUF[USART_RX_STA&0X3FFF]=res;
  82. USART_RX_STA++;
  83. if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//�������ݴ���,���¿�ʼ����
  84. }
  85. }
  86. }
  87. }
  88. #if SYSTEM_SUPPORT_OS //����SYSTEM_SUPPORT_OSΪ�棬����Ҫ֧��OS.
  89. OSIntExit();
  90. #endif
  91. }
  92. #endif
  93. //��ʼ��IO ����1
  94. //pclk2:PCLK2ʱ��Ƶ��(Mhz)
  95. //bound:������
  96. void uart_init(u32 pclk2,u32 bound)
  97. {
  98. float temp;
  99. u16 mantissa;
  100. u16 fraction;
  101. temp=(float)(pclk2*1000000)/(bound*16);//�õ�USARTDIV@OVER8=0
  102. mantissa=temp; //�õ���������
  103. fraction=(temp-mantissa)*16; //�õ�С������@OVER8=0
  104. mantissa<<=4;
  105. mantissa+=fraction;
  106. RCC->AHB1ENR|=1<<0; //ʹ��PORTA��ʱ��
  107. RCC->APB2ENR|=1<<4; //ʹ�ܴ���1ʱ��
  108. GPIO_Set(GPIOA,PIN9|PIN10,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);//PA9,PA10,���ù���,��������
  109. GPIO_AF_Set(GPIOA,9,7); //PA9,AF7
  110. GPIO_AF_Set(GPIOA,10,7);//PA10,AF7
  111. //����������
  112. USART1->BRR=mantissa; //����������
  113. USART1->CR1&=~(1<<15); //����OVER8=0
  114. USART1->CR1|=1<<3; //���ڷ���ʹ��
  115. #if EN_USART1_RX //����ʹ���˽���
  116. //ʹ�ܽ����ж�
  117. USART1->CR1|=1<<2; //���ڽ���ʹ��
  118. USART1->CR1|=1<<5; //���ջ������ǿ��ж�ʹ��
  119. MY_NVIC_Init(3,3,USART1_IRQn,2);//��2���������ȼ�
  120. #endif
  121. USART1->CR1|=1<<13; //����ʹ��
  122. }