mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/CAN-Demo.git
synced 2025-08-05 21:59:42 +00:00
working but messy
This commit is contained in:
@@ -23,6 +23,9 @@
|
||||
/* USER CODE BEGIN Includes */
|
||||
//#include <sta/devices/stm32/can.hpp>
|
||||
extern void testCan(CAN_HandleTypeDef * handle);
|
||||
extern void testCanMsg(CAN_HandleTypeDef * handle, uint8_t payload[8]);
|
||||
extern void unpackValues(uint8_t packedByte, uint8_t* type_id, uint8_t* sensor_ID, uint8_t* value, uint8_t* include);
|
||||
extern uint8_t packValues(uint8_t type_id, uint8_t sensor_ID, uint8_t value, uint8_t include);
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -35,8 +38,10 @@ extern void testCan(CAN_HandleTypeDef * handle);
|
||||
CAN_RxHeaderTypeDef rxHeader; //CAN Bus Transmit Header
|
||||
CAN_TxHeaderTypeDef txHeader; //CAN Bus Receive Header
|
||||
uint8_t canRX[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer
|
||||
CAN_FilterTypeDef canfil; //CAN Bus Filter
|
||||
CAN_FilterTypeDef canfilterconfig;
|
||||
uint32_t canMailbox; //CAN Bus Mail box variable
|
||||
|
||||
uint8_t received = 0;
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -99,19 +104,35 @@ int main(void)
|
||||
MX_CAN1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
txHeader.DLC = 8;
|
||||
/*txHeader.DLC = 8;
|
||||
txHeader.IDE = CAN_ID_STD;
|
||||
txHeader.RTR = CAN_RTR_DATA;
|
||||
txHeader.StdId = 0x030;
|
||||
txHeader.ExtId = 0x02;
|
||||
txHeader.TransmitGlobalTime = DISABLE;
|
||||
txHeader.TransmitGlobalTime = DISABLE;*/
|
||||
|
||||
HAL_CAN_ConfigFilter(&hcan1,&canfil);
|
||||
canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
|
||||
canfilterconfig.FilterBank = 18;
|
||||
canfilterconfig.FilterFIFOAssignment = CAN_RX_FIFO0;
|
||||
canfilterconfig.FilterIdHigh = 0;
|
||||
canfilterconfig.FilterIdLow = 0x466<<5;
|
||||
canfilterconfig.FilterMaskIdHigh = 0;
|
||||
canfilterconfig.FilterMaskIdLow = 0x466<<5;
|
||||
canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
|
||||
canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
|
||||
canfilterconfig.SlaveStartFilterBank = 20;
|
||||
|
||||
HAL_CAN_ConfigFilter(&hcan1, &canfilterconfig);
|
||||
HAL_CAN_Start(&hcan1);
|
||||
//HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
|
||||
|
||||
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
//HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12, GPIO_PIN_SET);
|
||||
//testCan(&hcan1);
|
||||
testCan(&hcan1);
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
@@ -124,18 +145,26 @@ int main(void)
|
||||
//HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12, GPIO_PIN_RESET);
|
||||
//HAL_Delay(100);
|
||||
//HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12, GPIO_PIN_SET);#
|
||||
//testCan(&hcan1);
|
||||
|
||||
HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rxHeader, canRX);
|
||||
/*HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rxHeader, canRX);
|
||||
if (canRX[0] != 0){
|
||||
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_8);
|
||||
HAL_Delay(1000);
|
||||
}
|
||||
|
||||
uint8_t csend[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
|
||||
HAL_CAN_AddTxMessage(&hcan1,&txHeader,csend,&canMailbox);
|
||||
//HAL_CAN_AddTxMessage(&hcan1,&txHeader,csend,&canMailbox);*/
|
||||
/*HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8, GPIO_PIN_RESET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8, GPIO_PIN_SET);*/
|
||||
|
||||
//testCanMsg(&hcan1, canRX);
|
||||
|
||||
if (received == 1){
|
||||
testCanMsg(&hcan1, canRX);
|
||||
received = 0;
|
||||
}
|
||||
HAL_Delay(1000);
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
@@ -282,7 +311,7 @@ static void MX_GPIO_Init(void)
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : PB12 PB13 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
|
||||
@@ -291,8 +320,8 @@ static void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PD8 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8;
|
||||
/*Configure GPIO pins : PD8 PD9 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
@@ -316,10 +345,22 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan1)
|
||||
{
|
||||
|
||||
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_8);
|
||||
HAL_Delay(1000);
|
||||
received = 1;
|
||||
|
||||
HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO0, &rxHeader, canRX);
|
||||
|
||||
/*uint8_t type_id, sensor_ID, value, include;
|
||||
|
||||
unpackValues(canRX, *type_id, *sensor_ID, *value, *include);
|
||||
|
||||
if (type_id == 0 && sensor_ID == 1){
|
||||
if (value == 0){
|
||||
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8, GPIO_PIN_RESET);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8, GPIO_PIN_SET);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -336,6 +377,8 @@ void Error_Handler(void)
|
||||
__disable_irq();
|
||||
while (1)
|
||||
{
|
||||
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_9);
|
||||
HAL_Delay(1000);
|
||||
}
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
|
Reference in New Issue
Block a user