1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stddef.h>
16 #include "sdkconfig.h"
17 #include "hal/twai_hal.h"
18 #include "soc/soc_caps.h"
19
20 //Default values written to various registers on initialization
21 #define TWAI_HAL_INIT_TEC 0
22 #define TWAI_HAL_INIT_REC 0
23 #define TWAI_HAL_INIT_EWL 96
24
25 /* ---------------------------- Init and Config ----------------------------- */
26
twai_hal_init(twai_hal_context_t * hal_ctx)27 bool twai_hal_init(twai_hal_context_t *hal_ctx)
28 {
29 //Initialize HAL context
30 hal_ctx->dev = &TWAI;
31 hal_ctx->state_flags = 0;
32 //Initialize TWAI controller, and set default values to registers
33 twai_ll_enter_reset_mode(hal_ctx->dev);
34 if (!twai_ll_is_in_reset_mode(hal_ctx->dev)) { //Must enter reset mode to write to config registers
35 return false;
36 }
37 #if SOC_TWAI_SUPPORT_MULTI_ADDRESS_LAYOUT
38 twai_ll_enable_extended_reg_layout(hal_ctx->dev); //Changes the address layout of the registers
39 #endif
40 twai_ll_set_mode(hal_ctx->dev, TWAI_MODE_LISTEN_ONLY); //Freeze REC by changing to LOM mode
41 //Both TEC and REC should start at 0
42 twai_ll_set_tec(hal_ctx->dev, TWAI_HAL_INIT_TEC);
43 twai_ll_set_rec(hal_ctx->dev, TWAI_HAL_INIT_REC);
44 twai_ll_set_err_warn_lim(hal_ctx->dev, TWAI_HAL_INIT_EWL); //Set default value of for EWL
45 return true;
46 }
47
twai_hal_deinit(twai_hal_context_t * hal_ctx)48 void twai_hal_deinit(twai_hal_context_t *hal_ctx)
49 {
50 //Clear any pending registers
51 (void) twai_ll_get_and_clear_intrs(hal_ctx->dev);
52 twai_ll_set_enabled_intrs(hal_ctx->dev, 0);
53 twai_ll_clear_arb_lost_cap(hal_ctx->dev);
54 twai_ll_clear_err_code_cap(hal_ctx->dev);
55 hal_ctx->dev = NULL;
56 }
57
twai_hal_configure(twai_hal_context_t * hal_ctx,const twai_timing_config_t * t_config,const twai_filter_config_t * f_config,uint32_t intr_mask,uint32_t clkout_divider)58 void twai_hal_configure(twai_hal_context_t *hal_ctx, const twai_timing_config_t *t_config, const twai_filter_config_t *f_config, uint32_t intr_mask, uint32_t clkout_divider)
59 {
60 //Configure bus timing, acceptance filter, CLKOUT, and interrupts
61 twai_ll_set_bus_timing(hal_ctx->dev, t_config->brp, t_config->sjw, t_config->tseg_1, t_config->tseg_2, t_config->triple_sampling);
62 twai_ll_set_acc_filter(hal_ctx->dev, f_config->acceptance_code, f_config->acceptance_mask, f_config->single_filter);
63 twai_ll_set_clkout(hal_ctx->dev, clkout_divider);
64 twai_ll_set_enabled_intrs(hal_ctx->dev, intr_mask);
65 (void) twai_ll_get_and_clear_intrs(hal_ctx->dev); //Clear any latched interrupts
66 }
67
68 /* -------------------------------- Actions --------------------------------- */
69
twai_hal_start(twai_hal_context_t * hal_ctx,twai_mode_t mode)70 void twai_hal_start(twai_hal_context_t *hal_ctx, twai_mode_t mode)
71 {
72 twai_ll_set_mode(hal_ctx->dev, mode); //Set operating mode
73 (void) twai_ll_get_and_clear_intrs(hal_ctx->dev); //Clear any latched interrupts
74 TWAI_HAL_SET_BITS(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_RUNNING);
75 twai_ll_exit_reset_mode(hal_ctx->dev);
76 }
77
twai_hal_stop(twai_hal_context_t * hal_ctx)78 void twai_hal_stop(twai_hal_context_t *hal_ctx)
79 {
80 twai_ll_enter_reset_mode(hal_ctx->dev);
81 (void) twai_ll_get_and_clear_intrs(hal_ctx->dev);
82 twai_ll_set_mode(hal_ctx->dev, TWAI_MODE_LISTEN_ONLY); //Freeze REC by changing to LOM mode
83 //Any TX is immediately halted on entering reset mode
84 TWAI_HAL_CLEAR_BITS(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_TX_BUFF_OCCUPIED | TWAI_HAL_STATE_FLAG_RUNNING);
85 }
86