elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Informática
| |-+  Electrónica
| | |-+  Control USB con PICF4x bajo GNU/Linux
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Control USB con PICF4x bajo GNU/Linux  (Leído 3,369 veces)
dac

Desconectado Desconectado

Mensajes: 94



Ver Perfil WWW
Control USB con PICF4x bajo GNU/Linux
« en: 9 Diciembre 2012, 16:52 pm »

Saludos comunidad!

Me gustaría que me echaran una mano con esto:

Quiero hacer un "helloled", es decir encender un led con un comando desde el host GNU/Linux por USB, pero todos los firmwares que he encontrado tienen algo que me lo imposibilita.

PUF: No me funciona en lo absoluto (ni siquiera el bootloader) (No quiero un bootloader por ahora)

Enzman FW: con el FirmWare precompilado el dispositivo se enumera correctamente, pero todo el código esta echo y se puede compilar con SDCC en windows (no logro compilarlo con SDCC en Linux)

Maccioni FW: Tanto el FW como la aplicación del host estan echos para Linux, pero el FW esta echo con MPLAB y compila con MC18 pero una versión "antigua" y no lo logro compilar tampoco (y el FW precompilado no logra enumerar el dispositivo)

Y de otro FW que he encontrado (para SDCC):

Código
  1. /*
  2.  * Firmware framework for USB I/O on PIC 18F2455 (and siblings)
  3.  * Copyright (C) 2005 Alexander Enzmann
  4.  * adapted to MCC18 by Alberto Maccioni on 1/8/09
  5.  * re-adapted to SDCC 2.9.0 by Enrique Olivares (EOL) on 22/06/10
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  20.  * or see <http://www.gnu.org/licenses/>
  21.  */
  22.  
  23.  
  24. #include <pic18f4550.h>
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "usb.h"
  29. // Note: there are timing related problems associated with GET_FEATURE
  30. // when run at less than 48 MHz
  31. #define CLK_48MHZ 1
  32.  
  33. // Define configuration registers (fuses)
  34.  
  35. #if defined(pic18f2550) || defined(pic18f2455) || defined(pic18f4550) || defined(pic18f4455)
  36.  
  37. #if CLK_48MHZ
  38.   code char at 0x300000 CONFIG1L = 0x24; // USBDIV=2(0x20), CPUDIV=no dividido(0x00),PLLDIV=5(0x04) 20MHz
  39. #else
  40.   code char at 0x300000 CONFIG1L = 0x3c; // USB, /6 post (16MHz), /5 pre (20 MHz)
  41. #endif
  42.  
  43.   code char at 0x300001 CONFIG1H = 0x0e; // (FOSC xtal alta velocidad,PLL enable) (0x01),FCMEN disable(0x00), IESO disable (0x00)
  44.   code char at 0x300002 CONFIG2L = 0x3d; // PWRTE disa(0x01),BODEN ena at start, disa on sleep (0x04),BORV 2.0(0x18),VREGEN ena (0x20)
  45.   code char at 0x300003 CONFIG2H = 0x1e; // WDT disable(0x00), WDTPS post scaler 1:32768(0x1e)
  46.   code char at 0x300004 CONFIG3L = 0xff; // Unused configuration bits
  47.   code char at 0x300005 CONFIG3H = 0x00; // CCP2MX RB3(0x00), PBADEN digital(0x00), LPT1OSC disa(0x00), MCLRE disa (a Vdd)(0x00)
  48.   code char at 0x300006 CONFIG4L = 0x81; // STVREN ena pila llena(0x01), LVP disa(0x00),XINST disa(0x00), DEBUG disa in-circuit(0x80)
  49.   code char at 0x300007 CONFIG4H = 0xff; // Unused configuration bits
  50.   code char at 0x300008 CONFIG5L = 0x0f; // Proteccion de codigo: disable CP_0(0x0), CP_1(0x02), CP_2(0x04), CP_3(0x08)
  51.   code char at 0x300009 CONFIG5H = 0xc0; // CPB disable(0x40)
  52.   code char at 0x30000A CONFIG6L = 0x0f; // WRT disable: WRT_0(0x01), WRT_1(0x02), WRT_2(0x04), WRT_3(0x08)
  53.   code char at 0x30000B CONFIG6H = 0xe0; // WRTC disable: WRTC disa (0x20), WRTB disa (0x40)
  54.   code char at 0x30000C CONFIG7L = 0x0f; // EBTR disable: EBTR_0 disa(0x01), EBTR_1 disa (0x02), EBTR_2 disa (0x04), EBTR_3 disa (0x08)
  55.   code char at 0x30000D CONFIG7H = 0x40; // EBTRB disable (0x40)
  56.  
  57. #endif
  58.  
  59.  
  60. // HID feature buffer
  61. //~ volatile unsigned char HIDFeatureBuffer[HID_FEATURE_REPORT_BYTES];
  62.  
  63. /******************************************************************************/
  64. void high_isr(void) shadowregs interrupt 1
  65. {
  66. if(PIR2bits.USBIF)
  67.    {
  68.        ProcessUSBTransactions();
  69.        UIRbits.SOFIF = 0;
  70.        UIRbits.URSTIF = 0;
  71.        PIR2bits.USBIF = 0;
  72.        UEIR = 0;
  73.    }
  74. }
  75.  
  76. /******************************************************************************/
  77. void low_isr(void) shadowregs interrupt 2
  78. {
  79. ;
  80. ;
  81. }
  82.  
  83.  
  84. // Allocate buffers in RAM for storage of bytes that have either just
  85. // come in from the SIE or are waiting to go out to the SIE.
  86. volatile char txBuffer[HID_INPUT_REPORT_BYTES];
  87. volatile char rxBuffer[64];
  88. unsigned int timestamp=0;
  89. extern byte transferType;
  90.  
  91. //Probando ADC.
  92.  
  93. //Espacio temporal para el resultado de la conversion.
  94. unsigned char adch = 0x00;
  95. unsigned char adcl = 0x00;
  96.  
  97. //Cachureos para probar.
  98. unsigned char       adchaux = 0x00;
  99. unsigned char       adclaux = 0x00;
  100. unsigned char     esperaaux = 0x00;
  101. unsigned char cuentasumaaux = 0x00;
  102.  
  103. //Campos de bits para banderas y semaforos.
  104. typedef struct {
  105. unsigned char flag01: 1;
  106. unsigned char flag02: 1;
  107. unsigned char flag03: 1;
  108. } banderas;
  109.  
  110. //Un valor de 16 bit para el ADC.
  111. unsigned int valor = 0;
  112.  
  113. //El resultado de la sumatoria de conversiones.
  114. unsigned int sumavalor = 0;
  115.  
  116. //El promedio de las sumatorias.
  117. unsigned int promedio = 0;
  118.  
  119. //El contador de las sumas de conversiones.
  120. unsigned char cuentasumavalor = 0;
  121.  
  122. //Probando el ADC. Aqui el estado ADC  GO/DONE. y otras cosas.
  123. banderas estado_adc;
  124.  
  125. //Un contador para construir un tiempo de espera
  126. unsigned char espera = 0;
  127.  
  128. /******************************************************************************/
  129. // Entry point for user initialization.
  130.  
  131. void UserInit(void)
  132. {
  133. LATA=0;
  134. TRISA=0b11111111;  //Todas son entradas ahora, cuando pruebo el ADC.
  135. LATB=0;
  136. TRISB=0;
  137. T0CON=0x80; // Timer0 period = T*2^16:2 = 5.46ms:2 = 10.93ms (PS=2)
  138. LATC=0;
  139. TRISC=0b00000000;
  140. ADCON1 = 0b00001110; //Vref+=vss. Vref-=vdd. AN0 es entrada analoga, el resto es I/O digital.
  141. TRISAbits.TRISA0=1;
  142. ADCON0 = 0b00000000; //canal 0 (AN0). ADC en espera.  ADC deshabilitado.
  143. ADCON2 = 0b10000101; //justificado a la derecha. 0 Tad (inicio manual). Fosc/16
  144. ADCON0bits.ADON = 1;  //modulo ADC habilitado.
  145. ADCON0bits.GO   = 1;  //Comienza conversion AD.
  146. //Inicializa flags.
  147. estado_adc.flag01 = 0;
  148. estado_adc.flag02 = 0;
  149. estado_adc.flag03 = 0;
  150. //Inicializa cosas para el ADC.
  151. sumavalor = 0;
  152. espera = 0;
  153. valor = 0;
  154. cuentasumavalor = 0;
  155. promedio = 0;
  156. }
  157. #define LOBYTE(x) (*((char *)&x))
  158. #define HIBYTE(x) (*(((char *)&x)+1))
  159. /******************************************************************************/
  160. // If we got some bytes from the host, then echo them back.
  161. static void USBEcho(void)
  162. {
  163. byte rxCnt,i=0;
  164. if ((deviceState < CONFIGURED) || (UCONbits.SUSPND==1))
  165. return;
  166.  
  167.     // Find out if an Output report has been received from the host.
  168. rxCnt=CDCgets(rxBuffer);
  169. // If no bytes in, then nothing to do
  170. if (rxCnt == 0)
  171. return;
  172. // Copy input bytes to the output buffer
  173. // for (i=0;i<rxCnt;i++)
  174. // txBuffer[i] = rxBuffer[i];
  175. for(i=0;i<=32;i++)
  176. {
  177. while ( ADCON0bits.GO == 1 ){;}
  178. txBuffer[2*i]=ADRESH;
  179. if(i<32)
  180. txBuffer[2*i+1]=ADRESL;
  181. ADCON0bits.GO=1;
  182. }
  183. // As long as the SIE is owned by the processor, we let USB tasks continue.
  184. while (EP_IN_BD(CDC_DATA_EP_NUM).Stat.UOWN)
  185. // ProcessUSBTransactions();
  186. ;
  187. if(strncmp(rxBuffer,"hola\r\n",6)==0)
  188. CDCputs(txBuffer,62);
  189. else
  190. CDCputs("nada",4);
  191. }
  192.  
  193. /******************************************************************************/
  194. void ProcessIO(void)
  195. {
  196. // User Application USB tasks
  197. if ((deviceState < CONFIGURED) || (UCONbits.SUSPND==1))
  198. return;
  199. // Process USB: Echo back any bytes that have come in.
  200. USBEcho();
  201. // Probando el ADC.
  202.    if ( ADCON0bits.GO == 0 )
  203. {
  204. if ( estado_adc.flag01 == 0 )
  205. {
  206. adch = ADRESH;
  207. adcl = ADRESL;
  208. estado_adc.flag01 = 1;
  209. //Dos bytes de 8 en un word de 16.
  210.            valor = (int)ADRESL | ( (int)ADRESH << 8 );
  211. sumavalor = sumavalor + valor;
  212.            cuentasumavalor++;
  213.            if ( cuentasumavalor >= 20 )
  214. {
  215.                //Rescata el numero de sumatorias.
  216. cuentasumaaux = cuentasumavalor;
  217. //Calcula el promedio.
  218. promedio = sumavalor / 20;
  219. //Desarma el promedio de 16 bit en dos partes de 8 bits.
  220. adchaux = HIBYTE(promedio);
  221. adclaux = LOBYTE(promedio);
  222. //Reinicia las areas de trabajo.
  223. sumavalor = 0;
  224. cuentasumavalor = 0;
  225. promedio = 0;
  226. }
  227. }
  228. espera++;
  229. if ( espera >= 100 )
  230. {
  231. //Rescata tiempo de espera.
  232. esperaaux = espera;
  233. //Reinicia ADC.
  234. ADCON0bits.GO=1;
  235. //Reinicia flag de entrada.
  236. estado_adc.flag01 = 0;
  237. espera = 0;
  238. }
  239. }
  240. }
  241. /******************************************************************************/
  242. // Handle control out.  This might be an alternate way of processing
  243. // an output report, so all that's needed is to point the output
  244. // pointer to the output buffer
  245. // Initialization for a SET_REPORT request.  This routine will be
  246. // invoked during the setup stage and is used to set up the buffer
  247. // for receiving data from the host.
  248. // void SetupOutputReport(byte reportID)
  249. // {
  250. // if (reportID == 0)
  251. // {
  252. // // When the report arrives in the data stage, the data will be  
  253. // // stored in HIDFeatureBuffer
  254. // // inPtr = (byte*)&HIDRxBuffer;
  255. // }
  256. // }
  257. /******************************************************************************/
  258. // Post processing for a SET_REPORT request.  After all the data has
  259. // been delivered from host to device, this will be invoked to perform
  260. // application specific processing.
  261. // void SetOutputReport(byte reportID)
  262. // {
  263. //
  264. // #if DEBUG_PRINT
  265. // printf("SetOutputReport(0x%hx)\r\n", reportID);
  266. // #endif
  267. //
  268. // // Currently only handling report 0, ignore any others.
  269. // if (reportID != 0)
  270. // return;
  271. //
  272. // // LATB=HIDRxBuffer[0];
  273. //
  274. // // x11 to signal SET_REPORT transfer
  275. // LATBbits.LATB1=1;
  276. // LATBbits.LATB0=1;
  277. //
  278. //
  279. // // TBD: do something.  Not currently implemented because the output
  280. // // report is being handled by an interrupt endpoint.
  281. // }
  282.  
  283. /******************************************************************************/
  284. // Handle a control input report.
  285.  
  286. // void GetInputReport(byte reportID)
  287. // {
  288. //
  289. // #if DEBUG_PRINT
  290. // printf("GetInputReport: 0x%uhx\r\n", reportID);
  291. // #endif
  292. //
  293. // if (reportID == 0)
  294. // {
  295. // byte i;
  296. // // Send back the contents of the HID report
  297. // // TBD: provide useful information...
  298. // // outPtr = (byte *)&HIDTxBuffer;
  299. //
  300. // // // HIDTxBuffer[0]=PORTA;
  301. // // // HIDTxBuffer[1]=0xF2; //this is to differentiate between different transfer types
  302. // // // HIDTxBuffer[2]=HIBYTE(timestamp);
  303. // // // HIDTxBuffer[3]=LOBYTE(timestamp);
  304. // for(i=4;i<HID_INPUT_REPORT_BYTES;i++)
  305. // //                     HIDTxBuffer[i]=HIDRxBuffer[i];
  306. //
  307. // // The number of bytes in the report (from usb.h).
  308. // wCount = HID_INPUT_REPORT_BYTES;
  309. // transferType=0;
  310. // }
  311. // }
  312. void init_CDC()
  313. {
  314. INTCON=0;
  315. INTCON2=0xC0;
  316. UCON=0;
  317. UCFG=0;
  318. UEP0=0;UEP1=0;UEP2=0;UEP3=0;UEP4=0;UEP5=0;
  319. UEP6=0;UEP7=0;UEP8=0;UEP9=0;UEP10=0;UEP11=0;
  320. UEP12=0;UEP13=0;UEP14=0;UEP15=0;
  321. // Initialize USB for CDC
  322. UCFG = 0x14; // Enable pullup resistors; full speed mode
  323. deviceState = DETACHED;
  324. remoteWakeup = 0x00;
  325. currentConfiguration = 0x00;
  326. // And enable USB module
  327. while(deviceState != CONFIGURED)
  328. {
  329. EnableUSBModule();
  330. if(UCFGbits.UTEYE != 1)
  331. ProcessUSBTransactions();
  332. }
  333. }
  334. /******************************************************************************/
  335. // Entry point of the firmware.
  336.  
  337. void main(void)
  338. {
  339. byte led_cnt=0;
  340.  
  341.    // Set all I/O pins to digital
  342. ADCON1 |= 0x0F;
  343. PIE1=0;
  344. PIE2=0;
  345.    // Initialize USB
  346. //     UCFG = 0x14; // Enable pullup resistors; full speed mode
  347. //
  348. //     deviceState = DETACHED;
  349. //     remoteWakeup = 0x00;
  350. //     currentConfiguration = 0x00;
  351. init_CDC();
  352. PIE2bits.USBIE = 1;
  353. INTCON = 0xC0;  
  354.    // Call user initialization function
  355. UserInit();
  356.  
  357. #define LED1              LATCbits.LATC0
  358. //#define LED2              LATAbits.LATA2
  359.  
  360.    while(1)
  361.        {
  362.                // Ensure USB module is available
  363. // EnableUSBModule(); //esto es de usb.c, lo deja en el estado powered
  364. //
  365. // // As long as we aren't in test mode (UTEYE), process
  366. // // USB transactions.
  367. // if(UCFGbits.UTEYE != 1)//no se debe conectar el usb si UTEYE esta activo por que este genera un testeo
  368. // ProcessUSBTransactions();//esta en usb.c tmb
  369.  
  370.                // Application specific tasks
  371. //                 ProcessIO();
  372. USBEcho();
  373.  
  374.                //Blink LED2 @ 1Hz when configured
  375. if(INTCONbits.TMR0IF){
  376. INTCONbits.TMR0IF=0;
  377. timestamp++;
  378. led_cnt++;
  379. if (deviceState<CONFIGURED&&led_cnt>=10){
  380. //LED2 = !LED2;
  381. LED1 = !LED1;
  382. led_cnt=0;
  383. }
  384. else if (deviceState == CONFIGURED&&led_cnt>=46){
  385. //LED2 = !LED2;
  386. LED1 = !LED1;
  387. led_cnt=0;
  388. }
  389. }
  390.        }
  391. }
  392.  

Simplemente no compila, arroja:

Citar

sdcc --use-non-free -mpic16 -p18f4550 -V --debug -I/home/alx/silly-bytes/usb/para piclinux/ -c PICUSBFW.c

+ "/usr/bin/sdcpp" -nostdinc -Wall -I/home/alx/silly-bytes/usb/para\ piclinux/ -Dpic18f4550 -D__18f4550 -D__SDCC_PIC18F4550 -DSTACK_MODEL_SMALL -D__STACK_MODEL_SMALL -obj-ext=.o -D__SDCC_USE_NON_FREE -DSDCC_USE_NON_FREE -D__SDCC=3_2_0 -DSDCC=320 -D__SDCC_REVISION=8008 -DSDCC_REVISION=8008 -D__SDCC_pic16 -DSDCC_pic16 -D__pic16 -D__STDC_NO_COMPLEX__ -D__STDC_NO_THREADS__ -D__STDC_NO_ATOMICS__ -D__STDC_NO_VLA__ -isystem "/usr/bin/../share/sdcc/include/pic16" -isystem "/usr/share/sdcc/include/pic16" -isystem "/usr/bin/../share/sdcc/include" -isystem "/usr/share/sdcc/include" -isystem "/usr/bin/../share/sdcc/non-free/include/pic16" -isystem "/usr/share/sdcc/non-free/include/pic16" -isystem "/usr/bin/../share/sdcc/non-free/include" -isystem "/usr/share/sdcc/non-free/include" "PICUSBFW.c"  

PICUSBFW.c:38: error: token -> 'char' ; column 12
*** Salida con estado: 1 ***


Alguien sabe de un proyecto libre para hacer esto? o alguien lo ha echo antes, como puedo hacerlo?

Muchas gracias!, Saludos.


« Última modificación: 10 Diciembre 2012, 04:14 am por simorg » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines