added silent mode, moved GPIO init

This commit is contained in:
Eric Evenchick 2015-05-14 17:41:28 -04:00
parent 6e7e7cad80
commit 0ab75d742d
4 changed files with 57 additions and 28 deletions

View File

@ -20,4 +20,4 @@ before_script:
script: make BUILD_NUMBER=$TRAVIS_BUILD_NUMBER
after_success:
"artifacts upload --target-paths=builds/b$TRAVIS_BUILD_NUMBER build/CANtact*"
"artifacts upload --target-paths=builds/b$TRAVIS_BUILD_NUMBER build/CANtact*"

View File

@ -2,9 +2,15 @@
#define _CAN_H
enum can_bitrate {
CAN_BITRATE_1000K,
CAN_BITRATE_500K,
CAN_BITRATE_250K,
CAN_BITRATE_125K,
CAN_BITRATE_100K,
CAN_BITRATE_83K3,
CAN_BITRATE_62K5,
CAN_BITRATE_50K,
CAN_BITRATE_33K3,
};
enum can_bus_state {

View File

@ -33,46 +33,75 @@ void can_init(void) {
hcan.Init.TXFP = DISABLE;
bus_state = OFF_BUS;
//status = HAL_CAN_Init(&hcan);
}
void can_enable(void) {
uint32_t status;
if (bus_state == OFF_BUS) {
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.pTxMsg = NULL;
status = HAL_CAN_Init(&hcan);
status = HAL_CAN_ConfigFilter(&hcan, &filter);
bus_state = ON_BUS;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.pTxMsg = NULL;
status = HAL_CAN_Init(&hcan);
status = HAL_CAN_ConfigFilter(&hcan, &filter);
bus_state = ON_BUS;
}
}
void can_disable(void) {
uint32_t status;
if (bus_state == ON_BUS) {
// do a bxCAN reset (set RESET bit to 1)
hcan.Instance->MCR |= CAN_MCR_RESET;
bus_state = OFF_BUS;
// do a bxCAN reset (set RESET bit to 1)
hcan.Instance->MCR |= CAN_MCR_RESET;
bus_state = OFF_BUS;
}
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
}
void can_set_bitrate(enum can_bitrate bitrate) {
if (bus_state == ON_BUS) {
// cannot set bitrate while on bus
return;
// cannot set bitrate while on bus
return;
}
switch (bitrate) {
case CAN_BITRATE_500K:
hcan.Init.Prescaler = 12;
break;
case CAN_BITRATE_250K:
hcan.Init.Prescaler = 24;
break;
case CAN_BITRATE_125K:
hcan.Init.Prescaler = 48;
break;
case CAN_BITRATE_1000K:
// TODO
break;
case CAN_BITRATE_500K:
hcan.Init.Prescaler = 12;
break;
case CAN_BITRATE_250K:
hcan.Init.Prescaler = 24;
break;
case CAN_BITRATE_125K:
hcan.Init.Prescaler = 1;
break;
case CAN_BITRATE_100K:
// TODO
break;
case CAN_BITRATE_83K3:
// TODO
break;
case CAN_BITRATE_62K5:
// TODO
break;
case CAN_BITRATE_50K:
// TODO
break;
case CAN_BITRATE_33K3:
// TODO
break;
}
}
void can_set_silent(uint8_t silent) {
if (bus_state == ON_BUS) {
// cannot set silent mode while on bus
return;
}
if (silent) {
hcan.Init.Mode = CAN_MODE_SILENT;
} else {
hcan.Init.Mode = CAN_MODE_NORMAL;
}
}
@ -100,7 +129,7 @@ uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout) {
uint8_t is_can_msg_pending(uint8_t fifo) {
if (bus_state == OFF_BUS) {
return 0;
return 0;
}
return (__HAL_CAN_MSG_PENDING(&hcan, fifo) > 0);
}

View File

@ -81,12 +81,6 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN CAN_MspInit 1 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStruct.Alternate = 0;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE END CAN_MspInit 1 */
}