SPI and I2C will accept unsupported frequencies and match them with the closest available frequency instead of generating an error.

Sleep is now implemented (deepsleep will cause nrf to work in System OFF mode).
pull/159/head
tkuyucu 2014-01-10 14:21:43 +01:00
parent c7eabcd977
commit 29560a3195
6 changed files with 74 additions and 46 deletions

View File

@ -45,7 +45,7 @@
#define DEVICE_LOCALFILESYSTEM 0
//#define DEVICE_ID_LENGTH 24
#define DEVICE_SLEEP 0
#define DEVICE_SLEEP 1
#define DEVICE_DEBUG_AWARENESS 0

View File

@ -105,13 +105,16 @@ inline int i2c_start(i2c_t *obj) {
}
inline int i2c_stop(i2c_t *obj) {
// int timeout = 0;
int timeOut = 100000;
obj->i2c->EVENTS_STOPPED = 0;
// write the stop bit
obj->i2c->TASKS_STOP=1;
while(!obj->i2c->EVENTS_STOPPED)
{
}
timeOut--;
if(timeOut<0)
return 1;
}
//obj->i2c->ADDRESS = 0;
addrSet=0;
i2c_reset(obj);
@ -121,7 +124,7 @@ inline int i2c_stop(i2c_t *obj) {
int i2c_do_write(i2c_t *obj, int value) {
int timeOut=1000;
int timeOut=100000;
obj->i2c->TXD = value;
while(!obj->i2c->EVENTS_TXDSENT){
timeOut--;
@ -132,7 +135,7 @@ int i2c_do_write(i2c_t *obj, int value) {
return 0;
}
int i2c_do_read(i2c_t *obj, char * data, int last) {
int timeOut=1000;
int timeOut=100000;
while(!obj->i2c->EVENTS_RXDREADY){
timeOut--;
if(timeOut<0)
@ -153,39 +156,40 @@ int i2c_do_read(i2c_t *obj, char * data, int last) {
void i2c_frequency(i2c_t *obj, int hz) {
obj->freq=hz;
switch(hz){
case 100000:obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);break;
case 250000:obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K250 << TWI_FREQUENCY_FREQUENCY_Pos);break;
case 400000:obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K400 << TWI_FREQUENCY_FREQUENCY_Pos);break;
default:error("I2C frequency requested is not supported"); break;
if(hz<250000){
obj->freq=100000;
obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);
}
else if(hz<400000){
obj->freq=250000;
obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K250 << TWI_FREQUENCY_FREQUENCY_Pos);
}
else{
obj->freq=400000;
obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K400 << TWI_FREQUENCY_FREQUENCY_Pos);
}
}
int checkError(i2c_t *obj)
{
if (obj->i2c->EVENTS_ERROR == 1)
{
if ((obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk) == (TWI_ERRORSRC_ANACK_Present << TWI_ERRORSRC_ANACK_Pos))
{
obj->i2c->EVENTS_ERROR = 0;
if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk)
{
obj->i2c->EVENTS_ERROR = 0;
obj->i2c->TASKS_STOP = 1;
obj->i2c->ERRORSRC |= (TWI_ERRORSRC_ANACK_Present << TWI_ERRORSRC_ANACK_Pos);
return I2C_ERROR_BUS_BUSY;
}
}
if (obj->i2c->EVENTS_ERROR == 1)
{
obj->i2c->EVENTS_ERROR = 0;
obj->i2c->TASKS_STOP = 1;
if ((obj->i2c->ERRORSRC & TWI_ERRORSRC_DNACK_Msk) == (TWI_ERRORSRC_DNACK_Present << TWI_ERRORSRC_DNACK_Pos))
{
obj->i2c->ERRORSRC |= (TWI_ERRORSRC_DNACK_Present << TWI_ERRORSRC_DNACK_Pos);
//else if (obj->i2c->ERRORSRC & TWI_ERRORSRC_DNACK_Msk)
// {
obj->i2c->EVENTS_ERROR = 0;
obj->i2c->TASKS_STOP = 1;
return I2C_ERROR_NO_SLAVE;
}
}
// }
}
return 0;
}
@ -198,9 +202,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
// Read in all except last byte
for (count = 0; count < (length - 1); count++) {
status = i2c_do_read(obj,&data[count], 0);
if (status) {
i2c_reset(obj);
if (status) {
int errorResult = checkError(obj);
i2c_reset(obj);
if(errorResult<0)
return errorResult;
return count;
@ -247,7 +251,9 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
// If not repeated start, send stop.
if (stop) {
i2c_stop(obj);
if(i2c_stop(obj)){
return I2C_ERROR_NO_SLAVE;
}
}

View File

@ -22,7 +22,7 @@ void sleep(void) {
// mbed_interface_disconnect();
NRF_POWER->TASKS_LOWPWR=1;
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
// SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
// wait for interrupt
__WFI();
@ -31,8 +31,7 @@ void sleep(void) {
void deepsleep(void) {
// PCON[PD] set to deepsleep
// sleep();
NRF_POWER->TASKS_LOWPWR=1;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();
sleep();
NRF_POWER->SYSTEMOFF=1;
// SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
}

View File

@ -178,17 +178,29 @@ void spi_frequency(spi_t *obj, int hz) {
if((int)obj->spi==NC)
return;
spi_disable(obj,0);
switch(hz)
{
case 125000: obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL); break;
case 250000:obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 1 ); break;
case 500000:obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 2 ); break;
case 1000000:obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 3 ); break;
case 2000000:obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 4 ); break;
case 4000000:obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 5 ); break;
case 8000000:obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 6 ); break;
default:error("Couldn't setup requested SPI frequency"); return;
if(hz<250000) { //125Khz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL);
}
else if(hz<500000){//250KHz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 1 );
}
else if(hz<1000000){//500KHz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 2 );
}
else if(hz<2000000){//1Mhz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 3 );
}
else if(hz<4000000){//2MHz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 4 );
}
else if(hz<8000000){//4MHz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 5 );
}
else{//8MHz
obj->spi->FREQUENCY = (uint32_t)( 0x02000000UL << 6 );
}
spi_enable(obj,0);
}

View File

@ -49,13 +49,19 @@ void test_close(FILE* f) {
}
}
DigitalOut led2(LED2);
int main() {
#if defined(TARGET_KL25Z)
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
#elif defined(TARGET_nRF51822)
//SDFileSystem sd(p20, p22, p25, p24, "sd");
SDFileSystem sd(p12, p13, p15, p14, "sd");
#else
SDFileSystem sd(p11, p12, p13, p14, "sd");
#endif
led2=1;
wait(0.5);
FILE *f;
char* str = TEST_STRING;
char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING));
@ -67,6 +73,7 @@ int main() {
sprintf(filename, "/sd/test_%d.txt", i);
printf("Creating file: %s\n", filename);
f = test_open(filename, "w");
led2=0;
test_write(f, str);
test_close(f);
}

View File

@ -4,12 +4,16 @@
#if defined(TARGET_KL25Z)
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
#elif defined(TARGET_nRF51822)
//SDFileSystem sd(p20, p22, p25, p24, "sd");
SDFileSystem sd(p12, p13, p15, p14, "sd");
#else
SDFileSystem sd(p11, p12, p13, p14, "sd");
#endif
#define SIZE 120
DigitalOut led1(LED1);
int main() {
FILE *f = fopen("/sd/out.txt", "w");