app_states_t app_init(void) { if(usb_get_state() == USB_REM_WU_ENABLE) { return APP_SUSP_WE; } if(usb_get_state() == USB_REM_WU_DISABLE) { return APP_SUSP_WD; } /* I3FR = 1; // rising edge SPI ready detect INTEXP = 0x01; //Slave SPI Interrupt SPI = 1; // Enable SPI Interrupt */ hal_nrf_enable_ack_payload(1); hal_nrf_enable_dynamic_payload(1); hal_nrf_setup_dynamic_payload(1 << 0); // Set up PIPE 0 to handle dynamic lengths hal_nrf_set_operation_mode(HAL_NRF_PRX); // Configure radio as primary receiver (PTX) //hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, 30); // Set payload width to 30 bytes hal_nrf_set_power_mode(HAL_NRF_PWR_UP); // Power up radio CE_HIGH(); // Enable receiver return APP_NORMAL; }
int usb_is_attached(void) { #ifndef USBHW_HAVE_ATTACH return 1; #else return usb_get_state()!=USB_STATE_DETACHED; #endif }
int usb_set_config(int cfn) { if (usb_get_state()!=USB_STATE_ADDRESS&&usb_get_state()!=USB_STATE_CONFIGURED) return -1; if (!cfn) { flags.config=0; usb_set_state(USB_STATE_ADDRESS); } else { if (usb_get_state()==USB_STATE_CONFIGURED) { if (flags.config==cfn) return 0; } if (!usb_have_config(cfn)) return -1; flags.config=cfn; if (activate_endpoints(cfn)) return -1; usb_set_state(USB_STATE_CONFIGURED); } return 0; }
void cdc_process(void) { /* See if USB is in usable state. */ if (usb_get_state() == USBST_CONFIGURED) { /* If endpoint is not busy, and receive buffer is empty */ if (!usb_ep_is_busy(CDC_RX_EP_NO) && (rx_length <= rx_ndx)) { /* Read out error status of endpoint. The reception may be aborted due to a status change on the USB (disconnect, sleep, etc..) or due to an error (CRC, bit stuffing, etc...). In both case wee need to restart reception if possible. */ switch(usb_ep_error(CDC_RX_EP_NO)) { case USBEPERR_NONE: /* Reception finished with no error. */ /* If received bytes are not yet handled. */ if (rx_length == 0) { /* Read out number of received bytes. This will make us to return received characters for the next call. Note: the transfer may contain 0 characters, so we return the first character only when the next call is made. */ rx_length=(hcc_u8)usb_get_done(CDC_RX_EP_NO); rx_ndx=0; /* If we did not received any data, then we need to start a new receive. */ if (rx_length) { break; } } case USBEPERR_PROTOCOL_ERROR: /* restart the reception */ rx_length=0; usb_receive(CDC_RX_EP_NO, (void *) 0, (void *) rx_buffer, sizeof(rx_buffer)); break; case USBEPERR_HOST_ABORT: /* This error can only be detected if error happens after execution of thefirst if in this function and before the switch. This is unlikely to happen. On the other hand this error meand usb has been disconnected or put to low power mode and thus we may safely ignore it. */ break; case USBEPERR_USER_ABORT: case USBEPERR_TO_MANY_DATA: default: /* Upps! unexpected error. Stop here. */ CMX_ASSERT(0); } } /* If tx buffer is not empty, start transmission. */ if (!usb_ep_is_busy(CDC_TX_EP_NO) && (tx_ndx != 0)) { /* Check the status of the next transfer. */ switch (usb_ep_error(CDC_TX_EP_NO)) { case USBEPERR_PROTOCOL_ERROR: /* Ignore error, send next chunk. */ case USBEPERR_HOST_ABORT: case USBEPERR_NONE: /* Finished with no error. */ /* Start sending next chunk. */ usb_send(CDC_TX_EP_NO, (void *) 0, (void *) cur_tx_buffer, tx_ndx, tx_ndx); /* Switch buffer. */ tx_ndx=0; cur_tx_buffer = (hcc_u8*)((cur_tx_buffer == (hcc_u8*)tx_buffer1) ? tx_buffer2 : tx_buffer1); break; case USBEPERR_USER_ABORT: case USBEPERR_TO_MANY_DATA: default: /* Upps! unexpected error. Stop here. */ CMX_ASSERT(0); } } } }
static app_states_t app_normal(void) { usb_state_t data temp_state = usb_get_state(); uint8_t i = 0; uint8_t xdata *buf_ptr; uint8_t xdata *bc_ptr; if(temp_state == USB_REM_WU_ENABLE) { return APP_SUSP_WE; } if(temp_state == USB_REM_WU_DISABLE) { return APP_SUSP_WD; } if(ep2_rx) // process data we got over USB { ep2_rx = false; hal_nrf_write_ack_payload(0, ackPayload, SIZEOFPROG); // hal_usb_send_data(2, "\x00\x00", 2); } if(blockNeedsSending) // process data from Wireless { blockNeedsSending = 0; /* hal_usb_send_data(1, payload[0], 31); */ P0_5=1; // Calculate the buffer pointer and byte count pointer buf_ptr = CALCULATE_BUF_IN_PTR(2); bc_ptr = CALCULATE_BC_IN_PTR(2); if( 0 == block ) { // buf_ptr = buf_ptr + 31; } // Copy the data into the USB controller for( i = 0; i < PKTLENGTH; ++i ) { buf_ptr[i] = payload[block][i]; } // buf_ptr[30] = block + (0xAA); *bc_ptr = PKTLENGTH; // Set the number of bytes we want to send to USB-host. This also trigger sending of data to USB-host. if(block) { block = 0; } else { block = 1; } P0_5=0; /********/ /* // Write payload to radio USB FIFO hal_usb_send_data(1, payload[(block^0x01)], 31); blockNeedsSending = 0;*/ } return APP_NORMAL; }