goose.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "head.h"
  2. //#include <linux/kernel.h>
  3. //#include <linux/init.h>
  4. //#include <linux/module.h>
  5. //#include <linux/moduleparam.h>
  6. #include <linux/fs.h>
  7. #include <linux/sched.h>
  8. //#include <linux/interrupt.h>
  9. //#include <linux/kthread.h>
  10. //#include <linux/delay.h>
  11. #include <linux/wait.h>
  12. //#include <asm/uaccess.h>
  13. //#include <asm/io.h>
  14. #include <linux/errno.h>
  15. //#include <asm/ioctls.h>
  16. #include <linux/ioctl.h>
  17. //#include <linux/signal.h>
  18. #include "rt_clock.h"
  19. #include "rt.h"
  20. #include "bsp.h"
  21. #include "app_goose.h"
  22. // 直接发送和接收时使用的设备编号
  23. enum ENUM_GOOSE_DEV_IDX
  24. {
  25. GOOSE_NET_DEVICE0 = 0,
  26. GOOSE_NET_DEVICE1,
  27. };
  28. static unsigned char g_goose_init_flag = 0;
  29. unsigned char goose_get_init_flag(void)
  30. {
  31. return g_goose_init_flag;
  32. }
  33. // sunxi 软中断进行goose数据发送
  34. int _goose_send_soft_isr(int irq, void *dev_id)
  35. {
  36. rt_sirq_clear(RT_SOFT_IRQ_GOOSE_SEND);
  37. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  38. if(!g_goose_init_flag)
  39. return 0;
  40. if(tFAg.bSend)
  41. {
  42. app_goose_pub_data_handle((char *)tFAg.outval, GOOSE_SEND_TBL_NUMBER);
  43. tFAg.bSend = false;
  44. }
  45. #endif // __IEC61850_GOOSE_FUNC__
  46. return 0;
  47. }
  48. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  49. #define GOOSE_READ_BUF_MAX 1540
  50. static unsigned char goose_recv_data_buf[GOOSE_READ_BUF_MAX] = {0x00};
  51. extern int goose_direct_read(int devno, char *data, int len);
  52. extern int goose_direct_write(int devno, const char *data, int len);
  53. static unsigned char g_can_enter_flag = 1;
  54. #endif // __IEC61850_GOOSE_FUNC__
  55. // 接收goose数据软中断,每468us中断一次
  56. int _goose_recv_soft_isr(int irq, void *dev_id)
  57. {
  58. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  59. int len = 0;
  60. int readlen = 0;
  61. #endif // __IEC61850_GOOSE_FUNC__
  62. rt_sirq_clear(RT_SOFT_IRQ_GOOSE_RECV);
  63. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  64. if(!g_can_enter_flag)
  65. return 0;
  66. g_can_enter_flag = 0;
  67. len = goose_direct_read(GOOSE_NET_DEVICE1, goose_recv_data_buf, GOOSE_READ_BUF_MAX);
  68. // 没有初始化或者缓冲区没有数据
  69. if(!g_goose_init_flag || !len)
  70. {
  71. g_can_enter_flag = 1;
  72. return 0;
  73. }
  74. if(len >= GOOSE_READ_BUF_MAX)
  75. len = GOOSE_READ_BUF_MAX;
  76. readlen = (GOOSE_RECV_TBL_NUMBER) * sizeof(int);
  77. if(readlen == app_goose_recv_data_handle(0, goose_recv_data_buf, len, (char *)tFAg.inval))
  78. fa_g_unpack(); //对接收的数据进行解析
  79. rt_sirq_force(RT_SOFT_IRQ_GOOSE_HANDLE);
  80. g_can_enter_flag = 1;
  81. #endif // __IEC61850_GOOSE_FUNC__
  82. return 0;
  83. }
  84. int _goose_handle_soft_isr(int irq, void *dev_id)
  85. {
  86. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  87. static uint32_t us_old = 0;
  88. uint32_t us_new, us_diff;
  89. #endif // __IEC61850_GOOSE_FUNC__
  90. rt_sirq_clear(RT_SOFT_IRQ_GOOSE_HANDLE);
  91. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  92. us_new = ustimer_get_origin();
  93. us_diff = us_new - us_old;
  94. if(!g_can_enter_flag)
  95. return 0;
  96. us_old = us_new;
  97. g_can_enter_flag = 0;
  98. app_goose_timer_data_handle(us_diff);
  99. g_can_enter_flag = 1;
  100. #endif // __IEC61850_GOOSE_FUNC__
  101. return 0;
  102. }
  103. // sunxi 20190425 把原来在 468us中断的函数处理中移到 500us 中断中来处理
  104. void goose_timer_handle(void)
  105. {
  106. if(g_goose_init_flag)
  107. {
  108. rt_sirq_force(RT_SOFT_IRQ_GOOSE_HANDLE);
  109. }
  110. }
  111. /*********************************************************/
  112. void goose_send_wakeup(void)
  113. {
  114. rt_sirq_force(RT_SOFT_IRQ_GOOSE_SEND);
  115. }
  116. void goose_recv_wakeup(void)
  117. {
  118. rt_sirq_force(RT_SOFT_IRQ_GOOSE_RECV);
  119. }
  120. //初始化
  121. int goose_init(void)
  122. {
  123. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  124. if(app_goose_init(goose_direct_write) == 0)
  125. {
  126. goose_pub_init((char *)tFAg.outval, GOOSE_SEND_TBL_NUMBER);
  127. // 登记主循环软中断
  128. rt_sirq_register(RT_SOFT_IRQ_GOOSE_RECV, _goose_recv_soft_isr, "_goose_recv_soft_isr");
  129. rt_sirq_register(RT_SOFT_IRQ_GOOSE_SEND, _goose_send_soft_isr, "_goose_send_soft_isr");
  130. rt_sirq_register(RT_SOFT_IRQ_GOOSE_HANDLE, _goose_handle_soft_isr, "_goose_handle_soft_isr");
  131. g_goose_init_flag = 1;
  132. rt_printf("user_goose_init ok.\n");
  133. }
  134. else // sunxi 20191204
  135. {
  136. rt_printf("user_goose_init failed.\n");
  137. return -1;
  138. }
  139. #endif // __IEC61850_GOOSE_FUNC__
  140. return 0;
  141. }
  142. int goose_exit(void)
  143. {
  144. #ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下,启动不编译进模块里面。即goose功能的启动作为独立的模块
  145. if(g_goose_init_flag)
  146. {
  147. g_goose_init_flag = 0;
  148. rt_sirq_unregister(RT_SOFT_IRQ_GOOSE_HANDLE);
  149. rt_sirq_unregister(RT_SOFT_IRQ_GOOSE_SEND);
  150. rt_sirq_unregister(RT_SOFT_IRQ_GOOSE_RECV);
  151. app_goose_exit();
  152. }
  153. #endif // __IEC61850_GOOSE_FUNC__
  154. rt_printf("user_goose_exit.\n");
  155. return 0;
  156. }