AnsweredAssumed Answered

Data seen on Oscilliscope from PB9

Question asked by wry.dragon on Jan 14, 2014
Ok Clive,
I see data coming out on PB9.
Should the code recognize this data if I connect back PB9 to PB8 ?
I tried this - but I do not see it entering the Interrupt func,
 but Loopback DOES generates Interrupt.

code:

 

#include "stm32f10x.h"
//#include "GLCD.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_can.h"
#include "misc.h"

#define  USB_HP_CAN1_TX_IRQn  19  /*!< USB Device High Priority or CAN1 TX Interrupts       */
#define  USB_LP_CAN1_RX0_IRQn 20  /*!< USB Device Low Priority or CAN1 RX0 Interrupts       */
#define  CAN1_RX1_IRQn   21  /*!< CAN1 RX1 Interrupt                                   */
#define  CAN1_TX_IRQn   19  /*!< USB Device High Priority or CAN1 TX Interrupts       */
#define  CAN1_RX0_IRQn   20  /*!< USB Device Low Priority or CAN1 RX0 Interrupts       */
#define  CAN1_RX1_IRQn   21   /*!< CAN1 RX1 Interrupt                                   */
#define  CAN1_SCE_IRQn   22  /*!< CAN1 SCE Interrupt                                   */

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
vu32 ret; /* for return of the interrupt handling */
volatile TestStatus TestRx;
ErrorStatus HSEStartUpStatus;

TestStatus CAN_Polling(void);
TestStatus CAN_Send_Interrupt(void);

int main(void)
{
long i;

 Init_All_Periph();

//  TestRx = FAILED;
//  while(TestRx == FAILED)
//  {

 /* CAN transmit at 100Kb/s and receive by polling in loopback mode*/
   TestRx = CAN_Polling();

   if (TestRx == FAILED)
   {
   GPIOC->ODR ^= (GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_14);
   }
   else
   {
   GPIOC->ODR ^= (GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_15);
   }

// for(i=0; i<1000; i++)
// {}
//  }

   /* CAN transmit at 500Kb/s and receive by interrupt in loopback mode*/
   TestRx = CAN_Send_Interrupt();

   if (TestRx == FAILED)
   {
   GPIOC->ODR ^= (GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_14);
   }
   else
   {
   GPIOC->ODR ^= (GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_15);
  }

  while(1)
   {
   // CAN transmit at 100Kb/s and receive by polling in loopback mode //
     TestRx = CAN_Polling();
     if (TestRx == FAILED)
     {
     GPIOC->ODR ^= (GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_14);
     }
     else
     {
     GPIOC->ODR ^= (GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_15);
     }

  // for(i=0; i<1000; i++)
  // {}
  //  }

     // CAN transmit at 500Kb/s and receive by interrupt in loopback mode //
     TestRx = CAN_Send_Interrupt();
     if (TestRx == FAILED)
     {
     GPIOC->ODR ^= (GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_14);
     }
     else
     {
     GPIOC->ODR ^= (GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_15);
    }

//   for(i=0; i<70000; i++)
    for(i=0; i<700000; i++)  /// - BLINKS once per Second
   {}

   GPIOC->ODR ^= (GPIO_Pin_8);
GPIOC->ODR ^= (GPIO_Pin_15);
   }

}

void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;

 /// LEDS : ///
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_15;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIOC->ODR |= (GPIO_Pin_15);

 // #define CANREMAP1_E  1
 // #ifdef CANREMAP1_E
 ///  Map1 CAN: RX=PB8,TX=PB9

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

 // Configure CAN pin: RX=PB8 //
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 // Configure CAN pin: TX=PB9 //
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 // #ifdef CANREMAP1_E
 // Remap1 is for PB8 and PB9
 GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE);
 // #endif

 // #endif // CANREMAP1_E

}

//
void NVIC_Configuration(void)
{
 NVIC_InitTypeDef NVIC_InitStructure;

   /* Configure the NVIC Preemption Priority Bits */
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

 #ifdef  VECT_TAB_RAM
   /* Set the Vector Table base location at 0x20000000 */
   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
 #else  /* VECT_TAB_FLASH  */
   /* Set the Vector Table base location at 0x08000000 */
   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
 #endif

 /* enabling interrupt */
   NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX0_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}

//
void RCC_Configuration(void)
{
 SystemInit();
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
                           |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
                           |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
         |RCC_APB2Periph_ADC1  | RCC_APB2Periph_AFIO
                           |RCC_APB2Periph_SPI1, ENABLE );
  //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4
                           |RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
                           , ENABLE );
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
 /* CAN Periph clock enable */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
}

void InitDis(void)
{
   /* LCD Module init */
//   GLCD_init();
//   GLCD_clear(White);
//   GLCD_setTextColor(Blue);
//   GLCD_displayStringLn(Line1, "     GoldBull");
//   GLCD_displayStringLn(Line2, "   CAN example");
//   GLCD_setTextColor(Red);
}

//ÅäÖÃËùÓÐÍâÉè
void Init_All_Periph(void)
{
 RCC_Configuration();
// InitDis();
// GLCD_Test();
 GPIO_Configuration();
 NVIC_Configuration();
}

/*******************************************************************************
* Function Name  : CAN_Polling
* Description    : Configures the CAN and transmit and receive by polling
* Input          : None
* Output         : None
* Return         : PASSED if the reception is well done, FAILED in other case
*******************************************************************************/
TestStatus CAN_Polling(void)
{
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  CanTxMsg TxMessage;
  CanRxMsg RxMessage;
  u32 i = 0;
  u8 TransmitMailbox;

  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE;
  CAN_InitStructure.CAN_ABOM = DISABLE;
  CAN_InitStructure.CAN_AWUM = DISABLE;
  CAN_InitStructure.CAN_NART = DISABLE;
  CAN_InitStructure.CAN_RFLM = DISABLE;
  CAN_InitStructure.CAN_TXFP = DISABLE;
//CAN_InitStructure.CAN_Mode   = CAN_Mode_LoopBack;
CAN_InitStructure.CAN_Mode   = CAN_Mode_Normal;
  CAN_InitStructure.CAN_SJW  = CAN_SJW_1tq;
  CAN_InitStructure.CAN_BS1  = CAN_BS1_8tq;
  CAN_InitStructure.CAN_BS2  = CAN_BS2_7tq;
  CAN_InitStructure.CAN_Prescaler = 5;
  CAN_Init(CAN1,&CAN_InitStructure);

  /* CAN filter init */
  CAN_FilterInitStructure.CAN_FilterNumber=0;
  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
  CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
  CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);

  /* transmit */
  TxMessage.StdId=0x11;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.IDE=CAN_ID_STD;
  TxMessage.DLC=2;
  TxMessage.Data[0]=0xCA;
  TxMessage.Data[1]=0xFE;

  TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
  i = 0;
  while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK) && (i != 0xFF))
  {
    i++;
  }

  i = 0;
  while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF))
  {
    i++;
  }

  /* receive */
  RxMessage.StdId=0x00;
  RxMessage.IDE=CAN_ID_STD;
  RxMessage.DLC=0;
  RxMessage.Data[0]=0x00;
  RxMessage.Data[1]=0x00;
  CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);

  if (RxMessage.StdId!=0x11)
   return FAILED;

  if (RxMessage.IDE!=CAN_ID_STD)
   return FAILED;

  if (RxMessage.DLC!=2)
   return FAILED;

  if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
   return FAILED;

  return PASSED; /* Test Passed */
}

/*******************************************************************************
* Function Name  : CAN_Send_Interrupt
* Description    : Configures the CAN
*      and transmit
*      and receive by interrupt
* Input          : None
* Output         : None
* Return         : PASSED if the reception is well done, FAILED in other case
*******************************************************************************/
TestStatus CAN_Send_Interrupt(void)
{
CAN_InitTypeDef         CAN_InitStructure;
CAN_FilterInitTypeDef   CAN_FilterInitStructure;
CanTxMsg     TxMessage;
u32      i = 0;

  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE;
  CAN_InitStructure.CAN_ABOM = DISABLE;
  CAN_InitStructure.CAN_AWUM = DISABLE;
  CAN_InitStructure.CAN_NART = DISABLE;
  CAN_InitStructure.CAN_RFLM = DISABLE;
  CAN_InitStructure.CAN_TXFP = DISABLE;
//CAN_InitStructure.CAN_Mode   = CAN_Mode_LoopBack;
CAN_InitStructure.CAN_Mode   = CAN_Mode_Normal;
  CAN_InitStructure.CAN_SJW  = CAN_SJW_1tq;
  CAN_InitStructure.CAN_BS1  = CAN_BS1_8tq;
  CAN_InitStructure.CAN_BS2  = CAN_BS2_7tq;
  CAN_InitStructure.CAN_Prescaler = 1;
  CAN_Init(CAN1,&CAN_InitStructure);

  /* CAN filter init */
  CAN_FilterInitStructure.CAN_FilterNumber  = 1;
  CAN_FilterInitStructure.CAN_FilterMode  = CAN_FilterMode_IdMask;
  CAN_FilterInitStructure.CAN_FilterScale  = CAN_FilterScale_32bit;
  CAN_FilterInitStructure.CAN_FilterIdHigh  = 0x0000;
  CAN_FilterInitStructure.CAN_FilterIdLow  = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);

  /* CAN FIFO0 message pending interrupt enable */
  CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);

  /* transmit 1 message */
  TxMessage.StdId=0x00;
  TxMessage.ExtId=0x1234;
  TxMessage.IDE=CAN_ID_EXT;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.DLC=2;
  TxMessage.Data[0]=0xDE;
  TxMessage.Data[1]=0xCA;
CAN_Transmit(CAN1,&TxMessage);

  /* initialize the value that will be returned */
  ret = 0xFF;

  /* receive message with interrupt handling */
  i=0;
  while((ret == 0xFF) && (i < 0xFFF))
  {
    i++;
  }

  if (i == 0xFFF)
  {
    ret=0;
  }

  /* disable interrupt handling */
  CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);

  return (TestStatus)ret;
}

/*******************************************************************************
* Function Name  : USB_LP_CAN_RX0_IRQHandler
* Description    : This function handles USB Low Priority or CAN RX0 interrupts
*                  requests.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
/// - will not RECEIVE anything unless CAN FILTER is ENABLED ! ///
//void CAN1_RX0_IRQHandler(void)
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;

  RxMessage.StdId=0x00;
  RxMessage.ExtId=0x00;
  RxMessage.IDE=0;
  RxMessage.DLC=0;
  RxMessage.FMI=0;
  RxMessage.Data[0]=0x00;
  RxMessage.Data[1]=0x00;

  CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);

  if(
    (RxMessage.ExtId==0x1234)
 && (RxMessage.IDE==CAN_ID_EXT)
 && (RxMessage.DLC==2)
    && ( (RxMessage.Data[1] | RxMessage.Data[0]<<8) == 0xDECA )
    )
  {
    ret = 1;
 GPIOC->ODR ^= (GPIO_Pin_8);
GPIOC->ODR ^= (GPIO_Pin_15);
  }
  else
  {
    ret = 0;
 GPIOC->ODR ^= (GPIO_Pin_9);
GPIOC->ODR ^= (GPIO_Pin_14);
  }
}

Outcomes