-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
474 lines (353 loc) · 10.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#warning Check clock settings when switching between dev/prod boards!!
#warning SET THE ALTERNATIVE OPTION BYTE!!!! for PWM to work
#include "main.h"
#include "stm8s_gpio.h"
#include "stm8s.h"
#include "SPI_Bitbang.h"
#include "delay.h"
#include "LT8900.h"
#include "uart.h"
#include "utils.c"
//#include "milight.h"
#include <intrinsics.h>
//prototypes
void initHardware(void);
void loop(void);
void sendData(void);
static void RADIO_init(void);
static void TIM1_Config(void);
static void TIM4_Config(void);
static void TestColors(void);
//radio protos
static void handlePacket(uint8_t *buff, uint8_t);
static void BroadcastPresenseRequest(void);
static void RequestStateRequest(void);
//global objs
//the bitbanged SPI port
SPI_BitBang spi(LT8900_PORT_CLK,
LT8900_PORT_MOSI,
LT8900_PORT_MISO,
PIN_BB_CLK,
PIN_BB_MOSI,
PIN_BB_MISO);
//the radio
LT8900 radio(&spi,
LT8900_PORT_SS,
LT8900_PORT_PKT,
LT8900_PORT_RESET,
LT8900_PORT_DVSS,
LT8900_PIN_SS,
LT8900_PIN_PKT,
LT8900_PIN_RESET,
LT8900_PIN_DVSS);
uint8_t CURRENT_COLORS[4], SETPOINT_COLORS[4],
TIME_TO_NEW=0;
//Settings
#define BROADCAST_HW_TICK 10000L //iedere 10 sec?
#define RED_COMPARE(x) (TIM1_SetCompare2(gamma8[x])) //as per pinout measured on rgbw Milight board
#define GREEN_COMPARE(x) (TIM1_SetCompare1(gamma8[x]))
#define BLUE_COMPARE(x) (TIM1_SetCompare4(gamma8[x]))
#define WHITE_COMPARE(x) (TIM1_SetCompare3(gamma8[x]))
#define PACKET_BROADCASTPRESENSE 0
#define PACKET_SETVALUES 1
#define PACKET_REQUESTSTATE 2
unsigned char myID[12];
//node settings
#define NODE_ID 1
#define NODE_TYPE 0
//periodic timer
long volatile tickMs = 0;
long lastTickMs =0;
//#pragma vector=25 //ITC_IRQ_TIM4_OVF
INTERRUPT_HANDLER(TIMER4_UPD_OVF_IRQHandler, ITC_IRQ_TIM4_OVF)
{
//called every 1ms
// GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
tickMs ++;
//don't forget to clear our interrupt!
TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}
int main()
{
initHardware();
#ifdef BREAD
//not supported on stm8s003??
myUniqueId(myID);
myID;
#endif
//https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms/
//R = (pwmIntervals * log10(2))/(log10(255)); // = 12.50882898658681381510320672289928392090409223903489242395
loop();
return 0;
}
void initHardware(){
/*
* Init System clock to 16mhz
*/
CLK_DeInit();
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); //cpu prescaler = 1
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); //prescaler 1 , 16mhz
#ifdef CRYSTALTIMER
//use crystal 12mhz
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, //auto switch
CLK_SOURCE_HSE, //internal osc
DISABLE, //no clk-switch-inter
CLK_CURRENTCLOCKSTATE_DISABLE); //disable prev. clk
#endif
#ifdef INTERNALTIMER
// use internal osc
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, //auto switch
CLK_SOURCE_HSI, //internal osc
DISABLE, //no clk-switch-inter
CLK_CURRENTCLOCKSTATE_DISABLE); //disable prev. clk
#endif
// Configure Quartz Clock
/*
* Init all ports used
*/
GPIO_DeInit(GPIOA);
//led port
GPIO_DeInit(GPIOB);
//sw spi
GPIO_DeInit(GPIOC);
//pins lt8900
GPIO_DeInit(GPIOD);
//configure onboard led:
GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);
// GPIO_WriteLow(GPIOB, GPIO_PIN_5);
//GPIO_WriteHigh(GPIOB, GPIO_PIN_5);
/*
* Init Timer 1
*/
//setup timer1 for pwm generation
TIM1_Config();
//setup timer to generate ticks
TIM4_Config();
//enable interrupts
//NOTE DELAY FUNCTION depends on tick increments!!
enableInterrupts();
/*
* init SPI
*/
spi.begin();
//INIT UART
//Uart_Init();
//SerialPutString("Uart init Complete\n");
//sendData();
/*
* Init Radio
*/
RADIO_init();
//init
//ColorsTest
TestColors();
//broadcast my presense
BroadcastPresenseRequest();
}
static void RADIO_init(void){
radio.begin();
radio.setCurrentControl(15, 15);
radio.setDataRate(LT8900::LT8900_1MBPS);
radio.setChannel(0x00);
radio.writeRegister(36, 0x147a);
radio.writeRegister(39, 0x258b);
radio.writeRegister2(32, 72, 0); // PL1167's Data Configure Register: LEN_PREAMBLE = 010 -> (0xAAAAAA) 3 bytes, LEN_SYNCWORD = 01 -> 32 bits, LEN_TRAILER = 000 -> (0x05) 4 bits, TYPE_PKT_DAT = 00 -> NRZ law data, TYPE_FEC = 00 -> No FEC
radio.writeRegister2(41, 176, 0); // PL1167's Miscellaneous Register: CRC_ON = 1 -> ON, SCR_ON = 0 -> OFF, EN_PACK_LEN = 1 -> ON, FW_TERM_TX = 1 -> ON, AUTO_ACK = 0 -> OFF, PKT_LEVEL = 0 -> PKT active high, CRC_INIT_DAT = 0
//verify chip registers.
for (int i = 0; i <= 50; i++)
{
uint16_t value = radio.readRegister(i);
//char str[10] = {0};
//printf("test");
//sprintf(str, "%d = %04x\r\n", i, value);
//SerialPutString((char*)str);
}
}
static void TIM1_Config(void){
/*
* WARNING, SET THE ALTERNATIVE OPTION BYTE!!!! for PWM to work
*/
#define PRESCALER 0
#define TOPVAL 255
#define REPEAT 0
#define CCR1_Val ((uint16_t)0)
#define CCR2_Val ((uint16_t)0)
#define CCR3_Val ((uint16_t)0)
#define CCR4_Val ((uint16_t)0)
TIM1_DeInit();
TIM1_TimeBaseInit(PRESCALER, TIM1_COUNTERMODE_UP, TOPVAL, REPEAT);
TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR2_Val*/
TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR2_Val,
TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR3_Val*/
TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR4_Val*/
TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, CCR4_Val, TIM1_OCPOLARITY_LOW,
TIM1_OCIDLESTATE_SET);
/* TIM1 counter enable */
TIM1_Cmd(ENABLE);
/* TIM1 Main Output Enable */
TIM1_CtrlPWMOutputs(ENABLE);
}
static void TIM4_Config(void){
//see en.CD00190271.pdf SMT8S registers doc
//page 249
CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER4 , ENABLE);
TIM4_DeInit();
/* Time base configuration */
#ifdef CRYSTALTIMER
//CPU on milight board is powerd by 12mhz crystal
//12.000 / 128 = ~93.75
TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0x5E); //94 DEC -> hex
#endif
TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0x7F);
#ifdef INTERNALTIMER
#endif
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
TIM4_Cmd(ENABLE); // Enable TIM4
}
///
/// Test routine to ease indentifying pwm pin mapping
///
static void TestColors(void){
for(int x=0;x<255;x++){
RED_COMPARE(x) ;
delay_ms(1);
}
RED_COMPARE(0);
for(int x=0;x<255;x++){
GREEN_COMPARE(x);
delay_ms(1);
}
GREEN_COMPARE(0);
for(int x=0;x<255;x++){
BLUE_COMPARE(x) ;
delay_ms(1);
}
BLUE_COMPARE(0) ;
for(int x=0;x<255;x++){
WHITE_COMPARE(x);
delay_ms(1);
}
WHITE_COMPARE(0);
}
static void BroadcastPresenseRequest(void){
const int pkgLen = 13;
uint8_t buff[pkgLen];
buff[0] = NODE_ID;
buff[1] = PACKET_BROADCASTPRESENSE;
for (int x=0;x<12;x++){
buff[x+2] = myID[x];
}
for (int x=0;x<10;x++)
radio.sendPacket(buff,pkgLen);
}
static void RequestStateRequest(void){
uint8_t buff[2];
buff[0] = NODE_ID;
buff[1] = PACKET_REQUESTSTATE;
radio.sendPacket(buff,2);
}
///
/// Main program loop
///
void loop(){
radio.startListening();
while(1){
if( lastTickMs != tickMs){
//prevent multiple exec of routine within same tick
lastTickMs = tickMs;
if(tickMs % BROADCAST_HW_TICK ==0 ){
//broadcast
BroadcastPresenseRequest();
RequestStateRequest();
}
if(radio.available()){
uint8_t buff[32];
int pLength = radio.read(buff,32);
if(pLength >0){
GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
handlePacket(buff, pLength);
}
else{
//failed
}
radio.startListening();
}
//calculate the increment/decrement for the setpoints
if(TIME_TO_NEW > 0){
long c = tickMs % (TIME_TO_NEW +1);
if (c == 0 ){
for(int x=0; x< 4;x++){
if (CURRENT_COLORS[x] < SETPOINT_COLORS[x])
CURRENT_COLORS[x] ++;
else if (CURRENT_COLORS[x] > SETPOINT_COLORS[x])
CURRENT_COLORS[x] --;
}
}
}
//load compare registers with modified colors
RED_COMPARE(CURRENT_COLORS[0]);
GREEN_COMPARE(CURRENT_COLORS[1]);
BLUE_COMPARE(CURRENT_COLORS[2]);
WHITE_COMPARE(CURRENT_COLORS[3]);
}
}
}
static void handlePacket(uint8_t *buff, uint8_t pLength){
uint8_t nodeId = buff[0];
char action = buff[1];
if(nodeId != NODE_ID){
//not for us, ignore
return;
}
switch(action){
case PACKET_REQUESTSTATE:
// ignore;
break;
case PACKET_BROADCASTPRESENSE:
//ignore
break;
case PACKET_SETVALUES:
///
//copy time to time-change
TIME_TO_NEW = buff[6];
for(int x = 2; x<6;x++){
//set colors to future registers
if (TIME_TO_NEW == 0){
//copy colors to CURRENT to immediately latch
CURRENT_COLORS[x-3] = buff[x];
}
//copy colors to setpoints
SETPOINT_COLORS[x-3] = buff[x];
}
break;
default:
//ignore
break;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif