Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- /******************************************************************************
- ************************ CANBUS FUNCTIONS START *******************************
- ******************************************************************************/
- #define STM32_CAN_TIR_TXRQ (1U << 0U) // Bit 0: Transmit Mailbox Request
- #define STM32_CAN_RIR_RTR (1U << 1U) // Bit 1: Remote Transmission Request
- #define STM32_CAN_RIR_IDE (1U << 2U) // Bit 2: Identifier Extension
- #define STM32_CAN_TIR_RTR (1U << 1U) // Bit 1: Remote Transmission Request
- #define STM32_CAN_TIR_IDE (1U << 2U) // Bit 2: Identifier Extension
- #define CAN_EXT_ID_MASK 0x1FFFFFFFU
- #define CAN_STD_ID_MASK 0x000007FFU
- /* Symbolic names for formats of CAN message */
- typedef enum {STANDARD_FORMAT = 0, EXTENDED_FORMAT} CAN_FORMAT;
- /* Symbolic names for type of CAN message */
- typedef enum {DATA_FRAME = 0, REMOTE_FRAME} CAN_FRAME;
- typedef struct
- {
- uint32_t id; /* 29 bit identifier */
- uint8_t data[8]; /* Data field */
- uint8_t len; /* Length of data field in bytes */
- uint8_t ch; /* Object channel(Not use) */
- uint8_t format; /* 0 - STANDARD, 1- EXTENDED IDENTIFIER */
- uint8_t type; /* 0 - DATA FRAME, 1 - REMOTE FRAME */
- } CAN_msg_t;
- typedef struct
- {
- uint16_t baud_rate_prescaler; /// [1 to 1024]
- uint8_t time_segment_1; /// [1 to 16]
- uint8_t time_segment_2; /// [1 to 8]
- uint8_t resynchronization_jump_width; /// [1 to 4] (recommended value is 1)
- } CAN_bit_timing_config_t;
- /**
- * Initializes the CAN filter registers.
- *
- * @preconditions - This register can be written only when the filter initialization mode is set (FINIT=1) in the CAN_FMR register.
- * @params: index - Specified filter index. index 27:14 are available in connectivity line devices only.
- * @params: scale - Select filter scale.
- * 0: Dual 16-bit scale configuration
- * 1: Single 32-bit scale configuration
- * @params: mode - Select filter mode.
- * 0: Two 32-bit registers of filter bank x are in Identifier Mask mode
- * 1: Two 32-bit registers of filter bank x are in Identifier List mode
- * @params: fifo - Select filter assigned.
- * 0: Filter assigned to FIFO 0
- * 1: Filter assigned to FIFO 1
- * @params: bank1 - Filter bank register 1
- * @params: bank2 - Filter bank register 2
- *
- */
- void CANSetFilter(uint8_t index, uint8_t scale, uint8_t mode, uint8_t fifo, uint32_t bank1, uint32_t bank2) {
- if (index > 27) return;
- CAN1->FA1R &= ~(0x1UL<<index); // Deactivate filter
- if (scale == 0) {
- CAN1->FS1R &= ~(0x1UL<<index); // Set filter to Dual 16-bit scale configuration
- } else {
- CAN1->FS1R |= (0x1UL<<index); // Set filter to single 32 bit configuration
- }
- if (mode == 0) {
- CAN1->FM1R &= ~(0x1UL<<index); // Set filter to Mask mode
- } else {
- CAN1->FM1R |= (0x1UL<<index); // Set filter to List mode
- }
- if (fifo == 0) {
- CAN1->FFA1R &= ~(0x1UL<<index); // Set filter assigned to FIFO 0
- } else {
- CAN1->FFA1R |= (0x1UL<<index); // Set filter assigned to FIFO 1
- }
- CAN1->sFilterRegister[index].FR1 = bank1; // Set filter bank registers1
- CAN1->sFilterRegister[index].FR2 = bank2; // Set filter bank registers2
- CAN1->FA1R |= (0x1UL<<index); // Activate filter
- }
- /**
- * Initializes the CAN controller with specified bit rate.
- *
- * @params: timings - Specified timings
- * @params: remap - Select CAN port.
- * =0:CAN_RX mapped to PA11, CAN_TX mapped to PA12
- * =1:Not used
- * =2:CAN_RX mapped to PB8, CAN_TX mapped to PB9 (not available on 36-pin package)
- * =3:CAN_RX mapped to PD0, CAN_TX mapped to PD1 (available on 100-pin and 144-pin package)
- *
- */
- bool CANInit(CAN_bit_timing_config_t &timings, int remap)
- {
- // Reference manual
- // 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
- RCC->APB1ENR |= 0x2000000UL; // Enable CAN clock
- RCC->APB2ENR |= 0x1UL; // Enable AFIO clock
- AFIO->MAPR &= 0xFFFF9FFF; // reset CAN remap
- // CAN_RX mapped to PA11, CAN_TX mapped to PA12
- if (remap == 0) {
- RCC->APB2ENR |= 0x4UL; // Enable GPIOA clock
- GPIOA->CRH &= ~(0xFF000UL); // Configure PA12(0b0000) and PA11(0b0000)
- // 0b0000
- // MODE=00(Input mode)
- // CNF=00(Analog mode)
- GPIOA->CRH |= 0xB8FFFUL; // Configure PA12(0b1011) and PA11(0b1000)
- // 0b1011
- // MODE=11(Output mode, max speed 50 MHz)
- // CNF=10(Alternate function output Push-pull
- // 0b1000
- // MODE=00(Input mode)
- // CNF=10(Input with pull-up / pull-down)
- GPIOA->ODR |= 0x1UL << 12; // PA12 Upll-up
- }
- if (remap == 2) {
- AFIO->MAPR |= 0x00004000; // set CAN remap
- // CAN_RX mapped to PB8, CAN_TX mapped to PB9 (not available on 36-pin package)
- RCC->APB2ENR |= 0x8UL; // Enable GPIOB clock
- GPIOB->CRH &= ~(0xFFUL); // Configure PB9(0b0000) and PB8(0b0000)
- // 0b0000
- // MODE=00(Input mode)
- // CNF=00(Analog mode)
- GPIOB->CRH |= 0xB8UL; // Configure PB9(0b1011) and PB8(0b1000)
- // 0b1011
- // MODE=11(Output mode, max speed 50 MHz)
- // CNF=10(Alternate function output Push-pull
- // 0b1000
- // MODE=00(Input mode)
- // CNF=10(Input with pull-up / pull-down)
- GPIOB->ODR |= 0x1UL << 8; // PB8 Upll-up
- }
- if (remap == 3) {
- AFIO->MAPR |= 0x00005000; // set CAN remap
- // CAN_RX mapped to PD0, CAN_TX mapped to PD1 (available on 100-pin and 144-pin package)
- RCC->APB2ENR |= 0x20UL; // Enable GPIOD clock
- GPIOD->CRL &= ~(0xFFUL); // Configure PD1(0b0000) and PD0(0b0000)
- // 0b0000
- // MODE=00(Input mode)
- // CNF=00(Analog mode)
- GPIOD->CRH |= 0xB8UL; // Configure PD1(0b1011) and PD0(0b1000)
- // 0b1000
- // MODE=00(Input mode)
- // CNF=10(Input with pull-up / pull-down)
- // 0b1011
- // MODE=11(Output mode, max speed 50 MHz)
- // CNF=10(Alternate function output Push-pull
- GPIOD->ODR |= 0x1UL << 0; // PD0 Upll-up
- }
- CAN1->MCR |= 0x1UL; // Require CAN1 to Initialization mode
- while (!(CAN1->MSR & 0x1UL)); // Wait for Initialization mode
- //CAN1->MCR = 0x51UL; // Hardware initialization(No automatic retransmission)
- CAN1->MCR = 0x41UL; // Hardware initialization(With automatic retransmission)
- // Set bit timing register
- CAN1->BTR = (((timings.resynchronization_jump_width - 1U) & 3U) << 24U) |
- (((timings.time_segment_1 - 1U) & 15U) << 16U) |
- (((timings.time_segment_2 - 1U) & 7U) << 20U) |
- ((timings.baud_rate_prescaler - 1U) & 1023U);
- // Configure Filters to default values
- CAN1->FMR |= 0x1UL; // Set to filter initialization mode
- CAN1->FMR &= 0xFFFFC0FF; // Clear CAN2 start bank
- // bxCAN has 28 filters.
- // These filters are shared by both CAN1 and CAN2.
- // STM32F103 has only CAN1, so all 28 are used for CAN1
- CAN1->FMR |= 0x1C << 8; // Assign all filters to CAN1
- // Set fileter 0
- // Single 32-bit scale configuration
- // Two 32-bit registers of filter bank x are in Identifier Mask mode
- // Filter assigned to FIFO 0
- // Filter bank register to all 0
- CANSetFilter(0, 1, 0, 0, 0x0UL, 0x0UL);
- CAN1->FMR &= ~(0x1UL); // Deactivate initialization mode
- uint16_t TimeoutMilliseconds = 1000;
- bool can1 = false;
- CAN1->MCR &= ~(0x1UL); // Require CAN1 to normal mode
- // Wait for normal mode
- // If the connection is not correct, it will not return to normal mode.
- for (uint16_t wait_ack = 0; wait_ack < TimeoutMilliseconds; wait_ack++) {
- if ((CAN1->MSR & 0x1UL) == 0) {
- can1 = true;
- break;
- }
- delayMicroseconds(1000);
- }
- //Serial.print("can1=");
- //Serial.println(can1);
- if (can1) {
- Serial.println("CAN1 initialize ok");
- } else {
- Serial.println("CAN1 initialize fail!!");
- return false;
- }
- return true;
- }
- /**
- * Decodes CAN messages from the data registers and populates a
- * CAN message struct with the data fields.
- *
- * @preconditions A valid CAN message is received
- * @params CAN_rx_msg - CAN message structure for reception
- *
- */
- void CANReceive(CAN_msg_t* CAN_rx_msg)
- {
- uint32_t id = CAN1->sFIFOMailBox[0].RIR;
- if ((id & STM32_CAN_RIR_IDE) == 0) { // Standard frame format
- CAN_rx_msg->format = STANDARD_FORMAT;;
- CAN_rx_msg->id = (CAN_STD_ID_MASK & (id >> 21U));
- }
- else { // Extended frame format
- CAN_rx_msg->format = EXTENDED_FORMAT;;
- CAN_rx_msg->id = (CAN_EXT_ID_MASK & (id >> 3U));
- }
- if ((id & STM32_CAN_RIR_RTR) == 0) { // Data frame
- CAN_rx_msg->type = DATA_FRAME;
- }
- else { // Remote frame
- CAN_rx_msg->type = REMOTE_FRAME;
- }
- CAN_rx_msg->len = (CAN1->sFIFOMailBox[0].RDTR) & 0xFUL;
- CAN_rx_msg->data[0] = 0xFFUL & CAN1->sFIFOMailBox[0].RDLR;
- CAN_rx_msg->data[1] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDLR >> 8);
- CAN_rx_msg->data[2] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDLR >> 16);
- CAN_rx_msg->data[3] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDLR >> 24);
- CAN_rx_msg->data[4] = 0xFFUL & CAN1->sFIFOMailBox[0].RDHR;
- CAN_rx_msg->data[5] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDHR >> 8);
- CAN_rx_msg->data[6] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDHR >> 16);
- CAN_rx_msg->data[7] = 0xFFUL & (CAN1->sFIFOMailBox[0].RDHR >> 24);
- // Release FIFO 0 output mailbox.
- // Make the next incoming message available.
- CAN1->RF0R |= 0x20UL;
- }
- /**
- * Encodes CAN messages using the CAN message struct and populates the
- * data registers with the sent.
- *
- * @params CAN_tx_msg - CAN message structure for transmission
- *
- */
- void CANSend(CAN_msg_t* CAN_tx_msg)
- {
- volatile int count = 0;
- uint32_t out = 0;
- if (CAN_tx_msg->format == EXTENDED_FORMAT) { // Extended frame format
- out = ((CAN_tx_msg->id & CAN_EXT_ID_MASK) << 3U) | STM32_CAN_TIR_IDE;
- }
- else { // Standard frame format
- out = ((CAN_tx_msg->id & CAN_STD_ID_MASK) << 21U);
- }
- // Remote frame
- if (CAN_tx_msg->type == REMOTE_FRAME) {
- out |= STM32_CAN_TIR_RTR;
- }
- CAN1->sTxMailBox[0].TDTR &= ~(0xF);
- CAN1->sTxMailBox[0].TDTR |= CAN_tx_msg->len & 0xFUL;
- CAN1->sTxMailBox[0].TDLR = (((uint32_t) CAN_tx_msg->data[3] << 24) |
- ((uint32_t) CAN_tx_msg->data[2] << 16) |
- ((uint32_t) CAN_tx_msg->data[1] << 8) |
- ((uint32_t) CAN_tx_msg->data[0] ));
- CAN1->sTxMailBox[0].TDHR = (((uint32_t) CAN_tx_msg->data[7] << 24) |
- ((uint32_t) CAN_tx_msg->data[6] << 16) |
- ((uint32_t) CAN_tx_msg->data[5] << 8) |
- ((uint32_t) CAN_tx_msg->data[4] ));
- // Send Go
- CAN1->sTxMailBox[0].TIR = out | STM32_CAN_TIR_TXRQ;
- // Wait until the mailbox is empty
- while(CAN1->sTxMailBox[0].TIR & 0x1UL && count++ < 1000000);
- // The mailbox don't becomes empty while loop
- if (CAN1->sTxMailBox[0].TIR & 0x1UL) {
- Serial.println("Send Fail");
- Serial.print(" CAN1->ESR ");Serial.print(CAN1->ESR);
- Serial.print(" CAN1->MSR ");Serial.print(CAN1->MSR);
- Serial.print(" CAN1->TSR ");Serial.print(CAN1->TSR);
- Serial.println();
- if (CAN1->TSR & CAN_TSR_RQCP0) {Serial.println(" ERR CAN_TSR_RQCP0");}
- if (CAN1->TSR & CAN_TSR_TXOK0) {Serial.println(" ERR CAN_TSR_TXOK0");}
- if (CAN1->TSR & CAN_TSR_ALST0) {Serial.println(" ERR CAN_TSR_ALST0");}
- if (CAN1->TSR & CAN_TSR_TERR0) {Serial.println(" ERR CAN_TSR_TERR0");}
- if (CAN1->TSR & CAN_TSR_ABRQ0) {Serial.println(" ERR CAN_TSR_ABRQ0");}
- if (CAN1->TSR & CAN_TSR_RQCP1) {Serial.println(" ERR CAN_TSR_RQCP1");}
- if (CAN1->TSR & CAN_TSR_TXOK1) {Serial.println(" ERR CAN_TSR_TXOK1");}
- if (CAN1->TSR & CAN_TSR_ALST1) {Serial.println(" ERR CAN_TSR_ALST1");}
- if (CAN1->TSR & CAN_TSR_TERR1) {Serial.println(" ERR CAN_TSR_TERR1");}
- if (CAN1->TSR & CAN_TSR_ABRQ1) {Serial.println(" ERR CAN_TSR_ABRQ1");}
- if (CAN1->TSR & CAN_TSR_RQCP2) {Serial.println(" ERR CAN_TSR_RQCP2");}
- if (CAN1->TSR & CAN_TSR_TXOK2) {Serial.println(" ERR CAN_TSR_TXOK2");}
- if (CAN1->TSR & CAN_TSR_ALST2) {Serial.println(" ERR CAN_TSR_ALST2");}
- if (CAN1->TSR & CAN_TSR_TERR2) {Serial.println(" ERR CAN_TSR_TERR2");}
- if (CAN1->TSR & CAN_TSR_ABRQ2) {Serial.println(" ERR CAN_TSR_ABRQ2");}
- if (CAN1->TSR & CAN_TSR_CODE) {Serial.println(" ERR CAN_TSR_CODE");}
- if (CAN1->TSR & CAN_TSR_TME) {Serial.println(" ERR CAN_TSR_TME");}
- if (CAN1->TSR & CAN_TSR_TME0) {Serial.println(" ERR CAN_TSR_TME0");}
- if (CAN1->TSR & CAN_TSR_TME1) {Serial.println(" ERR CAN_TSR_TME1");}
- if (CAN1->TSR & CAN_TSR_TME2) {Serial.println(" ERR CAN_TSR_TME2");}
- if (CAN1->TSR & CAN_TSR_LOW) {Serial.println(" ERR CAN_TSR_LOW");}
- if (CAN1->TSR & CAN_TSR_LOW0) {Serial.println(" ERR CAN_TSR_LOW0");}
- if (CAN1->TSR & CAN_TSR_LOW1) {Serial.println(" ERR CAN_TSR_LOW1");}
- if (CAN1->TSR & CAN_TSR_LOW2) {Serial.println(" ERR CAN_TSR_LOW2");}
- }else {
- Serial.println("Send OK");
- }
- }
- /**
- * Returns whether there are CAN messages available.
- *
- * @returns If pending CAN messages are in the CAN controller
- *
- */
- uint8_t CANMsgAvail(void)
- {
- // Check for pending FIFO 0 messages
- return CAN1->RF0R & 0x3UL;
- }
- /******************************************************************************
- ************************ CANBUS FUNCTIONS END *********************************
- ******************************************************************************/
- //-------------------------------------------------------------------------------------
- //--------------------------- PROGRAM START -------------------------------------------
- //-------------------------------------------------------------------------------------
- #define INPUT_PIN_POTENTIOMETER A0
- unsigned long previousMillis = 0;
- const long interval = 1000;
- void readCurrentMode()
- {
- CAN_msg_t CAN_RX_msg;
- if(CANMsgAvail()) {
- Serial.println("Received...");
- CANReceive(&CAN_RX_msg);
- if (CAN_RX_msg.format == EXTENDED_FORMAT) {
- Serial.print("Extended ID: 0x");
- if (CAN_RX_msg.id < 0x10000000) Serial.print("0");
- if (CAN_RX_msg.id < 0x1000000) Serial.print("00");
- if (CAN_RX_msg.id < 0x100000) Serial.print("000");
- if (CAN_RX_msg.id < 0x10000) Serial.print("0000");
- Serial.print(CAN_RX_msg.id, HEX);
- } else {
- Serial.print("Standard ID: 0x");
- if (CAN_RX_msg.id < 0x100) Serial.print("0");
- if (CAN_RX_msg.id < 0x10) Serial.print("00");
- Serial.print(CAN_RX_msg.id, HEX);
- Serial.print(" ");
- }
- Serial.print(" DLC: ");
- Serial.print(CAN_RX_msg.len);
- if (CAN_RX_msg.type == DATA_FRAME) {
- Serial.print(" Data: ");
- for(int i=0; i<CAN_RX_msg.len; i++) {
- Serial.print("0x");
- Serial.print(CAN_RX_msg.data[i], HEX);
- if (i != (CAN_RX_msg.len-1)) Serial.print(" ");
- }
- Serial.println();
- int16_t c1 = 0, c2 = 0, c3 = 0;
- c1 = CAN_RX_msg.data[0] | (CAN_RX_msg.data[1] << 8);
- c2 = CAN_RX_msg.data[2] | (CAN_RX_msg.data[3] << 8);
- c3 = CAN_RX_msg.data[4] | (CAN_RX_msg.data[5] << 8);
- double L1 = 0, L2 = 0, L3 = 0;
- L1 = ((double)c1 / 10) * 240;
- L2 = ((double)c2 / 10) * 240;
- L3 = ((double)c3 / 10) * 240;
- Serial.print(" C1 ");Serial.print((double)c1 / 10, 1);Serial.print("A L1 ");Serial.print((double)L1);Serial.println("W");
- Serial.print(" C2 ");Serial.print((double)c2 / 10, 1);Serial.print("A L2 ");Serial.print((double)L2);Serial.println("W");
- Serial.print(" C3 ");Serial.print((double)c3 / 10, 1);Serial.print("A L3 ");Serial.print((double)L3);Serial.println("W");
- Serial.println();
- } else {
- Serial.println(" Data: REMOTE REQUEST FRAME");
- }
- }
- }
- void wallboxLimitPowerMode()
- {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- Serial.println("\nWallboxLimitPowerMode...");
- int val = analogRead(INPUT_PIN_POTENTIOMETER);
- Serial.print(" potentiometer: ");Serial.print(val);
- Serial.println();
- double v = (double)val / 1024;
- Serial.print(" v: ");Serial.print(v, 1);
- Serial.println();
- double current = v * 32; // 32A is max
- Serial.print(" current: ");Serial.print(current, 1);Serial.print("A");
- Serial.println();
- double power = current * 240;
- Serial.print(" power: ");Serial.print(power, 1);Serial.print("W");
- Serial.println();
- int16_t L1 = current * 10, L2 = current * 10, L3 = current * 10;
- CAN_msg_t CAN_TX_msg;
- CAN_TX_msg.type = DATA_FRAME;
- CAN_TX_msg.format = EXTENDED_FORMAT;
- CAN_TX_msg.id = 0x10045400;
- CAN_TX_msg.data[0] = L1 & ((1 << 8) - 1);
- CAN_TX_msg.data[1] = L1 >> 8;
- CAN_TX_msg.data[2] = L2 & ((1 << 8) - 1);
- CAN_TX_msg.data[3] = L2 >> 8;
- CAN_TX_msg.data[4] = L3 & ((1 << 8) - 1);
- CAN_TX_msg.data[5] = L3 >> 8;
- CAN_TX_msg.len = 6;
- Serial.println(" send...");
- CANSend(&CAN_TX_msg);
- }
- }
- void setup() {
- Serial.begin(115200);
- Serial.println("Start");
- pinMode(INPUT_PIN_POTENTIOMETER, INPUT_ANALOG);
- CAN_bit_timing_config_t timings;
- const uint32_t peripheral_clock_rate = HAL_RCC_GetPCLK1Freq();
- timings.baud_rate_prescaler = 21;
- timings.time_segment_1 = 13;
- timings.time_segment_2 = 2;
- timings.resynchronization_jump_width = 1;
- Serial.print("peripheral_clock_rate="); Serial.println(peripheral_clock_rate);
- Serial.print("timings.baud_rate_prescaler="); Serial.println(timings.baud_rate_prescaler);
- Serial.print("timings.time_segment_1="); Serial.println(timings.time_segment_1);
- Serial.print("timings.time_segment_2="); Serial.println(timings.time_segment_2);
- Serial.print("timings.resynchronization_jump_width="); Serial.println(timings.resynchronization_jump_width);
- 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));
- Serial.print("computed_bitrate="); Serial.println(computed_bitrate, 3);
- bool ret = CANInit(timings, 2); // CAN_RX mapped to PB8, CAN_TX mapped to PB9
- if (!ret) while(true);
- }
- void loop() {
- // (un)comment to change mode
- // read house consumption from power guard device
- readCurrentMode(); // <<<<<<--------------
- // simulate house consumption
- //wallboxLimitPowerMode(); // <<<<<<--------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement