Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

body knee support #3

Merged
merged 18 commits into from
Aug 2, 2022
Merged
Prev Previous commit
Next Next commit
switch to i2c
  • Loading branch information
briskspirit committed Jun 23, 2022
commit d3afaf06ea1a7433e9bb5cdfd08725707c2c41ef
3 changes: 2 additions & 1 deletion board/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ c_sources = [
["hal_flash", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c"],
["hal_pwr", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c"],
["hal_rcc", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c"],
["hal_spi", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c"],
["hal_i2c", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c"],
["hal_i2c_ex", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c"],
["hal_tim", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c"],
["hal_tim_ex", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c"],
["hal_adc_ex", "inc/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c"],
Expand Down
15 changes: 7 additions & 8 deletions board/bldc/bldc.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
#include "BLDC_controller.h" /* Model's header file */
#include "rtwtypes.h"

extern hall_sensor hall_left;
extern hall_sensor hall_right;
extern board_t board;

extern RT_MODEL *const rtM_Left;
extern RT_MODEL *const rtM_Right;
Expand Down Expand Up @@ -169,9 +168,9 @@ void DMA2_Stream0_IRQHandler(void) {
enableFin = enable_motors && !rtY_Left.z_errCode && !rtY_Right.z_errCode;

// ========================= LEFT MOTOR ============================
uint8_t hall_ul = !(hall_left.hall_portA->IDR & hall_left.hall_pinA);
uint8_t hall_vl = !(hall_left.hall_portB->IDR & hall_left.hall_pinB);
uint8_t hall_wl = !(hall_left.hall_portC->IDR & hall_left.hall_pinC);
uint8_t hall_ul = !(board.hall_left.hall_portA->IDR & board.hall_left.hall_pinA);
uint8_t hall_vl = !(board.hall_left.hall_portB->IDR & board.hall_left.hall_pinB);
uint8_t hall_wl = !(board.hall_left.hall_portC->IDR & board.hall_left.hall_pinC);

rtU_Left.b_motEna = enableFin;
rtU_Left.z_ctrlModReq = ctrlModReq;
Expand Down Expand Up @@ -199,9 +198,9 @@ void DMA2_Stream0_IRQHandler(void) {


// ========================= RIGHT MOTOR ===========================
uint8_t hall_ur = !(hall_right.hall_portA->IDR & hall_right.hall_pinA);
uint8_t hall_vr = !(hall_right.hall_portB->IDR & hall_right.hall_pinB);
uint8_t hall_wr = !(hall_right.hall_portC->IDR & hall_right.hall_pinC);
uint8_t hall_ur = !(board.hall_right.hall_portA->IDR & board.hall_right.hall_pinA);
uint8_t hall_vr = !(board.hall_right.hall_portB->IDR & board.hall_right.hall_pinB);
uint8_t hall_wr = !(board.hall_right.hall_portC->IDR & board.hall_right.hall_pinC);

rtU_Right.b_motEna = enableFin;
rtU_Right.z_ctrlModReq = ctrlModReq;
Expand Down
109 changes: 71 additions & 38 deletions board/boards.h
Original file line number Diff line number Diff line change
@@ -1,46 +1,79 @@
extern hall_sensor hall_left;
extern hall_sensor hall_right;
uint32_t can_addr_offset;
board_t board;

void board_detect(void) {
hw_type = board_id();
// 0 = base, 1 = knee
// 0 = base, 3 = knee
if (hw_type == HW_TYPE_BASE) {
hall_left.hall_portA = GPIOC;
hall_left.hall_pinA = GPIO_PIN_13;
hall_left.hall_portB = GPIOC;
hall_left.hall_pinB = GPIO_PIN_14;
hall_left.hall_portC = GPIOC;
hall_left.hall_pinC = GPIO_PIN_15;

hall_right.hall_portA = GPIOC;
hall_right.hall_pinA = GPIO_PIN_10;
hall_right.hall_portB = GPIOC;
hall_right.hall_pinB = GPIO_PIN_11;
hall_right.hall_portC = GPIOC;
hall_right.hall_pinC = GPIO_PIN_12;

can_addr_offset = 0x0U;

MX_GPIO_LED_Base_Init();
board.hall_left.hall_portA = GPIOC;
board.hall_left.hall_pinA = GPIO_PIN_13;
board.hall_left.hall_portB = GPIOC;
board.hall_left.hall_pinB = GPIO_PIN_14;
board.hall_left.hall_portC = GPIOC;
board.hall_left.hall_pinC = GPIO_PIN_15;

board.hall_right.hall_portA = GPIOC;
board.hall_right.hall_pinA = GPIO_PIN_10;
board.hall_right.hall_portB = GPIOC;
board.hall_right.hall_pinB = GPIO_PIN_11;
board.hall_right.hall_portC = GPIOC;
board.hall_right.hall_pinC = GPIO_PIN_12;

board.CAN = CAN2;
board.can_alt_tx = GPIO_AF9_CAN2;
board.can_alt_rx = GPIO_AF9_CAN2;
board.can_pinRX = GPIO_PIN_5;
board.can_portRX = GPIOB;
board.can_pinTX = GPIO_PIN_6;
board.can_portTX = GPIOB;
board.can_pinEN = GPIO_PIN_7;
board.can_portEN = GPIOB;

board.led_pinR = GPIO_PIN_2;
board.led_portR = GPIOD;
board.led_pinG = GPIO_PIN_15;
board.led_portG = GPIOA;
board.led_pinB = GPIO_PIN_1;
board.led_portB = GPIOC;

board.can_addr_offset = 0x0U;

} else if (hw_type == HW_TYPE_KNEE) {
hall_left.hall_portA = GPIOC;
hall_left.hall_pinA = GPIO_PIN_14;
hall_left.hall_portB = GPIOC;
hall_left.hall_pinB = GPIO_PIN_15;
hall_left.hall_portC = GPIOC;
hall_left.hall_pinC = GPIO_PIN_13;

hall_right.hall_portA = GPIOD;
hall_right.hall_pinA = GPIO_PIN_2;
hall_right.hall_portB = GPIOC;
hall_right.hall_pinB = GPIO_PIN_0;
hall_right.hall_portC = GPIOC;
hall_right.hall_pinC = GPIO_PIN_1;

can_addr_offset = 0x100U;

MX_SPI3_Init();
board.hall_left.hall_portA = GPIOC;
board.hall_left.hall_pinA = GPIO_PIN_14;
board.hall_left.hall_portB = GPIOC;
board.hall_left.hall_pinB = GPIO_PIN_15;
board.hall_left.hall_portC = GPIOC;
board.hall_left.hall_pinC = GPIO_PIN_13;

board.hall_right.hall_portA = GPIOD;
board.hall_right.hall_pinA = GPIO_PIN_2;
board.hall_right.hall_portB = GPIOC;
board.hall_right.hall_pinB = GPIO_PIN_0;
board.hall_right.hall_portC = GPIOC;
board.hall_right.hall_pinC = GPIO_PIN_1;

board.CAN = CAN1;
board.can_alt_tx = GPIO_AF8_CAN1;
board.can_alt_rx = GPIO_AF9_CAN1;
board.can_pinRX = GPIO_PIN_11;
board.can_portRX = GPIOA;
board.can_pinTX = GPIO_PIN_9;
board.can_portTX = GPIOB;
board.can_pinEN = 0; // No pin, pulled down with 10k resistor
board.can_portEN = GPIOB;

board.led_pinR = GPIO_PIN_2;
board.led_portR = GPIOB;
board.led_pinG = GPIO_PIN_15;
board.led_portG = GPIOA;
board.led_pinB = GPIO_PIN_5;
board.led_portB = GPIOB;

board.can_addr_offset = 0x100U;

#ifndef BOOTSTUB
MX_I2C_Init();
#endif
} else {
// Fail to detect, halt
while(1) {}
Expand Down
6 changes: 2 additions & 4 deletions board/bootstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "drivers/llflash.h"
#include "provision.h"
#include "util.h"
#include "boards.h"

#include "flasher.h"

Expand Down Expand Up @@ -53,10 +54,7 @@ int main(void) {
SystemClock_Config();
MX_GPIO_Clocks_Init();

hw_type = board_id();
if (hw_type == HW_TYPE_BASE) {
MX_GPIO_LED_Base_Init();
}
board_detect();
MX_GPIO_Common_Init();

out_enable(POWERSWITCH, true);
Expand Down
70 changes: 44 additions & 26 deletions board/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern P rtP_Right;
extern volatile int16_t cmdL; // global variable for Left Command
extern volatile int16_t cmdR; // global variable for Right Command
extern uint8_t hw_type;
extern uint32_t can_addr_offset;
extern board_t board;

extern uint32_t enter_bootloader_mode;
extern volatile uint32_t torque_cmd_timeout;
Expand Down Expand Up @@ -85,38 +85,28 @@ void can_send_msg(uint32_t addr, uint32_t dhr, uint32_t dlr, uint8_t len) {

void process_can(void) {
CAN_FIFOMailBox_TypeDef to_send;
if (CAN2->TSR & CAN_TSR_TME0) {
if (board.CAN->TSR & CAN_TSR_TME0) {
if (can_pop(&can_tx_q, &to_send)) {
CAN2->sTxMailBox[0].TDLR = to_send.RDLR;
CAN2->sTxMailBox[0].TDHR = to_send.RDHR;
CAN2->sTxMailBox[0].TDTR = to_send.RDTR;
CAN2->sTxMailBox[0].TIR = to_send.RIR;
board.CAN->sTxMailBox[0].TDLR = to_send.RDLR;
board.CAN->sTxMailBox[0].TDHR = to_send.RDHR;
board.CAN->sTxMailBox[0].TDTR = to_send.RDTR;
board.CAN->sTxMailBox[0].TIR = to_send.RIR;
}
}
}

void CAN2_TX_IRQHandler(void) {
// clear interrupt
CAN2->TSR |= CAN_TSR_RQCP0;
process_can();
}

void CAN2_SCE_IRQHandler(void) {
llcan_clear_send(CAN2);
}

void CAN2_RX0_IRQHandler(void) {
while ((CAN2->RF0R & CAN_RF0R_FMP0) != 0) {
int address = CAN2->sFIFOMailBox[0].RIR >> 21;
if (address == (int32_t)(0x250U + can_addr_offset)) {
if ((GET_MAILBOX_BYTES_04(&CAN2->sFIFOMailBox[0]) == 0xdeadface) && (GET_MAILBOX_BYTES_48(&CAN2->sFIFOMailBox[0]) == 0x0ab00b1e)) {
void can_rx(void) {
while ((board.CAN->RF0R & CAN_RF0R_FMP0) != 0) {
int address = board.CAN->sFIFOMailBox[0].RIR >> 21;
if (address == (int32_t)(0x250U + board.can_addr_offset)) {
if ((GET_MAILBOX_BYTES_04(&board.CAN->sFIFOMailBox[0]) == 0xdeadface) && (GET_MAILBOX_BYTES_48(&board.CAN->sFIFOMailBox[0]) == 0x0ab00b1e)) {
enter_bootloader_mode = ENTER_SOFTLOADER_MAGIC;
NVIC_SystemReset();
}
#define MSG_TRQ_LEN 6
uint8_t dat[MSG_TRQ_LEN];
for (int i=0; i<MSG_TRQ_LEN; i++) {
dat[i] = GET_MAILBOX_BYTE(&CAN2->sFIFOMailBox[0], i);
dat[i] = GET_MAILBOX_BYTE(&board.CAN->sFIFOMailBox[0], i);
}
uint16_t valueL = ((dat[0] << 8U) | dat[1]);
uint16_t valueR = ((dat[2] << 8U) | dat[3]);
Expand All @@ -130,11 +120,11 @@ void CAN2_RX0_IRQHandler(void) {
}
current_idx = idx;
}
} else if (address == (int32_t)(0x251U + can_addr_offset)) {
} else if (address == (int32_t)(0x251U + board.can_addr_offset)) {
#define MSG_SPD_LEN 5
uint8_t dat[MSG_TRQ_LEN];
for (int i=0; i<MSG_TRQ_LEN; i++) {
dat[i] = GET_MAILBOX_BYTE(&CAN2->sFIFOMailBox[0], i);
dat[i] = GET_MAILBOX_BYTE(&board.CAN->sFIFOMailBox[0], i);
}
uint16_t valueL = ((dat[0] << 8U) | dat[1]);
uint16_t valueR = ((dat[2] << 8U) | dat[3]);
Expand All @@ -148,12 +138,40 @@ void CAN2_RX0_IRQHandler(void) {
}
}
} else if ((hw_type == HW_TYPE_BASE) && ((address == BROADCAST_ADDR) || (address == FALLBACK_ADDR) || (address == ECU_ADDR) || (address == DEBUG_ADDR))) { // Process UBS and OBD2 requests, ignore for knee
process_uds(address, GET_MAILBOX_BYTES_04(&CAN2->sFIFOMailBox[0]));
process_uds(address, GET_MAILBOX_BYTES_04(&board.CAN->sFIFOMailBox[0]));
}
out_enable(LED_BLUE, true);
// next
CAN2->RF0R |= CAN_RF0R_RFOM0;
board.CAN->RF0R |= CAN_RF0R_RFOM0;
}
}

void CAN1_TX_IRQHandler(void) {
// clear interrupt
board.CAN->TSR |= CAN_TSR_RQCP0;
process_can();
}

void CAN1_SCE_IRQHandler(void) {
llcan_clear_send(board.CAN);
}

void CAN1_RX0_IRQHandler(void) {
can_rx();
}

void CAN2_TX_IRQHandler(void) {
// clear interrupt
board.CAN->TSR |= CAN_TSR_RQCP0;
process_can();
}

void CAN2_SCE_IRQHandler(void) {
llcan_clear_send(board.CAN);
}

void CAN2_RX0_IRQHandler(void) {
can_rx();
}

#endif
50 changes: 26 additions & 24 deletions board/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,6 @@
#define BATT_PIN GPIO_PIN_4
#define BATT_PORT GPIOA

#define SPI3_SCK_PIN GPIO_PIN_10
#define SPI3_MISO_PIN GPIO_PIN_11
#define SPI3_MOSI_PIN GPIO_PIN_12
#define SPI3_PORT GPIOC

#define AS5048_CS_PORT GPIOB
#define AS5048A_CS_PIN GPIO_PIN_2

// GREEN
#define LED_GREEN_PIN GPIO_PIN_15
#define LED_GREEN_PORT GPIOA

// RED (only for base)
#define LED_RED_PIN GPIO_PIN_2
#define LED_RED_PORT GPIOD

// BLUE (only for base)
#define LED_BLUE_PIN GPIO_PIN_1
#define LED_BLUE_PORT GPIOC

#define CAN_STBY_PIN GPIO_PIN_7
#define CAN_STBY_PORT GPIOB

#define IGNITION_PIN GPIO_PIN_9
#define IGNITION_PORT GPIOB

Expand Down Expand Up @@ -148,7 +125,7 @@
#define TRANSCEIVER 5

#define HW_TYPE_BASE 0
#define HW_TYPE_KNEE 1
#define HW_TYPE_KNEE 3

typedef struct {
uint32_t rrB;
Expand All @@ -170,6 +147,31 @@ typedef struct {
uint16_t hall_pinC;
} hall_sensor;

typedef struct {
hall_sensor hall_left;
hall_sensor hall_right;

CAN_TypeDef* CAN;
uint8_t can_alt_tx;
uint8_t can_alt_rx;
GPIO_TypeDef* can_portTX;
uint16_t can_pinTX;
GPIO_TypeDef* can_portRX;
uint16_t can_pinRX;
GPIO_TypeDef* can_portEN;
uint16_t can_pinEN;

uint32_t can_addr_offset;

GPIO_TypeDef* led_portR;
uint16_t led_pinR;
GPIO_TypeDef* led_portG;
uint16_t led_pinG;
GPIO_TypeDef* led_portB;
uint16_t led_pinB;

} board_t;

uint8_t hw_type; // type of the board detected(0 - base, 1 - knee)

#endif // DEFINES_H
Loading