twai_hal.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include "hal/twai_hal.h"
  15. //Default values written to various registers on initialization
  16. #define TWAI_HAL_INIT_TEC 0
  17. #define TWAI_HAL_INIT_REC 0
  18. #define TWAI_HAL_INIT_EWL 96
  19. /* ---------------------------- Init and Config ----------------------------- */
  20. bool twai_hal_init(twai_hal_context_t *hal_ctx)
  21. {
  22. //Initialize HAL context
  23. hal_ctx->dev = &TWAI;
  24. hal_ctx->state_flags = 0;
  25. //Initialize TWAI controller, and set default values to registers
  26. twai_ll_enter_reset_mode(hal_ctx->dev);
  27. if (!twai_ll_is_in_reset_mode(hal_ctx->dev)) { //Must enter reset mode to write to config registers
  28. return false;
  29. }
  30. #if SOC_TWAI_SUPPORT_MULTI_ADDRESS_LAYOUT
  31. twai_ll_enable_extended_reg_layout(hal_ctx->dev); //Changes the address layout of the registers
  32. #endif
  33. twai_ll_set_mode(hal_ctx->dev, TWAI_MODE_LISTEN_ONLY); //Freeze REC by changing to LOM mode
  34. //Both TEC and REC should start at 0
  35. twai_ll_set_tec(hal_ctx->dev, TWAI_HAL_INIT_TEC);
  36. twai_ll_set_rec(hal_ctx->dev, TWAI_HAL_INIT_REC);
  37. twai_ll_set_err_warn_lim(hal_ctx->dev, TWAI_HAL_INIT_EWL); //Set default value of for EWL
  38. return true;
  39. }
  40. void twai_hal_deinit(twai_hal_context_t *hal_ctx)
  41. {
  42. //Clear any pending registers
  43. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev);
  44. twai_ll_set_enabled_intrs(hal_ctx->dev, 0);
  45. twai_ll_clear_arb_lost_cap(hal_ctx->dev);
  46. twai_ll_clear_err_code_cap(hal_ctx->dev);
  47. hal_ctx->dev = NULL;
  48. }
  49. 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)
  50. {
  51. //Configure bus timing, acceptance filter, CLKOUT, and interrupts
  52. 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);
  53. twai_ll_set_acc_filter(hal_ctx->dev, f_config->acceptance_code, f_config->acceptance_mask, f_config->single_filter);
  54. twai_ll_set_clkout(hal_ctx->dev, clkout_divider);
  55. twai_ll_set_enabled_intrs(hal_ctx->dev, intr_mask);
  56. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev); //Clear any latched interrupts
  57. }
  58. /* -------------------------------- Actions --------------------------------- */
  59. void twai_hal_start(twai_hal_context_t *hal_ctx, twai_mode_t mode)
  60. {
  61. twai_ll_set_mode(hal_ctx->dev, mode); //Set operating mode
  62. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev); //Clear any latched interrupts
  63. TWAI_HAL_SET_FLAG(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_RUNNING);
  64. twai_ll_exit_reset_mode(hal_ctx->dev);
  65. }
  66. void twai_hal_stop(twai_hal_context_t *hal_ctx)
  67. {
  68. twai_ll_enter_reset_mode(hal_ctx->dev);
  69. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev);
  70. twai_ll_set_mode(hal_ctx->dev, TWAI_MODE_LISTEN_ONLY); //Freeze REC by changing to LOM mode
  71. //Any TX is immediately halted on entering reset mode
  72. TWAI_HAL_RESET_FLAG(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_TX_BUFF_OCCUPIED);
  73. TWAI_HAL_RESET_FLAG(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_RUNNING);
  74. }