]> rtime.felk.cvut.cz Git - sysless.git/blob - board/arm/lpc17xx-common/libs/bspbase/bsp0hwinit.c
iap_kvpb: create the library at the level arch not in the machine level.
[sysless.git] / board / arm / lpc17xx-common / libs / bspbase / bsp0hwinit.c
1 #include "local_config.h"
2 #include <system_def.h>
3 #include <cpu_def.h>
4 #include <lt_timer_types.h>
5 #include <hal_machperiph.h>
6 #ifdef CONFIG_KEYVAL
7   #include <keyvalpb.h>
8   #include <lpciap.h>
9   #include <lpciap_kvpb.h>
10 #endif /* CONFIG_KEYVAL */
11 #ifdef CONFIG_STDIO_COM_PORT
12   #include <uart.h>
13 #endif 
14 #ifdef CONFIG_OC_UL_DRV_SYSLESS
15   #include <ul_lib/ulan.h>
16   #include <string.h>
17   #include <ul_drv_init.h>
18   #include <ul_drv_iac.h>
19   #include <ul_lib/ul_drvdef.h>
20   extern long int uld_jiffies;
21 #endif /* CONFIG_OC_UL_DRV_SYSLESS */
22
23
24 /* timers */
25 volatile lt_ticks_t sys_timer_ticks;
26
27 static void sysInit(void) 
28 {
29
30   //lpc_pll_off();
31   //lpc_pll_on();
32   system_clock_init();
33
34   // setup the parallel port pin
35   GPIO0->FIOCLR = P0IO_ZERO_BITS;         // clear the ZEROs output
36   GPIO0->FIOSET = P0IO_ONE_BITS;          // set the ONEs output
37   GPIO0->FIODIR = P0IO_OUTPUT_BITS;       // set the output bit direction
38
39   GPIO1->FIOCLR = P1IO_ZERO_BITS;         // clear the ZEROs output
40   GPIO1->FIOSET = P1IO_ONE_BITS;          // set the ONEs output
41   GPIO1->FIODIR = P1IO_OUTPUT_BITS;       // set the output bit direction
42
43   GPIO2->FIOCLR = P2IO_ZERO_BITS;         // clear the ZEROs output
44   GPIO2->FIOSET = P2IO_ONE_BITS;          // set the ONEs output
45   GPIO2->FIODIR = P2IO_OUTPUT_BITS;       // set the output bit direction
46
47   GPIO3->FIOCLR = P3IO_ZERO_BITS;         // clear the ZEROs output
48   GPIO3->FIOSET = P3IO_ONE_BITS;          // set the ONEs output
49   GPIO3->FIODIR = P3IO_OUTPUT_BITS;       // set the output bit direction
50
51   GPIO4->FIOCLR = P4IO_ZERO_BITS;         // clear the ZEROs output
52   GPIO4->FIOSET = P4IO_ONE_BITS;          // set the ONEs output
53   GPIO4->FIODIR = P4IO_OUTPUT_BITS;       // set the output bit direction
54 }
55
56 IRQ_HANDLER_FNC(timer0_isr)
57 {
58   unsigned int ir;
59
60   ir=TIM0->IR;
61   if (ir&0x01) {
62     do {
63       TIM0->MR0 += PCLK / SYS_TIMER_HZ;
64       TIM0->IR=0x01;               // Clear match0 interrupt
65      #ifdef CONFIG_OC_UL_DRV_SYSLESS
66       uld_jiffies++;
67      #endif
68       sys_timer_ticks++;
69     } while (((int32_t)(TIM0->MR0-TIM0->TC))<0);
70   }
71   return IRQ_HANDLED;
72 }
73
74 void timerInit(void)
75 {
76   sys_timer_ticks=0;
77
78   request_irq(TIMER0_IRQn, timer0_isr, 0, NULL,NULL);
79   enable_irq(TIMER0_IRQn);
80
81   TIM0->TC=0;
82   TIM0->MCR=0;
83
84   TIM0->MR0= PCLK / SYS_TIMER_HZ;
85   TIM0->MCR|=1;                           // TMCR_MR0_I;
86
87   TIM0->TCR = 1;                          //Run timer 0                                                                                                                                                                                */
88 }
89
90 #ifdef CONFIG_STDIO_COM_PORT
91
92 int uartcon_write(int file, const char * ptr, int len)
93 {
94   int cnt;
95   unsigned char ch;
96   for(cnt=0;cnt<len;cnt++,ptr++){
97     ch=*ptr;
98     if(ch==0xa)
99       uart0Putch(0xd);
100     uart0Putch(ch);
101   }
102   return cnt;
103 }
104
105 void init_system_stub(void) {
106   system_stub_ops.write=uartcon_write;
107 }
108
109 #endif /* CONFIG_STDIO_COM_PORT */
110
111 #ifdef CONFIG_OC_UL_DRV_SYSLESS
112
113 extern unsigned uld_debug_flg; /* Left application  set defaults */
114
115 #ifndef CONFIG_KEYVAL
116 unsigned long lpciap_buff[ISP_RAM2FLASH_BLOCK_SIZE/4];
117 #endif /* CONFIG_KEYVAL */
118
119 #define UL_MTYPE_START32BIT 0x100
120
121 static inline int ul_iac_mem_head_rd(uint8_t *buf, int len,
122                       uint32_t* pmtype, uint32_t* pstart, uint32_t* plen)
123 {
124   uint32_t val, mtype;
125   if (len<6) return -1;
126   mtype=*(buf++);       /* memory type */
127   mtype+=*(buf++)<<8;
128   val=*(buf++);           /* start address */
129   val+=*(buf++)<<8;
130   if(mtype&UL_MTYPE_START32BIT){
131     if (len<8) return -1;
132     val+=(uint32_t)*(buf++)<<16;
133     val+=(uint32_t)*(buf++)<<24;
134   }
135   *pstart=val;
136   val=*(buf++);           /* length */
137   val+=*(buf++)<<8;
138   if(mtype&UL_MTYPE_START32BIT){
139     if (len==10) {
140       val+=(uint32_t)*(buf++)<<16;
141       val+=(uint32_t)*(buf++)<<24;
142     }
143   }
144   *plen=val;
145   mtype&=~UL_MTYPE_START32BIT;       /* 32-bit start address */
146   *pmtype=mtype;
147   return 0;
148 }
149
150 int ul_iac_call_rdm(struct ul_drv *udrv,ul_msginfo *msginfo,char *ibuff,ul_iac_data *data)
151 {
152   uint32_t mtype,start,len;
153
154   data->len=0;
155
156   if(ul_iac_mem_head_rd((uint8_t *)ibuff, msginfo->len,&mtype,&start,&len))
157     return UL_IAC_RC_PROC;
158
159   if (mtype==0x00) {
160     data->len=len;
161     data->buff=(char*)start;
162     return UL_IAC_RC_FREEMSG;
163   }
164   return UL_IAC_RC_PROC;
165 }
166
167 int ul_iac_call_erm(struct ul_drv *udrv,ul_msginfo *msginfo,char *ibuff,ul_iac_data *data)
168 {
169   uint32_t mtype,start,len;
170
171   data->len=0;
172
173   if(ul_iac_mem_head_rd((uint8_t *)ibuff, msginfo->len,&mtype,&start,&len))
174     return UL_IAC_RC_PROC;
175
176  #ifdef CONFIG_KEYVAL
177   if (mtype==0x01) {
178     lpcisp_erase((void*)start,len);
179     data->len=0;
180     return UL_IAC_RC_FREEMSG;
181   }
182  #endif /* CONFIG_KEYVAL */
183   return UL_IAC_RC_PROC;
184 }
185
186 int ul_iac_call_wrm(struct ul_drv *udrv,ul_msginfo *msginfo,char *ibuff,ul_iac_data *data)
187 {
188   uint32_t mtype,start,len;
189
190   data->len=0;
191
192   if(ul_iac_mem_head_rd((uint8_t *)ibuff, msginfo->len,&mtype,&start,&len))
193     return UL_IAC_RC_PROC;
194
195   if (mtype==0x00) {
196     memcpy((void*)start,data->buff,data->len);
197     return UL_IAC_RC_FREEMSG;
198   }
199  #ifdef CONFIG_KEYVAL
200   if (mtype==0x01) {
201     lpcisp_write((char*)start, data->buff, ISP_RAM2FLASH_BLOCK_SIZE);
202     return UL_IAC_RC_FREEMSG;
203   }
204  #endif /* CONFIG_KEYVAL */
205   return UL_IAC_RC_PROC;
206 }
207
208
209 int ul_iac_call_deb(struct ul_drv *udrv,ul_msginfo *msginfo,char *ibuff,ul_iac_data *data)
210 {
211   uint32_t debcmd,mtype,start;
212   uint8_t *p=(uint8_t*)ibuff;
213
214   if (msginfo->len<1) return UL_IAC_RC_PROC;
215   debcmd=*(p++);
216   switch (debcmd) {
217     case 0x10: /* goto */
218       data->len=0;
219       if (msginfo->len<5) return UL_IAC_RC_PROC;
220       mtype=*(p++);
221       mtype+=*(p++)<<8;
222       start=*(p++);
223       start+=*(p++)<<8;
224       if(mtype&UL_MTYPE_START32BIT){
225         mtype&=~UL_MTYPE_START32BIT;
226         if (msginfo->len<7) return UL_IAC_RC_PROC;
227         start+=(uint32_t)*(p++)<<16;
228         start+=(uint32_t)*(p++)<<24;
229       }
230       if (mtype==0x00)
231         ((void (*)())start)();
232     default:break;
233   }
234   return UL_IAC_RC_PROC;
235 }
236
237 int ul_iac_call_res(struct ul_drv *udrv,ul_msginfo *msginfo,char *ibuff,ul_iac_data *data)
238 {
239   uint32_t rescmd,pass;
240   uint8_t *p=(uint8_t*)ibuff;
241
242   if (msginfo->len<1) return UL_IAC_RC_PROC;
243   rescmd=*(p++);
244   switch (rescmd) {
245     case ULRES_CPU: /* CPU */
246       data->len=0;
247       if (msginfo->len<3) return UL_IAC_RC_PROC;
248       pass=*(p++);
249       pass+=*(p++)<<8;
250       if (pass==0xaa55) {
251 //        MEMMAP=MEMMAP_FLASH;
252         lpc_watchdog_init(1,10); /* 10ms */
253         lpc_watchdog_feed();
254         while(1);
255       }
256     default:break;
257   }
258   return UL_IAC_RC_PROC;
259 }
260
261 int uLanInit()
262 {
263   struct ul_drv *udrv;
264
265   /* set rs485 mode for UART1 */
266   PINCON->PINSEL4 = (PINCON->PINSEL4 & ~0x00000F0F) | 0x00000A0A; /* dsr(rxd), dtr(rs485_dir), rxd, txd */
267
268   udrv=ul_drv_new(UL_DRV_SYSLESS_PORT,      /* port */
269              UL_DRV_SYSLESS_IRQ,            /* irq */
270              UL_DRV_SYSLESS_BAUD,           /* baud */
271              UL_DRV_SYSLESS_MY_ADR_DEFAULT, /* my adr */
272         #ifdef CONFIG_OC_UL_DRV_U450_VARPINS
273           #if defined(CONFIG_OC_UL_DRV_U450_VARPINS_DIRNEG) && defined(CONFIG_OC_UL_DRV_U450_VARPINS_MSRSWAP)
274              "16450-dirneg-msrswap",        /* chip name */
275           #elif defined(CONFIG_OC_UL_DRV_U450_VARPINS_MSRSWAP)
276              "16450-msrswap",               /* chip name */
277           #elif defined(CONFIG_OC_UL_DRV_U450_VARPINS_DIRNEG)
278              "16450-dirneg",                /* chip name */
279           #else
280              "16450",                       /* chip name */
281           #endif
282         #else /*CONFIG_OC_UL_DRV_U450_VARPINS*/
283              "16450",                       /* chip name */
284         #endif /*CONFIG_OC_UL_DRV_U450_VARPINS*/
285              0);                            /* baud base - default */
286
287   if (udrv==NULL)
288     return -1;
289
290   ul_drv_add_iac(udrv,UL_CMD_RDM,UL_IAC_OP_SND,ul_iac_call_rdm,NULL,0,0,NULL,0);
291   ul_drv_add_iac(udrv,UL_CMD_ERM,UL_IAC_OP_CALLBACK,ul_iac_call_erm,NULL,0,UL_IAC_BFL_CB_OFFLT,NULL,0);
292   ul_drv_add_iac(udrv,UL_CMD_WRM,UL_IAC_OP_REC,ul_iac_call_wrm,(char*)lpciap_buff,0,UL_IAC_BFL_CB_OFFLT,NULL,0);
293   ul_drv_add_iac(udrv,UL_CMD_DEB,UL_IAC_OP_CALLBACK,ul_iac_call_deb,NULL,0,UL_IAC_BFL_CB_OFFLT,NULL,0); 
294   ul_drv_add_iac(udrv,UL_CMD_RES,UL_IAC_OP_CALLBACK,ul_iac_call_res,NULL,0,UL_IAC_BFL_CB_OFFLT,NULL,0); 
295
296   return ul_drv_add_dev(udrv);
297 }
298 #endif /* CONFIG_OC_UL_DRV_SYSLESS */
299
300 void _setup_board()
301 {
302   // initialize the system
303   sysInit();
304
305   #ifdef WATCHDOG_ENABLED
306     lpc_watchdog_init(1,WATCHDOG_TIMEOUT_MS);
307     lpc_watchdog_feed();
308   #endif /* WATCHDOG_ENABLED */
309
310   #ifdef CONFIG_STDIO_COM_PORT
311     uart0Init( B57600 , UART_8N1, UART_FIFO_8);
312     init_system_stub();
313   #endif /* CONFIG_STDIO_COM_PORT */
314
315   // initialize the system timer
316   timerInit();
317
318   #ifdef CONFIG_OC_UL_DRV_SYSLESS
319 //    uld_debug_flg=0x3ff;
320     uLanInit();
321   #endif /* CONFIG_OC_UL_DRV_SYSLESS */
322
323 }