diff options
Diffstat (limited to 'software/rda1846dongle/rda1846.c')
-rw-r--r-- | software/rda1846dongle/rda1846.c | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/software/rda1846dongle/rda1846.c b/software/rda1846dongle/rda1846.c index 5277d3e..eaa0fbc 100644 --- a/software/rda1846dongle/rda1846.c +++ b/software/rda1846dongle/rda1846.c @@ -29,12 +29,10 @@ #define RDA1846_CHIP_ADDR 0xE2 #define RDA1846_ADDR_W (0<<7) #define RDA1846_ADDR_R (1<<7) +#define RDA1846_ADDR_LIMIT 0x7F -uint8_t rda1846_write_register(const uint8_t addr, const uint16_t data) +static uint8_t rda1846_write_register_raw(const uint8_t addr, const uint16_t data) { - if(addr >= 0x7F) // no support for this at the moment !!! - return 255; - if(TWI_StartTransmission(RDA1846_CHIP_ADDR | TWI_ADDRESS_WRITE,10) != TWI_ERROR_NoError) return 1; @@ -53,11 +51,8 @@ i2c_error: return 1; } -uint8_t rda1846_read_register(const uint8_t addr, uint16_t* data) +static uint8_t rda1846_read_register_raw(const uint8_t addr, uint16_t* data) { - if(addr >= 0x7F) // no support for this at the moment !!! - return 255; - if(TWI_StartTransmission(RDA1846_CHIP_ADDR | TWI_ADDRESS_WRITE,10) != TWI_ERROR_NoError) return 1; if(!TWI_SendByte(addr | RDA1846_ADDR_R)) @@ -71,7 +66,7 @@ uint8_t rda1846_read_register(const uint8_t addr, uint16_t* data) *data = tmp << 8; if(!TWI_ReceiveByte(&tmp, 1)) goto i2c_error; - data |= tmp; + *data |= tmp; TWI_StopTransmission(); return 0; @@ -81,7 +76,42 @@ i2c_error: return 1; } + void rda1846_init(void) { TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 200000)); } + +uint8_t rda1846_write_register(const uint8_t addr, const uint16_t data) +{ + if(addr < RDA1846_ADDR_LIMIT) + return rda1846_write_register_raw(addr, data); + + if(addr > RDA1846_ADDR_LIMIT) { + uint8_t ret = rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 1); + if(ret) return ret; + + ret = rda1846_write_register_raw(addr, data); + if(ret) return ret; + + return rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 0); + } + return 1; +} + +uint8_t rda1846_read_register(const uint8_t addr, uint16_t* data) +{ + if(addr < RDA1846_ADDR_LIMIT) + return rda1846_read_register_raw(addr, data); + + if(addr > RDA1846_ADDR_LIMIT) { + uint8_t ret = rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 1); + if(ret) return ret; + + ret = rda1846_read_register_raw(addr, data); + if(ret) return ret; + + return rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 0); + } + return 1; +} |