main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. \file main.c
  3. \brief led spark with systick, USART print and key example
  4. \version 2017-12-26, V1.0.0, firmware for GD32E10x
  5. */
  6. /*
  7. Copyright (c) 2017, GigaDevice Semiconductor Inc.
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "s600.h"
  31. #include "main.h"
  32. #include "s600_can.h"
  33. #include "s600_iap.h"
  34. #ifdef CONFIG_BOARD_PS100
  35. #include "bl_common.h"
  36. #include "bl_drv_usart.h"
  37. #include "bl_drv_usart_2.h"
  38. DELAY_COMMON send_delay;
  39. uint32_t g_event = NO_EVENT;
  40. static uint8_t app_rs485_buf[TX_BUFFER_SIZE] = { 0x17, 0x31, 0x42, 0x10, 0x8B, 0xBE, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  41. __STATIC_INLINE void Send_Sub_BMS_CMD_Delay(void)
  42. {
  43. if (send_delay.set) {
  44. ++send_delay.count;
  45. if(send_delay.count >= 4) {
  46. send_delay.count = 0;
  47. g_event |= SUB_BMS_2_SEND_CMD_EVENT;
  48. } else if(send_delay.count == 2) {
  49. g_event |= SUB_BMS_1_SEND_CMD_EVENT;
  50. }
  51. }
  52. }
  53. __STATIC_INLINE void BMS_Com_Initial(void)
  54. {
  55. Usart1_Initial();
  56. Usart2_Initial();
  57. send_delay.set = 1;
  58. send_delay.count = 4;
  59. rcu_periph_clock_enable(RCU_GPIOA);
  60. gpio_init(GPIOA,GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
  61. gpio_bit_reset(GPIOA, GPIO_PIN_12);
  62. rcu_periph_clock_enable(RCU_GPIOA);
  63. gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  64. gpio_bit_reset(GPIOA, GPIO_PIN_7);
  65. }
  66. __STATIC_INLINE int8_t Send_Sub_BMS_CMD_1(void)
  67. {
  68. return Send_Data_RS485(app_rs485_buf, app_rs485_buf[0]);
  69. }
  70. __STATIC_INLINE int8_t Send_Sub_BMS_CMD_2(void)
  71. {
  72. return Send_Data_2_RS485(app_rs485_buf, app_rs485_buf[0]);
  73. }
  74. #endif
  75. __STATIC_INLINE void systick_config(void)
  76. {
  77. /* setup systick timer for 10Hz interrupts */
  78. SysTick_Config(SystemCoreClock / 10);
  79. /* configure the systick handler priority */
  80. NVIC_SetPriority(SysTick_IRQn, 0x00U);
  81. }
  82. int main(void)
  83. {
  84. u8 command[] = { 0xF0, CONFIG_CAN_ID, 0x00 };
  85. s600_iap_boot(true);
  86. systick_config();
  87. #ifdef CONFIG_BOARD_PS100
  88. BMS_Com_Initial();
  89. #endif
  90. s600_can_device_config();
  91. s600_can_send_command(S600_CAN_ALL_ID, command, sizeof(command));
  92. while (1) {
  93. s600_can_poll();
  94. if (s600_iap_100ms > 600) {
  95. s600_iap_boot(false);
  96. s600_iap_100ms = 0;
  97. s600_can_send_command(S600_CAN_ALL_ID, command, sizeof(command));
  98. }
  99. #ifdef CONFIG_BOARD_PS100
  100. if (g_event & SUB_BMS_1_SEND_CMD_EVENT) {
  101. Send_Sub_BMS_CMD_1();
  102. g_event &= ~SUB_BMS_1_SEND_CMD_EVENT;
  103. }
  104. if (g_event & SUB_BMS_2_SEND_CMD_EVENT) {
  105. Send_Sub_BMS_CMD_2();
  106. g_event &= ~SUB_BMS_2_SEND_CMD_EVENT;
  107. }
  108. #endif
  109. }
  110. }
  111. void SysTick_Handler(void)
  112. {
  113. s600_iap_100ms++;
  114. #ifdef CONFIG_BOARD_PS100
  115. Send_Sub_BMS_CMD_Delay();
  116. #endif
  117. }