🎨 代码格式化

Signed-off-by: DAVE <ro7enkranz@qq.com>
This commit is contained in:
DAVE 2022-03-29 17:24:25 +08:00
parent cd8bc7e8e6
commit 62c4a43894
23 changed files with 2314 additions and 2209 deletions

152
.clang-format Normal file
View File

@ -0,0 +1,152 @@
---
Language: Cpp
DisableFormat: false
# BasedOnStyle: WebKit
Standard: Latest
ColumnLimit: 180
UseTab: Never
TabWidth: 4
IndentWidth: 4
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
# The extra indent or outdent of access modifiers, e.g. public:.
AccessModifierOffset: -4
# DeriveLineEnding: true
UseCRLF: true
# DerivePointerAlignment: false
PointerAlignment: Right
# clang-format 14
ReferenceAlignment: Right
# clang-format 14
# PackConstructorInitializers: CurrentLine
# clang-format 14
# SeparateDefinitionBlocks: Always
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments
AlignConsecutiveBitFields: AcrossEmptyLinesAndComments
AlignConsecutiveDeclarations: AcrossComments
AlignConsecutiveMacros: AcrossComments
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
# AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
# trailing comma insertion cannot be used with bin packing
InsertTrailingCommas: Wrapped
BitFieldColonSpacing: Both
# Allman Custom
BreakBeforeBraces: Custom
# If BreakBeforeBraces is set to Custom,
# use this to specify how each individual brace case should be handled. Otherwise, this is ignored.
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeConceptDeclarations: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: false
CompactNamespaces: false
Cpp11BracedListStyle: false
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Merge
IndentAccessModifiers: false
IndentCaseBlocks: true
IndentCaseLabels: true
IndentExternBlock: AfterExternBlock
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequires: false
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
PenaltyBreakAssignment: 20
# The penalty for breaking a function call after call(
PenaltyBreakBeforeFirstCallParameter: 20
PenaltyBreakComment: 300
# The penalty for breaking before the first <<
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
ReflowComments: true
SortIncludes: CaseSensitive
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptControlMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInParentheses: false
SpacesInSquareBrackets: false
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

View File

@ -1,3 +1,4 @@
{
"makefile.extensionOutputFolder": "./.vscode"
"makefile.extensionOutputFolder": "./.vscode",
"editor.detectIndentation": false
}

View File

@ -2,27 +2,23 @@
#include <stm32f0xx_hal.h>
#define GPIO_NOAF (0u)
#define _PIN_INIT( _PORT, _PIN, _MODE, _PULL, _SPEED, _AF )\
HAL_GPIO_Init( GPIO##_PORT, (GPIO_InitTypeDef[])\
{{\
.Pin = GPIO_PIN_##_PIN,\
.Mode = GPIO_##_MODE,\
.Pull = GPIO_##_PULL,\
.Speed = GPIO_##_SPEED,\
.Alternate = GPIO_##_AF\
}} )
#define _PIN_HI( _PORT, _PIN, ... ) GPIO##_PORT->BSRR = (1u<<_PIN)
#define _PIN_LOW( _PORT, _PIN, ... ) GPIO##_PORT->BSRR = (0x10000u<<_PIN)
#define _PIN_TOGGLE( _PORT, _PIN, ... )\
do{\
uint32_t odr = GPIO##_PORT->ODR;\
GPIO##_PORT->BSRR = ((odr & (1u<<_PIN)) << 16u) | (~odr & (1u<<_PIN));\
}while(0)
#define _PIN_INIT(_PORT, _PIN, _MODE, _PULL, _SPEED, _AF) \
HAL_GPIO_Init(GPIO##_PORT, \
(GPIO_InitTypeDef[]) { \
{.Pin = GPIO_PIN_##_PIN, .Mode = GPIO_##_MODE, .Pull = GPIO_##_PULL, .Speed = GPIO_##_SPEED, .Alternate = GPIO_##_AF}
})
#define _PIN_HI(_PORT, _PIN, ...) GPIO##_PORT->BSRR = (1u << _PIN)
#define _PIN_LOW(_PORT, _PIN, ...) GPIO##_PORT->BSRR = (0x10000u << _PIN)
#define _PIN_TOGGLE(_PORT, _PIN, ...) \
do { \
uint32_t odr = GPIO##_PORT->ODR; \
GPIO##_PORT->BSRR = ((odr & (1u << _PIN)) << 16u) | (~odr & (1u << _PIN)); \
} while (0)
#define _PIN_ENABLE_CLOCK( _PORT, ... ) __HAL_RCC_GPIO ## _PORT ## _CLK_ENABLE()
#define _PIN_ENABLE_CLOCK(_PORT, ...) __HAL_RCC_GPIO##_PORT##_CLK_ENABLE()
#define PIN_HI( CONFIG ) _PIN_HI( CONFIG )
#define PIN_LOW( CONFIG ) _PIN_LOW( CONFIG )
#define PIN_TOGGLE( CONFIG ) _PIN_TOGGLE( CONFIG )
#define PIN_INIT( CONFIG ) _PIN_INIT( CONFIG )
#define PIN_ENABLE_CLOCK( CONFIG ) _PIN_ENABLE_CLOCK( CONFIG )
#define PIN_HI(CONFIG) _PIN_HI(CONFIG)
#define PIN_LOW(CONFIG) _PIN_LOW(CONFIG)
#define PIN_TOGGLE(CONFIG) _PIN_TOGGLE(CONFIG)
#define PIN_INIT(CONFIG) _PIN_INIT(CONFIG)
#define PIN_ENABLE_CLOCK(CONFIG) _PIN_ENABLE_CLOCK(CONFIG)

View File

@ -1,11 +1,11 @@
#include "stm32f0xx_hal.h"
#include "pcan_timestamp.h"
#include "pcan_led.h"
#include "pcan_protocol.h"
#include "pcan_timestamp.h"
#include "pcan_usb.h"
#include "pcan_varian.h"
#include "stm32f0xx_hal.h"
void HAL_MspInit( void )
void HAL_MspInit(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
@ -15,53 +15,55 @@ void HAL_MspInit( void )
__HAL_RCC_GPIOB_CLK_ENABLE();
}
#if ( HSE_VALUE != 0 )
#if (HSE_VALUE != 0)
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
/* enable HSE */
__HAL_RCC_HSE_CONFIG(RCC_HSE_ON);
while( __HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET );
while (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
;
/* enable PLL */
__HAL_RCC_PLL_DISABLE();
while( __HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET );
#if ( HSE_VALUE == 16000000 )
__HAL_RCC_PLL_CONFIG( RCC_PLLSOURCE_HSE, RCC_PREDIV_DIV1, RCC_PLL_MUL3 );
#elif ( HSE_VALUE == 8000000 )
__HAL_RCC_PLL_CONFIG( RCC_PLLSOURCE_HSE, RCC_PREDIV_DIV1, RCC_PLL_MUL6 );
while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
;
#if (HSE_VALUE == 16000000)
__HAL_RCC_PLL_CONFIG(RCC_PLLSOURCE_HSE, RCC_PREDIV_DIV1, RCC_PLL_MUL3);
#elif (HSE_VALUE == 8000000)
__HAL_RCC_PLL_CONFIG(RCC_PLLSOURCE_HSE, RCC_PREDIV_DIV1, RCC_PLL_MUL6);
#endif
__HAL_RCC_PLL_ENABLE();
while( __HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET );
while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_1 );
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
__HAL_RCC_USB_CONFIG( RCC_USBCLKSOURCE_PLL );
__HAL_RCC_USB_CONFIG(RCC_USBCLKSOURCE_PLL);
}
#else
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_CRSInitTypeDef RCC_CRSInitStruct = {0};
RCC_CRSInitTypeDef RCC_CRSInitStruct = { 0 };
__HAL_RCC_HSI48_ENABLE();
while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == RESET );
while (__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == RESET)
;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
__HAL_RCC_USB_CONFIG( RCC_USBCLKSOURCE_HSI48 );
__HAL_RCC_USB_CONFIG(RCC_USBCLKSOURCE_HSI48);
/* CRS */
__HAL_RCC_CRS_CLK_ENABLE();
@ -69,20 +71,20 @@ void SystemClock_Config(void)
RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;
RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB;
RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE( 48000000, 1000 );
RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000);
RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT;
RCC_CRSInitStruct.HSI48CalibrationValue = RCC_CRS_HSI48CALIBRATION_DEFAULT;
HAL_RCCEx_CRSConfig( &RCC_CRSInitStruct );
HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);
}
#endif
void SysTick_Handler( void )
void SysTick_Handler(void)
{
HAL_IncTick();
}
int main( void )
int main(void)
{
HAL_Init();
HAL_IncTick();
@ -96,10 +98,10 @@ int main( void )
pcan_timestamp_init();
pcan_protocol_init();
pcan_led_set_mode( LED_CH0_RX, LED_MODE_BLINK_SLOW, 0 );
pcan_led_set_mode( LED_CH0_TX, LED_MODE_BLINK_SLOW, 0 );
pcan_led_set_mode(LED_CH0_RX, LED_MODE_BLINK_SLOW, 0);
pcan_led_set_mode(LED_CH0_TX, LED_MODE_BLINK_SLOW, 0);
for(;;)
for (;;)
{
pcan_usb_poll();
pcan_led_poll();

View File

@ -1,20 +1,15 @@
#include <stm32f0xx_hal.h>
#include <string.h>
#include <assert.h>
#include "pcan_can.h"
#include "pcan_timestamp.h"
#include "pcan_varian.h"
#include <assert.h>
#include <stm32f0xx_hal.h>
#include <string.h>
#define CAN_TX_FIFO_SIZE (100)
static CAN_HandleTypeDef g_hcan = { .Instance = CAN };
#define INTERNAL_CAN_IT_FLAGS ( CAN_IT_TX_MAILBOX_EMPTY |\
CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_RX_FIFO1_MSG_PENDING |\
CAN_IT_BUSOFF |\
CAN_IT_ERROR_WARNING |\
CAN_IT_ERROR_PASSIVE |\
CAN_IT_BUSOFF |\
CAN_IT_LAST_ERROR_CODE |\
CAN_IT_ERROR )
#define INTERNAL_CAN_IT_FLAGS \
(CAN_IT_TX_MAILBOX_EMPTY | CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_RX_FIFO1_MSG_PENDING | CAN_IT_BUSOFF | CAN_IT_ERROR_WARNING | CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF \
| CAN_IT_LAST_ERROR_CODE | CAN_IT_ERROR)
static struct
{
@ -30,9 +25,8 @@ static struct
uint32_t tx_head;
uint32_t tx_tail;
void (*rx_cb)(can_message_t *);
void (*can_err_cb)( uint8_t err, uint8_t rx_err, uint8_t tx_err );
}
can_dev = { 0 };
void (*can_err_cb)(uint8_t err, uint8_t rx_err, uint8_t tx_err);
} can_dev = { 0 };
void pcan_can_init(void)
{
@ -40,13 +34,13 @@ void pcan_can_init(void)
__HAL_RCC_CAN1_CLK_ENABLE();
PIN_ENABLE_CLOCK( CAN_RX );
PIN_ENABLE_CLOCK( CAN_TX );
PIN_ENABLE_CLOCK(CAN_RX);
PIN_ENABLE_CLOCK(CAN_TX);
PIN_INIT( CAN_RX );
PIN_INIT( CAN_TX );
PIN_INIT(CAN_RX);
PIN_INIT(CAN_TX);
HAL_CAN_DeInit( &g_hcan );
HAL_CAN_DeInit(&g_hcan);
g_hcan.Instance = CAN;
g_hcan.Init.Prescaler = 16;
@ -61,9 +55,9 @@ void pcan_can_init(void)
g_hcan.Init.ReceiveFifoLocked = DISABLE;
g_hcan.Init.TransmitFifoPriority = ENABLE;
if( HAL_CAN_Init( &g_hcan ) != HAL_OK )
if (HAL_CAN_Init(&g_hcan) != HAL_OK)
{
assert( 0 );
assert(0);
}
filter.FilterMode = CAN_FILTERMODE_IDMASK;
@ -73,41 +67,29 @@ void pcan_can_init(void)
filter.FilterBank = 0;
filter.SlaveStartFilterBank = 0;
if( HAL_CAN_ConfigFilter( &g_hcan, &filter ) != HAL_OK )
if (HAL_CAN_ConfigFilter(&g_hcan, &filter) != HAL_OK)
{
assert( 0 );
assert(0);
}
if( HAL_CAN_ActivateNotification( &g_hcan, INTERNAL_CAN_IT_FLAGS ) != HAL_OK )
if (HAL_CAN_ActivateNotification(&g_hcan, INTERNAL_CAN_IT_FLAGS) != HAL_OK)
{
assert( 0 );
assert(0);
}
}
void pcan_can_set_bitrate( uint16_t brp, uint8_t tseg1, uint8_t tseg2, uint8_t sjw )
void pcan_can_set_bitrate(uint16_t brp, uint8_t tseg1, uint8_t tseg2, uint8_t sjw)
{
static const uint32_t sjw_table[] =
{
CAN_SJW_1TQ, CAN_SJW_2TQ, CAN_SJW_3TQ, CAN_SJW_4TQ
};
static const uint32_t tseg1_table[] =
{
CAN_BS1_1TQ, CAN_BS1_2TQ, CAN_BS1_3TQ, CAN_BS1_4TQ,
CAN_BS1_5TQ, CAN_BS1_6TQ, CAN_BS1_7TQ, CAN_BS1_8TQ,
CAN_BS1_9TQ, CAN_BS1_10TQ, CAN_BS1_11TQ, CAN_BS1_12TQ,
CAN_BS1_13TQ, CAN_BS1_14TQ, CAN_BS1_15TQ, CAN_BS1_16TQ
};
static const uint32_t tseg2_table[] =
{
CAN_BS2_1TQ, CAN_BS2_2TQ, CAN_BS2_3TQ, CAN_BS2_4TQ,
CAN_BS2_5TQ, CAN_BS2_6TQ, CAN_BS2_7TQ, CAN_BS2_8TQ
};
static const uint32_t sjw_table[] = { CAN_SJW_1TQ, CAN_SJW_2TQ, CAN_SJW_3TQ, CAN_SJW_4TQ };
static const uint32_t tseg1_table[] = { CAN_BS1_1TQ, CAN_BS1_2TQ, CAN_BS1_3TQ, CAN_BS1_4TQ, CAN_BS1_5TQ, CAN_BS1_6TQ, CAN_BS1_7TQ, CAN_BS1_8TQ,
CAN_BS1_9TQ, CAN_BS1_10TQ, CAN_BS1_11TQ, CAN_BS1_12TQ, CAN_BS1_13TQ, CAN_BS1_14TQ, CAN_BS1_15TQ, CAN_BS1_16TQ };
static const uint32_t tseg2_table[] = { CAN_BS2_1TQ, CAN_BS2_2TQ, CAN_BS2_3TQ, CAN_BS2_4TQ, CAN_BS2_5TQ, CAN_BS2_6TQ, CAN_BS2_7TQ, CAN_BS2_8TQ };
if( sjw > 4 )
if (sjw > 4)
sjw = 4;
if( tseg1 > 16 )
if (tseg1 > 16)
tseg1 = 16;
if( tseg2 > 8 )
if (tseg2 > 8)
tseg2 = 8;
/* CAN bus freq is 48 */
@ -117,28 +99,28 @@ void pcan_can_set_bitrate( uint16_t brp, uint8_t tseg1, uint8_t tseg2, uint8_t s
g_hcan.Init.TimeSeg1 = tseg1_table[tseg1 - 1];
g_hcan.Init.TimeSeg2 = tseg2_table[tseg2 - 1];
if( HAL_CAN_Init( &g_hcan ) != HAL_OK )
if (HAL_CAN_Init(&g_hcan) != HAL_OK)
{
assert( 0 );
assert(0);
}
}
void pcan_can_install_rx_callback( void (*cb)( can_message_t * ) )
void pcan_can_install_rx_callback(void (*cb)(can_message_t *))
{
can_dev.rx_cb = cb;
}
void pcan_can_install_error_callback( void (*cb)( uint8_t, uint8_t, uint8_t ) )
void pcan_can_install_error_callback(void (*cb)(uint8_t, uint8_t, uint8_t))
{
can_dev.can_err_cb = cb;
}
static int pcan_try_send_message( const can_message_t *p_msg )
static int pcan_try_send_message(const can_message_t *p_msg)
{
CAN_TxHeaderTypeDef msg = { .TransmitGlobalTime = DISABLE };
uint32_t txMailbox = 0;
if( p_msg->flags & CAN_FLAG_EXTID )
if (p_msg->flags & CAN_FLAG_EXTID)
{
msg.ExtId = p_msg->id & 0x1FFFFFFF;
msg.IDE = CAN_ID_EXT;
@ -150,42 +132,42 @@ static int pcan_try_send_message( const can_message_t *p_msg )
}
msg.DLC = p_msg->dlc;
msg.RTR = (p_msg->flags & CAN_FLAG_RTR)?CAN_RTR_REMOTE:CAN_RTR_DATA;
msg.RTR = (p_msg->flags & CAN_FLAG_RTR) ? CAN_RTR_REMOTE : CAN_RTR_DATA;
if( HAL_CAN_AddTxMessage( &g_hcan, &msg, (void*)p_msg->data, &txMailbox ) != HAL_OK )
if (HAL_CAN_AddTxMessage(&g_hcan, &msg, (void *)p_msg->data, &txMailbox) != HAL_OK)
return -1;
return txMailbox;
}
static void pcan_can_flush_tx( void )
static void pcan_can_flush_tx(void)
{
can_message_t *p_msg;
/* empty fifo */
if( can_dev.tx_head == can_dev.tx_tail )
if (can_dev.tx_head == can_dev.tx_tail)
return;
p_msg = &can_dev.tx_fifo[can_dev.tx_tail];
if( pcan_try_send_message( p_msg ) < 0 )
if (pcan_try_send_message(p_msg) < 0)
return;
/* update fifo index */
uint32_t tail = can_dev.tx_tail+1;
if( tail == CAN_TX_FIFO_SIZE )
uint32_t tail = can_dev.tx_tail + 1;
if (tail == CAN_TX_FIFO_SIZE)
tail = 0;
can_dev.tx_tail = tail;
}
int pcan_can_send_message( const can_message_t *p_msg )
int pcan_can_send_message(const can_message_t *p_msg)
{
if( !p_msg )
if (!p_msg)
return 0;
uint32_t head = can_dev.tx_head+1;
if( head == CAN_TX_FIFO_SIZE )
uint32_t head = can_dev.tx_head + 1;
if (head == CAN_TX_FIFO_SIZE)
head = 0;
/* overflow ? just skip it */
if( head == can_dev.tx_tail )
if (head == can_dev.tx_tail)
{
++can_dev.tx_ovfs;
return -1;
@ -197,47 +179,47 @@ int pcan_can_send_message( const can_message_t *p_msg )
return 0;
}
void pcan_can_set_silent( uint8_t silent_mode )
void pcan_can_set_silent(uint8_t silent_mode)
{
g_hcan.Init.Mode = silent_mode ? CAN_MODE_SILENT: CAN_MODE_NORMAL;
if( HAL_CAN_Init( &g_hcan ) != HAL_OK )
g_hcan.Init.Mode = silent_mode ? CAN_MODE_SILENT : CAN_MODE_NORMAL;
if (HAL_CAN_Init(&g_hcan) != HAL_OK)
{
assert( 0 );
assert(0);
}
}
void pcan_can_set_loopback( uint8_t loopback )
void pcan_can_set_loopback(uint8_t loopback)
{
g_hcan.Init.Mode = loopback ? CAN_MODE_LOOPBACK: CAN_MODE_NORMAL;
if( HAL_CAN_Init( &g_hcan ) != HAL_OK )
g_hcan.Init.Mode = loopback ? CAN_MODE_LOOPBACK : CAN_MODE_NORMAL;
if (HAL_CAN_Init(&g_hcan) != HAL_OK)
{
assert( 0 );
assert(0);
}
}
void pcan_can_set_bus_active( uint16_t mode )
void pcan_can_set_bus_active(uint16_t mode)
{
if( mode )
if (mode)
{
HAL_CAN_Start( &g_hcan );
HAL_CAN_AbortTxRequest( &g_hcan, CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2 );
HAL_CAN_Start(&g_hcan);
HAL_CAN_AbortTxRequest(&g_hcan, CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2);
}
else
{
HAL_CAN_AbortTxRequest( &g_hcan, CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2 );
HAL_CAN_Stop( &g_hcan );
HAL_CAN_AbortTxRequest(&g_hcan, CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2);
HAL_CAN_Stop(&g_hcan);
}
}
static void pcan_can_rx_frame( CAN_HandleTypeDef *hcan, uint32_t fifo )
static void pcan_can_rx_frame(CAN_HandleTypeDef *hcan, uint32_t fifo)
{
CAN_RxHeaderTypeDef hdr = { 0 };
can_message_t msg = { 0 };
if( HAL_CAN_GetRxMessage( hcan, fifo, &hdr, msg.data ) != HAL_OK )
if (HAL_CAN_GetRxMessage(hcan, fifo, &hdr, msg.data) != HAL_OK)
return;
if( hdr.IDE == CAN_ID_STD )
if (hdr.IDE == CAN_ID_STD)
{
msg.id = hdr.StdId;
}
@ -247,7 +229,7 @@ static void pcan_can_rx_frame( CAN_HandleTypeDef *hcan, uint32_t fifo )
msg.flags |= CAN_FLAG_EXTID;
}
if( hdr.RTR == CAN_RTR_REMOTE )
if (hdr.RTR == CAN_RTR_REMOTE)
{
msg.flags |= CAN_FLAG_RTR;
}
@ -255,9 +237,9 @@ static void pcan_can_rx_frame( CAN_HandleTypeDef *hcan, uint32_t fifo )
msg.dlc = hdr.DLC;
msg.timestamp = pcan_timestamp_ticks();
if( can_dev.rx_cb )
if (can_dev.rx_cb)
{
can_dev.rx_cb( &msg );
can_dev.rx_cb(&msg);
}
++can_dev.rx_msgs;
@ -265,79 +247,85 @@ static void pcan_can_rx_frame( CAN_HandleTypeDef *hcan, uint32_t fifo )
void pcan_can_poll(void)
{
HAL_CAN_IRQHandler( &g_hcan );
HAL_CAN_IRQHandler(&g_hcan);
pcan_can_flush_tx();
}
void HAL_CAN_RxFifo0MsgPendingCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
pcan_can_rx_frame( hcan, CAN_RX_FIFO0 );
pcan_can_rx_frame(hcan, CAN_RX_FIFO0);
}
void HAL_CAN_RxFifo1MsgPendingCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
pcan_can_rx_frame( hcan, CAN_RX_FIFO1 );
pcan_can_rx_frame(hcan, CAN_RX_FIFO1);
}
void HAL_CAN_TxMailbox0CompleteCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
{
UNUSED( hcan );
UNUSED(hcan);
++can_dev.tx_msgs;
}
void HAL_CAN_TxMailbox1CompleteCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
{
UNUSED( hcan );
UNUSED(hcan);
++can_dev.tx_msgs;
}
void HAL_CAN_TxMailbox2CompleteCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
{
UNUSED( hcan );
UNUSED(hcan);
++can_dev.tx_msgs;
}
void HAL_CAN_RxFifo0FullCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
{
UNUSED( hcan );
UNUSED(hcan);
++can_dev.rx_ovfs;
}
void HAL_CAN_RxFifo1FullCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
{
UNUSED( hcan );
UNUSED(hcan);
++can_dev.rx_ovfs;
}
void HAL_CAN_SleepCallback( CAN_HandleTypeDef *hcan ){ UNUSED( hcan ); }
void HAL_CAN_WakeUpFromRxMsgCallback( CAN_HandleTypeDef *hcan ){ UNUSED( hcan ); }
void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
{
UNUSED(hcan);
}
void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
{
UNUSED(hcan);
}
void HAL_CAN_ErrorCallback( CAN_HandleTypeDef *hcan )
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
/* handle errors */
uint32_t err = HAL_CAN_GetError( hcan );
uint32_t err = HAL_CAN_GetError(hcan);
uint8_t can_err = 0;
if ( err & ( HAL_CAN_ERROR_TX_TERR0 | HAL_CAN_ERROR_TX_TERR1 | HAL_CAN_ERROR_TX_TERR2 ) )
if (err & (HAL_CAN_ERROR_TX_TERR0 | HAL_CAN_ERROR_TX_TERR1 | HAL_CAN_ERROR_TX_TERR2))
{
++can_dev.tx_errs;
can_err |= CAN_ERROR_FLAG_TX_ERR;
}
if( err & HAL_CAN_ERROR_BOF )
if (err & HAL_CAN_ERROR_BOF)
{
can_err |= CAN_ERROR_FLAG_BUSOFF;
}
if( err & ( HAL_CAN_ERROR_RX_FOV0 | HAL_CAN_ERROR_RX_FOV1 ) )
if (err & (HAL_CAN_ERROR_RX_FOV0 | HAL_CAN_ERROR_RX_FOV1))
{
can_err |= CAN_ERROR_FLAG_RX_OVF;
}
if( can_dev.can_err_cb && can_err )
if (can_dev.can_err_cb && can_err)
{
can_dev.can_err_cb( can_err, can_dev.tx_errs & 0xFF, can_dev.rx_errs );
can_dev.can_err_cb(can_err, can_dev.tx_errs & 0xFF, can_dev.rx_errs);
}
HAL_CAN_ResetError( hcan );
HAL_CAN_ResetError(hcan);
}

View File

@ -5,9 +5,9 @@
#define CAN_FLAG_RTR (0x40)
#define CAN_FLAG_EXTID (0x80)
#define CAN_ERROR_FLAG_BUSOFF (1<<0)
#define CAN_ERROR_FLAG_RX_OVF (1<<1)
#define CAN_ERROR_FLAG_TX_ERR (1<<2)
#define CAN_ERROR_FLAG_BUSOFF (1 << 0)
#define CAN_ERROR_FLAG_RX_OVF (1 << 1)
#define CAN_ERROR_FLAG_TX_ERR (1 << 2)
typedef struct
{
@ -19,15 +19,14 @@ typedef struct
uint8_t dummy;
/* in pcan ticks */
uint16_t timestamp;
}
can_message_t;
} can_message_t;
void pcan_can_init( void );
void pcan_can_poll( void );
void pcan_can_set_bitrate( uint16_t brp, uint8_t tseg1, uint8_t tseg2, uint8_t sjw );
int pcan_can_send_message( const can_message_t *msg );
void pcan_can_install_rx_callback( void (*cb)( can_message_t * ) );
void pcan_can_install_error_callback( void (*cb)( uint8_t, uint8_t, uint8_t ) );
void pcan_can_set_bus_active( uint16_t mode );
void pcan_can_set_silent( uint8_t silent_mode );
void pcan_can_set_loopback( uint8_t loopback );
void pcan_can_init(void);
void pcan_can_poll(void);
void pcan_can_set_bitrate(uint16_t brp, uint8_t tseg1, uint8_t tseg2, uint8_t sjw);
int pcan_can_send_message(const can_message_t *msg);
void pcan_can_install_rx_callback(void (*cb)(can_message_t *));
void pcan_can_install_error_callback(void (*cb)(uint8_t, uint8_t, uint8_t));
void pcan_can_set_bus_active(uint16_t mode);
void pcan_can_set_silent(uint8_t silent_mode);
void pcan_can_set_loopback(uint8_t loopback);

View File

@ -1,8 +1,8 @@
#include <assert.h>
#include "stm32f0xx_hal.h"
#include "pcan_timestamp.h"
#include "pcan_led.h"
#include "pcan_timestamp.h"
#include "pcan_varian.h"
#include "stm32f0xx_hal.h"
#include <assert.h>
static struct
{
@ -11,55 +11,54 @@ static struct
uint16_t delay;
uint16_t timestamp;
uint8_t state;
}
led_mode_array[LED_TOTAL] = { 0 };
} led_mode_array[LED_TOTAL] = { 0 };
void pcan_led_init( void )
void pcan_led_init(void)
{
#ifdef IOPIN_TX
PIN_ENABLE_CLOCK( IOPIN_TX );
PIN_INIT( IOPIN_TX );
PIN_ENABLE_CLOCK(IOPIN_TX);
PIN_INIT(IOPIN_TX);
#endif
#ifdef IOPIN_RX
PIN_ENABLE_CLOCK( IOPIN_RX );
PIN_INIT( IOPIN_RX );
PIN_ENABLE_CLOCK(IOPIN_RX);
PIN_INIT(IOPIN_RX);
#endif
}
void pcan_led_set_mode( int led, int mode, uint16_t arg )
void pcan_led_set_mode(int led, int mode, uint16_t arg)
{
assert( led < LED_TOTAL );
assert(led < LED_TOTAL);
uint16_t ts = pcan_timestamp_millis();
led_mode_array[led].mode = mode;
if( !led_mode_array[led].timestamp )
if (!led_mode_array[led].timestamp)
{
led_mode_array[led].timestamp = ts|1;
led_mode_array[led].timestamp = ts | 1;
}
led_mode_array[led].delay = 0;
/* set guard time */
if( mode == LED_MODE_BLINK_FAST || mode == LED_MODE_BLINK_SLOW )
if (mode == LED_MODE_BLINK_FAST || mode == LED_MODE_BLINK_SLOW)
{
led_mode_array[led].delay = ( mode == LED_MODE_BLINK_FAST ) ? 50: 200;
arg = arg?(ts + arg)|1:0;
led_mode_array[led].delay = (mode == LED_MODE_BLINK_FAST) ? 50 : 200;
arg = arg ? (ts + arg) | 1 : 0;
}
led_mode_array[led].arg = arg;
}
static void pcan_led_update_state( int led, uint8_t state )
static void pcan_led_update_state(int led, uint8_t state)
{
switch( led )
switch (led)
{
#ifdef IOPIN_TX
case LED_CH0_TX:
state ? (LED_ON( IOPIN_TX )): (LED_OFF( IOPIN_TX ));
state ? (LED_ON(IOPIN_TX)) : (LED_OFF(IOPIN_TX));
break;
#endif
#ifdef IOPIN_RX
case LED_CH0_RX:
state ? (LED_ON( IOPIN_RX )): (LED_OFF( IOPIN_RX ));
state ? (LED_ON(IOPIN_RX)) : (LED_OFF(IOPIN_RX));
break;
#endif
default:
@ -68,18 +67,18 @@ static void pcan_led_update_state( int led, uint8_t state )
}
}
void pcan_led_poll( void )
void pcan_led_poll(void)
{
uint16_t ts_ms = pcan_timestamp_millis();
for( int i = 0; i < LED_TOTAL; i++ )
for (int i = 0; i < LED_TOTAL; i++)
{
if( !led_mode_array[i].timestamp )
if (!led_mode_array[i].timestamp)
continue;
if( (uint16_t)( ts_ms - led_mode_array[i].timestamp ) < led_mode_array[i].delay )
if ((uint16_t)(ts_ms - led_mode_array[i].timestamp) < led_mode_array[i].delay)
continue;
switch( led_mode_array[i].mode )
switch (led_mode_array[i].mode)
{
default:
case LED_MODE_NONE:
@ -87,7 +86,7 @@ void pcan_led_poll( void )
break;
case LED_MODE_OFF:
case LED_MODE_ON:
led_mode_array[i].state = ( led_mode_array[i].mode == LED_MODE_ON );
led_mode_array[i].state = (led_mode_array[i].mode == LED_MODE_ON);
led_mode_array[i].timestamp = 0;
break;
case LED_MODE_BLINK_FAST:
@ -95,13 +94,13 @@ void pcan_led_poll( void )
led_mode_array[i].state ^= 1;
led_mode_array[i].timestamp += led_mode_array[i].delay;
led_mode_array[i].timestamp |= 1;
if( led_mode_array[i].arg && ( led_mode_array[i].arg <= ts_ms ) )
if (led_mode_array[i].arg && (led_mode_array[i].arg <= ts_ms))
{
pcan_led_set_mode( i, LED_MODE_OFF, 0 );
pcan_led_set_mode(i, LED_MODE_OFF, 0);
}
break;
}
pcan_led_update_state( i, led_mode_array[i].state );
pcan_led_update_state(i, led_mode_array[i].state);
}
}

View File

@ -19,6 +19,6 @@ enum e_pcan_led_mode
LED_MODE_BLINK_SLOW,
};
void pcan_led_init( void );
void pcan_led_set_mode( int led, int mode, uint16_t arg );
void pcan_led_poll( void );
void pcan_led_init(void);
void pcan_led_set_mode(int led, int mode, uint16_t arg);
void pcan_led_poll(void);

View File

@ -9,8 +9,7 @@ typedef struct
uint8_t function;
uint8_t number;
uint8_t param[14];
}
PCAN_USB_PARAM;
} PCAN_USB_PARAM;
#pragma pack(pop)
/* USB control cmds get/set */

View File

@ -1,12 +1,12 @@
#include "pcan_can.h"
#include "pcan_led.h"
#include "pcan_packet.h"
#include "pcan_timestamp.h"
#include "pcan_usb.h"
#include "punker.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "pcan_can.h"
#include "pcan_usb.h"
#include "pcan_packet.h"
#include "pcan_led.h"
#include "pcan_timestamp.h"
#include "punker.h"
#define SJA1000_BASICCAN 0
#define SJA1000_PELICAN 1
@ -32,8 +32,7 @@ static struct
uint8_t loopback;
uint8_t err_mask;
uint32_t quartz_freq;
}
can;
} can;
uint8_t sja1000_shadow[6];
uint8_t sja1000_ic_mode;
uint8_t device_id;
@ -53,7 +52,7 @@ pcan_device =
#define BLOCK_SIZE (64)
#define HEADER_SIZE (2)
#define PCAN_MAX_RECORD_SIZE (BLOCK_SIZE*14)
#define PCAN_MAX_RECORD_SIZE (BLOCK_SIZE * 14)
typedef struct
{
/* raw data array */
@ -61,26 +60,24 @@ typedef struct
/* ts counter, need to provide correct timestamp correction ( 2 or 1 byte ) */
uint16_t ts_counter;
uint16_t pos;
}
PCAN_RECORD_BUFFER_EX;
} PCAN_RECORD_BUFFER_EX;
static uint8_t temp_data_buffer[PCAN_MAX_RECORD_SIZE] = { 0 };
static struct t_m2h_fsm data_fsm =
{
static struct t_m2h_fsm data_fsm = {
.state = 0,
.ep_addr = PCAN_USB_EP_MSGIN,
.pdbuf = temp_data_buffer,
.dbsize = sizeof( temp_data_buffer ),
.dbsize = sizeof(temp_data_buffer),
};
static PCAN_RECORD_BUFFER_EX pcan_records;
void pcan_sja1000_write( uint8_t reg_addr, uint8_t value )
void pcan_sja1000_write(uint8_t reg_addr, uint8_t value)
{
if( pcan_device.sja1000_ic_mode == SJA1000_PELICAN )
if (pcan_device.sja1000_ic_mode == SJA1000_PELICAN)
{
reg_addr &= 0x7F;
switch( reg_addr )
switch (reg_addr)
{
case 6:
pcan_device.sja1000_shadow[2] = value;
@ -92,7 +89,7 @@ void pcan_sja1000_write( uint8_t reg_addr, uint8_t value )
pcan_device.sja1000_shadow[4] = value;
break;
case 15:
pcan_device.sja1000_shadow[5] = ( value == 0xFF ) ? 0x7F: value;
pcan_device.sja1000_shadow[5] = (value == 0xFF) ? 0x7F : value;
break;
case 16:
pcan_device.sja1000_shadow[0] = value;
@ -101,17 +98,17 @@ void pcan_sja1000_write( uint8_t reg_addr, uint8_t value )
pcan_device.sja1000_shadow[1] = value;
break;
case 31:
if( (value & 0x80 ) == 0 )
if ((value & 0x80) == 0)
{
pcan_device.sja1000_ic_mode = SJA1000_BASICCAN;
}
break;
}
}
else if( pcan_device.sja1000_ic_mode == SJA1000_BASICCAN )
else if (pcan_device.sja1000_ic_mode == SJA1000_BASICCAN)
{
reg_addr &= 0x1F;
switch( reg_addr )
switch (reg_addr)
{
case 4:
pcan_device.sja1000_shadow[0] = value;
@ -126,7 +123,7 @@ void pcan_sja1000_write( uint8_t reg_addr, uint8_t value )
pcan_device.sja1000_shadow[3] = value;
break;
case 31:
if( (value & 0x80 ) != 0 )
if ((value & 0x80) != 0)
{
pcan_device.sja1000_ic_mode = SJA1000_PELICAN;
}
@ -135,12 +132,12 @@ void pcan_sja1000_write( uint8_t reg_addr, uint8_t value )
}
}
uint8_t pcan_sja1000_read( uint8_t reg_addr )
uint8_t pcan_sja1000_read(uint8_t reg_addr)
{
if( pcan_device.sja1000_ic_mode == SJA1000_PELICAN )
if (pcan_device.sja1000_ic_mode == SJA1000_PELICAN)
{
reg_addr &= 0x7F;
switch( reg_addr )
switch (reg_addr)
{
case 0:
return 0x01;
@ -162,10 +159,10 @@ uint8_t pcan_sja1000_read( uint8_t reg_addr )
return 0;
}
}
else if( pcan_device.sja1000_ic_mode == SJA1000_BASICCAN )
else if (pcan_device.sja1000_ic_mode == SJA1000_BASICCAN)
{
reg_addr &= 0x1F;
switch( reg_addr )
switch (reg_addr)
{
case 0:
return 0x01;
@ -187,66 +184,66 @@ uint8_t pcan_sja1000_read( uint8_t reg_addr )
return 0;
}
void pcan_record_write_header( uint8_t *p )
void pcan_record_write_header(uint8_t *p)
{
p[0] = 0x02; /* record header */
p[1] = 0x00; /* record count */
}
void pcan_record_buffer_reset( PCAN_RECORD_BUFFER_EX *prec )
void pcan_record_buffer_reset(PCAN_RECORD_BUFFER_EX *prec)
{
pcan_record_write_header( prec->buffer );
pcan_record_write_header(prec->buffer);
prec->ts_counter = 0;
prec->pos = HEADER_SIZE;
}
/* request free slot for specific amount of data */
uint8_t *pcan_record_buffer_request( PCAN_RECORD_BUFFER_EX *prec, uint16_t size )
uint8_t *pcan_record_buffer_request(PCAN_RECORD_BUFFER_EX *prec, uint16_t size)
{
if( size > (BLOCK_SIZE-HEADER_SIZE) )
if (size > (BLOCK_SIZE - HEADER_SIZE))
return 0;
uint16_t unused = BLOCK_SIZE - ( prec->pos & (BLOCK_SIZE-1) );
uint16_t unused = BLOCK_SIZE - (prec->pos & (BLOCK_SIZE - 1));
/* allocate new slot ? */
if( unused < size )
if (unused < size)
{
/* align data to next slot bound */
uint8_t next = ( prec->pos + (BLOCK_SIZE-1) ) & (~((BLOCK_SIZE-1)));
if( (next+HEADER_SIZE) >= PCAN_MAX_RECORD_SIZE )
uint8_t next = (prec->pos + (BLOCK_SIZE - 1)) & (~((BLOCK_SIZE - 1)));
if ((next + HEADER_SIZE) >= PCAN_MAX_RECORD_SIZE)
return 0;
memset( &prec->buffer[next], 0x00, BLOCK_SIZE );
pcan_record_write_header( &prec->buffer[next] );
memset(&prec->buffer[next], 0x00, BLOCK_SIZE);
pcan_record_write_header(&prec->buffer[next]);
next += 2;
prec->pos = next;
/* reset ts_counter for next buffer */
prec->ts_counter = 0;
}
if( ( prec->pos + size ) > PCAN_MAX_RECORD_SIZE )
return (void*)0;
if ((prec->pos + size) > PCAN_MAX_RECORD_SIZE)
return (void *)0;
return &prec->buffer[prec->pos];
}
void pcan_record_buffer_commit( PCAN_RECORD_BUFFER_EX *prec, uint16_t size )
void pcan_record_buffer_commit(PCAN_RECORD_BUFFER_EX *prec, uint16_t size)
{
uint16_t pos = prec->pos&(~(BLOCK_SIZE-1));
uint16_t pos = prec->pos & (~(BLOCK_SIZE - 1));
/* increse record count */
++prec->buffer[pos+1];
++prec->buffer[pos + 1];
prec->pos += size;
}
uint16_t pcan_record_buffer_flush( PCAN_RECORD_BUFFER_EX *prec )
uint16_t pcan_record_buffer_flush(PCAN_RECORD_BUFFER_EX *prec)
{
uint16_t ts_ticks = pcan_timestamp_ticks();
int res, flush_size;
if( prec->pos <= HEADER_SIZE )
if (prec->pos <= HEADER_SIZE)
{
/* try to send ZLP, original 800 */
if( pcan_device.last_flush && (((uint16_t)(ts_ticks - pcan_device.last_flush)) >= PCAN_TICKS_FROM_US( 800 )) )
if (pcan_device.last_flush && (((uint16_t)(ts_ticks - pcan_device.last_flush)) >= PCAN_TICKS_FROM_US(800)))
{
res = pcan_flush_data( &data_fsm, 0, 0 );
if( res )
res = pcan_flush_data(&data_fsm, 0, 0);
if (res)
{
pcan_device.last_flush = 0;
}
@ -254,11 +251,11 @@ uint16_t pcan_record_buffer_flush( PCAN_RECORD_BUFFER_EX *prec )
return 0;
}
flush_size = ( prec->pos + (BLOCK_SIZE-1) ) & (~((BLOCK_SIZE-1)));
res = pcan_flush_data( &data_fsm, prec->buffer, flush_size );
if( res )
flush_size = (prec->pos + (BLOCK_SIZE - 1)) & (~((BLOCK_SIZE - 1)));
res = pcan_flush_data(&data_fsm, prec->buffer, flush_size);
if (res)
{
pcan_record_buffer_reset( prec );
pcan_record_buffer_reset(prec);
pcan_device.last_flush = ts_ticks;
return 1;
}
@ -312,62 +309,62 @@ static void pcan_bus_event( PCAN_RECORD_BUFFER_EX *prec, uint8_t flags )
#endif
#define BUFFER_MINIMAL_GAP_SIZE (16u)
static void pcan_rx_can_frame( PCAN_RECORD_BUFFER_EX *prec, const can_message_t *msg )
static void pcan_rx_can_frame(PCAN_RECORD_BUFFER_EX *prec, const can_message_t *msg)
{
uint8_t *ptr, pos = 0;
ptr = pcan_record_buffer_request( prec, BUFFER_MINIMAL_GAP_SIZE );
ptr = pcan_record_buffer_request(prec, BUFFER_MINIMAL_GAP_SIZE);
/* wow ! too fast too furious !!! */
if( !ptr )
if (!ptr)
{
if( !pcan_record_buffer_flush( prec ) )
if (!pcan_record_buffer_flush(prec))
{
pcan_device.can.err |= PCAN_USB_ERROR_RXQOVR;
return;
}
ptr = pcan_record_buffer_request( prec, BUFFER_MINIMAL_GAP_SIZE );
ptr = pcan_record_buffer_request(prec, BUFFER_MINIMAL_GAP_SIZE);
/* o_0 */
if( !ptr )
if (!ptr)
return;
}
/* recheck dlc for abnormal frames... */
uint8_t sl = msg->dlc > 8 ? 8: msg->dlc;
uint8_t sl = msg->dlc > 8 ? 8 : msg->dlc;
if( msg->flags & CAN_FLAG_EXTID )
if (msg->flags & CAN_FLAG_EXTID)
{
sl |= PCAN_USB_STATUSLEN_EXT_ID;
}
if( msg->flags & CAN_FLAG_RTR )
if (msg->flags & CAN_FLAG_RTR)
{
sl |= PCAN_USB_STATUSLEN_RTR;
}
pack_u8( ptr, sl );
pack_u8(ptr, sl);
ptr += 1;
pos += 1;
if( sl & PCAN_USB_STATUSLEN_EXT_ID )
if (sl & PCAN_USB_STATUSLEN_EXT_ID)
{
uint32_t id = ( msg->id & 0x1FFFFFFF ) << 3;
if( msg->flags & CAN_FLAG_ECHO )
uint32_t id = (msg->id & 0x1FFFFFFF) << 3;
if (msg->flags & CAN_FLAG_ECHO)
{
id |= 0x01;
}
pack_u32( ptr, id );
pack_u32(ptr, id);
ptr += 4;
pos += 4;
}
else
{
uint16_t id = ( msg->id & 0x7FF ) << 5;
if( msg->flags & CAN_FLAG_ECHO )
uint16_t id = (msg->id & 0x7FF) << 5;
if (msg->flags & CAN_FLAG_ECHO)
{
id |= 0x01;
}
pack_u16( ptr, id );
pack_u16(ptr, id);
ptr += 2;
pos += 2;
@ -376,112 +373,112 @@ static void pcan_rx_can_frame( PCAN_RECORD_BUFFER_EX *prec, const can_message_t
/* timestamp to ticks, 1 tick is 42.666us */
uint16_t timestamp16 = msg->timestamp;
if( prec->ts_counter == 0 )
if (prec->ts_counter == 0)
{
pack_u16( ptr, timestamp16 );
pack_u16(ptr, timestamp16);
ptr += 2;
pos += 2;
}
else
{
pack_u8( ptr, timestamp16&0xFF );
pack_u8(ptr, timestamp16 & 0xFF);
ptr += 1;
pos += 1;
}
++prec->ts_counter;
if( !( sl & PCAN_USB_STATUSLEN_RTR ) )
if (!(sl & PCAN_USB_STATUSLEN_RTR))
{
memcpy( ptr, msg->data, msg->dlc );
memcpy(ptr, msg->data, msg->dlc);
ptr += msg->dlc;
pos += msg->dlc;
}
if( msg->flags & CAN_FLAG_ECHO )
if (msg->flags & CAN_FLAG_ECHO)
{
pack_u8( ptr, msg->dummy );
pack_u8(ptr, msg->dummy);
ptr += 1;
pos += 1;
}
/* add new record */
pcan_record_buffer_commit( prec, pos );
pcan_record_buffer_commit(prec, pos);
}
static void pcan_rx_message( can_message_t *msg )
static void pcan_rx_message(can_message_t *msg)
{
if( !pcan_device.bus_active )
if (!pcan_device.bus_active)
return;
if( ( msg->flags & CAN_FLAG_ECHO ) == 0 )
if ((msg->flags & CAN_FLAG_ECHO) == 0)
{
pcan_led_set_mode( LED_CH0_RX, LED_MODE_BLINK_FAST, 237 );
pcan_led_set_mode(LED_CH0_RX, LED_MODE_BLINK_FAST, 237);
}
pcan_rx_can_frame( &pcan_records, msg );
pcan_rx_can_frame(&pcan_records, msg);
}
static void pcan_can_error( uint8_t err, uint8_t rx_err, uint8_t tx_err )
static void pcan_can_error(uint8_t err, uint8_t rx_err, uint8_t tx_err)
{
pcan_device.can.rx_err = rx_err;
pcan_device.can.tx_err = tx_err;
if( err & CAN_ERROR_FLAG_BUSOFF )
if (err & CAN_ERROR_FLAG_BUSOFF)
{
pcan_device.can.err |= PCAN_USB_ERROR_BUS_OFF;
}
}
#define WAIT_FOR_TXSLOTS 1
static uint8_t pcan_decode_data_frame( uint8_t *ptr, uint16_t size, uint8_t flags )
static uint8_t pcan_decode_data_frame(uint8_t *ptr, uint16_t size, uint8_t flags)
{
can_message_t msg = { 0 };
uint8_t rec_len = flags & PCAN_USB_STATUSLEN_DLC;
/* return back frame to PC */
uint8_t srr_flag = 0;
if( !pcan_device.bus_active )
if (!pcan_device.bus_active)
return 0;
if( flags & PCAN_USB_STATUSLEN_EXT_ID )
if (flags & PCAN_USB_STATUSLEN_EXT_ID)
{
uint32_t id;
if( size < sizeof( uint32_t ) )
if (size < sizeof(uint32_t))
return 0;
id = unpack_u32( ptr );
id = unpack_u32(ptr);
srr_flag = id & 0x01;
id >>= 3;
msg.flags = CAN_FLAG_EXTID;
msg.id = id;
ptr += sizeof( uint32_t );
size -= sizeof( uint32_t );
ptr += sizeof(uint32_t);
size -= sizeof(uint32_t);
}
else
{
uint16_t id;
if( size < sizeof( uint16_t ) )
if (size < sizeof(uint16_t))
return 0;
id = unpack_u16( ptr );
id = unpack_u16(ptr);
srr_flag = id & 0x01;
id >>= 5;
msg.id = id;
ptr += sizeof( uint16_t );
size -= sizeof( uint16_t );
ptr += sizeof(uint16_t);
size -= sizeof(uint16_t);
}
/* check for MAX DLC */
msg.dlc = rec_len < 8 ? rec_len: 8;
msg.dlc = rec_len < 8 ? rec_len : 8;
if( size < rec_len )
if (size < rec_len)
return 0;
if( !( flags & PCAN_USB_STATUSLEN_RTR ) )
if (!(flags & PCAN_USB_STATUSLEN_RTR))
{
memcpy( msg.data, ptr, msg.dlc );
memcpy(msg.data, ptr, msg.dlc);
ptr += rec_len;
size -= rec_len;
@ -492,32 +489,31 @@ static uint8_t pcan_decode_data_frame( uint8_t *ptr, uint16_t size, uint8_t flag
}
/* self receive flag ? */
if( srr_flag )
if (srr_flag)
{
if( size < sizeof( uint8_t ) )
if (size < sizeof(uint8_t))
return 0;
msg.dummy = unpack_u8( ptr );
msg.dummy = unpack_u8(ptr);
ptr += sizeof( uint8_t );
size -= sizeof( uint8_t );
ptr += sizeof(uint8_t);
size -= sizeof(uint8_t);
}
msg.timestamp = pcan_timestamp_ticks();
pcan_led_set_mode( LED_CH0_TX, LED_MODE_BLINK_FAST, 237 );
pcan_led_set_mode(LED_CH0_TX, LED_MODE_BLINK_FAST, 237);
#if WAIT_FOR_TXSLOTS
const uint16_t ts_poll = pcan_timestamp_ticks();
/* need more time to send data... ? */
while( pcan_can_send_message( &msg ) < 0 )
while (pcan_can_send_message(&msg) < 0)
{
/* USB will get NACK and we will not miss other data */
pcan_can_poll();
uint16_t ts_diff = pcan_timestamp_ticks() - ts_poll;
/* we can't tramsit couse bus off or timeout ? */
if( ( pcan_device.can.err & PCAN_USB_ERROR_BUS_OFF ) ||
( ts_diff >= PCAN_TICKS_FROM_US( 1000000u ) ) )
if ((pcan_device.can.err & PCAN_USB_ERROR_BUS_OFF) || (ts_diff >= PCAN_TICKS_FROM_US(1000000u)))
{
/* tx buffer overflow, drop all data */
pcan_device.can.err |= PCAN_USB_ERROR_TXFULL;
@ -525,7 +521,7 @@ static uint8_t pcan_decode_data_frame( uint8_t *ptr, uint16_t size, uint8_t flag
}
}
#else
if( pcan_can_send_message( &msg ) < 0 )
if (pcan_can_send_message(&msg) < 0)
{
/* tx buffer overflow, drop all data */
pcan_device.can.err |= PCAN_USB_ERROR_TXFULL;
@ -533,103 +529,103 @@ static uint8_t pcan_decode_data_frame( uint8_t *ptr, uint16_t size, uint8_t flag
}
#endif
/* return back to PC */
if( srr_flag )
if (srr_flag)
{
msg.flags |= CAN_FLAG_ECHO;
pcan_rx_message( &msg );
pcan_rx_message(&msg);
}
return size;
}
static void pcan_set_bitrate( uint8_t *ptr )
static void pcan_set_bitrate(uint8_t *ptr)
{
/* decode BTR1 */
volatile uint8_t tseg1 = (ptr[0] & 0xF) + 1;
volatile uint8_t tseg2 = ((ptr[0]>>4) & 0x7) + 1;
volatile uint8_t tseg2 = ((ptr[0] >> 4) & 0x7) + 1;
/* decode BTR0 */
volatile uint16_t brp = (ptr[1] & 0x3f) + 1;
volatile uint8_t sjw = ((ptr[1]>>6) & 0x3) + 1;
volatile uint8_t sjw = ((ptr[1] >> 6) & 0x3) + 1;
uint32_t bitrate = (((PCAN_USB_CRYSTAL_HZ/2)/brp)/(1/*tq*/ + tseg1 + tseg2 ));
uint32_t bitrate = (((PCAN_USB_CRYSTAL_HZ / 2) / brp) / (1 /*tq*/ + tseg1 + tseg2));
(void)bitrate;
pcan_can_set_bitrate( brp, tseg1, tseg2, sjw );
pcan_can_set_bitrate(brp, tseg1, tseg2, sjw);
}
/* create */
void pcan_timesync_event( PCAN_RECORD_BUFFER_EX *prec )
void pcan_timesync_event(PCAN_RECORD_BUFFER_EX *prec)
{
uint8_t sl = PCAN_USB_STATUSLEN_INTERNAL | 2 /* record data size */;
uint8_t *ptr, pos = 0;
ptr = pcan_record_buffer_request( prec, 5 );
if( !ptr )
ptr = pcan_record_buffer_request(prec, 5);
if (!ptr)
return;
uint16_t timestamp16 = pcan_timestamp_ticks();
pack_u8( ptr, sl );
pack_u8(ptr, sl);
ptr += 1;
pos += 1;
/* function */
pack_u8( ptr, PCAN_USB_REC_TS );
pack_u8(ptr, PCAN_USB_REC_TS);
ptr += 1;
pos += 1;
/* number */
pack_u8( ptr, 1 );
pack_u8(ptr, 1);
ptr += 1;
pos += 1;
/* timestamp */
pack_u16( ptr, timestamp16 );
pack_u16(ptr, timestamp16);
ptr += 2;
pos += 2;
/* add new record */
pcan_record_buffer_commit( prec, pos );
pcan_record_buffer_commit(prec, pos);
/* error record */
pos = 0;
ptr = pcan_record_buffer_request( prec, 3 );
if( !ptr )
ptr = pcan_record_buffer_request(prec, 3);
if (!ptr)
return;
pcan_device.can.err &= pcan_device.can.err_mask;
pack_u8( ptr, PCAN_USB_STATUSLEN_INTERNAL );
pack_u8(ptr, PCAN_USB_STATUSLEN_INTERNAL);
ptr += 1;
pos += 1;
pack_u8( ptr, PCAN_USB_REC_ERROR ); /* f */
pack_u8(ptr, PCAN_USB_REC_ERROR); /* f */
ptr += 1;
pos += 1;
pack_u8( ptr, pcan_device.can.err ); /* n */
pack_u8(ptr, pcan_device.can.err); /* n */
ptr += 1;
pos += 1;
/* add new record */
pcan_record_buffer_commit( prec, pos );
pcan_record_buffer_commit(prec, pos);
/* clean errors */
pcan_device.can.err = 0x00;
}
void pcan_protocol_process_command( uint8_t *ptr, uint16_t size )
void pcan_protocol_process_command(uint8_t *ptr, uint16_t size)
{
PCAN_USB_PARAM *cmd = (void*)ptr;
PCAN_USB_PARAM *cmd = (void *)ptr;
if( size < PCAN_USB_MSG_HEADER_LEN )
if (size < PCAN_USB_MSG_HEADER_LEN)
return;
switch( cmd->number )
switch (cmd->number)
{
case PCAN_USB_EX0:
switch( cmd->function )
switch (cmd->function)
{
/* set mass storage mode */
case PCAN_USB_SETCAN2FLASH:
@ -637,39 +633,39 @@ void pcan_protocol_process_command( uint8_t *ptr, uint16_t size )
}
break;
case PCAN_USB_EX3:
switch( cmd->function )
switch (cmd->function)
{
/* CAN silient control*/
case PCAN_USB_SET_SILENT_MODE:
pcan_device.can.silient = cmd->param[0];
pcan_can_set_silent( pcan_device.can.silient );
pcan_can_set_silent(pcan_device.can.silient);
break;
}
break;
case PCAN_USB_SET:
switch( cmd->function )
switch (cmd->function)
{
/* BTR0 BTR1 */
case PCAN_USB_CMD_BITRATE:
pcan_device.can.btr0 = cmd->param[1];
pcan_device.can.btr1 = cmd->param[0];
pcan_set_bitrate( cmd->param );
pcan_set_bitrate(cmd->param);
break;
/* set CAN on/off*/
case PCAN_USB_CMD_BUS:
pcan_device.bus_active = cmd->param[0];
pcan_led_set_mode( LED_STAT, pcan_device.bus_active ? LED_MODE_BLINK_SLOW:LED_MODE_OFF, 0 );
pcan_can_set_bus_active( pcan_device.bus_active );
pcan_led_set_mode(LED_STAT, pcan_device.bus_active ? LED_MODE_BLINK_SLOW : LED_MODE_OFF, 0);
pcan_can_set_bus_active(pcan_device.bus_active);
if( pcan_device.bus_active )
if (pcan_device.bus_active)
{
/* provide first timesync event */
pcan_device.last_timestamp_sync = pcan_timestamp_ticks();
pcan_timesync_event( &pcan_records );
pcan_timesync_event(&pcan_records);
}
/* led state */
pcan_led_set_mode( LED_CH0_TX, pcan_device.bus_active ? LED_MODE_OFF:LED_MODE_ON, 0 );
pcan_led_set_mode( LED_CH0_RX, pcan_device.bus_active ? LED_MODE_OFF:LED_MODE_ON, 0 );
pcan_led_set_mode(LED_CH0_TX, pcan_device.bus_active ? LED_MODE_OFF : LED_MODE_ON, 0);
pcan_led_set_mode(LED_CH0_RX, pcan_device.bus_active ? LED_MODE_OFF : LED_MODE_ON, 0);
break;
/* set device id 0-255 */
case PCAN_USB_CMD_DEVID:
@ -680,7 +676,7 @@ void pcan_protocol_process_command( uint8_t *ptr, uint16_t size )
break;
/* sja1000 reg write */
case PCAN_USB_CMD_REGISTER:
pcan_sja1000_write( cmd->param[0], cmd->param[1] );
pcan_sja1000_write(cmd->param[0], cmd->param[1]);
break;
/* ext VCC on/off */
case PCAN_USB_CMD_EXT_VCC:
@ -697,19 +693,19 @@ void pcan_protocol_process_command( uint8_t *ptr, uint16_t size )
}
break;
case PCAN_USB_GET:
switch( cmd->function )
switch (cmd->function)
{
/* BTR0 BTR1 */
case PCAN_USB_CMD_BITRATE:
cmd->param[1] = pcan_device.can.btr0;
cmd->param[0] = pcan_device.can.btr1;
pcan_usb_send_command_buffer( cmd, sizeof( PCAN_USB_PARAM ) );
pcan_usb_send_command_buffer(cmd, sizeof(PCAN_USB_PARAM));
break;
/* quarts freq in Mhz */
case PCAN_USB_CMD_CLOCK:
{
cmd->param[0] = pcan_device.can.quartz_freq/1000000u;
pcan_usb_send_command_buffer( cmd, sizeof( PCAN_USB_PARAM ) );
cmd->param[0] = pcan_device.can.quartz_freq / 1000000u;
pcan_usb_send_command_buffer(cmd, sizeof(PCAN_USB_PARAM));
}
break;
/* get device id */
@ -717,44 +713,44 @@ void pcan_protocol_process_command( uint8_t *ptr, uint16_t size )
{
/* default: 255 */
cmd->param[0] = pcan_device.device_id;
pcan_usb_send_command_buffer( cmd, sizeof( PCAN_USB_PARAM ) );
pcan_usb_send_command_buffer(cmd, sizeof(PCAN_USB_PARAM));
}
break;
/* serial */
case PCAN_USB_CMD_SN:
{
/* default: 4 bytes FFFFFFFF */
cmd->param[0] = (pcan_device.device_serial>>0x18)&0xFF;
cmd->param[1] = (pcan_device.device_serial>>0x10)&0xFF;
cmd->param[2] = (pcan_device.device_serial>>0x08)&0xFF;
cmd->param[3] = (pcan_device.device_serial>>0x00)&0xFF;
pcan_usb_send_command_buffer( cmd, sizeof( PCAN_USB_PARAM ) );
cmd->param[0] = (pcan_device.device_serial >> 0x18) & 0xFF;
cmd->param[1] = (pcan_device.device_serial >> 0x10) & 0xFF;
cmd->param[2] = (pcan_device.device_serial >> 0x08) & 0xFF;
cmd->param[3] = (pcan_device.device_serial >> 0x00) & 0xFF;
pcan_usb_send_command_buffer(cmd, sizeof(PCAN_USB_PARAM));
}
break;
/* sja1000 reg read */
case PCAN_USB_CMD_REGISTER:
cmd->param[1] = pcan_sja1000_read( cmd->param[0] );
pcan_usb_send_command_buffer( cmd, sizeof( PCAN_USB_PARAM ) );
cmd->param[1] = pcan_sja1000_read(cmd->param[0]);
pcan_usb_send_command_buffer(cmd, sizeof(PCAN_USB_PARAM));
break;
/* get led */
case PCAN_USB_CMD_LED:
cmd->param[0] = pcan_device.led_mode;
pcan_usb_send_command_buffer( cmd, sizeof( PCAN_USB_PARAM ) );
pcan_usb_send_command_buffer(cmd, sizeof(PCAN_USB_PARAM));
break;
}
break;
default:
assert( 0 );
assert(0);
break;
}
}
void pcan_protocol_process_data( uint8_t *ptr, uint16_t size )
void pcan_protocol_process_data(uint8_t *ptr, uint16_t size)
{
uint8_t msg_prefix, msg_rec_count;
/* invalid frame header len ? */
if( size < PCAN_USB_MSG_HEADER_LEN )
if (size < PCAN_USB_MSG_HEADER_LEN)
return;
msg_prefix = *ptr++;
@ -764,19 +760,20 @@ void pcan_protocol_process_data( uint8_t *ptr, uint16_t size )
size -= PCAN_USB_MSG_HEADER_LEN;
for( uint8_t i = 0; i < msg_rec_count; i++ )
for (uint8_t i = 0; i < msg_rec_count; i++)
{
uint16_t eated = 0, sl;
if( size < 1 )
if (size < 1)
return;
sl = unpack_u8( ptr );
ptr += sizeof( uint8_t );
size -= sizeof( uint8_t );;
sl = unpack_u8(ptr);
ptr += sizeof(uint8_t);
size -= sizeof(uint8_t);
;
/* status, error frame */
if( sl & PCAN_USB_STATUSLEN_INTERNAL )
if (sl & PCAN_USB_STATUSLEN_INTERNAL)
{
/* ??? abnormal, device support only CAN data frame */
eated = 0;
@ -784,16 +781,16 @@ void pcan_protocol_process_data( uint8_t *ptr, uint16_t size )
/* regular CAN data frame */
else
{
eated = pcan_decode_data_frame( ptr, size, sl );
eated = pcan_decode_data_frame(ptr, size, sl);
}
/* ? error indicator */
if( !eated )
if (!eated)
return;
eated = size - eated;
if( size < eated )
if (size < eated)
return;
ptr += eated;
@ -801,29 +798,29 @@ void pcan_protocol_process_data( uint8_t *ptr, uint16_t size )
}
}
void pcan_protocol_init( void )
void pcan_protocol_init(void)
{
pcan_device.sja1000_ic_mode = SJA1000_PELICAN;
pcan_record_buffer_reset( &pcan_records );
pcan_record_buffer_reset(&pcan_records);
pcan_can_init();
pcan_can_install_rx_callback( pcan_rx_message );
pcan_can_install_error_callback( pcan_can_error );
pcan_can_install_rx_callback(pcan_rx_message);
pcan_can_install_error_callback(pcan_can_error);
}
void pcan_protocol_poll( void )
void pcan_protocol_poll(void)
{
uint16_t ts_ms = pcan_timestamp_ticks();
if( pcan_device.bus_active )
if (pcan_device.bus_active)
{
/* each ~1000 ms */
if( ( ((uint16_t)( ts_ms - pcan_device.last_timestamp_sync )) >= PCAN_TICKS_FROM_US( 1000000u ) ) )
if ((((uint16_t)(ts_ms - pcan_device.last_timestamp_sync)) >= PCAN_TICKS_FROM_US(1000000u)))
{
pcan_device.last_timestamp_sync = ts_ms;
pcan_timesync_event( &pcan_records );
pcan_timesync_event(&pcan_records);
}
}
pcan_record_buffer_flush( &pcan_records );
pcan_record_buffer_flush(&pcan_records);
pcan_can_poll();
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <stdint.h>
void pcan_protocol_init( void );
void pcan_protocol_poll( void );
void pcan_protocol_process_command( uint8_t *ptr, uint16_t size );
void pcan_protocol_process_data( uint8_t *ptr, uint16_t size );
void pcan_protocol_init(void);
void pcan_protocol_poll(void);
void pcan_protocol_process_command(uint8_t *ptr, uint16_t size);
void pcan_protocol_process_data(uint8_t *ptr, uint16_t size);

View File

@ -3,16 +3,15 @@
#define TIM_BUS_FREQ (48000000)
void pcan_timestamp_init( void )
void pcan_timestamp_init(void)
{
switch( TIM_BUS_FREQ )
switch (TIM_BUS_FREQ)
{
case 48000000:
/* TIM3 on APB1 bus */
__HAL_RCC_TIM3_CLK_ENABLE();
TIM3->PSC = (2048-1); /* => tick = 42.666uS */
TIM3->PSC = (2048 - 1); /* => tick = 42.666uS */
/* set clock division to zero: */
TIM3->CR1 &= (uint16_t)(~TIM_CR1_CKD);
TIM3->CR1 |= TIM_CLOCKDIVISION_DIV1;
@ -20,17 +19,17 @@ void pcan_timestamp_init( void )
TIM3->CR1 |= TIM_CR1_CEN;
break;
default:
assert( 0 );
assert(0);
break;
}
}
uint16_t pcan_timestamp_millis( void )
uint16_t pcan_timestamp_millis(void)
{
return (HAL_GetTick()&0xFFFF);
return (HAL_GetTick() & 0xFFFF);
}
uint16_t pcan_timestamp_ticks( void )
uint16_t pcan_timestamp_ticks(void)
{
/* 1 pcan tick => 42.666 us */
return TIM3->CNT;

View File

@ -1,8 +1,8 @@
#pragma once
#include <stdint.h>
#define PCAN_TICKS_FROM_US( _us ) (((uint32_t)_us*1000u)/42666u)
#define PCAN_TICKS_FROM_US(_us) (((uint32_t)_us * 1000u) / 42666u)
void pcan_timestamp_init( void );
uint16_t pcan_timestamp_ticks( void );
uint16_t pcan_timestamp_millis( void );
void pcan_timestamp_init(void);
uint16_t pcan_timestamp_ticks(void);
uint16_t pcan_timestamp_millis(void);

View File

@ -1,12 +1,12 @@
#include <stm32f0xx_hal.h>
#include <assert.h>
#include "usbd_ctlreq.h"
#include "usbd_ioreq.h"
#include "usbd_conf.h"
#include "usbd_helper.h"
#include "pcan_protocol.h"
#include "pcan_led.h"
#include "pcan_usb.h"
#include "pcan_led.h"
#include "pcan_protocol.h"
#include "usbd_conf.h"
#include "usbd_ctlreq.h"
#include "usbd_helper.h"
#include "usbd_ioreq.h"
#include <assert.h>
#include <stm32f0xx_hal.h>
USBD_HandleTypeDef hUsbDeviceFS;
extern USBD_DescriptorsTypeDef FS_Desc;
@ -22,9 +22,8 @@ struct t_pcan_description
USB_ENDPOINT_DESCRIPTOR ep4;
};
__ALIGN_BEGIN static const USB_DEVICE_QUALIFIER_DESCRIPTOR dev_qua __ALIGN_END =
{
.bLength = sizeof( USB_DEVICE_QUALIFIER_DESCRIPTOR ),
__ALIGN_BEGIN static const USB_DEVICE_QUALIFIER_DESCRIPTOR dev_qua __ALIGN_END = {
.bLength = sizeof(USB_DEVICE_QUALIFIER_DESCRIPTOR),
.bDescriptorType = USB_QUALIFIER_DESCRIPTOR_TYPE,
.bcdUSB = 0x0100, /* 1.0 */
.bDeviceClass = 0,
@ -99,118 +98,113 @@ __ALIGN_BEGIN static struct t_pcan_description pcan_usb_dev __ALIGN_END =
};
static uint8_t device_init( USBD_HandleTypeDef *pdev, uint8_t cfgidx )
static uint8_t device_init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
USB_ENDPOINT_DESCRIPTOR *p_ep = &pcan_usb_dev.ep1;
UNUSED( cfgidx );
UNUSED(cfgidx);
for( int i = 0; i < pcan_usb_dev.if0.bNumEndpoints; i++ )
for (int i = 0; i < pcan_usb_dev.if0.bNumEndpoints; i++)
{
uint8_t ep_addr = p_ep[i].bEndpointAddress;
if( p_ep[i].bmAttributes == USB_ENDPOINT_TYPE_BULK )
if (p_ep[i].bmAttributes == USB_ENDPOINT_TYPE_BULK)
{
if( pdev->dev_speed == USBD_SPEED_FULL )
if (pdev->dev_speed == USBD_SPEED_FULL)
;
else if( pdev->dev_speed == USBD_SPEED_HIGH )
else if (pdev->dev_speed == USBD_SPEED_HIGH)
;
else
assert( 0 );
assert(0);
}
USBD_LL_OpenEP( pdev, ep_addr,
p_ep[i].bmAttributes,
p_ep[i].wMaxPacketSize );
USBD_LL_OpenEP(pdev, ep_addr, p_ep[i].bmAttributes, p_ep[i].wMaxPacketSize);
if( ( ep_addr & 0x80 ) != 0 )
if ((ep_addr & 0x80) != 0)
pdev->ep_in[ep_addr & EP_ADDR_MSK].is_used = 1;
else
pdev->ep_out[ep_addr & EP_ADDR_MSK].is_used = 1;
}
pdev->pClassData = (void*)&pcan_data;
pdev->pClassData = (void *)&pcan_data;
USBD_LL_PrepareReceive( pdev, PCAN_USB_EP_CMDOUT, pcan_data.buffer_cmd, sizeof( pcan_data.buffer_cmd ) );
USBD_LL_PrepareReceive( pdev, PCAN_USB_EP_MSGOUT, pcan_data.buffer_data, sizeof( pcan_data.buffer_data ) );
USBD_LL_PrepareReceive(pdev, PCAN_USB_EP_CMDOUT, pcan_data.buffer_cmd, sizeof(pcan_data.buffer_cmd));
USBD_LL_PrepareReceive(pdev, PCAN_USB_EP_MSGOUT, pcan_data.buffer_data, sizeof(pcan_data.buffer_data));
return USBD_OK;
}
static uint8_t device_deinit( USBD_HandleTypeDef *pdev, uint8_t cfgidx )
static uint8_t device_deinit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
USB_ENDPOINT_DESCRIPTOR const *p_ep = &pcan_usb_dev.ep1;
UNUSED( cfgidx );
UNUSED(cfgidx);
for( int i = 0; i < pcan_usb_dev.if0.bNumEndpoints; i++ )
for (int i = 0; i < pcan_usb_dev.if0.bNumEndpoints; i++)
{
uint8_t ep_addr = p_ep[i].bEndpointAddress;
USBD_LL_CloseEP( pdev, ep_addr );
if( ( ep_addr & 0x80 ) != 0 )
USBD_LL_CloseEP(pdev, ep_addr);
if ((ep_addr & 0x80) != 0)
pdev->ep_in[ep_addr & EP_ADDR_MSK].is_used = 0;
else
pdev->ep_out[ep_addr & EP_ADDR_MSK].is_used = 0;
}
pdev->pClassData = (void*)0;
pdev->pClassData = (void *)0;
return USBD_OK;
}
uint16_t pcan_usb_send_command_buffer( const void *p, uint16_t size )
uint16_t pcan_usb_send_command_buffer(const void *p, uint16_t size)
{
USBD_HandleTypeDef *pdev = &hUsbDeviceFS;
if( pdev->ep_in[PCAN_USB_EP_CMDIN & 0xFU].total_length )
if (pdev->ep_in[PCAN_USB_EP_CMDIN & 0xFU].total_length)
return 0;
pdev->ep_in[PCAN_USB_EP_CMDIN & 0xFU].total_length = size;
if( USBD_LL_Transmit( pdev, PCAN_USB_EP_CMDIN, (void*)p, size ) == USBD_OK )
if (USBD_LL_Transmit(pdev, PCAN_USB_EP_CMDIN, (void *)p, size) == USBD_OK)
return size;
return 0;
}
uint16_t pcan_usb_send_data_buffer( const void *p, uint16_t size )
uint16_t pcan_usb_send_data_buffer(const void *p, uint16_t size)
{
USBD_HandleTypeDef *pdev = &hUsbDeviceFS;
if( pdev->ep_in[PCAN_USB_EP_MSGIN & 0xFU].total_length )
if (pdev->ep_in[PCAN_USB_EP_MSGIN & 0xFU].total_length)
return 0;
pdev->ep_in[PCAN_USB_EP_MSGIN & 0xFU].total_length = size;
if( USBD_LL_Transmit( pdev, PCAN_USB_EP_MSGIN, (void*)p, size ) == USBD_OK )
if (USBD_LL_Transmit(pdev, PCAN_USB_EP_MSGIN, (void *)p, size) == USBD_OK)
return size;
return 0;
}
void pcan_usb_poll( void )
void pcan_usb_poll(void)
{
HAL_PCD_IRQHandler( &hpcd_USB_FS );
HAL_PCD_IRQHandler(&hpcd_USB_FS);
}
static uint8_t device_setup( USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req )
static uint8_t device_setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
switch( req->bRequest )
switch (req->bRequest)
{
/* get info */
case 0:
switch( req->wValue )
switch (req->wValue)
{
case 0: /* bootloader info */
{
static const uint8_t bootloader_info[] = {
0x00, 0x00, 0x08, 0x04, 0x00, 0x08, 0x07, 0x00,
0x04, 0x02, 0xe0, 0x07, 0x01, 0x00, 0x00, 0x00
};
USBD_CtlSendData( pdev, (void*)bootloader_info, sizeof( bootloader_info ) );
static const uint8_t bootloader_info[] = { 0x00, 0x00, 0x08, 0x04, 0x00, 0x08, 0x07, 0x00, 0x04, 0x02, 0xe0, 0x07, 0x01, 0x00, 0x00, 0x00 };
USBD_CtlSendData(pdev, (void *)bootloader_info, sizeof(bootloader_info));
pcan_led_set_mode( LED_CH0_RX, LED_MODE_ON, 0 );
pcan_led_set_mode( LED_CH0_TX, LED_MODE_ON, 0 );
pcan_led_set_mode(LED_CH0_RX, LED_MODE_ON, 0);
pcan_led_set_mode(LED_CH0_TX, LED_MODE_ON, 0);
/* reset endpoints */
pcan_flush_ep( PCAN_USB_EP_MSGIN );
pcan_flush_ep( PCAN_USB_EP_CMDIN );
pcan_flush_ep(PCAN_USB_EP_MSGIN);
pcan_flush_ep(PCAN_USB_EP_CMDIN);
}
break;
}
@ -219,18 +213,18 @@ static uint8_t device_setup( USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *re
return USBD_OK;
}
static uint8_t device_ep0_rx_ready( USBD_HandleTypeDef *pdev )
static uint8_t device_ep0_rx_ready(USBD_HandleTypeDef *pdev)
{
UNUSED( pdev );
UNUSED(pdev);
return USBD_OK;
}
/* data was sent to PC */
static uint8_t device_data_in( USBD_HandleTypeDef *pdev, uint8_t epnum )
static uint8_t device_data_in(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
struct t_class_data *p_data = (void*)pdev->pClassData;
struct t_class_data *p_data = (void *)pdev->pClassData;
if( pdev->pClassData == 0 )
if (pdev->pClassData == 0)
return USBD_FAIL;
/* use ZLP */
@ -265,25 +259,25 @@ static uint8_t device_data_in( USBD_HandleTypeDef *pdev, uint8_t epnum )
}
/* data was received from PC */
static uint8_t device_data_out( USBD_HandleTypeDef *pdev, uint8_t epnum )
static uint8_t device_data_out(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
int size;
if( pdev->pClassData == 0 )
if (pdev->pClassData == 0)
return USBD_FAIL;
size = USBD_LL_GetRxDataSize( pdev, epnum );
size = USBD_LL_GetRxDataSize(pdev, epnum);
(void)size;
if( epnum == PCAN_USB_EP_CMDOUT )
if (epnum == PCAN_USB_EP_CMDOUT)
{
pcan_protocol_process_command( pcan_data.buffer_cmd, size );
USBD_LL_PrepareReceive( pdev, PCAN_USB_EP_CMDOUT, pcan_data.buffer_cmd, sizeof( pcan_data.buffer_cmd ) );
pcan_protocol_process_command(pcan_data.buffer_cmd, size);
USBD_LL_PrepareReceive(pdev, PCAN_USB_EP_CMDOUT, pcan_data.buffer_cmd, sizeof(pcan_data.buffer_cmd));
}
else if( epnum == PCAN_USB_EP_MSGOUT )
else if (epnum == PCAN_USB_EP_MSGOUT)
{
pcan_protocol_process_data( pcan_data.buffer_data, size );
USBD_LL_PrepareReceive( pdev, PCAN_USB_EP_MSGOUT, pcan_data.buffer_data, sizeof( pcan_data.buffer_data ) );
pcan_protocol_process_data(pcan_data.buffer_data, size);
USBD_LL_PrepareReceive(pdev, PCAN_USB_EP_MSGOUT, pcan_data.buffer_data, sizeof(pcan_data.buffer_data));
}
else
{
@ -293,50 +287,46 @@ static uint8_t device_data_out( USBD_HandleTypeDef *pdev, uint8_t epnum )
return USBD_OK;
}
static uint8_t *device_get_hs_cfg( uint16_t *length )
static uint8_t *device_get_hs_cfg(uint16_t *length)
{
*length = sizeof( struct t_pcan_description );
return (void*)&pcan_usb_dev;
*length = sizeof(struct t_pcan_description);
return (void *)&pcan_usb_dev;
}
static uint8_t *device_get_fs_cfg( uint16_t *length )
static uint8_t *device_get_fs_cfg(uint16_t *length)
{
*length = sizeof( struct t_pcan_description );
return (void*)&pcan_usb_dev;
*length = sizeof(struct t_pcan_description);
return (void *)&pcan_usb_dev;
}
static uint8_t *device_get_other_speed_cfg( uint16_t *length )
static uint8_t *device_get_other_speed_cfg(uint16_t *length)
{
*length = sizeof( struct t_pcan_description );
return (void*)&pcan_usb_dev;
*length = sizeof(struct t_pcan_description);
return (void *)&pcan_usb_dev;
}
static uint8_t *device_get_device_qualifier( uint16_t *length )
static uint8_t *device_get_device_qualifier(uint16_t *length)
{
*length = sizeof( USB_DEVICE_QUALIFIER_DESCRIPTOR );
*length = sizeof(USB_DEVICE_QUALIFIER_DESCRIPTOR);
return (void*)&dev_qua;
return (void *)&dev_qua;
}
static uint8_t *device_get_user_string( USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *length )
static uint8_t *device_get_user_string(USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *length)
{
UNUSED( pdev );
if( index == 10 )
UNUSED(pdev);
if (index == 10)
{
__ALIGN_BEGIN static const uint16_t vendor_descriptor[1+24] __ALIGN_END =
{
0x0332,
'P','E','A','K','-','S','y','s','t','e','m',' ','T','e','c','h','n','i','k',' ','G','m','b','H'
};
*length = sizeof( vendor_descriptor );
return (uint8_t*)vendor_descriptor;
__ALIGN_BEGIN static const uint16_t vendor_descriptor[1 + 24] __ALIGN_END = { 0x0332, 'P', 'E', 'A', 'K', '-', 'S', 'y', 's', 't', 'e', 'm', ' ',
'T', 'e', 'c', 'h', 'n', 'i', 'k', ' ', 'G', 'm', 'b', 'H' };
*length = sizeof(vendor_descriptor);
return (uint8_t *)vendor_descriptor;
}
return 0;
}
USBD_ClassTypeDef usbd_pcan =
{
USBD_ClassTypeDef usbd_pcan = {
.Init = device_init,
.DeInit = device_deinit,
.Setup = device_setup,
@ -356,62 +346,62 @@ USBD_ClassTypeDef usbd_pcan =
#endif
};
void pcan_usb_init( void )
void pcan_usb_init(void)
{
if( USBD_Init( &hUsbDeviceFS, &FS_Desc, DEVICE_FS ) != USBD_OK )
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
{
assert( 0 );
assert(0);
}
if( USBD_RegisterClass( &hUsbDeviceFS, &usbd_pcan ) != USBD_OK )
if (USBD_RegisterClass(&hUsbDeviceFS, &usbd_pcan) != USBD_OK)
{
assert( 0 );
assert(0);
}
if( USBD_Start(&hUsbDeviceFS) != USBD_OK )
if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
{
assert( 0 );
assert(0);
}
}
int pcan_flush_ep( uint8_t ep )
int pcan_flush_ep(uint8_t ep)
{
USBD_HandleTypeDef *pdev = &hUsbDeviceFS;
struct t_class_data *p_data = (void*)pdev->pClassData;
struct t_class_data *p_data = (void *)pdev->pClassData;
p_data->ep_tx_in_use[ep&0x0F] = 0;
return USBD_LL_FlushEP( pdev, ep ) == USBD_OK;
p_data->ep_tx_in_use[ep & 0x0F] = 0;
return USBD_LL_FlushEP(pdev, ep) == USBD_OK;
}
int pcan_flush_data( struct t_m2h_fsm *pfsm, void *src, int size )
int pcan_flush_data(struct t_m2h_fsm *pfsm, void *src, int size)
{
USBD_HandleTypeDef *pdev = &hUsbDeviceFS;
struct t_class_data *p_data = (void*)pdev->pClassData;
struct t_class_data *p_data = (void *)pdev->pClassData;
if( !p_data )
if (!p_data)
return 0;
switch( pfsm->state )
switch (pfsm->state)
{
case 1:
if( p_data->ep_tx_in_use[pfsm->ep_addr&0x0F] )
if (p_data->ep_tx_in_use[pfsm->ep_addr & 0x0F])
{
p_data->ep_tx_data_pending[pfsm->ep_addr&0x0F] = size;
p_data->ep_tx_data_pending[pfsm->ep_addr & 0x0F] = size;
return 0;
}
pfsm->state = 0;
/* fall through */
case 0:
assert( p_data->ep_tx_in_use[pfsm->ep_addr&0x0F] == 0 );
if( size > pfsm->dbsize )
assert(p_data->ep_tx_in_use[pfsm->ep_addr & 0x0F] == 0);
if (size > pfsm->dbsize)
break;
memcpy( pfsm->pdbuf, src, size );
p_data->ep_tx_in_use[pfsm->ep_addr&0x0F] = 1;
memcpy(pfsm->pdbuf, src, size);
p_data->ep_tx_in_use[pfsm->ep_addr & 0x0F] = 1;
/* prepare data transmit */
pdev->ep_in[pfsm->ep_addr & EP_ADDR_MSK].total_length = size;
/* no pending data */
p_data->ep_tx_data_pending[pfsm->ep_addr&0x0F] = 0;
USBD_LL_Transmit( pdev, pfsm->ep_addr, pfsm->pdbuf, size );
p_data->ep_tx_data_pending[pfsm->ep_addr & 0x0F] = 0;
USBD_LL_Transmit(pdev, pfsm->ep_addr, pfsm->pdbuf, size);
pfsm->total_tx += size;
pfsm->state = 1;

View File

@ -24,11 +24,10 @@ struct t_m2h_fsm
uint32_t total_tx;
};
int pcan_flush_ep( uint8_t ep );
int pcan_flush_data( struct t_m2h_fsm *pfsm, void *src, int size );
uint16_t pcan_usb_send_command_buffer( const void *p, uint16_t size );
uint16_t pcan_usb_send_data_buffer( const void *p, uint16_t size );
void pcan_usb_init( void );
void pcan_usb_poll( void );
int pcan_flush_ep(uint8_t ep);
int pcan_flush_data(struct t_m2h_fsm *pfsm, void *src, int size);
uint16_t pcan_usb_send_command_buffer(const void *p, uint16_t size);
uint16_t pcan_usb_send_data_buffer(const void *p, uint16_t size);
void pcan_usb_init(void);
void pcan_usb_poll(void);

View File

@ -2,7 +2,7 @@
#include "io_macro.h"
#if (defined CANABLE) || (defined ENTREE) || (defined CANTACT_8 ) || (defined CANTACT_16 )
#if (defined CANABLE) || (defined ENTREE) || (defined CANTACT_8) || (defined CANTACT_16)
#define IOPIN_TX A, 1, MODE_OUTPUT_PP, NOPULL, SPEED_FREQ_MEDIUM, NOAF
#define IOPIN_RX A, 0, MODE_OUTPUT_PP, NOPULL, SPEED_FREQ_MEDIUM, NOAF
#define LED_ON PIN_HI

View File

@ -1,34 +1,34 @@
#pragma once
#define unpack_u8( ptr ) ( ((uint8_t*)(ptr))[0] )
#define unpack_u16( ptr ) (\
( ((uint8_t*)(ptr))[1] << 8 ) |\
( ((uint8_t*)(ptr))[0] ) )
#define unpack_u32( ptr ) (\
( ((uint8_t*)ptr)[3] << 24 ) |\
( ((uint8_t*)ptr)[2] << 16 ) |\
( ((uint8_t*)ptr)[1] << 8 ) |\
( ((uint8_t*)ptr)[0] ) )
#define unpack_u8(ptr) (((uint8_t *)(ptr))[0])
#define unpack_u16(ptr) ((((uint8_t *)(ptr))[1] << 8) | (((uint8_t *)(ptr))[0]))
#define unpack_u32(ptr) ((((uint8_t *)ptr)[3] << 24) | (((uint8_t *)ptr)[2] << 16) | (((uint8_t *)ptr)[1] << 8) | (((uint8_t *)ptr)[0]))
#define unpack_float( ptr ) *(float*)(uint32_t[]){ unpack_u32( ptr ) }
#define unpack_float(ptr) \
*(float *)(uint32_t[]) \
{ \
unpack_u32(ptr) \
}
#define pack_u8( ptr, data ) ( ((uint8_t*)(ptr))[0] = ( data ) )
#define pack_u16( ptr, data ) do{ \
((uint8_t*)(ptr))[0] = (data)&0xFF;\
((uint8_t*)(ptr))[1] = ((data)>>8)&0xFF;\
}while(0)
#define pack_u8(ptr, data) (((uint8_t *)(ptr))[0] = (data))
#define pack_u16(ptr, data) \
do { \
((uint8_t *)(ptr))[0] = (data)&0xFF; \
((uint8_t *)(ptr))[1] = ((data) >> 8) & 0xFF; \
} while (0)
#define pack_u24( ptr, data ) do{ \
(uint8_t*)(ptr)[0] = (data)& 0xFF; \
(uint8_t*)(ptr)[1] = ( ( data ) >> 8 ) & 0xFF; \
(uint8_t*)(ptr)[2] = ( ( data ) >> 16 ) & 0xFF; \
}while( 0 )
#define pack_u32( _xptr, _xdata ) do{ \
((uint8_t*)(_xptr))[0] = (_xdata)&0xFF;\
((uint8_t*)(_xptr))[1] = ((_xdata)>>8)&0xFF;\
((uint8_t*)(_xptr))[2] = ((_xdata)>>16)&0xFF;\
((uint8_t*)(_xptr))[3] = ((_xdata)>>24)&0xFF;\
}while(0)
#define pack_u24(ptr, data) \
do { \
(uint8_t *)(ptr)[0] = (data)&0xFF; \
(uint8_t *)(ptr)[1] = ((data) >> 8) & 0xFF; \
(uint8_t *)(ptr)[2] = ((data) >> 16) & 0xFF; \
} while (0)
#define pack_u32(_xptr, _xdata) \
do { \
((uint8_t *)(_xptr))[0] = (_xdata)&0xFF; \
((uint8_t *)(_xptr))[1] = ((_xdata) >> 8) & 0xFF; \
((uint8_t *)(_xptr))[2] = ((_xdata) >> 16) & 0xFF; \
((uint8_t *)(_xptr))[3] = ((_xdata) >> 24) & 0xFF; \
} while (0)

View File

@ -1,12 +1,12 @@
#pragma once
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
#define HAL_MODULE_ENABLED
/*#define HAL_ADC_MODULE_ENABLED */
/*#define HAL_ADC_MODULE_ENABLED */
/*#define HAL_CRYP_MODULE_ENABLED */
#define HAL_CAN_MODULE_ENABLED
/*#define HAL_CEC_MODULE_ENABLED */
@ -40,58 +40,60 @@
/* #define HAL_I2C_MODULE_ENABLED */
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */
#if !defined(HSE_VALUE)
#define HSE_VALUE ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined (HSE_STARTUP_TIMEOUT)
#define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */
#if !defined(HSE_STARTUP_TIMEOUT)
#define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */
#if !defined (HSI_VALUE)
#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
#if !defined(HSI_VALUE)
#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
#if !defined (HSI_STARTUP_TIMEOUT)
#define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */
#if !defined(HSI_STARTUP_TIMEOUT)
#define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */
#endif /* HSI_STARTUP_TIMEOUT */
/**
* @brief Internal High Speed oscillator for ADC (HSI14) value.
*/
#if !defined (HSI14_VALUE)
#define HSI14_VALUE ((uint32_t)14000000) /*!< Value of the Internal High Speed oscillator for ADC in Hz.
The real value may vary depending on the variations
#if !defined(HSI14_VALUE)
#define HSI14_VALUE \
((uint32_t)14000000) /*!< Value of the Internal High Speed oscillator for ADC in Hz. \
The real value may vary depending on the variations \
in voltage and temperature. */
#endif /* HSI14_VALUE */
/**
* @brief Internal High Speed oscillator for USB (HSI48) value.
*/
#if !defined (HSI48_VALUE)
#define HSI48_VALUE ((uint32_t)48000000) /*!< Value of the Internal High Speed oscillator for USB in Hz.
The real value may vary depending on the variations
#if !defined(HSI48_VALUE)
#define HSI48_VALUE \
((uint32_t)48000000) /*!< Value of the Internal High Speed oscillator for USB in Hz. \
The real value may vary depending on the variations \
in voltage and temperature. */
#endif /* HSI48_VALUE */
/**
* @brief Internal Low Speed oscillator (LSI) value.
*/
#if !defined (LSI_VALUE)
#define LSI_VALUE ((uint32_t)40000)
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
The real value may vary depending on the variations
#if !defined(LSI_VALUE)
#define LSI_VALUE ((uint32_t)40000)
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz \
The real value may vary depending on the variations \
in voltage and temperature. */
/**
* @brief External Low Speed oscillator (LSI) value.
*/
#if !defined (LSE_VALUE)
#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */
#if !defined(LSE_VALUE)
#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */
#endif /* LSE_VALUE */
#if !defined (LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
#if !defined(LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
#endif /* LSE_STARTUP_TIMEOUT */
/* Tip: To avoid modifying this file each time you need to use different HSE,
@ -143,111 +145,111 @@
*/
#ifdef HAL_RCC_MODULE_ENABLED
#include "stm32f0xx_hal_rcc.h"
#include "stm32f0xx_hal_rcc.h"
#endif /* HAL_RCC_MODULE_ENABLED */
#ifdef HAL_GPIO_MODULE_ENABLED
#include "stm32f0xx_hal_gpio.h"
#include "stm32f0xx_hal_gpio.h"
#endif /* HAL_GPIO_MODULE_ENABLED */
#ifdef HAL_EXTI_MODULE_ENABLED
#include "stm32f0xx_hal_exti.h"
#include "stm32f0xx_hal_exti.h"
#endif /* HAL_EXTI_MODULE_ENABLED */
#ifdef HAL_DMA_MODULE_ENABLED
#include "stm32f0xx_hal_dma.h"
#include "stm32f0xx_hal_dma.h"
#endif /* HAL_DMA_MODULE_ENABLED */
#ifdef HAL_CORTEX_MODULE_ENABLED
#include "stm32f0xx_hal_cortex.h"
#include "stm32f0xx_hal_cortex.h"
#endif /* HAL_CORTEX_MODULE_ENABLED */
#ifdef HAL_ADC_MODULE_ENABLED
#include "stm32f0xx_hal_adc.h"
#include "stm32f0xx_hal_adc.h"
#endif /* HAL_ADC_MODULE_ENABLED */
#ifdef HAL_CAN_MODULE_ENABLED
#include "stm32f0xx_hal_can.h"
#include "stm32f0xx_hal_can.h"
#endif /* HAL_CAN_MODULE_ENABLED */
#ifdef HAL_CEC_MODULE_ENABLED
#include "stm32f0xx_hal_cec.h"
#include "stm32f0xx_hal_cec.h"
#endif /* HAL_CEC_MODULE_ENABLED */
#ifdef HAL_COMP_MODULE_ENABLED
#include "stm32f0xx_hal_comp.h"
#include "stm32f0xx_hal_comp.h"
#endif /* HAL_COMP_MODULE_ENABLED */
#ifdef HAL_CRC_MODULE_ENABLED
#include "stm32f0xx_hal_crc.h"
#include "stm32f0xx_hal_crc.h"
#endif /* HAL_CRC_MODULE_ENABLED */
#ifdef HAL_DAC_MODULE_ENABLED
#include "stm32f0xx_hal_dac.h"
#include "stm32f0xx_hal_dac.h"
#endif /* HAL_DAC_MODULE_ENABLED */
#ifdef HAL_FLASH_MODULE_ENABLED
#include "stm32f0xx_hal_flash.h"
#include "stm32f0xx_hal_flash.h"
#endif /* HAL_FLASH_MODULE_ENABLED */
#ifdef HAL_I2C_MODULE_ENABLED
#include "stm32f0xx_hal_i2c.h"
#include "stm32f0xx_hal_i2c.h"
#endif /* HAL_I2C_MODULE_ENABLED */
#ifdef HAL_I2S_MODULE_ENABLED
#include "stm32f0xx_hal_i2s.h"
#include "stm32f0xx_hal_i2s.h"
#endif /* HAL_I2S_MODULE_ENABLED */
#ifdef HAL_IRDA_MODULE_ENABLED
#include "stm32f0xx_hal_irda.h"
#include "stm32f0xx_hal_irda.h"
#endif /* HAL_IRDA_MODULE_ENABLED */
#ifdef HAL_IWDG_MODULE_ENABLED
#include "stm32f0xx_hal_iwdg.h"
#include "stm32f0xx_hal_iwdg.h"
#endif /* HAL_IWDG_MODULE_ENABLED */
#ifdef HAL_PCD_MODULE_ENABLED
#include "stm32f0xx_hal_pcd.h"
#include "stm32f0xx_hal_pcd.h"
#endif /* HAL_PCD_MODULE_ENABLED */
#ifdef HAL_PWR_MODULE_ENABLED
#include "stm32f0xx_hal_pwr.h"
#include "stm32f0xx_hal_pwr.h"
#endif /* HAL_PWR_MODULE_ENABLED */
#ifdef HAL_RTC_MODULE_ENABLED
#include "stm32f0xx_hal_rtc.h"
#include "stm32f0xx_hal_rtc.h"
#endif /* HAL_RTC_MODULE_ENABLED */
#ifdef HAL_SMARTCARD_MODULE_ENABLED
#include "stm32f0xx_hal_smartcard.h"
#include "stm32f0xx_hal_smartcard.h"
#endif /* HAL_SMARTCARD_MODULE_ENABLED */
#ifdef HAL_SMBUS_MODULE_ENABLED
#include "stm32f0xx_hal_smbus.h"
#include "stm32f0xx_hal_smbus.h"
#endif /* HAL_SMBUS_MODULE_ENABLED */
#ifdef HAL_SPI_MODULE_ENABLED
#include "stm32f0xx_hal_spi.h"
#include "stm32f0xx_hal_spi.h"
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_TIM_MODULE_ENABLED
#include "stm32f0xx_hal_tim.h"
#include "stm32f0xx_hal_tim.h"
#endif /* HAL_TIM_MODULE_ENABLED */
#ifdef HAL_TSC_MODULE_ENABLED
#include "stm32f0xx_hal_tsc.h"
#include "stm32f0xx_hal_tsc.h"
#endif /* HAL_TSC_MODULE_ENABLED */
#ifdef HAL_UART_MODULE_ENABLED
#include "stm32f0xx_hal_uart.h"
#include "stm32f0xx_hal_uart.h"
#endif /* HAL_UART_MODULE_ENABLED */
#ifdef HAL_USART_MODULE_ENABLED
#include "stm32f0xx_hal_usart.h"
#include "stm32f0xx_hal_usart.h"
#endif /* HAL_USART_MODULE_ENABLED */
#ifdef HAL_WWDG_MODULE_ENABLED
#include "stm32f0xx_hal_wwdg.h"
#include "stm32f0xx_hal_wwdg.h"
#endif /* HAL_WWDG_MODULE_ENABLED */
/* Exported macro ------------------------------------------------------------*/
@ -260,11 +262,11 @@
* If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t* file, uint32_t line);
void assert_failed(uint8_t *file, uint32_t line);
#else
#define assert_param(expr) ((void)0U)
#define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */
#ifdef __cplusplus

View File

@ -80,18 +80,21 @@
/** @addtogroup STM32F0xx_System_Private_Defines
* @{
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
#if !defined(HSE_VALUE)
#define HSE_VALUE \
((uint32_t)8000000) /*!< Default value of the External oscillator in Hz. \
This value can be provided and adapted by the user application. */
#endif /* HSE_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
#if !defined(HSI_VALUE)
#define HSI_VALUE \
((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz. \
This value can be provided and adapted by the user application. */
#endif /* HSI_VALUE */
#if !defined (HSI48_VALUE)
#define HSI48_VALUE ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz.
#if !defined(HSI48_VALUE)
#define HSI48_VALUE \
((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz. \
This value can be provided and adapted by the user application. */
#endif /* HSI48_VALUE */
/**
@ -109,18 +112,18 @@
/** @addtogroup STM32F0xx_System_Private_Variables
* @{
*/
/* This variable is updated in three ways:
/* This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock there is no need to
call the 2 first functions listed above, since SystemCoreClock variable is
updated automatically.
*/
*/
uint32_t SystemCoreClock = 8000000;
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
const uint8_t AHBPrescTable[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9 };
const uint8_t APBPrescTable[8] = { 0, 0, 0, 0, 1, 2, 3, 4 };
/**
* @}
@ -189,7 +192,7 @@ void SystemInit(void)
* @param None
* @retval None
*/
void SystemCoreClockUpdate (void)
void SystemCoreClockUpdate(void)
{
uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
@ -208,33 +211,32 @@ void SystemCoreClockUpdate (void)
/* Get PLL clock source and multiplication factor ----------------------*/
pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
pllmull = ( pllmull >> 18) + 2;
pllmull = (pllmull >> 18) + 2;
predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
{
/* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */
SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull;
SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
}
#if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx)
else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV)
{
/* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */
SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull;
SystemCoreClock = (HSI48_VALUE / predivfactor) * pllmull;
}
#endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */
else
{
#if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) \
|| defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) \
|| defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
#if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) || defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F070xB) \
|| defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
/* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */
SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull;
SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull;
#else
/* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */
SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
#endif /* STM32F042x6 || STM32F048xx || STM32F070x6 ||
STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB ||
#endif /* STM32F042x6 || STM32F048xx || STM32F070x6 || \
STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB || \
STM32F091xC || STM32F098xx || STM32F030xC */
}
break;
@ -262,4 +264,3 @@ void SystemCoreClockUpdate (void)
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -1,71 +1,71 @@
#include <assert.h>
#include "usbd_conf.h"
#include "stm32f0xx.h"
#include "stm32f0xx_hal.h"
#include "usbd_def.h"
#include "usbd_core.h"
#include "usbd_conf.h"
#include "usbd_def.h"
#include <assert.h>
PCD_HandleTypeDef hpcd_USB_FS;
void Error_Handler(void);
static USBD_StatusTypeDef USBD_Get_USB_Status( HAL_StatusTypeDef hal_status );
static void SystemClockConfig_Resume( void );
extern void SystemClock_Config( void );
static USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status);
static void SystemClockConfig_Resume(void);
extern void SystemClock_Config(void);
void HAL_PCD_MspInit( PCD_HandleTypeDef* pcdHandle )
void HAL_PCD_MspInit(PCD_HandleTypeDef *pcdHandle)
{
if( pcdHandle->Instance == USB )
if (pcdHandle->Instance == USB)
{
__HAL_RCC_USB_CLK_ENABLE();
}
}
void HAL_PCD_MspDeInit( PCD_HandleTypeDef* pcdHandle )
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *pcdHandle)
{
if( pcdHandle->Instance == USB )
if (pcdHandle->Instance == USB)
{
__HAL_RCC_USB_CLK_DISABLE();
HAL_NVIC_DisableIRQ( USB_IRQn );
HAL_NVIC_DisableIRQ(USB_IRQn);
}
}
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
USBD_LL_SetupStage((USBD_HandleTypeDef *)hpcd->pData, (uint8_t *)hpcd->Setup);
}
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
USBD_LL_DataOutStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
}
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
USBD_LL_DataInStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
}
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
USBD_LL_SOF((USBD_HandleTypeDef *)hpcd->pData);
}
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
{
USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
if( hpcd->Init.speed != PCD_SPEED_FULL )
if (hpcd->Init.speed != PCD_SPEED_FULL)
{
assert(0);
}
USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);
USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
USBD_LL_SetSpeed((USBD_HandleTypeDef *)hpcd->pData, speed);
USBD_LL_Reset((USBD_HandleTypeDef *)hpcd->pData);
}
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_Suspend( (USBD_HandleTypeDef*)hpcd->pData );
USBD_LL_Suspend((USBD_HandleTypeDef *)hpcd->pData);
if (hpcd->Init.low_power_enable)
{
/* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
@ -78,30 +78,30 @@ void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
if (hpcd->Init.low_power_enable)
{
/* Reset SLEEPDEEP bit of Cortex System Control Register. */
SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
SCB->SCR &= (uint32_t) ~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
SystemClockConfig_Resume();
}
USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
USBD_LL_Resume((USBD_HandleTypeDef *)hpcd->pData);
}
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum);
}
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
USBD_LL_IsoINIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum);
}
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
USBD_LL_DevConnected((USBD_HandleTypeDef *)hpcd->pData);
}
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
USBD_LL_DevDisconnected((USBD_HandleTypeDef *)hpcd->pData);
}
USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
@ -117,17 +117,17 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
hpcd_USB_FS.Init.lpm_enable = DISABLE;
hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
if( HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK )
if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
{
assert( 0 );
assert(0);
}
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18+(0*USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x18+(1*USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x18+(2*USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0x18+(3*USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x02 , PCD_SNG_BUF, 0x18+(4*USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x18+(5*USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x00, PCD_SNG_BUF, 0x18 + (0 * USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x80, PCD_SNG_BUF, 0x18 + (1 * USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x01, PCD_SNG_BUF, 0x18 + (2 * USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x81, PCD_SNG_BUF, 0x18 + (3 * USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x02, PCD_SNG_BUF, 0x18 + (4 * USB_FS_MAX_PACKET_SIZE));
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x82, PCD_SNG_BUF, 0x18 + (5 * USB_FS_MAX_PACKET_SIZE));
return USBD_OK;
}
@ -230,9 +230,9 @@ USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_add
uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
{
PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData;
PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData;
if((ep_addr & 0x80) == 0x80)
if ((ep_addr & 0x80) == 0x80)
{
return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
}
@ -280,7 +280,7 @@ USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_a
uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
{
return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr);
return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef *)pdev->pData, ep_addr);
}
void USBD_LL_Delay(uint32_t Delay)
@ -311,19 +311,19 @@ USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status)
switch (hal_status)
{
case HAL_OK :
case HAL_OK:
usb_status = USBD_OK;
break;
case HAL_ERROR :
case HAL_ERROR:
usb_status = USBD_FAIL;
break;
case HAL_BUSY :
case HAL_BUSY:
usb_status = USBD_BUSY;
break;
case HAL_TIMEOUT :
case HAL_TIMEOUT:
usb_status = USBD_FAIL;
break;
default :
default:
usb_status = USBD_FAIL;
break;
}

View File

@ -1,10 +1,10 @@
#pragma once
#include "stm32f0xx.h"
#include "stm32f0xx_hal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f0xx.h"
#include "stm32f0xx_hal.h"
#define USBD_SUPPORT_USER_STRING_DESC (1)

View File

@ -1,21 +1,20 @@
#include "usbd_core.h"
#include "usbd_conf.h"
#include "usbd_core.h"
#define USBD_VID 0x0C72
#define USBD_PID_FS 0x000C
#define USBD_LANGID_STRING 1033
#define USBD_MAX_STR_DESC_SIZ 0x100U
uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
{
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
0x12, /*bLength */
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
0x00, /*bcdUSB */
@ -36,22 +35,19 @@ __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
};
__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
{
USB_LEN_LANGID_STR_DESC,
__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { USB_LEN_LANGID_STR_DESC,
USB_DESC_TYPE_STRING,
LOBYTE(USBD_LANGID_STRING),
HIBYTE(USBD_LANGID_STRING)
};
HIBYTE(USBD_LANGID_STRING) };
uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
*length = sizeof(USBD_FS_DeviceDesc);
return USBD_FS_DeviceDesc;
}
uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
*length = sizeof(USBD_LangIDDesc);
@ -59,30 +55,25 @@ uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
}
/* must be here: 1, 2 */
uint8_t * USBD_FS_HugeStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
uint8_t *USBD_FS_HugeStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED( speed );
UNUSED(speed);
/* little hack to save some flash memory */
__ALIGN_BEGIN static const uint16_t huge_descriptor[1/*+126*/] __ALIGN_END = { 0x03FE };
*length = sizeof( huge_descriptor );
return (uint8_t*)huge_descriptor;
__ALIGN_BEGIN static const uint16_t huge_descriptor[1 /*+126*/] __ALIGN_END = { 0x03FE };
*length = sizeof(huge_descriptor);
return (uint8_t *)huge_descriptor;
}
uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
__ALIGN_BEGIN static const uint16_t cfg_descriptor[1+8] __ALIGN_END =
{
0x0312,
'P','C','A','N','-','U','S','B'
};
*length = sizeof( cfg_descriptor );
return (uint8_t*)cfg_descriptor;
__ALIGN_BEGIN static const uint16_t cfg_descriptor[1 + 8] __ALIGN_END = { 0x0312, 'P', 'C', 'A', 'N', '-', 'U', 'S', 'B' };
*length = sizeof(cfg_descriptor);
return (uint8_t *)cfg_descriptor;
}
USBD_DescriptorsTypeDef FS_Desc =
{
USBD_DescriptorsTypeDef FS_Desc = {
.GetDeviceDescriptor = USBD_FS_DeviceDescriptor,
.GetLangIDStrDescriptor = USBD_FS_LangIDStrDescriptor,
.GetManufacturerStrDescriptor = USBD_FS_HugeStrDescriptor,
@ -91,4 +82,3 @@ USBD_DescriptorsTypeDef FS_Desc =
.GetConfigurationStrDescriptor = USBD_FS_ConfigStrDescriptor,
.GetInterfaceStrDescriptor = 0,
};

View File

@ -42,13 +42,12 @@
#define USB_DEVICE_CLASS_HUB 0x09
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
#pragma pack( push, 1 )
#pragma pack(push, 1)
typedef struct _USB_COMMON_DESCRIPTOR
{
uint8_t bLength;
uint8_t bDescriptorType;
}
USB_COMMON_DESCRIPTOR, *PUSB_COMMON_DESCRIPTOR;
} USB_COMMON_DESCRIPTOR, *PUSB_COMMON_DESCRIPTOR;
typedef struct _USB_CONFIGURATION_DESCRIPTOR
{
@ -60,8 +59,7 @@ typedef struct _USB_CONFIGURATION_DESCRIPTOR
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t MaxPower;
}
USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
} USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
typedef struct _USB_DEVICE_DESCRIPTOR
{
@ -79,8 +77,7 @@ typedef struct _USB_DEVICE_DESCRIPTOR
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
}
USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR
{
@ -93,8 +90,7 @@ typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR
uint8_t bMaxPacketSize0;
uint8_t bNumConfigurations;
uint8_t bReserved;
}
USB_DEVICE_QUALIFIER_DESCRIPTOR, *PUSB_DEVICE_QUALIFIER_DESCRIPTOR;
} USB_DEVICE_QUALIFIER_DESCRIPTOR, *PUSB_DEVICE_QUALIFIER_DESCRIPTOR;
typedef enum _USB_DEVICE_SPEED
{
@ -102,8 +98,7 @@ typedef enum _USB_DEVICE_SPEED
UsbFullSpeed,
UsbHighSpeed,
UsbSuperSpeed
}
USB_DEVICE_SPEED;
} USB_DEVICE_SPEED;
typedef struct _USB_ENDPOINT_DESCRIPTOR
{
@ -113,8 +108,7 @@ typedef struct _USB_ENDPOINT_DESCRIPTOR
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
}
USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
} USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
typedef struct _USB_INTERFACE_DESCRIPTOR
{
@ -127,16 +121,13 @@ typedef struct _USB_INTERFACE_DESCRIPTOR
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
}
USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
typedef struct _USB_STRING_DESCRIPTOR
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bString[1];
}
USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
#pragma pack( pop )
} USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
#pragma pack(pop)