Advertisement
josefkyrian

Enelion Stylo Energy Guard

May 13th, 2024
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.42 KB | None | 0 0
  1. #include <assert.h>
  2.  
  3. /******************************************************************************
  4. ************************ CANBUS FUNCTIONS START *******************************
  5. ******************************************************************************/
  6. #define STM32_CAN_TIR_TXRQ              (1U << 0U)  // Bit 0: Transmit Mailbox Request
  7. #define STM32_CAN_RIR_RTR               (1U << 1U)  // Bit 1: Remote Transmission Request
  8. #define STM32_CAN_RIR_IDE               (1U << 2U)  // Bit 2: Identifier Extension
  9. #define STM32_CAN_TIR_RTR               (1U << 1U)  // Bit 1: Remote Transmission Request
  10. #define STM32_CAN_TIR_IDE               (1U << 2U)  // Bit 2: Identifier Extension
  11.  
  12. #define CAN_EXT_ID_MASK                 0x1FFFFFFFU
  13. #define CAN_STD_ID_MASK                 0x000007FFU
  14.  
  15. /* Symbolic names for formats of CAN message                                 */
  16. typedef enum {STANDARD_FORMAT = 0, EXTENDED_FORMAT} CAN_FORMAT;
  17.  
  18. /* Symbolic names for type of CAN message                                    */
  19. typedef enum {DATA_FRAME = 0, REMOTE_FRAME}         CAN_FRAME;
  20.  
  21.  
  22. typedef struct
  23. {
  24.     uint32_t id;        /* 29 bit identifier                               */
  25.     uint8_t  data[8];   /* Data field                                      */
  26.     uint8_t  len;       /* Length of data field in bytes                   */
  27.     uint8_t  ch;        /* Object channel(Not use)                         */
  28.     uint8_t  format;    /* 0 - STANDARD, 1- EXTENDED IDENTIFIER            */
  29.     uint8_t  type;      /* 0 - DATA FRAME, 1 - REMOTE FRAME                */
  30. } CAN_msg_t;
  31.  
  32. typedef struct
  33. {
  34.         uint16_t baud_rate_prescaler;                /// [1 to 1024]
  35.         uint8_t time_segment_1;                      /// [1 to 16]
  36.         uint8_t time_segment_2;                      /// [1 to 8]
  37.         uint8_t resynchronization_jump_width;        /// [1 to 4] (recommended value is 1)
  38. } CAN_bit_timing_config_t;
  39.  
  40.  
  41. /**
  42.  * Initializes the CAN filter registers.
  43.  *
  44.  * @preconditions   - This register can be written only when the filter initialization mode is set (FINIT=1) in the CAN_FMR register.
  45.  * @params: index   - Specified filter index. index 27:14 are available in connectivity line devices only.
  46.  * @params: scale   - Select filter scale.
  47.  *                    0: Dual 16-bit scale configuration
  48.  *                    1: Single 32-bit scale configuration
  49.  * @params: mode    - Select filter mode.
  50.  *                    0: Two 32-bit registers of filter bank x are in Identifier Mask mode
  51.  *                    1: Two 32-bit registers of filter bank x are in Identifier List mode
  52.  * @params: fifo    - Select filter assigned.
  53.  *                    0: Filter assigned to FIFO 0
  54.  *                    1: Filter assigned to FIFO 1
  55.  * @params: bank1   - Filter bank register 1
  56.  * @params: bank2   - Filter bank register 2
  57.  *
  58.  */
  59. void CANSetFilter(uint8_t index, uint8_t scale, uint8_t mode, uint8_t fifo, uint32_t bank1, uint32_t bank2) {
  60.     if (index > 27) return;
  61.  
  62.     CAN1->FA1R &= ~(0x1UL<<index);               // Deactivate filter
  63.  
  64.     if (scale == 0) {
  65.         CAN1->FS1R &= ~(0x1UL<<index);             // Set filter to Dual 16-bit scale configuration
  66.     } else {
  67.         CAN1->FS1R |= (0x1UL<<index);              // Set filter to single 32 bit configuration
  68.     }
  69.         if (mode == 0) {
  70.         CAN1->FM1R &= ~(0x1UL<<index);             // Set filter to Mask mode
  71.     } else {
  72.         CAN1->FM1R |= (0x1UL<<index);              // Set filter to List mode
  73.     }
  74.  
  75.     if (fifo == 0) {
  76.         CAN1->FFA1R &= ~(0x1UL<<index);            // Set filter assigned to FIFO 0
  77.     } else {
  78.         CAN1->FFA1R |= (0x1UL<<index);             // Set filter assigned to FIFO 1
  79.     }
  80.  
  81.     CAN1->sFilterRegister[index].FR1 = bank1;    // Set filter bank registers1
  82.     CAN1->sFilterRegister[index].FR2 = bank2;    // Set filter bank registers2
  83.  
  84.     CAN1->FA1R |= (0x1UL<<index);                // Activate filter
  85.  
  86. }
  87.  
  88. /**
  89.  * Initializes the CAN controller with specified bit rate.
  90.  *
  91.  * @params: timings - Specified timings
  92.  * @params: remap   - Select CAN port.
  93.  *                    =0:CAN_RX mapped to PA11, CAN_TX mapped to PA12
  94.  *                    =1:Not used
  95.  *                    =2:CAN_RX mapped to PB8, CAN_TX mapped to PB9 (not available on 36-pin package)
  96.  *                    =3:CAN_RX mapped to PD0, CAN_TX mapped to PD1 (available on 100-pin and 144-pin package)
  97.  *
  98.  */
  99. bool CANInit(CAN_bit_timing_config_t &timings, int remap)
  100. {
  101.     // Reference manual
  102.     // https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf
  103.  
  104.     RCC->APB1ENR |= 0x2000000UL;       // Enable CAN clock
  105.     RCC->APB2ENR |= 0x1UL;             // Enable AFIO clock
  106.     AFIO->MAPR   &= 0xFFFF9FFF;        // reset CAN remap
  107.                                                                         // CAN_RX mapped to PA11, CAN_TX mapped to PA12
  108.  
  109.     if (remap == 0) {
  110.         RCC->APB2ENR |= 0x4UL;           // Enable GPIOA clock
  111.         GPIOA->CRH   &= ~(0xFF000UL);    // Configure PA12(0b0000) and PA11(0b0000)
  112.                                                                         // 0b0000
  113.                                                                         //   MODE=00(Input mode)
  114.                                                                         //   CNF=00(Analog mode)
  115.  
  116.         GPIOA->CRH   |= 0xB8FFFUL;       // Configure PA12(0b1011) and PA11(0b1000)
  117.                                                                         // 0b1011
  118.                                                                         //   MODE=11(Output mode, max speed 50 MHz)
  119.                                                                         //   CNF=10(Alternate function output Push-pull
  120.                                                                         // 0b1000
  121.                                                                         //   MODE=00(Input mode)
  122.                                                                         //   CNF=10(Input with pull-up / pull-down)
  123.                                                                        
  124.         GPIOA->ODR |= 0x1UL << 12;       // PA12 Upll-up
  125.        
  126.     }
  127.                                                                
  128.     if (remap == 2) {
  129.         AFIO->MAPR   |= 0x00004000;      // set CAN remap
  130.                                                                         // CAN_RX mapped to PB8, CAN_TX mapped to PB9 (not available on 36-pin package)
  131.  
  132.         RCC->APB2ENR |= 0x8UL;           // Enable GPIOB clock
  133.         GPIOB->CRH   &= ~(0xFFUL);       // Configure PB9(0b0000) and PB8(0b0000)
  134.                                                                         // 0b0000
  135.                                                                         //   MODE=00(Input mode)
  136.                                                                         //   CNF=00(Analog mode)
  137.  
  138.         GPIOB->CRH   |= 0xB8UL;          // Configure PB9(0b1011) and PB8(0b1000)
  139.                                                                         // 0b1011
  140.                                                                         //   MODE=11(Output mode, max speed 50 MHz)
  141.                                                                         //   CNF=10(Alternate function output Push-pull
  142.                                                                         // 0b1000
  143.                                                                         //   MODE=00(Input mode)
  144.                                                                         //   CNF=10(Input with pull-up / pull-down)
  145.                                                                        
  146.         GPIOB->ODR |= 0x1UL << 8;        // PB8 Upll-up
  147.     }
  148.    
  149.     if (remap == 3) {
  150.         AFIO->MAPR   |= 0x00005000;      // set CAN remap
  151.                                                                         // CAN_RX mapped to PD0, CAN_TX mapped to PD1 (available on 100-pin and 144-pin package)
  152.  
  153.         RCC->APB2ENR |= 0x20UL;          // Enable GPIOD clock
  154.         GPIOD->CRL   &= ~(0xFFUL);       // Configure PD1(0b0000) and PD0(0b0000)
  155.                                                                         // 0b0000
  156.                                                                         //   MODE=00(Input mode)
  157.                                                                         //   CNF=00(Analog mode)
  158.  
  159.         GPIOD->CRH   |= 0xB8UL;          // Configure PD1(0b1011) and PD0(0b1000)
  160.                                                                         // 0b1000
  161.                                                                         //   MODE=00(Input mode)
  162.                                                                         //   CNF=10(Input with pull-up / pull-down)
  163.                                                                         // 0b1011
  164.                                                                         //   MODE=11(Output mode, max speed 50 MHz)
  165.                                                                         //   CNF=10(Alternate function output Push-pull
  166.                                                                        
  167.         GPIOD->ODR |= 0x1UL << 0;        // PD0 Upll-up
  168.     }
  169.  
  170.     CAN1->MCR |= 0x1UL;                   // Require CAN1 to Initialization mode
  171.     while (!(CAN1->MSR & 0x1UL));         // Wait for Initialization mode
  172.  
  173.     //CAN1->MCR = 0x51UL;                 // Hardware initialization(No automatic retransmission)
  174.     CAN1->MCR = 0x41UL;                   // Hardware initialization(With automatic retransmission)
  175.    
  176.     // Set bit timing register
  177.     CAN1->BTR = (((timings.resynchronization_jump_width - 1U) &    3U) << 24U) |
  178.                             (((timings.time_segment_1 - 1U)               &   15U) << 16U) |
  179.                             (((timings.time_segment_2 - 1U)               &    7U) << 20U) |
  180.                             ((timings.baud_rate_prescaler - 1U)           & 1023U);
  181.  
  182.     // Configure Filters to default values
  183.     CAN1->FMR  |=   0x1UL;                // Set to filter initialization mode
  184.     CAN1->FMR  &= 0xFFFFC0FF;             // Clear CAN2 start bank
  185.  
  186.     // bxCAN has 28 filters.
  187.     // These filters are shared by both CAN1 and CAN2.
  188.     // STM32F103 has only CAN1, so all 28 are used for CAN1
  189.     CAN1->FMR  |= 0x1C << 8;              // Assign all filters to CAN1
  190.  
  191.     // Set fileter 0
  192.     // Single 32-bit scale configuration
  193.     // Two 32-bit registers of filter bank x are in Identifier Mask mode
  194.     // Filter assigned to FIFO 0
  195.     // Filter bank register to all 0
  196.     CANSetFilter(0, 1, 0, 0, 0x0UL, 0x0UL);
  197.    
  198.     CAN1->FMR   &= ~(0x1UL);              // Deactivate initialization mode
  199.  
  200.     uint16_t TimeoutMilliseconds = 1000;
  201.     bool can1 = false;
  202.     CAN1->MCR   &= ~(0x1UL);              // Require CAN1 to normal mode
  203.  
  204.     // Wait for normal mode
  205.     // If the connection is not correct, it will not return to normal mode.
  206.     for (uint16_t wait_ack = 0; wait_ack < TimeoutMilliseconds; wait_ack++) {
  207.         if ((CAN1->MSR & 0x1UL) == 0) {
  208.             can1 = true;
  209.             break;
  210.         }
  211.         delayMicroseconds(1000);
  212.     }
  213.     //Serial.print("can1=");
  214.     //Serial.println(can1);
  215.     if (can1) {
  216.         Serial.println("CAN1 initialize ok");
  217.     } else {
  218.         Serial.println("CAN1 initialize fail!!");
  219.         return false;
  220.     }
  221.     return true;
  222. }
  223.  
  224. /**
  225.  * Decodes CAN messages from the data registers and populates a
  226.  * CAN message struct with the data fields.
  227.  *
  228.  * @preconditions A valid CAN message is received
  229.  * @params CAN_rx_msg - CAN message structure for reception
  230.  *
  231.  */
  232. void CANReceive(CAN_msg_t* CAN_rx_msg)
  233. {
  234.     uint32_t id = CAN1->sFIFOMailBox[0].RIR;
  235.     if ((id & STM32_CAN_RIR_IDE) == 0) { // Standard frame format
  236.             CAN_rx_msg->format = STANDARD_FORMAT;;
  237.             CAN_rx_msg->id = (CAN_STD_ID_MASK & (id >> 21U));
  238.     }
  239.     else {                               // Extended frame format
  240.             CAN_rx_msg->format = EXTENDED_FORMAT;;
  241.             CAN_rx_msg->id = (CAN_EXT_ID_MASK & (id >> 3U));
  242.     }
  243.  
  244.     if ((id & STM32_CAN_RIR_RTR) == 0) { // Data frame
  245.             CAN_rx_msg->type = DATA_FRAME;
  246.     }
  247.     else {                               // Remote frame
  248.             CAN_rx_msg->type = REMOTE_FRAME;
  249.     }
  250.  
  251.    
  252.     CAN_rx_msg->len = (CAN1->sFIFOMailBox[0].RDTR) & 0xFUL;
  253.    
  254.     CAN_rx_msg->data[0] = 0xFFUL &  CAN1->sFIFOMailBox[0].RDLR;
  255.     CAN_rx_msg->data[1] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDLR >> 8);
  256.     CAN_rx_msg->data[2] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDLR >> 16);
  257.     CAN_rx_msg->data[3] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDLR >> 24);
  258.     CAN_rx_msg->data[4] = 0xFFUL &  CAN1->sFIFOMailBox[0].RDHR;
  259.     CAN_rx_msg->data[5] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDHR >> 8);
  260.     CAN_rx_msg->data[6] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDHR >> 16);
  261.     CAN_rx_msg->data[7] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDHR >> 24);
  262.  
  263.     // Release FIFO 0 output mailbox.
  264.     // Make the next incoming message available.
  265.     CAN1->RF0R |= 0x20UL;
  266. }
  267.  
  268. /**
  269.  * Encodes CAN messages using the CAN message struct and populates the
  270.  * data registers with the sent.
  271.  *
  272.  * @params CAN_tx_msg - CAN message structure for transmission
  273.  *
  274.  */
  275. void CANSend(CAN_msg_t* CAN_tx_msg)
  276. {
  277.     volatile int count = 0;
  278.  
  279.     uint32_t out = 0;
  280.     if (CAN_tx_msg->format == EXTENDED_FORMAT) { // Extended frame format
  281.             out = ((CAN_tx_msg->id & CAN_EXT_ID_MASK) << 3U) | STM32_CAN_TIR_IDE;
  282.     }
  283.     else {                                       // Standard frame format
  284.             out = ((CAN_tx_msg->id & CAN_STD_ID_MASK) << 21U);
  285.     }
  286.  
  287.     // Remote frame
  288.     if (CAN_tx_msg->type == REMOTE_FRAME) {
  289.             out |= STM32_CAN_TIR_RTR;
  290.     }
  291.  
  292.     CAN1->sTxMailBox[0].TDTR &= ~(0xF);
  293.     CAN1->sTxMailBox[0].TDTR |= CAN_tx_msg->len & 0xFUL;
  294.    
  295.     CAN1->sTxMailBox[0].TDLR  = (((uint32_t) CAN_tx_msg->data[3] << 24) |
  296.                                                             ((uint32_t) CAN_tx_msg->data[2] << 16) |
  297.                                                             ((uint32_t) CAN_tx_msg->data[1] <<  8) |
  298.                                                             ((uint32_t) CAN_tx_msg->data[0]      ));
  299.     CAN1->sTxMailBox[0].TDHR  = (((uint32_t) CAN_tx_msg->data[7] << 24) |
  300.                                                             ((uint32_t) CAN_tx_msg->data[6] << 16) |
  301.                                                             ((uint32_t) CAN_tx_msg->data[5] <<  8) |
  302.                                                             ((uint32_t) CAN_tx_msg->data[4]      ));
  303.  
  304.     // Send Go
  305.     CAN1->sTxMailBox[0].TIR = out | STM32_CAN_TIR_TXRQ;
  306.  
  307.     // Wait until the mailbox is empty
  308.     while(CAN1->sTxMailBox[0].TIR & 0x1UL && count++ < 1000000);
  309.  
  310.     // The mailbox don't becomes empty while loop
  311.     if (CAN1->sTxMailBox[0].TIR & 0x1UL) {
  312.         Serial.println("Send Fail");
  313.         Serial.print(" CAN1->ESR ");Serial.print(CAN1->ESR);
  314.         Serial.print(" CAN1->MSR ");Serial.print(CAN1->MSR);
  315.         Serial.print(" CAN1->TSR ");Serial.print(CAN1->TSR);
  316.         Serial.println();
  317.         if (CAN1->TSR & CAN_TSR_RQCP0) {Serial.println(" ERR CAN_TSR_RQCP0");}
  318.         if (CAN1->TSR & CAN_TSR_TXOK0) {Serial.println(" ERR CAN_TSR_TXOK0");}
  319.         if (CAN1->TSR & CAN_TSR_ALST0) {Serial.println(" ERR CAN_TSR_ALST0");}
  320.         if (CAN1->TSR & CAN_TSR_TERR0) {Serial.println(" ERR CAN_TSR_TERR0");}
  321.         if (CAN1->TSR & CAN_TSR_ABRQ0) {Serial.println(" ERR CAN_TSR_ABRQ0");}
  322.         if (CAN1->TSR & CAN_TSR_RQCP1) {Serial.println(" ERR CAN_TSR_RQCP1");}
  323.         if (CAN1->TSR & CAN_TSR_TXOK1) {Serial.println(" ERR CAN_TSR_TXOK1");}
  324.         if (CAN1->TSR & CAN_TSR_ALST1) {Serial.println(" ERR CAN_TSR_ALST1");}
  325.         if (CAN1->TSR & CAN_TSR_TERR1) {Serial.println(" ERR CAN_TSR_TERR1");}
  326.         if (CAN1->TSR & CAN_TSR_ABRQ1) {Serial.println(" ERR CAN_TSR_ABRQ1");}
  327.         if (CAN1->TSR & CAN_TSR_RQCP2) {Serial.println(" ERR CAN_TSR_RQCP2");}
  328.         if (CAN1->TSR & CAN_TSR_TXOK2) {Serial.println(" ERR CAN_TSR_TXOK2");}
  329.         if (CAN1->TSR & CAN_TSR_ALST2) {Serial.println(" ERR CAN_TSR_ALST2");}
  330.         if (CAN1->TSR & CAN_TSR_TERR2) {Serial.println(" ERR CAN_TSR_TERR2");}
  331.         if (CAN1->TSR & CAN_TSR_ABRQ2) {Serial.println(" ERR CAN_TSR_ABRQ2");}
  332.         if (CAN1->TSR & CAN_TSR_CODE) {Serial.println(" ERR CAN_TSR_CODE");}
  333.         if (CAN1->TSR & CAN_TSR_TME) {Serial.println(" ERR CAN_TSR_TME");}
  334.         if (CAN1->TSR & CAN_TSR_TME0) {Serial.println(" ERR CAN_TSR_TME0");}
  335.         if (CAN1->TSR & CAN_TSR_TME1) {Serial.println(" ERR CAN_TSR_TME1");}
  336.         if (CAN1->TSR & CAN_TSR_TME2) {Serial.println(" ERR CAN_TSR_TME2");}
  337.         if (CAN1->TSR & CAN_TSR_LOW) {Serial.println(" ERR CAN_TSR_LOW");}
  338.         if (CAN1->TSR & CAN_TSR_LOW0) {Serial.println(" ERR CAN_TSR_LOW0");}
  339.         if (CAN1->TSR & CAN_TSR_LOW1) {Serial.println(" ERR CAN_TSR_LOW1");}
  340.         if (CAN1->TSR & CAN_TSR_LOW2) {Serial.println(" ERR CAN_TSR_LOW2");}
  341.  
  342.     }else {
  343.         Serial.println("Send OK");
  344.     }
  345. }
  346.  
  347. /**
  348.  * Returns whether there are CAN messages available.
  349.  *
  350.  * @returns If pending CAN messages are in the CAN controller
  351.  *
  352.  */
  353. uint8_t CANMsgAvail(void)
  354. {
  355.     // Check for pending FIFO 0 messages
  356.     return CAN1->RF0R & 0x3UL;
  357. }
  358. /******************************************************************************
  359. ************************ CANBUS FUNCTIONS END *********************************
  360. ******************************************************************************/
  361.  
  362.  
  363. //-------------------------------------------------------------------------------------
  364. //--------------------------- PROGRAM START -------------------------------------------
  365. //-------------------------------------------------------------------------------------
  366. #define INPUT_PIN_POTENTIOMETER      A0
  367. unsigned long previousMillis = 0;
  368. const long interval = 1000;
  369.  
  370.  
  371. void readCurrentMode()
  372. {
  373.     CAN_msg_t CAN_RX_msg;
  374.  
  375.     if(CANMsgAvail()) {
  376.         Serial.println("Received...");
  377.         CANReceive(&CAN_RX_msg);
  378.  
  379.         if (CAN_RX_msg.format == EXTENDED_FORMAT) {
  380.             Serial.print("Extended ID: 0x");
  381.             if (CAN_RX_msg.id < 0x10000000) Serial.print("0");
  382.             if (CAN_RX_msg.id < 0x1000000) Serial.print("00");
  383.             if (CAN_RX_msg.id < 0x100000) Serial.print("000");
  384.             if (CAN_RX_msg.id < 0x10000) Serial.print("0000");
  385.             Serial.print(CAN_RX_msg.id, HEX);
  386.         } else {
  387.             Serial.print("Standard ID: 0x");
  388.             if (CAN_RX_msg.id < 0x100) Serial.print("0");
  389.             if (CAN_RX_msg.id < 0x10) Serial.print("00");
  390.             Serial.print(CAN_RX_msg.id, HEX);
  391.             Serial.print("     ");
  392.         }
  393.  
  394.         Serial.print(" DLC: ");
  395.         Serial.print(CAN_RX_msg.len);
  396.         if (CAN_RX_msg.type == DATA_FRAME) {
  397.             Serial.print(" Data: ");
  398.             for(int i=0; i<CAN_RX_msg.len; i++) {
  399.                 Serial.print("0x");
  400.                 Serial.print(CAN_RX_msg.data[i], HEX);
  401.                 if (i != (CAN_RX_msg.len-1))  Serial.print(" ");
  402.             }
  403.             Serial.println();
  404.  
  405.             int16_t c1 = 0, c2 = 0, c3 = 0;
  406.             c1 = CAN_RX_msg.data[0] | (CAN_RX_msg.data[1] << 8);
  407.             c2 = CAN_RX_msg.data[2] | (CAN_RX_msg.data[3] << 8);
  408.             c3 = CAN_RX_msg.data[4] | (CAN_RX_msg.data[5] << 8);
  409.             double L1 = 0, L2 = 0, L3 = 0;
  410.             L1 = ((double)c1 / 10) * 240;
  411.             L2 = ((double)c2 / 10) * 240;
  412.             L3 = ((double)c3 / 10) * 240;
  413.             Serial.print(" C1 ");Serial.print((double)c1 / 10, 1);Serial.print("A L1 ");Serial.print((double)L1);Serial.println("W");
  414.             Serial.print(" C2 ");Serial.print((double)c2 / 10, 1);Serial.print("A L2 ");Serial.print((double)L2);Serial.println("W");
  415.             Serial.print(" C3 ");Serial.print((double)c3 / 10, 1);Serial.print("A L3 ");Serial.print((double)L3);Serial.println("W");
  416.             Serial.println();
  417.         } else {
  418.             Serial.println(" Data: REMOTE REQUEST FRAME");
  419.         }
  420.     }
  421. }
  422.  
  423.  
  424. void wallboxLimitPowerMode()
  425. {
  426.     unsigned long currentMillis = millis();
  427.     if (currentMillis - previousMillis >= interval) {
  428.         previousMillis = currentMillis;
  429.         Serial.println("\nWallboxLimitPowerMode...");
  430.        
  431.         int val = analogRead(INPUT_PIN_POTENTIOMETER);
  432.         Serial.print("  potentiometer: ");Serial.print(val);
  433.         Serial.println();
  434.         double v = (double)val / 1024;
  435.         Serial.print("  v: ");Serial.print(v, 1);
  436.         Serial.println();
  437.  
  438.         double current = v * 32; // 32A is max
  439.         Serial.print("  current: ");Serial.print(current, 1);Serial.print("A");
  440.         Serial.println();
  441.  
  442.         double power = current * 240;
  443.         Serial.print("  power: ");Serial.print(power, 1);Serial.print("W");
  444.         Serial.println();
  445.  
  446.         int16_t L1 = current * 10, L2 = current * 10, L3 = current * 10;
  447.  
  448.         CAN_msg_t CAN_TX_msg;
  449.  
  450.         CAN_TX_msg.type = DATA_FRAME;
  451.         CAN_TX_msg.format = EXTENDED_FORMAT;
  452.         CAN_TX_msg.id = 0x10045400;
  453.  
  454.         CAN_TX_msg.data[0] = L1 & ((1 << 8) - 1);
  455.         CAN_TX_msg.data[1] = L1 >> 8;
  456.         CAN_TX_msg.data[2] = L2 & ((1 << 8) - 1);
  457.         CAN_TX_msg.data[3] = L2 >> 8;
  458.         CAN_TX_msg.data[4] = L3 & ((1 << 8) - 1);
  459.         CAN_TX_msg.data[5] = L3 >> 8;
  460.        
  461.         CAN_TX_msg.len = 6;
  462.         Serial.println("  send...");
  463.         CANSend(&CAN_TX_msg);
  464.     }
  465. }
  466.  
  467.  
  468. void setup() {
  469.     Serial.begin(115200);
  470.     Serial.println("Start");
  471.  
  472.     pinMode(INPUT_PIN_POTENTIOMETER, INPUT_ANALOG);
  473.  
  474.     CAN_bit_timing_config_t timings;
  475.     const uint32_t peripheral_clock_rate = HAL_RCC_GetPCLK1Freq();
  476.     timings.baud_rate_prescaler = 21;
  477.     timings.time_segment_1 = 13;
  478.     timings.time_segment_2 = 2;
  479.     timings.resynchronization_jump_width = 1;
  480.  
  481.     Serial.print("peripheral_clock_rate="); Serial.println(peripheral_clock_rate);
  482.  
  483.     Serial.print("timings.baud_rate_prescaler="); Serial.println(timings.baud_rate_prescaler);
  484.     Serial.print("timings.time_segment_1="); Serial.println(timings.time_segment_1);
  485.     Serial.print("timings.time_segment_2="); Serial.println(timings.time_segment_2);
  486.     Serial.print("timings.resynchronization_jump_width="); Serial.println(timings.resynchronization_jump_width);
  487.  
  488.     double computed_bitrate = 1 / (1 / (double)peripheral_clock_rate * timings.baud_rate_prescaler * (timings.time_segment_1 + timings.time_segment_2 + timings.resynchronization_jump_width));
  489.     Serial.print("computed_bitrate="); Serial.println(computed_bitrate, 3);
  490.  
  491.     bool ret = CANInit(timings, 2);  // CAN_RX mapped to PB8, CAN_TX mapped to PB9
  492.     if (!ret) while(true);
  493. }
  494.  
  495.  
  496. void loop() {
  497.     // (un)comment to change mode
  498.  
  499.     // read house consumption from power guard device
  500.     readCurrentMode(); // <<<<<<--------------
  501.  
  502.     // simulate house consumption
  503.     //wallboxLimitPowerMode(); // <<<<<<--------------
  504. }
  505.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement