mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
[CAN] adding raw class first stage
This commit is contained in:
parent
ecfea6da9f
commit
68fe402f8e
@ -1,129 +0,0 @@
|
||||
#include "CAN.h"
|
||||
#include "sn65hvd234.h"
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Variables
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
/* CAN0 Transceiver */
|
||||
SSN65HVD234_Data gCanTransceiver0 ;
|
||||
/* CAN1 Transceiver */
|
||||
SSN65HVD234_Data gCanTransceiver1 ;
|
||||
|
||||
/* CAN0 Transfer */
|
||||
SCanTransfer gCanTransfer0 ;
|
||||
/* CAN1 Transfer */
|
||||
SCanTransfer gCanTransfer1 ;
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Local functions
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
uint32_t CAN_InitShieldHardware( uint32_t )
|
||||
{
|
||||
// Initialize CAN0 pins
|
||||
PIO_Configure(
|
||||
g_APinDescription[PINS_CAN0].pPort,
|
||||
g_APinDescription[PINS_CAN0].ulPinType,
|
||||
g_APinDescription[PINS_CAN0].ulPin,
|
||||
g_APinDescription[PINS_CAN0].ulPinConfiguration);
|
||||
|
||||
/* Initialize CAN0 Transceiver */
|
||||
SN65HVD234_Init( &gCanTransceiver0 ) ;
|
||||
SN65HVD234_SetRs( &gCanTransceiver0, PIOB, PIO_PB20 ) ;
|
||||
SN65HVD234_SetEN( &gCanTransceiver0, PIOB, PIO_PB21 ) ;
|
||||
/* Enable CAN0 Transceiver */
|
||||
SN65HVD234_DisableLowPower( &gCanTransceiver0 ) ;
|
||||
SN65HVD234_Enable( &gCanTransceiver0 ) ;
|
||||
|
||||
// Initialize CAN1 pins
|
||||
PIO_Configure(
|
||||
g_APinDescription[PINS_CAN1].pPort,
|
||||
g_APinDescription[PINS_CAN1].ulPinType,
|
||||
g_APinDescription[PINS_CAN1].ulPin,
|
||||
g_APinDescription[PINS_CAN1].ulPinConfiguration);
|
||||
|
||||
/* Initialize CAN1 Transceiver */
|
||||
SN65HVD234_Init( &gCanTransceiver1 ) ;
|
||||
SN65HVD234_SetRs( &gCanTransceiver1, PIOE, PIO_PB15 ) ;
|
||||
SN65HVD234_SetEN( &gCanTransceiver1, PIOE, PIO_PB16 ) ;
|
||||
/* Enable CAN1 Transceiver */
|
||||
SN65HVD234_DisableLowPower( &gCanTransceiver1 ) ;
|
||||
SN65HVD234_Enable( &gCanTransceiver1 ) ;
|
||||
}
|
||||
|
||||
uint32_t CAN_DeInitShieldHardware( uint32_t )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Exported functions
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* \brief Default interrupt handler for CAN 0.
|
||||
*/
|
||||
void CAN0_IrqHandler( void )
|
||||
{
|
||||
CAN_Handler( CAN0, &gCanTransfer0 ) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Default interrupt handler for CAN 1.
|
||||
*/
|
||||
void CAN1_IrqHandler( void )
|
||||
{
|
||||
CAN_Handler( CAN1, &gCanTransfer1 ) ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* main function
|
||||
*/
|
||||
extern int main( void )
|
||||
{
|
||||
|
||||
if ( ( CAN_Init( CAN0, BOARD_MCK, 1000, &gCanTransfer0 ) == 1 ) &&
|
||||
( CAN_Init( CAN1, BOARD_MCK, 1000, &gCanTransfer1 ) == 1 ) )
|
||||
{
|
||||
puts( "CAN initialization complete."STRING_EOL ) ;
|
||||
|
||||
/* Run tests */
|
||||
puts( "Press any key to start test"STRING_EOL ) ;
|
||||
UART_GetChar() ;
|
||||
_Test1() ;
|
||||
|
||||
puts( "Press any key to continue..."STRING_EOL ) ;
|
||||
UART_GetChar() ;
|
||||
_Test2() ;
|
||||
|
||||
puts( "Press any key to continue..."STRING_EOL ) ;
|
||||
UART_GetChar() ;
|
||||
_Test3() ;
|
||||
|
||||
puts( "Press any key to continue..."STRING_EOL ) ;
|
||||
UART_GetChar() ;
|
||||
_Test4() ;
|
||||
|
||||
/* Disable CAN0 Controller */
|
||||
CAN_Disable(CAN0) ;
|
||||
/* Disable CAN0 Transceiver */
|
||||
SN65HVD234_EnableLowPower( &gCanTransceiver0 ) ;
|
||||
SN65HVD234_Disable( &gCanTransceiver0 ) ;
|
||||
|
||||
/* Disable CAN1 Controller */
|
||||
CAN_Disable(CAN1) ;
|
||||
/* Disable CAN1 Transceiver */
|
||||
SN65HVD234_EnableLowPower( &gCanTransceiver1 ) ;
|
||||
SN65HVD234_Disable( &gCanTransceiver1 ) ;
|
||||
|
||||
puts( "End of all test"STRING_EOL ) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
puts( "ERROR CAN initialisation (synchro)"STRING_EOL ) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
89
hardware/arduino/sam/libraries/CAN/CANRaw.cpp
Normal file
89
hardware/arduino/sam/libraries/CAN/CANRaw.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
#include "CANRaw.h"
|
||||
#include "sn65hvd234.h"
|
||||
|
||||
CANRawClass::CANRawClass( Can* pCan )
|
||||
{
|
||||
m_pCan=pCan ;
|
||||
}
|
||||
|
||||
uint32_t CANRawClass::begin( uint32_t dwPinEN, uint32_t dwPinRs, uint32_t dwBaudrate )
|
||||
{
|
||||
uint32_t dwPins ;
|
||||
|
||||
/* Initialize CAN pins */
|
||||
if ( m_pCan == CAN0 )
|
||||
{
|
||||
dwPins=PINS_CAN0 ;
|
||||
}
|
||||
else
|
||||
{
|
||||
dwPins=PINS_CAN1 ;
|
||||
}
|
||||
|
||||
PIO_Configure(
|
||||
g_APinDescription[dwPins].pPort,
|
||||
g_APinDescription[dwPins].ulPinType,
|
||||
g_APinDescription[dwPins].ulPin,
|
||||
g_APinDescription[dwPins].ulPinConfiguration);
|
||||
|
||||
/* Initialize CAN Transceiver */
|
||||
SN65HVD234_Init( &m_Transceiver ) ;
|
||||
SN65HVD234_SetRs( &m_Transceiver, dwPinRs ) ;
|
||||
SN65HVD234_SetEN( &m_Transceiver, dwPinEN ) ;
|
||||
/* Enable CAN Transceiver */
|
||||
SN65HVD234_DisableLowPower( &m_Transceiver ) ;
|
||||
SN65HVD234_Enable( &m_Transceiver ) ;
|
||||
|
||||
if ( can_init( m_pCan, SystemCoreClock, 1000 ) == 1 )
|
||||
{
|
||||
can_reset_all_mailbox( m_pCan ) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
puts( "ERROR CAN initialisation (synchro)" ) ;
|
||||
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
uint32_t CANRawClass::end( void )
|
||||
{
|
||||
/* Disable CAN Controller */
|
||||
can_disable( m_pCan ) ;
|
||||
|
||||
/* Disable CAN Transceiver */
|
||||
SN65HVD234_EnableLowPower( &m_Transceiver ) ;
|
||||
SN65HVD234_Disable( &m_Transceiver ) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Exported objects
|
||||
*----------------------------------------------------------------------------*/
|
||||
CANRawClass CANRaw0( CAN0 ) ;
|
||||
CANRawClass CANRaw1( CAN1 ) ;
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Exported functions
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* \brief Default interrupt handler for CAN 0.
|
||||
*/
|
||||
void CAN0_IrqHandler( void )
|
||||
{
|
||||
CAN_Handler( CAN0, &gCanTransfer0 ) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Default interrupt handler for CAN 1.
|
||||
*/
|
||||
void CAN1_IrqHandler( void )
|
||||
{
|
||||
CAN_Handler( CAN1, &gCanTransfer1 ) ;
|
||||
}
|
||||
|
||||
|
32
hardware/arduino/sam/libraries/CAN/CANRaw.h
Normal file
32
hardware/arduino/sam/libraries/CAN/CANRaw.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef _CAN_LIBRARY_
|
||||
#define _CAN_LIBRARY_
|
||||
|
||||
#include "sn65hvd234.h"
|
||||
|
||||
class CANRawClass
|
||||
{
|
||||
protected:
|
||||
/* CAN peripheral, set by constructor */
|
||||
Can* m_pCan ;
|
||||
|
||||
/* CAN Transceiver */
|
||||
SSN65HVD234_Data m_Transceiver ;
|
||||
|
||||
/** CAN Transfer */
|
||||
can_mb_conf_t m_Mailbox ;
|
||||
|
||||
private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
CANRawClass( Can* pCan ) ;
|
||||
|
||||
uint32_t begin( uint32_t dwPinEN, uint32_t dwPinRs, uint32_t dwBaudrate ) ;
|
||||
uint32_t end( void ) ;
|
||||
|
||||
} ;
|
||||
|
||||
extern CANRawClass CANRaw0 ;
|
||||
extern CANRawClass CANRaw1 ;
|
||||
|
||||
#endif // _CAN_LIBRARY_
|
@ -4,9 +4,8 @@
|
||||
// Modified from Atmel ASF SAM3X-EK CAN sample by Wilfredo Molina 2012
|
||||
|
||||
// Required libraries
|
||||
#include <can.h>
|
||||
#include <sysclk.h>
|
||||
#include <sam3x8e.h>
|
||||
#include "variant.h"
|
||||
#include "CAN.h"
|
||||
|
||||
#define TEST1_CAN_COMM_MB_IDX 0
|
||||
#define TEST1_CAN_TRANSFER_ID 0x07
|
||||
@ -25,52 +24,41 @@ can_mb_conf_t can1_mailbox;
|
||||
|
||||
void setup()
|
||||
{
|
||||
// start serial port at 9600 bps:
|
||||
// start serial port at 9600 bps:
|
||||
Serial.begin(9600);
|
||||
Serial.println("Type CAN message to send");
|
||||
while (Serial.available() == 0);
|
||||
while (Serial.available() == 0);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
|
||||
|
||||
while (Serial.available() > 0) {
|
||||
CAN_MSG_1 = Serial.parseInt();
|
||||
if (Serial.read() == '\n') {
|
||||
if (Serial.read() == '\n') {
|
||||
Serial.print("Sent value= ");
|
||||
Serial.println(CAN_MSG_1);
|
||||
delay(1000);
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ul_sysclk;
|
||||
|
||||
/* Initialize the SAM system. */
|
||||
sysclk_init();
|
||||
board_init();
|
||||
|
||||
// Enable CAN0 & CAN1 clock
|
||||
|
||||
// Enable CAN0 & CAN1 clock
|
||||
pmc_enable_periph_clk(ID_CAN0);
|
||||
pmc_enable_periph_clk(ID_CAN1);
|
||||
|
||||
|
||||
// Initialize CAN0 and CAN1, baudrate is 1Mb/s
|
||||
ul_sysclk = sysclk_get_cpu_hz();
|
||||
can_init(CAN0, ul_sysclk, CAN_BPS_1000K);
|
||||
can_init(CAN1, ul_sysclk, CAN_BPS_1000K);
|
||||
|
||||
// Reset all CAN0 and CAN1 mailboxes
|
||||
can_reset_all_mailbox(CAN0);
|
||||
can_reset_all_mailbox(CAN1);
|
||||
|
||||
can_init(CAN0, SystemCoreClock, CAN_BPS_1000K);
|
||||
can_init(CAN1, SystemCoreClock, CAN_BPS_1000K);
|
||||
|
||||
// Initialize CAN1 mailbox 0 as receiver, frame ID is 0x07
|
||||
reset_mailbox_conf(&can1_mailbox);
|
||||
can_reset_mailbox_data(&can1_mailbox);
|
||||
can1_mailbox.ul_mb_idx = TEST1_CAN_COMM_MB_IDX;
|
||||
can1_mailbox.uc_obj_type = CAN_MB_RX_MODE;
|
||||
can1_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
|
||||
can1_mailbox.ul_id = CAN_MID_MIDvA(TEST1_CAN_TRANSFER_ID);
|
||||
can_mailbox_init(CAN1, &can1_mailbox);
|
||||
|
||||
|
||||
// Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15
|
||||
reset_mailbox_conf(&can0_mailbox);
|
||||
can_reset_mailbox_data(&can0_mailbox);
|
||||
can0_mailbox.ul_mb_idx = TEST1_CAN_COMM_MB_IDX;
|
||||
can0_mailbox.uc_obj_type = CAN_MB_TX_MODE;
|
||||
can0_mailbox.uc_tx_prio = TEST1_CAN0_TX_PRIO;
|
||||
@ -78,16 +66,16 @@ void loop(){
|
||||
can0_mailbox.ul_id_msk = 0;
|
||||
can_mailbox_init(CAN0, &can0_mailbox);
|
||||
|
||||
// Prepare transmit ID, data and data length in CAN0 mailbox 0
|
||||
// Prepare transmit ID, data and data length in CAN0 mailbox 0
|
||||
can0_mailbox.ul_id = CAN_MID_MIDvA(TEST1_CAN_TRANSFER_ID);
|
||||
can0_mailbox.ul_datal = CAN_MSG_1;
|
||||
can0_mailbox.ul_datah = CAN_MSG_DUMMY_DATA;
|
||||
can0_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN;
|
||||
can_mailbox_write(CAN0, &can0_mailbox);
|
||||
|
||||
|
||||
// Send out the information in the mailbox
|
||||
can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
|
||||
|
||||
|
||||
// Wait for CAN1 mailbox 0 to receive the data
|
||||
while (!(can_mailbox_get_status(CAN1, 0) & CAN_MSR_MRDY)) {
|
||||
}
|
||||
@ -97,4 +85,4 @@ void loop(){
|
||||
Serial.println(can1_mailbox.ul_datal);
|
||||
|
||||
while (1) {}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user