50
50
#include "nrf_drv_twi.h"
51
51
#include "nrf_drv_gpiote.h"
52
52
#include "nrf_drv_ppi.h"
53
+ #include "nrf_drv_spi.h"
53
54
54
55
#include "app_uart.h"
55
56
65
66
TIMER0 (32 bit) not usable (softdevice)
66
67
TIMER1 (16 bit on nRF51, 32 bit on nRF52) used by jshardware util timer
67
68
TIMER2 (16 bit) free
68
- SPI0/1 free
69
+ SPI0 / TWI0 -> Espruino's SPI1 (only nRF52 - not enough flash on 51)
70
+ SPI1 / TWI1 -> Espruino's I2C1
71
+ SPI2 -> free
69
72
70
73
*/
71
74
@@ -81,6 +84,18 @@ unsigned int ticksSinceStart = 0;
81
84
82
85
JshPinFunction pinStates [JSH_PIN_COUNT ];
83
86
87
+ #if SPI_ENABLED
88
+ static const NRF_SPI_Type * spi0 = NRF_DRV_SPI_PERIPHERAL (0 );
89
+ bool spi0Initialised = false;
90
+ #endif
91
+
92
+ static const nrf_drv_twi_t TWI1 = NRF_DRV_TWI_INSTANCE (1 );
93
+ bool twi1Initialised = false;
94
+
95
+ const nrf_drv_twi_t * jshGetTWI (IOEventFlags device ) {
96
+ if (device == EV_I2C1 ) return & TWI1 ;
97
+ return 0 ;
98
+ }
84
99
85
100
86
101
/// Called when we have had an event that means we should execute JS
@@ -145,7 +160,17 @@ static NO_INLINE void jshPinSetFunction_int(JshPinFunction func, uint32_t pin) {
145
160
}
146
161
#endif
147
162
case JSH_USART1 : if (fInfo == JSH_USART_RX ) NRF_UART0 -> PSELRXD = pin ;
148
- else NRF_UART0 -> PSELTXD = pin ; break ;
163
+ else NRF_UART0 -> PSELTXD = pin ;
164
+ break ;
165
+ #if SPI_ENABLED
166
+ case JSH_SPI1 : if (fInfo == JSH_SPI_MISO ) NRF_SPI0 -> PSELMISO = pin ;
167
+ else if (fInfo == JSH_SPI_MOSI ) NRF_SPI0 -> PSELMOSI = pin ;
168
+ else NRF_SPI0 -> PSELSCK = pin ;
169
+ break ;
170
+ #endif
171
+ case JSH_I2C1 : if (fInfo == JSH_I2C_SDA ) NRF_TWI1 -> PSELSDA = pin ;
172
+ else NRF_TWI1 -> PSELSCL = pin ;
173
+ break ;
149
174
default : assert (0 );
150
175
}
151
176
}
@@ -779,44 +804,89 @@ void jshUSARTKick(IOEventFlags device) {
779
804
}
780
805
}
781
806
807
+
782
808
/** Set up SPI, if pins are -1 they will be guessed */
783
809
void jshSPISetup (IOEventFlags device , JshSPIInfo * inf ) {
810
+ #if SPI_ENABLED
811
+ if (device != EV_SPI1 ) return ;
812
+
813
+ nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG ;
814
+
815
+ nrf_spi_frequency_t freq ;
816
+ if (inf -> baudRate < ((125000 + 250000 )/2 ))
817
+ freq = SPI_FREQUENCY_FREQUENCY_K125 ;
818
+ else if (inf -> baudRate < ((250000 + 500000 )/2 ))
819
+ freq = SPI_FREQUENCY_FREQUENCY_K250 ;
820
+ else if (inf -> baudRate < ((500000 + 1000000 )/2 ))
821
+ freq = SPI_FREQUENCY_FREQUENCY_K500 ;
822
+ else if (inf -> baudRate < ((1000000 + 2000000 )/2 ))
823
+ freq = SPI_FREQUENCY_FREQUENCY_M1 ;
824
+ else if (inf -> baudRate < ((2000000 + 4000000 )/2 ))
825
+ freq = SPI_FREQUENCY_FREQUENCY_M2 ;
826
+ else if (inf -> baudRate < ((4000000 + 8000000 )/2 ))
827
+ freq = SPI_FREQUENCY_FREQUENCY_M4 ;
828
+ else
829
+ freq = SPI_FREQUENCY_FREQUENCY_M8 ;
830
+ spi_config .frequency = freq ;
831
+ spi_config .mode = inf -> spiMode ;
832
+ spi_config .bit_order = inf -> spiMSB ? NRF_DRV_SPI_BIT_ORDER_MSB_FIRST : NRF_DRV_SPI_BIT_ORDER_LSB_FIRST ;
833
+
834
+ if (jshIsPinValid (inf -> pinMISO ))
835
+ spi_config .miso_pin = (uint32_t )pinInfo [inf -> pinMISO ].pin ;
836
+ if (jshIsPinValid (inf -> pinMOSI ))
837
+ spi_config .mosi_pin = (uint32_t )pinInfo [inf -> pinMOSI ].pin ;
838
+ if (spi0Initialised ) nrf_drv_twi_uninit (& spi0 );
839
+ spi0Initialised = true;
840
+ // No event handler means SPI transfers are blocking
841
+ uint32_t err_code = nrf_drv_spi_init (& spi0 , & spi_config , NULL );
842
+ if (err_code != NRF_SUCCESS )
843
+ jsExceptionHere (JSET_INTERNALERROR , "SPI Initialisation Error %d\n" , err_code );
784
844
845
+ // nrf_drv_spi_init will set pins, but this ensures we know so can reset state later
846
+ if (jshIsPinValid (inf -> pinSCK )) {
847
+ jshPinSetFunction (inf -> pinSCK , JSH_SPI1 |JSH_SPI_SCK );
848
+ }
849
+ if (jshIsPinValid (inf -> pinMOSI )) {
850
+ jshPinSetFunction (inf -> pinMOSI , JSH_SPI1 |JSH_SPI_MOSI );
851
+ }
852
+ if (jshIsPinValid (inf -> pinMISO )) {
853
+ jshPinSetFunction (inf -> pinMISO , JSH_SPI1 |JSH_SPI_MISO );
854
+ }
855
+ #endif
785
856
}
786
857
787
858
/** Send data through the given SPI device (if data>=0), and return the result
788
859
* of the previous send (or -1). If data<0, no data is sent and the function
789
860
* waits for data to be returned */
790
861
int jshSPISend (IOEventFlags device , int data ) {
791
- return -1 ;
862
+ #if SPI_ENABLED
863
+ if (device != EV_SPI1 ) return -1 ;
864
+ uint8_t tx = (uint8_t )data ;
865
+ uint8_t rx = 0 ;
866
+ nrf_drv_spi_transfer (& spi0 , & tx , 1 , & rx , 1 );
867
+ return rx ;
868
+ #endif
792
869
}
793
870
794
871
/** Send 16 bit data through the given SPI device. */
795
872
void jshSPISend16 (IOEventFlags device , int data ) {
796
-
873
+ #if SPI_ENABLED
874
+ if (device != EV_SPI1 ) return ;
875
+ uint16_t tx = (uint16_t )data ;
876
+ nrf_drv_spi_transfer (& spi0 , (uint8_t * )& tx , 1 , 0 , 0 );
877
+ #endif
797
878
}
798
879
799
880
/** Set whether to send 16 bits or 8 over SPI */
800
881
void jshSPISet16 (IOEventFlags device , bool is16 ) {
801
-
802
882
}
803
883
804
884
/** Set whether to use the receive interrupt or not */
805
885
void jshSPISetReceive (IOEventFlags device , bool isReceive ) {
806
-
807
886
}
808
887
809
888
/** Wait until SPI send is finished, and flush all received data */
810
889
void jshSPIWait (IOEventFlags device ) {
811
-
812
- }
813
-
814
- const nrf_drv_twi_t TWI1 = NRF_DRV_TWI_INSTANCE (1 );
815
- bool twi1Initialised = false;
816
-
817
- const nrf_drv_twi_t * jshGetTWI (IOEventFlags device ) {
818
- if (device == EV_I2C1 ) return & TWI1 ;
819
- return 0 ;
820
890
}
821
891
822
892
/** Set up I2C, if pins are -1 they will be guessed */
@@ -840,6 +910,14 @@ void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) {
840
910
jsExceptionHere (JSET_INTERNALERROR , "I2C Initialisation Error %d\n" , err_code );
841
911
else
842
912
nrf_drv_twi_enable (twi );
913
+
914
+ // nrf_drv_spi_init will set pins, but this ensures we know so can reset state later
915
+ if (jshIsPinValid (inf -> pinSCL )) {
916
+ jshPinSetFunction (inf -> pinSCL , JSH_I2C1 |JSH_I2C_SCL );
917
+ }
918
+ if (jshIsPinValid (inf -> pinSDA )) {
919
+ jshPinSetFunction (inf -> pinSDA , JSH_I2C1 |JSH_I2C_SDA );
920
+ }
843
921
}
844
922
845
923
/** Addresses are 7 bit - that is, between 0 and 0x7F. sendStop is whether to send a stop bit or not */
0 commit comments