int usb_upld_hook(void* iface, char *packet, int bRequest, int something){ /* This hooks the USB Device Firmware Update upload function, so that we can transfer data out of the radio without asking the host's permission. */ //Really ought to do this with a struct instead of casts. //This is 1 if we control it, 0 or >=2 if the old code should take it. uint16_t blockadr = *(short*)(packet+2); //Seems to be forced to zero. //uint16_t index = *(short*)(packet+4); //We have to send this much. uint16_t length= *(short*)(packet+6); //This is the target address of the Application's DFU engine. char *dfutargetadr=(char*) *((int*)0x2000112c); //2.032 DFU target adr. /* The DFU protocol specifies reads from block 0 as something special, but it doesn't say way to do about an address of 1. Shall I take it over? Don't mind if I do! */ if(blockadr==1){ //Some special addresses need help before the transfer. if(dfutargetadr== dmesg_start){ /* We can't send the DMESG buffer itself, because it's in the tightly coupled memory, so we'll memcpy() it to a buffer in SRAM and then transmit it. */ memcpy(dmesg_tx_buf,dmesg_start,DMESG_SIZE); //Wipe out the old buffer and set the pointer to the beginning. for(int i=0;i<DMESG_SIZE;i++) dmesg_start[i]=0; dmesg_wcurs=0; //Send the doubled buffer and return. usb_send_packet(iface, //USB interface structure. dmesg_tx_buf, //Send the copy, not the original. length); //Length must match. return 0; } //Send the data from internal memory and return. usb_send_packet(iface, //USB interface structure. dfutargetadr, length); //Length must match. return 0; } //Return control the original function. return usb_upld_handle(iface, packet, bRequest, something); }
int iqrf_raw_write_unlocked(iqrf_t *iqrf, const unsigned char *buff, int tx_len) { int rv; usb_set_tx_len(iqrf->dev, tx_len); usb_write_tx_buff(iqrf->dev, buff, tx_len); rv = usb_send_packet(iqrf->dev); return rv; }
/* write and read data to/from endpoint */ int usb_send_receive_packet(device_t *dev) { int ret_val = 0; ret_val = usb_send_packet(dev); if (ret_val < 0) return -1; // if (!ret_val) ret_val = usb_retrieve_packet(dev); return ret_val; }
void main(void) { int i = 0; // local counter int total_pkt_count = 1; int addr_width = 5; int customized_plen = 0; int pipe_num = 6; epl_rf_en_auto_ack_t auto_ack = 0; //int mode = 1; // 1 for sender mode, 2 for dumper mode // new params unsigned char pipe_source; // used to store pipe_source number unsigned char ACKbuf[] = "ACK"; unsigned char temp_buf[34]; // used for dumper to get RF packets from RX FIFO unsigned char temp_addr[5]; unsigned char data_length = 0; unsigned char dynpd_pipe; unsigned char addr_buf[5]; // set pin direction P0EXP = 0x00; P0ALT = 0x00; P0DIR = 0x00; // uart init epl_uart_init(UART_BAUD_57K6); //init usb connection usb_init(); // Initialize USB EA = 1; // Enable global IRQ //Start RF tx mode epl_rf_en_quick_init(cfg); //Clear TX FIFO hal_nrf_write_reg(FLUSH_TX, 0); hal_nrf_write_reg(FLUSH_RX, 0); hal_nrf_lock_unlock(); hal_nrf_enable_dynamic_pl(); LED_Blink(20); epl_uart_putstr("start!"); while (1) { usb_recv_packet(); switch (ubuf[1]) { case EPL_SENDER_MODE: customized_plen = 0; for (i = 0; i < PLOAD_LEN; i++) packet[i] = i + 9; hal_nrf_close_pipe(HAL_NRF_PIPE1); hal_nrf_close_pipe(HAL_NRF_PIPE2); hal_nrf_close_pipe(HAL_NRF_PIPE3); hal_nrf_close_pipe(HAL_NRF_PIPE4); hal_nrf_close_pipe(HAL_NRF_PIPE5); break; case EPL_DUMPER_MODE: hal_nrf_close_pipe(HAL_NRF_PIPE1); hal_nrf_close_pipe(HAL_NRF_PIPE2); hal_nrf_close_pipe(HAL_NRF_PIPE3); hal_nrf_close_pipe(HAL_NRF_PIPE4); hal_nrf_close_pipe(HAL_NRF_PIPE5); break; case EPL_OUTPUT_POWER: //Host:set_output_power hal_nrf_set_output_power(ubuf[2]); epl_uart_putstr("EPL_OUTPUT_POWER\n"); usb_send_packet(ACKbuf, 3); epl_uart_putstr("EPL_OUTPUT_POWER END\n"); break; case EPL_CHANNEL: hal_nrf_set_rf_channel(ubuf[2]); usb_send_packet(ACKbuf, 3); break; case EPL_DATARATE: hal_nrf_set_datarate(ubuf[2]); usb_send_packet(ACKbuf, 3); break; case EPL_ADDR_WIDTH: addr_width = (int) ubuf[2]; hal_nrf_set_address_width(ubuf[2]); usb_send_packet(ACKbuf, 3); break; case EPL_AUTOACK_P0: auto_ack = ubuf[2]; usb_send_packet(ACKbuf, 3); break; case EPL_AUTOACK_P1: auto_ack = ubuf[2]; usb_send_packet(ACKbuf, 3); break; case EPL_AUTOACK_P2: auto_ack = ubuf[2]; usb_send_packet(ACKbuf, 3); break; case EPL_AUTOACK_P3: auto_ack = ubuf[2]; usb_send_packet(ACKbuf, 3); break; case EPL_AUTOACK_P4: auto_ack = ubuf[2]; usb_send_packet(ACKbuf, 3); break; case EPL_AUTOACK_P5: auto_ack = ubuf[2]; usb_send_packet(ACKbuf, 3); break; case EPL_DATA_LENGTH_P0: data_length = (int) ubuf[2]; epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE0, temp_addr, data_length, auto_ack); usb_send_packet(ACKbuf, 3); break; case EPL_DATA_LENGTH_P1: data_length = (int) ubuf[2]; epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE1, temp_addr, data_length, auto_ack); usb_send_packet(ACKbuf, 3); break; case EPL_DATA_LENGTH_P2: data_length = (int) ubuf[2]; epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE2, temp_addr, data_length, auto_ack); usb_send_packet(ACKbuf, 3); break; case EPL_DATA_LENGTH_P3: data_length = (int) ubuf[2]; epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE3, temp_addr, data_length, auto_ack); usb_send_packet(ACKbuf, 3); break; case EPL_DATA_LENGTH_P4: data_length = (int) ubuf[2]; epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE4, temp_addr, data_length, auto_ack); usb_send_packet(ACKbuf, 3); break; case EPL_DATA_LENGTH_P5: data_length = (int) ubuf[2]; epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE5, temp_addr, data_length, auto_ack); usb_send_packet(ACKbuf, 3); break; case EPL_CRC_MODE: if (ubuf[2] == 0) hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF); else if (ubuf[2] == 2) hal_nrf_set_crc_mode(HAL_NRF_CRC_8BIT); else if (ubuf[2] == 3) hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT); else ; usb_send_packet(ACKbuf, 3); break; case EPL_RX_ADDR_P0: for (i = 0; i < addr_width; i++) { temp_addr[i] = ubuf[i + 2]; } hal_nrf_set_address(HAL_NRF_PIPE0, temp_addr); epl_rf_en_set_dst_addr(temp_addr); usb_send_packet(ACKbuf, 3); break; case EPL_RX_ADDR_P1: for (i = 0; i < addr_width; i++) { temp_addr[i] = ubuf[i + 2]; } usb_send_packet(ACKbuf, 3); break; case EPL_RX_ADDR_P2: for (i = 0; i < addr_width; i++) { temp_addr[i] = ubuf[i + 2]; } usb_send_packet(ACKbuf, 3); break; case EPL_RX_ADDR_P3: for (i = 0; i < addr_width; i++) { temp_addr[i] = ubuf[i + 2]; } usb_send_packet(ACKbuf, 3); break; case EPL_RX_ADDR_P4: for (i = 0; i < addr_width; i++) { temp_addr[i] = ubuf[i + 2]; } usb_send_packet(ACKbuf, 3); break; case EPL_RX_ADDR_P5: for (i = 0; i < addr_width; i++) { temp_addr[i] = ubuf[i + 2]; } usb_send_packet(ACKbuf, 3); break; case EPL_USER_PLOAD: if (ubuf[2] == USRS_PLOAD) { customized_plen = (int) ubuf[3]; for (i = 0; i < customized_plen; i++) { packet[i] = ubuf[i + 4]; } } else { customized_plen = 0; for (i = 0; i < PLOAD_LEN; i++) { packet[i] = i + 9; } } usb_send_packet(ACKbuf, 3); break; case EPL_NEW_COUNTER: total_pkt_count = 1; usb_send_packet(ACKbuf, 3); break; /*20110221 celine*/ case EPL_DYNAMIC_PD: dynpd_pipe = (int)ubuf[2]; if ((int)ubuf[3] == 01){ //hal_nrf_setup_dyn_pl(dynpd_pipe); hal_nrf_write_reg(DYNPD, (1<<dynpd_pipe) | hal_nrf_read_reg(DYNPD)); } else { //hal_nrf_lock_unlock(); //hal_nrf_enable_dynamic_pl(); hal_nrf_write_reg(DYNPD, ~(1<<dynpd_pipe) & hal_nrf_read_reg(DYNPD)); } usb_send_packet(ACKbuf, 3); break; /**/ case EPL_RUN_SENDER: epl_rf_en_enter_tx_mode(); // clear Tx irq hal_nrf_clear_irq_flag(HAL_NRF_TX_DS); hal_nrf_clear_irq_flag(HAL_NRF_MAX_RT); if (ubuf[2] == AUTO_PLOAD) { epl_uart_putstr("\nauto pload\r\n"); packet[0] = total_pkt_count++; epl_rf_en_send(packet, data_length); } else { epl_uart_putstr("\nusrs pload\r\n"); epl_rf_en_send(packet, customized_plen); } LED_Blink(10); array_cp(temp_buf, ACKbuf, 3); temp_buf[3] = hal_nrf_read_reg(OBSERVE_TX) & 0x0F; usb_send_packet(temp_buf, 4); epl_rf_en_enter_rx_mode(); break; case EPL_RUN_DUMPER: hal_nrf_clear_irq_flag(HAL_NRF_RX_DR); hal_nrf_flush_rx(); epl_rf_en_enter_rx_mode(); while (1) { if (ubuf[1] == 0xf5) { // Host wants to terminate epl_uart_putstr("Terminate !\r\n"); break; }else if (hal_nrf_rx_fifo_empty() == 0) { // Rx_fifo is not empty LED0_Toggle(); pipe_source = hal_nrf_get_rx_data_source(); hal_nrf_read_rx_pload(temp_buf); // pending the data source on last byte temp_buf[32] = pipe_source; if(hal_nrf_read_reg(DYNPD)>>(int)pipe_source) temp_buf[33] = hal_nrf_read_reg(RD_RX_PLOAD_W); else temp_buf[33] = hal_nrf_read_reg(RX_PW_P0+pipe_source); // epl_uart_putstr("temp_buf[33] = "); // epl_uart_puthex(temp_buf[33]); // epl_uart_putstr("\r\n"); usb_send_packet(temp_buf, 34); if((hal_nrf_read_reg(STATUS))&0x10){ hal_nrf_write_reg(FLUSH_TX, 0); } LED0_Toggle(); } } break; case EPL_SHOW_CONFIG: epl_uart_putstr("\r\n0. CONFIG = "); epl_uart_puthex(hal_nrf_read_reg(CONFIG)); epl_uart_putstr("\r\n1. RF_CH = "); epl_uart_puthex(hal_nrf_read_reg(RF_CH)); epl_uart_putstr("\r\n2. EN_AA = "); epl_uart_puthex(hal_nrf_read_reg(EN_AA)); epl_uart_putstr("\r\n3. EN_RXADDR = "); epl_uart_puthex(hal_nrf_read_reg(EN_RXADDR)); epl_uart_putstr("\r\n4. TX_ADDR = "); hal_nrf_read_multibyte_reg(HAL_NRF_TX, addr_buf); epl_uart_puthex(addr_buf[0]); epl_uart_puthex(addr_buf[1]); epl_uart_puthex(addr_buf[2]); epl_uart_puthex(addr_buf[3]); epl_uart_puthex(addr_buf[4]); epl_uart_putstr("\r\n4. RX_ADDR_PO = "); hal_nrf_read_multibyte_reg(HAL_NRF_PIPE0, addr_buf); epl_uart_puthex(addr_buf[0]); epl_uart_puthex(addr_buf[1]); epl_uart_puthex(addr_buf[2]); epl_uart_puthex(addr_buf[3]); epl_uart_puthex(addr_buf[4]); epl_uart_putstr("\r\n RX_ADDR_P1 = "); hal_nrf_read_multibyte_reg(HAL_NRF_PIPE1, addr_buf); epl_uart_puthex(addr_buf[0]); epl_uart_puthex(addr_buf[1]); epl_uart_puthex(addr_buf[2]); epl_uart_puthex(addr_buf[3]); epl_uart_puthex(addr_buf[4]); epl_uart_putstr("\r\n RX_ADDR_P2 = "); epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P2)); epl_uart_putstr("\r\n RX_ADDR_P3 = "); epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P3)); epl_uart_putstr("\r\n RX_ADDR_P4 = "); epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P4)); epl_uart_putstr("\r\n RX_ADDR_P5 = "); epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P5)); epl_uart_putstr("\r\n5. RX_PW_P0 = "); epl_uart_puthex(hal_nrf_read_reg(RX_PW_P0)); epl_uart_putstr("\r\n RX_PW_P1 = "); epl_uart_puthex(hal_nrf_read_reg(RX_PW_P1)); epl_uart_putstr("\r\n RX_PW_P2 = "); epl_uart_puthex(hal_nrf_read_reg(RX_PW_P2)); epl_uart_putstr("\r\n RX_PW_P3 = "); epl_uart_puthex(hal_nrf_read_reg(RX_PW_P3)); epl_uart_putstr("\r\n RX_PW_P4 = "); epl_uart_puthex(hal_nrf_read_reg(RX_PW_P4)); epl_uart_putstr("\r\n RX_PW_P5 = "); epl_uart_puthex(hal_nrf_read_reg(RX_PW_P5)); epl_uart_putstr("\r\n6. RF_SETUP = "); epl_uart_puthex(hal_nrf_read_reg(RF_SETUP)); epl_uart_putstr("\r\n7. STATUS = "); epl_uart_puthex(hal_nrf_read_reg(STATUS)); epl_uart_putstr("\r\n8 .DYNPD = "); epl_uart_puthex(hal_nrf_read_reg(DYNPD)); epl_uart_putstr("\r\n9. FEATURE = "); epl_uart_puthex(hal_nrf_read_reg(FEATURE)); default: break; }// end switch case
static void handle_data(void){ #ifdef USE_STTERM unsigned char tmp_c; #endif uint32_t tmp=0; char crc=0; #ifdef USE_STTERM if(stlinky_rx_ready()){ scanf("%c", &tmp_c); INPUT[k] = tmp_c; gpio_toggle(GPIOD, GPIO15); #else if(usart_has_data()){ INPUT[k] = usart_read(); #endif k++; if(k==1 && INPUT[0]!=0x53){ // 'S' #ifdef DEBUG printf("ERROR AT START-BYTE %d\r\n",INPUT[0]); #endif k=0; } if(k==10 && INPUT[9]!=0x45){ // 'E' #ifdef DEBUG printf("ERROR AT END-BYTE %d\r\n",INPUT[8]); #endif k=0; } if(k==11){ #ifdef DEBUG printf("Package: "); for(tmp=0;tmp<10;tmp++) printf("%d ",INPUT[tmp]); printf("\n"); #endif for(tmp=0;tmp<8;tmp++) KEYS[tmp] = INPUT[tmp+1]; crc = getCRC(KEYS,8); if(crc==INPUT[10]){ #ifdef DEBUG printf("INPUT OK!\r\n"); #endif if(usb_ready == 3) usb_send_packet(KEYS, 8); }else{ #ifdef DEBUG printf("ERROR AT CRC-BYTE %d vs %d\r\n",INPUT[10],crc); #endif } k=0; } } } void tim3_isr(void){ if(timer_get_flag(TIM3, TIM_SR_UIF)){ handle_data(); timer_clear_flag(TIM3, TIM_SR_UIF); } } void tim4_isr(void){ if(timer_get_flag(TIM4, TIM_SR_UIF)){ if(usb_ready==3){ gpio_toggle(GPIOD,GPIO14); KEYS[0]=0; KEYS[1]=0; KEYS[3]=0; KEYS[4]=0; KEYS[5]=0; KEYS[6]=0; KEYS[7]=0; KEYS[2]=0x04; usb_send_packet(KEYS, 8); KEYS[2]=0x00; usb_send_packet(KEYS, 8); KEYS[2]=0x05; usb_send_packet(KEYS, 8); KEYS[2]=0x00; usb_send_packet(KEYS, 8); } timer_clear_flag(TIM4, TIM_SR_UIF); } } void tim5_isr(void){ if(timer_get_flag(TIM5, TIM_SR_UIF)){ gpio_toggle(GPIOD, GPIO15); timer_clear_flag(TIM5, TIM_SR_UIF); } } int main(void) { iwdg_reset(); iwdg_set_period_ms(5); iwdg_start(); rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); rcc_periph_clock_enable(RCC_GPIOD); systick_setup(); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); iwdg_set_period_ms(500); iwdg_reset(); msleep(400); iwdg_set_period_ms(5); iwdg_reset(); gpio_set(GPIOD, GPIO15); // msleep(100000); /* SLEEEEEEEEEEEEEEEEEEEEEEP */ #ifdef USE_STTERM stlinky_init(); #else usart_setup(); #endif timer2_setup(100); usb_setup(); //timer4_setup(1); timer3_setup(100); timer5_setup(2); // gpio_set(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); while(1) { iwdg_reset(); } return 0; }