Skip to content

Commit

Permalink
Request handling, filters, port remaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeusz Magura-Witkowski committed Mar 8, 2019
1 parent 5b4684f commit 4caf029
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 249 deletions.
81 changes: 76 additions & 5 deletions Core/Inc/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,94 @@
#define _CAN_H

#include "stm32f1xx_hal.h"
/*
CONTROLLER -> ALLREADER
0 T T T T 0 0 0 0 0 0
#define CANPYBARA_DEVICE_ADDR_LEN 7
CONTROLLER -> READER
0 T T T T X X X X X X
CONTROLLER <- READER
1 T T T T X X X X X X
1 0 0 0 0 1 1 1 1 1 1
T T T T
-------
0 0 0 0
<- VERSIONINFO
Wersja, info, bardziej dla ludzi, 8 znakow
0 0 0 1
-> STATUSREQ [RTR]
<- STATUS
00 00 00 00 00 00 00
^ TX RX ERR
|
- mode (00 - normal, 01 - bootloader)
0 0 1 0
-> INRDREQ [RTR]
<- INRD
00
^- stan portu (8 bitow)
0 0 1 1
-> OUTRDREQ [RTR]
-> OUTSET
00
^- nowy stan portu
<- OUTRD
00
^- stan portu
0 1 0 0
-> SCANRESP
00 - OK
01 - REJECT
<- SCAN
00 00 00 00
^- id breloczka
0 1 0 1
<- BTNCLICK
00
^- id przycisku
*/

// Matches bit 11 (direction) and device ID (X part of addr)
#define CANPYBARA_DEVICE_ADDR_BITMASK 0x87E0

#define CANPYBARA_DEVICE_ADDR_LEN 6

#define CANPYBARA_CONTROLER_ADDR (canpybara_can_get_my_address() | 1<<10)
#define CANPYBARA_CLIENT_REPORT(id) (CANPYBARA_CONTROLER_ADDR | ((id & 0x7) << CANPYBARA_DEVICE_ADDR_LEN))
#define CANPYBARA_CLIENT_REPORT(id) (CANPYBARA_CONTROLER_ADDR | ((id & 0xF) << CANPYBARA_DEVICE_ADDR_LEN))

#define CANPYBARA_REQUEST_VERSIONINFO 0x0
#define CANPYBARA_REQUEST_STATUS 0x01
#define CANPYBARA_REQUEST_INRD 0x02
#define CANPYBARA_REQUEST_OUTSET 0x3

#define CANPYBARA_REPORT_INRD CANPYBARA_CLIENT_REPORT(0x2)
#define CANPYBARA_REPORT_OUTRD CANPYBARA_CLIENT_REPORT(0x3)
#define CANPYBARA_REPORT_VERSIONINFO CANPYBARA_CLIENT_REPORT(CANPYBARA_REQUEST_VERSIONINFO)
#define CANPYBARA_REPORT_STATUS CANPYBARA_CLIENT_REPORT(CANPYBARA_REQUEST_STATUS)
#define CANPYBARA_REPORT_INRD CANPYBARA_CLIENT_REPORT(CANPYBARA_REQUEST_INRD)
#define CANPYBARA_REPORT_OUTRD CANPYBARA_CLIENT_REPORT(CANPYBARA_REQUEST_OUTSET)

#define CANPYBARA_CONTROLLER_REQUESTID(id) ((id >> CANPYBARA_DEVICE_ADDR_LEN) & 0xF)
#define CANPYBARA_REPORT_OUTSET 0x3


void canpybara_configure_filters(CAN_HandleTypeDef* hcan);
void canpybara_can_init(void);

void canpybara_can_rx(CAN_HandleTypeDef* hcan);
void canpybara_can_tx_ready(CAN_HandleTypeDef* hcan);
void canpybara_can_tx(CanTxMsgTypeDef *can_tx);

void canpybara_can_tx_complete(void);
void canpybara_can_error(void);

uint16_t canpybara_can_get_my_address(void);

Expand Down
6 changes: 3 additions & 3 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* COPYRIGHT(c) 2018 STMicroelectronics
* COPYRIGHT(c) 2019 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -87,9 +87,9 @@
#define IN4_GPIO_Port GPIOB
#define IN5_Pin GPIO_PIN_5
#define IN5_GPIO_Port GPIOB
#define OUT0_Pin GPIO_PIN_8
#define OUT0_Pin GPIO_PIN_6
#define OUT0_GPIO_Port GPIOB
#define OUT1_Pin GPIO_PIN_9
#define OUT1_Pin GPIO_PIN_7
#define OUT1_GPIO_Port GPIOB

/* ########################## Assert Selection ############################## */
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/stm32f1xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
* <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/stm32f1xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
*
* COPYRIGHT(c) 2018 STMicroelectronics
* COPYRIGHT(c) 2019 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
Expand Down
6 changes: 6 additions & 0 deletions Core/Inc/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef VERSION_H
#define VERSION_H

void canpybara_version_report(void);

#endif
178 changes: 78 additions & 100 deletions Core/Src/can.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "main.h"
#include "logger.h"
#include "can.h"
#include "version.h"

#include "stm32f1xx_hal.h"

Expand All @@ -12,47 +13,6 @@ CanRxMsgTypeDef hcan_CAN_Rx0;
CanRxMsgTypeDef hcan_CAN_Rx1;



// struct canpybara_can_pending_message
// {
// uint8_t enabled : 1;
// uint8_t sending : 1;

// CanTxMsgTypeDef message;
// };

// #define MESSAGE_QUEUE_LENGTH 8
// struct canpybara_can_pending_message message_queue[MESSAGE_QUEUE_LENGTH];

// void canpybara_mesage_queue_clear(void)
// {
// int i;

// for (i = 0; i < MESSAGE_QUEUE_LENGTH; ++i)
// {
// message_queue[i].enabled = 0;
// message_queue[i].sending = 0;
// }
// }

// void canpybara_send_message(CanTxMsgTypeDef *message)
// {
// int i;

// for (i = 0; i < MESSAGE_QUEUE_LENGTH; ++i)
// {
// struct canpybara_can_pending_message *item = message_queue + i;

// if(item->enabled == 0 && item->sending == 0)
// {
// item->message = *message;
// item->enabled = 1;

// return;
// }
// }
// }

uint16_t canpybara_can_get_my_address(void)
{
uint16_t result = 0;
Expand All @@ -79,33 +39,19 @@ uint16_t canpybara_can_get_my_address(void)
return result;
}

// void canpybara_can_tx_ready(CAN_HandleTypeDef* hcan)
// {
// int i;

// for (i = 0; i < MESSAGE_QUEUE_LENGTH; ++i)
// {
// if(message_queue[i].sending)
// {
// message_queue[i].sending = 0;
// }

// if(message_queue[i].enabled)
// {
// hcan->pTxMsg = &message_queue[i].message;

// message_queue[i].sending = 1;
// message_queue[i].enabled = 0;

// HAL_StatusTypeDef result = HAL_CAN_Transmit_IT(hcan);
// if(result != HAL_OK)
// {
// LOG("HAL_CAN_Transmit_IT called status: %d", result);
// _Error_Handler(__FILE__, __LINE__);
// }
// }
// }
// }
uint16_t canpybara_tx_frames = 0;
uint16_t canpybara_rx_frames = 0;
uint16_t canpybara_errors = 0;

void canpybara_can_tx_complete(void)
{
canpybara_tx_frames++;
}

void canpybara_can_error(void)
{
canpybara_errors++;
}

void canpybara_configure_filters(CAN_HandleTypeDef* hcan)
{
Expand All @@ -116,13 +62,15 @@ void canpybara_configure_filters(CAN_HandleTypeDef* hcan)
filter_config.FilterMode = CAN_FILTERMODE_IDMASK;
filter_config.FilterScale = CAN_FILTERSCALE_16BIT;

// 5 for matching STDID

// Filter 1 : My addr
filter_config.FilterIdHigh = my_address << 5;
filter_config.FilterMaskIdHigh = 0x8FE0;
filter_config.FilterMaskIdHigh = CANPYBARA_DEVICE_ADDR_BITMASK;

// Filter 2 : Addr 0
filter_config.FilterIdLow = 0x000 << 5;
filter_config.FilterMaskIdLow = 0xFFFF;
filter_config.FilterMaskIdLow = CANPYBARA_DEVICE_ADDR_BITMASK;

filter_config.FilterFIFOAssignment = CAN_FILTER_FIFO0;
filter_config.FilterActivation = ENABLE;
Expand Down Expand Up @@ -152,50 +100,69 @@ void canpybara_can_init(void)
{
LOG("Initializing CAN");

// hcan.pTxMsg = &hcan_CAN_Tx;
hcan.pRxMsg = &hcan_CAN_Rx0;
hcan.pRx1Msg = &hcan_CAN_Rx1;

// canpybara_mesage_queue_clear();

canpybara_configure_filters(&hcan);
canpybara_reload_canrx(&hcan);
}

void capybara_can_report_status(void)
{
static CanTxMsgTypeDef can_tx;
can_tx.StdId = CANPYBARA_REPORT_STATUS;
can_tx.ExtId = 0;
can_tx.IDE = CAN_ID_STD;
can_tx.RTR = CAN_RTR_DATA;

can_tx.DLC = 7;
int i = 0;
can_tx.Data[i++] = 0x00; // normal mode

// TX
can_tx.Data[i++] = canpybara_tx_frames >> 8;
can_tx.Data[i++] = canpybara_tx_frames;

// RX
can_tx.Data[i++] = canpybara_rx_frames >> 8;
can_tx.Data[i++] = canpybara_rx_frames;

// ERR
can_tx.Data[i++] = canpybara_errors >> 8;
can_tx.Data[i++] = canpybara_errors;

canpybara_can_tx(&can_tx);
}

void canpybara_can_rx(CAN_HandleTypeDef* hcan)
{
// hcan->pRxMsg->StdId |= 1<<5;

// if(hcan->pRxMsg->StdId )
// {
// LOG("Rcv byte: %d", (int)hcan->pRxMsg->Data[0]);
// }

// hcan->pTxMsg->StdId = hcan->pRxMsg->StdId | 1<<10;
// hcan->pTxMsg->ExtId = 0;
// hcan->pTxMsg->IDE = CAN_ID_STD;
// hcan->pTxMsg->RTR = CAN_RTR_DATA;
// hcan->pTxMsg->DLC = hcan->pRxMsg->DLC;
// int i;
// for (i = 0; i < hcan->pRxMsg->DLC; ++i)
// {
// hcan->pTxMsg->Data[i] = hcan->pRxMsg->Data[i];
// }

// HAL_StatusTypeDef result = HAL_CAN_Transmit_IT(hcan);
// if(result != HAL_OK)
// {
// LOG("Reply to CAN msg result: %d", result);
// _Error_Handler(__FILE__, __LINE__);
// }
canpybara_rx_frames++;

int request_id = CANPYBARA_CONTROLLER_REQUESTID(hcan->pRxMsg->StdId);

// LOG("Request id: %d for %"PRIu32, request_id, hcan->pRxMsg->StdId);
LOG("Request id: %d for %"PRIu32, request_id, hcan->pRxMsg->StdId);

switch(request_id)
{
case CANPYBARA_REPORT_OUTSET:
case CANPYBARA_REQUEST_VERSIONINFO:
if (hcan->pRxMsg->RTR == CAN_RTR_REMOTE)
{
canpybara_version_report();
}
break;
case CANPYBARA_REQUEST_STATUS:
if (hcan->pRxMsg->RTR == CAN_RTR_REMOTE)
{
capybara_can_report_status();
}
break;
case CANPYBARA_REQUEST_INRD:
if (hcan->pRxMsg->RTR == CAN_RTR_REMOTE)
{
canpybara_gpio_report();
}
break;
case CANPYBARA_REQUEST_OUTSET:
if(hcan->pRxMsg->DLC == 1)
{
canpybara_gpio_handle_outset(hcan->pRxMsg->Data[0]);
Expand All @@ -205,10 +172,21 @@ void canpybara_can_rx(CAN_HandleTypeDef* hcan)
canpybara_gpio_handle_outrdreq();
}
break;
default:

default:
LOG("Unknown request ID: %d", request_id);
}

canpybara_reload_canrx(hcan);
}


void canpybara_can_tx(CanTxMsgTypeDef *can_tx)
{
hcan.pTxMsg = can_tx;

if(HAL_CAN_Transmit(&hcan, 50) != HAL_OK)
{
LOG("Can transmit failure");
}
}
Loading

0 comments on commit 4caf029

Please sign in to comment.