static const char * sim900_wait_string(const char * const sz,const uint8_t isPGM) { #ifndef SIM900_USART_POLLED usart_buffer_flush(&sim900_usart_data); #endif const char * p=sz; char c2 = 0; while(c2=((isPGM)?(nvm_flash_read_byte(p++)):(*p++) )) { if(!sim900_wait_data_on_usart(20)) { debug_string_P(NORMAL,PSTR("(sim900_wait_string) timeout waiting for sim900 response\r\n(sim900_wait_string) OUT\r\n")); return NULL; } #ifdef SIM900_USART_POLLED const char c1=usart_get(USART_GPRS); #else const char c1=USART_RX_CBuffer_GetByte(&sim900_usart_data); #endif //if the sequence is not the same we start over again if(c1!=c2) { p = sz; continue; } } return sz; }
static void sim900_put_string(const char * const sz,const uint8_t isPGM) { // debug_string(NORMAL,PSTR("(sim900_put_string) IN\r\n"),true); const char * p = sz; if(isPGM) { while(1) { const char c = nvm_flash_read_byte((flash_addr_t)p++); if(0==c) break; while (usart_data_register_is_empty(USART_GPRS) == false) {} usart_put(USART_GPRS, c); if(g_log_verbosity>NORMAL) usart_putchar(USART_DEBUG,c); } } else { while(1) { const char c = *p++; if(0==c) break; while (usart_data_register_is_empty(USART_GPRS) == false) {} usart_put(USART_GPRS, c); if(g_log_verbosity>NORMAL) usart_putchar(USART_DEBUG,c); } } // debug_string(NORMAL,PSTR("(sim900_put_string) OUT\r\n"),true); }
status_code_t nvm_read_char(mem_type_t mem, uint32_t address, uint8_t *data) { switch (mem) { case INT_FLASH: *data = nvm_flash_read_byte((flash_addr_t)address); break; case INT_USERPAGE: nvm_user_sig_read_buffer((flash_addr_t)address, (void *)data, 1); break; case INT_EEPROM: *data = nvm_eeprom_read_byte((eeprom_addr_t)address); break; #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX) case AT45DBX: if (!at45dbx_read_byte_open(address)) { return ERR_BAD_ADDRESS; } *data = at45dbx_read_byte(); at45dbx_read_close(); break; #endif default: return ERR_INVALID_ARG; } return STATUS_OK; }
void debug_string(uint8_t level,const char * const sz,const uint8_t isPGM) { if(level>g_log_verbosity) return; const char * p = sz; if(isPGM) { nvm_wait_until_ready(); while(1) { const uint8_t c = nvm_flash_read_byte( p++ ); if(c==0) break; // while (udi_cdc_is_tx_ready()) {} // udi_cdc_putc(c); while (usart_data_register_is_empty(USART_DEBUG) == false) {} usart_put(USART_DEBUG, c); } } else { while(*p) { while (usart_data_register_is_empty(USART_DEBUG) == false) {} usart_put(USART_DEBUG, *p++); //while (udi_cdc_is_tx_ready()) {} //udi_cdc_putc(*p++); } } }
/** * \brief Read buffer within the application section * * \param address the address to where to read * \param buf pointer to the data * \param len the number of bytes to read */ void nvm_flash_read_buffer(flash_addr_t address, void *buf, uint16_t len) { #if (FLASH_SIZE>0x10000) uint32_t opt_address = address; #else uint16_t opt_address = (uint16_t)address; #endif nvm_wait_until_ready(); while ( len ) { *(uint8_t*)buf = nvm_flash_read_byte(opt_address); buf=(uint8_t*)buf+1; opt_address++; len--; } }
static uint8_t sim900_wait_data_on_usart(uint8_t seconds) { for(uint8_t d1=seconds;d1>0;--d1) { for(uint16_t d2=10000;d2>0;--d2) { #ifdef SIM900_USART_POLLED if(usart_rx_is_complete(USART_GPRS)) { #else if(USART_RX_CBuffer_Data_Available(&sim900_usart_data)) { #endif return 0xFF; } delay_us(100); } } debug_string(VERY_VERBOSE,PSTR("(sim900_wait_data_on_usart) timed-out\r\n"),PGM_STRING); return 0; } static uint8_t sim900_read_string(char * const szBuf,uint8_t * const lenBuf) { const uint8_t ec = *lenBuf; uint8_t l = 0; uint8_t r = 0; char c; #ifndef SIM900_USART_POLLED usart_buffer_flush(&sim900_usart_data); #endif while(1) { if(!sim900_wait_data_on_usart(10)) { debug_string(NORMAL,PSTR("(sim900_read_string) got timeout waiting for a character\r\n"),true); r=1; break; } #ifdef SIM900_USART_POLLED c = usart_get(USART_GPRS); #else c = USART_RX_CBuffer_GetByte(&sim900_usart_data); #endif if(c=='\r') { if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'@'); } else if(c=='\n') { if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'#'); } else break; } if(r==0) while(l<ec) { szBuf[l++] = c; if(!sim900_wait_data_on_usart(10)) { debug_string(NORMAL,PSTR("(sim900_read_string) got timeout waiting for a character\r\n"),true); r=1; break; } #ifdef SIM900_USART_POLLED c = usart_get(USART_GPRS); #else c = USART_RX_CBuffer_GetByte(&sim900_usart_data); #endif if(c=='\r') { //if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'@'); break; } else if(c=='\n') { //if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'#'); break; } //if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'.'); } if(ec==l) { r = 2; debug_string(NORMAL,PSTR("(sim900_read_string) Provided buffer is not big enough. Discarding chars\r\n"),true); l -= 1; } szBuf[l] = 0; //debug_string(VERBOSE,szBuf,RAM_STRING); //debug_string(VERBOSE,szCRLF,PGM_STRING); //debug_string(VERBOSE,PSTR("(sim900_read_string) out\r\n"),true); *lenBuf = l; return r; } static const char * sim900_wait4dictionary(const char * const dictionary[],uint8_t len) { uint8_t num = len; const char * p[len]; #ifndef SIM900_USART_POLLED usart_buffer_flush(&sim900_usart_data); #endif //debug_string(VERBOSE,PSTR("(sim900_wait4dictionary) IN\r\n"),true); while(num--) { p[num] = nvm_flash_read_word(dictionary+num); } while(1) { if(!sim900_wait_data_on_usart(20)) { debug_string(NORMAL,PSTR("(sim900_wait4dictionary) timeout waiting for sim900 response\r\n(sim900_wait4dictionary) OUT\r\n"),true); return NULL; } #ifdef SIM900_USART_POLLED const char c1 = usart_get(USART_GPRS); #else const char c1=USART_RX_CBuffer_GetByte(&sim900_usart_data); #endif for(uint8_t i=0;i<len;++i) { const char c2 = nvm_flash_read_byte(p[i]); if(c1!=c2) { p[i]=(char *) nvm_flash_read_word(dictionary+i); continue; } p[i]++; if(nvm_flash_read_byte(p[i])==0) { const char * const r = nvm_flash_read_word(dictionary+i); //debug_string(VERY_VERBOSE,PSTR("(sim900_wait4dictionary) got: "),true); //debug_string(VERY_VERBOSE,r,true); //debug_string(VERY_VERBOSE,szCRLF,true); //debug_string(NORMAL,PSTR("(sim900_wait4dictionary) OUT\r\n"),true); return r; } } } }