final
Dependencies: mbed FATFileSystem
Fork of KL46Z-USBHostMSD_HelloWorld by
Revision 4:77d6450f34d7, committed 2015-04-04
- Comitter:
- homzovam
- Date:
- Sat Apr 04 20:16:39 2015 +0000
- Parent:
- 3:4238ec88ddcf
- Commit message:
- prijimac-funkcni final
Changed in this revision
--- a/F401RE-USBHost.lib Fri Jun 13 01:57:39 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/va009039/code/F401RE-USBHost/#d14c06cc5c07
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/FATFileSystem.lib Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/FATFileSystem/#e960e2b81a3c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/IUSBEnumerator.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,36 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IUSBENUMERATOR_H_ +#define IUSBENUMERATOR_H_ + +#include "stdint.h" +#include "USBEndpoint.h" + +/* +Generic interface to implement for "smart" USB enumeration +*/ + +class IUSBEnumerator +{ +public: + virtual void setVidPid(uint16_t vid, uint16_t pid) = 0; + virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) = 0; //Must return true if the interface should be parsed + virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) = 0; //Must return true if the endpoint will be used +}; + +#endif /*IUSBENUMERATOR_H_*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBDeviceConnected.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,89 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "USBDeviceConnected.h" +#include "dbg.h" + +USBDeviceConnected::USBDeviceConnected() { + init(); +} + +void USBDeviceConnected::init() { + port = 0; + vid = 0; + pid = 0; + nb_interf = 0; + enumerated = false; + device_class = 0; + device_subclass = 0; + proto = 0; + lowSpeed = false; + hub_parent = NULL; +} + +bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) { + USB_DBG("intf_nb=%d", intf_nb); + if (intf.count(intf_nb) > 0) { + return false; + } + intf[intf_nb] = new INTERFACE(intf_class, intf_subclass, intf_protocol); + return true; +} + +bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) { + if (intf.count(intf_nb) > 0) { + intf[intf_nb]->ep.push_back(ept); + return true; + } + return false; +} + +void USBDeviceConnected::init(USBDeviceConnected* parent, uint8_t port_, bool lowSpeed_) { + USB_DBG("init dev: %p", this); + init(); + hub_parent = parent; + port = port_; + lowSpeed = lowSpeed_; +} + +void USBDeviceConnected::disconnect() { + //for(int i = 0; i < MAX_INTF; i++) { + // intf[i].detach.call(); + //} + //init(); +} + + +USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) { + USB_DBG("intf_nb=%d", intf_nb); + USB_TEST_ASSERT(intf.count(intf_nb) > 0); + INTERFACE* inter = intf[intf_nb]; + for (int i = 0; i < inter->ep.size(); i++) { + if ((inter->ep[i]->getType() == type) && (inter->ep[i]->getDir() == dir)) { + if(index) { + index--; + } else { + return inter->ep[i]; + } + } + } + return NULL; +} + +USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, uint8_t index) { + USB_TEST_ASSERT(intf.count(intf_nb) > 0); + return intf[intf_nb]->ep[index]; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBDeviceConnected.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,133 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include "USBEndpoint.h" +#include "USBHostConf.h" +#include "myvector.h" +#include "mymap.h" + +class USBEndpoint; + +struct INTERFACE { + INTERFACE(uint8_t _class, uint8_t _subclass, uint8_t _protocol) { + intf_class = _class; + intf_subclass = _subclass; + intf_protocol = _protocol; + } + uint8_t intf_class; + uint8_t intf_subclass; + uint8_t intf_protocol; + myvector<USBEndpoint*>ep; +}; + +/** +* USBDeviceConnected class +*/ +class USBDeviceConnected { +public: + + /** + * Constructor + */ + USBDeviceConnected(); + + /** + * Attach an USBEndpoint to this device + * + * @param intf_nb interface number + * @param ep pointeur on the USBEndpoint which will be attached + * @returns true if successful, false otherwise + */ + bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep); + + /** + * Retrieve an USBEndpoint by its TYPE and DIRECTION + * + * @param intf_nb the interface on which to lookup the USBEndpoint + * @param type type of the USBEndpoint looked for + * @param dir direction of the USBEndpoint looked for + * @param index the index of the USBEndpoint whitin the interface + * @returns pointer on the USBEndpoint if found, NULL otherwise + */ + USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0); + + /** + * Retrieve an USBEndpoint by its index + * + * @param intf_nb interface number + * @param index index of the USBEndpoint + * @returns pointer on the USBEndpoint if found, NULL otherwise + */ + USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index); + + /** + * Add a new interface to this device + * + * @param intf_nb interface number + * @param intf_class interface class + * @param intf_subclass interface subclass + * @param intf_protocol interface protocol + * @returns true if successful, false otherwise + */ + bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); + + /** + * Disconnect the device by calling a callback function registered by a driver + */ + void disconnect(); + + void init(USBDeviceConnected* parent, uint8_t _port, bool _lowSpeed); + void setAddress(uint8_t addr_) { addr = addr_; }; + void setVid(uint16_t vid_) { vid = vid_; }; + void setPid(uint16_t pid_) { pid = pid_; }; + void setClass(uint8_t device_class_) { device_class = device_class_; } + void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; }; + void setProtocol(uint8_t pr) { proto = pr; }; + void setEnumerated() { enumerated = true; }; + void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; }; + void setSpeed(bool _lowSpeed) { lowSpeed = _lowSpeed; } + void setName(const char * name_, uint8_t intf_nb) { return; }; + void setEpCtl(USBEndpoint* ep) { ep_ctl = ep; } + + static int getNewAddress() { + static int addr = 1; + return addr++; + } + uint8_t getAddress() { return addr; }; + uint16_t getVid() { return vid; }; + uint16_t getPid() { return pid; }; + uint8_t getClass() { return device_class; }; + bool getSpeed() { return lowSpeed; } + bool isEnumerated() { return enumerated; }; + USBEndpoint* getEpCtl() { return ep_ctl; } + +private: + USBDeviceConnected* hub_parent; + mymap<int,INTERFACE*>intf; + uint8_t port; + uint16_t vid; + uint16_t pid; + uint8_t addr; + uint8_t device_class; + uint8_t device_subclass; + uint8_t proto; + bool lowSpeed; + bool enumerated; + uint8_t nb_interf; + USBEndpoint* ep_ctl; + void init(); +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBEndpoint.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,145 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include "FunctionPointer.h" +#include "USBHostTypes.h" +#include "USBDeviceConnected.h" + +class USBDeviceConnected; + +/** +* USBEndpoint class +*/ +class USBEndpoint { +public: + /** + * Constructor + */ + USBEndpoint(USBDeviceConnected* _dev) { + init(CONTROL_ENDPOINT, IN, 8, 0); + dev = _dev; + } + + /** + * Initialize an endpoint + * + * @param type endpoint type + * @param dir endpoint direction + * @param size endpoint size + * @param ep_number endpoint number + */ + void init(ENDPOINT_TYPE _type, ENDPOINT_DIRECTION _dir, uint32_t size, uint8_t ep_number) { + type = _type; + dir = _dir; + MaxPacketSize = size; + address = ep_number; + data01_toggle = DATA0; + } + + /** + * Attach a member function to call when a transfer is finished + * + * @param tptr pointer to the object to call the member function on + * @param mptr pointer to the member function to be called + */ + template<typename T> + inline void attach(T* tptr, void (T::*mptr)(void)) { + if((mptr != NULL) && (tptr != NULL)) { + rx.attach(tptr, mptr); + } + } + + /** + * Attach a callback called when a transfer is finished + * + * @param fptr function pointer + */ + inline void attach(void (*fptr)(void)) { + if(fptr != NULL) { + rx.attach(fptr); + } + } + + /** + * Call the handler associted to the end of a transfer + */ + inline void call() { + rx.call(); + }; + + void setDevice(USBDeviceConnected* _dev) { dev = _dev; } + void setState(uint8_t st){}; // dummy + void setBuffer(uint8_t* buf, int size) { buf_start = buf, buf_size = size; } + void setLengthTransferred(int len) { transferred = len; }; + void setAddress(int addr) { address = addr; } + void setSize(int size) { MaxPacketSize = size; } + void setData01(uint8_t data01) { data01_toggle = data01; } + void setNextEndpoint(USBEndpoint* ep) { nextEp = ep; }; + + USBDeviceConnected* getDevice() { return dev; } + ENDPOINT_TYPE getType() { return type; }; + int getLengthTransferred() { return transferred; } + uint8_t *getBufStart() { return buf_start; } + int getBufSize() { return buf_size; } + uint8_t getAddress(){ return address; }; + int getSize() { return MaxPacketSize; } + ENDPOINT_DIRECTION getDir() { return dir; } + uint8_t getData01() { return data01_toggle; } + void toggleData01() { + data01_toggle = (data01_toggle == DATA0) ? DATA1 : DATA0; + } + USBEndpoint* nextEndpoint() { return nextEp; }; + +private: + ENDPOINT_TYPE type; + ENDPOINT_DIRECTION dir; + USBDeviceConnected* dev; + uint8_t data01_toggle; // DATA0,DATA1 + uint8_t address; + int transferred; + uint8_t * buf_start; + int buf_size; + FunctionPointer rx; + int MaxPacketSize; + USBEndpoint* nextEp; +}; + +class EndpointQueue { +public: + EndpointQueue():head(NULL),tail(NULL) {} + void push(USBEndpoint* ep) { + if (head) { + tail->setNextEndpoint(ep); + } else { + head = ep; + } + tail = ep; + ep->setNextEndpoint(NULL); + } + USBEndpoint* pop() { + USBEndpoint* ep = head; + if (ep) { + head = ep->nextEndpoint(); + } + return ep; + } + bool empty() { return head == NULL; } + +private: + USBEndpoint* head; + USBEndpoint* tail; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHALHost.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,9 @@ +#if defined(TARGET_NUCLEO_F401RE) +#include "USBHALHost_F401RE.h" +#elif defined(TARGET_KL46Z)||defined(TARGET_KL25Z) +#include "USBHALHost_KL46Z.h" +#else +#error "target error" +#endif + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHALHost_KL46Z.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,426 @@ +// Simple USBHost for FRDM-KL46Z +#if defined(TARGET_KL46Z)||defined(TARGET_KL25Z) +#include "USBHALHost_KL46Z.h" + +template <bool>struct CtAssert; +template <>struct CtAssert<true> {}; +#define CTASSERT(A) CtAssert<A>(); + + +#ifdef _USB_DBG +#define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0); +#define USB_DBG_HEX(A,B) debug_hex(A,B) +void debug_hex(uint8_t* buf, int size); +#else +#define USB_DBG(...) while(0) +#define USB_DBG_HEX(A,B) while(0) +#endif + +#ifdef _USB_TEST +#define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; +#define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) +#else +#define USB_TEST_ASSERT(A) while(0) +#define USB_TEST_ASSERT_FALSE(A) while(0) +#endif + +//#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");}while(0); + +#define BD_OWN_MASK (1<<7) +#define BD_DATA01_MASK (1<<6) +#define BD_KEEP_MASK (1<<5) +#define BD_NINC_MASK (1<<4) +#define BD_DTS_MASK (1<<3) +#define BD_STALL_MASK (1<<2) + +#define TX 1 +#define RX 0 + +#define EP0_BDT_IDX(dir, odd) (((2 * dir) + (1 * odd))) + +#define SETUP_TOKEN 0x0D +#define IN_TOKEN 0x09 +#define OUT_TOKEN 0x01 + +// for each endpt: 8 bytes +struct BDT { + uint8_t info; // BD[0:7] + uint8_t dummy; // RSVD: BD[8:15] + uint16_t byte_count; // BD[16:32] + uint32_t address; // Addr + void setBuffer(uint8_t* buf, int size) { + address = (uint32_t)buf; + byte_count = size; + } + uint8_t getStatus() { + return (info>>2)&0x0f; + } +}; + +__attribute__((__aligned__(512))) BDT bdt[64]; + +USBHALHost* USBHALHost::instHost; + +USBHALHost::USBHALHost() { + instHost = this; + report.clear(); +} + +void USBHALHost::init() { + // Disable IRQ + NVIC_DisableIRQ(USB0_IRQn); + + // choose usb src as PLL + SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL_MASK); + + // enable OTG clock + SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK; + + // USB Module Configuration + // Reset USB Module + USB0->USBTRC0 |= USB_USBTRC0_USBRESET_MASK; + while(USB0->USBTRC0 & USB_USBTRC0_USBRESET_MASK); + + // Clear interrupt flag + USB0->ISTAT = 0xff; + + // Set BDT Base Register + USB0->BDTPAGE1=(uint8_t)((uint32_t)bdt>>8); + USB0->BDTPAGE2=(uint8_t)((uint32_t)bdt>>16); + USB0->BDTPAGE3=(uint8_t)((uint32_t)bdt>>24); + + // Set SOF threshold + USB0->SOFTHLD = USB_SOFTHLD_CNT(1); + + // pulldown D+ and D- + USB0->USBCTRL = USB_USBCTRL_PDE_MASK; + + USB0->USBTRC0 |= 0x40; + + // Host mode + USB0->CTL |= USB_CTL_HOSTMODEEN_MASK; + // Desable SOF packet generation + USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK; + + NVIC_SetVector(USB0_IRQn, (uint32_t)_usbisr); + NVIC_EnableIRQ(USB0_IRQn); + + bool lowSpeed = wait_attach(); + + for(int retry = 2; retry > 0; retry--) { + // Enable RESET + USB0->CTL |= USB_CTL_RESET_MASK; + wait_ms(500); + USB0->CTL &= ~USB_CTL_RESET_MASK; + + // Enable SOF + USB0->CTL |= USB_CTL_USBENSOFEN_MASK; + wait_ms(100); + + // token transfer initialize + token_transfer_init(); + + USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK| + USB_INTEN_ERROREN_MASK; + USB0->ERREN |= USB_ERREN_PIDERREN_MASK| + USB_ERREN_CRC5EOFEN_MASK| + USB_ERREN_CRC16EN_MASK| + USB_ERREN_DFN8EN_MASK| + USB_ERREN_BTOERREN_MASK| + USB_ERREN_DMAERREN_MASK| + USB_ERREN_BTSERREN_MASK; + + if (addDevice(NULL, 0, lowSpeed)) { + break; + } + USB_DBG("retry=%d", retry); + USB_TEST_ASSERT(retry > 1); + } +} + +bool USBHALHost::wait_attach() { + attach_done = false; + USB0->INTEN = USB_INTEN_ATTACHEN_MASK; + Timer t; + t.reset(); + t.start(); + while(!attach_done) { + if (t.read_ms() > 5*1000) { + t.reset(); + //USB_INFO("Please attach USB device."); + } + } + wait_ms(100); + USB_TEST_ASSERT_FALSE(USB0->CTL & USB_CTL_SE0_MASK); + root_lowSpeed = (USB0->CTL & USB_CTL_JSTATE_MASK) ? false : true; + return root_lowSpeed; +} + +void USBHALHost::setAddr(int _addr, bool _lowSpeed) { + USB0->ADDR = (_lowSpeed ? USB_ADDR_LSEN_MASK : 0x00) | USB_ADDR_ADDR(_addr); +} + +void USBHALHost::setEndpoint() { + USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00)| + USB_ENDPT_RETRYDIS_MASK| + USB_ENDPT_EPCTLDIS_MASK| + USB_ENDPT_EPRXEN_MASK| + USB_ENDPT_EPTXEN_MASK| + USB_ENDPT_EPHSHK_MASK; +} + +void USBHALHost::token_transfer_init() { + tx_ptr = ODD; + rx_ptr = ODD; +} + +int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength) { + USBDeviceConnected* dev = ep->getDevice(); + for(int retry = 0;; retry++) { + token_ready(); + USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00) | + USB_ENDPT_RETRYDIS_MASK| + USB_ENDPT_EPRXEN_MASK| + USB_ENDPT_EPTXEN_MASK| + USB_ENDPT_EPHSHK_MASK; + CTASSERT(sizeof(SETUP_PACKET) == 8); + setup->wLength = wLength; + int idx = EP0_BDT_IDX(TX, tx_ptr); + bdt[idx].setBuffer((uint8_t*)setup, sizeof(SETUP_PACKET)); + bdt[idx].info = BD_OWN_MASK | + BD_DTS_MASK; // always data0 + token_done = false; + USB0->TOKEN = USB_TOKEN_TOKENPID(SETUP_TOKEN)| + USB_TOKEN_TOKENENDPT(ep->getAddress() & 0x7f); + while(!token_done); + LastStatus = bdt[idx].getStatus(); + if (LastStatus == ACK) { + if (retry > 0) { + USB_DBG("retry=%d %02x", retry, prev_LastStatus); + } + break; + } else if (LastStatus == STALL) { + report.stall++; + return STALL; + } + if (retry > 10) { + USB_DBG("retry=%d", retry); + break; + } + prev_LastStatus = LastStatus; + wait_ms(100 * retry); + } + ep->setData01(DATA1); // next toggle + return LastStatus; +} + +int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) { + for(int retry = 0;; retry++) { + token_ready(); + int idx = EP0_BDT_IDX(RX, rx_ptr); + bdt[idx].setBuffer(data, size); + bdt[idx].info = BD_OWN_MASK| + BD_DTS_MASK| + (ep->getData01() == DATA1 ? BD_DATA01_MASK : 0); + token_done = false; + USB0->TOKEN = USB_TOKEN_TOKENPID(IN_TOKEN)| + USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f); + while(!token_done); + LastStatus = bdt[idx].getStatus(); + int len = bdt[idx].byte_count; + if (LastStatus == DATA0 || LastStatus == DATA1) { + USB_TEST_ASSERT(ep->getData01() == LastStatus); + ep->setData01(LastStatus == DATA0 ? DATA1 : DATA0); + if (retry > 0) { + //USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus); + } + return len; + } else if (LastStatus == STALL) { + report.stall++; + return -1; + } else if (LastStatus == NAK) { + report.nak++; + if (retry >= retryLimit) { + if (retryLimit > 0) { + USB_DBG("retry=%d retryLimit=%d", retry, retryLimit); + } + return -1; + } + wait_ms(100 * retry); + } else if (LastStatus == Bus_Timeout) { + if (retry >= retryLimit) { + if (retryLimit > 0) { + USB_DBG("Bus_Timeout retry=%d retryLimit=%d", retry, retryLimit); + } + return -1; + } + wait_ms(500 + 100 * retry); + } else { + return -1; + } + prev_LastStatus = LastStatus; + } +} + +int USBHALHost::token_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) { + for(int retry = 0;; retry++) { + token_ready(); + int idx = EP0_BDT_IDX(TX, tx_ptr); + bdt[idx].setBuffer((uint8_t*)data, size); + bdt[idx].info = BD_OWN_MASK| + BD_DTS_MASK| + (ep->getData01() == DATA1 ? BD_DATA01_MASK : 0); + token_done = false; + USB0->TOKEN = USB_TOKEN_TOKENPID(OUT_TOKEN)| + USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f); + while(!token_done); + LastStatus = bdt[idx].getStatus(); + int len = bdt[idx].byte_count; + //USB_DBG("len=%d %02x", len, LastStatus); + if (LastStatus == ACK) { + ep->toggleData01(); + if (retry > 0) { + USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus); + } + return len; + } else if (LastStatus == STALL) { + report.stall++; + return -1; + } else if (LastStatus == NAK) { + report.nak++; + if (retry > retryLimit) { + USB_DBG("retry=%d retryLimit=%d", retry, retryLimit); + return -1; + } + wait_ms(100 * retry); + } else { + return -1; + } + prev_LastStatus = LastStatus; + } +} + +int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size) { + while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK); // TOKEN_BUSY ? + USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF + while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK)); + USB0->SOFTHLD = 0; // this is needed as without this you can get errors + USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF + + USB0->ENDPOINT[0].ENDPT = USB_ENDPT_EPCTLDIS_MASK| + USB_ENDPT_RETRYDIS_MASK| + USB_ENDPT_EPRXEN_MASK| + USB_ENDPT_EPTXEN_MASK; + int idx = EP0_BDT_IDX(RX, rx_ptr); + bdt[idx].setBuffer(data, size); + bdt[idx].info = BD_OWN_MASK| + BD_DTS_MASK; // always DATA0 + token_done = false; + USB0->TOKEN = USB_TOKEN_TOKENPID(IN_TOKEN)| + USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f); + while(!token_done); + LastStatus = bdt[idx].getStatus(); + int len = bdt[idx].byte_count; + if (LastStatus == DATA0) { + return len; + } + return -1; +} + +void USBHALHost::token_ready() { + while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK) { // TOKEN_BUSY ? + wait_ms(1); + } + USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF + while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK)); + USB0->SOFTHLD = 0; // this is needed as without this you can get errors + USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF +} + +void USBHALHost::_usbisr(void) { + if (instHost) { + instHost->UsbIrqhandler(); + } +} + +void USBHALHost::UsbIrqhandler() { + if (USB0->ISTAT & USB_ISTAT_TOKDNE_MASK) { + uint8_t stat = USB0->STAT; + ODD_EVEN next_ptr = (stat & USB_STAT_ODD_MASK) ? ODD : EVEN; + if (stat & USB_STAT_TX_MASK) { + tx_ptr = next_ptr; + } else { + rx_ptr = next_ptr; + } + USB0->ISTAT = USB_ISTAT_TOKDNE_MASK; + token_done = true; + } + if (USB0->ISTAT & USB_ISTAT_ATTACH_MASK) { + USB0->INTEN &= ~USB_INTEN_ATTACHEN_MASK; + USB0->ISTAT = USB_ISTAT_ATTACH_MASK; + attach_done = true; + } + if (USB0->ISTAT & USB_ISTAT_ERROR_MASK) { + uint8_t errstat = USB0->ERRSTAT; + if (errstat & USB_ERRSTAT_PIDERR_MASK) { + report.errstat_piderr++; + } + if (errstat & USB_ERRSTAT_CRC5EOF_MASK) { + report.errstat_crc5eof++; + } + if (errstat & USB_ERRSTAT_CRC16_MASK) { + report.errstat_crc16++; + } + if (errstat & USB_ERRSTAT_DFN8_MASK) { + report.errstat_dfn8++; + } + if (errstat & USB_ERRSTAT_BTOERR_MASK) { + report.errstat_btoerr++; + } + if (errstat & USB_ERRSTAT_DMAERR_MASK) { + report.errstat_dmaerr++; + } + if (errstat & USB_ERRSTAT_BTSERR_MASK) { + report.errstat_btoerr++; + } + USB0->ERRSTAT = errstat; + USB0->ISTAT = USB_ISTAT_ERROR_MASK; + } +} + +void Report::clear() { + errstat_piderr = 0; + errstat_crc5eof = 0; + errstat_crc16 = 0; + errstat_dfn8 = 0; + errstat_btoerr = 0; + errstat_dmaerr = 0; + errstat_bsterr = 0; + // + nak = 0; +} + +void Report::print_errstat() { + printf("ERRSTAT PID: %d, CRC5EOF: %d, CRC16: %d, DFN8: %d, BTO: %d, DMA: %d, BST: %d\n", + errstat_piderr, errstat_crc5eof, + errstat_crc16, errstat_dfn8, + errstat_btoerr, errstat_dmaerr, errstat_bsterr); +} + +void debug_hex(uint8_t* buf, int size) { + int n = 0; + for(int i = 0; i < size; i++) { + fprintf(stderr, "%02x ", buf[i]); + if (++n >= 16) { + fprintf(stderr, "\n"); + n = 0; + } + } + if (n > 0) { + fprintf(stderr, "\n"); + } +} + +#endif + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHALHost_KL46Z.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,71 @@ +// Simple USBHost for FRDM-KL46Z +#pragma once +#include "mbed.h" +#include "USBHostTypes.h" +#include "USBEndpoint.h" + +struct SETUP_PACKET { + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +}; + +#define GET_CONFIGURATION 8 + +// TOK_PID[5:2] +#define Bus_Timeout 0x00 +#define Data_Error 0x0f + +enum ODD_EVEN { + ODD = 0, + EVEN = 1, +}; + +class Report { +public: + void clear(); + void print_errstat(); + // error count + uint32_t errstat_piderr; // USBx_ERRSTAT[PIDERR] + uint32_t errstat_crc5eof;// USBx_ERRSTAT[CRC5EOF] + uint32_t errstat_crc16; // USBx_ERRSTAT[CRC16] + uint32_t errstat_dfn8; // USBx_ERRSTAT[DFN8] + uint32_t errstat_btoerr; // USBx_ERRSTAT[BTOERR] + uint32_t errstat_dmaerr; // USBx_ERRSTAT[DMAERR] + uint32_t errstat_bsterr; // USBx_ERRSTAT[BTSERR] + // + uint32_t nak; + uint32_t stall; +}; + +class USBHALHost { +public: + uint8_t LastStatus; + uint8_t prev_LastStatus; + Report report; + +protected: + USBHALHost(); + void init(); + virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed) = 0; + void setAddr(int addr, bool lowSpeed = false); + void setEndpoint(); + void token_transfer_init(); + int token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength = 0); + int token_in(USBEndpoint* ep, uint8_t* data = NULL, int size = 0, int retryLimit = 10); + int token_out(USBEndpoint* ep, const uint8_t* data = NULL, int size = 0, int retryLimit = 10); + int token_iso_in(USBEndpoint* ep, uint8_t* data, int size); + void token_ready(); +private: + static void _usbisr(void); + void UsbIrqhandler(); + __IO bool attach_done; + __IO bool token_done; + bool wait_attach(); + bool root_lowSpeed; + ODD_EVEN tx_ptr; + ODD_EVEN rx_ptr; + static USBHALHost * instHost; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHost.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,478 @@ +// Simple USBHost for FRDM-KL46Z +#include "USBHost.h" +#include <algorithm> + +USBHost* USBHost::inst = NULL; + +USBHost* USBHost::getHostInst() +{ + if (inst == NULL) { + inst = new USBHost(); + inst->init(); + } + return inst; +} + +void USBHost::poll() +{ + if (inst) { + inst->task(); + } +} + +USBHost::USBHost() { +} + +/* virtual */ bool USBHost::addDevice(USBDeviceConnected* parent, int port, bool lowSpeed) { + USBDeviceConnected* dev = new USBDeviceConnected; + USBEndpoint* ep = new USBEndpoint(dev); + dev->init(0, port, lowSpeed); + dev->setAddress(0); + dev->setEpCtl(ep); + uint8_t desc[18]; + wait_ms(100); + + int rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, 8); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + if (rc != USB_TYPE_OK) { + //USB_ERR("ADD DEVICE FAILD"); + } + USB_DBG_HEX(desc, 8); + DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc); + ep->setSize(dev_desc->bMaxPacketSize); + + int new_addr = USBDeviceConnected::getNewAddress(); + rc = controlWrite(dev, 0x00, SET_ADDRESS, new_addr, 0, NULL, 0); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + dev->setAddress(new_addr); + wait_ms(100); + + rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, sizeof(desc)); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + USB_DBG_HEX(desc, sizeof(desc)); + + dev->setVid(dev_desc->idVendor); + dev->setPid(dev_desc->idProduct); + dev->setClass(dev_desc->bDeviceClass); + //USB_INFO("parent:%p port:%d speed:%s VID:%04x PID:%04x class:%02x addr:%d", + //parent, port, (lowSpeed ? "low " : "full"), dev->getVid(), dev->getPid(), dev->getClass(), + //dev->getAddress()); + + DeviceLists.push_back(dev); + + if (dev->getClass() == HUB_CLASS) { + const int config = 1; + int rc = controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + wait_ms(100); + Hub(dev); + } + return true; +} + +// enumerate a device with the control USBEndpoint +USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator) +{ + if (dev->getClass() == HUB_CLASS) { // skip hub class + return USB_TYPE_OK; + } + uint8_t desc[18]; + USB_TYPE rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, sizeof(desc)); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + USB_DBG_HEX(desc, sizeof(desc)); + if (rc != USB_TYPE_OK) { + return rc; + } + DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc); + dev->setClass(dev_desc->bDeviceClass); + pEnumerator->setVidPid(dev->getVid(), dev->getPid()); + + rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, desc, 4); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + USB_DBG_HEX(desc, 4); + + int TotalLength = desc[2]|desc[3]<<8; + uint8_t* buf = new uint8_t[TotalLength]; + rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, buf, TotalLength); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + //USB_DBG_HEX(buf, TotalLength); + + // Parse the configuration descriptor + parseConfDescr(dev, buf, TotalLength, pEnumerator); + delete[] buf; + // only set configuration if not enumerated before + if (!dev->isEnumerated()) { + USB_DBG("Set configuration 1 on dev: %p", dev); + // sixth step: set configuration (only 1 supported) + int config = 1; + USB_TYPE res = controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); + if (res != USB_TYPE_OK) { + //USB_ERR("SET CONF FAILED"); + return res; + } + // Some devices may require this delay + wait_ms(100); + dev->setEnumerated(); + // Now the device is enumerated! + USB_DBG("dev %p is enumerated", dev); + } + return USB_TYPE_OK; +} + +// this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor. +void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) +{ + uint32_t index = 0; + uint32_t len_desc = 0; + uint8_t id = 0; + USBEndpoint * ep = NULL; + uint8_t intf_nb = 0; + bool parsing_intf = false; + uint8_t current_intf = 0; + EndpointDescriptor* ep_desc; + + while (index < len) { + len_desc = conf_descr[index]; + id = conf_descr[index+1]; + USB_DBG_HEX(conf_descr+index, len_desc); + switch (id) { + case CONFIGURATION_DESCRIPTOR: + USB_DBG("dev: %p has %d intf", dev, conf_descr[4]); + dev->setNbIntf(conf_descr[4]); + break; + case INTERFACE_DESCRIPTOR: + if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) { + intf_nb++; + current_intf = conf_descr[index + 2]; + dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]); + USB_DBG("ADD INTF %d on device %p: class: %d, subclass: %d, proto: %d", current_intf, dev, conf_descr[index + 5],conf_descr[index + 6],conf_descr[index + 7]); + parsing_intf = true; + } else { + parsing_intf = false; + } + break; + case ENDPOINT_DESCRIPTOR: + ep_desc = reinterpret_cast<EndpointDescriptor*>(conf_descr+index); + if (parsing_intf && (intf_nb <= MAX_INTF) ) { + ENDPOINT_TYPE type = (ENDPOINT_TYPE)(ep_desc->bmAttributes & 0x03); + ENDPOINT_DIRECTION dir = (ep_desc->bEndpointAddress & 0x80) ? IN : OUT; + if(pEnumerator->useEndpoint(current_intf, type, dir)) { + ep = new USBEndpoint(dev); + ep->init(type, dir, ep_desc->wMaxPacketSize, ep_desc->bEndpointAddress); + USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev); + dev->addEndpoint(current_intf, ep); + } + } + break; + case HID_DESCRIPTOR: + //lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8); + break; + default: + break; + } + index += len_desc; + } +} + +USB_TYPE USBHost::controlRead(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { + SETUP_PACKET setup = {requestType, request, value, index}; + int result = ControlRead(dev, &setup, buf, len); + //USB_DBG2("result=%d %02x", result, LastStatus); + return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR; +} + +USB_TYPE USBHost::controlWrite(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { + SETUP_PACKET setup = {requestType, request, value, index}; + int result = ControlWrite(dev, &setup, buf, len); + if (result >= 0) { + return USB_TYPE_OK; + } + USB_DBG("result=%d %02x", result, LastStatus); + USB_DBG_HEX(buf, len); + return USB_TYPE_ERROR; +} + +USB_TYPE USBHost::bulkRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { + if (blocking == false) { + ep->setBuffer(buf, len); + ep_queue.push(ep); + return USB_TYPE_PROCESSING; + } + int result = bulkReadBLOCK(ep, buf, len, -1); + if (result >= 0) { + return USB_TYPE_OK; + } + //USB_DBG2("result=%d %02x", result, host->LastStatus); + return USB_TYPE_ERROR; +} + +USB_TYPE USBHost::bulkWrite(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { + USB_TEST_ASSERT(blocking); + int result = bulkWriteNB(ep, buf, len); + if (result >= 0) { + return USB_TYPE_OK; + } + USB_DBG2("result=%d %02x", result, LastStatus); + return USB_TYPE_ERROR; +} + +USB_TYPE USBHost::interruptRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { + if (blocking == false) { + ep->setBuffer(buf, len); + ep_queue.push(ep); + return USB_TYPE_PROCESSING; + } + interruptReadNB(ep, buf, len); + return USB_TYPE_OK; +} + +USB_TYPE USBHost::interruptWrite(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { + USB_TEST_ASSERT(blocking); + interruptWriteNB(ep, buf, len); + return USB_TYPE_OK; +} + +USB_TYPE USBHost::isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { + if (blocking == false) { + ep->setBuffer(buf, len); + ep_queue.push(ep); + return USB_TYPE_PROCESSING; + } + isochronousReadNB(ep, buf, len); + return USB_TYPE_OK; +} + +int USBHost::ControlRead(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data, int size) { + USB_TEST_ASSERT(dev); + USBEndpoint* ep = dev->getEpCtl(); + USB_TEST_ASSERT(ep); + setAddr(dev->getAddress(), dev->getSpeed()); + token_setup(ep, setup, size); // setup stage + if (LastStatus != ACK) { + USB_DBG("setup %02x", LastStatus); + return -1; + } + int read_len = 0; + while(read_len < size) { + int size2 = std::min(size-read_len, ep->getSize()); + int result = token_in(ep, data+read_len, size2); + //USB_DBG("token_in result=%d %02x", result, LastStatus); + if (result < 0) { + USB_DBG("token_in %d/%d %02x", read_len, size, LastStatus); + return result; + } + read_len += result; + if (result < ep->getSize()) { + break; + } + } + ep->setData01(DATA1); + int result = token_out(ep); // status stage + if (result < 0) { + USB_DBG("status token_out %02x", LastStatus); + if (LastStatus == STALL) { + ep->setLengthTransferred(read_len); + return read_len; + } + return result; + } + ep->setLengthTransferred(read_len); + return read_len; +} + +int USBHost::ControlWrite(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data, int size) { + USB_TEST_ASSERT(dev); + USBEndpoint* ep = dev->getEpCtl(); + USB_TEST_ASSERT(ep); + setAddr(dev->getAddress(), dev->getSpeed()); + token_setup(ep, setup, size); // setup stage + if (LastStatus != ACK) { + USB_DBG("setup %02x", LastStatus); + return -1; + } + int write_len = 0; + if (data != NULL) { + write_len = token_out(ep, data, size); + if (write_len < 0) { + return -1; + } + } + ep->setData01(DATA1); + int result = token_in(ep); // status stage + if (result < 0) { + USB_DBG("result=%d %02x", result, LastStatus); + //return result; + } + ep->setLengthTransferred(write_len); + return write_len; +} + +int USBHost::interruptReadNB(USBEndpoint* ep, uint8_t* data, int size) +{ + USB_TEST_ASSERT(ep); + USBDeviceConnected* dev = ep->getDevice(); + USB_TEST_ASSERT(dev); + setAddr(dev->getAddress(), dev->getSpeed()); + setEndpoint(); + const int retryLimit = 0; + int read_len = 0; + for(int n = 0; read_len < size; n++) { + int size2 = std::min(size-read_len, ep->getSize()); + int result = token_in(ep, data+read_len, size2, retryLimit); + if (result < 0) { + if (LastStatus == NAK) { + if (n == 0) { + return -1; + } + break; + } + //USB_DBG("token_in result=%d %02x", result, LastStatus); + return result; + } + read_len += result; + if (result < ep->getSize()) { + break; + } + } + ep->setLengthTransferred(read_len); + return read_len; +} + +int USBHost::interruptWriteNB(USBEndpoint* ep, const uint8_t* data, int size) +{ + USB_TEST_ASSERT(ep); + USBDeviceConnected* dev = ep->getDevice(); + USB_TEST_ASSERT(dev); + setAddr(dev->getAddress(), dev->getSpeed()); + setEndpoint(); + const int retryLimit = 0; + int transferred_len = 0; + for(int n = 0; transferred_len < size; n++) { + int size2 = std::min(size-transferred_len, ep->getSize()); + int result = token_out(ep, data+transferred_len, size2, retryLimit); + if (result < 0) { + if (LastStatus == NAK) { + if (n == 0) { + return -1; + } + break; + } + //USB_DBG("token_in result=%d %02x", result, LastStatus); + return result; + } + transferred_len += result; + if (result < ep->getSize()) { + break; + } + } + ep->setLengthTransferred(transferred_len); + return transferred_len; +} + +int USBHost::bulkReadNB(USBEndpoint* ep, uint8_t* data, int size) +{ + return bulkReadBLOCK(ep, data, size, 0); +} + +int USBHost::bulkReadBLOCK(USBEndpoint* ep, uint8_t* data, int size, int timeout_ms) { + USB_TEST_ASSERT(ep); + USBDeviceConnected* dev = ep->getDevice(); + USB_TEST_ASSERT(dev); + setAddr(dev->getAddress()); + setEndpoint(); + int retryLimit = (timeout_ms == 0) ? 0 : 10; + int read_len = 0; + Timer t; + for(int n = 0; read_len < size; n++) { + int size2 = std::min(size-read_len, ep->getSize()); + int result = token_in(ep, data+read_len, size2, retryLimit); + if (result < 0) { + if (LastStatus == NAK) { + if (n == 0) { + return -1; + } + break; + } + //USB_DBG("token_in result=%d %02x", result, LastStatus); + return result; + } + read_len += result; + if (result < ep->getSize()) { + break; + } + if (timeout_ms > 0 && t.read_ms() > timeout_ms) { + USB_DBG("timeout_ms: %d", timeout_ms); + break; + } + } + ep->setLengthTransferred(read_len); + return read_len; +} + +int USBHost::bulkWriteNB(USBEndpoint* ep, const uint8_t* data, int size) { + USB_TEST_ASSERT(ep); + USBDeviceConnected* dev = ep->getDevice(); + USB_TEST_ASSERT(dev); + setAddr(dev->getAddress()); + setEndpoint(); + int write_len = 0; + for(int n = 0; write_len < size; n++) { + int size2 = std::min(size-write_len, ep->getSize()); + int result = token_out(ep, data+write_len, size2); + if (result < 0) { + if (LastStatus == NAK) { + if (n == 0) { + return -1; + } + break; + } + USB_DBG("token_out result=%d %02x", result, LastStatus); + return result; + } + write_len += result; + if (result < ep->getSize()) { + break; + } + } + ep->setLengthTransferred(write_len); + return write_len; +} + +int USBHost::isochronousReadNB(USBEndpoint* ep, uint8_t* data, int size) { + USBDeviceConnected* dev = ep->getDevice(); + USB_TEST_ASSERT(dev); + setAddr(dev->getAddress()); + int result = token_iso_in(ep, data, size); + if (result >= 0) { + ep->setLengthTransferred(result); + } + return result; +} + +void USBHost::task() +{ + if (ep_queue.empty()) { + return; + } + USBEndpoint* ep = ep_queue.pop(); + USB_TEST_ASSERT(ep); + ep->setLengthTransferred(0); + switch(ep->getType()) { + case INTERRUPT_ENDPOINT: + if (ep->getDir() == IN) { + interruptReadNB(ep, ep->getBufStart(), ep->getBufSize()); + } + break; + case BULK_ENDPOINT: + if (ep->getDir() == IN) { + bulkReadNB(ep, ep->getBufStart(), ep->getBufSize()); + } + break; + case ISOCHRONOUS_ENDPOINT: + if (ep->getDir() == IN) { + isochronousReadNB(ep, ep->getBufStart(), ep->getBufSize()); + } + break; + } + ep->call(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHost.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,196 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include "mbed.h" +#include "USBHALHost.h" +#include "USBDeviceConnected.h" +#include "IUSBEnumerator.h" +#include "USBHostConf.h" +#include "dbg.h" +#include "myvector.h" + +/** +* USBHost class +* This class is a singleton. All drivers have a reference on the static USBHost instance +*/ +class USBHost : public USBHALHost { +public: + /** + * Static method to create or retrieve the single USBHost instance + */ + static USBHost* getHostInst(); + + /** + * Control read: setup stage, data stage and status stage + * + * @param dev the control read will be done for this device + * @param requestType request type + * @param request request + * @param value value + * @param index index + * @param buf pointer on a buffer where will be store the data received + * @param len length of the transfer + * + * @returns status of the control read + */ + USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len); + + /** + * Control write: setup stage, data stage and status stage + * + * @param dev the control write will be done for this device + * @param requestType request type + * @param request request + * @param value value + * @param index index + * @param buf pointer on a buffer which will be written + * @param len length of the transfer + * + * @returns status of the control write + */ + USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len); + + /** + * Bulk read + * + * @param dev the bulk transfer will be done for this device + * @param ep USBEndpoint which will be used to read a packet + * @param buf pointer on a buffer where will be store the data received + * @param len length of the transfer + * @param blocking if true, the read is blocking (wait for completion) + * + * @returns status of the bulk read + */ + USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); + + /** + * Bulk write + * + * @param dev the bulk transfer will be done for this device + * @param ep USBEndpoint which will be used to write a packet + * @param buf pointer on a buffer which will be written + * @param len length of the transfer + * @param blocking if true, the write is blocking (wait for completion) + * + * @returns status of the bulk write + */ + USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); + + /** + * Interrupt read + * + * @param dev the interrupt transfer will be done for this device + * @param ep USBEndpoint which will be used to write a packet + * @param buf pointer on a buffer which will be written + * @param len length of the transfer + * @param blocking if true, the read is blocking (wait for completion) + * + * @returns status of the interrupt read + */ + USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); + + /** + * Interrupt write + * + * @param dev the interrupt transfer will be done for this device + * @param ep USBEndpoint which will be used to write a packet + * @param buf pointer on a buffer which will be written + * @param len length of the transfer + * @param blocking if true, the write is blocking (wait for completion) + * + * @returns status of the interrupt write + */ + USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); + + /** + * Isochronous read + * + * @param dev the isochronous transfer will be done for this device + * @param ep USBEndpoint which will be used to write a packet + * @param buf pointer on a buffer which will be written + * @param len length of the transfer + * @param blocking if true, the read is blocking (wait for completion) + * + * @returns status of the interrupt read + */ + USB_TYPE isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking = true); + + /** + * Enumerate a device. + * + * @param dev device which will be enumerated + * + * @returns status of the enumeration + */ + USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator); + + /** + * Get a device + * + * @param index index of the device which will be returned + * + * @returns pointer on the "index" device + */ + USBDeviceConnected * getDevice(uint8_t index) { + return index < DeviceLists.size() ? DeviceLists[index] : NULL; + } + + /** + * register a driver into the host associated with a callback function called when the device is disconnected + * + * @param dev device + * @param intf interface number + * @param tptr pointer to the object to call the member function on + * @param mptr pointer to the member function to be called + */ + template<typename T> + void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) { + } + + // KL46Z-USBHost extensions + int interruptReadNB(USBEndpoint* ep, uint8_t* data, int size); + int interruptWriteNB(USBEndpoint* ep, const uint8_t* data, int size); + int bulkReadNB(USBEndpoint*ep, uint8_t* data, int size); + int bulkWriteNB(USBEndpoint*ep, const uint8_t* data, int size); + int isochronousReadNB(USBEndpoint*ep, uint8_t* data, int size); + static void poll(); + +private: + USBHost(); + static USBHost* inst; + virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed); + void root_enumeration(USBDeviceConnected* dev); + void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator); + myvector<USBDeviceConnected*>DeviceLists; + + int ControlRead(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data, int size); + int ControlWrite(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data = NULL, int size = 0); + int bulkReadBLOCK(USBEndpoint*ep, uint8_t* data, int size, int timeout_ms); + void task(); + EndpointQueue ep_queue; + + // USB HUB + bool Hub(USBDeviceConnected* dev); + int SetPortPower(USBDeviceConnected* dev, int port); + int ClearPortPower(USBDeviceConnected* dev, int port); + int PortReset(USBDeviceConnected* dev, int port); + int SetPortFeature(USBDeviceConnected* dev, int feature, int index); + int ClearPortFeature(USBDeviceConnected* dev, int feature, int index); + int SetPortReset(USBDeviceConnected* dev, int port); + int GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status); +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHostConf.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,71 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USBHOST_CONF_H +#define USBHOST_CONF_H + +/* +* Maximum number of devices that can be connected +* to the usb host +*/ +#define MAX_DEVICE_CONNECTED 5 + +/* +* Maximum of Hub connected to the usb host +*/ +#define MAX_HUB_NB 2 + +/* +* Maximum number of ports on a USB hub +*/ +#define MAX_HUB_PORT 4 + +/* +* Enable USBHostMSD +*/ +#define USBHOST_MSD 1 + +/* +* Enable USBHostKeyboard +*/ +//#define USBHOST_KEYBOARD 1 + +/* +* Enable USBHostMouse +*/ +//#define USBHOST_MOUSE 1 + +/* +* Enable USBHostSerial or USBHostMultiSerial (if set > 1) +*/ +//#define USBHOST_SERIAL 1 + +/* +* Enable USB3Gmodule +*/ +//#define USBHOST_3GMODULE 1 + +/* +* Maximum number of interfaces of a usb device +*/ +#define MAX_INTF 4 + +/* +* usb_thread stack size +*/ +#define USB_THREAD_STACK (256*4 + MAX_HUB_NB*256*4) + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHostHub.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,112 @@ +#include "USBHost.h" + +#define PORT_CONNECTION 0 +#define PORT_ENABLE 1 +#define PORT_SUSPEND 2 +#define PORT_OVER_CURRENT 3 +#define PORT_RESET 4 +#define PORT_POWER 8 +#define PORT_LOW_SPEED 9 + +#define C_PORT_CONNECTION 16 +#define C_PORT_ENABLE 17 +#define C_PORT_SUSPEND 18 +#define C_PORT_OVER_CURRENT 19 +#define C_PORT_RESET 20 + +bool USBHost::Hub(USBDeviceConnected* dev) { + USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p]", dev->getVid(), dev->getPid(), dev); + HubDescriptor hubdesc; + // get HUB descriptor + int rc = controlRead(dev, + USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, + GET_DESCRIPTOR, + 0x29 << 8, 0, reinterpret_cast<uint8_t*>(&hubdesc), + sizeof(HubDescriptor)); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + if (rc != USB_TYPE_OK) { + return false; + } + USB_DBG_HEX((uint8_t*)&hubdesc, sizeof(hubdesc)); + + uint32_t status; + rc = controlRead( dev, + 0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4); + USB_TEST_ASSERT(rc == USB_TYPE_OK); + if (rc != USB_TYPE_OK) { + return false; + } + USB_DBG("HUB STATUS: %08X\n", status); + + for(int i = 1; i <= hubdesc.bNbrPorts; i++) { + SetPortPower(dev, i); // power on + wait_ms(hubdesc.bPwrOn2PwrGood*2); + uint32_t status; + GetPortStatus(dev, i, &status); + USB_DBG("port: %d status: %08X\n", i, status); + if (status & 0x010000) { // Connect Status Change, has changed + USB_TEST_ASSERT(status & 0x000001); + ClearPortFeature(dev, C_PORT_CONNECTION, i); + int lowSpeed = 0; + if (status & 0x0200) { + lowSpeed = 1; + } + PortReset(dev, i); + if (!addDevice(dev, i, lowSpeed)) { + ClearPortPower(dev, i); // power off + } + } else { + ClearPortPower(dev, i); // power off + } + } + return false; +} + + +int USBHost::SetPortPower(USBDeviceConnected* dev, int port) +{ + return SetPortFeature(dev, PORT_POWER, port); +} + +int USBHost::ClearPortPower(USBDeviceConnected* dev, int port) +{ + return ClearPortFeature(dev, PORT_POWER, port); +} + +int USBHost::SetPortFeature(USBDeviceConnected* dev, int feature, int index) +{ + return controlWrite(dev, 0x23, SET_FEATURE,feature,index,0,0); +} + +int USBHost::ClearPortFeature(USBDeviceConnected* dev, int feature, int index) +{ + return controlWrite(dev, 0x23, CLEAR_FEATURE,feature,index,0,0); +} + +int USBHost::SetPortReset(USBDeviceConnected* dev, int port) +{ + return SetPortFeature(dev, PORT_RESET, port); +} + +int USBHost::GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status) +{ + return controlRead(dev, 0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4); +} + +int USBHost::PortReset(USBDeviceConnected* dev, int port) +{ + USB_DBG("%p port=%d\n", this, port); + USB_TEST_ASSERT(port >= 1); + SetPortReset(dev, port); + // wait reset + for(int i = 0; i < 100; i++) { + uint32_t status; + GetPortStatus(dev, port, &status); + USB_DBG("RESET port: %d status: %08X\n", port, status); + if (status & 0x100000) { // Reset change , Reset complete + return USB_TYPE_OK; + } + wait_ms(5); + } + return USB_TYPE_ERROR; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/USBHostTypes.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,167 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USB_INC_H +#define USB_INC_H + +#include "mbed.h" +#include "toolchain.h" + +enum USB_TYPE { + USB_TYPE_OK = 0, + + // completion code + USB_TYPE_CRC_ERROR = 1, + USB_TYPE_BIT_STUFFING_ERROR = 2, + USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR = 3, + USB_TYPE_STALL_ERROR = 4, + USB_TYPE_DEVICE_NOT_RESPONDING_ERROR = 5, + USB_TYPE_PID_CHECK_FAILURE_ERROR = 6, + USB_TYPE_UNEXPECTED_PID_ERROR = 7, + USB_TYPE_DATA_OVERRUN_ERROR = 8, + USB_TYPE_DATA_UNDERRUN_ERROR = 9, + USB_TYPE_RESERVED = 9, + USB_TYPE_RESERVED_ = 10, + USB_TYPE_BUFFER_OVERRUN_ERROR = 12, + USB_TYPE_BUFFER_UNDERRUN_ERROR = 13, + + // general usb state + USB_TYPE_DISCONNECTED = 14, + USB_TYPE_FREE = 15, + USB_TYPE_IDLE = 16, + USB_TYPE_PROCESSING = 17, + + USB_TYPE_ERROR = 18, +}; + + +enum ENDPOINT_DIRECTION { + OUT = 1, + IN +}; + +enum ENDPOINT_TYPE { + CONTROL_ENDPOINT = 0, + ISOCHRONOUS_ENDPOINT, + BULK_ENDPOINT, + INTERRUPT_ENDPOINT +}; + +#define AUDIO_CLASS 0x01 +#define CDC_CLASS 0x02 +#define HID_CLASS 0x03 +#define MSD_CLASS 0x08 +#define HUB_CLASS 0x09 +#define SERIAL_CLASS 0x0A + +#define DEVICE_DESCRIPTOR (1) +#define CONFIGURATION_DESCRIPTOR (2) +#define INTERFACE_DESCRIPTOR (4) +#define ENDPOINT_DESCRIPTOR (5) +#define HID_DESCRIPTOR (33) + +// ----------- Control RequestType Fields ----------- +#define USB_DEVICE_TO_HOST 0x80 +#define USB_HOST_TO_DEVICE 0x00 +#define USB_REQUEST_TYPE_CLASS 0x20 +#define USB_REQUEST_TYPE_STANDARD 0x00 +#define USB_RECIPIENT_DEVICE 0x00 +#define USB_RECIPIENT_INTERFACE 0x01 +#define USB_RECIPIENT_ENDPOINT 0x02 + +// -------------- USB Standard Requests -------------- +#define GET_STATUS 0x00 +#define SET_FEATURE 0x03 +#define SET_ADDRESS 0x05 +#define GET_DESCRIPTOR 0x06 +#define SET_CONFIGURATION 0x09 +#define SET_INTERFACE 0x0b +#define CLEAR_FEATURE 0x01 + +// -------------- USB Descriptor Length -------------- +#define DEVICE_DESCRIPTOR_LENGTH 0x12 +#define CONFIGURATION_DESCRIPTOR_LENGTH 0x09 + +// PID +#define DATA0 0x03 +#define DATA1 0x0b +#define ACK 0x02 +#define STALL 0x0e +#define NAK 0x0a + +#pragma pack(push,1) +typedef struct { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} PACKED DeviceDescriptor; + +typedef struct { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t bMaxPower; +} PACKED ConfigurationDescriptor; + +typedef struct { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; +} InterfaceDescriptor; + +typedef struct { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; +} EndpointDescriptor; + +typedef struct { + uint8_t bDescLength; + uint8_t bDescriptorType; + uint8_t bNbrPorts; + uint16_t wHubCharacteristics; + uint8_t bPwrOn2PwrGood; + uint8_t bHubContrCurrent; + uint8_t DeviceRemovable; + uint8_t PortPweCtrlMak; +} HubDescriptor; +#pragma pack(pop) + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/dbg.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,93 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USB_DEBUG_H +#define USB_DEBUG_H + +//Debug is disabled by default +#ifndef DEBUG +#define DEBUG 3 /*INFO,ERR,WARN*/ +#endif +#ifndef DEBUG2 +#define DEBUG2 0 +#endif +#define DEBUG_TRANSFER 0 +#define DEBUG_EP_STATE 0 +#define DEBUG_EVENT 0 + +#if (DEBUG > 3) +#define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); +//#define USB_DBG(x, ...) std::printf("[USB_DBG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#define USB_DBG_HEX(A,B) debug_hex(A,B) +extern void debug_hex(uint8_t* buf, int size); +#define USB_DBG_ERRSTAT() report.print_errstat(); +#else +#define USB_DBG(x, ...) +#define USB_DBG_HEX(A,B) while(0) +#define USB_DBG_ERRSTAT() while(0) +#endif + +#if (DEBUG2 > 3) +#define USB_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); +#else +#define USB_DBG2(...) while(0); +#endif + +#if (DEBUG > 2) +#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");}while(0); +//#define USB_INFO(x, ...) std::printf("[USB_INFO: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define USB_INFO(x, ...) +#endif + +#if (DEBUG > 1) +//#define USB_WARN(x, ...) std::printf("[USB_WARNING: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define USB_WARN(x, ...) +#endif + +#if (DEBUG > 0) +//#define USB_ERR(x, ...) std::printf("[USB_ERR: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define USB_ERR(x, ...) +#endif + +#if (DEBUG_TRANSFER) +//#define USB_DBG_TRANSFER(x, ...) std::printf("[USB_TRANSFER: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define USB_DBG_TRANSFER(x, ...) +#endif + +#if (DEBUG_EVENT) +//#define USB_DBG_EVENT(x, ...) std::printf("[USB_EVENT: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define USB_DBG_EVENT(x, ...) +#endif + +template <bool>struct CtAssert; +template <>struct CtAssert<true> {}; +#define CTASSERT(A) CtAssert<A>(); + +#ifdef _USB_TEST +#define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; +#define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) +#else +#define USB_TEST_ASSERT(A) while(0) +#define USB_TEST_ASSERT_FALSE(A) while(0) +#endif + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/mydebug.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,13 @@ +#pragma once +template<class SERIAL_T> +void debug_hex(SERIAL_T& pc, const uint8_t* buf, int size) +{ + for(int i = 0; i < size; i++) { + pc.printf("%02x ", buf[i]); + if (i%16 == 15) { + pc.puts("\n"); + } + } + pc.puts("\n"); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/mymap.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,65 @@ +#pragma once + +template<class K,class T> +class mymap { + struct mypair { + K first; + T second; + }; +public: + mymap() { + m_size = 0; + } + T& operator[](const K& key) { + int it; + if (count(key) == 0) { + it = insert(key, 0); + } else { + it = find(key); + } + return m_buf[it].second; + } + bool empty() { return m_size == 0 ? true : false; } + int size() { return m_size; } + void clear() { m_size = 0; } + int count(K key) { + for(int i = 0; i < m_size; i++) { + if (m_buf[i].first == key) { + return 1; + } + } + return 0; + } + +private: + int find(K key) { + for(int i = 0; i < m_size; i++) { + if (m_buf[i].first == key) { + return i; + } + } + return -1; + } + int insert(K key, T value) { + int it = find(key); + if (it != -1) { + m_buf[it].second = value; + return it; + } + mypair* new_buf = new mypair[m_size+1]; + if (m_size > 0) { + for(int i = 0; i < m_size; i++) { + new_buf[i] = m_buf[i]; + } + delete[] m_buf; + } + m_buf = new_buf; + it = m_size++; + m_buf[it].first = key; + m_buf[it].second = value; + return it; + } + + int m_size; + mypair *m_buf; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHost/myvector.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,29 @@ +#pragma once + +template<class T> +class myvector { +public: + myvector() { + m_size = 0; + m_buf = NULL; + } + void push_back(T v) { + T* new_buf = new T[m_size+1]; + if (m_size > 0) { + for(int i = 0; i < m_size; i++) { + new_buf[i] = m_buf[i]; + } + delete[] m_buf; + } + m_buf = new_buf; + m_buf[m_size++] = v; + } + T& operator[](const int index) { + return m_buf[index]; + } + int size() { return m_size; } + +private: + int m_size; + T *m_buf; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHostMSD/USBHostMSD.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,356 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "USBHostMSD.h" + +#if USBHOST_MSD + +#include "dbg.h" + +#define CBW_SIGNATURE 0x43425355 +#define CSW_SIGNATURE 0x53425355 + +#define DEVICE_TO_HOST 0x80 +#define HOST_TO_DEVICE 0x00 + +#define GET_MAX_LUN (0xFE) +#define BO_MASS_STORAGE_RESET (0xFF) + +USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) +{ + host = USBHost::getHostInst(); + init(); +} + +void USBHostMSD::init() { + dev_connected = false; + dev = NULL; + bulk_in = NULL; + bulk_out = NULL; + dev_connected = false; + blockSize = 0; + blockCount = 0; + msd_intf = -1; + msd_device_found = false; + disk_init = false; + dev_connected = false; + nb_ep = 0; +} + + +bool USBHostMSD::connected() +{ + return dev_connected; +} + +bool USBHostMSD::connect() +{ + + if (dev_connected) { + return true; + } + + for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { + if ((dev = host->getDevice(i)) != NULL) { + + USB_DBG("Trying to connect MSD device\r\n"); + + if(host->enumerate(dev, this)) + break; + + if (msd_device_found) { + bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); + bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); + + if (!bulk_in || !bulk_out) + continue; + + /*USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); + dev->setName("MSD", msd_intf); + host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);*/ + + dev_connected = true; + return true; + } + } //if() + } //for() + init(); + return false; +} + +/*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) +{ + // we don't check VID/PID for MSD driver +} + +/*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed +{ + if ((msd_intf == -1) && + (intf_class == MSD_CLASS) && + (intf_subclass == 0x06) && + (intf_protocol == 0x50)) { + msd_intf = intf_nb; + return true; + } + return false; +} + +/*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used +{ + if (intf_nb == msd_intf) { + if (type == BULK_ENDPOINT) { + nb_ep++; + if (nb_ep == 2) + msd_device_found = true; + return true; + } + } + return false; +} + + +int USBHostMSD::testUnitReady() { + USB_DBG("Test unit ready"); + return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); +} + + +int USBHostMSD::readCapacity() { + USB_DBG("Read capacity"); + uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; + uint8_t result[8]; + int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); + if (status == 0) { + blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; + blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; + //USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); + } + return status; +} + + +int USBHostMSD::SCSIRequestSense() { + USB_DBG("Request sense"); + uint8_t cmd[6] = {0x03,0,0,0,18,0}; + uint8_t result[18]; + int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); + return status; +} + + +int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { + USB_DBG("Inquiry"); + uint8_t evpd = (page_code == 0) ? 0 : 1; + uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; + uint8_t result[36]; + int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); + if (status == 0) { + char vid_pid[17]; + memcpy(vid_pid, &result[8], 8); + vid_pid[8] = 0; + //USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); + + memcpy(vid_pid, &result[16], 16); + vid_pid[16] = 0; + //USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); + + memcpy(vid_pid, &result[32], 4); + vid_pid[4] = 0; + //USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); + } + return status; +} + +int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { + // if ep stalled: send clear feature + if (res == USB_TYPE_STALL_ERROR) { + res = host->controlWrite( dev, + USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, + CLEAR_FEATURE, + 0, ep->getAddress(), NULL, 0); + // set state to IDLE if clear feature successful + if (res == USB_TYPE_OK) { + ep->setState(USB_TYPE_IDLE); + } + } + + if (res != USB_TYPE_OK) + return -1; + + return 0; +} + + +int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { + + int res = 0; + + cbw.Signature = CBW_SIGNATURE; + cbw.Tag = 0; + cbw.DataLength = transfer_len; + cbw.Flags = flags; + cbw.LUN = 0; + cbw.CBLength = cmd_len; + memset(cbw.CB,0,sizeof(cbw.CB)); + if (cmd) { + memcpy(cbw.CB,cmd,cmd_len); + } + + // send the cbw + USB_DBG("Send CBW"); + res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); + if (checkResult(res, bulk_out)) + return -1; + + // data stage if needed + if (data) { + USB_DBG("data stage"); + if (flags == HOST_TO_DEVICE) { + + res = host->bulkWrite(dev, bulk_out, data, transfer_len); + if (checkResult(res, bulk_out)) + return -1; + + } else if (flags == DEVICE_TO_HOST) { + + res = host->bulkRead(dev, bulk_in, data, transfer_len); + if (checkResult(res, bulk_in)) + return -1; + } + } + + // status stage + csw.Signature = 0; + USB_DBG("Read CSW"); + res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); + if (checkResult(res, bulk_in)) + return -1; + + if (csw.Signature != CSW_SIGNATURE) { + return -1; + } + + USB_DBG("recv csw: status: %d", csw.Status); + + // ModeSense? + if ((csw.Status == 1) && (cmd[0] != 0x03)) { + USB_DBG("request mode sense"); + return SCSIRequestSense(); + } + + // perform reset recovery + if ((csw.Status == 2) && (cmd[0] != 0x03)) { + + // send Bulk-Only Mass Storage Reset request + res = host->controlWrite( dev, + USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, + BO_MASS_STORAGE_RESET, + 0, msd_intf, NULL, 0); + + // unstall both endpoints + res = host->controlWrite( dev, + USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, + CLEAR_FEATURE, + 0, bulk_in->getAddress(), NULL, 0); + + res = host->controlWrite( dev, + USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, + CLEAR_FEATURE, + 0, bulk_out->getAddress(), NULL, 0); + + } + + return csw.Status; +} + + +int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { + uint8_t cmd[10]; + memset(cmd,0,10); + cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; + + cmd[2] = (block >> 24) & 0xff; + cmd[3] = (block >> 16) & 0xff; + cmd[4] = (block >> 8) & 0xff; + cmd[5] = block & 0xff; + + cmd[7] = (nbBlock >> 8) & 0xff; + cmd[8] = nbBlock & 0xff; + + return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); +} + +int USBHostMSD::getMaxLun() { + uint8_t buf[1], res; + res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, + 0xfe, 0, msd_intf, buf, 1); + USB_DBG("max lun: %d", buf[0]); + return res; +} + +int USBHostMSD::disk_initialize() { + USB_DBG("FILESYSTEM: init"); + /* U16 */int i, timeout = 10; + + getMaxLun(); + + for (i = 0; i < timeout; i++) { + wait_ms(100); //Thread::wait(100); + if (!testUnitReady()) + break; + } + + if (i == timeout) { + disk_init = false; + return -1; + } + + inquiry(0, 0); + disk_init = 1; + return readCapacity(); +} + +int USBHostMSD::disk_write(const uint8_t *buffer, uint64_t block_number) { + USB_DBG("FILESYSTEM: write block: %lld", block_number); + if (!disk_init) { + disk_initialize(); + } + if (!disk_init) + return -1; + return dataTransfer((uint8_t *)buffer, block_number, 1, HOST_TO_DEVICE); +} + +int USBHostMSD::disk_read(uint8_t * buffer, uint64_t block_number) { + USB_DBG("FILESYSTEM: read block %lld", block_number); + if (!disk_init) { + disk_initialize(); + } + if (!disk_init) + return -1; + return dataTransfer((uint8_t *)buffer, block_number, 1, DEVICE_TO_HOST); +} + +uint64_t USBHostMSD::disk_sectors() { + USB_DBG("FILESYSTEM: sectors"); + if (!disk_init) { + disk_initialize(); + } + if (!disk_init) + return 0; + return blockCount; +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F401RE-USBHost/USBHostMSD/USBHostMSD.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,119 @@ +/* mbed USBHost Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USBHOSTMSD_H +#define USBHOSTMSD_H + +#include "USBHostConf.h" + +#if USBHOST_MSD + +#include "USBHost.h" +#include "FATFileSystem.h" + +/** + * A class to communicate a USB flash disk + */ +class USBHostMSD : public IUSBEnumerator, public FATFileSystem { +public: + /** + * Constructor + * + * @param rootdir mount name + */ + USBHostMSD(const char * rootdir); + + /** + * Check if a MSD device is connected + * + * @return true if a MSD device is connected + */ + bool connected(); + + /** + * Try to connect to a MSD device + * + * @return true if connection was successful + */ + bool connect(); + +protected: + //From IUSBEnumerator + virtual void setVidPid(uint16_t vid, uint16_t pid); + virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed + virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used + + // From FATFileSystem + virtual int disk_initialize(); + virtual int disk_status() {return 0;}; + virtual int disk_read(uint8_t * buffer, uint64_t sector); + virtual int disk_write(const uint8_t * buffer, uint64_t sector); + virtual int disk_sync() {return 0;}; + virtual uint64_t disk_sectors(); + +private: + USBHost * host; + USBDeviceConnected * dev; + bool dev_connected; + USBEndpoint * bulk_in; + USBEndpoint * bulk_out; + uint8_t nb_ep; + + // Bulk-only CBW + typedef struct { + uint32_t Signature; + uint32_t Tag; + uint32_t DataLength; + uint8_t Flags; + uint8_t LUN; + uint8_t CBLength; + uint8_t CB[16]; + } PACKED CBW; + + // Bulk-only CSW + typedef struct { + uint32_t Signature; + uint32_t Tag; + uint32_t DataResidue; + uint8_t Status; + } PACKED CSW; + + CBW cbw; + CSW csw; + + int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len); + int testUnitReady(); + int readCapacity(); + int inquiry(uint8_t lun, uint8_t page_code); + int SCSIRequestSense(); + int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction); + int checkResult(uint8_t res, USBEndpoint * ep); + int getMaxLun(); + + int blockSize; + uint64_t blockCount; + + int msd_intf; + bool msd_device_found; + bool disk_init; + + void init(); + +}; + +#endif + +#endif
--- a/main.cpp Fri Jun 13 01:57:39 2014 +0000 +++ b/main.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -1,40 +1,81 @@ #include "USBHostMSD.h" +#include "RF22.h" -DigitalOut led1(LED1); +DigitalOut led1(LED2); + +RF22 r(PTD4 , PTD6, PTD7, PTD5, PTD2); +//_slaveSelectPin(slaveSelectPin), _spi(mosi, miso, sclk), _interrupt(interrupt) -int main() { +Serial pc(USBTX, USBRX); + +int main() +{ USBHostMSD msd("usb"); - if (!msd.connect()) { + if (!msd.connect()) + { error("USB Flash drive not found.\n"); } + FILE* fp = fopen("/usb/test1.txt", "a"); - if (fp) { - fprintf(fp, "Hello from mbed.\n"); - for(int i = 0; i < 21; i++) { - fprintf(fp, " %d", i); - led1 = !led1; + if (fp) + { + fprintf(fp,"\n\r---------------------------------prijimac---------------------"); + fprintf(fp,"\n\r tlak \t\tteplota \tzrycheni \t\tcislo pak"); + fclose(fp); + } + + uint8_t data[8];//dolni bity, horni bity, teplota, zrychleni + uint8_t delka = 8; + uint8_t *ptrdelka=&delka; + + r.init(); + pc.printf("Mod zarizeni je: %d\n", r.mode()); + + r.setModeRx(); + pc.printf("Mod zarizeni je: %d\n", r.mode()); + r.setPromiscuous(true); + + while(1) + { + + //r.obsluhapreruseni(); + bool provedeno = r.recv(data, ptrdelka); + r.obsluhapreruseni(); + + //pc.printf("Prijato:"); + //pc.printf(provedeno ? "true\n" : "false\n"); + + if(provedeno == true) + { + + short tlak = data[1]<<8; + tlak = tlak + data[0]; + float press= tlak * 1.25; + pc.printf("\n\r Tlak je %f", press); + + short temp=data[2]-50; + pc.printf("\n\r Teplota je %i", temp); + + float acc = (data[3]*0.5)-12; //udavany v g + pc.printf("\n\r Zrychleni je %f", acc); + pc.printf("\n\r Cislo paketu je:%i\n", data[4]); + pc.printf("\n\r---------------------------------prijimac---------------------"); + + FILE* fp = fopen("/usb/test1.txt", "a"); + if (fp) + { + fprintf(fp,"\n\r %0.4f \t %i \t %0.2f \t %i", press, temp, acc, data[4]); + fprintf(fp, "\n"); + fclose(fp); + } } - fprintf(fp, "\n"); - fclose(fp); - } - fp = fopen("/usb/test1.txt", "r"); - if (fp) { - int n = 0; - while(1) { - int c = fgetc(fp); - if (c == EOF) { - break; - } - printf("%c", c); - n++; - led1 = !led1; + + //pc.printf("\r\n Obsah dat je: "); + //for (int a =0; a<5; a++)pc.printf(" %i", data[a]); + for(int a=0; a<8; a++) data[a]=0; + wait_ms(500); + //wait(2); + } - fclose(fp); - printf("%d bytes\n", n); - } +} - while(1) { - led1 = !led1; - wait_ms(200); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/radio22/RF22.cpp Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,798 @@ +// RF22.cpp + +#include "mbed.h" +#include "RF22.h" + +Serial pc1(USBTX, USBRX); + +// These are indexed by the values of ModemConfigChoic +// Stored in flash (program) memory to save SRAM +/*PROGMEM */ static const RF22::ModemConfig MODEM_CONFIG_TABLE[] = { + { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x00, 0x08 }, // Unmodulated carrier + { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x33, 0x08 }, // FSK, PN9 random modulation, 2, 5 + + // All the following enable FIFO with reg 71 + // 1c, 1f, 20, 21, 22, 23, 24, 25, 2c, 2d, 2e, 58, 69, 6e, 6f, 70, 71, 72 + // FSK, No Manchester, Max Rb err <1%, Xtal Tol 20ppm + { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x22, 0x08 }, // 2, 5 + { 0x1b, 0x03, 0x41, 0x60, 0x27, 0x52, 0x00, 0x07, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x22, 0x3a }, // 2.4, 36 + { 0x1d, 0x03, 0xa1, 0x20, 0x4e, 0xa5, 0x00, 0x13, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x27, 0x52, 0x2c, 0x22, 0x48 }, // 4.8, 45 + { 0x1e, 0x03, 0xd0, 0x00, 0x9d, 0x49, 0x00, 0x45, 0x40, 0x0a, 0x20, 0x80, 0x60, 0x4e, 0xa5, 0x2c, 0x22, 0x48 }, // 9.6, 45 + { 0x2b, 0x03, 0x34, 0x02, 0x75, 0x25, 0x07, 0xff, 0x40, 0x0a, 0x1b, 0x80, 0x60, 0x9d, 0x49, 0x2c, 0x22, 0x0f }, // 19.2, 9.6 + { 0x02, 0x03, 0x68, 0x01, 0x3a, 0x93, 0x04, 0xd5, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x09, 0xd5, 0x0c, 0x22, 0x1f }, // 38.4, 19.6 + { 0x06, 0x03, 0x45, 0x01, 0xd7, 0xdc, 0x07, 0x6e, 0x40, 0x0a, 0x2d, 0x80, 0x60, 0x0e, 0xbf, 0x0c, 0x22, 0x2e }, // 57.6. 28.8 + { 0x8a, 0x03, 0x60, 0x01, 0x55, 0x55, 0x02, 0xad, 0x40, 0x0a, 0x50, 0x80, 0x60, 0x20, 0x00, 0x0c, 0x22, 0xc8 }, // 125, 125 + + // GFSK, No Manchester, Max Rb err <1%, Xtal Tol 20ppm + // These differ from FSK only in register 71, for the modulation type + { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x23, 0x08 }, // 2, 5 + { 0x1b, 0x03, 0x41, 0x60, 0x27, 0x52, 0x00, 0x07, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x23, 0x3a }, // 2.4, 36 + { 0x1d, 0x03, 0xa1, 0x20, 0x4e, 0xa5, 0x00, 0x13, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x27, 0x52, 0x2c, 0x23, 0x48 }, // 4.8, 45 + { 0x1e, 0x03, 0xd0, 0x00, 0x9d, 0x49, 0x00, 0x45, 0x40, 0x0a, 0x20, 0x80, 0x60, 0x4e, 0xa5, 0x2c, 0x23, 0x48 }, // 9.6, 45 + { 0x2b, 0x03, 0x34, 0x02, 0x75, 0x25, 0x07, 0xff, 0x40, 0x0a, 0x1b, 0x80, 0x60, 0x9d, 0x49, 0x2c, 0x23, 0x0f }, // 19.2, 9.6 + { 0x02, 0x03, 0x68, 0x01, 0x3a, 0x93, 0x04, 0xd5, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x09, 0xd5, 0x0c, 0x23, 0x1f }, // 38.4, 19.6 + { 0x06, 0x03, 0x45, 0x01, 0xd7, 0xdc, 0x07, 0x6e, 0x40, 0x0a, 0x2d, 0x80, 0x60, 0x0e, 0xbf, 0x0c, 0x23, 0x2e }, // 57.6. 28.8 + { 0x8a, 0x03, 0x60, 0x01, 0x55, 0x55, 0x02, 0xad, 0x40, 0x0a, 0x50, 0x80, 0x60, 0x20, 0x00, 0x0c, 0x23, 0xc8 }, // 125, 125 + + // OOK, No Manchester, Max Rb err <1%, Xtal Tol 20ppm + { 0x51, 0x03, 0x68, 0x00, 0x3a, 0x93, 0x01, 0x3d, 0x2c, 0x11, 0x28, 0x80, 0x60, 0x09, 0xd5, 0x2c, 0x21, 0x08 }, // 1.2, 75 + { 0xc8, 0x03, 0x39, 0x20, 0x68, 0xdc, 0x00, 0x6b, 0x2a, 0x08, 0x2a, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x21, 0x08 }, // 2.4, 335 + { 0xc8, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x29, 0x04, 0x29, 0x80, 0x60, 0x27, 0x52, 0x2c, 0x21, 0x08 }, // 4.8, 335 + { 0xb8, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x28, 0x82, 0x29, 0x80, 0x60, 0x4e, 0xa5, 0x2c, 0x21, 0x08 }, // 9.6, 335 + { 0xa8, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x28, 0x41, 0x29, 0x80, 0x60, 0x9d, 0x49, 0x2c, 0x21, 0x08 }, // 19.2, 335 + { 0x98, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x28, 0x20, 0x29, 0x80, 0x60, 0x09, 0xd5, 0x0c, 0x21, 0x08 }, // 38.4, 335 + { 0x98, 0x03, 0x96, 0x00, 0xda, 0x74, 0x00, 0xdc, 0x28, 0x1f, 0x29, 0x80, 0x60, 0x0a, 0x3d, 0x0c, 0x21, 0x08 }, // 40, 335 + +}; + +RF22::RF22(PinName slaveSelectPin, PinName mosi, PinName miso, PinName sclk, PinName interrupt) + : _slaveSelectPin(slaveSelectPin), _spi(mosi, miso, sclk), _interrupt(interrupt) /*, led1(LED1), led2(LED2), led3(LED3), led4(LED4) */ +{ + + + _idleMode = RF22_XTON; // Default idle state is READY mode + _mode = RF22_MODE_IDLE; // We start up in idle mode + _rxGood = 0; + _rxBad = 0; + _txGood = 0; + + +} + +//moje dopsane metody + + void RF22::obsluhapreruseni() + { + handleInterrupt(); + } + + /* void RF22::vypisfifo() + { + _slaveSelectPin = 0; + _spi.write(RF22_REG_7F_FIFO_ACCESS); + pc1.printf("\n\r Obsah FIFA je:"); + for(int a = 0; a<10; a++) + { + uint8_t x = _spi.write(RF22_REG_7F_FIFO_ACCESS); + pc1.printf("\t %i", x); + } + _slaveSelectPin = 1; + }*/ + +boolean RF22::init() +{ + wait_ms(16); + + _slaveSelectPin = 1; // cip select + + wait_ms(100); + + _spi.format(8,0); //konfigurace SPI + _spi.frequency(10000000); + + // Software reset the device + reset(); + + // Get the device type and check it + // This also tests whether we are really connected to a device + _deviceType = spiRead(RF22_REG_00_DEVICE_TYPE); + + if ( _deviceType != RF22_DEVICE_TYPE_RX_TRX + && _deviceType != RF22_DEVICE_TYPE_TX) + return false; + + _interrupt.fall(this, &RF22::isr0); + + clearTxBuf(); + clearRxBuf(); + + // Most of these are the POR default + spiWrite(RF22_REG_7D_TX_FIFO_CONTROL2, RF22_TXFFAEM_THRESHOLD); + spiWrite(RF22_REG_7E_RX_FIFO_CONTROL, RF22_RXFFAFULL_THRESHOLD); + spiWrite(RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM); + // Configure the message headers + // Here we set up the standard packet format for use by the RF22 library + // 8 nibbles preamble + // 2 SYNC words 2d, d4 + // Header length 4 (to, from, id, flags) + // 1 octet of data length (0 to 255) + // 0 to 255 octets data + // 2 CRC octets as CRC16(IBM), computed on the header, length and data + // On reception the to address is check for validity against RF22_REG_3F_CHECK_HEADER3 + // or the broadcast address of 0xff + // If no changes are made after this, the transmitted + // to address will be 0xff, the from address will be 0xff + // and all such messages will be accepted. This permits the out-of the box + // RF22 config to act as an unaddresed, unreliable datagram service + spiWrite(RF22_REG_32_HEADER_CONTROL1, RF22_BCEN_HEADER3 | RF22_HDCH_HEADER3); + spiWrite(RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2); + setPreambleLength(8); //delka preambule + uint8_t syncwords[] = { 0x2d, 0xd4 }; + setSyncWords(syncwords, sizeof(syncwords)); + setPromiscuous(false); + // Check the TO header against RF22_DEFAULT_NODE_ADDRESS + spiWrite(RF22_REG_3F_CHECK_HEADER3, RF22_DEFAULT_NODE_ADDRESS); + // Set the default transmit header values + setHeaderTo(RF22_DEFAULT_NODE_ADDRESS); + setHeaderFrom(RF22_DEFAULT_NODE_ADDRESS); + setHeaderId(0); + setHeaderFlags(0); + + // Ensure the antenna can be switched automatically according to transmit and receive + // This assumes GPIO0(out) is connected to TX_ANT(in) to enable tx antenna during transmit + // This assumes GPIO1(out) is connected to RX_ANT(in) to enable rx antenna during receive + spiWrite (RF22_REG_0B_GPIO_CONFIGURATION0, 0x12) ; // TX state + spiWrite (RF22_REG_0C_GPIO_CONFIGURATION1, 0x15) ; // RX state + + // Enable interrupts + + spiWrite(RF22_REG_05_INTERRUPT_ENABLE1, RF22_ENTXFFAEM |RF22_ENRXFFAFULL | RF22_ENPKSENT |RF22_ENPKVALID| RF22_ENCRCERROR); + spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL); + + // Set some defaults. An innocuous ISM frequency, and reasonable pull-in + setFrequency(434.0, 0.05); +// setFrequency(900.0); + // Some slow, reliable default speed and modulation + setModemConfig(FSK_Rb2_4Fd36); +// setModemConfig(FSK_Rb125Fd125); + // Minimum power + setTxPower(RF22_TXPOW_8DBM); +// setTxPower(RF22_TXPOW_17DBM); + + + + return true; +} + +// C++ level interrupt handler for this instance +void RF22::handleInterrupt() +{ + uint8_t _lastInterruptFlags[2]; + + //led1 = 1; + + // Read the interrupt flags which clears the interrupt + spiBurstRead(RF22_REG_03_INTERRUPT_STATUS1, _lastInterruptFlags, 2); + +#if 0 + // Caution: Serial printing in this interrupt routine can cause mysterious crashes + Serial.print("interrupt "); + Serial.print(_lastInterruptFlags[0], HEX); + Serial.print(" "); + Serial.println(_lastInterruptFlags[1], HEX); + if (_lastInterruptFlags[0] == 0 && _lastInterruptFlags[1] == 0) + Serial.println("FUNNY: no interrupt!"); +#endif + +#if 0 + // TESTING: fake an RF22_IFFERROR + static int counter = 0; + if (_lastInterruptFlags[0] & RF22_IPKSENT && counter++ == 10) { + _lastInterruptFlags[0] = RF22_IFFERROR; + counter = 0; + } +#endif + + + if (_lastInterruptFlags[0] & RF22_IFFERROR) { +// Serial.println("IFFERROR"); + //led4 = !led4; + resetFifos(); // Clears the interrupt + if (_mode == RF22_MODE_TX) + restartTransmit(); + else if (_mode == RF22_MODE_RX){ + clearRxBuf(); + //stop and start Rx + setModeIdle(); + setModeRx(); + } + // stop handling the remaining interruppts as something went wrong here + return; + } + + // Caution, any delay here may cause a FF underflow or overflow + if (_lastInterruptFlags[0] & RF22_ITXFFAEM) { + // See if more data has to be loaded into the Tx FIFO + //led2 = !led2; + sendNextFragment(); +// Serial.println("ITXFFAEM"); + } + + if (_lastInterruptFlags[0] & RF22_IRXFFAFULL) { + // Caution, any delay here may cause a FF overflow + // Read some data from the Rx FIFO + //led4 = !led4; + readNextFragment(); +// Serial.println("IRXFFAFULL"); + } + if (_lastInterruptFlags[0] & RF22_IEXT) { + // This is not enabled by the base code, but users may want to enable it + //led2 = !led2; + handleExternalInterrupt(); +// Serial.println("IEXT"); + } + if (_lastInterruptFlags[1] & RF22_IWUT) { + // This is not enabled by the base code, but users may want to enable it + //led2 = !led2; + handleWakeupTimerInterrupt(); +// Serial.println("IWUT"); + } + if (_lastInterruptFlags[0] & RF22_IPKSENT) { +// Serial.println("IPKSENT"); + _txGood++; + //led4 = !led4; + // Transmission does not automatically clear the tx buffer. + // Could retransmit if we wanted + // RF22 transitions automatically to Idle + _mode = RF22_MODE_IDLE; + } + + if (_lastInterruptFlags[0] & RF22_IPKVALID) { + uint8_t len = spiRead(RF22_REG_4B_RECEIVED_PACKET_LENGTH); +// Serial.println("IPKVALID"); +// Serial.println(len); +// Serial.println(_bufLen); + + // May have already read one or more fragments + // Get any remaining unread octets, based on the expected length + // First make sure we dont overflow the buffer in the case of a stupid length + // or partial bad receives + + if ( len > RF22_MAX_MESSAGE_LEN || len < _bufLen) //pokud je delka zpravy delsi nez FIFO + { + _rxBad++; + _mode = RF22_MODE_IDLE; + clearRxBuf(); + return; // Hmmm receiver buffer overflow. + } + + spiBurstRead(RF22_REG_7F_FIFO_ACCESS, _buf + _bufLen, len - _bufLen); + __disable_irq(); // Disable Interrupts + _rxGood++; + _bufLen = len; + _mode = RF22_MODE_IDLE; + _rxBufValid = true; + // reset the fifo for next packet?? + //resetRxFifo(); + __enable_irq(); // Enable Interrupts + + //led3 = !led3; + + } + + if (_lastInterruptFlags[0] & RF22_ICRCERROR) { +// Serial.println("ICRCERR"); + _rxBad++; + //led2 = !led2; + clearRxBuf(); + resetRxFifo(); + _mode = RF22_MODE_IDLE; + setModeRx(); // Keep trying + } + + if (_lastInterruptFlags[1] & RF22_IPREAVAL) { +// Serial.println("IPREAVAL"); + + _lastRssi = spiRead(RF22_REG_26_RSSI); + + + // why clear the rx-buf here? charly + clearRxBuf(); + + + } + //led1 = 0; +} + +// These are low level functions that call the interrupt handler for the correct +// instance of RF22. +// 2 interrupts allows us to have 2 different devices + +void RF22::isr0() +{ + //handleInterrupt(); + obsluhapreruseni(); + +} + + +void RF22::reset() +{ + spiWrite(RF22_REG_07_OPERATING_MODE1, RF22_SWRES); //soft reset + wait_ms(1); +} + +uint8_t RF22::spiRead(uint8_t reg) +{ + __disable_irq(); // Disable Interrupts + + _slaveSelectPin=0; + + _spi.write(reg & ~RF22_SPI_WRITE_MASK); // Send the address with the write mask off + uint8_t val = _spi.write(0); // The written value is ignored, reg value is read ?????? + + _slaveSelectPin = 1; + __enable_irq(); // Enable Interrupts + return val; +} + +void RF22::spiWrite(uint8_t reg, uint8_t val) //zapis hodnot z define +{ + __disable_irq(); // Disable Interrupts + + _slaveSelectPin = 0; + _spi.write(reg | RF22_SPI_WRITE_MASK); // Send the address with the write mask on + _spi.write(val); // New value follows + + _slaveSelectPin = 1; + __enable_irq(); // Enable Interrupts +} + +void RF22::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len) //tady se vycitaji data +{ + _slaveSelectPin = 0; + _spi.write(reg & ~RF22_SPI_WRITE_MASK); // Send the start address with the write mask off + if(reg == RF22_REG_7F_FIFO_ACCESS) pc1.printf("\n\r Data primo z bufferu:"); + while (len--) + { + *dest++ = _spi.write(0); + if(reg == RF22_REG_7F_FIFO_ACCESS) pc1.printf(" %i", *dest); + } + _slaveSelectPin = 1; +} + +void RF22::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len) +{ + _slaveSelectPin = 0; + _spi.write(reg | RF22_SPI_WRITE_MASK); // Send the start address with the write mask on + while (len--) + _spi.write(*src++); + _slaveSelectPin = 1; +} + +uint8_t RF22::statusRead() +{ + return spiRead(RF22_REG_02_DEVICE_STATUS); +} + +uint8_t RF22::adcRead(uint8_t adcsel, + uint8_t adcref , + uint8_t adcgain, + uint8_t adcoffs) +{ + uint8_t configuration = adcsel | adcref | (adcgain & RF22_ADCGAIN); + spiWrite(RF22_REG_0F_ADC_CONFIGURATION, configuration | RF22_ADCSTART); + spiWrite(RF22_REG_10_ADC_SENSOR_AMP_OFFSET, adcoffs); + + // Conversion time is nominally 305usec + // Wait for the DONE bit + while (!(spiRead(RF22_REG_0F_ADC_CONFIGURATION) & RF22_ADCDONE)) + ; + // Return the value + return spiRead(RF22_REG_11_ADC_VALUE); +} + + +uint16_t RF22::wutRead() +{ + uint8_t buf[2]; + spiBurstRead(RF22_REG_17_WAKEUP_TIMER_VALUE1, buf, 2); + return ((uint16_t)buf[0] << 8) | buf[1]; // Dont rely on byte order +} + +// RFM-22 doc appears to be wrong: WUT for wtm = 10000, r, = 0, d = 0 is about 1 sec +void RF22::setWutPeriod(uint16_t wtm, uint8_t wtr, uint8_t wtd) +{ + uint8_t period[3]; + + period[0] = ((wtr & 0xf) << 2) | (wtd & 0x3); + period[1] = wtm >> 8; + period[2] = wtm & 0xff; + spiBurstWrite(RF22_REG_14_WAKEUP_TIMER_PERIOD1, period, sizeof(period)); +} + +// Returns true if centre + (fhch * fhs) is within limits +// Caution, different versions of the RF22 support different max freq +// so YMMV +boolean RF22::setFrequency(float centre, float afcPullInRange) +{ + uint8_t fbsel = RF22_SBSEL; + uint8_t afclimiter; + if (centre < 240.0 || centre > 960.0) // 930.0 for early silicon + return false; + if (centre >= 480.0) { + if (afcPullInRange < 0.0 || afcPullInRange > 0.318750) + return false; + centre /= 2; + fbsel |= RF22_HBSEL; + afclimiter = afcPullInRange * 1000000.0 / 1250.0; + } else { + if (afcPullInRange < 0.0 || afcPullInRange > 0.159375) + return false; + afclimiter = afcPullInRange * 1000000.0 / 625.0; + } + centre /= 10.0; + float integerPart = floor(centre); + float fractionalPart = centre - integerPart; + + uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23 + fbsel |= fb; + uint16_t fc = fractionalPart * 64000; + spiWrite(RF22_REG_73_FREQUENCY_OFFSET1, 0); // REVISIT + spiWrite(RF22_REG_74_FREQUENCY_OFFSET2, 0); + spiWrite(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel); + spiWrite(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8); + spiWrite(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff); + spiWrite(RF22_REG_2A_AFC_LIMITER, afclimiter); + return !(statusRead() & RF22_FREQERR); +} + +// Step size in 10kHz increments +// Returns true if centre + (fhch * fhs) is within limits +boolean RF22::setFHStepSize(uint8_t fhs) +{ + spiWrite(RF22_REG_7A_FREQUENCY_HOPPING_STEP_SIZE, fhs); + return !(statusRead() & RF22_FREQERR); +} + +// Adds fhch * fhs to centre frequency +// Returns true if centre + (fhch * fhs) is within limits +boolean RF22::setFHChannel(uint8_t fhch) +{ + spiWrite(RF22_REG_79_FREQUENCY_HOPPING_CHANNEL_SELECT, fhch); + return !(statusRead() & RF22_FREQERR); +} + +uint8_t RF22::rssiRead() +{ + return spiRead(RF22_REG_26_RSSI); +} + +uint8_t RF22::ezmacStatusRead() +{ + return spiRead(RF22_REG_31_EZMAC_STATUS); +} + +void RF22::setMode(uint8_t mode) +{ + spiWrite(RF22_REG_07_OPERATING_MODE1, mode); +} + +void RF22::setModeIdle() +{ + if (_mode != RF22_MODE_IDLE) { + setMode(_idleMode); + _mode = RF22_MODE_IDLE; + + } +} + +void RF22::setModeRx() +{ + if (_mode != RF22_MODE_RX) { + setMode(_idleMode | RF22_RXON); + _mode = RF22_MODE_RX; + } +} + +void RF22::setModeTx() +{ + if (_mode != RF22_MODE_TX) { + setMode(_idleMode | RF22_TXON); + _mode = RF22_MODE_TX; + // Hmmm, if you dont clear the RX FIFO here, then it appears that going + // to transmit mode in the middle of a receive can corrupt the + // RX FIFO + resetRxFifo(); + + } +} + +uint8_t RF22::mode() +{ + return _mode; +} + +void RF22::setTxPower(uint8_t power) +{ + spiWrite(RF22_REG_6D_TX_POWER, power); +} + +// Sets registers from a canned modem configuration structure +void RF22::setModemRegisters(const ModemConfig* config) +{ + spiWrite(RF22_REG_1C_IF_FILTER_BANDWIDTH, config->reg_1c); + spiWrite(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE, config->reg_1f); + spiBurstWrite(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE, &config->reg_20, 6); + spiBurstWrite(RF22_REG_2C_OOK_COUNTER_VALUE_1, &config->reg_2c, 3); + spiWrite(RF22_REG_58_CHARGE_PUMP_CURRENT_TRIMMING, config->reg_58); + spiWrite(RF22_REG_69_AGC_OVERRIDE1, config->reg_69); + spiBurstWrite(RF22_REG_6E_TX_DATA_RATE1, &config->reg_6e, 5); +} + +// Set one of the canned FSK Modem configs +// Returns true if its a valid choice +boolean RF22::setModemConfig(ModemConfigChoice index) +{ + if (index > (sizeof(MODEM_CONFIG_TABLE) / sizeof(ModemConfig))) + return false; + + RF22::ModemConfig cfg; + memcpy(&cfg, &MODEM_CONFIG_TABLE[index], sizeof(RF22::ModemConfig)); + setModemRegisters(&cfg); + + return true; +} + +// REVISIT: top bit is in Header Control 2 0x33 +void RF22::setPreambleLength(uint8_t nibbles) +{ + spiWrite(RF22_REG_34_PREAMBLE_LENGTH, nibbles); +} + +// Caution doesnt set sync word len in Header Control 2 0x33 +void RF22::setSyncWords(const uint8_t* syncWords, uint8_t len) +{ + spiBurstWrite(RF22_REG_36_SYNC_WORD3, syncWords, len); +} + +void RF22::clearRxBuf() +{ + __disable_irq(); // Disable Interrupts + _bufLen = 0; + _rxBufValid = false; + __enable_irq(); // Enable Interrupts +} + +boolean RF22::available() +{ + if (!_rxBufValid) + setModeRx(); // Make sure we are receiving + return _rxBufValid; +} + +// Blocks until a valid message is received +void RF22::waitAvailable() +{ + while (!available()) + ; +} + +// Blocks until a valid message is received or timeout expires +// Return true if there is a message available +bool RF22::waitAvailableTimeout(uint16_t timeout) +{ + Timer t; + t.start(); + unsigned long endtime = t.read_ms() + timeout; + while (t.read_ms() < endtime) + if (available()) + return true; + return false; +} + +void RF22::waitPacketSent() +{ + while (_mode == RF22_MODE_TX) + ; // Wait for any previous transmit to finish +} + +// Diagnostic help +void RF22::printBuffer(const uint8_t *buf, uint8_t len) +{ + + uint8_t i; + + pc1.printf("\n\rObsah Bufferu "); + for (i = 0; i < len; i++) { + if (i % 16 == 15) + pc1.printf("%d", buf[i]); + else { + pc1.printf("%d", buf[i]); + pc1.printf(" "); + } + } + pc1.printf(" "); + +} + +boolean RF22::recv(uint8_t* buf, uint8_t* len) +{ + if (!available()) + return false; + __disable_irq(); // Disable Interrupts + if (*len > _bufLen) + *len = _bufLen; + memcpy(buf, _buf, *len); + printBuffer(_buf, *len); + clearRxBuf(); + __enable_irq(); // Enable Interrupts + printBuffer( buf, *len); + + return true; +} + +void RF22::clearTxBuf() +{ + __disable_irq(); // Disable Interrupts + _bufLen = 0; + _txBufSentIndex = 0; + _txPacketSent = false; + __enable_irq(); // Enable Interrupts +} + +void RF22::startTransmit() +{ + sendNextFragment(); // Actually the first fragment + spiWrite(RF22_REG_3E_PACKET_LENGTH, _bufLen); // Total length that will be sent + setModeTx(); // Start the transmitter, turns off the receiver +} + +// Restart the transmission of a packet that had a problem +void RF22::restartTransmit() +{ + _mode = RF22_MODE_IDLE; + _txBufSentIndex = 0; +// Serial.println("Restart"); + startTransmit(); +} + +boolean RF22::send(const uint8_t* data, uint8_t len) //pocka, nez se odesle paket a potom odesle data +{ + waitPacketSent(); + { + if (!fillTxBuf(data, len)) + return false; + startTransmit(); + } +// printBuffer("send:", data, len); + return true; +} + +boolean RF22::fillTxBuf(const uint8_t* data, uint8_t len) +{ + clearTxBuf(); + if (!len) + return false; + return appendTxBuf(data, len); +} + +boolean RF22::appendTxBuf(const uint8_t* data, uint8_t len) +{ + if (((uint16_t)_bufLen + len) > RF22_MAX_MESSAGE_LEN) + return false; + __disable_irq(); // Disable Interrupts + memcpy(_buf + _bufLen, data, len); + _bufLen += len; + __enable_irq(); // Enable Interrupts + +// printBuffer("txbuf:", _buf, _bufLen); + return true; +} + +// Assumption: there is currently <= RF22_TXFFAEM_THRESHOLD bytes in the Tx FIFO +void RF22::sendNextFragment() +{ + if (_txBufSentIndex < _bufLen) { + // Some left to send? + uint8_t len = _bufLen - _txBufSentIndex; + // But dont send too much + if (len > (RF22_FIFO_SIZE - RF22_TXFFAEM_THRESHOLD - 1)) + len = (RF22_FIFO_SIZE - RF22_TXFFAEM_THRESHOLD - 1); + spiBurstWrite(RF22_REG_7F_FIFO_ACCESS, _buf + _txBufSentIndex, len); + _txBufSentIndex += len; + } +} + +// Assumption: there are at least RF22_RXFFAFULL_THRESHOLD in the RX FIFO +// That means it should only be called after a RXFFAFULL interrupt +void RF22::readNextFragment() +{ + if (((uint16_t)_bufLen + RF22_RXFFAFULL_THRESHOLD) > RF22_MAX_MESSAGE_LEN) + return; // Hmmm receiver overflow. Should never occur + + // Read the RF22_RXFFAFULL_THRESHOLD octets that should be there + spiBurstRead(RF22_REG_7F_FIFO_ACCESS, _buf + _bufLen, RF22_RXFFAFULL_THRESHOLD); + _bufLen += RF22_RXFFAFULL_THRESHOLD; +} + +// Clear the FIFOs +void RF22::resetFifos() +{ + spiWrite(RF22_REG_08_OPERATING_MODE2, RF22_FFCLRRX | RF22_FFCLRTX); + spiWrite(RF22_REG_08_OPERATING_MODE2, 0); +} + +// Clear the Rx FIFO +void RF22::resetRxFifo() +{ + spiWrite(RF22_REG_08_OPERATING_MODE2, RF22_FFCLRRX); + spiWrite(RF22_REG_08_OPERATING_MODE2, 0); +} + +// CLear the TX FIFO +void RF22::resetTxFifo() +{ + spiWrite(RF22_REG_08_OPERATING_MODE2, RF22_FFCLRTX); + spiWrite(RF22_REG_08_OPERATING_MODE2, 0); +} + +// Default implmentation does nothing. Override if you wish +void RF22::handleExternalInterrupt() +{ +} + +// Default implmentation does nothing. Override if you wish +void RF22::handleWakeupTimerInterrupt() +{ +} + +void RF22::setHeaderTo(uint8_t to) +{ + spiWrite(RF22_REG_3A_TRANSMIT_HEADER3, to); +} + +void RF22::setHeaderFrom(uint8_t from) +{ + spiWrite(RF22_REG_3B_TRANSMIT_HEADER2, from); +} + +void RF22::setHeaderId(uint8_t id) +{ + spiWrite(RF22_REG_3C_TRANSMIT_HEADER1, id); +} + +void RF22::setHeaderFlags(uint8_t flags) +{ + spiWrite(RF22_REG_3D_TRANSMIT_HEADER0, flags); +} + +uint8_t RF22::headerTo() +{ + return spiRead(RF22_REG_47_RECEIVED_HEADER3); +} + +uint8_t RF22::headerFrom() +{ + return spiRead(RF22_REG_48_RECEIVED_HEADER2); +} + +uint8_t RF22::headerId() +{ + return spiRead(RF22_REG_49_RECEIVED_HEADER1); +} + +uint8_t RF22::headerFlags() +{ + return spiRead(RF22_REG_4A_RECEIVED_HEADER0); +} + +uint8_t RF22::lastRssi() +{ + return _lastRssi; +} + +void RF22::setPromiscuous(boolean promiscuous) +{ + spiWrite(RF22_REG_43_HEADER_ENABLE3, promiscuous ? 0x00 : 0xff); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/radio22/RF22.h Sat Apr 04 20:16:39 2015 +0000 @@ -0,0 +1,1143 @@ +// RF22.h +// Author: Mike McCauley (mikem@open.com.au) +// Copyright (C) 2011 Mike McCauley +// $Id: RF22.h,v 1.23 2013/02/06 21:33:56 mikem Exp mikem $ +// +// ported to mbed by Karl Zweimueller +/// \mainpage RF22 library for Arduino +/// +/// This is the Arduino RF22 library. +/// It provides an object-oriented interface for sending and receiving data messages with Hope-RF +/// RF22B based radio modules, and compatible chips and modules, +/// including the RFM22B transceiver module such as +/// this bare module: http://www.sparkfun.com/products/10153 +/// and this shield: https://www.sparkfun.com/products/11018 +/// +/// RF22 also supports some of the features of ZigBee and XBee, +/// (such as mesh routing and automatic route discovery), +/// but with a much less complicated system and less expensive radios. +/// +/// The Hope-RF (http://www.hoperf.com) RFM22B (http://www.hoperf.com/rf_fsk/fsk/RFM22B.htm) +/// is a low-cost ISM transceiver module. It supports FSK, GFSK, OOK over a wide +/// range of frequencies and programmable data rates. +/// +/// This library provides functions for sending and receiving messages of up to 255 octets on any +/// frequency supported by the RF22B, in a range of predefined data rates and frequency deviations. +/// Frequency can be set with 312Hz precision to any frequency from 240.0MHz to 960.0MHz. +/// +/// Up to 2 RF22B modules can be connected to an Arduino, permitting the construction of translators +/// and frequency changers, etc. +/// +/// This library provides classes for +/// - RF22: unaddressed, unreliable messages +/// - RF22Datagram: addressed, unreliable messages +/// - RF22ReliableDatagram: addressed, reliable, retransmitted, acknowledged messages. +/// - RF22Router: multi hop delivery from source node to destination node via 0 or more intermediate nodes +/// - RF22Mesh: multi hop delivery with automatic route discovery and rediscovery. +/// +/// The following modulation types are suppported with a range of modem configurations for +/// common data rates and frequency deviations: +/// - GFSK Gaussian Frequency Shift Keying +/// - FSK Frequency Shift Keying +/// - OOK On-Off Keying +/// +/// Support for other RF22B features such as on-chip temperature measurement, analog-digital +/// converter, transmitter power control etc is also provided. +/// +/// The latest version of this documentation can be downloaded from +/// http://www.open.com.au/mikem/arduino/RF22 +/// +/// \par Packet Format +/// +/// All messages sent and received by this RF22 library must conform to this packet format: +/// +/// - 8 nibbles (4 octets) PREAMBLE +/// - 2 octets SYNC 0x2d, 0xd4 +/// - 4 octets HEADER: (TO, FROM, ID, FLAGS) +/// - 1 octet LENGTH (0 to 255), number of octets in DATA +/// - 0 to 255 octets DATA +/// - 2 octets CRC computed with CRC16(IBM), computed on HEADER, LENGTH and DATA +/// +/// \par Connecting RFM-22 to Arduino +/// +/// If you have the Sparkfun RFM22 Shield (https://www.sparkfun.com/products/11018) +/// the connections described below are done for you on the shield, no changes required, +/// just add headers and plug it in to an Arduino (but not and Arduino Mega, see below) +/// +/// The physical connection between the RF22B and the Arduino require 3.3V, the 3 x SPI pins (SCK, SDI, SDO), +/// a Slave Select pin and an interrupt pin. +/// Note also that on the RFF22B, it is required to control the TX_ANT and X_ANT pins of the RFM22 in order to enable the +/// antenna connection. The RF22 library is configured so that GPIO0 and GPIO1 outputs can control TX_ANT and RX_ANT input pins +/// automatically. You must connect GPIO0 to TX_ANT and GPIO1 to RX_ANT for this automatic antenna switching to occur. + +/// \par Interrupts +/// +/// The RF22 library uses interrupts to react to events in the RF22 module, +/// such as the reception of a new packet, or the completion of transmission of a packet. +/// The RF22 library interrupt service routine reads status from and writes data +/// to the the RF22 module via the SPI interface. It is very important therefore, +/// that if you are using the RF22 library with another SPI based deviced, that you +/// disable interrupts while you transfer data to and from that other device. +/// Use cli() to disable interrupts and sei() to reenable them. +/// +/// \par Memory +/// +/// The RF22 library requires non-trivial amounts of memory. The sample programs above all compile to +/// about 9 to 14kbytes each, which will fit in the flash proram memory of most Arduinos. However, +/// the RAM requirements are more critical. Most sample programs above will run on Duemilanova, +/// but not on Diecimila. Even on Duemilanova, the RAM requirements are very close to the +/// available memory of 2kbytes. Therefore, you should be vary sparing with RAM use in programs that use +/// the RF22 library on Duemilanova. +/// +/// The sample RF22Router and RF22Mesh programs compile to about 14kbytes, +/// and require more RAM than the others. +/// They will not run on Duemilanova or Diecimila, but will run on Arduino Mega. +/// +/// It is often hard to accurately identify when you are hitting RAM limits on Arduino. +/// The symptoms can include: +/// - Mysterious crashes and restarts +/// - Changes in behaviour when seemingly unrelated changes are made (such as adding print() statements) +/// - Hanging +/// - Output from Serial.print() not appearing +/// +/// With an Arduino Mega, with 8 kbytes of SRAM, there is much more RAM headroom for +/// your own elaborate programs. +/// This library is reported to work with Arduino Pro Mini, but that has not been tested by me. +/// +/// The Arduino UNO is now known to work with RF22. +/// +/// \par Automatic Frequency Control (AFC) +/// +/// The RF22M modules use an inexpensive crystal to control the frequency synthesizer, and therfore you can expect +/// the transmitter and receiver frequencies to be subject to the usual inaccuracies of such crystals. The RF22 +/// contains an AFC circuit to compensate for differences in transmitter and receiver frequencies. +/// It does this by altering the receiver frequency during reception by up to the pull-in frequency range. +/// This RF22 library enables the AFC and by default sets the pull-in frequency range to +/// 0.05MHz, which should be sufficient to handle most situations. However, if you observe unexplained packet losses +/// or failure to operate correctly all the time it may be because your modules have a wider frequency difference, and +/// you may need to set the afcPullInRange to a differentvalue, using setFrequency(); +/// +/// \par Performance +/// +/// Some simple speed performance tests have been conducted. +/// In general packet transmission rate will be limited by the modulation scheme. +/// Also, if your code does any slow operations like Serial printing it will also limit performance. +/// We disabled any printing in the tests below. +/// We tested with RF22::GFSK_Rb125Fd125, which is probably the fastest scheme available. +/// We tested with a 13 octet message length, over a very short distance of 10cm. +/// +/// Transmission (no reply) tests with modulation RF22::GFSK_Rb125Fd125 and a +/// 13 octet message show about 330 messages per second transmitted. +/// +/// Transmit-and-wait-for-a-reply tests with modulation RF22::GFSK_Rb125Fd125 and a +/// 13 octet message (send and receive) show about 160 round trips per second. +/// +/// \par Installation +/// +/// Install in the usual way: unzip the distribution zip file to the libraries +/// sub-folder of your sketchbook. +/// +/// This software is Copyright (C) 2011 Mike McCauley. Use is subject to license +/// conditions. The main licensing options available are GPL V2 or Commercial: +/// +/// \par Open Source Licensing GPL V2 +/// +/// This is the appropriate option if you want to share the source code of your +/// application with everyone you distribute it to, and you also want to give them +/// the right to share who uses it. If you wish to use this software under Open +/// Source Licensing, you must contribute all your source code to the open source +/// community in accordance with the GPL Version 2 when your application is +/// distributed. See http://www.gnu.org/copyleft/gpl.html +/// +/// \par Commercial Licensing +/// +/// This is the appropriate option if you are creating proprietary applications +/// and you are not prepared to distribute and share the source code of your +/// application. Contact info@open.com.au for details. +/// +/// \par Revision History +/// +/// \version 1.0 Initial release +/// +/// \version 1.1 Added rf22_snoop and rf22_specan examples +/// +/// \version 1.2 Changed default modulation to FSK_Rb2_4Fd36 +/// Some internal reorganisation. +/// Added RF22Router and RF22Mesh classes plus sample programs to support multi-hop and +/// automatic route discovery. +/// \version 1.3 Removed some unnecessary debug messages. Added virtual doArp and isPhysicalAddress +/// functions to RF22Mesh to support other physical address interpretation schemes (IPV4/IPV6?) +/// \version 1.4 RF22Router and RF22Mesh were inadvertently left out of the distro. +/// \version 1.5 Improvements contributed by Peter Mousley: Modem config table is now in flash rather than SRAM, +/// saving 400 bytes of SRAM. Allow a user-defined buffer size. Thanks Peter. +/// \version 1.6 Fixed some minor typos on doc and clarified that this code is for the RF22B. Fixed errors in the +/// definition of the power output constants which were incorrectly set to the values for the RF22. +/// Reported by Fred Slamen. If you were using a previous version of RF22, you probably were not getting the output +/// power you thought. +/// \version 1.7 Added code to initialise GPIO0 and GPIO1 so they can automatically control the TX_ANT and RX_ANT +/// antenna switching inputs. You must connect GPIO0 to TX_ANT and GPIO1 to RX_ANT for this automatic +/// antenna switching to occur. Updated doc to reflect this new connection requirement +/// \version 1.8 Changed the name of RF22_ENLBD in RF22_REG_06_INTERRUPT_ENABLE2 to RF22_ENLBDI because it collided +/// with a define of the same name in RF22_REG_07_OPERATING_MODE. RF22_REG_05_INTERRUPT_ENABLE1 enable mask +/// incorrectly used RF22_IFFERROR instead of RF22_ENFFERR. Reported by Steffan Woltjer. +/// \version 1.9 Fixed typos in RF22_REG_21_CLOCk*. Reported by Steffan Woltjer. +/// \version 1.10 Fixed a problem where a IFFERR during transmission could cause an infinite loop and a hang. +/// Reported by Raymond Gilbert. +/// \version 1.11 Fixed an innocuous typo in RF22::handleInterrupt. Reported by Zhentao. +/// +/// \version 1.12 Improvements to RF22::init from Guy Molinari to improve compatibility with some +/// Arduinos. Now reported to be working with official Mega 2560 and Uno. +/// Updated so compiles on Arduino 1.0. +/// +/// \version 1.13 Announce google support group +/// +/// \version 1.14 Added definitions for bits and masks in RF22_REG_1D_AFC_LOOP_GEARSHIFT_OVERRIDE +/// and RF22_REG_1E_AFC_TIMING_CONTROL +/// +/// \version 1.15 Small alterations to initialisation code so that SS pin is not set to output: may cause +/// interference with other devices connected to the Arduino. Testing with Uno: OK. +/// +/// \version 1.16 Fixed a problem that prevented building with arduino 0021 +/// +/// \version 1.17 Added optional AFC pull-in frequency range argument to setFrequency(). +/// Default AFC pull-in range set to 0.05MHz +/// +/// \version 1.18 Changed default value for slave slect pin in constructor to be SS, ie the normal one for +/// the compiled Arduino (D10 for Diecimila, Uno etc and D53 for Mega). This is because some Arduinos such as Mega 2560 +/// reportedly use the type of the SS pin to determine whether to run in slave or master mode. Therfore it +/// is preferred that the slave select pin actually be the normal SS pin. +/// +/// \version 1.19 Added new mode() function. +/// Fixed a potential race condition in RF22Datagram::recvfrom which might cause corrupt from, to, id or flags +/// under extreme circumstances. Improvements to interrupt hygeine by adding cli()_/sei() around all +/// RF22 register acceses. Found that 0 length transmit packets confuses the RF22, so they are now forbidden. +/// Added IPGateway example, which routes UDP messages from an internet connection using an +/// Ethernet Shield and sends them +/// to a radio whose ID is based on the UDP port. Replies are sent back to the originating UDP +/// address and port. +/// +/// \version 1.20 _mode is now volatile. +/// RF22::send() now waits until any previous transmission is complete before sending. +/// RF22::waitPacketSent() now waits for the RF22 to not be in _mode == RF22_MODE_TX +/// _txPacketSent member is now redundant and removed. +/// Improvements to interrupt handling and blocking. Now use ATOMIC_BLOCK(ATOMIC_RESTORESTATE) +/// to prevent reenabling interrupts too soon. Thanks to Roland Mieslinger for this suggestion. +/// Added some performance measurements to documentation. +/// +/// \version 1.21 Fixed a case where a receiver buffer overflow could occur. Reported by Joe Tuttle. +/// +/// \version 1.22 Added documentation after testing with Sparkfun RFM22 Shield DEV-11018. +/// Fixed incorrect link to register calculator excel file, reported by Joey Morin. +/// +/// \version 1.23 Added support for alternative SPI interfaces, with default implementation for the standard +/// Arduino hardware SPI interface. Contributed by Joanna Rutkowska. +/// +/// \version 1.24 Fixed a problem that could cause corrupted receive messages if a transmit interrupted +/// a partial receive (as was common with eg ReliableDatagram with poor reception. +/// Also fixed possible receive buffer overrun. +/// \version 1.25 More rigorous use of const, additional register defines (RF22_CRCHDRS RF22_VARPKLEN) +/// and two methods (setPreambleLength() +/// and setSyncWords())made public. Patch provided by +/// Matthijs Kooijman. +/// \author Mike McCauley (mikem@open.com.au) + +#ifndef RF22_h +#define RF22_h +#include "mbed.h" + +#define boolean bool + +//#include <wiring.h> +// These defs cause trouble on some versions of Arduino +#undef round +#undef double + +// This is the bit in the SPI address that marks it as a write +#define RF22_SPI_WRITE_MASK 0x80 + +// This is the maximum message length that can be supported by this library. Limited by +// the single message length octet in the header. +// Yes, 255 is correct even though the FIFO size in the RF22 is only +// 64 octets. We use interrupts to refill the Tx FIFO during transmission and to empty the +// Rx FIFO during reception +// Can be pre-defined to a smaller size (to save SRAM) prior to including this header +#ifndef RF22_MAX_MESSAGE_LEN +#define RF22_MAX_MESSAGE_LEN 255 +//#define RF22_MAX_MESSAGE_LEN 50 +#endif + +// Max number of octets the RF22 Rx and Tx FIFOs can hold +#define RF22_FIFO_SIZE 64 + +// Keep track of the mode the RF22 is in +#define RF22_MODE_IDLE 0 +#define RF22_MODE_RX 1 +#define RF22_MODE_TX 2 + +// These values we set for FIFO thresholds are actually the same as the POR values +#define RF22_TXFFAEM_THRESHOLD 4 +#define RF22_RXFFAFULL_THRESHOLD 55 + +// This is the default node address, +#define RF22_DEFAULT_NODE_ADDRESS 0 + +// This address in the TO addreess signifies a broadcast +#define RF22_BROADCAST_ADDRESS 0xff + +// Number of registers to be passed to setModemConfig() +#define RF22_NUM_MODEM_CONFIG_REGS 18 + +// Register names +#define RF22_REG_00_DEVICE_TYPE 0x00 +#define RF22_REG_01_VERSION_CODE 0x01 +#define RF22_REG_02_DEVICE_STATUS 0x02 +#define RF22_REG_03_INTERRUPT_STATUS1 0x03 +#define RF22_REG_04_INTERRUPT_STATUS2 0x04 +#define RF22_REG_05_INTERRUPT_ENABLE1 0x05 +#define RF22_REG_06_INTERRUPT_ENABLE2 0x06 +#define RF22_REG_07_OPERATING_MODE1 0x07 +#define RF22_REG_08_OPERATING_MODE2 0x08 +#define RF22_REG_09_OSCILLATOR_LOAD_CAPACITANCE 0x09 +#define RF22_REG_0A_UC_OUTPUT_CLOCK 0x0a +#define RF22_REG_0B_GPIO_CONFIGURATION0 0x0b +#define RF22_REG_0C_GPIO_CONFIGURATION1 0x0c +#define RF22_REG_0D_GPIO_CONFIGURATION2 0x0d +#define RF22_REG_0E_IO_PORT_CONFIGURATION 0x0e +#define RF22_REG_0F_ADC_CONFIGURATION 0x0f +#define RF22_REG_10_ADC_SENSOR_AMP_OFFSET 0x10 +#define RF22_REG_11_ADC_VALUE 0x11 +#define RF22_REG_12_TEMPERATURE_SENSOR_CALIBRATION 0x12 +#define RF22_REG_13_TEMPERATURE_VALUE_OFFSET 0x13 +#define RF22_REG_14_WAKEUP_TIMER_PERIOD1 0x14 +#define RF22_REG_15_WAKEUP_TIMER_PERIOD2 0x15 +#define RF22_REG_16_WAKEUP_TIMER_PERIOD3 0x16 +#define RF22_REG_17_WAKEUP_TIMER_VALUE1 0x17 +#define RF22_REG_18_WAKEUP_TIMER_VALUE2 0x18 +#define RF22_REG_19_LDC_MODE_DURATION 0x19 +#define RF22_REG_1A_LOW_BATTERY_DETECTOR_THRESHOLD 0x1a +#define RF22_REG_1B_BATTERY_VOLTAGE_LEVEL 0x1b +#define RF22_REG_1C_IF_FILTER_BANDWIDTH 0x1c +#define RF22_REG_1D_AFC_LOOP_GEARSHIFT_OVERRIDE 0x1d +#define RF22_REG_1E_AFC_TIMING_CONTROL 0x1e +#define RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE 0x1f +#define RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE 0x20 +#define RF22_REG_21_CLOCK_RECOVERY_OFFSET2 0x21 +#define RF22_REG_22_CLOCK_RECOVERY_OFFSET1 0x22 +#define RF22_REG_23_CLOCK_RECOVERY_OFFSET0 0x23 +#define RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1 0x24 +#define RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0 0x25 +#define RF22_REG_26_RSSI 0x26 +#define RF22_REG_27_RSSI_THRESHOLD 0x27 +#define RF22_REG_28_ANTENNA_DIVERSITY1 0x28 +#define RF22_REG_29_ANTENNA_DIVERSITY2 0x29 +#define RF22_REG_2A_AFC_LIMITER 0x2a +#define RF22_REG_2B_AFC_CORRECTION_READ 0x2b +#define RF22_REG_2C_OOK_COUNTER_VALUE_1 0x2c +#define RF22_REG_2D_OOK_COUNTER_VALUE_2 0x2d +#define RF22_REG_2E_SLICER_PEAK_HOLD 0x2e +#define RF22_REG_30_DATA_ACCESS_CONTROL 0x30 +#define RF22_REG_31_EZMAC_STATUS 0x31 +#define RF22_REG_32_HEADER_CONTROL1 0x32 +#define RF22_REG_33_HEADER_CONTROL2 0x33 +#define RF22_REG_34_PREAMBLE_LENGTH 0x34 +#define RF22_REG_35_PREAMBLE_DETECTION_CONTROL1 0x35 +#define RF22_REG_36_SYNC_WORD3 0x36 +#define RF22_REG_37_SYNC_WORD2 0x37 +#define RF22_REG_38_SYNC_WORD1 0x38 +#define RF22_REG_39_SYNC_WORD0 0x39 +#define RF22_REG_3A_TRANSMIT_HEADER3 0x3a +#define RF22_REG_3B_TRANSMIT_HEADER2 0x3b +#define RF22_REG_3C_TRANSMIT_HEADER1 0x3c +#define RF22_REG_3D_TRANSMIT_HEADER0 0x3d +#define RF22_REG_3E_PACKET_LENGTH 0x3e +#define RF22_REG_3F_CHECK_HEADER3 0x3f +#define RF22_REG_40_CHECK_HEADER2 0x40 +#define RF22_REG_41_CHECK_HEADER1 0x41 +#define RF22_REG_42_CHECK_HEADER0 0x42 +#define RF22_REG_43_HEADER_ENABLE3 0x43 +#define RF22_REG_44_HEADER_ENABLE2 0x44 +#define RF22_REG_45_HEADER_ENABLE1 0x45 +#define RF22_REG_46_HEADER_ENABLE0 0x46 +#define RF22_REG_47_RECEIVED_HEADER3 0x47 +#define RF22_REG_48_RECEIVED_HEADER2 0x48 +#define RF22_REG_49_RECEIVED_HEADER1 0x49 +#define RF22_REG_4A_RECEIVED_HEADER0 0x4a +#define RF22_REG_4B_RECEIVED_PACKET_LENGTH 0x4b +#define RF22_REG_50_ANALOG_TEST_BUS_SELECT 0x50 +#define RF22_REG_51_DIGITAL_TEST_BUS_SELECT 0x51 +#define RF22_REG_52_TX_RAMP_CONTROL 0x52 +#define RF22_REG_53_PLL_TUNE_TIME 0x53 +#define RF22_REG_55_CALIBRATION_CONTROL 0x55 +#define RF22_REG_56_MODEM_TEST 0x56 +#define RF22_REG_57_CHARGE_PUMP_TEST 0x57 +#define RF22_REG_58_CHARGE_PUMP_CURRENT_TRIMMING 0x58 +#define RF22_REG_59_DIVIDER_CURRENT_TRIMMING 0x59 +#define RF22_REG_5A_VCO_CURRENT_TRIMMING 0x5a +#define RF22_REG_5B_VCO_CALIBRATION 0x5b +#define RF22_REG_5C_SYNTHESIZER_TEST 0x5c +#define RF22_REG_5D_BLOCK_ENABLE_OVERRIDE1 0x5d +#define RF22_REG_5E_BLOCK_ENABLE_OVERRIDE2 0x5e +#define RF22_REG_5F_BLOCK_ENABLE_OVERRIDE3 0x5f +#define RF22_REG_60_CHANNEL_FILTER_COEFFICIENT_ADDRESS 0x60 +#define RF22_REG_61_CHANNEL_FILTER_COEFFICIENT_VALUE 0x61 +#define RF22_REG_62_CRYSTAL_OSCILLATOR_POR_CONTROL 0x62 +#define RF22_REG_63_RC_OSCILLATOR_COARSE_CALIBRATION 0x63 +#define RF22_REG_64_RC_OSCILLATOR_FINE_CALIBRATION 0x64 +#define RF22_REG_65_LDO_CONTROL_OVERRIDE 0x65 +#define RF22_REG_66_LDO_LEVEL_SETTINGS 0x66 +#define RF22_REG_67_DELTA_SIGMA_ADC_TUNING1 0x67 +#define RF22_REG_68_DELTA_SIGMA_ADC_TUNING2 0x68 +#define RF22_REG_69_AGC_OVERRIDE1 0x69 +#define RF22_REG_6A_AGC_OVERRIDE2 0x6a +#define RF22_REG_6B_GFSK_FIR_FILTER_COEFFICIENT_ADDRESS 0x6b +#define RF22_REG_6C_GFSK_FIR_FILTER_COEFFICIENT_VALUE 0x6c +#define RF22_REG_6D_TX_POWER 0x6d +#define RF22_REG_6E_TX_DATA_RATE1 0x6e +#define RF22_REG_6F_TX_DATA_RATE0 0x6f +#define RF22_REG_70_MODULATION_CONTROL1 0x70 +#define RF22_REG_71_MODULATION_CONTROL2 0x71 +#define RF22_REG_72_FREQUENCY_DEVIATION 0x72 +#define RF22_REG_73_FREQUENCY_OFFSET1 0x73 +#define RF22_REG_74_FREQUENCY_OFFSET2 0x74 +#define RF22_REG_75_FREQUENCY_BAND_SELECT 0x75 +#define RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1 0x76 +#define RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0 0x77 +#define RF22_REG_79_FREQUENCY_HOPPING_CHANNEL_SELECT 0x79 +#define RF22_REG_7A_FREQUENCY_HOPPING_STEP_SIZE 0x7a +#define RF22_REG_7C_TX_FIFO_CONTROL1 0x7c +#define RF22_REG_7D_TX_FIFO_CONTROL2 0x7d +#define RF22_REG_7E_RX_FIFO_CONTROL 0x7e +#define RF22_REG_7F_FIFO_ACCESS 0x7f + +// These register masks etc are named wherever possible +// corresponding to the bit and field names in the RF-22 Manual +// RF22_REG_00_DEVICE_TYPE 0x00 +#define RF22_DEVICE_TYPE_RX_TRX 0x08 +#define RF22_DEVICE_TYPE_TX 0x07 + +// RF22_REG_02_DEVICE_STATUS 0x02 +#define RF22_FFOVL 0x80 +#define RF22_FFUNFL 0x40 +#define RF22_RXFFEM 0x20 +#define RF22_HEADERR 0x10 +#define RF22_FREQERR 0x08 +#define RF22_LOCKDET 0x04 +#define RF22_CPS 0x03 +#define RF22_CPS_IDLE 0x00 +#define RF22_CPS_RX 0x01 +#define RF22_CPS_TX 0x10 + +// RF22_REG_03_INTERRUPT_STATUS1 0x03 +#define RF22_IFFERROR 0x80 +#define RF22_ITXFFAFULL 0x40 +#define RF22_ITXFFAEM 0x20 +#define RF22_IRXFFAFULL 0x10 +#define RF22_IEXT 0x08 +#define RF22_IPKSENT 0x04 +#define RF22_IPKVALID 0x02 +#define RF22_ICRCERROR 0x01 + +// RF22_REG_04_INTERRUPT_STATUS2 0x04 +#define RF22_ISWDET 0x80 +#define RF22_IPREAVAL 0x40 +#define RF22_IPREAINVAL 0x20 +#define RF22_IRSSI 0x10 +#define RF22_IWUT 0x08 +#define RF22_ILBD 0x04 +#define RF22_ICHIPRDY 0x02 +#define RF22_IPOR 0x01 + +// RF22_REG_05_INTERRUPT_ENABLE1 0x05 +#define RF22_ENFFERR 0x80 +#define RF22_ENTXFFAFULL 0x40 +#define RF22_ENTXFFAEM 0x20 +#define RF22_ENRXFFAFULL 0x10 +#define RF22_ENEXT 0x08 +#define RF22_ENPKSENT 0x04 +#define RF22_ENPKVALID 0x02 +#define RF22_ENCRCERROR 0x01 + +// RF22_REG_06_INTERRUPT_ENABLE2 0x06 +#define RF22_ENSWDET 0x80 +#define RF22_ENPREAVAL 0x40 +#define RF22_ENPREAINVAL 0x20 +#define RF22_ENRSSI 0x10 +#define RF22_ENWUT 0x08 +#define RF22_ENLBDI 0x04 +#define RF22_ENCHIPRDY 0x02 +#define RF22_ENPOR 0x01 + +// RF22_REG_07_OPERATING_MODE 0x07 +#define RF22_SWRES 0x80 +#define RF22_ENLBD 0x40 +#define RF22_ENWT 0x20 +#define RF22_X32KSEL 0x10 +#define RF22_TXON 0x08 +#define RF22_RXON 0x04 +#define RF22_PLLON 0x02 +#define RF22_XTON 0x01 + +// RF22_REG_08_OPERATING_MODE2 0x08 +#define RF22_ANTDIV 0xc0 +#define RF22_RXMPK 0x10 +#define RF22_AUTOTX 0x08 +#define RF22_ENLDM 0x04 +#define RF22_FFCLRRX 0x02 +#define RF22_FFCLRTX 0x01 + +// RF22_REG_0F_ADC_CONFIGURATION 0x0f +#define RF22_ADCSTART 0x80 +#define RF22_ADCDONE 0x80 +#define RF22_ADCSEL 0x70 +#define RF22_ADCSEL_INTERNAL_TEMPERATURE_SENSOR 0x00 +#define RF22_ADCSEL_GPIO0_SINGLE_ENDED 0x10 +#define RF22_ADCSEL_GPIO1_SINGLE_ENDED 0x20 +#define RF22_ADCSEL_GPIO2_SINGLE_ENDED 0x30 +#define RF22_ADCSEL_GPIO0_GPIO1_DIFFERENTIAL 0x40 +#define RF22_ADCSEL_GPIO1_GPIO2_DIFFERENTIAL 0x50 +#define RF22_ADCSEL_GPIO0_GPIO2_DIFFERENTIAL 0x60 +#define RF22_ADCSEL_GND 0x70 +#define RF22_ADCREF 0x0c +#define RF22_ADCREF_BANDGAP_VOLTAGE 0x00 +#define RF22_ADCREF_VDD_ON_3 0x08 +#define RF22_ADCREF_VDD_ON_2 0x0c +#define RF22_ADCGAIN 0x03 + +// RF22_REG_10_ADC_SENSOR_AMP_OFFSET 0x10 +#define RF22_ADCOFFS 0x0f + +// RF22_REG_12_TEMPERATURE_SENSOR_CALIBRATION 0x12 +#define RF22_TSRANGE 0xc0 +#define RF22_TSRANGE_M64_64C 0x00 +#define RF22_TSRANGE_M64_192C 0x40 +#define RF22_TSRANGE_0_128C 0x80 +#define RF22_TSRANGE_M40_216F 0xc0 +#define RF22_ENTSOFFS 0x20 +#define RF22_ENTSTRIM 0x10 +#define RF22_TSTRIM 0x0f + +// RF22_REG_14_WAKEUP_TIMER_PERIOD1 0x14 +#define RF22_WTR 0x3c +#define RF22_WTD 0x03 + +// RF22_REG_1D_AFC_LOOP_GEARSHIFT_OVERRIDE 0x1d +#define RF22_AFBCD 0x80 +#define RF22_ENAFC 0x40 +#define RF22_AFCGEARH 0x38 +#define RF22_AFCGEARL 0x07 + +// RF22_REG_1E_AFC_TIMING_CONTROL 0x1e +#define RF22_SWAIT_TIMER 0xc0 +#define RF22_SHWAIT 0x38 +#define RF22_ANWAIT 0x07 + +// RF22_REG_30_DATA_ACCESS_CONTROL 0x30 +#define RF22_ENPACRX 0x80 +#define RF22_MSBFRST 0x00 +#define RF22_LSBFRST 0x40 +#define RF22_CRCHDRS 0x00 +#define RF22_CRCDONLY 0x20 +#define RF22_ENPACTX 0x08 +#define RF22_ENCRC 0x04 +#define RF22_CRC 0x03 +#define RF22_CRC_CCITT 0x00 +#define RF22_CRC_CRC_16_IBM 0x01 +#define RF22_CRC_IEC_16 0x02 +#define RF22_CRC_BIACHEVA 0x03 + +// RF22_REG_32_HEADER_CONTROL1 0x32 +#define RF22_BCEN 0xf0 +#define RF22_BCEN_NONE 0x00 +#define RF22_BCEN_HEADER0 0x10 +#define RF22_BCEN_HEADER1 0x20 +#define RF22_BCEN_HEADER2 0x40 +#define RF22_BCEN_HEADER3 0x80 +#define RF22_HDCH 0x0f +#define RF22_HDCH_NONE 0x00 +#define RF22_HDCH_HEADER0 0x01 +#define RF22_HDCH_HEADER1 0x02 +#define RF22_HDCH_HEADER2 0x04 +#define RF22_HDCH_HEADER3 0x08 + +// RF22_REG_33_HEADER_CONTROL2 0x33 +#define RF22_HDLEN 0x70 +#define RF22_HDLEN_0 0x00 +#define RF22_HDLEN_1 0x10 +#define RF22_HDLEN_2 0x20 +#define RF22_HDLEN_3 0x30 +#define RF22_HDLEN_4 0x40 +#define RF22_VARPKLEN 0x00 +#define RF22_FIXPKLEN 0x08 +#define RF22_SYNCLEN 0x06 +#define RF22_SYNCLEN_1 0x00 +#define RF22_SYNCLEN_2 0x02 +#define RF22_SYNCLEN_3 0x04 +#define RF22_SYNCLEN_4 0x06 +#define RF22_PREALEN8 0x01 + +// RF22_REG_6D_TX_POWER 0x6d +#define RF22_TXPOW 0x07 +#define RF22_TXPOW_4X31 0x08 // Not used in RFM22B +#define RF22_TXPOW_1DBM 0x00 +#define RF22_TXPOW_2DBM 0x01 +#define RF22_TXPOW_5DBM 0x02 +#define RF22_TXPOW_8DBM 0x03 +#define RF22_TXPOW_11DBM 0x04 +#define RF22_TXPOW_14DBM 0x05 +#define RF22_TXPOW_17DBM 0x06 +#define RF22_TXPOW_20DBM 0x07 +// IN RFM23B +#define RF22_TXPOW_LNA_SW 0x08 + +// RF22_REG_71_MODULATION_CONTROL2 0x71 +#define RF22_TRCLK 0xc0 +#define RF22_TRCLK_NONE 0x00 +#define RF22_TRCLK_GPIO 0x40 +#define RF22_TRCLK_SDO 0x80 +#define RF22_TRCLK_NIRQ 0xc0 +#define RF22_DTMOD 0x30 +#define RF22_DTMOD_DIRECT_GPIO 0x00 +#define RF22_DTMOD_DIRECT_SDI 0x10 +#define RF22_DTMOD_FIFO 0x20 +#define RF22_DTMOD_PN9 0x30 +#define RF22_ENINV 0x08 +#define RF22_FD8 0x04 +#define RF22_MODTYP 0x30 +#define RF22_MODTYP_UNMODULATED 0x00 +#define RF22_MODTYP_OOK 0x01 +#define RF22_MODTYP_FSK 0x02 +#define RF22_MODTYP_GFSK 0x03 + +// RF22_REG_75_FREQUENCY_BAND_SELECT 0x75 +#define RF22_SBSEL 0x40 +#define RF22_HBSEL 0x20 +#define RF22_FB 0x1f + +// Define this to include Serial printing in diagnostic routines +//#define RF22_HAVE_SERIAL + +//#include <GenericSPI.h> +//#include <HardwareSPI.h> +///////////////////////////////////////////////////////////////////// +/// \class RF22 RF22.h <RF22.h> +/// \brief Send and receive unaddressed, unreliable datagrams. +/// +/// This base class provides basic functions for sending and receiving unaddressed, +/// unreliable datagrams of arbitrary length to 255 octets per packet. +/// +/// Subclasses may use this class to implement reliable, addressed datagrams and streams, +/// mesh routers, repeaters, translators etc. +/// +/// On transmission, the TO and FROM addresses default to 0x00, unless changed by a subclass. +/// On reception the TO addressed is checked against the node address (defaults to 0x00) or the +/// broadcast address (which is 0xff). The ID and FLAGS are set to 0, and not checked by this class. +/// This permits use of the this base RF22 class as an +/// unaddresed, unreliable datagram service. Subclasses are expected to change this behaviour to +/// add node address, ids, retransmission etc +/// +/// Naturally, for any 2 radios to communicate that must be configured to use the same frequence and +/// modulation scheme. +class RF22 +{ +public: + + /// \brief Defines register values for a set of modem configuration registers + /// + /// Defines register values for a set of modem configuration registers + /// that can be passed to setModemConfig() + /// if none of the choices in ModemConfigChoice suit your need + /// setModemConfig() writes the register values to the appropriate RF22 registers + /// to set the desired modulation type, data rate and deviation/bandwidth. + /// Suitable values for these registers can be computed using the register calculator at + /// http://www.hoperf.com/upload/rf/RF22B%2023B%2031B%2042B%2043B%20Register%20Settings_RevB1-v5.xls + + typedef struct + { + uint8_t hodnota; + uint8_t paket; + } zprava; + + typedef struct + { + uint8_t reg_1c; ///< Value for register RF22_REG_1C_IF_FILTER_BANDWIDTH + uint8_t reg_1f; ///< Value for register RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE + uint8_t reg_20; ///< Value for register RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE + uint8_t reg_21; ///< Value for register RF22_REG_21_CLOCK_RECOVERY_OFFSET2 + uint8_t reg_22; ///< Value for register RF22_REG_22_CLOCK_RECOVERY_OFFSET1 + uint8_t reg_23; ///< Value for register RF22_REG_23_CLOCK_RECOVERY_OFFSET0 + uint8_t reg_24; ///< Value for register RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1 + uint8_t reg_25; ///< Value for register RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0 + uint8_t reg_2c; ///< Value for register RF22_REG_2C_OOK_COUNTER_VALUE_1 + uint8_t reg_2d; ///< Value for register RF22_REG_2D_OOK_COUNTER_VALUE_2 + uint8_t reg_2e; ///< Value for register RF22_REG_2E_SLICER_PEAK_HOLD + uint8_t reg_58; ///< Value for register RF22_REG_58_CHARGE_PUMP_CURRENT_TRIMMING + uint8_t reg_69; ///< Value for register RF22_REG_69_AGC_OVERRIDE1 + uint8_t reg_6e; ///< Value for register RF22_REG_6E_TX_DATA_RATE1 + uint8_t reg_6f; ///< Value for register RF22_REG_6F_TX_DATA_RATE0 + uint8_t reg_70; ///< Value for register RF22_REG_70_MODULATION_CONTROL1 + uint8_t reg_71; ///< Value for register RF22_REG_71_MODULATION_CONTROL2 + uint8_t reg_72; ///< Value for register RF22_REG_72_FREQUENCY_DEVIATION + } ModemConfig; + + /// Choices for setModemConfig() for a selected subset of common modulation types, + /// and data rates. If you need another configuration, use the register calculator. + /// and call setModemRegisters() with your desired settings + /// These are indexes into _modemConfig + typedef enum + { + UnmodulatedCarrier = 0, ///< Unmodulated carrier for testing + FSK_PN9_Rb2Fd5, ///< FSK, No Manchester, Rb = 2kbs, Fd = 5kHz, PN9 random modulation for testing + + FSK_Rb2Fd5, ///< FSK, No Manchester, Rb = 2kbs, Fd = 5kHz + FSK_Rb2_4Fd36, ///< FSK, No Manchester, Rb = 2.4kbs, Fd = 36kHz + FSK_Rb4_8Fd45, ///< FSK, No Manchester, Rb = 4.8kbs, Fd = 45kHz + FSK_Rb9_6Fd45, ///< FSK, No Manchester, Rb = 9.6kbs, Fd = 45kHz + FSK_Rb19_2Fd9_6, ///< FSK, No Manchester, Rb = 19.2kbs, Fd = 9.6kHz + FSK_Rb38_4Fd19_6, ///< FSK, No Manchester, Rb = 38.4kbs, Fd = 19.6kHz + FSK_Rb57_6Fd28_8, ///< FSK, No Manchester, Rb = 57.6kbs, Fd = 28.8kHz + FSK_Rb125Fd125, ///< FSK, No Manchester, Rb = 125kbs, Fd = 125kHz + + GFSK_Rb2Fd5, ///< GFSK, No Manchester, Rb = 2kbs, Fd = 5kHz + GFSK_Rb2_4Fd36, ///< GFSK, No Manchester, Rb = 2.4kbs, Fd = 36kHz + GFSK_Rb4_8Fd45, ///< GFSK, No Manchester, Rb = 4.8kbs, Fd = 45kHz + GFSK_Rb9_6Fd45, ///< GFSK, No Manchester, Rb = 9.6kbs, Fd = 45kHz + GFSK_Rb19_2Fd9_6, ///< GFSK, No Manchester, Rb = 19.2kbs, Fd = 9.6kHz + GFSK_Rb38_4Fd19_6, ///< GFSK, No Manchester, Rb = 38.4kbs, Fd = 19.6kHz + GFSK_Rb57_6Fd28_8, ///< GFSK, No Manchester, Rb = 57.6kbs, Fd = 28.8kHz + GFSK_Rb125Fd125, ///< GFSK, No Manchester, Rb = 125kbs, Fd = 125kHz + + OOK_Rb1_2Bw75, ///< OOK, No Manchester, Rb = 1.2kbs, Rx Bandwidth = 75kHz + OOK_Rb2_4Bw335, ///< OOK, No Manchester, Rb = 2.4kbs, Rx Bandwidth = 335kHz + OOK_Rb4_8Bw335, ///< OOK, No Manchester, Rb = 4.8kbs, Rx Bandwidth = 335kHz + OOK_Rb9_6Bw335, ///< OOK, No Manchester, Rb = 9.6kbs, Rx Bandwidth = 335kHz + OOK_Rb19_2Bw335, ///< OOK, No Manchester, Rb = 19.2kbs, Rx Bandwidth = 335kHz + OOK_Rb38_4Bw335, ///< OOK, No Manchester, Rb = 38.4kbs, Rx Bandwidth = 335kHz + OOK_Rb40Bw335 ///< OOK, No Manchester, Rb = 40kbs, Rx Bandwidth = 335kHz + } ModemConfigChoice; + + /// Constructor. You can have multiple instances, but each instance must have its own + /// interrupt and slave select pin. After constructing, you must call init() to initialise the intnerface + /// and the radio module + /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the RF22 before + /// accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega) + /// \param[in] interrupt The interrupt number to use. Default is interrupt 0 (Arduino input pin 2) + RF22(PinName slaveSelectPin , PinName mosi, PinName miso, PinName sclk, PinName interrupt ); + + /// Initialises this instance and the radio module connected to it. + /// The following steps are taken: + /// - Initialise the slave select pin and the SPI interface library + /// - Software reset the RF22 module + /// - Checks the connected RF22 module is either a RF22_DEVICE_TYPE_RX_TRX or a RF22_DEVICE_TYPE_TX + /// - Attaches an interrupt handler + /// - Configures the RF22 module + /// - Sets the frequncy to 434.0 MHz + /// - Sets the modem data rate to FSK_Rb2_4Fd36 + /// \return true if everything was successful + + void obsluhapreruseni(); + void vypisfifo(); + + boolean init(); + + /// Issues a software reset to the + /// RF22 module. Blocks for 1ms to ensure the reset is complete. + void reset(); + + /// Reads a single register from the RF22 + /// \param[in] reg Register number, one of RF22_REG_* + /// \return The value of the register + uint8_t spiRead(uint8_t reg); + + /// Writes a single byte to the RF22 + /// \param[in] reg Register number, one of RF22_REG_* + /// \param[in] val The value to write + void spiWrite(uint8_t reg, uint8_t val); + + /// Reads a number of consecutive registers from the RF22 using burst read mode + /// \param[in] reg Register number of the first register, one of RF22_REG_* + /// \param[in] dest Array to write the register values to. Must be at least len bytes + /// \param[in] len Number of bytes to read + void spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len); + + /// Write a number of consecutive registers using burst write mode + /// \param[in] reg Register number of the first register, one of RF22_REG_* + /// \param[in] src Array of new register values to write. Must be at least len bytes + /// \param[in] len Number of bytes to write + void spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len); + + /// Reads and returns the device status register RF22_REG_02_DEVICE_STATUS + /// \return The value of the device status register + uint8_t statusRead(); + + /// Reads a value from the on-chip analog-digital converter + /// \param[in] adcsel Selects the ADC input to measure. One of RF22_ADCSEL_*. Defaults to the + /// internal temperature sensor + /// \param[in] adcref Specifies the refernce voltage to use. One of RF22_ADCREF_*. + /// Defaults to the internal bandgap voltage. + /// \param[in] adcgain Amplifier gain selection. + /// \param[in] adcoffs Amplifier offseet (0 to 15). + /// \return The analog value. 0 to 255. + uint8_t adcRead(uint8_t adcsel = RF22_ADCSEL_INTERNAL_TEMPERATURE_SENSOR, + uint8_t adcref = RF22_ADCREF_BANDGAP_VOLTAGE, + uint8_t adcgain = 0, + uint8_t adcoffs = 0); + + + /// Reads the wakeup timer value in registers RF22_REG_17_WAKEUP_TIMER_VALUE1 + /// and RF22_REG_18_WAKEUP_TIMER_VALUE2 + /// \return The wakeup timer value + uint16_t wutRead(); + + /// Sets the wakeup timer period registers RF22_REG_14_WAKEUP_TIMER_PERIOD1, + /// RF22_REG_15_WAKEUP_TIMER_PERIOD2 and RF22_R<EG_16_WAKEUP_TIMER_PERIOD3 + /// \param[in] wtm Wakeup timer mantissa value + /// \param[in] wtr Wakeup timer exponent R value + /// \param[in] wtd Wakeup timer exponent D value + void setWutPeriod(uint16_t wtm, uint8_t wtr = 0, uint8_t wtd = 0); + + /// Sets the transmitter and receiver centre frequency + /// \param[in] centre Frequency in MHz. 240.0 to 960.0. Caution, some versions of RF22 and derivatives + /// implemented more restricted frequency ranges. + /// \param[in] afcPullInRange Sets the AF Pull In Range in MHz. Defaults to 0.05MHz (50kHz). Range is 0.0 to 0.159375 + /// for frequencies 240.0 to 480MHz, and 0.0 to 0.318750MHz for frequencies 480.0 to 960MHz, + /// \return true if the selected frquency centre + (fhch * fhs) is within range and the afcPullInRange is within range + boolean setFrequency(float centre, float afcPullInRange = 0.05); + + /// Sets the frequency hopping step size. + /// \param[in] fhs Frequency Hopping step size in 10kHz increments + /// \return true if centre + (fhch * fhs) is within limits + boolean setFHStepSize(uint8_t fhs); + + /// Sets the frequncy hopping channel. Adds fhch * fhs to centre frequency + /// \param[in] fhch The channel number + /// \return true if the selected frquency centre + (fhch * fhs) is within range + boolean setFHChannel(uint8_t fhch); + + /// Reads and returns the current RSSI value from register RF22_REG_26_RSSI. If you want to find the RSSI + /// of the last received message, use lastRssi() instead. + /// \return The current RSSI value + uint8_t rssiRead(); + + /// Reads and returns the current EZMAC value from register RF22_REG_31_EZMAC_STATUS + /// \return The current EZMAC value + uint8_t ezmacStatusRead(); + + /// Sets the parameters for the RF22 Idle mode in register RF22_REG_07_OPERATING_MODE. + /// Idle mode is the mode the RF22 will be in when not transmitting or receiving. The default idle mode + /// is RF22_XTON ie READY mode. + /// \param[in] mode Mask of mode bits, using RF22_SWRES, RF22_ENLBD, RF22_ENWT, + /// RF22_X32KSEL, RF22_PLLON, RF22_XTON. + void setMode(uint8_t mode); + + /// If current mode is Rx or Tx changes it to Idle. If the transmitter or receiver is running, + /// disables them. + void setModeIdle(); + + /// If current mode is Tx or Idle, changes it to Rx. + /// Starts the receiver in the RF22. + void setModeRx(); + + /// If current mode is Rx or Idle, changes it to Rx. + /// Starts the transmitter in the RF22. + void setModeTx(); + + /// Returns the operating mode of the library. + /// \return the current mode, one of RF22_MODE_* + uint8_t mode(); + + /// Sets the transmitter power output level in register RF22_REG_6D_TX_POWER. + /// Be a good neighbour and set the lowest power level you need. + /// After init(), the power wil be set to RF22_TXPOW_8DBM. + /// Caution: In some countries you may only select RF22_TXPOW_17DBM if you + /// are also using frequency hopping. + /// \param[in] power Transmitter power level, one of RF22_TXPOW_* + void setTxPower(uint8_t power); + + /// Sets all the registered required to configure the data modem in the RF22, including the data rate, + /// bandwidths etc. You cas use this to configure the modem with custom configuraitons if none of the + /// canned configurations in ModemConfigChoice suit you. + /// \param[in] config A ModemConfig structure containing values for the modem configuration registers. + void setModemRegisters(const ModemConfig* config); + + /// Select one of the predefined modem configurations. If you need a modem configuration not provided + /// here, use setModemRegisters() with your own ModemConfig. + /// \param[in] index The configuration choice. + /// \return true if index is a valid choice. + boolean setModemConfig(ModemConfigChoice index); + + /// Starts the receiver and checks whether a received message is available. + /// This can be called multiple times in a timeout loop + /// \return true if a complete, valid message has been received and is able to be retrieved by + /// recv() + boolean available(); + + /// Starts the receiver and blocks until a valid received + /// message is available. + void waitAvailable(); + + /// Starts the receiver and blocks until a received message is available or a timeout + /// \param[in] timeout Maximum time to wait in milliseconds. + /// \return true if a message is available + bool waitAvailableTimeout(uint16_t timeout); + + /// Turns the receiver on if it not already on. + /// If there is a valid message available, copy it to buf and return true + /// else return false. + /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). + /// You should be sure to call this function frequently enough to not miss any messages + /// It is recommended that you call it in your main loop. + /// \param[in] buf Location to copy the received message + /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied. + /// \return true if a valid message was copied to buf + boolean recv(uint8_t* buf, uint8_t* len); + + /// Waits until any previous transmit packet is finished being transmitted with waitPacketSent(). + /// Then loads a message into the transmitter and starts the transmitter. Note that a message length + /// of 0 is NOT permitted. + /// \param[in] data Array of data to be sent + /// \param[in] len Number of bytes of data to send (> 0) + /// \return true if the message length was valid and it was correctly queued for transmit + boolean send(const uint8_t* data, uint8_t len); + + /// Blocks until the RF22 is not in mode RF22_MODE_TX (ie until the RF22 is not transmitting). + /// This effectively waits until any previous transmit packet is finished being transmitted. + void waitPacketSent(); + + /// Tells the receiver to accept messages with any TO address, not just messages + /// addressed to this node or the broadcast address + /// \param[in] promiscuous true if you wish to receive messages with any TO address + void setPromiscuous(boolean promiscuous); + + /// Returns the TO header of the last received message + /// \return The TO header + uint8_t headerTo(); + + /// Returns the FROM header of the last received message + /// \return The FROM header + uint8_t headerFrom(); + + /// Returns the ID header of the last received message + /// \return The ID header + uint8_t headerId(); + + /// Returns the FLAGS header of the last received message + /// \return The FLAGS header + uint8_t headerFlags(); + + /// Returns the RSSI (Receiver Signal Strength Indicator) + /// of the last received message. This measurement is taken when + /// the preamble has been received. It is a (non-linear) measure of the received signal strength. + /// \return The RSSI + uint8_t lastRssi(); + + /// Prints a data buffer in HEX. + /// For diagnostic use + /// \param[in] prompt string to preface the print + /// \param[in] buf Location of the buffer to print + /// \param[in] len Length of the buffer in octets. + static void printBuffer(const uint8_t *buf, uint8_t len); + + /// Sets the length of the preamble + /// in 4-bit nibbles. + /// Caution: this should be set to the same + /// value on all nodes in your network. Default is 8. + /// Sets the message preamble length in RF22_REG_34_PREAMBLE_LENGTH + /// \param[in] nibbles Preamble length in nibbles of 4 bits each. + void setPreambleLength(uint8_t nibbles); + + /// Sets the sync words for transmit and receive in registers RF22_REG_36_SYNC_WORD3 + /// to RF22_REG_39_SYNC_WORD0 + /// Caution: this should be set to the same + /// value on all nodes in your network. Default is { 0x2d, 0xd4 } + /// \param[in] syncWords Array of sync words + /// \param[in] len Number of sync words to set + void setSyncWords(const uint8_t* syncWords, uint8_t len); + +protected: + /// This is a low level function to handle the interrupts for one instance of RF22. + /// Called automatically by isr0() and isr1() + /// Should not need to be called. + void handleInterrupt(); + + /// Clears the receiver buffer. + /// Internal use only + void clearRxBuf(); + + /// Clears the transmitter buffer + /// Internal use only + void clearTxBuf(); + + /// Fills the transmitter buffer with the data of a mesage to be sent + /// \param[in] data Array of data bytes to be sent (1 to 255) + /// \param[in] len Number of data bytes in data (> 0) + /// \return true if the message length is valid + boolean fillTxBuf(const uint8_t* data, uint8_t len); + + /// Appends the transmitter buffer with the data of a mesage to be sent + /// \param[in] data Array of data bytes to be sent (0 to 255) + /// \param[in] len Number of data bytes in data + /// \return false if the resulting message would exceed RF22_MAX_MESSAGE_LEN, else true + boolean appendTxBuf(const uint8_t* data, uint8_t len); + + /// Internal function to load the next fragment of + /// the current message into the transmitter FIFO + /// Internal use only + void sendNextFragment(); + + /// function to copy the next fragment from + /// the receiver FIFO into the receiver buffer + void readNextFragment(); + + /// Clears the RF22 Rx and Tx FIFOs + /// Internal use only + void resetFifos(); + + /// Clears the RF22 Rx FIFO + /// Internal use only + void resetRxFifo(); + + /// Clears the RF22 Tx FIFO + /// Internal use only + void resetTxFifo(); + + /// This function will be called by handleInterrupt() if an RF22 external interrupt occurs. + /// This can only happen if external interrupts are enabled in the RF22 + /// (which they are not by default). + /// Subclasses may override this function to get control when an RF22 external interrupt occurs. + virtual void handleExternalInterrupt(); + + /// This function will be called by handleInterrupt() if an RF22 wakeup timer interrupt occurs. + /// This can only happen if wakeup timer interrupts are enabled in the RF22 + /// (which they are not by default). + /// Subclasses may override this function to get control when an RF22 wakeup timer interrupt occurs. + virtual void handleWakeupTimerInterrupt(); + + /// Sets the TO header to be sent in all subsequent messages + /// \param[in] to The new TO header value + void setHeaderTo(uint8_t to); + + /// Sets the FROM header to be sent in all subsequent messages + /// \param[in] from The new FROM header value + void setHeaderFrom(uint8_t from); + + /// Sets the ID header to be sent in all subsequent messages + /// \param[in] id The new ID header value + void setHeaderId(uint8_t id); + + /// Sets the FLAGS header to be sent in all subsequent messages + /// \param[in] flags The new FLAGS header value + void setHeaderFlags(uint8_t flags); + + /// Start the transmission of the contents + /// of the Tx buffer + void startTransmit(); + + /// ReStart the transmission of the contents + /// of the Tx buffer after a atransmission failure + void restartTransmit(); + +protected: + //GenericSPIClass* _spi; + + /// Low level interrupt service routine for RF22 connected to interrupt 0 + //static void isr0(); + void isr0(); + + /// Low level interrupt service routine for RF22 connected to interrupt 1 + //static void isr1(); +private: + /// Array of instances connected to interrupts 0 and 1 + //static RF22* _RF22ForInterrupt[]; + + + volatile uint8_t _mode; // One of RF22_MODE_* + + uint8_t _idleMode; + DigitalOut _slaveSelectPin; + SPI _spi; + InterruptIn _interrupt; + uint8_t _deviceType; + + //DigitalOut led1; + //DigitalOut led2; + //DigitalOut led3; + //DigitalOut led4; + + // These volatile members may get changed in the interrupt service routine + volatile uint8_t _bufLen; + uint8_t _buf[RF22_MAX_MESSAGE_LEN]; + + volatile boolean _rxBufValid; + + volatile boolean _txPacketSent; + volatile uint8_t _txBufSentIndex; + + volatile uint16_t _rxBad; + volatile uint16_t _rxGood; + volatile uint16_t _txGood; + + volatile uint8_t _lastRssi; +}; + +/// @example rf22_client.pde +/// Client side of simple client/server pair using RF22 class + +/// @example rf22_server.pde +/// Server side of simple client/server pair using RF22 class + +/// @example rf22_datagram_client.pde +/// Client side of simple client/server pair using RF22Datagram class + +/// @example rf22_datagram_server.pde +/// Server side of simple client/server pair using RF22Datagram class + +/// @example rf22_reliable_datagram_client.pde +/// Client side of simple client/server pair using RF22ReliableDatagram class + +/// @example rf22_reliable_datagram_server.pde +/// Server side of simple client/server pair using RF22ReliableDatagram class + +/// @example rf22_router_client.pde +/// Client side of RF22Router network chain + +/// @example rf22_router_server1.pde +/// Server node for RF22Router network chain + +/// @example rf22_router_server2.pde +/// Server node for RF22Router network chain + +/// @example rf22_router_server3.pde +/// Server node for RF22Router network chain + +/// @example rf22_mesh_client.pde +/// Client side of RF22Mesh network chain + +/// @example rf22_mesh_server1.pde +/// Server node for RF22Mesh network chain + +/// @example rf22_mesh_server2.pde +/// Server node for RF22Mesh network chain + +/// @example rf22_mesh_server3.pde +/// Server node for RF22Mesh network chain + +/// @example rf22_test.pde +/// Test suite for RF22 library + +/// @example rf22_snoop.pde +/// Capture and print RF22 packet from the air + +/// @example rf22_specan.pde +/// Simple spectrum analyser using the RSSI measurements of the RF22 +/// (see <a href="specan1.png">Sample output</a> showing a plot from 395.0MHz to 396.0MHz of a +/// signal generator at 395.5MHz amplitude modulated at 100% 1kHz) +/// + +/// @example IPGateway.pde +/// Sketch to provide an IP gateway for a set of RF22 radios (Datagram, ReliableDatagram, Router or Mesh) +/// Routes UDP messages from an internet connection using an Ethernet Shield and sends them +/// to a radio whose ID is based on the UDP port. Replies are sent back to the originating UDP +/// address and port + + +#endif