Skip to content

Commit

Permalink
Add slave support + python lib draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz-Juranek committed Mar 16, 2019
1 parent f3830a5 commit 5b36b02
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 109 deletions.
130 changes: 65 additions & 65 deletions embedded/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -2213,71 +2213,71 @@ HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
*/
void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
{
/* Check whether the set of advanced features to configure is properly set */
assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));

/* if required, configure TX pin active level inversion */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
{
assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
}

/* if required, configure RX pin active level inversion */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
{
assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
}

/* if required, configure data inversion */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
{
assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
}

/* if required, configure RX/TX pins swap */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
{
assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
}

/* if required, configure RX overrun detection disabling */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
{
assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
}

/* if required, configure DMA disabling on reception error */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
{
assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
}

/* if required, configure auto Baud rate detection scheme */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
{
assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
/* set auto Baudrate detection parameters if detection is enabled */
if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
{
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
}
}

/* if required, configure MSB first on communication line */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
{
assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
}
// /* Check whether the set of advanced features to configure is properly set */
// assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
//
// /* if required, configure TX pin active level inversion */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
// {
// assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
// }
//
// /* if required, configure RX pin active level inversion */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
// {
// assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
// }
//
// /* if required, configure data inversion */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
// {
// assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
// }
//
// /* if required, configure RX/TX pins swap */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
// {
// assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
// }
//
// /* if required, configure RX overrun detection disabling */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
// {
// assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
// MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
// }
//
// /* if required, configure DMA disabling on reception error */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
// {
// assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
// MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
// }
//
// /* if required, configure auto Baud rate detection scheme */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
// {
// assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
// assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
// /* set auto Baudrate detection parameters if detection is enabled */
// if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
// {
// assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
// }
// }
//
// /* if required, configure MSB first on communication line */
// if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
// {
// assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
// MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
// }
}

/**
Expand Down
23 changes: 18 additions & 5 deletions embedded/Src/lin_slcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ t_master_frame_table_item* slcan_get_master_table_row(open_lin_pid_t id, int8_t*
(*out_index) = i;
return &master_frame_table[i];
}

/*
* t016 3 112233 : id 0x16, dlc 3, data 0x11 0x22 0x33
* T02BA015 0 2 AA55 : extended id 0x2B period 0xA0 timeout 0x15 , dlc 2, data 0xAA 0x55
Expand All @@ -36,6 +37,8 @@ t_master_frame_table_item* slcan_get_master_table_row(open_lin_pid_t id, int8_t*
//T013151502AA55
//t0163112233

void open_lin_net_init(t_master_frame_table_item *a_slot_array, l_u8 a_slot_array_len);

uint8_t addLinMasterRow(uint8_t* line) {
uint32_t temp;
int8_t i,out_index;
Expand All @@ -55,18 +58,21 @@ uint8_t addLinMasterRow(uint8_t* line) {

// start sending
if (line[1] == '1'){
open_lin_master_dl_init(master_frame_table,master_frame_table_size);
if (lin_type == LIN_SLAVE)
{
open_lin_net_init(master_frame_table,master_frame_table_size);
} else {
open_lin_master_dl_init(master_frame_table,master_frame_table_size);
//wakeUpLin();
}
slcan_state = SLCAN_STATE_OPEN;
if (lin_type == LIN_MASTER){
//wakeUpLin();
}
return 1;
}

// id
if (!parseHex(&line[2], 2, &temp)) return 0;
array_ptr = slcan_get_master_table_row(temp, &out_index);

array_ptr = slcan_get_master_table_row(temp, &out_index);
array_ptr->slot.pid= temp;
// len
if (!parseHex(&line[4 + offset], 1, &temp)) return 0;
Expand Down Expand Up @@ -133,6 +139,11 @@ void lin_slcan_rx_handler(t_open_lin_data_layer_frame *f)
slcanReciveCanFrame(&lin_slcan_slot);
}

void open_lin_on_rx_frame(open_lin_frame_slot_t *slot)
{
slcanReciveCanFrame(slot);
}

void lin_slcan_rx_timeout_handler()
{
if (slcan_state == SLCAN_STATE_OPEN)
Expand All @@ -157,6 +168,8 @@ void lin_slcan_rx_timeout_handler()
lin_slcan_reset();
}



void lin_slcan_skip_header_reception(uint8_t pid)
{
open_lin_hw_reset();
Expand Down
1 change: 1 addition & 0 deletions embedded/Src/lin_slcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ uint8_t addLinMasterRow(uint8_t* line);
void lin_slcan_rx(l_u8 rx_byte);
void lin_slcan_rx_timeout_handler();
void lin_slcan_skip_header_reception(uint8_t pid);
void lin_slcan_reset();
#endif /* LIN_SLCAN_H_ */
23 changes: 18 additions & 5 deletions embedded/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "slcan.h"
#include "lin_slcan.h"


/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
Expand Down Expand Up @@ -86,6 +87,12 @@ uint32_t lin_baund_rate = 19200;
*
* @retval None
*/



void bootloaderSwitcher();


int main(void)
{
/* USER CODE BEGIN 1 */
Expand All @@ -94,7 +101,8 @@ int main(void)

/* MCU Configuration----------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
/* Reset of all periph
* erals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */
Expand Down Expand Up @@ -230,11 +238,16 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
uint8_t rbyte = Uart2RxFifo;
if (slcan_state == SLCAN_STATE_OPEN)
{
if (lin_type == LIN_MASTER)
switch (lin_type)
{
open_lin_master_dl_rx(rbyte);
} else {
lin_slcan_rx(rbyte);
case LIN_MASTER:
open_lin_master_dl_rx(rbyte);
break;
case LIN_SLAVE:
open_lin_slave_rx_header(rbyte);
default: /* Monitor */
lin_slcan_rx(rbyte);
break;
}
}
HAL_UART_Receive_IT(huart, &Uart2RxFifo, 1);
Expand Down
81 changes: 50 additions & 31 deletions embedded/Src/slcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

extern int32_t serialNumber;

void RebootToBootloader();
uint8_t slcan_state = SLCAN_STATE_CONFIG;
LinType_t lin_type = LIN_SLAVE;
LinType_t lin_type = LIN_MONITOR;
static uint8_t terminator = SLCAN_CR;

extern USBD_HandleTypeDef hUsbDeviceFS;
Expand Down Expand Up @@ -138,28 +139,34 @@ static uint8_t transmitStd(uint8_t* line) {
uint32_t temp;
open_lin_frame_slot_t slot;
uint8_t data_buff[8];
bool lin_data = (line[0] == 't');
uint8_t offset = 0;

bool lin_data = ((line[0] == 't') || (line[0] == 'T'));

slot.data_ptr = data_buff;
if (line[0] > 'Z') return 0;
if (line[0] < 'Z')
offset = 5;
// id
if (!parseHex(&line[1], 3, &temp)) return 0;
if (!parseHex(&line[2 + offset], 2, &temp)) return 0;
slot.pid = open_lin_data_layer_parity((open_lin_pid_t)temp); // add parity
// len
if (!parseHex(&line[4], 1, &temp)) return 0;
if (!parseHex(&line[4 + offset], 1, &temp)) return 0;
slot.data_length = temp;

if (slot.data_length > 8) return 0;
if (lin_data)
{
uint8_t i;
for (i = 0; i < slot.data_length; i++) {
if (!parseHex(&line[5 + i*2], 2, &temp)) return 0;
if (!parseHex(&line[5 + offset + i*2], 2, &temp)) return 0;
slot.data_ptr[i] = temp;
}
}

open_lin_master_data_tx_header(&slot);
if (offset == 0)
{
open_lin_master_data_tx_header(&slot);
}
if (lin_data)
{
open_lin_master_data_tx_data(&slot);
Expand All @@ -183,11 +190,10 @@ void slCanCheckCommand()
{
uint8_t result = SLCAN_BELL;
uint8_t *line = command;
if (line[0] == 0)
{
return ;
}

switch (line[0]) {
case 0:
return;
case 'a':
{
if (terminator == SLCAN_CR)
Expand Down Expand Up @@ -249,12 +255,19 @@ void slCanCheckCommand()
result = terminator;
}
break;
case 'L': // Slave mode
case 'l':
case 'L': // slave mode
if (slcan_state == SLCAN_STATE_CONFIG){
result = terminator;
lin_type = LIN_SLAVE;
slcan_state = SLCAN_STATE_OPEN;
open_lin_hw_reset();
lin_slcan_reset();
}
case 'l': // monitor
if (slcan_state == SLCAN_STATE_CONFIG)
{
result = terminator;
lin_type = LIN_SLAVE;
lin_type = LIN_MONITOR;
slcan_state = SLCAN_STATE_OPEN;
open_lin_hw_reset();
lin_slcan_reset();
Expand All @@ -271,23 +284,29 @@ void slCanCheckCommand()
case 'r': // Transmit header
case 'T':
case 't': // Transmit full frame
if (lin_type == LIN_MASTER)
{
addLinMasterRow(line);
if (line[0] < 'Z') slcanSetOutputChar('Z');
else slcanSetOutputChar('z');
result = terminator;
} else /* Lin_Monitor */
{
if (slcan_state == SLCAN_STATE_OPEN)
{
if (transmitStd(line) == HAL_OK) {
if (line[0] < 'Z') slcanSetOutputChar('Z');
else slcanSetOutputChar('z');
result = terminator;
}
}
}
switch (lin_type)
{
case LIN_MASTER:
case LIN_SLAVE:
if (addLinMasterRow(line) == 1){
if (line[0] < 'Z') slcanSetOutputChar('Z');
else slcanSetOutputChar('z');
}
result = terminator;
break;
case LIN_MONITOR:
if (slcan_state == SLCAN_STATE_OPEN)
{
if (transmitStd(line) == 1) {
if (line[0] < 'Z') slcanSetOutputChar('Z');
else slcanSetOutputChar('z');
result = terminator;
}
}
break;
default:
break;
}
break;
}

Expand Down
Loading

0 comments on commit 5b36b02

Please sign in to comment.