http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_hci_slip.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_hci_slip.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_hci_slip.c deleted file mode 100644 index 89f5f90..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_hci_slip.c +++ /dev/null @@ -1,634 +0,0 @@ - -/* Copyright (c) Nordic Semiconductor ASA - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of Nordic Semiconductor ASA nor the names of other - * contributors to this software may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * 4. This software must only be used in a processor manufactured by Nordic - * Semiconductor ASA, or in a processor manufactured by a third party that - * is used in combination with a processor manufactured by Nordic Semiconductor. - * - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdint.h> -#include <string.h> -#include <stdbool.h> - -#include "nrf_error.h" -#include "nrf_gpio.h" -#include "app_uart.h" -#include "ser_phy_hci.h" -#include "app_error.h" -#include "app_util_platform.h" -#include "nrf_soc.h" - -#ifdef SER_CONNECTIVITY -#include "ser_phy_config_conn_nrf51.h" -#else -#include "ser_phy_config_app_nrf51.h" -#endif /* SER_CONNECTIVITY */ - -#include "ser_config.h" -#define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */ -#define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */ -#define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */ -#define APP_SLIP_ESC_ESC 0xDD /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xDB. */ - -#define HDR_SIZE 4 -#define CRC_SIZE 2 -#define PKT_SIZE (SER_HAL_TRANSPORT_MAX_PKT_SIZE + HDR_SIZE + CRC_SIZE) - -static const app_uart_comm_params_t comm_params = -{ - .rx_pin_no = SER_PHY_UART_RX, - .tx_pin_no = SER_PHY_UART_TX, - .rts_pin_no = SER_PHY_UART_RTS, - .cts_pin_no = SER_PHY_UART_CTS, - // Below values are defined in ser_config.h common for application and connectivity - .flow_control = SER_PHY_UART_FLOW_CTRL, - .use_parity = SER_PHY_UART_PARITY, - .baud_rate = SER_PHY_UART_BAUDRATE -}; - -static uint8_t m_small_buffer[HDR_SIZE]; -static uint8_t m_big_buffer[PKT_SIZE]; - -static uint8_t * mp_small_buffer = NULL; -static uint8_t * mp_big_buffer = NULL; -static uint8_t * mp_buffer = NULL; - -static ser_phy_hci_pkt_params_t m_header; -static ser_phy_hci_pkt_params_t m_payload; -static ser_phy_hci_pkt_params_t m_crc; -static ser_phy_hci_pkt_params_t m_header_pending; -static ser_phy_hci_pkt_params_t m_payload_pending; -static ser_phy_hci_pkt_params_t m_crc_pending; - -static ser_phy_hci_slip_evt_t m_ser_phy_hci_slip_event; -static ser_phy_hci_slip_event_handler_t m_ser_phy_hci_slip_event_handler; /**< Event handler for upper layer */ - -static bool m_other_side_active = false; /**< Flag indicating that the other side is running */ -static uint8_t m_rx_byte; /**< Rx byte passed from low-level driver */ - -static bool m_rx_escape = false; -static bool m_tx_escape = false; - -static bool m_tx_busy = false; /**< Flag indicating that currently some transmission is ongoing */ - -static uint32_t m_tx_index; -static uint32_t m_rx_index; -static ser_phy_hci_pkt_params_t * mp_data = NULL; - -/* Function declarations */ -static uint32_t ser_phy_hci_tx_byte(void); -static bool slip_decode(uint8_t * p_received_byte); -static void ser_phi_hci_rx_byte(uint8_t rx_byte); -// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -__STATIC_INLINE void callback_hw_error(uint32_t error_src) -{ - m_ser_phy_hci_slip_event.evt_type = SER_PHY_HCI_SLIP_EVT_HW_ERROR; - - // Pass error source to upper layer - m_ser_phy_hci_slip_event.evt_params.hw_error.error_code = error_src; - m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event); -} - - -__STATIC_INLINE void slip_encode(void) -{ - switch (mp_data->p_buffer[m_tx_index]) - { - case APP_SLIP_END: - m_tx_escape = true; - (void)app_uart_put(APP_SLIP_ESC); - break; - - case APP_SLIP_ESC: - m_tx_escape = true; - (void)app_uart_put(APP_SLIP_ESC); - break; - - default: - (void)app_uart_put(mp_data->p_buffer[m_tx_index]); - m_tx_index++; - break; - } -} - - -__STATIC_INLINE bool check_pending_tx() -{ - bool tx_continue = false; - - if (m_header_pending.p_buffer != NULL) - { - m_header = m_header_pending; - m_payload = m_payload_pending; - m_crc = m_crc_pending; - - m_header_pending.p_buffer = NULL; - m_header_pending.num_of_bytes = 0; - m_payload_pending.p_buffer = NULL; - m_payload_pending.num_of_bytes = 0; - m_crc_pending.p_buffer = NULL; - m_crc_pending.num_of_bytes = 0; - - m_tx_index = 0; // may be also done in ser_phy_hci_tx_byte??? - tx_continue = true; - - /* Start sending pending packet */ - (void)ser_phy_hci_tx_byte(); - } - - return tx_continue; -} - - -static uint32_t ser_phy_hci_tx_byte() -{ - /* Flags informing about actually transmited part of packet*/ - static bool header = false; - static bool payload = false; - static bool crc = false; - - static bool ack_end = false; - static bool packet_end = false; - - if (ack_end == true) - { - ack_end = false; - m_tx_busy = check_pending_tx(); - /* Report end of ACK transmission*/ - m_ser_phy_hci_slip_event.evt_type = SER_PHY_HCI_SLIP_EVT_ACK_SENT; - m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event); - - } - else if (packet_end == true) - { - packet_end = false; - m_tx_busy = check_pending_tx(); - /* Report end of packet transmission*/ - m_ser_phy_hci_slip_event.evt_type = SER_PHY_HCI_SLIP_EVT_PKT_SENT; - m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event); - - } - else if ((m_tx_index == 0) && !header && !payload && !crc) - { - /* Beginning of packet - sent 0xC0*/ - header = true; - mp_data = &m_header; - (void)app_uart_put(APP_SLIP_END); - } - else if ((m_tx_index == mp_data->num_of_bytes) && crc == true) - { - /* End of packet - sent 0xC0*/ - (void)app_uart_put(APP_SLIP_END); - - m_crc.p_buffer = NULL; - crc = false; - m_tx_index = 0; - packet_end = true; - } - else if ((m_tx_index == mp_data->num_of_bytes) && header == true) - { - /* End of header transmission*/ - m_tx_index = 0; - - if (m_payload.p_buffer != NULL) - { - header = false; - payload = true; - mp_data = &m_payload; - - /* Handle every character in buffer accordingly to SLIP protocol*/ - slip_encode(); - } - else - { - /* End of ACK - sent 0xC0*/ - (void)app_uart_put(APP_SLIP_END); - - header = false; - ack_end = true; - } - } - else if ((m_tx_index == mp_data->num_of_bytes) && payload == true) - { - /* End of payload transmission*/ - m_tx_index = 0; - - if (m_crc.p_buffer != NULL) - { - m_payload.p_buffer = NULL; - payload = false; - crc = true; - mp_data = &m_crc; - - /* Handle every character in buffer accordingly to SLIP protocol*/ - slip_encode(); - } - /* CRC is not used for this packet -> finish packet transmission */ - else - { - /* End of packet - send 0xC0*/ - (void)app_uart_put(APP_SLIP_END); - - m_payload.p_buffer = NULL; - payload = false; - packet_end = true; - } - } - else if (m_tx_escape == false) - { - /* Handle every character in buffer accordingly to SLIP protocol*/ - slip_encode(); - } - else if (m_tx_escape == true) - { - /* Send SLIP special code*/ - m_tx_escape = false; - - if (mp_data->p_buffer[m_tx_index] == APP_SLIP_END) - { - (void)app_uart_put(APP_SLIP_ESC_END); - m_tx_index++; - } - else - { - (void)app_uart_put(APP_SLIP_ESC_ESC); - m_tx_index++; - } - } - - return NRF_SUCCESS; -} - - -uint32_t ser_phy_hci_slip_tx_pkt_send(const ser_phy_hci_pkt_params_t * p_header, - const ser_phy_hci_pkt_params_t * p_payload, - const ser_phy_hci_pkt_params_t * p_crc) -{ - /* Block TXRDY interrupts at this point*/ - // NRF_UART0->INTENCLR = (UART_INTENCLR_TXDRDY_Clear << UART_INTENCLR_TXDRDY_Pos); - CRITICAL_REGION_ENTER(); - - if (p_header == NULL) - { - return NRF_ERROR_NULL; - } - - /* Check if no tx is ongoing */ - if (!m_tx_busy) - { - m_header = *p_header; - - if (p_payload != NULL) - { - m_payload = *p_payload; - } - - if (p_crc != NULL) - { - m_crc = *p_crc; - } - } - /* Tx is ongoing, schedule transmission as pending */ - else - { - if (p_crc != NULL) - { - m_crc_pending = *p_crc; - } - - if (p_payload != NULL) - { - m_payload_pending = *p_payload; - } - - m_header_pending = *p_header; - } - - /* Start packet transmission only if no other tx is ongoing */ - if (!m_tx_busy) - { - m_tx_busy = true; - (void)ser_phy_hci_tx_byte(); - } - - /* Enable TXRDY interrupts at this point*/ - // NRF_UART0->INTENSET = (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos); - CRITICAL_REGION_EXIT(); - return NRF_SUCCESS; -} - - -/* Function returns false when last byte in packet is detected.*/ -static bool slip_decode(uint8_t * p_received_byte) -{ - switch (*p_received_byte) - { - case APP_SLIP_END: - return false; - - case APP_SLIP_ESC: - m_rx_escape = true; - break; - - case APP_SLIP_ESC_END: - - if (m_rx_escape == true) - { - m_rx_escape = false; - *p_received_byte = APP_SLIP_END; - } - break; - - case APP_SLIP_ESC_ESC: - - if (m_rx_escape == true) - { - m_rx_escape = false; - *p_received_byte = APP_SLIP_ESC; - } - break; - - /* Normal character - decoding not needed*/ - default: - break; - } - - return true; -} - - -static void ser_phi_hci_rx_byte(uint8_t rx_byte) -{ - static bool rx_sync = false; - uint8_t received_byte = rx_byte; - static bool big_buff_in_use = false; - - /* Test received byte for SLIP packet start: 0xC0*/ - if (!rx_sync) - { - if (received_byte == APP_SLIP_END) - { - m_rx_index = 0; - rx_sync = true; - } - return; - } - - /* Additional check needed in case rx_sync flag was set by end of previous packet*/ - if ((m_rx_index) == 0 && (received_byte == APP_SLIP_END)) - { - return; - } - - /* Check if small (ACK) buffer is available*/ - if ((mp_small_buffer != NULL) && (big_buff_in_use == false)) - { - if (m_rx_index == 0) - { - mp_buffer = mp_small_buffer; - } - - /* Check if switch between small and big buffer is needed*/ - if (m_rx_index == sizeof (m_small_buffer) /*NEW!!!*/ && received_byte != APP_SLIP_END) - { - /* Check if big (PKT) buffer is available*/ - if (mp_big_buffer != NULL) - { - /* Switch to big buffer*/ - memcpy(m_big_buffer, m_small_buffer, sizeof (m_small_buffer)); - mp_buffer = m_big_buffer; - } - else - { - /* Small buffer is too small and big buffer not available - cannot continue reception*/ - rx_sync = false; - return; - } - } - - /* Check if big buffer is full */ - if ((m_rx_index >= PKT_SIZE) && (received_byte != APP_SLIP_END)) - { - /* Do not notify upper layer - the packet is too big and cannot be handled by slip */ - rx_sync = false; - return; - } - - /* Decode byte. Will return false when it is 0xC0 - end of packet*/ - if (slip_decode(&received_byte)) - { - /* Write Rx byte only if it is not escape char */ - if (!m_rx_escape) - { - mp_buffer[m_rx_index++] = received_byte; - } - } - else - { - /* Reset pointers to signalise buffers are locked waiting for upper layer */ - if (mp_buffer == mp_small_buffer) - { - mp_small_buffer = NULL; - } - else - { - mp_big_buffer = NULL; - } - /* Report packet reception end*/ - m_ser_phy_hci_slip_event.evt_type = - SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED; - m_ser_phy_hci_slip_event.evt_params.received_pkt.p_buffer = mp_buffer; - m_ser_phy_hci_slip_event.evt_params.received_pkt.num_of_bytes = m_rx_index; - m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event); - - rx_sync = false; - } - } - else if (mp_big_buffer != NULL) - { - big_buff_in_use = true; - mp_buffer = mp_big_buffer; - - /* Check if big buffer is full */ - if ((m_rx_index >= PKT_SIZE) && (received_byte != APP_SLIP_END)) - { - /* Do not notify upper layer - the packet is too big and cannot be handled by slip */ - rx_sync = false; - return; - } - - /* Decode byte*/ - if (slip_decode(&received_byte)) - { - /* Write Rx byte only if it is not escape char */ - if (!m_rx_escape) - { - mp_buffer[m_rx_index++] = received_byte; - } - } - else - { - /* Report packet reception end*/ - m_ser_phy_hci_slip_event.evt_type = - SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED; - m_ser_phy_hci_slip_event.evt_params.received_pkt.p_buffer = mp_buffer; - m_ser_phy_hci_slip_event.evt_params.received_pkt.num_of_bytes = m_rx_index; - m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event); - - rx_sync = false; - mp_big_buffer = NULL; - big_buff_in_use = false; - } - } - else - { - /* Both buffers are not available - cannot continue reception*/ - rx_sync = false; - return; - } -} - - -uint32_t ser_phy_hci_slip_rx_buf_free(uint8_t * p_buffer) -{ - uint32_t err_code = NRF_SUCCESS; - - if (p_buffer == NULL) - { - return NRF_ERROR_NULL; - } - else if (p_buffer == m_small_buffer) - { - /* Free small buffer*/ - if (mp_small_buffer == NULL) - { - mp_small_buffer = m_small_buffer; - } - else - { - err_code = NRF_ERROR_INVALID_STATE; - } - } - else if (p_buffer == m_big_buffer) - { - /* Free big buffer*/ - if (mp_big_buffer == NULL) - { - mp_big_buffer = m_big_buffer; - } - else - { - err_code = NRF_ERROR_INVALID_STATE; - } - } - - return err_code; -} - - -static void ser_phy_uart_evt_callback(app_uart_evt_t * uart_evt) -{ - if (uart_evt == NULL) - { - return; - } - - switch (uart_evt->evt_type) - { - case APP_UART_COMMUNICATION_ERROR: - - // Process error only if this is parity or overrun error. - // Break and framing error is always present when app side is not active - if (uart_evt->data.error_communication & - (UART_ERRORSRC_PARITY_Msk | UART_ERRORSRC_OVERRUN_Msk)) - { - callback_hw_error(uart_evt->data.error_communication); - } - break; - - case APP_UART_TX_EMPTY: - (void)ser_phy_hci_tx_byte(); - break; - - case APP_UART_DATA: - - // After first reception disable pulldown - it was only needed before start - // of the other side - if (!m_other_side_active) - { - m_other_side_active = true; - } - - m_rx_byte = uart_evt->data.value; - ser_phi_hci_rx_byte(m_rx_byte); - break; - - default: - APP_ERROR_CHECK(NRF_ERROR_INTERNAL); - break; - } -} - - -uint32_t ser_phy_hci_slip_open(ser_phy_hci_slip_event_handler_t events_handler) -{ - uint32_t err_code; - - if (events_handler == NULL) - { - return NRF_ERROR_NULL; - } - - // Check if function was not called before - if (m_ser_phy_hci_slip_event_handler != NULL) - { - return NRF_ERROR_INVALID_STATE; - } - - // Configure UART and register handler - // uart_evt_handler is used to handle events produced by low-level uart driver - APP_UART_INIT(&comm_params, ser_phy_uart_evt_callback, UART_IRQ_PRIORITY, err_code); - - mp_small_buffer = m_small_buffer; - mp_big_buffer = m_big_buffer; - - m_ser_phy_hci_slip_event_handler = events_handler; - - return err_code; -} - - -void ser_phy_hci_slip_close(void) -{ - m_ser_phy_hci_slip_event_handler = NULL; - (void)app_uart_close(); -} -
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nohci.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nohci.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nohci.c deleted file mode 100644 index 7c9a9e5..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nohci.c +++ /dev/null @@ -1,356 +0,0 @@ -/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -/**@file - * - * @defgroup ser_phy_spi_phy_driver_slave ser_phy_nrf51_spi_slave.c - * @{ - * @ingroup ser_phy_spi_phy_driver_slave - * - * @brief SPI_RAW PHY slave driver. - */ - -#include <stddef.h> -#include <string.h> - -#include "app_error.h" -#include "app_util.h" -#include "app_util_platform.h" -#include "app_timer.h" -#include "app_mailbox.h" -#include "ser_phy.h" -#include "ser_phy_hci.h" -#include "crc16.h" -#include "nrf_soc.h" - -#include "ser_phy_debug_comm.h" - -static bool m_flag_nohci_init = false; -static bool m_flag_expect_ack; -static bool m_flag_buffer_reqested = false; - -static uint16_t m_rx_packet_length; -static uint8_t * m_p_rx_packet; - -static uint16_t m_rx_pending_packet_length; -static uint8_t * m_p_rx_pending_packet; - -static uint16_t m_rx_allocated_packet_length; -static uint8_t * m_p_rx_allocated_packet; - -static uint8_t * m_p_tx_packet = NULL; -static uint16_t m_tx_packet_length; - -static ser_phy_events_handler_t m_ser_phy_callback = NULL; - -#define PKT_HDR_SIZE 4 /**< Packet header size in number of bytes. */ -#define PKT_CRC_SIZE 2 /**< Packet CRC size in number of bytes. */ - -static void ser_phy_nohci_assert(bool cond) -{ - APP_ERROR_CHECK_BOOL(cond); -} - - -static void ser_phy_event_callback(ser_phy_evt_t event) -{ - if (m_ser_phy_callback) - { - m_ser_phy_callback(event); - } -} - - -static void memory_request_callback(uint16_t size) -{ - ser_phy_evt_t event; - - DEBUG_EVT_HCI_PHY_EVT_BUF_REQUEST(0); - - event.evt_type = SER_PHY_EVT_RX_BUF_REQUEST; - event.evt_params.rx_buf_request.num_of_bytes = size; - ser_phy_event_callback(event); -} - - -static void packet_received_callback(uint8_t * pBuffer, uint16_t size) -{ - ser_phy_evt_t event; - - DEBUG_EVT_HCI_PHY_EVT_RX_PKT_RECEIVED(0); - - event.evt_type = SER_PHY_EVT_RX_PKT_RECEIVED; - event.evt_params.rx_pkt_received.num_of_bytes = size; - event.evt_params.rx_pkt_received.p_buffer = pBuffer; - ser_phy_event_callback(event); -} - - -static void packet_dropped_callback(void) -{ - ser_phy_evt_t event; - - DEBUG_EVT_HCI_PHY_EVT_RX_PKT_DROPPED(0); - - event.evt_type = SER_PHY_EVT_RX_PKT_DROPPED; - ser_phy_event_callback(event); -} - - -static void packet_transmitted_callback(void) -{ - ser_phy_evt_t event; - - DEBUG_EVT_HCI_PHY_EVT_TX_PKT_SENT(0); - - event.evt_type = SER_PHY_EVT_TX_PKT_SENT; - ser_phy_event_callback(event); -} - - -static void hci_slip_event_handler(ser_phy_hci_slip_evt_t * p_event) -{ - if ( p_event->evt_type == SER_PHY_HCI_SLIP_EVT_PKT_SENT ) - { - DEBUG_EVT_SLIP_PACKET_TXED(0); - - if (!m_flag_expect_ack) - { - m_p_tx_packet = NULL; - packet_transmitted_callback(); - } - else - { - ser_phy_nohci_assert(false); // packet was send as a ACK packet, callback should be with ACK_SENT - } - - } - else if ( p_event->evt_type == SER_PHY_HCI_SLIP_EVT_ACK_SENT ) - { - DEBUG_EVT_SLIP_ACK_TXED(0); - - if (m_flag_expect_ack) - { - m_p_tx_packet = NULL; - packet_transmitted_callback(); - } - else - { - ser_phy_nohci_assert(false); // packet was send as a normal packet, callback should be with PKT_SENT - } - - } - else if ( p_event->evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED ) - { - CRITICAL_REGION_ENTER(); - - if (m_p_rx_packet == NULL) - { - m_p_rx_packet = p_event->evt_params.received_pkt.p_buffer; - m_rx_packet_length = p_event->evt_params.received_pkt.num_of_bytes; - m_p_rx_allocated_packet = m_p_rx_packet; - m_rx_allocated_packet_length = m_rx_packet_length; - m_flag_buffer_reqested = true; - memory_request_callback(m_rx_allocated_packet_length); - } - else if (m_p_rx_pending_packet == NULL) - { - m_p_rx_pending_packet = p_event->evt_params.received_pkt.p_buffer; - m_rx_pending_packet_length = p_event->evt_params.received_pkt.num_of_bytes; - } - else - { - // both buffers are not released; this is fault - ser_phy_nohci_assert(false); - } - CRITICAL_REGION_EXIT(); - } - else - { - // no other callbacks are expected - ser_phy_nohci_assert(false); - } -} - - -/* ser_phy API function */ -void ser_phy_interrupts_enable(void) -{ - - NVIC_EnableIRQ(UART0_IRQn); - return; -} - - -/* ser_phy API function */ -void ser_phy_interrupts_disable(void) -{ - NVIC_DisableIRQ(UART0_IRQn); - return; -} - - -/* ser_phy API function */ -uint32_t ser_phy_rx_buf_set(uint8_t * p_buffer) -{ - uint32_t status = NRF_SUCCESS; - - if (m_flag_buffer_reqested) - { - m_flag_buffer_reqested = false; - - if (p_buffer) - { - memcpy(p_buffer, m_p_rx_allocated_packet, m_rx_allocated_packet_length); - packet_received_callback(p_buffer, m_rx_allocated_packet_length); - } - else - { - packet_dropped_callback(); - } - - CRITICAL_REGION_ENTER(); - - if (m_p_rx_allocated_packet == m_p_rx_packet && (m_p_rx_pending_packet == NULL)) - { - // packet is copied and there is no pending packet - (void) ser_phy_hci_slip_rx_buf_free(m_p_rx_packet); - m_p_rx_packet = NULL; - m_p_rx_allocated_packet = NULL; - } - else if (m_p_rx_allocated_packet == m_p_rx_packet && (m_p_rx_pending_packet != NULL)) - { - // there is a pending packet - request memory for it - m_p_rx_allocated_packet = m_p_rx_pending_packet; - m_rx_allocated_packet_length = m_rx_pending_packet_length; - m_flag_buffer_reqested = true; - } - else if (m_p_rx_allocated_packet == m_p_rx_pending_packet ) - { - // the pending packet was serviced - release both - m_p_rx_allocated_packet = NULL; - (void) ser_phy_hci_slip_rx_buf_free(m_p_rx_packet); - m_p_rx_packet = NULL; - (void) ser_phy_hci_slip_rx_buf_free(m_p_rx_pending_packet); - m_p_rx_pending_packet = NULL; - } - else - { - // no other calls are expected - ser_phy_nohci_assert(false); - } - CRITICAL_REGION_EXIT(); - - // request memory for a pending - if (m_p_rx_allocated_packet) - { - memory_request_callback(m_rx_allocated_packet_length); - } - } - else - { - status = NRF_ERROR_BUSY; - } - return status; -} - - -/* ser_phy API function */ -uint32_t ser_phy_tx_pkt_send(const uint8_t * p_buffer, uint16_t num_of_bytes) -{ - uint32_t status = NRF_SUCCESS; - uint32_t err_code; - - if ( p_buffer == NULL || num_of_bytes == 0) - { - return NRF_ERROR_NULL; - } - - if ( m_p_tx_packet == NULL) - { - m_tx_packet_length = num_of_bytes; - m_p_tx_packet = (uint8_t *)p_buffer; - - if (m_tx_packet_length <= PKT_HDR_SIZE + PKT_CRC_SIZE) - { - ser_phy_hci_pkt_params_t pkt; // all packets smaller than 6 goes as ACK - - m_flag_expect_ack = true; - pkt.p_buffer = (uint8_t *)m_p_tx_packet; - pkt.num_of_bytes = m_tx_packet_length; - DEBUG_EVT_SLIP_ACK_TX(0); - err_code = ser_phy_hci_slip_tx_pkt_send(&pkt, NULL, NULL); // this will look like ACK for slip - ser_phy_nohci_assert(err_code == NRF_SUCCESS); - } - else - { - ser_phy_hci_pkt_params_t header; // this is fake header - just first 4 bytes - ser_phy_hci_pkt_params_t crc; // this is fake header - just last 2 bytes - ser_phy_hci_pkt_params_t payload; // this is fake payload - all except for header and crc - - m_flag_expect_ack = false; - header.p_buffer = (uint8_t *)m_p_tx_packet; - header.num_of_bytes = PKT_HDR_SIZE; - crc.p_buffer = (uint8_t *)m_p_tx_packet + m_tx_packet_length - PKT_CRC_SIZE; - crc.num_of_bytes = PKT_CRC_SIZE; - payload.p_buffer = (uint8_t *)m_p_tx_packet + PKT_HDR_SIZE; - payload.num_of_bytes = m_tx_packet_length - PKT_HDR_SIZE - PKT_CRC_SIZE; - DEBUG_EVT_SLIP_PACKET_TX(0); - err_code = ser_phy_hci_slip_tx_pkt_send(&header, &payload, &crc); // this will look like normal packet for slip - ser_phy_nohci_assert(err_code == NRF_SUCCESS); - } - } - else - { - status = NRF_ERROR_BUSY; - } - - return status; -} - - -/* ser_phy API function */ -uint32_t ser_phy_open(ser_phy_events_handler_t events_handler) -{ - uint32_t err_code; - - if (m_flag_nohci_init) - { - return NRF_ERROR_INVALID_STATE; - } - - if (events_handler == NULL) - { - return NRF_ERROR_NULL; - } - err_code = ser_phy_hci_slip_open(hci_slip_event_handler); - - if (err_code != NRF_SUCCESS) - { - return err_code; - } - - m_ser_phy_callback = events_handler; - m_flag_nohci_init = true; - return NRF_SUCCESS; -} - - -/* ser_phy API function */ -void ser_phy_close(void) -{ - m_ser_phy_callback = NULL; - ser_phy_hci_slip_close(); - m_flag_nohci_init = false; -} - - http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_nrf_drv_spi.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_nrf_drv_spi.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_nrf_drv_spi.c deleted file mode 100644 index 6bae1a8..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_nrf_drv_spi.c +++ /dev/null @@ -1,784 +0,0 @@ -/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -/**@file - * - * @defgroup ser_phy_spi_phy_driver_master ser_phy_nrf51_spi_master.c - * @{ - * @ingroup ser_phy_spi_phy_driver_master - * - * @brief SPI_RAW PHY master driver. - */ - -#include <stdio.h> -#include "nrf_drv_gpiote.h" -#include "nrf_drv_spi.h" -#include "ser_phy.h" -#include "ser_config.h" -#include "app_util.h" -#include "app_util_platform.h" -#include "app_error.h" -#include "nrf_error.h" -#include "nrf_gpio.h" -#include "nrf_gpiote.h" -#include "boards.h" -#include "app_error.h" -#include "ser_phy_config_app_nrf51.h" -#include "ser_phy_debug_app.h" - -#define notUSE_PendSV - -#ifdef USE_PendSV - -#define SW_IRQn PendSV_IRQn -#define SW_IRQ_Handler() PendSV_Handler() -#define SET_Pend_SW_IRQ() SCB->ICSR = SCB->ICSR | SCB_ICSR_PENDSVSET_Msk //NVIC_SetPendingIRQ(PendSV_IRQn) - PendSV_IRQn is a negative - does not work with CMSIS - -#else - -#ifdef NRF51 -#define SW_IRQn SWI3_IRQn -#define SW_IRQ_Handler() SWI3_IRQHandler() -#define SET_Pend_SW_IRQ() NVIC_SetPendingIRQ(SWI3_IRQn) -#elif defined NRF52 -#define SW_IRQn SWI3_EGU3_IRQn -#define SW_IRQ_Handler() SWI3_EGU3_IRQHandler() -#define SET_Pend_SW_IRQ() NVIC_SetPendingIRQ(SWI3_EGU3_IRQn) -#endif /* NRF51 */ - -#endif /* USE_PendSV */ - -typedef enum -{ - SER_PHY_STATE_IDLE = 0, - SER_PHY_STATE_TX_HEADER, - SER_PHY_STATE_TX_WAIT_FOR_RDY, - SER_PHY_STATE_TX_PAYLOAD, - SER_PHY_STATE_RX_WAIT_FOR_RDY, - SER_PHY_STATE_TX_ZERO_HEADER, - SER_PHY_STATE_RX_HEADER, - SER_PHY_STATE_MEMORY_REQUEST, - SER_PHY_STATE_RX_PAYLOAD, - SER_PHY_STATE_DISABLED -} ser_phy_spi_master_state_t; - -typedef enum -{ - SER_PHY_EVT_GPIO_RDY = 0, - SER_PHY_EVT_GPIO_REQ, - SER_PHY_EVT_SPI_TRANSFER_DONE, - SER_PHY_EVT_TX_API_CALL, - SER_PHY_EVT_RX_API_CALL -} ser_phy_event_source_t; - -#define _static static - -_static uint8_t * mp_tx_buffer = NULL; -_static uint16_t m_tx_buf_len = 0; - -_static uint8_t * mp_rx_buffer = NULL; -_static uint16_t m_rx_buf_len = 0; -_static uint8_t m_frame_buffer[SER_PHY_SPI_MTU_SIZE]; -_static uint8_t m_header_buffer[SER_PHY_HEADER_SIZE] = { 0 }; - -_static uint16_t m_tx_packet_length = 0; -_static uint16_t m_accumulated_tx_packet_length = 0; -_static uint16_t m_current_tx_packet_length = 0; - -_static uint16_t m_rx_packet_length = 0; -_static uint16_t m_accumulated_rx_packet_length = 0; -_static uint16_t m_current_rx_packet_length = 0; - -_static volatile bool m_pend_req_flag = 0; -_static volatile bool m_pend_rdy_flag = 0; -_static volatile bool m_pend_xfer_flag = 0; -_static volatile bool m_pend_rx_api_flag = 0; -_static volatile bool m_pend_tx_api_flag = 0; - -_static volatile bool m_slave_ready_flag = false; -_static volatile bool m_slave_request_flag = false; - -_static ser_phy_events_handler_t m_callback_events_handler = NULL; -_static ser_phy_spi_master_state_t m_spi_master_state = SER_PHY_STATE_DISABLED; - -_static const nrf_drv_spi_t m_spi_master = SER_PHY_SPI_MASTER_INSTANCE; - -static void ser_phy_switch_state(ser_phy_event_source_t evt_src); - -static void spi_master_raw_assert(bool cond) -{ - APP_ERROR_CHECK_BOOL(cond); -} - -void SW_IRQ_Handler() -{ - if (m_pend_req_flag) - { - m_pend_req_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_REQUEST(0); - ser_phy_switch_state(SER_PHY_EVT_GPIO_REQ); - } - - if (m_pend_rdy_flag) - { - m_pend_rdy_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_READY(0); - ser_phy_switch_state(SER_PHY_EVT_GPIO_RDY); - } - - if (m_pend_xfer_flag) - { - m_pend_xfer_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_XFER_DONE(0); - ser_phy_switch_state(SER_PHY_EVT_SPI_TRANSFER_DONE); - } - - if (m_pend_rx_api_flag) - { - m_pend_rx_api_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_API_CALL(0); - ser_phy_switch_state(SER_PHY_EVT_RX_API_CALL); - } - - if (m_pend_tx_api_flag) - { - m_pend_tx_api_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_API_CALL(0); - ser_phy_switch_state(SER_PHY_EVT_TX_API_CALL); - } -} - -void ser_phy_spi_master_ready(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) -{ - if (nrf_gpio_pin_read(pin) == 0) - { - m_slave_ready_flag = true; - m_pend_rdy_flag = true; - } - else - { - m_slave_ready_flag = false; - } - - DEBUG_EVT_SPI_MASTER_RAW_READY_EDGE((uint32_t) !m_slave_ready_flag); - SET_Pend_SW_IRQ(); -} - -void ser_phy_spi_master_request(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) -{ - if (nrf_gpio_pin_read(pin) == 0) - { - m_slave_request_flag = true; - m_pend_req_flag = true; - } - else - { - m_slave_request_flag = false; - } - - DEBUG_EVT_SPI_MASTER_RAW_REQUEST_EDGE((uint32_t) !m_slave_request_flag); - SET_Pend_SW_IRQ(); -} - -/* Send event SER_PHY_EVT_TX_PKT_SENT */ -static __INLINE void callback_packet_sent() -{ - ser_phy_evt_t event; - - DEBUG_EVT_SPI_MASTER_PHY_TX_PKT_SENT(0); - - event.evt_type = SER_PHY_EVT_TX_PKT_SENT; - m_callback_events_handler(event); -} - -/* Send event SER_PHY_EVT_RX_PKT_DROPPED */ -static __INLINE void callback_packet_dropped() -{ - ser_phy_evt_t event; - - DEBUG_EVT_SPI_MASTER_PHY_RX_PKT_DROPPED(0); - - event.evt_type = SER_PHY_EVT_RX_PKT_DROPPED; - m_callback_events_handler(event); -} - -/* Send event SER_PHY_EVT_RX_PKT_RECEIVED */ -static __INLINE void callback_packet_received() -{ - ser_phy_evt_t event; - - DEBUG_EVT_SPI_MASTER_PHY_RX_PKT_RECEIVED(0); - - event.evt_type = SER_PHY_EVT_RX_PKT_RECEIVED; - event.evt_params.rx_pkt_received.p_buffer = mp_rx_buffer; - event.evt_params.rx_pkt_received.num_of_bytes = m_rx_buf_len; - m_callback_events_handler(event); -} - -/* Send event SER_PHY_EVT_RX_BUF_REQUEST */ -static __INLINE void callback_mem_request() -{ - ser_phy_evt_t event; - - DEBUG_EVT_SPI_MASTER_PHY_BUF_REQUEST(0); - - event.evt_type = SER_PHY_EVT_RX_BUF_REQUEST; - event.evt_params.rx_buf_request.num_of_bytes = m_rx_buf_len; - m_callback_events_handler(event); -} - -/* Release buffer */ -static __INLINE void buffer_release(uint8_t * * const pp_buffer, - uint16_t * const p_buf_len) -{ - *pp_buffer = NULL; - *p_buf_len = 0; -} - -/* Function computes current packet length */ -static uint16_t compute_current_packet_length(const uint16_t packet_length, - const uint16_t accumulated_packet_length) -{ - uint16_t current_packet_length = packet_length - accumulated_packet_length; - - if (current_packet_length > SER_PHY_SPI_MTU_SIZE) - { - current_packet_length = SER_PHY_SPI_MTU_SIZE; - } - - return current_packet_length; -} - -static __INLINE uint32_t header_send(const uint16_t length) -{ - uint8_t buf_len_size = uint16_encode(length, m_header_buffer); - - return nrf_drv_spi_transfer(&m_spi_master, m_header_buffer, buf_len_size, NULL, 0); -} - - -static __INLINE uint32_t frame_send() -{ - uint32_t err_code; - - m_current_tx_packet_length = compute_current_packet_length(m_tx_packet_length, - m_accumulated_tx_packet_length); - err_code = - nrf_drv_spi_transfer(&m_spi_master, - &mp_tx_buffer[m_accumulated_tx_packet_length], - m_current_tx_packet_length, - NULL, - 0); - m_accumulated_tx_packet_length += m_current_tx_packet_length; - return err_code; -} - -static __INLINE uint32_t header_get() -{ - return nrf_drv_spi_transfer(&m_spi_master, NULL, 0, m_header_buffer, SER_PHY_HEADER_SIZE); -} - -static __INLINE uint32_t frame_get() -{ - uint32_t err_code; - - m_current_rx_packet_length = compute_current_packet_length(m_rx_packet_length, - m_accumulated_rx_packet_length); - - if (mp_rx_buffer) - { - err_code = nrf_drv_spi_transfer(&m_spi_master, - NULL, - 0, - &(mp_rx_buffer[m_accumulated_rx_packet_length]), - m_current_rx_packet_length); - } - else - { - err_code = nrf_drv_spi_transfer(&m_spi_master, - NULL, - 0, - m_frame_buffer, - m_current_rx_packet_length); - } - return err_code; -} - - -/** - * \brief Master driver main state machine - * Executed only in the context of PendSV_Handler() - * For UML graph, please refer to SDK documentation -*/ -static void ser_phy_switch_state(ser_phy_event_source_t evt_src) -{ - uint32_t err_code = NRF_SUCCESS; - static bool m_wait_for_ready_flag = false; //local scheduling flag to defer RDY events - - switch (m_spi_master_state) - { - - case SER_PHY_STATE_IDLE: - - if (evt_src == SER_PHY_EVT_GPIO_REQ) - { - m_wait_for_ready_flag = false; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - } - else - { - m_spi_master_state = SER_PHY_STATE_RX_WAIT_FOR_RDY; - } - } - else if (evt_src == SER_PHY_EVT_TX_API_CALL) - { - spi_master_raw_assert(mp_tx_buffer != NULL); //api event with tx_buffer == NULL has no sense - m_wait_for_ready_flag = false; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_HEADER; - err_code = header_send(m_tx_buf_len); - } - else - { - m_spi_master_state = SER_PHY_STATE_TX_WAIT_FOR_RDY; - } - } - break; - - case SER_PHY_STATE_TX_WAIT_FOR_RDY: - - if (evt_src == SER_PHY_EVT_GPIO_RDY) - { - m_spi_master_state = SER_PHY_STATE_TX_HEADER; - err_code = header_send(m_tx_buf_len); - } - break; - - case SER_PHY_STATE_RX_WAIT_FOR_RDY: - - if (evt_src == SER_PHY_EVT_GPIO_RDY) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - - } - break; - - case SER_PHY_STATE_TX_HEADER: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - m_tx_packet_length = m_tx_buf_len; - m_accumulated_tx_packet_length = 0; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_PAYLOAD; - err_code = frame_send(); - - } - else - { - m_wait_for_ready_flag = true; - } - } - else if ((evt_src == SER_PHY_EVT_GPIO_RDY) && m_wait_for_ready_flag) - { - m_wait_for_ready_flag = false; - m_spi_master_state = SER_PHY_STATE_TX_PAYLOAD; - err_code = frame_send(); - } - - break; - - case SER_PHY_STATE_TX_PAYLOAD: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - if (m_accumulated_tx_packet_length < m_tx_packet_length) - { - if (m_slave_ready_flag) - { - err_code = frame_send(); - } - else - { - m_wait_for_ready_flag = true; - } - } - else - { - spi_master_raw_assert(m_accumulated_tx_packet_length == m_tx_packet_length); - buffer_release(&mp_tx_buffer, &m_tx_buf_len); - callback_packet_sent(); - if ( m_slave_request_flag) - { - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - } - else - { - m_spi_master_state = SER_PHY_STATE_RX_WAIT_FOR_RDY; - } - } - else - { - m_spi_master_state = SER_PHY_STATE_IDLE; //m_Tx_buffer is NULL - have to wait for API event - } - } - } - else if ((evt_src == SER_PHY_EVT_GPIO_RDY) && m_wait_for_ready_flag ) - { - m_wait_for_ready_flag = false; - err_code = frame_send(); - } - - break; - - case SER_PHY_STATE_TX_ZERO_HEADER: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_RX_HEADER; - err_code = header_get(); - } - else - { - m_wait_for_ready_flag = true; - } - } - else if ( (evt_src == SER_PHY_EVT_GPIO_RDY) && m_wait_for_ready_flag) - { - m_wait_for_ready_flag = false; - m_spi_master_state = SER_PHY_STATE_RX_HEADER; - err_code = header_get(); - } - break; - - case SER_PHY_STATE_RX_HEADER: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - m_spi_master_state = SER_PHY_STATE_MEMORY_REQUEST; - m_rx_buf_len = uint16_decode(m_header_buffer); - m_rx_packet_length = m_rx_buf_len; - callback_mem_request(); - - } - break; - - case SER_PHY_STATE_MEMORY_REQUEST: - - if (evt_src == SER_PHY_EVT_RX_API_CALL) - { - m_accumulated_rx_packet_length = 0; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_RX_PAYLOAD; - err_code = frame_get(); - } - else - { - m_wait_for_ready_flag = true; - } - } - else if ((evt_src == SER_PHY_EVT_GPIO_RDY) && m_wait_for_ready_flag) - { - m_wait_for_ready_flag = false; - m_spi_master_state = SER_PHY_STATE_RX_PAYLOAD; - err_code = frame_get(); - } - break; - - case SER_PHY_STATE_RX_PAYLOAD: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - m_accumulated_rx_packet_length += m_current_rx_packet_length; - - if (m_accumulated_rx_packet_length < m_rx_packet_length) - { - if (m_slave_ready_flag) - { - err_code = frame_get(); - } - else - { - m_wait_for_ready_flag = true; - } - } - else - { - spi_master_raw_assert(m_accumulated_rx_packet_length == m_rx_packet_length); - - if (mp_rx_buffer == NULL) - { - callback_packet_dropped(); - } - else - { - callback_packet_received(); - } - buffer_release(&mp_rx_buffer, &m_rx_buf_len); - if (mp_tx_buffer != NULL) //mp_tx_buffer !=NULL, this means that API_EVT was scheduled - { - if (m_slave_ready_flag ) - { - err_code = header_send(m_tx_buf_len); - m_spi_master_state = SER_PHY_STATE_TX_HEADER; - } - else - { - m_spi_master_state = SER_PHY_STATE_TX_WAIT_FOR_RDY; - } - } - else if (m_slave_request_flag) - { - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - } - else - { - m_spi_master_state = SER_PHY_STATE_RX_WAIT_FOR_RDY; - } - } - else - { - m_spi_master_state = SER_PHY_STATE_IDLE; - - } - } - - } - else if ( evt_src == SER_PHY_EVT_GPIO_RDY && m_wait_for_ready_flag) - { - m_wait_for_ready_flag = false; - err_code = frame_get(); - } - break; - - default: - break; - } - - if (err_code != NRF_SUCCESS) - { - (void)err_code; - } -} - -static void ser_phy_spi_master_event_handler(nrf_drv_spi_evt_t const * p_event) -{ - switch (p_event->type) - { - case NRF_DRV_SPI_EVENT_DONE: - - /* Switch state */ - m_pend_xfer_flag = true; - SET_Pend_SW_IRQ(); - - break; - - default: - break; - } -} - -static __INLINE void ser_phy_init_PendSV() -{ - NVIC_SetPriority(SW_IRQn, APP_IRQ_PRIORITY_MID); - NVIC_EnableIRQ(SW_IRQn); -} - -static __INLINE void ser_phy_init_gpio() -{ - nrf_gpio_cfg_input(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST, NRF_GPIO_PIN_PULLUP); - nrf_gpio_cfg_input(SER_PHY_SPI_MASTER_PIN_SLAVE_READY, NRF_GPIO_PIN_PULLUP); -} - -static __INLINE ret_code_t ser_phy_init_gpiote() -{ - if (!nrf_drv_gpiote_is_init()) - { - (void)nrf_drv_gpiote_init(); - } - m_slave_request_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST)); - m_slave_ready_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_READY)); - - nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true); - /* Enable pullup to ensure high state while connectivity device is reset */ - config.pull = NRF_GPIO_PIN_PULLUP; - ret_code_t err_code = nrf_drv_gpiote_in_init(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST, &config, ser_phy_spi_master_request); - if (err_code != NRF_SUCCESS) - { - return err_code; - } - nrf_drv_gpiote_in_event_enable(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST,true); - - err_code = nrf_drv_gpiote_in_init(SER_PHY_SPI_MASTER_PIN_SLAVE_READY, &config, ser_phy_spi_master_ready); - if (err_code != NRF_SUCCESS) - { - return err_code; - } - nrf_drv_gpiote_in_event_enable(SER_PHY_SPI_MASTER_PIN_SLAVE_READY,true); - - NVIC_ClearPendingIRQ(SW_IRQn); - - return NRF_SUCCESS; -} - -static __INLINE void ser_phy_deinit_gpiote() -{ - nrf_drv_gpiote_in_uninit(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST); - nrf_drv_gpiote_in_uninit(SER_PHY_SPI_MASTER_PIN_SLAVE_READY); - -// (void)app_gpiote_end_irq_event_handler_unregister(); -} - -/* ser_phy API function */ -uint32_t ser_phy_tx_pkt_send(const uint8_t * p_buffer, uint16_t num_of_bytes) -{ - if (p_buffer == NULL) - { - return NRF_ERROR_NULL; - } - - if (num_of_bytes == 0) - { - return NRF_ERROR_INVALID_PARAM; - } - - if (mp_tx_buffer != NULL) - { - return NRF_ERROR_BUSY; - } - - //ser_phy_interrupts_disable(); - CRITICAL_REGION_ENTER(); - mp_tx_buffer = (uint8_t *)p_buffer; - m_tx_buf_len = num_of_bytes; - m_pend_tx_api_flag = true; - SET_Pend_SW_IRQ(); - //ser_phy_interrupts_enable(); - CRITICAL_REGION_EXIT(); - - return NRF_SUCCESS; -} -/* ser_phy API function */ -uint32_t ser_phy_rx_buf_set(uint8_t * p_buffer) -{ - if (m_spi_master_state != SER_PHY_STATE_MEMORY_REQUEST) - { - return NRF_ERROR_INVALID_STATE; - } - - //ser_phy_interrupts_disable(); - CRITICAL_REGION_ENTER(); - mp_rx_buffer = p_buffer; - m_pend_rx_api_flag = true; - SET_Pend_SW_IRQ(); - //ser_phy_interrupts_enable(); - CRITICAL_REGION_EXIT(); - - return NRF_SUCCESS; -} - -/* ser_phy API function */ -uint32_t ser_phy_open(ser_phy_events_handler_t events_handler) -{ - if (m_spi_master_state != SER_PHY_STATE_DISABLED) - { - return NRF_ERROR_INVALID_STATE; - } - - if (events_handler == NULL) - { - return NRF_ERROR_NULL; - } - - uint32_t err_code = NRF_SUCCESS; - - m_spi_master_state = SER_PHY_STATE_IDLE; - m_callback_events_handler = events_handler; - nrf_drv_spi_config_t spi_master_config = { - .sck_pin = SER_PHY_SPI_MASTER_PIN_SCK, - .mosi_pin = SER_PHY_SPI_MASTER_PIN_MOSI, - .miso_pin = SER_PHY_SPI_MASTER_PIN_MISO, - .ss_pin = SER_PHY_SPI_MASTER_PIN_SLAVE_SELECT, - .irq_priority = APP_IRQ_PRIORITY_MID, - .orc = 0, - .frequency = SER_PHY_SPI_FREQUENCY, - .mode = NRF_DRV_SPI_MODE_0, - .bit_order = NRF_DRV_SPI_BIT_ORDER_LSB_FIRST, - }; - err_code = nrf_drv_spi_init(&m_spi_master, &spi_master_config, - ser_phy_spi_master_event_handler); - if (err_code != NRF_SUCCESS) - { - return err_code; - } - - ser_phy_init_gpio(); - err_code = ser_phy_init_gpiote(); - ser_phy_init_PendSV(); - return err_code; -} - -/* ser_phy API function */ -void ser_phy_close(void) -{ - m_spi_master_state = SER_PHY_STATE_DISABLED; - - m_callback_events_handler = NULL; - - buffer_release(&mp_tx_buffer, &m_tx_buf_len); - buffer_release(&mp_rx_buffer, &m_rx_buf_len); - - m_tx_packet_length = 0; - m_accumulated_tx_packet_length = 0; - m_current_tx_packet_length = 0; - - m_rx_packet_length = 0; - m_accumulated_rx_packet_length = 0; - m_current_rx_packet_length = 0; - - ser_phy_deinit_gpiote(); - nrf_drv_spi_uninit(&m_spi_master); -} - -/* ser_phy API function */ -/* only PendSV may interact with ser_phy layer, other interrupts are internal */ -void ser_phy_interrupts_enable(void) -{ - NVIC_EnableIRQ(SW_IRQn); -} - -/* ser_phy API function */ -void ser_phy_interrupts_disable(void) -{ - NVIC_DisableIRQ(SW_IRQn); -} - -/** @} */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_spi_5W_master.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_spi_5W_master.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_spi_5W_master.c deleted file mode 100644 index 2b103c5..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/serialization/common/transport/ser_phy/ser_phy_nrf51_spi_5W_master.c +++ /dev/null @@ -1,803 +0,0 @@ -/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -/**@file - * - * @defgroup ser_phy_spi_5W_phy_driver_master ser_phy_nrf51_spi_5W_master.c - * @{ - * @ingroup ser_phy_spi_5W_phy_driver_master - * - * @brief SPI_5W_RAW PHY master driver. - */ - -#include <stdio.h> -#include "app_util.h" -#include "app_util_platform.h" -#include "boards.h" -#include "nrf_error.h" -#include "nrf_gpio.h" -#include "nrf_drv_gpiote.h" -#include "ser_config.h" -#include "ser_config_5W_app.h" -#include "ser_phy.h" -#include "ser_phy_config_app_nrf51.h" -#include "spi_5W_master.h" -#include "ser_phy_debug_app.h" -#include "app_error.h" -#define notUSE_PendSV - -#ifdef USE_PendSV -#define SW_IRQn PendSV_IRQn -#define SW_IRQ_Handler() PendSV_Handler() -#define SET_Pend_SW_IRQ() SCB->ICSR = SCB->ICSR | SCB_ICSR_PENDSVSET_Msk //NVIC_SetPendingIRQ(PendSV_IRQn) - PendSV_IRQn is a negative - does not work with CMSIS -#else -#define SW_IRQn SWI3_IRQn -#define SW_IRQ_Handler() SWI3_IRQHandler() -#define SET_Pend_SW_IRQ() NVIC_SetPendingIRQ(SWI3_IRQn) -#endif - -#define SER_PHY_SPI_5W_MTU_SIZE SER_PHY_SPI_MTU_SIZE - -typedef enum -{ - SER_PHY_STATE_IDLE = 0, - SER_PHY_STATE_TX_HEADER, - SER_PHY_STATE_TX_WAIT_FOR_RDY, - SER_PHY_STATE_TX_PAYLOAD, - SER_PHY_STATE_RX_WAIT_FOR_RDY, - SER_PHY_STATE_TX_ZERO_HEADER, - SER_PHY_STATE_RX_HEADER, - SER_PHY_STATE_MEMORY_REQUEST, - SER_PHY_STATE_RX_PAYLOAD, - SER_PHY_STATE_DISABLED -} ser_phy_spi_master_state_t; - -typedef enum -{ - SER_PHY_EVT_GPIO_RDY = 0, - SER_PHY_EVT_GPIO_REQ, - SER_PHY_EVT_SPI_TRANSFER_DONE, - SER_PHY_EVT_TX_API_CALL, - SER_PHY_EVT_RX_API_CALL -} ser_phy_event_source_t; - -#define _static static - -_static uint8_t * mp_tx_buffer = NULL; -_static uint16_t m_tx_buf_len = 0; - -_static uint8_t * mp_rx_buffer = NULL; -_static uint16_t m_rx_buf_len = 0; -_static uint8_t m_recv_buffer[SER_PHY_SPI_5W_MTU_SIZE]; -_static uint8_t m_len_buffer[SER_PHY_HEADER_SIZE + 1] = { 0 }; //len is asymmetric for 5W, there is a 1 byte guard when receiving - -_static uint16_t m_tx_packet_length = 0; -_static uint16_t m_accumulated_tx_packet_length = 0; -_static uint16_t m_current_tx_packet_length = 0; - -_static uint16_t m_rx_packet_length = 0; -_static uint16_t m_accumulated_rx_packet_length = 0; -_static uint16_t m_current_rx_packet_length = 0; - -_static volatile bool m_pend_req_flag = 0; -_static volatile bool m_pend_rdy_flag = 0; -_static volatile bool m_pend_xfer_flag = 0; -_static volatile bool m_pend_rx_api_flag = 0; -_static volatile bool m_pend_tx_api_flag = 0; - -_static volatile bool m_slave_ready_flag = false; -_static volatile bool m_slave_request_flag = false; - - -_static ser_phy_events_handler_t m_callback_events_handler = NULL; -_static ser_phy_spi_master_state_t m_spi_master_state = SER_PHY_STATE_DISABLED; - -static void ser_phy_switch_state(ser_phy_event_source_t evt_src); - -static void spi_master_raw_assert(bool cond) -{ - APP_ERROR_CHECK_BOOL(cond); -} - -void SW_IRQ_Handler() -{ - if (m_pend_req_flag) - { - m_pend_req_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_REQUEST(0); - ser_phy_switch_state(SER_PHY_EVT_GPIO_REQ); - } - - if (m_pend_rdy_flag) - { - m_pend_rdy_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_READY(0); - ser_phy_switch_state(SER_PHY_EVT_GPIO_RDY); - } - - if (m_pend_xfer_flag) - { - m_pend_xfer_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_XFER_DONE(0); - ser_phy_switch_state(SER_PHY_EVT_SPI_TRANSFER_DONE); - } - - if (m_pend_rx_api_flag) - { - m_pend_rx_api_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_API_CALL(0); - ser_phy_switch_state(SER_PHY_EVT_RX_API_CALL); - } - - if (m_pend_tx_api_flag) - { - m_pend_tx_api_flag = false; - DEBUG_EVT_SPI_MASTER_RAW_API_CALL(0); - ser_phy_switch_state(SER_PHY_EVT_TX_API_CALL); - } - -} - -void ser_phy_spi_master_ready(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) -{ - -#ifdef _SPI_5W_ - //For 5W slave is considered to be always READY - m_slave_ready_flag = true; - m_pend_rdy_flag = false; - #else - if (nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_READY) == 0) - { - m_slave_ready_flag = true; - m_pend_rdy_flag = true; - } - else - { - m_slave_ready_flag = false; - } - - DEBUG_EVT_SPI_MASTER_RAW_READY_EDGE((uint32_t) !m_slave_ready_flag); -#endif - -} - -void ser_phy_spi_master_request(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) -{ - if (nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST) == 0) - { - m_slave_request_flag = true; - m_pend_req_flag = true; - } - else - { - m_slave_request_flag = false; - } - - DEBUG_EVT_SPI_MASTER_RAW_REQUEST_EDGE((uint32_t) !m_slave_request_flag); - SET_Pend_SW_IRQ(); -} - -/* Send event SER_PHY_EVT_TX_PKT_SENT */ -static __INLINE void callback_packet_sent() -{ - ser_phy_evt_t event; - - event.evt_type = SER_PHY_EVT_TX_PKT_SENT; - m_callback_events_handler(event); -} - -/* Send event SER_PHY_EVT_RX_PKT_DROPPED */ -static __INLINE void callback_packet_dropped() -{ - ser_phy_evt_t event; - - event.evt_type = SER_PHY_EVT_RX_PKT_DROPPED; - m_callback_events_handler(event); -} - -/* Send event SER_PHY_EVT_RX_PKT_RECEIVED */ -static __INLINE void callback_packet_received() -{ - ser_phy_evt_t event; - - event.evt_type = SER_PHY_EVT_RX_PKT_RECEIVED; - event.evt_params.rx_pkt_received.p_buffer = mp_rx_buffer; - event.evt_params.rx_pkt_received.num_of_bytes = m_rx_buf_len; - m_callback_events_handler(event); -} - -/* Send event SER_PHY_EVT_RX_BUF_REQUEST */ -static __INLINE void callback_mem_request() -{ - ser_phy_evt_t event; - - event.evt_type = SER_PHY_EVT_RX_BUF_REQUEST; - event.evt_params.rx_buf_request.num_of_bytes = m_rx_buf_len; - m_callback_events_handler(event); -} - -static __INLINE void copy_buff(uint8_t * const p_dest, uint8_t const * const p_src, uint16_t len) -{ - uint16_t index; - - for (index = 0; index < len; index++) - { - p_dest[index] = p_src[index]; - } - return; -} - -static __INLINE void buffer_release(uint8_t * * const pp_buffer, uint16_t * const p_buf_len) -{ - *pp_buffer = NULL; - *p_buf_len = 0; -} - -static uint16_t compute_current_packet_length(const uint16_t packet_length, - const uint16_t accumulated_packet_length) -{ - uint16_t current_packet_length = packet_length - accumulated_packet_length; - - if (current_packet_length > SER_PHY_SPI_5W_MTU_SIZE) - { - current_packet_length = SER_PHY_SPI_5W_MTU_SIZE; - } - - return current_packet_length; -} - -static __INLINE uint32_t header_send(const uint16_t length) -{ - uint16_t buf_len_size = uint16_encode(length, m_len_buffer); - - return spi_master_send_recv(SER_PHY_SPI_MASTER, m_len_buffer, buf_len_size, NULL, 0); -} - -static __INLINE uint32_t frame_send() -{ - uint32_t err_code; - - m_current_tx_packet_length = compute_current_packet_length(m_tx_packet_length, - m_accumulated_tx_packet_length); - err_code = - spi_master_send_recv(SER_PHY_SPI_MASTER, - &mp_tx_buffer[m_accumulated_tx_packet_length], - m_current_tx_packet_length, - NULL, - 0); - m_accumulated_tx_packet_length += m_current_tx_packet_length; - return err_code; -} - -static __INLINE uint32_t header_get() -{ - return spi_master_send_recv(SER_PHY_SPI_MASTER, NULL, 0, m_len_buffer, SER_PHY_HEADER_SIZE + 1); //add 0 byte guard when receiving -} - -static __INLINE uint32_t frame_get() -{ - uint32_t err_code; - - m_current_rx_packet_length = compute_current_packet_length(m_rx_packet_length, - m_accumulated_rx_packet_length); - - if (m_current_rx_packet_length < SER_PHY_SPI_5W_MTU_SIZE) - { - m_current_rx_packet_length++; //take into account guard byte when receiving - } - err_code = spi_master_send_recv(SER_PHY_SPI_MASTER, - NULL, - 0, - m_recv_buffer, - m_current_rx_packet_length); - return err_code; -} - -/** - * \brief Master driver main state machine - * Executed only in the context of PendSV_Handler() - * For UML graph, please refer to SDK documentation -*/ - -static void ser_phy_switch_state(ser_phy_event_source_t evt_src) -{ - uint32_t err_code = NRF_SUCCESS; - static bool m_waitForReadyFlag = false; //local scheduling flag to defer RDY events - - switch (m_spi_master_state) - { - - case SER_PHY_STATE_IDLE: - - if (evt_src == SER_PHY_EVT_GPIO_REQ) - { - m_waitForReadyFlag = false; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - } - else - { - m_spi_master_state = SER_PHY_STATE_RX_WAIT_FOR_RDY; - } - } - else if (evt_src == SER_PHY_EVT_TX_API_CALL) - { - spi_master_raw_assert(mp_tx_buffer != NULL); //api event with tx_buffer == NULL has no sense - m_waitForReadyFlag = false; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_HEADER; - err_code = header_send(m_tx_buf_len); - } - else - { - m_spi_master_state = SER_PHY_STATE_TX_WAIT_FOR_RDY; - } - } - break; - - case SER_PHY_STATE_TX_WAIT_FOR_RDY: - - if (evt_src == SER_PHY_EVT_GPIO_RDY) - { - m_spi_master_state = SER_PHY_STATE_TX_HEADER; - err_code = header_send(m_tx_buf_len); - } - break; - - case SER_PHY_STATE_RX_WAIT_FOR_RDY: - - if (evt_src == SER_PHY_EVT_GPIO_RDY) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - - } - break; - - case SER_PHY_STATE_TX_HEADER: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - m_tx_packet_length = m_tx_buf_len; - m_accumulated_tx_packet_length = 0; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_PAYLOAD; - err_code = frame_send(); - - } - else - { - m_waitForReadyFlag = true; - } - } - else if ((evt_src == SER_PHY_EVT_GPIO_RDY) && m_waitForReadyFlag) - { - m_waitForReadyFlag = false; - m_spi_master_state = SER_PHY_STATE_TX_PAYLOAD; - err_code = frame_send(); - } - - break; - - case SER_PHY_STATE_TX_PAYLOAD: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - if (m_accumulated_tx_packet_length < m_tx_packet_length) - { - if (m_slave_ready_flag) - { - err_code = frame_send(); - } - else - { - m_waitForReadyFlag = true; - } - } - else - { - spi_master_raw_assert(m_accumulated_tx_packet_length == m_tx_packet_length); - //Release TX buffer - buffer_release(&mp_tx_buffer, &m_tx_buf_len); - callback_packet_sent(); - - if ( m_slave_request_flag) - { - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - } - else - { - m_spi_master_state = SER_PHY_STATE_RX_WAIT_FOR_RDY; - } - } - else - { - m_spi_master_state = SER_PHY_STATE_IDLE; //m_Tx_buffer is NULL - have to wait for API event - } - } - } - else if ((evt_src == SER_PHY_EVT_GPIO_RDY) && m_waitForReadyFlag ) - { - m_waitForReadyFlag = false; - err_code = frame_send(); - } - - break; - - case SER_PHY_STATE_TX_ZERO_HEADER: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_RX_HEADER; - err_code = header_get(); - } - else - { - m_waitForReadyFlag = true; - } - } - else if ( (evt_src == SER_PHY_EVT_GPIO_RDY) && m_waitForReadyFlag) - { - m_waitForReadyFlag = false; - m_spi_master_state = SER_PHY_STATE_RX_HEADER; - err_code = header_get(); - } - break; - - case SER_PHY_STATE_RX_HEADER: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - m_spi_master_state = SER_PHY_STATE_MEMORY_REQUEST; - m_rx_buf_len = uint16_decode(&(m_len_buffer[1])); //skip guard when receiving - m_rx_packet_length = m_rx_buf_len; - callback_mem_request(); - } - break; - - case SER_PHY_STATE_MEMORY_REQUEST: - - if (evt_src == SER_PHY_EVT_RX_API_CALL) - { - m_accumulated_rx_packet_length = 0; - - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_RX_PAYLOAD; - err_code = frame_get(); - } - else - { - m_waitForReadyFlag = true; - } - } - else if ((evt_src == SER_PHY_EVT_GPIO_RDY) && m_waitForReadyFlag) - { - m_waitForReadyFlag = false; - m_spi_master_state = SER_PHY_STATE_RX_PAYLOAD; - err_code = frame_get(); - } - break; - - case SER_PHY_STATE_RX_PAYLOAD: - - if (evt_src == SER_PHY_EVT_SPI_TRANSFER_DONE) - { - if (mp_rx_buffer) - { - copy_buff(&(mp_rx_buffer[m_accumulated_rx_packet_length]), - &(m_recv_buffer[1]), - m_current_rx_packet_length - 1); //skip guard byte when receiving - } - m_accumulated_rx_packet_length += (m_current_rx_packet_length - 1); - - if (m_accumulated_rx_packet_length < m_rx_packet_length) - { - if (m_slave_ready_flag) - { - err_code = frame_get(); - } - else - { - m_waitForReadyFlag = true; - } - } - else - { - spi_master_raw_assert(m_accumulated_rx_packet_length == m_rx_packet_length); - - if (mp_rx_buffer == NULL) - { - callback_packet_dropped(); - } - else - { - callback_packet_received(); - } - //Release RX buffer - buffer_release(&mp_rx_buffer, &m_rx_buf_len); - - if ((mp_tx_buffer != NULL)) //mp_tx_buffer !=NULL, this means that API_EVT was scheduled - { - if (m_slave_ready_flag ) - { - err_code = header_send(m_tx_buf_len); - m_spi_master_state = SER_PHY_STATE_TX_HEADER; - } - else - { - m_spi_master_state = SER_PHY_STATE_TX_WAIT_FOR_RDY; - } - } - else if (m_slave_request_flag) - { - if (m_slave_ready_flag) - { - m_spi_master_state = SER_PHY_STATE_TX_ZERO_HEADER; - err_code = header_send(0); - } - else - { - m_spi_master_state = SER_PHY_STATE_RX_WAIT_FOR_RDY; - } - } - else - { - m_spi_master_state = SER_PHY_STATE_IDLE; - } - } - } - else if ( evt_src == SER_PHY_EVT_GPIO_RDY && m_waitForReadyFlag) - { - m_waitForReadyFlag = false; - err_code = frame_get(); - } - - - break; - - default: - break; - } - - - if (err_code != NRF_SUCCESS) - { - (void)err_code; - } -} - -/* SPI master event handler */ -static void ser_phy_spi_master_event_handler(spi_master_evt_t spi_master_evt) -{ - switch (spi_master_evt.type) - { - case SPI_MASTER_EVT_TRANSFER_COMPLETED: - - /* Switch state */ - m_pend_xfer_flag = true; - SET_Pend_SW_IRQ(); - - break; - - default: - break; - } -} - -/* Initialize GPIO */ -static __INLINE void ser_phy_init_gpio() -{ - nrf_gpio_cfg_input(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST, NRF_GPIO_PIN_PULLUP); - nrf_gpio_cfg_input(SER_PHY_SPI_MASTER_PIN_SLAVE_READY, NRF_GPIO_PIN_PULLUP); -} - -/* Initialize GPIO */ -static __INLINE void ser_phy_init_pendSV() -{ - NVIC_SetPriority(SW_IRQn, APP_IRQ_PRIORITY_MID); - NVIC_EnableIRQ(SW_IRQn); -} - -/* Initialize GPIOTE */ -static __INLINE void ser_phy_init_gpiote() -{ - if (!nrf_drv_gpiote_is_init()) - { - (void)nrf_drv_gpiote_init(); - } - m_slave_request_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST)); - - nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true); - (void)nrf_drv_gpiote_in_init(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST, &config, ser_phy_spi_master_request); - nrf_drv_gpiote_in_event_enable(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST,true); - m_slave_request_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST)); - - -#ifdef _SPI_5W_ - m_slave_ready_flag = true; - -#else - m_slave_ready_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_READY)); - (void)nrf_drv_gpiote_in_init(SER_PHY_SPI_MASTER_PIN_SLAVE_READY, &config, ser_phy_spi_master_ready); - - nrf_drv_gpiote_in_event_enable(SER_PHY_SPI_MASTER_PIN_SLAVE_READY,true); -#endif - - NVIC_ClearPendingIRQ(SW_IRQn); -} - -static __INLINE void ser_phy_deinit_gpiote() -{ - nrf_drv_gpiote_in_uninit(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST); -#ifndef _SPI_5W_ - nrf_drv_gpiote_in_uninit(SER_PHY_SPI_MASTER_PIN_SLAVE_READY); -#endif - - -} - -/* ser_phy API function */ -uint32_t ser_phy_tx_pkt_send(const uint8_t * p_buffer, uint16_t num_of_bytes) -{ - if (p_buffer == NULL) - { - return NRF_ERROR_NULL; - } - - if (num_of_bytes == 0) - { - return NRF_ERROR_INVALID_PARAM; - } - - if (mp_tx_buffer != NULL) - { - return NRF_ERROR_BUSY; - } - - ser_phy_interrupts_disable(); - mp_tx_buffer = (uint8_t *)p_buffer; - m_tx_buf_len = num_of_bytes; - m_pend_tx_api_flag = true; - SET_Pend_SW_IRQ(); - ser_phy_interrupts_enable(); - return NRF_SUCCESS; -} - -/* ser_phy API function */ -uint32_t ser_phy_rx_buf_set(uint8_t * p_buffer) -{ - if (m_spi_master_state != SER_PHY_STATE_MEMORY_REQUEST) - { - return NRF_ERROR_INVALID_STATE; - } - ser_phy_interrupts_disable(); - mp_rx_buffer = p_buffer; - m_pend_rx_api_flag = true; - SET_Pend_SW_IRQ(); - ser_phy_interrupts_enable(); - return NRF_SUCCESS; -} - -/* ser_phy API function */ -uint32_t ser_phy_open(ser_phy_events_handler_t events_handler) -{ - - if (m_spi_master_state != SER_PHY_STATE_DISABLED) - { - return NRF_ERROR_INVALID_STATE; - } - - if (events_handler == NULL) - { - return NRF_ERROR_NULL; - } - - uint32_t err_code = NRF_SUCCESS; - ser_phy_init_gpio(); - m_spi_master_state = SER_PHY_STATE_IDLE; - m_callback_events_handler = events_handler; - ser_phy_init_gpiote(); - - /* Configure SPI Master driver */ - spi_master_config_t spi_master_config; - spi_master_config.SPI_Freq = SPI_FREQUENCY_FREQUENCY_M1; - spi_master_config.SPI_Pin_SCK = SER_PHY_SPI_MASTER_PIN_SCK; - spi_master_config.SPI_Pin_MISO = SER_PHY_SPI_MASTER_PIN_MISO; - spi_master_config.SPI_Pin_MOSI = SER_PHY_SPI_MASTER_PIN_MOSI; - spi_master_config.SPI_Pin_SS = SER_PHY_SPI_MASTER_PIN_SLAVE_SELECT; - spi_master_config.SPI_ORDER = SPI_CONFIG_ORDER_LsbFirst; - spi_master_config.SPI_CPOL = SPI_CONFIG_CPOL_ActiveHigh; - spi_master_config.SPI_CPHA = SPI_CONFIG_CPHA_Leading; - - err_code = spi_master_open(SER_PHY_SPI_MASTER, &spi_master_config); - - if (err_code != NRF_SUCCESS) - { - return err_code; - } -#ifdef _SPI_5W_ - spi_5W_master_evt_handler_reg(SER_PHY_SPI_MASTER, ser_phy_spi_master_event_handler); -#else - spi_master_evt_handler_reg(SER_PHY_SPI_MASTER, ser_phy_spi_master_event_handler); -#endif - ser_phy_init_pendSV(); - - return err_code; -} - -/* ser_phy API function */ -void ser_phy_close(void) -{ - m_spi_master_state = SER_PHY_STATE_DISABLED; - - m_callback_events_handler = NULL; - - buffer_release(&mp_tx_buffer, &m_tx_buf_len); - buffer_release(&mp_rx_buffer, &m_rx_buf_len); - m_tx_packet_length = 0; - m_accumulated_tx_packet_length = 0; - m_current_tx_packet_length = 0; - m_rx_packet_length = 0; - m_accumulated_rx_packet_length = 0; - m_current_rx_packet_length = 0; - ser_phy_deinit_gpiote(); - spi_master_close(SER_PHY_SPI_MASTER); -} - -/* ser_phy API function */ -void ser_phy_interrupts_enable(void) -{ - NVIC_EnableIRQ(SW_IRQn); -} - -/* ser_phy API function */ -void ser_phy_interrupts_disable(void) -{ - NVIC_DisableIRQ(SW_IRQn); -} - - -#ifdef SER_PHY_DEBUG_APP_ENABLE - -static spi_master_raw_callback_t m_spi_master_raw_evt_callback; - -void debug_evt(spi_master_raw_evt_type_t evt, uint32_t data) -{ - if (m_spi_master_raw_evt_callback) - { - spi_master_raw_evt_t e; - e.evt = evt; - e.data = data; - m_spi_master_raw_evt_callback(e); - } -} - -void debug_init(spi_master_raw_callback_t spi_master_raw_evt_callback) -{ - m_spi_master_raw_evt_callback = spi_master_raw_evt_callback; -} - -#endif -/** @} */
