Skip to content

Commit 2ab643f

Browse files
committed
- added compile all warning & report on maintainer script
- added Millis using timer2 - fixed timer test - add report style, error control in proj manager
1 parent 7df0646 commit 2ab643f

File tree

14 files changed

+220
-46
lines changed

14 files changed

+220
-46
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ check:
1313
# comipile all projects
1414
compile:
1515
$(TOP)/project_manager.sh -m test/
16-
$(TOP)/project_manager.sh -m c-project/
17-
$(TOP)/project_manager.sh -m cpp-project/
16+
$(TOP)/project_manager.sh -f -m c-project/
17+
$(TOP)/project_manager.sh -f -m cpp-project/
1818

1919
# clean all projects for maintaining purpose
2020
clean:

c-project/without-os/i2c-console/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DEBUG = no
44
FLOAT_SUPPORT = no # support printf, sscanf floats
55
UTILS_SUPPORT = YES # extra utils such as soft serial, i2c, serial debug
66
PROJECT_NAME = i2c-console
7-
OPTIMIZE = no
7+
OPTIMIZE = yes
88
TOP = ../../../
99

1010
# required

c-project/without-os/i2c-console/main.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "PgmStorage.h"
1010
#include "Timer.h"
1111
#include "I2C.h"
12+
#include "Millis.h"
1213

1314
#include <stdio.h>
1415
#include <util/delay.h>
@@ -31,8 +32,6 @@ const char PROGMEM help72[] = "HISTORY \t\t - show past entered messages";
3132
const char PROGMEM help73[] = "SCAN \t\t\t - scan all I2C devices on the bus";
3233
const char PROGMEM help8[] = "========================================================================";
3334

34-
static volatile unsigned long mMillis;
35-
3635
void processMessage(const char * buffer);
3736

3837
void showHelp()
@@ -199,11 +198,6 @@ void runScan()
199198
}
200199
}
201200

202-
static void incMillis()
203-
{
204-
++mMillis;
205-
}
206-
207201
void parseNRunMessage(const char * message)
208202
{
209203
I2CConsoleMessage cmd;
@@ -273,8 +267,8 @@ void loopCommand(const char * message)
273267
SerialDebugPrint("looping %d seconds for '%s'", loop_time,
274268
message + strlen("loop ") + strlen(token) + 1);
275269

276-
uint8_t initSec = mMillis / 1000;
277-
while ((uint8_t) (mMillis / 1000) - initSec
270+
uint8_t initSec = Millis() / 1000;
271+
while ((uint8_t) (Millis() / 1000) - initSec
278272
<= (uint8_t) loop_time)
279273
{
280274
parseNRunMessage(
@@ -326,10 +320,6 @@ int main()
326320
I2CConsoleStackInit();
327321
#endif
328322

329-
Timer1Init(1);
330-
mMillis = 0;
331-
Timer1SetCallback(incMillis);
332-
333323
while (1)
334324
{
335325
char buffer[I2CMESSAGE_MAXLEN];

common/makefiles/without-os.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,22 @@ all:
7575
.c.o:
7676
@mkdir -p $(OBJ_DIR)
7777
$(COMPILE) -c $< -o $(OBJ_DIR)/$(notdir $@)
78+
@echo ""
7879

7980
.cpp.o:
8081
@mkdir -p $(OBJ_DIR)
8182
$(COMPILE_CPP) -c $< -o $(OBJ_DIR)/$(notdir $@)
83+
@echo ""
8284

8385
%.c.libo: %.c
8486
@mkdir -p $(OBJ_DIR)
8587
$(UTILS_COMPILE) -c $< -o $(OBJ_DIR)/$(notdir $@)
88+
@echo ""
8689

8790
%.cpp.libo: %.cpp
8891
@mkdir -p $(OBJ_DIR)
8992
$(UTILS_COMPILE_CPP) -c $< -o $(OBJ_DIR)/$(notdir $@)
93+
@echo ""
9094

9195
flash: all
9296
$(AVRDUDE) -U flash:w:$(HEX_NAME):i
@@ -104,12 +108,12 @@ load: all
104108
clean:
105109
rm -f $(HEX_NAME) $(BINARY_NAME) $(LIBUTILS)
106110
rm -rf $(OBJ_DIR)
111+
rm -f $(UTILS_DIR)/*.d
107112

108113
# file targets:
109114
ifeq ($(wildcard $(LIBUTILS)),) # check if libutils.a exists
110115
$(LIBUTILS): $(UTILS_OBJECTS)
111-
echo $(wildcard $(LIBUTILS))
112-
$(foreach obj, $(UTILS_OBJ_TMP), $(LINK_LIBUTILS) $(obj) ; )
116+
$(foreach obj, $(UTILS_OBJ_TMP), $(LINK_LIBUTILS) $(obj) ;)
113117
else
114118
$(LIBUTILS):
115119

common/utils/I2C.c

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
#include <compat/twi.h>
1212

1313
#include "SerialDebug.h"
14-
#include "Timer.h"
15-
16-
static volatile long _sec;
14+
#include "Millis.h"
1715

1816
/*
1917
i2c_io - write and read bytes to a slave I2C device
@@ -241,11 +239,6 @@ uint8_t i2c_io(uint8_t device_addr, const uint8_t *ap, uint16_t an,
241239
return (status);
242240
}
243241

244-
static void incSec()
245-
{
246-
++_sec;
247-
}
248-
249242
/*
250243
i2c_init - Initialize the I2C port
251244
*/
@@ -254,10 +247,7 @@ void i2c_init(uint8_t bdiv)
254247
TWSR = 0; // Set prescalar for 1
255248
TWBR = bdiv; // Set bit rate register
256249

257-
// init timer 2
258-
_sec = 0;
259-
// Timer1Init(1000);
260-
// Timer1SetCallback(incSec);
250+
MillisInit();
261251
}
262252

263253
static volatile uint8_t _i2cIsRunning;
@@ -314,13 +304,35 @@ uint8_t I2CSendnRecvData(uint8_t address, const uint8_t * txdata,
314304
*/
315305
uint8_t I2CCheckAlive(uint8_t address)
316306
{
307+
if (_i2cIsRunning)
308+
return 1;
309+
_i2cIsRunning = 1;
310+
317311
uint8_t status;
318312
uint8_t retVal = 0;
313+
unsigned long long initMillis = 0;
314+
uint8_t isValid = 0;
319315

320316
// send I2C start
321317
TRACE()
322318
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA); // Send start condition
323-
while (!(TWCR & (1 << TWINT))) ; // Wait for TWINT to be set
319+
320+
initMillis = Millis();
321+
isValid = 0;
322+
while ( Millis() - initMillis < 100)
323+
{
324+
if ((TWCR & (1 << TWINT))) // Wait for TWINT to be set in 100ms
325+
{
326+
isValid = 1;
327+
break;
328+
}
329+
}
330+
if (!isValid)
331+
{
332+
retVal = 4;
333+
goto _I2CCheckAlive_sendstop;
334+
}
335+
324336
TRACE()
325337
status = TWSR & 0xf8;
326338
if (status != 0x08) // Check that START was sent OK
@@ -334,7 +346,22 @@ uint8_t I2CCheckAlive(uint8_t address)
334346
{
335347
TWDR = (address << 1) & 0xfe; // Load device address and R/W = 0;
336348
TWCR = (1 << TWINT) | (1 << TWEN); // Start transmission
337-
while (!(TWCR & (1 << TWINT))); // Wait for TWINT to be set
349+
350+
initMillis = Millis();
351+
isValid = 0;
352+
while ( Millis() - initMillis < 100)
353+
{
354+
if ((TWCR & (1 << TWINT))) // Wait for TWINT to be set in 100ms
355+
{
356+
isValid = 1;
357+
break;
358+
}
359+
}
360+
if(!isValid)
361+
{
362+
retVal = 4;
363+
goto _I2CCheckAlive_sendstop;
364+
}
338365

339366
TRACE()
340367
status = TWSR & 0xf8;
@@ -346,14 +373,16 @@ uint8_t I2CCheckAlive(uint8_t address)
346373
}
347374
else
348375
{
349-
retVal = 4;
376+
retVal = 5;
350377
}
351378
}
352379
}
353380

381+
_I2CCheckAlive_sendstop:
354382
// send stop
355383
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); // Send STOP condition
356384
_delay_ms(1);
357385

386+
_i2cIsRunning = 0;
358387
return retVal;
359388
}

common/utils/I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ uint8_t I2CSendnRecvData(uint8_t address, const uint8_t * txdata, uint8_t txdata
3434
* 1 on NAK recv
3535
* 2 on START not ok
3636
* 3 on address not sent ok
37+
* 4 on timeout
38+
* 5 on unknown error
3739
*/
3840
uint8_t I2CCheckAlive(uint8_t address);
3941

common/utils/Millis.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Millis.c
3+
*
4+
* Created on: Jun 8, 2017
5+
* Author: dat
6+
*/
7+
8+
#include "Millis.h"
9+
#include "Timer.h"
10+
11+
static Timer2Callback _mCallback_DB[MILLIS_MAX_CALLBACKS];
12+
static uint8_t _nCallback;
13+
14+
static volatile unsigned long long _mmillis;
15+
static void incMillis()
16+
{
17+
++_mmillis;
18+
19+
if (_nCallback)
20+
{
21+
uint8_t i;
22+
for (i = 0; i < _nCallback; ++i)
23+
{
24+
_mCallback_DB[i]();
25+
}
26+
}
27+
}
28+
29+
30+
void MillisInit()
31+
{
32+
static char isInit = 0;
33+
if (isInit)
34+
{
35+
return;
36+
}
37+
isInit = 1;
38+
39+
_mmillis = 0;
40+
_nCallback = 0;
41+
42+
Timer2Init(1000);
43+
Timer2SetCallback(incMillis);
44+
}
45+
46+
unsigned long long Millis()
47+
{
48+
MillisInit();
49+
return _mmillis;
50+
}
51+
52+
uint8_t MillisRegisterCallback(Timer2Callback callback)
53+
{
54+
uint8_t retVal = 0;
55+
MillisInit();
56+
57+
if ( _nCallback == MILLIS_MAX_CALLBACKS)
58+
{
59+
return 1;
60+
}
61+
else
62+
{
63+
_mCallback_DB[_nCallback++] = callback;
64+
}
65+
66+
return retVal;
67+
}

common/utils/Millis.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Millis.h
3+
*
4+
* Created on: Jun 8, 2017
5+
* Author: dat
6+
*/
7+
8+
#ifndef MILLIS_H_
9+
#define MILLIS_H_
10+
11+
#include "Timer.h"
12+
13+
#define MILLIS_MAX_CALLBACKS 3
14+
15+
/**
16+
* Init millisecond, this will use timer2, don't use timer2 if use this Millis
17+
*
18+
*/
19+
void MillisInit();
20+
21+
/**
22+
* main getter of millis
23+
*/
24+
unsigned long long Millis();
25+
26+
/**
27+
* Register callback function for every millisecond
28+
* Supports up to MILLIS_MAX_CALLBACKS functions
29+
*
30+
* @param callback
31+
* @return 0 on success , 1 on overload
32+
*/
33+
uint8_t MillisRegisterCallback(Timer2Callback callback);
34+
35+
#endif /* MILLIS_H_ */

common/utils/SoftSerial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void SoftSerialInit()
193193
TOGGLE_PIN(SS_DDR_PORT_RX, SS_RX_PIN, 0); // input on rx
194194

195195
// config interrupt
196-
Timer2init(1000000ul / SS_BAUD - 2);
196+
Timer2Init(1000000ul / SS_BAUD - 2);
197197
Timer2SetCallback(fetchChar);
198198

199199
// config getcdata

common/utils/Timer.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "Timer.h"
99
#include <avr/interrupt.h>
1010

11-
static Timer1Callback mT1Callback, mT1Callback2;
11+
static Timer1Callback mT1Callback1, mT1Callback2;
1212
static Timer2Callback mT2Callback;
1313

1414
void Timer1Init(uint16_t ms)
@@ -18,7 +18,7 @@ void Timer1Init(uint16_t ms)
1818
return;
1919
isInited = 1;
2020

21-
mT1Callback = 0;
21+
mT1Callback1 = 0;
2222
mT1Callback2 = 0;
2323

2424
cli();
@@ -33,9 +33,9 @@ void Timer1Init(uint16_t ms)
3333

3434
void Timer1SetCallback(Timer1Callback callback)
3535
{
36-
if (!mT1Callback)
36+
if (!mT1Callback1)
3737
{
38-
mT1Callback = callback;
38+
mT1Callback1 = callback;
3939
}
4040
else
4141
{
@@ -50,9 +50,9 @@ void Timer2SetCallback(Timer2Callback callback)
5050

5151
ISR(TIMER1_COMPA_vect)
5252
{
53-
if (mT1Callback)
53+
if (mT1Callback1)
5454
{
55-
mT1Callback();
55+
mT1Callback1();
5656
}
5757

5858
if (mT1Callback2)
@@ -69,7 +69,7 @@ ISR(TIMER2_COMPA_vect)
6969
}
7070
}
7171

72-
void Timer2init(uint16_t n)
72+
void Timer2Init(uint16_t n)
7373
{
7474
static uint8_t isInited = 0;
7575
if (isInited)

0 commit comments

Comments
 (0)