drv_can.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #define CAN_BAUDRATE_1M ((uint32_t)1000)
  40. #define CAN_BAUDRATE_500K ((uint32_t)500)
  41. #define CAN_BAUDRATE_250K ((uint32_t)250)
  42. #define CAN_BAUDRATE_125K ((uint32_t)125)
  43. #define CAN_BAUDRATE_100K ((uint32_t)100)
  44. #define CAN_BAUDRATE_50K ((uint32_t)50)
  45. #define CAN_BAUDRATE_20K ((uint32_t)20)
  46. #define CAN_BAUDRATE_10K ((uint32_t)10)
  47. #define CAN_BTR_CALCULATE ((uint32_t)6000)
  48. #define CAN_TX_MAILBOX0 (0x00000001U) /*!< Tx Mailbox 0 */
  49. #define CAN_TX_MAILBOX1 (0x00000002U) /*!< Tx Mailbox 1 */
  50. #define CAN_TX_MAILBOX2 (0x00000004U) /*!< Tx Mailbox 2 */
  51. #define CAN_FILTERNUM0 ((uint8_t)0)
  52. /* attention !!! baud calculation example: Tclk / ((ss + bs1 + bs2) * brp) 36 / ((1 + 8 + 3) * 3) = 1MHz*/
  53. /* Default config for serial_configure structure */
  54. #define RT_CAN_FILTER_CONFIG_DEFAULT \
  55. { \
  56. BAUD_RATE_115200, /* 115200 bits/s */ \
  57. DATA_BITS_8, /* 8 databits */ \
  58. STOP_BITS_1, /* 1 stopbit */ \
  59. PARITY_NONE, /* No parity */ \
  60. HFC_CONTROL_NONE, /* No Hardwareflow control */ \
  61. TX_RX_MODE, /* Tx_Rx mode */ \
  62. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  63. 0 \
  64. }
  65. struct n32_baud_rate_tab
  66. {
  67. rt_uint32_t baud_rate;
  68. rt_uint32_t config_data;
  69. };
  70. /**
  71. * @brief CAN handle Structure definition
  72. */
  73. typedef struct
  74. {
  75. CAN_Module *Instance; /*!< Register base address */
  76. CAN_InitType Init; /*!< CAN required parameters */
  77. CanTxMessage* pTxMsg; /*!< Pointer to transmit structure */
  78. CanRxMessage* pRxMsg; /*!< Pointer to reception structure for RX FIFO0 msg */
  79. CanRxMessage* pRx1Msg; /*!< Pointer to reception structure for RX FIFO1 msg */
  80. uint32_t State; /*!< CAN communication state */
  81. FlagStatus Lock; /*!< CAN locking object */
  82. uint32_t ErrorCode; /*!< CAN Error code */
  83. }CAN_HandleTypeDef;
  84. /* n32 can device */
  85. struct n32_can
  86. {
  87. char *name;
  88. CAN_HandleTypeDef CanHandle;
  89. CAN_FilterInitType FilterConfig;
  90. struct rt_can_device device; /* inherit from can device */
  91. };
  92. int rt_hw_can_init(void);
  93. #endif /* __DRV_CAN_H__ */