twai_hal.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "sdkconfig.h"
  15. #include "hal/twai_hal.h"
  16. #include "soc/soc_caps.h"
  17. //Default values written to various registers on initialization
  18. #define TWAI_HAL_INIT_TEC 0
  19. #define TWAI_HAL_INIT_REC 0
  20. #define TWAI_HAL_INIT_EWL 96
  21. /* ---------------------------- Init and Config ----------------------------- */
  22. bool twai_hal_init(twai_hal_context_t *hal_ctx)
  23. {
  24. //Initialize HAL context
  25. hal_ctx->dev = &TWAI;
  26. hal_ctx->state_flags = 0;
  27. //Initialize TWAI controller, and set default values to registers
  28. twai_ll_enter_reset_mode(hal_ctx->dev);
  29. if (!twai_ll_is_in_reset_mode(hal_ctx->dev)) { //Must enter reset mode to write to config registers
  30. return false;
  31. }
  32. #if SOC_TWAI_SUPPORT_MULTI_ADDRESS_LAYOUT
  33. twai_ll_enable_extended_reg_layout(hal_ctx->dev); //Changes the address layout of the registers
  34. #endif
  35. twai_ll_set_mode(hal_ctx->dev, TWAI_MODE_LISTEN_ONLY); //Freeze REC by changing to LOM mode
  36. //Both TEC and REC should start at 0
  37. twai_ll_set_tec(hal_ctx->dev, TWAI_HAL_INIT_TEC);
  38. twai_ll_set_rec(hal_ctx->dev, TWAI_HAL_INIT_REC);
  39. twai_ll_set_err_warn_lim(hal_ctx->dev, TWAI_HAL_INIT_EWL); //Set default value of for EWL
  40. return true;
  41. }
  42. void twai_hal_deinit(twai_hal_context_t *hal_ctx)
  43. {
  44. //Clear any pending registers
  45. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev);
  46. twai_ll_set_enabled_intrs(hal_ctx->dev, 0);
  47. twai_ll_clear_arb_lost_cap(hal_ctx->dev);
  48. twai_ll_clear_err_code_cap(hal_ctx->dev);
  49. hal_ctx->dev = NULL;
  50. }
  51. 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)
  52. {
  53. //Configure bus timing, acceptance filter, CLKOUT, and interrupts
  54. 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);
  55. twai_ll_set_acc_filter(hal_ctx->dev, f_config->acceptance_code, f_config->acceptance_mask, f_config->single_filter);
  56. twai_ll_set_clkout(hal_ctx->dev, clkout_divider);
  57. twai_ll_set_enabled_intrs(hal_ctx->dev, intr_mask);
  58. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev); //Clear any latched interrupts
  59. }
  60. /* -------------------------------- Actions --------------------------------- */
  61. void twai_hal_start(twai_hal_context_t *hal_ctx, twai_mode_t mode)
  62. {
  63. twai_ll_set_mode(hal_ctx->dev, mode); //Set operating mode
  64. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev); //Clear any latched interrupts
  65. TWAI_HAL_SET_BITS(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_RUNNING);
  66. twai_ll_exit_reset_mode(hal_ctx->dev);
  67. }
  68. void twai_hal_stop(twai_hal_context_t *hal_ctx)
  69. {
  70. twai_ll_enter_reset_mode(hal_ctx->dev);
  71. (void) twai_ll_get_and_clear_intrs(hal_ctx->dev);
  72. twai_ll_set_mode(hal_ctx->dev, TWAI_MODE_LISTEN_ONLY); //Freeze REC by changing to LOM mode
  73. //Any TX is immediately halted on entering reset mode
  74. TWAI_HAL_CLEAR_BITS(hal_ctx->state_flags, TWAI_HAL_STATE_FLAG_TX_BUFF_OCCUPIED | TWAI_HAL_STATE_FLAG_RUNNING);
  75. }