slave_485.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @Author: 李建
  3. * @Date: 2024/8/13 10:38
  4. * Description:
  5. * Copyright: Copyright (©) 2024 永续绿建. All rights reserved.
  6. */
  7. #include "slave_485.h"
  8. #include "mb.h"
  9. #include "cmsis_os2.h"
  10. #include "fan.h"
  11. #include "tim.h"
  12. #include "usart.h"
  13. #include "flash.h"
  14. #include "iwdg.h"
  15. osThreadAttr_t task_attributes = {
  16. .name = "slave_485",
  17. .stack_size = 128 * 6,
  18. .priority = (osPriority_t) osPriorityAboveNormal,
  19. };
  20. USHORT usSRegHoldStart = S_REG_HOLDING_START;
  21. USHORT usSRegHoldBuf[S_REG_HOLDING_NREGS] = {0};
  22. static uint8_t wire_controller_acc = 0; // 线控器通讯累加器
  23. eMBRTUSlaveObj wireControllerSlaveObj;
  24. uint8_t test = 0;
  25. static eMBErrorCode RegInputCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs) {
  26. return MB_ENOERR;
  27. }
  28. static eMBErrorCode RegHoldingCB(UCHAR *pucRegBuffer, USHORT usAddress,
  29. USHORT usNRegs, eMBRegisterMode eMode) {
  30. eMBErrorCode eStatus = MB_ENOERR;
  31. USHORT iRegIndex;
  32. USHORT *pusRegHoldingBuf;
  33. USHORT REG_HOLDING_START;
  34. USHORT REG_HOLDING_NREGS;
  35. USHORT usRegHoldStart;
  36. pusRegHoldingBuf = usSRegHoldBuf;
  37. REG_HOLDING_START = S_REG_HOLDING_START;
  38. REG_HOLDING_NREGS = S_REG_HOLDING_NREGS;
  39. usRegHoldStart = usSRegHoldStart;
  40. /* it already plus one in modbus function method. */
  41. usAddress--;
  42. if ((usAddress >= REG_HOLDING_START)
  43. && (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS)) {
  44. iRegIndex = usAddress - usRegHoldStart;
  45. switch (eMode) {
  46. /* read current register values from the protocol stack. */
  47. case MB_REG_READ:
  48. while (usNRegs > 0) {
  49. *pucRegBuffer++ = (UCHAR) (pusRegHoldingBuf[iRegIndex] >> 8);
  50. *pucRegBuffer++ = (UCHAR) (pusRegHoldingBuf[iRegIndex] & 0xFF);
  51. iRegIndex++;
  52. usNRegs--;
  53. }
  54. break;
  55. /* write current register values with new values from the protocol stack. */
  56. case MB_REG_WRITE:
  57. while (usNRegs > 0) {
  58. pusRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
  59. pusRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
  60. iRegIndex++;
  61. usNRegs--;
  62. }
  63. break;
  64. }
  65. } else {
  66. eStatus = MB_ENOREG;
  67. }
  68. wire_controller_acc = 0;
  69. return eStatus;
  70. }
  71. /**
  72. * Modbus slave coils callback function.
  73. *
  74. * @param pucRegBuffer coils buffer
  75. * @param usAddress coils address
  76. * @param usNCoils coils number
  77. * @param eMode read or write
  78. *
  79. * @return result
  80. */
  81. eMBErrorCode RegCoilsCB(UCHAR *pucRegBuffer, USHORT usAddress,
  82. USHORT usNCoils, eMBRegisterMode eMode) {
  83. return MB_ENOERR;
  84. }
  85. /**
  86. * Modbus slave discrete callback function.
  87. *
  88. * @param pucRegBuffer discrete buffer
  89. * @param usAddress discrete address
  90. * @param usNDiscrete discrete number
  91. *
  92. * @return result
  93. */
  94. eMBErrorCode RegDiscreteCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete) {
  95. return MB_ENOERR;
  96. }
  97. void set_reg_value(uint8_t reg_address, uint16_t reg_value) {
  98. usSRegHoldBuf[reg_address] = reg_value;
  99. }
  100. void get_reg_value(uint8_t reg_address, uint16_t *reg_value) {
  101. *reg_value = usSRegHoldBuf[reg_address];
  102. }
  103. uint16_t get_reg_value2(uint8_t reg_address) {
  104. return usSRegHoldBuf[reg_address];
  105. }
  106. _Noreturn void reg_check_task(void *pv) {
  107. uint16_t power=0, fan_level=0,dipnum=0;
  108. for (;;) {
  109. dipnum=read_dip5();//0 1 2 ,
  110. for(int i=0;i<dipnum;i++)
  111. {
  112. HAL_TIM_PWM_Start(PWMComParam[i].htim, PWMComParam[i].timChannel);
  113. get_reg_value(FAN1_LEVEL_REG_ADDRESS+dipBuf[i],&fan_level);
  114. fan_control(PWMComParam[i],fan_level);
  115. }
  116. HAL_IWDG_Refresh(&hiwdg);
  117. osDelay(500);
  118. }
  119. }
  120. _Noreturn void slave_485_task(void *pvParameters) {
  121. // 回调配置
  122. mb_reg_callback_t calls = {
  123. .eMbRegHoldingCb = RegHoldingCB,
  124. .eMbRegInputCb = RegInputCB,
  125. };
  126. wireControllerSlaveObj.eMode = MB_RTU;
  127. wireControllerSlaveObj.ucSlaveAddress = (uint8_t) pvParameters;
  128. wireControllerSlaveObj.ulBaudRate = 9600;
  129. wireControllerSlaveObj.eParity = MB_PAR_NONE;
  130. wireControllerSlaveObj.pTimHandle = htim2;
  131. wireControllerSlaveObj.pUartHandle = huart2;
  132. wireControllerSlaveObj.enPort = GPIOA;
  133. wireControllerSlaveObj.enPin = GPIO_PIN_1;
  134. wireControllerSlaveObj.callback = &calls;
  135. eMBInit(&wireControllerSlaveObj);
  136. eMBEnable(&wireControllerSlaveObj);
  137. for (;;) {
  138. eMBPoll(&wireControllerSlaveObj);
  139. osDelay(5);
  140. }
  141. }
  142. /**
  143. * 初始化485从站
  144. */
  145. void slave_485_init(uint8_t address) {
  146. osThreadNew(slave_485_task, (void *) address, &task_attributes);
  147. osThreadNew(reg_check_task, NULL, &task_attributes);
  148. }