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