| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802 |
- /*
- * Copyright (c) 2006-2024, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2024-08-30 shelton first version
- * 2024-12-18 shelton add support f455/f456 and f457
- */
- #include "drv_can_v2.h"
- #include "drv_config.h"
- #ifdef BSP_USING_CAN_V2
- #define LOG_TAG "drv_can"
- #include <drv_log.h>
- #ifdef RT_CAN_USING_CANFD
- static const uint8_t dlc_to_bytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
- #endif
- #ifdef SOC_SERIES_AT32M412
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 180 / ((45 + 135) * 1) = 1MHz*/
- /* attention !!! default apbclk 180 mhz */
- static const struct at32_baud_rate can_baud_rate_tab[] =
- {
- {CAN1MBaud, 1 , 45, 135, 45},
- {CAN800kBaud, 1 , 45, 180, 45},
- {CAN500kBaud, 1 , 90, 270, 90},
- {CAN250kBaud, 2 , 90, 270, 90},
- {CAN125kBaud, 3 , 120, 360, 120},
- {CAN100kBaud, 5 , 90, 270, 90},
- {CAN50kBaud, 9 , 100, 300, 100},
- {CAN20kBaud, 18, 125, 375, 125},
- {CAN10kBaud, 30, 120, 480, 120},
- };
- #endif
- #ifdef SOC_SERIES_AT32M416
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 180 / ((45 + 135) * 1) = 1MHz*/
- /* attention !!! default apbclk 180 mhz */
- static const struct at32_baud_rate can_baud_rate_tab[] =
- {
- {CAN1MBaud, 1 , 45, 135, 45},
- {CAN800kBaud, 1 , 45, 180, 45},
- {CAN500kBaud, 1 , 90, 270, 90},
- {CAN250kBaud, 2 , 90, 270, 90},
- {CAN125kBaud, 3 , 120, 360, 120},
- {CAN100kBaud, 5 , 90, 270, 90},
- {CAN50kBaud, 9 , 100, 300, 100},
- {CAN20kBaud, 18, 125, 375, 125},
- {CAN10kBaud, 30, 120, 480, 120},
- };
- #ifdef RT_CAN_USING_CANFD
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 180 / ((45 + 135) * 1) = 1MHz*/
- /* attention !!! default apbclk 180 mhz, ssoffset default value is 'seg1 + 1', baud_rate_fd.div must equal to baud_rate.div */
- static const struct at32_baud_rate_fd canfd_baud_rate_tab[] =
- {
- {CANFD_DATA_1MBaud, 1 , 45, 135, 45, 136},
- {CANFD_DATA_2MBaud, 1 , 18, 72, 18, 73},
- {CANFD_DATA_3MBaud, 1 , 15, 45, 15, 46},
- {CANFD_DATA_4MBaud, 1 , 9, 36, 9, 37},
- {CANFD_DATA_5MBaud, 1 , 9, 27, 9, 28},
- {CANFD_DATA_6MBaud, 1 , 6, 24, 6, 25},
- };
- #endif
- #endif
- #ifdef SOC_SERIES_AT32F455
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
- /* attention !!! default apbclk 192 mhz */
- static const struct at32_baud_rate can_baud_rate_tab[] =
- {
- {CAN1MBaud, 1 , 48, 144, 48},
- {CAN800kBaud, 1 , 60, 180, 60},
- {CAN500kBaud, 1 , 96, 288, 96},
- {CAN250kBaud, 2 , 96, 288, 96},
- {CAN125kBaud, 3 , 128, 384, 128},
- {CAN100kBaud, 4 , 120, 260, 120},
- {CAN50kBaud, 8, 120, 260, 120},
- {CAN20kBaud, 16, 150, 450, 150},
- {CAN10kBaud, 24, 200, 600, 200},
- };
- #endif
- #ifdef SOC_SERIES_AT32F456
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
- /* attention !!! default apbclk 192 mhz */
- static const struct at32_baud_rate can_baud_rate_tab[] =
- {
- {CAN1MBaud, 1 , 48, 144, 48},
- {CAN800kBaud, 1 , 60, 180, 60},
- {CAN500kBaud, 1 , 96, 288, 96},
- {CAN250kBaud, 2 , 96, 288, 96},
- {CAN125kBaud, 3 , 128, 384, 128},
- {CAN100kBaud, 4 , 120, 260, 120},
- {CAN50kBaud, 8, 120, 260, 120},
- {CAN20kBaud, 16, 150, 450, 150},
- {CAN10kBaud, 24, 200, 600, 200},
- };
- #ifdef RT_CAN_USING_CANFD
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
- /* attention !!! default apbclk 192 mhz, ssoffset default value is 'seg1 + 1', baud_rate_fd.div must equal to baud_rate.div */
- static const struct at32_baud_rate_fd canfd_baud_rate_tab[] =
- {
- {CANFD_DATA_1MBaud, 1 , 48, 144, 48, 145},
- {CANFD_DATA_2MBaud, 1 , 24, 72, 24, 25},
- {CANFD_DATA_3MBaud, 1 , 16, 48, 16, 49},
- {CANFD_DATA_4MBaud, 1 , 12, 36, 12, 37},
- {CANFD_DATA_6MBaud, 1 , 12, 24, 12, 25},
- };
- #endif
- #endif
- #ifdef SOC_SERIES_AT32F457
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
- /* attention !!! default apbclk 192 mhz */
- static const struct at32_baud_rate can_baud_rate_tab[] =
- {
- {CAN1MBaud, 1 , 48, 144, 48},
- {CAN800kBaud, 1 , 60, 180, 60},
- {CAN500kBaud, 1 , 96, 288, 96},
- {CAN250kBaud, 2 , 96, 288, 96},
- {CAN125kBaud, 3 , 128, 384, 128},
- {CAN100kBaud, 4 , 120, 260, 120},
- {CAN50kBaud, 8, 120, 260, 120},
- {CAN20kBaud, 16, 150, 450, 150},
- {CAN10kBaud, 24, 200, 600, 200},
- };
- #ifdef RT_CAN_USING_CANFD
- /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
- /* attention !!! default apbclk 192 mhz, ssoffset default value is 'seg1 + 1', baud_rate_fd.div must equal to baud_rate.div */
- static const struct at32_baud_rate_fd canfd_baud_rate_tab[] =
- {
- {CANFD_DATA_1MBaud, 1 , 48, 144, 48, 145},
- {CANFD_DATA_2MBaud, 1 , 24, 72, 24, 25},
- {CANFD_DATA_3MBaud, 1 , 16, 48, 16, 49},
- {CANFD_DATA_4MBaud, 1 , 12, 36, 12, 37},
- {CANFD_DATA_6MBaud, 1 , 12, 24, 12, 25},
- };
- #endif
- #endif
- enum {
- #ifdef BSP_USING_CAN1
- CAN1_INDEX,
- #endif
- #ifdef BSP_USING_CAN2
- CAN2_INDEX,
- #endif
- #ifdef BSP_USING_CAN3
- CAN3_INDEX,
- #endif
- };
- static struct at32_can can_config[] = {
- #ifdef BSP_USING_CAN1
- CAN1_CONFIG,
- #endif
- #ifdef BSP_USING_CAN2
- CAN2_CONFIG,
- #endif
- #ifdef BSP_USING_CAN3
- CAN3_CONFIG,
- #endif
- };
- static rt_uint32_t get_can_baud_index(rt_uint32_t baud)
- {
- rt_uint32_t len, index;
- len = sizeof(can_baud_rate_tab) / sizeof(can_baud_rate_tab[0]);
- for (index = 0; index < len; index++)
- {
- if (can_baud_rate_tab[index].baud_rate == baud)
- return index;
- }
- /* default baud is CAN1MBaud */
- return 0;
- }
- #ifdef RT_CAN_USING_CANFD
- static rt_uint32_t get_canfd_baud_index(rt_uint32_t baud)
- {
- rt_uint32_t len, index;
- len = sizeof(canfd_baud_rate_tab) / sizeof(canfd_baud_rate_tab[0]);
- for (index = 0; index < len; index++)
- {
- if (canfd_baud_rate_tab[index].baud_rate == baud)
- return index;
- }
- /* default baud is CAN1MBaud */
- return 0;
- }
- #endif
- static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg)
- {
- rt_uint32_t baud_index;
- can_mode_type can_mode;
- can_transmit_status_type statues;
- RT_ASSERT(can);
- RT_ASSERT(cfg);
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- RT_ASSERT(can_instance);
- at32_msp_can_init((void *)can_instance->can_x);
- /* config can baudrate */
- do {
- can_transmit_status_get(can_instance->can_x, &statues);
- }
- while((statues.current_tstat != CAN_TSTAT_IDLE) || (can_stb_status_get(can_instance->can_x) != CAN_STB_STATUS_EMPTY) || \
- (can_rxbuf_status_get(can_instance->can_x) != CAN_RXBUF_STATUS_EMPTY));
- can_software_reset(can_instance->can_x, TRUE);
- #ifdef RT_CAN_USING_CANFD
- can_instance->enable_canfd = cfg->enable_canfd;
- if (cfg->use_bit_timing != 0U)
- {
- can_instance->can_x->lbtcfg_bit.presc = cfg->can_timing.prescaler - 1;
- can_instance->can_x->actime_bit.ac_seg_1 = cfg->can_timing.num_seg1 - 2;
- can_instance->can_x->actime_bit.ac_seg_2 = cfg->can_timing.num_seg2 - 1;
- can_instance->can_x->actime_bit.ac_sjw = cfg->can_timing.num_sjw - 1;
- can_instance->can_x->fdtime_bit.fd_seg_1 = cfg->canfd_timing.num_seg1 - 2;
- can_instance->can_x->fdtime_bit.fd_seg_2 = cfg->canfd_timing.num_seg2 - 1;
- can_instance->can_x->fdtime_bit.fd_sjw = cfg->canfd_timing.num_sjw - 1;
- can_instance->can_x->lbtcfg_bit.fd_sspoff = cfg->canfd_timing.num_sspoff;
- }
- else if(cfg->baud_rate_fd != 0U)
- {
- baud_index = get_canfd_baud_index(cfg->baud_rate_fd);
- /* get baudrate parameters */
- can_instance->can_x->fdtime_bit.fd_seg_1 = canfd_baud_rate_tab[baud_index].div - 1;
- can_instance->can_x->fdtime_bit.fd_seg_1 = canfd_baud_rate_tab[baud_index].bts1_size - 2;
- can_instance->can_x->fdtime_bit.fd_seg_2 = canfd_baud_rate_tab[baud_index].bts2_size - 1;
- can_instance->can_x->fdtime_bit.fd_sjw = canfd_baud_rate_tab[baud_index].rsaw_size - 1;
- can_instance->can_x->lbtcfg_bit.fd_sspoff = canfd_baud_rate_tab[baud_index].ssoffset;
- }
- else
- #endif
- {
- baud_index = get_can_baud_index(cfg->baud_rate);
- /* get baudrate parameters */
- can_instance->can_x->lbtcfg_bit.presc = can_baud_rate_tab[baud_index].div - 1;
- can_instance->can_x->actime_bit.ac_seg_1 = can_baud_rate_tab[baud_index].bts1_size - 2;
- can_instance->can_x->actime_bit.ac_seg_2 = can_baud_rate_tab[baud_index].bts2_size - 1;
- can_instance->can_x->actime_bit.ac_sjw = can_baud_rate_tab[baud_index].rsaw_size - 1;
- }
- can_stb_transmit_mode_set(can_instance->can_x, CAN_STB_TRANSMIT_BY_FIFO);
- can_software_reset(can_instance->can_x, FALSE);
- /* config can base parameters */
- switch (cfg->mode)
- {
- case RT_CAN_MODE_NORMAL:
- can_mode = CAN_MODE_COMMUNICATE;
- break;
- case RT_CAN_MODE_LISTEN:
- can_mode = CAN_MODE_LISTENONLY;
- break;
- case RT_CAN_MODE_LOOPBACK:
- can_mode = CAN_MODE_EXT_LOOPBACK;
- break;
- case RT_CAN_MODE_LOOPBACKANLISTEN:
- can_mode = CAN_MODE_LISTENONLY_EXT;
- break;
- }
- can_mode_set(can_instance->can_x, can_mode);
- can_retransmission_limit_set(can_instance->can_x, CAN_RE_TRANS_TIMES_UNLIMIT);
- can_rearbitration_limit_set(can_instance->can_x, CAN_RE_ARBI_TIMES_UNLIMIT);
- can_rxbuf_warning_set(can_instance->can_x, 3);
- return RT_EOK;
- }
- static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
- {
- rt_uint32_t argval;
- rt_uint32_t errtype;
- rt_uint8_t filter_number = 0;
- can_transmit_status_type statues;
- struct rt_can_filter_config *filter_cfg;
- RT_ASSERT(can != RT_NULL);
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- RT_ASSERT(can_instance != RT_NULL);
- switch (cmd)
- {
- case RT_DEVICE_CTRL_CLR_INT:
- argval = (rt_uint32_t) arg;
- if (argval == RT_DEVICE_FLAG_INT_RX)
- {
- nvic_irq_disable(can_instance->rx_irqn);
- /* disable interrupt */
- can_interrupt_enable(can_instance->can_x, CAN_RAFIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_RFIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_ROIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_RIE_INT, FALSE);
- }
- else if (argval == RT_DEVICE_FLAG_INT_TX)
- {
- nvic_irq_disable(can_instance->tx_irqn);
- /* disable interrupt */
- can_interrupt_enable(can_instance->can_x, CAN_TSIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_TPIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_AIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_ALIE_INT, FALSE);
- }
- else if (argval == RT_DEVICE_CAN_INT_ERR)
- {
- nvic_irq_disable(can_instance->err_irqn);
- /* disable interrupt */
- can_interrupt_enable(can_instance->can_x, CAN_EIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_BEIE_INT, FALSE);
- can_interrupt_enable(can_instance->can_x, CAN_EPIE_INT, FALSE);
- }
- break;
- case RT_DEVICE_CTRL_SET_INT:
- argval = (rt_uint32_t) arg;
- if (argval == RT_DEVICE_FLAG_INT_RX)
- {
- /* enable interrupt */
- can_interrupt_enable(can_instance->can_x, CAN_RAFIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_RFIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_ROIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_RIE_INT, TRUE);
- nvic_irq_enable(can_instance->rx_irqn, 1, 0);
- }
- else if (argval == RT_DEVICE_FLAG_INT_TX)
- {
- /* enable interrupt */
- can_interrupt_enable(can_instance->can_x, CAN_TSIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_TPIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_AIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_ALIE_INT, TRUE);
- nvic_irq_enable(can_instance->tx_irqn, 1, 0);
- }
- else if (argval == RT_DEVICE_CAN_INT_ERR)
- {
- /* enable interrupt */
- can_interrupt_enable(can_instance->can_x, CAN_EIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_BEIE_INT, TRUE);
- can_interrupt_enable(can_instance->can_x, CAN_EPIE_INT, TRUE);
- nvic_irq_enable(can_instance->err_irqn, 1, 0);
- }
- break;
- case RT_CAN_CMD_SET_FILTER:
- if (RT_NULL == arg)
- {
- /* default filter config */
- can_filter_default_para_init(&can_instance->filter_init_struct);
- }
- else
- {
- filter_cfg = (struct rt_can_filter_config *)arg;
- /* get default filter */
- for (int i = 0; i < filter_cfg->count; i++)
- {
- if (filter_cfg->items[i].hdr_bank == -1)
- {
- filter_number = i;
- }
- else
- {
- filter_number = filter_cfg->items[i].hdr_bank;
- }
- can_instance->filter_init_struct.code_para.id = filter_cfg->items[i].id;
- can_instance->filter_init_struct.code_para.id_type = (can_identifier_type)filter_cfg->items[i].ide;
- can_instance->filter_init_struct.mask_para.id = (~(filter_cfg->items[i].mask) & ~(filter_cfg->items[i].id & filter_cfg->items[i].mask));
- can_instance->filter_init_struct.mask_para.id_type = FALSE;
- can_instance->filter_init_struct.mask_para.data_length = 0xF;
- can_instance->filter_init_struct.mask_para.frame_type = TRUE;
- can_instance->filter_init_struct.mask_para.recv_frame = TRUE;
- /* filter configuration */
- do {
- can_transmit_status_get(can_instance->can_x, &statues);
- }
- while((statues.current_tstat != CAN_TSTAT_IDLE) || (can_stb_status_get(can_instance->can_x) != CAN_STB_STATUS_EMPTY) || \
- (can_rxbuf_status_get(can_instance->can_x) != CAN_RXBUF_STATUS_EMPTY));
- can_software_reset(can_instance->can_x, TRUE);
- can_filter_set(can_instance->can_x, (can_filter_type)filter_number, &can_instance->filter_init_struct);
- can_software_reset(can_instance->can_x, FALSE);
- can_filter_enable(can_instance->can_x, (can_filter_type)filter_number, TRUE);
- }
- }
- break;
- case RT_CAN_CMD_SET_MODE:
- argval = (rt_uint32_t) arg;
- if (argval != RT_CAN_MODE_NORMAL &&
- argval != RT_CAN_MODE_LISTEN &&
- argval != RT_CAN_MODE_LOOPBACK &&
- argval != RT_CAN_MODE_LOOPBACKANLISTEN)
- {
- return -RT_ERROR;
- }
- if (argval != can_instance->device.config.mode)
- {
- can_instance->device.config.mode = argval;
- return _can_config(&can_instance->device, &can_instance->device.config);
- }
- break;
- case RT_CAN_CMD_SET_BAUD:
- argval = (rt_uint32_t) arg;
- if (argval != CAN1MBaud &&
- argval != CAN800kBaud &&
- argval != CAN500kBaud &&
- argval != CAN250kBaud &&
- argval != CAN125kBaud &&
- argval != CAN100kBaud &&
- argval != CAN50kBaud &&
- argval != CAN20kBaud &&
- argval != CAN10kBaud)
- {
- return -RT_ERROR;
- }
- if (argval != can_instance->device.config.baud_rate)
- {
- can_instance->device.config.baud_rate = argval;
- return _can_config(&can_instance->device, &can_instance->device.config);
- }
- break;
- case RT_CAN_CMD_SET_PRIV:
- argval = (rt_uint32_t) arg;
- if (argval != RT_CAN_MODE_PRIV &&
- argval != RT_CAN_MODE_NOPRIV)
- {
- return -RT_ERROR;
- }
- if (argval != can_instance->device.config.privmode)
- {
- can_instance->device.config.privmode = argval;
- return _can_config(&can_instance->device, &can_instance->device.config);
- }
- break;
- case RT_CAN_CMD_GET_STATUS:
- errtype = can_instance->can_x->err;
- can_instance->device.status.rcverrcnt = errtype >> 24;
- can_instance->device.status.snderrcnt = ((errtype >> 16) & 0xFF);
- can_instance->device.status.lasterrtype = ((errtype >> 13) & 0x7);
- rt_memcpy(arg, &can_instance->device.status, sizeof(can_instance->device.status));
- break;
- #ifdef RT_CAN_USING_CANFD
- case RT_CAN_CMD_SET_CANFD:
- if(can_instance->enable_canfd != argval)
- {
- can_instance->enable_canfd = (rt_uint32_t) argval;
- }
- break;
- case RT_CAN_CMD_SET_BAUD_FD:
- argval = (rt_uint32_t) arg;
- if (argval != CANFD_DATA_1MBaud &&
- argval != CANFD_DATA_2MBaud &&
- argval != CANFD_DATA_3MBaud &&
- argval != CANFD_DATA_4MBaud &&
- argval != CANFD_DATA_5MBaud &&
- argval != CANFD_DATA_6MBaud)
- {
- return -RT_ERROR;
- }
- if (argval != can_instance->device.config.baud_rate_fd)
- {
- can_instance->device.config.baud_rate_fd = argval;
- return _can_config(&can_instance->device, &can_instance->device.config);
- }
- break;
- case RT_CAN_CMD_SET_BITTIMING:
- {
- struct rt_can_bit_timing_config *timing_configs = (struct rt_can_bit_timing_config*)arg;
- if ((timing_configs == RT_NULL) || (timing_configs->count < 1) || (timing_configs->count > 2))
- {
- return -RT_ERROR;
- }
- if (timing_configs->count != 0U)
- {
- can_instance->device.config.can_timing = timing_configs->items[0];
- }
- if (timing_configs->count == 2)
- {
- can_instance->device.config.canfd_timing = timing_configs->items[1];
- }
- _can_config(&can_instance->device, &can_instance->device.config);
- }
- break;
- #endif
- }
- return RT_EOK;
- }
- static rt_ssize_t _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num)
- {
- rt_uint8_t copy_length = 0;
- can_txbuf_type tx_message;
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
- tx_message.id = pmsg->id;
- if (RT_CAN_STDID == pmsg->ide)
- {
- tx_message.id_type = CAN_ID_STANDARD;
- }
- else
- {
- tx_message.id_type = CAN_ID_EXTENDED;
- }
- if (RT_CAN_DTR == pmsg->rtr)
- {
- tx_message.frame_type = CAN_FRAME_DATA;
- }
- else
- {
- tx_message.frame_type = CAN_FRAME_REMOTE;
- }
- #ifdef RT_CAN_USING_CANFD
- if (pmsg->fd_frame != 0)
- {
- tx_message.fd_format = CAN_FORMAT_FD;
- tx_message.fd_rate_switch = (can_rate_switch_type)pmsg->brs;
- RT_ASSERT(pmsg->len <= 15);
- }
- else
- #endif
- {
- RT_ASSERT(pmsg->len <= 8);
- }
- #ifdef RT_CAN_USING_CANFD
- copy_length = dlc_to_bytes[pmsg->len];
- #else
- copy_length = pmsg->len;
- #endif
- /* set up the data field */
- rt_memcpy(&tx_message.data[0], &pmsg->data[0], copy_length);
- /* set up the dlc */
- tx_message.data_length = (can_data_length_type)pmsg->len;
- can_txbuf_write(can_instance->can_x, CAN_TXBUF_STB, &tx_message);
- can_txbuf_transmit(can_instance->can_x, CAN_TRANSMIT_STB_ALL);
- return RT_EOK;
- }
- static rt_ssize_t _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo)
- {
- can_rxbuf_type rx_message;
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
- /* get data */
- can_rxbuf_read(can_instance->can_x, &rx_message);
- #ifdef RT_CAN_USING_CANFD
- pmsg->len = dlc_to_bytes[rx_message.data_length];
- pmsg->fd_frame = rx_message.fd_format;
- pmsg->brs = rx_message.fd_rate_switch;
- #else
- if(rx_message.data_length > 8)
- pmsg->len = 8;
- else
- pmsg->len = rx_message.data_length;
- #endif
- pmsg->id = rx_message.id;
- /* get the data field */
- rt_memcpy(&pmsg->data[0], &rx_message.data[0], pmsg->len);
- if (rx_message.id_type == CAN_ID_STANDARD)
- {
- pmsg->ide = RT_CAN_STDID;
- }
- else
- {
- pmsg->ide = RT_CAN_EXTID;
- }
- pmsg->rtr = rx_message.frame_type;
- return RT_EOK;
- }
- static const struct rt_can_ops _can_ops =
- {
- _can_config,
- _can_control,
- _can_sendmsg,
- _can_recvmsg,
- };
- static void _can_rx_isr(struct rt_can_device *can)
- {
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- /* rx_buffer had data be received */
- if(can_interrupt_flag_get(can_instance->can_x, CAN_RIF_FLAG) != RESET)
- {
- can_flag_clear(can_instance->can_x, CAN_RIF_FLAG);
- rt_hw_can_isr(can, RT_CAN_EVENT_RX_IND);
- }
- /* rx_buffer almost full */
- if(can_interrupt_flag_get(can_instance->can_x, CAN_RAFIF_FLAG) != RESET)
- {
- can_flag_clear(can_instance->can_x, CAN_RAFIF_FLAG);
- }
- /* rx_buffer full */
- if(can_interrupt_flag_get(can_instance->can_x, CAN_RFIF_FLAG) != RESET)
- {
- can_flag_clear(can_instance->can_x, CAN_RFIF_FLAG);
- }
- /* rx_buffer overflow */
- if(can_interrupt_flag_get(can_instance->can_x, CAN_ROIF_FLAG) != RESET)
- {
- can_flag_clear(can_instance->can_x, CAN_ROIF_FLAG);
- rt_hw_can_isr(can, RT_CAN_EVENT_RXOF_IND);
- }
- }
- static void _can_tx_isr(struct rt_can_device *can)
- {
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- /* transmit buffer be completed */
- if(can_interrupt_flag_get(can_instance->can_x, CAN_TSIF_FLAG) != RESET)
- {
- can_flag_clear(can_instance->can_x, CAN_TSIF_FLAG);
- rt_hw_can_isr(can, RT_CAN_EVENT_TX_DONE);
- }
- }
- void _can_err_isr(struct rt_can_device *can)
- {
- rt_uint32_t errtype;
- struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
- errtype = can_instance->can_x->err;
- switch (((errtype >> 13) & 0x7))
- {
- case RT_CAN_BUS_BIT_PAD_ERR:
- can_instance->device.status.bitpaderrcnt++;
- break;
- case RT_CAN_BUS_FORMAT_ERR:
- can_instance->device.status.formaterrcnt++;
- break;
- case RT_CAN_BUS_ACK_ERR:
- can_instance->device.status.ackerrcnt++;
- rt_hw_can_isr(&can_instance->device, RT_CAN_EVENT_TX_FAIL);
- case RT_CAN_BUS_IMPLICIT_BIT_ERR:
- case RT_CAN_BUS_EXPLICIT_BIT_ERR:
- can_instance->device.status.biterrcnt++;
- break;
- case RT_CAN_BUS_CRC_ERR:
- can_instance->device.status.crcerrcnt++;
- break;
- }
- can_instance->device.status.rcverrcnt = errtype >> 24;
- can_instance->device.status.snderrcnt = (errtype >> 16 & 0xFF);
- can_instance->device.status.lasterrtype = (errtype >> 13 & 0x7);
- can_flag_clear(can_instance->can_x, CAN_BEIF_FLAG);
- }
- #ifdef BSP_USING_CAN1
- void CAN1_TX_IRQHandler(void) {
- rt_interrupt_enter();
- _can_tx_isr(&can_config[CAN1_INDEX].device);
- rt_interrupt_leave();
- }
- void CAN1_RX_IRQHandler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- _can_rx_isr(&can_config[CAN1_INDEX].device);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- void CAN1_ERR_IRQHandler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- _can_err_isr(&can_config[CAN1_INDEX].device);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- #endif
- #ifdef BSP_USING_CAN2
- void CAN2_TX_IRQHandler(void) {
- rt_interrupt_enter();
- _can_tx_isr(&can_config[CAN2_INDEX].device);
- rt_interrupt_leave();
- }
- void CAN2_RX_IRQHandler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- _can_rx_isr(&can_config[CAN2_INDEX].device);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- void CAN2_ERR_IRQHandler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- _can_err_isr(&can_config[CAN2_INDEX].device);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- #endif
- #ifdef BSP_USING_CAN3
- void CAN3_TX_IRQHandler(void) {
- rt_interrupt_enter();
- _can_tx_isr(&can_config[CAN3_INDEX].device);
- rt_interrupt_leave();
- }
- void CAN3_RX_IRQHandler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- _can_rx_isr(&can_config[CAN3_INDEX].device);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- void CAN3_ERR_IRQHandler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- _can_err_isr(&can_config[CAN3_INDEX].device);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- #endif
- int rt_hw_can_init(void)
- {
- rt_size_t obj_num;
- int index;
- obj_num = sizeof(can_config) / sizeof(struct at32_can);
- struct can_configure config = CANDEFAULTCONFIG;
- config.privmode = RT_CAN_MODE_NOPRIV;
- config.ticks = 50;
- #ifdef RT_CAN_USING_HDR
- config.maxhdr = 16;
- #endif
- for (index = 0; index < obj_num; index++) {
- /* config default filter */
- can_filter_config_type filter_config;
- can_filter_default_para_init(&filter_config);
- can_config[index].filter_init_struct = filter_config;
- can_config[index].device.config = config;
- rt_hw_can_register(&can_config[index].device,
- can_config[index].name,
- &_can_ops,
- &can_config[index]);
- }
- return 0;
- }
- INIT_BOARD_EXPORT(rt_hw_can_init);
- #endif /* BSP_USING_CAN_V2 */
|