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.
 
 
 
 

97 line
3.4 KiB

  1. #include "stm32f4xx.h"
  2. #include "hardware.h"
  3. uint8_t ModbusRxBuf[MODBUS_RX_BUF_SIZE];
  4. uint16_t ModbusRxLen = 0;
  5. void ModbusGpioInit(void)
  6. {
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  11. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  13. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  14. GPIO_Init(GPIOA, &GPIO_InitStructure);
  15. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  16. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  17. }
  18. void ModbusUsartInit(void)
  19. {
  20. USART_InitTypeDef USART_InitStructure;
  21. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  22. USART_InitStructure.USART_BaudRate = MODBUS_USART_BAUDRATE;
  23. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  24. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  25. USART_InitStructure.USART_Parity = USART_Parity_No;
  26. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  27. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  28. USART_Init(USART1, &USART_InitStructure);
  29. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
  30. USART_Cmd(USART1, ENABLE);
  31. }
  32. void ModbusDmaInit(void)
  33. {
  34. DMA_InitTypeDef DMA_InitStructure;
  35. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  36. /* DMA2_Stream2 用于 USART1_RX */
  37. DMA_DeInit(DMA2_Stream2);
  38. DMA_InitStructure.DMA_Channel = DMA_Channel_4;
  39. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR);
  40. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ModbusRxBuf;
  41. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  42. DMA_InitStructure.DMA_BufferSize = MODBUS_RX_BUF_SIZE;
  43. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  44. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  45. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  46. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  47. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  48. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  49. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  50. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
  51. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  52. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  53. DMA_Init(DMA2_Stream2, &DMA_InitStructure);
  54. /* DMA2_Stream7 用于 USART1_TX */
  55. DMA_DeInit(DMA2_Stream7);
  56. DMA_InitStructure.DMA_Channel = DMA_Channel_4;
  57. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR);
  58. DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  59. DMA_Init(DMA2_Stream7, &DMA_InitStructure);
  60. /* 使能USART的DMA请求 */
  61. USART_DMACmd(USART1, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);
  62. DMA_Cmd(DMA2_Stream2, ENABLE);
  63. }
  64. void ModbusNvicInit(void)
  65. {
  66. NVIC_InitTypeDef NVIC_InitStructure;
  67. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  68. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
  69. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  70. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  71. NVIC_Init(&NVIC_InitStructure);
  72. }
  73. void MdobusHardwareInit(void)
  74. {
  75. ModbusGpioInit();
  76. ModbusUsartInit();
  77. ModbusDmaInit();
  78. ModbusNvicInit();
  79. }