drv_can.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file drv_can.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __DRV_CAN_H__
  35. #define __DRV_CAN_H__
  36. #include <rtdevice.h>
  37. #include <rtthread.h>
  38. #include "n32g45x_can.h"
  39. #include "can.h"
  40. #define CAN_BAUDRATE_1M ((uint32_t)1000)
  41. #define CAN_BAUDRATE_500K ((uint32_t)500)
  42. #define CAN_BAUDRATE_250K ((uint32_t)250)
  43. #define CAN_BAUDRATE_125K ((uint32_t)125)
  44. #define CAN_BAUDRATE_100K ((uint32_t)100)
  45. #define CAN_BAUDRATE_50K ((uint32_t)50)
  46. #define CAN_BAUDRATE_20K ((uint32_t)20)
  47. #define CAN_BAUDRATE_10K ((uint32_t)10)
  48. #define CAN_BTR_CALCULATE ((uint32_t)6000)
  49. #define CAN_TX_MAILBOX0 (0x00000001U) /*!< Tx Mailbox 0 */
  50. #define CAN_TX_MAILBOX1 (0x00000002U) /*!< Tx Mailbox 1 */
  51. #define CAN_TX_MAILBOX2 (0x00000004U) /*!< Tx Mailbox 2 */
  52. #define CAN_FILTERNUM0 ((uint8_t)0)
  53. /* attention !!! baud calculation example: Tclk / ((ss + bs1 + bs2) * brp) 36 / ((1 + 8 + 3) * 3) = 1MHz*/
  54. /* Default config for serial_configure structure */
  55. #define RT_CAN_FILTER_CONFIG_DEFAULT \
  56. { \
  57. BAUD_RATE_115200, /* 115200 bits/s */ \
  58. DATA_BITS_8, /* 8 databits */ \
  59. STOP_BITS_1, /* 1 stopbit */ \
  60. PARITY_NONE, /* No parity */ \
  61. HFC_CONTROL_NONE, /* No Hardwareflow control */ \
  62. TX_RX_MODE, /* Tx_Rx mode */ \
  63. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  64. 0 \
  65. }
  66. struct n32g45x_baud_rate_tab
  67. {
  68. rt_uint32_t baud_rate;
  69. rt_uint32_t config_data;
  70. };
  71. /**
  72. * @brief CAN handle Structure definition
  73. */
  74. typedef struct
  75. {
  76. CAN_Module *Instance; /*!< Register base address */
  77. CAN_InitType Init; /*!< CAN required parameters */
  78. CanTxMessage* pTxMsg; /*!< Pointer to transmit structure */
  79. CanRxMessage* pRxMsg; /*!< Pointer to reception structure for RX FIFO0 msg */
  80. CanRxMessage* pRx1Msg; /*!< Pointer to reception structure for RX FIFO1 msg */
  81. uint32_t State; /*!< CAN communication state */
  82. FlagStatus Lock; /*!< CAN locking object */
  83. uint32_t ErrorCode; /*!< CAN Error code */
  84. }CAN_HandleTypeDef;
  85. /* n32g45x can device */
  86. struct n32g45x_can
  87. {
  88. char *name;
  89. CAN_HandleTypeDef CanHandle;
  90. CAN_FilterInitType FilterConfig;
  91. struct rt_can_device device; /* inherit from can device */
  92. };
  93. int rt_hw_can_init(void);
  94. #endif /* __DRV_CAN_H__ */