Ярлыки

_GetPixelIndex (1) _SetPixelIndex (1) 3-phase (1) 800x480 (1) АЦП (1) генератор (1) синхронный усилитель (2) структура (1) учебный курс (1) шаговый двигатель (1) ШИМ (2) accert (1) AD7608 (1) AD8429 (1) ADC (5) amplifer (1) arccos (1) arcsin (1) arctang (2) arctg (3) ARM (2) arm_sqrt_q15 (2) assembler (6) ASSERT (1) atan (2) bit (1) Bitband (1) boot (3) bootlloader (1) BUTTON (1) C (5) C# (1) CAN (2) CC2530 (5) CMSIS (4) command (1) Cordic (1) Core746I (1) CubeMX (4) DBGMCU (2) debug (2) debug.ini (1) delegate (1) Digital Potentiometers (1) DigitalPOT (1) Discovery (1) DMA (9) DMA2D (1) DSP (1) DSP library (1) DWT (1) EFM32 (5) EmWin (9) EXTI (1) FATFS (1) FMC (2) FreeRTOS (2) gl868-dual cmux (1) GPIO (4) GUI (2) GUIBuilder (1) GUIDRV_CompactColor_16 (1) HAL (3) HappyGecko (1) Hard Fault (2) heap (1) I2C (1) ID (1) ILI9320 (1) ILI9325 (1) Initialisation (1) InitLTDC (1) Instrumentithion (1) Interrupt (4) ITR (1) JTAG (1) Keil (5) LCDConf (2) lock-in (1) LTCD (1) LTDC (3) main (1) memory (1) MINI_STM32 Revision 01 (1) nBoot0 (1) NVIC (1) OnePulse (2) OSAL (4) pack (1) phase (1) printf (3) Pulse (1) PWM (12) RCC (2) RCR (1) Register (1) RESET (2) RS232 (3) RSS (1) RTC (3) RTOS-RTX (1) RTT (1) RTX-RTOS (1) SDCard (1) SDRAM (6) Segger (2) SPI (3) sqrt (3) SSD1298 (1) SSD1963 (1) Standart Peripherial Library (3) STANDBAY (1) startup (1) STemWin (8) stepper motor (1) STlink (2) STM32 (17) STM32429ZI (1) STM32Cube (1) STM32DBG.IN (1) STM32F (28) STM32F0 (4) STM32F1 (13) STM32F4 (10) STM32F4 Discovery (1) STM32F407ZG (1) STM32F429 (2) STM32F746 (1) STOP (1) string (1) struct (1) SWD (1) SWD JTAG (1) Synhronization (1) system_stm32f4xx.c (1) SystemInit (1) SysTick (1) task (4) telit (1) TIM (27) typedef (1) UART (1) USART (9) viewer (2) WM_PAINT (1) Z-stack (5) ZigBee (5)

суббота, 9 апреля 2016 г.

bxCAN STM32F042 transmit and receive.

Transmitter

#include "stm32f0xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;

I2C_HandleTypeDef hi2c1;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

unsigned int val_Tx = 0, val_Rx = 0;              /* Globals used for display */
volatile uint32_t msTicks;                        /* counts 1ms timeTicks     */




uint8_t xBuffer[3],rrr,www,send;
uint8_t UART_Buffer[3]={0x31,0x32,0x0D};
uint8_t DEVADDR;
uint32_t CAN_ReceiveMessage = 0;

typedef struct  {
  unsigned int   id;                    /* 29 bit identifier */
  unsigned char  data[8];               /* Data field */
  unsigned char  len;                   /* Length of data field in bytes */
  unsigned char  format;                /* 0 - STANDARD, 1- EXTENDED IDENTIFIER */
  unsigned char  type;                  /* 0 - DATA FRAME, 1 - REMOTE FRAME */
} CAN_msg;

CAN_msg       CAN_RxMsg;                      /* CAN message for receiving */                              
CAN_msg       CAN_TxMsg[2];                      /* CAN message for sending */


uint32_t      CAN_RxRdy = 0;              /* CAN HW received a message */
uint32_t      CAN_TxRdy = 0;              /* CAN HW transmit a message */

static uint32_t CAN_filterIdx[2] = {0,0};        /* static variable for the filter index */

uint32_t      CAN_msgId     = 0;

#define STANDARD_FORMAT  0
#define EXTENDED_FORMAT  1

#define DATA_FRAME       0
#define REMOTE_FRAME     1
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
static void MX_I2C1_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
//==============================================================================
void CAN_wrFilter (uint32_t ctrl, uint32_t id, uint8_t format)  {

  if (CAN_filterIdx[ctrl-1] > 13) {                 /* check if Filter Memory is full*/
    return;
  }
                                            /* Setup identifier information  */
  if (format == STANDARD_FORMAT)  {         /*   Standard ID                 */
      CAN_msgId |= (uint32_t)(id << 21) | CAN_ID_STD;
  }  else  {                                /*   Extended ID                 */
      CAN_msgId |= (uint32_t)(id <<  3) | CAN_ID_EXT;
  }

  CAN->FMR  |=   CAN_FMR_FINIT;            /* set initMode for filter banks */
  CAN->FA1R &=  ~(1UL << CAN_filterIdx[ctrl-1]);   /* deactivate filter             */

                                            /* initialize filter             */
  CAN->FS1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]);     /* set 32-bit scale configuration    */

/*! !* To disable the CAN Filters: Swap the comment on the next two lines and the two lines after the next line. */
CAN->FM1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]);   /* set to 32-bit Identifier List mode */
//CAN->FM1R |= 0x0;     /* DISABLES FILTERS  set to 32-bit Identifier Mask mode */

  CAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR1 = 0; //CAN_msgId; /*  32-bit identifier   */

/* !!*  To disable the CAN Filters: Swap the comment on the next two lines.    */
  CAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR2 = CAN_msgId; /*  32-bit identifier (33) for identifier mode  */
//CAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR2 = 0; /*  DISABLES FILTERS  32-bit MASK for mask mode */
 
 
  CAN->FFA1R &= ~(uint32_t)(1 << CAN_filterIdx[ctrl-1]);   /* assign filter to FIFO 0  */
  CAN->FA1R  |=  (uint32_t)(1 << CAN_filterIdx[ctrl-1]);   /* activate filter          */

  CAN->FMR &= ~CAN_FMR_FINIT;              /* reset initMode for filterBanks*/

CAN_filterIdx[ctrl-1]++;                       /* increase filter index  */
}

//===========================================================================================


void CAN_wrMsg (uint32_t ctrl, CAN_msg *msg)  {
//  CAN_TypeDef *pCAN = (ctrl == 1) ? CAN1 : CAN2;

  CAN->sTxMailBox[0].TIR  = (uint32_t)0; /* reset TXRQ bit */
                                          /* Setup identifier information */
  if (msg->format == STANDARD_FORMAT) {   /*    Standard ID                   */
    CAN->sTxMailBox[0].TIR |= (uint32_t)(msg->id << 21) | CAN_ID_STD;
  } else {                                /* Extended ID                      */
    CAN->sTxMailBox[0].TIR |= (uint32_t)(msg->id <<  3) | CAN_ID_EXT;
  }

                                          /* Setup type information           */
  if (msg->type == DATA_FRAME)  {         /* DATA FRAME                       */
    CAN->sTxMailBox[0].TIR |= CAN_RTR_DATA;
  } else {                                /* REMOTE FRAME                     */
    CAN->sTxMailBox[0].TIR |= CAN_RTR_REMOTE;
  }
                                          /* Setup data bytes                 */
  CAN->sTxMailBox[0].TDLR = (((uint32_t)msg->data[3] << 24) |
                              ((uint32_t)msg->data[2] << 16) |
                              ((uint32_t)msg->data[1] <<  8) |
                              ((uint32_t)msg->data[0])        );
  CAN->sTxMailBox[0].TDHR = (((uint32_t)msg->data[7] << 24) |
                              ((uint32_t)msg->data[6] << 16) |
                              ((uint32_t)msg->data[5] <<  8) |
                              ((uint32_t)msg->data[4])        );
                                          /* Setup length                     */
  CAN->sTxMailBox[0].TDTR &= ~CAN_TDT0R_DLC;
  CAN->sTxMailBox[0].TDTR |=  (msg->len & CAN_TDT0R_DLC);

  CAN->IER |= CAN_IER_TMEIE;                 /* enable  TME interrupt        */
  CAN->sTxMailBox[0].TIR |=  CAN_TI0R_TXRQ;  /* transmit message             */
}

//============================================================================================
void CAN_rdMsg (uint32_t ctrl, CAN_msg *msg)  {
                                              /* Read identifier information  */
  if ((CAN->sFIFOMailBox[0].RIR & CAN_ID_EXT) == 0) {
    msg->format = STANDARD_FORMAT;
    msg->id     = 0x000007FF & (CAN->sFIFOMailBox[0].RIR >> 21);
  } else {
    msg->format = EXTENDED_FORMAT;
    msg->id     = 0x1FFFFFFF & (CAN->sFIFOMailBox[0].RIR >> 3);
  }
                                              /* Read type information        */
  if ((CAN->sFIFOMailBox[0].RIR & CAN_RTR_REMOTE) == 0) {
    msg->type =   DATA_FRAME;
  } else {
    msg->type = REMOTE_FRAME;
  }
                                              /* Read number of rec. bytes    */
  msg->len     = (CAN->sFIFOMailBox[0].RDTR      ) & 0x0F;
                                              /* Read data bytes              */
  msg->data[0] = (CAN->sFIFOMailBox[0].RDLR      ) & 0xFF;
  msg->data[1] = (CAN->sFIFOMailBox[0].RDLR >>  8) & 0xFF;
  msg->data[2] = (CAN->sFIFOMailBox[0].RDLR >> 16) & 0xFF;
  msg->data[3] = (CAN->sFIFOMailBox[0].RDLR >> 24) & 0xFF;

  msg->data[4] = (CAN->sFIFOMailBox[0].RDHR      ) & 0xFF;
  msg->data[5] = (CAN->sFIFOMailBox[0].RDHR >>  8) & 0xFF;
  msg->data[6] = (CAN->sFIFOMailBox[0].RDHR >> 16) & 0xFF;
  msg->data[7] = (CAN->sFIFOMailBox[0].RDHR >> 24) & 0xFF;

  CAN->RF0R |= CAN_RF0R_RFOM0;             /* Release FIFO 0 output mailbox */
}
//=============================================================================
void read_CAN(void)
{
CAN_rdMsg (1, &CAN_RxMsg);
}

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

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

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

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN_Init();
  MX_I2C1_Init();

  /* USER CODE BEGIN 2 */
 CAN->IER |= CAN_IER_FMPIE0; /* enable FIFO 0 msg pending IRQ    */

 CAN->IER |= CAN_IER_TMEIE; /* enable FIFO 0 msg pending IRQ    */

 CAN_wrFilter (1, 199, STANDARD_FORMAT);    /* filter reception of msgs */                          
 CAN->MCR &= ~CAN_MCR_INRQ;             /* normal operating mode, reset INRQ*/
 while (CAN->MSR & CAN_MCR_INRQ); /* start CAN Controller  */                    

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); //on Power LED
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); //off CAN LED


  int i;
  //LED_Init ();                                    /* initialize the LEDs      */
  SystemCoreClockUpdate();                        /* Get Core Clock Frequency */

  SysTick_Config(SystemCoreClock /1000);         /* SysTick 1 msec irq       */

  CAN_TxMsg[1].id = 199;                           /* initialize msg to send   */
  for (i = 0; i < 8; i++) CAN_TxMsg[0].data[i] = 0;
  CAN_TxMsg[1].len = 1;
  CAN_TxMsg[1].format = STANDARD_FORMAT;
CAN_TxMsg[1].type = DATA_FRAME;

      CAN_TxRdy = 1;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

//    if (CAN_RxRdy == 1)   /* rx msg on CAN Ctrl */
// {                        
// CAN_RxRdy = 0; //clear CAM flag
// xBuffer[1] = 0; //write command
// xBuffer[0] = 0x12; //data for write command
//// xBuffer[0] = CAN_ReceiveMessage;
// DEVADDR=0x5e; //asdress of digital potentiometr
// www=HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) DEVADDR, xBuffer, 2, 10);  //write to address  (2-number of bytes)
//        HAL_Delay(1); //memory write delay
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2); //toggle CAN LED
// }


    val_Tx = val_Tx + 1;
    if (CAN_TxRdy) {                           /* tx msg on CAN Ctrl #2    */
      CAN_TxRdy = 0;
      CAN_TxMsg[1].data[0] = val_Tx;              /* data[0] = ADC value      */
for (i = 1; i < 8; i++) CAN_TxMsg[1].data[i] = val_Tx;
      CAN_wrMsg (2, &CAN_TxMsg[1]);               /* transmit message         */

// void CAN_wrMsg (uint32_t ctrl, CAN_msg *msg)  {
//  CAN_TypeDef *pCAN = (ctrl == 1) ? CAN1 : CAN2;

  CAN->sTxMailBox[0].TIR  = (uint32_t)0; /* reset TXRQ bit */
                                          /* Setup identifier information */
  if (CAN_TxMsg->format == STANDARD_FORMAT) {   /*    Standard ID                   */
    CAN->sTxMailBox[0].TIR |= (uint32_t)(CAN_TxMsg->id << 21) | CAN_ID_STD;
  } else {                                /* Extended ID                      */
    CAN->sTxMailBox[0].TIR |= (uint32_t)(CAN_TxMsg->id <<  3) | CAN_ID_EXT;
  }

                                          /* Setup type information           */
  if (CAN_TxMsg->type == DATA_FRAME)  {         /* DATA FRAME                       */
    CAN->sTxMailBox[0].TIR |= CAN_RTR_DATA;
  } else {                                /* REMOTE FRAME                     */
    CAN->sTxMailBox[0].TIR |= CAN_RTR_REMOTE;
  }
                                          /* Setup data bytes                 */
  CAN->sTxMailBox[0].TDLR = (((uint32_t)CAN_TxMsg->data[3] << 24) |
                              ((uint32_t)CAN_TxMsg->data[2] << 16) |
                              ((uint32_t)CAN_TxMsg->data[1] <<  8) |
                              ((uint32_t)CAN_TxMsg->data[0])        );
  CAN->sTxMailBox[0].TDHR = (((uint32_t)CAN_TxMsg->data[7] << 24) |
                              ((uint32_t)CAN_TxMsg->data[6] << 16) |
                              ((uint32_t)CAN_TxMsg->data[5] <<  8) |
                              ((uint32_t)CAN_TxMsg->data[4])        );
                                          /* Setup length                     */
  CAN->sTxMailBox[0].TDTR &= ~CAN_TDT0R_DLC;
  CAN->sTxMailBox[0].TDTR |=  (CAN_TxMsg->len & CAN_TDT0R_DLC);

  CAN->IER |= CAN_IER_TMEIE;                 /* enable  TME interrupt        */
  CAN->sTxMailBox[0].TIR |=  CAN_TI0R_TXRQ;  /* transmit message             */
//}


//HAL_CAN_Transmit_IT(&hcan2);

    }
    HAL_Delay (10);                                   /* delay for 10ms           */
    if (CAN_RxRdy) {                           /* rx msg on CAN Ctrl #1    */
      CAN_RxRdy = 0;
      val_Rx = CAN_RxMsg.data[0];
    }
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2); //toggle CAN LED

    HAL_Delay (200);                                  /* delay for 500ms          */



  }
  /* USER CODE END 3 */

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
  ******************************************************************************
  * @file    stm32f0xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "stm32f0xx.h"
#include "stm32f0xx_it.h"

/* USER CODE BEGIN 0 */
extern void read_CAN (void);
extern uint32_t CAN_ReceiveMessage;
extern uint32_t CAN_RxRdy;
extern uint32_t CAN_TxRdy;
/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern CAN_HandleTypeDef hcan;
extern I2C_HandleTypeDef hi2c1;

/******************************************************************************/
/*            Cortex-M0 Processor Interruption and Exception Handlers         */ 
/******************************************************************************/

/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
  }
  /* USER CODE BEGIN HardFault_IRQn 1 */

  /* USER CODE END HardFault_IRQn 1 */
}

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F0xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f0xx.s).                    */
/******************************************************************************/

/**
* @brief This function handles I2C1 event global interrupt / I2C1 wake-up interrupt through EXTI line 23.
*/
void I2C1_IRQHandler(void)
{
  /* USER CODE BEGIN I2C1_IRQn 0 */

  /* USER CODE END I2C1_IRQn 0 */
  if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {
    HAL_I2C_ER_IRQHandler(&hi2c1);
  } else {
    HAL_I2C_EV_IRQHandler(&hi2c1);
  }
  /* USER CODE BEGIN I2C1_IRQn 1 */

  /* USER CODE END I2C1_IRQn 1 */
}

/**
* @brief This function handles HDMI-CEC and CAN global interrupts / HDMI-CEC wake-up interrupt through EXTI line 27.
*/
void CEC_CAN_IRQHandler(void)
{
  /* USER CODE BEGIN CEC_CAN_IRQn 0 */
  if (CAN->RF0R & CAN_RF0R_FMP0) {     /* message pending ?              */
 read_CAN ();           /* read the message               */
    CAN_RxRdy = 1;                       /*  set receive flag              */
CAN_ReceiveMessage = CAN->sFIFOMailBox[0].RDLR; /* read data */
    CAN->RF0R |= CAN_RF0R_RFOM0; /* release FIFO */
  }
  if (CAN->TSR & CAN_TSR_RQCP0) {          /* request completed mbx 0        */
    CAN->TSR |= CAN_TSR_RQCP0;             /* reset request complete mbx 0   */
    CAN->IER &= ~CAN_IER_TMEIE;            /* disable  TME interrupt         */

 CAN_TxRdy = 1; 
  }

  /* USER CODE END CEC_CAN_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN CEC_CAN_IRQn 1 */

  /* USER CODE END CEC_CAN_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



Receiver

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;

I2C_HandleTypeDef hi2c1;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t xBuffer[3],rrr,www1,www2,send;
uint8_t UART_Buffer[3]={0x31,0x32,0x0D};
uint8_t DEVADDR;

uint32_t CAN_ReceiveMessage1 = 0;
uint32_t CAN_ReceiveMessage2 = 0;

typedef struct  {
  unsigned int   id;                    /* 29 bit identifier */
  unsigned char  data[8];               /* Data field */
  unsigned char  len;                   /* Length of data field in bytes */
  unsigned char  format;                /* 0 - STANDARD, 1- EXTENDED IDENTIFIER */
  unsigned char  type;                  /* 0 - DATA FRAME, 1 - REMOTE FRAME */
} CAN_msg;

CAN_msg       CAN_RxMsg;                      /* CAN message for receiving */                                

uint32_t      CAN_RxRdy = 0;              /* CAN HW received a message */

static uint32_t CAN_filterIdx[2] = {0,0};        /* static variable for the filter index */

uint32_t      CAN_msgId     = 0;

#define STANDARD_FORMAT  0
#define EXTENDED_FORMAT  1

#define DATA_FRAME       0
#define REMOTE_FRAME     1
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
static void MX_I2C1_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
//==============================================================================
void CAN_wrFilter (uint32_t ctrl, uint32_t id, uint8_t format)  {
  
  if (CAN_filterIdx[ctrl-1] > 13) {                 /* check if Filter Memory is full*/
    return;
  }
                                            /* Setup identifier information  */
  if (format == STANDARD_FORMAT)  {         /*   Standard ID                 */
      CAN_msgId |= (uint32_t)(id << 21) | CAN_ID_STD;
  }  else  {                                /*   Extended ID                 */
      CAN_msgId |= (uint32_t)(id <<  3) | CAN_ID_EXT;
  }

  CAN->FMR  |=   CAN_FMR_FINIT;            /* set initMode for filter banks */
  CAN->FA1R &=  ~(1UL << CAN_filterIdx[ctrl-1]);   /* deactivate filter             */

                                            /* initialize filter             */
  CAN->FS1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]);     /* set 32-bit scale configuration    */
/*! !* To disable the CAN Filters: Swap the comment on the next two lines and the two lines after the next line. */
CAN->FM1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]);   /* set to 32-bit Identifier List mode */
//CAN->FM1R |= 0x0;     /* DISABLES FILTERS  set to 32-bit Identifier Mask mode */
  CAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR1 = 0; //CAN_msgId; /*  32-bit identifier   */
/* !!*  To disable the CAN Filters: Swap the comment on the next two lines.    */
  CAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR2 = CAN_msgId; /*  32-bit identifier (33) for identifier mode  */
//CAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR2 = 0; /*  DISABLES FILTERS  32-bit MASK for mask mode */
   
   
  CAN->FFA1R &= ~(uint32_t)(1 << CAN_filterIdx[ctrl-1]);   /* assign filter to FIFO 0  */
  CAN->FA1R  |=  (uint32_t)(1 << CAN_filterIdx[ctrl-1]);   /* activate filter          */
  CAN->FMR &= ~CAN_FMR_FINIT;              /* reset initMode for filterBanks*/
CAN_filterIdx[ctrl-1]++;                       /* increase filter index  */
}

//===========================================================================================
void CAN_rdMsg (uint32_t ctrl, CAN_msg *msg)  {
                                              /* Read identifier information  */
  if ((CAN->sFIFOMailBox[0].RIR & CAN_ID_EXT) == 0) {
    msg->format = STANDARD_FORMAT;
    msg->id     = 0x000007FF & (CAN->sFIFOMailBox[0].RIR >> 21);
  } else {
    msg->format = EXTENDED_FORMAT;
    msg->id     = 0x1FFFFFFF & (CAN->sFIFOMailBox[0].RIR >> 3);
  }
                                              /* Read type information        */
  if ((CAN->sFIFOMailBox[0].RIR & CAN_RTR_REMOTE) == 0) {
    msg->type =   DATA_FRAME;
  } else {
    msg->type = REMOTE_FRAME;
  }
                                              /* Read number of rec. bytes    */
  msg->len     = (CAN->sFIFOMailBox[0].RDTR      ) & 0x0F;
                                              /* Read data bytes              */
  msg->data[0] = (CAN->sFIFOMailBox[0].RDLR      ) & 0xFF;
  msg->data[1] = (CAN->sFIFOMailBox[0].RDLR >>  8) & 0xFF;
  msg->data[2] = (CAN->sFIFOMailBox[0].RDLR >> 16) & 0xFF;
  msg->data[3] = (CAN->sFIFOMailBox[0].RDLR >> 24) & 0xFF;

  msg->data[4] = (CAN->sFIFOMailBox[0].RDHR      ) & 0xFF;
  msg->data[5] = (CAN->sFIFOMailBox[0].RDHR >>  8) & 0xFF;
  msg->data[6] = (CAN->sFIFOMailBox[0].RDHR >> 16) & 0xFF;
  msg->data[7] = (CAN->sFIFOMailBox[0].RDHR >> 24) & 0xFF;

  CAN->RF0R |= CAN_RF0R_RFOM0;             /* Release FIFO 0 output mailbox */
}
//=============================================================================
void read_CAN(void)
{
CAN_rdMsg (1, &CAN_RxMsg);
}

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */

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

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

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN_Init();
  MX_I2C1_Init();

  /* USER CODE BEGIN 2 */
 CAN->IER |= CAN_IER_FMPIE0; /* enable FIFO 0 msg pending IRQ    */ 
 CAN_wrFilter (1, 199, STANDARD_FORMAT);    /* filter reception of msgs */                            
 CAN->MCR &= ~CAN_MCR_INRQ;             /* normal operating mode, reset INRQ*/
 while (CAN->MSR & CAN_MCR_INRQ); /* start CAN Controller  */                       

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); //on Power LED
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET); //off CAN LED

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
    if (CAN_RxRdy == 1)   // rx msg on CAN Ctrl 
{                          
CAN_RxRdy = 0; //clear CAM flag

xBuffer[0] = 0x0; //write byte to potentiometr 0
xBuffer[1] = CAN_ReceiveMessage1;
DEVADDR=0x50; //asdress of digital potentiometr
www1=HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) DEVADDR, xBuffer, 2, 10);   //write to address  (2-number of bytes)      
//!!!       www1 = 0 if Transmit OK
xBuffer[0] = 0x10; //write byte to potentiometr 1
xBuffer[1] = CAN_ReceiveMessage2;
DEVADDR=0x50; //asdress of digital potentiometr
www2=HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) DEVADDR, xBuffer, 2, 10);   //write to address  (2-number of bytes)
//!!!        www2 = 0 if Transmit OK
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_3); //toggle CAN LED
}
//==========================================================================================================================
// TEST for digi pot
//==========================================================================================================================
//for (rrr=0;rrr<=255;rrr++)
//{
// xBuffer[0] = 0x0; //write byte to potentiometr 0
// xBuffer[1] = rrr;
// DEVADDR=0x5e; //asdress of digital potentiometr
// www1=HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) DEVADDR, xBuffer, 2, 10);  //write to address  (2-number of bytes) //memory write delay
// xBuffer[0] = 0x10; //write byte to potentiometr 1
// xBuffer[1] = rrr;
// DEVADDR=0x5e; //asdress of digital potentiometr
// www2=HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) DEVADDR, xBuffer, 2, 10);  //write to address  (2-number of bytes)
//HAL_Delay(200);  //delay for debug
//}
//===========================================================================================================================
  }
  /* USER CODE END 3 */

}


/**
  ******************************************************************************
  * @file    stm32f0xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "stm32f0xx.h"
#include "stm32f0xx_it.h"

/* USER CODE BEGIN 0 */
extern void read_CAN (void);
extern uint32_t CAN_ReceiveMessage1, CAN_ReceiveMessage2;
extern uint32_t CAN_RxRdy;
/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern CAN_HandleTypeDef hcan;
extern I2C_HandleTypeDef hi2c1;

/******************************************************************************/
/*            Cortex-M0 Processor Interruption and Exception Handlers         */ 
/******************************************************************************/

/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
  }
  /* USER CODE BEGIN HardFault_IRQn 1 */

  /* USER CODE END HardFault_IRQn 1 */
}

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F0xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f0xx.s).                    */
/******************************************************************************/

/**
* @brief This function handles I2C1 event global interrupt / I2C1 wake-up interrupt through EXTI line 23.
*/
void I2C1_IRQHandler(void)
{
  /* USER CODE BEGIN I2C1_IRQn 0 */

  /* USER CODE END I2C1_IRQn 0 */
  if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {
    HAL_I2C_ER_IRQHandler(&hi2c1);
  } else {
    HAL_I2C_EV_IRQHandler(&hi2c1);
  }
  /* USER CODE BEGIN I2C1_IRQn 1 */

  /* USER CODE END I2C1_IRQn 1 */
}

/**
* @brief This function handles HDMI-CEC and CAN global interrupts / HDMI-CEC wake-up interrupt through EXTI line 27.
*/
void CEC_CAN_IRQHandler(void)
{
  /* USER CODE BEGIN CEC_CAN_IRQn 0 */
  if (CAN->RF0R & CAN_RF0R_FMP0) {     /* message pending ?              */
 read_CAN ();           /* read the message               */
    CAN_RxRdy = 1;                       /*  set receive flag              */
CAN_ReceiveMessage1 = CAN->sFIFOMailBox[0].RDLR; /* read data */
CAN_ReceiveMessage2 = CAN->sFIFOMailBox[0].RDLR >> 8; /* read data */
    CAN->RF0R |= CAN_RF0R_RFOM0; /* release FIFO */
  }

  /* USER CODE END CEC_CAN_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN CEC_CAN_IRQn 1 */

  /* USER CODE END CEC_CAN_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


Комментариев нет:

Отправить комментарий