static void pgm_recv_to_flash() { i2cee_write_block( pgm_recv_slot * PGM_MAX_BYTES + pgm_recv_addr, pgm_recv_buffer, pgm_recv_buf_idx ); while( !i2cee_is_write_finished() ); pgm_recv_addr += pgm_recv_buf_idx; pgm_recv_buf_idx = 0; nrf_set_mode( NRF_MODE_TX ); nrf_send_packet( remote_addr, pgm_radio_ack, 4 ); nrf_set_mode( NRF_MODE_RX ); }
int main(void) { ADCON1 = 0x0F; CMCON = 0x07; TRISA = 0x00; PORTA = 0x00; interrupt_initialize(); timer_initialize(); nrf_initialize(nrf_transmit_mode, nrf_channel_0, CAFE_ADDRESS | NODE_ID); nrf_packet_t packet; uint8_t differ = 0x00; PORTA = 0x00; while(1) { #if CONSTANT_TRANSMIT #if SEND_TO_APP packet.address = CAFE_ADDRESS | HUB_ID; packet.payload_length = 18; packet.payload[0] = 0x05; // Branch packet.payload[1] = (NODE_ID >> 8); // Source ID packet.payload[2] = (NODE_ID & 0xFF); packet.payload[3] = (HUB_ID >> 8); // Destination ID packet.payload[4] = (HUB_ID & 0xFF); packet.payload[5] = 0x00; // Source App packet.payload[6] = 0x00; packet.payload[7] = 0x80; // Destination App packet.payload[8] = 0x41; packet.payload[9] = 0x07; // Command packet.payload[10] = 0x00; // Sub-Command packet.payload[11] = 0x06; // Payload Length packet.payload[12] = 0xba; // Payload packet.payload[13] = 0xba; packet.payload[14] = 0xde; packet.payload[15] = 0xca; packet.payload[16] = 0xfe; packet.payload[17] = differ++; nrf_set_mode(nrf_transmit_mode, true); nrf_flush_fifo(true); nrf_transmit_packet(packet, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x10)); PORTA ^= 0x01; timer_delay_ms(1000); #endif #if SEND_TO_HUB_APP packet.address = CAFE_ADDRESS | HUB_ID; packet.payload_length = 12; packet.payload[0] = 0x05; // Branch packet.payload[1] = (NODE_ID >> 8); // Source ID packet.payload[2] = (NODE_ID & 0xFF); packet.payload[3] = (HUB_ID >> 8); // Destination ID packet.payload[4] = (HUB_ID & 0xFF); packet.payload[5] = 0x00; // Source App packet.payload[6] = 0x00; packet.payload[7] = 0x00; // Destination App packet.payload[8] = 0x00; packet.payload[9] = 0x04; // Command packet.payload[10] = 0x05; // Sub-Command packet.payload[11] = 0x00; // Payload Length nrf_set_mode(nrf_transmit_mode, true); nrf_flush_fifo(true); nrf_transmit_packet(packet, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x10)); PORTA ^= 0x02; timer_delay_ms(1000); #endif #if SEND_TO_HUB packet.address = CAFE_ADDRESS; packet.payload_length = 13; packet.payload[0] = 0x05; // Branch packet.payload[1] = (NODE_ID >> 8); // Source ID packet.payload[2] = (NODE_ID & 0xFF); packet.payload[3] = 0x00; // Destination ID packet.payload[4] = 0x00; packet.payload[5] = 0x00; // Source App packet.payload[6] = 0x00; packet.payload[7] = 0x00; // Destination App packet.payload[8] = 0x00; packet.payload[9] = 0x05; // Command packet.payload[10] = 0x05; // Sub-Command packet.payload[11] = 0x01; // Payload Length packet.payload[11] = differ; // Payload nrf_set_mode(nrf_transmit_mode, true); nrf_flush_fifo(true); nrf_transmit_packet(packet, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x10)); PORTA ^= 0x02; timer_delay_ms(1000); #endif #if SEND_HUB_ID_REQUEST packet.address = CAFE_ADDRESS | HUB_ID; packet.payload_length = 13; packet.payload[0] = 0x05; // Branch packet.payload[1] = (NODE_ID >> 8); // Source ID packet.payload[2] = (NODE_ID & 0xFF); packet.payload[3] = (HUB_ID >> 8); // Destination ID packet.payload[4] = (HUB_ID & 0xFF); packet.payload[5] = 0x00; // Source App packet.payload[6] = 0x00; packet.payload[7] = 0x00; // Destination App packet.payload[8] = 0x00; packet.payload[9] = 0x06; // Command packet.payload[10] = 0x05; // Sub-Command packet.payload[11] = 0x01; // Payload Length packet.payload[12] = 0x01; // Payload nrf_set_mode(nrf_transmit_mode, true); nrf_flush_fifo(true); nrf_transmit_packet(packet, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x10)); PORTA ^= 0x02; timer_delay_ms(1000); #endif #elif SEND_PACKETS packet.address = CAFE_ADDRESS | HUB_ID; packet.payload_length = 18; packet.payload[0] = 0x05; // Branch packet.payload[1] = (NODE_ID >> 8); // Source ID packet.payload[2] = (NODE_ID & 0xFF); packet.payload[3] = (HUB_ID >> 8); // Destination ID packet.payload[4] = (HUB_ID & 0xFF); packet.payload[5] = 0x00; // Source App packet.payload[6] = 0x00; packet.payload[7] = 0x00; // Destination App packet.payload[8] = 0x02; packet.payload[9] = 0x01; // Command packet.payload[10] = 0x00; // Sub-Command packet.payload[11] = 0x00; // Payload Length PORTA ^= 0x01; nrf_set_mode(nrf_transmit_mode, true); nrf_flush_fifo(true); nrf_transmit_packet(packet, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x10)); PORTA ^= 0x01; nrf_set_mode(nrf_receive_mode, true); nrf_flush_fifo(false); uint8_t delay_count = 0; PORTA ^= 0x02; while((nrf_read_register(NRF_REG_FIFO_STATUS) & 0x01) && (delay_count < 40)) { timer_delay_ms(25); delay_count++; } PORTA ^= 0x02; if(delay_count < 40) { if(nrf_read_packet(&packet) != SUCCESS) delay_count = 40; else PORTA ^= 0x04; } if(delay_count < 40) timer_delay_ms(2000); timer_delay_ms(2000); #elif RECEIVE_PACKETS nrf_set_mode(nrf_receive_mode, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x01)) { timer_delay_ms(50); PORTA ^= 0x02; nrf_read_packet(&packet); uint16_t temp; temp = (packet.payload[1] << 8); temp |= packet.payload[2]; packet.address = CAFE_ADDRESS | temp; packet.payload_length = 12; packet.payload[1] = (NODE_ID >> 8); packet.payload[2] = (NODE_ID & 0xFF); packet.payload[3] = (temp >> 8); packet.payload[4] = (temp & 0xFF); temp = (packet.payload[5] << 8); temp |= packet.payload[6]; packet.payload[5] = packet.payload[7]; packet.payload[6] = packet.payload[8]; packet.payload[7] = (temp >> 8); packet.payload[8] = (temp & 0xFF); packet.payload[9] ^= 0x80; packet.payload[10] = differ++; packet.payload[11] = 0x00; PORTA ^= 0x01; nrf_set_mode(nrf_transmit_mode, true); nrf_flush_fifo(true); nrf_transmit_packet(packet, false); while(!(nrf_read_register(NRF_REG_FIFO_STATUS) & 0x10)); PORTA ^= 0x01; nrf_set_mode(nrf_receive_mode, false); nrf_write_register(NRF_REG_STATUS, 0x20); } #endif } }
int main() { u8 data[ 4 ]; int res; leds_init(); btn_init(); ind_init(); uart_init(); sei(); ledvm_init(); i2cee_init(); pgm_init(); //stdout = &mystdout; nrf_init(); nrf_set_own_addr( lights_addr ); nrf_set_mode( NRF_MODE_RX ); while( 1 ) { // Something to execute? if( pgm_crt != -1 ) { res = ledvm_run(); if( res != LEDVM_ERR_OK ) { if( res == LEDVM_ERR_FINISHED ) // program finished, look for the next one { if( pgm_cycle ) { res = pgm_get_next_index( pgm_crt, 1 ); if( res != -1 ) // if found use the other program, otherwise don't change it pgm_crt = res; } ledvm_init(); } else { ledvm_ll_setrgb( 0, 0, 0 ); pgm_crt = -1; } } } // New data via radio? if( nrf_get_packet( data, 4, NULL ) == 4 ) { // Got data, interpret it if( data[ 0 ] == CMD_START_WRITE ) { pgm_recv_on = 1; pgm_recv_slot = data[ 3 ]; pgm_recv_addr = 0; pgm_recv_buf_idx = 0; if( pgm_recv_slot == pgm_crt ) pgm_crt = -1; } else if( data[ 0 ] == CMD_END_WRITE ) { pgm_recv_on = 0; pgm_recv_to_flash(); pgm_set_program_state( pgm_recv_slot, 1 ); if( pgm_crt == -1 ) pgm_crt = pgm_get_first_index(); } else if( data[ 0 ] == CMD_RUN ) { if( pgm_mask & ( 1 << data[ 3 ] ) ) { pgm_crt = data[ 3 ]; ledvm_init(); } } else if( data[ 0 ] == CMD_RAW ) { pgm_crt = -1; ledvm_ll_setrgb( data[ 1 ], data[ 2 ], data[ 3 ] ); } else if( data[ 0 ] == CMD_DEL ) { pgm_set_program_state( data[ 3 ], 0 ); if( pgm_crt == data[ 3 ] ) { pgm_crt = pgm_get_first_index(); ledvm_init(); } } else if( pgm_recv_on ) // instruction { memcpy( pgm_recv_buffer + pgm_recv_buf_idx, data, 4 ); pgm_recv_buf_idx += 4; if( pgm_recv_buf_idx == PGM_EE_PAGE_SIZE ) pgm_recv_to_flash(); } } // Handle buttons if( pgm_crt != -1 ) { btn_handler(); if( btn_is_pressed( BTN_NEXT_PIN ) ) { res = pgm_get_next_index( pgm_crt, 1 ); if( res != -1 ) pgm_crt = res; ledvm_init(); } else if( btn_is_pressed( BTN_PREV_PIN ) ) { res = pgm_get_next_index( pgm_crt, -1 ); if( res != -1 ) pgm_crt = res; ledvm_init(); } else if( btn_is_pressed( BTN_REPEAT_PIN ) ) ind_set( 1 - pgm_cycle ); } } }