|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | +#include "hrt.h" |
| 7 | +#include <hal/nrf_vpr_csr_vio.h> |
| 8 | +#include <hal/nrf_vpr_csr_vtim.h> |
| 9 | + |
| 10 | +#define TOP 4 |
| 11 | + |
| 12 | +void write_single_by_word(volatile uint32_t *data, uint8_t data_len, uint32_t counter_top, |
| 13 | + uint8_t word_size, bool ce_enable_state, bool hold_ce) |
| 14 | +{ |
| 15 | + NRFX_ASSERT(word_size > MAX_WORD_SIZE); |
| 16 | + |
| 17 | + /* Configuration step */ |
| 18 | + uint16_t dir = nrf_vpr_csr_vio_dir_get(); |
| 19 | + |
| 20 | + nrf_vpr_csr_vio_dir_set(dir | PIN_DIR_OUT_MASK(D0_PIN) | PIN_DIR_OUT_MASK(CS_PIN) | |
| 21 | + PIN_DIR_OUT_MASK(SCLK_PIN)); |
| 22 | + |
| 23 | + uint16_t out = nrf_vpr_csr_vio_out_get(); |
| 24 | + |
| 25 | + nrf_vpr_csr_vio_out_set(out | PIN_OUT_LOW_MASK(D0_PIN) | PIN_OUT_HIGH_MASK(CS_PIN) | |
| 26 | + PIN_OUT_LOW_MASK(SCLK_PIN)); |
| 27 | + |
| 28 | + nrf_vpr_csr_vio_mode_out_t out_mode = { |
| 29 | + .mode = NRF_VPR_CSR_VIO_SHIFT_OUTB_TOGGLE, |
| 30 | + .frame_width = 1, |
| 31 | + }; |
| 32 | + |
| 33 | + nrf_vpr_csr_vio_mode_out_set(&out_mode); |
| 34 | + nrf_vpr_csr_vio_mode_in_buffered_set(NRF_VPR_CSR_VIO_MODE_IN_CONTINUOUS); |
| 35 | + |
| 36 | + nrf_vpr_csr_vio_config_t config; |
| 37 | + |
| 38 | + nrf_vpr_csr_vio_config_get(&config); |
| 39 | + config.input_sel = false; |
| 40 | + nrf_vpr_csr_vio_config_set(&config); |
| 41 | + |
| 42 | + /* Fix position of data if word size < MAX_WORD_SIZE, |
| 43 | + * so that leading zeros would not be printed instead of data bits. |
| 44 | + */ |
| 45 | + if (word_size < MAX_WORD_SIZE) { |
| 46 | + for (uint8_t i = 0; i < data_len; i++) { |
| 47 | + data[i] = data[i] << (MAX_WORD_SIZE - word_size); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /* Counter settings */ |
| 52 | + nrf_vpr_csr_vtim_count_mode_set(0, NRF_VPR_CSR_VTIM_COUNT_RELOAD); |
| 53 | + nrf_vpr_csr_vtim_simple_counter_top_set(0, counter_top); |
| 54 | + |
| 55 | + /* Set number of shifts before OUTB needs to be updated. |
| 56 | + * First shift needs to be increased by 1. |
| 57 | + */ |
| 58 | + nrf_vpr_csr_vio_shift_cnt_out_set(word_size); |
| 59 | + nrf_vpr_csr_vio_shift_cnt_out_buffered_set(word_size - 1); |
| 60 | + |
| 61 | + /* Enable CS */ |
| 62 | + out = nrf_vpr_csr_vio_out_get(); |
| 63 | + out &= ~PIN_OUT_HIGH_MASK(CS_PIN); |
| 64 | + out |= ce_enable_state ? PIN_OUT_HIGH_MASK(CS_PIN) : PIN_OUT_LOW_MASK(CS_PIN); |
| 65 | + nrf_vpr_csr_vio_out_set(out); |
| 66 | + |
| 67 | + /* Start counter */ |
| 68 | + nrf_vpr_csr_vtim_simple_counter_set(0, 3 * counter_top); |
| 69 | + |
| 70 | + /* Send data */ |
| 71 | + for (uint8_t i = 0; i < data_len; i++) { |
| 72 | + nrf_vpr_csr_vio_out_buffered_reversed_byte_set(data[i]); |
| 73 | + } |
| 74 | + |
| 75 | + /* Clear all bits, wait until the last word is sent */ |
| 76 | + nrf_vpr_csr_vio_out_buffered_set(0); |
| 77 | + |
| 78 | + /* Final configuration */ |
| 79 | + out_mode.mode = NRF_VPR_CSR_VIO_SHIFT_NONE; |
| 80 | + nrf_vpr_csr_vio_mode_out_buffered_set(&out_mode); |
| 81 | + nrf_vpr_csr_vio_mode_in_buffered_set(NRF_VPR_CSR_VIO_MODE_IN_CONTINUOUS); |
| 82 | + |
| 83 | + /* Deselect slave */ |
| 84 | + if (!hold_ce) { |
| 85 | + out = nrf_vpr_csr_vio_out_get(); |
| 86 | + out &= ~(PIN_OUT_HIGH_MASK(CS_PIN) | PIN_OUT_HIGH_MASK(SCLK_PIN)); |
| 87 | + out |= ce_enable_state ? PIN_OUT_LOW_MASK(CS_PIN) : PIN_OUT_HIGH_MASK(CS_PIN); |
| 88 | + out |= PIN_OUT_LOW_MASK(SCLK_PIN); |
| 89 | + nrf_vpr_csr_vio_out_set(out); |
| 90 | + } |
| 91 | + |
| 92 | + /* Stop counter */ |
| 93 | + nrf_vpr_csr_vtim_count_mode_set(0, NRF_VPR_CSR_VTIM_COUNT_STOP); |
| 94 | +} |
| 95 | + |
| 96 | +void write_quad_by_word(volatile uint32_t *data, uint8_t data_len, uint32_t counter_top, |
| 97 | + uint8_t word_size, bool ce_enable_state, bool hold_ce) |
| 98 | +{ |
| 99 | + NRFX_ASSERT(word_size > MAX_WORD_SIZE); |
| 100 | + NRFX_ASSERT(word_size % 4 == 0); |
| 101 | + |
| 102 | + /* Configuration step */ |
| 103 | + uint16_t dir = nrf_vpr_csr_vio_dir_get(); |
| 104 | + |
| 105 | + nrf_vpr_csr_vio_dir_set(dir | PIN_DIR_OUT_MASK(D0_PIN) | PIN_DIR_OUT_MASK(D1_PIN) | |
| 106 | + PIN_DIR_OUT_MASK(D2_PIN) | PIN_DIR_OUT_MASK(D3_PIN) | |
| 107 | + PIN_DIR_OUT_MASK(CS_PIN) | PIN_DIR_OUT_MASK(SCLK_PIN)); |
| 108 | + |
| 109 | + uint16_t out = nrf_vpr_csr_vio_out_get(); |
| 110 | + |
| 111 | + nrf_vpr_csr_vio_out_set(out | PIN_OUT_LOW_MASK(D0_PIN) | PIN_OUT_LOW_MASK(D1_PIN) | |
| 112 | + PIN_OUT_LOW_MASK(D2_PIN) | PIN_OUT_LOW_MASK(D3_PIN) | |
| 113 | + PIN_OUT_HIGH_MASK(CS_PIN) | PIN_OUT_LOW_MASK(SCLK_PIN)); |
| 114 | + |
| 115 | + nrf_vpr_csr_vio_mode_out_t out_mode = { |
| 116 | + .mode = NRF_VPR_CSR_VIO_SHIFT_OUTB_TOGGLE, |
| 117 | + .frame_width = 4, |
| 118 | + }; |
| 119 | + |
| 120 | + nrf_vpr_csr_vio_mode_out_set(&out_mode); |
| 121 | + nrf_vpr_csr_vio_mode_in_buffered_set(NRF_VPR_CSR_VIO_MODE_IN_CONTINUOUS); |
| 122 | + |
| 123 | + nrf_vpr_csr_vio_config_t config; |
| 124 | + |
| 125 | + nrf_vpr_csr_vio_config_get(&config); |
| 126 | + config.input_sel = false; |
| 127 | + nrf_vpr_csr_vio_config_set(&config); |
| 128 | + |
| 129 | + /* Fix position of data if word size < MAX_WORD_SIZE, |
| 130 | + * so that leading zeros would not be printed instead of data. |
| 131 | + */ |
| 132 | + if (word_size < MAX_WORD_SIZE) { |
| 133 | + for (uint8_t i = 0; i < data_len; i++) { |
| 134 | + data[i] = data[i] << (MAX_WORD_SIZE - word_size); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /* Counter settings */ |
| 139 | + nrf_vpr_csr_vtim_count_mode_set(0, NRF_VPR_CSR_VTIM_COUNT_RELOAD); |
| 140 | + nrf_vpr_csr_vtim_simple_counter_top_set(0, counter_top); |
| 141 | + |
| 142 | + /* Set number of shifts before OUTB needs to be updated. |
| 143 | + * First shift needs to be increased by 1. |
| 144 | + */ |
| 145 | + nrf_vpr_csr_vio_shift_cnt_out_set(word_size / 4); |
| 146 | + nrf_vpr_csr_vio_shift_cnt_out_buffered_set(word_size / 4 - 1); |
| 147 | + |
| 148 | + /* Enable CS */ |
| 149 | + out = nrf_vpr_csr_vio_out_get(); |
| 150 | + out &= ~PIN_OUT_HIGH_MASK(CS_PIN); |
| 151 | + out |= ce_enable_state ? PIN_OUT_HIGH_MASK(CS_PIN) : PIN_OUT_LOW_MASK(CS_PIN); |
| 152 | + nrf_vpr_csr_vio_out_set(out); |
| 153 | + |
| 154 | + /* Start counter */ |
| 155 | + nrf_vpr_csr_vtim_simple_counter_set(0, 3 * counter_top); |
| 156 | + |
| 157 | + /* Send data */ |
| 158 | + for (uint8_t i = 0; i < data_len; i++) { |
| 159 | + nrf_vpr_csr_vio_out_buffered_reversed_byte_set(data[i]); |
| 160 | + } |
| 161 | + |
| 162 | + /* Clear all bits, wait until the last word is sent */ |
| 163 | + nrf_vpr_csr_vio_out_buffered_set(0); |
| 164 | + |
| 165 | + /* Final configuration */ |
| 166 | + out_mode.mode = NRF_VPR_CSR_VIO_SHIFT_NONE; |
| 167 | + nrf_vpr_csr_vio_mode_out_buffered_set(&out_mode); |
| 168 | + nrf_vpr_csr_vio_mode_in_buffered_set(NRF_VPR_CSR_VIO_MODE_IN_CONTINUOUS); |
| 169 | + |
| 170 | + /* Deselect slave */ |
| 171 | + if (!hold_ce) { |
| 172 | + out = nrf_vpr_csr_vio_out_get(); |
| 173 | + out &= ~(PIN_OUT_HIGH_MASK(CS_PIN) | PIN_OUT_HIGH_MASK(SCLK_PIN)); |
| 174 | + out |= ce_enable_state ? PIN_OUT_LOW_MASK(CS_PIN) : PIN_OUT_HIGH_MASK(CS_PIN); |
| 175 | + out |= PIN_OUT_LOW_MASK(SCLK_PIN); |
| 176 | + nrf_vpr_csr_vio_out_set(out); |
| 177 | + } |
| 178 | + |
| 179 | + /* Stop counter */ |
| 180 | + nrf_vpr_csr_vtim_count_mode_set(0, NRF_VPR_CSR_VTIM_COUNT_STOP); |
| 181 | +} |
0 commit comments