U8 chb_write(U16 addr, oFrame *frame, U8 len) { U8 status, frm_len, hdr_len; while (len > 0) { // calculate which frame len to use. if greater than max payload, split // up operation. frm_len = (len > CHB_MAX_PAYLOAD) ? CHB_MAX_PAYLOAD : len; // gen frame header hdr_len = chb_gen_hdr(hdr, addr, frm_len); // Build the complete frame oFrame_Define(&chb_oFrame); oFrame_Set(hdr, 0, hdr_len, 0, frame); // send data to chip //status = chb_tx(hdr, data, frm_len); status = chb_tx(&chb_oFrame); if (status != CHB_SUCCESS) { switch (status) { case RADIO_SUCCESS: // fall through case CHB_SUCCESS_DATA_PENDING: pcb.txd_success++; break; case CHB_NO_ACK: pcb.txd_noack++; break; case CHB_CHANNEL_ACCESS_FAILURE: pcb.txd_channel_fail++; break; default: break; } if (status == RADIO_SUCCESS) // Added by Dario to manage the return properly return 1; else return 0; } // adjust len and restart len = len - frm_len; } return CHB_SUCCESS; }
U8 chb_write(U16 addr, U8 *data, U32 len) { U8 status, frm_len, frm_offset, hdr_len, hdr[CHB_HDR_SZ + 1]; int rtry; frm_offset = 0; while (len > 0) { // calculate which frame len to use. if greater than max payload, split // up operation. frm_len = (len > CHB_MAX_PAYLOAD) ? CHB_MAX_PAYLOAD : len; // gen frame header hdr_len = chb_gen_hdr(hdr, addr, frm_len); // send data to chip rtry = 0; do{ status = chb_tx(hdr, data+frm_offset, frm_len); switch (status) { case RADIO_SUCCESS: //fall through case CHB_SUCCESS_DATA_PENDING: pcb.txd_success++; break; case CHB_NO_ACK: pcb.txd_noack++; rtry++; break; case CHB_CHANNEL_ACCESS_FAILURE: pcb.txd_channel_fail++; rtry++; break; default: break; } if(rtry>0) _delay_us(10); //if not successfully sent the first time, wait some time and try again if(rtry==20) return status;; //after 20 tries give up on sending the message } while(status != CHB_SUCCESS); // adjust len and restart frm_offset += frm_len; len = len - frm_len; //_delay_ms(1000); //wait a little before sending next message } return CHB_SUCCESS; }
U8 chb_write(U16 addr, U8 *data, U8 len) { U8 status, frm_len, hdr_len, hdr[CHB_HDR_SZ + 1]; while (len > 0) { // calculate which frame len to use. if greater than max payload, split // up operation. frm_len = (len > CHB_MAX_PAYLOAD) ? CHB_MAX_PAYLOAD : len; // gen frame header hdr_len = chb_gen_hdr(hdr, addr, frm_len); // send data to chip status = chb_tx(hdr, data, frm_len); if (status != CHB_SUCCESS) { switch (status) { case RADIO_SUCCESS: // fall through case CHB_SUCCESS_DATA_PENDING: pcb.txd_success++; break; case CHB_NO_ACK: pcb.txd_noack++; break; case CHB_CHANNEL_ACCESS_FAILURE: pcb.txd_channel_fail++; break; default: break; } return status; } // adjust len and restart len = len - frm_len; } return CHB_SUCCESS; }